mountd.c revision 1.52 1 1.52 christos /* $NetBSD: mountd.c,v 1.52 1999/01/12 15:11:53 christos Exp $ */
2 1.19 cgd
3 1.1 cgd /*
4 1.12 mycroft * Copyright (c) 1989, 1993
5 1.12 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.12 mycroft * Herb Hasler and Rick Macklem at The University of Guelph.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.1 cgd * 3. All advertising materials mentioning features or use of this software
19 1.1 cgd * must display the following acknowledgement:
20 1.1 cgd * This product includes software developed by the University of
21 1.1 cgd * California, Berkeley and its contributors.
22 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
23 1.1 cgd * may be used to endorse or promote products derived from this software
24 1.1 cgd * without specific prior written permission.
25 1.1 cgd *
26 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.1 cgd * SUCH DAMAGE.
37 1.1 cgd */
38 1.1 cgd
39 1.36 fvdl
40 1.36 fvdl /*
41 1.36 fvdl * XXX The ISO support can't possibly work..
42 1.36 fvdl */
43 1.36 fvdl
44 1.41 lukem #include <sys/cdefs.h>
45 1.1 cgd #ifndef lint
46 1.41 lukem __COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
47 1.41 lukem The Regents of the University of California. All rights reserved.\n");
48 1.47 christos #endif /* not lint */
49 1.1 cgd
50 1.1 cgd #ifndef lint
51 1.19 cgd #if 0
52 1.47 christos static char sccsid[] = "@(#)mountd.c 8.15 (Berkeley) 5/1/95";
53 1.19 cgd #else
54 1.52 christos __RCSID("$NetBSD: mountd.c,v 1.52 1999/01/12 15:11:53 christos Exp $");
55 1.19 cgd #endif
56 1.47 christos #endif /* not lint */
57 1.1 cgd
58 1.1 cgd #include <sys/param.h>
59 1.12 mycroft #include <sys/file.h>
60 1.1 cgd #include <sys/ioctl.h>
61 1.1 cgd #include <sys/mount.h>
62 1.1 cgd #include <sys/socket.h>
63 1.12 mycroft #include <sys/stat.h>
64 1.21 jtc #include <syslog.h>
65 1.12 mycroft #include <sys/ucred.h>
66 1.12 mycroft
67 1.1 cgd #include <rpc/rpc.h>
68 1.1 cgd #include <rpc/pmap_clnt.h>
69 1.1 cgd #include <rpc/pmap_prot.h>
70 1.45 christos #include <rpcsvc/mount.h>
71 1.12 mycroft #ifdef ISO
72 1.12 mycroft #include <netiso/iso.h>
73 1.12 mycroft #endif
74 1.12 mycroft #include <nfs/rpcv2.h>
75 1.31 fvdl #include <nfs/nfsproto.h>
76 1.43 fvdl #include <nfs/nfs.h>
77 1.43 fvdl #include <nfs/nfsmount.h>
78 1.43 fvdl
79 1.43 fvdl #include <ufs/ufs/ufsmount.h>
80 1.43 fvdl #include <isofs/cd9660/cd9660_mount.h>
81 1.43 fvdl #include <msdosfs/msdosfsmount.h>
82 1.43 fvdl #include <adosfs/adosfs.h>
83 1.12 mycroft
84 1.12 mycroft #include <arpa/inet.h>
85 1.12 mycroft
86 1.12 mycroft #include <ctype.h>
87 1.12 mycroft #include <errno.h>
88 1.12 mycroft #include <grp.h>
89 1.12 mycroft #include <netdb.h>
90 1.12 mycroft #include <pwd.h>
91 1.41 lukem #include <netgroup.h>
92 1.12 mycroft #include <signal.h>
93 1.12 mycroft #include <stdio.h>
94 1.12 mycroft #include <stdlib.h>
95 1.12 mycroft #include <string.h>
96 1.12 mycroft #include <unistd.h>
97 1.47 christos #include <netgroup.h>
98 1.47 christos #include <util.h>
99 1.1 cgd #include "pathnames.h"
100 1.45 christos #ifdef KERBEROS
101 1.45 christos #include <kerberosIV/krb.h>
102 1.45 christos #include "kuid.h"
103 1.45 christos #endif
104 1.1 cgd
105 1.12 mycroft #include <stdarg.h>
106 1.12 mycroft
107 1.1 cgd /*
108 1.1 cgd * Structures for keeping the mount list and export list
109 1.1 cgd */
110 1.1 cgd struct mountlist {
111 1.1 cgd struct mountlist *ml_next;
112 1.47 christos char ml_host[RPCMNT_NAMELEN + 1];
113 1.47 christos char ml_dirp[RPCMNT_PATHLEN + 1];
114 1.47 christos int ml_flag;/* XXX more flags (same as dp_flag) */
115 1.1 cgd };
116 1.1 cgd
117 1.12 mycroft struct dirlist {
118 1.47 christos struct dirlist *dp_left;
119 1.47 christos struct dirlist *dp_right;
120 1.47 christos int dp_flag;
121 1.47 christos struct hostlist *dp_hosts; /* List of hosts this dir exported to */
122 1.47 christos char dp_dirp[1]; /* Actually malloc'd to size of dir */
123 1.12 mycroft };
124 1.12 mycroft /* dp_flag bits */
125 1.12 mycroft #define DP_DEFSET 0x1
126 1.31 fvdl #define DP_HOSTSET 0x2
127 1.31 fvdl #define DP_KERB 0x4
128 1.37 fvdl #define DP_NORESMNT 0x8
129 1.12 mycroft
130 1.1 cgd struct exportlist {
131 1.1 cgd struct exportlist *ex_next;
132 1.47 christos struct dirlist *ex_dirl;
133 1.47 christos struct dirlist *ex_defdir;
134 1.47 christos int ex_flag;
135 1.47 christos fsid_t ex_fs;
136 1.47 christos char *ex_fsdir;
137 1.47 christos char *ex_indexfile;
138 1.12 mycroft };
139 1.12 mycroft /* ex_flag bits */
140 1.12 mycroft #define EX_LINKED 0x1
141 1.12 mycroft
142 1.12 mycroft struct netmsk {
143 1.47 christos u_int32_t nt_net;
144 1.47 christos u_int32_t nt_mask;
145 1.47 christos char *nt_name;
146 1.12 mycroft };
147 1.12 mycroft
148 1.12 mycroft union grouptypes {
149 1.12 mycroft struct hostent *gt_hostent;
150 1.47 christos struct netmsk gt_net;
151 1.12 mycroft #ifdef ISO
152 1.12 mycroft struct sockaddr_iso *gt_isoaddr;
153 1.12 mycroft #endif
154 1.1 cgd };
155 1.1 cgd
156 1.1 cgd struct grouplist {
157 1.47 christos int gr_type;
158 1.12 mycroft union grouptypes gr_ptr;
159 1.1 cgd struct grouplist *gr_next;
160 1.12 mycroft };
161 1.12 mycroft /* Group types */
162 1.12 mycroft #define GT_NULL 0x0
163 1.12 mycroft #define GT_HOST 0x1
164 1.12 mycroft #define GT_NET 0x2
165 1.12 mycroft #define GT_ISO 0x4
166 1.12 mycroft
167 1.12 mycroft struct hostlist {
168 1.47 christos int ht_flag;/* Uses DP_xx bits */
169 1.12 mycroft struct grouplist *ht_grp;
170 1.47 christos struct hostlist *ht_next;
171 1.1 cgd };
172 1.1 cgd
173 1.31 fvdl struct fhreturn {
174 1.47 christos int fhr_flag;
175 1.47 christos int fhr_vers;
176 1.47 christos nfsfh_t fhr_fh;
177 1.31 fvdl };
178 1.31 fvdl
179 1.1 cgd /* Global defs */
180 1.47 christos static char *add_expdir __P((struct dirlist **, char *, int));
181 1.47 christos static void add_dlist __P((struct dirlist **, struct dirlist *,
182 1.47 christos struct grouplist *, int));
183 1.47 christos static void add_mlist __P((char *, char *, int));
184 1.52 christos static int check_dirpath __P((const char *, size_t, char *));
185 1.52 christos static int check_options __P((const char *, size_t, struct dirlist *));
186 1.47 christos static int chk_host __P((struct dirlist *, u_int32_t, int *, int *));
187 1.47 christos static int del_mlist __P((char *, char *, struct sockaddr *));
188 1.47 christos static struct dirlist *dirp_search __P((struct dirlist *, char *));
189 1.52 christos static int do_mount __P((const char *, size_t, struct exportlist *,
190 1.52 christos struct grouplist *, int, struct ucred *, char *, int, struct statfs *));
191 1.52 christos static int do_opt __P((const char *, size_t, char **, char **,
192 1.52 christos struct exportlist *, struct grouplist *, int *, int *, struct ucred *));
193 1.47 christos static struct exportlist *ex_search __P((fsid_t *));
194 1.49 christos static int parse_directory __P((const char *, size_t, struct grouplist *,
195 1.49 christos int, char *, struct exportlist **, struct statfs *));
196 1.47 christos static int parse_host_netgroup __P((const char *, size_t, struct exportlist *,
197 1.47 christos struct grouplist *, char *, int *, struct grouplist **));
198 1.49 christos static struct exportlist *get_exp __P((void));
199 1.47 christos static void free_dir __P((struct dirlist *));
200 1.47 christos static void free_exp __P((struct exportlist *));
201 1.47 christos static void free_grp __P((struct grouplist *));
202 1.47 christos static void free_host __P((struct hostlist *));
203 1.47 christos static void get_exportlist __P((int));
204 1.52 christos static int get_host __P((const char *, size_t, const char *,
205 1.52 christos struct grouplist *));
206 1.47 christos static struct hostlist *get_ht __P((void));
207 1.47 christos static void get_mountlist __P((void));
208 1.47 christos static int get_net __P((char *, struct netmsk *, int));
209 1.52 christos static void free_exp_grp __P((struct exportlist *, struct grouplist *));
210 1.47 christos static struct grouplist *get_grp __P((void));
211 1.47 christos static void hang_dirp __P((struct dirlist *, struct grouplist *,
212 1.47 christos struct exportlist *, int));
213 1.47 christos static void mntsrv __P((struct svc_req *, SVCXPRT *));
214 1.47 christos static void nextfield __P((char **, char **));
215 1.47 christos static void parsecred __P((char *, struct ucred *));
216 1.47 christos static int put_exlist __P((struct dirlist *, XDR *, struct dirlist *, int *));
217 1.47 christos static int scan_tree __P((struct dirlist *, u_int32_t));
218 1.47 christos static void send_umntall __P((int));
219 1.47 christos static int umntall_each __P((caddr_t, struct sockaddr_in *));
220 1.47 christos static int xdr_dir __P((XDR *, char *));
221 1.47 christos static int xdr_explist __P((XDR *, caddr_t));
222 1.47 christos static int xdr_fhs __P((XDR *, caddr_t));
223 1.47 christos static int xdr_mlist __P((XDR *, caddr_t));
224 1.47 christos static void *emalloc __P((size_t));
225 1.47 christos static char *estrdup __P((const char *));
226 1.52 christos #ifdef ISO
227 1.52 christos static int get_isoaddr __P((const char *, size_t, char *, struct grouplist *));
228 1.52 christos #endif
229 1.47 christos static struct exportlist *exphead;
230 1.47 christos static struct mountlist *mlhead;
231 1.47 christos static struct grouplist *grphead;
232 1.47 christos static char *exname;
233 1.47 christos static struct ucred def_anon = {
234 1.12 mycroft 1,
235 1.47 christos (uid_t) - 2,
236 1.47 christos (gid_t) - 2,
237 1.23 cgd 0,
238 1.47 christos {}
239 1.12 mycroft };
240 1.47 christos static int opt_flags;
241 1.12 mycroft /* Bits for above */
242 1.37 fvdl #define OP_MAPROOT 0x001
243 1.37 fvdl #define OP_MAPALL 0x002
244 1.37 fvdl #define OP_KERB 0x004
245 1.37 fvdl #define OP_MASK 0x008
246 1.37 fvdl #define OP_NET 0x010
247 1.37 fvdl #define OP_ISO 0x020
248 1.37 fvdl #define OP_ALLDIRS 0x040
249 1.37 fvdl #define OP_NORESPORT 0x080
250 1.37 fvdl #define OP_NORESMNT 0x100
251 1.12 mycroft
252 1.47 christos static int debug = 0;
253 1.47 christos #if 0
254 1.47 christos static void SYSLOG __P((int, const char *,...));
255 1.47 christos #endif
256 1.47 christos int main __P((int, char *[]));
257 1.1 cgd
258 1.1 cgd /*
259 1.1 cgd * Mountd server for NFS mount protocol as described in:
260 1.1 cgd * NFS: Network File System Protocol Specification, RFC1094, Appendix A
261 1.1 cgd * The optional arguments are the exports file name
262 1.1 cgd * default: _PATH_EXPORTS
263 1.28 thorpej * "-d" to enable debugging
264 1.1 cgd * and "-n" to allow nonroot mount.
265 1.1 cgd */
266 1.12 mycroft int
267 1.1 cgd main(argc, argv)
268 1.1 cgd int argc;
269 1.1 cgd char **argv;
270 1.1 cgd {
271 1.16 mycroft SVCXPRT *udptransp, *tcptransp;
272 1.44 mrg FILE *pidfile;
273 1.6 brezak int c;
274 1.1 cgd
275 1.41 lukem while ((c = getopt(argc, argv, "dnr")) != -1)
276 1.1 cgd switch (c) {
277 1.28 thorpej case 'd':
278 1.28 thorpej debug = 1;
279 1.28 thorpej break;
280 1.44 mrg /* Compatibility */
281 1.1 cgd case 'n':
282 1.31 fvdl case 'r':
283 1.31 fvdl break;
284 1.1 cgd default:
285 1.44 mrg fprintf(stderr, "Usage: mountd [-d] [export_file]\n");
286 1.1 cgd exit(1);
287 1.1 cgd };
288 1.1 cgd argc -= optind;
289 1.1 cgd argv += optind;
290 1.47 christos grphead = NULL;
291 1.47 christos exphead = NULL;
292 1.47 christos mlhead = NULL;
293 1.44 mrg if (argc == 1)
294 1.44 mrg exname = *argv;
295 1.44 mrg else
296 1.44 mrg exname = _PATH_EXPORTS;
297 1.12 mycroft openlog("mountd", LOG_PID, LOG_DAEMON);
298 1.12 mycroft if (debug)
299 1.47 christos (void)fprintf(stderr, "Getting export list.\n");
300 1.45 christos get_exportlist(0);
301 1.12 mycroft if (debug)
302 1.47 christos (void)fprintf(stderr, "Getting mount list.\n");
303 1.12 mycroft get_mountlist();
304 1.12 mycroft if (debug)
305 1.47 christos (void)fprintf(stderr, "Here we go.\n");
306 1.12 mycroft if (debug == 0) {
307 1.1 cgd daemon(0, 0);
308 1.47 christos (void)signal(SIGINT, SIG_IGN);
309 1.47 christos (void)signal(SIGQUIT, SIG_IGN);
310 1.1 cgd }
311 1.45 christos (void)signal(SIGHUP, get_exportlist);
312 1.45 christos (void)signal(SIGTERM, send_umntall);
313 1.44 mrg pidfile = fopen(_PATH_MOUNTDPID, "w");
314 1.44 mrg if (pidfile != NULL) {
315 1.47 christos (void)fprintf(pidfile, "%d\n", getpid());
316 1.47 christos (void)fclose(pidfile);
317 1.12 mycroft }
318 1.16 mycroft if ((udptransp = svcudp_create(RPC_ANYSOCK)) == NULL ||
319 1.16 mycroft (tcptransp = svctcp_create(RPC_ANYSOCK, 0, 0)) == NULL) {
320 1.12 mycroft syslog(LOG_ERR, "Can't create socket");
321 1.1 cgd exit(1);
322 1.1 cgd }
323 1.12 mycroft pmap_unset(RPCPROG_MNT, RPCMNT_VER1);
324 1.31 fvdl pmap_unset(RPCPROG_MNT, RPCMNT_VER3);
325 1.16 mycroft if (!svc_register(udptransp, RPCPROG_MNT, RPCMNT_VER1, mntsrv,
326 1.47 christos IPPROTO_UDP) ||
327 1.31 fvdl !svc_register(udptransp, RPCPROG_MNT, RPCMNT_VER3, mntsrv,
328 1.47 christos IPPROTO_UDP) ||
329 1.16 mycroft !svc_register(tcptransp, RPCPROG_MNT, RPCMNT_VER1, mntsrv,
330 1.47 christos IPPROTO_TCP) ||
331 1.31 fvdl !svc_register(tcptransp, RPCPROG_MNT, RPCMNT_VER3, mntsrv,
332 1.47 christos IPPROTO_TCP)) {
333 1.1 cgd syslog(LOG_ERR, "Can't register mount");
334 1.1 cgd exit(1);
335 1.1 cgd }
336 1.45 christos #ifdef KERBEROS
337 1.45 christos kuidinit();
338 1.45 christos #endif
339 1.1 cgd svc_run();
340 1.1 cgd syslog(LOG_ERR, "Mountd died");
341 1.1 cgd exit(1);
342 1.1 cgd }
343 1.1 cgd
344 1.1 cgd /*
345 1.1 cgd * The mount rpc service
346 1.1 cgd */
347 1.6 brezak void
348 1.1 cgd mntsrv(rqstp, transp)
349 1.12 mycroft struct svc_req *rqstp;
350 1.12 mycroft SVCXPRT *transp;
351 1.1 cgd {
352 1.12 mycroft struct exportlist *ep;
353 1.12 mycroft struct dirlist *dp;
354 1.31 fvdl struct fhreturn fhr;
355 1.47 christos struct stat stb;
356 1.47 christos struct statfs fsb;
357 1.1 cgd struct hostent *hp;
358 1.47 christos struct in_addr saddr;
359 1.47 christos u_short sport;
360 1.47 christos char rpcpath[RPCMNT_PATHLEN + 1], dirpath[MAXPATHLEN];
361 1.47 christos long bad = EACCES;
362 1.47 christos int defset, hostset, ret;
363 1.47 christos sigset_t sighup_mask;
364 1.1 cgd
365 1.47 christos (void)sigemptyset(&sighup_mask);
366 1.47 christos (void)sigaddset(&sighup_mask, SIGHUP);
367 1.36 fvdl saddr = transp->xp_raddr.sin_addr;
368 1.14 cgd sport = ntohs(transp->xp_raddr.sin_port);
369 1.47 christos hp = NULL;
370 1.45 christos #ifdef KERBEROS
371 1.45 christos kuidreset();
372 1.45 christos #endif
373 1.41 lukem ret = 0;
374 1.1 cgd switch (rqstp->rq_proc) {
375 1.1 cgd case NULLPROC:
376 1.47 christos if (!svc_sendreply(transp, xdr_void, NULL))
377 1.1 cgd syslog(LOG_ERR, "Can't send reply");
378 1.1 cgd return;
379 1.45 christos case MOUNTPROC_MNT:
380 1.12 mycroft if (!svc_getargs(transp, xdr_dir, rpcpath)) {
381 1.1 cgd svcerr_decode(transp);
382 1.1 cgd return;
383 1.1 cgd }
384 1.12 mycroft /*
385 1.14 cgd * Get the real pathname and make sure it is a file or
386 1.14 cgd * directory that exists.
387 1.12 mycroft */
388 1.12 mycroft if (realpath(rpcpath, dirpath) == 0 ||
389 1.12 mycroft stat(dirpath, &stb) < 0 ||
390 1.12 mycroft (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) ||
391 1.12 mycroft statfs(dirpath, &fsb) < 0) {
392 1.47 christos (void)chdir("/"); /* Just in case realpath doesn't */
393 1.12 mycroft if (debug)
394 1.47 christos (void)fprintf(stderr, "stat failed on %s\n",
395 1.47 christos dirpath);
396 1.47 christos if (!svc_sendreply(transp, xdr_long, (caddr_t) &bad))
397 1.1 cgd syslog(LOG_ERR, "Can't send reply");
398 1.1 cgd return;
399 1.1 cgd }
400 1.1 cgd /* Check in the exports list */
401 1.47 christos (void)sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
402 1.12 mycroft ep = ex_search(&fsb.f_fsid);
403 1.31 fvdl hostset = defset = 0;
404 1.36 fvdl if (ep && (chk_host(ep->ex_defdir, saddr.s_addr, &defset,
405 1.47 christos &hostset) || ((dp = dirp_search(ep->ex_dirl, dirpath)) &&
406 1.47 christos chk_host(dp, saddr.s_addr, &defset, &hostset)) ||
407 1.47 christos (defset && scan_tree(ep->ex_defdir, saddr.s_addr) == 0 &&
408 1.47 christos scan_tree(ep->ex_dirl, saddr.s_addr) == 0))) {
409 1.36 fvdl if (sport >= IPPORT_RESERVED &&
410 1.37 fvdl !(hostset & DP_NORESMNT)) {
411 1.36 fvdl syslog(LOG_NOTICE,
412 1.47 christos "Refused mount RPC from host %s port %d",
413 1.47 christos inet_ntoa(saddr), sport);
414 1.36 fvdl svcerr_weakauth(transp);
415 1.44 mrg goto out;
416 1.36 fvdl }
417 1.31 fvdl if (hostset & DP_HOSTSET)
418 1.31 fvdl fhr.fhr_flag = hostset;
419 1.31 fvdl else
420 1.31 fvdl fhr.fhr_flag = defset;
421 1.31 fvdl fhr.fhr_vers = rqstp->rq_vers;
422 1.12 mycroft /* Get the file handle */
423 1.47 christos (void)memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t));
424 1.47 christos if (getfh(dirpath, (fhandle_t *) &fhr.fhr_fh) < 0) {
425 1.12 mycroft bad = errno;
426 1.12 mycroft syslog(LOG_ERR, "Can't get fh for %s", dirpath);
427 1.12 mycroft if (!svc_sendreply(transp, xdr_long,
428 1.47 christos (char *)&bad))
429 1.12 mycroft syslog(LOG_ERR, "Can't send reply");
430 1.44 mrg goto out;
431 1.1 cgd }
432 1.47 christos if (!svc_sendreply(transp, xdr_fhs, (char *) &fhr))
433 1.12 mycroft syslog(LOG_ERR, "Can't send reply");
434 1.12 mycroft if (hp == NULL)
435 1.47 christos hp = gethostbyaddr((const char *) &saddr,
436 1.12 mycroft sizeof(saddr), AF_INET);
437 1.12 mycroft if (hp)
438 1.36 fvdl add_mlist(hp->h_name, dirpath, hostset);
439 1.12 mycroft else
440 1.12 mycroft add_mlist(inet_ntoa(transp->xp_raddr.sin_addr),
441 1.47 christos dirpath, hostset);
442 1.12 mycroft if (debug)
443 1.47 christos (void)fprintf(stderr, "Mount successful.\n");
444 1.12 mycroft } else {
445 1.47 christos if (!svc_sendreply(transp, xdr_long, (caddr_t) &bad))
446 1.1 cgd syslog(LOG_ERR, "Can't send reply");
447 1.1 cgd }
448 1.44 mrg out:
449 1.47 christos (void)sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
450 1.1 cgd return;
451 1.45 christos case MOUNTPROC_DUMP:
452 1.47 christos if (!svc_sendreply(transp, xdr_mlist, NULL))
453 1.1 cgd syslog(LOG_ERR, "Can't send reply");
454 1.1 cgd return;
455 1.45 christos case MOUNTPROC_UMNT:
456 1.36 fvdl if (!svc_getargs(transp, xdr_dir, dirpath)) {
457 1.36 fvdl svcerr_decode(transp);
458 1.1 cgd return;
459 1.1 cgd }
460 1.47 christos hp = gethostbyaddr((caddr_t) &saddr, sizeof(saddr), AF_INET);
461 1.36 fvdl if (hp)
462 1.36 fvdl ret = del_mlist(hp->h_name, dirpath,
463 1.47 christos (struct sockaddr *) &transp->xp_raddr);
464 1.36 fvdl ret |= del_mlist(inet_ntoa(transp->xp_raddr.sin_addr), dirpath,
465 1.47 christos (struct sockaddr *) &transp->xp_raddr);
466 1.36 fvdl if (ret) {
467 1.36 fvdl svcerr_weakauth(transp);
468 1.1 cgd return;
469 1.1 cgd }
470 1.47 christos if (!svc_sendreply(transp, xdr_void, NULL))
471 1.1 cgd syslog(LOG_ERR, "Can't send reply");
472 1.36 fvdl return;
473 1.45 christos case MOUNTPROC_UMNTALL:
474 1.1 cgd hp = gethostbyaddr((caddr_t)&saddr, sizeof(saddr), AF_INET);
475 1.1 cgd if (hp)
476 1.47 christos ret = del_mlist(hp->h_name, NULL,
477 1.36 fvdl (struct sockaddr *)&transp->xp_raddr);
478 1.36 fvdl ret |= del_mlist(inet_ntoa(transp->xp_raddr.sin_addr),
479 1.47 christos NULL, (struct sockaddr *)&transp->xp_raddr);
480 1.36 fvdl if (ret) {
481 1.1 cgd svcerr_weakauth(transp);
482 1.1 cgd return;
483 1.1 cgd }
484 1.47 christos if (!svc_sendreply(transp, xdr_void, NULL))
485 1.1 cgd syslog(LOG_ERR, "Can't send reply");
486 1.1 cgd return;
487 1.45 christos case MOUNTPROC_EXPORT:
488 1.45 christos case MOUNTPROC_EXPORTALL:
489 1.47 christos if (!svc_sendreply(transp, xdr_explist, NULL))
490 1.1 cgd syslog(LOG_ERR, "Can't send reply");
491 1.1 cgd return;
492 1.45 christos
493 1.45 christos #ifdef KERBEROS
494 1.45 christos case MOUNTPROC_KUIDMAP:
495 1.45 christos case MOUNTPROC_KUIDUMAP:
496 1.45 christos case MOUNTPROC_KUIDPURGE:
497 1.45 christos case MOUNTPROC_KUIDUPURGE:
498 1.45 christos kuidops(rqstp, transp);
499 1.45 christos return;
500 1.45 christos #endif
501 1.45 christos
502 1.1 cgd default:
503 1.1 cgd svcerr_noproc(transp);
504 1.1 cgd return;
505 1.1 cgd }
506 1.1 cgd }
507 1.1 cgd
508 1.1 cgd /*
509 1.1 cgd * Xdr conversion for a dirpath string
510 1.1 cgd */
511 1.47 christos static int
512 1.1 cgd xdr_dir(xdrsp, dirp)
513 1.1 cgd XDR *xdrsp;
514 1.1 cgd char *dirp;
515 1.1 cgd {
516 1.44 mrg
517 1.12 mycroft return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
518 1.1 cgd }
519 1.1 cgd
520 1.1 cgd /*
521 1.31 fvdl * Xdr routine to generate file handle reply
522 1.1 cgd */
523 1.47 christos static int
524 1.31 fvdl xdr_fhs(xdrsp, cp)
525 1.1 cgd XDR *xdrsp;
526 1.31 fvdl caddr_t cp;
527 1.1 cgd {
528 1.47 christos struct fhreturn *fhrp = (struct fhreturn *) cp;
529 1.31 fvdl long ok = 0, len, auth;
530 1.1 cgd
531 1.1 cgd if (!xdr_long(xdrsp, &ok))
532 1.1 cgd return (0);
533 1.31 fvdl switch (fhrp->fhr_vers) {
534 1.31 fvdl case 1:
535 1.31 fvdl return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH));
536 1.31 fvdl case 3:
537 1.31 fvdl len = NFSX_V3FH;
538 1.31 fvdl if (!xdr_long(xdrsp, &len))
539 1.31 fvdl return (0);
540 1.31 fvdl if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len))
541 1.31 fvdl return (0);
542 1.31 fvdl if (fhrp->fhr_flag & DP_KERB)
543 1.31 fvdl auth = RPCAUTH_KERB4;
544 1.31 fvdl else
545 1.31 fvdl auth = RPCAUTH_UNIX;
546 1.31 fvdl len = 1;
547 1.31 fvdl if (!xdr_long(xdrsp, &len))
548 1.31 fvdl return (0);
549 1.31 fvdl return (xdr_long(xdrsp, &auth));
550 1.31 fvdl };
551 1.31 fvdl return (0);
552 1.1 cgd }
553 1.1 cgd
554 1.12 mycroft int
555 1.1 cgd xdr_mlist(xdrsp, cp)
556 1.1 cgd XDR *xdrsp;
557 1.1 cgd caddr_t cp;
558 1.1 cgd {
559 1.12 mycroft struct mountlist *mlp;
560 1.1 cgd int true = 1;
561 1.1 cgd int false = 0;
562 1.1 cgd char *strp;
563 1.1 cgd
564 1.1 cgd mlp = mlhead;
565 1.1 cgd while (mlp) {
566 1.1 cgd if (!xdr_bool(xdrsp, &true))
567 1.1 cgd return (0);
568 1.1 cgd strp = &mlp->ml_host[0];
569 1.12 mycroft if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
570 1.1 cgd return (0);
571 1.1 cgd strp = &mlp->ml_dirp[0];
572 1.12 mycroft if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
573 1.1 cgd return (0);
574 1.1 cgd mlp = mlp->ml_next;
575 1.1 cgd }
576 1.1 cgd if (!xdr_bool(xdrsp, &false))
577 1.1 cgd return (0);
578 1.1 cgd return (1);
579 1.1 cgd }
580 1.1 cgd
581 1.1 cgd /*
582 1.1 cgd * Xdr conversion for export list
583 1.1 cgd */
584 1.12 mycroft int
585 1.1 cgd xdr_explist(xdrsp, cp)
586 1.1 cgd XDR *xdrsp;
587 1.1 cgd caddr_t cp;
588 1.1 cgd {
589 1.12 mycroft struct exportlist *ep;
590 1.1 cgd int false = 0;
591 1.20 mycroft int putdef;
592 1.31 fvdl sigset_t sighup_mask;
593 1.1 cgd
594 1.47 christos (void)sigemptyset(&sighup_mask);
595 1.47 christos (void)sigaddset(&sighup_mask, SIGHUP);
596 1.47 christos (void)sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
597 1.12 mycroft ep = exphead;
598 1.12 mycroft while (ep) {
599 1.12 mycroft putdef = 0;
600 1.12 mycroft if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir, &putdef))
601 1.1 cgd goto errout;
602 1.12 mycroft if (ep->ex_defdir && putdef == 0 &&
603 1.47 christos put_exlist(ep->ex_defdir, xdrsp, NULL, &putdef))
604 1.1 cgd goto errout;
605 1.1 cgd ep = ep->ex_next;
606 1.1 cgd }
607 1.47 christos (void)sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
608 1.1 cgd if (!xdr_bool(xdrsp, &false))
609 1.1 cgd return (0);
610 1.1 cgd return (1);
611 1.1 cgd errout:
612 1.47 christos (void)sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
613 1.1 cgd return (0);
614 1.1 cgd }
615 1.1 cgd
616 1.12 mycroft /*
617 1.12 mycroft * Called from xdr_explist() to traverse the tree and export the
618 1.44 mrg * directory paths. Assumes SIGHUP has already been masked.
619 1.12 mycroft */
620 1.12 mycroft int
621 1.12 mycroft put_exlist(dp, xdrsp, adp, putdefp)
622 1.12 mycroft struct dirlist *dp;
623 1.12 mycroft XDR *xdrsp;
624 1.12 mycroft struct dirlist *adp;
625 1.12 mycroft int *putdefp;
626 1.12 mycroft {
627 1.12 mycroft struct grouplist *grp;
628 1.12 mycroft struct hostlist *hp;
629 1.12 mycroft int true = 1;
630 1.12 mycroft int false = 0;
631 1.12 mycroft int gotalldir = 0;
632 1.12 mycroft char *strp;
633 1.12 mycroft
634 1.12 mycroft if (dp) {
635 1.12 mycroft if (put_exlist(dp->dp_left, xdrsp, adp, putdefp))
636 1.12 mycroft return (1);
637 1.12 mycroft if (!xdr_bool(xdrsp, &true))
638 1.12 mycroft return (1);
639 1.12 mycroft strp = dp->dp_dirp;
640 1.12 mycroft if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
641 1.12 mycroft return (1);
642 1.12 mycroft if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) {
643 1.12 mycroft gotalldir = 1;
644 1.12 mycroft *putdefp = 1;
645 1.12 mycroft }
646 1.12 mycroft if ((dp->dp_flag & DP_DEFSET) == 0 &&
647 1.12 mycroft (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) {
648 1.12 mycroft hp = dp->dp_hosts;
649 1.12 mycroft while (hp) {
650 1.12 mycroft grp = hp->ht_grp;
651 1.12 mycroft if (grp->gr_type == GT_HOST) {
652 1.12 mycroft if (!xdr_bool(xdrsp, &true))
653 1.12 mycroft return (1);
654 1.12 mycroft strp = grp->gr_ptr.gt_hostent->h_name;
655 1.47 christos if (!xdr_string(xdrsp, &strp,
656 1.47 christos RPCMNT_NAMELEN))
657 1.12 mycroft return (1);
658 1.12 mycroft } else if (grp->gr_type == GT_NET) {
659 1.12 mycroft if (!xdr_bool(xdrsp, &true))
660 1.12 mycroft return (1);
661 1.12 mycroft strp = grp->gr_ptr.gt_net.nt_name;
662 1.47 christos if (!xdr_string(xdrsp, &strp,
663 1.47 christos RPCMNT_NAMELEN))
664 1.12 mycroft return (1);
665 1.12 mycroft }
666 1.12 mycroft hp = hp->ht_next;
667 1.47 christos if (gotalldir && hp == NULL) {
668 1.12 mycroft hp = adp->dp_hosts;
669 1.12 mycroft gotalldir = 0;
670 1.12 mycroft }
671 1.12 mycroft }
672 1.12 mycroft }
673 1.12 mycroft if (!xdr_bool(xdrsp, &false))
674 1.12 mycroft return (1);
675 1.12 mycroft if (put_exlist(dp->dp_right, xdrsp, adp, putdefp))
676 1.12 mycroft return (1);
677 1.12 mycroft }
678 1.12 mycroft return (0);
679 1.12 mycroft }
680 1.12 mycroft
681 1.47 christos static int
682 1.47 christos parse_host_netgroup(line, lineno, ep, tgrp, cp, has_host, grp)
683 1.47 christos const char *line;
684 1.47 christos size_t lineno;
685 1.47 christos struct exportlist *ep;
686 1.47 christos struct grouplist *tgrp;
687 1.47 christos char *cp;
688 1.47 christos int *has_host;
689 1.47 christos struct grouplist **grp;
690 1.47 christos {
691 1.47 christos const char *hst, *usr, *dom;
692 1.47 christos int netgrp;
693 1.47 christos
694 1.47 christos if (ep == NULL) {
695 1.52 christos syslog(LOG_ERR, "\"%s\", line %ld: No current export",
696 1.52 christos line, (unsigned long)lineno);
697 1.52 christos return 0;
698 1.47 christos }
699 1.47 christos setnetgrent(cp);
700 1.47 christos netgrp = getnetgrent(&hst, &usr, &dom);
701 1.47 christos do {
702 1.47 christos if (*has_host) {
703 1.47 christos (*grp)->gr_next = get_grp();
704 1.47 christos *grp = (*grp)->gr_next;
705 1.47 christos }
706 1.47 christos if (netgrp) {
707 1.52 christos if (hst == NULL) {
708 1.52 christos syslog(LOG_ERR,
709 1.52 christos "\"%s\", line %ld: No host in netgroup %s",
710 1.52 christos line, (unsigned long)lineno, cp);
711 1.52 christos goto bad;
712 1.52 christos }
713 1.52 christos if (get_host(line, lineno, hst, *grp))
714 1.52 christos goto bad;
715 1.52 christos } else if (get_host(line, lineno, cp, *grp))
716 1.52 christos goto bad;
717 1.47 christos *has_host = TRUE;
718 1.47 christos } while (netgrp && getnetgrent(&hst, &usr, &dom));
719 1.47 christos
720 1.47 christos endnetgrent();
721 1.47 christos return 1;
722 1.52 christos bad:
723 1.52 christos endnetgrent();
724 1.52 christos return 0;
725 1.52 christos
726 1.47 christos }
727 1.47 christos
728 1.47 christos static int
729 1.49 christos parse_directory(line, lineno, tgrp, got_nondir, cp, ep, fsp)
730 1.47 christos const char *line;
731 1.47 christos size_t lineno;
732 1.47 christos struct grouplist *tgrp;
733 1.47 christos int got_nondir;
734 1.47 christos char *cp;
735 1.47 christos struct exportlist **ep;
736 1.49 christos struct statfs *fsp;
737 1.46 ross {
738 1.52 christos if (!check_dirpath(line, lineno, cp))
739 1.52 christos return 0;
740 1.52 christos
741 1.52 christos if (statfs(cp, fsp) == -1) {
742 1.52 christos syslog(LOG_ERR, "\"%s\", line %ld: statfs for `%s' failed: %m",
743 1.52 christos line, (unsigned long)lineno, cp);
744 1.47 christos return 0;
745 1.47 christos }
746 1.52 christos
747 1.47 christos if (got_nondir) {
748 1.52 christos syslog(LOG_ERR,
749 1.52 christos "\"%s\", line %ld: Directories must precede files",
750 1.52 christos line, (unsigned long)lineno);
751 1.47 christos return 0;
752 1.47 christos }
753 1.47 christos if (*ep) {
754 1.49 christos if ((*ep)->ex_fs.val[0] != fsp->f_fsid.val[0] ||
755 1.49 christos (*ep)->ex_fs.val[1] != fsp->f_fsid.val[1]) {
756 1.52 christos syslog(LOG_ERR,
757 1.52 christos "\"%s\", line %ld: filesystem ids disagree",
758 1.52 christos line, (unsigned long)lineno);
759 1.47 christos return 0;
760 1.47 christos }
761 1.47 christos } else {
762 1.47 christos /*
763 1.47 christos * See if this directory is already
764 1.47 christos * in the list.
765 1.47 christos */
766 1.49 christos *ep = ex_search(&fsp->f_fsid);
767 1.47 christos if (*ep == NULL) {
768 1.47 christos *ep = get_exp();
769 1.49 christos (*ep)->ex_fs = fsp->f_fsid;
770 1.49 christos (*ep)->ex_fsdir = estrdup(fsp->f_mntonname);
771 1.47 christos if (debug)
772 1.47 christos (void)fprintf(stderr,
773 1.47 christos "Making new ep fs=0x%x,0x%x\n",
774 1.49 christos fsp->f_fsid.val[0], fsp->f_fsid.val[1]);
775 1.47 christos } else {
776 1.47 christos if (debug)
777 1.47 christos (void)fprintf(stderr,
778 1.49 christos "Found ep fs=0x%x,0x%x\n",
779 1.49 christos fsp->f_fsid.val[0], fsp->f_fsid.val[1]);
780 1.47 christos }
781 1.47 christos }
782 1.47 christos
783 1.47 christos return 1;
784 1.46 ross }
785 1.46 ross
786 1.1 cgd
787 1.1 cgd /*
788 1.1 cgd * Get the export list
789 1.1 cgd */
790 1.47 christos /* ARGSUSED */
791 1.1 cgd void
792 1.45 christos get_exportlist(n)
793 1.45 christos int n;
794 1.1 cgd {
795 1.12 mycroft struct exportlist *ep, *ep2;
796 1.12 mycroft struct grouplist *grp, *tgrp;
797 1.12 mycroft struct exportlist **epp;
798 1.12 mycroft struct dirlist *dirhead;
799 1.12 mycroft struct statfs fsb, *fsp;
800 1.12 mycroft struct hostent *hpe;
801 1.12 mycroft struct ucred anon;
802 1.41 lukem char *cp, *endcp, *dirp, savedc;
803 1.47 christos int has_host, exflags, got_nondir, dirplen, num, i;
804 1.47 christos FILE *exp_file;
805 1.47 christos char *line;
806 1.48 christos size_t lineno = 0, len;
807 1.47 christos
808 1.12 mycroft
809 1.1 cgd /*
810 1.1 cgd * First, get rid of the old list
811 1.1 cgd */
812 1.12 mycroft ep = exphead;
813 1.12 mycroft while (ep) {
814 1.1 cgd ep2 = ep;
815 1.1 cgd ep = ep->ex_next;
816 1.1 cgd free_exp(ep2);
817 1.1 cgd }
818 1.47 christos exphead = NULL;
819 1.12 mycroft
820 1.41 lukem dirp = NULL;
821 1.41 lukem dirplen = 0;
822 1.12 mycroft grp = grphead;
823 1.12 mycroft while (grp) {
824 1.12 mycroft tgrp = grp;
825 1.12 mycroft grp = grp->gr_next;
826 1.12 mycroft free_grp(tgrp);
827 1.12 mycroft }
828 1.47 christos grphead = NULL;
829 1.12 mycroft
830 1.12 mycroft /*
831 1.12 mycroft * And delete exports that are in the kernel for all local
832 1.12 mycroft * file systems.
833 1.12 mycroft * XXX: Should know how to handle all local exportable file systems
834 1.30 jtc * instead of just MOUNT_FFS.
835 1.12 mycroft */
836 1.12 mycroft num = getmntinfo(&fsp, MNT_NOWAIT);
837 1.12 mycroft for (i = 0; i < num; i++) {
838 1.12 mycroft union {
839 1.12 mycroft struct ufs_args ua;
840 1.12 mycroft struct iso_args ia;
841 1.12 mycroft struct mfs_args ma;
842 1.13 ws struct msdosfs_args da;
843 1.27 chopps struct adosfs_args aa;
844 1.12 mycroft } targs;
845 1.12 mycroft
846 1.25 cgd if (!strncmp(fsp->f_fstypename, MOUNT_MFS, MFSNAMELEN) ||
847 1.30 jtc !strncmp(fsp->f_fstypename, MOUNT_FFS, MFSNAMELEN) ||
848 1.39 bouyer !strncmp(fsp->f_fstypename, MOUNT_EXT2FS, MFSNAMELEN) ||
849 1.25 cgd !strncmp(fsp->f_fstypename, MOUNT_MSDOS, MFSNAMELEN) ||
850 1.27 chopps !strncmp(fsp->f_fstypename, MOUNT_ADOSFS, MFSNAMELEN) ||
851 1.25 cgd !strncmp(fsp->f_fstypename, MOUNT_CD9660, MFSNAMELEN)) {
852 1.47 christos bzero((char *) &targs, sizeof(targs));
853 1.12 mycroft targs.ua.fspec = NULL;
854 1.12 mycroft targs.ua.export.ex_flags = MNT_DELEXPORT;
855 1.12 mycroft if (mount(fsp->f_fstypename, fsp->f_mntonname,
856 1.47 christos fsp->f_flags | MNT_UPDATE, &targs) == -1)
857 1.12 mycroft syslog(LOG_ERR, "Can't delete exports for %s",
858 1.47 christos fsp->f_mntonname);
859 1.12 mycroft }
860 1.12 mycroft fsp++;
861 1.12 mycroft }
862 1.1 cgd
863 1.1 cgd /*
864 1.1 cgd * Read in the exports file and build the list, calling
865 1.12 mycroft * mount() as we go along to push the export rules into the kernel.
866 1.1 cgd */
867 1.12 mycroft if ((exp_file = fopen(exname, "r")) == NULL) {
868 1.32 mrg syslog(LOG_ERR, "Can't open %s: %m", exname);
869 1.1 cgd exit(2);
870 1.1 cgd }
871 1.47 christos dirhead = NULL;
872 1.47 christos while ((line = fparseln(exp_file, &len, &lineno, NULL, 0)) != NULL) {
873 1.12 mycroft if (debug)
874 1.47 christos (void)fprintf(stderr, "Got line %s\n", line);
875 1.1 cgd cp = line;
876 1.1 cgd nextfield(&cp, &endcp);
877 1.51 christos if (cp == endcp)
878 1.51 christos continue; /* skip empty line */
879 1.1 cgd /*
880 1.12 mycroft * Set defaults.
881 1.1 cgd */
882 1.12 mycroft has_host = FALSE;
883 1.12 mycroft anon = def_anon;
884 1.12 mycroft exflags = MNT_EXPORTED;
885 1.12 mycroft got_nondir = 0;
886 1.12 mycroft opt_flags = 0;
887 1.47 christos ep = NULL;
888 1.1 cgd
889 1.1 cgd /*
890 1.1 cgd * Create new exports list entry
891 1.1 cgd */
892 1.47 christos len = endcp - cp;
893 1.12 mycroft tgrp = grp = get_grp();
894 1.1 cgd while (len > 0) {
895 1.12 mycroft if (len > RPCMNT_NAMELEN) {
896 1.52 christos *endcp = '\0';
897 1.52 christos syslog(LOG_ERR,
898 1.52 christos "\"%s\", line %ld: name `%s' is too long",
899 1.52 christos line, (unsigned long)lineno, cp);
900 1.52 christos goto badline;
901 1.12 mycroft }
902 1.47 christos switch (*cp) {
903 1.47 christos case '-':
904 1.47 christos /*
905 1.47 christos * Option
906 1.47 christos */
907 1.47 christos if (ep == NULL) {
908 1.52 christos syslog(LOG_ERR,
909 1.52 christos "\"%s\", line %ld: No current export list",
910 1.52 christos line, (unsigned long)lineno);
911 1.52 christos goto badline;
912 1.12 mycroft }
913 1.47 christos if (debug)
914 1.47 christos (void)fprintf(stderr, "doing opt %s\n",
915 1.47 christos cp);
916 1.47 christos got_nondir = 1;
917 1.52 christos if (do_opt(line, lineno, &cp, &endcp, ep, grp,
918 1.52 christos &has_host, &exflags, &anon))
919 1.52 christos goto badline;
920 1.47 christos break;
921 1.12 mycroft
922 1.47 christos case '/':
923 1.47 christos /*
924 1.47 christos * Directory
925 1.47 christos */
926 1.47 christos savedc = *endcp;
927 1.47 christos *endcp = '\0';
928 1.47 christos
929 1.47 christos if (!parse_directory(line, lineno, tgrp,
930 1.49 christos got_nondir, cp, &ep, &fsb))
931 1.52 christos goto badline;
932 1.12 mycroft /*
933 1.12 mycroft * Add dirpath to export mount point.
934 1.12 mycroft */
935 1.12 mycroft dirp = add_expdir(&dirhead, cp, len);
936 1.12 mycroft dirplen = len;
937 1.12 mycroft
938 1.47 christos *endcp = savedc;
939 1.47 christos break;
940 1.47 christos
941 1.47 christos default:
942 1.47 christos /*
943 1.47 christos * Host or netgroup.
944 1.47 christos */
945 1.47 christos savedc = *endcp;
946 1.47 christos *endcp = '\0';
947 1.47 christos
948 1.47 christos if (!parse_host_netgroup(line, lineno, ep,
949 1.47 christos tgrp, cp, &has_host, &grp))
950 1.52 christos goto badline;
951 1.47 christos
952 1.47 christos got_nondir = 1;
953 1.47 christos
954 1.47 christos *endcp = savedc;
955 1.47 christos break;
956 1.1 cgd }
957 1.47 christos
958 1.1 cgd cp = endcp;
959 1.1 cgd nextfield(&cp, &endcp);
960 1.1 cgd len = endcp - cp;
961 1.1 cgd }
962 1.52 christos if (check_options(line, lineno, dirhead))
963 1.52 christos goto badline;
964 1.52 christos
965 1.12 mycroft if (!has_host) {
966 1.12 mycroft grp->gr_type = GT_HOST;
967 1.12 mycroft if (debug)
968 1.47 christos (void)fprintf(stderr,
969 1.47 christos "Adding a default entry\n");
970 1.12 mycroft /* add a default group and make the grp list NULL */
971 1.47 christos hpe = emalloc(sizeof(struct hostent));
972 1.47 christos hpe->h_name = estrdup("Default");
973 1.12 mycroft hpe->h_addrtype = AF_INET;
974 1.47 christos hpe->h_length = sizeof(u_int32_t);
975 1.47 christos hpe->h_addr_list = NULL;
976 1.12 mycroft grp->gr_ptr.gt_hostent = hpe;
977 1.12 mycroft
978 1.12 mycroft } else if ((opt_flags & OP_NET) && tgrp->gr_next) {
979 1.47 christos /*
980 1.47 christos * Don't allow a network export coincide with a list of
981 1.47 christos * host(s) on the same line.
982 1.47 christos */
983 1.52 christos syslog(LOG_ERR,
984 1.52 christos "\"%s\", line %ld: Mixed exporting of networks and hosts is disallowed",
985 1.52 christos line, (unsigned long)lineno);
986 1.52 christos goto badline;
987 1.12 mycroft }
988 1.12 mycroft /*
989 1.12 mycroft * Loop through hosts, pushing the exports into the kernel.
990 1.12 mycroft * After loop, tgrp points to the start of the list and
991 1.12 mycroft * grp points to the last entry in the list.
992 1.12 mycroft */
993 1.12 mycroft grp = tgrp;
994 1.12 mycroft do {
995 1.52 christos if (do_mount(line, lineno, ep, grp, exflags, &anon,
996 1.52 christos dirp, dirplen, &fsb))
997 1.52 christos goto badline;
998 1.12 mycroft } while (grp->gr_next && (grp = grp->gr_next));
999 1.12 mycroft
1000 1.12 mycroft /*
1001 1.12 mycroft * Success. Update the data structures.
1002 1.12 mycroft */
1003 1.12 mycroft if (has_host) {
1004 1.31 fvdl hang_dirp(dirhead, tgrp, ep, opt_flags);
1005 1.12 mycroft grp->gr_next = grphead;
1006 1.12 mycroft grphead = tgrp;
1007 1.12 mycroft } else {
1008 1.47 christos hang_dirp(dirhead, NULL, ep, opt_flags);
1009 1.12 mycroft free_grp(grp);
1010 1.12 mycroft }
1011 1.47 christos dirhead = NULL;
1012 1.12 mycroft if ((ep->ex_flag & EX_LINKED) == 0) {
1013 1.12 mycroft ep2 = exphead;
1014 1.12 mycroft epp = &exphead;
1015 1.12 mycroft
1016 1.12 mycroft /*
1017 1.12 mycroft * Insert in the list in alphabetical order.
1018 1.12 mycroft */
1019 1.12 mycroft while (ep2 && strcmp(ep2->ex_fsdir, ep->ex_fsdir) < 0) {
1020 1.12 mycroft epp = &ep2->ex_next;
1021 1.12 mycroft ep2 = ep2->ex_next;
1022 1.12 mycroft }
1023 1.12 mycroft if (ep2)
1024 1.12 mycroft ep->ex_next = ep2;
1025 1.12 mycroft *epp = ep;
1026 1.12 mycroft ep->ex_flag |= EX_LINKED;
1027 1.12 mycroft }
1028 1.52 christos goto nextline;
1029 1.52 christos badline:
1030 1.52 christos free_exp_grp(ep, grp);
1031 1.12 mycroft nextline:
1032 1.12 mycroft if (dirhead) {
1033 1.12 mycroft free_dir(dirhead);
1034 1.47 christos dirhead = NULL;
1035 1.12 mycroft }
1036 1.50 christos free(line);
1037 1.12 mycroft }
1038 1.47 christos (void)fclose(exp_file);
1039 1.12 mycroft }
1040 1.12 mycroft
1041 1.12 mycroft /*
1042 1.12 mycroft * Allocate an export list element
1043 1.12 mycroft */
1044 1.47 christos static struct exportlist *
1045 1.12 mycroft get_exp()
1046 1.12 mycroft {
1047 1.12 mycroft struct exportlist *ep;
1048 1.12 mycroft
1049 1.47 christos ep = emalloc(sizeof(struct exportlist));
1050 1.47 christos (void)memset(ep, 0, sizeof(struct exportlist));
1051 1.12 mycroft return (ep);
1052 1.12 mycroft }
1053 1.12 mycroft
1054 1.12 mycroft /*
1055 1.12 mycroft * Allocate a group list element
1056 1.12 mycroft */
1057 1.47 christos static struct grouplist *
1058 1.12 mycroft get_grp()
1059 1.12 mycroft {
1060 1.12 mycroft struct grouplist *gp;
1061 1.12 mycroft
1062 1.47 christos gp = emalloc(sizeof(struct grouplist));
1063 1.47 christos (void)memset(gp, 0, sizeof(struct grouplist));
1064 1.12 mycroft return (gp);
1065 1.12 mycroft }
1066 1.12 mycroft
1067 1.12 mycroft /*
1068 1.12 mycroft * Clean up upon an error in get_exportlist().
1069 1.12 mycroft */
1070 1.47 christos static void
1071 1.52 christos free_exp_grp(ep, grp)
1072 1.12 mycroft struct exportlist *ep;
1073 1.12 mycroft struct grouplist *grp;
1074 1.12 mycroft {
1075 1.12 mycroft struct grouplist *tgrp;
1076 1.12 mycroft
1077 1.12 mycroft if (ep && (ep->ex_flag & EX_LINKED) == 0)
1078 1.12 mycroft free_exp(ep);
1079 1.12 mycroft while (grp) {
1080 1.12 mycroft tgrp = grp;
1081 1.12 mycroft grp = grp->gr_next;
1082 1.12 mycroft free_grp(tgrp);
1083 1.12 mycroft }
1084 1.12 mycroft }
1085 1.12 mycroft
1086 1.12 mycroft /*
1087 1.12 mycroft * Search the export list for a matching fs.
1088 1.12 mycroft */
1089 1.47 christos static struct exportlist *
1090 1.12 mycroft ex_search(fsid)
1091 1.12 mycroft fsid_t *fsid;
1092 1.12 mycroft {
1093 1.12 mycroft struct exportlist *ep;
1094 1.12 mycroft
1095 1.12 mycroft ep = exphead;
1096 1.12 mycroft while (ep) {
1097 1.12 mycroft if (ep->ex_fs.val[0] == fsid->val[0] &&
1098 1.12 mycroft ep->ex_fs.val[1] == fsid->val[1])
1099 1.12 mycroft return (ep);
1100 1.12 mycroft ep = ep->ex_next;
1101 1.12 mycroft }
1102 1.12 mycroft return (ep);
1103 1.12 mycroft }
1104 1.12 mycroft
1105 1.12 mycroft /*
1106 1.12 mycroft * Add a directory path to the list.
1107 1.12 mycroft */
1108 1.47 christos static char *
1109 1.12 mycroft add_expdir(dpp, cp, len)
1110 1.12 mycroft struct dirlist **dpp;
1111 1.12 mycroft char *cp;
1112 1.12 mycroft int len;
1113 1.12 mycroft {
1114 1.12 mycroft struct dirlist *dp;
1115 1.12 mycroft
1116 1.47 christos dp = emalloc(sizeof(struct dirlist) + len);
1117 1.12 mycroft dp->dp_left = *dpp;
1118 1.47 christos dp->dp_right = NULL;
1119 1.12 mycroft dp->dp_flag = 0;
1120 1.47 christos dp->dp_hosts = NULL;
1121 1.47 christos (void)strcpy(dp->dp_dirp, cp);
1122 1.12 mycroft *dpp = dp;
1123 1.12 mycroft return (dp->dp_dirp);
1124 1.12 mycroft }
1125 1.12 mycroft
1126 1.12 mycroft /*
1127 1.12 mycroft * Hang the dir list element off the dirpath binary tree as required
1128 1.12 mycroft * and update the entry for host.
1129 1.12 mycroft */
1130 1.12 mycroft void
1131 1.31 fvdl hang_dirp(dp, grp, ep, flags)
1132 1.12 mycroft struct dirlist *dp;
1133 1.12 mycroft struct grouplist *grp;
1134 1.12 mycroft struct exportlist *ep;
1135 1.31 fvdl int flags;
1136 1.12 mycroft {
1137 1.12 mycroft struct hostlist *hp;
1138 1.12 mycroft struct dirlist *dp2;
1139 1.12 mycroft
1140 1.31 fvdl if (flags & OP_ALLDIRS) {
1141 1.12 mycroft if (ep->ex_defdir)
1142 1.47 christos free(dp);
1143 1.12 mycroft else
1144 1.12 mycroft ep->ex_defdir = dp;
1145 1.47 christos if (grp == NULL) {
1146 1.12 mycroft ep->ex_defdir->dp_flag |= DP_DEFSET;
1147 1.31 fvdl if (flags & OP_KERB)
1148 1.31 fvdl ep->ex_defdir->dp_flag |= DP_KERB;
1149 1.37 fvdl if (flags & OP_NORESMNT)
1150 1.37 fvdl ep->ex_defdir->dp_flag |= DP_NORESMNT;
1151 1.47 christos } else
1152 1.47 christos while (grp) {
1153 1.47 christos hp = get_ht();
1154 1.47 christos if (flags & OP_KERB)
1155 1.47 christos hp->ht_flag |= DP_KERB;
1156 1.47 christos if (flags & OP_NORESMNT)
1157 1.47 christos hp->ht_flag |= DP_NORESMNT;
1158 1.47 christos hp->ht_grp = grp;
1159 1.47 christos hp->ht_next = ep->ex_defdir->dp_hosts;
1160 1.47 christos ep->ex_defdir->dp_hosts = hp;
1161 1.47 christos grp = grp->gr_next;
1162 1.47 christos }
1163 1.12 mycroft } else {
1164 1.12 mycroft
1165 1.12 mycroft /*
1166 1.12 mycroft * Loop throught the directories adding them to the tree.
1167 1.12 mycroft */
1168 1.12 mycroft while (dp) {
1169 1.12 mycroft dp2 = dp->dp_left;
1170 1.31 fvdl add_dlist(&ep->ex_dirl, dp, grp, flags);
1171 1.12 mycroft dp = dp2;
1172 1.12 mycroft }
1173 1.12 mycroft }
1174 1.12 mycroft }
1175 1.12 mycroft
1176 1.12 mycroft /*
1177 1.12 mycroft * Traverse the binary tree either updating a node that is already there
1178 1.12 mycroft * for the new directory or adding the new node.
1179 1.12 mycroft */
1180 1.47 christos static void
1181 1.31 fvdl add_dlist(dpp, newdp, grp, flags)
1182 1.12 mycroft struct dirlist **dpp;
1183 1.12 mycroft struct dirlist *newdp;
1184 1.12 mycroft struct grouplist *grp;
1185 1.31 fvdl int flags;
1186 1.12 mycroft {
1187 1.12 mycroft struct dirlist *dp;
1188 1.12 mycroft struct hostlist *hp;
1189 1.12 mycroft int cmp;
1190 1.12 mycroft
1191 1.12 mycroft dp = *dpp;
1192 1.12 mycroft if (dp) {
1193 1.12 mycroft cmp = strcmp(dp->dp_dirp, newdp->dp_dirp);
1194 1.12 mycroft if (cmp > 0) {
1195 1.31 fvdl add_dlist(&dp->dp_left, newdp, grp, flags);
1196 1.12 mycroft return;
1197 1.12 mycroft } else if (cmp < 0) {
1198 1.31 fvdl add_dlist(&dp->dp_right, newdp, grp, flags);
1199 1.12 mycroft return;
1200 1.12 mycroft } else
1201 1.47 christos free(newdp);
1202 1.12 mycroft } else {
1203 1.12 mycroft dp = newdp;
1204 1.47 christos dp->dp_left = NULL;
1205 1.12 mycroft *dpp = dp;
1206 1.12 mycroft }
1207 1.12 mycroft if (grp) {
1208 1.12 mycroft
1209 1.12 mycroft /*
1210 1.12 mycroft * Hang all of the host(s) off of the directory point.
1211 1.12 mycroft */
1212 1.12 mycroft do {
1213 1.12 mycroft hp = get_ht();
1214 1.31 fvdl if (flags & OP_KERB)
1215 1.31 fvdl hp->ht_flag |= DP_KERB;
1216 1.37 fvdl if (flags & OP_NORESMNT)
1217 1.37 fvdl hp->ht_flag |= DP_NORESMNT;
1218 1.12 mycroft hp->ht_grp = grp;
1219 1.12 mycroft hp->ht_next = dp->dp_hosts;
1220 1.12 mycroft dp->dp_hosts = hp;
1221 1.12 mycroft grp = grp->gr_next;
1222 1.12 mycroft } while (grp);
1223 1.31 fvdl } else {
1224 1.12 mycroft dp->dp_flag |= DP_DEFSET;
1225 1.31 fvdl if (flags & OP_KERB)
1226 1.31 fvdl dp->dp_flag |= DP_KERB;
1227 1.37 fvdl if (flags & OP_NORESMNT)
1228 1.37 fvdl dp->dp_flag |= DP_NORESMNT;
1229 1.31 fvdl }
1230 1.12 mycroft }
1231 1.12 mycroft
1232 1.12 mycroft /*
1233 1.12 mycroft * Search for a dirpath on the export point.
1234 1.12 mycroft */
1235 1.47 christos static struct dirlist *
1236 1.45 christos dirp_search(dp, dirp)
1237 1.12 mycroft struct dirlist *dp;
1238 1.45 christos char *dirp;
1239 1.12 mycroft {
1240 1.12 mycroft int cmp;
1241 1.12 mycroft
1242 1.12 mycroft if (dp) {
1243 1.45 christos cmp = strcmp(dp->dp_dirp, dirp);
1244 1.12 mycroft if (cmp > 0)
1245 1.45 christos return (dirp_search(dp->dp_left, dirp));
1246 1.12 mycroft else if (cmp < 0)
1247 1.45 christos return (dirp_search(dp->dp_right, dirp));
1248 1.12 mycroft else
1249 1.12 mycroft return (dp);
1250 1.12 mycroft }
1251 1.12 mycroft return (dp);
1252 1.12 mycroft }
1253 1.12 mycroft
1254 1.12 mycroft /*
1255 1.12 mycroft * Scan for a host match in a directory tree.
1256 1.12 mycroft */
1257 1.47 christos static int
1258 1.31 fvdl chk_host(dp, saddr, defsetp, hostsetp)
1259 1.12 mycroft struct dirlist *dp;
1260 1.36 fvdl u_int32_t saddr;
1261 1.12 mycroft int *defsetp;
1262 1.31 fvdl int *hostsetp;
1263 1.12 mycroft {
1264 1.12 mycroft struct hostlist *hp;
1265 1.12 mycroft struct grouplist *grp;
1266 1.35 cgd u_int32_t **addrp;
1267 1.12 mycroft
1268 1.12 mycroft if (dp) {
1269 1.12 mycroft if (dp->dp_flag & DP_DEFSET)
1270 1.31 fvdl *defsetp = dp->dp_flag;
1271 1.12 mycroft hp = dp->dp_hosts;
1272 1.12 mycroft while (hp) {
1273 1.12 mycroft grp = hp->ht_grp;
1274 1.12 mycroft switch (grp->gr_type) {
1275 1.12 mycroft case GT_HOST:
1276 1.47 christos addrp = (u_int32_t **)
1277 1.47 christos grp->gr_ptr.gt_hostent->h_addr_list;
1278 1.47 christos for (; *addrp; addrp++) {
1279 1.47 christos if (**addrp != saddr)
1280 1.47 christos continue;
1281 1.47 christos *hostsetp = (hp->ht_flag | DP_HOSTSET);
1282 1.47 christos return (1);
1283 1.31 fvdl }
1284 1.47 christos break;
1285 1.12 mycroft case GT_NET:
1286 1.47 christos if ((saddr & grp->gr_ptr.gt_net.nt_mask) ==
1287 1.47 christos grp->gr_ptr.gt_net.nt_net) {
1288 1.47 christos *hostsetp = (hp->ht_flag | DP_HOSTSET);
1289 1.47 christos return (1);
1290 1.47 christos }
1291 1.47 christos break;
1292 1.12 mycroft };
1293 1.12 mycroft hp = hp->ht_next;
1294 1.12 mycroft }
1295 1.12 mycroft }
1296 1.12 mycroft return (0);
1297 1.12 mycroft }
1298 1.12 mycroft
1299 1.12 mycroft /*
1300 1.12 mycroft * Scan tree for a host that matches the address.
1301 1.12 mycroft */
1302 1.47 christos static int
1303 1.12 mycroft scan_tree(dp, saddr)
1304 1.12 mycroft struct dirlist *dp;
1305 1.36 fvdl u_int32_t saddr;
1306 1.12 mycroft {
1307 1.31 fvdl int defset, hostset;
1308 1.12 mycroft
1309 1.12 mycroft if (dp) {
1310 1.12 mycroft if (scan_tree(dp->dp_left, saddr))
1311 1.12 mycroft return (1);
1312 1.31 fvdl if (chk_host(dp, saddr, &defset, &hostset))
1313 1.12 mycroft return (1);
1314 1.12 mycroft if (scan_tree(dp->dp_right, saddr))
1315 1.12 mycroft return (1);
1316 1.12 mycroft }
1317 1.12 mycroft return (0);
1318 1.12 mycroft }
1319 1.12 mycroft
1320 1.12 mycroft /*
1321 1.12 mycroft * Traverse the dirlist tree and free it up.
1322 1.12 mycroft */
1323 1.47 christos static void
1324 1.12 mycroft free_dir(dp)
1325 1.12 mycroft struct dirlist *dp;
1326 1.12 mycroft {
1327 1.12 mycroft
1328 1.12 mycroft if (dp) {
1329 1.12 mycroft free_dir(dp->dp_left);
1330 1.12 mycroft free_dir(dp->dp_right);
1331 1.12 mycroft free_host(dp->dp_hosts);
1332 1.47 christos free(dp);
1333 1.12 mycroft }
1334 1.12 mycroft }
1335 1.12 mycroft
1336 1.12 mycroft /*
1337 1.12 mycroft * Parse the option string and update fields.
1338 1.12 mycroft * Option arguments may either be -<option>=<value> or
1339 1.12 mycroft * -<option> <value>
1340 1.12 mycroft */
1341 1.47 christos static int
1342 1.52 christos do_opt(line, lineno, cpp, endcpp, ep, grp, has_hostp, exflagsp, cr)
1343 1.52 christos const char *line;
1344 1.52 christos size_t lineno;
1345 1.12 mycroft char **cpp, **endcpp;
1346 1.12 mycroft struct exportlist *ep;
1347 1.12 mycroft struct grouplist *grp;
1348 1.12 mycroft int *has_hostp;
1349 1.12 mycroft int *exflagsp;
1350 1.12 mycroft struct ucred *cr;
1351 1.12 mycroft {
1352 1.12 mycroft char *cpoptarg, *cpoptend;
1353 1.12 mycroft char *cp, *endcp, *cpopt, savedc, savedc2;
1354 1.12 mycroft int allflag, usedarg;
1355 1.12 mycroft
1356 1.12 mycroft cpopt = *cpp;
1357 1.12 mycroft cpopt++;
1358 1.12 mycroft cp = *endcpp;
1359 1.12 mycroft savedc = *cp;
1360 1.12 mycroft *cp = '\0';
1361 1.12 mycroft while (cpopt && *cpopt) {
1362 1.12 mycroft allflag = 1;
1363 1.12 mycroft usedarg = -2;
1364 1.41 lukem savedc2 = '\0';
1365 1.41 lukem if ((cpoptend = strchr(cpopt, ',')) != NULL) {
1366 1.12 mycroft *cpoptend++ = '\0';
1367 1.41 lukem if ((cpoptarg = strchr(cpopt, '=')) != NULL)
1368 1.12 mycroft *cpoptarg++ = '\0';
1369 1.12 mycroft } else {
1370 1.41 lukem if ((cpoptarg = strchr(cpopt, '=')) != NULL)
1371 1.12 mycroft *cpoptarg++ = '\0';
1372 1.12 mycroft else {
1373 1.12 mycroft *cp = savedc;
1374 1.12 mycroft nextfield(&cp, &endcp);
1375 1.12 mycroft **endcpp = '\0';
1376 1.12 mycroft if (endcp > cp && *cp != '-') {
1377 1.12 mycroft cpoptarg = cp;
1378 1.12 mycroft savedc2 = *endcp;
1379 1.12 mycroft *endcp = '\0';
1380 1.12 mycroft usedarg = 0;
1381 1.1 cgd }
1382 1.1 cgd }
1383 1.12 mycroft }
1384 1.12 mycroft if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
1385 1.12 mycroft *exflagsp |= MNT_EXRDONLY;
1386 1.12 mycroft } else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
1387 1.12 mycroft !(allflag = strcmp(cpopt, "mapall")) ||
1388 1.12 mycroft !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
1389 1.12 mycroft usedarg++;
1390 1.12 mycroft parsecred(cpoptarg, cr);
1391 1.12 mycroft if (allflag == 0) {
1392 1.12 mycroft *exflagsp |= MNT_EXPORTANON;
1393 1.12 mycroft opt_flags |= OP_MAPALL;
1394 1.12 mycroft } else
1395 1.12 mycroft opt_flags |= OP_MAPROOT;
1396 1.12 mycroft } else if (!strcmp(cpopt, "kerb") || !strcmp(cpopt, "k")) {
1397 1.12 mycroft *exflagsp |= MNT_EXKERB;
1398 1.12 mycroft opt_flags |= OP_KERB;
1399 1.12 mycroft } else if (cpoptarg && (!strcmp(cpopt, "mask") ||
1400 1.47 christos !strcmp(cpopt, "m"))) {
1401 1.12 mycroft if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
1402 1.52 christos syslog(LOG_ERR,
1403 1.52 christos "\"%s\", line %ld: Bad mask: %s",
1404 1.52 christos line, (unsigned long)lineno, cpoptarg);
1405 1.12 mycroft return (1);
1406 1.12 mycroft }
1407 1.12 mycroft usedarg++;
1408 1.12 mycroft opt_flags |= OP_MASK;
1409 1.12 mycroft } else if (cpoptarg && (!strcmp(cpopt, "network") ||
1410 1.47 christos !strcmp(cpopt, "n"))) {
1411 1.12 mycroft if (grp->gr_type != GT_NULL) {
1412 1.52 christos syslog(LOG_ERR,
1413 1.52 christos "\"%s\", line %ld: Network/host conflict",
1414 1.52 christos line, (unsigned long)lineno);
1415 1.12 mycroft return (1);
1416 1.12 mycroft } else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
1417 1.52 christos syslog(LOG_ERR,
1418 1.52 christos "\"%s\", line %ld: Bad net: %s",
1419 1.52 christos line, (unsigned long)lineno, cpoptarg);
1420 1.12 mycroft return (1);
1421 1.12 mycroft }
1422 1.12 mycroft grp->gr_type = GT_NET;
1423 1.12 mycroft *has_hostp = 1;
1424 1.12 mycroft usedarg++;
1425 1.12 mycroft opt_flags |= OP_NET;
1426 1.12 mycroft } else if (!strcmp(cpopt, "alldirs")) {
1427 1.12 mycroft opt_flags |= OP_ALLDIRS;
1428 1.37 fvdl } else if (!strcmp(cpopt, "noresvmnt")) {
1429 1.37 fvdl opt_flags |= OP_NORESMNT;
1430 1.37 fvdl } else if (!strcmp(cpopt, "noresvport")) {
1431 1.36 fvdl opt_flags |= OP_NORESPORT;
1432 1.36 fvdl *exflagsp |= MNT_EXNORESPORT;
1433 1.38 fvdl } else if (!strcmp(cpopt, "public")) {
1434 1.47 christos *exflagsp |= (MNT_EXNORESPORT | MNT_EXPUBLIC);
1435 1.38 fvdl opt_flags |= OP_NORESPORT;
1436 1.38 fvdl } else if (!strcmp(cpopt, "webnfs")) {
1437 1.47 christos *exflagsp |= (MNT_EXNORESPORT | MNT_EXPUBLIC |
1438 1.47 christos MNT_EXRDONLY | MNT_EXPORTANON);
1439 1.47 christos opt_flags |= (OP_MAPALL | OP_NORESPORT);
1440 1.38 fvdl } else if (cpoptarg && !strcmp(cpopt, "index")) {
1441 1.38 fvdl ep->ex_indexfile = strdup(cpoptarg);
1442 1.12 mycroft #ifdef ISO
1443 1.12 mycroft } else if (cpoptarg && !strcmp(cpopt, "iso")) {
1444 1.52 christos if (get_isoaddr(line, lineno, cpoptarg, grp))
1445 1.12 mycroft return (1);
1446 1.12 mycroft *has_hostp = 1;
1447 1.12 mycroft usedarg++;
1448 1.12 mycroft opt_flags |= OP_ISO;
1449 1.12 mycroft #endif /* ISO */
1450 1.12 mycroft } else {
1451 1.52 christos syslog(LOG_ERR,
1452 1.52 christos "\"%s\", line %ld: Bad opt %s",
1453 1.52 christos line, (unsigned long)lineno, cpopt);
1454 1.12 mycroft return (1);
1455 1.12 mycroft }
1456 1.12 mycroft if (usedarg >= 0) {
1457 1.12 mycroft *endcp = savedc2;
1458 1.12 mycroft **endcpp = savedc;
1459 1.12 mycroft if (usedarg > 0) {
1460 1.12 mycroft *cpp = cp;
1461 1.12 mycroft *endcpp = endcp;
1462 1.12 mycroft }
1463 1.12 mycroft return (0);
1464 1.12 mycroft }
1465 1.12 mycroft cpopt = cpoptend;
1466 1.12 mycroft }
1467 1.12 mycroft **endcpp = savedc;
1468 1.12 mycroft return (0);
1469 1.12 mycroft }
1470 1.12 mycroft
1471 1.12 mycroft /*
1472 1.12 mycroft * Translate a character string to the corresponding list of network
1473 1.12 mycroft * addresses for a hostname.
1474 1.12 mycroft */
1475 1.47 christos static int
1476 1.52 christos get_host(line, lineno, cp, grp)
1477 1.52 christos const char *line;
1478 1.52 christos size_t lineno;
1479 1.41 lukem const char *cp;
1480 1.12 mycroft struct grouplist *grp;
1481 1.12 mycroft {
1482 1.12 mycroft struct hostent *hp, *nhp;
1483 1.12 mycroft char **addrp, **naddrp;
1484 1.47 christos struct hostent t_host;
1485 1.12 mycroft int i;
1486 1.36 fvdl u_int32_t saddr;
1487 1.12 mycroft char *aptr[2];
1488 1.12 mycroft
1489 1.52 christos if (grp->gr_type != GT_NULL) {
1490 1.52 christos syslog(LOG_ERR,
1491 1.52 christos "\"%s\", line %ld: Bad netgroup type for ip host %s",
1492 1.52 christos line, (unsigned long)lineno, cp);
1493 1.12 mycroft return (1);
1494 1.52 christos }
1495 1.12 mycroft if ((hp = gethostbyname(cp)) == NULL) {
1496 1.12 mycroft if (isdigit(*cp)) {
1497 1.12 mycroft saddr = inet_addr(cp);
1498 1.12 mycroft if (saddr == -1) {
1499 1.52 christos syslog(LOG_ERR,
1500 1.52 christos "\"%s\", line %ld: inet_addr failed for %s",
1501 1.52 christos line, (unsigned long) lineno, cp);
1502 1.12 mycroft return (1);
1503 1.12 mycroft }
1504 1.47 christos if ((hp = gethostbyaddr((const char *) &saddr,
1505 1.47 christos sizeof(saddr), AF_INET)) == NULL) {
1506 1.12 mycroft hp = &t_host;
1507 1.47 christos hp->h_name = (char *) cp;
1508 1.12 mycroft hp->h_addrtype = AF_INET;
1509 1.47 christos hp->h_length = sizeof(u_int32_t);
1510 1.12 mycroft hp->h_addr_list = aptr;
1511 1.47 christos aptr[0] = (char *) &saddr;
1512 1.47 christos aptr[1] = NULL;
1513 1.12 mycroft }
1514 1.12 mycroft } else {
1515 1.52 christos syslog(LOG_ERR,
1516 1.52 christos "\"%s\", line %ld: gethostbyname failed for %s: %s",
1517 1.52 christos line, (unsigned long) lineno, cp,
1518 1.47 christos hstrerror(h_errno));
1519 1.12 mycroft return (1);
1520 1.12 mycroft }
1521 1.12 mycroft }
1522 1.12 mycroft grp->gr_type = GT_HOST;
1523 1.47 christos nhp = grp->gr_ptr.gt_hostent = emalloc(sizeof(struct hostent));
1524 1.47 christos (void)memcpy(nhp, hp, sizeof(struct hostent));
1525 1.47 christos nhp->h_name = estrdup(hp->h_name);
1526 1.12 mycroft addrp = hp->h_addr_list;
1527 1.12 mycroft i = 1;
1528 1.12 mycroft while (*addrp++)
1529 1.12 mycroft i++;
1530 1.47 christos naddrp = nhp->h_addr_list = emalloc(i * sizeof(char *));
1531 1.12 mycroft addrp = hp->h_addr_list;
1532 1.12 mycroft while (*addrp) {
1533 1.47 christos *naddrp = emalloc(hp->h_length);
1534 1.47 christos (void)memcpy(*naddrp, *addrp, hp->h_length);
1535 1.12 mycroft addrp++;
1536 1.12 mycroft naddrp++;
1537 1.12 mycroft }
1538 1.47 christos *naddrp = NULL;
1539 1.12 mycroft if (debug)
1540 1.47 christos (void)fprintf(stderr, "got host %s\n", hp->h_name);
1541 1.12 mycroft return (0);
1542 1.12 mycroft }
1543 1.12 mycroft
1544 1.12 mycroft /*
1545 1.12 mycroft * Free up an exports list component
1546 1.12 mycroft */
1547 1.47 christos static void
1548 1.12 mycroft free_exp(ep)
1549 1.12 mycroft struct exportlist *ep;
1550 1.12 mycroft {
1551 1.12 mycroft
1552 1.12 mycroft if (ep->ex_defdir) {
1553 1.12 mycroft free_host(ep->ex_defdir->dp_hosts);
1554 1.47 christos free(ep->ex_defdir);
1555 1.12 mycroft }
1556 1.12 mycroft if (ep->ex_fsdir)
1557 1.12 mycroft free(ep->ex_fsdir);
1558 1.38 fvdl if (ep->ex_indexfile)
1559 1.38 fvdl free(ep->ex_indexfile);
1560 1.12 mycroft free_dir(ep->ex_dirl);
1561 1.47 christos free(ep);
1562 1.12 mycroft }
1563 1.12 mycroft
1564 1.12 mycroft /*
1565 1.12 mycroft * Free hosts.
1566 1.12 mycroft */
1567 1.47 christos static void
1568 1.12 mycroft free_host(hp)
1569 1.12 mycroft struct hostlist *hp;
1570 1.12 mycroft {
1571 1.12 mycroft struct hostlist *hp2;
1572 1.12 mycroft
1573 1.12 mycroft while (hp) {
1574 1.12 mycroft hp2 = hp;
1575 1.12 mycroft hp = hp->ht_next;
1576 1.47 christos free(hp2);
1577 1.12 mycroft }
1578 1.12 mycroft }
1579 1.12 mycroft
1580 1.47 christos static struct hostlist *
1581 1.12 mycroft get_ht()
1582 1.12 mycroft {
1583 1.12 mycroft struct hostlist *hp;
1584 1.12 mycroft
1585 1.47 christos hp = emalloc(sizeof(struct hostlist));
1586 1.47 christos hp->ht_next = NULL;
1587 1.31 fvdl hp->ht_flag = 0;
1588 1.12 mycroft return (hp);
1589 1.12 mycroft }
1590 1.12 mycroft
1591 1.12 mycroft #ifdef ISO
1592 1.12 mycroft /*
1593 1.12 mycroft * Translate an iso address.
1594 1.12 mycroft */
1595 1.47 christos static int
1596 1.52 christos get_isoaddr(line, lineno, cp, grp)
1597 1.52 christos const char *line;
1598 1.52 christos size_t lineno;
1599 1.12 mycroft char *cp;
1600 1.12 mycroft struct grouplist *grp;
1601 1.12 mycroft {
1602 1.12 mycroft struct iso_addr *isop;
1603 1.12 mycroft struct sockaddr_iso *isoaddr;
1604 1.12 mycroft
1605 1.52 christos if (grp->gr_type != GT_NULL) {
1606 1.52 christos syslog(LOG_ERR,
1607 1.52 christos "\"%s\", line %ld: Bad netgroup type for iso addr %s",
1608 1.52 christos line, (unsigned long)lineno, cp);
1609 1.12 mycroft return (1);
1610 1.52 christos }
1611 1.12 mycroft if ((isop = iso_addr(cp)) == NULL) {
1612 1.52 christos syslog(LOG_ERR,
1613 1.52 christos "\"%s\", line %ld: Bad iso addr %s",
1614 1.52 christos line, (unsigned long)lineno, cp);
1615 1.12 mycroft return (1);
1616 1.12 mycroft }
1617 1.47 christos isoaddr = emalloc(sizeof(struct sockaddr_iso));
1618 1.47 christos (void)memset(isoaddr, 0, sizeof(struct sockaddr_iso));
1619 1.47 christos (void)memcpy(&isoaddr->siso_addr, isop, sizeof(struct iso_addr));
1620 1.15 mycroft isoaddr->siso_len = sizeof(struct sockaddr_iso);
1621 1.12 mycroft isoaddr->siso_family = AF_ISO;
1622 1.12 mycroft grp->gr_type = GT_ISO;
1623 1.12 mycroft grp->gr_ptr.gt_isoaddr = isoaddr;
1624 1.12 mycroft return (0);
1625 1.12 mycroft }
1626 1.47 christos #endif /* ISO */
1627 1.12 mycroft
1628 1.12 mycroft /*
1629 1.47 christos * error checked malloc and strdup
1630 1.12 mycroft */
1631 1.47 christos static void *
1632 1.47 christos emalloc(n)
1633 1.47 christos size_t n;
1634 1.47 christos {
1635 1.47 christos void *ptr = malloc(n);
1636 1.47 christos
1637 1.47 christos if (ptr == NULL) {
1638 1.47 christos syslog(LOG_ERR, "%m");
1639 1.47 christos exit(2);
1640 1.47 christos }
1641 1.47 christos return ptr;
1642 1.47 christos }
1643 1.47 christos
1644 1.47 christos static char *
1645 1.47 christos estrdup(s)
1646 1.47 christos const char *s;
1647 1.12 mycroft {
1648 1.47 christos char *n = strdup(s);
1649 1.12 mycroft
1650 1.47 christos if (n == NULL) {
1651 1.47 christos syslog(LOG_ERR, "%m");
1652 1.47 christos exit(2);
1653 1.47 christos }
1654 1.47 christos return n;
1655 1.12 mycroft }
1656 1.12 mycroft
1657 1.12 mycroft /*
1658 1.12 mycroft * Do the mount syscall with the update flag to push the export info into
1659 1.12 mycroft * the kernel.
1660 1.12 mycroft */
1661 1.47 christos static int
1662 1.52 christos do_mount(line, lineno, ep, grp, exflags, anoncrp, dirp, dirplen, fsb)
1663 1.52 christos const char *line;
1664 1.52 christos size_t lineno;
1665 1.12 mycroft struct exportlist *ep;
1666 1.12 mycroft struct grouplist *grp;
1667 1.12 mycroft int exflags;
1668 1.12 mycroft struct ucred *anoncrp;
1669 1.12 mycroft char *dirp;
1670 1.12 mycroft int dirplen;
1671 1.12 mycroft struct statfs *fsb;
1672 1.12 mycroft {
1673 1.47 christos char *cp = NULL;
1674 1.35 cgd u_int32_t **addrp;
1675 1.12 mycroft int done;
1676 1.12 mycroft char savedc = '\0';
1677 1.12 mycroft struct sockaddr_in sin, imask;
1678 1.12 mycroft union {
1679 1.12 mycroft struct ufs_args ua;
1680 1.12 mycroft struct iso_args ia;
1681 1.12 mycroft struct mfs_args ma;
1682 1.13 ws struct msdosfs_args da;
1683 1.27 chopps struct adosfs_args aa;
1684 1.12 mycroft } args;
1685 1.36 fvdl u_int32_t net;
1686 1.12 mycroft
1687 1.12 mycroft args.ua.fspec = 0;
1688 1.12 mycroft args.ua.export.ex_flags = exflags;
1689 1.12 mycroft args.ua.export.ex_anon = *anoncrp;
1690 1.38 fvdl args.ua.export.ex_indexfile = ep->ex_indexfile;
1691 1.47 christos (void)memset(&sin, 0, sizeof(sin));
1692 1.47 christos (void)memset(&imask, 0, sizeof(imask));
1693 1.12 mycroft sin.sin_family = AF_INET;
1694 1.12 mycroft sin.sin_len = sizeof(sin);
1695 1.12 mycroft imask.sin_family = AF_INET;
1696 1.12 mycroft imask.sin_len = sizeof(sin);
1697 1.12 mycroft if (grp->gr_type == GT_HOST)
1698 1.47 christos addrp = (u_int32_t **) grp->gr_ptr.gt_hostent->h_addr_list;
1699 1.12 mycroft else
1700 1.47 christos addrp = NULL;
1701 1.12 mycroft done = FALSE;
1702 1.12 mycroft while (!done) {
1703 1.12 mycroft switch (grp->gr_type) {
1704 1.12 mycroft case GT_HOST:
1705 1.12 mycroft if (addrp) {
1706 1.12 mycroft sin.sin_addr.s_addr = **addrp;
1707 1.12 mycroft args.ua.export.ex_addrlen = sizeof(sin);
1708 1.12 mycroft } else
1709 1.12 mycroft args.ua.export.ex_addrlen = 0;
1710 1.12 mycroft args.ua.export.ex_addr = (struct sockaddr *)&sin;
1711 1.12 mycroft args.ua.export.ex_masklen = 0;
1712 1.12 mycroft break;
1713 1.12 mycroft case GT_NET:
1714 1.12 mycroft if (grp->gr_ptr.gt_net.nt_mask)
1715 1.47 christos imask.sin_addr.s_addr =
1716 1.47 christos grp->gr_ptr.gt_net.nt_mask;
1717 1.12 mycroft else {
1718 1.47 christos net = ntohl(grp->gr_ptr.gt_net.nt_net);
1719 1.47 christos if (IN_CLASSA(net))
1720 1.47 christos imask.sin_addr.s_addr =
1721 1.47 christos inet_addr("255.0.0.0");
1722 1.47 christos else if (IN_CLASSB(net))
1723 1.47 christos imask.sin_addr.s_addr =
1724 1.47 christos inet_addr("255.255.0.0");
1725 1.47 christos else
1726 1.47 christos imask.sin_addr.s_addr =
1727 1.47 christos inet_addr("255.255.255.0");
1728 1.47 christos grp->gr_ptr.gt_net.nt_mask =
1729 1.47 christos imask.sin_addr.s_addr;
1730 1.12 mycroft }
1731 1.12 mycroft sin.sin_addr.s_addr = grp->gr_ptr.gt_net.nt_net;
1732 1.47 christos args.ua.export.ex_addr = (struct sockaddr *) &sin;
1733 1.47 christos args.ua.export.ex_addrlen = sizeof(sin);
1734 1.47 christos args.ua.export.ex_mask = (struct sockaddr *) &imask;
1735 1.47 christos args.ua.export.ex_masklen = sizeof(imask);
1736 1.12 mycroft break;
1737 1.12 mycroft #ifdef ISO
1738 1.12 mycroft case GT_ISO:
1739 1.12 mycroft args.ua.export.ex_addr =
1740 1.47 christos (struct sockaddr *) grp->gr_ptr.gt_isoaddr;
1741 1.12 mycroft args.ua.export.ex_addrlen =
1742 1.47 christos sizeof(struct sockaddr_iso);
1743 1.12 mycroft args.ua.export.ex_masklen = 0;
1744 1.12 mycroft break;
1745 1.47 christos #endif /* ISO */
1746 1.12 mycroft default:
1747 1.52 christos syslog(LOG_ERR, "\"%s\", line %ld: Bad netgroup type",
1748 1.52 christos line, (unsigned long)lineno);
1749 1.1 cgd if (cp)
1750 1.1 cgd *cp = savedc;
1751 1.12 mycroft return (1);
1752 1.12 mycroft };
1753 1.12 mycroft
1754 1.12 mycroft /*
1755 1.12 mycroft * XXX:
1756 1.12 mycroft * Maybe I should just use the fsb->f_mntonname path instead
1757 1.12 mycroft * of looping back up the dirp to the mount point??
1758 1.12 mycroft * Also, needs to know how to export all types of local
1759 1.30 jtc * exportable file systems and not just MOUNT_FFS.
1760 1.12 mycroft */
1761 1.12 mycroft while (mount(fsb->f_fstypename, dirp,
1762 1.47 christos fsb->f_flags | MNT_UPDATE, &args) == -1) {
1763 1.12 mycroft if (cp)
1764 1.12 mycroft *cp-- = savedc;
1765 1.12 mycroft else
1766 1.12 mycroft cp = dirp + dirplen - 1;
1767 1.12 mycroft if (errno == EPERM) {
1768 1.12 mycroft syslog(LOG_ERR,
1769 1.52 christos "\"%s\", line %ld: Can't change attributes for %s to %s",
1770 1.52 christos line, (unsigned long)lineno,
1771 1.47 christos dirp, (grp->gr_type == GT_HOST) ?
1772 1.47 christos grp->gr_ptr.gt_hostent->h_name :
1773 1.32 mrg (grp->gr_type == GT_NET) ?
1774 1.47 christos grp->gr_ptr.gt_net.nt_name :
1775 1.47 christos "Unknown");
1776 1.12 mycroft return (1);
1777 1.12 mycroft }
1778 1.12 mycroft if (opt_flags & OP_ALLDIRS) {
1779 1.52 christos syslog(LOG_ERR,
1780 1.52 christos "\"%s\", line %ld: Could not remount %s: %m",
1781 1.52 christos line, (unsigned long)lineno,
1782 1.47 christos dirp);
1783 1.12 mycroft return (1);
1784 1.12 mycroft }
1785 1.12 mycroft /* back up over the last component */
1786 1.12 mycroft while (*cp == '/' && cp > dirp)
1787 1.12 mycroft cp--;
1788 1.12 mycroft while (*(cp - 1) != '/' && cp > dirp)
1789 1.12 mycroft cp--;
1790 1.12 mycroft if (cp == dirp) {
1791 1.12 mycroft if (debug)
1792 1.47 christos (void)fprintf(stderr, "mnt unsucc\n");
1793 1.52 christos syslog(LOG_ERR,
1794 1.52 christos "\"%s\", line %ld: Can't export %s",
1795 1.52 christos line, (unsigned long)lineno, dirp);
1796 1.12 mycroft return (1);
1797 1.12 mycroft }
1798 1.12 mycroft savedc = *cp;
1799 1.12 mycroft *cp = '\0';
1800 1.12 mycroft }
1801 1.12 mycroft if (addrp) {
1802 1.12 mycroft ++addrp;
1803 1.47 christos if (*addrp == NULL)
1804 1.12 mycroft done = TRUE;
1805 1.12 mycroft } else
1806 1.12 mycroft done = TRUE;
1807 1.12 mycroft }
1808 1.12 mycroft if (cp)
1809 1.12 mycroft *cp = savedc;
1810 1.12 mycroft return (0);
1811 1.12 mycroft }
1812 1.12 mycroft
1813 1.12 mycroft /*
1814 1.12 mycroft * Translate a net address.
1815 1.12 mycroft */
1816 1.47 christos static int
1817 1.12 mycroft get_net(cp, net, maskflg)
1818 1.12 mycroft char *cp;
1819 1.12 mycroft struct netmsk *net;
1820 1.12 mycroft int maskflg;
1821 1.12 mycroft {
1822 1.12 mycroft struct netent *np;
1823 1.12 mycroft long netaddr;
1824 1.12 mycroft struct in_addr inetaddr, inetaddr2;
1825 1.12 mycroft char *name;
1826 1.12 mycroft
1827 1.41 lukem if ((np = getnetbyname(cp)) != NULL)
1828 1.12 mycroft inetaddr = inet_makeaddr(np->n_net, 0);
1829 1.12 mycroft else if (isdigit(*cp)) {
1830 1.12 mycroft if ((netaddr = inet_network(cp)) == -1)
1831 1.12 mycroft return (1);
1832 1.12 mycroft inetaddr = inet_makeaddr(netaddr, 0);
1833 1.12 mycroft /*
1834 1.12 mycroft * Due to arbritrary subnet masks, you don't know how many
1835 1.12 mycroft * bits to shift the address to make it into a network,
1836 1.12 mycroft * however you do know how to make a network address into
1837 1.12 mycroft * a host with host == 0 and then compare them.
1838 1.12 mycroft * (What a pest)
1839 1.12 mycroft */
1840 1.12 mycroft if (!maskflg) {
1841 1.12 mycroft setnetent(0);
1842 1.41 lukem while ((np = getnetent()) != NULL) {
1843 1.12 mycroft inetaddr2 = inet_makeaddr(np->n_net, 0);
1844 1.12 mycroft if (inetaddr2.s_addr == inetaddr.s_addr)
1845 1.12 mycroft break;
1846 1.9 ws }
1847 1.12 mycroft endnetent();
1848 1.11 ws }
1849 1.12 mycroft } else
1850 1.12 mycroft return (1);
1851 1.12 mycroft if (maskflg)
1852 1.12 mycroft net->nt_mask = inetaddr.s_addr;
1853 1.12 mycroft else {
1854 1.12 mycroft if (np)
1855 1.12 mycroft name = np->n_name;
1856 1.12 mycroft else
1857 1.12 mycroft name = inet_ntoa(inetaddr);
1858 1.47 christos net->nt_name = estrdup(name);
1859 1.12 mycroft net->nt_net = inetaddr.s_addr;
1860 1.1 cgd }
1861 1.12 mycroft return (0);
1862 1.1 cgd }
1863 1.1 cgd
1864 1.1 cgd /*
1865 1.1 cgd * Parse out the next white space separated field
1866 1.1 cgd */
1867 1.47 christos static void
1868 1.1 cgd nextfield(cp, endcp)
1869 1.1 cgd char **cp;
1870 1.1 cgd char **endcp;
1871 1.1 cgd {
1872 1.12 mycroft char *p;
1873 1.1 cgd
1874 1.1 cgd p = *cp;
1875 1.1 cgd while (*p == ' ' || *p == '\t')
1876 1.1 cgd p++;
1877 1.12 mycroft if (*p == '\n' || *p == '\0')
1878 1.1 cgd *cp = *endcp = p;
1879 1.12 mycroft else {
1880 1.12 mycroft *cp = p++;
1881 1.12 mycroft while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
1882 1.12 mycroft p++;
1883 1.12 mycroft *endcp = p;
1884 1.1 cgd }
1885 1.1 cgd }
1886 1.1 cgd
1887 1.1 cgd /*
1888 1.12 mycroft * Parse a description of a credential.
1889 1.1 cgd */
1890 1.47 christos static void
1891 1.12 mycroft parsecred(namelist, cr)
1892 1.12 mycroft char *namelist;
1893 1.12 mycroft struct ucred *cr;
1894 1.1 cgd {
1895 1.12 mycroft char *name;
1896 1.12 mycroft int cnt;
1897 1.12 mycroft char *names;
1898 1.12 mycroft struct passwd *pw;
1899 1.12 mycroft struct group *gr;
1900 1.12 mycroft int ngroups, groups[NGROUPS + 1];
1901 1.1 cgd
1902 1.12 mycroft /*
1903 1.12 mycroft * Set up the unpriviledged user.
1904 1.12 mycroft */
1905 1.12 mycroft cr->cr_ref = 1;
1906 1.12 mycroft cr->cr_uid = -2;
1907 1.22 mycroft cr->cr_gid = -2;
1908 1.22 mycroft cr->cr_ngroups = 0;
1909 1.12 mycroft /*
1910 1.12 mycroft * Get the user's password table entry.
1911 1.12 mycroft */
1912 1.12 mycroft names = strsep(&namelist, " \t\n");
1913 1.12 mycroft name = strsep(&names, ":");
1914 1.12 mycroft if (isdigit(*name) || *name == '-')
1915 1.12 mycroft pw = getpwuid(atoi(name));
1916 1.12 mycroft else
1917 1.12 mycroft pw = getpwnam(name);
1918 1.12 mycroft /*
1919 1.12 mycroft * Credentials specified as those of a user.
1920 1.12 mycroft */
1921 1.12 mycroft if (names == NULL) {
1922 1.12 mycroft if (pw == NULL) {
1923 1.12 mycroft syslog(LOG_ERR, "Unknown user: %s", name);
1924 1.12 mycroft return;
1925 1.12 mycroft }
1926 1.12 mycroft cr->cr_uid = pw->pw_uid;
1927 1.12 mycroft ngroups = NGROUPS + 1;
1928 1.12 mycroft if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups))
1929 1.12 mycroft syslog(LOG_ERR, "Too many groups");
1930 1.12 mycroft /*
1931 1.12 mycroft * Convert from int's to gid_t's and compress out duplicate
1932 1.12 mycroft */
1933 1.12 mycroft cr->cr_ngroups = ngroups - 1;
1934 1.22 mycroft cr->cr_gid = groups[0];
1935 1.22 mycroft for (cnt = 1; cnt < ngroups; cnt++)
1936 1.12 mycroft cr->cr_groups[cnt - 1] = groups[cnt];
1937 1.12 mycroft return;
1938 1.12 mycroft }
1939 1.12 mycroft /*
1940 1.12 mycroft * Explicit credential specified as a colon separated list:
1941 1.12 mycroft * uid:gid:gid:...
1942 1.12 mycroft */
1943 1.12 mycroft if (pw != NULL)
1944 1.12 mycroft cr->cr_uid = pw->pw_uid;
1945 1.12 mycroft else if (isdigit(*name) || *name == '-')
1946 1.12 mycroft cr->cr_uid = atoi(name);
1947 1.12 mycroft else {
1948 1.12 mycroft syslog(LOG_ERR, "Unknown user: %s", name);
1949 1.12 mycroft return;
1950 1.12 mycroft }
1951 1.12 mycroft cr->cr_ngroups = 0;
1952 1.12 mycroft while (names != NULL && *names != '\0' && cr->cr_ngroups < NGROUPS) {
1953 1.12 mycroft name = strsep(&names, ":");
1954 1.12 mycroft if (isdigit(*name) || *name == '-') {
1955 1.12 mycroft cr->cr_groups[cr->cr_ngroups++] = atoi(name);
1956 1.12 mycroft } else {
1957 1.12 mycroft if ((gr = getgrnam(name)) == NULL) {
1958 1.12 mycroft syslog(LOG_ERR, "Unknown group: %s", name);
1959 1.12 mycroft continue;
1960 1.12 mycroft }
1961 1.12 mycroft cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
1962 1.12 mycroft }
1963 1.1 cgd }
1964 1.12 mycroft if (names != NULL && *names != '\0' && cr->cr_ngroups == NGROUPS)
1965 1.12 mycroft syslog(LOG_ERR, "Too many groups");
1966 1.1 cgd }
1967 1.1 cgd
1968 1.12 mycroft #define STRSIZ (RPCMNT_NAMELEN+RPCMNT_PATHLEN+50)
1969 1.1 cgd /*
1970 1.1 cgd * Routines that maintain the remote mounttab
1971 1.1 cgd */
1972 1.47 christos static void
1973 1.12 mycroft get_mountlist()
1974 1.1 cgd {
1975 1.12 mycroft struct mountlist *mlp, **mlpp;
1976 1.15 mycroft char *host, *dirp, *cp;
1977 1.1 cgd char str[STRSIZ];
1978 1.1 cgd FILE *mlfile;
1979 1.1 cgd
1980 1.11 ws if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
1981 1.32 mrg syslog(LOG_ERR, "Can't open %s: %m", _PATH_RMOUNTLIST);
1982 1.1 cgd return;
1983 1.1 cgd }
1984 1.1 cgd mlpp = &mlhead;
1985 1.1 cgd while (fgets(str, STRSIZ, mlfile) != NULL) {
1986 1.15 mycroft cp = str;
1987 1.15 mycroft host = strsep(&cp, " \t\n");
1988 1.15 mycroft dirp = strsep(&cp, " \t\n");
1989 1.15 mycroft if (host == NULL || dirp == NULL)
1990 1.1 cgd continue;
1991 1.47 christos mlp = emalloc(sizeof(*mlp));
1992 1.47 christos (void)strncpy(mlp->ml_host, host, RPCMNT_NAMELEN);
1993 1.15 mycroft mlp->ml_host[RPCMNT_NAMELEN] = '\0';
1994 1.47 christos (void)strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
1995 1.15 mycroft mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
1996 1.47 christos mlp->ml_next = NULL;
1997 1.1 cgd *mlpp = mlp;
1998 1.1 cgd mlpp = &mlp->ml_next;
1999 1.1 cgd }
2000 1.47 christos (void)fclose(mlfile);
2001 1.1 cgd }
2002 1.1 cgd
2003 1.47 christos static int
2004 1.36 fvdl del_mlist(hostp, dirp, saddr)
2005 1.12 mycroft char *hostp, *dirp;
2006 1.36 fvdl struct sockaddr *saddr;
2007 1.1 cgd {
2008 1.12 mycroft struct mountlist *mlp, **mlpp;
2009 1.12 mycroft struct mountlist *mlp2;
2010 1.36 fvdl struct sockaddr_in *sin = (struct sockaddr_in *)saddr;
2011 1.1 cgd FILE *mlfile;
2012 1.36 fvdl int fnd = 0, ret = 0;
2013 1.1 cgd
2014 1.1 cgd mlpp = &mlhead;
2015 1.1 cgd mlp = mlhead;
2016 1.1 cgd while (mlp) {
2017 1.1 cgd if (!strcmp(mlp->ml_host, hostp) &&
2018 1.1 cgd (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
2019 1.37 fvdl if (!(mlp->ml_flag & DP_NORESMNT) &&
2020 1.36 fvdl ntohs(sin->sin_port) >= IPPORT_RESERVED) {
2021 1.36 fvdl syslog(LOG_NOTICE,
2022 1.47 christos "Umount request for %s:%s from %s refused\n",
2023 1.47 christos mlp->ml_host, mlp->ml_dirp,
2024 1.47 christos inet_ntoa(sin->sin_addr));
2025 1.36 fvdl ret = -1;
2026 1.42 fvdl goto cont;
2027 1.36 fvdl }
2028 1.1 cgd fnd = 1;
2029 1.12 mycroft mlp2 = mlp;
2030 1.12 mycroft *mlpp = mlp = mlp->ml_next;
2031 1.47 christos free(mlp2);
2032 1.12 mycroft } else {
2033 1.42 fvdl cont:
2034 1.12 mycroft mlpp = &mlp->ml_next;
2035 1.12 mycroft mlp = mlp->ml_next;
2036 1.1 cgd }
2037 1.1 cgd }
2038 1.1 cgd if (fnd) {
2039 1.1 cgd if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
2040 1.47 christos syslog(LOG_ERR, "Can't update %s: %m",
2041 1.47 christos _PATH_RMOUNTLIST);
2042 1.41 lukem return ret;
2043 1.1 cgd }
2044 1.1 cgd mlp = mlhead;
2045 1.1 cgd while (mlp) {
2046 1.47 christos (void)fprintf(mlfile, "%s %s\n", mlp->ml_host,
2047 1.47 christos mlp->ml_dirp);
2048 1.1 cgd mlp = mlp->ml_next;
2049 1.1 cgd }
2050 1.47 christos (void)fclose(mlfile);
2051 1.1 cgd }
2052 1.36 fvdl return ret;
2053 1.1 cgd }
2054 1.1 cgd
2055 1.47 christos static void
2056 1.36 fvdl add_mlist(hostp, dirp, flags)
2057 1.12 mycroft char *hostp, *dirp;
2058 1.36 fvdl int flags;
2059 1.1 cgd {
2060 1.12 mycroft struct mountlist *mlp, **mlpp;
2061 1.1 cgd FILE *mlfile;
2062 1.1 cgd
2063 1.1 cgd mlpp = &mlhead;
2064 1.1 cgd mlp = mlhead;
2065 1.1 cgd while (mlp) {
2066 1.1 cgd if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
2067 1.1 cgd return;
2068 1.1 cgd mlpp = &mlp->ml_next;
2069 1.1 cgd mlp = mlp->ml_next;
2070 1.1 cgd }
2071 1.47 christos mlp = emalloc(sizeof(*mlp));
2072 1.12 mycroft strncpy(mlp->ml_host, hostp, RPCMNT_NAMELEN);
2073 1.12 mycroft mlp->ml_host[RPCMNT_NAMELEN] = '\0';
2074 1.12 mycroft strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
2075 1.12 mycroft mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
2076 1.36 fvdl mlp->ml_flag = flags;
2077 1.47 christos mlp->ml_next = NULL;
2078 1.1 cgd *mlpp = mlp;
2079 1.1 cgd if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
2080 1.32 mrg syslog(LOG_ERR, "Can't update %s: %m", _PATH_RMOUNTLIST);
2081 1.1 cgd return;
2082 1.1 cgd }
2083 1.47 christos (void)fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
2084 1.47 christos (void)fclose(mlfile);
2085 1.1 cgd }
2086 1.1 cgd
2087 1.1 cgd /*
2088 1.1 cgd * This function is called via. SIGTERM when the system is going down.
2089 1.1 cgd * It sends a broadcast RPCMNT_UMNTALL.
2090 1.1 cgd */
2091 1.47 christos /* ARGSUSED */
2092 1.47 christos static void
2093 1.45 christos send_umntall(n)
2094 1.45 christos int n;
2095 1.1 cgd {
2096 1.47 christos (void)clnt_broadcast(RPCPROG_MNT, RPCMNT_VER1, RPCMNT_UMNTALL,
2097 1.47 christos xdr_void, NULL, xdr_void, NULL, umntall_each);
2098 1.12 mycroft exit(0);
2099 1.1 cgd }
2100 1.1 cgd
2101 1.47 christos static int
2102 1.1 cgd umntall_each(resultsp, raddr)
2103 1.1 cgd caddr_t resultsp;
2104 1.1 cgd struct sockaddr_in *raddr;
2105 1.1 cgd {
2106 1.1 cgd return (1);
2107 1.1 cgd }
2108 1.1 cgd
2109 1.1 cgd /*
2110 1.12 mycroft * Free up a group list.
2111 1.12 mycroft */
2112 1.47 christos static void
2113 1.12 mycroft free_grp(grp)
2114 1.12 mycroft struct grouplist *grp;
2115 1.12 mycroft {
2116 1.12 mycroft char **addrp;
2117 1.12 mycroft
2118 1.12 mycroft if (grp->gr_type == GT_HOST) {
2119 1.12 mycroft if (grp->gr_ptr.gt_hostent->h_name) {
2120 1.12 mycroft addrp = grp->gr_ptr.gt_hostent->h_addr_list;
2121 1.46 ross if (addrp) {
2122 1.47 christos while (*addrp)
2123 1.46 ross free(*addrp++);
2124 1.46 ross free(grp->gr_ptr.gt_hostent->h_addr_list);
2125 1.46 ross }
2126 1.12 mycroft free(grp->gr_ptr.gt_hostent->h_name);
2127 1.12 mycroft }
2128 1.47 christos free(grp->gr_ptr.gt_hostent);
2129 1.12 mycroft } else if (grp->gr_type == GT_NET) {
2130 1.12 mycroft if (grp->gr_ptr.gt_net.nt_name)
2131 1.12 mycroft free(grp->gr_ptr.gt_net.nt_name);
2132 1.12 mycroft }
2133 1.12 mycroft #ifdef ISO
2134 1.12 mycroft else if (grp->gr_type == GT_ISO)
2135 1.47 christos free(grp->gr_ptr.gt_isoaddr);
2136 1.12 mycroft #endif
2137 1.47 christos free(grp);
2138 1.12 mycroft }
2139 1.12 mycroft
2140 1.47 christos #if 0
2141 1.47 christos static void
2142 1.47 christos SYSLOG(int pri, const char *fmt,...)
2143 1.12 mycroft {
2144 1.12 mycroft va_list ap;
2145 1.12 mycroft
2146 1.12 mycroft va_start(ap, fmt);
2147 1.28 thorpej
2148 1.28 thorpej if (debug)
2149 1.28 thorpej vfprintf(stderr, fmt, ap);
2150 1.28 thorpej else
2151 1.28 thorpej vsyslog(pri, fmt, ap);
2152 1.28 thorpej
2153 1.12 mycroft va_end(ap);
2154 1.12 mycroft }
2155 1.47 christos #endif
2156 1.12 mycroft
2157 1.12 mycroft /*
2158 1.12 mycroft * Check options for consistency.
2159 1.12 mycroft */
2160 1.47 christos static int
2161 1.52 christos check_options(line, lineno, dp)
2162 1.52 christos const char *line;
2163 1.52 christos size_t lineno;
2164 1.12 mycroft struct dirlist *dp;
2165 1.12 mycroft {
2166 1.12 mycroft
2167 1.52 christos if (dp == NULL) {
2168 1.52 christos syslog(LOG_ERR,
2169 1.52 christos "\"%s\", line %ld: missing directory list",
2170 1.52 christos line, (unsigned long)lineno);
2171 1.47 christos return (1);
2172 1.52 christos }
2173 1.47 christos if ((opt_flags & (OP_MAPROOT|OP_MAPALL)) == (OP_MAPROOT|OP_MAPALL) ||
2174 1.47 christos (opt_flags & (OP_MAPROOT|OP_KERB)) == (OP_MAPROOT|OP_KERB) ||
2175 1.47 christos (opt_flags & (OP_MAPALL|OP_KERB)) == (OP_MAPALL|OP_KERB)) {
2176 1.47 christos syslog(LOG_ERR,
2177 1.52 christos "\"%s\", line %ld: -mapall, -maproot and -kerb mutually exclusive",
2178 1.52 christos line, (unsigned long)lineno);
2179 1.47 christos return (1);
2180 1.12 mycroft }
2181 1.12 mycroft if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
2182 1.52 christos syslog(LOG_ERR, "\"%s\", line %ld: -mask requires -net",
2183 1.52 christos line, (unsigned long)lineno);
2184 1.47 christos return (1);
2185 1.12 mycroft }
2186 1.47 christos if ((opt_flags & (OP_NET|OP_ISO)) == (OP_NET|OP_ISO)) {
2187 1.52 christos syslog(LOG_ERR,
2188 1.52 christos "\"%s\", line %ld: -net and -iso mutually exclusive",
2189 1.52 christos line, (unsigned long)lineno);
2190 1.47 christos return (1);
2191 1.12 mycroft }
2192 1.12 mycroft if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
2193 1.52 christos syslog(LOG_ERR,
2194 1.52 christos "\"%s\", line %ld: -alldir has multiple directories",
2195 1.52 christos line, (unsigned long)lineno);
2196 1.47 christos return (1);
2197 1.12 mycroft }
2198 1.12 mycroft return (0);
2199 1.12 mycroft }
2200 1.12 mycroft
2201 1.12 mycroft /*
2202 1.12 mycroft * Check an absolute directory path for any symbolic links. Return true
2203 1.12 mycroft * if no symbolic links are found.
2204 1.1 cgd */
2205 1.47 christos static int
2206 1.52 christos check_dirpath(line, lineno, dirp)
2207 1.52 christos const char *line;
2208 1.52 christos size_t lineno;
2209 1.12 mycroft char *dirp;
2210 1.1 cgd {
2211 1.12 mycroft char *cp;
2212 1.12 mycroft struct stat sb;
2213 1.52 christos char *file = "";
2214 1.12 mycroft
2215 1.52 christos for (cp = dirp + 1; *cp; cp++) {
2216 1.12 mycroft if (*cp == '/') {
2217 1.12 mycroft *cp = '\0';
2218 1.52 christos if (lstat(dirp, &sb) == -1)
2219 1.52 christos goto bad;
2220 1.52 christos if (!S_ISDIR(sb.st_mode))
2221 1.52 christos goto bad1;
2222 1.12 mycroft *cp = '/';
2223 1.12 mycroft }
2224 1.1 cgd }
2225 1.52 christos
2226 1.52 christos cp = NULL;
2227 1.52 christos if (lstat(dirp, &sb) == -1)
2228 1.52 christos goto bad;
2229 1.52 christos
2230 1.52 christos if (!S_ISDIR(sb.st_mode) && !S_ISREG(sb.st_mode)) {
2231 1.52 christos file = " file or a";
2232 1.52 christos goto bad1;
2233 1.52 christos }
2234 1.52 christos
2235 1.52 christos return 1;
2236 1.52 christos
2237 1.52 christos bad:
2238 1.52 christos syslog(LOG_ERR,
2239 1.52 christos "\"%s\", line %ld: lstat for `%s' failed: %m",
2240 1.52 christos line, (unsigned long)lineno, dirp);
2241 1.52 christos if (cp)
2242 1.52 christos *cp = '/';
2243 1.52 christos return 0;
2244 1.52 christos
2245 1.52 christos bad1:
2246 1.52 christos syslog(LOG_ERR,
2247 1.52 christos "\"%s\", line %ld: `%s' is not a%s directory",
2248 1.52 christos line, (unsigned long)lineno, file, dirp);
2249 1.52 christos if (cp)
2250 1.52 christos *cp = '/';
2251 1.52 christos return 0;
2252 1.1 cgd }
2253