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