1151497Sru/*	$NetBSD: mount_msdos.c,v 1.18 1997/09/16 12:24:18 lukem Exp $	*/
2151497Sru
3151497Sru/*
4151497Sru * Copyright (c) 1994 Christopher G. Demetriou
5151497Sru * All rights reserved.
6151497Sru *
7151497Sru * Redistribution and use in source and binary forms, with or without
8151497Sru * modification, are permitted provided that the following conditions
9151497Sru * are met:
10151497Sru * 1. Redistributions of source code must retain the above copyright
11151497Sru *    notice, this list of conditions and the following disclaimer.
12151497Sru * 2. Redistributions in binary form must reproduce the above copyright
13151497Sru *    notice, this list of conditions and the following disclaimer in the
14151497Sru *    documentation and/or other materials provided with the distribution.
15151497Sru * 3. All advertising materials mentioning features or use of this software
16151497Sru *    must display the following acknowledgement:
17151497Sru *      This product includes software developed by Christopher G. Demetriou.
18151497Sru * 4. The name of the author may not be used to endorse or promote products
19151497Sru *    derived from this software without specific prior written permission
20151497Sru *
21151497Sru * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22151497Sru * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23151497Sru * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24151497Sru * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25151497Sru * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26151497Sru * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27151497Sru * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28151497Sru * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29151497Sru * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30151497Sru * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31151497Sru */
32151497Sru
33151497Sru#ifndef lint
34151497Srustatic const char rcsid[] =
35151497Sru  "$FreeBSD$";
36151497Sru#endif /* not lint */
37151497Sru
38151497Sru#include <sys/param.h>
39151497Sru#include <sys/mount.h>
40151497Sru#include <sys/stat.h>
41151497Sru#include <sys/iconv.h>
42151497Sru#include <sys/linker.h>
43151497Sru#include <sys/module.h>
44151497Sru
45151497Sru#include <ctype.h>
46151497Sru#include <err.h>
47151497Sru#include <grp.h>
48151497Sru#include <locale.h>
49151497Sru#include <pwd.h>
50151497Sru#include <stdio.h>
51151497Sru/* must be after stdio to declare fparseln */
52151497Sru#include <libutil.h>
53151497Sru#include <stdlib.h>
54151497Sru#include <string.h>
55151497Sru#include <sysexits.h>
56151497Sru#include <unistd.h>
57151497Sru
58151497Sru#include "mntopts.h"
59151497Sru
60151497Srustatic gid_t	a_gid(char *);
61151497Srustatic uid_t	a_uid(char *);
62151497Srustatic mode_t	a_mask(char *);
63151497Srustatic void	usage(void) __dead2;
64151497Srustatic int	set_charset(struct iovec **iov, int *iovlen, const char *, const char *);
65151497Sru
66151497Sruint
67151497Srumain(int argc, char **argv)
68151497Sru{
69151497Sru	struct iovec *iov = NULL;
70151497Sru	int iovlen = 0;
71151497Sru	struct stat sb;
72151497Sru	int c, set_gid, set_uid, set_mask, set_dirmask;
73151497Sru	char *dev, *dir, mntpath[MAXPATHLEN], *csp;
74151497Sru	char fstype[] = "msdosfs";
75151497Sru	char errmsg[255] = {0};
76151497Sru	char *cs_dos = NULL;
77151497Sru	char *cs_local = NULL;
78151497Sru	mode_t mask = 0, dirmask = 0;
79151497Sru	uid_t uid = 0;
80151497Sru	gid_t gid = 0;
81151497Sru
82151497Sru	set_gid = set_uid = set_mask = set_dirmask = 0;
83151497Sru
84151497Sru	while ((c = getopt(argc, argv, "sl9u:g:m:M:o:L:D:W:")) != -1) {
85151497Sru		switch (c) {
86151497Sru		case 's':
87151497Sru			build_iovec(&iov, &iovlen, "shortnames", NULL, (size_t)-1);
88151497Sru			break;
89151497Sru		case 'l':
90151497Sru			build_iovec(&iov, &iovlen, "longnames", NULL, (size_t)-1);
91151497Sru			break;
92151497Sru		case '9':
93151497Sru			build_iovec_argf(&iov, &iovlen, "nowin95", "", (size_t)-1);
94151497Sru			break;
95151497Sru		case 'u':
96151497Sru			uid = a_uid(optarg);
97151497Sru			set_uid = 1;
98151497Sru			break;
99151497Sru		case 'g':
100151497Sru			gid = a_gid(optarg);
101151497Sru			set_gid = 1;
102151497Sru			break;
103151497Sru		case 'm':
104151497Sru			mask = a_mask(optarg);
105151497Sru			set_mask = 1;
106151497Sru			break;
107151497Sru		case 'M':
108151497Sru			dirmask = a_mask(optarg);
109151497Sru			set_dirmask = 1;
110151497Sru			break;
111151497Sru		case 'L': {
112151497Sru			const char *quirk = NULL;
113151497Sru			if (setlocale(LC_CTYPE, optarg) == NULL)
114151497Sru				err(EX_CONFIG, "%s", optarg);
115151497Sru			csp = strchr(optarg,'.');
116151497Sru			if (!csp)
117151497Sru				err(EX_CONFIG, "%s", optarg);
118151497Sru			quirk = kiconv_quirkcs(csp + 1, KICONV_VENDOR_MICSFT);
119151497Sru			build_iovec_argf(&iov, &iovlen, "cs_local", quirk);
120151497Sru			cs_local = strdup(quirk);
121151497Sru			}
122151497Sru			break;
123151497Sru		case 'D':
124151497Sru			cs_dos = strdup(optarg);
125151497Sru			build_iovec_argf(&iov, &iovlen, "cs_dos", cs_dos, (size_t)-1);
126151497Sru			break;
127151497Sru		case 'o': {
128151497Sru			char *p = NULL;
129151497Sru			char *val = strdup("");
130151497Sru			p = strchr(optarg, '=');
131151497Sru			if (p != NULL) {
132151497Sru				free(val);
133151497Sru				*p = '\0';
134151497Sru				val = p + 1;
135151497Sru			}
136151497Sru			build_iovec(&iov, &iovlen, optarg, val, (size_t)-1);
137151497Sru			}
138151497Sru			break;
139151497Sru		case 'W':
140151497Sru			if (strcmp(optarg, "iso22dos") == 0) {
141151497Sru				cs_local = strdup("ISO8859-2");
142151497Sru				cs_dos = strdup("CP852");
143151497Sru			} else if (strcmp(optarg, "iso72dos") == 0) {
144151497Sru				cs_local = strdup("ISO8859-7");
145151497Sru				cs_dos = strdup("CP737");
146151497Sru			} else if (strcmp(optarg, "koi2dos") == 0) {
147151497Sru				cs_local = strdup("KOI8-R");
148151497Sru				cs_dos = strdup("CP866");
149151497Sru			} else if (strcmp(optarg, "koi8u2dos") == 0) {
150151497Sru				cs_local = strdup("KOI8-U");
151151497Sru				cs_dos = strdup("CP866");
152151497Sru			} else {
153151497Sru				err(EX_NOINPUT, "%s", optarg);
154151497Sru			}
155151497Sru			build_iovec(&iov, &iovlen, "cs_local", cs_local, (size_t)-1);
156151497Sru			build_iovec(&iov, &iovlen, "cs_dos", cs_dos, (size_t)-1);
157151497Sru			break;
158151497Sru		case '?':
159151497Sru		default:
160151497Sru			usage();
161151497Sru			break;
162151497Sru		}
163151497Sru	}
164151497Sru
165151497Sru	if (optind + 2 != argc)
166151497Sru		usage();
167151497Sru
168151497Sru	if (set_mask && !set_dirmask) {
169151497Sru		dirmask = mask;
170151497Sru		set_dirmask = 1;
171151497Sru	}
172151497Sru	else if (set_dirmask && !set_mask) {
173151497Sru		mask = dirmask;
174151497Sru		set_mask = 1;
175151497Sru	}
176151497Sru
177151497Sru	dev = argv[optind];
178151497Sru	dir = argv[optind + 1];
179151497Sru
180151497Sru	if (cs_local != NULL) {
181151497Sru		if (set_charset(&iov, &iovlen, cs_local, cs_dos) == -1)
182151497Sru			err(EX_OSERR, "msdosfs_iconv");
183151497Sru		build_iovec_argf(&iov, &iovlen, "kiconv", "");
184151497Sru	} else if (cs_dos != NULL) {
185151497Sru		build_iovec_argf(&iov, &iovlen, "cs_local", "ISO8859-1");
186151497Sru		if (set_charset(&iov, &iovlen, "ISO8859-1", cs_dos) == -1)
187151497Sru			err(EX_OSERR, "msdosfs_iconv");
188151497Sru		build_iovec_argf(&iov, &iovlen, "kiconv", "");
189151497Sru	}
190151497Sru
191151497Sru	/*
192151497Sru	 * Resolve the mountpoint with realpath(3) and remove unnecessary
193151497Sru	 * slashes from the devicename if there are any.
194151497Sru	 */
195151497Sru	if (checkpath(dir, mntpath) != 0)
196151497Sru		err(EX_USAGE, "%s", mntpath);
197151497Sru	(void)rmslashes(dev, dev);
198151497Sru
199151497Sru	if (!set_gid || !set_uid || !set_mask) {
200151497Sru		if (stat(mntpath, &sb) == -1)
201151497Sru			err(EX_OSERR, "stat %s", mntpath);
202151497Sru
203151497Sru		if (!set_uid)
204151497Sru			uid = sb.st_uid;
205151497Sru		if (!set_gid)
206151497Sru			gid = sb.st_gid;
207151497Sru		if (!set_mask)
208151497Sru			mask = dirmask =
209151497Sru				sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
210151497Sru	}
211151497Sru
212151497Sru	build_iovec(&iov, &iovlen, "fstype", fstype, (size_t)-1);
213151497Sru	build_iovec(&iov, &iovlen, "fspath", mntpath, (size_t)-1);
214151497Sru	build_iovec(&iov, &iovlen, "from", dev, (size_t)-1);
215151497Sru	build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
216151497Sru	build_iovec_argf(&iov, &iovlen, "uid", "%d", uid);
217151497Sru	build_iovec_argf(&iov, &iovlen, "gid", "%u", gid);
218151497Sru	build_iovec_argf(&iov, &iovlen, "mask", "%u", mask);
219151497Sru	build_iovec_argf(&iov, &iovlen, "dirmask", "%u", dirmask);
220151497Sru
221151497Sru	if (nmount(iov, iovlen, 0) < 0) {
222151497Sru		if (errmsg[0])
223151497Sru			err(1, "%s: %s", dev, errmsg);
224151497Sru		else
225151497Sru			err(1, "%s", dev);
226151497Sru	}
227151497Sru
228151497Sru	exit (0);
229151497Sru}
230151497Sru
231151497Srugid_t
232151497Srua_gid(char *s)
233151497Sru{
234151497Sru	struct group *gr;
235151497Sru	char *gname;
236151497Sru	gid_t gid;
237151497Sru
238151497Sru	if ((gr = getgrnam(s)) != NULL)
239151497Sru		gid = gr->gr_gid;
240151497Sru	else {
241151497Sru		for (gname = s; *s && isdigit(*s); ++s);
242151497Sru		if (!*s)
243151497Sru			gid = atoi(gname);
244151497Sru		else
245151497Sru			errx(EX_NOUSER, "unknown group id: %s", gname);
246151497Sru	}
247151497Sru	return (gid);
248151497Sru}
249151497Sru
250151497Sruuid_t
251151497Srua_uid(char *s)
252151497Sru{
253151497Sru	struct passwd *pw;
254151497Sru	char *uname;
255151497Sru	uid_t uid;
256151497Sru
257151497Sru	if ((pw = getpwnam(s)) != NULL)
258151497Sru		uid = pw->pw_uid;
259151497Sru	else {
260151497Sru		for (uname = s; *s && isdigit(*s); ++s);
261151497Sru		if (!*s)
262151497Sru			uid = atoi(uname);
263151497Sru		else
264151497Sru			errx(EX_NOUSER, "unknown user id: %s", uname);
265151497Sru	}
266151497Sru	return (uid);
267151497Sru}
268151497Sru
269151497Srumode_t
270151497Srua_mask(char *s)
271151497Sru{
272151497Sru	int done, rv;
273151497Sru	char *ep;
274151497Sru
275151497Sru	done = 0;
276151497Sru	rv = -1;
277151497Sru	if (*s >= '0' && *s <= '7') {
278151497Sru		done = 1;
279151497Sru		rv = strtol(optarg, &ep, 8);
280151497Sru	}
281151497Sru	if (!done || rv < 0 || *ep)
282151497Sru		errx(EX_USAGE, "invalid file mode: %s", s);
283151497Sru	return (rv);
284151497Sru}
285151497Sru
286151497Sruvoid
287151497Sruusage(void)
288151497Sru{
289151497Sru	fprintf(stderr, "%s\n%s\n%s\n",
290151497Sru	"usage: mount_msdosfs [-9ls] [-D DOS_codepage] [-g gid] [-L locale]",
291151497Sru	"                     [-M mask] [-m mask] [-o options] [-u uid]",
292151497Sru	"		      [-W table] special node");
293151497Sru	exit(EX_USAGE);
294151497Sru}
295151497Sru
296151497Sruint
297151497Sruset_charset(struct iovec **iov, int *iovlen, const char *cs_local, const char *cs_dos)
298151497Sru{
299151497Sru	int error;
300151497Sru
301151497Sru	if (modfind("msdosfs_iconv") < 0)
302151497Sru		if (kldload("msdosfs_iconv") < 0 || modfind("msdosfs_iconv") < 0) {
303151497Sru			warnx("cannot find or load \"msdosfs_iconv\" kernel module");
304151497Sru			return (-1);
305151497Sru		}
306151497Sru
307151497Sru	build_iovec_argf(iov, iovlen, "cs_win", ENCODING_UNICODE);
308151497Sru	error = kiconv_add_xlat16_cspairs(ENCODING_UNICODE, cs_local);
309151497Sru	if (error)
310151497Sru		return (-1);
311151497Sru	if (cs_dos != NULL) {
312151497Sru		error = kiconv_add_xlat16_cspairs(cs_dos, cs_local);
313151497Sru		if (error)
314151497Sru			return (-1);
315151497Sru	} else {
316151497Sru		build_iovec_argf(iov, iovlen, "cs_dos", cs_local);
317151497Sru		error = kiconv_add_xlat16_cspair(cs_local, cs_local,
318151497Sru				KICONV_FROM_UPPER | KICONV_LOWER);
319151497Sru		if (error)
320151497Sru			return (-1);
321151497Sru	}
322151497Sru
323151497Sru	return (0);
324151497Sru}
325151497Sru