mount_nfs.c revision 18286
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.3 (Berkeley) 3/27/94";
46*/
47static const char rcsid[] =
48	"$Id: mount_nfs.c,v 1.13 1996/05/13 17:43:06 wollman 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 <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#ifdef __FreeBSD__
95#define	ALTF_BG		0x1
96#define ALTF_NOCONN	0x2
97#define ALTF_DUMBTIMR	0x4
98#define ALTF_INTR	0x8
99#define ALTF_KERB	0x10
100#define ALTF_NFSV3	0x20
101#define ALTF_RDIRPLUS	0x40
102#define	ALTF_MNTUDP	0x80
103#define ALTF_RESVPORT	0x100
104#define ALTF_SEQPACKET	0x200
105#define ALTF_NQNFS	0x400
106#define ALTF_SOFT	0x800
107#define ALTF_TCP	0x1000
108#define ALTF_PORT	0x2000
109
110struct mntopt mopts[] = {
111	MOPT_STDOPTS,
112	MOPT_FORCE,
113	MOPT_UPDATE,
114	{ "bg", 0, ALTF_BG, 1 },
115	{ "conn", 1, ALTF_NOCONN, 1 },
116	{ "dumbtimer", 0, ALTF_DUMBTIMR, 1 },
117	{ "intr", 0, ALTF_INTR, 1 },
118#ifdef NFSKERB
119	{ "kerb", 0, ALTF_KERB, 1 },
120#endif
121	{ "nfsv3", 0, ALTF_NFSV3, 1 },
122	{ "rdirplus", 0, ALTF_RDIRPLUS, 1 },
123	{ "mntudp", 0, ALTF_MNTUDP, 1 },
124	{ "resvport", 0, ALTF_RESVPORT, 1 },
125#ifdef ISO
126	{ "seqpacket", 0, ALTF_SEQPACKET, 1 },
127#endif
128	{ "nqnfs", 0, ALTF_NQNFS, 1 },
129	{ "soft", 0, ALTF_SOFT, 1 },
130	{ "tcp", 0, ALTF_TCP, 1 },
131	{ "port=", 0, ALTF_PORT, 1 },
132	{ NULL }
133};
134#else
135struct mntopt mopts[] = {
136	MOPT_STDOPTS,
137	MOPT_FORCE,
138	MOPT_UPDATE,
139	{ NULL }
140};
141#endif
142
143struct nfs_args nfsdefargs = {
144	(struct sockaddr *)0,
145	sizeof (struct sockaddr_in),
146	SOCK_DGRAM,
147	0,
148	(u_char *)0,
149	0,
150	0,
151	NFS_WSIZE,
152	NFS_RSIZE,
153	NFS_READDIRSIZE,
154	10,
155	NFS_RETRANS,
156	NFS_MAXGRPS,
157	NFS_DEFRAHEAD,
158	NQ_DEFLEASE,
159	NQ_DEADTHRESH,
160	(char *)0,
161};
162
163struct nfhret {
164	u_long		stat;
165	long		vers;
166	long		auth;
167	long		fhsize;
168	u_char		nfh[NFSX_V3FHMAX];
169};
170#define	DEF_RETRY	10000
171#define	BGRND	1
172#define	ISBGRND	2
173int retrycnt = DEF_RETRY;
174int opflags = 0;
175int nfsproto = IPPROTO_UDP;
176int mnttcp_ok = 1;
177u_short port_no = 0;
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
203int
204main(argc, argv)
205	int argc;
206	char *argv[];
207{
208	register int c;
209	register struct nfs_args *nfsargsp;
210	struct nfs_args nfsargs;
211	struct nfsd_cargs ncd;
212	int mntflags, altflags, i, nfssvc_flag, num;
213	char *name, *p, *spec;
214	struct vfsconf *vfc;
215#ifdef NFSKERB
216	uid_t last_ruid;
217
218	last_ruid = -1;
219	(void)strcpy(realm, KRB_REALM);
220	if (sizeof (struct nfsrpc_nickverf) != RPCX_NICKVERF ||
221	    sizeof (struct nfsrpc_fullblock) != RPCX_FULLBLOCK ||
222	    ((char *)&ktick.kt) - ((char *)&ktick) != NFSX_UNSIGNED ||
223	    ((char *)ktick.kt.dat) - ((char *)&ktick) != 2 * NFSX_UNSIGNED)
224		fprintf(stderr, "Yikes! NFSKERB structs not packed!!\n");
225#endif /* NFSKERB */
226	retrycnt = DEF_RETRY;
227
228	mntflags = 0;
229	altflags = 0;
230	nfsargs = nfsdefargs;
231	nfsargsp = &nfsargs;
232	while ((c = getopt(argc, argv,
233	    "3a:bcdD:g:I:iKL:lm:o:PpqR:r:sTt:w:x:U")) != EOF)
234		switch (c) {
235		case '3':
236			nfsargsp->flags |= NFSMNT_NFSV3;
237			break;
238		case 'a':
239			num = strtol(optarg, &p, 10);
240			if (*p || num < 0)
241				errx(1, "illegal -a value -- %s", optarg);
242			nfsargsp->readahead = num;
243			nfsargsp->flags |= NFSMNT_READAHEAD;
244			break;
245		case 'b':
246			opflags |= BGRND;
247			break;
248		case 'c':
249			nfsargsp->flags |= NFSMNT_NOCONN;
250			break;
251		case 'D':
252			num = strtol(optarg, &p, 10);
253			if (*p || num <= 0)
254				errx(1, "illegal -D value -- %s", optarg);
255			nfsargsp->deadthresh = num;
256			nfsargsp->flags |= NFSMNT_DEADTHRESH;
257			break;
258		case 'd':
259			nfsargsp->flags |= NFSMNT_DUMBTIMR;
260			break;
261		case 'g':
262			num = strtol(optarg, &p, 10);
263			if (*p || num <= 0)
264				errx(1, "illegal -g value -- %s", optarg);
265#ifdef __FreeBSD__
266			set_rpc_maxgrouplist(num);
267#endif
268			nfsargsp->maxgrouplist = num;
269			nfsargsp->flags |= NFSMNT_MAXGRPS;
270			break;
271		case 'I':
272			num = strtol(optarg, &p, 10);
273			if (*p || num <= 0)
274				errx(1, "illegal -I value -- %s", optarg);
275			nfsargsp->readdirsize = num;
276			nfsargsp->flags |= NFSMNT_READDIRSIZE;
277			break;
278		case 'i':
279			nfsargsp->flags |= NFSMNT_INT;
280			break;
281#ifdef NFSKERB
282		case 'K':
283			nfsargsp->flags |= NFSMNT_KERB;
284			break;
285#endif
286		case 'L':
287			num = strtol(optarg, &p, 10);
288			if (*p || num < 2)
289				errx(1, "illegal -L value -- %s", optarg);
290			nfsargsp->leaseterm = num;
291			nfsargsp->flags |= NFSMNT_LEASETERM;
292			break;
293		case 'l':
294			nfsargsp->flags |= NFSMNT_RDIRPLUS;
295			break;
296#ifdef NFSKERB
297		case 'm':
298			(void)strncpy(realm, optarg, REALM_SZ - 1);
299			realm[REALM_SZ - 1] = '\0';
300			break;
301#endif
302		case 'o':
303#ifdef __FreeBSD__
304			getmntopts(optarg, mopts, &mntflags, &altflags);
305			if(altflags & ALTF_BG)
306				opflags |= BGRND;
307			if(altflags & ALTF_NOCONN)
308				nfsargsp->flags |= NFSMNT_NOCONN;
309			if(altflags & ALTF_DUMBTIMR)
310				nfsargsp->flags |= NFSMNT_DUMBTIMR;
311			if(altflags & ALTF_INTR)
312				nfsargsp->flags |= NFSMNT_INT;
313#ifdef NFSKERB
314			if(altflags & ALTF_KERB)
315				nfsargsp->flags |= NFSMNT_KERB;
316#endif
317			if(altflags & ALTF_NFSV3)
318				nfsargsp->flags |= NFSMNT_NFSV3;
319			if(altflags & ALTF_RDIRPLUS)
320				nfsargsp->flags |= NFSMNT_RDIRPLUS;
321			if(altflags & ALTF_MNTUDP)
322				mnttcp_ok = 0;
323			if(altflags & ALTF_RESVPORT)
324				nfsargsp->flags |= NFSMNT_RESVPORT;
325#ifdef ISO
326			if(altflags & ALTF_SEQPACKET)
327				nfsargsp->sotype = SOCK_SEQPACKET;
328#endif
329			if(altflags & ALTF_NQNFS)
330				nfsargsp->flags |= (NFSMNT_NQNFS|NFSMNT_NFSV3);
331			if(altflags & ALTF_SOFT)
332				nfsargsp->flags |= NFSMNT_SOFT;
333			if(altflags & ALTF_TCP) {
334				nfsargsp->sotype = SOCK_STREAM;
335				nfsproto = IPPROTO_TCP;
336			}
337			if(altflags & ALTF_PORT)
338				port_no = atoi(strstr(optarg, "port=") + 5);
339			altflags = 0;
340#else
341			getmntopts(optarg, mopts, &mntflags);
342#endif
343			break;
344		case 'P':
345			nfsargsp->flags |= NFSMNT_RESVPORT;
346			break;
347#ifdef ISO
348		case 'p':
349			nfsargsp->sotype = SOCK_SEQPACKET;
350			break;
351#endif
352		case 'q':
353			nfsargsp->flags |= (NFSMNT_NQNFS | NFSMNT_NFSV3);
354			break;
355		case 'R':
356			num = strtol(optarg, &p, 10);
357			if (*p || num <= 0)
358				errx(1, "illegal -R value -- %s", optarg);
359			retrycnt = num;
360			break;
361		case 'r':
362			num = strtol(optarg, &p, 10);
363			if (*p || num <= 0)
364				errx(1, "illegal -r value -- %s", optarg);
365			nfsargsp->rsize = num;
366			nfsargsp->flags |= NFSMNT_RSIZE;
367			break;
368		case 's':
369			nfsargsp->flags |= NFSMNT_SOFT;
370			break;
371		case 'T':
372			nfsargsp->sotype = SOCK_STREAM;
373			nfsproto = IPPROTO_TCP;
374			break;
375		case 't':
376			num = strtol(optarg, &p, 10);
377			if (*p || num <= 0)
378				errx(1, "illegal -t value -- %s", optarg);
379			nfsargsp->timeo = num;
380			nfsargsp->flags |= NFSMNT_TIMEO;
381			break;
382		case 'w':
383			num = strtol(optarg, &p, 10);
384			if (*p || num <= 0)
385				errx(1, "illegal -w value -- %s", optarg);
386			nfsargsp->wsize = num;
387			nfsargsp->flags |= NFSMNT_WSIZE;
388			break;
389		case 'x':
390			num = strtol(optarg, &p, 10);
391			if (*p || num <= 0)
392				errx(1, "illegal -x value -- %s", optarg);
393			nfsargsp->retrans = num;
394			nfsargsp->flags |= NFSMNT_RETRANS;
395			break;
396		case 'U':
397			mnttcp_ok = 0;
398			break;
399		default:
400			usage();
401			break;
402		}
403	argc -= optind;
404	argv += optind;
405
406	if (argc != 2)
407		usage();
408
409	spec = *argv++;
410	name = *argv;
411
412	if (!getnfsargs(spec, nfsargsp))
413		exit(1);
414
415#ifdef __FreeBSD__
416	vfc = getvfsbyname("nfs");
417	if(!vfc && vfsisloadable("nfs")) {
418		if(vfsload("nfs"))
419			err(EX_OSERR, "vfsload(nfs)");
420		endvfsent();	/* flush cache */
421		vfc = getvfsbyname("nfs");
422	}
423	if (!vfc)
424		errx(EX_OSERR, "nfs filesystem is not loadable");
425
426	if (mount(vfc->vfc_index, name, mntflags, nfsargsp))
427#else
428	if (mount(MOUNT_NFS, name, mntflags, nfsargsp))
429#endif
430		err(1, "%s", name);
431	if (nfsargsp->flags & (NFSMNT_NQNFS | NFSMNT_KERB)) {
432		if ((opflags & ISBGRND) == 0) {
433			if (i = fork()) {
434				if (i == -1)
435					err(1, "nqnfs 1");
436				exit(0);
437			}
438			(void) setsid();
439			(void) close(STDIN_FILENO);
440			(void) close(STDOUT_FILENO);
441			(void) close(STDERR_FILENO);
442			(void) chdir("/");
443		}
444		openlog("mount_nfs:", LOG_PID, LOG_DAEMON);
445		nfssvc_flag = NFSSVC_MNTD;
446		ncd.ncd_dirp = name;
447		while (nfssvc(nfssvc_flag, (caddr_t)&ncd) < 0) {
448			if (errno != ENEEDAUTH) {
449				syslog(LOG_ERR, "nfssvc err %m");
450				continue;
451			}
452			nfssvc_flag =
453			    NFSSVC_MNTD | NFSSVC_GOTAUTH | NFSSVC_AUTHINFAIL;
454#ifdef NFSKERB
455			/*
456			 * Set up as ncd_authuid for the kerberos call.
457			 * Must set ruid to ncd_authuid and reset the
458			 * ticket name iff ncd_authuid is not the same
459			 * as last time, so that the right ticket file
460			 * is found.
461			 * Get the Kerberos credential structure so that
462			 * we have the seesion key and get a ticket for
463			 * this uid.
464			 * For more info see the IETF Draft "Authentication
465			 * in ONC RPC".
466			 */
467			if (ncd.ncd_authuid != last_ruid) {
468				char buf[512];
469				(void)sprintf(buf, "%s%d",
470					      TKT_ROOT, ncd.ncd_authuid);
471				krb_set_tkt_string(buf);
472				last_ruid = ncd.ncd_authuid;
473			}
474			setreuid(ncd.ncd_authuid, 0);
475			kret = krb_get_cred(NFS_KERBSRV, inst, realm, &kcr);
476			if (kret == RET_NOTKT) {
477		            kret = get_ad_tkt(NFS_KERBSRV, inst, realm,
478				DEFAULT_TKT_LIFE);
479			    if (kret == KSUCCESS)
480				kret = krb_get_cred(NFS_KERBSRV, inst, realm,
481				    &kcr);
482			}
483			if (kret == KSUCCESS)
484			    kret = krb_mk_req(&ktick.kt, NFS_KERBSRV, inst,
485				realm, 0);
486
487			/*
488			 * Fill in the AKN_FULLNAME authenticator and verfier.
489			 * Along with the Kerberos ticket, we need to build
490			 * the timestamp verifier and encrypt it in CBC mode.
491			 */
492			if (kret == KSUCCESS &&
493			    ktick.kt.length <= (RPCAUTH_MAXSIZ-3*NFSX_UNSIGNED)
494			    && gettimeofday(&ktv, (struct timezone *)0) == 0) {
495			    ncd.ncd_authtype = RPCAUTH_KERB4;
496			    ncd.ncd_authstr = (u_char *)&ktick;
497			    ncd.ncd_authlen = nfsm_rndup(ktick.kt.length) +
498				3 * NFSX_UNSIGNED;
499			    ncd.ncd_verfstr = (u_char *)&kverf;
500			    ncd.ncd_verflen = sizeof (kverf);
501			    bcopy((caddr_t)kcr.session, (caddr_t)ncd.ncd_key,
502				sizeof (kcr.session));
503			    kin.t1 = htonl(ktv.tv_sec);
504			    kin.t2 = htonl(ktv.tv_usec);
505			    kin.w1 = htonl(NFS_KERBTTL);
506			    kin.w2 = htonl(NFS_KERBTTL - 1);
507			    bzero((caddr_t)kivec, sizeof (kivec));
508
509			    /*
510			     * Encrypt kin in CBC mode using the session
511			     * key in kcr.
512			     */
513			    XXX
514
515			    /*
516			     * Finally, fill the timestamp verifier into the
517			     * authenticator and verifier.
518			     */
519			    ktick.kind = htonl(RPCAKN_FULLNAME);
520			    kverf.kind = htonl(RPCAKN_FULLNAME);
521			    NFS_KERBW1(ktick.kt) = kout.w1;
522			    ktick.kt.length = htonl(ktick.kt.length);
523			    kverf.verf.t1 = kout.t1;
524			    kverf.verf.t2 = kout.t2;
525			    kverf.verf.w2 = kout.w2;
526			    nfssvc_flag = NFSSVC_MNTD | NFSSVC_GOTAUTH;
527			}
528			setreuid(0, 0);
529#endif /* NFSKERB */
530		}
531	}
532	exit(0);
533}
534
535int
536getnfsargs(spec, nfsargsp)
537	char *spec;
538	struct nfs_args *nfsargsp;
539{
540	register CLIENT *clp;
541	struct hostent *hp;
542	static struct sockaddr_in saddr;
543#ifdef ISO
544	static struct sockaddr_iso isoaddr;
545	struct iso_addr *isop;
546	int isoflag = 0;
547#endif
548	struct timeval pertry, try;
549	enum clnt_stat clnt_stat;
550	int so = RPC_ANYSOCK, i, nfsvers, mntvers;
551	char *hostp, *delimp;
552#ifdef NFSKERB
553	char *cp;
554#endif
555	u_short tport;
556	static struct nfhret nfhret;
557	static char nam[MNAMELEN + 1];
558
559	strncpy(nam, spec, MNAMELEN);
560	nam[MNAMELEN] = '\0';
561	if ((delimp = strchr(spec, '@')) != NULL) {
562		hostp = delimp + 1;
563	} else if ((delimp = strchr(spec, ':')) != NULL) {
564		hostp = spec;
565		spec = delimp + 1;
566	} else {
567		warnx("no <host>:<dirpath> or <dirpath>@<host> spec");
568		return (0);
569	}
570	*delimp = '\0';
571	/*
572	 * DUMB!! Until the mount protocol works on iso transport, we must
573	 * supply both an iso and an inet address for the host.
574	 */
575#ifdef ISO
576	if (!strncmp(hostp, "iso=", 4)) {
577		u_short isoport;
578
579		hostp += 4;
580		isoflag++;
581		if ((delimp = strchr(hostp, '+')) == NULL) {
582			warnx("no iso+inet address");
583			return (0);
584		}
585		*delimp = '\0';
586		if ((isop = iso_addr(hostp)) == NULL) {
587			warnx("bad ISO address");
588			return (0);
589		}
590		bzero((caddr_t)&isoaddr, sizeof (isoaddr));
591		bcopy((caddr_t)isop, (caddr_t)&isoaddr.siso_addr,
592			sizeof (struct iso_addr));
593		isoaddr.siso_len = sizeof (isoaddr);
594		isoaddr.siso_family = AF_ISO;
595		isoaddr.siso_tlen = 2;
596		isoport = htons(NFS_PORT);
597		bcopy((caddr_t)&isoport, TSEL(&isoaddr), isoaddr.siso_tlen);
598		hostp = delimp + 1;
599	}
600#endif /* ISO */
601
602	/*
603	 * Handle an internet host address and reverse resolve it if
604	 * doing Kerberos.
605	 */
606	if (isdigit(*hostp)) {
607		if ((saddr.sin_addr.s_addr = inet_addr(hostp)) == -1) {
608			warnx("bad net address %s", hostp);
609			return (0);
610		}
611	} else if ((hp = gethostbyname(hostp)) != NULL) {
612		bcopy(hp->h_addr, (caddr_t)&saddr.sin_addr, hp->h_length);
613	} else {
614		warnx("can't get net id for host");
615		return (0);
616        }
617#ifdef NFSKERB
618	if ((nfsargsp->flags & NFSMNT_KERB)) {
619		if ((hp = gethostbyaddr((char *)&saddr.sin_addr.s_addr,
620		    sizeof (u_long), AF_INET)) == (struct hostent *)0) {
621			warnx("can't reverse resolve net address");
622			return (0);
623		}
624		bcopy(hp->h_addr, (caddr_t)&saddr.sin_addr, hp->h_length);
625		strncpy(inst, hp->h_name, INST_SZ);
626		inst[INST_SZ - 1] = '\0';
627		if (cp = strchr(inst, '.'))
628			*cp = '\0';
629	}
630#endif /* NFSKERB */
631
632	if (nfsargsp->flags & NFSMNT_NFSV3) {
633		nfsvers = 3;
634		mntvers = 3;
635	} else {
636		nfsvers = 2;
637		mntvers = 1;
638	}
639	nfhret.stat = EACCES;	/* Mark not yet successful */
640	while (retrycnt > 0) {
641		saddr.sin_family = AF_INET;
642		saddr.sin_port = htons(PMAPPORT);
643		if ((tport = port_no ? port_no :
644		     pmap_getport(&saddr, RPCPROG_NFS,
645		    		  nfsvers, nfsproto)) == 0) {
646			if ((opflags & ISBGRND) == 0)
647				clnt_pcreateerror("NFS Portmap");
648		} else {
649			saddr.sin_port = 0;
650			pertry.tv_sec = 10;
651			pertry.tv_usec = 0;
652			if (mnttcp_ok && nfsargsp->sotype == SOCK_STREAM)
653			    clp = clnttcp_create(&saddr, RPCPROG_MNT, mntvers,
654				&so, 0, 0);
655			else
656			    clp = clntudp_create(&saddr, RPCPROG_MNT, mntvers,
657				pertry, &so);
658			if (clp == NULL) {
659				if ((opflags & ISBGRND) == 0)
660					clnt_pcreateerror("Cannot MNT RPC");
661			} else {
662				clp->cl_auth = authunix_create_default();
663				try.tv_sec = 10;
664				try.tv_usec = 0;
665				if (nfsargsp->flags & NFSMNT_KERB)
666				    nfhret.auth = RPCAUTH_KERB4;
667				else
668				    nfhret.auth = RPCAUTH_UNIX;
669				nfhret.vers = mntvers;
670				clnt_stat = clnt_call(clp, RPCMNT_MOUNT,
671				    xdr_dir, spec, xdr_fh, &nfhret, try);
672				if (clnt_stat != RPC_SUCCESS) {
673					if ((opflags & ISBGRND) == 0)
674						warnx("%s", clnt_sperror(clp,
675						    "bad MNT RPC"));
676				} else {
677					auth_destroy(clp->cl_auth);
678					clnt_destroy(clp);
679					retrycnt = 0;
680				}
681			}
682		}
683		if (--retrycnt > 0) {
684			if (opflags & BGRND) {
685				opflags &= ~BGRND;
686				if (i = fork()) {
687					if (i == -1)
688						err(1, "nqnfs 2");
689					exit(0);
690				}
691				(void) setsid();
692				(void) close(STDIN_FILENO);
693				(void) close(STDOUT_FILENO);
694				(void) close(STDERR_FILENO);
695				(void) chdir("/");
696				opflags |= ISBGRND;
697			}
698			sleep(60);
699		}
700	}
701	if (nfhret.stat) {
702		if (opflags & ISBGRND)
703			exit(1);
704		errno = nfhret.stat;
705		warn("can't access %s", spec);
706		return (0);
707	}
708	saddr.sin_port = htons(tport);
709#ifdef ISO
710	if (isoflag) {
711		nfsargsp->addr = (struct sockaddr *) &isoaddr;
712		nfsargsp->addrlen = sizeof (isoaddr);
713	} else
714#endif /* ISO */
715	{
716		nfsargsp->addr = (struct sockaddr *) &saddr;
717		nfsargsp->addrlen = sizeof (saddr);
718	}
719	nfsargsp->fh = nfhret.nfh;
720	nfsargsp->fhsize = nfhret.fhsize;
721	nfsargsp->hostname = nam;
722	return (1);
723}
724
725/*
726 * xdr routines for mount rpc's
727 */
728int
729xdr_dir(xdrsp, dirp)
730	XDR *xdrsp;
731	char *dirp;
732{
733	return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
734}
735
736int
737xdr_fh(xdrsp, np)
738	XDR *xdrsp;
739	register struct nfhret *np;
740{
741	register int i;
742	long auth, authcnt, authfnd = 0;
743
744	if (!xdr_u_long(xdrsp, &np->stat))
745		return (0);
746	if (np->stat)
747		return (1);
748	switch (np->vers) {
749	case 1:
750		np->fhsize = NFSX_V2FH;
751		return (xdr_opaque(xdrsp, (caddr_t)np->nfh, NFSX_V2FH));
752	case 3:
753		if (!xdr_long(xdrsp, &np->fhsize))
754			return (0);
755		if (np->fhsize <= 0 || np->fhsize > NFSX_V3FHMAX)
756			return (0);
757		if (!xdr_opaque(xdrsp, (caddr_t)np->nfh, np->fhsize))
758			return (0);
759		if (!xdr_long(xdrsp, &authcnt))
760			return (0);
761		for (i = 0; i < authcnt; i++) {
762			if (!xdr_long(xdrsp, &auth))
763				return (0);
764			if (auth == np->auth)
765				authfnd++;
766		}
767
768		/*
769		 * Some servers, such as DEC's OSF/1 return a nil authenticator
770		 * list to indicate RPCAUTH_UNIX.
771		 */
772		if (!authfnd && (authcnt > 0 || np->auth != RPCAUTH_UNIX))
773			np->stat = EAUTH;
774		return (1);
775	};
776	return (0);
777}
778
779void
780usage()
781{
782	(void)fprintf(stderr, "\
783usage: mount_nfs [-3KPTUbcdilqs] [-D deadthresh] [-I readdirsize]\n\
784       [-L leaseterm] [-R retrycnt] [-a maxreadahead] [-g maxgroups]\n\
785       [-m realm] [-o options] [-r readsize] [-t timeout] [-w writesize]\n\
786       [-x retrans] rhost:path node\n");
787	exit(1);
788}
789