1/* $NetBSD: mount_msdos.c,v 1.46 2008/08/05 20:57:45 pooka Exp $ */
2
3/*
4 * Copyright (c) 1994 Christopher G. Demetriou
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *          This product includes software developed for the
18 *          NetBSD Project.  See http://www.NetBSD.org/ for
19 *          information about NetBSD.
20 * 4. The name of the author may not be used to endorse or promote products
21 *    derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
35 */
36
37#include <sys/cdefs.h>
38#ifndef lint
39__RCSID("$NetBSD: mount_msdos.c,v 1.46 2008/08/05 20:57:45 pooka Exp $");
40#endif /* not lint */
41
42#include <sys/param.h>
43#include <sys/mount.h>
44#include <sys/stat.h>
45#include <msdosfs/msdosfsmount.h>
46#include <err.h>
47#include <grp.h>
48#include <pwd.h>
49#include <stdio.h>
50#include <stdlib.h>
51#include <string.h>
52#include <time.h>
53#include <unistd.h>
54#include <util.h>
55
56#include <mntopts.h>
57
58#include "mountprog.h"
59#include "mount_msdos.h"
60
61static const struct mntopt mopts[] = {
62	MOPT_STDOPTS,
63	MOPT_ASYNC,
64	MOPT_SYNC,
65	MOPT_UPDATE,
66	MOPT_GETARGS,
67	MOPT_NULL,
68};
69
70static void	usage(void) __dead;
71
72#ifndef MOUNT_NOMAIN
73int
74main(int argc, char **argv)
75{
76
77	setprogname(argv[0]);
78	return mount_msdos(argc, argv);
79}
80#endif
81
82void
83mount_msdos_parseargs(int argc, char **argv,
84	struct msdosfs_args *args, int *mntflags,
85	char *canon_dev, char *canon_dir)
86{
87	struct stat sb;
88	int c, set_gid, set_uid, set_mask, set_dirmask, set_gmtoff;
89	char *dev, *dir;
90	time_t now;
91	struct tm *tm;
92	mntoptparse_t mp;
93
94	*mntflags = set_gid = set_uid = set_mask = set_dirmask = set_gmtoff = 0;
95	(void)memset(args, '\0', sizeof(*args));
96
97	while ((c = getopt(argc, argv, "Gsl9u:g:m:M:o:t:")) != -1) {
98		switch (c) {
99		case 'G':
100			args->flags |= MSDOSFSMNT_GEMDOSFS;
101			break;
102		case 's':
103			args->flags |= MSDOSFSMNT_SHORTNAME;
104			break;
105		case 'l':
106			args->flags |= MSDOSFSMNT_LONGNAME;
107			break;
108		case '9':
109			args->flags |= MSDOSFSMNT_NOWIN95;
110			break;
111		case 'u':
112			args->uid = a_uid(optarg);
113			set_uid = 1;
114			break;
115		case 'g':
116			args->gid = a_gid(optarg);
117			set_gid = 1;
118			break;
119		case 'm':
120			args->mask = a_mask(optarg);
121			set_mask = 1;
122			break;
123		case 'M':
124			args->dirmask = a_mask(optarg);
125			set_dirmask = 1;
126			break;
127		case 'o':
128			mp = getmntopts(optarg, mopts, mntflags, 0);
129			if (mp == NULL)
130				err(1, "getmntopts");
131			freemntopts(mp);
132			break;
133		case 't':
134			args->gmtoff = atoi(optarg);
135			set_gmtoff = 1;
136			break;
137		case '?':
138		default:
139			usage();
140			break;
141		}
142	}
143
144	if (optind + 2 != argc)
145		usage();
146
147	if (set_mask && !set_dirmask) {
148		args->dirmask = args->mask;
149		set_dirmask = 1;
150	} else if (set_dirmask && !set_mask) {
151		args->mask = args->dirmask;
152		set_mask = 1;
153	}
154
155	dev = argv[optind];
156	dir = argv[optind + 1];
157
158	pathadj(dev, canon_dev);
159	pathadj(dir, canon_dir);
160
161	args->fspec = canon_dev;
162	if (!set_gid || !set_uid || !set_mask) {
163		if (stat(dir, &sb) == -1)
164			err(1, "stat %s", dir);
165
166		if (!set_uid)
167			args->uid = sb.st_uid;
168		if (!set_gid)
169			args->gid = sb.st_gid;
170		if (!set_mask) {
171			args->mask = args->dirmask =
172				sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
173		}
174	}
175
176	if (!set_gmtoff) {
177		/* use user's time zone as default */
178		time(&now);
179		tm = localtime(&now);
180		args->gmtoff = tm->tm_gmtoff;
181
182	}
183	args->flags |= MSDOSFSMNT_VERSIONED;
184	args->version = MSDOSFSMNT_VERSION;
185}
186
187int
188mount_msdos(int argc, char **argv)
189{
190	struct msdosfs_args args;
191	char canon_dev[MAXPATHLEN], canon_dir[MAXPATHLEN];
192	int mntflags;
193
194	mount_msdos_parseargs(argc, argv, &args, &mntflags,
195	    canon_dev, canon_dir);
196
197	if (mount(MOUNT_MSDOS, canon_dir, mntflags, &args, sizeof args) == -1)
198		err(1, "%s on %s", canon_dev, canon_dir);
199
200	if (mntflags & MNT_GETARGS) {
201		char buf[1024];
202		(void)snprintb(buf, sizeof(buf), MSDOSFSMNT_BITS, args.flags);
203		printf("uid=%d, gid=%d, mask=0%o, dirmask=0%o, flags=%s\n",
204		    args.uid, args.gid, args.mask, args.dirmask, buf);
205	}
206
207	exit (0);
208}
209
210static void
211usage(void)
212{
213
214	fprintf(stderr, "usage: %s [-9Gls] [-g gid] [-M mask] [-m mask] "
215	    "[-o options]\n\t[-t gmtoff] [-u uid] special mountpath\n",
216	    getprogname());
217	exit(1);
218}
219