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