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