Home | History | Annotate | Line # | Download | only in mount_nfs
mount_nfs.c revision 1.45
      1  1.45       agc /*	$NetBSD: mount_nfs.c,v 1.45 2003/08/07 10:04:29 agc Exp $	*/
      2   1.8       cgd 
      3   1.1   mycroft /*
      4   1.1   mycroft  * Copyright (c) 1992, 1993, 1994
      5   1.1   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1   mycroft  *
      7   1.1   mycroft  * This code is derived from software contributed to Berkeley by
      8   1.1   mycroft  * Rick Macklem at The University of Guelph.
      9   1.1   mycroft  *
     10   1.1   mycroft  * Redistribution and use in source and binary forms, with or without
     11   1.1   mycroft  * modification, are permitted provided that the following conditions
     12   1.1   mycroft  * are met:
     13   1.1   mycroft  * 1. Redistributions of source code must retain the above copyright
     14   1.1   mycroft  *    notice, this list of conditions and the following disclaimer.
     15   1.1   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1   mycroft  *    notice, this list of conditions and the following disclaimer in the
     17   1.1   mycroft  *    documentation and/or other materials provided with the distribution.
     18  1.45       agc  * 3. Neither the name of the University nor the names of its contributors
     19   1.1   mycroft  *    may be used to endorse or promote products derived from this software
     20   1.1   mycroft  *    without specific prior written permission.
     21   1.1   mycroft  *
     22   1.1   mycroft  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23   1.1   mycroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   1.1   mycroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.1   mycroft  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26   1.1   mycroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.1   mycroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28   1.1   mycroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.1   mycroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30   1.1   mycroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.1   mycroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.1   mycroft  * SUCH DAMAGE.
     33   1.1   mycroft  */
     34   1.1   mycroft 
     35  1.17     lukem #include <sys/cdefs.h>
     36   1.1   mycroft #ifndef lint
     37  1.17     lukem __COPYRIGHT("@(#) Copyright (c) 1992, 1993, 1994\n\
     38  1.17     lukem 	The Regents of the University of California.  All rights reserved.\n");
     39   1.1   mycroft #endif /* not lint */
     40   1.1   mycroft 
     41   1.1   mycroft #ifndef lint
     42   1.8       cgd #if 0
     43  1.11      fvdl static char sccsid[] = "@(#)mount_nfs.c	8.11 (Berkeley) 5/4/95";
     44   1.8       cgd #else
     45  1.45       agc __RCSID("$NetBSD: mount_nfs.c,v 1.45 2003/08/07 10:04:29 agc Exp $");
     46   1.8       cgd #endif
     47   1.1   mycroft #endif /* not lint */
     48   1.1   mycroft 
     49   1.1   mycroft #include <sys/param.h>
     50   1.1   mycroft #include <sys/mount.h>
     51   1.1   mycroft #include <sys/socket.h>
     52   1.1   mycroft #include <sys/stat.h>
     53   1.9       jtc #include <syslog.h>
     54   1.1   mycroft 
     55   1.1   mycroft #include <rpc/rpc.h>
     56   1.1   mycroft #include <rpc/pmap_clnt.h>
     57   1.1   mycroft #include <rpc/pmap_prot.h>
     58   1.1   mycroft 
     59   1.1   mycroft #ifdef ISO
     60   1.1   mycroft #include <netiso/iso.h>
     61   1.1   mycroft #endif
     62   1.1   mycroft 
     63  1.11      fvdl #ifdef NFSKERB
     64  1.44    itojun #include <des.h>
     65   1.1   mycroft #include <kerberosIV/krb.h>
     66   1.1   mycroft #endif
     67   1.1   mycroft 
     68   1.1   mycroft #include <nfs/rpcv2.h>
     69  1.11      fvdl #include <nfs/nfsproto.h>
     70   1.1   mycroft #include <nfs/nfs.h>
     71   1.1   mycroft #include <nfs/nqnfs.h>
     72  1.20      fvdl #include <nfs/nfsmount.h>
     73   1.1   mycroft 
     74   1.1   mycroft #include <arpa/inet.h>
     75   1.1   mycroft 
     76   1.1   mycroft #include <ctype.h>
     77   1.1   mycroft #include <err.h>
     78   1.1   mycroft #include <errno.h>
     79   1.1   mycroft #include <fcntl.h>
     80   1.1   mycroft #include <netdb.h>
     81   1.1   mycroft #include <signal.h>
     82   1.1   mycroft #include <stdio.h>
     83   1.1   mycroft #include <stdlib.h>
     84  1.19     perry #include <string.h>
     85   1.1   mycroft #include <unistd.h>
     86  1.34  christos #include <util.h>
     87   1.1   mycroft 
     88  1.37  jdolecek #include <mntopts.h>
     89   1.1   mycroft 
     90  1.40  christos #define	ALTF_BG		0x00000001
     91  1.40  christos #define ALTF_CONN	0x00000002
     92  1.40  christos #define ALTF_DUMBTIMR	0x00000004
     93  1.40  christos #define ALTF_INTR	0x00000008
     94  1.40  christos #define ALTF_KERB	0x00000010
     95  1.40  christos #define ALTF_NFSV3	0x00000020
     96  1.40  christos #define ALTF_RDIRPLUS	0x00000040
     97  1.40  christos #define	ALTF_MNTUDP	0x00000080
     98  1.40  christos #define ALTF_NORESPORT	0x00000100
     99  1.40  christos #define ALTF_SEQPACKET	0x00000200
    100  1.40  christos #define ALTF_NQNFS	0x00000400
    101  1.40  christos #define ALTF_SOFT	0x00000800
    102  1.40  christos #define ALTF_TCP	0x00001000
    103  1.40  christos #define ALTF_NFSV2	0x00002000
    104  1.40  christos #define ALTF_PORT	0x00004000
    105  1.40  christos #define ALTF_RSIZE	0x00008000
    106  1.40  christos #define ALTF_WSIZE	0x00010000
    107  1.40  christos #define ALTF_RDIRSIZE	0x00020000
    108  1.40  christos #define ALTF_MAXGRPS	0x00040000
    109  1.40  christos #define ALTF_LEASETERM	0x00080000
    110  1.40  christos #define ALTF_READAHEAD	0x00100000
    111  1.40  christos #define ALTF_DEADTHRESH	0x00200000
    112  1.40  christos #define ALTF_TIMEO	0x00400000
    113  1.40  christos #define ALTF_RETRANS	0x00800000
    114  1.11      fvdl 
    115  1.29  jdolecek static const struct mntopt mopts[] = {
    116   1.1   mycroft 	MOPT_STDOPTS,
    117   1.1   mycroft 	MOPT_FORCE,
    118   1.1   mycroft 	MOPT_UPDATE,
    119  1.34  christos 	MOPT_GETARGS,
    120  1.11      fvdl 	{ "bg", 0, ALTF_BG, 1 },
    121  1.14  christos 	{ "conn", 0, ALTF_CONN, 1 },
    122  1.11      fvdl 	{ "dumbtimer", 0, ALTF_DUMBTIMR, 1 },
    123  1.11      fvdl 	{ "intr", 0, ALTF_INTR, 1 },
    124  1.11      fvdl #ifdef NFSKERB
    125  1.11      fvdl 	{ "kerb", 0, ALTF_KERB, 1 },
    126  1.11      fvdl #endif
    127  1.11      fvdl 	{ "nfsv3", 0, ALTF_NFSV3, 1 },
    128  1.11      fvdl 	{ "rdirplus", 0, ALTF_RDIRPLUS, 1 },
    129  1.11      fvdl 	{ "mntudp", 0, ALTF_MNTUDP, 1 },
    130  1.15      fvdl 	{ "noresport", 0, ALTF_NORESPORT, 1 },
    131  1.11      fvdl #ifdef ISO
    132  1.11      fvdl 	{ "seqpacket", 0, ALTF_SEQPACKET, 1 },
    133  1.11      fvdl #endif
    134  1.11      fvdl 	{ "nqnfs", 0, ALTF_NQNFS, 1 },
    135  1.11      fvdl 	{ "soft", 0, ALTF_SOFT, 1 },
    136  1.11      fvdl 	{ "tcp", 0, ALTF_TCP, 1 },
    137  1.13      fvdl 	{ "nfsv2", 0, ALTF_NFSV2, 1 },
    138  1.40  christos 	{ "port", 0, ALTF_PORT, 1 },
    139  1.40  christos 	{ "rsize", 0, ALTF_RSIZE, 1 },
    140  1.40  christos 	{ "wsize", 0, ALTF_WSIZE, 1 },
    141  1.40  christos 	{ "rdirsize", 0, ALTF_RDIRSIZE, 1 },
    142  1.40  christos 	{ "maxgrps", 0, ALTF_MAXGRPS, 1 },
    143  1.40  christos 	{ "leaseterm", 0, ALTF_LEASETERM, 1 },
    144  1.40  christos 	{ "readahead", 0, ALTF_READAHEAD, 1 },
    145  1.40  christos 	{ "deadthresh", 0, ALTF_DEADTHRESH, 1 },
    146  1.40  christos 	{ "timeo", 0, ALTF_TIMEO, 1 },
    147   1.1   mycroft 	{ NULL }
    148  1.40  christos 
    149   1.1   mycroft };
    150   1.1   mycroft 
    151   1.1   mycroft struct nfs_args nfsdefargs = {
    152  1.11      fvdl 	NFS_ARGSVERSION,
    153   1.1   mycroft 	(struct sockaddr *)0,
    154   1.1   mycroft 	sizeof (struct sockaddr_in),
    155   1.1   mycroft 	SOCK_DGRAM,
    156   1.1   mycroft 	0,
    157  1.11      fvdl 	(u_char *)0,
    158   1.1   mycroft 	0,
    159  1.15      fvdl 	NFSMNT_NFSV3|NFSMNT_NOCONN|NFSMNT_RESVPORT,
    160   1.1   mycroft 	NFS_WSIZE,
    161   1.1   mycroft 	NFS_RSIZE,
    162  1.11      fvdl 	NFS_READDIRSIZE,
    163  1.11      fvdl 	10,
    164   1.1   mycroft 	NFS_RETRANS,
    165   1.1   mycroft 	NFS_MAXGRPS,
    166   1.1   mycroft 	NFS_DEFRAHEAD,
    167   1.1   mycroft 	NQ_DEFLEASE,
    168   1.1   mycroft 	NQ_DEADTHRESH,
    169   1.1   mycroft 	(char *)0,
    170   1.1   mycroft };
    171   1.1   mycroft 
    172   1.1   mycroft struct nfhret {
    173  1.11      fvdl 	u_long		stat;
    174  1.11      fvdl 	long		vers;
    175  1.11      fvdl 	long		auth;
    176  1.11      fvdl 	long		fhsize;
    177  1.11      fvdl 	u_char		nfh[NFSX_V3FHMAX];
    178   1.1   mycroft };
    179   1.1   mycroft #define	DEF_RETRY	10000
    180   1.1   mycroft #define	BGRND	1
    181   1.1   mycroft #define	ISBGRND	2
    182  1.11      fvdl int retrycnt;
    183   1.1   mycroft int opflags = 0;
    184  1.11      fvdl int nfsproto = IPPROTO_UDP;
    185  1.11      fvdl int force2 = 0;
    186  1.11      fvdl int force3 = 0;
    187  1.25      fvdl int mnttcp_ok = 1;
    188  1.40  christos int port = 0;
    189   1.1   mycroft 
    190  1.11      fvdl #ifdef NFSKERB
    191  1.29  jdolecek static char inst[INST_SZ];
    192  1.29  jdolecek static char realm[REALM_SZ];
    193  1.29  jdolecek static struct {
    194  1.11      fvdl 	u_long		kind;
    195  1.11      fvdl 	KTEXT_ST	kt;
    196  1.11      fvdl } ktick;
    197  1.29  jdolecek static struct nfsrpc_nickverf kverf;
    198  1.29  jdolecek static struct nfsrpc_fullblock kin, kout;
    199  1.29  jdolecek static NFSKERBKEY_T kivec;
    200  1.29  jdolecek static CREDENTIALS kcr;
    201  1.29  jdolecek static struct timeval ktv;
    202  1.29  jdolecek static NFSKERBKEYSCHED_T kerb_keysched;
    203   1.1   mycroft #endif
    204   1.1   mycroft 
    205  1.34  christos static void	shownfsargs __P((const struct nfs_args *));
    206  1.29  jdolecek static int	getnfsargs __P((char *, struct nfs_args *));
    207   1.1   mycroft #ifdef ISO
    208  1.29  jdolecek static struct	iso_addr *iso_addr __P((const char *));
    209   1.1   mycroft #endif
    210  1.17     lukem int	main __P((int, char *[]));
    211  1.29  jdolecek int	mount_nfs __P((int argc, char **argv));
    212  1.29  jdolecek /* void	set_rpc_maxgrouplist __P((int)); */
    213  1.29  jdolecek static void	usage __P((void));
    214  1.29  jdolecek static int	xdr_dir __P((XDR *, char *));
    215  1.29  jdolecek static int	xdr_fh __P((XDR *, struct nfhret *));
    216   1.1   mycroft 
    217  1.29  jdolecek #ifndef MOUNT_NOMAIN
    218   1.1   mycroft int
    219   1.1   mycroft main(argc, argv)
    220   1.1   mycroft 	int argc;
    221  1.29  jdolecek 	char **argv;
    222  1.29  jdolecek {
    223  1.29  jdolecek 	return mount_nfs(argc, argv);
    224  1.29  jdolecek }
    225  1.29  jdolecek #endif
    226  1.29  jdolecek 
    227  1.29  jdolecek int
    228  1.29  jdolecek mount_nfs(argc, argv)
    229  1.29  jdolecek 	int argc;
    230   1.1   mycroft 	char *argv[];
    231   1.1   mycroft {
    232  1.33  wrstuden 	int c, retval;
    233  1.17     lukem 	struct nfs_args *nfsargsp;
    234   1.1   mycroft 	struct nfs_args nfsargs;
    235   1.1   mycroft 	struct nfsd_cargs ncd;
    236  1.36     enami 	struct sockaddr_storage sa;
    237  1.11      fvdl 	int mntflags, altflags, i, nfssvc_flag, num;
    238  1.23  perseant 	char *name, *p, *spec, *ospec;
    239  1.40  christos 	mntoptparse_t mp;
    240  1.11      fvdl #ifdef NFSKERB
    241   1.1   mycroft 	uid_t last_ruid;
    242   1.1   mycroft 
    243   1.1   mycroft 	last_ruid = -1;
    244   1.3    brezak 	if (krb_get_lrealm(realm, 0) != KSUCCESS)
    245  1.43    itojun 	    (void)strlcpy(realm, KRB_REALM, sizeof(realm));
    246  1.11      fvdl 	if (sizeof (struct nfsrpc_nickverf) != RPCX_NICKVERF ||
    247  1.11      fvdl 	    sizeof (struct nfsrpc_fullblock) != RPCX_FULLBLOCK ||
    248  1.11      fvdl 	    ((char *)&ktick.kt) - ((char *)&ktick) != NFSX_UNSIGNED ||
    249  1.11      fvdl 	    ((char *)ktick.kt.dat) - ((char *)&ktick) != 2 * NFSX_UNSIGNED)
    250  1.35    itojun 		warnx("Yikes! NFSKERB structs not packed!!\n");
    251   1.1   mycroft #endif
    252   1.1   mycroft 	retrycnt = DEF_RETRY;
    253   1.1   mycroft 
    254   1.1   mycroft 	mntflags = 0;
    255  1.11      fvdl 	altflags = 0;
    256   1.1   mycroft 	nfsargs = nfsdefargs;
    257   1.1   mycroft 	nfsargsp = &nfsargs;
    258   1.1   mycroft 	while ((c = getopt(argc, argv,
    259  1.18      fvdl 	    "23a:bcCdD:g:I:iKL:lm:o:PpqR:r:sTt:w:x:UX")) != -1)
    260   1.1   mycroft 		switch (c) {
    261  1.11      fvdl 		case '3':
    262  1.11      fvdl 			if (force2)
    263  1.11      fvdl 				errx(1, "-2 and -3 are mutually exclusive");
    264  1.11      fvdl 			force3 = 1;
    265  1.11      fvdl 			break;
    266  1.11      fvdl 		case '2':
    267  1.11      fvdl 			if (force3)
    268  1.11      fvdl 				errx(1, "-2 and -3 are mutually exclusive");
    269  1.11      fvdl 			force2 = 1;
    270  1.11      fvdl 			nfsargsp->flags &= ~NFSMNT_NFSV3;
    271  1.11      fvdl 			break;
    272   1.1   mycroft 		case 'a':
    273   1.1   mycroft 			num = strtol(optarg, &p, 10);
    274   1.1   mycroft 			if (*p || num < 0)
    275   1.1   mycroft 				errx(1, "illegal -a value -- %s", optarg);
    276   1.1   mycroft 			nfsargsp->readahead = num;
    277   1.1   mycroft 			nfsargsp->flags |= NFSMNT_READAHEAD;
    278   1.1   mycroft 			break;
    279   1.1   mycroft 		case 'b':
    280   1.1   mycroft 			opflags |= BGRND;
    281   1.1   mycroft 			break;
    282   1.1   mycroft 		case 'c':
    283   1.1   mycroft 			nfsargsp->flags |= NFSMNT_NOCONN;
    284   1.1   mycroft 			break;
    285  1.14  christos 		case 'C':
    286  1.14  christos 			nfsargsp->flags &= ~NFSMNT_NOCONN;
    287  1.14  christos 			break;
    288   1.1   mycroft 		case 'D':
    289   1.1   mycroft 			num = strtol(optarg, &p, 10);
    290   1.1   mycroft 			if (*p || num <= 0)
    291   1.1   mycroft 				errx(1, "illegal -D value -- %s", optarg);
    292   1.1   mycroft 			nfsargsp->deadthresh = num;
    293   1.1   mycroft 			nfsargsp->flags |= NFSMNT_DEADTHRESH;
    294   1.1   mycroft 			break;
    295   1.1   mycroft 		case 'd':
    296   1.1   mycroft 			nfsargsp->flags |= NFSMNT_DUMBTIMR;
    297   1.1   mycroft 			break;
    298   1.1   mycroft #if 0 /* XXXX */
    299   1.1   mycroft 		case 'g':
    300   1.1   mycroft 			num = strtol(optarg, &p, 10);
    301   1.1   mycroft 			if (*p || num <= 0)
    302   1.1   mycroft 				errx(1, "illegal -g value -- %s", optarg);
    303   1.1   mycroft 			set_rpc_maxgrouplist(num);
    304   1.1   mycroft 			nfsargsp->maxgrouplist = num;
    305   1.1   mycroft 			nfsargsp->flags |= NFSMNT_MAXGRPS;
    306   1.1   mycroft 			break;
    307   1.1   mycroft #endif
    308  1.11      fvdl 		case 'I':
    309  1.11      fvdl 			num = strtol(optarg, &p, 10);
    310  1.11      fvdl 			if (*p || num <= 0)
    311  1.11      fvdl 				errx(1, "illegal -I value -- %s", optarg);
    312  1.11      fvdl 			nfsargsp->readdirsize = num;
    313  1.11      fvdl 			nfsargsp->flags |= NFSMNT_READDIRSIZE;
    314  1.11      fvdl 			break;
    315   1.1   mycroft 		case 'i':
    316   1.1   mycroft 			nfsargsp->flags |= NFSMNT_INT;
    317   1.1   mycroft 			break;
    318  1.11      fvdl #ifdef NFSKERB
    319   1.1   mycroft 		case 'K':
    320   1.1   mycroft 			nfsargsp->flags |= NFSMNT_KERB;
    321   1.1   mycroft 			break;
    322   1.1   mycroft #endif
    323   1.1   mycroft 		case 'L':
    324   1.1   mycroft 			num = strtol(optarg, &p, 10);
    325   1.1   mycroft 			if (*p || num < 2)
    326   1.1   mycroft 				errx(1, "illegal -L value -- %s", optarg);
    327   1.1   mycroft 			nfsargsp->leaseterm = num;
    328   1.1   mycroft 			nfsargsp->flags |= NFSMNT_LEASETERM;
    329   1.1   mycroft 			break;
    330   1.1   mycroft 		case 'l':
    331  1.11      fvdl 			nfsargsp->flags |= NFSMNT_RDIRPLUS;
    332   1.1   mycroft 			break;
    333  1.11      fvdl #ifdef NFSKERB
    334   1.1   mycroft 		case 'm':
    335  1.43    itojun 			(void)strlcpy(realm, optarg, sizeof(realm));
    336   1.1   mycroft 			break;
    337   1.1   mycroft #endif
    338   1.1   mycroft 		case 'o':
    339  1.40  christos 			mp = getmntopts(optarg, mopts, &mntflags, &altflags);
    340  1.40  christos 			if (mp == NULL)
    341  1.40  christos 				err(1, NULL);
    342  1.35    itojun 			if (altflags & ALTF_BG)
    343  1.11      fvdl 				opflags |= BGRND;
    344  1.35    itojun 			if (altflags & ALTF_CONN)
    345  1.14  christos 				nfsargsp->flags &= ~NFSMNT_NOCONN;
    346  1.35    itojun 			if (altflags & ALTF_DUMBTIMR)
    347  1.11      fvdl 				nfsargsp->flags |= NFSMNT_DUMBTIMR;
    348  1.35    itojun 			if (altflags & ALTF_INTR)
    349  1.11      fvdl 				nfsargsp->flags |= NFSMNT_INT;
    350  1.11      fvdl #ifdef NFSKERB
    351  1.35    itojun 			if (altflags & ALTF_KERB)
    352  1.11      fvdl 				nfsargsp->flags |= NFSMNT_KERB;
    353  1.11      fvdl #endif
    354  1.35    itojun 			if (altflags & ALTF_NFSV3) {
    355  1.13      fvdl 				if (force2)
    356  1.35    itojun 					errx(1, "conflicting version options");
    357  1.13      fvdl 				force3 = 1;
    358  1.13      fvdl 			}
    359  1.35    itojun 			if (altflags & ALTF_NFSV2) {
    360  1.13      fvdl 				if (force3)
    361  1.35    itojun 					errx(1, "conflicting version options");
    362  1.13      fvdl 				force2 = 1;
    363  1.13      fvdl 				nfsargsp->flags &= ~NFSMNT_NFSV3;
    364  1.13      fvdl 			}
    365  1.35    itojun 			if (altflags & ALTF_RDIRPLUS)
    366  1.11      fvdl 				nfsargsp->flags |= NFSMNT_RDIRPLUS;
    367  1.35    itojun 			if (altflags & ALTF_MNTUDP)
    368  1.11      fvdl 				mnttcp_ok = 0;
    369  1.35    itojun 			if (altflags & ALTF_NORESPORT)
    370  1.15      fvdl 				nfsargsp->flags &= ~NFSMNT_RESVPORT;
    371  1.11      fvdl #ifdef ISO
    372  1.35    itojun 			if (altflags & ALTF_SEQPACKET)
    373  1.11      fvdl 				nfsargsp->sotype = SOCK_SEQPACKET;
    374  1.11      fvdl #endif
    375  1.35    itojun 			if (altflags & ALTF_NQNFS) {
    376  1.13      fvdl 				if (force2)
    377  1.35    itojun 					errx(1, "nqnfs only available with v3");
    378  1.13      fvdl 				force3 = 1;
    379  1.13      fvdl 				nfsargsp->flags |= NFSMNT_NQNFS;
    380  1.13      fvdl 			}
    381  1.35    itojun 			if (altflags & ALTF_SOFT)
    382  1.11      fvdl 				nfsargsp->flags |= NFSMNT_SOFT;
    383  1.35    itojun 			if (altflags & ALTF_TCP) {
    384  1.11      fvdl 				nfsargsp->sotype = SOCK_STREAM;
    385  1.11      fvdl 				nfsproto = IPPROTO_TCP;
    386  1.11      fvdl 			}
    387  1.40  christos 			if (altflags & ALTF_PORT) {
    388  1.40  christos 				port = getmntoptnum(mp, "port");
    389  1.40  christos 			}
    390  1.40  christos 			if (altflags & ALTF_RSIZE) {
    391  1.40  christos 				nfsargsp->rsize =
    392  1.40  christos 				    (int)getmntoptnum(mp, "rsize");
    393  1.40  christos 				nfsargsp->flags |= NFSMNT_RSIZE;
    394  1.40  christos 			}
    395  1.40  christos 			if (altflags & ALTF_WSIZE) {
    396  1.42      yamt 				nfsargsp->wsize =
    397  1.40  christos 				    (int)getmntoptnum(mp, "wsize");
    398  1.40  christos 				nfsargsp->flags |= NFSMNT_WSIZE;
    399  1.40  christos 			}
    400  1.40  christos 			if (altflags & ALTF_RDIRSIZE) {
    401  1.40  christos 				nfsargsp->rsize =
    402  1.40  christos 				    (int)getmntoptnum(mp, "rdirsize");
    403  1.40  christos 				nfsargsp->flags |= NFSMNT_READDIRSIZE;
    404  1.40  christos 			}
    405  1.40  christos #if 0
    406  1.40  christos 			if (altflags & ALTF_MAXGRPS) {
    407  1.40  christos 				set_rpc_maxgrouplist(num);
    408  1.40  christos 				nfsargsp->maxgrouplist =
    409  1.40  christos 				    (int)getmntoptnum(mp, "maxgrps");
    410  1.40  christos 				nfsargsp->flags |= NFSMNT_MAXGRPS;
    411  1.40  christos 			}
    412  1.40  christos #endif
    413  1.40  christos 			if (altflags & ALTF_LEASETERM) {
    414  1.40  christos 				nfsargsp->leaseterm =
    415  1.40  christos 				(int)getmntoptnum(mp, "leaseterm");
    416  1.40  christos 				nfsargsp->flags |= NFSMNT_LEASETERM;
    417  1.40  christos 			}
    418  1.40  christos 			if (altflags & ALTF_READAHEAD) {
    419  1.40  christos 				nfsargsp->readahead =
    420  1.40  christos 				    (int)getmntoptnum(mp, "readahead");
    421  1.40  christos 				nfsargsp->flags |= NFSMNT_READAHEAD;
    422  1.40  christos 			}
    423  1.40  christos 			if (altflags & ALTF_DEADTHRESH) {
    424  1.40  christos 				nfsargsp->deadthresh =
    425  1.40  christos 				    (int)getmntoptnum(mp, "deadthresh");
    426  1.40  christos 				nfsargsp->flags |= NFSMNT_DEADTHRESH;
    427  1.40  christos 			}
    428  1.40  christos 			if (altflags & ALTF_TIMEO) {
    429  1.40  christos 				nfsargsp->timeo =
    430  1.40  christos 				    (int)getmntoptnum(mp, "timeo");
    431  1.40  christos 				nfsargsp->flags |= NFSMNT_TIMEO;
    432  1.40  christos 			}
    433  1.40  christos 			if (altflags & ALTF_RETRANS) {
    434  1.40  christos 				nfsargsp->retrans =
    435  1.40  christos 				    (int)getmntoptnum(mp, "retrans");
    436  1.40  christos 				nfsargsp->flags |= NFSMNT_RETRANS;
    437  1.40  christos 			}
    438  1.11      fvdl 			altflags = 0;
    439  1.40  christos 			freemntopts(mp);
    440   1.1   mycroft 			break;
    441   1.1   mycroft 		case 'P':
    442   1.1   mycroft 			nfsargsp->flags |= NFSMNT_RESVPORT;
    443   1.1   mycroft 			break;
    444   1.1   mycroft 		case 'p':
    445  1.15      fvdl 			nfsargsp->flags &= ~NFSMNT_RESVPORT;
    446   1.1   mycroft 			break;
    447   1.1   mycroft 		case 'q':
    448  1.13      fvdl 			if (force2)
    449  1.35    itojun 				errx(1, "nqnfs only available with v3");
    450  1.13      fvdl 			force3 = 1;
    451  1.13      fvdl 			nfsargsp->flags |= NFSMNT_NQNFS;
    452   1.1   mycroft 			break;
    453   1.1   mycroft 		case 'R':
    454   1.1   mycroft 			num = strtol(optarg, &p, 10);
    455   1.1   mycroft 			if (*p || num <= 0)
    456   1.1   mycroft 				errx(1, "illegal -R value -- %s", optarg);
    457   1.1   mycroft 			retrycnt = num;
    458   1.1   mycroft 			break;
    459   1.1   mycroft 		case 'r':
    460   1.1   mycroft 			num = strtol(optarg, &p, 10);
    461   1.1   mycroft 			if (*p || num <= 0)
    462   1.1   mycroft 				errx(1, "illegal -r value -- %s", optarg);
    463   1.1   mycroft 			nfsargsp->rsize = num;
    464   1.1   mycroft 			nfsargsp->flags |= NFSMNT_RSIZE;
    465   1.1   mycroft 			break;
    466  1.15      fvdl #ifdef ISO
    467  1.15      fvdl 		case 'S':
    468  1.15      fvdl 			nfsargsp->sotype = SOCK_SEQPACKET;
    469  1.15      fvdl 			break;
    470  1.15      fvdl #endif
    471   1.1   mycroft 		case 's':
    472   1.1   mycroft 			nfsargsp->flags |= NFSMNT_SOFT;
    473   1.1   mycroft 			break;
    474   1.1   mycroft 		case 'T':
    475   1.1   mycroft 			nfsargsp->sotype = SOCK_STREAM;
    476  1.11      fvdl 			nfsproto = IPPROTO_TCP;
    477   1.1   mycroft 			break;
    478   1.1   mycroft 		case 't':
    479   1.1   mycroft 			num = strtol(optarg, &p, 10);
    480   1.1   mycroft 			if (*p || num <= 0)
    481   1.1   mycroft 				errx(1, "illegal -t value -- %s", optarg);
    482   1.1   mycroft 			nfsargsp->timeo = num;
    483   1.1   mycroft 			nfsargsp->flags |= NFSMNT_TIMEO;
    484   1.1   mycroft 			break;
    485   1.1   mycroft 		case 'w':
    486   1.1   mycroft 			num = strtol(optarg, &p, 10);
    487   1.1   mycroft 			if (*p || num <= 0)
    488   1.1   mycroft 				errx(1, "illegal -w value -- %s", optarg);
    489   1.1   mycroft 			nfsargsp->wsize = num;
    490   1.1   mycroft 			nfsargsp->flags |= NFSMNT_WSIZE;
    491   1.1   mycroft 			break;
    492   1.1   mycroft 		case 'x':
    493   1.1   mycroft 			num = strtol(optarg, &p, 10);
    494   1.1   mycroft 			if (*p || num <= 0)
    495   1.1   mycroft 				errx(1, "illegal -x value -- %s", optarg);
    496   1.1   mycroft 			nfsargsp->retrans = num;
    497   1.1   mycroft 			nfsargsp->flags |= NFSMNT_RETRANS;
    498  1.18      fvdl 			break;
    499  1.18      fvdl 		case 'X':
    500  1.18      fvdl 			nfsargsp->flags |= NFSMNT_XLATECOOKIE;
    501   1.1   mycroft 			break;
    502  1.11      fvdl 		case 'U':
    503  1.11      fvdl 			mnttcp_ok = 0;
    504  1.11      fvdl 			break;
    505   1.1   mycroft 		default:
    506   1.1   mycroft 			usage();
    507   1.1   mycroft 			break;
    508   1.1   mycroft 		}
    509   1.1   mycroft 	argc -= optind;
    510   1.1   mycroft 	argv += optind;
    511   1.1   mycroft 
    512   1.1   mycroft 	if (argc != 2)
    513   1.7       cgd 		usage();
    514   1.1   mycroft 
    515   1.1   mycroft 	spec = *argv++;
    516   1.1   mycroft 	name = *argv;
    517  1.35    itojun 	if ((ospec = strdup(spec)) == NULL) {
    518  1.35    itojun 		err(1, "strdup");
    519  1.23  perseant 	}
    520   1.1   mycroft 
    521  1.36     enami 	if ((mntflags & MNT_GETARGS) != 0) {
    522  1.36     enami 		memset(&sa, 0, sizeof(sa));
    523  1.36     enami 		nfsargsp->addr = (struct sockaddr *)&sa;
    524  1.36     enami 		nfsargsp->addrlen = sizeof(sa);
    525  1.36     enami 	} else {
    526  1.34  christos 		if (!getnfsargs(spec, nfsargsp))
    527  1.34  christos 			exit(1);
    528  1.34  christos 	}
    529  1.33  wrstuden 	if ((retval = mount(MOUNT_NFS, name, mntflags, nfsargsp))) {
    530  1.33  wrstuden 		/* Did we just default to v3 on a v2-only kernel?
    531  1.33  wrstuden 		 * If so, default to v2 & try again */
    532  1.33  wrstuden 		if ((errno == EPROGMISMATCH) && !force3) {
    533  1.33  wrstuden 			nfsargsp->flags &= ~NFSMNT_NFSV3;
    534  1.33  wrstuden 			retval = mount(MOUNT_NFS, name, mntflags, nfsargsp);
    535  1.33  wrstuden 		}
    536  1.33  wrstuden 	}
    537  1.33  wrstuden 	if (retval)
    538  1.23  perseant 		err(1, "%s on %s", ospec, name);
    539  1.34  christos 	if (mntflags & MNT_GETARGS) {
    540  1.34  christos 		shownfsargs(nfsargsp);
    541  1.36     enami 		return (0);
    542  1.34  christos 	}
    543  1.34  christos 
    544   1.1   mycroft 	if (nfsargsp->flags & (NFSMNT_NQNFS | NFSMNT_KERB)) {
    545   1.1   mycroft 		if ((opflags & ISBGRND) == 0) {
    546  1.17     lukem 			if ((i = fork()) != 0) {
    547   1.1   mycroft 				if (i == -1)
    548   1.1   mycroft 					err(1, "nqnfs 1");
    549   1.1   mycroft 				exit(0);
    550   1.1   mycroft 			}
    551   1.1   mycroft 			(void) setsid();
    552   1.1   mycroft 			(void) close(STDIN_FILENO);
    553   1.1   mycroft 			(void) close(STDOUT_FILENO);
    554   1.1   mycroft 			(void) close(STDERR_FILENO);
    555   1.1   mycroft 			(void) chdir("/");
    556   1.1   mycroft 		}
    557  1.30     lukem 		openlog("mount_nfs", LOG_PID, LOG_DAEMON);
    558   1.1   mycroft 		nfssvc_flag = NFSSVC_MNTD;
    559   1.1   mycroft 		ncd.ncd_dirp = name;
    560   1.1   mycroft 		while (nfssvc(nfssvc_flag, (caddr_t)&ncd) < 0) {
    561   1.1   mycroft 			if (errno != ENEEDAUTH) {
    562   1.1   mycroft 				syslog(LOG_ERR, "nfssvc err %m");
    563   1.1   mycroft 				continue;
    564   1.1   mycroft 			}
    565   1.1   mycroft 			nfssvc_flag =
    566   1.1   mycroft 			    NFSSVC_MNTD | NFSSVC_GOTAUTH | NFSSVC_AUTHINFAIL;
    567  1.11      fvdl #ifdef NFSKERB
    568   1.1   mycroft 			/*
    569   1.1   mycroft 			 * Set up as ncd_authuid for the kerberos call.
    570   1.1   mycroft 			 * Must set ruid to ncd_authuid and reset the
    571   1.1   mycroft 			 * ticket name iff ncd_authuid is not the same
    572   1.1   mycroft 			 * as last time, so that the right ticket file
    573   1.1   mycroft 			 * is found.
    574  1.11      fvdl 			 * Get the Kerberos credential structure so that
    575  1.11      fvdl 			 * we have the seesion key and get a ticket for
    576  1.11      fvdl 			 * this uid.
    577  1.11      fvdl 			 * For more info see the IETF Draft "Authentication
    578  1.11      fvdl 			 * in ONC RPC".
    579   1.1   mycroft 			 */
    580   1.1   mycroft 			if (ncd.ncd_authuid != last_ruid) {
    581   1.1   mycroft 				krb_set_tkt_string("");
    582   1.1   mycroft 				last_ruid = ncd.ncd_authuid;
    583   1.1   mycroft 			}
    584   1.1   mycroft 			setreuid(ncd.ncd_authuid, 0);
    585  1.11      fvdl 			kret = krb_get_cred(NFS_KERBSRV, inst, realm, &kcr);
    586  1.11      fvdl 			if (kret == RET_NOTKT) {
    587  1.11      fvdl 		            kret = get_ad_tkt(NFS_KERBSRV, inst, realm,
    588  1.11      fvdl 				DEFAULT_TKT_LIFE);
    589  1.11      fvdl 			    if (kret == KSUCCESS)
    590  1.11      fvdl 				kret = krb_get_cred(NFS_KERBSRV, inst, realm,
    591  1.11      fvdl 				    &kcr);
    592  1.11      fvdl 			}
    593  1.11      fvdl 			if (kret == KSUCCESS)
    594  1.11      fvdl 			    kret = krb_mk_req(&ktick.kt, NFS_KERBSRV, inst,
    595  1.11      fvdl 				realm, 0);
    596  1.11      fvdl 
    597  1.11      fvdl 			/*
    598  1.11      fvdl 			 * Fill in the AKN_FULLNAME authenticator and verfier.
    599  1.11      fvdl 			 * Along with the Kerberos ticket, we need to build
    600  1.11      fvdl 			 * the timestamp verifier and encrypt it in CBC mode.
    601  1.11      fvdl 			 */
    602  1.11      fvdl 			if (kret == KSUCCESS &&
    603  1.11      fvdl 			    ktick.kt.length <= (RPCAUTH_MAXSIZ-3*NFSX_UNSIGNED)
    604  1.11      fvdl 			    && gettimeofday(&ktv, (struct timezone *)0) == 0) {
    605  1.11      fvdl 			    ncd.ncd_authtype = RPCAUTH_KERB4;
    606  1.11      fvdl 			    ncd.ncd_authstr = (u_char *)&ktick;
    607  1.11      fvdl 			    ncd.ncd_authlen = nfsm_rndup(ktick.kt.length) +
    608  1.11      fvdl 				3 * NFSX_UNSIGNED;
    609  1.11      fvdl 			    ncd.ncd_verfstr = (u_char *)&kverf;
    610  1.11      fvdl 			    ncd.ncd_verflen = sizeof (kverf);
    611  1.11      fvdl 			    memmove(ncd.ncd_key, kcr.session,
    612  1.11      fvdl 				sizeof (kcr.session));
    613  1.11      fvdl 			    kin.t1 = htonl(ktv.tv_sec);
    614  1.11      fvdl 			    kin.t2 = htonl(ktv.tv_usec);
    615  1.11      fvdl 			    kin.w1 = htonl(NFS_KERBTTL);
    616  1.11      fvdl 			    kin.w2 = htonl(NFS_KERBTTL - 1);
    617  1.17     lukem 			    memset((caddr_t)kivec, 0, sizeof (kivec));
    618  1.11      fvdl 
    619  1.11      fvdl 			    /*
    620  1.11      fvdl 			     * Encrypt kin in CBC mode using the session
    621  1.11      fvdl 			     * key in kcr.
    622  1.11      fvdl 			     */
    623  1.11      fvdl 			    XXX
    624  1.11      fvdl 
    625  1.11      fvdl 			    /*
    626  1.11      fvdl 			     * Finally, fill the timestamp verifier into the
    627  1.11      fvdl 			     * authenticator and verifier.
    628  1.11      fvdl 			     */
    629  1.11      fvdl 			    ktick.kind = htonl(RPCAKN_FULLNAME);
    630  1.11      fvdl 			    kverf.kind = htonl(RPCAKN_FULLNAME);
    631  1.11      fvdl 			    NFS_KERBW1(ktick.kt) = kout.w1;
    632  1.11      fvdl 			    ktick.kt.length = htonl(ktick.kt.length);
    633  1.11      fvdl 			    kverf.verf.t1 = kout.t1;
    634  1.11      fvdl 			    kverf.verf.t2 = kout.t2;
    635  1.11      fvdl 			    kverf.verf.w2 = kout.w2;
    636  1.11      fvdl 			    nfssvc_flag = NFSSVC_MNTD | NFSSVC_GOTAUTH;
    637   1.1   mycroft 			}
    638   1.1   mycroft 			setreuid(0, 0);
    639  1.11      fvdl #endif /* NFSKERB */
    640   1.1   mycroft 		}
    641   1.1   mycroft 	}
    642   1.1   mycroft 	exit(0);
    643  1.34  christos }
    644  1.34  christos 
    645  1.34  christos static void
    646  1.34  christos shownfsargs(nfsargsp)
    647  1.34  christos 	const struct nfs_args *nfsargsp;
    648  1.34  christos {
    649  1.34  christos 	char fbuf[2048];
    650  1.36     enami 	char host[NI_MAXHOST], serv[NI_MAXSERV];
    651  1.36     enami 	int error;
    652  1.34  christos 
    653  1.34  christos 	(void)snprintb(fbuf, sizeof(fbuf), NFSMNT_BITS, nfsargsp->flags);
    654  1.36     enami 	if (nfsargsp->addr != NULL) {
    655  1.36     enami 		error = getnameinfo(nfsargsp->addr, nfsargsp->addrlen, host,
    656  1.36     enami 		    sizeof(host), serv, sizeof(serv),
    657  1.36     enami 		    NI_NUMERICHOST | NI_NUMERICSERV);
    658  1.36     enami 		if (error != 0)
    659  1.36     enami 			warnx("getnameinfo: %s", gai_strerror(error));
    660  1.36     enami 	} else
    661  1.36     enami 		error = -1;
    662  1.36     enami 
    663  1.36     enami 	if (error == 0)
    664  1.39     lukem 		printf("addr=%s, port=%s, addrlen=%d, ",
    665  1.36     enami 		    host, serv, nfsargsp->addrlen);
    666  1.38      fvdl 	printf("sotype=%d, proto=%d, fhsize=%d, "
    667  1.34  christos 	    "flags=%s, wsize=%d, rsize=%d, readdirsize=%d, timeo=%d, "
    668  1.34  christos 	    "retrans=%d, maxgrouplist=%d, readahead=%d, leaseterm=%d, "
    669  1.34  christos 	    "deadthresh=%d\n",
    670  1.34  christos 	    nfsargsp->sotype,
    671  1.34  christos 	    nfsargsp->proto,
    672  1.34  christos 	    nfsargsp->fhsize,
    673  1.34  christos 	    fbuf,
    674  1.34  christos 	    nfsargsp->wsize,
    675  1.34  christos 	    nfsargsp->rsize,
    676  1.34  christos 	    nfsargsp->readdirsize,
    677  1.34  christos 	    nfsargsp->timeo,
    678  1.34  christos 	    nfsargsp->retrans,
    679  1.34  christos 	    nfsargsp->maxgrouplist,
    680  1.34  christos 	    nfsargsp->readahead,
    681  1.34  christos 	    nfsargsp->leaseterm,
    682  1.34  christos 	    nfsargsp->deadthresh);
    683   1.1   mycroft }
    684   1.1   mycroft 
    685  1.29  jdolecek static int
    686   1.1   mycroft getnfsargs(spec, nfsargsp)
    687   1.1   mycroft 	char *spec;
    688   1.1   mycroft 	struct nfs_args *nfsargsp;
    689   1.1   mycroft {
    690  1.17     lukem 	CLIENT *clp;
    691  1.25      fvdl 	struct addrinfo hints, *ai_nfs, *ai;
    692  1.25      fvdl 	int ecode;
    693  1.25      fvdl 	char host[NI_MAXHOST], serv[NI_MAXSERV];
    694  1.25      fvdl 	static struct netbuf nfs_nb;
    695  1.25      fvdl 	static struct sockaddr_storage nfs_ss;
    696  1.25      fvdl 	struct netconfig *nconf;
    697  1.25      fvdl 	char *netid;
    698   1.1   mycroft #ifdef ISO
    699   1.1   mycroft 	static struct sockaddr_iso isoaddr;
    700   1.1   mycroft 	struct iso_addr *isop;
    701   1.1   mycroft 	int isoflag = 0;
    702   1.1   mycroft #endif
    703   1.1   mycroft 	struct timeval pertry, try;
    704   1.1   mycroft 	enum clnt_stat clnt_stat;
    705  1.25      fvdl 	int i, nfsvers, mntvers, orgcnt;
    706   1.1   mycroft 	char *hostp, *delimp;
    707  1.11      fvdl #ifdef NFSKERB
    708   1.1   mycroft 	char *cp;
    709   1.1   mycroft #endif
    710   1.1   mycroft 	static struct nfhret nfhret;
    711   1.1   mycroft 	static char nam[MNAMELEN + 1];
    712   1.1   mycroft 
    713   1.1   mycroft 	strncpy(nam, spec, MNAMELEN);
    714   1.1   mycroft 	nam[MNAMELEN] = '\0';
    715   1.1   mycroft 	if ((delimp = strchr(spec, '@')) != NULL) {
    716   1.1   mycroft 		hostp = delimp + 1;
    717  1.25      fvdl 	} else if ((delimp = strrchr(spec, ':')) != NULL) {
    718   1.1   mycroft 		hostp = spec;
    719   1.1   mycroft 		spec = delimp + 1;
    720   1.1   mycroft 	} else {
    721   1.1   mycroft 		warnx("no <host>:<dirpath> or <dirpath>@<host> spec");
    722   1.1   mycroft 		return (0);
    723   1.1   mycroft 	}
    724   1.1   mycroft 	*delimp = '\0';
    725   1.1   mycroft 	/*
    726   1.1   mycroft 	 * DUMB!! Until the mount protocol works on iso transport, we must
    727   1.1   mycroft 	 * supply both an iso and an inet address for the host.
    728   1.1   mycroft 	 */
    729   1.1   mycroft #ifdef ISO
    730   1.1   mycroft 	if (!strncmp(hostp, "iso=", 4)) {
    731   1.1   mycroft 		u_short isoport;
    732   1.1   mycroft 
    733   1.1   mycroft 		hostp += 4;
    734   1.1   mycroft 		isoflag++;
    735   1.1   mycroft 		if ((delimp = strchr(hostp, '+')) == NULL) {
    736   1.1   mycroft 			warnx("no iso+inet address");
    737   1.1   mycroft 			return (0);
    738   1.1   mycroft 		}
    739   1.1   mycroft 		*delimp = '\0';
    740   1.1   mycroft 		if ((isop = iso_addr(hostp)) == NULL) {
    741   1.1   mycroft 			warnx("bad ISO address");
    742   1.1   mycroft 			return (0);
    743   1.1   mycroft 		}
    744   1.6   mycroft 		memset(&isoaddr, 0, sizeof (isoaddr));
    745   1.6   mycroft 		memcpy(&isoaddr.siso_addr, isop, sizeof (struct iso_addr));
    746   1.1   mycroft 		isoaddr.siso_len = sizeof (isoaddr);
    747   1.1   mycroft 		isoaddr.siso_family = AF_ISO;
    748   1.1   mycroft 		isoaddr.siso_tlen = 2;
    749   1.1   mycroft 		isoport = htons(NFS_PORT);
    750   1.6   mycroft 		memcpy(TSEL(&isoaddr), &isoport, isoaddr.siso_tlen);
    751   1.1   mycroft 		hostp = delimp + 1;
    752   1.1   mycroft 	}
    753   1.1   mycroft #endif /* ISO */
    754   1.1   mycroft 
    755   1.1   mycroft 	/*
    756   1.1   mycroft 	 * Handle an internet host address and reverse resolve it if
    757   1.1   mycroft 	 * doing Kerberos.
    758   1.1   mycroft 	 */
    759  1.25      fvdl 	memset(&hints, 0, sizeof hints);
    760  1.25      fvdl 	hints.ai_flags = AI_NUMERICHOST;
    761  1.25      fvdl 	hints.ai_socktype = nfsargsp->sotype;
    762  1.25      fvdl 	if (getaddrinfo(hostp, "nfs", &hints, &ai_nfs) == 0) {
    763  1.10   mycroft 		if ((nfsargsp->flags & NFSMNT_KERB)) {
    764  1.25      fvdl 			hints.ai_flags = 0;
    765  1.25      fvdl 			if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host,
    766  1.25      fvdl 			    sizeof host, serv, sizeof serv, 0) != 0) {
    767  1.31       mrg 				warnx("can't reverse resolve net address for "
    768  1.31       mrg 				    "host \"%s\": %s", hostp,
    769  1.31       mrg 				    gai_strerror(ecode));
    770  1.10   mycroft 				return (0);
    771  1.10   mycroft 			}
    772  1.25      fvdl 			hostp = host;
    773   1.1   mycroft 		}
    774  1.10   mycroft 	} else {
    775  1.25      fvdl 		hints.ai_flags = 0;
    776  1.25      fvdl 		if ((ecode = getaddrinfo(hostp, "nfs", &hints, &ai_nfs)) != 0) {
    777  1.31       mrg 			warnx("can't get net id for host \"%s\": %s", hostp,
    778  1.25      fvdl 			    gai_strerror(ecode));
    779   1.1   mycroft 			return (0);
    780   1.1   mycroft 		}
    781  1.10   mycroft 	}
    782  1.11      fvdl #ifdef NFSKERB
    783   1.1   mycroft 	if (nfsargsp->flags & NFSMNT_KERB) {
    784   1.1   mycroft 		strncpy(inst, hp->h_name, INST_SZ);
    785   1.1   mycroft 		inst[INST_SZ - 1] = '\0';
    786   1.1   mycroft 		if (cp = strchr(inst, '.'))
    787   1.1   mycroft 			*cp = '\0';
    788   1.1   mycroft 	}
    789  1.11      fvdl #endif /* NFSKERB */
    790   1.1   mycroft 
    791  1.11      fvdl 	if (force2) {
    792  1.11      fvdl 		nfsvers = NFS_VER2;
    793  1.11      fvdl 		mntvers = RPCMNT_VER1;
    794  1.11      fvdl 	} else {
    795  1.11      fvdl 		nfsvers = NFS_VER3;
    796  1.11      fvdl 		mntvers = RPCMNT_VER3;
    797  1.11      fvdl 	}
    798  1.11      fvdl 	orgcnt = retrycnt;
    799  1.25      fvdl 	nfhret.stat = EACCES;	/* Mark not yet successful */
    800  1.25      fvdl 
    801  1.35    itojun     for (ai = ai_nfs; ai; ai = ai->ai_next) {
    802  1.25      fvdl 	/*
    803  1.25      fvdl 	 * XXX. Nead a generic (family, type, proto) -> nconf interface.
    804  1.25      fvdl 	 * __rpc_*2nconf exist, maybe they should be exported.
    805  1.25      fvdl 	 */
    806  1.25      fvdl 	if (nfsargsp->sotype == SOCK_STREAM) {
    807  1.25      fvdl 		if (ai->ai_family == AF_INET6)
    808  1.25      fvdl 			netid = "tcp6";
    809  1.25      fvdl 		else
    810  1.25      fvdl 			netid = "tcp";
    811  1.25      fvdl 	} else {
    812  1.25      fvdl 		if (ai->ai_family == AF_INET6)
    813  1.25      fvdl 			netid = "udp6";
    814  1.25      fvdl 		else
    815  1.25      fvdl 			netid = "udp";
    816  1.25      fvdl 	}
    817  1.25      fvdl 
    818  1.25      fvdl 	nconf = getnetconfigent(netid);
    819  1.25      fvdl 
    820  1.11      fvdl tryagain:
    821  1.25      fvdl 	retrycnt = orgcnt;
    822  1.25      fvdl 
    823   1.1   mycroft 	while (retrycnt > 0) {
    824  1.25      fvdl 		nfs_nb.buf = &nfs_ss;
    825  1.25      fvdl 		nfs_nb.maxlen = sizeof nfs_ss;
    826  1.25      fvdl 		if (!rpcb_getaddr(RPCPROG_NFS, nfsvers, nconf, &nfs_nb, hostp)){
    827  1.26      fvdl 			if (rpc_createerr.cf_stat == RPC_SYSTEMERROR) {
    828  1.26      fvdl 				nfhret.stat = rpc_createerr.cf_error.re_errno;
    829  1.26      fvdl 				break;
    830  1.26      fvdl 			}
    831  1.26      fvdl 			if (rpc_createerr.cf_stat == RPC_UNKNOWNPROTO) {
    832  1.26      fvdl 				nfhret.stat = EPROTONOSUPPORT;
    833  1.26      fvdl 				break;
    834  1.26      fvdl 			}
    835   1.1   mycroft 			if ((opflags & ISBGRND) == 0)
    836  1.25      fvdl 				clnt_pcreateerror(
    837  1.32       cjs 				    "mount_nfs: rpcbind to nfs on server");
    838   1.1   mycroft 		} else {
    839   1.1   mycroft 			pertry.tv_sec = 10;
    840   1.1   mycroft 			pertry.tv_usec = 0;
    841  1.25      fvdl 			/*
    842  1.25      fvdl 			 * XXX relies on clnt_tcp_create to bind to a reserved
    843  1.25      fvdl 			 * socket.
    844  1.25      fvdl 			 */
    845  1.25      fvdl 			clp = clnt_tp_create(hostp, RPCPROG_MNT, mntvers,
    846  1.25      fvdl 			     mnttcp_ok ? nconf : getnetconfigent("udp"));
    847  1.11      fvdl 			if (clp == NULL) {
    848  1.32       cjs 				if ((opflags & ISBGRND) == 0) {
    849  1.32       cjs 					clnt_pcreateerror(
    850  1.32       cjs 					    "Cannot MNT RPC (mountd)");
    851  1.32       cjs 				}
    852   1.1   mycroft 			} else {
    853  1.25      fvdl 				CLNT_CONTROL(clp, CLSET_RETRY_TIMEOUT,
    854  1.25      fvdl 				    (char *)&pertry);
    855  1.25      fvdl 				clp->cl_auth = authsys_create_default();
    856   1.1   mycroft 				try.tv_sec = 10;
    857   1.1   mycroft 				try.tv_usec = 0;
    858  1.11      fvdl 				if (nfsargsp->flags & NFSMNT_KERB)
    859  1.11      fvdl 				    nfhret.auth = RPCAUTH_KERB4;
    860  1.11      fvdl 				else
    861  1.11      fvdl 				    nfhret.auth = RPCAUTH_UNIX;
    862  1.11      fvdl 				nfhret.vers = mntvers;
    863   1.1   mycroft 				clnt_stat = clnt_call(clp, RPCMNT_MOUNT,
    864   1.1   mycroft 				    xdr_dir, spec, xdr_fh, &nfhret, try);
    865  1.15      fvdl 				switch (clnt_stat) {
    866  1.15      fvdl 				case RPC_PROGVERSMISMATCH:
    867  1.15      fvdl 					if (nfsvers == NFS_VER3 && !force3) {
    868  1.15      fvdl 						nfsvers = NFS_VER2;
    869  1.15      fvdl 						mntvers = RPCMNT_VER1;
    870  1.15      fvdl 						nfsargsp->flags &=
    871  1.15      fvdl 							~NFSMNT_NFSV3;
    872  1.15      fvdl 						goto tryagain;
    873  1.15      fvdl 					} else {
    874  1.15      fvdl 						errx(1, "%s", clnt_sperror(clp,
    875  1.15      fvdl 							"MNT RPC"));
    876  1.11      fvdl 					}
    877  1.15      fvdl 				case RPC_SUCCESS:
    878  1.15      fvdl 					auth_destroy(clp->cl_auth);
    879  1.15      fvdl 					clnt_destroy(clp);
    880  1.15      fvdl 					retrycnt = 0;
    881  1.15      fvdl 					break;
    882  1.15      fvdl 				default:
    883  1.15      fvdl 					/* XXX should give up on some errors */
    884   1.1   mycroft 					if ((opflags & ISBGRND) == 0)
    885   1.1   mycroft 						warnx("%s", clnt_sperror(clp,
    886   1.1   mycroft 						    "bad MNT RPC"));
    887  1.15      fvdl 					break;
    888   1.1   mycroft 				}
    889   1.1   mycroft 			}
    890   1.1   mycroft 		}
    891   1.1   mycroft 		if (--retrycnt > 0) {
    892   1.1   mycroft 			if (opflags & BGRND) {
    893   1.1   mycroft 				opflags &= ~BGRND;
    894  1.17     lukem 				if ((i = fork()) != 0) {
    895   1.1   mycroft 					if (i == -1)
    896   1.1   mycroft 						err(1, "nqnfs 2");
    897   1.1   mycroft 					exit(0);
    898   1.1   mycroft 				}
    899   1.1   mycroft 				(void) setsid();
    900   1.1   mycroft 				(void) close(STDIN_FILENO);
    901   1.1   mycroft 				(void) close(STDOUT_FILENO);
    902   1.1   mycroft 				(void) close(STDERR_FILENO);
    903   1.1   mycroft 				(void) chdir("/");
    904   1.1   mycroft 				opflags |= ISBGRND;
    905   1.1   mycroft 			}
    906   1.1   mycroft 			sleep(60);
    907   1.1   mycroft 		}
    908  1.28    itojun 	}
    909  1.28    itojun 	if (nfhret.stat == 0)
    910  1.25      fvdl 		break;
    911  1.28    itojun     }
    912  1.25      fvdl 	freeaddrinfo(ai_nfs);
    913   1.1   mycroft 	if (nfhret.stat) {
    914   1.1   mycroft 		if (opflags & ISBGRND)
    915   1.1   mycroft 			exit(1);
    916   1.4   mycroft 		errno = nfhret.stat;
    917  1.11      fvdl 		warnx("can't access %s: %s", spec, strerror(nfhret.stat));
    918   1.1   mycroft 		return (0);
    919   1.1   mycroft 	}
    920   1.1   mycroft #ifdef ISO
    921   1.1   mycroft 	if (isoflag) {
    922   1.1   mycroft 		nfsargsp->addr = (struct sockaddr *) &isoaddr;
    923   1.1   mycroft 		nfsargsp->addrlen = sizeof (isoaddr);
    924   1.1   mycroft 	} else
    925   1.1   mycroft #endif /* ISO */
    926   1.1   mycroft 	{
    927  1.25      fvdl 		nfsargsp->addr = (struct sockaddr *) nfs_nb.buf;
    928  1.25      fvdl 		nfsargsp->addrlen = nfs_nb.len;
    929  1.40  christos 		if (port != 0) {
    930  1.40  christos 			struct sockaddr *sa = nfsargsp->addr;
    931  1.40  christos 			switch (sa->sa_family) {
    932  1.40  christos 			case AF_INET:
    933  1.40  christos 				((struct sockaddr_in *)sa)->sin_port = port;
    934  1.40  christos #ifdef INET6
    935  1.40  christos 			case AF_INET6:
    936  1.40  christos 				((struct sockaddr_in6 *)sa)->sin6_port = port;
    937  1.40  christos 				break;
    938  1.40  christos #endif
    939  1.40  christos 			default:
    940  1.40  christos 				errx(1, "Unsupported socket family %d",
    941  1.40  christos 				    sa->sa_family);
    942  1.40  christos 			}
    943  1.40  christos 		}
    944   1.1   mycroft 	}
    945  1.11      fvdl 	nfsargsp->fh = nfhret.nfh;
    946  1.11      fvdl 	nfsargsp->fhsize = nfhret.fhsize;
    947   1.1   mycroft 	nfsargsp->hostname = nam;
    948   1.1   mycroft 	return (1);
    949   1.1   mycroft }
    950   1.1   mycroft 
    951   1.1   mycroft /*
    952   1.1   mycroft  * xdr routines for mount rpc's
    953   1.1   mycroft  */
    954  1.29  jdolecek static int
    955   1.1   mycroft xdr_dir(xdrsp, dirp)
    956   1.1   mycroft 	XDR *xdrsp;
    957   1.1   mycroft 	char *dirp;
    958   1.1   mycroft {
    959   1.1   mycroft 	return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
    960   1.1   mycroft }
    961   1.1   mycroft 
    962  1.29  jdolecek static int
    963   1.1   mycroft xdr_fh(xdrsp, np)
    964   1.1   mycroft 	XDR *xdrsp;
    965  1.17     lukem 	struct nfhret *np;
    966   1.1   mycroft {
    967  1.17     lukem 	int i;
    968  1.11      fvdl 	long auth, authcnt, authfnd = 0;
    969  1.11      fvdl 
    970  1.11      fvdl 	if (!xdr_u_long(xdrsp, &np->stat))
    971   1.1   mycroft 		return (0);
    972   1.1   mycroft 	if (np->stat)
    973   1.1   mycroft 		return (1);
    974  1.11      fvdl 	switch (np->vers) {
    975  1.11      fvdl 	case 1:
    976  1.11      fvdl 		np->fhsize = NFSX_V2FH;
    977  1.11      fvdl 		return (xdr_opaque(xdrsp, (caddr_t)np->nfh, NFSX_V2FH));
    978  1.11      fvdl 	case 3:
    979  1.11      fvdl 		if (!xdr_long(xdrsp, &np->fhsize))
    980  1.11      fvdl 			return (0);
    981  1.11      fvdl 		if (np->fhsize <= 0 || np->fhsize > NFSX_V3FHMAX)
    982  1.11      fvdl 			return (0);
    983  1.11      fvdl 		if (!xdr_opaque(xdrsp, (caddr_t)np->nfh, np->fhsize))
    984  1.11      fvdl 			return (0);
    985  1.11      fvdl 		if (!xdr_long(xdrsp, &authcnt))
    986  1.11      fvdl 			return (0);
    987  1.11      fvdl 		for (i = 0; i < authcnt; i++) {
    988  1.11      fvdl 			if (!xdr_long(xdrsp, &auth))
    989  1.11      fvdl 				return (0);
    990  1.11      fvdl 			if (auth == np->auth)
    991  1.11      fvdl 				authfnd++;
    992  1.11      fvdl 		}
    993  1.11      fvdl 		/*
    994  1.11      fvdl 		 * Some servers, such as DEC's OSF/1 return a nil authenticator
    995  1.11      fvdl 		 * list to indicate RPCAUTH_UNIX.
    996  1.11      fvdl 		 */
    997  1.11      fvdl 		if (!authfnd && (authcnt > 0 || np->auth != RPCAUTH_UNIX))
    998  1.11      fvdl 			np->stat = EAUTH;
    999  1.11      fvdl 		return (1);
   1000  1.11      fvdl 	};
   1001  1.11      fvdl 	return (0);
   1002   1.1   mycroft }
   1003   1.1   mycroft 
   1004  1.29  jdolecek static void
   1005   1.1   mycroft usage()
   1006   1.1   mycroft {
   1007  1.24       mjl 	(void)fprintf(stderr, "usage: mount_nfs %s\n%s\n%s\n%s\n%s\n",
   1008  1.24       mjl "[-23bcCdiKlpPqsTUX] [-a maxreadahead] [-D deadthresh]",
   1009  1.24       mjl "\t[-g maxgroups] [-I readdirsize] [-L leaseterm] [-m realm]",
   1010  1.24       mjl "\t[-o options] [-R retrycnt] [-r readsize] [-t timeout]",
   1011  1.24       mjl "\t[-w writesize] [-x retrans]",
   1012   1.1   mycroft "\trhost:path node");
   1013   1.1   mycroft 	exit(1);
   1014   1.1   mycroft }
   1015