xref: /freebsd/sbin/mount_nfs/mount_nfs.c (revision 3f16af55)
18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
48fae3551SRodney W. Grimes  * Copyright (c) 1992, 1993, 1994
58fae3551SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
68fae3551SRodney W. Grimes  *
78fae3551SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
88fae3551SRodney W. Grimes  * Rick Macklem at The University of Guelph.
98fae3551SRodney W. Grimes  *
108fae3551SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
118fae3551SRodney W. Grimes  * modification, are permitted provided that the following conditions
128fae3551SRodney W. Grimes  * are met:
138fae3551SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
148fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
158fae3551SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
168fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
178fae3551SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
198fae3551SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
208fae3551SRodney W. Grimes  *    without specific prior written permission.
218fae3551SRodney W. Grimes  *
228fae3551SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
238fae3551SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
248fae3551SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
258fae3551SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
268fae3551SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
278fae3551SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
288fae3551SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
298fae3551SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
308fae3551SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
318fae3551SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
328fae3551SRodney W. Grimes  * SUCH DAMAGE.
338fae3551SRodney W. Grimes  */
348fae3551SRodney W. Grimes 
35c69284caSDavid E. O'Brien #if 0
368fae3551SRodney W. Grimes #ifndef lint
3746fc8f78SPhilippe Charnier static const char copyright[] =
388fae3551SRodney W. Grimes "@(#) Copyright (c) 1992, 1993, 1994\n\
398fae3551SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
408fae3551SRodney W. Grimes #endif /* not lint */
418fae3551SRodney W. Grimes 
428fae3551SRodney W. Grimes #ifndef lint
434a4c5285SPeter Wemm static char sccsid[] = "@(#)mount_nfs.c	8.11 (Berkeley) 5/4/95";
448fae3551SRodney W. Grimes #endif /* not lint */
45c69284caSDavid E. O'Brien #endif
46c69284caSDavid E. O'Brien #include <sys/cdefs.h>
47c69284caSDavid E. O'Brien __FBSDID("$FreeBSD$");
488fae3551SRodney W. Grimes 
498fae3551SRodney W. Grimes #include <sys/param.h>
50011981fdSRick Macklem #include <sys/linker.h>
51011981fdSRick Macklem #include <sys/module.h>
528fae3551SRodney W. Grimes #include <sys/mount.h>
538360efbdSAlfred Perlstein #include <sys/socket.h>
548fae3551SRodney W. Grimes #include <sys/stat.h>
558fae3551SRodney W. Grimes #include <sys/syslog.h>
56c5aa1dc8SCraig Rodrigues #include <sys/uio.h>
578fae3551SRodney W. Grimes 
588fae3551SRodney W. Grimes #include <rpc/rpc.h>
598fae3551SRodney W. Grimes #include <rpc/pmap_clnt.h>
608fae3551SRodney W. Grimes #include <rpc/pmap_prot.h>
610775314bSDoug Rabson #include <rpcsvc/nfs_prot.h>
620775314bSDoug Rabson #include <rpcsvc/mount.h>
638fae3551SRodney W. Grimes 
6456c049a0SRick Macklem #include <fs/nfs/nfsproto.h>
6556c049a0SRick Macklem #include <fs/nfs/nfsv4_errstr.h>
668fae3551SRodney W. Grimes 
678fae3551SRodney W. Grimes #include <arpa/inet.h>
688cd9718fSGleb Smirnoff #include <net/route.h>
698cd9718fSGleb Smirnoff #include <net/if.h>
708fae3551SRodney W. Grimes 
718fae3551SRodney W. Grimes #include <ctype.h>
728fae3551SRodney W. Grimes #include <err.h>
738fae3551SRodney W. Grimes #include <errno.h>
748360efbdSAlfred Perlstein #include <fcntl.h>
758fae3551SRodney W. Grimes #include <netdb.h>
768fae3551SRodney W. Grimes #include <stdio.h>
778fae3551SRodney W. Grimes #include <stdlib.h>
78c04affffSMatthew N. Dodd #include <string.h>
798fae3551SRodney W. Grimes #include <strings.h>
805e074e31SGarrett Wollman #include <sysexits.h>
818fae3551SRodney W. Grimes #include <unistd.h>
828fae3551SRodney W. Grimes 
838fae3551SRodney W. Grimes #include "mntopts.h"
84a69497d7SMatthew Dillon #include "mounttab.h"
858fae3551SRodney W. Grimes 
86a3d8f94bSIan Dowse /* Table for af,sotype -> netid conversions. */
876575d184SEdward Tomasz Napierala static struct nc_protos {
884b5bc283SCraig Rodrigues 	const char *netid;
89a3d8f94bSIan Dowse 	int af;
90a3d8f94bSIan Dowse 	int sotype;
91a3d8f94bSIan Dowse } nc_protos[] = {
92a3d8f94bSIan Dowse 	{"udp",		AF_INET,	SOCK_DGRAM},
93a3d8f94bSIan Dowse 	{"tcp",		AF_INET,	SOCK_STREAM},
94a3d8f94bSIan Dowse 	{"udp6",	AF_INET6,	SOCK_DGRAM},
95a3d8f94bSIan Dowse 	{"tcp6",	AF_INET6,	SOCK_STREAM},
96b535b298SCraig Rodrigues 	{NULL,		0,		0}
97a3d8f94bSIan Dowse };
98a3d8f94bSIan Dowse 
998fae3551SRodney W. Grimes struct nfhret {
1008fae3551SRodney W. Grimes 	u_long		stat;
101a62dc406SDoug Rabson 	long		vers;
102a62dc406SDoug Rabson 	long		auth;
103a62dc406SDoug Rabson 	long		fhsize;
1040775314bSDoug Rabson 	u_char		nfh[NFS3_FHSIZE];
1058fae3551SRodney W. Grimes };
1068fae3551SRodney W. Grimes #define	BGRND	1
1078fae3551SRodney W. Grimes #define	ISBGRND	2
108302f15f9SMatthew N. Dodd #define	OF_NOINET4	4
109302f15f9SMatthew N. Dodd #define	OF_NOINET6	8
1106575d184SEdward Tomasz Napierala static int retrycnt = -1;
1116575d184SEdward Tomasz Napierala static int opflags = 0;
1126575d184SEdward Tomasz Napierala static int nfsproto = IPPROTO_TCP;
1136575d184SEdward Tomasz Napierala static int mnttcp_ok = 1;
1146575d184SEdward Tomasz Napierala static int noconn = 0;
1156575d184SEdward Tomasz Napierala /* The 'portspec' is the server nfs port; NULL means look up via rpcbind. */
1166575d184SEdward Tomasz Napierala static const char *portspec = NULL;
1176575d184SEdward Tomasz Napierala static struct sockaddr *addr;
1186575d184SEdward Tomasz Napierala static int addrlen = 0;
1196575d184SEdward Tomasz Napierala static u_char *fh = NULL;
1206575d184SEdward Tomasz Napierala static int fhsize = 0;
1216575d184SEdward Tomasz Napierala static int secflavor = -1;
1226575d184SEdward Tomasz Napierala static int got_principal = 0;
1234b5bc283SCraig Rodrigues 
1246575d184SEdward Tomasz Napierala static enum mountmode {
1252cd1c32cSDoug Rabson 	ANY,
1262cd1c32cSDoug Rabson 	V2,
1270c269d1fSCraig Rodrigues 	V3,
128011981fdSRick Macklem 	V4
1292cd1c32cSDoug Rabson } mountmode = ANY;
1308fae3551SRodney W. Grimes 
131317d5933SIan Dowse /* Return codes for nfs_tryproto. */
132317d5933SIan Dowse enum tryret {
133317d5933SIan Dowse 	TRYRET_SUCCESS,
134317d5933SIan Dowse 	TRYRET_TIMEOUT,		/* No response received. */
135317d5933SIan Dowse 	TRYRET_REMOTEERR,	/* Error received from remote server. */
136317d5933SIan Dowse 	TRYRET_LOCALERR		/* Local failure. */
137317d5933SIan Dowse };
138317d5933SIan Dowse 
1396575d184SEdward Tomasz Napierala static int	sec_name_to_num(const char *sec);
1406575d184SEdward Tomasz Napierala static const char	*sec_num_to_name(int num);
141b352972bSXin LI static int	getnfsargs(char *, struct iovec **iov, int *iovlen);
14285429990SWarner Losh /* void	set_rpc_maxgrouplist(int); */
143b352972bSXin LI static struct netconfig *getnetconf_cached(const char *netid);
144b352972bSXin LI static const char	*netidbytype(int af, int sotype);
145b352972bSXin LI static void	usage(void) __dead2;
146b352972bSXin LI static int	xdr_dir(XDR *, char *);
147b352972bSXin LI static int	xdr_fh(XDR *, struct nfhret *);
148b352972bSXin LI static enum tryret nfs_tryproto(struct addrinfo *ai, char *hostp, char *spec,
1494b5bc283SCraig Rodrigues     char **errstr, struct iovec **iov, int *iovlen);
150b352972bSXin LI static enum tryret returncode(enum clnt_stat stat, struct rpc_err *rpcerr);
1518fae3551SRodney W. Grimes 
1528fae3551SRodney W. Grimes int
15333924ad4SWarner Losh main(int argc, char *argv[])
1548fae3551SRodney W. Grimes {
1553d438ad6SDavid E. O'Brien 	int c;
156c5aa1dc8SCraig Rodrigues 	struct iovec *iov;
1577039dfb3SJung-uk Kim 	int num, iovlen;
1586575d184SEdward Tomasz Napierala 	char *mntname, *p, *spec, *tmp;
159a6b26402SCraig Rodrigues 	char mntpath[MAXPATHLEN], errmsg[255];
1606575d184SEdward Tomasz Napierala 	char hostname[MAXHOSTNAMELEN + 1], gssn[MAXHOSTNAMELEN + 50];
16156c049a0SRick Macklem 	const char *gssname, *nmount_errstr;
1628fae3551SRodney W. Grimes 
163c5aa1dc8SCraig Rodrigues 	iov = NULL;
164c5aa1dc8SCraig Rodrigues 	iovlen = 0;
165a6b26402SCraig Rodrigues 	memset(errmsg, 0, sizeof(errmsg));
166011981fdSRick Macklem 	gssname = NULL;
167c5aa1dc8SCraig Rodrigues 
1688fae3551SRodney W. Grimes 	while ((c = getopt(argc, argv,
16986ce6a83SRobert Watson 	    "23a:bcdD:g:I:iLlNo:PR:r:sTt:w:x:U")) != -1)
1708fae3551SRodney W. Grimes 		switch (c) {
1712cd1c32cSDoug Rabson 		case '2':
1722cd1c32cSDoug Rabson 			mountmode = V2;
1732cd1c32cSDoug Rabson 			break;
174a62dc406SDoug Rabson 		case '3':
1752cd1c32cSDoug Rabson 			mountmode = V3;
176a62dc406SDoug Rabson 			break;
1778fae3551SRodney W. Grimes 		case 'a':
178baa0988aSJaakko Heinonen 			printf("-a deprecated, use -o readahead=<value>\n");
1794b5bc283SCraig Rodrigues 			build_iovec(&iov, &iovlen, "readahead", optarg, (size_t)-1);
1808fae3551SRodney W. Grimes 			break;
1818fae3551SRodney W. Grimes 		case 'b':
1828fae3551SRodney W. Grimes 			opflags |= BGRND;
1838fae3551SRodney W. Grimes 			break;
1848fae3551SRodney W. Grimes 		case 'c':
1854b5bc283SCraig Rodrigues 			printf("-c deprecated, use -o noconn\n");
1864b5bc283SCraig Rodrigues 			build_iovec(&iov, &iovlen, "noconn", NULL, 0);
1874b5bc283SCraig Rodrigues 			noconn = 1;
1888fae3551SRodney W. Grimes 			break;
1898fae3551SRodney W. Grimes 		case 'D':
1904b5bc283SCraig Rodrigues 			printf("-D deprecated, use -o deadthresh=<value>\n");
1914b5bc283SCraig Rodrigues 			build_iovec(&iov, &iovlen, "deadthresh", optarg, (size_t)-1);
1928fae3551SRodney W. Grimes 			break;
1938fae3551SRodney W. Grimes 		case 'd':
1944b5bc283SCraig Rodrigues 			printf("-d deprecated, use -o dumbtimer");
1954b5bc283SCraig Rodrigues 			build_iovec(&iov, &iovlen, "dumbtimer", NULL, 0);
1968fae3551SRodney W. Grimes 			break;
1978fae3551SRodney W. Grimes 		case 'g':
1984b5bc283SCraig Rodrigues 			printf("-g deprecated, use -o maxgroups");
1998fae3551SRodney W. Grimes 			num = strtol(optarg, &p, 10);
2008fae3551SRodney W. Grimes 			if (*p || num <= 0)
2018fae3551SRodney W. Grimes 				errx(1, "illegal -g value -- %s", optarg);
2024b5bc283SCraig Rodrigues 			//set_rpc_maxgrouplist(num);
2034b5bc283SCraig Rodrigues 			build_iovec(&iov, &iovlen, "maxgroups", optarg, (size_t)-1);
2048fae3551SRodney W. Grimes 			break;
205a62dc406SDoug Rabson 		case 'I':
2064b5bc283SCraig Rodrigues 			printf("-I deprecated, use -o readdirsize=<value>\n");
2074b5bc283SCraig Rodrigues 			build_iovec(&iov, &iovlen, "readdirsize", optarg, (size_t)-1);
208a62dc406SDoug Rabson 			break;
2098fae3551SRodney W. Grimes 		case 'i':
2104b5bc283SCraig Rodrigues 			printf("-i deprecated, use -o intr\n");
2114b5bc283SCraig Rodrigues 			build_iovec(&iov, &iovlen, "intr", NULL, 0);
2128fae3551SRodney W. Grimes 			break;
21313190d87SAlfred Perlstein 		case 'L':
214d97326d0SSimon L. B. Nielsen 			printf("-L deprecated, use -o nolockd\n");
2154b5bc283SCraig Rodrigues 			build_iovec(&iov, &iovlen, "nolockd", NULL, 0);
21613190d87SAlfred Perlstein 			break;
2178fae3551SRodney W. Grimes 		case 'l':
2184b5bc283SCraig Rodrigues 			printf("-l deprecated, -o rdirplus\n");
2194b5bc283SCraig Rodrigues 			build_iovec(&iov, &iovlen, "rdirplus", NULL, 0);
2208fae3551SRodney W. Grimes 			break;
221cc75b131SJoerg Wunsch 		case 'N':
2224b5bc283SCraig Rodrigues 			printf("-N deprecated, do not specify -o resvport\n");
223cc75b131SJoerg Wunsch 			break;
2244b5bc283SCraig Rodrigues 		case 'o': {
2254b5bc283SCraig Rodrigues 			int pass_flag_to_nmount;
2264b5bc283SCraig Rodrigues 			char *opt = optarg;
2274b5bc283SCraig Rodrigues 			while (opt) {
2284b5bc283SCraig Rodrigues 				char *pval = NULL;
2294b5bc283SCraig Rodrigues 				char *pnextopt = NULL;
2306575d184SEdward Tomasz Napierala 				const char *val = "";
2314b5bc283SCraig Rodrigues 				pass_flag_to_nmount = 1;
2324b5bc283SCraig Rodrigues 				pnextopt = strchr(opt, ',');
233bdb40379SJaakko Heinonen 				if (pnextopt != NULL) {
234bdb40379SJaakko Heinonen 					*pnextopt = '\0';
235bdb40379SJaakko Heinonen 					pnextopt++;
236bdb40379SJaakko Heinonen 				}
237bdb40379SJaakko Heinonen 				pval = strchr(opt, '=');
2384b5bc283SCraig Rodrigues 				if (pval != NULL) {
2394b5bc283SCraig Rodrigues 					*pval = '\0';
2404b5bc283SCraig Rodrigues 					val = pval + 1;
2414b5bc283SCraig Rodrigues 				}
2424b5bc283SCraig Rodrigues 				if (strcmp(opt, "bg") == 0) {
2433fa88decSGarrett Wollman 					opflags |= BGRND;
2444b5bc283SCraig Rodrigues 					pass_flag_to_nmount=0;
2454b5bc283SCraig Rodrigues 				} else if (strcmp(opt, "fg") == 0) {
2464b5bc283SCraig Rodrigues 					/* same as not specifying -o bg */
2474b5bc283SCraig Rodrigues 					pass_flag_to_nmount=0;
248011981fdSRick Macklem 				} else if (strcmp(opt, "gssname") == 0) {
249011981fdSRick Macklem 					pass_flag_to_nmount = 0;
250011981fdSRick Macklem 					gssname = val;
2514b5bc283SCraig Rodrigues 				} else if (strcmp(opt, "mntudp") == 0) {
252a62dc406SDoug Rabson 					mnttcp_ok = 0;
253bf005f32SKris Kennaway 					nfsproto = IPPROTO_UDP;
2544b5bc283SCraig Rodrigues 				} else if (strcmp(opt, "udp") == 0) {
2554b5bc283SCraig Rodrigues 					nfsproto = IPPROTO_UDP;
2564b5bc283SCraig Rodrigues 				} else if (strcmp(opt, "tcp") == 0) {
257a62dc406SDoug Rabson 					nfsproto = IPPROTO_TCP;
2584b5bc283SCraig Rodrigues 				} else if (strcmp(opt, "noinet4") == 0) {
2594b5bc283SCraig Rodrigues 					pass_flag_to_nmount=0;
2604b5bc283SCraig Rodrigues 					opflags |= OF_NOINET4;
2614b5bc283SCraig Rodrigues 				} else if (strcmp(opt, "noinet6") == 0) {
2624b5bc283SCraig Rodrigues 					pass_flag_to_nmount=0;
2634b5bc283SCraig Rodrigues 					opflags |= OF_NOINET6;
2644b5bc283SCraig Rodrigues 				} else if (strcmp(opt, "noconn") == 0) {
2654b5bc283SCraig Rodrigues 					noconn = 1;
2664b5bc283SCraig Rodrigues 				} else if (strcmp(opt, "nfsv2") == 0) {
2674b5bc283SCraig Rodrigues 					pass_flag_to_nmount=0;
2684b5bc283SCraig Rodrigues 					mountmode = V2;
2694b5bc283SCraig Rodrigues 				} else if (strcmp(opt, "nfsv3") == 0) {
2704b5bc283SCraig Rodrigues 					mountmode = V3;
271011981fdSRick Macklem 				} else if (strcmp(opt, "nfsv4") == 0) {
272011981fdSRick Macklem 					pass_flag_to_nmount=0;
273011981fdSRick Macklem 					mountmode = V4;
274011981fdSRick Macklem 					nfsproto = IPPROTO_TCP;
275011981fdSRick Macklem 					if (portspec == NULL)
276011981fdSRick Macklem 						portspec = "2049";
2774b5bc283SCraig Rodrigues 				} else if (strcmp(opt, "port") == 0) {
2784b5bc283SCraig Rodrigues 					pass_flag_to_nmount=0;
2796575d184SEdward Tomasz Napierala 					asprintf(&tmp, "%d", atoi(val));
2806575d184SEdward Tomasz Napierala 					if (tmp == NULL)
281317d5933SIan Dowse 						err(1, "asprintf");
2826575d184SEdward Tomasz Napierala 					portspec = tmp;
283011981fdSRick Macklem 				} else if (strcmp(opt, "principal") == 0) {
284011981fdSRick Macklem 					got_principal = 1;
2855a06ac35SEdward Tomasz Napierala 				} else if (strcmp(opt, "proto") == 0) {
2865a06ac35SEdward Tomasz Napierala 					pass_flag_to_nmount=0;
2875a06ac35SEdward Tomasz Napierala 					if (strcmp(val, "tcp") == 0) {
2885a06ac35SEdward Tomasz Napierala 						nfsproto = IPPROTO_TCP;
2895a06ac35SEdward Tomasz Napierala 						opflags |= OF_NOINET6;
2905a06ac35SEdward Tomasz Napierala 						build_iovec(&iov, &iovlen,
2915a06ac35SEdward Tomasz Napierala 						    "tcp", NULL, 0);
2925a06ac35SEdward Tomasz Napierala 					} else if (strcmp(val, "udp") == 0) {
2935a06ac35SEdward Tomasz Napierala 						mnttcp_ok = 0;
2945a06ac35SEdward Tomasz Napierala 						nfsproto = IPPROTO_UDP;
2955a06ac35SEdward Tomasz Napierala 						opflags |= OF_NOINET6;
2965a06ac35SEdward Tomasz Napierala 						build_iovec(&iov, &iovlen,
2975a06ac35SEdward Tomasz Napierala 						    "udp", NULL, 0);
2985a06ac35SEdward Tomasz Napierala 					} else if (strcmp(val, "tcp6") == 0) {
2995a06ac35SEdward Tomasz Napierala 						nfsproto = IPPROTO_TCP;
3005a06ac35SEdward Tomasz Napierala 						opflags |= OF_NOINET4;
3015a06ac35SEdward Tomasz Napierala 						build_iovec(&iov, &iovlen,
3025a06ac35SEdward Tomasz Napierala 						    "tcp", NULL, 0);
3035a06ac35SEdward Tomasz Napierala 					} else if (strcmp(val, "udp6") == 0) {
3045a06ac35SEdward Tomasz Napierala 						mnttcp_ok = 0;
3055a06ac35SEdward Tomasz Napierala 						nfsproto = IPPROTO_UDP;
3065a06ac35SEdward Tomasz Napierala 						opflags |= OF_NOINET4;
3075a06ac35SEdward Tomasz Napierala 						build_iovec(&iov, &iovlen,
3085a06ac35SEdward Tomasz Napierala 						    "udp", NULL, 0);
3095a06ac35SEdward Tomasz Napierala 					} else {
3105a06ac35SEdward Tomasz Napierala 						errx(1,
3115a06ac35SEdward Tomasz Napierala 						    "illegal proto value -- %s",
3125a06ac35SEdward Tomasz Napierala 						    val);
3135a06ac35SEdward Tomasz Napierala 					}
314a9148abdSDoug Rabson 				} else if (strcmp(opt, "sec") == 0) {
315a9148abdSDoug Rabson 					/*
316a9148abdSDoug Rabson 					 * Don't add this option to
317a9148abdSDoug Rabson 					 * the iovec yet - we will
318a9148abdSDoug Rabson 					 * negotiate which sec flavor
319a9148abdSDoug Rabson 					 * to use with the remote
320a9148abdSDoug Rabson 					 * mountd.
321a9148abdSDoug Rabson 					 */
322a9148abdSDoug Rabson 					pass_flag_to_nmount=0;
323a9148abdSDoug Rabson 					secflavor = sec_name_to_num(val);
324a9148abdSDoug Rabson 					if (secflavor < 0) {
325a9148abdSDoug Rabson 						errx(1,
326a9148abdSDoug Rabson 						    "illegal sec value -- %s",
327a9148abdSDoug Rabson 						    val);
328a9148abdSDoug Rabson 					}
3294b5bc283SCraig Rodrigues 				} else if (strcmp(opt, "retrycnt") == 0) {
3304b5bc283SCraig Rodrigues 					pass_flag_to_nmount=0;
3314b5bc283SCraig Rodrigues 					num = strtol(val, &p, 10);
3324b5bc283SCraig Rodrigues 					if (*p || num < 0)
3334b5bc283SCraig Rodrigues 						errx(1, "illegal retrycnt value -- %s", val);
3344b5bc283SCraig Rodrigues 					retrycnt = num;
3354b5bc283SCraig Rodrigues 				} else if (strcmp(opt, "maxgroups") == 0) {
3364b5bc283SCraig Rodrigues 					num = strtol(val, &p, 10);
3374b5bc283SCraig Rodrigues 					if (*p || num <= 0)
3384b5bc283SCraig Rodrigues 						errx(1, "illegal maxgroups value -- %s", val);
3394b5bc283SCraig Rodrigues 					//set_rpc_maxgrouplist(num);
340528c159fSBjoern A. Zeeb 				} else if (strcmp(opt, "vers") == 0) {
341528c159fSBjoern A. Zeeb 					num = strtol(val, &p, 10);
342528c159fSBjoern A. Zeeb 					if (*p || num <= 0)
343528c159fSBjoern A. Zeeb 						errx(1, "illegal vers value -- "
344528c159fSBjoern A. Zeeb 						    "%s", val);
345528c159fSBjoern A. Zeeb 					switch (num) {
346528c159fSBjoern A. Zeeb 					case 2:
347528c159fSBjoern A. Zeeb 						mountmode = V2;
348528c159fSBjoern A. Zeeb 						break;
349528c159fSBjoern A. Zeeb 					case 3:
350528c159fSBjoern A. Zeeb 						mountmode = V3;
351528c159fSBjoern A. Zeeb 						build_iovec(&iov, &iovlen,
352528c159fSBjoern A. Zeeb 						    "nfsv3", NULL, 0);
353528c159fSBjoern A. Zeeb 						break;
354528c159fSBjoern A. Zeeb 					case 4:
355528c159fSBjoern A. Zeeb 						mountmode = V4;
356528c159fSBjoern A. Zeeb 						nfsproto = IPPROTO_TCP;
357528c159fSBjoern A. Zeeb 						if (portspec == NULL)
358528c159fSBjoern A. Zeeb 							portspec = "2049";
359528c159fSBjoern A. Zeeb 						break;
360528c159fSBjoern A. Zeeb 					default:
361528c159fSBjoern A. Zeeb 						errx(1, "illegal nfs version "
362528c159fSBjoern A. Zeeb 						    "value -- %s", val);
363528c159fSBjoern A. Zeeb 					}
364528c159fSBjoern A. Zeeb 					pass_flag_to_nmount=0;
365317d5933SIan Dowse 				}
3666575d184SEdward Tomasz Napierala 				if (pass_flag_to_nmount) {
3676575d184SEdward Tomasz Napierala 					build_iovec(&iov, &iovlen, opt,
3686575d184SEdward Tomasz Napierala 					    __DECONST(void *, val),
3694b5bc283SCraig Rodrigues 					    strlen(val) + 1);
3706575d184SEdward Tomasz Napierala 				}
3714b5bc283SCraig Rodrigues 				opt = pnextopt;
3724db56604SPeter Wemm 			}
3734db56604SPeter Wemm 			}
3748fae3551SRodney W. Grimes 			break;
3758fae3551SRodney W. Grimes 		case 'P':
3764b5bc283SCraig Rodrigues 			/* obsolete for -o noresvport now default */
3774b5bc283SCraig Rodrigues 			printf("-P deprecated, use -o noresvport\n");
3784b5bc283SCraig Rodrigues 			build_iovec(&iov, &iovlen, "noresvport", NULL, 0);
3798fae3551SRodney W. Grimes 			break;
3808fae3551SRodney W. Grimes 		case 'R':
3814b5bc283SCraig Rodrigues 			printf("-R deprecated, use -o retrycnt=<retrycnt>\n");
3828fae3551SRodney W. Grimes 			num = strtol(optarg, &p, 10);
383e16873daSIan Dowse 			if (*p || num < 0)
3848fae3551SRodney W. Grimes 				errx(1, "illegal -R value -- %s", optarg);
3858fae3551SRodney W. Grimes 			retrycnt = num;
3868fae3551SRodney W. Grimes 			break;
3878fae3551SRodney W. Grimes 		case 'r':
3884b5bc283SCraig Rodrigues 			printf("-r deprecated, use -o rsize=<rsize>\n");
3894b5bc283SCraig Rodrigues 			build_iovec(&iov, &iovlen, "rsize", optarg, (size_t)-1);
3908fae3551SRodney W. Grimes 			break;
3918fae3551SRodney W. Grimes 		case 's':
3924b5bc283SCraig Rodrigues 			printf("-s deprecated, use -o soft\n");
3934b5bc283SCraig Rodrigues 			build_iovec(&iov, &iovlen, "soft", NULL, 0);
3948fae3551SRodney W. Grimes 			break;
3958fae3551SRodney W. Grimes 		case 'T':
396a62dc406SDoug Rabson 			nfsproto = IPPROTO_TCP;
3974b5bc283SCraig Rodrigues 			printf("-T deprecated, use -o tcp\n");
3988fae3551SRodney W. Grimes 			break;
3998fae3551SRodney W. Grimes 		case 't':
4004b5bc283SCraig Rodrigues 			printf("-t deprecated, use -o timeout=<value>\n");
4014b5bc283SCraig Rodrigues 			build_iovec(&iov, &iovlen, "timeout", optarg, (size_t)-1);
4028fae3551SRodney W. Grimes 			break;
4038fae3551SRodney W. Grimes 		case 'w':
4044b5bc283SCraig Rodrigues 			printf("-w deprecated, use -o wsize=<value>\n");
4054b5bc283SCraig Rodrigues 			build_iovec(&iov, &iovlen, "wsize", optarg, (size_t)-1);
4068fae3551SRodney W. Grimes 			break;
4078fae3551SRodney W. Grimes 		case 'x':
4084b5bc283SCraig Rodrigues 			printf("-x deprecated, use -o retrans=<value>\n");
4094b5bc283SCraig Rodrigues 			build_iovec(&iov, &iovlen, "retrans", optarg, (size_t)-1);
4108fae3551SRodney W. Grimes 			break;
411a62dc406SDoug Rabson 		case 'U':
4124b5bc283SCraig Rodrigues 			printf("-U deprecated, use -o mntudp\n");
413a62dc406SDoug Rabson 			mnttcp_ok = 0;
4140c269d1fSCraig Rodrigues 			nfsproto = IPPROTO_UDP;
4154b5bc283SCraig Rodrigues 			build_iovec(&iov, &iovlen, "mntudp", NULL, 0);
416a62dc406SDoug Rabson 			break;
4178fae3551SRodney W. Grimes 		default:
4188fae3551SRodney W. Grimes 			usage();
4198fae3551SRodney W. Grimes 			break;
4208fae3551SRodney W. Grimes 		}
4218fae3551SRodney W. Grimes 	argc -= optind;
4228fae3551SRodney W. Grimes 	argv += optind;
4238fae3551SRodney W. Grimes 
4244a4c5285SPeter Wemm 	if (argc != 2) {
4252f21d07aSDavid Greenman 		usage();
4264a4c5285SPeter Wemm 		/* NOTREACHED */
4274a4c5285SPeter Wemm 	}
4288fae3551SRodney W. Grimes 
4298fae3551SRodney W. Grimes 	spec = *argv++;
4306575d184SEdward Tomasz Napierala 	mntname = *argv;
4318fae3551SRodney W. Grimes 
432e16873daSIan Dowse 	if (retrycnt == -1)
4332bc53e11SIan Dowse 		/* The default is to keep retrying forever. */
4342bc53e11SIan Dowse 		retrycnt = 0;
4350c269d1fSCraig Rodrigues 
436011981fdSRick Macklem 	if (modfind("nfscl") < 0) {
437011981fdSRick Macklem 		/* Not present in kernel, try loading it */
438011981fdSRick Macklem 		if (kldload("nfscl") < 0 ||
439011981fdSRick Macklem 		    modfind("nfscl") < 0)
440011981fdSRick Macklem 			errx(1, "nfscl is not available");
441011981fdSRick Macklem 	}
442011981fdSRick Macklem 
443011981fdSRick Macklem 	/*
444011981fdSRick Macklem 	 * Add the fqdn to the gssname, as required.
445011981fdSRick Macklem 	 */
446011981fdSRick Macklem 	if (gssname != NULL) {
447011981fdSRick Macklem 		if (strchr(gssname, '@') == NULL &&
448011981fdSRick Macklem 		    gethostname(hostname, MAXHOSTNAMELEN) == 0) {
449011981fdSRick Macklem 			snprintf(gssn, sizeof (gssn), "%s@%s", gssname,
450011981fdSRick Macklem 			    hostname);
451011981fdSRick Macklem 			gssname = gssn;
452011981fdSRick Macklem 		}
4536575d184SEdward Tomasz Napierala 		build_iovec(&iov, &iovlen, "gssname",
4546575d184SEdward Tomasz Napierala 		    __DECONST(void *, gssname), strlen(gssname) + 1);
455011981fdSRick Macklem 	}
456011981fdSRick Macklem 
4574b5bc283SCraig Rodrigues 	if (!getnfsargs(spec, &iov, &iovlen))
4588fae3551SRodney W. Grimes 		exit(1);
459d599144dSGarrett Wollman 
46073dd3167SPoul-Henning Kamp 	/* resolve the mountpoint with realpath(3) */
4616575d184SEdward Tomasz Napierala 	if (checkpath(mntname, mntpath) != 0)
462d3250014SJaakko Heinonen 		err(1, "%s", mntpath);
46373dd3167SPoul-Henning Kamp 
46425585679SBrooks Davis 	build_iovec_argf(&iov, &iovlen, "fstype", "nfs");
465c5aa1dc8SCraig Rodrigues 	build_iovec(&iov, &iovlen, "fspath", mntpath, (size_t)-1);
466a6b26402SCraig Rodrigues 	build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
467c5aa1dc8SCraig Rodrigues 
46856c049a0SRick Macklem 	if (nmount(iov, iovlen, 0)) {
46956c049a0SRick Macklem 		nmount_errstr = nfsv4_geterrstr(errno);
47056c049a0SRick Macklem 		if (mountmode == V4 && nmount_errstr != NULL)
47156c049a0SRick Macklem 			errx(1, "nmount: %s, %s", mntpath, nmount_errstr);
47256c049a0SRick Macklem 		else
473773e5651SConrad Meyer 			err(1, "nmount: %s%s%s", mntpath, errmsg[0] ? ", " : "",
474773e5651SConrad Meyer 			    errmsg);
47556c049a0SRick Macklem 	}
476a62dc406SDoug Rabson 
4778fae3551SRodney W. Grimes 	exit(0);
4788fae3551SRodney W. Grimes }
4798fae3551SRodney W. Grimes 
4804b5bc283SCraig Rodrigues static int
4816575d184SEdward Tomasz Napierala sec_name_to_num(const char *sec)
482a9148abdSDoug Rabson {
483a9148abdSDoug Rabson 	if (!strcmp(sec, "krb5"))
484a9148abdSDoug Rabson 		return (RPCSEC_GSS_KRB5);
485a9148abdSDoug Rabson 	if (!strcmp(sec, "krb5i"))
486a9148abdSDoug Rabson 		return (RPCSEC_GSS_KRB5I);
487a9148abdSDoug Rabson 	if (!strcmp(sec, "krb5p"))
488a9148abdSDoug Rabson 		return (RPCSEC_GSS_KRB5P);
489a9148abdSDoug Rabson 	if (!strcmp(sec, "sys"))
490a9148abdSDoug Rabson 		return (AUTH_SYS);
491a9148abdSDoug Rabson 	return (-1);
492a9148abdSDoug Rabson }
493a9148abdSDoug Rabson 
4946575d184SEdward Tomasz Napierala static const char *
495a9148abdSDoug Rabson sec_num_to_name(int flavor)
496a9148abdSDoug Rabson {
497a9148abdSDoug Rabson 	switch (flavor) {
498a9148abdSDoug Rabson 	case RPCSEC_GSS_KRB5:
499a9148abdSDoug Rabson 		return ("krb5");
500a9148abdSDoug Rabson 	case RPCSEC_GSS_KRB5I:
501a9148abdSDoug Rabson 		return ("krb5i");
502a9148abdSDoug Rabson 	case RPCSEC_GSS_KRB5P:
503a9148abdSDoug Rabson 		return ("krb5p");
504a9148abdSDoug Rabson 	case AUTH_SYS:
505a9148abdSDoug Rabson 		return ("sys");
506a9148abdSDoug Rabson 	}
507a9148abdSDoug Rabson 	return (NULL);
508a9148abdSDoug Rabson }
509a9148abdSDoug Rabson 
5108cd9718fSGleb Smirnoff /*
5118cd9718fSGleb Smirnoff  * Wait for RTM_IFINFO message with interface that is IFF_UP and with
5128cd9718fSGleb Smirnoff  * link on, or until timeout expires.  Returns seconds left.
5138cd9718fSGleb Smirnoff  */
5148cd9718fSGleb Smirnoff static time_t
5158cd9718fSGleb Smirnoff rtm_ifinfo_sleep(time_t sec)
5168cd9718fSGleb Smirnoff {
5173f16af55SGleb Smirnoff 	char buf[2048] __aligned(__alignof(struct if_msghdr));
5188cd9718fSGleb Smirnoff 	fd_set rfds;
5198cd9718fSGleb Smirnoff 	struct timeval tv, start;
5208cd9718fSGleb Smirnoff 	ssize_t nread;
5218cd9718fSGleb Smirnoff 	int n, s;
5228cd9718fSGleb Smirnoff 
5238cd9718fSGleb Smirnoff 	s = socket(PF_ROUTE, SOCK_RAW, 0);
5248cd9718fSGleb Smirnoff 	if (s < 0)
5258cd9718fSGleb Smirnoff 		err(EX_OSERR, "socket");
5268cd9718fSGleb Smirnoff 	(void)gettimeofday(&start, NULL);
5278cd9718fSGleb Smirnoff 
5288cd9718fSGleb Smirnoff 	for (tv.tv_sec = sec, tv.tv_usec = 0;
5298cd9718fSGleb Smirnoff 	    tv.tv_sec > 0;
5308cd9718fSGleb Smirnoff 	    (void)gettimeofday(&tv, NULL),
5318cd9718fSGleb Smirnoff 	    tv.tv_sec = sec - (tv.tv_sec - start.tv_sec)) {
5328cd9718fSGleb Smirnoff 		FD_ZERO(&rfds);
5338cd9718fSGleb Smirnoff 		FD_SET(s, &rfds);
5348cd9718fSGleb Smirnoff 		n = select(s + 1, &rfds, NULL, NULL, &tv);
5358cd9718fSGleb Smirnoff 		if (n == 0)
5368cd9718fSGleb Smirnoff 			continue;
5378cd9718fSGleb Smirnoff 		if (n == -1) {
5388cd9718fSGleb Smirnoff 			if (errno == EINTR)
5398cd9718fSGleb Smirnoff 				continue;
5408cd9718fSGleb Smirnoff 			else
5418cd9718fSGleb Smirnoff 				err(EX_SOFTWARE, "select");
5428cd9718fSGleb Smirnoff 		}
5438cd9718fSGleb Smirnoff 		nread = read(s, buf, 2048);
5448cd9718fSGleb Smirnoff 		if (nread < 0)
5458cd9718fSGleb Smirnoff 			err(EX_OSERR, "read");
5468cd9718fSGleb Smirnoff 		if ((size_t)nread >= sizeof(struct if_msghdr)) {
5478cd9718fSGleb Smirnoff 			struct if_msghdr *ifm;
5488cd9718fSGleb Smirnoff 
5498cd9718fSGleb Smirnoff 			ifm = (struct if_msghdr *)buf;
5508cd9718fSGleb Smirnoff 			if (ifm->ifm_version == RTM_VERSION &&
5518cd9718fSGleb Smirnoff 			    ifm->ifm_type == RTM_IFINFO &&
5528cd9718fSGleb Smirnoff 			    (ifm->ifm_flags & IFF_UP) &&
5538cd9718fSGleb Smirnoff 			    ifm->ifm_data.ifi_link_state != LINK_STATE_DOWN)
5548cd9718fSGleb Smirnoff 				break;
5558cd9718fSGleb Smirnoff 		}
5568cd9718fSGleb Smirnoff 	}
5578cd9718fSGleb Smirnoff 
5588cd9718fSGleb Smirnoff 	close(s);
5598cd9718fSGleb Smirnoff 
5608cd9718fSGleb Smirnoff 	return (tv.tv_sec);
5618cd9718fSGleb Smirnoff }
5628cd9718fSGleb Smirnoff 
563b352972bSXin LI static int
5644b5bc283SCraig Rodrigues getnfsargs(char *spec, struct iovec **iov, int *iovlen)
5658fae3551SRodney W. Grimes {
5668360efbdSAlfred Perlstein 	struct addrinfo hints, *ai_nfs, *ai;
567317d5933SIan Dowse 	enum tryret ret;
568a505d435SHajimu UMEMOTO 	int ecode, speclen, remoteerr, offset, have_bracket = 0;
569317d5933SIan Dowse 	char *hostp, *delimp, *errstr;
57066a84ea7SBrian Feldman 	size_t len;
571011981fdSRick Macklem 	static char nam[MNAMELEN + 1], pname[MAXHOSTNAMELEN + 5];
5728fae3551SRodney W. Grimes 
573a505d435SHajimu UMEMOTO 	if (*spec == '[' && (delimp = strchr(spec + 1, ']')) != NULL &&
574a505d435SHajimu UMEMOTO 	    *(delimp + 1) == ':') {
575a505d435SHajimu UMEMOTO 		hostp = spec + 1;
576a505d435SHajimu UMEMOTO 		spec = delimp + 2;
577a505d435SHajimu UMEMOTO 		have_bracket = 1;
578a505d435SHajimu UMEMOTO 	} else if ((delimp = strrchr(spec, ':')) != NULL) {
5798fae3551SRodney W. Grimes 		hostp = spec;
5808fae3551SRodney W. Grimes 		spec = delimp + 1;
58173dd3167SPoul-Henning Kamp 	} else if ((delimp = strrchr(spec, '@')) != NULL) {
58273dd3167SPoul-Henning Kamp 		warnx("path@server syntax is deprecated, use server:path");
58373dd3167SPoul-Henning Kamp 		hostp = delimp + 1;
5848fae3551SRodney W. Grimes 	} else {
58573dd3167SPoul-Henning Kamp 		warnx("no <host>:<dirpath> nfs-name");
5868fae3551SRodney W. Grimes 		return (0);
5878fae3551SRodney W. Grimes 	}
5888fae3551SRodney W. Grimes 	*delimp = '\0';
58973dd3167SPoul-Henning Kamp 
59073dd3167SPoul-Henning Kamp 	/*
59173dd3167SPoul-Henning Kamp 	 * If there has been a trailing slash at mounttime it seems
59273dd3167SPoul-Henning Kamp 	 * that some mountd implementations fail to remove the mount
59373dd3167SPoul-Henning Kamp 	 * entries from their mountlist while unmounting.
59473dd3167SPoul-Henning Kamp 	 */
59566a84ea7SBrian Feldman 	for (speclen = strlen(spec);
59666a84ea7SBrian Feldman 		speclen > 1 && spec[speclen - 1] == '/';
59766a84ea7SBrian Feldman 		speclen--)
59873dd3167SPoul-Henning Kamp 		spec[speclen - 1] = '\0';
59973dd3167SPoul-Henning Kamp 	if (strlen(hostp) + strlen(spec) + 1 > MNAMELEN) {
60073dd3167SPoul-Henning Kamp 		warnx("%s:%s: %s", hostp, spec, strerror(ENAMETOOLONG));
60173dd3167SPoul-Henning Kamp 		return (0);
60273dd3167SPoul-Henning Kamp 	}
60373dd3167SPoul-Henning Kamp 	/* Make both '@' and ':' notations equal */
60466a84ea7SBrian Feldman 	if (*hostp != '\0') {
60566a84ea7SBrian Feldman 		len = strlen(hostp);
606a505d435SHajimu UMEMOTO 		offset = 0;
607a505d435SHajimu UMEMOTO 		if (have_bracket)
608a505d435SHajimu UMEMOTO 			nam[offset++] = '[';
609a505d435SHajimu UMEMOTO 		memmove(nam + offset, hostp, len);
610a505d435SHajimu UMEMOTO 		if (have_bracket)
611a505d435SHajimu UMEMOTO 			nam[len + offset++] = ']';
612a505d435SHajimu UMEMOTO 		nam[len + offset++] = ':';
613a505d435SHajimu UMEMOTO 		memmove(nam + len + offset, spec, speclen);
614a505d435SHajimu UMEMOTO 		nam[len + speclen + offset] = '\0';
61566a84ea7SBrian Feldman 	}
6168fae3551SRodney W. Grimes 
6178fae3551SRodney W. Grimes 	/*
61891196234SPeter Wemm 	 * Handle an internet host address.
6198fae3551SRodney W. Grimes 	 */
6208360efbdSAlfred Perlstein 	memset(&hints, 0, sizeof hints);
6218360efbdSAlfred Perlstein 	hints.ai_flags = AI_NUMERICHOST;
6224b5bc283SCraig Rodrigues 	if (nfsproto == IPPROTO_TCP)
6234b5bc283SCraig Rodrigues 		hints.ai_socktype = SOCK_STREAM;
6244b5bc283SCraig Rodrigues 	else if (nfsproto == IPPROTO_UDP)
6254b5bc283SCraig Rodrigues 		hints.ai_socktype = SOCK_DGRAM;
6264b5bc283SCraig Rodrigues 
62791196234SPeter Wemm 	if (getaddrinfo(hostp, portspec, &hints, &ai_nfs) != 0) {
628011981fdSRick Macklem 		hints.ai_flags = AI_CANONNAME;
629317d5933SIan Dowse 		if ((ecode = getaddrinfo(hostp, portspec, &hints, &ai_nfs))
630317d5933SIan Dowse 		    != 0) {
631317d5933SIan Dowse 			if (portspec == NULL)
632317d5933SIan Dowse 				errx(1, "%s: %s", hostp, gai_strerror(ecode));
633317d5933SIan Dowse 			else
634317d5933SIan Dowse 				errx(1, "%s:%s: %s", hostp, portspec,
6358360efbdSAlfred Perlstein 				    gai_strerror(ecode));
6368360efbdSAlfred Perlstein 			return (0);
6378360efbdSAlfred Perlstein 		}
638011981fdSRick Macklem 
639011981fdSRick Macklem 		/*
640011981fdSRick Macklem 		 * For a Kerberized nfs mount where the "principal"
641011981fdSRick Macklem 		 * argument has not been set, add it here.
642011981fdSRick Macklem 		 */
643c088e62eSRick Macklem 		if (got_principal == 0 && secflavor != AUTH_SYS &&
644c088e62eSRick Macklem 		    ai_nfs->ai_canonname != NULL) {
645011981fdSRick Macklem 			snprintf(pname, sizeof (pname), "nfs@%s",
646011981fdSRick Macklem 			    ai_nfs->ai_canonname);
647011981fdSRick Macklem 			build_iovec(iov, iovlen, "principal", pname,
648011981fdSRick Macklem 			    strlen(pname) + 1);
649011981fdSRick Macklem 		}
6508360efbdSAlfred Perlstein 	}
6518fae3551SRodney W. Grimes 
652317d5933SIan Dowse 	ret = TRYRET_LOCALERR;
653e16873daSIan Dowse 	for (;;) {
654deffdffaSAndrey A. Chernov 		/*
655317d5933SIan Dowse 		 * Try each entry returned by getaddrinfo(). Note the
6564b85a12fSUlrich Spörlein 		 * occurrence of remote errors by setting `remoteerr'.
657deffdffaSAndrey A. Chernov 		 */
658317d5933SIan Dowse 		remoteerr = 0;
659317d5933SIan Dowse 		for (ai = ai_nfs; ai != NULL; ai = ai->ai_next) {
660302f15f9SMatthew N. Dodd 			if ((ai->ai_family == AF_INET6) &&
661302f15f9SMatthew N. Dodd 			    (opflags & OF_NOINET6))
662302f15f9SMatthew N. Dodd 				continue;
663302f15f9SMatthew N. Dodd 			if ((ai->ai_family == AF_INET) &&
664302f15f9SMatthew N. Dodd 			    (opflags & OF_NOINET4))
665302f15f9SMatthew N. Dodd 				continue;
6664b5bc283SCraig Rodrigues 			ret = nfs_tryproto(ai, hostp, spec, &errstr, iov,
6674b5bc283SCraig Rodrigues 			    iovlen);
668317d5933SIan Dowse 			if (ret == TRYRET_SUCCESS)
669317d5933SIan Dowse 				break;
670317d5933SIan Dowse 			if (ret != TRYRET_LOCALERR)
671317d5933SIan Dowse 				remoteerr = 1;
672317d5933SIan Dowse 			if ((opflags & ISBGRND) == 0)
673317d5933SIan Dowse 				fprintf(stderr, "%s\n", errstr);
674deffdffaSAndrey A. Chernov 		}
675317d5933SIan Dowse 		if (ret == TRYRET_SUCCESS)
676317d5933SIan Dowse 			break;
677deffdffaSAndrey A. Chernov 
678e16873daSIan Dowse 		/* Exit if all errors were local. */
679e16873daSIan Dowse 		if (!remoteerr)
680deffdffaSAndrey A. Chernov 			exit(1);
681317d5933SIan Dowse 
682e16873daSIan Dowse 		/*
683e16873daSIan Dowse 		 * If retrycnt == 0, we are to keep retrying forever.
684e16873daSIan Dowse 		 * Otherwise decrement it, and exit if it hits zero.
685e16873daSIan Dowse 		 */
686e16873daSIan Dowse 		if (retrycnt != 0 && --retrycnt == 0)
687317d5933SIan Dowse 			exit(1);
688317d5933SIan Dowse 
689317d5933SIan Dowse 		if ((opflags & (BGRND | ISBGRND)) == BGRND) {
690317d5933SIan Dowse 			warnx("Cannot immediately mount %s:%s, backgrounding",
691317d5933SIan Dowse 			    hostp, spec);
692317d5933SIan Dowse 			opflags |= ISBGRND;
693317d5933SIan Dowse 			if (daemon(0, 0) != 0)
694317d5933SIan Dowse 				err(1, "daemon");
6958fae3551SRodney W. Grimes 		}
6968cd9718fSGleb Smirnoff 		/*
6978cd9718fSGleb Smirnoff 		 * If rtm_ifinfo_sleep() returns non-zero, don't count
6988cd9718fSGleb Smirnoff 		 * that as a retry attempt.
6998cd9718fSGleb Smirnoff 		 */
7008cd9718fSGleb Smirnoff 		if (rtm_ifinfo_sleep(60) && retrycnt != 0)
7018cd9718fSGleb Smirnoff 			retrycnt++;
7028fae3551SRodney W. Grimes 	}
7038360efbdSAlfred Perlstein 	freeaddrinfo(ai_nfs);
7044b5bc283SCraig Rodrigues 
7054b5bc283SCraig Rodrigues 	build_iovec(iov, iovlen, "hostname", nam, (size_t)-1);
706a69497d7SMatthew Dillon 	/* Add mounted file system to PATH_MOUNTTAB */
70713622763SRick Macklem 	if (mountmode != V4 && !add_mtab(hostp, spec))
708a69497d7SMatthew Dillon 		warnx("can't update %s for %s:%s", PATH_MOUNTTAB, hostp, spec);
7098fae3551SRodney W. Grimes 	return (1);
7108fae3551SRodney W. Grimes }
7118fae3551SRodney W. Grimes 
7128fae3551SRodney W. Grimes /*
713317d5933SIan Dowse  * Try to set up the NFS arguments according to the address
714317d5933SIan Dowse  * family, protocol (and possibly port) specified in `ai'.
715317d5933SIan Dowse  *
716317d5933SIan Dowse  * Returns TRYRET_SUCCESS if successful, or:
717317d5933SIan Dowse  *   TRYRET_TIMEOUT		The server did not respond.
718317d5933SIan Dowse  *   TRYRET_REMOTEERR		The server reported an error.
719317d5933SIan Dowse  *   TRYRET_LOCALERR		Local failure.
720317d5933SIan Dowse  *
721317d5933SIan Dowse  * In all error cases, *errstr will be set to a statically-allocated string
722317d5933SIan Dowse  * describing the error.
723317d5933SIan Dowse  */
724b352972bSXin LI static enum tryret
7254b5bc283SCraig Rodrigues nfs_tryproto(struct addrinfo *ai, char *hostp, char *spec, char **errstr,
7264b5bc283SCraig Rodrigues     struct iovec **iov, int *iovlen)
727317d5933SIan Dowse {
728317d5933SIan Dowse 	static char errbuf[256];
729317d5933SIan Dowse 	struct sockaddr_storage nfs_ss;
730317d5933SIan Dowse 	struct netbuf nfs_nb;
731317d5933SIan Dowse 	struct nfhret nfhret;
732317d5933SIan Dowse 	struct timeval try;
733317d5933SIan Dowse 	struct rpc_err rpcerr;
734317d5933SIan Dowse 	CLIENT *clp;
735317d5933SIan Dowse 	struct netconfig *nconf, *nconf_mnt;
7366575d184SEdward Tomasz Napierala 	const char *netid, *netid_mnt, *secname;
7374b5bc283SCraig Rodrigues 	int doconnect, nfsvers, mntvers, sotype;
7386575d184SEdward Tomasz Napierala 	enum clnt_stat clntstat;
739317d5933SIan Dowse 	enum mountmode trymntmode;
740317d5933SIan Dowse 
741d0654c85SKevin Lo 	sotype = 0;
742317d5933SIan Dowse 	trymntmode = mountmode;
743317d5933SIan Dowse 	errbuf[0] = '\0';
744317d5933SIan Dowse 	*errstr = errbuf;
745317d5933SIan Dowse 
7464b5bc283SCraig Rodrigues 	if (nfsproto == IPPROTO_TCP)
7474b5bc283SCraig Rodrigues 		sotype = SOCK_STREAM;
7484b5bc283SCraig Rodrigues 	else if (nfsproto == IPPROTO_UDP)
7494b5bc283SCraig Rodrigues 		sotype = SOCK_DGRAM;
7504b5bc283SCraig Rodrigues 
7514b5bc283SCraig Rodrigues 	if ((netid = netidbytype(ai->ai_family, sotype)) == NULL) {
752a3d8f94bSIan Dowse 		snprintf(errbuf, sizeof errbuf,
7534b5bc283SCraig Rodrigues 		    "af %d sotype %d not supported", ai->ai_family, sotype);
754a3d8f94bSIan Dowse 		return (TRYRET_LOCALERR);
755a3d8f94bSIan Dowse 	}
756a3d8f94bSIan Dowse 	if ((nconf = getnetconf_cached(netid)) == NULL) {
757317d5933SIan Dowse 		snprintf(errbuf, sizeof errbuf, "%s: %s", netid, nc_sperror());
758317d5933SIan Dowse 		return (TRYRET_LOCALERR);
759317d5933SIan Dowse 	}
760317d5933SIan Dowse 	/* The RPCPROG_MNT netid may be different. */
761317d5933SIan Dowse 	if (mnttcp_ok) {
762ba33efd9SIan Dowse 		netid_mnt = netid;
763317d5933SIan Dowse 		nconf_mnt = nconf;
764317d5933SIan Dowse 	} else {
765a3d8f94bSIan Dowse 		if ((netid_mnt = netidbytype(ai->ai_family, SOCK_DGRAM))
766a3d8f94bSIan Dowse 		     == NULL) {
767a3d8f94bSIan Dowse 			snprintf(errbuf, sizeof errbuf,
768a3d8f94bSIan Dowse 			    "af %d sotype SOCK_DGRAM not supported",
769a3d8f94bSIan Dowse 			     ai->ai_family);
770a3d8f94bSIan Dowse 			return (TRYRET_LOCALERR);
771a3d8f94bSIan Dowse 		}
772a3d8f94bSIan Dowse 		if ((nconf_mnt = getnetconf_cached(netid_mnt)) == NULL) {
773ba33efd9SIan Dowse 			snprintf(errbuf, sizeof errbuf, "%s: %s", netid_mnt,
774317d5933SIan Dowse 			    nc_sperror());
775317d5933SIan Dowse 			return (TRYRET_LOCALERR);
776317d5933SIan Dowse 		}
777317d5933SIan Dowse 	}
778317d5933SIan Dowse 
779317d5933SIan Dowse tryagain:
780011981fdSRick Macklem 	if (trymntmode == V4) {
781011981fdSRick Macklem 		nfsvers = 4;
7826575d184SEdward Tomasz Napierala 		mntvers = 3; /* Workaround for GCC. */
783011981fdSRick Macklem 	} else if (trymntmode == V2) {
784317d5933SIan Dowse 		nfsvers = 2;
785317d5933SIan Dowse 		mntvers = 1;
786317d5933SIan Dowse 	} else {
787317d5933SIan Dowse 		nfsvers = 3;
788317d5933SIan Dowse 		mntvers = 3;
789317d5933SIan Dowse 	}
790317d5933SIan Dowse 
791317d5933SIan Dowse 	if (portspec != NULL) {
792317d5933SIan Dowse 		/* `ai' contains the complete nfsd sockaddr. */
793317d5933SIan Dowse 		nfs_nb.buf = ai->ai_addr;
794317d5933SIan Dowse 		nfs_nb.len = nfs_nb.maxlen = ai->ai_addrlen;
795317d5933SIan Dowse 	} else {
796317d5933SIan Dowse 		/* Ask the remote rpcbind. */
797317d5933SIan Dowse 		nfs_nb.buf = &nfs_ss;
798317d5933SIan Dowse 		nfs_nb.len = nfs_nb.maxlen = sizeof nfs_ss;
799317d5933SIan Dowse 
8000775314bSDoug Rabson 		if (!rpcb_getaddr(NFS_PROGRAM, nfsvers, nconf, &nfs_nb,
801317d5933SIan Dowse 		    hostp)) {
802317d5933SIan Dowse 			if (rpc_createerr.cf_stat == RPC_PROGVERSMISMATCH &&
803317d5933SIan Dowse 			    trymntmode == ANY) {
804317d5933SIan Dowse 				trymntmode = V2;
805317d5933SIan Dowse 				goto tryagain;
806317d5933SIan Dowse 			}
807317d5933SIan Dowse 			snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s",
808317d5933SIan Dowse 			    netid, hostp, spec,
809317d5933SIan Dowse 			    clnt_spcreateerror("RPCPROG_NFS"));
810317d5933SIan Dowse 			return (returncode(rpc_createerr.cf_stat,
811317d5933SIan Dowse 			    &rpc_createerr.cf_error));
812317d5933SIan Dowse 		}
813317d5933SIan Dowse 	}
814317d5933SIan Dowse 
815317d5933SIan Dowse 	/* Check that the server (nfsd) responds on the port we have chosen. */
8160775314bSDoug Rabson 	clp = clnt_tli_create(RPC_ANYFD, nconf, &nfs_nb, NFS_PROGRAM, nfsvers,
817317d5933SIan Dowse 	    0, 0);
818317d5933SIan Dowse 	if (clp == NULL) {
819317d5933SIan Dowse 		snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid,
820317d5933SIan Dowse 		    hostp, spec, clnt_spcreateerror("nfsd: RPCPROG_NFS"));
821317d5933SIan Dowse 		return (returncode(rpc_createerr.cf_stat,
822317d5933SIan Dowse 		    &rpc_createerr.cf_error));
823317d5933SIan Dowse 	}
8244b5bc283SCraig Rodrigues 	if (sotype == SOCK_DGRAM && noconn == 0) {
825eca1c24eSIan Dowse 		/*
826eca1c24eSIan Dowse 		 * Use connect(), to match what the kernel does. This
827eca1c24eSIan Dowse 		 * catches cases where the server responds from the
828eca1c24eSIan Dowse 		 * wrong source address.
829eca1c24eSIan Dowse 		 */
830eca1c24eSIan Dowse 		doconnect = 1;
831eca1c24eSIan Dowse 		if (!clnt_control(clp, CLSET_CONNECT, (char *)&doconnect)) {
832eca1c24eSIan Dowse 			clnt_destroy(clp);
833eca1c24eSIan Dowse 			snprintf(errbuf, sizeof errbuf,
834eca1c24eSIan Dowse 			    "[%s] %s:%s: CLSET_CONNECT failed", netid, hostp,
835eca1c24eSIan Dowse 			    spec);
836eca1c24eSIan Dowse 			return (TRYRET_LOCALERR);
837eca1c24eSIan Dowse 		}
838eca1c24eSIan Dowse 	}
839eca1c24eSIan Dowse 
840317d5933SIan Dowse 	try.tv_sec = 10;
841317d5933SIan Dowse 	try.tv_usec = 0;
8426575d184SEdward Tomasz Napierala 	clntstat = clnt_call(clp, NFSPROC_NULL, (xdrproc_t)xdr_void, NULL,
843011981fdSRick Macklem 			 (xdrproc_t)xdr_void, NULL, try);
8446575d184SEdward Tomasz Napierala 	if (clntstat != RPC_SUCCESS) {
8456575d184SEdward Tomasz Napierala 		if (clntstat == RPC_PROGVERSMISMATCH && trymntmode == ANY) {
846317d5933SIan Dowse 			clnt_destroy(clp);
847317d5933SIan Dowse 			trymntmode = V2;
848317d5933SIan Dowse 			goto tryagain;
849317d5933SIan Dowse 		}
850317d5933SIan Dowse 		clnt_geterr(clp, &rpcerr);
851317d5933SIan Dowse 		snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid,
852317d5933SIan Dowse 		    hostp, spec, clnt_sperror(clp, "NFSPROC_NULL"));
853317d5933SIan Dowse 		clnt_destroy(clp);
8546575d184SEdward Tomasz Napierala 		return (returncode(clntstat, &rpcerr));
855317d5933SIan Dowse 	}
856317d5933SIan Dowse 	clnt_destroy(clp);
857317d5933SIan Dowse 
858011981fdSRick Macklem 	/*
859011981fdSRick Macklem 	 * For NFSv4, there is no mount protocol.
860011981fdSRick Macklem 	 */
861011981fdSRick Macklem 	if (trymntmode == V4) {
862011981fdSRick Macklem 		/*
863011981fdSRick Macklem 		 * Store the server address in nfsargsp, making
864011981fdSRick Macklem 		 * sure to copy any locally allocated structures.
865011981fdSRick Macklem 		 */
866011981fdSRick Macklem 		addrlen = nfs_nb.len;
867011981fdSRick Macklem 		addr = malloc(addrlen);
868011981fdSRick Macklem 		if (addr == NULL)
869011981fdSRick Macklem 			err(1, "malloc");
870011981fdSRick Macklem 		bcopy(nfs_nb.buf, addr, addrlen);
871011981fdSRick Macklem 
872011981fdSRick Macklem 		build_iovec(iov, iovlen, "addr", addr, addrlen);
873011981fdSRick Macklem 		secname = sec_num_to_name(secflavor);
8746575d184SEdward Tomasz Napierala 		if (secname != NULL) {
8756575d184SEdward Tomasz Napierala 			build_iovec(iov, iovlen, "sec",
8766575d184SEdward Tomasz Napierala 			    __DECONST(void *, secname), (size_t)-1);
8776575d184SEdward Tomasz Napierala 		}
878011981fdSRick Macklem 		build_iovec(iov, iovlen, "nfsv4", NULL, 0);
879011981fdSRick Macklem 		build_iovec(iov, iovlen, "dirpath", spec, (size_t)-1);
880011981fdSRick Macklem 
881011981fdSRick Macklem 		return (TRYRET_SUCCESS);
882011981fdSRick Macklem 	}
883011981fdSRick Macklem 
8840775314bSDoug Rabson 	/* Send the MOUNTPROC_MNT RPC to get the root filehandle. */
885317d5933SIan Dowse 	try.tv_sec = 10;
886317d5933SIan Dowse 	try.tv_usec = 0;
8870775314bSDoug Rabson 	clp = clnt_tp_create(hostp, MOUNTPROG, mntvers, nconf_mnt);
888317d5933SIan Dowse 	if (clp == NULL) {
889ba33efd9SIan Dowse 		snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid_mnt,
890317d5933SIan Dowse 		    hostp, spec, clnt_spcreateerror("RPCMNT: clnt_create"));
891317d5933SIan Dowse 		return (returncode(rpc_createerr.cf_stat,
892317d5933SIan Dowse 		    &rpc_createerr.cf_error));
893317d5933SIan Dowse 	}
894317d5933SIan Dowse 	clp->cl_auth = authsys_create_default();
895a9148abdSDoug Rabson 	nfhret.auth = secflavor;
896317d5933SIan Dowse 	nfhret.vers = mntvers;
8976575d184SEdward Tomasz Napierala 	clntstat = clnt_call(clp, MOUNTPROC_MNT, (xdrproc_t)xdr_dir, spec,
898c04affffSMatthew N. Dodd 			 (xdrproc_t)xdr_fh, &nfhret,
899317d5933SIan Dowse 	    try);
900317d5933SIan Dowse 	auth_destroy(clp->cl_auth);
9016575d184SEdward Tomasz Napierala 	if (clntstat != RPC_SUCCESS) {
9026575d184SEdward Tomasz Napierala 		if (clntstat == RPC_PROGVERSMISMATCH && trymntmode == ANY) {
903317d5933SIan Dowse 			clnt_destroy(clp);
904317d5933SIan Dowse 			trymntmode = V2;
905317d5933SIan Dowse 			goto tryagain;
906317d5933SIan Dowse 		}
907317d5933SIan Dowse 		clnt_geterr(clp, &rpcerr);
908ba33efd9SIan Dowse 		snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid_mnt,
909317d5933SIan Dowse 		    hostp, spec, clnt_sperror(clp, "RPCPROG_MNT"));
910317d5933SIan Dowse 		clnt_destroy(clp);
9116575d184SEdward Tomasz Napierala 		return (returncode(clntstat, &rpcerr));
912317d5933SIan Dowse 	}
913317d5933SIan Dowse 	clnt_destroy(clp);
914317d5933SIan Dowse 
915317d5933SIan Dowse 	if (nfhret.stat != 0) {
916ba33efd9SIan Dowse 		snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid_mnt,
917317d5933SIan Dowse 		    hostp, spec, strerror(nfhret.stat));
918317d5933SIan Dowse 		return (TRYRET_REMOTEERR);
919317d5933SIan Dowse 	}
920317d5933SIan Dowse 
921317d5933SIan Dowse 	/*
922317d5933SIan Dowse 	 * Store the filehandle and server address in nfsargsp, making
923317d5933SIan Dowse 	 * sure to copy any locally allocated structures.
924317d5933SIan Dowse 	 */
9254b5bc283SCraig Rodrigues 	addrlen = nfs_nb.len;
9264b5bc283SCraig Rodrigues 	addr = malloc(addrlen);
9274b5bc283SCraig Rodrigues 	fhsize = nfhret.fhsize;
9284b5bc283SCraig Rodrigues 	fh = malloc(fhsize);
9294b5bc283SCraig Rodrigues 	if (addr == NULL || fh == NULL)
930317d5933SIan Dowse 		err(1, "malloc");
9314b5bc283SCraig Rodrigues 	bcopy(nfs_nb.buf, addr, addrlen);
9324b5bc283SCraig Rodrigues 	bcopy(nfhret.nfh, fh, fhsize);
933317d5933SIan Dowse 
9344b5bc283SCraig Rodrigues 	build_iovec(iov, iovlen, "addr", addr, addrlen);
9354b5bc283SCraig Rodrigues 	build_iovec(iov, iovlen, "fh", fh, fhsize);
936a9148abdSDoug Rabson 	secname = sec_num_to_name(nfhret.auth);
9376575d184SEdward Tomasz Napierala 	if (secname) {
9386575d184SEdward Tomasz Napierala 		build_iovec(iov, iovlen, "sec",
9396575d184SEdward Tomasz Napierala 		    __DECONST(void *, secname), (size_t)-1);
9406575d184SEdward Tomasz Napierala 	}
941317d5933SIan Dowse 	if (nfsvers == 3)
9424b5bc283SCraig Rodrigues 		build_iovec(iov, iovlen, "nfsv3", NULL, 0);
943317d5933SIan Dowse 
944317d5933SIan Dowse 	return (TRYRET_SUCCESS);
945317d5933SIan Dowse }
946317d5933SIan Dowse 
9470c269d1fSCraig Rodrigues /*
948317d5933SIan Dowse  * Catagorise a RPC return status and error into an `enum tryret'
949317d5933SIan Dowse  * return code.
950317d5933SIan Dowse  */
951b352972bSXin LI static enum tryret
9526575d184SEdward Tomasz Napierala returncode(enum clnt_stat clntstat, struct rpc_err *rpcerr)
953317d5933SIan Dowse {
9546575d184SEdward Tomasz Napierala 
9556575d184SEdward Tomasz Napierala 	switch (clntstat) {
956317d5933SIan Dowse 	case RPC_TIMEDOUT:
957317d5933SIan Dowse 		return (TRYRET_TIMEOUT);
958317d5933SIan Dowse 	case RPC_PMAPFAILURE:
959317d5933SIan Dowse 	case RPC_PROGNOTREGISTERED:
960317d5933SIan Dowse 	case RPC_PROGVERSMISMATCH:
961eca1c24eSIan Dowse 	/* XXX, these can be local or remote. */
962eca1c24eSIan Dowse 	case RPC_CANTSEND:
963eca1c24eSIan Dowse 	case RPC_CANTRECV:
964317d5933SIan Dowse 		return (TRYRET_REMOTEERR);
965317d5933SIan Dowse 	case RPC_SYSTEMERROR:
966317d5933SIan Dowse 		switch (rpcerr->re_errno) {
967317d5933SIan Dowse 		case ETIMEDOUT:
968317d5933SIan Dowse 			return (TRYRET_TIMEOUT);
969317d5933SIan Dowse 		case ENOMEM:
970317d5933SIan Dowse 			break;
971317d5933SIan Dowse 		default:
972317d5933SIan Dowse 			return (TRYRET_REMOTEERR);
973317d5933SIan Dowse 		}
974317d5933SIan Dowse 		/* FALLTHROUGH */
975317d5933SIan Dowse 	default:
976317d5933SIan Dowse 		break;
977317d5933SIan Dowse 	}
978317d5933SIan Dowse 	return (TRYRET_LOCALERR);
979317d5933SIan Dowse }
980317d5933SIan Dowse 
981317d5933SIan Dowse /*
982a3d8f94bSIan Dowse  * Look up a netid based on an address family and socket type.
983a3d8f94bSIan Dowse  * `af' is the address family, and `sotype' is SOCK_DGRAM or SOCK_STREAM.
984a3d8f94bSIan Dowse  *
985a3d8f94bSIan Dowse  * XXX there should be a library function for this.
986a3d8f94bSIan Dowse  */
987b352972bSXin LI static const char *
98833924ad4SWarner Losh netidbytype(int af, int sotype)
98933924ad4SWarner Losh {
990a3d8f94bSIan Dowse 	struct nc_protos *p;
991a3d8f94bSIan Dowse 
992a3d8f94bSIan Dowse 	for (p = nc_protos; p->netid != NULL; p++) {
993a3d8f94bSIan Dowse 		if (af != p->af || sotype != p->sotype)
994a3d8f94bSIan Dowse 			continue;
995a3d8f94bSIan Dowse 		return (p->netid);
996a3d8f94bSIan Dowse 	}
997a3d8f94bSIan Dowse 	return (NULL);
998a3d8f94bSIan Dowse }
999a3d8f94bSIan Dowse 
1000a3d8f94bSIan Dowse /*
1001a3d8f94bSIan Dowse  * Look up a netconfig entry based on a netid, and cache the result so
1002a3d8f94bSIan Dowse  * that we don't need to remember to call freenetconfigent().
1003a3d8f94bSIan Dowse  *
1004a3d8f94bSIan Dowse  * Otherwise it behaves just like getnetconfigent(), so nc_*error()
1005a3d8f94bSIan Dowse  * work on failure.
1006a3d8f94bSIan Dowse  */
1007b352972bSXin LI static struct netconfig *
100833924ad4SWarner Losh getnetconf_cached(const char *netid)
100933924ad4SWarner Losh {
1010a3d8f94bSIan Dowse 	static struct nc_entry {
1011a3d8f94bSIan Dowse 		struct netconfig *nconf;
1012a3d8f94bSIan Dowse 		struct nc_entry *next;
1013a3d8f94bSIan Dowse 	} *head;
1014a3d8f94bSIan Dowse 	struct nc_entry *p;
1015a3d8f94bSIan Dowse 	struct netconfig *nconf;
1016a3d8f94bSIan Dowse 
1017a3d8f94bSIan Dowse 	for (p = head; p != NULL; p = p->next)
1018a3d8f94bSIan Dowse 		if (strcmp(netid, p->nconf->nc_netid) == 0)
1019a3d8f94bSIan Dowse 			return (p->nconf);
1020a3d8f94bSIan Dowse 
1021a3d8f94bSIan Dowse 	if ((nconf = getnetconfigent(netid)) == NULL)
1022a3d8f94bSIan Dowse 		return (NULL);
1023a3d8f94bSIan Dowse 	if ((p = malloc(sizeof(*p))) == NULL)
1024a3d8f94bSIan Dowse 		err(1, "malloc");
1025a3d8f94bSIan Dowse 	p->nconf = nconf;
1026a3d8f94bSIan Dowse 	p->next = head;
1027a3d8f94bSIan Dowse 	head = p;
1028a3d8f94bSIan Dowse 
1029a3d8f94bSIan Dowse 	return (p->nconf);
1030a3d8f94bSIan Dowse }
1031a3d8f94bSIan Dowse 
1032a3d8f94bSIan Dowse /*
10338fae3551SRodney W. Grimes  * xdr routines for mount rpc's
10348fae3551SRodney W. Grimes  */
1035b352972bSXin LI static int
103633924ad4SWarner Losh xdr_dir(XDR *xdrsp, char *dirp)
10378fae3551SRodney W. Grimes {
10380775314bSDoug Rabson 	return (xdr_string(xdrsp, &dirp, MNTPATHLEN));
10398fae3551SRodney W. Grimes }
10408fae3551SRodney W. Grimes 
1041b352972bSXin LI static int
104233924ad4SWarner Losh xdr_fh(XDR *xdrsp, struct nfhret *np)
10438fae3551SRodney W. Grimes {
10443d438ad6SDavid E. O'Brien 	int i;
1045a62dc406SDoug Rabson 	long auth, authcnt, authfnd = 0;
1046a62dc406SDoug Rabson 
1047a62dc406SDoug Rabson 	if (!xdr_u_long(xdrsp, &np->stat))
10488fae3551SRodney W. Grimes 		return (0);
10498fae3551SRodney W. Grimes 	if (np->stat)
10508fae3551SRodney W. Grimes 		return (1);
1051a62dc406SDoug Rabson 	switch (np->vers) {
1052a62dc406SDoug Rabson 	case 1:
10530775314bSDoug Rabson 		np->fhsize = NFS_FHSIZE;
10540775314bSDoug Rabson 		return (xdr_opaque(xdrsp, (caddr_t)np->nfh, NFS_FHSIZE));
1055a62dc406SDoug Rabson 	case 3:
1056a62dc406SDoug Rabson 		if (!xdr_long(xdrsp, &np->fhsize))
1057a62dc406SDoug Rabson 			return (0);
10580775314bSDoug Rabson 		if (np->fhsize <= 0 || np->fhsize > NFS3_FHSIZE)
1059a62dc406SDoug Rabson 			return (0);
1060a62dc406SDoug Rabson 		if (!xdr_opaque(xdrsp, (caddr_t)np->nfh, np->fhsize))
1061a62dc406SDoug Rabson 			return (0);
1062a62dc406SDoug Rabson 		if (!xdr_long(xdrsp, &authcnt))
1063a62dc406SDoug Rabson 			return (0);
1064a62dc406SDoug Rabson 		for (i = 0; i < authcnt; i++) {
1065a62dc406SDoug Rabson 			if (!xdr_long(xdrsp, &auth))
1066a62dc406SDoug Rabson 				return (0);
10674b5bc283SCraig Rodrigues 			if (np->auth == -1) {
10684b5bc283SCraig Rodrigues 				np->auth = auth;
1069a62dc406SDoug Rabson 				authfnd++;
10704b5bc283SCraig Rodrigues 			} else if (auth == np->auth) {
10714b5bc283SCraig Rodrigues 				authfnd++;
10724b5bc283SCraig Rodrigues 			}
1073a62dc406SDoug Rabson 		}
1074a62dc406SDoug Rabson 		/*
1075a62dc406SDoug Rabson 		 * Some servers, such as DEC's OSF/1 return a nil authenticator
1076a62dc406SDoug Rabson 		 * list to indicate RPCAUTH_UNIX.
1077a62dc406SDoug Rabson 		 */
10784b5bc283SCraig Rodrigues 		if (authcnt == 0 && np->auth == -1)
10794b5bc283SCraig Rodrigues 			np->auth = AUTH_SYS;
10804b5bc283SCraig Rodrigues 		if (!authfnd && (authcnt > 0 || np->auth != AUTH_SYS))
1081a62dc406SDoug Rabson 			np->stat = EAUTH;
1082a62dc406SDoug Rabson 		return (1);
108380c7cc1cSPedro F. Giffuni 	}
1084a62dc406SDoug Rabson 	return (0);
10858fae3551SRodney W. Grimes }
10868fae3551SRodney W. Grimes 
1087b352972bSXin LI static void
1088b352972bSXin LI usage(void)
10898fae3551SRodney W. Grimes {
109046fc8f78SPhilippe Charnier 	(void)fprintf(stderr, "%s\n%s\n%s\n%s\n",
109186ce6a83SRobert Watson "usage: mount_nfs [-23bcdiLlNPsTU] [-a maxreadahead] [-D deadthresh]",
10928d646af5SRuslan Ermilov "                 [-g maxgroups] [-I readdirsize] [-o options] [-R retrycnt]",
10938d646af5SRuslan Ermilov "                 [-r readsize] [-t timeout] [-w writesize] [-x retrans]",
10948d646af5SRuslan Ermilov "                 rhost:path node");
10958fae3551SRodney W. Grimes 	exit(1);
10968fae3551SRodney W. Grimes }
1097