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 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#) Copyright (c) 1992, 1993, 1994 The Regents of the University of California. All rights reserved.
37 * @(#)mount_umap.c 8.5 (Berkeley) 4/26/95
38 * $FreeBSD: src/sbin/mount_umap/mount_umap.c,v 1.15 1999/10/09 11:54:13 phk Exp $
39 * $DragonFly: src/sbin/mount_umap/Attic/mount_umap.c,v 1.2 2003/06/17 04:27:33 dillon Exp $
42 #include <sys/param.h>
43 #include <sys/mount.h>
46 #include <miscfs/umapfs/umap.h>
58 * This define controls whether any user but the superuser can own and
59 * write mapfiles. If other users can, system security can be gravely
60 * compromised. If this is not a concern, undefine SECURITY.
65 * This routine provides the user interface to mounting a umap layer.
66 * It takes 4 mandatory parameters. The mandatory arguments are the place
67 * where the next lower level is mounted, the place where the umap layer is to
68 * be mounted, the name of the user mapfile, and the name of the group
69 * mapfile. The routine checks the ownerships and permissions on the
70 * mapfiles, then opens and reads them. Then it calls mount(), which
71 * will, in turn, call the umap version of mount.
74 static struct mntopt mopts[] = {
79 static void usage __P((void)) __dead2;
86 static char not[] = "; not mounted";
88 struct umap_args args;
90 u_long gmapdata[GMAPFILEENTRIES][2], mapdata[MAPFILEENTRIES][2];
91 int ch, count, gnentries, mntflags, nentries;
92 char *gmapfile, *mapfile, buf[20];
93 char source[MAXPATHLEN], target[MAXPATHLEN];
98 mapfile = gmapfile = NULL;
99 while ((ch = getopt(argc, argv, "g:o:u:")) != -1)
105 getmntopts(optarg, mopts, &mntflags, 0);
117 if (argc != 2 || mapfile == NULL || gmapfile == NULL)
120 /* resolve both target and source with realpath(3) */
121 (void)checkpath(argv[0], source);
122 (void)checkpath(argv[1], target);
124 /* Read in uid mapping data. */
125 if ((fp = fopen(mapfile, "r")) == NULL)
126 err(EX_NOINPUT, "%s%s", mapfile, not);
130 * Check that group and other don't have write permissions on
131 * this mapfile, and that the mapfile belongs to root.
133 if (fstat(fileno(fp), &statbuf))
134 err(EX_OSERR, "%s%s", mapfile, not);
135 if (statbuf.st_mode & S_IWGRP || statbuf.st_mode & S_IWOTH) {
136 strmode(statbuf.st_mode, buf);
137 err(EX_NOPERM, "%s: improper write permissions (%s)%s",
140 if (statbuf.st_uid != ROOTUSER)
141 errx(EX_NOPERM, "%s does not belong to root%s", mapfile, not);
142 #endif /* MAPSECURITY */
144 if ((fscanf(fp, "%d\n", &nentries)) != 1)
145 errx(EX_DATAERR, "%s: nentries not found%s", mapfile, not);
146 if (nentries > MAPFILEENTRIES)
148 "maximum number of entries is %d%s", MAPFILEENTRIES, not);
150 (void)printf("reading %d entries\n", nentries);
152 for (count = 0; count < nentries; ++count) {
153 if ((fscanf(fp, "%lu %lu\n",
154 &(mapdata[count][0]), &(mapdata[count][1]))) != 2) {
156 err(EX_OSERR, "%s%s", mapfile, not);
158 errx(EX_DATAERR, "%s: unexpected end-of-file%s",
160 errx(EX_DATAERR, "%s: illegal format (line %d)%s",
161 mapfile, count + 2, not);
164 /* Fix a security hole. */
165 if (mapdata[count][1] == 0)
166 errx(1, "mapping id 0 not permitted (line %d)%s",
171 /* Read in gid mapping data. */
172 if ((gfp = fopen(gmapfile, "r")) == NULL)
173 err(EX_NOINPUT, "%s%s", gmapfile, not);
177 * Check that group and other don't have write permissions on
178 * this group mapfile, and that the file belongs to root.
180 if (fstat(fileno(gfp), &statbuf))
181 err(EX_OSERR, "%s%s", gmapfile, not);
182 if (statbuf.st_mode & S_IWGRP || statbuf.st_mode & S_IWOTH) {
183 strmode(statbuf.st_mode, buf);
184 err(EX_NOPERM, "%s: improper write permissions (%s)%s",
187 if (statbuf.st_uid != ROOTUSER)
188 errx(EX_NOPERM, "%s does not belong to root%s", gmapfile, not);
189 #endif /* MAPSECURITY */
191 if ((fscanf(gfp, "%d\n", &gnentries)) != 1)
192 errx(EX_DATAERR, "%s: nentries not found%s", gmapfile, not);
193 if (gnentries > MAPFILEENTRIES)
195 "maximum number of entries is %d%s", GMAPFILEENTRIES, not);
197 (void)printf("reading %d group entries\n", gnentries);
200 for (count = 0; count < gnentries; ++count)
201 if ((fscanf(gfp, "%lu %lu\n",
202 &(gmapdata[count][0]), &(gmapdata[count][1]))) != 2) {
204 err(EX_OSERR, "%s%s", gmapfile, not);
206 errx(EX_DATAERR, "%s: unexpected end-of-file%s",
208 errx(EX_DATAERR, "%s: illegal format (line %d)%s",
209 gmapfile, count + 2, not);
213 /* Setup mount call args. */
214 args.target = source;
215 args.nentries = nentries;
216 args.mapdata = mapdata;
217 args.gnentries = gnentries;
218 args.gmapdata = gmapdata;
220 error = getvfsbyname("umap", &vfc);
221 if (error && vfsisloadable("umap")) {
223 err(1, "vfsload(umap)");
225 error = getvfsbyname("umap", &vfc);
228 errx(1, "umap filesystem is not available");
230 if (mount(vfc.vfc_name, argv[1], mntflags, &args))
238 (void)fprintf(stderr,
239 "usage: mount_umap [-o options] -u usermap -g groupmap target_fs mount_point\n");