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