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