1 /* $NetBSD: mount_msdos.c,v 1.18 1997/09/16 12:24:18 lukem Exp $ */
4 * Copyright (c) 1994 Christopher G. Demetriou
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Christopher G. Demetriou.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 static const char rcsid[] =
38 #include <sys/param.h>
39 #include <sys/mount.h>
41 #include <sys/iconv.h>
42 #include <sys/linker.h>
43 #include <sys/module.h>
51 /* must be after stdio to declare fparseln */
60 static gid_t a_gid(char *);
61 static uid_t a_uid(char *);
62 static mode_t a_mask(char *);
63 static void usage(void) __dead2;
64 static int set_charset(struct iovec **iov, int *iovlen, const char *, const char *);
67 main(int argc, char **argv)
69 struct iovec *iov = NULL;
72 int c, set_gid, set_uid, set_mask, set_dirmask;
73 char *dev, *dir, mntpath[MAXPATHLEN], *csp;
74 char fstype[] = "msdosfs";
75 char errmsg[255] = {0};
77 char *cs_local = NULL;
78 mode_t mask = 0, dirmask = 0;
82 set_gid = set_uid = set_mask = set_dirmask = 0;
84 while ((c = getopt(argc, argv, "sl9u:g:m:M:o:L:D:W:")) != -1) {
87 build_iovec(&iov, &iovlen, "shortnames", NULL, (size_t)-1);
90 build_iovec(&iov, &iovlen, "longnames", NULL, (size_t)-1);
93 build_iovec_argf(&iov, &iovlen, "nowin95", "", (size_t)-1);
104 mask = a_mask(optarg);
108 dirmask = a_mask(optarg);
112 const char *quirk = NULL;
113 if (setlocale(LC_CTYPE, optarg) == NULL)
114 err(EX_CONFIG, "%s", optarg);
115 csp = strchr(optarg,'.');
117 err(EX_CONFIG, "%s", optarg);
118 quirk = kiconv_quirkcs(csp + 1, KICONV_VENDOR_MICSFT);
119 build_iovec_argf(&iov, &iovlen, "cs_local", quirk);
120 cs_local = strdup(quirk);
124 cs_dos = strdup(optarg);
125 build_iovec_argf(&iov, &iovlen, "cs_dos", cs_dos, (size_t)-1);
129 char *val = strdup("");
130 p = strchr(optarg, '=');
136 build_iovec(&iov, &iovlen, optarg, val, (size_t)-1);
140 if (strcmp(optarg, "iso22dos") == 0) {
141 cs_local = strdup("ISO8859-2");
142 cs_dos = strdup("CP852");
143 } else if (strcmp(optarg, "iso72dos") == 0) {
144 cs_local = strdup("ISO8859-7");
145 cs_dos = strdup("CP737");
146 } else if (strcmp(optarg, "koi2dos") == 0) {
147 cs_local = strdup("KOI8-R");
148 cs_dos = strdup("CP866");
149 } else if (strcmp(optarg, "koi8u2dos") == 0) {
150 cs_local = strdup("KOI8-U");
151 cs_dos = strdup("CP866");
153 err(EX_NOINPUT, "%s", optarg);
155 build_iovec(&iov, &iovlen, "cs_local", cs_local, (size_t)-1);
156 build_iovec(&iov, &iovlen, "cs_dos", cs_dos, (size_t)-1);
165 if (optind + 2 != argc)
168 if (set_mask && !set_dirmask) {
172 else if (set_dirmask && !set_mask) {
178 dir = argv[optind + 1];
180 if (cs_local != NULL) {
181 if (set_charset(&iov, &iovlen, cs_local, cs_dos) == -1)
182 err(EX_OSERR, "msdosfs_iconv");
183 build_iovec_argf(&iov, &iovlen, "kiconv", "");
184 } else if (cs_dos != NULL) {
185 build_iovec_argf(&iov, &iovlen, "cs_local", "ISO8859-1");
186 if (set_charset(&iov, &iovlen, "ISO8859-1", cs_dos) == -1)
187 err(EX_OSERR, "msdosfs_iconv");
188 build_iovec_argf(&iov, &iovlen, "kiconv", "");
192 * Resolve the mountpoint with realpath(3) and remove unnecessary
193 * slashes from the devicename if there are any.
195 if (checkpath(dir, mntpath) != 0)
196 err(EX_USAGE, "%s", mntpath);
197 (void)rmslashes(dev, dev);
199 if (!set_gid || !set_uid || !set_mask) {
200 if (stat(mntpath, &sb) == -1)
201 err(EX_OSERR, "stat %s", mntpath);
209 sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
212 build_iovec(&iov, &iovlen, "fstype", fstype, (size_t)-1);
213 build_iovec(&iov, &iovlen, "fspath", mntpath, (size_t)-1);
214 build_iovec(&iov, &iovlen, "from", dev, (size_t)-1);
215 build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
216 build_iovec_argf(&iov, &iovlen, "uid", "%d", uid);
217 build_iovec_argf(&iov, &iovlen, "gid", "%u", gid);
218 build_iovec_argf(&iov, &iovlen, "mask", "%u", mask);
219 build_iovec_argf(&iov, &iovlen, "dirmask", "%u", dirmask);
221 if (nmount(iov, iovlen, 0) < 0) {
223 err(1, "%s: %s", dev, errmsg);
238 if ((gr = getgrnam(s)) != NULL)
241 for (gname = s; *s && isdigit(*s); ++s);
245 errx(EX_NOUSER, "unknown group id: %s", gname);
257 if ((pw = getpwnam(s)) != NULL)
260 for (uname = s; *s && isdigit(*s); ++s);
264 errx(EX_NOUSER, "unknown user id: %s", uname);
277 if (*s >= '0' && *s <= '7') {
279 rv = strtol(optarg, &ep, 8);
281 if (!done || rv < 0 || *ep)
282 errx(EX_USAGE, "invalid file mode: %s", s);
289 fprintf(stderr, "%s\n%s\n%s\n",
290 "usage: mount_msdosfs [-9ls] [-D DOS_codepage] [-g gid] [-L locale]",
291 " [-M mask] [-m mask] [-o options] [-u uid]",
292 " [-W table] special node");
297 set_charset(struct iovec **iov, int *iovlen, const char *cs_local, const char *cs_dos)
301 if (modfind("msdosfs_iconv") < 0)
302 if (kldload("msdosfs_iconv") < 0 || modfind("msdosfs_iconv") < 0) {
303 warnx("cannot find or load \"msdosfs_iconv\" kernel module");
307 build_iovec_argf(iov, iovlen, "cs_win", ENCODING_UNICODE);
308 error = kiconv_add_xlat16_cspairs(ENCODING_UNICODE, cs_local);
311 if (cs_dos != NULL) {
312 error = kiconv_add_xlat16_cspairs(cs_dos, cs_local);
316 build_iovec_argf(iov, iovlen, "cs_dos", cs_local);
317 error = kiconv_add_xlat16_cspair(cs_local, cs_local,
318 KICONV_FROM_UPPER | KICONV_LOWER);