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