Home | History | Annotate | Line # | Download | only in mount_umap
mount_umap.c revision 1.4
      1 /*	$NetBSD: mount_umap.c,v 1.4 1995/04/24 13:23:11 cgd Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1992, 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software donated to Berkeley by
      8  * Jan-Simon Pendry.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     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. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  */
     38 
     39 #ifndef lint
     40 char copyright[] =
     41 "@(#) Copyright (c) 1992, 1993, 1994\n\
     42 	The Regents of the University of California.  All rights reserved.\n";
     43 #endif /* not lint */
     44 
     45 #ifndef lint
     46 #if 0
     47 static char sccsid[] = "@(#)mount_umap.c	8.3 (Berkeley) 3/27/94";
     48 #else
     49 static char rcsid[] = "$NetBSD: mount_umap.c,v 1.4 1995/04/24 13:23:11 cgd Exp $";
     50 #endif
     51 #endif /* not lint */
     52 
     53 #include <sys/param.h>
     54 #include <sys/mount.h>
     55 #include <sys/stat.h>
     56 
     57 #include <miscfs/umapfs/umap.h>
     58 
     59 #include <err.h>
     60 #include <stdio.h>
     61 #include <stdlib.h>
     62 #include <string.h>
     63 #include <unistd.h>
     64 
     65 #include "mntopts.h"
     66 
     67 #define ROOTUSER 0
     68 /*
     69  * This define controls whether any user but the superuser can own and
     70  * write mapfiles.  If other users can, system security can be gravely
     71  * compromised.  If this is not a concern, undefine SECURITY.
     72  */
     73 #define MAPSECURITY 1
     74 
     75 /*
     76  * This routine provides the user interface to mounting a umap layer.
     77  * It takes 4 mandatory parameters.  The mandatory arguments are the place
     78  * where the next lower level is mounted, the place where the umap layer is to
     79  * be mounted, the name of the user mapfile, and the name of the group
     80  * mapfile.  The routine checks the ownerships and permissions on the
     81  * mapfiles, then opens and reads them.  Then it calls mount(), which
     82  * will, in turn, call the umap version of mount.
     83  */
     84 
     85 struct mntopt mopts[] = {
     86 	MOPT_STDOPTS,
     87 	{ NULL }
     88 };
     89 
     90 void	usage __P((void));
     91 
     92 int
     93 main(argc, argv)
     94 	int argc;
     95 	char *argv[];
     96 {
     97 	static char not[] = "; not mounted.";
     98 	struct stat statbuf;
     99 	struct umap_args args;
    100 	FILE *fp, *gfp;
    101 	long d1, d2;
    102 	u_long mapdata[MAPFILEENTRIES][2];
    103 	u_long gmapdata[GMAPFILEENTRIES][2];
    104 	int ch, count, gnentries, mntflags, nentries;
    105 	char *gmapfile, *mapfile, *source, *target, buf[20];
    106 
    107 	mntflags = 0;
    108 	mapfile = gmapfile = NULL;
    109 	while ((ch = getopt(argc, argv, "g:o:u:")) != EOF)
    110 		switch (ch) {
    111 		case 'g':
    112 			gmapfile = optarg;
    113 			break;
    114 		case 'o':
    115 			getmntopts(optarg, mopts, &mntflags);
    116 			break;
    117 		case 'u':
    118 			mapfile = optarg;
    119 			break;
    120 		case '?':
    121 		default:
    122 			usage();
    123 		}
    124 	argc -= optind;
    125 	argv += optind;
    126 
    127 	if (argc != 2 || mapfile == NULL || gmapfile == NULL)
    128 		usage();
    129 
    130 	source = argv[0];
    131 	target = argv[1];
    132 
    133 	/* Read in uid mapping data. */
    134 	if ((fp = fopen(mapfile, "r")) == NULL)
    135 		err(1, "%s%s", mapfile, not);
    136 
    137 #ifdef MAPSECURITY
    138 	/*
    139 	 * Check that group and other don't have write permissions on
    140 	 * this mapfile, and that the mapfile belongs to root.
    141 	 */
    142 	if (fstat(fileno(fp), &statbuf))
    143 		err(1, "%s%s", mapfile, not);
    144 	if (statbuf.st_mode & S_IWGRP || statbuf.st_mode & S_IWOTH) {
    145 		strmode(statbuf.st_mode, buf);
    146 		err(1, "%s: improper write permissions (%s)%s",
    147 		    mapfile, buf, not);
    148 	}
    149 	if (statbuf.st_uid != ROOTUSER)
    150 		errx(1, "%s does not belong to root%s", mapfile, not);
    151 #endif /* MAPSECURITY */
    152 
    153 	if ((fscanf(fp, "%d\n", &nentries)) != 1)
    154 		errx(1, "%s: nentries not found%s", mapfile, not);
    155 	if (nentries > MAPFILEENTRIES)
    156 		errx(1,
    157 		    "maximum number of entries is %d%s", MAPFILEENTRIES, not);
    158 #if 0
    159 	(void)printf("reading %d entries\n", nentries);
    160 #endif
    161 	for (count = 0; count < nentries; ++count) {
    162 		if ((fscanf(fp, "%lu %lu\n", &d1, &d2)) != 2) {
    163 			if (ferror(fp))
    164 				err(1, "%s%s", mapfile, not);
    165 			if (feof(fp))
    166 				errx(1, "%s: unexpected end-of-file%s",
    167 				    mapfile, not);
    168 			errx(1, "%s: illegal format (line %d)%s",
    169 			    mapfile, count + 2, not);
    170 		}
    171 		mapdata[count][0] = d1;
    172 		mapdata[count][1] = d2;
    173 #if 0
    174 		/* Fix a security hole. */
    175 		if (mapdata[count][1] == 0)
    176 			errx(1, "mapping id 0 not permitted (line %d)%s",
    177 			    count + 2, not);
    178 #endif
    179 	}
    180 
    181 	/* Read in gid mapping data. */
    182 	if ((gfp = fopen(gmapfile, "r")) == NULL)
    183 		err(1, "%s%s", gmapfile, not);
    184 
    185 #ifdef MAPSECURITY
    186 	/*
    187 	 * Check that group and other don't have write permissions on
    188 	 * this group mapfile, and that the file belongs to root.
    189 	 */
    190 	if (fstat(fileno(gfp), &statbuf))
    191 		err(1, "%s%s", gmapfile, not);
    192 	if (statbuf.st_mode & S_IWGRP || statbuf.st_mode & S_IWOTH) {
    193 		strmode(statbuf.st_mode, buf);
    194 		err(1, "%s: improper write permissions (%s)%s",
    195 		    gmapfile, buf, not);
    196 	}
    197 	if (statbuf.st_uid != ROOTUSER)
    198 		errx(1, "%s does not belong to root%s", gmapfile, not);
    199 #endif /* MAPSECURITY */
    200 
    201 	if ((fscanf(gfp, "%d\n", &gnentries)) != 1)
    202 		errx(1, "nentries not found%s", gmapfile, not);
    203 	if (gnentries > MAPFILEENTRIES)
    204 		errx(1,
    205 		    "maximum number of entries is %d%s", GMAPFILEENTRIES, not);
    206 #if 0
    207 	(void)printf("reading %d group entries\n", gnentries);
    208 #endif
    209 
    210 	for (count = 0; count < gnentries; ++count) {
    211 		if ((fscanf(gfp, "%lu %lu\n", &d1, &d2)) != 2) {
    212 			if (ferror(gfp))
    213 				err(1, "%s%s", gmapfile, not);
    214 			if (feof(gfp))
    215 				errx(1, "%s: unexpected end-of-file%s",
    216 				    gmapfile, not);
    217 			errx(1, "%s: illegal format (line %d)%s",
    218 			    gmapfile, count + 2, not);
    219 		}
    220 		gmapdata[count][0] = d1;
    221 		gmapdata[count][1] = d2;
    222 	}
    223 
    224 
    225 	/* Setup mount call args. */
    226 	args.target = source;
    227 	args.nentries = nentries;
    228 	args.mapdata = mapdata;
    229 	args.gnentries = gnentries;
    230 	args.gmapdata = gmapdata;
    231 
    232 	if (mount(MOUNT_UMAP, argv[1], mntflags, &args))
    233 		err(1, NULL);
    234 	exit(0);
    235 }
    236 
    237 void
    238 usage()
    239 {
    240 	(void)fprintf(stderr,
    241 "usage: mount_umap [-o options] -u usermap -g groupmap target_fs mount_point\n");
    242 	exit(1);
    243 }
    244