Home | History | Annotate | Line # | Download | only in mount_msdos
mount_msdos.c revision 1.23
      1 /* $NetBSD: mount_msdos.c,v 1.23 2000/06/14 17:25:27 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 for the
     18  *          NetBSD Project.  See http://www.netbsd.org/ for
     19  *          information about NetBSD.
     20  * 4. The name of the author may not be used to endorse or promote products
     21  *    derived from this software without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  *
     34  * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 #ifndef lint
     39 __RCSID("$NetBSD: mount_msdos.c,v 1.23 2000/06/14 17:25:27 cgd Exp $");
     40 #endif /* not lint */
     41 
     42 #include <sys/cdefs.h>
     43 #include <sys/param.h>
     44 #include <sys/mount.h>
     45 #include <sys/stat.h>
     46 #include <msdosfs/msdosfsmount.h>
     47 #include <ctype.h>
     48 #include <err.h>
     49 #include <grp.h>
     50 #include <pwd.h>
     51 #include <stdio.h>
     52 #include <stdlib.h>
     53 #include <string.h>
     54 #include <unistd.h>
     55 
     56 #include "mntopts.h"
     57 
     58 const struct mntopt mopts[] = {
     59 	MOPT_STDOPTS,
     60 	MOPT_ASYNC,
     61 	MOPT_SYNC,
     62 	MOPT_UPDATE,
     63 	{ NULL }
     64 };
     65 
     66 gid_t	a_gid __P((char *));
     67 uid_t	a_uid __P((char *));
     68 mode_t	a_mask __P((char *));
     69 int	main __P((int, char *[]));
     70 void	usage __P((void));
     71 
     72 int
     73 main(argc, argv)
     74 	int argc;
     75 	char **argv;
     76 {
     77 	struct msdosfs_args args;
     78 	struct stat sb;
     79 	int c, mntflags, set_gid, set_uid, set_mask;
     80 	char *dev, *dir, ndir[MAXPATHLEN+1];
     81 
     82 	mntflags = set_gid = set_uid = set_mask = 0;
     83 	(void)memset(&args, '\0', sizeof(args));
     84 
     85 	while ((c = getopt(argc, argv, "Gsl9u:g:m:o:")) != -1) {
     86 		switch (c) {
     87 		case 'G':
     88 			args.flags |= MSDOSFSMNT_GEMDOSFS;
     89 			break;
     90 		case 's':
     91 			args.flags |= MSDOSFSMNT_SHORTNAME;
     92 			break;
     93 		case 'l':
     94 			args.flags |= MSDOSFSMNT_LONGNAME;
     95 			break;
     96 		case '9':
     97 			args.flags |= MSDOSFSMNT_NOWIN95;
     98 			break;
     99 		case 'u':
    100 			args.uid = a_uid(optarg);
    101 			set_uid = 1;
    102 			break;
    103 		case 'g':
    104 			args.gid = a_gid(optarg);
    105 			set_gid = 1;
    106 			break;
    107 		case 'm':
    108 			args.mask = a_mask(optarg);
    109 			set_mask = 1;
    110 			break;
    111 		case 'o':
    112 			getmntopts(optarg, mopts, &mntflags, 0);
    113 			break;
    114 		case '?':
    115 		default:
    116 			usage();
    117 			break;
    118 		}
    119 	}
    120 
    121 	if (optind + 2 != argc)
    122 		usage();
    123 
    124 	dev = argv[optind];
    125 	dir = argv[optind + 1];
    126 	if (dir[0] != '/') {
    127 		warnx("\"%s\" is a relative path.", dir);
    128 		if (getcwd(ndir, sizeof(ndir)) == NULL)
    129 			err(1, "getcwd");
    130 		strncat(ndir, "/", sizeof(ndir) - strlen(ndir) - 1);
    131 		strncat(ndir, dir, sizeof(ndir) - strlen(ndir) - 1);
    132 		dir = ndir;
    133 		warnx("using \"%s\" instead.", dir);
    134 	}
    135 
    136 	args.fspec = dev;
    137 	args.export.ex_root = -2;	/* unchecked anyway on DOS fs */
    138 	if (mntflags & MNT_RDONLY)
    139 		args.export.ex_flags = MNT_EXRDONLY;
    140 	else
    141 		args.export.ex_flags = 0;
    142 	if (!set_gid || !set_uid || !set_mask) {
    143 		if (stat(dir, &sb) == -1)
    144 			err(1, "stat %s", dir);
    145 
    146 		if (!set_uid)
    147 			args.uid = sb.st_uid;
    148 		if (!set_gid)
    149 			args.gid = sb.st_gid;
    150 		if (!set_mask)
    151 			args.mask = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
    152 	}
    153 
    154 	if (mount(MOUNT_MSDOS, dir, mntflags, &args) < 0)
    155 		err(1, "%s on %s", dev, dir);
    156 
    157 	exit (0);
    158 }
    159 
    160 gid_t
    161 a_gid(s)
    162 	char *s;
    163 {
    164 	struct group *gr;
    165 	char *gname;
    166 	gid_t gid;
    167 
    168 	if ((gr = getgrnam(s)) != NULL)
    169 		gid = gr->gr_gid;
    170 	else {
    171 		for (gname = s; *s && isdigit(*s); ++s);
    172 		if (!*s)
    173 			gid = atoi(gname);
    174 		else
    175 			errx(1, "unknown group id: %s", gname);
    176 	}
    177 	return (gid);
    178 }
    179 
    180 uid_t
    181 a_uid(s)
    182 	char *s;
    183 {
    184 	struct passwd *pw;
    185 	char *uname;
    186 	uid_t uid;
    187 
    188 	if ((pw = getpwnam(s)) != NULL)
    189 		uid = pw->pw_uid;
    190 	else {
    191 		for (uname = s; *s && isdigit(*s); ++s);
    192 		if (!*s)
    193 			uid = atoi(uname);
    194 		else
    195 			errx(1, "unknown user id: %s", uname);
    196 	}
    197 	return (uid);
    198 }
    199 
    200 mode_t
    201 a_mask(s)
    202 	char *s;
    203 {
    204 	int done, rv;
    205 	char *ep;
    206 
    207 	done = 0;
    208 	rv = -1;
    209 	if (*s >= '0' && *s <= '7') {
    210 		done = 1;
    211 		rv = strtol(optarg, &ep, 8);
    212 	}
    213 	if (!done || rv < 0 || *ep)
    214 		errx(1, "invalid file mode: %s", s);
    215 	return (rv);
    216 }
    217 
    218 void
    219 usage()
    220 {
    221 
    222 	fprintf(stderr, "usage: mount_msdos [-o options] [-u user] [-g group] [-m mask] bdev dir\n");
    223 	exit(1);
    224 }
    225