mount_umap.c revision 1.14 1 /* $NetBSD: mount_umap.c,v 1.14 2003/08/07 10:04:32 agc 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.14 2003/08/07 10:04:32 agc 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 main __P((int, char *[]));
87 int mount_umap __P((int argc, char **argv));
88 static void usage __P((void));
89
90 #ifndef MOUNT_NOMAIN
91 int
92 main(argc, argv)
93 int argc;
94 char **argv;
95 {
96 return mount_umap(argc, argv);
97 }
98 #endif
99
100 int
101 mount_umap(argc, argv)
102 int argc;
103 char *argv[];
104 {
105 static char not[] = "; not mounted.";
106 struct stat statbuf;
107 struct umap_args args;
108 FILE *fp, *gfp;
109 long d1, d2;
110 u_long mapdata[MAPFILEENTRIES][2];
111 u_long gmapdata[GMAPFILEENTRIES][2];
112 int ch, count, gnentries, mntflags, nentries;
113 char *gmapfile, *mapfile, *source, *target, buf[20];
114
115 mntflags = 0;
116 mapfile = gmapfile = NULL;
117 while ((ch = getopt(argc, argv, "g:o:u:")) != -1)
118 switch (ch) {
119 case 'g':
120 gmapfile = optarg;
121 break;
122 case 'o':
123 getmntopts(optarg, mopts, &mntflags, 0);
124 break;
125 case 'u':
126 mapfile = optarg;
127 break;
128 case '?':
129 default:
130 usage();
131 }
132 argc -= optind;
133 argv += optind;
134
135 if (argc != 2 || mapfile == NULL || gmapfile == NULL)
136 usage();
137
138 source = argv[0];
139 target = argv[1];
140
141 /* Read in uid mapping data. */
142 if ((fp = fopen(mapfile, "r")) == NULL)
143 err(1, "%s%s", mapfile, not);
144
145 #ifdef MAPSECURITY
146 /*
147 * Check that group and other don't have write permissions on
148 * this mapfile, and that the mapfile belongs to root.
149 */
150 if (fstat(fileno(fp), &statbuf))
151 err(1, "%s%s", mapfile, not);
152 if (statbuf.st_mode & S_IWGRP || statbuf.st_mode & S_IWOTH) {
153 strmode(statbuf.st_mode, buf);
154 err(1, "%s: improper write permissions (%s)%s",
155 mapfile, buf, not);
156 }
157 if (statbuf.st_uid != ROOTUSER)
158 errx(1, "%s does not belong to root%s", mapfile, not);
159 #endif /* MAPSECURITY */
160
161 if ((fscanf(fp, "%d\n", &nentries)) != 1)
162 errx(1, "%s: nentries not found%s", mapfile, not);
163 if (nentries > MAPFILEENTRIES)
164 errx(1,
165 "maximum number of entries is %d%s", MAPFILEENTRIES, not);
166 #if 0
167 (void)printf("reading %d entries\n", nentries);
168 #endif
169 for (count = 0; count < nentries; ++count) {
170 if ((fscanf(fp, "%lu %lu\n", &d1, &d2)) != 2) {
171 if (ferror(fp))
172 err(1, "%s%s", mapfile, not);
173 if (feof(fp))
174 errx(1, "%s: unexpected end-of-file%s",
175 mapfile, not);
176 errx(1, "%s: illegal format (line %d)%s",
177 mapfile, count + 2, not);
178 }
179 mapdata[count][0] = d1;
180 mapdata[count][1] = d2;
181 #if 0
182 /* Fix a security hole. */
183 if (mapdata[count][1] == 0)
184 errx(1, "mapping id 0 not permitted (line %d)%s",
185 count + 2, not);
186 #endif
187 }
188
189 /* Read in gid mapping data. */
190 if ((gfp = fopen(gmapfile, "r")) == NULL)
191 err(1, "%s%s", gmapfile, not);
192
193 #ifdef MAPSECURITY
194 /*
195 * Check that group and other don't have write permissions on
196 * this group mapfile, and that the file belongs to root.
197 */
198 if (fstat(fileno(gfp), &statbuf))
199 err(1, "%s%s", gmapfile, not);
200 if (statbuf.st_mode & S_IWGRP || statbuf.st_mode & S_IWOTH) {
201 strmode(statbuf.st_mode, buf);
202 err(1, "%s: improper write permissions (%s)%s",
203 gmapfile, buf, not);
204 }
205 if (statbuf.st_uid != ROOTUSER)
206 errx(1, "%s does not belong to root%s", gmapfile, not);
207 #endif /* MAPSECURITY */
208
209 if ((fscanf(gfp, "%d\n", &gnentries)) != 1)
210 errx(1, "nentries not found%s", not);
211 if (gnentries > MAPFILEENTRIES)
212 errx(1,
213 "maximum number of entries is %d%s", GMAPFILEENTRIES, not);
214 #if 0
215 (void)printf("reading %d group entries\n", gnentries);
216 #endif
217
218 for (count = 0; count < gnentries; ++count) {
219 if ((fscanf(gfp, "%lu %lu\n",
220 &(gmapdata[count][0]), &(gmapdata[count][1]))) != 2) {
221 if (ferror(gfp))
222 err(1, "%s%s", gmapfile, not);
223 if (feof(gfp))
224 errx(1, "%s: unexpected end-of-file%s",
225 gmapfile, not);
226 errx(1, "%s: illegal format (line %d)%s",
227 gmapfile, count + 2, not);
228 }
229 }
230
231
232 /* Setup mount call args. */
233 args.la.target = source;
234 args.nentries = nentries;
235 args.mapdata = mapdata;
236 args.gnentries = gnentries;
237 args.gmapdata = gmapdata;
238
239 if (mount(MOUNT_UMAP, argv[1], mntflags, &args))
240 err(1, "%s on %s", source, argv[1]);
241 if (mntflags & MNT_GETARGS) {
242 printf("nentries=%d, gnentries=%d\n", args.nentries,
243 args.gnentries);
244 }
245 exit(0);
246 }
247
248 static void
249 usage()
250 {
251 (void)fprintf(stderr,
252 "usage: mount_umap [-o options] -u usermap -g groupmap target_fs mount_point\n");
253 exit(1);
254 }
255