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