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