Home | History | Annotate | Line # | Download | only in mount_msdos
mount_msdos.c revision 1.20
      1 /*	$NetBSD: mount_msdos.c,v 1.20 1999/06/25 19:28:36 perseant 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.20 1999/06/25 19:28:36 perseant 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_UPDATE,
     57 	{ NULL }
     58 };
     59 
     60 gid_t	a_gid __P((char *));
     61 uid_t	a_uid __P((char *));
     62 mode_t	a_mask __P((char *));
     63 int	main __P((int, char *[]));
     64 void	usage __P((void));
     65 
     66 int
     67 main(argc, argv)
     68 	int argc;
     69 	char **argv;
     70 {
     71 	struct msdosfs_args args;
     72 	struct stat sb;
     73 	int c, mntflags, set_gid, set_uid, set_mask;
     74 	char *dev, *dir, ndir[MAXPATHLEN+1];
     75 
     76 	mntflags = set_gid = set_uid = set_mask = 0;
     77 	(void)memset(&args, '\0', sizeof(args));
     78 
     79 	while ((c = getopt(argc, argv, "Gsl9u:g:m:o:")) != -1) {
     80 		switch (c) {
     81 		case 'G':
     82 			args.flags |= MSDOSFSMNT_GEMDOSFS;
     83 			break;
     84 		case 's':
     85 			args.flags |= MSDOSFSMNT_SHORTNAME;
     86 			break;
     87 		case 'l':
     88 			args.flags |= MSDOSFSMNT_LONGNAME;
     89 			break;
     90 		case '9':
     91 			args.flags |= MSDOSFSMNT_NOWIN95;
     92 			break;
     93 		case 'u':
     94 			args.uid = a_uid(optarg);
     95 			set_uid = 1;
     96 			break;
     97 		case 'g':
     98 			args.gid = a_gid(optarg);
     99 			set_gid = 1;
    100 			break;
    101 		case 'm':
    102 			args.mask = a_mask(optarg);
    103 			set_mask = 1;
    104 			break;
    105 		case 'o':
    106 			getmntopts(optarg, mopts, &mntflags, 0);
    107 			break;
    108 		case '?':
    109 		default:
    110 			usage();
    111 			break;
    112 		}
    113 	}
    114 
    115 	if (optind + 2 != argc)
    116 		usage();
    117 
    118 	dev = argv[optind];
    119 	dir = argv[optind + 1];
    120 	if (dir[0] != '/') {
    121 		warnx("\"%s\" is a relative path.", dir);
    122 		if (getcwd(ndir, sizeof(ndir)) == NULL)
    123 			err(1, "getcwd");
    124 		strncat(ndir, "/", sizeof(ndir) - strlen(ndir) - 1);
    125 		strncat(ndir, dir, sizeof(ndir) - strlen(ndir) - 1);
    126 		dir = ndir;
    127 		warnx("using \"%s\" instead.", dir);
    128 	}
    129 
    130 	args.fspec = dev;
    131 	args.export.ex_root = -2;	/* unchecked anyway on DOS fs */
    132 	if (mntflags & MNT_RDONLY)
    133 		args.export.ex_flags = MNT_EXRDONLY;
    134 	else
    135 		args.export.ex_flags = 0;
    136 	if (!set_gid || !set_uid || !set_mask) {
    137 		if (stat(dir, &sb) == -1)
    138 			err(1, "stat %s", dir);
    139 
    140 		if (!set_uid)
    141 			args.uid = sb.st_uid;
    142 		if (!set_gid)
    143 			args.gid = sb.st_gid;
    144 		if (!set_mask)
    145 			args.mask = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
    146 	}
    147 
    148 	if (mount(MOUNT_MSDOS, dir, mntflags, &args) < 0)
    149 		err(1, "%s on %s", dev, dir);
    150 
    151 	exit (0);
    152 }
    153 
    154 gid_t
    155 a_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(1, "unknown group id: %s", gname);
    170 	}
    171 	return (gid);
    172 }
    173 
    174 uid_t
    175 a_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(1, "unknown user id: %s", uname);
    190 	}
    191 	return (uid);
    192 }
    193 
    194 mode_t
    195 a_mask(s)
    196 	char *s;
    197 {
    198 	int done, rv;
    199 	char *ep;
    200 
    201 	done = 0;
    202 	rv = -1;
    203 	if (*s >= '0' && *s <= '7') {
    204 		done = 1;
    205 		rv = strtol(optarg, &ep, 8);
    206 	}
    207 	if (!done || rv < 0 || *ep)
    208 		errx(1, "invalid file mode: %s", s);
    209 	return (rv);
    210 }
    211 
    212 void
    213 usage()
    214 {
    215 
    216 	fprintf(stderr, "usage: mount_msdos [-o options] [-u user] [-g group] [-m mask] bdev dir\n");
    217 	exit(1);
    218 }
    219