mount_msdosfs.c revision 33749
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 33#ifndef lint 34static const char rcsid[] = 35 "$Id: mount_msdos.c,v 1.11 1998/02/18 09:30:31 jkh Exp $"; 36#endif /* not lint */ 37 38#include <sys/param.h> 39#include <sys/mount.h> 40#include <sys/stat.h> 41 42#include <msdosfs/msdosfsmount.h> 43 44#include <ctype.h> 45#include <err.h> 46#include <grp.h> 47#include <pwd.h> 48#include <stdio.h> 49#include <stdlib.h> 50#include <string.h> 51#include <sysexits.h> 52#include <unistd.h> 53 54#include "mntopts.h" 55 56static struct mntopt mopts[] = { 57 MOPT_STDOPTS, 58 MOPT_FORCE, 59 MOPT_SYNC, 60 MOPT_UPDATE, 61 { NULL } 62}; 63 64static gid_t a_gid __P((char *)); 65static uid_t a_uid __P((char *)); 66static mode_t a_mask __P((char *)); 67static void usage __P((void)) __dead2; 68static void load_u2wtable __P((u_int16_t *, char *)); 69 70int 71main(argc, argv) 72 int argc; 73 char **argv; 74{ 75 struct msdosfs_args args; 76 struct stat sb; 77 int c, error, mntflags, set_gid, set_uid, set_mask; 78 char *dev, *dir, ndir[MAXPATHLEN+1]; 79 struct vfsconf vfc; 80 81 mntflags = set_gid = set_uid = set_mask = 0; 82 (void)memset(&args, '\0', sizeof(args)); 83 args.magic = MSDOSFS_ARGSMAGIC; 84 85 while ((c = getopt(argc, argv, "sl9u:g:m:o:w:")) != -1) { 86 switch (c) { 87#ifdef MSDOSFSMNT_GEMDOSFS 88 case 'G': 89 args.flags |= MSDOSFSMNT_GEMDOSFS; 90 break; 91#endif 92 case 's': 93 args.flags |= MSDOSFSMNT_SHORTNAME; 94 break; 95 case 'l': 96 args.flags |= MSDOSFSMNT_LONGNAME; 97 break; 98 case '9': 99 args.flags |= MSDOSFSMNT_NOWIN95; 100 break; 101 case 'u': 102 args.uid = a_uid(optarg); 103 set_uid = 1; 104 break; 105 case 'g': 106 args.gid = a_gid(optarg); 107 set_gid = 1; 108 break; 109 case 'm': 110 args.mask = a_mask(optarg); 111 set_mask = 1; 112 break; 113 case 'w': 114 load_u2wtable(args.u2w, optarg); 115 args.flags |= MSDOSFSMNT_U2WTABLE; 116 break; 117 case 'o': 118 getmntopts(optarg, mopts, &mntflags, 0); 119 break; 120 case '?': 121 default: 122 usage(); 123 break; 124 } 125 } 126 127 if (optind + 2 != argc) 128 usage(); 129 130 dev = argv[optind]; 131 dir = argv[optind + 1]; 132 if (dir[0] != '/') { 133 warnx("\"%s\" is a relative path", dir); 134 if (getcwd(ndir, sizeof(ndir)) == NULL) 135 err(EX_OSERR, "getcwd"); 136 strncat(ndir, "/", sizeof(ndir) - strlen(ndir) - 1); 137 strncat(ndir, dir, sizeof(ndir) - strlen(ndir) - 1); 138 dir = ndir; 139 warnx("using \"%s\" instead", dir); 140 } 141 142 args.fspec = dev; 143 args.export.ex_root = -2; /* unchecked anyway on DOS fs */ 144 if (mntflags & MNT_RDONLY) 145 args.export.ex_flags = MNT_EXRDONLY; 146 else 147 args.export.ex_flags = 0; 148 if (!set_gid || !set_uid || !set_mask) { 149 if (stat(dir, &sb) == -1) 150 err(EX_OSERR, "stat %s", dir); 151 152 if (!set_uid) 153 args.uid = sb.st_uid; 154 if (!set_gid) 155 args.gid = sb.st_gid; 156 if (!set_mask) 157 args.mask = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO); 158 } 159 160 error = getvfsbyname("msdos", &vfc); 161 if (error && vfsisloadable("msdos")) { 162 if (vfsload("msdos")) 163 err(EX_OSERR, "vfsload(msdos)"); 164 endvfsent(); /* clear cache */ 165 error = getvfsbyname("msdos", &vfc); 166 } 167 if (error) 168 errx(EX_OSERR, "msdos filesystem is not available"); 169 170 if (mount(vfc.vfc_name, dir, mntflags, &args) < 0) 171 err(EX_OSERR, "%s", dev); 172 173 exit (0); 174} 175 176gid_t 177a_gid(s) 178 char *s; 179{ 180 struct group *gr; 181 char *gname; 182 gid_t gid; 183 184 if ((gr = getgrnam(s)) != NULL) 185 gid = gr->gr_gid; 186 else { 187 for (gname = s; *s && isdigit(*s); ++s); 188 if (!*s) 189 gid = atoi(gname); 190 else 191 errx(EX_NOUSER, "unknown group id: %s", gname); 192 } 193 return (gid); 194} 195 196uid_t 197a_uid(s) 198 char *s; 199{ 200 struct passwd *pw; 201 char *uname; 202 uid_t uid; 203 204 if ((pw = getpwnam(s)) != NULL) 205 uid = pw->pw_uid; 206 else { 207 for (uname = s; *s && isdigit(*s); ++s); 208 if (!*s) 209 uid = atoi(uname); 210 else 211 errx(EX_NOUSER, "unknown user id: %s", uname); 212 } 213 return (uid); 214} 215 216mode_t 217a_mask(s) 218 char *s; 219{ 220 int done, rv; 221 char *ep; 222 223 done = 0; 224 rv = -1; 225 if (*s >= '0' && *s <= '7') { 226 done = 1; 227 rv = strtol(optarg, &ep, 8); 228 } 229 if (!done || rv < 0 || *ep) 230 errx(EX_USAGE, "invalid file mode: %s", s); 231 return (rv); 232} 233 234void 235usage() 236{ 237 fprintf(stderr, "usage: mount_msdos [-o options] [-u user] [-g group] [-m mask] [-s] [-l] [-9] [-w table] bdev dir\n"); 238 exit(EX_USAGE); 239} 240 241void 242load_u2wtable (table, name) 243 u_int16_t *table; 244 char *name; 245{ 246 FILE *f; 247 int i, code; 248 char buf[128]; 249 char *fn; 250 251 if (*name == '/') 252 fn = name; 253 else { 254 snprintf(buf, sizeof(buf), "/usr/libdata/msdosfs/%s", name); 255 buf[127] = '\0'; 256 fn = buf; 257 } 258 if ((f = fopen(fn, "r")) == NULL) 259 err(EX_NOINPUT, "%s", fn); 260 for (i = 0; i < 128; i++) { 261 if (fscanf(f, "%i", &code) != 1) 262 errx(EX_DATAERR, "missing item number %d", i); 263 table[i] = code; 264 } 265 fclose(f); 266} 267