2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software donated to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static const char copyright[] =
35 "@(#) Copyright (c) 1992, 1993, 1994\n\
36 The Regents of the University of California. All rights reserved.\n";
41 static char sccsid[] = "@(#)mount_umap.c 8.5 (Berkeley) 4/26/95";
43 static const char rcsid[] =
47 #include <sys/param.h>
48 #include <sys/mount.h>
51 #include <fs/umapfs/umap.h>
64 * This define controls whether any user but the superuser can own and
65 * write mapfiles. If other users can, system security can be gravely
66 * compromised. If this is not a concern, undefine SECURITY.
71 * This routine provides the user interface to mounting a umap layer.
72 * It takes 4 mandatory parameters. The mandatory arguments are the place
73 * where the next lower level is mounted, the place where the umap layer is to
74 * be mounted, the name of the user mapfile, and the name of the group
75 * mapfile. The routine checks the ownerships and permissions on the
76 * mapfiles, then opens and reads them. Then it calls mount(), which
77 * will, in turn, call the umap version of mount.
80 static struct mntopt mopts[] = {
85 static void usage(void) __dead2;
92 static char not[] = "; not mounted";
94 struct umap_args args;
96 u_long gmapdata[GMAPFILEENTRIES][2], mapdata[MAPFILEENTRIES][2];
97 int ch, count, gnentries, mntflags, nentries;
98 char *gmapfile, *mapfile, buf[20];
99 char source[MAXPATHLEN], target[MAXPATHLEN];
102 mapfile = gmapfile = NULL;
103 while ((ch = getopt(argc, argv, "g:o:u:")) != -1)
109 getmntopts(optarg, mopts, &mntflags, 0);
121 if (argc != 2 || mapfile == NULL || gmapfile == NULL)
124 /* resolve both target and source with realpath(3) */
125 (void)checkpath(argv[0], source);
126 (void)checkpath(argv[1], target);
128 /* Read in uid mapping data. */
129 if ((fp = fopen(mapfile, "r")) == NULL)
130 err(EX_NOINPUT, "%s%s", mapfile, not);
134 * Check that group and other don't have write permissions on
135 * this mapfile, and that the mapfile belongs to root.
137 if (fstat(fileno(fp), &statbuf))
138 err(EX_OSERR, "%s%s", mapfile, not);
139 if (statbuf.st_mode & S_IWGRP || statbuf.st_mode & S_IWOTH) {
140 strmode(statbuf.st_mode, buf);
141 err(EX_NOPERM, "%s: improper write permissions (%s)%s",
144 if (statbuf.st_uid != ROOTUSER)
145 errx(EX_NOPERM, "%s does not belong to root%s", mapfile, not);
146 #endif /* MAPSECURITY */
148 if ((fscanf(fp, "%d\n", &nentries)) != 1)
149 errx(EX_DATAERR, "%s: nentries not found%s", mapfile, not);
150 if (nentries > MAPFILEENTRIES)
152 "maximum number of entries is %d%s", MAPFILEENTRIES, not);
154 (void)printf("reading %d entries\n", nentries);
156 for (count = 0; count < nentries; ++count) {
157 if ((fscanf(fp, "%lu %lu\n",
158 &(mapdata[count][0]), &(mapdata[count][1]))) != 2) {
160 err(EX_OSERR, "%s%s", mapfile, not);
162 errx(EX_DATAERR, "%s: unexpected end-of-file%s",
164 errx(EX_DATAERR, "%s: illegal format (line %d)%s",
165 mapfile, count + 2, not);
168 /* Fix a security hole. */
169 if (mapdata[count][1] == 0)
170 errx(1, "mapping id 0 not permitted (line %d)%s",
175 /* Read in gid mapping data. */
176 if ((gfp = fopen(gmapfile, "r")) == NULL)
177 err(EX_NOINPUT, "%s%s", gmapfile, not);
181 * Check that group and other don't have write permissions on
182 * this group mapfile, and that the file belongs to root.
184 if (fstat(fileno(gfp), &statbuf))
185 err(EX_OSERR, "%s%s", gmapfile, not);
186 if (statbuf.st_mode & S_IWGRP || statbuf.st_mode & S_IWOTH) {
187 strmode(statbuf.st_mode, buf);
188 err(EX_NOPERM, "%s: improper write permissions (%s)%s",
191 if (statbuf.st_uid != ROOTUSER)
192 errx(EX_NOPERM, "%s does not belong to root%s", gmapfile, not);
193 #endif /* MAPSECURITY */
195 if ((fscanf(gfp, "%d\n", &gnentries)) != 1)
196 errx(EX_DATAERR, "%s: nentries not found%s", gmapfile, not);
197 if (gnentries > MAPFILEENTRIES)
199 "maximum number of entries is %d%s", GMAPFILEENTRIES, not);
201 (void)printf("reading %d group entries\n", gnentries);
204 for (count = 0; count < gnentries; ++count)
205 if ((fscanf(gfp, "%lu %lu\n",
206 &(gmapdata[count][0]), &(gmapdata[count][1]))) != 2) {
208 err(EX_OSERR, "%s%s", gmapfile, not);
210 errx(EX_DATAERR, "%s: unexpected end-of-file%s",
212 errx(EX_DATAERR, "%s: illegal format (line %d)%s",
213 gmapfile, count + 2, not);
217 /* Setup mount call args. */
218 args.target = source;
219 args.nentries = nentries;
220 args.mapdata = mapdata;
221 args.gnentries = gnentries;
222 args.gmapdata = gmapdata;
224 if (mount("umapfs", argv[1], mntflags, &args))
232 (void)fprintf(stderr,
233 "usage: mount_umapfs [-o options] -u uid-mapfile -g gid-mapfile target mount-point\n");