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