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