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