Home | History | Annotate | Line # | Download | only in mount_nfs
mount_nfs.c revision 1.13
      1 /*	$NetBSD: mount_nfs.c,v 1.13 1996/05/23 22:52:49 fvdl 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 #ifndef lint
     40 static char copyright[] =
     41 "@(#) 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 static char rcsid[] = "$NetBSD: mount_nfs.c,v 1.13 1996/05/23 22:52:49 fvdl 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/socketvar.h>
     57 #include <sys/stat.h>
     58 #include <syslog.h>
     59 
     60 #include <rpc/rpc.h>
     61 #include <rpc/pmap_clnt.h>
     62 #include <rpc/pmap_prot.h>
     63 
     64 #ifdef ISO
     65 #include <netiso/iso.h>
     66 #endif
     67 
     68 #ifdef NFSKERB
     69 #include <kerberosIV/des.h>
     70 #include <kerberosIV/krb.h>
     71 #endif
     72 
     73 #include <nfs/rpcv2.h>
     74 #include <nfs/nfsproto.h>
     75 #define _KERNEL
     76 #include <nfs/nfs.h>
     77 #undef _KERNEL
     78 #include <nfs/nqnfs.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 <strings.h>
     91 #include <unistd.h>
     92 
     93 #include "mntopts.h"
     94 
     95 #define	ALTF_BG		0x1
     96 #define ALTF_NOCONN	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_RESVPORT	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 const struct mntopt mopts[] = {
    111 	MOPT_STDOPTS,
    112 	MOPT_FORCE,
    113 	MOPT_UPDATE,
    114 	{ "bg", 0, ALTF_BG, 1 },
    115 	{ "conn", 1, ALTF_NOCONN, 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 	{ "resvport", 0, ALTF_RESVPORT, 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,
    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 mnttcp_ok = 1;
    170 int force2 = 0;
    171 int force3 = 0;
    172 
    173 #ifdef NFSKERB
    174 char inst[INST_SZ];
    175 char realm[REALM_SZ];
    176 struct {
    177 	u_long		kind;
    178 	KTEXT_ST	kt;
    179 } ktick;
    180 struct nfsrpc_nickverf kverf;
    181 struct nfsrpc_fullblock kin, kout;
    182 NFSKERBKEY_T kivec;
    183 CREDENTIALS kcr;
    184 struct timeval ktv;
    185 NFSKERBKEYSCHED_T kerb_keysched;
    186 #endif
    187 
    188 int	getnfsargs __P((char *, struct nfs_args *));
    189 #ifdef ISO
    190 struct	iso_addr *iso_addr __P((const char *));
    191 #endif
    192 void	set_rpc_maxgrouplist __P((int));
    193 __dead	void usage __P((void));
    194 int	xdr_dir __P((XDR *, char *));
    195 int	xdr_fh __P((XDR *, struct nfhret *));
    196 
    197 int
    198 main(argc, argv)
    199 	int argc;
    200 	char *argv[];
    201 {
    202 	register int c;
    203 	register struct nfs_args *nfsargsp;
    204 	struct nfs_args nfsargs;
    205 	struct nfsd_cargs ncd;
    206 	int mntflags, altflags, i, nfssvc_flag, num;
    207 	char *name, *p, *spec;
    208 	int error = 0;
    209 #ifdef NFSKERB
    210 	uid_t last_ruid;
    211 
    212 	last_ruid = -1;
    213 	if (krb_get_lrealm(realm, 0) != KSUCCESS)
    214 	    (void)strcpy(realm, KRB_REALM);
    215 	if (sizeof (struct nfsrpc_nickverf) != RPCX_NICKVERF ||
    216 	    sizeof (struct nfsrpc_fullblock) != RPCX_FULLBLOCK ||
    217 	    ((char *)&ktick.kt) - ((char *)&ktick) != NFSX_UNSIGNED ||
    218 	    ((char *)ktick.kt.dat) - ((char *)&ktick) != 2 * NFSX_UNSIGNED)
    219 		fprintf(stderr, "Yikes! NFSKERB structs not packed!!\n");
    220 
    221 #endif
    222 	retrycnt = DEF_RETRY;
    223 
    224 	mntflags = 0;
    225 	altflags = 0;
    226 	nfsargs = nfsdefargs;
    227 	nfsargsp = &nfsargs;
    228 	while ((c = getopt(argc, argv,
    229 	    "23a:bcdD:g:I:iKL:lm:o:PpqR:r:sTt:w:x:U")) != EOF)
    230 		switch (c) {
    231 		case '3':
    232 			if (force2)
    233 				errx(1, "-2 and -3 are mutually exclusive");
    234 			force3 = 1;
    235 			break;
    236 		case '2':
    237 			if (force3)
    238 				errx(1, "-2 and -3 are mutually exclusive");
    239 			force2 = 1;
    240 			nfsargsp->flags &= ~NFSMNT_NFSV3;
    241 			break;
    242 		case 'a':
    243 			num = strtol(optarg, &p, 10);
    244 			if (*p || num < 0)
    245 				errx(1, "illegal -a value -- %s", optarg);
    246 			nfsargsp->readahead = num;
    247 			nfsargsp->flags |= NFSMNT_READAHEAD;
    248 			break;
    249 		case 'b':
    250 			opflags |= BGRND;
    251 			break;
    252 		case 'c':
    253 			nfsargsp->flags |= NFSMNT_NOCONN;
    254 			break;
    255 		case 'D':
    256 			num = strtol(optarg, &p, 10);
    257 			if (*p || num <= 0)
    258 				errx(1, "illegal -D value -- %s", optarg);
    259 			nfsargsp->deadthresh = num;
    260 			nfsargsp->flags |= NFSMNT_DEADTHRESH;
    261 			break;
    262 		case 'd':
    263 			nfsargsp->flags |= NFSMNT_DUMBTIMR;
    264 			break;
    265 #if 0 /* XXXX */
    266 		case 'g':
    267 			num = strtol(optarg, &p, 10);
    268 			if (*p || num <= 0)
    269 				errx(1, "illegal -g value -- %s", optarg);
    270 			set_rpc_maxgrouplist(num);
    271 			nfsargsp->maxgrouplist = num;
    272 			nfsargsp->flags |= NFSMNT_MAXGRPS;
    273 			break;
    274 #endif
    275 		case 'I':
    276 			num = strtol(optarg, &p, 10);
    277 			if (*p || num <= 0)
    278 				errx(1, "illegal -I value -- %s", optarg);
    279 			nfsargsp->readdirsize = num;
    280 			nfsargsp->flags |= NFSMNT_READDIRSIZE;
    281 			break;
    282 		case 'i':
    283 			nfsargsp->flags |= NFSMNT_INT;
    284 			break;
    285 #ifdef NFSKERB
    286 		case 'K':
    287 			nfsargsp->flags |= NFSMNT_KERB;
    288 			break;
    289 #endif
    290 		case 'L':
    291 			num = strtol(optarg, &p, 10);
    292 			if (*p || num < 2)
    293 				errx(1, "illegal -L value -- %s", optarg);
    294 			nfsargsp->leaseterm = num;
    295 			nfsargsp->flags |= NFSMNT_LEASETERM;
    296 			break;
    297 		case 'l':
    298 			nfsargsp->flags |= NFSMNT_RDIRPLUS;
    299 			break;
    300 #ifdef NFSKERB
    301 		case 'm':
    302 			(void)strncpy(realm, optarg, REALM_SZ - 1);
    303 			realm[REALM_SZ - 1] = '\0';
    304 			break;
    305 #endif
    306 		case 'o':
    307 			getmntopts(optarg, mopts, &mntflags, &altflags);
    308 			if(altflags & ALTF_BG)
    309 				opflags |= BGRND;
    310 			if(altflags & ALTF_NOCONN)
    311 				nfsargsp->flags |= NFSMNT_NOCONN;
    312 			if(altflags & ALTF_DUMBTIMR)
    313 				nfsargsp->flags |= NFSMNT_DUMBTIMR;
    314 			if(altflags & ALTF_INTR)
    315 				nfsargsp->flags |= NFSMNT_INT;
    316 #ifdef NFSKERB
    317 			if(altflags & ALTF_KERB)
    318 				nfsargsp->flags |= NFSMNT_KERB;
    319 #endif
    320 			if(altflags & ALTF_NFSV3) {
    321 				if (force2)
    322 					errx(1,"conflicting version options");
    323 				force3 = 1;
    324 			}
    325 			if(altflags & ALTF_NFSV2) {
    326 				if (force3)
    327 					errx(1,"conflicting version options");
    328 				force2 = 1;
    329 				nfsargsp->flags &= ~NFSMNT_NFSV3;
    330 			}
    331 			if(altflags & ALTF_RDIRPLUS)
    332 				nfsargsp->flags |= NFSMNT_RDIRPLUS;
    333 			if(altflags & ALTF_MNTUDP)
    334 				mnttcp_ok = 0;
    335 			if(altflags & ALTF_RESVPORT)
    336 				nfsargsp->flags |= NFSMNT_RESVPORT;
    337 #ifdef ISO
    338 			if(altflags & ALTF_SEQPACKET)
    339 				nfsargsp->sotype = SOCK_SEQPACKET;
    340 #endif
    341 			if(altflags & ALTF_NQNFS) {
    342 				if (force2)
    343 					errx(1,"nqnfs only available with v3");
    344 				force3 = 1;
    345 				nfsargsp->flags |= NFSMNT_NQNFS;
    346 			}
    347 			if(altflags & ALTF_SOFT)
    348 				nfsargsp->flags |= NFSMNT_SOFT;
    349 			if(altflags & ALTF_TCP) {
    350 				nfsargsp->sotype = SOCK_STREAM;
    351 				nfsproto = IPPROTO_TCP;
    352 			}
    353 			altflags = 0;
    354 			break;
    355 		case 'P':
    356 			nfsargsp->flags |= NFSMNT_RESVPORT;
    357 			break;
    358 #ifdef ISO
    359 		case 'p':
    360 			nfsargsp->sotype = SOCK_SEQPACKET;
    361 			break;
    362 #endif
    363 		case 'q':
    364 			if (force2)
    365 				errx(1,"nqnfs only available with v3");
    366 			force3 = 1;
    367 			nfsargsp->flags |= NFSMNT_NQNFS;
    368 			break;
    369 		case 'R':
    370 			num = strtol(optarg, &p, 10);
    371 			if (*p || num <= 0)
    372 				errx(1, "illegal -R value -- %s", optarg);
    373 			retrycnt = num;
    374 			break;
    375 		case 'r':
    376 			num = strtol(optarg, &p, 10);
    377 			if (*p || num <= 0)
    378 				errx(1, "illegal -r value -- %s", optarg);
    379 			nfsargsp->rsize = num;
    380 			nfsargsp->flags |= NFSMNT_RSIZE;
    381 			break;
    382 		case 's':
    383 			nfsargsp->flags |= NFSMNT_SOFT;
    384 			break;
    385 		case 'T':
    386 			nfsargsp->sotype = SOCK_STREAM;
    387 			nfsproto = IPPROTO_TCP;
    388 			break;
    389 		case 't':
    390 			num = strtol(optarg, &p, 10);
    391 			if (*p || num <= 0)
    392 				errx(1, "illegal -t value -- %s", optarg);
    393 			nfsargsp->timeo = num;
    394 			nfsargsp->flags |= NFSMNT_TIMEO;
    395 			break;
    396 		case 'w':
    397 			num = strtol(optarg, &p, 10);
    398 			if (*p || num <= 0)
    399 				errx(1, "illegal -w value -- %s", optarg);
    400 			nfsargsp->wsize = num;
    401 			nfsargsp->flags |= NFSMNT_WSIZE;
    402 			break;
    403 		case 'x':
    404 			num = strtol(optarg, &p, 10);
    405 			if (*p || num <= 0)
    406 				errx(1, "illegal -x value -- %s", optarg);
    407 			nfsargsp->retrans = num;
    408 			nfsargsp->flags |= NFSMNT_RETRANS;
    409 			break;
    410 		case 'U':
    411 			mnttcp_ok = 0;
    412 			break;
    413 		default:
    414 			usage();
    415 			break;
    416 		}
    417 	argc -= optind;
    418 	argv += optind;
    419 
    420 	if (argc != 2)
    421 		usage();
    422 
    423 	spec = *argv++;
    424 	name = *argv;
    425 
    426 	if (!getnfsargs(spec, nfsargsp))
    427 		exit(1);
    428 	if (mount(MOUNT_NFS, name, mntflags, nfsargsp))
    429 		err(1, "%s", name);
    430 	if (nfsargsp->flags & (NFSMNT_NQNFS | NFSMNT_KERB)) {
    431 		if ((opflags & ISBGRND) == 0) {
    432 			if (i = fork()) {
    433 				if (i == -1)
    434 					err(1, "nqnfs 1");
    435 				exit(0);
    436 			}
    437 			(void) setsid();
    438 			(void) close(STDIN_FILENO);
    439 			(void) close(STDOUT_FILENO);
    440 			(void) close(STDERR_FILENO);
    441 			(void) chdir("/");
    442 		}
    443 		openlog("mount_nfs:", LOG_PID, LOG_DAEMON);
    444 		nfssvc_flag = NFSSVC_MNTD;
    445 		ncd.ncd_dirp = name;
    446 		while (nfssvc(nfssvc_flag, (caddr_t)&ncd) < 0) {
    447 			if (errno != ENEEDAUTH) {
    448 				syslog(LOG_ERR, "nfssvc err %m");
    449 				continue;
    450 			}
    451 			nfssvc_flag =
    452 			    NFSSVC_MNTD | NFSSVC_GOTAUTH | NFSSVC_AUTHINFAIL;
    453 #ifdef NFSKERB
    454 			/*
    455 			 * Set up as ncd_authuid for the kerberos call.
    456 			 * Must set ruid to ncd_authuid and reset the
    457 			 * ticket name iff ncd_authuid is not the same
    458 			 * as last time, so that the right ticket file
    459 			 * is found.
    460 			 * Get the Kerberos credential structure so that
    461 			 * we have the seesion key and get a ticket for
    462 			 * this uid.
    463 			 * For more info see the IETF Draft "Authentication
    464 			 * in ONC RPC".
    465 			 */
    466 			if (ncd.ncd_authuid != last_ruid) {
    467 				krb_set_tkt_string("");
    468 				last_ruid = ncd.ncd_authuid;
    469 			}
    470 			setreuid(ncd.ncd_authuid, 0);
    471 			kret = krb_get_cred(NFS_KERBSRV, inst, realm, &kcr);
    472 			if (kret == RET_NOTKT) {
    473 		            kret = get_ad_tkt(NFS_KERBSRV, inst, realm,
    474 				DEFAULT_TKT_LIFE);
    475 			    if (kret == KSUCCESS)
    476 				kret = krb_get_cred(NFS_KERBSRV, inst, realm,
    477 				    &kcr);
    478 			}
    479 			if (kret == KSUCCESS)
    480 			    kret = krb_mk_req(&ktick.kt, NFS_KERBSRV, inst,
    481 				realm, 0);
    482 
    483 			/*
    484 			 * Fill in the AKN_FULLNAME authenticator and verfier.
    485 			 * Along with the Kerberos ticket, we need to build
    486 			 * the timestamp verifier and encrypt it in CBC mode.
    487 			 */
    488 			if (kret == KSUCCESS &&
    489 			    ktick.kt.length <= (RPCAUTH_MAXSIZ-3*NFSX_UNSIGNED)
    490 			    && gettimeofday(&ktv, (struct timezone *)0) == 0) {
    491 			    ncd.ncd_authtype = RPCAUTH_KERB4;
    492 			    ncd.ncd_authstr = (u_char *)&ktick;
    493 			    ncd.ncd_authlen = nfsm_rndup(ktick.kt.length) +
    494 				3 * NFSX_UNSIGNED;
    495 			    ncd.ncd_verfstr = (u_char *)&kverf;
    496 			    ncd.ncd_verflen = sizeof (kverf);
    497 			    memmove(ncd.ncd_key, kcr.session,
    498 				sizeof (kcr.session));
    499 			    kin.t1 = htonl(ktv.tv_sec);
    500 			    kin.t2 = htonl(ktv.tv_usec);
    501 			    kin.w1 = htonl(NFS_KERBTTL);
    502 			    kin.w2 = htonl(NFS_KERBTTL - 1);
    503 			    bzero((caddr_t)kivec, sizeof (kivec));
    504 
    505 			    /*
    506 			     * Encrypt kin in CBC mode using the session
    507 			     * key in kcr.
    508 			     */
    509 			    XXX
    510 
    511 			    /*
    512 			     * Finally, fill the timestamp verifier into the
    513 			     * authenticator and verifier.
    514 			     */
    515 			    ktick.kind = htonl(RPCAKN_FULLNAME);
    516 			    kverf.kind = htonl(RPCAKN_FULLNAME);
    517 			    NFS_KERBW1(ktick.kt) = kout.w1;
    518 			    ktick.kt.length = htonl(ktick.kt.length);
    519 			    kverf.verf.t1 = kout.t1;
    520 			    kverf.verf.t2 = kout.t2;
    521 			    kverf.verf.w2 = kout.w2;
    522 			    nfssvc_flag = NFSSVC_MNTD | NFSSVC_GOTAUTH;
    523 			}
    524 			setreuid(0, 0);
    525 #endif /* NFSKERB */
    526 		}
    527 	}
    528 	exit(0);
    529 }
    530 
    531 int
    532 getnfsargs(spec, nfsargsp)
    533 	char *spec;
    534 	struct nfs_args *nfsargsp;
    535 {
    536 	register CLIENT *clp;
    537 	struct hostent *hp;
    538 	static struct sockaddr_in saddr;
    539 #ifdef ISO
    540 	static struct sockaddr_iso isoaddr;
    541 	struct iso_addr *isop;
    542 	int isoflag = 0;
    543 #endif
    544 	struct timeval pertry, try;
    545 	enum clnt_stat clnt_stat;
    546 	int so = RPC_ANYSOCK, i, nfsvers, mntvers, orgcnt;
    547 	char *hostp, *delimp;
    548 #ifdef NFSKERB
    549 	char *cp;
    550 #endif
    551 	u_short tport;
    552 	static struct nfhret nfhret;
    553 	static char nam[MNAMELEN + 1];
    554 
    555 	strncpy(nam, spec, MNAMELEN);
    556 	nam[MNAMELEN] = '\0';
    557 	if ((delimp = strchr(spec, '@')) != NULL) {
    558 		hostp = delimp + 1;
    559 	} else if ((delimp = strchr(spec, ':')) != NULL) {
    560 		hostp = spec;
    561 		spec = delimp + 1;
    562 	} else {
    563 		warnx("no <host>:<dirpath> or <dirpath>@<host> spec");
    564 		return (0);
    565 	}
    566 	*delimp = '\0';
    567 	/*
    568 	 * DUMB!! Until the mount protocol works on iso transport, we must
    569 	 * supply both an iso and an inet address for the host.
    570 	 */
    571 #ifdef ISO
    572 	if (!strncmp(hostp, "iso=", 4)) {
    573 		u_short isoport;
    574 
    575 		hostp += 4;
    576 		isoflag++;
    577 		if ((delimp = strchr(hostp, '+')) == NULL) {
    578 			warnx("no iso+inet address");
    579 			return (0);
    580 		}
    581 		*delimp = '\0';
    582 		if ((isop = iso_addr(hostp)) == NULL) {
    583 			warnx("bad ISO address");
    584 			return (0);
    585 		}
    586 		memset(&isoaddr, 0, sizeof (isoaddr));
    587 		memcpy(&isoaddr.siso_addr, isop, sizeof (struct iso_addr));
    588 		isoaddr.siso_len = sizeof (isoaddr);
    589 		isoaddr.siso_family = AF_ISO;
    590 		isoaddr.siso_tlen = 2;
    591 		isoport = htons(NFS_PORT);
    592 		memcpy(TSEL(&isoaddr), &isoport, isoaddr.siso_tlen);
    593 		hostp = delimp + 1;
    594 	}
    595 #endif /* ISO */
    596 
    597 	/*
    598 	 * Handle an internet host address and reverse resolve it if
    599 	 * doing Kerberos.
    600 	 */
    601 	if (inet_aton(hostp, &saddr.sin_addr) != 0) {
    602 		if ((nfsargsp->flags & NFSMNT_KERB)) {
    603 			if ((hp = gethostbyaddr((char *)&saddr.sin_addr.s_addr,
    604 			    sizeof (u_long), AF_INET)) == (struct hostent *)0) {
    605 				warnx("can't reverse resolve net address");
    606 				return (0);
    607 			}
    608 		}
    609 	} else {
    610 		hp = gethostbyname(hostp);
    611 		if (hp == NULL) {
    612 			warnx("can't get net id for host");
    613 			return (0);
    614 		}
    615 		memcpy(&saddr.sin_addr, hp->h_addr, hp->h_length);
    616 	}
    617 #ifdef NFSKERB
    618 	if (nfsargsp->flags & NFSMNT_KERB) {
    619 		strncpy(inst, hp->h_name, INST_SZ);
    620 		inst[INST_SZ - 1] = '\0';
    621 		if (cp = strchr(inst, '.'))
    622 			*cp = '\0';
    623 	}
    624 #endif /* NFSKERB */
    625 
    626 	if (force2) {
    627 		nfsvers = NFS_VER2;
    628 		mntvers = RPCMNT_VER1;
    629 	} else {
    630 		nfsvers = NFS_VER3;
    631 		mntvers = RPCMNT_VER3;
    632 	}
    633 	orgcnt = retrycnt;
    634 tryagain:
    635 	nfhret.stat = EACCES;	/* Mark not yet successful */
    636 	while (retrycnt > 0) {
    637 		saddr.sin_family = AF_INET;
    638 		saddr.sin_port = htons(PMAPPORT);
    639 		if ((tport = pmap_getport(&saddr, RPCPROG_NFS,
    640 		    nfsvers, nfsargsp->sotype == SOCK_STREAM ? IPPROTO_TCP :
    641 		    IPPROTO_UDP)) == 0) {
    642 			if ((opflags & ISBGRND) == 0)
    643 				clnt_pcreateerror("NFS Portmap");
    644 		} else {
    645 			saddr.sin_port = 0;
    646 			pertry.tv_sec = 10;
    647 			pertry.tv_usec = 0;
    648 			if (mnttcp_ok && nfsargsp->sotype == SOCK_STREAM)
    649 			    clp = clnttcp_create(&saddr, RPCPROG_MNT, mntvers,
    650 				&so, 0, 0);
    651 			else
    652 			    clp = clntudp_create(&saddr, RPCPROG_MNT, mntvers,
    653 				pertry, &so);
    654 			if (clp == NULL) {
    655 				if ((opflags & ISBGRND) == 0)
    656 					clnt_pcreateerror("Cannot MNT PRC");
    657 			} else {
    658 				clp->cl_auth = authunix_create_default();
    659 				try.tv_sec = 10;
    660 				try.tv_usec = 0;
    661 				if (nfsargsp->flags & NFSMNT_KERB)
    662 				    nfhret.auth = RPCAUTH_KERB4;
    663 				else
    664 				    nfhret.auth = RPCAUTH_UNIX;
    665 				nfhret.vers = mntvers;
    666 				clnt_stat = clnt_call(clp, RPCMNT_MOUNT,
    667 				    xdr_dir, spec, xdr_fh, &nfhret, try);
    668 				if (clnt_stat != RPC_SUCCESS) {
    669 					if (clnt_stat == RPC_PROGVERSMISMATCH) {
    670 						if (nfsvers == NFS_VER3 &&
    671 						    !force3) {
    672 							retrycnt = orgcnt;
    673 							nfsvers = NFS_VER2;
    674 							mntvers = RPCMNT_VER1;
    675 							nfsargsp->flags &=
    676 								~NFSMNT_NFSV3;
    677 							goto tryagain;
    678 						} else {
    679 							errx(1, "%s",
    680 							    clnt_sperror(clp,
    681 								"MNT RPC"));
    682 						}
    683 					}
    684 					if ((opflags & ISBGRND) == 0)
    685 						warnx("%s", clnt_sperror(clp,
    686 						    "bad MNT RPC"));
    687 				} else {
    688 					auth_destroy(clp->cl_auth);
    689 					clnt_destroy(clp);
    690 					retrycnt = 0;
    691 				}
    692 			}
    693 		}
    694 		if (--retrycnt > 0) {
    695 			if (opflags & BGRND) {
    696 				opflags &= ~BGRND;
    697 				if (i = fork()) {
    698 					if (i == -1)
    699 						err(1, "nqnfs 2");
    700 					exit(0);
    701 				}
    702 				(void) setsid();
    703 				(void) close(STDIN_FILENO);
    704 				(void) close(STDOUT_FILENO);
    705 				(void) close(STDERR_FILENO);
    706 				(void) chdir("/");
    707 				opflags |= ISBGRND;
    708 			}
    709 			sleep(60);
    710 		}
    711 	}
    712 	if (nfhret.stat) {
    713 		if (opflags & ISBGRND)
    714 			exit(1);
    715 		errno = nfhret.stat;
    716 		warnx("can't access %s: %s", spec, strerror(nfhret.stat));
    717 		return (0);
    718 	}
    719 	saddr.sin_port = htons(tport);
    720 #ifdef ISO
    721 	if (isoflag) {
    722 		nfsargsp->addr = (struct sockaddr *) &isoaddr;
    723 		nfsargsp->addrlen = sizeof (isoaddr);
    724 	} else
    725 #endif /* ISO */
    726 	{
    727 		nfsargsp->addr = (struct sockaddr *) &saddr;
    728 		nfsargsp->addrlen = sizeof (saddr);
    729 	}
    730 	nfsargsp->fh = nfhret.nfh;
    731 	nfsargsp->fhsize = nfhret.fhsize;
    732 	nfsargsp->hostname = nam;
    733 	return (1);
    734 }
    735 
    736 /*
    737  * xdr routines for mount rpc's
    738  */
    739 int
    740 xdr_dir(xdrsp, dirp)
    741 	XDR *xdrsp;
    742 	char *dirp;
    743 {
    744 	return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
    745 }
    746 
    747 int
    748 xdr_fh(xdrsp, np)
    749 	XDR *xdrsp;
    750 	register struct nfhret *np;
    751 {
    752 	register int i;
    753 	long auth, authcnt, authfnd = 0;
    754 
    755 	if (!xdr_u_long(xdrsp, &np->stat))
    756 		return (0);
    757 	if (np->stat)
    758 		return (1);
    759 	switch (np->vers) {
    760 	case 1:
    761 		np->fhsize = NFSX_V2FH;
    762 		return (xdr_opaque(xdrsp, (caddr_t)np->nfh, NFSX_V2FH));
    763 	case 3:
    764 		if (!xdr_long(xdrsp, &np->fhsize))
    765 			return (0);
    766 		if (np->fhsize <= 0 || np->fhsize > NFSX_V3FHMAX)
    767 			return (0);
    768 		if (!xdr_opaque(xdrsp, (caddr_t)np->nfh, np->fhsize))
    769 			return (0);
    770 		if (!xdr_long(xdrsp, &authcnt))
    771 			return (0);
    772 		for (i = 0; i < authcnt; i++) {
    773 			if (!xdr_long(xdrsp, &auth))
    774 				return (0);
    775 			if (auth == np->auth)
    776 				authfnd++;
    777 		}
    778 		/*
    779 		 * Some servers, such as DEC's OSF/1 return a nil authenticator
    780 		 * list to indicate RPCAUTH_UNIX.
    781 		 */
    782 		if (!authfnd && (authcnt > 0 || np->auth != RPCAUTH_UNIX))
    783 			np->stat = EAUTH;
    784 		return (1);
    785 	};
    786 	return (0);
    787 }
    788 
    789 __dead void
    790 usage()
    791 {
    792 	(void)fprintf(stderr, "usage: mount_nfs %s\n%s\n%s\n%s\n",
    793 "[-23bcdiKklMPqsT] [-a maxreadahead] [-D deadthresh]",
    794 "\t[-g maxgroups] [-L leaseterm] [-m realm] [-o options] [-R retrycnt]",
    795 "\t[-r readsize] [-t timeout] [-w writesize] [-x retrans]",
    796 "\trhost:path node");
    797 	exit(1);
    798 }
    799