xref: /netbsd-src/sbin/mount_filecore/mount_filecore.c (revision dc306354b0b29af51801a7632f1e95265a68cd81)
1 /*	$NetBSD: mount_filecore.c,v 1.1 1998/08/14 03:38:51 mark Exp $	*/
2 
3 /*
4  * Copyright (c) 1998 Andrew McMurry
5  * Copyright (c) 1992, 1993, 1994 The Regents of the University of California.
6  * All rights reserved.
7  *
8  * This code is contains code contributed to the NetBSD project by
9  * Christopher G. Demetriou
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the University of
22  *	California, Berkeley and its contributors.
23  * 4. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  *
39  *      mount_filecore.c	1.1	1998/6/26
40  */
41 
42 #include <sys/cdefs.h>
43 #ifndef lint
44 __COPYRIGHT("@(#) Copyright (c) 1992, 1993, 1994\
45         The Regents of the University of California.\n\
46 	Copyright (c) 1998 Andrew McMurry\n\
47 	All rights reserved.\n");
48 #endif /* not lint */
49 
50 #include <sys/param.h>
51 #include <sys/mount.h>
52 
53 #include <ctype.h>
54 #include <err.h>
55 #include <grp.h>
56 #include <pwd.h>
57 #include <stdlib.h>
58 #include <stdio.h>
59 #include <string.h>
60 #include <unistd.h>
61 
62 #include <filecorefs/filecore_mount.h>
63 
64 #include "mntopts.h"
65 
66 const struct mntopt mopts[] = {
67 	MOPT_STDOPTS,
68 	MOPT_UPDATE,
69 	{ NULL }
70 };
71 
72 int	main __P((int, char *[]));
73 void	usage __P((void));
74 gid_t   a_gid __P((char *));
75 uid_t   a_uid __P((char *));
76 
77 int
78 main(argc, argv)
79 	int argc;
80 	char **argv;
81 {
82 	struct filecore_args args;
83 	int ch, mntflags, opts, useuid;
84 	char *dev, *dir;
85 
86 	mntflags = opts = 0;
87 	useuid = 1;
88 	args.uid = 0;
89 	args.gid = 0;
90 
91 	while ((ch = getopt(argc, argv, "anRfu:g:o:")) != -1)
92 		switch (ch) {
93 		case 'a':
94 			args.flags |= FILECOREMNT_ALLACCESS;
95 			break;
96 		case 'n':
97 			args.flags |= FILECOREMNT_OWNACCESS;
98 			break;
99 		case 'R':
100 			args.flags |= FILECOREMNT_OWNREAD;
101 			break;
102 		case 'f':
103 			args.flags |= FILECOREMNT_FILETYPE;
104 			break;
105                 case 'u':
106                         args.uid = a_uid(optarg);
107 			useuid = 0;
108                         break;
109                 case 'g':
110                         args.gid = a_gid(optarg);
111 			useuid = 0;
112                         break;
113 		case 'o':
114 			getmntopts(optarg, mopts, &mntflags, 0);
115 			break;
116 		case '?':
117 		default:
118 			usage();
119 		}
120 	argc -= optind;
121 	argv += optind;
122 
123 	if (argc != 2)
124 		usage();
125 
126 	dev = argv[0];
127 	dir = argv[1];
128 
129 #define DEFAULT_ROOTUID	-2
130 	/*
131 	 * FILECORE filesystems are not writeable.
132 	 */
133 	mntflags |= MNT_RDONLY;
134 	if (useuid) args.flags |= FILECOREMNT_USEUID;
135 	args.export.ex_flags = MNT_EXRDONLY;
136 	args.fspec = dev;
137 	args.export.ex_root = DEFAULT_ROOTUID;
138 	args.flags = opts;
139 
140 	if (mount(MOUNT_FILECORE, dir, mntflags, &args) < 0)
141 		err(1, "%s", "");
142 	exit(0);
143 }
144 
145 void
146 usage()
147 {
148 	(void)fprintf(stderr,
149 		"usage: mount_filecore [-o options] special node\n");
150 	exit(1);
151 }
152 
153 gid_t
154 a_gid(s)
155         char *s;
156 {
157         struct group *gr;
158         char *gname;
159         gid_t gid;
160 
161         if ((gr = getgrnam(s)) != NULL)
162                 gid = gr->gr_gid;
163         else {
164                 for (gname = s; *s && isdigit(*s); ++s);
165                 if (!*s)
166                         gid = atoi(gname);
167                 else
168                         errx(1, "unknown group id: %s", gname);
169         }
170         return (gid);
171 }
172 
173 uid_t
174 a_uid(s)
175         char *s;
176 {
177         struct passwd *pw;
178         char *uname;
179         uid_t uid;
180 
181         if ((pw = getpwnam(s)) != NULL)
182                 uid = pw->pw_uid;
183         else {
184                 for (uname = s; *s && isdigit(*s); ++s);
185                 if (!*s)
186                         uid = atoi(uname);
187                 else
188                         errx(1, "unknown user id: %s", uname);
189         }
190         return (uid);
191 }
192