mount_msdosfs.c revision 28731
1/* 2 * Copyright (c) 1994 Christopher G. Demetriou 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by Christopher G. Demetriou. 16 * 4. The name of the author may not be used to endorse or promote products 17 * derived from this software without specific prior written permission 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31#ifndef lint 32static const char rcsid[] = 33 "$Id: mount_msdos.c,v 1.9 1997/03/29 03:32:25 imp Exp $"; 34#endif /* not lint */ 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 <pwd.h> 46#include <stdio.h> 47#include <stdlib.h> 48#include <string.h> 49#include <sysexits.h> 50#include <unistd.h> 51 52#include "mntopts.h" 53 54static struct mntopt mopts[] = { 55 MOPT_STDOPTS, 56 MOPT_FORCE, 57 MOPT_SYNC, 58 MOPT_UPDATE, 59 { NULL } 60}; 61 62static gid_t a_gid __P((char *)); 63static uid_t a_uid __P((char *)); 64static mode_t a_mask __P((char *)); 65static void usage __P((void)) __dead2; 66 67int 68main(argc, argv) 69 int argc; 70 char **argv; 71{ 72 struct msdosfs_args args; 73 struct stat sb; 74 int c, error, mntflags, set_gid, set_uid, set_mask; 75 char *dev, *dir, ndir[MAXPATHLEN+1]; 76 struct vfsconf vfc; 77 78 mntflags = set_gid = set_uid = set_mask = 0; 79 (void)memset(&args, '\0', sizeof(args)); 80 81 while ((c = getopt(argc, argv, "u:g:m:o:")) != -1) { 82 switch (c) { 83 case 'u': 84 args.uid = a_uid(optarg); 85 set_uid = 1; 86 break; 87 case 'g': 88 args.gid = a_gid(optarg); 89 set_gid = 1; 90 break; 91 case 'm': 92 args.mask = a_mask(optarg); 93 set_mask = 1; 94 break; 95 case 'o': 96 getmntopts(optarg, mopts, &mntflags, 0); 97 break; 98 case '?': 99 default: 100 usage(); 101 break; 102 } 103 } 104 105 if (optind + 2 != argc) 106 usage(); 107 108 dev = argv[optind]; 109 dir = argv[optind + 1]; 110 if (dir[0] != '/') { 111 warnx("\"%s\" is a relative path", dir); 112 if (getcwd(ndir, sizeof(ndir)) == NULL) 113 err(EX_OSERR, "getcwd"); 114 strncat(ndir, "/", sizeof(ndir) - strlen(ndir) - 1); 115 strncat(ndir, dir, sizeof(ndir) - strlen(ndir) - 1); 116 dir = ndir; 117 warnx("using \"%s\" instead", dir); 118 } 119 120 args.fspec = dev; 121 args.export.ex_root = -2; /* unchecked anyway on DOS fs */ 122 if (mntflags & MNT_RDONLY) 123 args.export.ex_flags = MNT_EXRDONLY; 124 else 125 args.export.ex_flags = 0; 126 if (!set_gid || !set_uid || !set_mask) { 127 if (stat(dir, &sb) == -1) 128 err(EX_OSERR, "stat %s", dir); 129 130 if (!set_uid) 131 args.uid = sb.st_uid; 132 if (!set_gid) 133 args.gid = sb.st_gid; 134 if (!set_mask) 135 args.mask = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO); 136 } 137 138 error = getvfsbyname("msdos", &vfc); 139 if (error && vfsisloadable("msdos")) { 140 if (vfsload("msdos")) 141 err(EX_OSERR, "vfsload(msdos)"); 142 endvfsent(); /* clear cache */ 143 error = getvfsbyname("msdos", &vfc); 144 } 145 if (error) 146 errx(EX_OSERR, "msdos filesystem is not available"); 147 148 if (mount(vfc.vfc_name, dir, mntflags, &args) < 0) 149 err(EX_OSERR, "%s", dev); 150 151 exit (0); 152} 153 154gid_t 155a_gid(s) 156 char *s; 157{ 158 struct group *gr; 159 char *gname; 160 gid_t gid; 161 162 if ((gr = getgrnam(s)) != NULL) 163 gid = gr->gr_gid; 164 else { 165 for (gname = s; *s && isdigit(*s); ++s); 166 if (!*s) 167 gid = atoi(gname); 168 else 169 errx(EX_NOUSER, "unknown group id: %s", gname); 170 } 171 return (gid); 172} 173 174uid_t 175a_uid(s) 176 char *s; 177{ 178 struct passwd *pw; 179 char *uname; 180 uid_t uid; 181 182 if ((pw = getpwnam(s)) != NULL) 183 uid = pw->pw_uid; 184 else { 185 for (uname = s; *s && isdigit(*s); ++s); 186 if (!*s) 187 uid = atoi(uname); 188 else 189 errx(EX_NOUSER, "unknown user id: %s", uname); 190 } 191 return (uid); 192} 193 194mode_t 195a_mask(s) 196 char *s; 197{ 198 int done, rv; 199 char *ep; 200 201 done = 0; 202 if (*s >= '0' && *s <= '7') { 203 done = 1; 204 rv = strtol(optarg, &ep, 8); 205 } 206 if (!done || rv < 0 || *ep) 207 errx(EX_USAGE, "invalid file mode: %s", s); 208 return (rv); 209} 210 211void 212usage() 213{ 214 fprintf(stderr, "usage: mount_msdos [-F flags] [-u user] [-g group] [-m mask] bdev dir\n"); 215 exit(EX_USAGE); 216} 217