xref: /freebsd/sbin/mount_msdosfs/mount_msdosfs.c (revision f5463265955b829775bbb32e1fd0bc11dafc36ce)
1 /*	$NetBSD: mount_msdos.c,v 1.18 1997/09/16 12:24:18 lukem Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-4-Clause
5  *
6  * Copyright (c) 1994 Christopher G. Demetriou
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed by Christopher G. Demetriou.
20  * 4. The name of the author may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include <sys/param.h>
36 #include <sys/mount.h>
37 #include <sys/stat.h>
38 #include <sys/iconv.h>
39 #include <sys/linker.h>
40 #include <sys/module.h>
41 
42 #include <ctype.h>
43 #include <err.h>
44 #include <errno.h>
45 #include <grp.h>
46 #include <locale.h>
47 #include <pwd.h>
48 #include <stdio.h>
49 /* must be after stdio to declare fparseln */
50 #include <libutil.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <sysexits.h>
54 #include <unistd.h>
55 
56 #include "mntopts.h"
57 
58 static gid_t	a_gid(char *);
59 static uid_t	a_uid(char *);
60 static mode_t	a_mask(char *);
61 static void	usage(void) __dead2;
62 static int	set_charset(struct iovec **iov, int *iovlen, const char *, const char *);
63 
64 int
65 main(int argc, char **argv)
66 {
67 	struct iovec *iov = NULL;
68 	int iovlen = 0;
69 	struct stat sb;
70 	int c, set_gid, set_uid, set_mask, set_dirmask;
71 	char *dev, *dir, mntpath[MAXPATHLEN], *csp;
72 	char fstype[] = "msdosfs";
73 	char errmsg[255] = {0};
74 	char *cs_dos = NULL;
75 	char *cs_local = NULL;
76 	mode_t mask = 0, dirmask = 0;
77 	uid_t uid = 0;
78 	gid_t gid = 0;
79 
80 	set_gid = set_uid = set_mask = set_dirmask = 0;
81 
82 	while ((c = getopt(argc, argv, "sl9u:g:m:M:o:L:D:W:")) != -1) {
83 		switch (c) {
84 		case 's':
85 			build_iovec(&iov, &iovlen, "shortnames", NULL, (size_t)-1);
86 			break;
87 		case 'l':
88 			build_iovec(&iov, &iovlen, "longnames", NULL, (size_t)-1);
89 			break;
90 		case '9':
91 			build_iovec_argf(&iov, &iovlen, "nowin95", "", (size_t)-1);
92 			break;
93 		case 'u':
94 			uid = a_uid(optarg);
95 			set_uid = 1;
96 			break;
97 		case 'g':
98 			gid = a_gid(optarg);
99 			set_gid = 1;
100 			break;
101 		case 'm':
102 			mask = a_mask(optarg);
103 			set_mask = 1;
104 			break;
105 		case 'M':
106 			dirmask = a_mask(optarg);
107 			set_dirmask = 1;
108 			break;
109 		case 'L': {
110 			const char *quirk = NULL;
111 			if (setlocale(LC_CTYPE, optarg) == NULL)
112 				err(EX_CONFIG, "%s", optarg);
113 			csp = strchr(optarg,'.');
114 			if (!csp)
115 				err(EX_CONFIG, "%s", optarg);
116 			quirk = kiconv_quirkcs(csp + 1, KICONV_VENDOR_MICSFT);
117 			build_iovec_argf(&iov, &iovlen, "cs_local", quirk);
118 			cs_local = strdup(quirk);
119 			}
120 			break;
121 		case 'D':
122 			cs_dos = strdup(optarg);
123 			build_iovec_argf(&iov, &iovlen, "cs_dos", cs_dos, (size_t)-1);
124 			break;
125 		case 'o': {
126 			char *p = NULL;
127 			char *val = strdup("");
128 			p = strchr(optarg, '=');
129 			if (p != NULL) {
130 				free(val);
131 				*p = '\0';
132 				val = p + 1;
133 			}
134 			build_iovec(&iov, &iovlen, optarg, val, (size_t)-1);
135 			}
136 			break;
137 		case 'W':
138 			if (strcmp(optarg, "iso22dos") == 0) {
139 				cs_local = strdup("ISO8859-2");
140 				cs_dos = strdup("CP852");
141 			} else if (strcmp(optarg, "iso72dos") == 0) {
142 				cs_local = strdup("ISO8859-7");
143 				cs_dos = strdup("CP737");
144 			} else if (strcmp(optarg, "koi2dos") == 0) {
145 				cs_local = strdup("KOI8-R");
146 				cs_dos = strdup("CP866");
147 			} else if (strcmp(optarg, "koi8u2dos") == 0) {
148 				cs_local = strdup("KOI8-U");
149 				cs_dos = strdup("CP866");
150 			} else {
151 				err(EX_NOINPUT, "%s", optarg);
152 			}
153 			build_iovec(&iov, &iovlen, "cs_local", cs_local, (size_t)-1);
154 			build_iovec(&iov, &iovlen, "cs_dos", cs_dos, (size_t)-1);
155 			break;
156 		case '?':
157 		default:
158 			usage();
159 			break;
160 		}
161 	}
162 
163 	if (optind + 2 != argc)
164 		usage();
165 
166 	if (set_mask && !set_dirmask) {
167 		dirmask = mask;
168 		set_dirmask = 1;
169 	}
170 	else if (set_dirmask && !set_mask) {
171 		mask = dirmask;
172 		set_mask = 1;
173 	}
174 
175 	dev = argv[optind];
176 	dir = argv[optind + 1];
177 
178 	if (cs_local != NULL) {
179 		if (set_charset(&iov, &iovlen, cs_local, cs_dos) == -1)
180 			err(EX_OSERR, "msdosfs_iconv");
181 		build_iovec_argf(&iov, &iovlen, "kiconv", "");
182 	} else if (cs_dos != NULL) {
183 		build_iovec_argf(&iov, &iovlen, "cs_local", "ISO8859-1");
184 		if (set_charset(&iov, &iovlen, "ISO8859-1", cs_dos) == -1)
185 			err(EX_OSERR, "msdosfs_iconv");
186 		build_iovec_argf(&iov, &iovlen, "kiconv", "");
187 	}
188 
189 	/*
190 	 * Resolve the mountpoint with realpath(3) and remove unnecessary
191 	 * slashes from the devicename if there are any.
192 	 */
193 	if (checkpath(dir, mntpath) != 0)
194 		err(EX_USAGE, "%s", mntpath);
195 	(void)rmslashes(dev, dev);
196 
197 	if (!set_gid || !set_uid || !set_mask) {
198 		if (stat(mntpath, &sb) == -1)
199 			err(EX_OSERR, "stat %s", mntpath);
200 
201 		if (!set_uid)
202 			uid = sb.st_uid;
203 		if (!set_gid)
204 			gid = sb.st_gid;
205 		if (!set_mask)
206 			mask = dirmask =
207 				sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
208 	}
209 
210 	build_iovec(&iov, &iovlen, "fstype", fstype, (size_t)-1);
211 	build_iovec(&iov, &iovlen, "fspath", mntpath, (size_t)-1);
212 	build_iovec(&iov, &iovlen, "from", dev, (size_t)-1);
213 	build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
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, 0) < 0) {
220 		if (errmsg[0])
221 			err(1, "%s: %s", dev, errmsg);
222 		else
223 			err(1, "%s", dev);
224 	}
225 
226 	exit (0);
227 }
228 
229 gid_t
230 a_gid(char *s)
231 {
232 	struct group *gr;
233 	char *gname;
234 	gid_t gid;
235 
236 	if ((gr = getgrnam(s)) != NULL)
237 		gid = gr->gr_gid;
238 	else {
239 		for (gname = s; *s && isdigit(*s); ++s);
240 		if (!*s)
241 			gid = atoi(gname);
242 		else
243 			errx(EX_NOUSER, "unknown group id: %s", gname);
244 	}
245 	return (gid);
246 }
247 
248 uid_t
249 a_uid(char *s)
250 {
251 	struct passwd *pw;
252 	char *uname;
253 	uid_t uid;
254 
255 	if ((pw = getpwnam(s)) != NULL)
256 		uid = pw->pw_uid;
257 	else {
258 		for (uname = s; *s && isdigit(*s); ++s);
259 		if (!*s)
260 			uid = atoi(uname);
261 		else
262 			errx(EX_NOUSER, "unknown user id: %s", uname);
263 	}
264 	return (uid);
265 }
266 
267 mode_t
268 a_mask(char *s)
269 {
270 	int done, rv;
271 	char *ep;
272 
273 	done = 0;
274 	rv = -1;
275 	if (*s >= '0' && *s <= '7') {
276 		done = 1;
277 		rv = strtol(optarg, &ep, 8);
278 	}
279 	if (!done || rv < 0 || *ep)
280 		errx(EX_USAGE, "invalid file mode: %s", s);
281 	return (rv);
282 }
283 
284 void
285 usage(void)
286 {
287 	fprintf(stderr, "%s\n%s\n%s\n",
288 	"usage: mount_msdosfs [-9ls] [-D DOS_codepage] [-g gid] [-L locale]",
289 	"                     [-M mask] [-m mask] [-o options] [-u uid]",
290 	"		      [-W table] special node");
291 	exit(EX_USAGE);
292 }
293 
294 int
295 set_charset(struct iovec **iov, int *iovlen, const char *cs_local, const char *cs_dos)
296 {
297 	int error;
298 
299 	if (modfind("msdosfs_iconv") < 0)
300 		if (kldload("msdosfs_iconv") < 0 || modfind("msdosfs_iconv") < 0) {
301 			warnx("cannot find or load \"msdosfs_iconv\" kernel module");
302 			return (-1);
303 		}
304 
305 	build_iovec_argf(iov, iovlen, "cs_win", ENCODING_UNICODE);
306 	error = kiconv_add_xlat16_cspairs(ENCODING_UNICODE, cs_local);
307 	if (error && errno != EEXIST)
308 		return (-1);
309 	if (cs_dos != NULL) {
310 		error = kiconv_add_xlat16_cspairs(cs_dos, cs_local);
311 		if (error && errno != EEXIST)
312 			return (-1);
313 	} else {
314 		build_iovec_argf(iov, iovlen, "cs_dos", cs_local);
315 		error = kiconv_add_xlat16_cspair(cs_local, cs_local,
316 				KICONV_FROM_UPPER | KICONV_LOWER);
317 		if (error && errno != EEXIST)
318 			return (-1);
319 	}
320 
321 	return (0);
322 }
323