154900Sheideman /*
266477Sbostic * Copyright (c) 1992, 1993, 1994
361517Sbostic * The Regents of the University of California. All rights reserved.
454900Sheideman *
554900Sheideman * This code is derived from software donated to Berkeley by
654900Sheideman * Jan-Simon Pendry.
754900Sheideman *
854900Sheideman * %sccs.include.redist.c%
954900Sheideman */
1054900Sheideman
1166477Sbostic #ifndef lint
1266477Sbostic char copyright[] =
1366477Sbostic "@(#) Copyright (c) 1992, 1993, 1994\n\
1466477Sbostic The Regents of the University of California. All rights reserved.\n";
1566477Sbostic #endif /* not lint */
1666477Sbostic
1766477Sbostic #ifndef lint
18*68904Smckusick static char sccsid[] = "@(#)mount_umap.c 8.5 (Berkeley) 04/26/95";
1966477Sbostic #endif /* not lint */
2066477Sbostic
2154900Sheideman #include <sys/param.h>
2254900Sheideman #include <sys/mount.h>
2354950Sheideman #include <sys/stat.h>
2466477Sbostic
2555042Sbostic #include <miscfs/umapfs/umap.h>
2654900Sheideman
2766477Sbostic #include <err.h>
2854900Sheideman #include <stdio.h>
2954900Sheideman #include <stdlib.h>
3054900Sheideman #include <string.h>
3166477Sbostic #include <unistd.h>
3254900Sheideman
3366477Sbostic #include "mntopts.h"
3454900Sheideman
3554900Sheideman #define ROOTUSER 0
3666477Sbostic /*
3766477Sbostic * This define controls whether any user but the superuser can own and
3854950Sheideman * write mapfiles. If other users can, system security can be gravely
3954950Sheideman * compromised. If this is not a concern, undefine SECURITY.
4054950Sheideman */
4154950Sheideman #define MAPSECURITY 1
4254950Sheideman
4366477Sbostic /*
4466477Sbostic * This routine provides the user interface to mounting a umap layer.
4554900Sheideman * It takes 4 mandatory parameters. The mandatory arguments are the place
4654900Sheideman * where the next lower level is mounted, the place where the umap layer is to
4754900Sheideman * be mounted, the name of the user mapfile, and the name of the group
4854900Sheideman * mapfile. The routine checks the ownerships and permissions on the
4954900Sheideman * mapfiles, then opens and reads them. Then it calls mount(), which
5054900Sheideman * will, in turn, call the umap version of mount.
5154900Sheideman */
5254900Sheideman
5366477Sbostic struct mntopt mopts[] = {
5466477Sbostic MOPT_STDOPTS,
5566477Sbostic { NULL }
5666477Sbostic };
5766477Sbostic
5866477Sbostic void usage __P((void));
5966477Sbostic
6054900Sheideman int
main(argc,argv)6154900Sheideman main(argc, argv)
6254900Sheideman int argc;
6354900Sheideman char *argv[];
6454900Sheideman {
6566477Sbostic static char not[] = "; not mounted.";
6654900Sheideman struct stat statbuf;
6754950Sheideman struct umap_args args;
6866477Sbostic FILE *fp, *gfp;
6966477Sbostic u_long gmapdata[GMAPFILEENTRIES][2], mapdata[MAPFILEENTRIES][2];
7066477Sbostic int ch, count, gnentries, mntflags, nentries;
7166477Sbostic char *gmapfile, *mapfile, *source, *target, buf[20];
7254900Sheideman
7354900Sheideman mntflags = 0;
7466477Sbostic mapfile = gmapfile = NULL;
7566477Sbostic while ((ch = getopt(argc, argv, "g:o:u:")) != EOF)
7666477Sbostic switch (ch) {
7766477Sbostic case 'g':
7866477Sbostic gmapfile = optarg;
7954900Sheideman break;
8066477Sbostic case 'o':
81*68904Smckusick getmntopts(optarg, mopts, &mntflags, 0);
8266477Sbostic break;
8366477Sbostic case 'u':
8466477Sbostic mapfile = optarg;
8566477Sbostic break;
8654900Sheideman case '?':
8754900Sheideman default:
8854900Sheideman usage();
8954900Sheideman }
9054900Sheideman argc -= optind;
9154900Sheideman argv += optind;
9254900Sheideman
9366477Sbostic if (argc != 2 || mapfile == NULL || gmapfile == NULL)
9454900Sheideman usage();
9554900Sheideman
9666477Sbostic source = argv[0];
9766477Sbostic target = argv[1];
9854900Sheideman
9966477Sbostic /* Read in uid mapping data. */
10066477Sbostic if ((fp = fopen(mapfile, "r")) == NULL)
10166477Sbostic err(1, "%s%s", mapfile, not);
10266477Sbostic
10354950Sheideman #ifdef MAPSECURITY
10454900Sheideman /*
10554900Sheideman * Check that group and other don't have write permissions on
10654900Sheideman * this mapfile, and that the mapfile belongs to root.
10754900Sheideman */
10866477Sbostic if (fstat(fileno(fp), &statbuf))
10966477Sbostic err(1, "%s%s", mapfile, not);
11065490Spendry if (statbuf.st_mode & S_IWGRP || statbuf.st_mode & S_IWOTH) {
11166477Sbostic strmode(statbuf.st_mode, buf);
11266477Sbostic err(1, "%s: improper write permissions (%s)%s",
11366477Sbostic mapfile, buf, not);
11454900Sheideman }
11566477Sbostic if (statbuf.st_uid != ROOTUSER)
11666477Sbostic errx(1, "%s does not belong to root%s", mapfile, not);
11766477Sbostic #endif /* MAPSECURITY */
11854900Sheideman
11966477Sbostic if ((fscanf(fp, "%d\n", &nentries)) != 1)
12066477Sbostic errx(1, "%s: nentries not found%s", mapfile, not);
12154900Sheideman if (nentries > MAPFILEENTRIES)
12266477Sbostic errx(1,
12366477Sbostic "maximum number of entries is %d%s", MAPFILEENTRIES, not);
12454954Sheideman #if 0
12566477Sbostic (void)printf("reading %d entries\n", nentries);
12654954Sheideman #endif
12766477Sbostic for (count = 0; count < nentries; ++count) {
12866477Sbostic if ((fscanf(fp, "%lu %lu\n",
12966477Sbostic &(mapdata[count][0]), &(mapdata[count][1]))) != 2) {
13066477Sbostic if (ferror(fp))
13166477Sbostic err(1, "%s%s", mapfile, not);
13266477Sbostic if (feof(fp))
13366477Sbostic errx(1, "%s: unexpected end-of-file%s",
13466477Sbostic mapfile, not);
13566477Sbostic errx(1, "%s: illegal format (line %d)%s",
13666477Sbostic mapfile, count + 2, not);
13754900Sheideman }
13854900Sheideman #if 0
13966477Sbostic /* Fix a security hole. */
14066477Sbostic if (mapdata[count][1] == 0)
14166477Sbostic errx(1, "mapping id 0 not permitted (line %d)%s",
14266477Sbostic count + 2, not);
14354900Sheideman #endif
14454900Sheideman }
14554900Sheideman
14666477Sbostic /* Read in gid mapping data. */
14766477Sbostic if ((gfp = fopen(gmapfile, "r")) == NULL)
14866477Sbostic err(1, "%s%s", gmapfile, not);
14966477Sbostic
15066477Sbostic #ifdef MAPSECURITY
15154900Sheideman /*
15254900Sheideman * Check that group and other don't have write permissions on
15354900Sheideman * this group mapfile, and that the file belongs to root.
15454900Sheideman */
15566477Sbostic if (fstat(fileno(gfp), &statbuf))
15666477Sbostic err(1, "%s%s", gmapfile, not);
15765490Spendry if (statbuf.st_mode & S_IWGRP || statbuf.st_mode & S_IWOTH) {
15866477Sbostic strmode(statbuf.st_mode, buf);
15966477Sbostic err(1, "%s: improper write permissions (%s)%s",
16066477Sbostic gmapfile, buf, not);
16154900Sheideman }
16266477Sbostic if (statbuf.st_uid != ROOTUSER)
16366477Sbostic errx(1, "%s does not belong to root%s", gmapfile, not);
16466477Sbostic #endif /* MAPSECURITY */
16554900Sheideman
16667382Smkm if ((fscanf(gfp, "%d\n", &gnentries)) != 1)
16766477Sbostic errx(1, "nentries not found%s", gmapfile, not);
16866477Sbostic if (gnentries > MAPFILEENTRIES)
16966477Sbostic errx(1,
17066477Sbostic "maximum number of entries is %d%s", GMAPFILEENTRIES, not);
17154954Sheideman #if 0
17266477Sbostic (void)printf("reading %d group entries\n", gnentries);
17354954Sheideman #endif
17454900Sheideman
17566477Sbostic for (count = 0; count < gnentries; ++count)
17667382Smkm if ((fscanf(gfp, "%lu %lu\n",
17766477Sbostic &(gmapdata[count][0]), &(gmapdata[count][1]))) != 2) {
17867382Smkm if (ferror(gfp))
17966477Sbostic err(1, "%s%s", gmapfile, not);
18067382Smkm if (feof(gfp))
18166477Sbostic errx(1, "%s: unexpected end-of-file%s",
18266477Sbostic gmapfile, not);
18366477Sbostic errx(1, "%s: illegal format (line %d)%s",
18466477Sbostic gmapfile, count + 2, not);
18554900Sheideman }
18654900Sheideman
18754900Sheideman
18866477Sbostic /* Setup mount call args. */
18954950Sheideman args.target = source;
19054900Sheideman args.nentries = nentries;
19165490Spendry args.mapdata = mapdata;
19254900Sheideman args.gnentries = gnentries;
19365490Spendry args.gmapdata = gmapdata;
19454900Sheideman
195*68904Smckusick if (mount("umap", argv[1], mntflags, &args))
19666477Sbostic err(1, NULL);
19754900Sheideman exit(0);
19854900Sheideman }
19954900Sheideman
20054900Sheideman void
usage()20154900Sheideman usage()
20254900Sheideman {
20354900Sheideman (void)fprintf(stderr,
20466477Sbostic "usage: mount_umap [-o options] -u usermap -g groupmap target_fs mount_point\n");
20554900Sheideman exit(1);
20654900Sheideman }
207