mount_msdosfs.c revision 22990
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$"; 34#endif /* not lint */ 35 36#include <sys/cdefs.h> 37#include <sys/param.h> 38#define MSDOSFS 39#include <sys/mount.h> 40#include <sys/stat.h> 41#include <ctype.h> 42#include <err.h> 43#include <grp.h> 44#include <pwd.h> 45#include <stdio.h> 46#include <stdlib.h> 47#include <string.h> 48#include <sysexits.h> 49#include <unistd.h> 50 51#include "mntopts.h" 52 53static struct mntopt mopts[] = { 54 MOPT_STDOPTS, 55 { NULL } 56}; 57 58static gid_t a_gid __P((char *)); 59static uid_t a_uid __P((char *)); 60static mode_t a_mask __P((char *)); 61static void usage __P((void)) __dead2; 62 63int 64main(argc, argv) 65 int argc; 66 char **argv; 67{ 68 struct msdosfs_args args; 69 struct stat sb; 70 int c, mntflags, set_gid, set_uid, set_mask; 71 char *dev, *dir, ndir[MAXPATHLEN+1]; 72 struct vfsconf *vfc; 73 74 mntflags = set_gid = set_uid = set_mask = 0; 75 (void)memset(&args, '\0', sizeof(args)); 76 77 while ((c = getopt(argc, argv, "u:g:m:o:")) != EOF) { 78 switch (c) { 79 case 'u': 80 args.uid = a_uid(optarg); 81 set_uid = 1; 82 break; 83 case 'g': 84 args.gid = a_gid(optarg); 85 set_gid = 1; 86 break; 87 case 'm': 88 args.mask = a_mask(optarg); 89 set_mask = 1; 90 break; 91 case 'o': 92 getmntopts(optarg, mopts, &mntflags, 0); 93 break; 94 case '?': 95 default: 96 usage(); 97 break; 98 } 99 } 100 101 if (optind + 2 != argc) 102 usage(); 103 104 dev = argv[optind]; 105 dir = argv[optind + 1]; 106 if (dir[0] != '/') { 107 warnx("\"%s\" is a relative path", dir); 108 if (getcwd(ndir, sizeof(ndir)) == NULL) 109 err(EX_OSERR, "getcwd"); 110 strncat(ndir, "/", sizeof(ndir) - strlen(ndir) - 1); 111 strncat(ndir, dir, sizeof(ndir) - strlen(ndir) - 1); 112 dir = ndir; 113 warnx("using \"%s\" instead", dir); 114 } 115 116 args.fspec = dev; 117 args.export.ex_root = -2; /* unchecked anyway on DOS fs */ 118 if (mntflags & MNT_RDONLY) 119 args.export.ex_flags = MNT_EXRDONLY; 120 else 121 args.export.ex_flags = 0; 122 if (!set_gid || !set_uid || !set_mask) { 123 if (stat(dir, &sb) == -1) 124 err(EX_OSERR, "stat %s", dir); 125 126 if (!set_uid) 127 args.uid = sb.st_uid; 128 if (!set_gid) 129 args.gid = sb.st_gid; 130 if (!set_mask) 131 args.mask = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO); 132 } 133 134 vfc = getvfsbyname("msdos"); 135 if(!vfc && vfsisloadable("msdos")) { 136 if(vfsload("msdos")) 137 err(EX_OSERR, "vfsload(msdos)"); 138 endvfsent(); /* clear cache */ 139 vfc = getvfsbyname("msdos"); 140 } 141 if (!vfc) 142 errx(EX_OSERR, "msdos filesystem is not available"); 143 144 if (mount(vfc->vfc_index, dir, mntflags, &args) < 0) 145 err(EX_OSERR, "%s", dev); 146 147 exit (0); 148} 149 150gid_t 151a_gid(s) 152 char *s; 153{ 154 struct group *gr; 155 char *gname; 156 gid_t gid; 157 158 if ((gr = getgrnam(s)) != NULL) 159 gid = gr->gr_gid; 160 else { 161 for (gname = s; *s && isdigit(*s); ++s); 162 if (!*s) 163 gid = atoi(gname); 164 else 165 errx(EX_NOUSER, "unknown group id: %s", gname); 166 } 167 return (gid); 168} 169 170uid_t 171a_uid(s) 172 char *s; 173{ 174 struct passwd *pw; 175 char *uname; 176 uid_t uid; 177 178 if ((pw = getpwnam(s)) != NULL) 179 uid = pw->pw_uid; 180 else { 181 for (uname = s; *s && isdigit(*s); ++s); 182 if (!*s) 183 uid = atoi(uname); 184 else 185 errx(EX_NOUSER, "unknown user id: %s", uname); 186 } 187 return (uid); 188} 189 190mode_t 191a_mask(s) 192 char *s; 193{ 194 int done, rv; 195 char *ep; 196 197 done = 0; 198 if (*s >= '0' && *s <= '7') { 199 done = 1; 200 rv = strtol(optarg, &ep, 8); 201 } 202 if (!done || rv < 0 || *ep) 203 errx(EX_USAGE, "invalid file mode: %s", s); 204 return (rv); 205} 206 207void 208usage() 209{ 210 fprintf(stderr, "usage: mount_msdos [-F flags] [-u user] [-g group] [-m mask] bdev dir\n"); 211 exit(EX_USAGE); 212} 213