Commit | Line | Data |
---|---|---|
bcd4439c JH |
1 | /* |
2 | * Copyright (c) 1992 The Regents of the University of California | |
3 | * Copyright (c) 1990, 1992 Jan-Simon Pendry | |
4 | * All rights reserved. | |
5 | * | |
6 | * This code is derived from software donated to Berkeley by | |
7 | * Jan-Simon Pendry. | |
8 | * | |
9 | * %sccs.include.redist.c% | |
10 | * | |
11 | * @(#)mount_umap.c 5.1 (Berkeley) %G% | |
12 | */ | |
13 | ||
14 | #include <sys/param.h> | |
15 | #include <sys/mount.h> | |
16 | #include <umapfs/umap_info.h> | |
17 | ||
18 | #include <errno.h> | |
19 | #include <stdio.h> | |
20 | #include <unistd.h> | |
21 | #include <stdlib.h> | |
22 | #include <string.h> | |
23 | ||
24 | void usage __P((void)); | |
25 | ||
26 | #define ROOTUSER 0 | |
27 | ||
28 | /* This routine provides the user interface to mounting a umap layer. | |
29 | * It takes 4 mandatory parameters. The mandatory arguments are the place | |
30 | * where the next lower level is mounted, the place where the umap layer is to | |
31 | * be mounted, the name of the user mapfile, and the name of the group | |
32 | * mapfile. The routine checks the ownerships and permissions on the | |
33 | * mapfiles, then opens and reads them. Then it calls mount(), which | |
34 | * will, in turn, call the umap version of mount. | |
35 | */ | |
36 | ||
37 | int | |
38 | main(argc, argv) | |
39 | int argc; | |
40 | char *argv[]; | |
41 | { | |
42 | int ch, mntflags; | |
43 | int e, i, nentries, gnentries, count; | |
44 | int mapdata[MAPFILEENTRIES][2]; | |
45 | int gmapdata[GMAPFILEENTRIES][2]; | |
46 | int flags = M_NEWTYPE; | |
47 | char *fs_type="umap"; | |
48 | char *source, *target; | |
49 | char *mapfile, *gmapfile; | |
50 | struct _iobuf *fp, *gfp, *fopen(); | |
51 | struct stat statbuf; | |
52 | struct umap_mountargs args; | |
53 | ||
54 | mntflags = 0; | |
55 | while ((ch = getopt(argc, argv, "F:")) != EOF) | |
56 | switch(ch) { | |
57 | case 'F': | |
58 | mntflags = atoi(optarg); | |
59 | break; | |
60 | case '?': | |
61 | default: | |
62 | usage(); | |
63 | } | |
64 | argc -= optind; | |
65 | argv += optind; | |
66 | ||
67 | if (argc != 4) | |
68 | usage(); | |
69 | ||
70 | source = argv[i++]; | |
71 | target = argv[i++]; | |
72 | mapfile = argv[i++]; | |
73 | gmapfile = argv[i++]; | |
74 | ||
75 | /* | |
76 | * Check that group and other don't have write permissions on | |
77 | * this mapfile, and that the mapfile belongs to root. | |
78 | */ | |
79 | if ( stat(mapfile, &statbuf) ) | |
80 | { | |
81 | printf("mount_umap: can't stat %s\n",mapfile); | |
82 | perror("mount_umap: error status"); | |
83 | notMounted(); | |
84 | } | |
85 | ||
86 | if (statbuf.st_mode & S_IWGRP || statbuf.st_mode & S_IWOTH) | |
87 | { | |
88 | printf("mount_umap: Improper write permissions for %s, mode %x\n", | |
89 | mapfile, statbuf.st_mode); | |
90 | notMounted(); | |
91 | } | |
92 | ||
93 | if ( statbuf.st_uid != ROOTUSER ) | |
94 | { | |
95 | printf("mount_umap: %s does not belong to root\n", mapfile); | |
96 | notMounted(); | |
97 | } | |
98 | ||
99 | /* | |
100 | * Read in uid mapping data. | |
101 | */ | |
102 | ||
103 | if ((fp = fopen(mapfile, "r")) == NULL) { | |
104 | printf("mount_umap: can't open %s\n",mapfile); | |
105 | notMounted(); | |
106 | } | |
107 | fscanf(fp, "%d\n", &nentries); | |
108 | if (nentries > MAPFILEENTRIES) | |
109 | printf("mount_umap: nentries exceeds maximum\n"); | |
110 | else | |
111 | printf("reading %d entries\n", nentries); | |
112 | ||
113 | for(count = 0; count<nentries;count++) { | |
114 | if ((fscanf(fp, "%d %d\n", &(mapdata[count][0]), | |
115 | &(mapdata[count][1]))) == EOF) { | |
116 | printf("mount_umap: %s, premature eof\n",mapfile); | |
117 | notMounted(); | |
118 | } | |
119 | #if 0 | |
120 | /* fix a security hole */ | |
121 | if (mapdata[count][1] == 0) { | |
122 | printf("mount_umap: Mapping to UID 0 not allowed\n"); | |
123 | notMounted(); | |
124 | } | |
125 | #endif | |
126 | } | |
127 | ||
128 | /* | |
129 | * Check that group and other don't have write permissions on | |
130 | * this group mapfile, and that the file belongs to root. | |
131 | */ | |
132 | if ( stat(gmapfile, &statbuf) ) | |
133 | { | |
134 | printf("mount_umap: can't stat %s\n",gmapfile); | |
135 | perror("mount_umap: error status"); | |
136 | notMounted(); | |
137 | } | |
138 | ||
139 | if (statbuf.st_mode & S_IWGRP || statbuf.st_mode & S_IWOTH) | |
140 | { | |
141 | printf("mount_umap: Improper write permissions for %s, mode %x\n", | |
142 | gmapfile, statbuf.st_mode); | |
143 | } | |
144 | ||
145 | if ( statbuf.st_uid != ROOTUSER ) | |
146 | { | |
147 | printf("mount_umap: %s does not belong to root\n", mapfile); | |
148 | } | |
149 | ||
150 | /* | |
151 | * Read in gid mapping data. | |
152 | */ | |
153 | if ((gfp = fopen(gmapfile, "r")) == NULL) { | |
154 | printf("mount_umap: can't open %s\n",gmapfile); | |
155 | notMounted(); | |
156 | } | |
157 | fscanf(gfp, "%d\n", &gnentries); | |
158 | if (gnentries > GMAPFILEENTRIES) | |
159 | printf("mount_umap: gnentries exceeds maximum\n"); | |
160 | else | |
161 | printf("reading %d group entries\n", gnentries); | |
162 | ||
163 | for(count = 0; count<gnentries;count++) { | |
164 | if ((fscanf(gfp, "%d %d\n", &(gmapdata[count][0]), | |
165 | &(gmapdata[count][1]))) == EOF) { | |
166 | printf("mount_umap: %s, premature eof on group mapfile\n", | |
167 | gmapfile); | |
168 | notMounted(); | |
169 | } | |
170 | } | |
171 | ||
172 | ||
173 | /* | |
174 | * Setup mount call args. | |
175 | */ | |
176 | args.source = source; | |
177 | args.nentries = nentries; | |
178 | args.mapdata = &(mapdata[0][0]); | |
179 | args.gnentries = gnentries; | |
180 | args.gmapdata = &(gmapdata[0][0]); | |
181 | ||
182 | printf("calling mount_umap(%s,%d,<%s>)\n",target,flags, | |
183 | args.source); | |
184 | if (mount(MOUNT_UMAP, argv[1], mntflags, &args)) { | |
185 | (void)fprintf(stderr, "mount_umap: %s\n", strerror(errno)); | |
186 | } | |
187 | exit(0); | |
188 | } | |
189 | ||
190 | void | |
191 | usage() | |
192 | { | |
193 | (void)fprintf(stderr, | |
194 | "usage: mount_umap [ -F fsoptions ] target_fs mount_point\n"); | |
195 | exit(1); | |
196 | } | |
197 | ||
198 | void | |
199 | notMounted() | |
200 | { | |
201 | (void)fprintf(stderr, "file system not mounted\n"); | |
202 | } |