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