mount_nfs.c revision 101270
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 const 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#if 0
45static char sccsid[] = "@(#)mount_nfs.c	8.11 (Berkeley) 5/4/95";
46#endif
47static const char rcsid[] =
48  "$FreeBSD: head/sbin/mount_nfs/mount_nfs.c 101270 2002-08-03 16:03:21Z mux $";
49#endif /* not lint */
50
51#include <sys/param.h>
52#include <sys/mount.h>
53#include <sys/socket.h>
54#include <sys/stat.h>
55#include <sys/syslog.h>
56
57#include <rpc/rpc.h>
58#include <rpc/pmap_clnt.h>
59#include <rpc/pmap_prot.h>
60
61#include <nfs/rpcv2.h>
62#include <nfs/nfsproto.h>
63#include <nfsclient/nfs.h>
64#include <nfsclient/nfsargs.h>
65
66#include <arpa/inet.h>
67
68#include <ctype.h>
69#include <err.h>
70#include <errno.h>
71#include <fcntl.h>
72#include <netdb.h>
73#include <stdio.h>
74#include <stdlib.h>
75#include <strings.h>
76#include <sysexits.h>
77#include <unistd.h>
78
79#include "mntopts.h"
80#include "mounttab.h"
81
82#define	ALTF_BG		0x1
83#define ALTF_NOCONN	0x2
84#define ALTF_DUMBTIMR	0x4
85#define ALTF_INTR	0x8
86#define ALTF_NFSV3	0x20
87#define ALTF_RDIRPLUS	0x40
88#define ALTF_MNTUDP	0x80
89#define ALTF_RESVPORT	0x100
90#define ALTF_SEQPACKET	0x200
91#define ALTF_SOFT	0x800
92#define ALTF_TCP	0x1000
93#define ALTF_PORT	0x2000
94#define ALTF_NFSV2	0x4000
95#define ALTF_ACREGMIN	0x8000
96#define ALTF_ACREGMAX	0x10000
97#define ALTF_ACDIRMIN	0x20000
98#define ALTF_ACDIRMAX	0x40000
99#define ALTF_NOLOCKD	0x80000
100
101struct mntopt mopts[] = {
102	MOPT_STDOPTS,
103	MOPT_FORCE,
104	MOPT_UPDATE,
105	MOPT_ASYNC,
106	{ "bg", 0, ALTF_BG, 1 },
107	{ "conn", 1, ALTF_NOCONN, 1 },
108	{ "dumbtimer", 0, ALTF_DUMBTIMR, 1 },
109	{ "intr", 0, ALTF_INTR, 1 },
110	{ "nfsv3", 0, ALTF_NFSV3, 1 },
111	{ "rdirplus", 0, ALTF_RDIRPLUS, 1 },
112	{ "mntudp", 0, ALTF_MNTUDP, 1 },
113	{ "resvport", 0, ALTF_RESVPORT, 1 },
114	{ "soft", 0, ALTF_SOFT, 1 },
115	{ "tcp", 0, ALTF_TCP, 1 },
116	{ "port=", 0, ALTF_PORT, 1 },
117	{ "nfsv2", 0, ALTF_NFSV2, 1 },
118	{ "acregmin=", 0, ALTF_ACREGMIN, 1 },
119	{ "acregmax=", 0, ALTF_ACREGMAX, 1 },
120	{ "acdirmin=", 0, ALTF_ACDIRMIN, 1 },
121	{ "acdirmax=", 0, ALTF_ACDIRMAX, 1 },
122	{ "lockd", 1, ALTF_NOLOCKD, 1 },
123	{ NULL }
124};
125
126struct nfs_args nfsdefargs = {
127	NFS_ARGSVERSION,
128	NULL,
129	sizeof (struct sockaddr_in),
130	SOCK_DGRAM,
131	0,
132	NULL,
133	0,
134	NFSMNT_RESVPORT,
135	NFS_WSIZE,
136	NFS_RSIZE,
137	NFS_READDIRSIZE,
138	10,
139	NFS_RETRANS,
140	NFS_MAXGRPS,
141	NFS_DEFRAHEAD,
142	0,			/* was: NQ_DEFLEASE */
143	NFS_MAXDEADTHRESH,	/* was: NQ_DEADTHRESH */
144	NULL,
145	/* args version 4 */
146	NFS_MINATTRTIMO,
147	NFS_MAXATTRTIMO,
148	NFS_MINDIRATTRTIMO,
149	NFS_MAXDIRATTRTIMO,
150};
151
152/* Table for af,sotype -> netid conversions. */
153struct nc_protos {
154	char *netid;
155	int af;
156	int sotype;
157} nc_protos[] = {
158	{"udp",		AF_INET,	SOCK_DGRAM},
159	{"tcp",		AF_INET,	SOCK_STREAM},
160	{"udp6",	AF_INET6,	SOCK_DGRAM},
161	{"tcp6",	AF_INET6,	SOCK_STREAM},
162	{NULL}
163};
164
165struct nfhret {
166	u_long		stat;
167	long		vers;
168	long		auth;
169	long		fhsize;
170	u_char		nfh[NFSX_V3FHMAX];
171};
172#define	BGRND	1
173#define	ISBGRND	2
174int retrycnt = -1;
175int opflags = 0;
176int nfsproto = IPPROTO_UDP;
177int mnttcp_ok = 1;
178char *portspec = NULL;	/* Server nfs port; NULL means look up via rpcbind. */
179enum mountmode {
180	ANY,
181	V2,
182	V3
183} mountmode = ANY;
184
185/* Return codes for nfs_tryproto. */
186enum tryret {
187	TRYRET_SUCCESS,
188	TRYRET_TIMEOUT,		/* No response received. */
189	TRYRET_REMOTEERR,	/* Error received from remote server. */
190	TRYRET_LOCALERR		/* Local failure. */
191};
192
193int	getnfsargs(char *, struct nfs_args *);
194/* void	set_rpc_maxgrouplist(int); */
195struct netconfig *getnetconf_cached(const char *netid);
196char	*netidbytype(int af, int sotype);
197void	usage(void) __dead2;
198int	xdr_dir(XDR *, char *);
199int	xdr_fh(XDR *, struct nfhret *);
200enum tryret nfs_tryproto(struct nfs_args *nfsargsp, struct addrinfo *ai,
201    char *hostp, char *spec, char **errstr);
202enum tryret returncode(enum clnt_stat stat, struct rpc_err *rpcerr);
203
204/*
205 * Used to set mount flags with getmntopts.  Call with dir=TRUE to
206 * initialize altflags from the current mount flags.  Call with
207 * dir=FALSE to update mount flags with the new value of altflags after
208 * the call to getmntopts.
209 */
210static void
211set_flags(int* altflags, int* nfsflags, int dir)
212{
213#define F2(af, nf)					\
214	if (dir) {					\
215		if (*nfsflags & NFSMNT_##nf)		\
216			*altflags |= ALTF_##af;		\
217		else					\
218			*altflags &= ~ALTF_##af;	\
219	} else {					\
220		if (*altflags & ALTF_##af)		\
221			*nfsflags |= NFSMNT_##nf;	\
222		else					\
223			*nfsflags &= ~NFSMNT_##nf;	\
224	}
225#define F(f)	F2(f,f)
226
227	F(NOCONN);
228	F(DUMBTIMR);
229	F2(INTR, INT);
230	F(RDIRPLUS);
231	F(RESVPORT);
232	F(SOFT);
233	F(NOLOCKD);
234
235#undef F
236#undef F2
237}
238
239int
240main(argc, argv)
241	int argc;
242	char *argv[];
243{
244	int c;
245	struct nfs_args *nfsargsp;
246	struct nfs_args nfsargs;
247	int mntflags, altflags, nfssvc_flag, num;
248	char *name, *p, *spec;
249	char mntpath[MAXPATHLEN];
250
251	mntflags = 0;
252	altflags = 0;
253	nfsargs = nfsdefargs;
254	nfsargsp = &nfsargs;
255	while ((c = getopt(argc, argv,
256	    "23a:bcdD:g:I:iLl:No:PpR:r:sTt:w:x:U")) != -1)
257		switch (c) {
258		case '2':
259			mountmode = V2;
260			break;
261		case '3':
262			mountmode = V3;
263			break;
264		case 'a':
265			num = strtol(optarg, &p, 10);
266			if (*p || num < 0)
267				errx(1, "illegal -a value -- %s", optarg);
268			nfsargsp->readahead = num;
269			nfsargsp->flags |= NFSMNT_READAHEAD;
270			break;
271		case 'b':
272			opflags |= BGRND;
273			break;
274		case 'c':
275			nfsargsp->flags |= NFSMNT_NOCONN;
276			break;
277		case 'D':
278			num = strtol(optarg, &p, 10);
279			if (*p || num <= 0)
280				errx(1, "illegal -D value -- %s", optarg);
281			nfsargsp->deadthresh = num;
282			nfsargsp->flags |= NFSMNT_DEADTHRESH;
283			break;
284		case 'd':
285			nfsargsp->flags |= NFSMNT_DUMBTIMR;
286			break;
287#if 0 /* XXXX */
288		case 'g':
289			num = strtol(optarg, &p, 10);
290			if (*p || num <= 0)
291				errx(1, "illegal -g value -- %s", optarg);
292			set_rpc_maxgrouplist(num);
293			nfsargsp->maxgrouplist = num;
294			nfsargsp->flags |= NFSMNT_MAXGRPS;
295			break;
296#endif
297		case 'I':
298			num = strtol(optarg, &p, 10);
299			if (*p || num <= 0)
300				errx(1, "illegal -I value -- %s", optarg);
301			nfsargsp->readdirsize = num;
302			nfsargsp->flags |= NFSMNT_READDIRSIZE;
303			break;
304		case 'i':
305			nfsargsp->flags |= NFSMNT_INT;
306			break;
307		case 'L':
308			nfsargsp->flags |= NFSMNT_NOLOCKD;
309			break;
310		case 'l':
311			nfsargsp->flags |= NFSMNT_RDIRPLUS;
312			break;
313		case 'N':
314			nfsargsp->flags &= ~NFSMNT_RESVPORT;
315			break;
316		case 'o':
317			altflags = 0;
318			set_flags(&altflags, &nfsargsp->flags, TRUE);
319			if (mountmode == V2)
320				altflags |= ALTF_NFSV2;
321			else if (mountmode == V3)
322				altflags |= ALTF_NFSV3;
323			getmntopts(optarg, mopts, &mntflags, &altflags);
324			set_flags(&altflags, &nfsargsp->flags, FALSE);
325			/*
326			 * Handle altflags which don't map directly to
327			 * mount flags.
328			 */
329			if(altflags & ALTF_BG)
330				opflags |= BGRND;
331			if(altflags & ALTF_MNTUDP)
332				mnttcp_ok = 0;
333			if(altflags & ALTF_TCP) {
334				nfsargsp->sotype = SOCK_STREAM;
335				nfsproto = IPPROTO_TCP;
336			}
337			if(altflags & ALTF_PORT) {
338				/*
339				 * XXX Converting from a string to an int
340				 * and back again is silly, and we should
341				 * allow /etc/services names.
342				 */
343				asprintf(&portspec, "%d",
344				    atoi(strstr(optarg, "port=") + 5));
345				if (portspec == NULL)
346					err(1, "asprintf");
347			}
348			mountmode = ANY;
349			if(altflags & ALTF_NFSV2)
350				mountmode = V2;
351			if(altflags & ALTF_NFSV3)
352				mountmode = V3;
353			if(altflags & ALTF_ACREGMIN)
354				nfsargsp->acregmin = atoi(strstr(optarg,
355				    "acregmin=") + 9);
356			if(altflags & ALTF_ACREGMAX)
357				nfsargsp->acregmax = atoi(strstr(optarg,
358				    "acregmax=") + 9);
359			if(altflags & ALTF_ACDIRMIN)
360				nfsargsp->acdirmin = atoi(strstr(optarg,
361				    "acdirmin=") + 9);
362			if(altflags & ALTF_ACDIRMAX)
363				nfsargsp->acdirmax = atoi(strstr(optarg,
364				    "acdirmax=") + 9);
365			break;
366		case 'P':
367			/* obsolete for NFSMNT_RESVPORT, now default */
368			break;
369		case 'R':
370			num = strtol(optarg, &p, 10);
371			if (*p || num < 0)
372				errx(1, "illegal -R value -- %s", optarg);
373			retrycnt = num;
374			break;
375		case 'r':
376			num = strtol(optarg, &p, 10);
377			if (*p || num <= 0)
378				errx(1, "illegal -r value -- %s", optarg);
379			nfsargsp->rsize = num;
380			nfsargsp->flags |= NFSMNT_RSIZE;
381			break;
382		case 's':
383			nfsargsp->flags |= NFSMNT_SOFT;
384			break;
385		case 'T':
386			nfsargsp->sotype = SOCK_STREAM;
387			nfsproto = IPPROTO_TCP;
388			break;
389		case 't':
390			num = strtol(optarg, &p, 10);
391			if (*p || num <= 0)
392				errx(1, "illegal -t value -- %s", optarg);
393			nfsargsp->timeo = num;
394			nfsargsp->flags |= NFSMNT_TIMEO;
395			break;
396		case 'w':
397			num = strtol(optarg, &p, 10);
398			if (*p || num <= 0)
399				errx(1, "illegal -w value -- %s", optarg);
400			nfsargsp->wsize = num;
401			nfsargsp->flags |= NFSMNT_WSIZE;
402			break;
403		case 'x':
404			num = strtol(optarg, &p, 10);
405			if (*p || num <= 0)
406				errx(1, "illegal -x value -- %s", optarg);
407			nfsargsp->retrans = num;
408			nfsargsp->flags |= NFSMNT_RETRANS;
409			break;
410		case 'U':
411			mnttcp_ok = 0;
412			break;
413		default:
414			usage();
415			break;
416		}
417	argc -= optind;
418	argv += optind;
419
420	if (argc != 2) {
421		usage();
422		/* NOTREACHED */
423	}
424
425	spec = *argv++;
426	name = *argv;
427
428	if (retrycnt == -1)
429		/* The default is to keep retrying forever. */
430		retrycnt = 0;
431	if (!getnfsargs(spec, nfsargsp))
432		exit(1);
433
434	/* resolve the mountpoint with realpath(3) */
435	(void)checkpath(name, mntpath);
436
437	if (mount("nfs", mntpath, mntflags, nfsargsp))
438		err(1, "%s", mntpath);
439
440	exit(0);
441}
442
443int
444getnfsargs(spec, nfsargsp)
445	char *spec;
446	struct nfs_args *nfsargsp;
447{
448	struct addrinfo hints, *ai_nfs, *ai;
449	enum tryret ret;
450	int ecode, speclen, remoteerr;
451	char *hostp, *delimp, *errstr;
452	size_t len;
453	static char nam[MNAMELEN + 1];
454
455	if ((delimp = strrchr(spec, ':')) != NULL) {
456		hostp = spec;
457		spec = delimp + 1;
458	} else if ((delimp = strrchr(spec, '@')) != NULL) {
459		warnx("path@server syntax is deprecated, use server:path");
460		hostp = delimp + 1;
461	} else {
462		warnx("no <host>:<dirpath> nfs-name");
463		return (0);
464	}
465	*delimp = '\0';
466
467	/*
468	 * If there has been a trailing slash at mounttime it seems
469	 * that some mountd implementations fail to remove the mount
470	 * entries from their mountlist while unmounting.
471	 */
472	for (speclen = strlen(spec);
473		speclen > 1 && spec[speclen - 1] == '/';
474		speclen--)
475		spec[speclen - 1] = '\0';
476	if (strlen(hostp) + strlen(spec) + 1 > MNAMELEN) {
477		warnx("%s:%s: %s", hostp, spec, strerror(ENAMETOOLONG));
478		return (0);
479	}
480	/* Make both '@' and ':' notations equal */
481	if (*hostp != '\0') {
482		len = strlen(hostp);
483		memmove(nam, hostp, len);
484		nam[len] = ':';
485		memmove(nam + len + 1, spec, speclen);
486		nam[len + speclen + 1] = '\0';
487	}
488
489	/*
490	 * Handle an internet host address.
491	 */
492	memset(&hints, 0, sizeof hints);
493	hints.ai_flags = AI_NUMERICHOST;
494	hints.ai_socktype = nfsargsp->sotype;
495	if (getaddrinfo(hostp, portspec, &hints, &ai_nfs) != 0) {
496		hints.ai_flags = 0;
497		if ((ecode = getaddrinfo(hostp, portspec, &hints, &ai_nfs))
498		    != 0) {
499			if (portspec == NULL)
500				errx(1, "%s: %s", hostp, gai_strerror(ecode));
501			else
502				errx(1, "%s:%s: %s", hostp, portspec,
503				    gai_strerror(ecode));
504			return (0);
505		}
506	}
507
508	ret = TRYRET_LOCALERR;
509	for (;;) {
510		/*
511		 * Try each entry returned by getaddrinfo(). Note the
512		 * occurence of remote errors by setting `remoteerr'.
513		 */
514		remoteerr = 0;
515		for (ai = ai_nfs; ai != NULL; ai = ai->ai_next) {
516			ret = nfs_tryproto(nfsargsp, ai, hostp, spec, &errstr);
517			if (ret == TRYRET_SUCCESS)
518				break;
519			if (ret != TRYRET_LOCALERR)
520				remoteerr = 1;
521			if ((opflags & ISBGRND) == 0)
522				fprintf(stderr, "%s\n", errstr);
523		}
524		if (ret == TRYRET_SUCCESS)
525			break;
526
527		/* Exit if all errors were local. */
528		if (!remoteerr)
529			exit(1);
530
531		/*
532		 * If retrycnt == 0, we are to keep retrying forever.
533		 * Otherwise decrement it, and exit if it hits zero.
534		 */
535		if (retrycnt != 0 && --retrycnt == 0)
536			exit(1);
537
538		if ((opflags & (BGRND | ISBGRND)) == BGRND) {
539			warnx("Cannot immediately mount %s:%s, backgrounding",
540			    hostp, spec);
541			opflags |= ISBGRND;
542			if (daemon(0, 0) != 0)
543				err(1, "daemon");
544		}
545		sleep(60);
546	}
547	freeaddrinfo(ai_nfs);
548	nfsargsp->hostname = nam;
549	/* Add mounted filesystem to PATH_MOUNTTAB */
550	if (!add_mtab(hostp, spec))
551		warnx("can't update %s for %s:%s", PATH_MOUNTTAB, hostp, spec);
552	return (1);
553}
554
555/*
556 * Try to set up the NFS arguments according to the address
557 * family, protocol (and possibly port) specified in `ai'.
558 *
559 * Returns TRYRET_SUCCESS if successful, or:
560 *   TRYRET_TIMEOUT		The server did not respond.
561 *   TRYRET_REMOTEERR		The server reported an error.
562 *   TRYRET_LOCALERR		Local failure.
563 *
564 * In all error cases, *errstr will be set to a statically-allocated string
565 * describing the error.
566 */
567enum tryret
568nfs_tryproto(struct nfs_args *nfsargsp, struct addrinfo *ai, char *hostp,
569    char *spec, char **errstr)
570{
571	static char errbuf[256];
572	struct sockaddr_storage nfs_ss;
573	struct netbuf nfs_nb;
574	struct nfhret nfhret;
575	struct timeval try;
576	struct rpc_err rpcerr;
577	CLIENT *clp;
578	struct netconfig *nconf, *nconf_mnt;
579	char *netid, *netid_mnt;
580	int doconnect, nfsvers, mntvers;
581	enum clnt_stat stat;
582	enum mountmode trymntmode;
583
584	trymntmode = mountmode;
585	errbuf[0] = '\0';
586	*errstr = errbuf;
587
588	if ((netid = netidbytype(ai->ai_family, nfsargsp->sotype)) == NULL) {
589		snprintf(errbuf, sizeof errbuf,
590		    "af %d sotype %d not supported", ai->ai_family,
591		    nfsargsp->sotype);
592		return (TRYRET_LOCALERR);
593	}
594	if ((nconf = getnetconf_cached(netid)) == NULL) {
595		snprintf(errbuf, sizeof errbuf, "%s: %s", netid, nc_sperror());
596		return (TRYRET_LOCALERR);
597	}
598	/* The RPCPROG_MNT netid may be different. */
599	if (mnttcp_ok) {
600		netid_mnt = netid;
601		nconf_mnt = nconf;
602	} else {
603		if ((netid_mnt = netidbytype(ai->ai_family, SOCK_DGRAM))
604		     == NULL) {
605			snprintf(errbuf, sizeof errbuf,
606			    "af %d sotype SOCK_DGRAM not supported",
607			     ai->ai_family);
608			return (TRYRET_LOCALERR);
609		}
610		if ((nconf_mnt = getnetconf_cached(netid_mnt)) == NULL) {
611			snprintf(errbuf, sizeof errbuf, "%s: %s", netid_mnt,
612			    nc_sperror());
613			return (TRYRET_LOCALERR);
614		}
615	}
616
617tryagain:
618	if (trymntmode == V2) {
619		nfsvers = 2;
620		mntvers = 1;
621	} else {
622		nfsvers = 3;
623		mntvers = 3;
624	}
625
626	if (portspec != NULL) {
627		/* `ai' contains the complete nfsd sockaddr. */
628		nfs_nb.buf = ai->ai_addr;
629		nfs_nb.len = nfs_nb.maxlen = ai->ai_addrlen;
630	} else {
631		/* Ask the remote rpcbind. */
632		nfs_nb.buf = &nfs_ss;
633		nfs_nb.len = nfs_nb.maxlen = sizeof nfs_ss;
634
635		if (!rpcb_getaddr(RPCPROG_NFS, nfsvers, nconf, &nfs_nb,
636		    hostp)) {
637			if (rpc_createerr.cf_stat == RPC_PROGVERSMISMATCH &&
638			    trymntmode == ANY) {
639				trymntmode = V2;
640				goto tryagain;
641			}
642			snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s",
643			    netid, hostp, spec,
644			    clnt_spcreateerror("RPCPROG_NFS"));
645			return (returncode(rpc_createerr.cf_stat,
646			    &rpc_createerr.cf_error));
647		}
648	}
649
650	/* Check that the server (nfsd) responds on the port we have chosen. */
651	clp = clnt_tli_create(RPC_ANYFD, nconf, &nfs_nb, RPCPROG_NFS, nfsvers,
652	    0, 0);
653	if (clp == NULL) {
654		snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid,
655		    hostp, spec, clnt_spcreateerror("nfsd: RPCPROG_NFS"));
656		return (returncode(rpc_createerr.cf_stat,
657		    &rpc_createerr.cf_error));
658	}
659	if (nfsargsp->sotype == SOCK_DGRAM) {
660		/*
661		 * Use connect(), to match what the kernel does. This
662		 * catches cases where the server responds from the
663		 * wrong source address.
664		 */
665		doconnect = 1;
666		if (!clnt_control(clp, CLSET_CONNECT, (char *)&doconnect)) {
667			clnt_destroy(clp);
668			snprintf(errbuf, sizeof errbuf,
669			    "[%s] %s:%s: CLSET_CONNECT failed", netid, hostp,
670			    spec);
671			return (TRYRET_LOCALERR);
672		}
673	}
674
675	try.tv_sec = 10;
676	try.tv_usec = 0;
677	stat = clnt_call(clp, NFSPROC_NULL, xdr_void, NULL, xdr_void, NULL,
678	    try);
679	if (stat != RPC_SUCCESS) {
680		if (stat == RPC_PROGVERSMISMATCH && trymntmode == ANY) {
681			clnt_destroy(clp);
682			trymntmode = V2;
683			goto tryagain;
684		}
685		clnt_geterr(clp, &rpcerr);
686		snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid,
687		    hostp, spec, clnt_sperror(clp, "NFSPROC_NULL"));
688		clnt_destroy(clp);
689		return (returncode(stat, &rpcerr));
690	}
691	clnt_destroy(clp);
692
693	/* Send the RPCMNT_MOUNT RPC to get the root filehandle. */
694	try.tv_sec = 10;
695	try.tv_usec = 0;
696	clp = clnt_tp_create(hostp, RPCPROG_MNT, mntvers, nconf_mnt);
697	if (clp == NULL) {
698		snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid_mnt,
699		    hostp, spec, clnt_spcreateerror("RPCMNT: clnt_create"));
700		return (returncode(rpc_createerr.cf_stat,
701		    &rpc_createerr.cf_error));
702	}
703	clp->cl_auth = authsys_create_default();
704	nfhret.auth = RPCAUTH_UNIX;
705	nfhret.vers = mntvers;
706	stat = clnt_call(clp, RPCMNT_MOUNT, xdr_dir, spec, xdr_fh, &nfhret,
707	    try);
708	auth_destroy(clp->cl_auth);
709	if (stat != RPC_SUCCESS) {
710		if (stat == RPC_PROGVERSMISMATCH && trymntmode == ANY) {
711			clnt_destroy(clp);
712			trymntmode = V2;
713			goto tryagain;
714		}
715		clnt_geterr(clp, &rpcerr);
716		snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid_mnt,
717		    hostp, spec, clnt_sperror(clp, "RPCPROG_MNT"));
718		clnt_destroy(clp);
719		return (returncode(stat, &rpcerr));
720	}
721	clnt_destroy(clp);
722
723	if (nfhret.stat != 0) {
724		snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid_mnt,
725		    hostp, spec, strerror(nfhret.stat));
726		return (TRYRET_REMOTEERR);
727	}
728
729	/*
730	 * Store the filehandle and server address in nfsargsp, making
731	 * sure to copy any locally allocated structures.
732	 */
733	nfsargsp->addrlen = nfs_nb.len;
734	nfsargsp->addr = malloc(nfsargsp->addrlen);
735	nfsargsp->fhsize = nfhret.fhsize;
736	nfsargsp->fh = malloc(nfsargsp->fhsize);
737	if (nfsargsp->addr == NULL || nfsargsp->fh == NULL)
738		err(1, "malloc");
739	bcopy(nfs_nb.buf, nfsargsp->addr, nfsargsp->addrlen);
740	bcopy(nfhret.nfh, nfsargsp->fh, nfsargsp->fhsize);
741
742	if (nfsvers == 3)
743		nfsargsp->flags |= NFSMNT_NFSV3;
744	else
745		nfsargsp->flags &= ~NFSMNT_NFSV3;
746
747	return (TRYRET_SUCCESS);
748}
749
750
751/*
752 * Catagorise a RPC return status and error into an `enum tryret'
753 * return code.
754 */
755enum tryret
756returncode(enum clnt_stat stat, struct rpc_err *rpcerr)
757{
758	switch (stat) {
759	case RPC_TIMEDOUT:
760		return (TRYRET_TIMEOUT);
761	case RPC_PMAPFAILURE:
762	case RPC_PROGNOTREGISTERED:
763	case RPC_PROGVERSMISMATCH:
764	/* XXX, these can be local or remote. */
765	case RPC_CANTSEND:
766	case RPC_CANTRECV:
767		return (TRYRET_REMOTEERR);
768	case RPC_SYSTEMERROR:
769		switch (rpcerr->re_errno) {
770		case ETIMEDOUT:
771			return (TRYRET_TIMEOUT);
772		case ENOMEM:
773			break;
774		default:
775			return (TRYRET_REMOTEERR);
776		}
777		/* FALLTHROUGH */
778	default:
779		break;
780	}
781	return (TRYRET_LOCALERR);
782}
783
784/*
785 * Look up a netid based on an address family and socket type.
786 * `af' is the address family, and `sotype' is SOCK_DGRAM or SOCK_STREAM.
787 *
788 * XXX there should be a library function for this.
789 */
790char *
791netidbytype(int af, int sotype) {
792	struct nc_protos *p;
793
794	for (p = nc_protos; p->netid != NULL; p++) {
795		if (af != p->af || sotype != p->sotype)
796			continue;
797		return (p->netid);
798	}
799	return (NULL);
800}
801
802/*
803 * Look up a netconfig entry based on a netid, and cache the result so
804 * that we don't need to remember to call freenetconfigent().
805 *
806 * Otherwise it behaves just like getnetconfigent(), so nc_*error()
807 * work on failure.
808 */
809struct netconfig *
810getnetconf_cached(const char *netid) {
811	static struct nc_entry {
812		struct netconfig *nconf;
813		struct nc_entry *next;
814	} *head;
815	struct nc_entry *p;
816	struct netconfig *nconf;
817
818	for (p = head; p != NULL; p = p->next)
819		if (strcmp(netid, p->nconf->nc_netid) == 0)
820			return (p->nconf);
821
822	if ((nconf = getnetconfigent(netid)) == NULL)
823		return (NULL);
824	if ((p = malloc(sizeof(*p))) == NULL)
825		err(1, "malloc");
826	p->nconf = nconf;
827	p->next = head;
828	head = p;
829
830	return (p->nconf);
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	struct nfhret *np;
848{
849	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, "%s\n%s\n%s\n%s\n",
890"usage: mount_nfs [-23KNPTUbcdilqs] [-D deadthresh] [-I readdirsize]",
891"                 [-R retrycnt] [-a maxreadahead]",
892"                 [-g maxgroups] [-m realm] [-o options] [-r readsize]",
893"                 [-t timeout] [-w writesize] [-x retrans] rhost:path node");
894	exit(1);
895}
896