]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - 6/sbin/mount_umapfs/mount_umapfs.c
merge fix for boot-time hang on centos' xen
[FreeBSD/FreeBSD.git] / 6 / sbin / mount_umapfs / mount_umapfs.c
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  * 4. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32
33 #ifndef lint
34 static const char copyright[] =
35 "@(#) Copyright (c) 1992, 1993, 1994\n\
36         The Regents of the University of California.  All rights reserved.\n";
37 #endif /* not lint */
38
39 #ifndef lint
40 #if 0
41 static char sccsid[] = "@(#)mount_umap.c        8.5 (Berkeley) 4/26/95";
42 #endif
43 static const char rcsid[] =
44   "$FreeBSD$";
45 #endif /* not lint */
46
47 #include <sys/param.h>
48 #include <sys/mount.h>
49 #include <sys/stat.h>
50
51 #include <fs/umapfs/umap.h>
52
53 #include <err.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <sysexits.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 static struct mntopt mopts[] = {
81         MOPT_STDOPTS,
82         MOPT_END
83 };
84
85 static void     usage(void) __dead2;
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, buf[20];
99         char source[MAXPATHLEN], target[MAXPATHLEN];
100
101         mntflags = 0;
102         mapfile = gmapfile = NULL;
103         while ((ch = getopt(argc, argv, "g:o:u:")) != -1)
104                 switch (ch) {
105                 case 'g':
106                         gmapfile = optarg;
107                         break;
108                 case 'o':
109                         getmntopts(optarg, mopts, &mntflags, 0);
110                         break;
111                 case 'u':
112                         mapfile = optarg;
113                         break;
114                 case '?':
115                 default:
116                         usage();
117                 }
118         argc -= optind;
119         argv += optind;
120
121         if (argc != 2 || mapfile == NULL || gmapfile == NULL)
122                 usage();
123
124         /* resolve both target and source with realpath(3) */
125         (void)checkpath(argv[0], source);
126         (void)checkpath(argv[1], target);
127
128         /* Read in uid mapping data. */
129         if ((fp = fopen(mapfile, "r")) == NULL)
130                 err(EX_NOINPUT, "%s%s", mapfile, not);
131
132 #ifdef MAPSECURITY
133         /*
134          * Check that group and other don't have write permissions on
135          * this mapfile, and that the mapfile belongs to root.
136          */
137         if (fstat(fileno(fp), &statbuf))
138                 err(EX_OSERR, "%s%s", mapfile, not);
139         if (statbuf.st_mode & S_IWGRP || statbuf.st_mode & S_IWOTH) {
140                 strmode(statbuf.st_mode, buf);
141                 err(EX_NOPERM, "%s: improper write permissions (%s)%s",
142                     mapfile, buf, not);
143         }
144         if (statbuf.st_uid != ROOTUSER)
145                 errx(EX_NOPERM, "%s does not belong to root%s", mapfile, not);
146 #endif /* MAPSECURITY */
147
148         if ((fscanf(fp, "%d\n", &nentries)) != 1)
149                 errx(EX_DATAERR, "%s: nentries not found%s", mapfile, not);
150         if (nentries > MAPFILEENTRIES)
151                 errx(EX_DATAERR,
152                     "maximum number of entries is %d%s", MAPFILEENTRIES, not);
153 #if 0
154         (void)printf("reading %d entries\n", nentries);
155 #endif
156         for (count = 0; count < nentries; ++count) {
157                 if ((fscanf(fp, "%lu %lu\n",
158                     &(mapdata[count][0]), &(mapdata[count][1]))) != 2) {
159                         if (ferror(fp))
160                                 err(EX_OSERR, "%s%s", mapfile, not);
161                         if (feof(fp))
162                                 errx(EX_DATAERR, "%s: unexpected end-of-file%s",
163                                     mapfile, not);
164                         errx(EX_DATAERR, "%s: illegal format (line %d)%s",
165                             mapfile, count + 2, not);
166                 }
167 #if 0
168                 /* Fix a security hole. */
169                 if (mapdata[count][1] == 0)
170                         errx(1, "mapping id 0 not permitted (line %d)%s",
171                             count + 2, not);
172 #endif
173         }
174
175         /* Read in gid mapping data. */
176         if ((gfp = fopen(gmapfile, "r")) == NULL)
177                 err(EX_NOINPUT, "%s%s", gmapfile, not);
178
179 #ifdef MAPSECURITY
180         /*
181          * Check that group and other don't have write permissions on
182          * this group mapfile, and that the file belongs to root.
183          */
184         if (fstat(fileno(gfp), &statbuf))
185                 err(EX_OSERR, "%s%s", gmapfile, not);
186         if (statbuf.st_mode & S_IWGRP || statbuf.st_mode & S_IWOTH) {
187                 strmode(statbuf.st_mode, buf);
188                 err(EX_NOPERM, "%s: improper write permissions (%s)%s",
189                     gmapfile, buf, not);
190         }
191         if (statbuf.st_uid != ROOTUSER)
192                 errx(EX_NOPERM, "%s does not belong to root%s", gmapfile, not);
193 #endif /* MAPSECURITY */
194
195         if ((fscanf(gfp, "%d\n", &gnentries)) != 1)
196                 errx(EX_DATAERR, "%s: nentries not found%s", gmapfile, not);
197         if (gnentries > MAPFILEENTRIES)
198                 errx(EX_DATAERR,
199                     "maximum number of entries is %d%s", GMAPFILEENTRIES, not);
200 #if 0
201         (void)printf("reading %d group entries\n", gnentries);
202 #endif
203
204         for (count = 0; count < gnentries; ++count)
205                 if ((fscanf(gfp, "%lu %lu\n",
206                     &(gmapdata[count][0]), &(gmapdata[count][1]))) != 2) {
207                         if (ferror(gfp))
208                                 err(EX_OSERR, "%s%s", gmapfile, not);
209                         if (feof(gfp))
210                                 errx(EX_DATAERR, "%s: unexpected end-of-file%s",
211                                     gmapfile, not);
212                         errx(EX_DATAERR, "%s: illegal format (line %d)%s",
213                             gmapfile, count + 2, not);
214                 }
215
216
217         /* Setup mount call args. */
218         args.target = source;
219         args.nentries = nentries;
220         args.mapdata = mapdata;
221         args.gnentries = gnentries;
222         args.gmapdata = gmapdata;
223
224         if (mount("umapfs", argv[1], mntflags, &args))
225                 err(1, NULL);
226         exit(0);
227 }
228
229 void
230 usage()
231 {
232         (void)fprintf(stderr,
233 "usage: mount_umapfs [-o options] -u uid-mapfile -g gid-mapfile target mount-point\n");
234         exit(EX_USAGE);
235 }