Home | History | Annotate | Line # | Download | only in mount_umap
mount_umap.c revision 1.23.42.1
      1 /*	$NetBSD: mount_umap.c,v 1.23.42.1 2020/04/13 08:03:21 martin 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. 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.
     21  *
     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
     32  * SUCH DAMAGE.
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 #ifndef lint
     37 __COPYRIGHT("@(#) Copyright (c) 1992, 1993, 1994\
     38  The Regents of the University of California.  All rights reserved.");
     39 #endif /* not lint */
     40 
     41 #ifndef lint
     42 #if 0
     43 static char sccsid[] = "@(#)mount_umap.c	8.5 (Berkeley) 4/26/95";
     44 #else
     45 __RCSID("$NetBSD: mount_umap.c,v 1.23.42.1 2020/04/13 08:03:21 martin Exp $");
     46 #endif
     47 #endif /* not lint */
     48 
     49 #include <sys/param.h>
     50 #include <sys/mount.h>
     51 #include <sys/stat.h>
     52 
     53 #include <miscfs/umapfs/umap.h>
     54 
     55 #include <err.h>
     56 #include <stdio.h>
     57 #include <stdlib.h>
     58 #include <string.h>
     59 #include <unistd.h>
     60 
     61 #include <mntopts.h>
     62 
     63 #define ROOTUSER 0
     64 /*
     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.
     68  */
     69 #define MAPSECURITY 1
     70 
     71 /*
     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.
     79  */
     80 
     81 static const struct mntopt mopts[] = {
     82 	MOPT_STDOPTS,
     83 	MOPT_NULL,
     84 };
     85 
     86 int	mount_umap(int argc, char **argv);
     87 __dead static void	usage(void);
     88 
     89 #ifndef MOUNT_NOMAIN
     90 int
     91 main(int argc, char **argv)
     92 {
     93 	return mount_umap(argc, argv);
     94 }
     95 #endif
     96 
     97 int
     98 mount_umap(int argc, char *argv[])
     99 {
    100 	static char not[] = "; not mounted.";
    101 	struct stat statbuf;
    102 	struct umap_args args;
    103 	FILE *fp, *gfp;
    104 	long d1, d2;
    105 	u_long mapdata[MAPFILEENTRIES][2];
    106 	u_long gmapdata[GMAPFILEENTRIES][2];
    107 	u_long fsid;
    108 	int ch, count, gnentries, mntflags, nentries;
    109 	char *gmapfile, *mapfile, buf[20];
    110 	char source[MAXPATHLEN], target[MAXPATHLEN];
    111 	mntoptparse_t mp;
    112 
    113 	fsid = mntflags = 0;
    114 	mapfile = gmapfile = NULL;
    115 	while ((ch = getopt(argc, argv, "g:i:o:u:")) != -1)
    116 		switch (ch) {
    117 		case 'g':
    118 			gmapfile = optarg;
    119 			break;
    120 		case 'i':
    121 			fsid = strtoul(optarg, NULL, 16);
    122 			break;
    123 		case 'o':
    124 			mp = getmntopts(optarg, mopts, &mntflags, 0);
    125 			if (mp == NULL)
    126 				err(1, "getmntopts");
    127 			freemntopts(mp);
    128 			break;
    129 		case 'u':
    130 			mapfile = optarg;
    131 			break;
    132 		case '?':
    133 		default:
    134 			usage();
    135 		}
    136 	argc -= optind;
    137 	argv += optind;
    138 
    139 	if (argc != 2 || mapfile == NULL || gmapfile == NULL)
    140 		usage();
    141 
    142 	if (realpath(argv[0], source) == NULL)        /* Check source path */
    143 		err(1, "realpath %s", argv[0]);
    144 	if (strncmp(argv[0], source, MAXPATHLEN)) {
    145 		warnx("\"%s\" is a relative path.", argv[0]);
    146 		warnx("using \"%s\" instead.", source);
    147 	}
    148 
    149 	if (realpath(argv[1], target) == NULL)        /* Check mounton path */
    150 		err(1, "realpath %s", argv[1]);
    151 	if (strncmp(argv[1], target, MAXPATHLEN)) {
    152 		warnx("\"%s\" is a relative path.", argv[1]);
    153 		warnx("using \"%s\" instead.", target);
    154 	}
    155 
    156 	/* Read in uid mapping data. */
    157 	if ((fp = fopen(mapfile, "r")) == NULL)
    158 		err(1, "%s%s", mapfile, not);
    159 
    160 #ifdef MAPSECURITY
    161 	/*
    162 	 * Check that group and other don't have write permissions on
    163 	 * this mapfile, and that the mapfile belongs to root.
    164 	 */
    165 	if (fstat(fileno(fp), &statbuf))
    166 		err(1, "%s%s", mapfile, not);
    167 	if (statbuf.st_mode & S_IWGRP || statbuf.st_mode & S_IWOTH) {
    168 		strmode(statbuf.st_mode, buf);
    169 		err(1, "%s: improper write permissions (%s)%s",
    170 		    mapfile, buf, not);
    171 	}
    172 	if (statbuf.st_uid != ROOTUSER)
    173 		errx(1, "%s does not belong to root%s", mapfile, not);
    174 #endif /* MAPSECURITY */
    175 
    176 	if ((fscanf(fp, "%d\n", &nentries)) != 1)
    177 		errx(1, "%s: nentries not found%s", mapfile, not);
    178 	if (nentries > MAPFILEENTRIES)
    179 		errx(1,
    180 		    "maximum number of entries is %d%s", MAPFILEENTRIES, not);
    181 #if 0
    182 	(void)printf("reading %d entries\n", nentries);
    183 #endif
    184 	for (count = 0; count < nentries; ++count) {
    185 		if ((fscanf(fp, "%lu %lu\n", &d1, &d2)) != 2) {
    186 			if (ferror(fp))
    187 				err(1, "%s%s", mapfile, not);
    188 			if (feof(fp))
    189 				errx(1, "%s: unexpected end-of-file%s",
    190 				    mapfile, not);
    191 			errx(1, "%s: illegal format (line %d)%s",
    192 			    mapfile, count + 2, not);
    193 		}
    194 		mapdata[count][0] = d1;
    195 		mapdata[count][1] = d2;
    196 #if 0
    197 		/* Fix a security hole. */
    198 		if (mapdata[count][1] == 0)
    199 			errx(1, "mapping id 0 not permitted (line %d)%s",
    200 			    count + 2, not);
    201 #endif
    202 	}
    203 
    204 	/* Read in gid mapping data. */
    205 	if ((gfp = fopen(gmapfile, "r")) == NULL)
    206 		err(1, "%s%s", gmapfile, not);
    207 
    208 #ifdef MAPSECURITY
    209 	/*
    210 	 * Check that group and other don't have write permissions on
    211 	 * this group mapfile, and that the file belongs to root.
    212 	 */
    213 	if (fstat(fileno(gfp), &statbuf))
    214 		err(1, "%s%s", gmapfile, not);
    215 	if (statbuf.st_mode & S_IWGRP || statbuf.st_mode & S_IWOTH) {
    216 		strmode(statbuf.st_mode, buf);
    217 		err(1, "%s: improper write permissions (%s)%s",
    218 		    gmapfile, buf, not);
    219 	}
    220 	if (statbuf.st_uid != ROOTUSER)
    221 		errx(1, "%s does not belong to root%s", gmapfile, not);
    222 #endif /* MAPSECURITY */
    223 
    224 	if ((fscanf(gfp, "%d\n", &gnentries)) != 1)
    225 		errx(1, "nentries not found%s", not);
    226 	if (gnentries > MAPFILEENTRIES)
    227 		errx(1,
    228 		    "maximum number of entries is %d%s", GMAPFILEENTRIES, not);
    229 #if 0
    230 	(void)printf("reading %d group entries\n", gnentries);
    231 #endif
    232 
    233 	for (count = 0; count < gnentries; ++count) {
    234 		if ((fscanf(gfp, "%lu %lu\n",
    235 		    &(gmapdata[count][0]), &(gmapdata[count][1]))) != 2) {
    236 			if (ferror(gfp))
    237 				err(1, "%s%s", gmapfile, not);
    238 			if (feof(gfp))
    239 				errx(1, "%s: unexpected end-of-file%s",
    240 				    gmapfile, not);
    241 			errx(1, "%s: illegal format (line %d)%s",
    242 			    gmapfile, count + 2, not);
    243 		}
    244 	}
    245 
    246 
    247 	/* Setup mount call args. */
    248 	args.la.target = source;
    249 	args.nentries = nentries;
    250 	args.mapdata = mapdata;
    251 	args.gnentries = gnentries;
    252 	args.gmapdata = gmapdata;
    253 	args.fsid = fsid;
    254 
    255 	if (mount(MOUNT_UMAP, target, mntflags, &args, sizeof args) == -1)
    256 		err(1, "%s on %s", source, target);
    257 	if (mntflags & MNT_GETARGS) {
    258 		printf("nentries=%d, gnentries=%d\n", args.nentries,
    259 		    args.gnentries);
    260 	}
    261 	exit(0);
    262 }
    263 
    264 static void
    265 usage(void)
    266 {
    267 	(void)fprintf(stderr,
    268 "usage: mount_umap [-i fsid] [-o options] -g groupmap -u usermap target_fs mount_point\n");
    269 	exit(1);
    270 }
    271