mount_umap.c revision 1.1 1 /*
2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software donated to Berkeley by
6 * Jan-Simon Pendry.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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.
23 *
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
34 * SUCH DAMAGE.
35 */
36
37 #ifndef lint
38 char copyright[] =
39 "@(#) Copyright (c) 1992, 1993, 1994\n\
40 The Regents of the University of California. All rights reserved.\n";
41 #endif /* not lint */
42
43 #ifndef lint
44 /*static char sccsid[] = "from: @(#)mount_umap.c 8.3 (Berkeley) 3/27/94";*/
45 static char *rcsid = "$Id: mount_umap.c,v 1.1 1994/06/08 19:28:13 mycroft Exp $";
46 #endif /* not lint */
47
48 #include <sys/param.h>
49 #include <sys/mount.h>
50 #include <sys/stat.h>
51
52 #include <miscfs/umapfs/umap.h>
53
54 #include <err.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <unistd.h>
59
60 #include "mntopts.h"
61
62 #define ROOTUSER 0
63 /*
64 * This define controls whether any user but the superuser can own and
65 * write mapfiles. If other users can, system security can be gravely
66 * compromised. If this is not a concern, undefine SECURITY.
67 */
68 #define MAPSECURITY 1
69
70 /*
71 * This routine provides the user interface to mounting a umap layer.
72 * It takes 4 mandatory parameters. The mandatory arguments are the place
73 * where the next lower level is mounted, the place where the umap layer is to
74 * be mounted, the name of the user mapfile, and the name of the group
75 * mapfile. The routine checks the ownerships and permissions on the
76 * mapfiles, then opens and reads them. Then it calls mount(), which
77 * will, in turn, call the umap version of mount.
78 */
79
80 struct mntopt mopts[] = {
81 MOPT_STDOPTS,
82 { NULL }
83 };
84
85 void usage __P((void));
86
87 int
88 main(argc, argv)
89 int argc;
90 char *argv[];
91 {
92 static char not[] = "; not mounted.";
93 struct stat statbuf;
94 struct umap_args args;
95 FILE *fp, *gfp;
96 u_long gmapdata[GMAPFILEENTRIES][2], mapdata[MAPFILEENTRIES][2];
97 int ch, count, gnentries, mntflags, nentries;
98 char *gmapfile, *mapfile, *source, *target, buf[20];
99
100 mntflags = 0;
101 mapfile = gmapfile = NULL;
102 while ((ch = getopt(argc, argv, "g:o:u:")) != EOF)
103 switch (ch) {
104 case 'g':
105 gmapfile = optarg;
106 break;
107 case 'o':
108 getmntopts(optarg, mopts, &mntflags);
109 break;
110 case 'u':
111 mapfile = optarg;
112 break;
113 case '?':
114 default:
115 usage();
116 }
117 argc -= optind;
118 argv += optind;
119
120 if (argc != 2 || mapfile == NULL || gmapfile == NULL)
121 usage();
122
123 source = argv[0];
124 target = argv[1];
125
126 /* Read in uid mapping data. */
127 if ((fp = fopen(mapfile, "r")) == NULL)
128 err(1, "%s%s", mapfile, not);
129
130 #ifdef MAPSECURITY
131 /*
132 * Check that group and other don't have write permissions on
133 * this mapfile, and that the mapfile belongs to root.
134 */
135 if (fstat(fileno(fp), &statbuf))
136 err(1, "%s%s", mapfile, not);
137 if (statbuf.st_mode & S_IWGRP || statbuf.st_mode & S_IWOTH) {
138 strmode(statbuf.st_mode, buf);
139 err(1, "%s: improper write permissions (%s)%s",
140 mapfile, buf, not);
141 }
142 if (statbuf.st_uid != ROOTUSER)
143 errx(1, "%s does not belong to root%s", mapfile, not);
144 #endif /* MAPSECURITY */
145
146 if ((fscanf(fp, "%d\n", &nentries)) != 1)
147 errx(1, "%s: nentries not found%s", mapfile, not);
148 if (nentries > MAPFILEENTRIES)
149 errx(1,
150 "maximum number of entries is %d%s", MAPFILEENTRIES, not);
151 #if 0
152 (void)printf("reading %d entries\n", nentries);
153 #endif
154 for (count = 0; count < nentries; ++count) {
155 if ((fscanf(fp, "%lu %lu\n",
156 &(mapdata[count][0]), &(mapdata[count][1]))) != 2) {
157 if (ferror(fp))
158 err(1, "%s%s", mapfile, not);
159 if (feof(fp))
160 errx(1, "%s: unexpected end-of-file%s",
161 mapfile, not);
162 errx(1, "%s: illegal format (line %d)%s",
163 mapfile, count + 2, not);
164 }
165 #if 0
166 /* Fix a security hole. */
167 if (mapdata[count][1] == 0)
168 errx(1, "mapping id 0 not permitted (line %d)%s",
169 count + 2, not);
170 #endif
171 }
172
173 /* Read in gid mapping data. */
174 if ((gfp = fopen(gmapfile, "r")) == NULL)
175 err(1, "%s%s", gmapfile, not);
176
177 #ifdef MAPSECURITY
178 /*
179 * Check that group and other don't have write permissions on
180 * this group mapfile, and that the file belongs to root.
181 */
182 if (fstat(fileno(gfp), &statbuf))
183 err(1, "%s%s", gmapfile, not);
184 if (statbuf.st_mode & S_IWGRP || statbuf.st_mode & S_IWOTH) {
185 strmode(statbuf.st_mode, buf);
186 err(1, "%s: improper write permissions (%s)%s",
187 gmapfile, buf, not);
188 }
189 if (statbuf.st_uid != ROOTUSER)
190 errx(1, "%s does not belong to root%s", gmapfile, not);
191 #endif /* MAPSECURITY */
192
193 if ((fscanf(gfp, "%d\n", &gnentries)) != 1)
194 errx(1, "nentries not found%s", gmapfile, not);
195 if (gnentries > MAPFILEENTRIES)
196 errx(1,
197 "maximum number of entries is %d%s", GMAPFILEENTRIES, not);
198 #if 0
199 (void)printf("reading %d group entries\n", gnentries);
200 #endif
201
202 for (count = 0; count < gnentries; ++count)
203 if ((fscanf(gfp, "%lu %lu\n",
204 &(gmapdata[count][0]), &(gmapdata[count][1]))) != 2) {
205 if (ferror(gfp))
206 err(1, "%s%s", gmapfile, not);
207 if (feof(gfp))
208 errx(1, "%s: unexpected end-of-file%s",
209 gmapfile, not);
210 errx(1, "%s: illegal format (line %d)%s",
211 gmapfile, count + 2, not);
212 }
213
214
215 /* Setup mount call args. */
216 args.target = source;
217 args.nentries = nentries;
218 args.mapdata = mapdata;
219 args.gnentries = gnentries;
220 args.gmapdata = gmapdata;
221
222 if (mount(MOUNT_UMAP, argv[1], mntflags, &args))
223 err(1, NULL);
224 exit(0);
225 }
226
227 void
228 usage()
229 {
230 (void)fprintf(stderr,
231 "usage: mount_umap [-o options] -u usermap -g groupmap target_fs mount_point\n");
232 exit(1);
233 }
234