Home | History | Annotate | Line # | Download | only in mount_ados
mount_ados.c revision 1.8.2.1
      1  1.8.2.1       he /*	$NetBSD: mount_ados.c,v 1.8.2.1 1999/09/05 15:03:45 he Exp $	*/
      2      1.4      cgd 
      3      1.1   chopps /*
      4      1.1   chopps  * Copyright (c) 1994 Christopher G. Demetriou
      5      1.1   chopps  * All rights reserved.
      6      1.1   chopps  *
      7      1.1   chopps  * Redistribution and use in source and binary forms, with or without
      8      1.1   chopps  * modification, are permitted provided that the following conditions
      9      1.1   chopps  * are met:
     10      1.1   chopps  * 1. Redistributions of source code must retain the above copyright
     11      1.1   chopps  *    notice, this list of conditions and the following disclaimer.
     12      1.1   chopps  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1   chopps  *    notice, this list of conditions and the following disclaimer in the
     14      1.1   chopps  *    documentation and/or other materials provided with the distribution.
     15      1.1   chopps  * 3. All advertising materials mentioning features or use of this software
     16      1.1   chopps  *    must display the following acknowledgement:
     17      1.1   chopps  *      This product includes software developed by Christopher G. Demetriou.
     18      1.1   chopps  * 4. The name of the author may not be used to endorse or promote products
     19      1.1   chopps  *    derived from this software without specific prior written permission
     20      1.1   chopps  *
     21      1.1   chopps  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22      1.1   chopps  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23      1.1   chopps  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24      1.1   chopps  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25      1.1   chopps  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26      1.1   chopps  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27      1.1   chopps  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28      1.1   chopps  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29      1.1   chopps  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30      1.1   chopps  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31      1.1   chopps  */
     32      1.1   chopps 
     33      1.6    lukem #include <sys/cdefs.h>
     34      1.1   chopps #ifndef lint
     35  1.8.2.1       he __RCSID("$NetBSD: mount_ados.c,v 1.8.2.1 1999/09/05 15:03:45 he Exp $");
     36      1.1   chopps #endif /* not lint */
     37      1.1   chopps 
     38      1.1   chopps #include <sys/cdefs.h>
     39      1.1   chopps #include <sys/param.h>
     40      1.1   chopps #include <sys/mount.h>
     41      1.1   chopps #include <sys/stat.h>
     42      1.1   chopps #include <ctype.h>
     43      1.1   chopps #include <err.h>
     44      1.1   chopps #include <grp.h>
     45      1.1   chopps #include <pwd.h>
     46      1.1   chopps #include <stdio.h>
     47      1.1   chopps #include <stdlib.h>
     48      1.1   chopps #include <string.h>
     49      1.1   chopps #include <unistd.h>
     50      1.8     fvdl 
     51      1.8     fvdl #include <adosfs/adosfs.h>
     52      1.1   chopps 
     53      1.3  mycroft #include "mntopts.h"
     54      1.3  mycroft 
     55      1.5      jtc const struct mntopt mopts[] = {
     56      1.3  mycroft 	MOPT_STDOPTS,
     57      1.3  mycroft 	{ NULL }
     58      1.3  mycroft };
     59      1.3  mycroft 
     60      1.1   chopps gid_t	a_gid __P((char *));
     61      1.1   chopps uid_t	a_uid __P((char *));
     62      1.1   chopps mode_t	a_mask __P((char *));
     63      1.6    lukem int	main __P((int, char *[]));
     64      1.1   chopps void	usage __P((void));
     65      1.3  mycroft 
     66      1.1   chopps int
     67      1.1   chopps main(argc, argv)
     68      1.1   chopps 	int argc;
     69      1.1   chopps 	char **argv;
     70      1.1   chopps {
     71      1.1   chopps 	struct adosfs_args args;
     72      1.1   chopps 	struct stat sb;
     73      1.3  mycroft 	int c, mntflags, set_gid, set_uid, set_mask;
     74      1.1   chopps 	char *dev, *dir, ndir[MAXPATHLEN+1];
     75      1.1   chopps 
     76      1.3  mycroft 	mntflags = set_gid = set_uid = set_mask = 0;
     77      1.1   chopps 	(void)memset(&args, '\0', sizeof(args));
     78      1.1   chopps 
     79      1.6    lukem 	while ((c = getopt(argc, argv, "u:g:m:o:")) != -1) {
     80      1.1   chopps 		switch (c) {
     81      1.1   chopps 		case 'u':
     82      1.1   chopps 			args.uid = a_uid(optarg);
     83      1.1   chopps 			set_uid = 1;
     84      1.1   chopps 			break;
     85      1.1   chopps 		case 'g':
     86      1.1   chopps 			args.gid = a_gid(optarg);
     87      1.1   chopps 			set_gid = 1;
     88      1.1   chopps 			break;
     89      1.1   chopps 		case 'm':
     90      1.1   chopps 			args.mask = a_mask(optarg);
     91      1.1   chopps 			set_mask = 1;
     92      1.1   chopps 			break;
     93      1.3  mycroft 		case 'o':
     94      1.7    lukem 			getmntopts(optarg, mopts, &mntflags, 0);
     95      1.3  mycroft 			break;
     96      1.1   chopps 		case '?':
     97      1.1   chopps 		default:
     98      1.1   chopps 			usage();
     99      1.1   chopps 			break;
    100      1.1   chopps 		}
    101      1.1   chopps 	}
    102      1.1   chopps 
    103      1.1   chopps 	if (optind + 2 != argc)
    104      1.1   chopps 		usage();
    105      1.1   chopps 
    106      1.1   chopps 	dev = argv[optind];
    107      1.1   chopps 	dir = argv[optind + 1];
    108      1.1   chopps 	if (dir[0] != '/') {
    109      1.1   chopps 		warnx("\"%s\" is a relative path.", dir);
    110      1.1   chopps 		if (getcwd(ndir, sizeof(ndir)) == NULL)
    111      1.1   chopps 			err(1, "getcwd");
    112      1.1   chopps 		strncat(ndir, "/", sizeof(ndir) - strlen(ndir) - 1);
    113      1.1   chopps 		strncat(ndir, dir, sizeof(ndir) - strlen(ndir) - 1);
    114      1.1   chopps 		dir = ndir;
    115      1.1   chopps 		warnx("using \"%s\" instead.", dir);
    116      1.1   chopps 	}
    117      1.1   chopps 
    118      1.1   chopps 	args.fspec = dev;
    119      1.3  mycroft 	args.export.ex_root = -2;	/* unchecked anyway on DOS fs */
    120      1.3  mycroft 	if (mntflags & MNT_RDONLY)
    121      1.3  mycroft 		args.export.ex_flags = MNT_EXRDONLY;
    122      1.3  mycroft 	else
    123      1.3  mycroft 		args.export.ex_flags = 0;
    124      1.1   chopps 	if (!set_gid || !set_uid || !set_mask) {
    125      1.1   chopps 		if (stat(dir, &sb) == -1)
    126      1.1   chopps 			err(1, "stat %s", dir);
    127      1.1   chopps 
    128      1.1   chopps 		if (!set_uid)
    129      1.1   chopps 			args.uid = sb.st_uid;
    130      1.1   chopps 		if (!set_gid)
    131      1.1   chopps 			args.gid = sb.st_gid;
    132      1.1   chopps 		if (!set_mask)
    133      1.1   chopps 			args.mask = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
    134      1.1   chopps 	}
    135      1.1   chopps 
    136      1.3  mycroft 	if (mount(MOUNT_ADOSFS, dir, mntflags, &args) < 0)
    137  1.8.2.1       he 		err(1, "%s on %s", dev, dir);
    138      1.1   chopps 
    139      1.1   chopps 	exit (0);
    140      1.1   chopps }
    141      1.1   chopps 
    142      1.1   chopps gid_t
    143      1.1   chopps a_gid(s)
    144      1.1   chopps 	char *s;
    145      1.1   chopps {
    146      1.1   chopps 	struct group *gr;
    147      1.1   chopps 	char *gname;
    148      1.1   chopps 	gid_t gid;
    149      1.1   chopps 
    150      1.1   chopps 	if ((gr = getgrnam(s)) != NULL)
    151      1.1   chopps 		gid = gr->gr_gid;
    152      1.1   chopps 	else {
    153      1.1   chopps 		for (gname = s; *s && isdigit(*s); ++s);
    154      1.1   chopps 		if (!*s)
    155      1.1   chopps 			gid = atoi(gname);
    156      1.1   chopps 		else
    157      1.1   chopps 			errx(1, "unknown group id: %s", gname);
    158      1.1   chopps 	}
    159      1.1   chopps 	return (gid);
    160      1.1   chopps }
    161      1.1   chopps 
    162      1.1   chopps uid_t
    163      1.1   chopps a_uid(s)
    164      1.1   chopps 	char *s;
    165      1.1   chopps {
    166      1.1   chopps 	struct passwd *pw;
    167      1.1   chopps 	char *uname;
    168      1.1   chopps 	uid_t uid;
    169      1.1   chopps 
    170      1.1   chopps 	if ((pw = getpwnam(s)) != NULL)
    171      1.1   chopps 		uid = pw->pw_uid;
    172      1.1   chopps 	else {
    173      1.1   chopps 		for (uname = s; *s && isdigit(*s); ++s);
    174      1.1   chopps 		if (!*s)
    175      1.1   chopps 			uid = atoi(uname);
    176      1.1   chopps 		else
    177      1.1   chopps 			errx(1, "unknown user id: %s", uname);
    178      1.1   chopps 	}
    179      1.1   chopps 	return (uid);
    180      1.1   chopps }
    181      1.1   chopps 
    182      1.1   chopps mode_t
    183      1.1   chopps a_mask(s)
    184      1.1   chopps 	char *s;
    185      1.1   chopps {
    186      1.1   chopps 	int done, rv;
    187      1.1   chopps 	char *ep;
    188      1.1   chopps 
    189      1.1   chopps 	done = 0;
    190      1.6    lukem 	rv = -1;
    191      1.1   chopps 	if (*s >= '0' && *s <= '7') {
    192      1.1   chopps 		done = 1;
    193      1.1   chopps 		rv = strtol(optarg, &ep, 8);
    194      1.1   chopps 	}
    195      1.1   chopps 	if (!done || rv < 0 || *ep)
    196      1.1   chopps 		errx(1, "invalid file mode: %s", s);
    197      1.1   chopps 	return (rv);
    198      1.1   chopps }
    199      1.1   chopps 
    200      1.1   chopps void
    201      1.1   chopps usage()
    202      1.1   chopps {
    203      1.2   chopps 
    204      1.3  mycroft 	fprintf(stderr, "usage: mount_ados [-o options] [-u user] [-g group] [-m mask] bdev dir\n");
    205      1.2   chopps 	exit(1);
    206      1.2   chopps }
    207