Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sbin / mount_umap / mount_umap.c
blob186f3189d176e25ff7c9dacc163d15187160edfd
1 /* $NetBSD: mount_umap.c,v 1.21 2007/07/16 17:06:54 pooka Exp $ */
3 /*
4 * Copyright (c) 1992, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software donated to Berkeley by
8 * Jan-Simon Pendry.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
35 #include <sys/cdefs.h>
36 #ifndef lint
37 __COPYRIGHT("@(#) Copyright (c) 1992, 1993, 1994\
38 The Regents of the University of California. All rights reserved.");
39 #endif /* not lint */
41 #ifndef lint
42 #if 0
43 static char sccsid[] = "@(#)mount_umap.c 8.5 (Berkeley) 4/26/95";
44 #else
45 __RCSID("$NetBSD: mount_umap.c,v 1.21 2007/07/16 17:06:54 pooka Exp $");
46 #endif
47 #endif /* not lint */
49 #include <sys/param.h>
50 #include <sys/mount.h>
51 #include <sys/stat.h>
53 #include <miscfs/umapfs/umap.h>
55 #include <err.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <unistd.h>
61 #include <mntopts.h>
63 #define ROOTUSER 0
65 * This define controls whether any user but the superuser can own and
66 * write mapfiles. If other users can, system security can be gravely
67 * compromised. If this is not a concern, undefine SECURITY.
69 #define MAPSECURITY 1
72 * This routine provides the user interface to mounting a umap layer.
73 * It takes 4 mandatory parameters. The mandatory arguments are the place
74 * where the next lower level is mounted, the place where the umap layer is to
75 * be mounted, the name of the user mapfile, and the name of the group
76 * mapfile. The routine checks the ownerships and permissions on the
77 * mapfiles, then opens and reads them. Then it calls mount(), which
78 * will, in turn, call the umap version of mount.
81 static const struct mntopt mopts[] = {
82 MOPT_STDOPTS,
83 MOPT_NULL,
86 int mount_umap(int argc, char **argv);
87 static void usage(void);
89 #ifndef MOUNT_NOMAIN
90 int
91 main(int argc, char **argv)
93 return mount_umap(argc, argv);
95 #endif
97 int
98 mount_umap(int argc, char *argv[])
100 static char not[] = "; not mounted.";
101 struct stat statbuf;
102 struct umap_args args;
103 FILE *fp, *gfp;
104 long d1, d2;
105 u_long mapdata[MAPFILEENTRIES][2];
106 u_long gmapdata[GMAPFILEENTRIES][2];
107 int ch, count, gnentries, mntflags, nentries;
108 char *gmapfile, *mapfile, buf[20];
109 char source[MAXPATHLEN], target[MAXPATHLEN];
110 mntoptparse_t mp;
112 mntflags = 0;
113 mapfile = gmapfile = NULL;
114 while ((ch = getopt(argc, argv, "g:o:u:")) != -1)
115 switch (ch) {
116 case 'g':
117 gmapfile = optarg;
118 break;
119 case 'o':
120 mp = getmntopts(optarg, mopts, &mntflags, 0);
121 if (mp == NULL)
122 err(1, "getmntopts");
123 freemntopts(mp);
124 break;
125 case 'u':
126 mapfile = optarg;
127 break;
128 case '?':
129 default:
130 usage();
132 argc -= optind;
133 argv += optind;
135 if (argc != 2 || mapfile == NULL || gmapfile == NULL)
136 usage();
138 if (realpath(argv[0], source) == NULL) /* Check source path */
139 err(1, "realpath %s", argv[0]);
140 if (strncmp(argv[0], source, MAXPATHLEN)) {
141 warnx("\"%s\" is a relative path.", argv[0]);
142 warnx("using \"%s\" instead.", source);
145 if (realpath(argv[1], target) == NULL) /* Check mounton path */
146 err(1, "realpath %s", argv[1]);
147 if (strncmp(argv[1], target, MAXPATHLEN)) {
148 warnx("\"%s\" is a relative path.", argv[1]);
149 warnx("using \"%s\" instead.", target);
152 /* Read in uid mapping data. */
153 if ((fp = fopen(mapfile, "r")) == NULL)
154 err(1, "%s%s", mapfile, not);
156 #ifdef MAPSECURITY
158 * Check that group and other don't have write permissions on
159 * this mapfile, and that the mapfile belongs to root.
161 if (fstat(fileno(fp), &statbuf))
162 err(1, "%s%s", mapfile, not);
163 if (statbuf.st_mode & S_IWGRP || statbuf.st_mode & S_IWOTH) {
164 strmode(statbuf.st_mode, buf);
165 err(1, "%s: improper write permissions (%s)%s",
166 mapfile, buf, not);
168 if (statbuf.st_uid != ROOTUSER)
169 errx(1, "%s does not belong to root%s", mapfile, not);
170 #endif /* MAPSECURITY */
172 if ((fscanf(fp, "%d\n", &nentries)) != 1)
173 errx(1, "%s: nentries not found%s", mapfile, not);
174 if (nentries > MAPFILEENTRIES)
175 errx(1,
176 "maximum number of entries is %d%s", MAPFILEENTRIES, not);
177 #if 0
178 (void)printf("reading %d entries\n", nentries);
179 #endif
180 for (count = 0; count < nentries; ++count) {
181 if ((fscanf(fp, "%lu %lu\n", &d1, &d2)) != 2) {
182 if (ferror(fp))
183 err(1, "%s%s", mapfile, not);
184 if (feof(fp))
185 errx(1, "%s: unexpected end-of-file%s",
186 mapfile, not);
187 errx(1, "%s: illegal format (line %d)%s",
188 mapfile, count + 2, not);
190 mapdata[count][0] = d1;
191 mapdata[count][1] = d2;
192 #if 0
193 /* Fix a security hole. */
194 if (mapdata[count][1] == 0)
195 errx(1, "mapping id 0 not permitted (line %d)%s",
196 count + 2, not);
197 #endif
200 /* Read in gid mapping data. */
201 if ((gfp = fopen(gmapfile, "r")) == NULL)
202 err(1, "%s%s", gmapfile, not);
204 #ifdef MAPSECURITY
206 * Check that group and other don't have write permissions on
207 * this group mapfile, and that the file belongs to root.
209 if (fstat(fileno(gfp), &statbuf))
210 err(1, "%s%s", gmapfile, not);
211 if (statbuf.st_mode & S_IWGRP || statbuf.st_mode & S_IWOTH) {
212 strmode(statbuf.st_mode, buf);
213 err(1, "%s: improper write permissions (%s)%s",
214 gmapfile, buf, not);
216 if (statbuf.st_uid != ROOTUSER)
217 errx(1, "%s does not belong to root%s", gmapfile, not);
218 #endif /* MAPSECURITY */
220 if ((fscanf(gfp, "%d\n", &gnentries)) != 1)
221 errx(1, "nentries not found%s", not);
222 if (gnentries > MAPFILEENTRIES)
223 errx(1,
224 "maximum number of entries is %d%s", GMAPFILEENTRIES, not);
225 #if 0
226 (void)printf("reading %d group entries\n", gnentries);
227 #endif
229 for (count = 0; count < gnentries; ++count) {
230 if ((fscanf(gfp, "%lu %lu\n",
231 &(gmapdata[count][0]), &(gmapdata[count][1]))) != 2) {
232 if (ferror(gfp))
233 err(1, "%s%s", gmapfile, not);
234 if (feof(gfp))
235 errx(1, "%s: unexpected end-of-file%s",
236 gmapfile, not);
237 errx(1, "%s: illegal format (line %d)%s",
238 gmapfile, count + 2, not);
243 /* Setup mount call args. */
244 args.la.target = source;
245 args.nentries = nentries;
246 args.mapdata = mapdata;
247 args.gnentries = gnentries;
248 args.gmapdata = gmapdata;
250 if (mount(MOUNT_UMAP, target, mntflags, &args, sizeof args) == -1)
251 err(1, "%s on %s", source, target);
252 if (mntflags & MNT_GETARGS) {
253 printf("nentries=%d, gnentries=%d\n", args.nentries,
254 args.gnentries);
256 exit(0);
259 static void
260 usage(void)
262 (void)fprintf(stderr,
263 "usage: mount_umap [-o options] -g groupmap -u usermap target_fs mount_point\n");
264 exit(1);