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