Home | History | Annotate | Line # | Download | only in mount_nfs
mount_nfs.c revision 1.42
      1 /*	$NetBSD: mount_nfs.c,v 1.42 2003/05/16 15:33:45 yamt 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.42 2003/05/16 15:33:45 yamt 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)strcpy(realm, KRB_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)strncpy(realm, optarg, REALM_SZ - 1);
    340 			realm[REALM_SZ - 1] = '\0';
    341 			break;
    342 #endif
    343 		case 'o':
    344 			mp = getmntopts(optarg, mopts, &mntflags, &altflags);
    345 			if (mp == NULL)
    346 				err(1, NULL);
    347 			if (altflags & ALTF_BG)
    348 				opflags |= BGRND;
    349 			if (altflags & ALTF_CONN)
    350 				nfsargsp->flags &= ~NFSMNT_NOCONN;
    351 			if (altflags & ALTF_DUMBTIMR)
    352 				nfsargsp->flags |= NFSMNT_DUMBTIMR;
    353 			if (altflags & ALTF_INTR)
    354 				nfsargsp->flags |= NFSMNT_INT;
    355 #ifdef NFSKERB
    356 			if (altflags & ALTF_KERB)
    357 				nfsargsp->flags |= NFSMNT_KERB;
    358 #endif
    359 			if (altflags & ALTF_NFSV3) {
    360 				if (force2)
    361 					errx(1, "conflicting version options");
    362 				force3 = 1;
    363 			}
    364 			if (altflags & ALTF_NFSV2) {
    365 				if (force3)
    366 					errx(1, "conflicting version options");
    367 				force2 = 1;
    368 				nfsargsp->flags &= ~NFSMNT_NFSV3;
    369 			}
    370 			if (altflags & ALTF_RDIRPLUS)
    371 				nfsargsp->flags |= NFSMNT_RDIRPLUS;
    372 			if (altflags & ALTF_MNTUDP)
    373 				mnttcp_ok = 0;
    374 			if (altflags & ALTF_NORESPORT)
    375 				nfsargsp->flags &= ~NFSMNT_RESVPORT;
    376 #ifdef ISO
    377 			if (altflags & ALTF_SEQPACKET)
    378 				nfsargsp->sotype = SOCK_SEQPACKET;
    379 #endif
    380 			if (altflags & ALTF_NQNFS) {
    381 				if (force2)
    382 					errx(1, "nqnfs only available with v3");
    383 				force3 = 1;
    384 				nfsargsp->flags |= NFSMNT_NQNFS;
    385 			}
    386 			if (altflags & ALTF_SOFT)
    387 				nfsargsp->flags |= NFSMNT_SOFT;
    388 			if (altflags & ALTF_TCP) {
    389 				nfsargsp->sotype = SOCK_STREAM;
    390 				nfsproto = IPPROTO_TCP;
    391 			}
    392 			if (altflags & ALTF_PORT) {
    393 				port = getmntoptnum(mp, "port");
    394 			}
    395 			if (altflags & ALTF_RSIZE) {
    396 				nfsargsp->rsize =
    397 				    (int)getmntoptnum(mp, "rsize");
    398 				nfsargsp->flags |= NFSMNT_RSIZE;
    399 			}
    400 			if (altflags & ALTF_WSIZE) {
    401 				nfsargsp->wsize =
    402 				    (int)getmntoptnum(mp, "wsize");
    403 				nfsargsp->flags |= NFSMNT_WSIZE;
    404 			}
    405 			if (altflags & ALTF_RDIRSIZE) {
    406 				nfsargsp->rsize =
    407 				    (int)getmntoptnum(mp, "rdirsize");
    408 				nfsargsp->flags |= NFSMNT_READDIRSIZE;
    409 			}
    410 #if 0
    411 			if (altflags & ALTF_MAXGRPS) {
    412 				set_rpc_maxgrouplist(num);
    413 				nfsargsp->maxgrouplist =
    414 				    (int)getmntoptnum(mp, "maxgrps");
    415 				nfsargsp->flags |= NFSMNT_MAXGRPS;
    416 			}
    417 #endif
    418 			if (altflags & ALTF_LEASETERM) {
    419 				nfsargsp->leaseterm =
    420 				(int)getmntoptnum(mp, "leaseterm");
    421 				nfsargsp->flags |= NFSMNT_LEASETERM;
    422 			}
    423 			if (altflags & ALTF_READAHEAD) {
    424 				nfsargsp->readahead =
    425 				    (int)getmntoptnum(mp, "readahead");
    426 				nfsargsp->flags |= NFSMNT_READAHEAD;
    427 			}
    428 			if (altflags & ALTF_DEADTHRESH) {
    429 				nfsargsp->deadthresh =
    430 				    (int)getmntoptnum(mp, "deadthresh");
    431 				nfsargsp->flags |= NFSMNT_DEADTHRESH;
    432 			}
    433 			if (altflags & ALTF_TIMEO) {
    434 				nfsargsp->timeo =
    435 				    (int)getmntoptnum(mp, "timeo");
    436 				nfsargsp->flags |= NFSMNT_TIMEO;
    437 			}
    438 			if (altflags & ALTF_RETRANS) {
    439 				nfsargsp->retrans =
    440 				    (int)getmntoptnum(mp, "retrans");
    441 				nfsargsp->flags |= NFSMNT_RETRANS;
    442 			}
    443 			altflags = 0;
    444 			freemntopts(mp);
    445 			break;
    446 		case 'P':
    447 			nfsargsp->flags |= NFSMNT_RESVPORT;
    448 			break;
    449 		case 'p':
    450 			nfsargsp->flags &= ~NFSMNT_RESVPORT;
    451 			break;
    452 		case 'q':
    453 			if (force2)
    454 				errx(1, "nqnfs only available with v3");
    455 			force3 = 1;
    456 			nfsargsp->flags |= NFSMNT_NQNFS;
    457 			break;
    458 		case 'R':
    459 			num = strtol(optarg, &p, 10);
    460 			if (*p || num <= 0)
    461 				errx(1, "illegal -R value -- %s", optarg);
    462 			retrycnt = num;
    463 			break;
    464 		case 'r':
    465 			num = strtol(optarg, &p, 10);
    466 			if (*p || num <= 0)
    467 				errx(1, "illegal -r value -- %s", optarg);
    468 			nfsargsp->rsize = num;
    469 			nfsargsp->flags |= NFSMNT_RSIZE;
    470 			break;
    471 #ifdef ISO
    472 		case 'S':
    473 			nfsargsp->sotype = SOCK_SEQPACKET;
    474 			break;
    475 #endif
    476 		case 's':
    477 			nfsargsp->flags |= NFSMNT_SOFT;
    478 			break;
    479 		case 'T':
    480 			nfsargsp->sotype = SOCK_STREAM;
    481 			nfsproto = IPPROTO_TCP;
    482 			break;
    483 		case 't':
    484 			num = strtol(optarg, &p, 10);
    485 			if (*p || num <= 0)
    486 				errx(1, "illegal -t value -- %s", optarg);
    487 			nfsargsp->timeo = num;
    488 			nfsargsp->flags |= NFSMNT_TIMEO;
    489 			break;
    490 		case 'w':
    491 			num = strtol(optarg, &p, 10);
    492 			if (*p || num <= 0)
    493 				errx(1, "illegal -w value -- %s", optarg);
    494 			nfsargsp->wsize = num;
    495 			nfsargsp->flags |= NFSMNT_WSIZE;
    496 			break;
    497 		case 'x':
    498 			num = strtol(optarg, &p, 10);
    499 			if (*p || num <= 0)
    500 				errx(1, "illegal -x value -- %s", optarg);
    501 			nfsargsp->retrans = num;
    502 			nfsargsp->flags |= NFSMNT_RETRANS;
    503 			break;
    504 		case 'X':
    505 			nfsargsp->flags |= NFSMNT_XLATECOOKIE;
    506 			break;
    507 		case 'U':
    508 			mnttcp_ok = 0;
    509 			break;
    510 		default:
    511 			usage();
    512 			break;
    513 		}
    514 	argc -= optind;
    515 	argv += optind;
    516 
    517 	if (argc != 2)
    518 		usage();
    519 
    520 	spec = *argv++;
    521 	name = *argv;
    522 	if ((ospec = strdup(spec)) == NULL) {
    523 		err(1, "strdup");
    524 	}
    525 
    526 	if ((mntflags & MNT_GETARGS) != 0) {
    527 		memset(&sa, 0, sizeof(sa));
    528 		nfsargsp->addr = (struct sockaddr *)&sa;
    529 		nfsargsp->addrlen = sizeof(sa);
    530 	} else {
    531 		if (!getnfsargs(spec, nfsargsp))
    532 			exit(1);
    533 	}
    534 	if ((retval = mount(MOUNT_NFS, name, mntflags, nfsargsp))) {
    535 		/* Did we just default to v3 on a v2-only kernel?
    536 		 * If so, default to v2 & try again */
    537 		if ((errno == EPROGMISMATCH) && !force3) {
    538 			nfsargsp->flags &= ~NFSMNT_NFSV3;
    539 			retval = mount(MOUNT_NFS, name, mntflags, nfsargsp);
    540 		}
    541 	}
    542 	if (retval)
    543 		err(1, "%s on %s", ospec, name);
    544 	if (mntflags & MNT_GETARGS) {
    545 		shownfsargs(nfsargsp);
    546 		return (0);
    547 	}
    548 
    549 	if (nfsargsp->flags & (NFSMNT_NQNFS | NFSMNT_KERB)) {
    550 		if ((opflags & ISBGRND) == 0) {
    551 			if ((i = fork()) != 0) {
    552 				if (i == -1)
    553 					err(1, "nqnfs 1");
    554 				exit(0);
    555 			}
    556 			(void) setsid();
    557 			(void) close(STDIN_FILENO);
    558 			(void) close(STDOUT_FILENO);
    559 			(void) close(STDERR_FILENO);
    560 			(void) chdir("/");
    561 		}
    562 		openlog("mount_nfs", LOG_PID, LOG_DAEMON);
    563 		nfssvc_flag = NFSSVC_MNTD;
    564 		ncd.ncd_dirp = name;
    565 		while (nfssvc(nfssvc_flag, (caddr_t)&ncd) < 0) {
    566 			if (errno != ENEEDAUTH) {
    567 				syslog(LOG_ERR, "nfssvc err %m");
    568 				continue;
    569 			}
    570 			nfssvc_flag =
    571 			    NFSSVC_MNTD | NFSSVC_GOTAUTH | NFSSVC_AUTHINFAIL;
    572 #ifdef NFSKERB
    573 			/*
    574 			 * Set up as ncd_authuid for the kerberos call.
    575 			 * Must set ruid to ncd_authuid and reset the
    576 			 * ticket name iff ncd_authuid is not the same
    577 			 * as last time, so that the right ticket file
    578 			 * is found.
    579 			 * Get the Kerberos credential structure so that
    580 			 * we have the seesion key and get a ticket for
    581 			 * this uid.
    582 			 * For more info see the IETF Draft "Authentication
    583 			 * in ONC RPC".
    584 			 */
    585 			if (ncd.ncd_authuid != last_ruid) {
    586 				krb_set_tkt_string("");
    587 				last_ruid = ncd.ncd_authuid;
    588 			}
    589 			setreuid(ncd.ncd_authuid, 0);
    590 			kret = krb_get_cred(NFS_KERBSRV, inst, realm, &kcr);
    591 			if (kret == RET_NOTKT) {
    592 		            kret = get_ad_tkt(NFS_KERBSRV, inst, realm,
    593 				DEFAULT_TKT_LIFE);
    594 			    if (kret == KSUCCESS)
    595 				kret = krb_get_cred(NFS_KERBSRV, inst, realm,
    596 				    &kcr);
    597 			}
    598 			if (kret == KSUCCESS)
    599 			    kret = krb_mk_req(&ktick.kt, NFS_KERBSRV, inst,
    600 				realm, 0);
    601 
    602 			/*
    603 			 * Fill in the AKN_FULLNAME authenticator and verfier.
    604 			 * Along with the Kerberos ticket, we need to build
    605 			 * the timestamp verifier and encrypt it in CBC mode.
    606 			 */
    607 			if (kret == KSUCCESS &&
    608 			    ktick.kt.length <= (RPCAUTH_MAXSIZ-3*NFSX_UNSIGNED)
    609 			    && gettimeofday(&ktv, (struct timezone *)0) == 0) {
    610 			    ncd.ncd_authtype = RPCAUTH_KERB4;
    611 			    ncd.ncd_authstr = (u_char *)&ktick;
    612 			    ncd.ncd_authlen = nfsm_rndup(ktick.kt.length) +
    613 				3 * NFSX_UNSIGNED;
    614 			    ncd.ncd_verfstr = (u_char *)&kverf;
    615 			    ncd.ncd_verflen = sizeof (kverf);
    616 			    memmove(ncd.ncd_key, kcr.session,
    617 				sizeof (kcr.session));
    618 			    kin.t1 = htonl(ktv.tv_sec);
    619 			    kin.t2 = htonl(ktv.tv_usec);
    620 			    kin.w1 = htonl(NFS_KERBTTL);
    621 			    kin.w2 = htonl(NFS_KERBTTL - 1);
    622 			    memset((caddr_t)kivec, 0, sizeof (kivec));
    623 
    624 			    /*
    625 			     * Encrypt kin in CBC mode using the session
    626 			     * key in kcr.
    627 			     */
    628 			    XXX
    629 
    630 			    /*
    631 			     * Finally, fill the timestamp verifier into the
    632 			     * authenticator and verifier.
    633 			     */
    634 			    ktick.kind = htonl(RPCAKN_FULLNAME);
    635 			    kverf.kind = htonl(RPCAKN_FULLNAME);
    636 			    NFS_KERBW1(ktick.kt) = kout.w1;
    637 			    ktick.kt.length = htonl(ktick.kt.length);
    638 			    kverf.verf.t1 = kout.t1;
    639 			    kverf.verf.t2 = kout.t2;
    640 			    kverf.verf.w2 = kout.w2;
    641 			    nfssvc_flag = NFSSVC_MNTD | NFSSVC_GOTAUTH;
    642 			}
    643 			setreuid(0, 0);
    644 #endif /* NFSKERB */
    645 		}
    646 	}
    647 	exit(0);
    648 }
    649 
    650 static void
    651 shownfsargs(nfsargsp)
    652 	const struct nfs_args *nfsargsp;
    653 {
    654 	char fbuf[2048];
    655 	char host[NI_MAXHOST], serv[NI_MAXSERV];
    656 	int error;
    657 
    658 	(void)snprintb(fbuf, sizeof(fbuf), NFSMNT_BITS, nfsargsp->flags);
    659 	if (nfsargsp->addr != NULL) {
    660 		error = getnameinfo(nfsargsp->addr, nfsargsp->addrlen, host,
    661 		    sizeof(host), serv, sizeof(serv),
    662 		    NI_NUMERICHOST | NI_NUMERICSERV);
    663 		if (error != 0)
    664 			warnx("getnameinfo: %s", gai_strerror(error));
    665 	} else
    666 		error = -1;
    667 
    668 	if (error == 0)
    669 		printf("addr=%s, port=%s, addrlen=%d, ",
    670 		    host, serv, nfsargsp->addrlen);
    671 	printf("sotype=%d, proto=%d, fhsize=%d, "
    672 	    "flags=%s, wsize=%d, rsize=%d, readdirsize=%d, timeo=%d, "
    673 	    "retrans=%d, maxgrouplist=%d, readahead=%d, leaseterm=%d, "
    674 	    "deadthresh=%d\n",
    675 	    nfsargsp->sotype,
    676 	    nfsargsp->proto,
    677 	    nfsargsp->fhsize,
    678 	    fbuf,
    679 	    nfsargsp->wsize,
    680 	    nfsargsp->rsize,
    681 	    nfsargsp->readdirsize,
    682 	    nfsargsp->timeo,
    683 	    nfsargsp->retrans,
    684 	    nfsargsp->maxgrouplist,
    685 	    nfsargsp->readahead,
    686 	    nfsargsp->leaseterm,
    687 	    nfsargsp->deadthresh);
    688 }
    689 
    690 static int
    691 getnfsargs(spec, nfsargsp)
    692 	char *spec;
    693 	struct nfs_args *nfsargsp;
    694 {
    695 	CLIENT *clp;
    696 	struct addrinfo hints, *ai_nfs, *ai;
    697 	int ecode;
    698 	char host[NI_MAXHOST], serv[NI_MAXSERV];
    699 	static struct netbuf nfs_nb;
    700 	static struct sockaddr_storage nfs_ss;
    701 	struct netconfig *nconf;
    702 	char *netid;
    703 #ifdef ISO
    704 	static struct sockaddr_iso isoaddr;
    705 	struct iso_addr *isop;
    706 	int isoflag = 0;
    707 #endif
    708 	struct timeval pertry, try;
    709 	enum clnt_stat clnt_stat;
    710 	int i, nfsvers, mntvers, orgcnt;
    711 	char *hostp, *delimp;
    712 #ifdef NFSKERB
    713 	char *cp;
    714 #endif
    715 	static struct nfhret nfhret;
    716 	static char nam[MNAMELEN + 1];
    717 
    718 	strncpy(nam, spec, MNAMELEN);
    719 	nam[MNAMELEN] = '\0';
    720 	if ((delimp = strchr(spec, '@')) != NULL) {
    721 		hostp = delimp + 1;
    722 	} else if ((delimp = strrchr(spec, ':')) != NULL) {
    723 		hostp = spec;
    724 		spec = delimp + 1;
    725 	} else {
    726 		warnx("no <host>:<dirpath> or <dirpath>@<host> spec");
    727 		return (0);
    728 	}
    729 	*delimp = '\0';
    730 	/*
    731 	 * DUMB!! Until the mount protocol works on iso transport, we must
    732 	 * supply both an iso and an inet address for the host.
    733 	 */
    734 #ifdef ISO
    735 	if (!strncmp(hostp, "iso=", 4)) {
    736 		u_short isoport;
    737 
    738 		hostp += 4;
    739 		isoflag++;
    740 		if ((delimp = strchr(hostp, '+')) == NULL) {
    741 			warnx("no iso+inet address");
    742 			return (0);
    743 		}
    744 		*delimp = '\0';
    745 		if ((isop = iso_addr(hostp)) == NULL) {
    746 			warnx("bad ISO address");
    747 			return (0);
    748 		}
    749 		memset(&isoaddr, 0, sizeof (isoaddr));
    750 		memcpy(&isoaddr.siso_addr, isop, sizeof (struct iso_addr));
    751 		isoaddr.siso_len = sizeof (isoaddr);
    752 		isoaddr.siso_family = AF_ISO;
    753 		isoaddr.siso_tlen = 2;
    754 		isoport = htons(NFS_PORT);
    755 		memcpy(TSEL(&isoaddr), &isoport, isoaddr.siso_tlen);
    756 		hostp = delimp + 1;
    757 	}
    758 #endif /* ISO */
    759 
    760 	/*
    761 	 * Handle an internet host address and reverse resolve it if
    762 	 * doing Kerberos.
    763 	 */
    764 	memset(&hints, 0, sizeof hints);
    765 	hints.ai_flags = AI_NUMERICHOST;
    766 	hints.ai_socktype = nfsargsp->sotype;
    767 	if (getaddrinfo(hostp, "nfs", &hints, &ai_nfs) == 0) {
    768 		if ((nfsargsp->flags & NFSMNT_KERB)) {
    769 			hints.ai_flags = 0;
    770 			if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host,
    771 			    sizeof host, serv, sizeof serv, 0) != 0) {
    772 				warnx("can't reverse resolve net address for "
    773 				    "host \"%s\": %s", hostp,
    774 				    gai_strerror(ecode));
    775 				return (0);
    776 			}
    777 			hostp = host;
    778 		}
    779 	} else {
    780 		hints.ai_flags = 0;
    781 		if ((ecode = getaddrinfo(hostp, "nfs", &hints, &ai_nfs)) != 0) {
    782 			warnx("can't get net id for host \"%s\": %s", hostp,
    783 			    gai_strerror(ecode));
    784 			return (0);
    785 		}
    786 	}
    787 #ifdef NFSKERB
    788 	if (nfsargsp->flags & NFSMNT_KERB) {
    789 		strncpy(inst, hp->h_name, INST_SZ);
    790 		inst[INST_SZ - 1] = '\0';
    791 		if (cp = strchr(inst, '.'))
    792 			*cp = '\0';
    793 	}
    794 #endif /* NFSKERB */
    795 
    796 	if (force2) {
    797 		nfsvers = NFS_VER2;
    798 		mntvers = RPCMNT_VER1;
    799 	} else {
    800 		nfsvers = NFS_VER3;
    801 		mntvers = RPCMNT_VER3;
    802 	}
    803 	orgcnt = retrycnt;
    804 	nfhret.stat = EACCES;	/* Mark not yet successful */
    805 
    806     for (ai = ai_nfs; ai; ai = ai->ai_next) {
    807 	/*
    808 	 * XXX. Nead a generic (family, type, proto) -> nconf interface.
    809 	 * __rpc_*2nconf exist, maybe they should be exported.
    810 	 */
    811 	if (nfsargsp->sotype == SOCK_STREAM) {
    812 		if (ai->ai_family == AF_INET6)
    813 			netid = "tcp6";
    814 		else
    815 			netid = "tcp";
    816 	} else {
    817 		if (ai->ai_family == AF_INET6)
    818 			netid = "udp6";
    819 		else
    820 			netid = "udp";
    821 	}
    822 
    823 	nconf = getnetconfigent(netid);
    824 
    825 tryagain:
    826 	retrycnt = orgcnt;
    827 
    828 	while (retrycnt > 0) {
    829 		nfs_nb.buf = &nfs_ss;
    830 		nfs_nb.maxlen = sizeof nfs_ss;
    831 		if (!rpcb_getaddr(RPCPROG_NFS, nfsvers, nconf, &nfs_nb, hostp)){
    832 			if (rpc_createerr.cf_stat == RPC_SYSTEMERROR) {
    833 				nfhret.stat = rpc_createerr.cf_error.re_errno;
    834 				break;
    835 			}
    836 			if (rpc_createerr.cf_stat == RPC_UNKNOWNPROTO) {
    837 				nfhret.stat = EPROTONOSUPPORT;
    838 				break;
    839 			}
    840 			if ((opflags & ISBGRND) == 0)
    841 				clnt_pcreateerror(
    842 				    "mount_nfs: rpcbind to nfs on server");
    843 		} else {
    844 			pertry.tv_sec = 10;
    845 			pertry.tv_usec = 0;
    846 			/*
    847 			 * XXX relies on clnt_tcp_create to bind to a reserved
    848 			 * socket.
    849 			 */
    850 			clp = clnt_tp_create(hostp, RPCPROG_MNT, mntvers,
    851 			     mnttcp_ok ? nconf : getnetconfigent("udp"));
    852 			if (clp == NULL) {
    853 				if ((opflags & ISBGRND) == 0) {
    854 					clnt_pcreateerror(
    855 					    "Cannot MNT RPC (mountd)");
    856 				}
    857 			} else {
    858 				CLNT_CONTROL(clp, CLSET_RETRY_TIMEOUT,
    859 				    (char *)&pertry);
    860 				clp->cl_auth = authsys_create_default();
    861 				try.tv_sec = 10;
    862 				try.tv_usec = 0;
    863 				if (nfsargsp->flags & NFSMNT_KERB)
    864 				    nfhret.auth = RPCAUTH_KERB4;
    865 				else
    866 				    nfhret.auth = RPCAUTH_UNIX;
    867 				nfhret.vers = mntvers;
    868 				clnt_stat = clnt_call(clp, RPCMNT_MOUNT,
    869 				    xdr_dir, spec, xdr_fh, &nfhret, try);
    870 				switch (clnt_stat) {
    871 				case RPC_PROGVERSMISMATCH:
    872 					if (nfsvers == NFS_VER3 && !force3) {
    873 						nfsvers = NFS_VER2;
    874 						mntvers = RPCMNT_VER1;
    875 						nfsargsp->flags &=
    876 							~NFSMNT_NFSV3;
    877 						goto tryagain;
    878 					} else {
    879 						errx(1, "%s", clnt_sperror(clp,
    880 							"MNT RPC"));
    881 					}
    882 				case RPC_SUCCESS:
    883 					auth_destroy(clp->cl_auth);
    884 					clnt_destroy(clp);
    885 					retrycnt = 0;
    886 					break;
    887 				default:
    888 					/* XXX should give up on some errors */
    889 					if ((opflags & ISBGRND) == 0)
    890 						warnx("%s", clnt_sperror(clp,
    891 						    "bad MNT RPC"));
    892 					break;
    893 				}
    894 			}
    895 		}
    896 		if (--retrycnt > 0) {
    897 			if (opflags & BGRND) {
    898 				opflags &= ~BGRND;
    899 				if ((i = fork()) != 0) {
    900 					if (i == -1)
    901 						err(1, "nqnfs 2");
    902 					exit(0);
    903 				}
    904 				(void) setsid();
    905 				(void) close(STDIN_FILENO);
    906 				(void) close(STDOUT_FILENO);
    907 				(void) close(STDERR_FILENO);
    908 				(void) chdir("/");
    909 				opflags |= ISBGRND;
    910 			}
    911 			sleep(60);
    912 		}
    913 	}
    914 	if (nfhret.stat == 0)
    915 		break;
    916     }
    917 	freeaddrinfo(ai_nfs);
    918 	if (nfhret.stat) {
    919 		if (opflags & ISBGRND)
    920 			exit(1);
    921 		errno = nfhret.stat;
    922 		warnx("can't access %s: %s", spec, strerror(nfhret.stat));
    923 		return (0);
    924 	}
    925 #ifdef ISO
    926 	if (isoflag) {
    927 		nfsargsp->addr = (struct sockaddr *) &isoaddr;
    928 		nfsargsp->addrlen = sizeof (isoaddr);
    929 	} else
    930 #endif /* ISO */
    931 	{
    932 		nfsargsp->addr = (struct sockaddr *) nfs_nb.buf;
    933 		nfsargsp->addrlen = nfs_nb.len;
    934 		if (port != 0) {
    935 			struct sockaddr *sa = nfsargsp->addr;
    936 			switch (sa->sa_family) {
    937 			case AF_INET:
    938 				((struct sockaddr_in *)sa)->sin_port = port;
    939 #ifdef INET6
    940 			case AF_INET6:
    941 				((struct sockaddr_in6 *)sa)->sin6_port = port;
    942 				break;
    943 #endif
    944 			default:
    945 				errx(1, "Unsupported socket family %d",
    946 				    sa->sa_family);
    947 			}
    948 		}
    949 	}
    950 	nfsargsp->fh = nfhret.nfh;
    951 	nfsargsp->fhsize = nfhret.fhsize;
    952 	nfsargsp->hostname = nam;
    953 	return (1);
    954 }
    955 
    956 /*
    957  * xdr routines for mount rpc's
    958  */
    959 static int
    960 xdr_dir(xdrsp, dirp)
    961 	XDR *xdrsp;
    962 	char *dirp;
    963 {
    964 	return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
    965 }
    966 
    967 static int
    968 xdr_fh(xdrsp, np)
    969 	XDR *xdrsp;
    970 	struct nfhret *np;
    971 {
    972 	int i;
    973 	long auth, authcnt, authfnd = 0;
    974 
    975 	if (!xdr_u_long(xdrsp, &np->stat))
    976 		return (0);
    977 	if (np->stat)
    978 		return (1);
    979 	switch (np->vers) {
    980 	case 1:
    981 		np->fhsize = NFSX_V2FH;
    982 		return (xdr_opaque(xdrsp, (caddr_t)np->nfh, NFSX_V2FH));
    983 	case 3:
    984 		if (!xdr_long(xdrsp, &np->fhsize))
    985 			return (0);
    986 		if (np->fhsize <= 0 || np->fhsize > NFSX_V3FHMAX)
    987 			return (0);
    988 		if (!xdr_opaque(xdrsp, (caddr_t)np->nfh, np->fhsize))
    989 			return (0);
    990 		if (!xdr_long(xdrsp, &authcnt))
    991 			return (0);
    992 		for (i = 0; i < authcnt; i++) {
    993 			if (!xdr_long(xdrsp, &auth))
    994 				return (0);
    995 			if (auth == np->auth)
    996 				authfnd++;
    997 		}
    998 		/*
    999 		 * Some servers, such as DEC's OSF/1 return a nil authenticator
   1000 		 * list to indicate RPCAUTH_UNIX.
   1001 		 */
   1002 		if (!authfnd && (authcnt > 0 || np->auth != RPCAUTH_UNIX))
   1003 			np->stat = EAUTH;
   1004 		return (1);
   1005 	};
   1006 	return (0);
   1007 }
   1008 
   1009 static void
   1010 usage()
   1011 {
   1012 	(void)fprintf(stderr, "usage: mount_nfs %s\n%s\n%s\n%s\n%s\n",
   1013 "[-23bcCdiKlpPqsTUX] [-a maxreadahead] [-D deadthresh]",
   1014 "\t[-g maxgroups] [-I readdirsize] [-L leaseterm] [-m realm]",
   1015 "\t[-o options] [-R retrycnt] [-r readsize] [-t timeout]",
   1016 "\t[-w writesize] [-x retrans]",
   1017 "\trhost:path node");
   1018 	exit(1);
   1019 }
   1020