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