mountd.c revision 1.100 1 /* $NetBSD: mountd.c,v 1.100 2005/12/04 18:01:53 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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35
36 /*
37 * XXX The ISO support can't possibly work..
38 */
39
40 #include <sys/cdefs.h>
41 #ifndef lint
42 __COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
43 The Regents of the University of California. All rights reserved.\n");
44 #endif /* not lint */
45
46 #ifndef lint
47 #if 0
48 static char sccsid[] = "@(#)mountd.c 8.15 (Berkeley) 5/1/95";
49 #else
50 __RCSID("$NetBSD: mountd.c,v 1.100 2005/12/04 18:01:53 christos Exp $");
51 #endif
52 #endif /* not lint */
53
54 #include <sys/param.h>
55 #include <sys/file.h>
56 #include <sys/ioctl.h>
57 #include <sys/mount.h>
58 #include <sys/socket.h>
59 #include <sys/stat.h>
60 #include <syslog.h>
61 #include <sys/ucred.h>
62
63 #include <rpc/rpc.h>
64 #include <rpc/pmap_clnt.h>
65 #include <rpc/pmap_prot.h>
66 #include <rpcsvc/mount.h>
67 #ifdef ISO
68 #include <netiso/iso.h>
69 #endif
70 #include <nfs/rpcv2.h>
71 #include <nfs/nfsproto.h>
72 #include <nfs/nfs.h>
73 #include <nfs/nfsmount.h>
74
75 #include <arpa/inet.h>
76
77 #include <ctype.h>
78 #include <errno.h>
79 #include <grp.h>
80 #include <netdb.h>
81 #include <pwd.h>
82 #include <netgroup.h>
83 #include <signal.h>
84 #include <stdio.h>
85 #include <stdlib.h>
86 #include <string.h>
87 #include <unistd.h>
88 #include <netgroup.h>
89 #include <err.h>
90 #include <util.h>
91 #include "pathnames.h"
92 #ifdef KERBEROS
93 #include <kerberosIV/krb.h>
94 #include "kuid.h"
95 #endif
96
97 #ifdef IPSEC
98 #include <netinet6/ipsec.h>
99 #ifndef IPSEC_POLICY_IPSEC /* no ipsec support on old ipsec */
100 #undef IPSEC
101 #endif
102 #include "ipsec.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 struct sockaddr_storage nt_net;
144 int nt_len;
145 char *nt_name;
146 };
147
148 union grouptypes {
149 struct addrinfo *gt_addrinfo;
150 struct netmsk gt_net;
151 #ifdef ISO
152 struct sockaddr_iso *gt_isoaddr;
153 #endif
154 };
155
156 struct grouplist {
157 int gr_type;
158 union grouptypes gr_ptr;
159 struct grouplist *gr_next;
160 };
161 /* Group types */
162 #define GT_NULL 0x0
163 #define GT_HOST 0x1
164 #define GT_NET 0x2
165 #define GT_ISO 0x4
166
167 struct hostlist {
168 int ht_flag;/* Uses DP_xx bits */
169 struct grouplist *ht_grp;
170 struct hostlist *ht_next;
171 };
172
173 struct fhreturn {
174 int fhr_flag;
175 int fhr_vers;
176 nfsfh_t fhr_fh;
177 };
178
179 /* Global defs */
180 static char *add_expdir __P((struct dirlist **, char *, int));
181 static void add_dlist __P((struct dirlist **, struct dirlist *,
182 struct grouplist *, int));
183 static void add_mlist __P((char *, char *, int));
184 static int check_dirpath __P((const char *, size_t, char *));
185 static int check_options __P((const char *, size_t, struct dirlist *));
186 static int chk_host __P((struct dirlist *, struct sockaddr *, int *, int *));
187 static int del_mlist __P((char *, char *, struct sockaddr *));
188 static struct dirlist *dirp_search __P((struct dirlist *, char *));
189 static int do_nfssvc __P((const char *, size_t, struct exportlist *,
190 struct grouplist *, int, struct uucred *, char *, int, struct statvfs *));
191 static int do_opt __P((const char *, size_t, char **, char **,
192 struct exportlist *, struct grouplist *, int *, int *, struct uucred *));
193 static struct exportlist *ex_search __P((fsid_t *));
194 static int parse_directory __P((const char *, size_t, struct grouplist *,
195 int, char *, struct exportlist **, struct statvfs *));
196 static int parse_host_netgroup __P((const char *, size_t, struct exportlist *,
197 struct grouplist *, char *, int *, struct grouplist **));
198 static struct exportlist *get_exp __P((void));
199 static void free_dir __P((struct dirlist *));
200 static void free_exp __P((struct exportlist *));
201 static void free_grp __P((struct grouplist *));
202 static void free_host __P((struct hostlist *));
203 static void get_exportlist __P((int));
204 static int get_host __P((const char *, size_t, const char *,
205 struct grouplist *));
206 static struct hostlist *get_ht __P((void));
207 static void get_mountlist __P((void));
208 static int get_net __P((char *, struct netmsk *, int));
209 static void free_exp_grp __P((struct exportlist *, struct grouplist *));
210 static struct grouplist *get_grp __P((void));
211 static void hang_dirp __P((struct dirlist *, struct grouplist *,
212 struct exportlist *, int));
213 static void mntsrv __P((struct svc_req *, SVCXPRT *));
214 static void nextfield __P((char **, char **));
215 static void parsecred __P((char *, struct uucred *));
216 static int put_exlist __P((struct dirlist *, XDR *, struct dirlist *, int *));
217 static int scan_tree __P((struct dirlist *, struct sockaddr *));
218 static void send_umntall __P((int));
219 static int umntall_each __P((caddr_t, struct sockaddr_in *));
220 static int xdr_dir __P((XDR *, char *));
221 static int xdr_explist __P((XDR *, caddr_t));
222 static int xdr_fhs __P((XDR *, caddr_t));
223 static int xdr_mlist __P((XDR *, caddr_t));
224 static void *emalloc __P((size_t));
225 static char *estrdup __P((const char *));
226 static int bitcmp __P((void *, void *, int));
227 static int netpartcmp __P((struct sockaddr *, struct sockaddr *, int));
228 static int sacmp __P((struct sockaddr *, struct sockaddr *));
229 static int allones __P((struct sockaddr_storage *, int));
230 static int countones __P((struct sockaddr *));
231 #ifdef ISO
232 static int get_isoaddr __P((const char *, size_t, char *, struct grouplist *));
233 #endif
234 static void bind_resv_port __P((int, sa_family_t, in_port_t));
235 static struct exportlist *exphead;
236 static struct mountlist *mlhead;
237 static struct grouplist *grphead;
238 static char *exname;
239 static struct uucred def_anon = {
240 1,
241 (uid_t) -2,
242 (gid_t) -2,
243 0,
244 {}
245 };
246
247 static int opt_flags;
248 static int have_v6 = 1;
249 static const int ninumeric = NI_NUMERICHOST;
250
251 /* Bits for above */
252 #define OP_MAPROOT 0x001
253 #define OP_MAPALL 0x002
254 #define OP_KERB 0x004
255 #define OP_MASK 0x008
256 #define OP_NET 0x010
257 #define OP_ISO 0x020
258 #define OP_ALLDIRS 0x040
259 #define OP_NORESPORT 0x080
260 #define OP_NORESMNT 0x100
261 #define OP_MASKLEN 0x200
262
263 static int debug = 0;
264 #if 0
265 static void SYSLOG __P((int, const char *,...));
266 #endif
267 int main __P((int, char *[]));
268
269 /*
270 * If this is non-zero, -noresvport and -noresvmnt are implied for
271 * each export.
272 */
273 static int noprivports;
274
275 /*
276 * Mountd server for NFS mount protocol as described in:
277 * NFS: Network File System Protocol Specification, RFC1094, Appendix A
278 * The optional arguments are the exports file name
279 * default: _PATH_EXPORTS
280 * "-d" to enable debugging
281 * and "-n" to allow nonroot mount.
282 */
283 int
284 main(argc, argv)
285 int argc;
286 char **argv;
287 {
288 SVCXPRT *udptransp, *tcptransp, *udp6transp, *tcp6transp;
289 struct netconfig *udpconf, *tcpconf, *udp6conf, *tcp6conf;
290 int udpsock, tcpsock, udp6sock, tcp6sock;
291 int xcreated = 0, s;
292 int c, one = 1;
293 int maxrec = RPC_MAXDATASIZE;
294 in_port_t forcedport = 0;
295 #ifdef IPSEC
296 char *policy = NULL;
297 #define ADDOPTS "P:"
298 #else
299 #define ADDOPTS
300 #endif
301
302 while ((c = getopt(argc, argv, "dNnrp:" ADDOPTS)) != -1)
303 switch (c) {
304 #ifdef IPSEC
305 case 'P':
306 if (ipsecsetup_test(policy = optarg))
307 errx(1, "Invalid ipsec policy `%s'", policy);
308 break;
309 #endif
310 case 'p':
311 /* A forced port "0" will dynamically allocate a port */
312 forcedport = atoi(optarg);
313 break;
314 case 'd':
315 debug = 1;
316 break;
317 case 'N':
318 noprivports = 1;
319 break;
320 /* Compatibility */
321 case 'n':
322 case 'r':
323 break;
324 default:
325 fprintf(stderr, "usage: %s [-dNn]"
326 #ifdef IPSEC
327 " [-P policy]"
328 #endif
329 " [-p port] [exportsfile]\n", getprogname());
330 exit(1);
331 };
332 argc -= optind;
333 argv += optind;
334 grphead = NULL;
335 exphead = NULL;
336 mlhead = NULL;
337 if (argc == 1)
338 exname = *argv;
339 else
340 exname = _PATH_EXPORTS;
341 openlog("mountd", LOG_PID | (debug ? LOG_PERROR : 0), LOG_DAEMON);
342
343 s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
344 if (s < 0)
345 have_v6 = 0;
346 else
347 close(s);
348
349 if (debug)
350 (void)fprintf(stderr, "Getting export list.\n");
351 get_exportlist(0);
352 if (debug)
353 (void)fprintf(stderr, "Getting mount list.\n");
354 get_mountlist();
355 if (debug)
356 (void)fprintf(stderr, "Here we go.\n");
357 if (debug == 0) {
358 daemon(0, 0);
359 (void)signal(SIGINT, SIG_IGN);
360 (void)signal(SIGQUIT, SIG_IGN);
361 }
362 (void)signal(SIGHUP, get_exportlist);
363 (void)signal(SIGTERM, send_umntall);
364 pidfile(NULL);
365
366 rpcb_unset(RPCPROG_MNT, RPCMNT_VER1, NULL);
367 rpcb_unset(RPCPROG_MNT, RPCMNT_VER3, NULL);
368
369 udpsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
370 tcpsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
371 udp6sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
372 tcp6sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
373
374 /*
375 * We're doing host-based access checks here, so don't allow
376 * v4-in-v6 to confuse things. The kernel will disable it
377 * by default on NFS sockets too.
378 */
379 if (udp6sock != -1 && setsockopt(udp6sock, IPPROTO_IPV6,
380 IPV6_V6ONLY, &one, sizeof one) < 0){
381 syslog(LOG_ERR, "can't disable v4-in-v6 on UDP socket");
382 exit(1);
383 }
384 if (tcp6sock != -1 && setsockopt(tcp6sock, IPPROTO_IPV6,
385 IPV6_V6ONLY, &one, sizeof one) < 0){
386 syslog(LOG_ERR, "can't disable v4-in-v6 on UDP socket");
387 exit(1);
388 }
389
390 udpconf = getnetconfigent("udp");
391 tcpconf = getnetconfigent("tcp");
392 udp6conf = getnetconfigent("udp6");
393 tcp6conf = getnetconfigent("tcp6");
394
395 rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
396
397 if (udpsock != -1 && udpconf != NULL) {
398 bind_resv_port(udpsock, AF_INET, forcedport);
399 #ifdef IPSEC
400 if (policy)
401 ipsecsetup(AF_INET, udpsock, policy);
402 #endif
403 udptransp = svc_dg_create(udpsock, 0, 0);
404 if (udptransp != NULL) {
405 if (!svc_reg(udptransp, RPCPROG_MNT, RPCMNT_VER1,
406 mntsrv, udpconf) ||
407 !svc_reg(udptransp, RPCPROG_MNT, RPCMNT_VER3,
408 mntsrv, udpconf))
409 syslog(LOG_WARNING, "can't register UDP service");
410 else
411 xcreated++;
412 } else
413 syslog(LOG_WARNING, "can't create UDP service");
414
415 }
416
417 if (tcpsock != -1 && tcpconf != NULL) {
418 bind_resv_port(tcpsock, AF_INET, forcedport);
419 #ifdef IPSEC
420 if (policy)
421 ipsecsetup(AF_INET, tcpsock, policy);
422 #endif
423 listen(tcpsock, SOMAXCONN);
424 tcptransp = svc_vc_create(tcpsock, RPC_MAXDATASIZE,
425 RPC_MAXDATASIZE);
426 if (tcptransp != NULL) {
427 if (!svc_reg(tcptransp, RPCPROG_MNT, RPCMNT_VER1,
428 mntsrv, tcpconf) ||
429 !svc_reg(tcptransp, RPCPROG_MNT, RPCMNT_VER3,
430 mntsrv, tcpconf))
431 syslog(LOG_WARNING, "can't register TCP service");
432 else
433 xcreated++;
434 } else
435 syslog(LOG_WARNING, "can't create TCP service");
436
437 }
438
439 if (udp6sock != -1 && udp6conf != NULL) {
440 bind_resv_port(udp6sock, AF_INET6, forcedport);
441 #ifdef IPSEC
442 if (policy)
443 ipsecsetup(AF_INET6, tcpsock, policy);
444 #endif
445 udp6transp = svc_dg_create(udp6sock, 0, 0);
446 if (udp6transp != NULL) {
447 if (!svc_reg(udp6transp, RPCPROG_MNT, RPCMNT_VER1,
448 mntsrv, udp6conf) ||
449 !svc_reg(udp6transp, RPCPROG_MNT, RPCMNT_VER3,
450 mntsrv, udp6conf))
451 syslog(LOG_WARNING, "can't register UDP6 service");
452 else
453 xcreated++;
454 } else
455 syslog(LOG_WARNING, "can't create UDP6 service");
456
457 }
458
459 if (tcp6sock != -1 && tcp6conf != NULL) {
460 bind_resv_port(tcp6sock, AF_INET6, forcedport);
461 #ifdef IPSEC
462 if (policy)
463 ipsecsetup(AF_INET6, tcpsock, policy);
464 #endif
465 listen(tcp6sock, SOMAXCONN);
466 tcp6transp = svc_vc_create(tcp6sock, RPC_MAXDATASIZE,
467 RPC_MAXDATASIZE);
468 if (tcp6transp != NULL) {
469 if (!svc_reg(tcp6transp, RPCPROG_MNT, RPCMNT_VER1,
470 mntsrv, tcp6conf) ||
471 !svc_reg(tcp6transp, RPCPROG_MNT, RPCMNT_VER3,
472 mntsrv, tcp6conf))
473 syslog(LOG_WARNING, "can't register TCP6 service");
474 else
475 xcreated++;
476 } else
477 syslog(LOG_WARNING, "can't create TCP6 service");
478
479 }
480
481 if (xcreated == 0) {
482 syslog(LOG_ERR, "could not create any services");
483 exit(1);
484 }
485
486 #ifdef KERBEROS
487 kuidinit();
488 #endif
489 svc_run();
490 syslog(LOG_ERR, "Mountd died");
491 exit(1);
492 }
493
494 /*
495 * The mount rpc service
496 */
497 void
498 mntsrv(rqstp, transp)
499 struct svc_req *rqstp;
500 SVCXPRT *transp;
501 {
502 struct exportlist *ep;
503 struct dirlist *dp;
504 struct fhreturn fhr;
505 struct stat stb;
506 struct statvfs fsb;
507 struct addrinfo *ai;
508 char host[NI_MAXHOST], numerichost[NI_MAXHOST];
509 int lookup_failed = 1;
510 struct sockaddr *saddr;
511 u_short sport;
512 char rpcpath[RPCMNT_PATHLEN + 1], dirpath[MAXPATHLEN];
513 long bad = EACCES;
514 int defset, hostset, ret;
515 sigset_t sighup_mask;
516 struct sockaddr_in6 *sin6;
517 struct sockaddr_in *sin;
518
519 (void)sigemptyset(&sighup_mask);
520 (void)sigaddset(&sighup_mask, SIGHUP);
521 saddr = svc_getrpccaller(transp)->buf;
522 switch (saddr->sa_family) {
523 case AF_INET6:
524 sin6 = (struct sockaddr_in6 *)saddr;
525 sport = ntohs(sin6->sin6_port);
526 break;
527 case AF_INET:
528 sin = (struct sockaddr_in *)saddr;
529 sport = ntohs(sin->sin_port);
530 break;
531 default:
532 syslog(LOG_ERR, "request from unknown address family");
533 return;
534 }
535 lookup_failed = getnameinfo(saddr, saddr->sa_len, host, sizeof host,
536 NULL, 0, 0);
537 if (getnameinfo(saddr, saddr->sa_len, numerichost,
538 sizeof numerichost, NULL, 0, ninumeric) != 0)
539 strlcpy(numerichost, "?", sizeof(numerichost));
540 ai = NULL;
541 #ifdef KERBEROS
542 kuidreset();
543 #endif
544 ret = 0;
545 switch (rqstp->rq_proc) {
546 case NULLPROC:
547 if (!svc_sendreply(transp, xdr_void, NULL))
548 syslog(LOG_ERR, "Can't send reply");
549 return;
550 case MOUNTPROC_MNT:
551 if (debug)
552 fprintf(stderr,
553 "got mount request from %s\n", numerichost);
554 if (!svc_getargs(transp, xdr_dir, rpcpath)) {
555 if (debug)
556 fprintf(stderr, "-> garbage args\n");
557 svcerr_decode(transp);
558 return;
559 }
560 if (debug)
561 fprintf(stderr,
562 "-> rpcpath: %s\n", rpcpath);
563 /*
564 * Get the real pathname and make sure it is a file or
565 * directory that exists.
566 */
567 if (realpath(rpcpath, dirpath) == 0 ||
568 stat(dirpath, &stb) < 0 ||
569 (!S_ISDIR(stb.st_mode) && !S_ISREG(stb.st_mode)) ||
570 statvfs(dirpath, &fsb) < 0) {
571 (void)chdir("/"); /* Just in case realpath doesn't */
572 if (debug)
573 (void)fprintf(stderr, "-> stat failed on %s\n",
574 dirpath);
575 if (!svc_sendreply(transp, xdr_long, (caddr_t) &bad))
576 syslog(LOG_ERR, "Can't send reply");
577 return;
578 }
579 if (debug)
580 fprintf(stderr,
581 "-> dirpath: %s\n", dirpath);
582 /* Check in the exports list */
583 (void)sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
584 ep = ex_search(&fsb.f_fsidx);
585 hostset = defset = 0;
586 if (ep && (chk_host(ep->ex_defdir, saddr, &defset,
587 &hostset) || ((dp = dirp_search(ep->ex_dirl, dirpath)) &&
588 chk_host(dp, saddr, &defset, &hostset)) ||
589 (defset && scan_tree(ep->ex_defdir, saddr) == 0 &&
590 scan_tree(ep->ex_dirl, saddr) == 0))) {
591 if ((hostset & DP_HOSTSET) == 0) {
592 hostset = defset;
593 }
594 if (sport >= IPPORT_RESERVED &&
595 !(hostset & DP_NORESMNT)) {
596 syslog(LOG_NOTICE,
597 "Refused mount RPC from host %s port %d",
598 numerichost, sport);
599 svcerr_weakauth(transp);
600 goto out;
601 }
602 fhr.fhr_flag = hostset;
603 fhr.fhr_vers = rqstp->rq_vers;
604 /* Get the file handle */
605 (void)memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t));
606 if (getfh(dirpath, (fhandle_t *) &fhr.fhr_fh) < 0) {
607 bad = errno;
608 syslog(LOG_ERR, "Can't get fh for %s", dirpath);
609 if (!svc_sendreply(transp, xdr_long,
610 (char *)&bad))
611 syslog(LOG_ERR, "Can't send reply");
612 goto out;
613 }
614 if (!svc_sendreply(transp, xdr_fhs, (char *) &fhr))
615 syslog(LOG_ERR, "Can't send reply");
616 if (!lookup_failed)
617 add_mlist(host, dirpath, hostset);
618 else
619 add_mlist(numerichost, dirpath, hostset);
620 if (debug)
621 (void)fprintf(stderr, "Mount successful.\n");
622 } else {
623 if (!svc_sendreply(transp, xdr_long, (caddr_t) &bad))
624 syslog(LOG_ERR, "Can't send reply");
625 }
626 out:
627 (void)sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
628 return;
629 case MOUNTPROC_DUMP:
630 if (!svc_sendreply(transp, xdr_mlist, NULL))
631 syslog(LOG_ERR, "Can't send reply");
632 return;
633 case MOUNTPROC_UMNT:
634 if (!svc_getargs(transp, xdr_dir, dirpath)) {
635 svcerr_decode(transp);
636 return;
637 }
638 if (!lookup_failed)
639 ret = del_mlist(host, dirpath, saddr);
640 ret |= del_mlist(numerichost, dirpath, saddr);
641 if (ret) {
642 svcerr_weakauth(transp);
643 return;
644 }
645 if (!svc_sendreply(transp, xdr_void, NULL))
646 syslog(LOG_ERR, "Can't send reply");
647 return;
648 case MOUNTPROC_UMNTALL:
649 if (!lookup_failed)
650 ret = del_mlist(host, NULL, saddr);
651 ret |= del_mlist(numerichost, NULL, saddr);
652 if (ret) {
653 svcerr_weakauth(transp);
654 return;
655 }
656 if (!svc_sendreply(transp, xdr_void, NULL))
657 syslog(LOG_ERR, "Can't send reply");
658 return;
659 case MOUNTPROC_EXPORT:
660 case MOUNTPROC_EXPORTALL:
661 if (!svc_sendreply(transp, xdr_explist, NULL))
662 syslog(LOG_ERR, "Can't send reply");
663 return;
664
665 #ifdef KERBEROS
666 case MOUNTPROC_KUIDMAP:
667 case MOUNTPROC_KUIDUMAP:
668 case MOUNTPROC_KUIDPURGE:
669 case MOUNTPROC_KUIDUPURGE:
670 kuidops(rqstp, transp);
671 return;
672 #endif
673
674 default:
675 svcerr_noproc(transp);
676 return;
677 }
678 }
679
680 /*
681 * Xdr conversion for a dirpath string
682 */
683 static int
684 xdr_dir(xdrsp, dirp)
685 XDR *xdrsp;
686 char *dirp;
687 {
688
689 return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
690 }
691
692 /*
693 * Xdr routine to generate file handle reply
694 */
695 static int
696 xdr_fhs(xdrsp, cp)
697 XDR *xdrsp;
698 caddr_t cp;
699 {
700 struct fhreturn *fhrp = (struct fhreturn *) cp;
701 long ok = 0, len, auth;
702
703 if (!xdr_long(xdrsp, &ok))
704 return (0);
705 switch (fhrp->fhr_vers) {
706 case 1:
707 return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH));
708 case 3:
709 len = NFSX_V3FH;
710 if (!xdr_long(xdrsp, &len))
711 return (0);
712 if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len))
713 return (0);
714 if (fhrp->fhr_flag & DP_KERB)
715 auth = RPCAUTH_KERB4;
716 else
717 auth = RPCAUTH_UNIX;
718 len = 1;
719 if (!xdr_long(xdrsp, &len))
720 return (0);
721 return (xdr_long(xdrsp, &auth));
722 };
723 return (0);
724 }
725
726 int
727 xdr_mlist(xdrsp, cp)
728 XDR *xdrsp;
729 caddr_t cp;
730 {
731 struct mountlist *mlp;
732 int true = 1;
733 int false = 0;
734 char *strp;
735
736 mlp = mlhead;
737 while (mlp) {
738 if (!xdr_bool(xdrsp, &true))
739 return (0);
740 strp = &mlp->ml_host[0];
741 if (!xdr_string(xdrsp, &strp, RPCMNT_NAMELEN))
742 return (0);
743 strp = &mlp->ml_dirp[0];
744 if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
745 return (0);
746 mlp = mlp->ml_next;
747 }
748 if (!xdr_bool(xdrsp, &false))
749 return (0);
750 return (1);
751 }
752
753 /*
754 * Xdr conversion for export list
755 */
756 int
757 xdr_explist(xdrsp, cp)
758 XDR *xdrsp;
759 caddr_t cp;
760 {
761 struct exportlist *ep;
762 int false = 0;
763 int putdef;
764 sigset_t sighup_mask;
765
766 (void)sigemptyset(&sighup_mask);
767 (void)sigaddset(&sighup_mask, SIGHUP);
768 (void)sigprocmask(SIG_BLOCK, &sighup_mask, NULL);
769 ep = exphead;
770 while (ep) {
771 putdef = 0;
772 if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir, &putdef))
773 goto errout;
774 if (ep->ex_defdir && putdef == 0 &&
775 put_exlist(ep->ex_defdir, xdrsp, NULL, &putdef))
776 goto errout;
777 ep = ep->ex_next;
778 }
779 (void)sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
780 if (!xdr_bool(xdrsp, &false))
781 return (0);
782 return (1);
783 errout:
784 (void)sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL);
785 return (0);
786 }
787
788 /*
789 * Called from xdr_explist() to traverse the tree and export the
790 * directory paths. Assumes SIGHUP has already been masked.
791 */
792 int
793 put_exlist(dp, xdrsp, adp, putdefp)
794 struct dirlist *dp;
795 XDR *xdrsp;
796 struct dirlist *adp;
797 int *putdefp;
798 {
799 struct grouplist *grp;
800 struct hostlist *hp;
801 int true = 1;
802 int false = 0;
803 int gotalldir = 0;
804 char *strp;
805
806 if (dp) {
807 if (put_exlist(dp->dp_left, xdrsp, adp, putdefp))
808 return (1);
809 if (!xdr_bool(xdrsp, &true))
810 return (1);
811 strp = dp->dp_dirp;
812 if (!xdr_string(xdrsp, &strp, RPCMNT_PATHLEN))
813 return (1);
814 if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) {
815 gotalldir = 1;
816 *putdefp = 1;
817 }
818 if ((dp->dp_flag & DP_DEFSET) == 0 &&
819 (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) {
820 hp = dp->dp_hosts;
821 while (hp) {
822 grp = hp->ht_grp;
823 if (grp->gr_type == GT_HOST) {
824 if (!xdr_bool(xdrsp, &true))
825 return (1);
826 strp =
827 grp->gr_ptr.gt_addrinfo->ai_canonname;
828 if (!xdr_string(xdrsp, &strp,
829 RPCMNT_NAMELEN))
830 return (1);
831 } else if (grp->gr_type == GT_NET) {
832 if (!xdr_bool(xdrsp, &true))
833 return (1);
834 strp = grp->gr_ptr.gt_net.nt_name;
835 if (!xdr_string(xdrsp, &strp,
836 RPCMNT_NAMELEN))
837 return (1);
838 }
839 hp = hp->ht_next;
840 if (gotalldir && hp == NULL) {
841 hp = adp->dp_hosts;
842 gotalldir = 0;
843 }
844 }
845 }
846 if (!xdr_bool(xdrsp, &false))
847 return (1);
848 if (put_exlist(dp->dp_right, xdrsp, adp, putdefp))
849 return (1);
850 }
851 return (0);
852 }
853
854 static int
855 parse_host_netgroup(line, lineno, ep, tgrp, cp, has_host, grp)
856 const char *line;
857 size_t lineno;
858 struct exportlist *ep;
859 struct grouplist *tgrp;
860 char *cp;
861 int *has_host;
862 struct grouplist **grp;
863 {
864 const char *hst, *usr, *dom;
865 int netgrp;
866
867 if (ep == NULL) {
868 syslog(LOG_ERR, "\"%s\", line %ld: No current export",
869 line, (unsigned long)lineno);
870 return 0;
871 }
872 setnetgrent(cp);
873 netgrp = getnetgrent(&hst, &usr, &dom);
874 do {
875 if (*has_host) {
876 (*grp)->gr_next = get_grp();
877 *grp = (*grp)->gr_next;
878 }
879 if (netgrp) {
880 if (hst == NULL) {
881 syslog(LOG_ERR,
882 "\"%s\", line %ld: No host in netgroup %s",
883 line, (unsigned long)lineno, cp);
884 goto bad;
885 }
886 if (get_host(line, lineno, hst, *grp))
887 goto bad;
888 } else if (get_host(line, lineno, cp, *grp))
889 goto bad;
890 *has_host = TRUE;
891 } while (netgrp && getnetgrent(&hst, &usr, &dom));
892
893 endnetgrent();
894 return 1;
895 bad:
896 endnetgrent();
897 return 0;
898
899 }
900
901 static int
902 parse_directory(line, lineno, tgrp, got_nondir, cp, ep, fsp)
903 const char *line;
904 size_t lineno;
905 struct grouplist *tgrp;
906 int got_nondir;
907 char *cp;
908 struct exportlist **ep;
909 struct statvfs *fsp;
910 {
911 if (!check_dirpath(line, lineno, cp))
912 return 0;
913
914 if (statvfs(cp, fsp) == -1) {
915 syslog(LOG_ERR, "\"%s\", line %ld: statvfs for `%s' failed: %m",
916 line, (unsigned long)lineno, cp);
917 return 0;
918 }
919
920 if (got_nondir) {
921 syslog(LOG_ERR,
922 "\"%s\", line %ld: Directories must precede files",
923 line, (unsigned long)lineno);
924 return 0;
925 }
926 if (*ep) {
927 if ((*ep)->ex_fs.__fsid_val[0] != fsp->f_fsidx.__fsid_val[0] ||
928 (*ep)->ex_fs.__fsid_val[1] != fsp->f_fsidx.__fsid_val[1]) {
929 syslog(LOG_ERR,
930 "\"%s\", line %ld: filesystem ids disagree",
931 line, (unsigned long)lineno);
932 return 0;
933 }
934 } else {
935 /*
936 * See if this directory is already
937 * in the list.
938 */
939 *ep = ex_search(&fsp->f_fsidx);
940 if (*ep == NULL) {
941 *ep = get_exp();
942 (*ep)->ex_fs = fsp->f_fsidx;
943 (*ep)->ex_fsdir = estrdup(fsp->f_mntonname);
944 if (debug)
945 (void)fprintf(stderr,
946 "Making new ep fs=0x%x,0x%x\n",
947 fsp->f_fsidx.__fsid_val[0], fsp->f_fsidx.__fsid_val[1]);
948 } else {
949 if (debug)
950 (void)fprintf(stderr,
951 "Found ep fs=0x%x,0x%x\n",
952 fsp->f_fsidx.__fsid_val[0], fsp->f_fsidx.__fsid_val[1]);
953 }
954 }
955
956 return 1;
957 }
958
959
960 /*
961 * Get the export list
962 */
963 /* ARGSUSED */
964 void
965 get_exportlist(n)
966 int n;
967 {
968 struct exportlist *ep, *ep2;
969 struct grouplist *grp, *tgrp;
970 struct exportlist **epp;
971 struct dirlist *dirhead;
972 struct statvfs fsb, *fsp;
973 struct addrinfo *ai;
974 struct uucred anon;
975 char *cp, *endcp, *dirp, savedc;
976 int has_host, exflags, got_nondir, dirplen, num, i;
977 FILE *exp_file;
978 char *line;
979 size_t lineno = 0, len;
980
981
982 /*
983 * First, get rid of the old list
984 */
985 ep = exphead;
986 while (ep) {
987 ep2 = ep;
988 ep = ep->ex_next;
989 free_exp(ep2);
990 }
991 exphead = NULL;
992
993 dirp = NULL;
994 dirplen = 0;
995 grp = grphead;
996 while (grp) {
997 tgrp = grp;
998 grp = grp->gr_next;
999 free_grp(tgrp);
1000 }
1001 grphead = NULL;
1002
1003 /*
1004 * And delete exports that are in the kernel for all local
1005 * file systems.
1006 */
1007 num = getmntinfo(&fsp, MNT_NOWAIT);
1008 for (i = 0; i < num; i++) {
1009 struct mountd_exports_list mel;
1010
1011 /* Delete all entries from the export list. */
1012 mel.mel_path = fsp->f_mntonname;
1013 mel.mel_nexports = 0;
1014 if (nfssvc(NFSSVC_SETEXPORTSLIST, &mel) == -1 &&
1015 errno != EOPNOTSUPP)
1016 syslog(LOG_ERR, "Can't delete exports for %s (%m)",
1017 fsp->f_mntonname);
1018
1019 fsp++;
1020 }
1021
1022 /*
1023 * Read in the exports file and build the list, calling
1024 * mount() as we go along to push the export rules into the kernel.
1025 */
1026 if ((exp_file = fopen(exname, "r")) == NULL) {
1027 /*
1028 * Don't exit here; we can still reload the config
1029 * after a SIGHUP.
1030 */
1031 if (debug)
1032 (void)fprintf(stderr, "Can't open %s: %s\n", exname,
1033 strerror(errno));
1034 return;
1035 }
1036 dirhead = NULL;
1037 while ((line = fparseln(exp_file, &len, &lineno, NULL, 0)) != NULL) {
1038 if (debug)
1039 (void)fprintf(stderr, "Got line %s\n", line);
1040 cp = line;
1041 nextfield(&cp, &endcp);
1042 if (cp == endcp)
1043 goto nextline; /* skip empty line */
1044 /*
1045 * Set defaults.
1046 */
1047 has_host = FALSE;
1048 anon = def_anon;
1049 exflags = MNT_EXPORTED;
1050 got_nondir = 0;
1051 opt_flags = 0;
1052 ep = NULL;
1053
1054 if (noprivports) {
1055 opt_flags |= OP_NORESMNT | OP_NORESPORT;
1056 exflags |= MNT_EXNORESPORT;
1057 }
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_nfssvc(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.__fsid_val[0] == fsid->__fsid_val[0] &&
1273 ep->ex_fs.__fsid_val[1] == fsid->__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 through 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 nfssvc syscall to push the export info into the kernel.
1991 */
1992 static int
1993 do_nfssvc(line, lineno, ep, grp, exflags, anoncrp, dirp, dirplen, fsb)
1994 const char *line;
1995 size_t lineno;
1996 struct exportlist *ep;
1997 struct grouplist *grp;
1998 int exflags;
1999 struct uucred *anoncrp;
2000 char *dirp;
2001 int dirplen;
2002 struct statvfs *fsb;
2003 {
2004 struct sockaddr *addrp;
2005 struct sockaddr_storage ss;
2006 struct addrinfo *ai;
2007 int addrlen;
2008 char *cp = NULL;
2009 int done;
2010 char savedc = '\0';
2011 struct export_args export;
2012
2013 export.ex_flags = exflags;
2014 export.ex_anon = *anoncrp;
2015 export.ex_indexfile = ep->ex_indexfile;
2016 if (grp->gr_type == GT_HOST) {
2017 ai = grp->gr_ptr.gt_addrinfo;
2018 addrp = ai->ai_addr;
2019 addrlen = ai->ai_addrlen;
2020 } else {
2021 addrp = NULL;
2022 ai = NULL; /* XXXGCC -Wuninitialized */
2023 addrlen = 0; /* XXXGCC -Wuninitialized */
2024 }
2025 done = FALSE;
2026 while (!done) {
2027 switch (grp->gr_type) {
2028 case GT_HOST:
2029 if (addrp != NULL && addrp->sa_family == AF_INET6 &&
2030 have_v6 == 0)
2031 goto skip;
2032 export.ex_addr = addrp;
2033 export.ex_addrlen = addrlen;
2034 export.ex_masklen = 0;
2035 break;
2036 case GT_NET:
2037 export.ex_addr = (struct sockaddr *)
2038 &grp->gr_ptr.gt_net.nt_net;
2039 if (export.ex_addr->sa_family == AF_INET6 &&
2040 have_v6 == 0)
2041 goto skip;
2042 export.ex_addrlen = export.ex_addr->sa_len;
2043 memset(&ss, 0, sizeof ss);
2044 ss.ss_family = export.ex_addr->sa_family;
2045 ss.ss_len = export.ex_addr->sa_len;
2046 if (allones(&ss, grp->gr_ptr.gt_net.nt_len) != 0) {
2047 syslog(LOG_ERR,
2048 "\"%s\", line %ld: Bad network flag",
2049 line, (unsigned long)lineno);
2050 if (cp)
2051 *cp = savedc;
2052 return (1);
2053 }
2054 export.ex_mask = (struct sockaddr *)&ss;
2055 export.ex_masklen = ss.ss_len;
2056 break;
2057 #ifdef ISO
2058 case GT_ISO:
2059 export.ex_addr =
2060 (struct sockaddr *) grp->gr_ptr.gt_isoaddr;
2061 export.ex_addrlen =
2062 sizeof(struct sockaddr_iso);
2063 export.ex_masklen = 0;
2064 break;
2065 #endif /* ISO */
2066 default:
2067 syslog(LOG_ERR, "\"%s\", line %ld: Bad netgroup type",
2068 line, (unsigned long)lineno);
2069 if (cp)
2070 *cp = savedc;
2071 return (1);
2072 };
2073
2074 /*
2075 * XXX:
2076 * Maybe I should just use the fsb->f_mntonname path instead
2077 * of looping back up the dirp to the mount point??
2078 */
2079 for (;;) {
2080 struct mountd_exports_list mel;
2081
2082 mel.mel_path = dirp;
2083 mel.mel_nexports = 1;
2084 mel.mel_exports = &export;
2085
2086 if (nfssvc(NFSSVC_SETEXPORTSLIST, &mel) != -1)
2087 break;
2088
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: %m",
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: %m",
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 (void)memset(&sin, 0, sizeof(sin));
2163 if ((opt_flags & OP_MASKLEN) && !maskflg) {
2164 p = strchr(cp, '/');
2165 *p = '\0';
2166 prefp = p + 1;
2167 } else {
2168 p = NULL; /* XXXGCC -Wuninitialized */
2169 prefp = NULL; /* XXXGCC -Wuninitialized */
2170 }
2171
2172 if ((np = getnetbyname(cp)) != NULL) {
2173 sin.sin_family = AF_INET;
2174 sin.sin_len = sizeof sin;
2175 sin.sin_addr = inet_makeaddr(np->n_net, 0);
2176 sa = (struct sockaddr *)&sin;
2177 } else if (isdigit((unsigned char)*cp)) {
2178 memset(&hints, 0, sizeof hints);
2179 hints.ai_family = AF_UNSPEC;
2180 hints.ai_flags = AI_NUMERICHOST;
2181 if (getaddrinfo(cp, NULL, &hints, &ai) != 0) {
2182 /*
2183 * If getaddrinfo() failed, try the inet4 network
2184 * notation with less than 3 dots.
2185 */
2186 sin.sin_family = AF_INET;
2187 sin.sin_len = sizeof sin;
2188 sin.sin_addr = inet_makeaddr(inet_network(cp),0);
2189 if (debug)
2190 fprintf(stderr, "get_net: v4 addr %x\n",
2191 sin.sin_addr.s_addr);
2192 sa = (struct sockaddr *)&sin;
2193 } else
2194 sa = ai->ai_addr;
2195 } else if (isxdigit((unsigned char)*cp) || *cp == ':') {
2196 memset(&hints, 0, sizeof hints);
2197 hints.ai_family = AF_UNSPEC;
2198 hints.ai_flags = AI_NUMERICHOST;
2199 if (getaddrinfo(cp, NULL, &hints, &ai) == 0)
2200 sa = ai->ai_addr;
2201 else
2202 goto fail;
2203 } else
2204 goto fail;
2205
2206 /*
2207 * Only allow /pref notation for v6 addresses.
2208 */
2209 if (sa->sa_family == AF_INET6 && (!(opt_flags & OP_MASKLEN) || maskflg))
2210 return 1;
2211
2212 ecode = getnameinfo(sa, sa->sa_len, netname, sizeof netname,
2213 NULL, 0, ninumeric);
2214 if (ecode != 0)
2215 goto fail;
2216
2217 if (maskflg)
2218 net->nt_len = countones(sa);
2219 else {
2220 if (opt_flags & OP_MASKLEN) {
2221 errno = 0;
2222 preflen = strtol(prefp, NULL, 10);
2223 if (preflen == LONG_MIN && errno == ERANGE)
2224 goto fail;
2225 net->nt_len = (int)preflen;
2226 *p = '/';
2227 }
2228
2229 if (np)
2230 name = np->n_name;
2231 else {
2232 if (getnameinfo(sa, sa->sa_len, netname, sizeof netname,
2233 NULL, 0, ninumeric) != 0)
2234 strlcpy(netname, "?", sizeof(netname));
2235 name = netname;
2236 }
2237 net->nt_name = estrdup(name);
2238 memcpy(&net->nt_net, sa, sa->sa_len);
2239 }
2240
2241 if (!maskflg && sa->sa_family == AF_INET &&
2242 !(opt_flags & (OP_MASK|OP_MASKLEN))) {
2243 sinp = (struct sockaddr_in *)sa;
2244 if (IN_CLASSA(sinp->sin_addr.s_addr))
2245 net->nt_len = 8;
2246 else if (IN_CLASSB(sinp->sin_addr.s_addr))
2247 net->nt_len = 16;
2248 else if (IN_CLASSC(sinp->sin_addr.s_addr))
2249 net->nt_len = 24;
2250 else if (IN_CLASSD(sinp->sin_addr.s_addr))
2251 net->nt_len = 28;
2252 else
2253 net->nt_len = 32; /* XXX */
2254 }
2255
2256 if (ai)
2257 freeaddrinfo(ai);
2258 return 0;
2259
2260 fail:
2261 if (ai)
2262 freeaddrinfo(ai);
2263 return 1;
2264 }
2265
2266 /*
2267 * Parse out the next white space separated field
2268 */
2269 static void
2270 nextfield(cp, endcp)
2271 char **cp;
2272 char **endcp;
2273 {
2274 char *p;
2275
2276 p = *cp;
2277 while (*p == ' ' || *p == '\t')
2278 p++;
2279 if (*p == '\n' || *p == '\0')
2280 *cp = *endcp = p;
2281 else {
2282 *cp = p++;
2283 while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
2284 p++;
2285 *endcp = p;
2286 }
2287 }
2288
2289 /*
2290 * Parse a description of a credential.
2291 */
2292 static void
2293 parsecred(namelist, cr)
2294 char *namelist;
2295 struct uucred *cr;
2296 {
2297 char *name;
2298 int cnt;
2299 char *names;
2300 struct passwd *pw;
2301 struct group *gr;
2302 int ngroups, groups[NGROUPS + 1];
2303
2304 /*
2305 * Set up the unprivileged user.
2306 */
2307 *cr = def_anon;
2308 /*
2309 * Get the user's password table entry.
2310 */
2311 names = strsep(&namelist, " \t\n");
2312 name = strsep(&names, ":");
2313 if (isdigit((unsigned char)*name) || *name == '-')
2314 pw = getpwuid(atoi(name));
2315 else
2316 pw = getpwnam(name);
2317 /*
2318 * Credentials specified as those of a user.
2319 */
2320 if (names == NULL) {
2321 if (pw == NULL) {
2322 syslog(LOG_ERR, "Unknown user: %s", name);
2323 return;
2324 }
2325 cr->cr_uid = pw->pw_uid;
2326 ngroups = NGROUPS + 1;
2327 if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups))
2328 syslog(LOG_ERR, "Too many groups");
2329 /*
2330 * Convert from int's to gid_t's and compress out duplicate
2331 */
2332 cr->cr_ngroups = ngroups - 1;
2333 cr->cr_gid = groups[0];
2334 for (cnt = 1; cnt < ngroups; cnt++)
2335 cr->cr_groups[cnt - 1] = groups[cnt];
2336 return;
2337 }
2338 /*
2339 * Explicit credential specified as a colon separated list:
2340 * uid:gid:gid:...
2341 */
2342 if (pw != NULL)
2343 cr->cr_uid = pw->pw_uid;
2344 else if (isdigit((unsigned char)*name) || *name == '-')
2345 cr->cr_uid = atoi(name);
2346 else {
2347 syslog(LOG_ERR, "Unknown user: %s", name);
2348 return;
2349 }
2350 cr->cr_ngroups = 0;
2351 while (names != NULL && *names != '\0' && cr->cr_ngroups < NGROUPS) {
2352 name = strsep(&names, ":");
2353 if (isdigit((unsigned char)*name) || *name == '-') {
2354 cr->cr_groups[cr->cr_ngroups++] = atoi(name);
2355 } else {
2356 if ((gr = getgrnam(name)) == NULL) {
2357 syslog(LOG_ERR, "Unknown group: %s", name);
2358 continue;
2359 }
2360 cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
2361 }
2362 }
2363 if (names != NULL && *names != '\0' && cr->cr_ngroups == NGROUPS)
2364 syslog(LOG_ERR, "Too many groups");
2365 }
2366
2367 #define STRSIZ (RPCMNT_NAMELEN+RPCMNT_PATHLEN+50)
2368 /*
2369 * Routines that maintain the remote mounttab
2370 */
2371 static void
2372 get_mountlist()
2373 {
2374 struct mountlist *mlp, **mlpp;
2375 char *host, *dirp, *cp;
2376 char str[STRSIZ];
2377 FILE *mlfile;
2378
2379 if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
2380 syslog(LOG_ERR, "Can't open %s: %m", _PATH_RMOUNTLIST);
2381 return;
2382 }
2383 mlpp = &mlhead;
2384 while (fgets(str, STRSIZ, mlfile) != NULL) {
2385 cp = str;
2386 host = strsep(&cp, " \t\n");
2387 dirp = strsep(&cp, " \t\n");
2388 if (host == NULL || dirp == NULL)
2389 continue;
2390 mlp = emalloc(sizeof(*mlp));
2391 (void)strncpy(mlp->ml_host, host, RPCMNT_NAMELEN);
2392 mlp->ml_host[RPCMNT_NAMELEN] = '\0';
2393 (void)strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
2394 mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
2395 mlp->ml_next = NULL;
2396 *mlpp = mlp;
2397 mlpp = &mlp->ml_next;
2398 }
2399 (void)fclose(mlfile);
2400 }
2401
2402 static int
2403 del_mlist(hostp, dirp, saddr)
2404 char *hostp, *dirp;
2405 struct sockaddr *saddr;
2406 {
2407 struct mountlist *mlp, **mlpp;
2408 struct mountlist *mlp2;
2409 u_short sport;
2410 FILE *mlfile;
2411 int fnd = 0, ret = 0;
2412 char host[NI_MAXHOST];
2413
2414 switch (saddr->sa_family) {
2415 case AF_INET6:
2416 sport = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port);
2417 break;
2418 case AF_INET:
2419 sport = ntohs(((struct sockaddr_in *)saddr)->sin_port);
2420 break;
2421 default:
2422 return -1;
2423 }
2424 mlpp = &mlhead;
2425 mlp = mlhead;
2426 while (mlp) {
2427 if (!strcmp(mlp->ml_host, hostp) &&
2428 (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
2429 if (!(mlp->ml_flag & DP_NORESMNT) &&
2430 sport >= IPPORT_RESERVED) {
2431 if (getnameinfo(saddr, saddr->sa_len, host,
2432 sizeof host, NULL, 0, ninumeric) != 0)
2433 strlcpy(host, "?", sizeof(host));
2434 syslog(LOG_NOTICE,
2435 "Umount request for %s:%s from %s refused\n",
2436 mlp->ml_host, mlp->ml_dirp, host);
2437 ret = -1;
2438 goto cont;
2439 }
2440 fnd = 1;
2441 mlp2 = mlp;
2442 *mlpp = mlp = mlp->ml_next;
2443 free(mlp2);
2444 } else {
2445 cont:
2446 mlpp = &mlp->ml_next;
2447 mlp = mlp->ml_next;
2448 }
2449 }
2450 if (fnd) {
2451 if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
2452 syslog(LOG_ERR, "Can't update %s: %m",
2453 _PATH_RMOUNTLIST);
2454 return ret;
2455 }
2456 mlp = mlhead;
2457 while (mlp) {
2458 (void)fprintf(mlfile, "%s %s\n", mlp->ml_host,
2459 mlp->ml_dirp);
2460 mlp = mlp->ml_next;
2461 }
2462 (void)fclose(mlfile);
2463 }
2464 return ret;
2465 }
2466
2467 static void
2468 add_mlist(hostp, dirp, flags)
2469 char *hostp, *dirp;
2470 int flags;
2471 {
2472 struct mountlist *mlp, **mlpp;
2473 FILE *mlfile;
2474
2475 mlpp = &mlhead;
2476 mlp = mlhead;
2477 while (mlp) {
2478 if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
2479 return;
2480 mlpp = &mlp->ml_next;
2481 mlp = mlp->ml_next;
2482 }
2483 mlp = emalloc(sizeof(*mlp));
2484 strncpy(mlp->ml_host, hostp, RPCMNT_NAMELEN);
2485 mlp->ml_host[RPCMNT_NAMELEN] = '\0';
2486 strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
2487 mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
2488 mlp->ml_flag = flags;
2489 mlp->ml_next = NULL;
2490 *mlpp = mlp;
2491 if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
2492 syslog(LOG_ERR, "Can't update %s: %m", _PATH_RMOUNTLIST);
2493 return;
2494 }
2495 (void)fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
2496 (void)fclose(mlfile);
2497 }
2498
2499 /*
2500 * This function is called via. SIGTERM when the system is going down.
2501 * It sends a broadcast RPCMNT_UMNTALL.
2502 */
2503 /* ARGSUSED */
2504 static void
2505 send_umntall(n)
2506 int n;
2507 {
2508 (void)clnt_broadcast(RPCPROG_MNT, RPCMNT_VER1, RPCMNT_UMNTALL,
2509 xdr_void, NULL, xdr_void, NULL, (resultproc_t)umntall_each);
2510 exit(0);
2511 }
2512
2513 static int
2514 umntall_each(resultsp, raddr)
2515 caddr_t resultsp;
2516 struct sockaddr_in *raddr;
2517 {
2518 return (1);
2519 }
2520
2521 /*
2522 * Free up a group list.
2523 */
2524 static void
2525 free_grp(grp)
2526 struct grouplist *grp;
2527 {
2528
2529 if (grp->gr_type == GT_HOST) {
2530 if (grp->gr_ptr.gt_addrinfo != NULL)
2531 freeaddrinfo(grp->gr_ptr.gt_addrinfo);
2532 } else if (grp->gr_type == GT_NET) {
2533 if (grp->gr_ptr.gt_net.nt_name)
2534 free(grp->gr_ptr.gt_net.nt_name);
2535 }
2536 #ifdef ISO
2537 else if (grp->gr_type == GT_ISO)
2538 free(grp->gr_ptr.gt_isoaddr);
2539 #endif
2540 free(grp);
2541 }
2542
2543 #if 0
2544 static void
2545 SYSLOG(int pri, const char *fmt,...)
2546 {
2547 va_list ap;
2548
2549 va_start(ap, fmt);
2550
2551 if (debug)
2552 vfprintf(stderr, fmt, ap);
2553 else
2554 vsyslog(pri, fmt, ap);
2555
2556 va_end(ap);
2557 }
2558 #endif
2559
2560 /*
2561 * Check options for consistency.
2562 */
2563 static int
2564 check_options(line, lineno, dp)
2565 const char *line;
2566 size_t lineno;
2567 struct dirlist *dp;
2568 {
2569
2570 if (dp == NULL) {
2571 syslog(LOG_ERR,
2572 "\"%s\", line %ld: missing directory list",
2573 line, (unsigned long)lineno);
2574 return (1);
2575 }
2576 if ((opt_flags & (OP_MAPROOT|OP_MAPALL)) == (OP_MAPROOT|OP_MAPALL) ||
2577 (opt_flags & (OP_MAPROOT|OP_KERB)) == (OP_MAPROOT|OP_KERB) ||
2578 (opt_flags & (OP_MAPALL|OP_KERB)) == (OP_MAPALL|OP_KERB)) {
2579 syslog(LOG_ERR,
2580 "\"%s\", line %ld: -mapall, -maproot and -kerb mutually exclusive",
2581 line, (unsigned long)lineno);
2582 return (1);
2583 }
2584 if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
2585 syslog(LOG_ERR, "\"%s\", line %ld: -mask requires -net",
2586 line, (unsigned long)lineno);
2587 return (1);
2588 }
2589 if ((opt_flags & OP_MASK) && (opt_flags & OP_MASKLEN) != 0) {
2590 syslog(LOG_ERR, "\"%s\", line %ld: /pref and -mask mutually"
2591 " exclusive",
2592 line, (unsigned long)lineno);
2593 return (1);
2594 }
2595 if ((opt_flags & (OP_NET|OP_ISO)) == (OP_NET|OP_ISO)) {
2596 syslog(LOG_ERR,
2597 "\"%s\", line %ld: -net and -iso mutually exclusive",
2598 line, (unsigned long)lineno);
2599 return (1);
2600 }
2601 if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
2602 syslog(LOG_ERR,
2603 "\"%s\", line %ld: -alldir has multiple directories",
2604 line, (unsigned long)lineno);
2605 return (1);
2606 }
2607 return (0);
2608 }
2609
2610 /*
2611 * Check an absolute directory path for any symbolic links. Return true
2612 * if no symbolic links are found.
2613 */
2614 static int
2615 check_dirpath(line, lineno, dirp)
2616 const char *line;
2617 size_t lineno;
2618 char *dirp;
2619 {
2620 char *cp;
2621 struct stat sb;
2622 char *file = "";
2623
2624 for (cp = dirp + 1; *cp; cp++) {
2625 if (*cp == '/') {
2626 *cp = '\0';
2627 if (lstat(dirp, &sb) == -1)
2628 goto bad;
2629 if (!S_ISDIR(sb.st_mode))
2630 goto bad1;
2631 *cp = '/';
2632 }
2633 }
2634
2635 cp = NULL;
2636 if (lstat(dirp, &sb) == -1)
2637 goto bad;
2638
2639 if (!S_ISDIR(sb.st_mode) && !S_ISREG(sb.st_mode)) {
2640 file = " file or a";
2641 goto bad1;
2642 }
2643
2644 return 1;
2645
2646 bad:
2647 syslog(LOG_ERR,
2648 "\"%s\", line %ld: lstat for `%s' failed: %m",
2649 line, (unsigned long)lineno, dirp);
2650 if (cp)
2651 *cp = '/';
2652 return 0;
2653
2654 bad1:
2655 syslog(LOG_ERR,
2656 "\"%s\", line %ld: `%s' is not a%s directory",
2657 line, (unsigned long)lineno, dirp, file);
2658 if (cp)
2659 *cp = '/';
2660 return 0;
2661 }
2662
2663 static void
2664 bind_resv_port(int sock, sa_family_t family, in_port_t port)
2665 {
2666 struct sockaddr *sa;
2667 struct sockaddr_in sasin;
2668 struct sockaddr_in6 sasin6;
2669
2670 switch (family) {
2671 case AF_INET:
2672 (void)memset(&sasin, 0, sizeof(sasin));
2673 sasin.sin_len = sizeof(sasin);
2674 sasin.sin_family = family;
2675 sasin.sin_port = htons(port);
2676 sa = (struct sockaddr *)(void *)&sasin;
2677 break;
2678 case AF_INET6:
2679 (void)memset(&sasin6, 0, sizeof(sasin6));
2680 sasin6.sin6_len = sizeof(sasin6);
2681 sasin6.sin6_family = family;
2682 sasin6.sin6_port = htons(port);
2683 sa = (struct sockaddr *)(void *)&sasin6;
2684 break;
2685 default:
2686 syslog(LOG_ERR, "Unsupported address family %d", family);
2687 return;
2688 }
2689 if (bindresvport_sa(sock, sa) == -1)
2690 syslog(LOG_ERR, "Cannot bind to reserved port %d (%m)", port);
2691 }
2692