mount_msdosfs.c revision 159128
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 159128 2006-06-01 02:25:00Z rodrigc $";
36#endif /* not lint */
37
38#include <sys/param.h>
39#include <sys/mount.h>
40#include <sys/stat.h>
41#include <sys/iconv.h>
42#include <sys/linker.h>
43#include <sys/module.h>
44
45#include <ctype.h>
46#include <err.h>
47#include <grp.h>
48#include <locale.h>
49#include <pwd.h>
50#include <stdio.h>
51/* must be after stdio to declare fparseln */
52#include <libutil.h>
53#include <stdlib.h>
54#include <string.h>
55#include <sysexits.h>
56#include <unistd.h>
57
58#include "mntopts.h"
59
60static gid_t	a_gid(char *);
61static uid_t	a_uid(char *);
62static mode_t	a_mask(char *);
63static void	usage(void) __dead2;
64static int	set_charset(struct iovec **iov, int *iovlen, const char *, const char *);
65
66int
67main(int argc, char **argv)
68{
69	struct iovec *iov = NULL;
70	int iovlen = 0;
71	struct stat sb;
72	int c, mntflags, set_gid, set_uid, set_mask, set_dirmask;
73	char *dev, *dir, mntpath[MAXPATHLEN], *csp;
74	char fstype[] = "msdosfs";
75	char *cs_dos = NULL;
76	char *cs_local = NULL;
77	mode_t mask = 0, dirmask = 0;
78	uid_t uid = 0;
79	gid_t gid = 0;
80	getmnt_silent = 1;
81
82	mntflags = set_gid = set_uid = set_mask = set_dirmask = 0;
83
84	while ((c = getopt(argc, argv, "sl9u:g:m:M:o:L:D:W:")) != -1) {
85		switch (c) {
86		case 's':
87			build_iovec(&iov, &iovlen, "shortnames", NULL, (size_t)-1);
88			break;
89		case 'l':
90			build_iovec(&iov, &iovlen, "longnames", NULL, (size_t)-1);
91			break;
92		case '9':
93			build_iovec_argf(&iov, &iovlen, "nowin95", "", (size_t)-1);
94			break;
95		case 'u':
96			uid = a_uid(optarg);
97			set_uid = 1;
98			break;
99		case 'g':
100			gid = a_gid(optarg);
101			set_gid = 1;
102			break;
103		case 'm':
104			mask = a_mask(optarg);
105			set_mask = 1;
106			break;
107		case 'M':
108			dirmask = a_mask(optarg);
109			set_dirmask = 1;
110			break;
111		case 'L': {
112			const char *quirk = NULL;
113			if (setlocale(LC_CTYPE, optarg) == NULL)
114				err(EX_CONFIG, "%s", optarg);
115			csp = strchr(optarg,'.');
116			if (!csp)
117				err(EX_CONFIG, "%s", optarg);
118			quirk = kiconv_quirkcs(csp + 1, KICONV_VENDOR_MICSFT);
119			build_iovec_argf(&iov, &iovlen, "cs_local", quirk);
120			cs_local = strdup(quirk);
121			}
122			break;
123		case 'D':
124			cs_dos = strdup(optarg);
125			build_iovec_argf(&iov, &iovlen, "cs_dos", cs_dos, (size_t)-1);
126			break;
127		case 'o': {
128			char *p = NULL;
129			char *val = strdup("");
130			p = strchr(optarg, '=');
131			if (p != NULL) {
132				free(val);
133				*p = '\0';
134				val = p + 1;
135			}
136			build_iovec(&iov, &iovlen, optarg, val, (size_t)-1);
137			}
138			break;
139		case 'W':
140			if (strcmp(optarg, "iso22dos") == 0) {
141				cs_local = strdup("ISO8859-2");
142				cs_dos = strdup("CP852");
143			} else if (strcmp(optarg, "iso72dos") == 0) {
144				cs_local = strdup("ISO8859-7");
145				cs_dos = strdup("CP737");
146			} else if (strcmp(optarg, "koi2dos") == 0) {
147				cs_local = strdup("KOI8-R");
148				cs_dos = strdup("CP866");
149			} else if (strcmp(optarg, "koi8u2dos") == 0) {
150				cs_local = strdup("KOI8-U");
151				cs_dos = strdup("CP866");
152			} else {
153				err(EX_NOINPUT, "%s", optarg);
154			}
155			build_iovec(&iov, &iovlen, "cs_local", cs_local, (size_t)-1);
156			build_iovec(&iov, &iovlen, "cs_dos", cs_dos, (size_t)-1);
157			break;
158		case '?':
159		default:
160			usage();
161			break;
162		}
163	}
164
165	if (optind + 2 != argc)
166		usage();
167
168	if (set_mask && !set_dirmask) {
169		dirmask = mask;
170		set_dirmask = 1;
171	}
172	else if (set_dirmask && !set_mask) {
173		mask = dirmask;
174		set_mask = 1;
175	}
176
177	dev = argv[optind];
178	dir = argv[optind + 1];
179
180	if (cs_local != NULL) {
181		if (set_charset(&iov, &iovlen, cs_local, cs_dos) == -1)
182			err(EX_OSERR, "msdosfs_iconv");
183		build_iovec_argf(&iov, &iovlen, "kiconv", "");
184	} else if (cs_dos != NULL) {
185		build_iovec_argf(&iov, &iovlen, "cs_local", "ISO8859-1");
186		if (set_charset(&iov, &iovlen, "ISO8859-1", cs_dos) == -1)
187			err(EX_OSERR, "msdosfs_iconv");
188		build_iovec_argf(&iov, &iovlen, "kiconv", "");
189	}
190
191	/*
192	 * Resolve the mountpoint with realpath(3) and remove unnecessary
193	 * slashes from the devicename if there are any.
194	 */
195	(void)checkpath(dir, mntpath);
196	(void)rmslashes(dev, dev);
197
198	if (!set_gid || !set_uid || !set_mask) {
199		if (stat(mntpath, &sb) == -1)
200			err(EX_OSERR, "stat %s", mntpath);
201
202		if (!set_uid)
203			uid = sb.st_uid;
204		if (!set_gid)
205			gid = sb.st_gid;
206		if (!set_mask)
207			mask = dirmask =
208				sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
209	}
210
211	build_iovec(&iov, &iovlen, "fstype", fstype, (size_t)-1);
212	build_iovec(&iov, &iovlen, "fspath", mntpath, (size_t)-1);
213	build_iovec(&iov, &iovlen, "from", dev, (size_t)-1);
214	build_iovec_argf(&iov, &iovlen, "uid", "%d", uid);
215	build_iovec_argf(&iov, &iovlen, "gid", "%u", gid);
216	build_iovec_argf(&iov, &iovlen, "mask", "%u", mask);
217	build_iovec_argf(&iov, &iovlen, "dirmask", "%u", dirmask);
218
219	if (nmount(iov, iovlen, mntflags) < 0)
220		err(1, "%s", dev);
221
222	exit (0);
223}
224
225gid_t
226a_gid(s)
227	char *s;
228{
229	struct group *gr;
230	char *gname;
231	gid_t gid;
232
233	if ((gr = getgrnam(s)) != NULL)
234		gid = gr->gr_gid;
235	else {
236		for (gname = s; *s && isdigit(*s); ++s);
237		if (!*s)
238			gid = atoi(gname);
239		else
240			errx(EX_NOUSER, "unknown group id: %s", gname);
241	}
242	return (gid);
243}
244
245uid_t
246a_uid(s)
247	char *s;
248{
249	struct passwd *pw;
250	char *uname;
251	uid_t uid;
252
253	if ((pw = getpwnam(s)) != NULL)
254		uid = pw->pw_uid;
255	else {
256		for (uname = s; *s && isdigit(*s); ++s);
257		if (!*s)
258			uid = atoi(uname);
259		else
260			errx(EX_NOUSER, "unknown user id: %s", uname);
261	}
262	return (uid);
263}
264
265mode_t
266a_mask(s)
267	char *s;
268{
269	int done, rv;
270	char *ep;
271
272	done = 0;
273	rv = -1;
274	if (*s >= '0' && *s <= '7') {
275		done = 1;
276		rv = strtol(optarg, &ep, 8);
277	}
278	if (!done || rv < 0 || *ep)
279		errx(EX_USAGE, "invalid file mode: %s", s);
280	return (rv);
281}
282
283void
284usage()
285{
286	fprintf(stderr, "%s\n%s\n%s\n",
287	"usage: mount_msdosfs [-9ls] [-D DOS_codepage] [-g gid] [-L locale]",
288	"                     [-M mask] [-m mask] [-o options] [-u uid]",
289	"		      [-W table] special node");
290	exit(EX_USAGE);
291}
292
293int
294set_charset(struct iovec **iov, int *iovlen, const char *cs_local, const char *cs_dos)
295{
296	int error;
297
298	if (modfind("msdosfs_iconv") < 0)
299		if (kldload("msdosfs_iconv") < 0 || modfind("msdosfs_iconv") < 0) {
300			warnx("cannot find or load \"msdosfs_iconv\" kernel module");
301			return (-1);
302		}
303
304	build_iovec_argf(iov, iovlen, "cs_win", ENCODING_UNICODE);
305	error = kiconv_add_xlat16_cspairs(ENCODING_UNICODE, cs_local);
306	if (error)
307		return (-1);
308	if (cs_dos != NULL) {
309		error = kiconv_add_xlat16_cspairs(cs_dos, cs_local);
310		if (error)
311			return (-1);
312	} else {
313		build_iovec_argf(iov, iovlen, "cs_dos", cs_local);
314		error = kiconv_add_xlat16_cspair(cs_local, cs_local,
315				KICONV_FROM_UPPER | KICONV_LOWER);
316		if (error)
317			return (-1);
318	}
319
320	return (0);
321}
322