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