mount_nfs.c revision 26417
1/*
2 * Copyright (c) 1992, 1993, 1994
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by the University of
19 *	California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#ifndef lint
38static char copyright[] =
39"@(#) Copyright (c) 1992, 1993, 1994\n\
40	The Regents of the University of California.  All rights reserved.\n";
41#endif /* not lint */
42
43#ifndef lint
44/*
45static char sccsid[] = "@(#)mount_nfs.c	8.11 (Berkeley) 5/4/95";
46*/
47static const char rcsid[] =
48	"$Id: mount_nfs.c,v 1.22 1997/05/01 12:30:02 dfr Exp $";
49#endif /* not lint */
50
51#include <sys/param.h>
52#include <sys/mount.h>
53#include <sys/socket.h>
54#include <sys/socketvar.h>
55#include <sys/stat.h>
56#include <sys/syslog.h>
57
58#include <rpc/rpc.h>
59#include <rpc/pmap_clnt.h>
60#include <rpc/pmap_prot.h>
61
62#ifdef ISO
63#include <netiso/iso.h>
64#endif
65
66#ifdef NFSKERB
67#include <kerberosIV/des.h>
68#include <kerberosIV/krb.h>
69#endif
70
71#include <nfs/rpcv2.h>
72#include <nfs/nfsproto.h>
73#define KERNEL
74#include <nfs/nfs.h>
75#undef KERNEL
76#include <nfs/nqnfs.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 <strings.h>
89#include <sysexits.h>
90#include <unistd.h>
91
92#include "mntopts.h"
93
94#define	ALTF_BG		0x1
95#define ALTF_NOCONN	0x2
96#define ALTF_DUMBTIMR	0x4
97#define ALTF_INTR	0x8
98#define ALTF_KERB	0x10
99#define ALTF_NFSV3	0x20
100#define ALTF_RDIRPLUS	0x40
101#define	ALTF_MNTUDP	0x80
102#define ALTF_RESVPORT	0x100
103#define ALTF_SEQPACKET	0x200
104#define ALTF_NQNFS	0x400
105#define ALTF_SOFT	0x800
106#define ALTF_TCP	0x1000
107#define ALTF_PORT	0x2000
108#define ALTF_NFSV2	0x4000
109
110struct mntopt mopts[] = {
111	MOPT_STDOPTS,
112	MOPT_FORCE,
113	MOPT_UPDATE,
114	MOPT_ASYNC,
115	{ "bg", 0, ALTF_BG, 1 },
116	{ "conn", 1, ALTF_NOCONN, 1 },
117	{ "dumbtimer", 0, ALTF_DUMBTIMR, 1 },
118	{ "intr", 0, ALTF_INTR, 1 },
119#ifdef NFSKERB
120	{ "kerb", 0, ALTF_KERB, 1 },
121#endif
122	{ "nfsv3", 0, ALTF_NFSV3, 1 },
123	{ "rdirplus", 0, ALTF_RDIRPLUS, 1 },
124	{ "mntudp", 0, ALTF_MNTUDP, 1 },
125	{ "resvport", 0, ALTF_RESVPORT, 1 },
126#ifdef ISO
127	{ "seqpacket", 0, ALTF_SEQPACKET, 1 },
128#endif
129	{ "nqnfs", 0, ALTF_NQNFS, 1 },
130	{ "soft", 0, ALTF_SOFT, 1 },
131	{ "tcp", 0, ALTF_TCP, 1 },
132	{ "port=", 0, ALTF_PORT, 1 },
133	{ "nfsv2", 0, ALTF_NFSV2, 1 },
134	{ NULL }
135};
136
137struct 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_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
158struct 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
168int retrycnt = DEF_RETRY;
169int opflags = 0;
170int nfsproto = IPPROTO_UDP;
171int mnttcp_ok = 1;
172u_short port_no = 0;
173enum {
174	ANY,
175	V2,
176	V3
177} mountmode = ANY;
178
179#ifdef NFSKERB
180char inst[INST_SZ];
181char realm[REALM_SZ];
182struct {
183	u_long		kind;
184	KTEXT_ST	kt;
185} ktick;
186struct nfsrpc_nickverf kverf;
187struct nfsrpc_fullblock kin, kout;
188NFSKERBKEY_T kivec;
189CREDENTIALS kcr;
190struct timeval ktv;
191NFSKERBKEYSCHED_T kerb_keysched;
192#endif
193
194int	getnfsargs __P((char *, struct nfs_args *));
195#ifdef ISO
196struct	iso_addr *iso_addr __P((const char *));
197#endif
198void	set_rpc_maxgrouplist __P((int));
199void	usage __P((void)) __dead2;
200int	xdr_dir __P((XDR *, char *));
201int	xdr_fh __P((XDR *, struct nfhret *));
202
203/*
204 * Used to set mount flags with getmntopts.  Call with dir=TRUE to
205 * initialise altflags from the current mount flags.  Call with
206 * dir=FALSE to update mount flags with the new value of altflags after
207 * the call to getmntopts.
208 */
209static void
210setflags(int* altflags, int* nfsflags, int dir)
211{
212#define F2(af, nf)					\
213	if (dir) {					\
214		if (*nfsflags & NFSMNT_##nf)		\
215			*altflags |= ALTF_##af;		\
216		else					\
217			*altflags &= ~ALTF_##af;	\
218	} else {					\
219		if (*altflags & ALTF_##af)		\
220			*nfsflags |= NFSMNT_##nf;	\
221		else					\
222			*nfsflags &= ~NFSMNT_##nf;	\
223	}
224#define F(f)	F2(f,f)
225
226	F(NOCONN);
227	F(DUMBTIMR);
228	F2(INTR, INT);
229#ifdef NFSKERB
230	F(KERB);
231#endif
232	F(RDIRPLUS);
233	F(RESVPORT);
234	F(NQNFS);
235	F(SOFT);
236
237#undef F
238#undef F2
239}
240
241int
242main(argc, argv)
243	int argc;
244	char *argv[];
245{
246	register int c;
247	register struct nfs_args *nfsargsp;
248	struct nfs_args nfsargs;
249	struct nfsd_cargs ncd;
250	int mntflags, altflags, i, nfssvc_flag, num;
251	char *name, *p, *spec;
252	struct vfsconf vfc;
253	int error = 0;
254#ifdef NFSKERB
255	uid_t last_ruid;
256
257	last_ruid = -1;
258	(void)strcpy(realm, KRB_REALM);
259	if (sizeof (struct nfsrpc_nickverf) != RPCX_NICKVERF ||
260	    sizeof (struct nfsrpc_fullblock) != RPCX_FULLBLOCK ||
261	    ((char *)&ktick.kt) - ((char *)&ktick) != NFSX_UNSIGNED ||
262	    ((char *)ktick.kt.dat) - ((char *)&ktick) != 2 * NFSX_UNSIGNED)
263		fprintf(stderr, "Yikes! NFSKERB structs not packed!!\n");
264#endif /* NFSKERB */
265	retrycnt = DEF_RETRY;
266
267	mntflags = 0;
268	altflags = 0;
269	nfsargs = nfsdefargs;
270	nfsargsp = &nfsargs;
271	while ((c = getopt(argc, argv,
272	    "23a:bcdD:g:I:iKL:lm:o:PpqR:r:sTt:w:x:U")) != -1)
273		switch (c) {
274		case '2':
275			mountmode = V2;
276			break;
277		case '3':
278			mountmode = V3;
279			break;
280		case 'a':
281			num = strtol(optarg, &p, 10);
282			if (*p || num < 0)
283				errx(1, "illegal -a value -- %s", optarg);
284			nfsargsp->readahead = num;
285			nfsargsp->flags |= NFSMNT_READAHEAD;
286			break;
287		case 'b':
288			opflags |= BGRND;
289			break;
290		case 'c':
291			nfsargsp->flags |= NFSMNT_NOCONN;
292			break;
293		case 'D':
294			num = strtol(optarg, &p, 10);
295			if (*p || num <= 0)
296				errx(1, "illegal -D value -- %s", optarg);
297			nfsargsp->deadthresh = num;
298			nfsargsp->flags |= NFSMNT_DEADTHRESH;
299			break;
300		case 'd':
301			nfsargsp->flags |= NFSMNT_DUMBTIMR;
302			break;
303		case 'g':
304			num = strtol(optarg, &p, 10);
305			if (*p || num <= 0)
306				errx(1, "illegal -g value -- %s", optarg);
307#ifdef __FreeBSD__
308			set_rpc_maxgrouplist(num);
309#endif
310			nfsargsp->maxgrouplist = num;
311			nfsargsp->flags |= NFSMNT_MAXGRPS;
312			break;
313		case 'I':
314			num = strtol(optarg, &p, 10);
315			if (*p || num <= 0)
316				errx(1, "illegal -I value -- %s", optarg);
317			nfsargsp->readdirsize = num;
318			nfsargsp->flags |= NFSMNT_READDIRSIZE;
319			break;
320		case 'i':
321			nfsargsp->flags |= NFSMNT_INT;
322			break;
323#ifdef NFSKERB
324		case 'K':
325			nfsargsp->flags |= NFSMNT_KERB;
326			break;
327#endif
328		case 'L':
329			num = strtol(optarg, &p, 10);
330			if (*p || num < 2)
331				errx(1, "illegal -L value -- %s", optarg);
332			nfsargsp->leaseterm = num;
333			nfsargsp->flags |= NFSMNT_LEASETERM;
334			break;
335		case 'l':
336			nfsargsp->flags |= NFSMNT_RDIRPLUS;
337			break;
338#ifdef NFSKERB
339		case 'm':
340			(void)strncpy(realm, optarg, REALM_SZ - 1);
341			realm[REALM_SZ - 1] = '\0';
342			break;
343#endif
344		case 'o':
345			altflags = 0;
346			setflags(&altflags, &nfsargsp->flags, TRUE);
347			if (mountmode == V2)
348				altflags |= ALTF_NFSV2;
349			else if (mountmode == V3)
350				altflags |= ALTF_NFSV3;
351			getmntopts(optarg, mopts, &mntflags, &altflags);
352			setflags(&altflags, &nfsargsp->flags, FALSE);
353			/*
354			 * Handle altflags which don't map directly to
355			 * mount flags.
356			 */
357			if(altflags & ALTF_BG)
358				opflags |= BGRND;
359			if(altflags & ALTF_MNTUDP)
360				mnttcp_ok = 0;
361#ifdef ISO
362			if(altflags & ALTF_SEQPACKET)
363				nfsargsp->sotype = SOCK_SEQPACKET;
364#endif
365			if(altflags & ALTF_TCP) {
366				nfsargsp->sotype = SOCK_STREAM;
367				nfsproto = IPPROTO_TCP;
368			}
369			if(altflags & ALTF_PORT)
370				port_no = atoi(strstr(optarg, "port=") + 5);
371			mountmode = ANY;
372			if(altflags & ALTF_NFSV2)
373				mountmode = V2;
374			if(altflags & ALTF_NFSV3)
375				mountmode = V3;
376			break;
377		case 'P':
378			nfsargsp->flags |= NFSMNT_RESVPORT;
379			break;
380#ifdef ISO
381		case 'p':
382			nfsargsp->sotype = SOCK_SEQPACKET;
383			break;
384#endif
385		case 'q':
386			mountmode = V3;
387			nfsargsp->flags |= NFSMNT_NQNFS;
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			retrycnt = num;
394			break;
395		case 'r':
396			num = strtol(optarg, &p, 10);
397			if (*p || num <= 0)
398				errx(1, "illegal -r value -- %s", optarg);
399			nfsargsp->rsize = num;
400			nfsargsp->flags |= NFSMNT_RSIZE;
401			break;
402		case 's':
403			nfsargsp->flags |= NFSMNT_SOFT;
404			break;
405		case 'T':
406			nfsargsp->sotype = SOCK_STREAM;
407			nfsproto = IPPROTO_TCP;
408			break;
409		case 't':
410			num = strtol(optarg, &p, 10);
411			if (*p || num <= 0)
412				errx(1, "illegal -t value -- %s", optarg);
413			nfsargsp->timeo = num;
414			nfsargsp->flags |= NFSMNT_TIMEO;
415			break;
416		case 'w':
417			num = strtol(optarg, &p, 10);
418			if (*p || num <= 0)
419				errx(1, "illegal -w value -- %s", optarg);
420			nfsargsp->wsize = num;
421			nfsargsp->flags |= NFSMNT_WSIZE;
422			break;
423		case 'x':
424			num = strtol(optarg, &p, 10);
425			if (*p || num <= 0)
426				errx(1, "illegal -x value -- %s", optarg);
427			nfsargsp->retrans = num;
428			nfsargsp->flags |= NFSMNT_RETRANS;
429			break;
430		case 'U':
431			mnttcp_ok = 0;
432			break;
433		default:
434			usage();
435			break;
436		}
437	argc -= optind;
438	argv += optind;
439
440	if (argc != 2) {
441		usage();
442		/* NOTREACHED */
443	}
444
445	spec = *argv++;
446	name = *argv;
447
448	if (!getnfsargs(spec, nfsargsp))
449		exit(1);
450
451#ifdef __FreeBSD__
452	error = getvfsbyname("nfs", &vfc);
453	if (error && vfsisloadable("nfs")) {
454		if(vfsload("nfs"))
455			err(EX_OSERR, "vfsload(nfs)");
456		endvfsent();	/* clear cache */
457		error = getvfsbyname("nfs", &vfc);
458	}
459	if (error)
460		errx(EX_OSERR, "nfs filesystem is not available");
461
462	if (mount(vfc.vfc_name, name, mntflags, nfsargsp))
463		err(1, "%s", name);
464#else
465	if (mount("nfs", name, mntflags, nfsargsp))
466		err(1, "%s", name);
467#endif
468	if (nfsargsp->flags & (NFSMNT_NQNFS | NFSMNT_KERB)) {
469		if ((opflags & ISBGRND) == 0) {
470			if (i = fork()) {
471				if (i == -1)
472					err(1, "nqnfs 1");
473				exit(0);
474			}
475			(void) setsid();
476			(void) close(STDIN_FILENO);
477			(void) close(STDOUT_FILENO);
478			(void) close(STDERR_FILENO);
479			(void) chdir("/");
480		}
481		openlog("mount_nfs:", LOG_PID, LOG_DAEMON);
482		nfssvc_flag = NFSSVC_MNTD;
483		ncd.ncd_dirp = name;
484		while (nfssvc(nfssvc_flag, (caddr_t)&ncd) < 0) {
485			if (errno != ENEEDAUTH) {
486				syslog(LOG_ERR, "nfssvc err %m");
487				continue;
488			}
489			nfssvc_flag =
490			    NFSSVC_MNTD | NFSSVC_GOTAUTH | NFSSVC_AUTHINFAIL;
491#ifdef NFSKERB
492			/*
493			 * Set up as ncd_authuid for the kerberos call.
494			 * Must set ruid to ncd_authuid and reset the
495			 * ticket name iff ncd_authuid is not the same
496			 * as last time, so that the right ticket file
497			 * is found.
498			 * Get the Kerberos credential structure so that
499			 * we have the seesion key and get a ticket for
500			 * this uid.
501			 * For more info see the IETF Draft "Authentication
502			 * in ONC RPC".
503			 */
504			if (ncd.ncd_authuid != last_ruid) {
505				char buf[512];
506				(void)sprintf(buf, "%s%d",
507					      TKT_ROOT, ncd.ncd_authuid);
508				krb_set_tkt_string(buf);
509				last_ruid = ncd.ncd_authuid;
510			}
511			setreuid(ncd.ncd_authuid, 0);
512			kret = krb_get_cred(NFS_KERBSRV, inst, realm, &kcr);
513			if (kret == RET_NOTKT) {
514		            kret = get_ad_tkt(NFS_KERBSRV, inst, realm,
515				DEFAULT_TKT_LIFE);
516			    if (kret == KSUCCESS)
517				kret = krb_get_cred(NFS_KERBSRV, inst, realm,
518				    &kcr);
519			}
520			if (kret == KSUCCESS)
521			    kret = krb_mk_req(&ktick.kt, NFS_KERBSRV, inst,
522				realm, 0);
523
524			/*
525			 * Fill in the AKN_FULLNAME authenticator and verfier.
526			 * Along with the Kerberos ticket, we need to build
527			 * the timestamp verifier and encrypt it in CBC mode.
528			 */
529			if (kret == KSUCCESS &&
530			    ktick.kt.length <= (RPCAUTH_MAXSIZ-3*NFSX_UNSIGNED)
531			    && gettimeofday(&ktv, (struct timezone *)0) == 0) {
532			    ncd.ncd_authtype = RPCAUTH_KERB4;
533			    ncd.ncd_authstr = (u_char *)&ktick;
534			    ncd.ncd_authlen = nfsm_rndup(ktick.kt.length) +
535				3 * NFSX_UNSIGNED;
536			    ncd.ncd_verfstr = (u_char *)&kverf;
537			    ncd.ncd_verflen = sizeof (kverf);
538			    memmove(ncd.ncd_key, kcr.session,
539				sizeof (kcr.session));
540			    kin.t1 = htonl(ktv.tv_sec);
541			    kin.t2 = htonl(ktv.tv_usec);
542			    kin.w1 = htonl(NFS_KERBTTL);
543			    kin.w2 = htonl(NFS_KERBTTL - 1);
544			    bzero((caddr_t)kivec, sizeof (kivec));
545
546			    /*
547			     * Encrypt kin in CBC mode using the session
548			     * key in kcr.
549			     */
550			    XXX
551
552			    /*
553			     * Finally, fill the timestamp verifier into the
554			     * authenticator and verifier.
555			     */
556			    ktick.kind = htonl(RPCAKN_FULLNAME);
557			    kverf.kind = htonl(RPCAKN_FULLNAME);
558			    NFS_KERBW1(ktick.kt) = kout.w1;
559			    ktick.kt.length = htonl(ktick.kt.length);
560			    kverf.verf.t1 = kout.t1;
561			    kverf.verf.t2 = kout.t2;
562			    kverf.verf.w2 = kout.w2;
563			    nfssvc_flag = NFSSVC_MNTD | NFSSVC_GOTAUTH;
564			}
565			setreuid(0, 0);
566#endif /* NFSKERB */
567		}
568	}
569	exit(0);
570}
571
572/*
573 * Return RPC_SUCCESS if server responds.
574 */
575enum clnt_stat
576pingnfsserver(addr, version, sotype)
577	struct sockaddr_in *addr;
578	int version;
579	int sotype;
580{
581	struct sockaddr_in sin;
582	int tport;
583	CLIENT *clp;
584	int so = RPC_ANYSOCK;
585	enum clnt_stat stat;
586	struct timeval pertry, try;
587
588	sin = *addr;
589
590	if ((tport = port_no ? port_no :
591	     pmap_getport(&sin, RPCPROG_NFS, version, nfsproto)) == 0) {
592		return rpc_createerr.cf_stat;
593	}
594
595	sin.sin_port = htons(tport);
596
597	pertry.tv_sec = 10;
598	pertry.tv_usec = 0;
599	if (sotype == SOCK_STREAM)
600		clp = clnttcp_create(&sin, RPCPROG_NFS, version,
601				     &so, 0, 0);
602	else
603		clp = clntudp_create(&sin, RPCPROG_NFS, version,
604				     pertry, &so);
605	if (clp == NULL)
606		return rpc_createerr.cf_stat;
607
608	try.tv_sec = 10;
609	try.tv_usec = 0;
610	stat = clnt_call(clp, NFSPROC_NULL,
611			 xdr_void, NULL, xdr_void, NULL, try);
612
613	clnt_destroy(clp);
614
615	return stat;
616}
617
618int
619getnfsargs(spec, nfsargsp)
620	char *spec;
621	struct nfs_args *nfsargsp;
622{
623	register CLIENT *clp;
624	struct hostent *hp;
625	static struct sockaddr_in saddr;
626#ifdef ISO
627	static struct sockaddr_iso isoaddr;
628	struct iso_addr *isop;
629	int isoflag = 0;
630#endif
631	struct timeval pertry, try;
632	enum clnt_stat clnt_stat;
633	int so = RPC_ANYSOCK, i, nfsvers, mntvers, orgcnt;
634	char *hostp, *delimp;
635#ifdef NFSKERB
636	char *cp;
637#endif
638	u_short tport;
639	static struct nfhret nfhret;
640	static char nam[MNAMELEN + 1];
641
642	strncpy(nam, spec, MNAMELEN);
643	nam[MNAMELEN] = '\0';
644	if ((delimp = strchr(spec, '@')) != NULL) {
645		hostp = delimp + 1;
646	} else if ((delimp = strchr(spec, ':')) != NULL) {
647		hostp = spec;
648		spec = delimp + 1;
649	} else {
650		warnx("no <host>:<dirpath> or <dirpath>@<host> spec");
651		return (0);
652	}
653	*delimp = '\0';
654	/*
655	 * DUMB!! Until the mount protocol works on iso transport, we must
656	 * supply both an iso and an inet address for the host.
657	 */
658#ifdef ISO
659	if (!strncmp(hostp, "iso=", 4)) {
660		u_short isoport;
661
662		hostp += 4;
663		isoflag++;
664		if ((delimp = strchr(hostp, '+')) == NULL) {
665			warnx("no iso+inet address");
666			return (0);
667		}
668		*delimp = '\0';
669		if ((isop = iso_addr(hostp)) == NULL) {
670			warnx("bad ISO address");
671			return (0);
672		}
673		memset(&isoaddr, 0, sizeof (isoaddr));
674		memmove(&isoaddr.siso_addr, isop, sizeof (struct iso_addr));
675		isoaddr.siso_len = sizeof (isoaddr);
676		isoaddr.siso_family = AF_ISO;
677		isoaddr.siso_tlen = 2;
678		isoport = htons(NFS_PORT);
679		memmove(TSEL(&isoaddr), &isoport, isoaddr.siso_tlen);
680		hostp = delimp + 1;
681	}
682#endif /* ISO */
683
684	/*
685	 * Handle an internet host address and reverse resolve it if
686	 * doing Kerberos.
687	 */
688	if (isdigit(*hostp)) {
689		if ((saddr.sin_addr.s_addr = inet_addr(hostp)) == -1) {
690			warnx("bad net address %s", hostp);
691			return (0);
692		}
693	} else if ((hp = gethostbyname(hostp)) != NULL)
694		memmove(&saddr.sin_addr, hp->h_addr, hp->h_length);
695	else {
696		warnx("can't get net id for host");
697		return (0);
698        }
699#ifdef NFSKERB
700	if ((nfsargsp->flags & NFSMNT_KERB)) {
701		if ((hp = gethostbyaddr((char *)&saddr.sin_addr.s_addr,
702		    sizeof (u_long), AF_INET)) == (struct hostent *)0) {
703			warnx("can't reverse resolve net address");
704			return (0);
705		}
706		memmove(&saddr.sin_addr, hp->h_addr, hp->h_length);
707		strncpy(inst, hp->h_name, INST_SZ);
708		inst[INST_SZ - 1] = '\0';
709		if (cp = strchr(inst, '.'))
710			*cp = '\0';
711	}
712#endif /* NFSKERB */
713
714	orgcnt = retrycnt;
715tryagain:
716	if (mountmode == ANY || mountmode == V3) {
717		nfsvers = 3;
718		mntvers = 3;
719		nfsargsp->flags |= NFSMNT_NFSV3;
720	} else {
721		nfsvers = 2;
722		mntvers = 1;
723		nfsargsp->flags &= ~NFSMNT_NFSV3;
724	}
725	nfhret.stat = EACCES;	/* Mark not yet successful */
726	while (retrycnt > 0) {
727		saddr.sin_family = AF_INET;
728		saddr.sin_port = htons(PMAPPORT);
729		if ((tport = port_no ? port_no :
730		     pmap_getport(&saddr, RPCPROG_NFS,
731		    		  nfsvers, nfsproto)) == 0) {
732			if ((opflags & ISBGRND) == 0)
733				clnt_pcreateerror("NFS Portmap");
734		} else {
735			/*
736			 * First ping the nfs server to see if it supports
737			 * the version of the protocol we want to use.
738			 */
739			clnt_stat = pingnfsserver(&saddr, nfsvers,
740						  nfsargsp->sotype);
741			if (clnt_stat == RPC_PROGVERSMISMATCH) {
742				if (mountmode == ANY) {
743					mountmode = V2;
744					goto tryagain;
745				} else {
746					errx(1, "Can't contact NFS server");
747				}
748			}
749			saddr.sin_port = 0;
750			pertry.tv_sec = 10;
751			pertry.tv_usec = 0;
752			if (mnttcp_ok && nfsargsp->sotype == SOCK_STREAM)
753			    clp = clnttcp_create(&saddr, RPCPROG_MNT, mntvers,
754				&so, 0, 0);
755			else
756			    clp = clntudp_create(&saddr, RPCPROG_MNT, mntvers,
757				pertry, &so);
758			if (clp == NULL) {
759				if ((opflags & ISBGRND) == 0)
760					clnt_pcreateerror("Cannot MNT RPC");
761			} else {
762				clp->cl_auth = authunix_create_default();
763				try.tv_sec = 10;
764				try.tv_usec = 0;
765				if (nfsargsp->flags & NFSMNT_KERB)
766				    nfhret.auth = RPCAUTH_KERB4;
767				else
768				    nfhret.auth = RPCAUTH_UNIX;
769				nfhret.vers = mntvers;
770				clnt_stat = clnt_call(clp, RPCMNT_MOUNT,
771				    xdr_dir, spec, xdr_fh, &nfhret, try);
772				if (clnt_stat != RPC_SUCCESS) {
773					if (clnt_stat == RPC_PROGVERSMISMATCH) {
774						if (mountmode == ANY) {
775							mountmode = V2;
776							goto tryagain;
777						} else {
778							errx(1, "%s",
779							     clnt_sperror(clp, "MNT RPC"));
780						}
781					}
782					if ((opflags & ISBGRND) == 0)
783						warnx("%s", clnt_sperror(clp,
784						    "bad MNT RPC"));
785				} else {
786					auth_destroy(clp->cl_auth);
787					clnt_destroy(clp);
788					retrycnt = 0;
789				}
790			}
791		}
792		if (--retrycnt > 0) {
793			if (opflags & BGRND) {
794				opflags &= ~BGRND;
795				if (i = fork()) {
796					if (i == -1)
797						err(1, "nqnfs 2");
798					exit(0);
799				}
800				(void) setsid();
801				(void) close(STDIN_FILENO);
802				(void) close(STDOUT_FILENO);
803				(void) close(STDERR_FILENO);
804				(void) chdir("/");
805				opflags |= ISBGRND;
806			}
807			sleep(60);
808		}
809	}
810	if (nfhret.stat) {
811		if (opflags & ISBGRND)
812			exit(1);
813		warnx("can't access %s: %s", spec, strerror(nfhret.stat));
814		return (0);
815	}
816	saddr.sin_port = htons(tport);
817#ifdef ISO
818	if (isoflag) {
819		nfsargsp->addr = (struct sockaddr *) &isoaddr;
820		nfsargsp->addrlen = sizeof (isoaddr);
821	} else
822#endif /* ISO */
823	{
824		nfsargsp->addr = (struct sockaddr *) &saddr;
825		nfsargsp->addrlen = sizeof (saddr);
826	}
827	nfsargsp->fh = nfhret.nfh;
828	nfsargsp->fhsize = nfhret.fhsize;
829	nfsargsp->hostname = nam;
830	return (1);
831}
832
833/*
834 * xdr routines for mount rpc's
835 */
836int
837xdr_dir(xdrsp, dirp)
838	XDR *xdrsp;
839	char *dirp;
840{
841	return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
842}
843
844int
845xdr_fh(xdrsp, np)
846	XDR *xdrsp;
847	register struct nfhret *np;
848{
849	register int i;
850	long auth, authcnt, authfnd = 0;
851
852	if (!xdr_u_long(xdrsp, &np->stat))
853		return (0);
854	if (np->stat)
855		return (1);
856	switch (np->vers) {
857	case 1:
858		np->fhsize = NFSX_V2FH;
859		return (xdr_opaque(xdrsp, (caddr_t)np->nfh, NFSX_V2FH));
860	case 3:
861		if (!xdr_long(xdrsp, &np->fhsize))
862			return (0);
863		if (np->fhsize <= 0 || np->fhsize > NFSX_V3FHMAX)
864			return (0);
865		if (!xdr_opaque(xdrsp, (caddr_t)np->nfh, np->fhsize))
866			return (0);
867		if (!xdr_long(xdrsp, &authcnt))
868			return (0);
869		for (i = 0; i < authcnt; i++) {
870			if (!xdr_long(xdrsp, &auth))
871				return (0);
872			if (auth == np->auth)
873				authfnd++;
874		}
875		/*
876		 * Some servers, such as DEC's OSF/1 return a nil authenticator
877		 * list to indicate RPCAUTH_UNIX.
878		 */
879		if (!authfnd && (authcnt > 0 || np->auth != RPCAUTH_UNIX))
880			np->stat = EAUTH;
881		return (1);
882	};
883	return (0);
884}
885
886void
887usage()
888{
889	(void)fprintf(stderr, "\
890usage: mount_nfs [-23KPTUbcdilqs] [-D deadthresh] [-I readdirsize]\n\
891       [-L leaseterm] [-R retrycnt] [-a maxreadahead] [-g maxgroups]\n\
892       [-m realm] [-o options] [-r readsize] [-t timeout] [-w writesize]\n\
893       [-x retrans] rhost:path node\n");
894	exit(1);
895}
896