mount_umap.c revision 1.17 1 /* $NetBSD: mount_umap.c,v 1.17 2005/09/11 23:40:54 wiz Exp $ */
2
3 /*
4 * Copyright (c) 1992, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software donated to Berkeley by
8 * Jan-Simon Pendry.
9 *
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.
21 *
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.
33 */
34
35 #include <sys/cdefs.h>
36 #ifndef lint
37 __COPYRIGHT("@(#) Copyright (c) 1992, 1993, 1994\n\
38 The Regents of the University of California. All rights reserved.\n");
39 #endif /* not lint */
40
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.17 2005/09/11 23:40:54 wiz Exp $");
46 #endif
47 #endif /* not lint */
48
49 #include <sys/param.h>
50 #include <sys/mount.h>
51 #include <sys/stat.h>
52
53 #include <miscfs/umapfs/umap.h>
54
55 #include <err.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <unistd.h>
60
61 #include <mntopts.h>
62
63 #define ROOTUSER 0
64 /*
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.
68 */
69 #define MAPSECURITY 1
70
71 /*
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.
79 */
80
81 static const struct mntopt mopts[] = {
82 MOPT_STDOPTS,
83 { NULL }
84 };
85
86 int mount_umap(int argc, char **argv);
87 static void usage(void);
88
89 #ifndef MOUNT_NOMAIN
90 int
91 main(int argc, char **argv)
92 {
93 return mount_umap(argc, argv);
94 }
95 #endif
96
97 int
98 mount_umap(int argc, char *argv[])
99 {
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
111 mntflags = 0;
112 mapfile = gmapfile = NULL;
113 while ((ch = getopt(argc, argv, "g:o:u:")) != -1)
114 switch (ch) {
115 case 'g':
116 gmapfile = optarg;
117 break;
118 case 'o':
119 getmntopts(optarg, mopts, &mntflags, 0);
120 break;
121 case 'u':
122 mapfile = optarg;
123 break;
124 case '?':
125 default:
126 usage();
127 }
128 argc -= optind;
129 argv += optind;
130
131 if (argc != 2 || mapfile == NULL || gmapfile == NULL)
132 usage();
133
134 if (realpath(argv[0], source) == NULL) /* Check source path */
135 err(1, "realpath %s", argv[0]);
136 if (strncmp(argv[0], source, MAXPATHLEN)) {
137 warnx("\"%s\" is a relative path.", argv[0]);
138 warnx("using \"%s\" instead.", source);
139 }
140
141 if (realpath(argv[1], target) == NULL) /* Check mounton path */
142 err(1, "realpath %s", argv[1]);
143 if (strncmp(argv[1], target, MAXPATHLEN)) {
144 warnx("\"%s\" is a relative path.", argv[1]);
145 warnx("using \"%s\" instead.", target);
146 }
147
148 /* Read in uid mapping data. */
149 if ((fp = fopen(mapfile, "r")) == NULL)
150 err(1, "%s%s", mapfile, not);
151
152 #ifdef MAPSECURITY
153 /*
154 * Check that group and other don't have write permissions on
155 * this mapfile, and that the mapfile belongs to root.
156 */
157 if (fstat(fileno(fp), &statbuf))
158 err(1, "%s%s", mapfile, not);
159 if (statbuf.st_mode & S_IWGRP || statbuf.st_mode & S_IWOTH) {
160 strmode(statbuf.st_mode, buf);
161 err(1, "%s: improper write permissions (%s)%s",
162 mapfile, buf, not);
163 }
164 if (statbuf.st_uid != ROOTUSER)
165 errx(1, "%s does not belong to root%s", mapfile, not);
166 #endif /* MAPSECURITY */
167
168 if ((fscanf(fp, "%d\n", &nentries)) != 1)
169 errx(1, "%s: nentries not found%s", mapfile, not);
170 if (nentries > MAPFILEENTRIES)
171 errx(1,
172 "maximum number of entries is %d%s", MAPFILEENTRIES, not);
173 #if 0
174 (void)printf("reading %d entries\n", nentries);
175 #endif
176 for (count = 0; count < nentries; ++count) {
177 if ((fscanf(fp, "%lu %lu\n", &d1, &d2)) != 2) {
178 if (ferror(fp))
179 err(1, "%s%s", mapfile, not);
180 if (feof(fp))
181 errx(1, "%s: unexpected end-of-file%s",
182 mapfile, not);
183 errx(1, "%s: illegal format (line %d)%s",
184 mapfile, count + 2, not);
185 }
186 mapdata[count][0] = d1;
187 mapdata[count][1] = d2;
188 #if 0
189 /* Fix a security hole. */
190 if (mapdata[count][1] == 0)
191 errx(1, "mapping id 0 not permitted (line %d)%s",
192 count + 2, not);
193 #endif
194 }
195
196 /* Read in gid mapping data. */
197 if ((gfp = fopen(gmapfile, "r")) == NULL)
198 err(1, "%s%s", gmapfile, not);
199
200 #ifdef MAPSECURITY
201 /*
202 * Check that group and other don't have write permissions on
203 * this group mapfile, and that the file belongs to root.
204 */
205 if (fstat(fileno(gfp), &statbuf))
206 err(1, "%s%s", gmapfile, not);
207 if (statbuf.st_mode & S_IWGRP || statbuf.st_mode & S_IWOTH) {
208 strmode(statbuf.st_mode, buf);
209 err(1, "%s: improper write permissions (%s)%s",
210 gmapfile, buf, not);
211 }
212 if (statbuf.st_uid != ROOTUSER)
213 errx(1, "%s does not belong to root%s", gmapfile, not);
214 #endif /* MAPSECURITY */
215
216 if ((fscanf(gfp, "%d\n", &gnentries)) != 1)
217 errx(1, "nentries not found%s", not);
218 if (gnentries > MAPFILEENTRIES)
219 errx(1,
220 "maximum number of entries is %d%s", GMAPFILEENTRIES, not);
221 #if 0
222 (void)printf("reading %d group entries\n", gnentries);
223 #endif
224
225 for (count = 0; count < gnentries; ++count) {
226 if ((fscanf(gfp, "%lu %lu\n",
227 &(gmapdata[count][0]), &(gmapdata[count][1]))) != 2) {
228 if (ferror(gfp))
229 err(1, "%s%s", gmapfile, not);
230 if (feof(gfp))
231 errx(1, "%s: unexpected end-of-file%s",
232 gmapfile, not);
233 errx(1, "%s: illegal format (line %d)%s",
234 gmapfile, count + 2, not);
235 }
236 }
237
238
239 /* Setup mount call args. */
240 args.la.target = source;
241 args.nentries = nentries;
242 args.mapdata = mapdata;
243 args.gnentries = gnentries;
244 args.gmapdata = gmapdata;
245
246 if (mount(MOUNT_UMAP, target, mntflags, &args))
247 err(1, "%s on %s", source, target);
248 if (mntflags & MNT_GETARGS) {
249 printf("nentries=%d, gnentries=%d\n", args.nentries,
250 args.gnentries);
251 }
252 exit(0);
253 }
254
255 static void
256 usage(void)
257 {
258 (void)fprintf(stderr,
259 "usage: mount_umap [-o options] -g groupmap -u usermap target_fs mount_point\n");
260 exit(1);
261 }
262