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