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