133549Sjkh/*	$NetBSD: mount_msdos.c,v 1.18 1997/09/16 12:24:18 lukem Exp $	*/
233549Sjkh
32892Sdfr/*
42892Sdfr * Copyright (c) 1994 Christopher G. Demetriou
52892Sdfr * All rights reserved.
62892Sdfr *
72892Sdfr * Redistribution and use in source and binary forms, with or without
82892Sdfr * modification, are permitted provided that the following conditions
92892Sdfr * are met:
102892Sdfr * 1. Redistributions of source code must retain the above copyright
112892Sdfr *    notice, this list of conditions and the following disclaimer.
122892Sdfr * 2. Redistributions in binary form must reproduce the above copyright
132892Sdfr *    notice, this list of conditions and the following disclaimer in the
142892Sdfr *    documentation and/or other materials provided with the distribution.
152892Sdfr * 3. All advertising materials mentioning features or use of this software
162892Sdfr *    must display the following acknowledgement:
172892Sdfr *      This product includes software developed by Christopher G. Demetriou.
182892Sdfr * 4. The name of the author may not be used to endorse or promote products
192892Sdfr *    derived from this software without specific prior written permission
202892Sdfr *
212892Sdfr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
222892Sdfr * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
232892Sdfr * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
242892Sdfr * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
252892Sdfr * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
262892Sdfr * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
272892Sdfr * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
282892Sdfr * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
292892Sdfr * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
302892Sdfr * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
312892Sdfr */
322892Sdfr
332892Sdfr#ifndef lint
3415771Swollmanstatic const char rcsid[] =
3550476Speter  "$FreeBSD: stable/10/sbin/mount_msdosfs/mount_msdosfs.c 340954 2018-11-26 11:23:01Z eugen $";
362892Sdfr#endif /* not lint */
372892Sdfr
382892Sdfr#include <sys/param.h>
392892Sdfr#include <sys/mount.h>
402892Sdfr#include <sys/stat.h>
41120492Sfjoe#include <sys/iconv.h>
42121363Strhodes#include <sys/linker.h>
43121429Strhodes#include <sys/module.h>
4423335Sbde
452892Sdfr#include <ctype.h>
462892Sdfr#include <err.h>
47340954Seugen#include <errno.h>
482892Sdfr#include <grp.h>
4933761Sache#include <locale.h>
502892Sdfr#include <pwd.h>
512892Sdfr#include <stdio.h>
5255613Sache/* must be after stdio to declare fparseln */
5355613Sache#include <libutil.h>
542892Sdfr#include <stdlib.h>
552892Sdfr#include <string.h>
5615771Swollman#include <sysexits.h>
572892Sdfr#include <unistd.h>
58121429Strhodes
592892Sdfr#include "mntopts.h"
602892Sdfr
6192882Simpstatic gid_t	a_gid(char *);
6292882Simpstatic uid_t	a_uid(char *);
6392882Simpstatic mode_t	a_mask(char *);
6492882Simpstatic void	usage(void) __dead2;
65152809Savatarstatic int	set_charset(struct iovec **iov, int *iovlen, const char *, const char *);
662892Sdfr
672892Sdfrint
68121373Strhodesmain(int argc, char **argv)
692892Sdfr{
70152416Smaxim	struct iovec *iov = NULL;
71152416Smaxim	int iovlen = 0;
722892Sdfr	struct stat sb;
73247856Sjkim	int c, set_gid, set_uid, set_mask, set_dirmask;
74120492Sfjoe	char *dev, *dir, mntpath[MAXPATHLEN], *csp;
75152362Srodrigc	char fstype[] = "msdosfs";
76166326Srodrigc	char errmsg[255] = {0};
77152362Srodrigc	char *cs_dos = NULL;
78152362Srodrigc	char *cs_local = NULL;
79152362Srodrigc	mode_t mask = 0, dirmask = 0;
80152362Srodrigc	uid_t uid = 0;
81152362Srodrigc	gid_t gid = 0;
822892Sdfr
83247856Sjkim	set_gid = set_uid = set_mask = set_dirmask = 0;
842892Sdfr
85152731Savatar	while ((c = getopt(argc, argv, "sl9u:g:m:M:o:L:D:W:")) != -1) {
862892Sdfr		switch (c) {
8733549Sjkh		case 's':
88152362Srodrigc			build_iovec(&iov, &iovlen, "shortnames", NULL, (size_t)-1);
8933549Sjkh			break;
9033549Sjkh		case 'l':
91152362Srodrigc			build_iovec(&iov, &iovlen, "longnames", NULL, (size_t)-1);
9233549Sjkh			break;
9333549Sjkh		case '9':
94152731Savatar			build_iovec_argf(&iov, &iovlen, "nowin95", "", (size_t)-1);
9533549Sjkh			break;
962892Sdfr		case 'u':
97152362Srodrigc			uid = a_uid(optarg);
982892Sdfr			set_uid = 1;
992892Sdfr			break;
1002892Sdfr		case 'g':
101152362Srodrigc			gid = a_gid(optarg);
1022892Sdfr			set_gid = 1;
1032892Sdfr			break;
1042892Sdfr		case 'm':
105152362Srodrigc			mask = a_mask(optarg);
1062892Sdfr			set_mask = 1;
1072892Sdfr			break;
108118837Strhodes		case 'M':
109152362Srodrigc			dirmask = a_mask(optarg);
110118837Strhodes			set_dirmask = 1;
111118837Strhodes			break;
112152362Srodrigc		case 'L': {
113152362Srodrigc			const char *quirk = NULL;
114120492Sfjoe			if (setlocale(LC_CTYPE, optarg) == NULL)
115120492Sfjoe				err(EX_CONFIG, "%s", optarg);
116120492Sfjoe			csp = strchr(optarg,'.');
117120492Sfjoe			if (!csp)
118120492Sfjoe				err(EX_CONFIG, "%s", optarg);
119152362Srodrigc			quirk = kiconv_quirkcs(csp + 1, KICONV_VENDOR_MICSFT);
120152362Srodrigc			build_iovec_argf(&iov, &iovlen, "cs_local", quirk);
121152974Savatar			cs_local = strdup(quirk);
122152362Srodrigc			}
12333761Sache			break;
124120492Sfjoe		case 'D':
125152362Srodrigc			cs_dos = strdup(optarg);
126152362Srodrigc			build_iovec_argf(&iov, &iovlen, "cs_dos", cs_dos, (size_t)-1);
12733749Sache			break;
128152362Srodrigc		case 'o': {
129152362Srodrigc			char *p = NULL;
130152362Srodrigc			char *val = strdup("");
131152416Smaxim			p = strchr(optarg, '=');
132152416Smaxim			if (p != NULL) {
133152362Srodrigc				free(val);
134152362Srodrigc				*p = '\0';
135152362Srodrigc				val = p + 1;
136152362Srodrigc			}
137152362Srodrigc			build_iovec(&iov, &iovlen, optarg, val, (size_t)-1);
138152362Srodrigc			}
1392892Sdfr			break;
140120492Sfjoe		case 'W':
141120492Sfjoe			if (strcmp(optarg, "iso22dos") == 0) {
142152362Srodrigc				cs_local = strdup("ISO8859-2");
143152362Srodrigc				cs_dos = strdup("CP852");
144120492Sfjoe			} else if (strcmp(optarg, "iso72dos") == 0) {
145152362Srodrigc				cs_local = strdup("ISO8859-7");
146152362Srodrigc				cs_dos = strdup("CP737");
147120492Sfjoe			} else if (strcmp(optarg, "koi2dos") == 0) {
148152362Srodrigc				cs_local = strdup("KOI8-R");
149152362Srodrigc				cs_dos = strdup("CP866");
150120492Sfjoe			} else if (strcmp(optarg, "koi8u2dos") == 0) {
151152362Srodrigc				cs_local = strdup("KOI8-U");
152152362Srodrigc				cs_dos = strdup("CP866");
153120492Sfjoe			} else {
154120492Sfjoe				err(EX_NOINPUT, "%s", optarg);
155120492Sfjoe			}
156152362Srodrigc			build_iovec(&iov, &iovlen, "cs_local", cs_local, (size_t)-1);
157152362Srodrigc			build_iovec(&iov, &iovlen, "cs_dos", cs_dos, (size_t)-1);
158120492Sfjoe			break;
1592892Sdfr		case '?':
1602892Sdfr		default:
1612892Sdfr			usage();
1622892Sdfr			break;
1632892Sdfr		}
1642892Sdfr	}
1652892Sdfr
1662892Sdfr	if (optind + 2 != argc)
1672892Sdfr		usage();
1682892Sdfr
169118837Strhodes	if (set_mask && !set_dirmask) {
170152362Srodrigc		dirmask = mask;
171118837Strhodes		set_dirmask = 1;
172118837Strhodes	}
173118837Strhodes	else if (set_dirmask && !set_mask) {
174152362Srodrigc		mask = dirmask;
175118837Strhodes		set_mask = 1;
176118837Strhodes	}
177118837Strhodes
1782892Sdfr	dev = argv[optind];
1792892Sdfr	dir = argv[optind + 1];
1802892Sdfr
181152362Srodrigc	if (cs_local != NULL) {
182152809Savatar		if (set_charset(&iov, &iovlen, cs_local, cs_dos) == -1)
183120492Sfjoe			err(EX_OSERR, "msdosfs_iconv");
184152362Srodrigc		build_iovec_argf(&iov, &iovlen, "kiconv", "");
185152362Srodrigc	} else if (cs_dos != NULL) {
186152362Srodrigc		build_iovec_argf(&iov, &iovlen, "cs_local", "ISO8859-1");
187152809Savatar		if (set_charset(&iov, &iovlen, "ISO8859-1", cs_dos) == -1)
188120492Sfjoe			err(EX_OSERR, "msdosfs_iconv");
189152362Srodrigc		build_iovec_argf(&iov, &iovlen, "kiconv", "");
190120492Sfjoe	}
191120492Sfjoe
19252055Sphk	/*
19352055Sphk	 * Resolve the mountpoint with realpath(3) and remove unnecessary
19452055Sphk	 * slashes from the devicename if there are any.
19552055Sphk	 */
196230226Sjh	if (checkpath(dir, mntpath) != 0)
197230226Sjh		err(EX_USAGE, "%s", mntpath);
19852055Sphk	(void)rmslashes(dev, dev);
19952055Sphk
2002892Sdfr	if (!set_gid || !set_uid || !set_mask) {
20152055Sphk		if (stat(mntpath, &sb) == -1)
20252055Sphk			err(EX_OSERR, "stat %s", mntpath);
2032892Sdfr
2042892Sdfr		if (!set_uid)
205152362Srodrigc			uid = sb.st_uid;
2062892Sdfr		if (!set_gid)
207152362Srodrigc			gid = sb.st_gid;
2082892Sdfr		if (!set_mask)
209152362Srodrigc			mask = dirmask =
210118837Strhodes				sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
2112892Sdfr	}
2122892Sdfr
213152362Srodrigc	build_iovec(&iov, &iovlen, "fstype", fstype, (size_t)-1);
214152362Srodrigc	build_iovec(&iov, &iovlen, "fspath", mntpath, (size_t)-1);
215152362Srodrigc	build_iovec(&iov, &iovlen, "from", dev, (size_t)-1);
216166326Srodrigc	build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
217152362Srodrigc	build_iovec_argf(&iov, &iovlen, "uid", "%d", uid);
218152362Srodrigc	build_iovec_argf(&iov, &iovlen, "gid", "%u", gid);
219152362Srodrigc	build_iovec_argf(&iov, &iovlen, "mask", "%u", mask);
220152362Srodrigc	build_iovec_argf(&iov, &iovlen, "dirmask", "%u", dirmask);
2212892Sdfr
222247856Sjkim	if (nmount(iov, iovlen, 0) < 0) {
223185422Simp		if (errmsg[0])
224185422Simp			err(1, "%s: %s", dev, errmsg);
225185422Simp		else
226185422Simp			err(1, "%s", dev);
227185422Simp	}
228152362Srodrigc
2292892Sdfr	exit (0);
2302892Sdfr}
2312892Sdfr
2322892Sdfrgid_t
233201227Seda_gid(char *s)
2342892Sdfr{
2352892Sdfr	struct group *gr;
2362892Sdfr	char *gname;
2372892Sdfr	gid_t gid;
2382892Sdfr
2392892Sdfr	if ((gr = getgrnam(s)) != NULL)
2402892Sdfr		gid = gr->gr_gid;
2412892Sdfr	else {
2422892Sdfr		for (gname = s; *s && isdigit(*s); ++s);
2432892Sdfr		if (!*s)
2442892Sdfr			gid = atoi(gname);
2452892Sdfr		else
24615771Swollman			errx(EX_NOUSER, "unknown group id: %s", gname);
2472892Sdfr	}
2482892Sdfr	return (gid);
2492892Sdfr}
2502892Sdfr
2512892Sdfruid_t
252201227Seda_uid(char *s)
2532892Sdfr{
2542892Sdfr	struct passwd *pw;
2552892Sdfr	char *uname;
2562892Sdfr	uid_t uid;
2572892Sdfr
2582892Sdfr	if ((pw = getpwnam(s)) != NULL)
2592892Sdfr		uid = pw->pw_uid;
2602892Sdfr	else {
2612892Sdfr		for (uname = s; *s && isdigit(*s); ++s);
2622892Sdfr		if (!*s)
2632892Sdfr			uid = atoi(uname);
2642892Sdfr		else
26515771Swollman			errx(EX_NOUSER, "unknown user id: %s", uname);
2662892Sdfr	}
2672892Sdfr	return (uid);
2682892Sdfr}
2692892Sdfr
2702892Sdfrmode_t
271201227Seda_mask(char *s)
2722892Sdfr{
2732892Sdfr	int done, rv;
2742892Sdfr	char *ep;
2752892Sdfr
2762892Sdfr	done = 0;
27733549Sjkh	rv = -1;
2782892Sdfr	if (*s >= '0' && *s <= '7') {
2792892Sdfr		done = 1;
2802892Sdfr		rv = strtol(optarg, &ep, 8);
2812892Sdfr	}
2822892Sdfr	if (!done || rv < 0 || *ep)
28315771Swollman		errx(EX_USAGE, "invalid file mode: %s", s);
2842892Sdfr	return (rv);
2852892Sdfr}
2862892Sdfr
2872892Sdfrvoid
288201227Sedusage(void)
2892892Sdfr{
290121007Sfjoe	fprintf(stderr, "%s\n%s\n%s\n",
291121429Strhodes	"usage: mount_msdosfs [-9ls] [-D DOS_codepage] [-g gid] [-L locale]",
292121429Strhodes	"                     [-M mask] [-m mask] [-o options] [-u uid]",
293121429Strhodes	"		      [-W table] special node");
29415771Swollman	exit(EX_USAGE);
2952892Sdfr}
29633749Sache
297120492Sfjoeint
298152809Savatarset_charset(struct iovec **iov, int *iovlen, const char *cs_local, const char *cs_dos)
29933749Sache{
300120492Sfjoe	int error;
30133749Sache
302120492Sfjoe	if (modfind("msdosfs_iconv") < 0)
303120492Sfjoe		if (kldload("msdosfs_iconv") < 0 || modfind("msdosfs_iconv") < 0) {
304134565Strhodes			warnx("cannot find or load \"msdosfs_iconv\" kernel module");
305120492Sfjoe			return (-1);
306120492Sfjoe		}
307120492Sfjoe
308152809Savatar	build_iovec_argf(iov, iovlen, "cs_win", ENCODING_UNICODE);
309152362Srodrigc	error = kiconv_add_xlat16_cspairs(ENCODING_UNICODE, cs_local);
310340954Seugen	if (error && errno != EEXIST)
311120492Sfjoe		return (-1);
312152362Srodrigc	if (cs_dos != NULL) {
313152362Srodrigc		error = kiconv_add_xlat16_cspairs(cs_dos, cs_local);
314340954Seugen		if (error && errno != EEXIST)
315120492Sfjoe			return (-1);
316120492Sfjoe	} else {
317152809Savatar		build_iovec_argf(iov, iovlen, "cs_dos", cs_local);
318152362Srodrigc		error = kiconv_add_xlat16_cspair(cs_local, cs_local,
319120492Sfjoe				KICONV_FROM_UPPER | KICONV_LOWER);
320340954Seugen		if (error && errno != EEXIST)
321120492Sfjoe			return (-1);
32233749Sache	}
32333761Sache
324120492Sfjoe	return (0);
32533761Sache}
326