1 /* $NetBSD: mount_msdos.c,v 1.18 1997/09/16 12:24:18 lukem Exp $ */ 2 3 /* 4 * Copyright (c) 1994 Christopher G. Demetriou 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 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 20 * 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. 31 * 32 * $FreeBSD: src/sbin/mount_msdos/mount_msdos.c,v 1.19.2.1 2000/07/20 10:35:13 kris Exp $ 33 * $DragonFly: src/sbin/mount_msdos/mount_msdos.c,v 1.2 2003/06/17 04:27:33 dillon Exp $ 34 */ 35 36 #include <sys/param.h> 37 #include <sys/mount.h> 38 #include <sys/stat.h> 39 40 #include <msdosfs/msdosfsmount.h> 41 42 #include <ctype.h> 43 #include <err.h> 44 #include <grp.h> 45 #include <locale.h> 46 #include <pwd.h> 47 #include <stdio.h> 48 /* must be after stdio to declare fparseln */ 49 #include <libutil.h> 50 #include <stdlib.h> 51 #include <string.h> 52 #include <sysexits.h> 53 #include <unistd.h> 54 55 #include "mntopts.h" 56 57 /* 58 * XXX - no way to specify "foo=<bar>"-type options; that's what we'd 59 * want for "-u", "-g", "-m", "-L", and "-W". 60 */ 61 static struct mntopt mopts[] = { 62 MOPT_STDOPTS, 63 MOPT_FORCE, 64 MOPT_SYNC, 65 MOPT_UPDATE, 66 #ifdef MSDOSFSMNT_GEMDOSFS 67 { "gemdosfs", 0, MSDOSFSMNT_GEMDOSFS, 1 }, 68 #endif 69 { "shortnames", 0, MSDOSFSMNT_SHORTNAME, 1 }, 70 { "longnames", 0, MSDOSFSMNT_LONGNAME, 1 }, 71 { "nowin95", 0, MSDOSFSMNT_NOWIN95, 1 }, 72 { NULL } 73 }; 74 75 static gid_t a_gid __P((char *)); 76 static uid_t a_uid __P((char *)); 77 static mode_t a_mask __P((char *)); 78 static void usage __P((void)) __dead2; 79 static void load_u2wtable __P((struct msdosfs_args *, char *)); 80 static void load_ultable __P((struct msdosfs_args *, char *)); 81 82 int 83 main(argc, argv) 84 int argc; 85 char **argv; 86 { 87 struct msdosfs_args args; 88 struct stat sb; 89 int c, error, mntflags, set_gid, set_uid, set_mask; 90 char *dev, *dir, mntpath[MAXPATHLEN]; 91 struct vfsconf vfc; 92 93 mntflags = set_gid = set_uid = set_mask = 0; 94 (void)memset(&args, '\0', sizeof(args)); 95 args.magic = MSDOSFS_ARGSMAGIC; 96 97 while ((c = getopt(argc, argv, "sl9u:g:m:o:L:W:")) != -1) { 98 switch (c) { 99 #ifdef MSDOSFSMNT_GEMDOSFS 100 case 'G': 101 args.flags |= MSDOSFSMNT_GEMDOSFS; 102 break; 103 #endif 104 case 's': 105 args.flags |= MSDOSFSMNT_SHORTNAME; 106 break; 107 case 'l': 108 args.flags |= MSDOSFSMNT_LONGNAME; 109 break; 110 case '9': 111 args.flags |= MSDOSFSMNT_NOWIN95; 112 break; 113 case 'u': 114 args.uid = a_uid(optarg); 115 set_uid = 1; 116 break; 117 case 'g': 118 args.gid = a_gid(optarg); 119 set_gid = 1; 120 break; 121 case 'm': 122 args.mask = a_mask(optarg); 123 set_mask = 1; 124 break; 125 case 'L': 126 load_ultable(&args, optarg); 127 args.flags |= MSDOSFSMNT_ULTABLE; 128 break; 129 case 'W': 130 load_u2wtable(&args, optarg); 131 args.flags |= MSDOSFSMNT_U2WTABLE; 132 break; 133 case 'o': 134 getmntopts(optarg, mopts, &mntflags, &args.flags); 135 break; 136 case '?': 137 default: 138 usage(); 139 break; 140 } 141 } 142 143 if (optind + 2 != argc) 144 usage(); 145 146 dev = argv[optind]; 147 dir = argv[optind + 1]; 148 149 /* 150 * Resolve the mountpoint with realpath(3) and remove unnecessary 151 * slashes from the devicename if there are any. 152 */ 153 (void)checkpath(dir, mntpath); 154 (void)rmslashes(dev, dev); 155 156 args.fspec = dev; 157 args.export.ex_root = -2; /* unchecked anyway on DOS fs */ 158 if (mntflags & MNT_RDONLY) 159 args.export.ex_flags = MNT_EXRDONLY; 160 else 161 args.export.ex_flags = 0; 162 if (!set_gid || !set_uid || !set_mask) { 163 if (stat(mntpath, &sb) == -1) 164 err(EX_OSERR, "stat %s", mntpath); 165 166 if (!set_uid) 167 args.uid = sb.st_uid; 168 if (!set_gid) 169 args.gid = sb.st_gid; 170 if (!set_mask) 171 args.mask = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO); 172 } 173 174 error = getvfsbyname("msdos", &vfc); 175 if (error && vfsisloadable("msdos")) { 176 if (vfsload("msdos")) 177 err(EX_OSERR, "vfsload(msdos)"); 178 endvfsent(); /* clear cache */ 179 error = getvfsbyname("msdos", &vfc); 180 } 181 if (error) 182 errx(EX_OSERR, "msdos filesystem is not available"); 183 184 if (mount(vfc.vfc_name, mntpath, mntflags, &args) < 0) 185 err(EX_OSERR, "%s", dev); 186 187 exit (0); 188 } 189 190 gid_t 191 a_gid(s) 192 char *s; 193 { 194 struct group *gr; 195 char *gname; 196 gid_t gid; 197 198 if ((gr = getgrnam(s)) != NULL) 199 gid = gr->gr_gid; 200 else { 201 for (gname = s; *s && isdigit(*s); ++s); 202 if (!*s) 203 gid = atoi(gname); 204 else 205 errx(EX_NOUSER, "unknown group id: %s", gname); 206 } 207 return (gid); 208 } 209 210 uid_t 211 a_uid(s) 212 char *s; 213 { 214 struct passwd *pw; 215 char *uname; 216 uid_t uid; 217 218 if ((pw = getpwnam(s)) != NULL) 219 uid = pw->pw_uid; 220 else { 221 for (uname = s; *s && isdigit(*s); ++s); 222 if (!*s) 223 uid = atoi(uname); 224 else 225 errx(EX_NOUSER, "unknown user id: %s", uname); 226 } 227 return (uid); 228 } 229 230 mode_t 231 a_mask(s) 232 char *s; 233 { 234 int done, rv; 235 char *ep; 236 237 done = 0; 238 rv = -1; 239 if (*s >= '0' && *s <= '7') { 240 done = 1; 241 rv = strtol(optarg, &ep, 8); 242 } 243 if (!done || rv < 0 || *ep) 244 errx(EX_USAGE, "invalid file mode: %s", s); 245 return (rv); 246 } 247 248 void 249 usage() 250 { 251 fprintf(stderr, "%s\n%s\n", 252 "usage: mount_msdos [-o options] [-u user] [-g group] [-m mask]", 253 " [-s] [-l] [-9] [-L locale] [-W table] bdev dir"); 254 exit(EX_USAGE); 255 } 256 257 void 258 load_u2wtable (pargs, name) 259 struct msdosfs_args *pargs; 260 char *name; 261 { 262 FILE *f; 263 int i, j, code[8]; 264 size_t line = 0; 265 char buf[128]; 266 char *fn, *s, *p; 267 268 if (*name == '/') 269 fn = name; 270 else { 271 snprintf(buf, sizeof(buf), "/usr/libdata/msdosfs/%s", name); 272 buf[127] = '\0'; 273 fn = buf; 274 } 275 if ((f = fopen(fn, "r")) == NULL) 276 err(EX_NOINPUT, "%s", fn); 277 p = NULL; 278 for (i = 0; i < 16; i++) { 279 do { 280 if (p != NULL) free(p); 281 if ((p = s = fparseln(f, NULL, &line, NULL, 0)) == NULL) 282 errx(EX_DATAERR, "can't read u2w table row %d near line %d", i, line); 283 while (isspace((unsigned char)*s)) 284 s++; 285 } while (*s == '\0'); 286 if (sscanf(s, "%i%i%i%i%i%i%i%i", 287 code, code + 1, code + 2, code + 3, code + 4, code + 5, code + 6, code + 7) != 8) 288 errx(EX_DATAERR, "u2w table: missing item(s) in row %d, line %d", i, line); 289 for (j = 0; j < 8; j++) 290 pargs->u2w[i * 8 + j] = code[j]; 291 } 292 for (i = 0; i < 16; i++) { 293 do { 294 free(p); 295 if ((p = s = fparseln(f, NULL, &line, NULL, 0)) == NULL) 296 errx(EX_DATAERR, "can't read d2u table row %d near line %d", i, line); 297 while (isspace((unsigned char)*s)) 298 s++; 299 } while (*s == '\0'); 300 if (sscanf(s, "%i%i%i%i%i%i%i%i", 301 code, code + 1, code + 2, code + 3, code + 4, code + 5, code + 6, code + 7) != 8) 302 errx(EX_DATAERR, "d2u table: missing item(s) in row %d, line %d", i, line); 303 for (j = 0; j < 8; j++) 304 pargs->d2u[i * 8 + j] = code[j]; 305 } 306 for (i = 0; i < 16; i++) { 307 do { 308 free(p); 309 if ((p = s = fparseln(f, NULL, &line, NULL, 0)) == NULL) 310 errx(EX_DATAERR, "can't read u2d table row %d near line %d", i, line); 311 while (isspace((unsigned char)*s)) 312 s++; 313 } while (*s == '\0'); 314 if (sscanf(s, "%i%i%i%i%i%i%i%i", 315 code, code + 1, code + 2, code + 3, code + 4, code + 5, code + 6, code + 7) != 8) 316 errx(EX_DATAERR, "u2d table: missing item(s) in row %d, line %d", i, line); 317 for (j = 0; j < 8; j++) 318 pargs->u2d[i * 8 + j] = code[j]; 319 } 320 free(p); 321 fclose(f); 322 } 323 324 void 325 load_ultable (pargs, name) 326 struct msdosfs_args *pargs; 327 char *name; 328 { 329 int i; 330 331 if (setlocale(LC_CTYPE, name) == NULL) 332 err(EX_CONFIG, "%s", name); 333 for (i = 0; i < 128; i++) { 334 pargs->ul[i] = tolower(i | 0x80); 335 pargs->lu[i] = toupper(i | 0x80); 336 } 337 } 338