mount_msdosfs.c revision 33549
1/*	$NetBSD: mount_msdos.c,v 1.18 1997/09/16 12:24:18 lukem 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 by Christopher G. Demetriou.
18 * 4. The name of the author may not be used to endorse or promote products
19 *    derived from this software without specific prior written permission
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#ifndef lint
34static const char rcsid[] =
35	"$Id: mount_msdos.c,v 1.10 1997/08/25 20:23:16 bde Exp $";
36#endif /* not lint */
37
38#include <sys/param.h>
39#include <sys/mount.h>
40#include <sys/stat.h>
41
42#include <msdosfs/msdosfsmount.h>
43
44#include <ctype.h>
45#include <err.h>
46#include <grp.h>
47#include <pwd.h>
48#include <stdio.h>
49#include <stdlib.h>
50#include <string.h>
51#include <sysexits.h>
52#include <unistd.h>
53
54#include "mntopts.h"
55
56static struct mntopt mopts[] = {
57	MOPT_STDOPTS,
58	MOPT_FORCE,
59	MOPT_SYNC,
60	MOPT_UPDATE,
61	{ NULL }
62};
63
64static gid_t	a_gid __P((char *));
65static uid_t	a_uid __P((char *));
66static mode_t	a_mask __P((char *));
67static void	usage __P((void)) __dead2;
68
69int
70main(argc, argv)
71	int argc;
72	char **argv;
73{
74	struct msdosfs_args args;
75	struct stat sb;
76	int c, error, mntflags, set_gid, set_uid, set_mask;
77	char *dev, *dir, ndir[MAXPATHLEN+1];
78	struct vfsconf vfc;
79
80	mntflags = set_gid = set_uid = set_mask = 0;
81	(void)memset(&args, '\0', sizeof(args));
82	args.magic = MSDOSFS_ARGSMAGIC;
83
84	while ((c = getopt(argc, argv, "sl9u:g:m:o:")) != -1) {
85		switch (c) {
86#ifdef MSDOSFSMNT_GEMDOSFS
87		case 'G':
88			args.flags |= MSDOSFSMNT_GEMDOSFS;
89			break;
90#endif
91		case 's':
92			args.flags |= MSDOSFSMNT_SHORTNAME;
93			break;
94		case 'l':
95			args.flags |= MSDOSFSMNT_LONGNAME;
96			break;
97		case '9':
98			args.flags |= MSDOSFSMNT_NOWIN95;
99			break;
100		case 'u':
101			args.uid = a_uid(optarg);
102			set_uid = 1;
103			break;
104		case 'g':
105			args.gid = a_gid(optarg);
106			set_gid = 1;
107			break;
108		case 'm':
109			args.mask = a_mask(optarg);
110			set_mask = 1;
111			break;
112		case 'o':
113			getmntopts(optarg, mopts, &mntflags, 0);
114			break;
115		case '?':
116		default:
117			usage();
118			break;
119		}
120	}
121
122	if (optind + 2 != argc)
123		usage();
124
125	dev = argv[optind];
126	dir = argv[optind + 1];
127	if (dir[0] != '/') {
128		warnx("\"%s\" is a relative path", dir);
129		if (getcwd(ndir, sizeof(ndir)) == NULL)
130			err(EX_OSERR, "getcwd");
131		strncat(ndir, "/", sizeof(ndir) - strlen(ndir) - 1);
132		strncat(ndir, dir, sizeof(ndir) - strlen(ndir) - 1);
133		dir = ndir;
134		warnx("using \"%s\" instead", dir);
135	}
136
137	args.fspec = dev;
138	args.export.ex_root = -2;	/* unchecked anyway on DOS fs */
139	if (mntflags & MNT_RDONLY)
140		args.export.ex_flags = MNT_EXRDONLY;
141	else
142		args.export.ex_flags = 0;
143	if (!set_gid || !set_uid || !set_mask) {
144		if (stat(dir, &sb) == -1)
145			err(EX_OSERR, "stat %s", dir);
146
147		if (!set_uid)
148			args.uid = sb.st_uid;
149		if (!set_gid)
150			args.gid = sb.st_gid;
151		if (!set_mask)
152			args.mask = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
153	}
154
155	error = getvfsbyname("msdos", &vfc);
156	if (error && vfsisloadable("msdos")) {
157		if (vfsload("msdos"))
158			err(EX_OSERR, "vfsload(msdos)");
159		endvfsent();	/* clear cache */
160		error = getvfsbyname("msdos", &vfc);
161	}
162	if (error)
163		errx(EX_OSERR, "msdos filesystem is not available");
164
165	if (mount(vfc.vfc_name, dir, mntflags, &args) < 0)
166		err(EX_OSERR, "%s", dev);
167
168	exit (0);
169}
170
171gid_t
172a_gid(s)
173	char *s;
174{
175	struct group *gr;
176	char *gname;
177	gid_t gid;
178
179	if ((gr = getgrnam(s)) != NULL)
180		gid = gr->gr_gid;
181	else {
182		for (gname = s; *s && isdigit(*s); ++s);
183		if (!*s)
184			gid = atoi(gname);
185		else
186			errx(EX_NOUSER, "unknown group id: %s", gname);
187	}
188	return (gid);
189}
190
191uid_t
192a_uid(s)
193	char *s;
194{
195	struct passwd *pw;
196	char *uname;
197	uid_t uid;
198
199	if ((pw = getpwnam(s)) != NULL)
200		uid = pw->pw_uid;
201	else {
202		for (uname = s; *s && isdigit(*s); ++s);
203		if (!*s)
204			uid = atoi(uname);
205		else
206			errx(EX_NOUSER, "unknown user id: %s", uname);
207	}
208	return (uid);
209}
210
211mode_t
212a_mask(s)
213	char *s;
214{
215	int done, rv;
216	char *ep;
217
218	done = 0;
219	rv = -1;
220	if (*s >= '0' && *s <= '7') {
221		done = 1;
222		rv = strtol(optarg, &ep, 8);
223	}
224	if (!done || rv < 0 || *ep)
225		errx(EX_USAGE, "invalid file mode: %s", s);
226	return (rv);
227}
228
229void
230usage()
231{
232	fprintf(stderr, "usage: mount_msdos [-o options] [-u user] [-g group] [-m mask] bdev dir\n");
233	exit(EX_USAGE);
234}
235