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