1 /* $NetBSD: mount_umap.c,v 1.21 2007/07/16 17:06:54 pooka Exp $ */
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
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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
35 #include <sys/cdefs.h>
37 __COPYRIGHT("@(#) Copyright (c) 1992, 1993, 1994\
38 The Regents of the University of California. All rights reserved.");
43 static char sccsid
[] = "@(#)mount_umap.c 8.5 (Berkeley) 4/26/95";
45 __RCSID("$NetBSD: mount_umap.c,v 1.21 2007/07/16 17:06:54 pooka Exp $");
49 #include <sys/param.h>
50 #include <sys/mount.h>
53 #include <miscfs/umapfs/umap.h>
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.
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
[] = {
86 int mount_umap(int argc
, char **argv
);
87 static void usage(void);
91 main(int argc
, char **argv
)
93 return mount_umap(argc
, argv
);
98 mount_umap(int argc
, char *argv
[])
100 static char not[] = "; not mounted.";
102 struct umap_args args
;
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
];
113 mapfile
= gmapfile
= NULL
;
114 while ((ch
= getopt(argc
, argv
, "g:o:u:")) != -1)
120 mp
= getmntopts(optarg
, mopts
, &mntflags
, 0);
122 err(1, "getmntopts");
135 if (argc
!= 2 || mapfile
== NULL
|| gmapfile
== NULL
)
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);
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",
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
)
176 "maximum number of entries is %d%s", MAPFILEENTRIES
, not);
178 (void)printf("reading %d entries\n", nentries
);
180 for (count
= 0; count
< nentries
; ++count
) {
181 if ((fscanf(fp
, "%lu %lu\n", &d1
, &d2
)) != 2) {
183 err(1, "%s%s", mapfile
, not);
185 errx(1, "%s: unexpected end-of-file%s",
187 errx(1, "%s: illegal format (line %d)%s",
188 mapfile
, count
+ 2, not);
190 mapdata
[count
][0] = d1
;
191 mapdata
[count
][1] = d2
;
193 /* Fix a security hole. */
194 if (mapdata
[count
][1] == 0)
195 errx(1, "mapping id 0 not permitted (line %d)%s",
200 /* Read in gid mapping data. */
201 if ((gfp
= fopen(gmapfile
, "r")) == NULL
)
202 err(1, "%s%s", gmapfile
, not);
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",
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
)
224 "maximum number of entries is %d%s", GMAPFILEENTRIES
, not);
226 (void)printf("reading %d group entries\n", gnentries
);
229 for (count
= 0; count
< gnentries
; ++count
) {
230 if ((fscanf(gfp
, "%lu %lu\n",
231 &(gmapdata
[count
][0]), &(gmapdata
[count
][1]))) != 2) {
233 err(1, "%s%s", gmapfile
, not);
235 errx(1, "%s: unexpected end-of-file%s",
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
,
262 (void)fprintf(stderr
,
263 "usage: mount_umap [-o options] -g groupmap -u usermap target_fs mount_point\n");