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