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