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 #ifndef lint 36 static const char rcsid[] = 37 "$FreeBSD$"; 38 #endif /* not lint */ 39 40 #include <sys/param.h> 41 #include <sys/mount.h> 42 #include <sys/stat.h> 43 #include <sys/iconv.h> 44 #include <sys/linker.h> 45 #include <sys/module.h> 46 47 #include <ctype.h> 48 #include <err.h> 49 #include <grp.h> 50 #include <locale.h> 51 #include <pwd.h> 52 #include <stdio.h> 53 /* must be after stdio to declare fparseln */ 54 #include <libutil.h> 55 #include <stdlib.h> 56 #include <string.h> 57 #include <sysexits.h> 58 #include <unistd.h> 59 60 #include "mntopts.h" 61 62 static gid_t a_gid(char *); 63 static uid_t a_uid(char *); 64 static mode_t a_mask(char *); 65 static void usage(void) __dead2; 66 static int set_charset(struct iovec **iov, int *iovlen, const char *, const char *); 67 68 int 69 main(int argc, char **argv) 70 { 71 struct iovec *iov = NULL; 72 int iovlen = 0; 73 struct stat sb; 74 int c, set_gid, set_uid, set_mask, set_dirmask; 75 char *dev, *dir, mntpath[MAXPATHLEN], *csp; 76 char fstype[] = "msdosfs"; 77 char errmsg[255] = {0}; 78 char *cs_dos = NULL; 79 char *cs_local = NULL; 80 mode_t mask = 0, dirmask = 0; 81 uid_t uid = 0; 82 gid_t gid = 0; 83 84 set_gid = set_uid = set_mask = set_dirmask = 0; 85 86 while ((c = getopt(argc, argv, "sl9u:g:m:M:o:L:D:W:")) != -1) { 87 switch (c) { 88 case 's': 89 build_iovec(&iov, &iovlen, "shortnames", NULL, (size_t)-1); 90 break; 91 case 'l': 92 build_iovec(&iov, &iovlen, "longnames", NULL, (size_t)-1); 93 break; 94 case '9': 95 build_iovec_argf(&iov, &iovlen, "nowin95", "", (size_t)-1); 96 break; 97 case 'u': 98 uid = a_uid(optarg); 99 set_uid = 1; 100 break; 101 case 'g': 102 gid = a_gid(optarg); 103 set_gid = 1; 104 break; 105 case 'm': 106 mask = a_mask(optarg); 107 set_mask = 1; 108 break; 109 case 'M': 110 dirmask = a_mask(optarg); 111 set_dirmask = 1; 112 break; 113 case 'L': { 114 const char *quirk = NULL; 115 if (setlocale(LC_CTYPE, optarg) == NULL) 116 err(EX_CONFIG, "%s", optarg); 117 csp = strchr(optarg,'.'); 118 if (!csp) 119 err(EX_CONFIG, "%s", optarg); 120 quirk = kiconv_quirkcs(csp + 1, KICONV_VENDOR_MICSFT); 121 build_iovec_argf(&iov, &iovlen, "cs_local", quirk); 122 cs_local = strdup(quirk); 123 } 124 break; 125 case 'D': 126 cs_dos = strdup(optarg); 127 build_iovec_argf(&iov, &iovlen, "cs_dos", cs_dos, (size_t)-1); 128 break; 129 case 'o': { 130 char *p = NULL; 131 char *val = strdup(""); 132 p = strchr(optarg, '='); 133 if (p != NULL) { 134 free(val); 135 *p = '\0'; 136 val = p + 1; 137 } 138 build_iovec(&iov, &iovlen, optarg, val, (size_t)-1); 139 } 140 break; 141 case 'W': 142 if (strcmp(optarg, "iso22dos") == 0) { 143 cs_local = strdup("ISO8859-2"); 144 cs_dos = strdup("CP852"); 145 } else if (strcmp(optarg, "iso72dos") == 0) { 146 cs_local = strdup("ISO8859-7"); 147 cs_dos = strdup("CP737"); 148 } else if (strcmp(optarg, "koi2dos") == 0) { 149 cs_local = strdup("KOI8-R"); 150 cs_dos = strdup("CP866"); 151 } else if (strcmp(optarg, "koi8u2dos") == 0) { 152 cs_local = strdup("KOI8-U"); 153 cs_dos = strdup("CP866"); 154 } else { 155 err(EX_NOINPUT, "%s", optarg); 156 } 157 build_iovec(&iov, &iovlen, "cs_local", cs_local, (size_t)-1); 158 build_iovec(&iov, &iovlen, "cs_dos", cs_dos, (size_t)-1); 159 break; 160 case '?': 161 default: 162 usage(); 163 break; 164 } 165 } 166 167 if (optind + 2 != argc) 168 usage(); 169 170 if (set_mask && !set_dirmask) { 171 dirmask = mask; 172 set_dirmask = 1; 173 } 174 else if (set_dirmask && !set_mask) { 175 mask = dirmask; 176 set_mask = 1; 177 } 178 179 dev = argv[optind]; 180 dir = argv[optind + 1]; 181 182 if (cs_local != NULL) { 183 if (set_charset(&iov, &iovlen, cs_local, cs_dos) == -1) 184 err(EX_OSERR, "msdosfs_iconv"); 185 build_iovec_argf(&iov, &iovlen, "kiconv", ""); 186 } else if (cs_dos != NULL) { 187 build_iovec_argf(&iov, &iovlen, "cs_local", "ISO8859-1"); 188 if (set_charset(&iov, &iovlen, "ISO8859-1", cs_dos) == -1) 189 err(EX_OSERR, "msdosfs_iconv"); 190 build_iovec_argf(&iov, &iovlen, "kiconv", ""); 191 } 192 193 /* 194 * Resolve the mountpoint with realpath(3) and remove unnecessary 195 * slashes from the devicename if there are any. 196 */ 197 if (checkpath(dir, mntpath) != 0) 198 err(EX_USAGE, "%s", mntpath); 199 (void)rmslashes(dev, dev); 200 201 if (!set_gid || !set_uid || !set_mask) { 202 if (stat(mntpath, &sb) == -1) 203 err(EX_OSERR, "stat %s", mntpath); 204 205 if (!set_uid) 206 uid = sb.st_uid; 207 if (!set_gid) 208 gid = sb.st_gid; 209 if (!set_mask) 210 mask = dirmask = 211 sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO); 212 } 213 214 build_iovec(&iov, &iovlen, "fstype", fstype, (size_t)-1); 215 build_iovec(&iov, &iovlen, "fspath", mntpath, (size_t)-1); 216 build_iovec(&iov, &iovlen, "from", dev, (size_t)-1); 217 build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg)); 218 build_iovec_argf(&iov, &iovlen, "uid", "%d", uid); 219 build_iovec_argf(&iov, &iovlen, "gid", "%u", gid); 220 build_iovec_argf(&iov, &iovlen, "mask", "%u", mask); 221 build_iovec_argf(&iov, &iovlen, "dirmask", "%u", dirmask); 222 223 if (nmount(iov, iovlen, 0) < 0) { 224 if (errmsg[0]) 225 err(1, "%s: %s", dev, errmsg); 226 else 227 err(1, "%s", dev); 228 } 229 230 exit (0); 231 } 232 233 gid_t 234 a_gid(char *s) 235 { 236 struct group *gr; 237 char *gname; 238 gid_t gid; 239 240 if ((gr = getgrnam(s)) != NULL) 241 gid = gr->gr_gid; 242 else { 243 for (gname = s; *s && isdigit(*s); ++s); 244 if (!*s) 245 gid = atoi(gname); 246 else 247 errx(EX_NOUSER, "unknown group id: %s", gname); 248 } 249 return (gid); 250 } 251 252 uid_t 253 a_uid(char *s) 254 { 255 struct passwd *pw; 256 char *uname; 257 uid_t uid; 258 259 if ((pw = getpwnam(s)) != NULL) 260 uid = pw->pw_uid; 261 else { 262 for (uname = s; *s && isdigit(*s); ++s); 263 if (!*s) 264 uid = atoi(uname); 265 else 266 errx(EX_NOUSER, "unknown user id: %s", uname); 267 } 268 return (uid); 269 } 270 271 mode_t 272 a_mask(char *s) 273 { 274 int done, rv; 275 char *ep; 276 277 done = 0; 278 rv = -1; 279 if (*s >= '0' && *s <= '7') { 280 done = 1; 281 rv = strtol(optarg, &ep, 8); 282 } 283 if (!done || rv < 0 || *ep) 284 errx(EX_USAGE, "invalid file mode: %s", s); 285 return (rv); 286 } 287 288 void 289 usage(void) 290 { 291 fprintf(stderr, "%s\n%s\n%s\n", 292 "usage: mount_msdosfs [-9ls] [-D DOS_codepage] [-g gid] [-L locale]", 293 " [-M mask] [-m mask] [-o options] [-u uid]", 294 " [-W table] special node"); 295 exit(EX_USAGE); 296 } 297 298 int 299 set_charset(struct iovec **iov, int *iovlen, const char *cs_local, const char *cs_dos) 300 { 301 int error; 302 303 if (modfind("msdosfs_iconv") < 0) 304 if (kldload("msdosfs_iconv") < 0 || modfind("msdosfs_iconv") < 0) { 305 warnx("cannot find or load \"msdosfs_iconv\" kernel module"); 306 return (-1); 307 } 308 309 build_iovec_argf(iov, iovlen, "cs_win", ENCODING_UNICODE); 310 error = kiconv_add_xlat16_cspairs(ENCODING_UNICODE, cs_local); 311 if (error) 312 return (-1); 313 if (cs_dos != NULL) { 314 error = kiconv_add_xlat16_cspairs(cs_dos, cs_local); 315 if (error) 316 return (-1); 317 } else { 318 build_iovec_argf(iov, iovlen, "cs_dos", cs_local); 319 error = kiconv_add_xlat16_cspair(cs_local, cs_local, 320 KICONV_FROM_UPPER | KICONV_LOWER); 321 if (error) 322 return (-1); 323 } 324 325 return (0); 326 } 327