mount_msdosfs.c revision 33769
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.13 1998/02/23 09:41:02 ache 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 <locale.h>
48#include <pwd.h>
49#include <stdio.h>
50#include <stdlib.h>
51#include <string.h>
52#include <sysexits.h>
53#include <unistd.h>
54
55#include "mntopts.h"
56
57static struct mntopt mopts[] = {
58	MOPT_STDOPTS,
59	MOPT_FORCE,
60	MOPT_SYNC,
61	MOPT_UPDATE,
62	{ NULL }
63};
64
65static gid_t	a_gid __P((char *));
66static uid_t	a_uid __P((char *));
67static mode_t	a_mask __P((char *));
68static void	usage __P((void)) __dead2;
69static void     load_u2wtable __P((struct msdosfs_args *, char *));
70static void     load_ultable __P((struct msdosfs_args *, char *));
71
72int
73main(argc, argv)
74	int argc;
75	char **argv;
76{
77	struct msdosfs_args args;
78	struct stat sb;
79	int c, error, mntflags, set_gid, set_uid, set_mask;
80	char *dev, *dir, ndir[MAXPATHLEN+1];
81	struct vfsconf vfc;
82
83	mntflags = set_gid = set_uid = set_mask = 0;
84	(void)memset(&args, '\0', sizeof(args));
85	args.magic = MSDOSFS_ARGSMAGIC;
86
87	while ((c = getopt(argc, argv, "sl9u:g:m:o:L:W:")) != -1) {
88		switch (c) {
89#ifdef MSDOSFSMNT_GEMDOSFS
90		case 'G':
91			args.flags |= MSDOSFSMNT_GEMDOSFS;
92			break;
93#endif
94		case 's':
95			args.flags |= MSDOSFSMNT_SHORTNAME;
96			break;
97		case 'l':
98			args.flags |= MSDOSFSMNT_LONGNAME;
99			break;
100		case '9':
101			args.flags |= MSDOSFSMNT_NOWIN95;
102			break;
103		case 'u':
104			args.uid = a_uid(optarg);
105			set_uid = 1;
106			break;
107		case 'g':
108			args.gid = a_gid(optarg);
109			set_gid = 1;
110			break;
111		case 'm':
112			args.mask = a_mask(optarg);
113			set_mask = 1;
114			break;
115		case 'L':
116			load_ultable(&args, optarg);
117			args.flags |= MSDOSFSMNT_ULTABLE;
118			break;
119		case 'W':
120			load_u2wtable(&args, optarg);
121			args.flags |= MSDOSFSMNT_U2WTABLE;
122			break;
123		case 'o':
124			getmntopts(optarg, mopts, &mntflags, 0);
125			break;
126		case '?':
127		default:
128			usage();
129			break;
130		}
131	}
132
133	if (optind + 2 != argc)
134		usage();
135
136	dev = argv[optind];
137	dir = argv[optind + 1];
138	if (dir[0] != '/') {
139		warnx("\"%s\" is a relative path", dir);
140		if (getcwd(ndir, sizeof(ndir)) == NULL)
141			err(EX_OSERR, "getcwd");
142		strncat(ndir, "/", sizeof(ndir) - strlen(ndir) - 1);
143		strncat(ndir, dir, sizeof(ndir) - strlen(ndir) - 1);
144		dir = ndir;
145		warnx("using \"%s\" instead", dir);
146	}
147
148	args.fspec = dev;
149	args.export.ex_root = -2;	/* unchecked anyway on DOS fs */
150	if (mntflags & MNT_RDONLY)
151		args.export.ex_flags = MNT_EXRDONLY;
152	else
153		args.export.ex_flags = 0;
154	if (!set_gid || !set_uid || !set_mask) {
155		if (stat(dir, &sb) == -1)
156			err(EX_OSERR, "stat %s", dir);
157
158		if (!set_uid)
159			args.uid = sb.st_uid;
160		if (!set_gid)
161			args.gid = sb.st_gid;
162		if (!set_mask)
163			args.mask = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
164	}
165
166	error = getvfsbyname("msdos", &vfc);
167	if (error && vfsisloadable("msdos")) {
168		if (vfsload("msdos"))
169			err(EX_OSERR, "vfsload(msdos)");
170		endvfsent();	/* clear cache */
171		error = getvfsbyname("msdos", &vfc);
172	}
173	if (error)
174		errx(EX_OSERR, "msdos filesystem is not available");
175
176	if (mount(vfc.vfc_name, dir, mntflags, &args) < 0)
177		err(EX_OSERR, "%s", dev);
178
179	exit (0);
180}
181
182gid_t
183a_gid(s)
184	char *s;
185{
186	struct group *gr;
187	char *gname;
188	gid_t gid;
189
190	if ((gr = getgrnam(s)) != NULL)
191		gid = gr->gr_gid;
192	else {
193		for (gname = s; *s && isdigit(*s); ++s);
194		if (!*s)
195			gid = atoi(gname);
196		else
197			errx(EX_NOUSER, "unknown group id: %s", gname);
198	}
199	return (gid);
200}
201
202uid_t
203a_uid(s)
204	char *s;
205{
206	struct passwd *pw;
207	char *uname;
208	uid_t uid;
209
210	if ((pw = getpwnam(s)) != NULL)
211		uid = pw->pw_uid;
212	else {
213		for (uname = s; *s && isdigit(*s); ++s);
214		if (!*s)
215			uid = atoi(uname);
216		else
217			errx(EX_NOUSER, "unknown user id: %s", uname);
218	}
219	return (uid);
220}
221
222mode_t
223a_mask(s)
224	char *s;
225{
226	int done, rv;
227	char *ep;
228
229	done = 0;
230	rv = -1;
231	if (*s >= '0' && *s <= '7') {
232		done = 1;
233		rv = strtol(optarg, &ep, 8);
234	}
235	if (!done || rv < 0 || *ep)
236		errx(EX_USAGE, "invalid file mode: %s", s);
237	return (rv);
238}
239
240void
241usage()
242{
243	fprintf(stderr, "usage: mount_msdos [-o options] [-u user] [-g group] [-m mask] [-s] [-l] [-9] [-L locale] [-W table] bdev dir\n");
244	exit(EX_USAGE);
245}
246
247void
248load_u2wtable (pargs, name)
249	struct msdosfs_args *pargs;
250	char *name;
251{
252	FILE *f;
253	int i, code;
254	char buf[128];
255	char *fn;
256
257	if (*name == '/')
258		fn = name;
259	else {
260		snprintf(buf, sizeof(buf), "/usr/libdata/msdosfs/%s", name);
261		buf[127] = '\0';
262		fn = buf;
263	}
264	if ((f = fopen(fn, "r")) == NULL)
265		err(EX_NOINPUT, "%s", fn);
266	for (i = 0; i < 128; i++) {
267		if (fscanf(f, "%i", &code) != 1)
268			errx(EX_DATAERR, "u2w: missing item number %d", i);
269		pargs->u2w[i] = code;
270	}
271	for (i = 0; i < 128; i++) {
272		if (fscanf(f, "%i", &code) != 1)
273			errx(EX_DATAERR, "d2u: missing item number %d", i);
274		pargs->d2u[i] = code;
275	}
276	for (i = 0; i < 128; i++) {
277		if (fscanf(f, "%i", &code) != 1)
278			errx(EX_DATAERR, "u2d: missing item number %d", i);
279		pargs->u2d[i] = code;
280	}
281	fclose(f);
282}
283
284void
285load_ultable (pargs, name)
286	struct msdosfs_args *pargs;
287	char *name;
288{
289	int i;
290
291	if (setlocale(LC_CTYPE, name) == NULL)
292		err(EX_CONFIG, name);
293	for (i = 0; i < 128; i++) {
294		pargs->ul[i] = tolower(i | 0x80);
295		pargs->lu[i] = toupper(i | 0x80);
296	}
297}
298