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