mountd.c revision 1.104 1 /* $NetBSD: mountd.c,v 1.104 2006/05/14 01:26:34 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.104 2006/05/14 01:26:34 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
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, *cpopt, savedc, savedc2;
1687 char *endcp = NULL; /* XXX: GCC */
1688 int allflag, usedarg;
1689
1690 cpopt = *cpp;
1691 cpopt++;
1692 cp = *endcpp;
1693 savedc = *cp;
1694 *cp = '\0';
1695 while (cpopt && *cpopt) {
1696 allflag = 1;
1697 usedarg = -2;
1698 savedc2 = '\0';
1699 if ((cpoptend = strchr(cpopt, ',')) != NULL) {
1700 *cpoptend++ = '\0';
1701 if ((cpoptarg = strchr(cpopt, '=')) != NULL)
1702 *cpoptarg++ = '\0';
1703 } else {
1704 if ((cpoptarg = strchr(cpopt, '=')) != NULL)
1705 *cpoptarg++ = '\0';
1706 else {
1707 *cp = savedc;
1708 nextfield(&cp, &endcp);
1709 **endcpp = '\0';
1710 if (endcp > cp && *cp != '-') {
1711 cpoptarg = cp;
1712 savedc2 = *endcp;
1713 *endcp = '\0';
1714 usedarg = 0;
1715 }
1716 }
1717 }
1718 if (!strcmp(cpopt, "ro") || !strcmp(cpopt, "o")) {
1719 *exflagsp |= MNT_EXRDONLY;
1720 } else if (cpoptarg && (!strcmp(cpopt, "maproot") ||
1721 !(allflag = strcmp(cpopt, "mapall")) ||
1722 !strcmp(cpopt, "root") || !strcmp(cpopt, "r"))) {
1723 usedarg++;
1724 parsecred(cpoptarg, cr);
1725 if (allflag == 0) {
1726 *exflagsp |= MNT_EXPORTANON;
1727 opt_flags |= OP_MAPALL;
1728 } else
1729 opt_flags |= OP_MAPROOT;
1730 } else if (!strcmp(cpopt, "kerb") || !strcmp(cpopt, "k")) {
1731 *exflagsp |= MNT_EXKERB;
1732 opt_flags |= OP_KERB;
1733 } else if (cpoptarg && (!strcmp(cpopt, "mask") ||
1734 !strcmp(cpopt, "m"))) {
1735 if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 1)) {
1736 syslog(LOG_ERR,
1737 "\"%s\", line %ld: Bad mask: %s",
1738 line, (unsigned long)lineno, cpoptarg);
1739 return (1);
1740 }
1741 usedarg++;
1742 opt_flags |= OP_MASK;
1743 } else if (cpoptarg && (!strcmp(cpopt, "network") ||
1744 !strcmp(cpopt, "n"))) {
1745 if (strchr(cpoptarg, '/') != NULL) {
1746 if (debug)
1747 fprintf(stderr, "setting OP_MASKLEN\n");
1748 opt_flags |= OP_MASKLEN;
1749 }
1750 if (grp->gr_type != GT_NULL) {
1751 syslog(LOG_ERR,
1752 "\"%s\", line %ld: Network/host conflict",
1753 line, (unsigned long)lineno);
1754 return (1);
1755 } else if (get_net(cpoptarg, &grp->gr_ptr.gt_net, 0)) {
1756 syslog(LOG_ERR,
1757 "\"%s\", line %ld: Bad net: %s",
1758 line, (unsigned long)lineno, cpoptarg);
1759 return (1);
1760 }
1761 grp->gr_type = GT_NET;
1762 *has_hostp = 1;
1763 usedarg++;
1764 opt_flags |= OP_NET;
1765 } else if (!strcmp(cpopt, "alldirs")) {
1766 opt_flags |= OP_ALLDIRS;
1767 } else if (!strcmp(cpopt, "noresvmnt")) {
1768 opt_flags |= OP_NORESMNT;
1769 } else if (!strcmp(cpopt, "noresvport")) {
1770 opt_flags |= OP_NORESPORT;
1771 *exflagsp |= MNT_EXNORESPORT;
1772 } else if (!strcmp(cpopt, "public")) {
1773 *exflagsp |= (MNT_EXNORESPORT | MNT_EXPUBLIC);
1774 opt_flags |= OP_NORESPORT;
1775 } else if (!strcmp(cpopt, "webnfs")) {
1776 *exflagsp |= (MNT_EXNORESPORT | MNT_EXPUBLIC |
1777 MNT_EXRDONLY | MNT_EXPORTANON);
1778 opt_flags |= (OP_MAPALL | OP_NORESPORT);
1779 } else if (cpoptarg && !strcmp(cpopt, "index")) {
1780 ep->ex_indexfile = strdup(cpoptarg);
1781 #ifdef ISO
1782 } else if (cpoptarg && !strcmp(cpopt, "iso")) {
1783 if (get_isoaddr(line, lineno, cpoptarg, grp))
1784 return (1);
1785 *has_hostp = 1;
1786 usedarg++;
1787 opt_flags |= OP_ISO;
1788 #endif /* ISO */
1789 } else {
1790 syslog(LOG_ERR,
1791 "\"%s\", line %ld: Bad opt %s",
1792 line, (unsigned long)lineno, cpopt);
1793 return (1);
1794 }
1795 if (usedarg >= 0) {
1796 *endcp = savedc2;
1797 **endcpp = savedc;
1798 if (usedarg > 0) {
1799 *cpp = cp;
1800 *endcpp = endcp;
1801 }
1802 return (0);
1803 }
1804 cpopt = cpoptend;
1805 }
1806 **endcpp = savedc;
1807 return (0);
1808 }
1809
1810 /*
1811 * Translate a character string to the corresponding list of network
1812 * addresses for a hostname.
1813 */
1814 static int
1815 get_host(line, lineno, cp, grp)
1816 const char *line;
1817 size_t lineno;
1818 const char *cp;
1819 struct grouplist *grp;
1820 {
1821 struct addrinfo *ai, hints;
1822 int ecode;
1823 char host[NI_MAXHOST];
1824
1825 if (grp->gr_type != GT_NULL) {
1826 syslog(LOG_ERR,
1827 "\"%s\", line %ld: Bad netgroup type for ip host %s",
1828 line, (unsigned long)lineno, cp);
1829 return (1);
1830 }
1831 memset(&hints, 0, sizeof hints);
1832 hints.ai_flags = AI_CANONNAME;
1833 hints.ai_protocol = IPPROTO_UDP;
1834 ecode = getaddrinfo(cp, NULL, &hints, &ai);
1835 if (ecode != 0) {
1836 syslog(LOG_ERR, "\"%s\", line %ld: can't get address info for "
1837 "host %s",
1838 line, (long)lineno, cp);
1839 return 1;
1840 }
1841 grp->gr_type = GT_HOST;
1842 grp->gr_ptr.gt_addrinfo = ai;
1843 while (ai != NULL) {
1844 if (ai->ai_canonname == NULL) {
1845 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host,
1846 sizeof host, NULL, 0, ninumeric) != 0)
1847 strlcpy(host, "?", sizeof(host));
1848 ai->ai_canonname = estrdup(host);
1849 ai->ai_flags |= AI_CANONNAME;
1850 } else
1851 ai->ai_flags &= ~AI_CANONNAME;
1852 if (debug)
1853 (void)fprintf(stderr, "got host %s\n", ai->ai_canonname);
1854 ai = ai->ai_next;
1855 }
1856 return (0);
1857 }
1858
1859 /*
1860 * Free up an exports list component
1861 */
1862 static void
1863 free_exp(ep)
1864 struct exportlist *ep;
1865 {
1866
1867 if (ep->ex_defdir) {
1868 free_host(ep->ex_defdir->dp_hosts);
1869 free(ep->ex_defdir);
1870 }
1871 if (ep->ex_fsdir)
1872 free(ep->ex_fsdir);
1873 if (ep->ex_indexfile)
1874 free(ep->ex_indexfile);
1875 free_dir(ep->ex_dirl);
1876 free(ep);
1877 }
1878
1879 /*
1880 * Free hosts.
1881 */
1882 static void
1883 free_host(hp)
1884 struct hostlist *hp;
1885 {
1886 struct hostlist *hp2;
1887
1888 while (hp) {
1889 hp2 = hp;
1890 hp = hp->ht_next;
1891 free(hp2);
1892 }
1893 }
1894
1895 static struct hostlist *
1896 get_ht()
1897 {
1898 struct hostlist *hp;
1899
1900 hp = emalloc(sizeof(struct hostlist));
1901 hp->ht_next = NULL;
1902 hp->ht_flag = 0;
1903 return (hp);
1904 }
1905
1906 #ifdef ISO
1907 /*
1908 * Translate an iso address.
1909 */
1910 static int
1911 get_isoaddr(line, lineno, cp, grp)
1912 const char *line;
1913 size_t lineno;
1914 char *cp;
1915 struct grouplist *grp;
1916 {
1917 struct iso_addr *isop;
1918 struct sockaddr_iso *isoaddr;
1919
1920 if (grp->gr_type != GT_NULL) {
1921 syslog(LOG_ERR,
1922 "\"%s\", line %ld: Bad netgroup type for iso addr %s",
1923 line, (unsigned long)lineno, cp);
1924 return (1);
1925 }
1926 if ((isop = iso_addr(cp)) == NULL) {
1927 syslog(LOG_ERR,
1928 "\"%s\", line %ld: Bad iso addr %s",
1929 line, (unsigned long)lineno, cp);
1930 return (1);
1931 }
1932 isoaddr = emalloc(sizeof(struct sockaddr_iso));
1933 (void)memset(isoaddr, 0, sizeof(struct sockaddr_iso));
1934 (void)memcpy(&isoaddr->siso_addr, isop, sizeof(struct iso_addr));
1935 isoaddr->siso_len = sizeof(struct sockaddr_iso);
1936 isoaddr->siso_family = AF_ISO;
1937 grp->gr_type = GT_ISO;
1938 grp->gr_ptr.gt_isoaddr = isoaddr;
1939 return (0);
1940 }
1941 #endif /* ISO */
1942
1943 /*
1944 * error checked malloc and strdup
1945 */
1946 static void *
1947 emalloc(n)
1948 size_t n;
1949 {
1950 void *ptr = malloc(n);
1951
1952 if (ptr == NULL) {
1953 syslog(LOG_ERR, "%m");
1954 exit(2);
1955 }
1956 return ptr;
1957 }
1958
1959 static char *
1960 estrdup(s)
1961 const char *s;
1962 {
1963 char *n = strdup(s);
1964
1965 if (n == NULL) {
1966 syslog(LOG_ERR, "%m");
1967 exit(2);
1968 }
1969 return n;
1970 }
1971
1972 /*
1973 * Do the nfssvc syscall to push the export info into the kernel.
1974 */
1975 static int
1976 do_nfssvc(line, lineno, ep, grp, exflags, anoncrp, dirp, dirplen, fsb)
1977 const char *line;
1978 size_t lineno;
1979 struct exportlist *ep;
1980 struct grouplist *grp;
1981 int exflags;
1982 struct uucred *anoncrp;
1983 char *dirp;
1984 int dirplen;
1985 struct statvfs *fsb;
1986 {
1987 struct sockaddr *addrp;
1988 struct sockaddr_storage ss;
1989 struct addrinfo *ai;
1990 int addrlen;
1991 char *cp = NULL;
1992 int done;
1993 char savedc = '\0';
1994 struct export_args export;
1995
1996 export.ex_flags = exflags;
1997 export.ex_anon = *anoncrp;
1998 export.ex_indexfile = ep->ex_indexfile;
1999 if (grp->gr_type == GT_HOST) {
2000 ai = grp->gr_ptr.gt_addrinfo;
2001 addrp = ai->ai_addr;
2002 addrlen = ai->ai_addrlen;
2003 } else {
2004 addrp = NULL;
2005 ai = NULL; /* XXXGCC -Wuninitialized */
2006 addrlen = 0; /* XXXGCC -Wuninitialized */
2007 }
2008 done = FALSE;
2009 while (!done) {
2010 struct mountd_exports_list mel;
2011
2012 switch (grp->gr_type) {
2013 case GT_HOST:
2014 if (addrp != NULL && addrp->sa_family == AF_INET6 &&
2015 have_v6 == 0)
2016 goto skip;
2017 export.ex_addr = addrp;
2018 export.ex_addrlen = addrlen;
2019 export.ex_masklen = 0;
2020 break;
2021 case GT_NET:
2022 export.ex_addr = (struct sockaddr *)
2023 &grp->gr_ptr.gt_net.nt_net;
2024 if (export.ex_addr->sa_family == AF_INET6 &&
2025 have_v6 == 0)
2026 goto skip;
2027 export.ex_addrlen = export.ex_addr->sa_len;
2028 memset(&ss, 0, sizeof ss);
2029 ss.ss_family = export.ex_addr->sa_family;
2030 ss.ss_len = export.ex_addr->sa_len;
2031 if (allones(&ss, grp->gr_ptr.gt_net.nt_len) != 0) {
2032 syslog(LOG_ERR,
2033 "\"%s\", line %ld: Bad network flag",
2034 line, (unsigned long)lineno);
2035 if (cp)
2036 *cp = savedc;
2037 return (1);
2038 }
2039 export.ex_mask = (struct sockaddr *)&ss;
2040 export.ex_masklen = ss.ss_len;
2041 break;
2042 #ifdef ISO
2043 case GT_ISO:
2044 export.ex_addr =
2045 (struct sockaddr *) grp->gr_ptr.gt_isoaddr;
2046 export.ex_addrlen =
2047 sizeof(struct sockaddr_iso);
2048 export.ex_masklen = 0;
2049 break;
2050 #endif /* ISO */
2051 default:
2052 syslog(LOG_ERR, "\"%s\", line %ld: Bad netgroup type",
2053 line, (unsigned long)lineno);
2054 if (cp)
2055 *cp = savedc;
2056 return (1);
2057 };
2058
2059 /*
2060 * XXX:
2061 * Maybe I should just use the fsb->f_mntonname path?
2062 */
2063
2064 mel.mel_path = dirp;
2065 mel.mel_nexports = 1;
2066 mel.mel_exports = &export;
2067
2068 if (nfssvc(NFSSVC_SETEXPORTSLIST, &mel) != 0) {
2069 syslog(LOG_ERR,
2070 "\"%s\", line %ld: Can't change attributes for %s to %s: %m",
2071 line, (unsigned long)lineno,
2072 dirp, (grp->gr_type == GT_HOST) ?
2073 grp->gr_ptr.gt_addrinfo->ai_canonname :
2074 (grp->gr_type == GT_NET) ?
2075 grp->gr_ptr.gt_net.nt_name :
2076 "Unknown");
2077 return (1);
2078 }
2079 skip:
2080 if (addrp) {
2081 ai = ai->ai_next;
2082 if (ai == NULL)
2083 done = TRUE;
2084 else {
2085 addrp = ai->ai_addr;
2086 addrlen = ai->ai_addrlen;
2087 }
2088 } else
2089 done = TRUE;
2090 }
2091 if (cp)
2092 *cp = savedc;
2093 return (0);
2094 }
2095
2096 /*
2097 * Translate a net address.
2098 */
2099 static int
2100 get_net(cp, net, maskflg)
2101 char *cp;
2102 struct netmsk *net;
2103 int maskflg;
2104 {
2105 struct netent *np;
2106 char *name, *p, *prefp;
2107 struct sockaddr_in sin, *sinp;
2108 struct sockaddr *sa;
2109 struct addrinfo hints, *ai = NULL;
2110 char netname[NI_MAXHOST];
2111 long preflen;
2112 int ecode;
2113
2114 (void)memset(&sin, 0, sizeof(sin));
2115 if ((opt_flags & OP_MASKLEN) && !maskflg) {
2116 p = strchr(cp, '/');
2117 *p = '\0';
2118 prefp = p + 1;
2119 } else {
2120 p = NULL; /* XXXGCC -Wuninitialized */
2121 prefp = NULL; /* XXXGCC -Wuninitialized */
2122 }
2123
2124 if ((np = getnetbyname(cp)) != NULL) {
2125 sin.sin_family = AF_INET;
2126 sin.sin_len = sizeof sin;
2127 sin.sin_addr = inet_makeaddr(np->n_net, 0);
2128 sa = (struct sockaddr *)&sin;
2129 } else if (isdigit((unsigned char)*cp)) {
2130 memset(&hints, 0, sizeof hints);
2131 hints.ai_family = AF_UNSPEC;
2132 hints.ai_flags = AI_NUMERICHOST;
2133 if (getaddrinfo(cp, NULL, &hints, &ai) != 0) {
2134 /*
2135 * If getaddrinfo() failed, try the inet4 network
2136 * notation with less than 3 dots.
2137 */
2138 sin.sin_family = AF_INET;
2139 sin.sin_len = sizeof sin;
2140 sin.sin_addr = inet_makeaddr(inet_network(cp),0);
2141 if (debug)
2142 fprintf(stderr, "get_net: v4 addr %x\n",
2143 sin.sin_addr.s_addr);
2144 sa = (struct sockaddr *)&sin;
2145 } else
2146 sa = ai->ai_addr;
2147 } else if (isxdigit((unsigned char)*cp) || *cp == ':') {
2148 memset(&hints, 0, sizeof hints);
2149 hints.ai_family = AF_UNSPEC;
2150 hints.ai_flags = AI_NUMERICHOST;
2151 if (getaddrinfo(cp, NULL, &hints, &ai) == 0)
2152 sa = ai->ai_addr;
2153 else
2154 goto fail;
2155 } else
2156 goto fail;
2157
2158 /*
2159 * Only allow /pref notation for v6 addresses.
2160 */
2161 if (sa->sa_family == AF_INET6 && (!(opt_flags & OP_MASKLEN) || maskflg))
2162 return 1;
2163
2164 ecode = getnameinfo(sa, sa->sa_len, netname, sizeof netname,
2165 NULL, 0, ninumeric);
2166 if (ecode != 0)
2167 goto fail;
2168
2169 if (maskflg)
2170 net->nt_len = countones(sa);
2171 else {
2172 if (opt_flags & OP_MASKLEN) {
2173 errno = 0;
2174 preflen = strtol(prefp, NULL, 10);
2175 if (preflen == LONG_MIN && errno == ERANGE)
2176 goto fail;
2177 net->nt_len = (int)preflen;
2178 *p = '/';
2179 }
2180
2181 if (np)
2182 name = np->n_name;
2183 else {
2184 if (getnameinfo(sa, sa->sa_len, netname, sizeof netname,
2185 NULL, 0, ninumeric) != 0)
2186 strlcpy(netname, "?", sizeof(netname));
2187 name = netname;
2188 }
2189 net->nt_name = estrdup(name);
2190 memcpy(&net->nt_net, sa, sa->sa_len);
2191 }
2192
2193 if (!maskflg && sa->sa_family == AF_INET &&
2194 !(opt_flags & (OP_MASK|OP_MASKLEN))) {
2195 sinp = (struct sockaddr_in *)sa;
2196 if (IN_CLASSA(sinp->sin_addr.s_addr))
2197 net->nt_len = 8;
2198 else if (IN_CLASSB(sinp->sin_addr.s_addr))
2199 net->nt_len = 16;
2200 else if (IN_CLASSC(sinp->sin_addr.s_addr))
2201 net->nt_len = 24;
2202 else if (IN_CLASSD(sinp->sin_addr.s_addr))
2203 net->nt_len = 28;
2204 else
2205 net->nt_len = 32; /* XXX */
2206 }
2207
2208 if (ai)
2209 freeaddrinfo(ai);
2210 return 0;
2211
2212 fail:
2213 if (ai)
2214 freeaddrinfo(ai);
2215 return 1;
2216 }
2217
2218 /*
2219 * Parse out the next white space separated field
2220 */
2221 static void
2222 nextfield(cp, endcp)
2223 char **cp;
2224 char **endcp;
2225 {
2226 char *p;
2227
2228 p = *cp;
2229 while (*p == ' ' || *p == '\t')
2230 p++;
2231 if (*p == '\n' || *p == '\0')
2232 *cp = *endcp = p;
2233 else {
2234 *cp = p++;
2235 while (*p != ' ' && *p != '\t' && *p != '\n' && *p != '\0')
2236 p++;
2237 *endcp = p;
2238 }
2239 }
2240
2241 /*
2242 * Parse a description of a credential.
2243 */
2244 static void
2245 parsecred(namelist, cr)
2246 char *namelist;
2247 struct uucred *cr;
2248 {
2249 char *name;
2250 int cnt;
2251 char *names;
2252 struct passwd *pw;
2253 struct group *gr;
2254 int ngroups;
2255 gid_t groups[NGROUPS + 1];
2256
2257 /*
2258 * Set up the unprivileged user.
2259 */
2260 *cr = def_anon;
2261 /*
2262 * Get the user's password table entry.
2263 */
2264 names = strsep(&namelist, " \t\n");
2265 name = strsep(&names, ":");
2266 if (isdigit((unsigned char)*name) || *name == '-')
2267 pw = getpwuid(atoi(name));
2268 else
2269 pw = getpwnam(name);
2270 /*
2271 * Credentials specified as those of a user.
2272 */
2273 if (names == NULL) {
2274 if (pw == NULL) {
2275 syslog(LOG_ERR, "Unknown user: %s", name);
2276 return;
2277 }
2278 cr->cr_uid = pw->pw_uid;
2279 ngroups = NGROUPS + 1;
2280 if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups))
2281 syslog(LOG_ERR, "Too many groups");
2282 /*
2283 * Convert from int's to gid_t's and compress out duplicate
2284 */
2285 cr->cr_ngroups = ngroups - 1;
2286 cr->cr_gid = groups[0];
2287 for (cnt = 1; cnt < ngroups; cnt++)
2288 cr->cr_groups[cnt - 1] = groups[cnt];
2289 return;
2290 }
2291 /*
2292 * Explicit credential specified as a colon separated list:
2293 * uid:gid:gid:...
2294 */
2295 if (pw != NULL)
2296 cr->cr_uid = pw->pw_uid;
2297 else if (isdigit((unsigned char)*name) || *name == '-')
2298 cr->cr_uid = atoi(name);
2299 else {
2300 syslog(LOG_ERR, "Unknown user: %s", name);
2301 return;
2302 }
2303 cr->cr_ngroups = 0;
2304 while (names != NULL && *names != '\0' && cr->cr_ngroups < NGROUPS) {
2305 name = strsep(&names, ":");
2306 if (isdigit((unsigned char)*name) || *name == '-') {
2307 cr->cr_groups[cr->cr_ngroups++] = atoi(name);
2308 } else {
2309 if ((gr = getgrnam(name)) == NULL) {
2310 syslog(LOG_ERR, "Unknown group: %s", name);
2311 continue;
2312 }
2313 cr->cr_groups[cr->cr_ngroups++] = gr->gr_gid;
2314 }
2315 }
2316 if (names != NULL && *names != '\0' && cr->cr_ngroups == NGROUPS)
2317 syslog(LOG_ERR, "Too many groups");
2318 }
2319
2320 #define STRSIZ (RPCMNT_NAMELEN+RPCMNT_PATHLEN+50)
2321 /*
2322 * Routines that maintain the remote mounttab
2323 */
2324 static void
2325 get_mountlist()
2326 {
2327 struct mountlist *mlp, **mlpp;
2328 char *host, *dirp, *cp;
2329 char str[STRSIZ];
2330 FILE *mlfile;
2331
2332 if ((mlfile = fopen(_PATH_RMOUNTLIST, "r")) == NULL) {
2333 syslog(LOG_ERR, "Can't open %s: %m", _PATH_RMOUNTLIST);
2334 return;
2335 }
2336 mlpp = &mlhead;
2337 while (fgets(str, STRSIZ, mlfile) != NULL) {
2338 cp = str;
2339 host = strsep(&cp, " \t\n");
2340 dirp = strsep(&cp, " \t\n");
2341 if (host == NULL || dirp == NULL)
2342 continue;
2343 mlp = emalloc(sizeof(*mlp));
2344 (void)strncpy(mlp->ml_host, host, RPCMNT_NAMELEN);
2345 mlp->ml_host[RPCMNT_NAMELEN] = '\0';
2346 (void)strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
2347 mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
2348 mlp->ml_next = NULL;
2349 *mlpp = mlp;
2350 mlpp = &mlp->ml_next;
2351 }
2352 (void)fclose(mlfile);
2353 }
2354
2355 static int
2356 del_mlist(hostp, dirp, saddr)
2357 char *hostp, *dirp;
2358 struct sockaddr *saddr;
2359 {
2360 struct mountlist *mlp, **mlpp;
2361 struct mountlist *mlp2;
2362 u_short sport;
2363 FILE *mlfile;
2364 int fnd = 0, ret = 0;
2365 char host[NI_MAXHOST];
2366
2367 switch (saddr->sa_family) {
2368 case AF_INET6:
2369 sport = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port);
2370 break;
2371 case AF_INET:
2372 sport = ntohs(((struct sockaddr_in *)saddr)->sin_port);
2373 break;
2374 default:
2375 return -1;
2376 }
2377 mlpp = &mlhead;
2378 mlp = mlhead;
2379 while (mlp) {
2380 if (!strcmp(mlp->ml_host, hostp) &&
2381 (!dirp || !strcmp(mlp->ml_dirp, dirp))) {
2382 if (!(mlp->ml_flag & DP_NORESMNT) &&
2383 sport >= IPPORT_RESERVED) {
2384 if (getnameinfo(saddr, saddr->sa_len, host,
2385 sizeof host, NULL, 0, ninumeric) != 0)
2386 strlcpy(host, "?", sizeof(host));
2387 syslog(LOG_NOTICE,
2388 "Umount request for %s:%s from %s refused\n",
2389 mlp->ml_host, mlp->ml_dirp, host);
2390 ret = -1;
2391 goto cont;
2392 }
2393 fnd = 1;
2394 mlp2 = mlp;
2395 *mlpp = mlp = mlp->ml_next;
2396 free(mlp2);
2397 } else {
2398 cont:
2399 mlpp = &mlp->ml_next;
2400 mlp = mlp->ml_next;
2401 }
2402 }
2403 if (fnd) {
2404 if ((mlfile = fopen(_PATH_RMOUNTLIST, "w")) == NULL) {
2405 syslog(LOG_ERR, "Can't update %s: %m",
2406 _PATH_RMOUNTLIST);
2407 return ret;
2408 }
2409 mlp = mlhead;
2410 while (mlp) {
2411 (void)fprintf(mlfile, "%s %s\n", mlp->ml_host,
2412 mlp->ml_dirp);
2413 mlp = mlp->ml_next;
2414 }
2415 (void)fclose(mlfile);
2416 }
2417 return ret;
2418 }
2419
2420 static void
2421 add_mlist(hostp, dirp, flags)
2422 char *hostp, *dirp;
2423 int flags;
2424 {
2425 struct mountlist *mlp, **mlpp;
2426 FILE *mlfile;
2427
2428 mlpp = &mlhead;
2429 mlp = mlhead;
2430 while (mlp) {
2431 if (!strcmp(mlp->ml_host, hostp) && !strcmp(mlp->ml_dirp, dirp))
2432 return;
2433 mlpp = &mlp->ml_next;
2434 mlp = mlp->ml_next;
2435 }
2436 mlp = emalloc(sizeof(*mlp));
2437 strncpy(mlp->ml_host, hostp, RPCMNT_NAMELEN);
2438 mlp->ml_host[RPCMNT_NAMELEN] = '\0';
2439 strncpy(mlp->ml_dirp, dirp, RPCMNT_PATHLEN);
2440 mlp->ml_dirp[RPCMNT_PATHLEN] = '\0';
2441 mlp->ml_flag = flags;
2442 mlp->ml_next = NULL;
2443 *mlpp = mlp;
2444 if ((mlfile = fopen(_PATH_RMOUNTLIST, "a")) == NULL) {
2445 syslog(LOG_ERR, "Can't update %s: %m", _PATH_RMOUNTLIST);
2446 return;
2447 }
2448 (void)fprintf(mlfile, "%s %s\n", mlp->ml_host, mlp->ml_dirp);
2449 (void)fclose(mlfile);
2450 }
2451
2452 /*
2453 * This function is called via. SIGTERM when the system is going down.
2454 * It sends a broadcast RPCMNT_UMNTALL.
2455 */
2456 /* ARGSUSED */
2457 static void
2458 send_umntall(n)
2459 int n;
2460 {
2461 (void)clnt_broadcast(RPCPROG_MNT, RPCMNT_VER1, RPCMNT_UMNTALL,
2462 xdr_void, NULL, xdr_void, NULL, (resultproc_t)umntall_each);
2463 exit(0);
2464 }
2465
2466 static int
2467 umntall_each(resultsp, raddr)
2468 caddr_t resultsp;
2469 struct sockaddr_in *raddr;
2470 {
2471 return (1);
2472 }
2473
2474 /*
2475 * Free up a group list.
2476 */
2477 static void
2478 free_grp(grp)
2479 struct grouplist *grp;
2480 {
2481
2482 if (grp->gr_type == GT_HOST) {
2483 if (grp->gr_ptr.gt_addrinfo != NULL)
2484 freeaddrinfo(grp->gr_ptr.gt_addrinfo);
2485 } else if (grp->gr_type == GT_NET) {
2486 if (grp->gr_ptr.gt_net.nt_name)
2487 free(grp->gr_ptr.gt_net.nt_name);
2488 }
2489 #ifdef ISO
2490 else if (grp->gr_type == GT_ISO)
2491 free(grp->gr_ptr.gt_isoaddr);
2492 #endif
2493 free(grp);
2494 }
2495
2496 #if 0
2497 static void
2498 SYSLOG(int pri, const char *fmt,...)
2499 {
2500 va_list ap;
2501
2502 va_start(ap, fmt);
2503
2504 if (debug)
2505 vfprintf(stderr, fmt, ap);
2506 else
2507 vsyslog(pri, fmt, ap);
2508
2509 va_end(ap);
2510 }
2511 #endif
2512
2513 /*
2514 * Check options for consistency.
2515 */
2516 static int
2517 check_options(line, lineno, dp)
2518 const char *line;
2519 size_t lineno;
2520 struct dirlist *dp;
2521 {
2522
2523 if (dp == NULL) {
2524 syslog(LOG_ERR,
2525 "\"%s\", line %ld: missing directory list",
2526 line, (unsigned long)lineno);
2527 return (1);
2528 }
2529 if ((opt_flags & (OP_MAPROOT|OP_MAPALL)) == (OP_MAPROOT|OP_MAPALL) ||
2530 (opt_flags & (OP_MAPROOT|OP_KERB)) == (OP_MAPROOT|OP_KERB) ||
2531 (opt_flags & (OP_MAPALL|OP_KERB)) == (OP_MAPALL|OP_KERB)) {
2532 syslog(LOG_ERR,
2533 "\"%s\", line %ld: -mapall, -maproot and -kerb mutually exclusive",
2534 line, (unsigned long)lineno);
2535 return (1);
2536 }
2537 if ((opt_flags & OP_MASK) && (opt_flags & OP_NET) == 0) {
2538 syslog(LOG_ERR, "\"%s\", line %ld: -mask requires -net",
2539 line, (unsigned long)lineno);
2540 return (1);
2541 }
2542 if ((opt_flags & OP_MASK) && (opt_flags & OP_MASKLEN) != 0) {
2543 syslog(LOG_ERR, "\"%s\", line %ld: /pref and -mask mutually"
2544 " exclusive",
2545 line, (unsigned long)lineno);
2546 return (1);
2547 }
2548 if ((opt_flags & (OP_NET|OP_ISO)) == (OP_NET|OP_ISO)) {
2549 syslog(LOG_ERR,
2550 "\"%s\", line %ld: -net and -iso mutually exclusive",
2551 line, (unsigned long)lineno);
2552 return (1);
2553 }
2554 if ((opt_flags & OP_ALLDIRS) && dp->dp_left) {
2555 syslog(LOG_ERR,
2556 "\"%s\", line %ld: -alldir has multiple directories",
2557 line, (unsigned long)lineno);
2558 return (1);
2559 }
2560 return (0);
2561 }
2562
2563 /*
2564 * Check an absolute directory path for any symbolic links. Return true
2565 * if no symbolic links are found.
2566 */
2567 static int
2568 check_dirpath(line, lineno, dirp)
2569 const char *line;
2570 size_t lineno;
2571 char *dirp;
2572 {
2573 char *cp;
2574 struct stat sb;
2575 char *file = "";
2576
2577 for (cp = dirp + 1; *cp; cp++) {
2578 if (*cp == '/') {
2579 *cp = '\0';
2580 if (lstat(dirp, &sb) == -1)
2581 goto bad;
2582 if (!S_ISDIR(sb.st_mode))
2583 goto bad1;
2584 *cp = '/';
2585 }
2586 }
2587
2588 cp = NULL;
2589 if (lstat(dirp, &sb) == -1)
2590 goto bad;
2591
2592 if (!S_ISDIR(sb.st_mode) && !S_ISREG(sb.st_mode)) {
2593 file = " file or a";
2594 goto bad1;
2595 }
2596
2597 return 1;
2598
2599 bad:
2600 syslog(LOG_ERR,
2601 "\"%s\", line %ld: lstat for `%s' failed: %m",
2602 line, (unsigned long)lineno, dirp);
2603 if (cp)
2604 *cp = '/';
2605 return 0;
2606
2607 bad1:
2608 syslog(LOG_ERR,
2609 "\"%s\", line %ld: `%s' is not a%s directory",
2610 line, (unsigned long)lineno, dirp, file);
2611 if (cp)
2612 *cp = '/';
2613 return 0;
2614 }
2615
2616 static void
2617 bind_resv_port(int sock, sa_family_t family, in_port_t port)
2618 {
2619 struct sockaddr *sa;
2620 struct sockaddr_in sasin;
2621 struct sockaddr_in6 sasin6;
2622
2623 switch (family) {
2624 case AF_INET:
2625 (void)memset(&sasin, 0, sizeof(sasin));
2626 sasin.sin_len = sizeof(sasin);
2627 sasin.sin_family = family;
2628 sasin.sin_port = htons(port);
2629 sa = (struct sockaddr *)(void *)&sasin;
2630 break;
2631 case AF_INET6:
2632 (void)memset(&sasin6, 0, sizeof(sasin6));
2633 sasin6.sin6_len = sizeof(sasin6);
2634 sasin6.sin6_family = family;
2635 sasin6.sin6_port = htons(port);
2636 sa = (struct sockaddr *)(void *)&sasin6;
2637 break;
2638 default:
2639 syslog(LOG_ERR, "Unsupported address family %d", family);
2640 return;
2641 }
2642 if (bindresvport_sa(sock, sa) == -1)
2643 syslog(LOG_ERR, "Cannot bind to reserved port %d (%m)", port);
2644 }
2645