mount_msdosfs.c revision 121373
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  "$FreeBSD: head/sbin/mount_msdosfs/mount_msdosfs.c 121373 2003-10-22 20:58:57Z trhodes $";
36#endif /* not lint */
37
38#include <sys/param.h>
39#include <sys/mount.h>
40#include <sys/stat.h>
41#include <sys/module.h>
42#include <sys/iconv.h>
43#include <sys/linker.h>
44
45#include <fs/msdosfs/msdosfsmount.h>
46
47#include <ctype.h>
48#include <err.h>
49#include <grp.h>
50#include <locale.h>
51#include <pwd.h>
52#include <stdio.h>
53/* must be after stdio to declare fparseln */
54#include <libutil.h>
55#include <stdlib.h>
56#include <string.h>
57#include <sysexits.h>
58#include <unistd.h>
59#include "mntopts.h"
60
61#define TRANSITION_PERIOD_HACK
62
63/*
64 * XXX - no way to specify "foo=<bar>"-type options; that's what we'd
65 * want for "-u", "-g", "-m", "-M", "-L", "-D", and "-W".
66 */
67static struct mntopt mopts[] = {
68	MOPT_STDOPTS,
69	MOPT_FORCE,
70	MOPT_SYNC,
71	MOPT_UPDATE,
72#ifdef MSDOSFSMNT_GEMDOSFS
73	{ "gemdosfs", 0, MSDOSFSMNT_GEMDOSFS, 1 },
74#endif
75	{ "shortnames", 0, MSDOSFSMNT_SHORTNAME, 1 },
76	{ "longnames", 0, MSDOSFSMNT_LONGNAME, 1 },
77	{ "nowin95", 0, MSDOSFSMNT_NOWIN95, 1 },
78	{ NULL }
79};
80
81static gid_t	a_gid(char *);
82static uid_t	a_uid(char *);
83static mode_t	a_mask(char *);
84static void	usage(void) __dead2;
85static int	set_charset(struct msdosfs_args *);
86
87int
88main(int argc, char **argv)
89{
90	struct msdosfs_args args;
91	struct stat sb;
92	int c, mntflags, set_gid, set_uid, set_mask, set_dirmask;
93	char *dev, *dir, mntpath[MAXPATHLEN], *csp;
94
95	mntflags = set_gid = set_uid = set_mask = set_dirmask = 0;
96	(void)memset(&args, '\0', sizeof(args));
97	args.magic = MSDOSFS_ARGSMAGIC;
98
99	args.cs_win = NULL;
100	args.cs_dos = NULL;
101	args.cs_local = NULL;
102#ifdef TRANSITION_PERIOD_HACK
103	while ((c = getopt(argc, argv, "sl9u:g:m:M:o:L:D:W:")) != -1) {
104#else
105	while ((c = getopt(argc, argv, "sl9u:g:m:M:o:L:D:")) != -1) {
106#endif
107		switch (c) {
108#ifdef MSDOSFSMNT_GEMDOSFS
109		case 'G':
110			args.flags |= MSDOSFSMNT_GEMDOSFS;
111			break;
112#endif
113		case 's':
114			args.flags |= MSDOSFSMNT_SHORTNAME;
115			break;
116		case 'l':
117			args.flags |= MSDOSFSMNT_LONGNAME;
118			break;
119		case '9':
120			args.flags |= MSDOSFSMNT_NOWIN95;
121			break;
122		case 'u':
123			args.uid = a_uid(optarg);
124			set_uid = 1;
125			break;
126		case 'g':
127			args.gid = a_gid(optarg);
128			set_gid = 1;
129			break;
130		case 'm':
131			args.mask = a_mask(optarg);
132			set_mask = 1;
133			break;
134		case 'M':
135			args.dirmask = a_mask(optarg);
136			set_dirmask = 1;
137			break;
138		case 'L':
139			if (setlocale(LC_CTYPE, optarg) == NULL)
140				err(EX_CONFIG, "%s", optarg);
141			csp = strchr(optarg,'.');
142			if (!csp)
143				err(EX_CONFIG, "%s", optarg);
144			args.cs_local = malloc(ICONV_CSNMAXLEN);
145			if (args.cs_local == NULL)
146				err(EX_OSERR, "malloc()");
147			strncpy(args.cs_local,
148			    kiconv_quirkcs(csp + 1, KICONV_VENDOR_MICSFT),
149			    ICONV_CSNMAXLEN);
150			break;
151		case 'D':
152			args.cs_dos = malloc(ICONV_CSNMAXLEN);
153			if (args.cs_dos == NULL)
154				err(EX_OSERR, "malloc()");
155			strncpy(args.cs_dos, optarg, ICONV_CSNMAXLEN);
156			break;
157		case 'o':
158			getmntopts(optarg, mopts, &mntflags, &args.flags);
159			break;
160#ifdef TRANSITION_PERIOD_HACK
161		case 'W':
162			args.cs_local = malloc(ICONV_CSNMAXLEN);
163			if (args.cs_local == NULL)
164				err(EX_OSERR, "malloc()");
165			args.cs_dos = malloc(ICONV_CSNMAXLEN);
166			if (args.cs_dos == NULL)
167				err(EX_OSERR, "malloc()");
168			if (strcmp(optarg, "iso22dos") == 0) {
169				strcpy(args.cs_local, "ISO8859-2");
170				strcpy(args.cs_dos, "CP852");
171			} else if (strcmp(optarg, "iso72dos") == 0) {
172				strcpy(args.cs_local, "ISO8859-7");
173				strcpy(args.cs_dos, "CP737");
174			} else if (strcmp(optarg, "koi2dos") == 0) {
175				strcpy(args.cs_local, "KOI8-R");
176				strcpy(args.cs_dos, "CP866");
177			} else if (strcmp(optarg, "koi8u2dos") == 0) {
178				strcpy(args.cs_local, "KOI8-U");
179				strcpy(args.cs_dos, "CP866");
180			} else {
181				err(EX_NOINPUT, "%s", optarg);
182			}
183			break;
184#endif /* TRANSITION_PERIOD_HACK */
185		case '?':
186		default:
187			usage();
188			break;
189		}
190	}
191
192	if (optind + 2 != argc)
193		usage();
194
195	if (set_mask && !set_dirmask) {
196		args.dirmask = args.mask;
197		set_dirmask = 1;
198	}
199	else if (set_dirmask && !set_mask) {
200		args.mask = args.dirmask;
201		set_mask = 1;
202	}
203
204	dev = argv[optind];
205	dir = argv[optind + 1];
206
207	if (args.cs_local) {
208		if (set_charset(&args) == -1)
209			err(EX_OSERR, "msdosfs_iconv");
210		args.flags |= MSDOSFSMNT_KICONV;
211	} else if (args.cs_dos) {
212		if ((args.cs_local = malloc(ICONV_CSNMAXLEN)) == NULL)
213			err(EX_OSERR, "malloc()");
214		strcpy(args.cs_local, "ISO8859-1");
215		if (set_charset(&args) == -1)
216			err(EX_OSERR, "msdosfs_iconv");
217		args.flags |= MSDOSFSMNT_KICONV;
218	}
219
220	/*
221	 * Resolve the mountpoint with realpath(3) and remove unnecessary
222	 * slashes from the devicename if there are any.
223	 */
224	(void)checkpath(dir, mntpath);
225	(void)rmslashes(dev, dev);
226
227	args.fspec = dev;
228	args.export.ex_root = -2;	/* unchecked anyway on DOS fs */
229	if (mntflags & MNT_RDONLY)
230		args.export.ex_flags = MNT_EXRDONLY;
231	else
232		args.export.ex_flags = 0;
233	if (!set_gid || !set_uid || !set_mask) {
234		if (stat(mntpath, &sb) == -1)
235			err(EX_OSERR, "stat %s", mntpath);
236
237		if (!set_uid)
238			args.uid = sb.st_uid;
239		if (!set_gid)
240			args.gid = sb.st_gid;
241		if (!set_mask)
242			args.mask = args.dirmask =
243				sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
244	}
245
246	if (mount("msdosfs", mntpath, mntflags, &args) < 0)
247		err(EX_OSERR, "%s", dev);
248
249	exit (0);
250}
251
252gid_t
253a_gid(s)
254	char *s;
255{
256	struct group *gr;
257	char *gname;
258	gid_t gid;
259
260	if ((gr = getgrnam(s)) != NULL)
261		gid = gr->gr_gid;
262	else {
263		for (gname = s; *s && isdigit(*s); ++s);
264		if (!*s)
265			gid = atoi(gname);
266		else
267			errx(EX_NOUSER, "unknown group id: %s", gname);
268	}
269	return (gid);
270}
271
272uid_t
273a_uid(s)
274	char *s;
275{
276	struct passwd *pw;
277	char *uname;
278	uid_t uid;
279
280	if ((pw = getpwnam(s)) != NULL)
281		uid = pw->pw_uid;
282	else {
283		for (uname = s; *s && isdigit(*s); ++s);
284		if (!*s)
285			uid = atoi(uname);
286		else
287			errx(EX_NOUSER, "unknown user id: %s", uname);
288	}
289	return (uid);
290}
291
292mode_t
293a_mask(s)
294	char *s;
295{
296	int done, rv;
297	char *ep;
298
299	done = 0;
300	rv = -1;
301	if (*s >= '0' && *s <= '7') {
302		done = 1;
303		rv = strtol(optarg, &ep, 8);
304	}
305	if (!done || rv < 0 || *ep)
306		errx(EX_USAGE, "invalid file mode: %s", s);
307	return (rv);
308}
309
310void
311usage()
312{
313#ifdef TRANSITION_PERIOD_HACK
314	fprintf(stderr, "%s\n%s\n%s\n",
315	"usage: mount_msdosfs [-o options] [-u user] [-g group] [-m mask] [-M mask]",
316	"                     [-s] [-l] [-9] [-L locale] [-D dos-codepage] [-W table]",
317	"                     bdev dir");
318#else
319	fprintf(stderr, "%s\n%s\n",
320	"usage: mount_msdosfs [-o options] [-u user] [-g group] [-m mask] [-M mask]",
321	"                     [-s] [-l] [-9] [-L locale] [-D dos-codepage] bdev dir");
322#endif
323	exit(EX_USAGE);
324}
325
326int
327set_charset(struct msdosfs_args *args)
328{
329	int error;
330
331	if (modfind("msdosfs_iconv") < 0)
332		if (kldload("msdosfs_iconv") < 0 || modfind("msdosfs_iconv") < 0) {
333			warnx( "cannot find or load \"msdosfs_iconv\" kernel module");
334			return (-1);
335		}
336
337	if ((args->cs_win = malloc(ICONV_CSNMAXLEN)) == NULL)
338		return (-1);
339	strncpy(args->cs_win, ENCODING_UNICODE, ICONV_CSNMAXLEN);
340	error = kiconv_add_xlat16_cspair(args->cs_win, args->cs_local, 0);
341	if (error)
342		return (-1);
343	error = kiconv_add_xlat16_cspair(args->cs_local, args->cs_win, 0);
344	if (error)
345		return (-1);
346	if (args->cs_dos) {
347		error = kiconv_add_xlat16_cspair(args->cs_dos, args->cs_local, KICONV_FROM_UPPER);
348		if (error)
349			return (-1);
350		error = kiconv_add_xlat16_cspair(args->cs_local, args->cs_dos, KICONV_LOWER);
351		if (error)
352			return (-1);
353	} else {
354		if ((args->cs_dos = malloc(ICONV_CSNMAXLEN)) == NULL)
355			return (-1);
356		strcpy(args->cs_dos, args->cs_local);
357		error = kiconv_add_xlat16_cspair(args->cs_local, args->cs_local,
358				KICONV_FROM_UPPER | KICONV_LOWER);
359		if (error)
360			return (-1);
361	}
362
363	return (0);
364}
365