xref: /dragonfly/sbin/mount_msdos/mount_msdos.c (revision 8f2ce533)
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 #include <vfs/msdosfs/msdosfsmount.h>
42 
43 #include <ctype.h>
44 #include <err.h>
45 #include <grp.h>
46 #include <libutil.h>
47 #include <locale.h>
48 #include <mntopts.h>
49 #include <pwd.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <sysexits.h>
54 #include <unistd.h>
55 
56 /*
57  * XXX - no way to specify "foo=<bar>"-type options; that's what we'd
58  * want for "-u", "-g", "-m", "-M", "-L", and "-D".
59  */
60 static struct mntopt mopts[] = {
61 	MOPT_STDOPTS,
62 	MOPT_FORCE,
63 	MOPT_SYNC,
64 	MOPT_ASYNC,
65 	MOPT_UPDATE,
66 	{ "shortnames", 0, MSDOSFSMNT_SHORTNAME, 1 },
67 	{ "longnames", 0, MSDOSFSMNT_LONGNAME, 1 },
68 	{ "nowin95", 0, MSDOSFSMNT_NOWIN95, 1 },
69 	MOPT_NULL
70 };
71 
72 static gid_t a_gid(char *);
73 static uid_t a_uid(char *);
74 static mode_t a_mask(char *);
75 static void usage(void) __dead2;
76 int set_charset(struct msdosfs_args *, const char *, const char *);
77 
78 int
79 main(int argc, char **argv)
80 {
81 	struct msdosfs_args args;
82 	struct stat sb;
83 	int c, error, mntflags, set_gid, set_uid, set_mask, set_dirmask;
84 	char *dev, *dir, mntpath[MAXPATHLEN], *csp;
85 	const char *quirk = NULL;
86 	char *cs_local = NULL;
87 	char *cs_dos = NULL;
88 	struct vfsconf vfc;
89 	mntflags = set_gid = set_uid = set_mask = set_dirmask = 0;
90 	memset(&args, '\0', sizeof(args));
91 
92 	while ((c = getopt(argc, argv, "sl9u:g:m:M:o:L:D:")) != -1) {
93 		switch (c) {
94 		case 's':
95 			args.flags |= MSDOSFSMNT_SHORTNAME;
96 			break;
97 		case 'l':
98 			args.flags |= MSDOSFSMNT_LONGNAME;
99 			break;
100 		case '9':
101 			args.flags |= MSDOSFSMNT_NOWIN95;
102 			break;
103 		case 'u':
104 			args.uid = a_uid(optarg);
105 			set_uid = 1;
106 			break;
107 		case 'g':
108 			args.gid = a_gid(optarg);
109 			set_gid = 1;
110 			break;
111 		case 'm':
112 			args.mask = a_mask(optarg);
113 			set_mask = 1;
114 			break;
115 		case 'M':
116 			args.dirmask = a_mask(optarg);
117 			set_dirmask = 1;
118 			break;
119 		case 'L':
120 			if (setlocale(LC_CTYPE, optarg) == NULL)
121 				err(EX_CONFIG, "%s", optarg);
122 			csp = strchr(optarg,'.');
123 			if (!csp)
124 				err(EX_CONFIG, "%s", optarg);
125 			quirk = kiconv_quirkcs(csp + 1, KICONV_VENDOR_MICSFT);
126 			cs_local = strdup(quirk);
127 			args.flags |= MSDOSFSMNT_KICONV;
128 			break;
129 		case 'D':
130 			csp = optarg;
131 			cs_dos = strdup(optarg);
132 			args.flags |= MSDOSFSMNT_KICONV;
133 			break;
134 		case 'o':
135 			getmntopts(optarg, mopts, &mntflags, &args.flags);
136 			break;
137 		case '?':
138 		default:
139 			usage();
140 			break;
141 		}
142 	}
143 
144 	if (optind + 2 != argc)
145 		usage();
146 
147 	if (set_mask && !set_dirmask) {
148 		args.dirmask = args.mask;
149 		set_dirmask = 1;
150 	}
151 	else if (set_dirmask && !set_mask) {
152 		args.mask = args.dirmask;
153 		set_mask = 1;
154 	}
155 
156 	dev = argv[optind];
157 	dir = argv[optind + 1];
158 
159 	/*
160 	 * Resolve the mountpoint with realpath(3) and remove unnecessary
161 	 * slashes from the devicename if there are any.
162 	 */
163 	checkpath(dir, mntpath);
164 	rmslashes(dev, dev);
165 
166 	/*
167 	 * If the device path is not absolute then automatically
168 	 * add the /dev prefix.
169 	 */
170 	if (dev[0] != '/')
171 		asprintf(&args.fspec, "/dev/%s", dev);
172 	else
173 		args.fspec = dev;
174 	args.export.ex_root = -2;	/* unchecked anyway on DOS fs */
175 
176 	if (cs_local != NULL) {
177 		if (set_charset(&args, cs_local, cs_dos) == -1)
178 			err(EX_OSERR, "msdos_iconv");
179 	} else if (cs_dos != NULL) {
180 		if (set_charset(&args, "ISO8859-1", cs_dos) == -1)
181 			err(EX_OSERR, "msdos_iconv");
182 	}
183 
184 	if (mntflags & MNT_RDONLY)
185 		args.export.ex_flags = MNT_EXRDONLY;
186 	else
187 		args.export.ex_flags = 0;
188 	if (!set_gid || !set_uid || !set_mask) {
189 		if (stat(mntpath, &sb) == -1)
190 			err(EX_OSERR, "stat %s", mntpath);
191 
192 		if (!set_uid)
193 			args.uid = sb.st_uid;
194 		if (!set_gid)
195 			args.gid = sb.st_gid;
196 		if (!set_mask)
197 			args.mask = args.dirmask =
198 				sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
199 	}
200 
201 	error = getvfsbyname("msdos", &vfc);
202 	if (error && vfsisloadable("msdos")) {
203 		if (vfsload("msdos"))
204 			err(EX_OSERR, "vfsload(msdos)");
205 		endvfsent();	/* clear cache */
206 		error = getvfsbyname("msdos", &vfc);
207 	}
208 	if (error)
209 		errx(EX_OSERR, "msdos filesystem is not available");
210 
211 	if (mount(vfc.vfc_name, mntpath, mntflags, &args) < 0)
212 		err(EX_OSERR, "%s", dev);
213 
214 	exit (0);
215 }
216 
217 static gid_t
218 a_gid(char *s)
219 {
220 	struct group *gr;
221 	char *gname;
222 	gid_t gid;
223 
224 	if ((gr = getgrnam(s)) != NULL)
225 		gid = gr->gr_gid;
226 	else {
227 		for (gname = s; *s && isdigit(*s); ++s);
228 		if (!*s)
229 			gid = atoi(gname);
230 		else
231 			errx(EX_NOUSER, "unknown group id: %s", gname);
232 	}
233 	return (gid);
234 }
235 
236 static uid_t
237 a_uid(char *s)
238 {
239 	struct passwd *pw;
240 	char *uname;
241 	uid_t uid;
242 
243 	if ((pw = getpwnam(s)) != NULL)
244 		uid = pw->pw_uid;
245 	else {
246 		for (uname = s; *s && isdigit(*s); ++s);
247 		if (!*s)
248 			uid = atoi(uname);
249 		else
250 			errx(EX_NOUSER, "unknown user id: %s", uname);
251 	}
252 	return (uid);
253 }
254 
255 static mode_t
256 a_mask(char *s)
257 {
258 	int done, rv;
259 	char *ep;
260 
261 	done = 0;
262 	rv = -1;
263 	if (*s >= '0' && *s <= '7') {
264 		done = 1;
265 		rv = strtol(optarg, &ep, 8);
266 	}
267 	if (!done || rv < 0 || *ep)
268 		errx(EX_USAGE, "invalid file mode: %s", s);
269 	return (rv);
270 }
271 
272 static void
273 usage(void)
274 {
275 	fprintf(stderr, "%s\n%s\n%s\n",
276 	    "usage: mount_msdos [-9ls] [-D DOS_codepage] [-g gid] [-L locale]",
277 	    "                   [-M mask] [-m mask] [-o options] [-u uid]",
278 	    "                   special node");
279 	exit(EX_USAGE);
280 }
281 
282 int
283 set_charset(struct msdosfs_args *args, const char *cs_local, const char *cs_dos)
284 {
285 	int error;
286 	if (modfind("msdos_iconv") < 0) {
287 		if (kldload("msdos_iconv") < 0 || modfind("msdos_iconv") < 0) {
288 			warnx("cannot find or load \"msdos_iconv\" kernel module");
289 			return (-1);
290 		}
291 	}
292 	snprintf(args->cs_local, ICONV_CSNMAXLEN, "%s", cs_local);
293 	error = kiconv_add_xlat16_cspairs(ENCODING_UNICODE, cs_local);
294 	if (error)
295 		return (-1);
296 	if (!cs_dos)
297 		cs_dos = strdup("CP437");
298 	snprintf(args->cs_dos, ICONV_CSNMAXLEN, "%s", cs_dos);
299 	error = kiconv_add_xlat16_cspairs(cs_dos, cs_local);
300 	if (error)
301 		return (-1);
302 	return (0);
303 }
304