K&R style function removal. Update functions to ANSI style.
[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.4 2003/09/28 14:39:20 hmp Exp $
40  */
41
42 #include <sys/param.h>
43 #include <sys/mount.h>
44 #include <sys/stat.h>
45
46 #include <vfs/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(int argc, char **argv)
83 {
84         static char not[] = "; not mounted";
85         struct stat statbuf;
86         struct umap_args args;
87         FILE *fp, *gfp;
88         u_long gmapdata[GMAPFILEENTRIES][2], mapdata[MAPFILEENTRIES][2];
89         int ch, count, gnentries, mntflags, nentries;
90         char *gmapfile, *mapfile, buf[20];
91         char source[MAXPATHLEN], target[MAXPATHLEN];
92         struct vfsconf vfc;
93         int error;
94
95         mntflags = 0;
96         mapfile = gmapfile = NULL;
97         while ((ch = getopt(argc, argv, "g:o:u:")) != -1)
98                 switch (ch) {
99                 case 'g':
100                         gmapfile = optarg;
101                         break;
102                 case 'o':
103                         getmntopts(optarg, mopts, &mntflags, 0);
104                         break;
105                 case 'u':
106                         mapfile = optarg;
107                         break;
108                 case '?':
109                 default:
110                         usage();
111                 }
112         argc -= optind;
113         argv += optind;
114
115         if (argc != 2 || mapfile == NULL || gmapfile == NULL)
116                 usage();
117
118         /* resolve both target and source with realpath(3) */
119         (void)checkpath(argv[0], source);
120         (void)checkpath(argv[1], target);
121
122         /* Read in uid mapping data. */
123         if ((fp = fopen(mapfile, "r")) == NULL)
124                 err(EX_NOINPUT, "%s%s", mapfile, not);
125
126 #ifdef MAPSECURITY
127         /*
128          * Check that group and other don't have write permissions on
129          * this mapfile, and that the mapfile belongs to root.
130          */
131         if (fstat(fileno(fp), &statbuf))
132                 err(EX_OSERR, "%s%s", mapfile, not);
133         if (statbuf.st_mode & S_IWGRP || statbuf.st_mode & S_IWOTH) {
134                 strmode(statbuf.st_mode, buf);
135                 err(EX_NOPERM, "%s: improper write permissions (%s)%s",
136                     mapfile, buf, not);
137         }
138         if (statbuf.st_uid != ROOTUSER)
139                 errx(EX_NOPERM, "%s does not belong to root%s", mapfile, not);
140 #endif /* MAPSECURITY */
141
142         if ((fscanf(fp, "%d\n", &nentries)) != 1)
143                 errx(EX_DATAERR, "%s: nentries not found%s", mapfile, not);
144         if (nentries > MAPFILEENTRIES)
145                 errx(EX_DATAERR,
146                     "maximum number of entries is %d%s", MAPFILEENTRIES, not);
147 #if 0
148         (void)printf("reading %d entries\n", nentries);
149 #endif
150         for (count = 0; count < nentries; ++count) {
151                 if ((fscanf(fp, "%lu %lu\n",
152                     &(mapdata[count][0]), &(mapdata[count][1]))) != 2) {
153                         if (ferror(fp))
154                                 err(EX_OSERR, "%s%s", mapfile, not);
155                         if (feof(fp))
156                                 errx(EX_DATAERR, "%s: unexpected end-of-file%s",
157                                     mapfile, not);
158                         errx(EX_DATAERR, "%s: illegal format (line %d)%s",
159                             mapfile, count + 2, not);
160                 }
161 #if 0
162                 /* Fix a security hole. */
163                 if (mapdata[count][1] == 0)
164                         errx(1, "mapping id 0 not permitted (line %d)%s",
165                             count + 2, not);
166 #endif
167         }
168
169         /* Read in gid mapping data. */
170         if ((gfp = fopen(gmapfile, "r")) == NULL)
171                 err(EX_NOINPUT, "%s%s", gmapfile, not);
172
173 #ifdef MAPSECURITY
174         /*
175          * Check that group and other don't have write permissions on
176          * this group mapfile, and that the file belongs to root.
177          */
178         if (fstat(fileno(gfp), &statbuf))
179                 err(EX_OSERR, "%s%s", gmapfile, not);
180         if (statbuf.st_mode & S_IWGRP || statbuf.st_mode & S_IWOTH) {
181                 strmode(statbuf.st_mode, buf);
182                 err(EX_NOPERM, "%s: improper write permissions (%s)%s",
183                     gmapfile, buf, not);
184         }
185         if (statbuf.st_uid != ROOTUSER)
186                 errx(EX_NOPERM, "%s does not belong to root%s", gmapfile, not);
187 #endif /* MAPSECURITY */
188
189         if ((fscanf(gfp, "%d\n", &gnentries)) != 1)
190                 errx(EX_DATAERR, "%s: nentries not found%s", gmapfile, not);
191         if (gnentries > MAPFILEENTRIES)
192                 errx(EX_DATAERR,
193                     "maximum number of entries is %d%s", GMAPFILEENTRIES, not);
194 #if 0
195         (void)printf("reading %d group entries\n", gnentries);
196 #endif
197
198         for (count = 0; count < gnentries; ++count)
199                 if ((fscanf(gfp, "%lu %lu\n",
200                     &(gmapdata[count][0]), &(gmapdata[count][1]))) != 2) {
201                         if (ferror(gfp))
202                                 err(EX_OSERR, "%s%s", gmapfile, not);
203                         if (feof(gfp))
204                                 errx(EX_DATAERR, "%s: unexpected end-of-file%s",
205                                     gmapfile, not);
206                         errx(EX_DATAERR, "%s: illegal format (line %d)%s",
207                             gmapfile, count + 2, not);
208                 }
209
210
211         /* Setup mount call args. */
212         args.target = source;
213         args.nentries = nentries;
214         args.mapdata = mapdata;
215         args.gnentries = gnentries;
216         args.gmapdata = gmapdata;
217
218         error = getvfsbyname("umap", &vfc);
219         if (error && vfsisloadable("umap")) {
220                 if(vfsload("umap"))
221                         err(1, "vfsload(umap)");
222                 endvfsent();
223                 error = getvfsbyname("umap", &vfc);
224         }
225         if (error)
226                 errx(1, "umap filesystem is not available");
227
228         if (mount(vfc.vfc_name, argv[1], mntflags, &args))
229                 err(1, NULL);
230         exit(0);
231 }
232
233 void
234 usage(void)
235 {
236         (void)fprintf(stderr,
237 "usage: mount_umap [-o options] -u usermap -g groupmap target_fs mount_point\n");
238         exit(EX_USAGE);
239 }