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