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