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