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