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.4 2003/09/28 14:39:20 hmp Exp $
42 #include <sys/param.h>
43 #include <sys/mount.h>
46 #include <vfs/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;
82 main(int argc, char **argv)
84 static char not[] = "; not mounted";
86 struct umap_args args;
88 u_long gmapdata[GMAPFILEENTRIES][2], mapdata[MAPFILEENTRIES][2];
89 int ch, count, gnentries, mntflags, nentries;
90 char *gmapfile, *mapfile, buf[20];
91 char source[MAXPATHLEN], target[MAXPATHLEN];
96 mapfile = gmapfile = NULL;
97 while ((ch = getopt(argc, argv, "g:o:u:")) != -1)
103 getmntopts(optarg, mopts, &mntflags, 0);
115 if (argc != 2 || mapfile == NULL || gmapfile == NULL)
118 /* resolve both target and source with realpath(3) */
119 (void)checkpath(argv[0], source);
120 (void)checkpath(argv[1], target);
122 /* Read in uid mapping data. */
123 if ((fp = fopen(mapfile, "r")) == NULL)
124 err(EX_NOINPUT, "%s%s", mapfile, not);
128 * Check that group and other don't have write permissions on
129 * this mapfile, and that the mapfile belongs to root.
131 if (fstat(fileno(fp), &statbuf))
132 err(EX_OSERR, "%s%s", mapfile, not);
133 if (statbuf.st_mode & S_IWGRP || statbuf.st_mode & S_IWOTH) {
134 strmode(statbuf.st_mode, buf);
135 err(EX_NOPERM, "%s: improper write permissions (%s)%s",
138 if (statbuf.st_uid != ROOTUSER)
139 errx(EX_NOPERM, "%s does not belong to root%s", mapfile, not);
140 #endif /* MAPSECURITY */
142 if ((fscanf(fp, "%d\n", &nentries)) != 1)
143 errx(EX_DATAERR, "%s: nentries not found%s", mapfile, not);
144 if (nentries > MAPFILEENTRIES)
146 "maximum number of entries is %d%s", MAPFILEENTRIES, not);
148 (void)printf("reading %d entries\n", nentries);
150 for (count = 0; count < nentries; ++count) {
151 if ((fscanf(fp, "%lu %lu\n",
152 &(mapdata[count][0]), &(mapdata[count][1]))) != 2) {
154 err(EX_OSERR, "%s%s", mapfile, not);
156 errx(EX_DATAERR, "%s: unexpected end-of-file%s",
158 errx(EX_DATAERR, "%s: illegal format (line %d)%s",
159 mapfile, count + 2, not);
162 /* Fix a security hole. */
163 if (mapdata[count][1] == 0)
164 errx(1, "mapping id 0 not permitted (line %d)%s",
169 /* Read in gid mapping data. */
170 if ((gfp = fopen(gmapfile, "r")) == NULL)
171 err(EX_NOINPUT, "%s%s", gmapfile, not);
175 * Check that group and other don't have write permissions on
176 * this group mapfile, and that the file belongs to root.
178 if (fstat(fileno(gfp), &statbuf))
179 err(EX_OSERR, "%s%s", gmapfile, not);
180 if (statbuf.st_mode & S_IWGRP || statbuf.st_mode & S_IWOTH) {
181 strmode(statbuf.st_mode, buf);
182 err(EX_NOPERM, "%s: improper write permissions (%s)%s",
185 if (statbuf.st_uid != ROOTUSER)
186 errx(EX_NOPERM, "%s does not belong to root%s", gmapfile, not);
187 #endif /* MAPSECURITY */
189 if ((fscanf(gfp, "%d\n", &gnentries)) != 1)
190 errx(EX_DATAERR, "%s: nentries not found%s", gmapfile, not);
191 if (gnentries > MAPFILEENTRIES)
193 "maximum number of entries is %d%s", GMAPFILEENTRIES, not);
195 (void)printf("reading %d group entries\n", gnentries);
198 for (count = 0; count < gnentries; ++count)
199 if ((fscanf(gfp, "%lu %lu\n",
200 &(gmapdata[count][0]), &(gmapdata[count][1]))) != 2) {
202 err(EX_OSERR, "%s%s", gmapfile, not);
204 errx(EX_DATAERR, "%s: unexpected end-of-file%s",
206 errx(EX_DATAERR, "%s: illegal format (line %d)%s",
207 gmapfile, count + 2, not);
211 /* Setup mount call args. */
212 args.target = source;
213 args.nentries = nentries;
214 args.mapdata = mapdata;
215 args.gnentries = gnentries;
216 args.gmapdata = gmapdata;
218 error = getvfsbyname("umap", &vfc);
219 if (error && vfsisloadable("umap")) {
221 err(1, "vfsload(umap)");
223 error = getvfsbyname("umap", &vfc);
226 errx(1, "umap filesystem is not available");
228 if (mount(vfc.vfc_name, argv[1], mntflags, &args))
236 (void)fprintf(stderr,
237 "usage: mount_umap [-o options] -u usermap -g groupmap target_fs mount_point\n");