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