mount_nfs.c revision 24495
11558Srgrimes/*
21558Srgrimes * Copyright (c) 1992, 1993, 1994
31558Srgrimes *	The Regents of the University of California.  All rights reserved.
41558Srgrimes *
51558Srgrimes * This code is derived from software contributed to Berkeley by
61558Srgrimes * Rick Macklem at The University of Guelph.
71558Srgrimes *
81558Srgrimes * Redistribution and use in source and binary forms, with or without
91558Srgrimes * modification, are permitted provided that the following conditions
101558Srgrimes * are met:
111558Srgrimes * 1. Redistributions of source code must retain the above copyright
121558Srgrimes *    notice, this list of conditions and the following disclaimer.
131558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141558Srgrimes *    notice, this list of conditions and the following disclaimer in the
151558Srgrimes *    documentation and/or other materials provided with the distribution.
161558Srgrimes * 3. All advertising materials mentioning features or use of this software
171558Srgrimes *    must display the following acknowledgement:
181558Srgrimes *	This product includes software developed by the University of
191558Srgrimes *	California, Berkeley and its contributors.
201558Srgrimes * 4. Neither the name of the University nor the names of its contributors
211558Srgrimes *    may be used to endorse or promote products derived from this software
221558Srgrimes *    without specific prior written permission.
231558Srgrimes *
241558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271558Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341558Srgrimes * SUCH DAMAGE.
351558Srgrimes */
361558Srgrimes
371558Srgrimes#ifndef lint
381558Srgrimesstatic char copyright[] =
391558Srgrimes"@(#) Copyright (c) 1992, 1993, 1994\n\
401558Srgrimes	The Regents of the University of California.  All rights reserved.\n";
411558Srgrimes#endif /* not lint */
421558Srgrimes
431558Srgrimes#ifndef lint
4415770Swollman/*
4523680Speterstatic char sccsid[] = "@(#)mount_nfs.c	8.11 (Berkeley) 5/4/95";
4615770Swollman*/
4715770Swollmanstatic const char rcsid[] =
4824495Sguido	"$Id: mount_nfs.c,v 1.18 1997/03/29 03:32:39 imp Exp $";
491558Srgrimes#endif /* not lint */
501558Srgrimes
511558Srgrimes#include <sys/param.h>
521558Srgrimes#include <sys/mount.h>
531558Srgrimes#include <sys/socket.h>
541558Srgrimes#include <sys/socketvar.h>
551558Srgrimes#include <sys/stat.h>
561558Srgrimes#include <sys/syslog.h>
571558Srgrimes
581558Srgrimes#include <rpc/rpc.h>
591558Srgrimes#include <rpc/pmap_clnt.h>
601558Srgrimes#include <rpc/pmap_prot.h>
611558Srgrimes
621558Srgrimes#ifdef ISO
631558Srgrimes#include <netiso/iso.h>
641558Srgrimes#endif
651558Srgrimes
669336Sdfr#ifdef NFSKERB
6723680Speter#include <kerberosIV/des.h>
681558Srgrimes#include <kerberosIV/krb.h>
691558Srgrimes#endif
701558Srgrimes
711558Srgrimes#include <nfs/rpcv2.h>
729336Sdfr#include <nfs/nfsproto.h>
7323680Speter#define KERNEL
741558Srgrimes#include <nfs/nfs.h>
7523680Speter#undef KERNEL
761558Srgrimes#include <nfs/nqnfs.h>
771558Srgrimes
781558Srgrimes#include <arpa/inet.h>
791558Srgrimes
801558Srgrimes#include <ctype.h>
811558Srgrimes#include <err.h>
821558Srgrimes#include <errno.h>
831558Srgrimes#include <fcntl.h>
841558Srgrimes#include <netdb.h>
851558Srgrimes#include <signal.h>
861558Srgrimes#include <stdio.h>
871558Srgrimes#include <stdlib.h>
881558Srgrimes#include <strings.h>
8915770Swollman#include <sysexits.h>
901558Srgrimes#include <unistd.h>
911558Srgrimes
921558Srgrimes#include "mntopts.h"
931558Srgrimes
944065Swollman#define	ALTF_BG		0x1
954065Swollman#define ALTF_NOCONN	0x2
964065Swollman#define ALTF_DUMBTIMR	0x4
974065Swollman#define ALTF_INTR	0x8
984065Swollman#define ALTF_KERB	0x10
999336Sdfr#define ALTF_NFSV3	0x20
1009336Sdfr#define ALTF_RDIRPLUS	0x40
1019336Sdfr#define	ALTF_MNTUDP	0x80
1024065Swollman#define ALTF_RESVPORT	0x100
1034065Swollman#define ALTF_SEQPACKET	0x200
1044065Swollman#define ALTF_NQNFS	0x400
1054065Swollman#define ALTF_SOFT	0x800
1064065Swollman#define ALTF_TCP	0x1000
1079230Skarl#define ALTF_PORT	0x2000
1084065Swollman
1091558Srgrimesstruct mntopt mopts[] = {
1101558Srgrimes	MOPT_STDOPTS,
1111558Srgrimes	MOPT_FORCE,
1121558Srgrimes	MOPT_UPDATE,
1134065Swollman	{ "bg", 0, ALTF_BG, 1 },
1144065Swollman	{ "conn", 1, ALTF_NOCONN, 1 },
1154065Swollman	{ "dumbtimer", 0, ALTF_DUMBTIMR, 1 },
1164065Swollman	{ "intr", 0, ALTF_INTR, 1 },
1179336Sdfr#ifdef NFSKERB
1184065Swollman	{ "kerb", 0, ALTF_KERB, 1 },
1194065Swollman#endif
1209336Sdfr	{ "nfsv3", 0, ALTF_NFSV3, 1 },
1219336Sdfr	{ "rdirplus", 0, ALTF_RDIRPLUS, 1 },
1229336Sdfr	{ "mntudp", 0, ALTF_MNTUDP, 1 },
1234065Swollman	{ "resvport", 0, ALTF_RESVPORT, 1 },
1244065Swollman#ifdef ISO
1254065Swollman	{ "seqpacket", 0, ALTF_SEQPACKET, 1 },
1264065Swollman#endif
1274065Swollman	{ "nqnfs", 0, ALTF_NQNFS, 1 },
1284065Swollman	{ "soft", 0, ALTF_SOFT, 1 },
1294065Swollman	{ "tcp", 0, ALTF_TCP, 1 },
1309230Skarl	{ "port=", 0, ALTF_PORT, 1 },
1311558Srgrimes	{ NULL }
1321558Srgrimes};
1331558Srgrimes
1341558Srgrimesstruct nfs_args nfsdefargs = {
13523680Speter	NFS_ARGSVERSION,
1361558Srgrimes	(struct sockaddr *)0,
1371558Srgrimes	sizeof (struct sockaddr_in),
1381558Srgrimes	SOCK_DGRAM,
1391558Srgrimes	0,
1409336Sdfr	(u_char *)0,
1411558Srgrimes	0,
14224495Sguido	NFSMNT_RESVPORT,
1431558Srgrimes	NFS_WSIZE,
1441558Srgrimes	NFS_RSIZE,
1459336Sdfr	NFS_READDIRSIZE,
1469336Sdfr	10,
1471558Srgrimes	NFS_RETRANS,
1481558Srgrimes	NFS_MAXGRPS,
1491558Srgrimes	NFS_DEFRAHEAD,
1501558Srgrimes	NQ_DEFLEASE,
1511558Srgrimes	NQ_DEADTHRESH,
1521558Srgrimes	(char *)0,
1531558Srgrimes};
1541558Srgrimes
1551558Srgrimesstruct nfhret {
1569336Sdfr	u_long		stat;
1579336Sdfr	long		vers;
1589336Sdfr	long		auth;
1599336Sdfr	long		fhsize;
1609336Sdfr	u_char		nfh[NFSX_V3FHMAX];
1611558Srgrimes};
1621558Srgrimes#define	DEF_RETRY	10000
1631558Srgrimes#define	BGRND	1
1641558Srgrimes#define	ISBGRND	2
1651558Srgrimesint retrycnt = DEF_RETRY;
1661558Srgrimesint opflags = 0;
1679336Sdfrint nfsproto = IPPROTO_UDP;
1689336Sdfrint mnttcp_ok = 1;
1699230Skarlu_short port_no = 0;
1701558Srgrimes
1719336Sdfr#ifdef NFSKERB
1721558Srgrimeschar inst[INST_SZ];
1731558Srgrimeschar realm[REALM_SZ];
1749336Sdfrstruct {
1759336Sdfr	u_long		kind;
1769336Sdfr	KTEXT_ST	kt;
1779336Sdfr} ktick;
1789336Sdfrstruct nfsrpc_nickverf kverf;
1799336Sdfrstruct nfsrpc_fullblock kin, kout;
1809336SdfrNFSKERBKEY_T kivec;
1819336SdfrCREDENTIALS kcr;
1829336Sdfrstruct timeval ktv;
1839336SdfrNFSKERBKEYSCHED_T kerb_keysched;
1841558Srgrimes#endif
1851558Srgrimes
1861558Srgrimesint	getnfsargs __P((char *, struct nfs_args *));
1871558Srgrimes#ifdef ISO
1881558Srgrimesstruct	iso_addr *iso_addr __P((const char *));
1891558Srgrimes#endif
1901558Srgrimesvoid	set_rpc_maxgrouplist __P((int));
19118286Sbdevoid	usage __P((void)) __dead2;
1921558Srgrimesint	xdr_dir __P((XDR *, char *));
1931558Srgrimesint	xdr_fh __P((XDR *, struct nfhret *));
1941558Srgrimes
1951558Srgrimesint
1961558Srgrimesmain(argc, argv)
1971558Srgrimes	int argc;
1981558Srgrimes	char *argv[];
1991558Srgrimes{
2001558Srgrimes	register int c;
2011558Srgrimes	register struct nfs_args *nfsargsp;
2021558Srgrimes	struct nfs_args nfsargs;
2031558Srgrimes	struct nfsd_cargs ncd;
2044065Swollman	int mntflags, altflags, i, nfssvc_flag, num;
2051558Srgrimes	char *name, *p, *spec;
20623680Speter	struct vfsconf vfc;
20723680Speter	int error = 0;
2089336Sdfr#ifdef NFSKERB
2091558Srgrimes	uid_t last_ruid;
2101558Srgrimes
2111558Srgrimes	last_ruid = -1;
2121558Srgrimes	(void)strcpy(realm, KRB_REALM);
2139336Sdfr	if (sizeof (struct nfsrpc_nickverf) != RPCX_NICKVERF ||
2149336Sdfr	    sizeof (struct nfsrpc_fullblock) != RPCX_FULLBLOCK ||
2159336Sdfr	    ((char *)&ktick.kt) - ((char *)&ktick) != NFSX_UNSIGNED ||
2169336Sdfr	    ((char *)ktick.kt.dat) - ((char *)&ktick) != 2 * NFSX_UNSIGNED)
2179336Sdfr		fprintf(stderr, "Yikes! NFSKERB structs not packed!!\n");
2189336Sdfr#endif /* NFSKERB */
2191558Srgrimes	retrycnt = DEF_RETRY;
2201558Srgrimes
2211558Srgrimes	mntflags = 0;
2224065Swollman	altflags = 0;
2231558Srgrimes	nfsargs = nfsdefargs;
2241558Srgrimes	nfsargsp = &nfsargs;
2251558Srgrimes	while ((c = getopt(argc, argv,
22624359Simp	    "3a:bcdD:g:I:iKL:lm:o:PpqR:r:sTt:w:x:U")) != -1)
2271558Srgrimes		switch (c) {
2289336Sdfr		case '3':
2299336Sdfr			nfsargsp->flags |= NFSMNT_NFSV3;
2309336Sdfr			break;
2311558Srgrimes		case 'a':
2321558Srgrimes			num = strtol(optarg, &p, 10);
2331558Srgrimes			if (*p || num < 0)
2341558Srgrimes				errx(1, "illegal -a value -- %s", optarg);
2351558Srgrimes			nfsargsp->readahead = num;
2361558Srgrimes			nfsargsp->flags |= NFSMNT_READAHEAD;
2371558Srgrimes			break;
2381558Srgrimes		case 'b':
2391558Srgrimes			opflags |= BGRND;
2401558Srgrimes			break;
2411558Srgrimes		case 'c':
2421558Srgrimes			nfsargsp->flags |= NFSMNT_NOCONN;
2431558Srgrimes			break;
2441558Srgrimes		case 'D':
2451558Srgrimes			num = strtol(optarg, &p, 10);
2461558Srgrimes			if (*p || num <= 0)
2471558Srgrimes				errx(1, "illegal -D value -- %s", optarg);
2481558Srgrimes			nfsargsp->deadthresh = num;
2491558Srgrimes			nfsargsp->flags |= NFSMNT_DEADTHRESH;
2501558Srgrimes			break;
2511558Srgrimes		case 'd':
2521558Srgrimes			nfsargsp->flags |= NFSMNT_DUMBTIMR;
2531558Srgrimes			break;
2541558Srgrimes		case 'g':
2551558Srgrimes			num = strtol(optarg, &p, 10);
2561558Srgrimes			if (*p || num <= 0)
2571558Srgrimes				errx(1, "illegal -g value -- %s", optarg);
2589336Sdfr#ifdef __FreeBSD__
2591558Srgrimes			set_rpc_maxgrouplist(num);
2609336Sdfr#endif
2611558Srgrimes			nfsargsp->maxgrouplist = num;
2621558Srgrimes			nfsargsp->flags |= NFSMNT_MAXGRPS;
2631558Srgrimes			break;
2649336Sdfr		case 'I':
2659336Sdfr			num = strtol(optarg, &p, 10);
2669336Sdfr			if (*p || num <= 0)
2679336Sdfr				errx(1, "illegal -I value -- %s", optarg);
2689336Sdfr			nfsargsp->readdirsize = num;
2699336Sdfr			nfsargsp->flags |= NFSMNT_READDIRSIZE;
2709336Sdfr			break;
2711558Srgrimes		case 'i':
2721558Srgrimes			nfsargsp->flags |= NFSMNT_INT;
2731558Srgrimes			break;
2749336Sdfr#ifdef NFSKERB
2751558Srgrimes		case 'K':
2761558Srgrimes			nfsargsp->flags |= NFSMNT_KERB;
2771558Srgrimes			break;
2781558Srgrimes#endif
2791558Srgrimes		case 'L':
2801558Srgrimes			num = strtol(optarg, &p, 10);
2811558Srgrimes			if (*p || num < 2)
2821558Srgrimes				errx(1, "illegal -L value -- %s", optarg);
2831558Srgrimes			nfsargsp->leaseterm = num;
2841558Srgrimes			nfsargsp->flags |= NFSMNT_LEASETERM;
2851558Srgrimes			break;
2861558Srgrimes		case 'l':
2879336Sdfr			nfsargsp->flags |= NFSMNT_RDIRPLUS;
2881558Srgrimes			break;
2899336Sdfr#ifdef NFSKERB
2901558Srgrimes		case 'm':
2911558Srgrimes			(void)strncpy(realm, optarg, REALM_SZ - 1);
2921558Srgrimes			realm[REALM_SZ - 1] = '\0';
2931558Srgrimes			break;
2941558Srgrimes#endif
2951558Srgrimes		case 'o':
2964065Swollman			getmntopts(optarg, mopts, &mntflags, &altflags);
2974065Swollman			if(altflags & ALTF_BG)
2984065Swollman				opflags |= BGRND;
2994065Swollman			if(altflags & ALTF_NOCONN)
3004065Swollman				nfsargsp->flags |= NFSMNT_NOCONN;
3014065Swollman			if(altflags & ALTF_DUMBTIMR)
3024065Swollman				nfsargsp->flags |= NFSMNT_DUMBTIMR;
3034065Swollman			if(altflags & ALTF_INTR)
3044065Swollman				nfsargsp->flags |= NFSMNT_INT;
3059336Sdfr#ifdef NFSKERB
3064065Swollman			if(altflags & ALTF_KERB)
3074065Swollman				nfsargsp->flags |= NFSMNT_KERB;
3084065Swollman#endif
3099336Sdfr			if(altflags & ALTF_NFSV3)
3109336Sdfr				nfsargsp->flags |= NFSMNT_NFSV3;
3119336Sdfr			if(altflags & ALTF_RDIRPLUS)
3129336Sdfr				nfsargsp->flags |= NFSMNT_RDIRPLUS;
3139336Sdfr			if(altflags & ALTF_MNTUDP)
3149336Sdfr				mnttcp_ok = 0;
3154065Swollman			if(altflags & ALTF_RESVPORT)
3164065Swollman				nfsargsp->flags |= NFSMNT_RESVPORT;
3174065Swollman#ifdef ISO
3184065Swollman			if(altflags & ALTF_SEQPACKET)
3194065Swollman				nfsargsp->sotype = SOCK_SEQPACKET;
3204065Swollman#endif
3214065Swollman			if(altflags & ALTF_NQNFS)
3229336Sdfr				nfsargsp->flags |= (NFSMNT_NQNFS|NFSMNT_NFSV3);
3234065Swollman			if(altflags & ALTF_SOFT)
3244065Swollman				nfsargsp->flags |= NFSMNT_SOFT;
3259336Sdfr			if(altflags & ALTF_TCP) {
3264065Swollman				nfsargsp->sotype = SOCK_STREAM;
3279336Sdfr				nfsproto = IPPROTO_TCP;
3289336Sdfr			}
3299230Skarl			if(altflags & ALTF_PORT)
3309230Skarl				port_no = atoi(strstr(optarg, "port=") + 5);
3314065Swollman			altflags = 0;
3321558Srgrimes			break;
3331558Srgrimes		case 'P':
3341558Srgrimes			nfsargsp->flags |= NFSMNT_RESVPORT;
3351558Srgrimes			break;
3361558Srgrimes#ifdef ISO
3371558Srgrimes		case 'p':
3381558Srgrimes			nfsargsp->sotype = SOCK_SEQPACKET;
3391558Srgrimes			break;
3401558Srgrimes#endif
3411558Srgrimes		case 'q':
3429336Sdfr			nfsargsp->flags |= (NFSMNT_NQNFS | NFSMNT_NFSV3);
3431558Srgrimes			break;
3441558Srgrimes		case 'R':
3451558Srgrimes			num = strtol(optarg, &p, 10);
3461558Srgrimes			if (*p || num <= 0)
3471558Srgrimes				errx(1, "illegal -R value -- %s", optarg);
3481558Srgrimes			retrycnt = num;
3491558Srgrimes			break;
3501558Srgrimes		case 'r':
3511558Srgrimes			num = strtol(optarg, &p, 10);
3521558Srgrimes			if (*p || num <= 0)
3531558Srgrimes				errx(1, "illegal -r value -- %s", optarg);
3541558Srgrimes			nfsargsp->rsize = num;
3551558Srgrimes			nfsargsp->flags |= NFSMNT_RSIZE;
3561558Srgrimes			break;
3571558Srgrimes		case 's':
3581558Srgrimes			nfsargsp->flags |= NFSMNT_SOFT;
3591558Srgrimes			break;
3601558Srgrimes		case 'T':
3611558Srgrimes			nfsargsp->sotype = SOCK_STREAM;
3629336Sdfr			nfsproto = IPPROTO_TCP;
3631558Srgrimes			break;
3641558Srgrimes		case 't':
3651558Srgrimes			num = strtol(optarg, &p, 10);
3661558Srgrimes			if (*p || num <= 0)
3671558Srgrimes				errx(1, "illegal -t value -- %s", optarg);
3681558Srgrimes			nfsargsp->timeo = num;
3691558Srgrimes			nfsargsp->flags |= NFSMNT_TIMEO;
3701558Srgrimes			break;
3711558Srgrimes		case 'w':
3721558Srgrimes			num = strtol(optarg, &p, 10);
3731558Srgrimes			if (*p || num <= 0)
3741558Srgrimes				errx(1, "illegal -w value -- %s", optarg);
3751558Srgrimes			nfsargsp->wsize = num;
3761558Srgrimes			nfsargsp->flags |= NFSMNT_WSIZE;
3771558Srgrimes			break;
3781558Srgrimes		case 'x':
3791558Srgrimes			num = strtol(optarg, &p, 10);
3801558Srgrimes			if (*p || num <= 0)
3811558Srgrimes				errx(1, "illegal -x value -- %s", optarg);
3821558Srgrimes			nfsargsp->retrans = num;
3831558Srgrimes			nfsargsp->flags |= NFSMNT_RETRANS;
3841558Srgrimes			break;
3859336Sdfr		case 'U':
3869336Sdfr			mnttcp_ok = 0;
3879336Sdfr			break;
3881558Srgrimes		default:
3891558Srgrimes			usage();
3901558Srgrimes			break;
3911558Srgrimes		}
3921558Srgrimes	argc -= optind;
3931558Srgrimes	argv += optind;
3941558Srgrimes
39523680Speter	if (argc != 2) {
3965966Sdg		usage();
39723680Speter		/* NOTREACHED */
39823680Speter	}
3991558Srgrimes
4001558Srgrimes	spec = *argv++;
4011558Srgrimes	name = *argv;
4021558Srgrimes
4031558Srgrimes	if (!getnfsargs(spec, nfsargsp))
4041558Srgrimes		exit(1);
4052999Swollman
4069336Sdfr#ifdef __FreeBSD__
40723680Speter	error = getvfsbyname("nfs", &vfc);
40823680Speter	if (error && vfsisloadable("nfs")) {
4092999Swollman		if(vfsload("nfs"))
41015770Swollman			err(EX_OSERR, "vfsload(nfs)");
41123680Speter		endvfsent();	/* clear cache */
41223680Speter		error = getvfsbyname("nfs", &vfc);
4132999Swollman	}
41423680Speter	if (error)
41523680Speter		errx(EX_OSERR, "nfs filesystem is not available");
4162999Swollman
41723680Speter	if (mount(vfc.vfc_name, name, mntflags, nfsargsp))
41823680Speter		err(1, "%s", name);
4199336Sdfr#else
42023680Speter	if (mount("nfs", name, mntflags, nfsargsp))
42123680Speter		err(1, "%s", name);
4229336Sdfr#endif
4231558Srgrimes	if (nfsargsp->flags & (NFSMNT_NQNFS | NFSMNT_KERB)) {
4241558Srgrimes		if ((opflags & ISBGRND) == 0) {
4251558Srgrimes			if (i = fork()) {
4261558Srgrimes				if (i == -1)
4271558Srgrimes					err(1, "nqnfs 1");
4281558Srgrimes				exit(0);
4291558Srgrimes			}
4301558Srgrimes			(void) setsid();
4311558Srgrimes			(void) close(STDIN_FILENO);
4321558Srgrimes			(void) close(STDOUT_FILENO);
4331558Srgrimes			(void) close(STDERR_FILENO);
4341558Srgrimes			(void) chdir("/");
4351558Srgrimes		}
4361558Srgrimes		openlog("mount_nfs:", LOG_PID, LOG_DAEMON);
4371558Srgrimes		nfssvc_flag = NFSSVC_MNTD;
4381558Srgrimes		ncd.ncd_dirp = name;
4391558Srgrimes		while (nfssvc(nfssvc_flag, (caddr_t)&ncd) < 0) {
4401558Srgrimes			if (errno != ENEEDAUTH) {
4411558Srgrimes				syslog(LOG_ERR, "nfssvc err %m");
4421558Srgrimes				continue;
4431558Srgrimes			}
4441558Srgrimes			nfssvc_flag =
4451558Srgrimes			    NFSSVC_MNTD | NFSSVC_GOTAUTH | NFSSVC_AUTHINFAIL;
4469336Sdfr#ifdef NFSKERB
4471558Srgrimes			/*
4481558Srgrimes			 * Set up as ncd_authuid for the kerberos call.
4491558Srgrimes			 * Must set ruid to ncd_authuid and reset the
4501558Srgrimes			 * ticket name iff ncd_authuid is not the same
4511558Srgrimes			 * as last time, so that the right ticket file
4521558Srgrimes			 * is found.
4539336Sdfr			 * Get the Kerberos credential structure so that
4549336Sdfr			 * we have the seesion key and get a ticket for
4559336Sdfr			 * this uid.
4569336Sdfr			 * For more info see the IETF Draft "Authentication
4579336Sdfr			 * in ONC RPC".
4581558Srgrimes			 */
4591558Srgrimes			if (ncd.ncd_authuid != last_ruid) {
4606043Sdfr				char buf[512];
4616043Sdfr				(void)sprintf(buf, "%s%d",
4626043Sdfr					      TKT_ROOT, ncd.ncd_authuid);
4636043Sdfr				krb_set_tkt_string(buf);
4641558Srgrimes				last_ruid = ncd.ncd_authuid;
4651558Srgrimes			}
4669336Sdfr			setreuid(ncd.ncd_authuid, 0);
4679336Sdfr			kret = krb_get_cred(NFS_KERBSRV, inst, realm, &kcr);
4689336Sdfr			if (kret == RET_NOTKT) {
4699336Sdfr		            kret = get_ad_tkt(NFS_KERBSRV, inst, realm,
4709336Sdfr				DEFAULT_TKT_LIFE);
4719336Sdfr			    if (kret == KSUCCESS)
4729336Sdfr				kret = krb_get_cred(NFS_KERBSRV, inst, realm,
4739336Sdfr				    &kcr);
4741558Srgrimes			}
4759336Sdfr			if (kret == KSUCCESS)
4769336Sdfr			    kret = krb_mk_req(&ktick.kt, NFS_KERBSRV, inst,
4779336Sdfr				realm, 0);
4789336Sdfr
4799336Sdfr			/*
4809336Sdfr			 * Fill in the AKN_FULLNAME authenticator and verfier.
4819336Sdfr			 * Along with the Kerberos ticket, we need to build
4829336Sdfr			 * the timestamp verifier and encrypt it in CBC mode.
4839336Sdfr			 */
4849336Sdfr			if (kret == KSUCCESS &&
4859336Sdfr			    ktick.kt.length <= (RPCAUTH_MAXSIZ-3*NFSX_UNSIGNED)
4869336Sdfr			    && gettimeofday(&ktv, (struct timezone *)0) == 0) {
4879336Sdfr			    ncd.ncd_authtype = RPCAUTH_KERB4;
4889336Sdfr			    ncd.ncd_authstr = (u_char *)&ktick;
4899336Sdfr			    ncd.ncd_authlen = nfsm_rndup(ktick.kt.length) +
4909336Sdfr				3 * NFSX_UNSIGNED;
4919336Sdfr			    ncd.ncd_verfstr = (u_char *)&kverf;
4929336Sdfr			    ncd.ncd_verflen = sizeof (kverf);
49323680Speter			    memmove(ncd.ncd_key, kcr.session,
4949336Sdfr				sizeof (kcr.session));
4959336Sdfr			    kin.t1 = htonl(ktv.tv_sec);
4969336Sdfr			    kin.t2 = htonl(ktv.tv_usec);
4979336Sdfr			    kin.w1 = htonl(NFS_KERBTTL);
4989336Sdfr			    kin.w2 = htonl(NFS_KERBTTL - 1);
4999336Sdfr			    bzero((caddr_t)kivec, sizeof (kivec));
5009336Sdfr
5019336Sdfr			    /*
5029336Sdfr			     * Encrypt kin in CBC mode using the session
5039336Sdfr			     * key in kcr.
5049336Sdfr			     */
5059336Sdfr			    XXX
5069336Sdfr
5079336Sdfr			    /*
5089336Sdfr			     * Finally, fill the timestamp verifier into the
5099336Sdfr			     * authenticator and verifier.
5109336Sdfr			     */
5119336Sdfr			    ktick.kind = htonl(RPCAKN_FULLNAME);
5129336Sdfr			    kverf.kind = htonl(RPCAKN_FULLNAME);
5139336Sdfr			    NFS_KERBW1(ktick.kt) = kout.w1;
5149336Sdfr			    ktick.kt.length = htonl(ktick.kt.length);
5159336Sdfr			    kverf.verf.t1 = kout.t1;
5169336Sdfr			    kverf.verf.t2 = kout.t2;
5179336Sdfr			    kverf.verf.w2 = kout.w2;
5189336Sdfr			    nfssvc_flag = NFSSVC_MNTD | NFSSVC_GOTAUTH;
5199336Sdfr			}
5209336Sdfr			setreuid(0, 0);
5219336Sdfr#endif /* NFSKERB */
5221558Srgrimes		}
5231558Srgrimes	}
5241558Srgrimes	exit(0);
5251558Srgrimes}
5261558Srgrimes
5271558Srgrimesint
5281558Srgrimesgetnfsargs(spec, nfsargsp)
5291558Srgrimes	char *spec;
5301558Srgrimes	struct nfs_args *nfsargsp;
5311558Srgrimes{
5321558Srgrimes	register CLIENT *clp;
5331558Srgrimes	struct hostent *hp;
5341558Srgrimes	static struct sockaddr_in saddr;
5351558Srgrimes#ifdef ISO
5361558Srgrimes	static struct sockaddr_iso isoaddr;
5371558Srgrimes	struct iso_addr *isop;
5381558Srgrimes	int isoflag = 0;
5391558Srgrimes#endif
5401558Srgrimes	struct timeval pertry, try;
5411558Srgrimes	enum clnt_stat clnt_stat;
5429336Sdfr	int so = RPC_ANYSOCK, i, nfsvers, mntvers;
5431558Srgrimes	char *hostp, *delimp;
5449336Sdfr#ifdef NFSKERB
5451558Srgrimes	char *cp;
5461558Srgrimes#endif
5471558Srgrimes	u_short tport;
5481558Srgrimes	static struct nfhret nfhret;
5491558Srgrimes	static char nam[MNAMELEN + 1];
5501558Srgrimes
5511558Srgrimes	strncpy(nam, spec, MNAMELEN);
5521558Srgrimes	nam[MNAMELEN] = '\0';
5531558Srgrimes	if ((delimp = strchr(spec, '@')) != NULL) {
5541558Srgrimes		hostp = delimp + 1;
5551558Srgrimes	} else if ((delimp = strchr(spec, ':')) != NULL) {
5561558Srgrimes		hostp = spec;
5571558Srgrimes		spec = delimp + 1;
5581558Srgrimes	} else {
5591558Srgrimes		warnx("no <host>:<dirpath> or <dirpath>@<host> spec");
5601558Srgrimes		return (0);
5611558Srgrimes	}
5621558Srgrimes	*delimp = '\0';
5631558Srgrimes	/*
5641558Srgrimes	 * DUMB!! Until the mount protocol works on iso transport, we must
5651558Srgrimes	 * supply both an iso and an inet address for the host.
5661558Srgrimes	 */
5671558Srgrimes#ifdef ISO
5681558Srgrimes	if (!strncmp(hostp, "iso=", 4)) {
5691558Srgrimes		u_short isoport;
5701558Srgrimes
5711558Srgrimes		hostp += 4;
5721558Srgrimes		isoflag++;
5731558Srgrimes		if ((delimp = strchr(hostp, '+')) == NULL) {
5741558Srgrimes			warnx("no iso+inet address");
5751558Srgrimes			return (0);
5761558Srgrimes		}
5771558Srgrimes		*delimp = '\0';
5781558Srgrimes		if ((isop = iso_addr(hostp)) == NULL) {
5791558Srgrimes			warnx("bad ISO address");
5801558Srgrimes			return (0);
5811558Srgrimes		}
58223680Speter		memset(&isoaddr, 0, sizeof (isoaddr));
58323680Speter		memmove(&isoaddr.siso_addr, isop, sizeof (struct iso_addr));
5841558Srgrimes		isoaddr.siso_len = sizeof (isoaddr);
5851558Srgrimes		isoaddr.siso_family = AF_ISO;
5861558Srgrimes		isoaddr.siso_tlen = 2;
5871558Srgrimes		isoport = htons(NFS_PORT);
58823680Speter		memmove(TSEL(&isoaddr), &isoport, isoaddr.siso_tlen);
5891558Srgrimes		hostp = delimp + 1;
5901558Srgrimes	}
5911558Srgrimes#endif /* ISO */
5921558Srgrimes
5931558Srgrimes	/*
5941558Srgrimes	 * Handle an internet host address and reverse resolve it if
5951558Srgrimes	 * doing Kerberos.
5961558Srgrimes	 */
5971558Srgrimes	if (isdigit(*hostp)) {
5981558Srgrimes		if ((saddr.sin_addr.s_addr = inet_addr(hostp)) == -1) {
5991558Srgrimes			warnx("bad net address %s", hostp);
6001558Srgrimes			return (0);
6011558Srgrimes		}
60223680Speter	} else if ((hp = gethostbyname(hostp)) != NULL)
60323680Speter		memmove(&saddr.sin_addr, hp->h_addr, hp->h_length);
60423680Speter	else {
6052776Sphk		warnx("can't get net id for host");
6062776Sphk		return (0);
6072776Sphk        }
6089336Sdfr#ifdef NFSKERB
6092776Sphk	if ((nfsargsp->flags & NFSMNT_KERB)) {
6102776Sphk		if ((hp = gethostbyaddr((char *)&saddr.sin_addr.s_addr,
6111558Srgrimes		    sizeof (u_long), AF_INET)) == (struct hostent *)0) {
6121558Srgrimes			warnx("can't reverse resolve net address");
6131558Srgrimes			return (0);
6141558Srgrimes		}
61523680Speter		memmove(&saddr.sin_addr, hp->h_addr, hp->h_length);
6161558Srgrimes		strncpy(inst, hp->h_name, INST_SZ);
6171558Srgrimes		inst[INST_SZ - 1] = '\0';
6181558Srgrimes		if (cp = strchr(inst, '.'))
6191558Srgrimes			*cp = '\0';
6201558Srgrimes	}
6219336Sdfr#endif /* NFSKERB */
6221558Srgrimes
6239336Sdfr	if (nfsargsp->flags & NFSMNT_NFSV3) {
6249336Sdfr		nfsvers = 3;
6259336Sdfr		mntvers = 3;
6269336Sdfr	} else {
6279336Sdfr		nfsvers = 2;
6289336Sdfr		mntvers = 1;
6299336Sdfr	}
6301558Srgrimes	nfhret.stat = EACCES;	/* Mark not yet successful */
6311558Srgrimes	while (retrycnt > 0) {
6321558Srgrimes		saddr.sin_family = AF_INET;
6331558Srgrimes		saddr.sin_port = htons(PMAPPORT);
6349230Skarl		if ((tport = port_no ? port_no :
6359230Skarl		     pmap_getport(&saddr, RPCPROG_NFS,
6369336Sdfr		    		  nfsvers, nfsproto)) == 0) {
6371558Srgrimes			if ((opflags & ISBGRND) == 0)
6381558Srgrimes				clnt_pcreateerror("NFS Portmap");
6391558Srgrimes		} else {
6401558Srgrimes			saddr.sin_port = 0;
6411558Srgrimes			pertry.tv_sec = 10;
6421558Srgrimes			pertry.tv_usec = 0;
6439336Sdfr			if (mnttcp_ok && nfsargsp->sotype == SOCK_STREAM)
6449336Sdfr			    clp = clnttcp_create(&saddr, RPCPROG_MNT, mntvers,
6459336Sdfr				&so, 0, 0);
6469336Sdfr			else
6479336Sdfr			    clp = clntudp_create(&saddr, RPCPROG_MNT, mntvers,
6489336Sdfr				pertry, &so);
6499336Sdfr			if (clp == NULL) {
6501558Srgrimes				if ((opflags & ISBGRND) == 0)
6514822Sats					clnt_pcreateerror("Cannot MNT RPC");
6521558Srgrimes			} else {
6531558Srgrimes				clp->cl_auth = authunix_create_default();
6541558Srgrimes				try.tv_sec = 10;
6551558Srgrimes				try.tv_usec = 0;
6569336Sdfr				if (nfsargsp->flags & NFSMNT_KERB)
6579336Sdfr				    nfhret.auth = RPCAUTH_KERB4;
6589336Sdfr				else
6599336Sdfr				    nfhret.auth = RPCAUTH_UNIX;
6609336Sdfr				nfhret.vers = mntvers;
6611558Srgrimes				clnt_stat = clnt_call(clp, RPCMNT_MOUNT,
6621558Srgrimes				    xdr_dir, spec, xdr_fh, &nfhret, try);
6631558Srgrimes				if (clnt_stat != RPC_SUCCESS) {
6641558Srgrimes					if ((opflags & ISBGRND) == 0)
6651558Srgrimes						warnx("%s", clnt_sperror(clp,
6661558Srgrimes						    "bad MNT RPC"));
6671558Srgrimes				} else {
6681558Srgrimes					auth_destroy(clp->cl_auth);
6691558Srgrimes					clnt_destroy(clp);
6701558Srgrimes					retrycnt = 0;
6711558Srgrimes				}
6721558Srgrimes			}
6731558Srgrimes		}
6741558Srgrimes		if (--retrycnt > 0) {
6751558Srgrimes			if (opflags & BGRND) {
6761558Srgrimes				opflags &= ~BGRND;
6771558Srgrimes				if (i = fork()) {
6781558Srgrimes					if (i == -1)
6791558Srgrimes						err(1, "nqnfs 2");
6801558Srgrimes					exit(0);
6811558Srgrimes				}
6821558Srgrimes				(void) setsid();
6831558Srgrimes				(void) close(STDIN_FILENO);
6841558Srgrimes				(void) close(STDOUT_FILENO);
6851558Srgrimes				(void) close(STDERR_FILENO);
6861558Srgrimes				(void) chdir("/");
6871558Srgrimes				opflags |= ISBGRND;
6881558Srgrimes			}
6891558Srgrimes			sleep(60);
6901558Srgrimes		}
6911558Srgrimes	}
6921558Srgrimes	if (nfhret.stat) {
6931558Srgrimes		if (opflags & ISBGRND)
6941558Srgrimes			exit(1);
69523680Speter		warnx("can't access %s: %s", spec, strerror(nfhret.stat));
6961558Srgrimes		return (0);
6971558Srgrimes	}
6981558Srgrimes	saddr.sin_port = htons(tport);
6991558Srgrimes#ifdef ISO
7001558Srgrimes	if (isoflag) {
7011558Srgrimes		nfsargsp->addr = (struct sockaddr *) &isoaddr;
7021558Srgrimes		nfsargsp->addrlen = sizeof (isoaddr);
7031558Srgrimes	} else
7041558Srgrimes#endif /* ISO */
7051558Srgrimes	{
7061558Srgrimes		nfsargsp->addr = (struct sockaddr *) &saddr;
7071558Srgrimes		nfsargsp->addrlen = sizeof (saddr);
7081558Srgrimes	}
7099336Sdfr	nfsargsp->fh = nfhret.nfh;
7109336Sdfr	nfsargsp->fhsize = nfhret.fhsize;
7111558Srgrimes	nfsargsp->hostname = nam;
7121558Srgrimes	return (1);
7131558Srgrimes}
7141558Srgrimes
7151558Srgrimes/*
7161558Srgrimes * xdr routines for mount rpc's
7171558Srgrimes */
7181558Srgrimesint
7191558Srgrimesxdr_dir(xdrsp, dirp)
7201558Srgrimes	XDR *xdrsp;
7211558Srgrimes	char *dirp;
7221558Srgrimes{
7231558Srgrimes	return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
7241558Srgrimes}
7251558Srgrimes
7261558Srgrimesint
7271558Srgrimesxdr_fh(xdrsp, np)
7281558Srgrimes	XDR *xdrsp;
7299336Sdfr	register struct nfhret *np;
7301558Srgrimes{
7319336Sdfr	register int i;
7329336Sdfr	long auth, authcnt, authfnd = 0;
7339336Sdfr
7349336Sdfr	if (!xdr_u_long(xdrsp, &np->stat))
7351558Srgrimes		return (0);
7361558Srgrimes	if (np->stat)
7371558Srgrimes		return (1);
7389336Sdfr	switch (np->vers) {
7399336Sdfr	case 1:
7409336Sdfr		np->fhsize = NFSX_V2FH;
7419336Sdfr		return (xdr_opaque(xdrsp, (caddr_t)np->nfh, NFSX_V2FH));
7429336Sdfr	case 3:
7439336Sdfr		if (!xdr_long(xdrsp, &np->fhsize))
7449336Sdfr			return (0);
7459336Sdfr		if (np->fhsize <= 0 || np->fhsize > NFSX_V3FHMAX)
7469336Sdfr			return (0);
7479336Sdfr		if (!xdr_opaque(xdrsp, (caddr_t)np->nfh, np->fhsize))
7489336Sdfr			return (0);
7499336Sdfr		if (!xdr_long(xdrsp, &authcnt))
7509336Sdfr			return (0);
7519336Sdfr		for (i = 0; i < authcnt; i++) {
7529336Sdfr			if (!xdr_long(xdrsp, &auth))
7539336Sdfr				return (0);
7549336Sdfr			if (auth == np->auth)
7559336Sdfr				authfnd++;
7569336Sdfr		}
7579336Sdfr		/*
7589336Sdfr		 * Some servers, such as DEC's OSF/1 return a nil authenticator
7599336Sdfr		 * list to indicate RPCAUTH_UNIX.
7609336Sdfr		 */
7619336Sdfr		if (!authfnd && (authcnt > 0 || np->auth != RPCAUTH_UNIX))
7629336Sdfr			np->stat = EAUTH;
7639336Sdfr		return (1);
7649336Sdfr	};
7659336Sdfr	return (0);
7661558Srgrimes}
7671558Srgrimes
76818286Sbdevoid
7691558Srgrimesusage()
7701558Srgrimes{
7719336Sdfr	(void)fprintf(stderr, "\
7729336Sdfrusage: mount_nfs [-3KPTUbcdilqs] [-D deadthresh] [-I readdirsize]\n\
7739336Sdfr       [-L leaseterm] [-R retrycnt] [-a maxreadahead] [-g maxgroups]\n\
7749336Sdfr       [-m realm] [-o options] [-r readsize] [-t timeout] [-w writesize]\n\
7759336Sdfr       [-x retrans] rhost:path node\n");
7761558Srgrimes	exit(1);
7771558Srgrimes}
778