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