xref: /freebsd/sbin/mount_nfs/mount_nfs.c (revision 0b8224d1)
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 
358fae3551SRodney W. Grimes #include <sys/param.h>
36011981fdSRick Macklem #include <sys/linker.h>
37011981fdSRick Macklem #include <sys/module.h>
388fae3551SRodney W. Grimes #include <sys/mount.h>
398360efbdSAlfred Perlstein #include <sys/socket.h>
408fae3551SRodney W. Grimes #include <sys/stat.h>
418fae3551SRodney W. Grimes #include <sys/syslog.h>
42c5aa1dc8SCraig Rodrigues #include <sys/uio.h>
438fae3551SRodney W. Grimes 
448fae3551SRodney W. Grimes #include <rpc/rpc.h>
458fae3551SRodney W. Grimes #include <rpc/pmap_clnt.h>
468fae3551SRodney W. Grimes #include <rpc/pmap_prot.h>
470775314bSDoug Rabson #include <rpcsvc/nfs_prot.h>
480775314bSDoug Rabson #include <rpcsvc/mount.h>
498fae3551SRodney W. Grimes 
5056c049a0SRick Macklem #include <fs/nfs/nfsproto.h>
5156c049a0SRick Macklem #include <fs/nfs/nfsv4_errstr.h>
528fae3551SRodney W. Grimes 
538fae3551SRodney W. Grimes #include <arpa/inet.h>
548cd9718fSGleb Smirnoff #include <net/route.h>
558cd9718fSGleb Smirnoff #include <net/if.h>
568fae3551SRodney W. Grimes 
578fae3551SRodney W. Grimes #include <ctype.h>
588fae3551SRodney W. Grimes #include <err.h>
598fae3551SRodney W. Grimes #include <errno.h>
608360efbdSAlfred Perlstein #include <fcntl.h>
618fae3551SRodney W. Grimes #include <netdb.h>
62c0d14b02SRick Macklem #include <stdbool.h>
638fae3551SRodney W. Grimes #include <stdio.h>
648fae3551SRodney W. Grimes #include <stdlib.h>
65c04affffSMatthew N. Dodd #include <string.h>
668fae3551SRodney W. Grimes #include <strings.h>
675e074e31SGarrett Wollman #include <sysexits.h>
688fae3551SRodney W. Grimes #include <unistd.h>
698fae3551SRodney W. Grimes 
708fae3551SRodney W. Grimes #include "mntopts.h"
71a69497d7SMatthew Dillon #include "mounttab.h"
728fae3551SRodney W. Grimes 
73a3d8f94bSIan Dowse /* Table for af,sotype -> netid conversions. */
746575d184SEdward Tomasz Napierala static struct nc_protos {
754b5bc283SCraig Rodrigues 	const char *netid;
76a3d8f94bSIan Dowse 	int af;
77a3d8f94bSIan Dowse 	int sotype;
78a3d8f94bSIan Dowse } nc_protos[] = {
79a3d8f94bSIan Dowse 	{"udp",		AF_INET,	SOCK_DGRAM},
80a3d8f94bSIan Dowse 	{"tcp",		AF_INET,	SOCK_STREAM},
81a3d8f94bSIan Dowse 	{"udp6",	AF_INET6,	SOCK_DGRAM},
82a3d8f94bSIan Dowse 	{"tcp6",	AF_INET6,	SOCK_STREAM},
83b535b298SCraig Rodrigues 	{NULL,		0,		0}
84a3d8f94bSIan Dowse };
85a3d8f94bSIan Dowse 
868fae3551SRodney W. Grimes struct nfhret {
878fae3551SRodney W. Grimes 	u_long		stat;
88a62dc406SDoug Rabson 	long		vers;
89a62dc406SDoug Rabson 	long		auth;
90a62dc406SDoug Rabson 	long		fhsize;
910775314bSDoug Rabson 	u_char		nfh[NFS3_FHSIZE];
928fae3551SRodney W. Grimes };
9308a8d16cSRick Macklem #define	BGRND		0x01
9408a8d16cSRick Macklem #define	ISBGRND		0x02
9508a8d16cSRick Macklem #define	OF_NOINET4	0x04
9608a8d16cSRick Macklem #define	OF_NOINET6	0x08
9708a8d16cSRick Macklem #define	BGRNDNOW	0x10
986575d184SEdward Tomasz Napierala static int retrycnt = -1;
996575d184SEdward Tomasz Napierala static int opflags = 0;
1006575d184SEdward Tomasz Napierala static int nfsproto = IPPROTO_TCP;
1016575d184SEdward Tomasz Napierala static int mnttcp_ok = 1;
1026575d184SEdward Tomasz Napierala static int noconn = 0;
1036575d184SEdward Tomasz Napierala /* The 'portspec' is the server nfs port; NULL means look up via rpcbind. */
1046575d184SEdward Tomasz Napierala static const char *portspec = NULL;
1056575d184SEdward Tomasz Napierala static struct sockaddr *addr;
1066575d184SEdward Tomasz Napierala static int addrlen = 0;
1076575d184SEdward Tomasz Napierala static u_char *fh = NULL;
1086575d184SEdward Tomasz Napierala static int fhsize = 0;
1096575d184SEdward Tomasz Napierala static int secflavor = -1;
1106575d184SEdward Tomasz Napierala static int got_principal = 0;
1114b5bc283SCraig Rodrigues 
1126575d184SEdward Tomasz Napierala static enum mountmode {
1132cd1c32cSDoug Rabson 	ANY,
1142cd1c32cSDoug Rabson 	V2,
1150c269d1fSCraig Rodrigues 	V3,
116011981fdSRick Macklem 	V4
1172cd1c32cSDoug Rabson } mountmode = ANY;
1188fae3551SRodney W. Grimes 
119317d5933SIan Dowse /* Return codes for nfs_tryproto. */
120317d5933SIan Dowse enum tryret {
121317d5933SIan Dowse 	TRYRET_SUCCESS,
122317d5933SIan Dowse 	TRYRET_TIMEOUT,		/* No response received. */
123317d5933SIan Dowse 	TRYRET_REMOTEERR,	/* Error received from remote server. */
124317d5933SIan Dowse 	TRYRET_LOCALERR		/* Local failure. */
125317d5933SIan Dowse };
126317d5933SIan Dowse 
1276575d184SEdward Tomasz Napierala static int	sec_name_to_num(const char *sec);
1286575d184SEdward Tomasz Napierala static const char	*sec_num_to_name(int num);
1297cab630bSRick Macklem static int	getnfsargs(char **, char **, struct iovec **iov, int *iovlen);
13085429990SWarner Losh /* void	set_rpc_maxgrouplist(int); */
131b352972bSXin LI static struct netconfig *getnetconf_cached(const char *netid);
132b352972bSXin LI static const char	*netidbytype(int af, int sotype);
133b352972bSXin LI static void	usage(void) __dead2;
134b352972bSXin LI static int	xdr_dir(XDR *, char *);
135b352972bSXin LI static int	xdr_fh(XDR *, struct nfhret *);
136b352972bSXin LI static enum tryret nfs_tryproto(struct addrinfo *ai, char *hostp, char *spec,
1374b5bc283SCraig Rodrigues     char **errstr, struct iovec **iov, int *iovlen);
138b352972bSXin LI static enum tryret returncode(enum clnt_stat stat, struct rpc_err *rpcerr);
1398fae3551SRodney W. Grimes 
1408fae3551SRodney W. Grimes int
main(int argc,char * argv[])14133924ad4SWarner Losh main(int argc, char *argv[])
1428fae3551SRodney W. Grimes {
1433d438ad6SDavid E. O'Brien 	int c;
144c5aa1dc8SCraig Rodrigues 	struct iovec *iov;
1457039dfb3SJung-uk Kim 	int num, iovlen;
1467cab630bSRick Macklem 	char *host, *mntname, *p, *spec, *tmp;
147a6b26402SCraig Rodrigues 	char mntpath[MAXPATHLEN], errmsg[255];
1486575d184SEdward Tomasz Napierala 	char hostname[MAXHOSTNAMELEN + 1], gssn[MAXHOSTNAMELEN + 50];
14956c049a0SRick Macklem 	const char *gssname, *nmount_errstr;
150c0d14b02SRick Macklem 	bool softintr;
1518fae3551SRodney W. Grimes 
152c0d14b02SRick Macklem 	softintr = false;
153c5aa1dc8SCraig Rodrigues 	iov = NULL;
154c5aa1dc8SCraig Rodrigues 	iovlen = 0;
155a6b26402SCraig Rodrigues 	memset(errmsg, 0, sizeof(errmsg));
156011981fdSRick Macklem 	gssname = NULL;
157c5aa1dc8SCraig Rodrigues 
1588fae3551SRodney W. Grimes 	while ((c = getopt(argc, argv,
15986ce6a83SRobert Watson 	    "23a:bcdD:g:I:iLlNo:PR:r:sTt:w:x:U")) != -1)
1608fae3551SRodney W. Grimes 		switch (c) {
1612cd1c32cSDoug Rabson 		case '2':
1622cd1c32cSDoug Rabson 			mountmode = V2;
1632cd1c32cSDoug Rabson 			break;
164a62dc406SDoug Rabson 		case '3':
1652cd1c32cSDoug Rabson 			mountmode = V3;
166a62dc406SDoug Rabson 			break;
1678fae3551SRodney W. Grimes 		case 'a':
168baa0988aSJaakko Heinonen 			printf("-a deprecated, use -o readahead=<value>\n");
1694b5bc283SCraig Rodrigues 			build_iovec(&iov, &iovlen, "readahead", optarg, (size_t)-1);
1708fae3551SRodney W. Grimes 			break;
1718fae3551SRodney W. Grimes 		case 'b':
1728fae3551SRodney W. Grimes 			opflags |= BGRND;
1738fae3551SRodney W. Grimes 			break;
1748fae3551SRodney W. Grimes 		case 'c':
1754b5bc283SCraig Rodrigues 			printf("-c deprecated, use -o noconn\n");
1764b5bc283SCraig Rodrigues 			build_iovec(&iov, &iovlen, "noconn", NULL, 0);
1774b5bc283SCraig Rodrigues 			noconn = 1;
1788fae3551SRodney W. Grimes 			break;
1798fae3551SRodney W. Grimes 		case 'D':
1804b5bc283SCraig Rodrigues 			printf("-D deprecated, use -o deadthresh=<value>\n");
1814b5bc283SCraig Rodrigues 			build_iovec(&iov, &iovlen, "deadthresh", optarg, (size_t)-1);
1828fae3551SRodney W. Grimes 			break;
1838fae3551SRodney W. Grimes 		case 'd':
1844b5bc283SCraig Rodrigues 			printf("-d deprecated, use -o dumbtimer");
1854b5bc283SCraig Rodrigues 			build_iovec(&iov, &iovlen, "dumbtimer", NULL, 0);
1868fae3551SRodney W. Grimes 			break;
1878fae3551SRodney W. Grimes 		case 'g':
1884b5bc283SCraig Rodrigues 			printf("-g deprecated, use -o maxgroups");
1898fae3551SRodney W. Grimes 			num = strtol(optarg, &p, 10);
1908fae3551SRodney W. Grimes 			if (*p || num <= 0)
1918fae3551SRodney W. Grimes 				errx(1, "illegal -g value -- %s", optarg);
1924b5bc283SCraig Rodrigues 			//set_rpc_maxgrouplist(num);
1934b5bc283SCraig Rodrigues 			build_iovec(&iov, &iovlen, "maxgroups", optarg, (size_t)-1);
1948fae3551SRodney W. Grimes 			break;
195a62dc406SDoug Rabson 		case 'I':
1964b5bc283SCraig Rodrigues 			printf("-I deprecated, use -o readdirsize=<value>\n");
1974b5bc283SCraig Rodrigues 			build_iovec(&iov, &iovlen, "readdirsize", optarg, (size_t)-1);
198a62dc406SDoug Rabson 			break;
1998fae3551SRodney W. Grimes 		case 'i':
2004b5bc283SCraig Rodrigues 			printf("-i deprecated, use -o intr\n");
2014b5bc283SCraig Rodrigues 			build_iovec(&iov, &iovlen, "intr", NULL, 0);
202c0d14b02SRick Macklem 			softintr = true;
2038fae3551SRodney W. Grimes 			break;
20413190d87SAlfred Perlstein 		case 'L':
205d97326d0SSimon L. B. Nielsen 			printf("-L deprecated, use -o nolockd\n");
2064b5bc283SCraig Rodrigues 			build_iovec(&iov, &iovlen, "nolockd", NULL, 0);
20713190d87SAlfred Perlstein 			break;
2088fae3551SRodney W. Grimes 		case 'l':
2094b5bc283SCraig Rodrigues 			printf("-l deprecated, -o rdirplus\n");
2104b5bc283SCraig Rodrigues 			build_iovec(&iov, &iovlen, "rdirplus", NULL, 0);
2118fae3551SRodney W. Grimes 			break;
212cc75b131SJoerg Wunsch 		case 'N':
2134b5bc283SCraig Rodrigues 			printf("-N deprecated, do not specify -o resvport\n");
214cc75b131SJoerg Wunsch 			break;
2154b5bc283SCraig Rodrigues 		case 'o': {
2164b5bc283SCraig Rodrigues 			int pass_flag_to_nmount;
2174b5bc283SCraig Rodrigues 			char *opt = optarg;
2184b5bc283SCraig Rodrigues 			while (opt) {
2194b5bc283SCraig Rodrigues 				char *pval = NULL;
2204b5bc283SCraig Rodrigues 				char *pnextopt = NULL;
2216575d184SEdward Tomasz Napierala 				const char *val = "";
2224b5bc283SCraig Rodrigues 				pass_flag_to_nmount = 1;
2234b5bc283SCraig Rodrigues 				pnextopt = strchr(opt, ',');
224bdb40379SJaakko Heinonen 				if (pnextopt != NULL) {
225bdb40379SJaakko Heinonen 					*pnextopt = '\0';
226bdb40379SJaakko Heinonen 					pnextopt++;
227bdb40379SJaakko Heinonen 				}
228bdb40379SJaakko Heinonen 				pval = strchr(opt, '=');
2294b5bc283SCraig Rodrigues 				if (pval != NULL) {
2304b5bc283SCraig Rodrigues 					*pval = '\0';
2314b5bc283SCraig Rodrigues 					val = pval + 1;
2324b5bc283SCraig Rodrigues 				}
2334b5bc283SCraig Rodrigues 				if (strcmp(opt, "bg") == 0) {
2343fa88decSGarrett Wollman 					opflags |= BGRND;
2354b5bc283SCraig Rodrigues 					pass_flag_to_nmount=0;
23608a8d16cSRick Macklem 				} else if (strcmp(opt, "bgnow") == 0) {
23708a8d16cSRick Macklem 					opflags |= BGRNDNOW;
23808a8d16cSRick Macklem 					pass_flag_to_nmount=0;
2394b5bc283SCraig Rodrigues 				} else if (strcmp(opt, "fg") == 0) {
2404b5bc283SCraig Rodrigues 					/* same as not specifying -o bg */
2414b5bc283SCraig Rodrigues 					pass_flag_to_nmount=0;
242011981fdSRick Macklem 				} else if (strcmp(opt, "gssname") == 0) {
243011981fdSRick Macklem 					pass_flag_to_nmount = 0;
244011981fdSRick Macklem 					gssname = val;
2454b5bc283SCraig Rodrigues 				} else if (strcmp(opt, "mntudp") == 0) {
246a62dc406SDoug Rabson 					mnttcp_ok = 0;
247bf005f32SKris Kennaway 					nfsproto = IPPROTO_UDP;
2484b5bc283SCraig Rodrigues 				} else if (strcmp(opt, "udp") == 0) {
2494b5bc283SCraig Rodrigues 					nfsproto = IPPROTO_UDP;
2504b5bc283SCraig Rodrigues 				} else if (strcmp(opt, "tcp") == 0) {
251a62dc406SDoug Rabson 					nfsproto = IPPROTO_TCP;
2524b5bc283SCraig Rodrigues 				} else if (strcmp(opt, "noinet4") == 0) {
2534b5bc283SCraig Rodrigues 					pass_flag_to_nmount=0;
2544b5bc283SCraig Rodrigues 					opflags |= OF_NOINET4;
2554b5bc283SCraig Rodrigues 				} else if (strcmp(opt, "noinet6") == 0) {
2564b5bc283SCraig Rodrigues 					pass_flag_to_nmount=0;
2574b5bc283SCraig Rodrigues 					opflags |= OF_NOINET6;
2584b5bc283SCraig Rodrigues 				} else if (strcmp(opt, "noconn") == 0) {
2594b5bc283SCraig Rodrigues 					noconn = 1;
2604b5bc283SCraig Rodrigues 				} else if (strcmp(opt, "nfsv2") == 0) {
2614b5bc283SCraig Rodrigues 					pass_flag_to_nmount=0;
2624b5bc283SCraig Rodrigues 					mountmode = V2;
2634b5bc283SCraig Rodrigues 				} else if (strcmp(opt, "nfsv3") == 0) {
2644b5bc283SCraig Rodrigues 					mountmode = V3;
265011981fdSRick Macklem 				} else if (strcmp(opt, "nfsv4") == 0) {
266011981fdSRick Macklem 					pass_flag_to_nmount=0;
267011981fdSRick Macklem 					mountmode = V4;
268011981fdSRick Macklem 					nfsproto = IPPROTO_TCP;
269011981fdSRick Macklem 					if (portspec == NULL)
270011981fdSRick Macklem 						portspec = "2049";
2714b5bc283SCraig Rodrigues 				} else if (strcmp(opt, "port") == 0) {
2724b5bc283SCraig Rodrigues 					pass_flag_to_nmount=0;
2736575d184SEdward Tomasz Napierala 					asprintf(&tmp, "%d", atoi(val));
2746575d184SEdward Tomasz Napierala 					if (tmp == NULL)
275317d5933SIan Dowse 						err(1, "asprintf");
2766575d184SEdward Tomasz Napierala 					portspec = tmp;
277011981fdSRick Macklem 				} else if (strcmp(opt, "principal") == 0) {
278011981fdSRick Macklem 					got_principal = 1;
2795a06ac35SEdward Tomasz Napierala 				} else if (strcmp(opt, "proto") == 0) {
2805a06ac35SEdward Tomasz Napierala 					pass_flag_to_nmount=0;
2815a06ac35SEdward Tomasz Napierala 					if (strcmp(val, "tcp") == 0) {
2825a06ac35SEdward Tomasz Napierala 						nfsproto = IPPROTO_TCP;
2835a06ac35SEdward Tomasz Napierala 						opflags |= OF_NOINET6;
2845a06ac35SEdward Tomasz Napierala 						build_iovec(&iov, &iovlen,
2855a06ac35SEdward Tomasz Napierala 						    "tcp", NULL, 0);
2865a06ac35SEdward Tomasz Napierala 					} else if (strcmp(val, "udp") == 0) {
2875a06ac35SEdward Tomasz Napierala 						mnttcp_ok = 0;
2885a06ac35SEdward Tomasz Napierala 						nfsproto = IPPROTO_UDP;
2895a06ac35SEdward Tomasz Napierala 						opflags |= OF_NOINET6;
2905a06ac35SEdward Tomasz Napierala 						build_iovec(&iov, &iovlen,
2915a06ac35SEdward Tomasz Napierala 						    "udp", NULL, 0);
2925a06ac35SEdward Tomasz Napierala 					} else if (strcmp(val, "tcp6") == 0) {
2935a06ac35SEdward Tomasz Napierala 						nfsproto = IPPROTO_TCP;
2945a06ac35SEdward Tomasz Napierala 						opflags |= OF_NOINET4;
2955a06ac35SEdward Tomasz Napierala 						build_iovec(&iov, &iovlen,
2965a06ac35SEdward Tomasz Napierala 						    "tcp", NULL, 0);
2975a06ac35SEdward Tomasz Napierala 					} else if (strcmp(val, "udp6") == 0) {
2985a06ac35SEdward Tomasz Napierala 						mnttcp_ok = 0;
2995a06ac35SEdward Tomasz Napierala 						nfsproto = IPPROTO_UDP;
3005a06ac35SEdward Tomasz Napierala 						opflags |= OF_NOINET4;
3015a06ac35SEdward Tomasz Napierala 						build_iovec(&iov, &iovlen,
3025a06ac35SEdward Tomasz Napierala 						    "udp", NULL, 0);
3035a06ac35SEdward Tomasz Napierala 					} else {
3045a06ac35SEdward Tomasz Napierala 						errx(1,
3055a06ac35SEdward Tomasz Napierala 						    "illegal proto value -- %s",
3065a06ac35SEdward Tomasz Napierala 						    val);
3075a06ac35SEdward Tomasz Napierala 					}
308a9148abdSDoug Rabson 				} else if (strcmp(opt, "sec") == 0) {
309a9148abdSDoug Rabson 					/*
310a9148abdSDoug Rabson 					 * Don't add this option to
311a9148abdSDoug Rabson 					 * the iovec yet - we will
312a9148abdSDoug Rabson 					 * negotiate which sec flavor
313a9148abdSDoug Rabson 					 * to use with the remote
314a9148abdSDoug Rabson 					 * mountd.
315a9148abdSDoug Rabson 					 */
316a9148abdSDoug Rabson 					pass_flag_to_nmount=0;
317a9148abdSDoug Rabson 					secflavor = sec_name_to_num(val);
318a9148abdSDoug Rabson 					if (secflavor < 0) {
319a9148abdSDoug Rabson 						errx(1,
320a9148abdSDoug Rabson 						    "illegal sec value -- %s",
321a9148abdSDoug Rabson 						    val);
322a9148abdSDoug Rabson 					}
3234b5bc283SCraig Rodrigues 				} else if (strcmp(opt, "retrycnt") == 0) {
3244b5bc283SCraig Rodrigues 					pass_flag_to_nmount=0;
3254b5bc283SCraig Rodrigues 					num = strtol(val, &p, 10);
3264b5bc283SCraig Rodrigues 					if (*p || num < 0)
3274b5bc283SCraig Rodrigues 						errx(1, "illegal retrycnt value -- %s", val);
3284b5bc283SCraig Rodrigues 					retrycnt = num;
3294b5bc283SCraig Rodrigues 				} else if (strcmp(opt, "maxgroups") == 0) {
3304b5bc283SCraig Rodrigues 					num = strtol(val, &p, 10);
3314b5bc283SCraig Rodrigues 					if (*p || num <= 0)
3324b5bc283SCraig Rodrigues 						errx(1, "illegal maxgroups value -- %s", val);
3334b5bc283SCraig Rodrigues 					//set_rpc_maxgrouplist(num);
334528c159fSBjoern A. Zeeb 				} else if (strcmp(opt, "vers") == 0) {
335528c159fSBjoern A. Zeeb 					num = strtol(val, &p, 10);
336528c159fSBjoern A. Zeeb 					if (*p || num <= 0)
337528c159fSBjoern A. Zeeb 						errx(1, "illegal vers value -- "
338528c159fSBjoern A. Zeeb 						    "%s", val);
339528c159fSBjoern A. Zeeb 					switch (num) {
340528c159fSBjoern A. Zeeb 					case 2:
341528c159fSBjoern A. Zeeb 						mountmode = V2;
342528c159fSBjoern A. Zeeb 						break;
343528c159fSBjoern A. Zeeb 					case 3:
344528c159fSBjoern A. Zeeb 						mountmode = V3;
345528c159fSBjoern A. Zeeb 						build_iovec(&iov, &iovlen,
346528c159fSBjoern A. Zeeb 						    "nfsv3", NULL, 0);
347528c159fSBjoern A. Zeeb 						break;
348528c159fSBjoern A. Zeeb 					case 4:
349528c159fSBjoern A. Zeeb 						mountmode = V4;
350528c159fSBjoern A. Zeeb 						nfsproto = IPPROTO_TCP;
351528c159fSBjoern A. Zeeb 						if (portspec == NULL)
352528c159fSBjoern A. Zeeb 							portspec = "2049";
353528c159fSBjoern A. Zeeb 						break;
354528c159fSBjoern A. Zeeb 					default:
355528c159fSBjoern A. Zeeb 						errx(1, "illegal nfs version "
356528c159fSBjoern A. Zeeb 						    "value -- %s", val);
357528c159fSBjoern A. Zeeb 					}
358528c159fSBjoern A. Zeeb 					pass_flag_to_nmount=0;
359c0d14b02SRick Macklem 				} else if (strcmp(opt, "soft") == 0) {
360c0d14b02SRick Macklem 					softintr = true;
361c0d14b02SRick Macklem 				} else if (strcmp(opt, "intr") == 0) {
362c0d14b02SRick Macklem 					softintr = true;
363317d5933SIan Dowse 				}
3646575d184SEdward Tomasz Napierala 				if (pass_flag_to_nmount) {
3656575d184SEdward Tomasz Napierala 					build_iovec(&iov, &iovlen, opt,
3666575d184SEdward Tomasz Napierala 					    __DECONST(void *, val),
3674b5bc283SCraig Rodrigues 					    strlen(val) + 1);
3686575d184SEdward Tomasz Napierala 				}
3694b5bc283SCraig Rodrigues 				opt = pnextopt;
3704db56604SPeter Wemm 			}
3714db56604SPeter Wemm 			}
3728fae3551SRodney W. Grimes 			break;
3738fae3551SRodney W. Grimes 		case 'P':
3744b5bc283SCraig Rodrigues 			/* obsolete for -o noresvport now default */
3754b5bc283SCraig Rodrigues 			printf("-P deprecated, use -o noresvport\n");
3764b5bc283SCraig Rodrigues 			build_iovec(&iov, &iovlen, "noresvport", NULL, 0);
3778fae3551SRodney W. Grimes 			break;
3788fae3551SRodney W. Grimes 		case 'R':
3794b5bc283SCraig Rodrigues 			printf("-R deprecated, use -o retrycnt=<retrycnt>\n");
3808fae3551SRodney W. Grimes 			num = strtol(optarg, &p, 10);
381e16873daSIan Dowse 			if (*p || num < 0)
3828fae3551SRodney W. Grimes 				errx(1, "illegal -R value -- %s", optarg);
3838fae3551SRodney W. Grimes 			retrycnt = num;
3848fae3551SRodney W. Grimes 			break;
3858fae3551SRodney W. Grimes 		case 'r':
3864b5bc283SCraig Rodrigues 			printf("-r deprecated, use -o rsize=<rsize>\n");
3874b5bc283SCraig Rodrigues 			build_iovec(&iov, &iovlen, "rsize", optarg, (size_t)-1);
3888fae3551SRodney W. Grimes 			break;
3898fae3551SRodney W. Grimes 		case 's':
3904b5bc283SCraig Rodrigues 			printf("-s deprecated, use -o soft\n");
3914b5bc283SCraig Rodrigues 			build_iovec(&iov, &iovlen, "soft", NULL, 0);
392c0d14b02SRick Macklem 			softintr = true;
3938fae3551SRodney W. Grimes 			break;
3948fae3551SRodney W. Grimes 		case 'T':
395a62dc406SDoug Rabson 			nfsproto = IPPROTO_TCP;
3964b5bc283SCraig Rodrigues 			printf("-T deprecated, use -o tcp\n");
3978fae3551SRodney W. Grimes 			break;
3988fae3551SRodney W. Grimes 		case 't':
3994b5bc283SCraig Rodrigues 			printf("-t deprecated, use -o timeout=<value>\n");
4004b5bc283SCraig Rodrigues 			build_iovec(&iov, &iovlen, "timeout", optarg, (size_t)-1);
4018fae3551SRodney W. Grimes 			break;
4028fae3551SRodney W. Grimes 		case 'w':
4034b5bc283SCraig Rodrigues 			printf("-w deprecated, use -o wsize=<value>\n");
4044b5bc283SCraig Rodrigues 			build_iovec(&iov, &iovlen, "wsize", optarg, (size_t)-1);
4058fae3551SRodney W. Grimes 			break;
4068fae3551SRodney W. Grimes 		case 'x':
4074b5bc283SCraig Rodrigues 			printf("-x deprecated, use -o retrans=<value>\n");
4084b5bc283SCraig Rodrigues 			build_iovec(&iov, &iovlen, "retrans", optarg, (size_t)-1);
4098fae3551SRodney W. Grimes 			break;
410a62dc406SDoug Rabson 		case 'U':
4114b5bc283SCraig Rodrigues 			printf("-U deprecated, use -o mntudp\n");
412a62dc406SDoug Rabson 			mnttcp_ok = 0;
4130c269d1fSCraig Rodrigues 			nfsproto = IPPROTO_UDP;
4144b5bc283SCraig Rodrigues 			build_iovec(&iov, &iovlen, "mntudp", NULL, 0);
415a62dc406SDoug Rabson 			break;
4168fae3551SRodney W. Grimes 		default:
4178fae3551SRodney W. Grimes 			usage();
4188fae3551SRodney W. Grimes 			break;
4198fae3551SRodney W. Grimes 		}
4208fae3551SRodney W. Grimes 	argc -= optind;
4218fae3551SRodney W. Grimes 	argv += optind;
4228fae3551SRodney W. Grimes 
42308a8d16cSRick Macklem 	if ((opflags & (BGRND | BGRNDNOW)) == (BGRND | BGRNDNOW))
42408a8d16cSRick Macklem 		errx(1, "Options bg and bgnow are mutually exclusive");
42508a8d16cSRick Macklem 
4264a4c5285SPeter Wemm 	if (argc != 2) {
4272f21d07aSDavid Greenman 		usage();
4284a4c5285SPeter Wemm 		/* NOTREACHED */
4294a4c5285SPeter Wemm 	}
4308fae3551SRodney W. Grimes 
431c0d14b02SRick Macklem 	/* Warn that NFSv4 mounts only work correctly as hard mounts. */
432c0d14b02SRick Macklem 	if (mountmode == V4 && softintr)
433c0d14b02SRick Macklem 		warnx("Warning, options soft and/or intr cannot be safely used"
434c0d14b02SRick Macklem 		    " for NFSv4. See the BUGS section of mount_nfs(8)");
435c0d14b02SRick Macklem 
4368fae3551SRodney W. Grimes 	spec = *argv++;
4376575d184SEdward Tomasz Napierala 	mntname = *argv;
4388fae3551SRodney W. Grimes 
439e16873daSIan Dowse 	if (retrycnt == -1)
4402bc53e11SIan Dowse 		/* The default is to keep retrying forever. */
4412bc53e11SIan Dowse 		retrycnt = 0;
4420c269d1fSCraig Rodrigues 
443011981fdSRick Macklem 	if (modfind("nfscl") < 0) {
444011981fdSRick Macklem 		/* Not present in kernel, try loading it */
445011981fdSRick Macklem 		if (kldload("nfscl") < 0 ||
446011981fdSRick Macklem 		    modfind("nfscl") < 0)
447011981fdSRick Macklem 			errx(1, "nfscl is not available");
448011981fdSRick Macklem 	}
449011981fdSRick Macklem 
450011981fdSRick Macklem 	/*
451011981fdSRick Macklem 	 * Add the fqdn to the gssname, as required.
452011981fdSRick Macklem 	 */
453011981fdSRick Macklem 	if (gssname != NULL) {
454011981fdSRick Macklem 		if (strchr(gssname, '@') == NULL &&
455011981fdSRick Macklem 		    gethostname(hostname, MAXHOSTNAMELEN) == 0) {
456011981fdSRick Macklem 			snprintf(gssn, sizeof (gssn), "%s@%s", gssname,
457011981fdSRick Macklem 			    hostname);
458011981fdSRick Macklem 			gssname = gssn;
459011981fdSRick Macklem 		}
4606575d184SEdward Tomasz Napierala 		build_iovec(&iov, &iovlen, "gssname",
4616575d184SEdward Tomasz Napierala 		    __DECONST(void *, gssname), strlen(gssname) + 1);
462011981fdSRick Macklem 	}
463011981fdSRick Macklem 
4647cab630bSRick Macklem 	if (!getnfsargs(&spec, &host, &iov, &iovlen))
4658fae3551SRodney W. Grimes 		exit(1);
466d599144dSGarrett Wollman 
46773dd3167SPoul-Henning Kamp 	/* resolve the mountpoint with realpath(3) */
4686575d184SEdward Tomasz Napierala 	if (checkpath(mntname, mntpath) != 0)
469d3250014SJaakko Heinonen 		err(1, "%s", mntpath);
47073dd3167SPoul-Henning Kamp 
47125585679SBrooks Davis 	build_iovec_argf(&iov, &iovlen, "fstype", "nfs");
472c5aa1dc8SCraig Rodrigues 	build_iovec(&iov, &iovlen, "fspath", mntpath, (size_t)-1);
473a6b26402SCraig Rodrigues 	build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
474c5aa1dc8SCraig Rodrigues 
47556c049a0SRick Macklem 	if (nmount(iov, iovlen, 0)) {
47656c049a0SRick Macklem 		nmount_errstr = nfsv4_geterrstr(errno);
47756c049a0SRick Macklem 		if (mountmode == V4 && nmount_errstr != NULL)
47856c049a0SRick Macklem 			errx(1, "nmount: %s, %s", mntpath, nmount_errstr);
47956c049a0SRick Macklem 		else
480773e5651SConrad Meyer 			err(1, "nmount: %s%s%s", mntpath, errmsg[0] ? ", " : "",
481773e5651SConrad Meyer 			    errmsg);
4827cab630bSRick Macklem 	} else if (mountmode != V4 && !add_mtab(host, spec)) {
4837cab630bSRick Macklem 		/* Add mounted file system to PATH_MOUNTTAB */
4847cab630bSRick Macklem 		warnx("can't update %s for %s:%s", PATH_MOUNTTAB, host, spec);
48556c049a0SRick Macklem 	}
486a62dc406SDoug Rabson 
4878fae3551SRodney W. Grimes 	exit(0);
4888fae3551SRodney W. Grimes }
4898fae3551SRodney W. Grimes 
4904b5bc283SCraig Rodrigues static int
sec_name_to_num(const char * sec)4916575d184SEdward Tomasz Napierala sec_name_to_num(const char *sec)
492a9148abdSDoug Rabson {
493a9148abdSDoug Rabson 	if (!strcmp(sec, "krb5"))
494a9148abdSDoug Rabson 		return (RPCSEC_GSS_KRB5);
495a9148abdSDoug Rabson 	if (!strcmp(sec, "krb5i"))
496a9148abdSDoug Rabson 		return (RPCSEC_GSS_KRB5I);
497a9148abdSDoug Rabson 	if (!strcmp(sec, "krb5p"))
498a9148abdSDoug Rabson 		return (RPCSEC_GSS_KRB5P);
499a9148abdSDoug Rabson 	if (!strcmp(sec, "sys"))
500a9148abdSDoug Rabson 		return (AUTH_SYS);
501a9148abdSDoug Rabson 	return (-1);
502a9148abdSDoug Rabson }
503a9148abdSDoug Rabson 
5046575d184SEdward Tomasz Napierala static const char *
sec_num_to_name(int flavor)505a9148abdSDoug Rabson sec_num_to_name(int flavor)
506a9148abdSDoug Rabson {
507a9148abdSDoug Rabson 	switch (flavor) {
508a9148abdSDoug Rabson 	case RPCSEC_GSS_KRB5:
509a9148abdSDoug Rabson 		return ("krb5");
510a9148abdSDoug Rabson 	case RPCSEC_GSS_KRB5I:
511a9148abdSDoug Rabson 		return ("krb5i");
512a9148abdSDoug Rabson 	case RPCSEC_GSS_KRB5P:
513a9148abdSDoug Rabson 		return ("krb5p");
514a9148abdSDoug Rabson 	case AUTH_SYS:
515a9148abdSDoug Rabson 		return ("sys");
516a9148abdSDoug Rabson 	}
517a9148abdSDoug Rabson 	return (NULL);
518a9148abdSDoug Rabson }
519a9148abdSDoug Rabson 
5208cd9718fSGleb Smirnoff /*
5218cd9718fSGleb Smirnoff  * Wait for RTM_IFINFO message with interface that is IFF_UP and with
5228cd9718fSGleb Smirnoff  * link on, or until timeout expires.  Returns seconds left.
5238cd9718fSGleb Smirnoff  */
5248cd9718fSGleb Smirnoff static time_t
rtm_ifinfo_sleep(time_t sec)5258cd9718fSGleb Smirnoff rtm_ifinfo_sleep(time_t sec)
5268cd9718fSGleb Smirnoff {
5273f16af55SGleb Smirnoff 	char buf[2048] __aligned(__alignof(struct if_msghdr));
5288cd9718fSGleb Smirnoff 	fd_set rfds;
5298cd9718fSGleb Smirnoff 	struct timeval tv, start;
5308cd9718fSGleb Smirnoff 	ssize_t nread;
5318cd9718fSGleb Smirnoff 	int n, s;
5328cd9718fSGleb Smirnoff 
5338cd9718fSGleb Smirnoff 	s = socket(PF_ROUTE, SOCK_RAW, 0);
5348cd9718fSGleb Smirnoff 	if (s < 0)
5358cd9718fSGleb Smirnoff 		err(EX_OSERR, "socket");
5368cd9718fSGleb Smirnoff 	(void)gettimeofday(&start, NULL);
5378cd9718fSGleb Smirnoff 
5388cd9718fSGleb Smirnoff 	for (tv.tv_sec = sec, tv.tv_usec = 0;
5398cd9718fSGleb Smirnoff 	    tv.tv_sec > 0;
5408cd9718fSGleb Smirnoff 	    (void)gettimeofday(&tv, NULL),
5418cd9718fSGleb Smirnoff 	    tv.tv_sec = sec - (tv.tv_sec - start.tv_sec)) {
5428cd9718fSGleb Smirnoff 		FD_ZERO(&rfds);
5438cd9718fSGleb Smirnoff 		FD_SET(s, &rfds);
5448cd9718fSGleb Smirnoff 		n = select(s + 1, &rfds, NULL, NULL, &tv);
5458cd9718fSGleb Smirnoff 		if (n == 0)
5468cd9718fSGleb Smirnoff 			continue;
5478cd9718fSGleb Smirnoff 		if (n == -1) {
5488cd9718fSGleb Smirnoff 			if (errno == EINTR)
5498cd9718fSGleb Smirnoff 				continue;
5508cd9718fSGleb Smirnoff 			else
5518cd9718fSGleb Smirnoff 				err(EX_SOFTWARE, "select");
5528cd9718fSGleb Smirnoff 		}
5538cd9718fSGleb Smirnoff 		nread = read(s, buf, 2048);
5548cd9718fSGleb Smirnoff 		if (nread < 0)
5558cd9718fSGleb Smirnoff 			err(EX_OSERR, "read");
5568cd9718fSGleb Smirnoff 		if ((size_t)nread >= sizeof(struct if_msghdr)) {
5578cd9718fSGleb Smirnoff 			struct if_msghdr *ifm;
5588cd9718fSGleb Smirnoff 
5598cd9718fSGleb Smirnoff 			ifm = (struct if_msghdr *)buf;
5608cd9718fSGleb Smirnoff 			if (ifm->ifm_version == RTM_VERSION &&
5618cd9718fSGleb Smirnoff 			    ifm->ifm_type == RTM_IFINFO &&
5628cd9718fSGleb Smirnoff 			    (ifm->ifm_flags & IFF_UP) &&
5638cd9718fSGleb Smirnoff 			    ifm->ifm_data.ifi_link_state != LINK_STATE_DOWN)
5648cd9718fSGleb Smirnoff 				break;
5658cd9718fSGleb Smirnoff 		}
5668cd9718fSGleb Smirnoff 	}
5678cd9718fSGleb Smirnoff 
5688cd9718fSGleb Smirnoff 	close(s);
5698cd9718fSGleb Smirnoff 
5708cd9718fSGleb Smirnoff 	return (tv.tv_sec);
5718cd9718fSGleb Smirnoff }
5728cd9718fSGleb Smirnoff 
573b352972bSXin LI static int
getnfsargs(char ** specp,char ** hostpp,struct iovec ** iov,int * iovlen)5747cab630bSRick Macklem getnfsargs(char **specp, char **hostpp, struct iovec **iov, int *iovlen)
5758fae3551SRodney W. Grimes {
5768360efbdSAlfred Perlstein 	struct addrinfo hints, *ai_nfs, *ai;
577317d5933SIan Dowse 	enum tryret ret;
578a505d435SHajimu UMEMOTO 	int ecode, speclen, remoteerr, offset, have_bracket = 0;
5797cab630bSRick Macklem 	char *hostp, *delimp, *errstr, *spec;
58066a84ea7SBrian Feldman 	size_t len;
581011981fdSRick Macklem 	static char nam[MNAMELEN + 1], pname[MAXHOSTNAMELEN + 5];
5828fae3551SRodney W. Grimes 
5837cab630bSRick Macklem 	spec = *specp;
584a505d435SHajimu UMEMOTO 	if (*spec == '[' && (delimp = strchr(spec + 1, ']')) != NULL &&
585a505d435SHajimu UMEMOTO 	    *(delimp + 1) == ':') {
586a505d435SHajimu UMEMOTO 		hostp = spec + 1;
587a505d435SHajimu UMEMOTO 		spec = delimp + 2;
588a505d435SHajimu UMEMOTO 		have_bracket = 1;
589a505d435SHajimu UMEMOTO 	} else if ((delimp = strrchr(spec, ':')) != NULL) {
5908fae3551SRodney W. Grimes 		hostp = spec;
5918fae3551SRodney W. Grimes 		spec = delimp + 1;
59273dd3167SPoul-Henning Kamp 	} else if ((delimp = strrchr(spec, '@')) != NULL) {
59373dd3167SPoul-Henning Kamp 		warnx("path@server syntax is deprecated, use server:path");
59473dd3167SPoul-Henning Kamp 		hostp = delimp + 1;
5958fae3551SRodney W. Grimes 	} else {
59673dd3167SPoul-Henning Kamp 		warnx("no <host>:<dirpath> nfs-name");
5978fae3551SRodney W. Grimes 		return (0);
5988fae3551SRodney W. Grimes 	}
5998fae3551SRodney W. Grimes 	*delimp = '\0';
60073dd3167SPoul-Henning Kamp 
60173dd3167SPoul-Henning Kamp 	/*
60273dd3167SPoul-Henning Kamp 	 * If there has been a trailing slash at mounttime it seems
60373dd3167SPoul-Henning Kamp 	 * that some mountd implementations fail to remove the mount
60473dd3167SPoul-Henning Kamp 	 * entries from their mountlist while unmounting.
60573dd3167SPoul-Henning Kamp 	 */
60666a84ea7SBrian Feldman 	for (speclen = strlen(spec);
60766a84ea7SBrian Feldman 		speclen > 1 && spec[speclen - 1] == '/';
60866a84ea7SBrian Feldman 		speclen--)
60973dd3167SPoul-Henning Kamp 		spec[speclen - 1] = '\0';
61073dd3167SPoul-Henning Kamp 	if (strlen(hostp) + strlen(spec) + 1 > MNAMELEN) {
61173dd3167SPoul-Henning Kamp 		warnx("%s:%s: %s", hostp, spec, strerror(ENAMETOOLONG));
61273dd3167SPoul-Henning Kamp 		return (0);
61373dd3167SPoul-Henning Kamp 	}
61473dd3167SPoul-Henning Kamp 	/* Make both '@' and ':' notations equal */
61566a84ea7SBrian Feldman 	if (*hostp != '\0') {
61666a84ea7SBrian Feldman 		len = strlen(hostp);
617a505d435SHajimu UMEMOTO 		offset = 0;
618a505d435SHajimu UMEMOTO 		if (have_bracket)
619a505d435SHajimu UMEMOTO 			nam[offset++] = '[';
620a505d435SHajimu UMEMOTO 		memmove(nam + offset, hostp, len);
621a505d435SHajimu UMEMOTO 		if (have_bracket)
622a505d435SHajimu UMEMOTO 			nam[len + offset++] = ']';
623a505d435SHajimu UMEMOTO 		nam[len + offset++] = ':';
624a505d435SHajimu UMEMOTO 		memmove(nam + len + offset, spec, speclen);
625a505d435SHajimu UMEMOTO 		nam[len + speclen + offset] = '\0';
62666a84ea7SBrian Feldman 	}
6278fae3551SRodney W. Grimes 
6288fae3551SRodney W. Grimes 	/*
62991196234SPeter Wemm 	 * Handle an internet host address.
6308fae3551SRodney W. Grimes 	 */
6318360efbdSAlfred Perlstein 	memset(&hints, 0, sizeof hints);
6328360efbdSAlfred Perlstein 	hints.ai_flags = AI_NUMERICHOST;
6334b5bc283SCraig Rodrigues 	if (nfsproto == IPPROTO_TCP)
6344b5bc283SCraig Rodrigues 		hints.ai_socktype = SOCK_STREAM;
6354b5bc283SCraig Rodrigues 	else if (nfsproto == IPPROTO_UDP)
6364b5bc283SCraig Rodrigues 		hints.ai_socktype = SOCK_DGRAM;
6374b5bc283SCraig Rodrigues 
63891196234SPeter Wemm 	if (getaddrinfo(hostp, portspec, &hints, &ai_nfs) != 0) {
639011981fdSRick Macklem 		hints.ai_flags = AI_CANONNAME;
640317d5933SIan Dowse 		if ((ecode = getaddrinfo(hostp, portspec, &hints, &ai_nfs))
641317d5933SIan Dowse 		    != 0) {
642317d5933SIan Dowse 			if (portspec == NULL)
643317d5933SIan Dowse 				errx(1, "%s: %s", hostp, gai_strerror(ecode));
644317d5933SIan Dowse 			else
645317d5933SIan Dowse 				errx(1, "%s:%s: %s", hostp, portspec,
6468360efbdSAlfred Perlstein 				    gai_strerror(ecode));
6478360efbdSAlfred Perlstein 			return (0);
6488360efbdSAlfred Perlstein 		}
649011981fdSRick Macklem 
650011981fdSRick Macklem 		/*
651011981fdSRick Macklem 		 * For a Kerberized nfs mount where the "principal"
652011981fdSRick Macklem 		 * argument has not been set, add it here.
653011981fdSRick Macklem 		 */
654c088e62eSRick Macklem 		if (got_principal == 0 && secflavor != AUTH_SYS &&
655c088e62eSRick Macklem 		    ai_nfs->ai_canonname != NULL) {
656011981fdSRick Macklem 			snprintf(pname, sizeof (pname), "nfs@%s",
657011981fdSRick Macklem 			    ai_nfs->ai_canonname);
658011981fdSRick Macklem 			build_iovec(iov, iovlen, "principal", pname,
659011981fdSRick Macklem 			    strlen(pname) + 1);
660011981fdSRick Macklem 		}
6618360efbdSAlfred Perlstein 	}
6628fae3551SRodney W. Grimes 
66308a8d16cSRick Macklem 	if ((opflags & (BGRNDNOW | ISBGRND)) == BGRNDNOW) {
66408a8d16cSRick Macklem 		warnx("Mount %s:%s, backgrounding",
66508a8d16cSRick Macklem 		    hostp, spec);
66608a8d16cSRick Macklem 		opflags |= ISBGRND;
66708a8d16cSRick Macklem 		if (daemon(0, 0) != 0)
66808a8d16cSRick Macklem 			err(1, "daemon");
66908a8d16cSRick Macklem 	}
67008a8d16cSRick Macklem 
671317d5933SIan Dowse 	ret = TRYRET_LOCALERR;
672e16873daSIan Dowse 	for (;;) {
673deffdffaSAndrey A. Chernov 		/*
674317d5933SIan Dowse 		 * Try each entry returned by getaddrinfo(). Note the
6754b85a12fSUlrich Spörlein 		 * occurrence of remote errors by setting `remoteerr'.
676deffdffaSAndrey A. Chernov 		 */
677317d5933SIan Dowse 		remoteerr = 0;
678317d5933SIan Dowse 		for (ai = ai_nfs; ai != NULL; ai = ai->ai_next) {
679302f15f9SMatthew N. Dodd 			if ((ai->ai_family == AF_INET6) &&
680302f15f9SMatthew N. Dodd 			    (opflags & OF_NOINET6))
681302f15f9SMatthew N. Dodd 				continue;
682302f15f9SMatthew N. Dodd 			if ((ai->ai_family == AF_INET) &&
683302f15f9SMatthew N. Dodd 			    (opflags & OF_NOINET4))
684302f15f9SMatthew N. Dodd 				continue;
6854b5bc283SCraig Rodrigues 			ret = nfs_tryproto(ai, hostp, spec, &errstr, iov,
6864b5bc283SCraig Rodrigues 			    iovlen);
687317d5933SIan Dowse 			if (ret == TRYRET_SUCCESS)
688317d5933SIan Dowse 				break;
689317d5933SIan Dowse 			if (ret != TRYRET_LOCALERR)
690317d5933SIan Dowse 				remoteerr = 1;
691317d5933SIan Dowse 			if ((opflags & ISBGRND) == 0)
692317d5933SIan Dowse 				fprintf(stderr, "%s\n", errstr);
693deffdffaSAndrey A. Chernov 		}
694317d5933SIan Dowse 		if (ret == TRYRET_SUCCESS)
695317d5933SIan Dowse 			break;
696deffdffaSAndrey A. Chernov 
697e16873daSIan Dowse 		/* Exit if all errors were local. */
698e16873daSIan Dowse 		if (!remoteerr)
699deffdffaSAndrey A. Chernov 			exit(1);
700317d5933SIan Dowse 
701e16873daSIan Dowse 		/*
702e16873daSIan Dowse 		 * If retrycnt == 0, we are to keep retrying forever.
703e16873daSIan Dowse 		 * Otherwise decrement it, and exit if it hits zero.
704e16873daSIan Dowse 		 */
705e16873daSIan Dowse 		if (retrycnt != 0 && --retrycnt == 0)
706317d5933SIan Dowse 			exit(1);
707317d5933SIan Dowse 
708317d5933SIan Dowse 		if ((opflags & (BGRND | ISBGRND)) == BGRND) {
709317d5933SIan Dowse 			warnx("Cannot immediately mount %s:%s, backgrounding",
710317d5933SIan Dowse 			    hostp, spec);
711317d5933SIan Dowse 			opflags |= ISBGRND;
712317d5933SIan Dowse 			if (daemon(0, 0) != 0)
713317d5933SIan Dowse 				err(1, "daemon");
7148fae3551SRodney W. Grimes 		}
7158cd9718fSGleb Smirnoff 		/*
7168cd9718fSGleb Smirnoff 		 * If rtm_ifinfo_sleep() returns non-zero, don't count
7178cd9718fSGleb Smirnoff 		 * that as a retry attempt.
7188cd9718fSGleb Smirnoff 		 */
7198cd9718fSGleb Smirnoff 		if (rtm_ifinfo_sleep(60) && retrycnt != 0)
7208cd9718fSGleb Smirnoff 			retrycnt++;
7218fae3551SRodney W. Grimes 	}
7228360efbdSAlfred Perlstein 	freeaddrinfo(ai_nfs);
7234b5bc283SCraig Rodrigues 
7244b5bc283SCraig Rodrigues 	build_iovec(iov, iovlen, "hostname", nam, (size_t)-1);
7257cab630bSRick Macklem 
7267cab630bSRick Macklem 	*specp = spec;
7277cab630bSRick Macklem 	*hostpp = hostp;
7288fae3551SRodney W. Grimes 	return (1);
7298fae3551SRodney W. Grimes }
7308fae3551SRodney W. Grimes 
7318fae3551SRodney W. Grimes /*
732317d5933SIan Dowse  * Try to set up the NFS arguments according to the address
733317d5933SIan Dowse  * family, protocol (and possibly port) specified in `ai'.
734317d5933SIan Dowse  *
735317d5933SIan Dowse  * Returns TRYRET_SUCCESS if successful, or:
736317d5933SIan Dowse  *   TRYRET_TIMEOUT		The server did not respond.
737317d5933SIan Dowse  *   TRYRET_REMOTEERR		The server reported an error.
738317d5933SIan Dowse  *   TRYRET_LOCALERR		Local failure.
739317d5933SIan Dowse  *
740317d5933SIan Dowse  * In all error cases, *errstr will be set to a statically-allocated string
741317d5933SIan Dowse  * describing the error.
742317d5933SIan Dowse  */
743b352972bSXin LI static enum tryret
nfs_tryproto(struct addrinfo * ai,char * hostp,char * spec,char ** errstr,struct iovec ** iov,int * iovlen)7444b5bc283SCraig Rodrigues nfs_tryproto(struct addrinfo *ai, char *hostp, char *spec, char **errstr,
7454b5bc283SCraig Rodrigues     struct iovec **iov, int *iovlen)
746317d5933SIan Dowse {
747317d5933SIan Dowse 	static char errbuf[256];
748317d5933SIan Dowse 	struct sockaddr_storage nfs_ss;
749317d5933SIan Dowse 	struct netbuf nfs_nb;
750317d5933SIan Dowse 	struct nfhret nfhret;
751317d5933SIan Dowse 	struct timeval try;
752317d5933SIan Dowse 	struct rpc_err rpcerr;
753317d5933SIan Dowse 	CLIENT *clp;
754317d5933SIan Dowse 	struct netconfig *nconf, *nconf_mnt;
7556575d184SEdward Tomasz Napierala 	const char *netid, *netid_mnt, *secname;
7564b5bc283SCraig Rodrigues 	int doconnect, nfsvers, mntvers, sotype;
7576575d184SEdward Tomasz Napierala 	enum clnt_stat clntstat;
758317d5933SIan Dowse 	enum mountmode trymntmode;
759317d5933SIan Dowse 
760d0654c85SKevin Lo 	sotype = 0;
761317d5933SIan Dowse 	trymntmode = mountmode;
762317d5933SIan Dowse 	errbuf[0] = '\0';
763317d5933SIan Dowse 	*errstr = errbuf;
764317d5933SIan Dowse 
7654b5bc283SCraig Rodrigues 	if (nfsproto == IPPROTO_TCP)
7664b5bc283SCraig Rodrigues 		sotype = SOCK_STREAM;
7674b5bc283SCraig Rodrigues 	else if (nfsproto == IPPROTO_UDP)
7684b5bc283SCraig Rodrigues 		sotype = SOCK_DGRAM;
7694b5bc283SCraig Rodrigues 
7704b5bc283SCraig Rodrigues 	if ((netid = netidbytype(ai->ai_family, sotype)) == NULL) {
771a3d8f94bSIan Dowse 		snprintf(errbuf, sizeof errbuf,
7724b5bc283SCraig Rodrigues 		    "af %d sotype %d not supported", ai->ai_family, sotype);
773a3d8f94bSIan Dowse 		return (TRYRET_LOCALERR);
774a3d8f94bSIan Dowse 	}
775a3d8f94bSIan Dowse 	if ((nconf = getnetconf_cached(netid)) == NULL) {
776317d5933SIan Dowse 		snprintf(errbuf, sizeof errbuf, "%s: %s", netid, nc_sperror());
777317d5933SIan Dowse 		return (TRYRET_LOCALERR);
778317d5933SIan Dowse 	}
779317d5933SIan Dowse 	/* The RPCPROG_MNT netid may be different. */
780317d5933SIan Dowse 	if (mnttcp_ok) {
781ba33efd9SIan Dowse 		netid_mnt = netid;
782317d5933SIan Dowse 		nconf_mnt = nconf;
783317d5933SIan Dowse 	} else {
784a3d8f94bSIan Dowse 		if ((netid_mnt = netidbytype(ai->ai_family, SOCK_DGRAM))
785a3d8f94bSIan Dowse 		     == NULL) {
786a3d8f94bSIan Dowse 			snprintf(errbuf, sizeof errbuf,
787a3d8f94bSIan Dowse 			    "af %d sotype SOCK_DGRAM not supported",
788a3d8f94bSIan Dowse 			     ai->ai_family);
789a3d8f94bSIan Dowse 			return (TRYRET_LOCALERR);
790a3d8f94bSIan Dowse 		}
791a3d8f94bSIan Dowse 		if ((nconf_mnt = getnetconf_cached(netid_mnt)) == NULL) {
792ba33efd9SIan Dowse 			snprintf(errbuf, sizeof errbuf, "%s: %s", netid_mnt,
793317d5933SIan Dowse 			    nc_sperror());
794317d5933SIan Dowse 			return (TRYRET_LOCALERR);
795317d5933SIan Dowse 		}
796317d5933SIan Dowse 	}
797317d5933SIan Dowse 
798317d5933SIan Dowse tryagain:
799011981fdSRick Macklem 	if (trymntmode == V4) {
800011981fdSRick Macklem 		nfsvers = 4;
8016575d184SEdward Tomasz Napierala 		mntvers = 3; /* Workaround for GCC. */
802011981fdSRick Macklem 	} else if (trymntmode == V2) {
803317d5933SIan Dowse 		nfsvers = 2;
804317d5933SIan Dowse 		mntvers = 1;
805317d5933SIan Dowse 	} else {
806317d5933SIan Dowse 		nfsvers = 3;
807317d5933SIan Dowse 		mntvers = 3;
808317d5933SIan Dowse 	}
809317d5933SIan Dowse 
810317d5933SIan Dowse 	if (portspec != NULL) {
811317d5933SIan Dowse 		/* `ai' contains the complete nfsd sockaddr. */
812317d5933SIan Dowse 		nfs_nb.buf = ai->ai_addr;
813317d5933SIan Dowse 		nfs_nb.len = nfs_nb.maxlen = ai->ai_addrlen;
814317d5933SIan Dowse 	} else {
815317d5933SIan Dowse 		/* Ask the remote rpcbind. */
816317d5933SIan Dowse 		nfs_nb.buf = &nfs_ss;
817317d5933SIan Dowse 		nfs_nb.len = nfs_nb.maxlen = sizeof nfs_ss;
818317d5933SIan Dowse 
8190775314bSDoug Rabson 		if (!rpcb_getaddr(NFS_PROGRAM, nfsvers, nconf, &nfs_nb,
820317d5933SIan Dowse 		    hostp)) {
821317d5933SIan Dowse 			if (rpc_createerr.cf_stat == RPC_PROGVERSMISMATCH &&
822317d5933SIan Dowse 			    trymntmode == ANY) {
823317d5933SIan Dowse 				trymntmode = V2;
824317d5933SIan Dowse 				goto tryagain;
825317d5933SIan Dowse 			}
826317d5933SIan Dowse 			snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s",
827317d5933SIan Dowse 			    netid, hostp, spec,
828317d5933SIan Dowse 			    clnt_spcreateerror("RPCPROG_NFS"));
829317d5933SIan Dowse 			return (returncode(rpc_createerr.cf_stat,
830317d5933SIan Dowse 			    &rpc_createerr.cf_error));
831317d5933SIan Dowse 		}
832317d5933SIan Dowse 	}
833317d5933SIan Dowse 
834317d5933SIan Dowse 	/* Check that the server (nfsd) responds on the port we have chosen. */
8350775314bSDoug Rabson 	clp = clnt_tli_create(RPC_ANYFD, nconf, &nfs_nb, NFS_PROGRAM, nfsvers,
836317d5933SIan Dowse 	    0, 0);
837317d5933SIan Dowse 	if (clp == NULL) {
838317d5933SIan Dowse 		snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid,
839317d5933SIan Dowse 		    hostp, spec, clnt_spcreateerror("nfsd: RPCPROG_NFS"));
840317d5933SIan Dowse 		return (returncode(rpc_createerr.cf_stat,
841317d5933SIan Dowse 		    &rpc_createerr.cf_error));
842317d5933SIan Dowse 	}
8434b5bc283SCraig Rodrigues 	if (sotype == SOCK_DGRAM && noconn == 0) {
844eca1c24eSIan Dowse 		/*
845eca1c24eSIan Dowse 		 * Use connect(), to match what the kernel does. This
846eca1c24eSIan Dowse 		 * catches cases where the server responds from the
847eca1c24eSIan Dowse 		 * wrong source address.
848eca1c24eSIan Dowse 		 */
849eca1c24eSIan Dowse 		doconnect = 1;
850eca1c24eSIan Dowse 		if (!clnt_control(clp, CLSET_CONNECT, (char *)&doconnect)) {
851eca1c24eSIan Dowse 			clnt_destroy(clp);
852eca1c24eSIan Dowse 			snprintf(errbuf, sizeof errbuf,
853eca1c24eSIan Dowse 			    "[%s] %s:%s: CLSET_CONNECT failed", netid, hostp,
854eca1c24eSIan Dowse 			    spec);
855eca1c24eSIan Dowse 			return (TRYRET_LOCALERR);
856eca1c24eSIan Dowse 		}
857eca1c24eSIan Dowse 	}
858eca1c24eSIan Dowse 
859317d5933SIan Dowse 	try.tv_sec = 10;
860317d5933SIan Dowse 	try.tv_usec = 0;
8616575d184SEdward Tomasz Napierala 	clntstat = clnt_call(clp, NFSPROC_NULL, (xdrproc_t)xdr_void, NULL,
862011981fdSRick Macklem 			 (xdrproc_t)xdr_void, NULL, try);
8636575d184SEdward Tomasz Napierala 	if (clntstat != RPC_SUCCESS) {
8646575d184SEdward Tomasz Napierala 		if (clntstat == RPC_PROGVERSMISMATCH && trymntmode == ANY) {
865317d5933SIan Dowse 			clnt_destroy(clp);
866317d5933SIan Dowse 			trymntmode = V2;
867317d5933SIan Dowse 			goto tryagain;
868317d5933SIan Dowse 		}
869317d5933SIan Dowse 		clnt_geterr(clp, &rpcerr);
870317d5933SIan Dowse 		snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid,
871317d5933SIan Dowse 		    hostp, spec, clnt_sperror(clp, "NFSPROC_NULL"));
872317d5933SIan Dowse 		clnt_destroy(clp);
8736575d184SEdward Tomasz Napierala 		return (returncode(clntstat, &rpcerr));
874317d5933SIan Dowse 	}
875317d5933SIan Dowse 	clnt_destroy(clp);
876317d5933SIan Dowse 
877011981fdSRick Macklem 	/*
878011981fdSRick Macklem 	 * For NFSv4, there is no mount protocol.
879011981fdSRick Macklem 	 */
880011981fdSRick Macklem 	if (trymntmode == V4) {
881011981fdSRick Macklem 		/*
882011981fdSRick Macklem 		 * Store the server address in nfsargsp, making
883011981fdSRick Macklem 		 * sure to copy any locally allocated structures.
884011981fdSRick Macklem 		 */
885011981fdSRick Macklem 		addrlen = nfs_nb.len;
886011981fdSRick Macklem 		addr = malloc(addrlen);
887011981fdSRick Macklem 		if (addr == NULL)
888011981fdSRick Macklem 			err(1, "malloc");
889011981fdSRick Macklem 		bcopy(nfs_nb.buf, addr, addrlen);
890011981fdSRick Macklem 
891011981fdSRick Macklem 		build_iovec(iov, iovlen, "addr", addr, addrlen);
892011981fdSRick Macklem 		secname = sec_num_to_name(secflavor);
8936575d184SEdward Tomasz Napierala 		if (secname != NULL) {
8946575d184SEdward Tomasz Napierala 			build_iovec(iov, iovlen, "sec",
8956575d184SEdward Tomasz Napierala 			    __DECONST(void *, secname), (size_t)-1);
8966575d184SEdward Tomasz Napierala 		}
897011981fdSRick Macklem 		build_iovec(iov, iovlen, "nfsv4", NULL, 0);
898011981fdSRick Macklem 		build_iovec(iov, iovlen, "dirpath", spec, (size_t)-1);
899011981fdSRick Macklem 
900011981fdSRick Macklem 		return (TRYRET_SUCCESS);
901011981fdSRick Macklem 	}
902011981fdSRick Macklem 
9030775314bSDoug Rabson 	/* Send the MOUNTPROC_MNT RPC to get the root filehandle. */
904317d5933SIan Dowse 	try.tv_sec = 10;
905317d5933SIan Dowse 	try.tv_usec = 0;
9060775314bSDoug Rabson 	clp = clnt_tp_create(hostp, MOUNTPROG, mntvers, nconf_mnt);
907317d5933SIan Dowse 	if (clp == NULL) {
908ba33efd9SIan Dowse 		snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid_mnt,
909317d5933SIan Dowse 		    hostp, spec, clnt_spcreateerror("RPCMNT: clnt_create"));
910317d5933SIan Dowse 		return (returncode(rpc_createerr.cf_stat,
911317d5933SIan Dowse 		    &rpc_createerr.cf_error));
912317d5933SIan Dowse 	}
913317d5933SIan Dowse 	clp->cl_auth = authsys_create_default();
914a9148abdSDoug Rabson 	nfhret.auth = secflavor;
915317d5933SIan Dowse 	nfhret.vers = mntvers;
9166575d184SEdward Tomasz Napierala 	clntstat = clnt_call(clp, MOUNTPROC_MNT, (xdrproc_t)xdr_dir, spec,
917c04affffSMatthew N. Dodd 			 (xdrproc_t)xdr_fh, &nfhret,
918317d5933SIan Dowse 	    try);
919317d5933SIan Dowse 	auth_destroy(clp->cl_auth);
9206575d184SEdward Tomasz Napierala 	if (clntstat != RPC_SUCCESS) {
9216575d184SEdward Tomasz Napierala 		if (clntstat == RPC_PROGVERSMISMATCH && trymntmode == ANY) {
922317d5933SIan Dowse 			clnt_destroy(clp);
923317d5933SIan Dowse 			trymntmode = V2;
924317d5933SIan Dowse 			goto tryagain;
925317d5933SIan Dowse 		}
926317d5933SIan Dowse 		clnt_geterr(clp, &rpcerr);
927ba33efd9SIan Dowse 		snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid_mnt,
928317d5933SIan Dowse 		    hostp, spec, clnt_sperror(clp, "RPCPROG_MNT"));
929317d5933SIan Dowse 		clnt_destroy(clp);
9306575d184SEdward Tomasz Napierala 		return (returncode(clntstat, &rpcerr));
931317d5933SIan Dowse 	}
932317d5933SIan Dowse 	clnt_destroy(clp);
933317d5933SIan Dowse 
934317d5933SIan Dowse 	if (nfhret.stat != 0) {
935ba33efd9SIan Dowse 		snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid_mnt,
936317d5933SIan Dowse 		    hostp, spec, strerror(nfhret.stat));
937317d5933SIan Dowse 		return (TRYRET_REMOTEERR);
938317d5933SIan Dowse 	}
939317d5933SIan Dowse 
940317d5933SIan Dowse 	/*
941317d5933SIan Dowse 	 * Store the filehandle and server address in nfsargsp, making
942317d5933SIan Dowse 	 * sure to copy any locally allocated structures.
943317d5933SIan Dowse 	 */
9444b5bc283SCraig Rodrigues 	addrlen = nfs_nb.len;
9454b5bc283SCraig Rodrigues 	addr = malloc(addrlen);
9464b5bc283SCraig Rodrigues 	fhsize = nfhret.fhsize;
9474b5bc283SCraig Rodrigues 	fh = malloc(fhsize);
9484b5bc283SCraig Rodrigues 	if (addr == NULL || fh == NULL)
949317d5933SIan Dowse 		err(1, "malloc");
9504b5bc283SCraig Rodrigues 	bcopy(nfs_nb.buf, addr, addrlen);
9514b5bc283SCraig Rodrigues 	bcopy(nfhret.nfh, fh, fhsize);
952317d5933SIan Dowse 
9534b5bc283SCraig Rodrigues 	build_iovec(iov, iovlen, "addr", addr, addrlen);
9544b5bc283SCraig Rodrigues 	build_iovec(iov, iovlen, "fh", fh, fhsize);
955a9148abdSDoug Rabson 	secname = sec_num_to_name(nfhret.auth);
9566575d184SEdward Tomasz Napierala 	if (secname) {
9576575d184SEdward Tomasz Napierala 		build_iovec(iov, iovlen, "sec",
9586575d184SEdward Tomasz Napierala 		    __DECONST(void *, secname), (size_t)-1);
9596575d184SEdward Tomasz Napierala 	}
960317d5933SIan Dowse 	if (nfsvers == 3)
9614b5bc283SCraig Rodrigues 		build_iovec(iov, iovlen, "nfsv3", NULL, 0);
962317d5933SIan Dowse 
963317d5933SIan Dowse 	return (TRYRET_SUCCESS);
964317d5933SIan Dowse }
965317d5933SIan Dowse 
9660c269d1fSCraig Rodrigues /*
967317d5933SIan Dowse  * Catagorise a RPC return status and error into an `enum tryret'
968317d5933SIan Dowse  * return code.
969317d5933SIan Dowse  */
970b352972bSXin LI static enum tryret
returncode(enum clnt_stat clntstat,struct rpc_err * rpcerr)9716575d184SEdward Tomasz Napierala returncode(enum clnt_stat clntstat, struct rpc_err *rpcerr)
972317d5933SIan Dowse {
9736575d184SEdward Tomasz Napierala 
9746575d184SEdward Tomasz Napierala 	switch (clntstat) {
975317d5933SIan Dowse 	case RPC_TIMEDOUT:
976317d5933SIan Dowse 		return (TRYRET_TIMEOUT);
977317d5933SIan Dowse 	case RPC_PMAPFAILURE:
978317d5933SIan Dowse 	case RPC_PROGNOTREGISTERED:
979317d5933SIan Dowse 	case RPC_PROGVERSMISMATCH:
980eca1c24eSIan Dowse 	/* XXX, these can be local or remote. */
981eca1c24eSIan Dowse 	case RPC_CANTSEND:
982eca1c24eSIan Dowse 	case RPC_CANTRECV:
983317d5933SIan Dowse 		return (TRYRET_REMOTEERR);
984317d5933SIan Dowse 	case RPC_SYSTEMERROR:
985317d5933SIan Dowse 		switch (rpcerr->re_errno) {
986317d5933SIan Dowse 		case ETIMEDOUT:
987317d5933SIan Dowse 			return (TRYRET_TIMEOUT);
988317d5933SIan Dowse 		case ENOMEM:
989317d5933SIan Dowse 			break;
990317d5933SIan Dowse 		default:
991317d5933SIan Dowse 			return (TRYRET_REMOTEERR);
992317d5933SIan Dowse 		}
993317d5933SIan Dowse 		/* FALLTHROUGH */
994317d5933SIan Dowse 	default:
995317d5933SIan Dowse 		break;
996317d5933SIan Dowse 	}
997317d5933SIan Dowse 	return (TRYRET_LOCALERR);
998317d5933SIan Dowse }
999317d5933SIan Dowse 
1000317d5933SIan Dowse /*
1001a3d8f94bSIan Dowse  * Look up a netid based on an address family and socket type.
1002a3d8f94bSIan Dowse  * `af' is the address family, and `sotype' is SOCK_DGRAM or SOCK_STREAM.
1003a3d8f94bSIan Dowse  *
1004a3d8f94bSIan Dowse  * XXX there should be a library function for this.
1005a3d8f94bSIan Dowse  */
1006b352972bSXin LI static const char *
netidbytype(int af,int sotype)100733924ad4SWarner Losh netidbytype(int af, int sotype)
100833924ad4SWarner Losh {
1009a3d8f94bSIan Dowse 	struct nc_protos *p;
1010a3d8f94bSIan Dowse 
1011a3d8f94bSIan Dowse 	for (p = nc_protos; p->netid != NULL; p++) {
1012a3d8f94bSIan Dowse 		if (af != p->af || sotype != p->sotype)
1013a3d8f94bSIan Dowse 			continue;
1014a3d8f94bSIan Dowse 		return (p->netid);
1015a3d8f94bSIan Dowse 	}
1016a3d8f94bSIan Dowse 	return (NULL);
1017a3d8f94bSIan Dowse }
1018a3d8f94bSIan Dowse 
1019a3d8f94bSIan Dowse /*
1020a3d8f94bSIan Dowse  * Look up a netconfig entry based on a netid, and cache the result so
1021a3d8f94bSIan Dowse  * that we don't need to remember to call freenetconfigent().
1022a3d8f94bSIan Dowse  *
1023a3d8f94bSIan Dowse  * Otherwise it behaves just like getnetconfigent(), so nc_*error()
1024a3d8f94bSIan Dowse  * work on failure.
1025a3d8f94bSIan Dowse  */
1026b352972bSXin LI static struct netconfig *
getnetconf_cached(const char * netid)102733924ad4SWarner Losh getnetconf_cached(const char *netid)
102833924ad4SWarner Losh {
1029a3d8f94bSIan Dowse 	static struct nc_entry {
1030a3d8f94bSIan Dowse 		struct netconfig *nconf;
1031a3d8f94bSIan Dowse 		struct nc_entry *next;
1032a3d8f94bSIan Dowse 	} *head;
1033a3d8f94bSIan Dowse 	struct nc_entry *p;
1034a3d8f94bSIan Dowse 	struct netconfig *nconf;
1035a3d8f94bSIan Dowse 
1036a3d8f94bSIan Dowse 	for (p = head; p != NULL; p = p->next)
1037a3d8f94bSIan Dowse 		if (strcmp(netid, p->nconf->nc_netid) == 0)
1038a3d8f94bSIan Dowse 			return (p->nconf);
1039a3d8f94bSIan Dowse 
1040a3d8f94bSIan Dowse 	if ((nconf = getnetconfigent(netid)) == NULL)
1041a3d8f94bSIan Dowse 		return (NULL);
1042a3d8f94bSIan Dowse 	if ((p = malloc(sizeof(*p))) == NULL)
1043a3d8f94bSIan Dowse 		err(1, "malloc");
1044a3d8f94bSIan Dowse 	p->nconf = nconf;
1045a3d8f94bSIan Dowse 	p->next = head;
1046a3d8f94bSIan Dowse 	head = p;
1047a3d8f94bSIan Dowse 
1048a3d8f94bSIan Dowse 	return (p->nconf);
1049a3d8f94bSIan Dowse }
1050a3d8f94bSIan Dowse 
1051a3d8f94bSIan Dowse /*
10528fae3551SRodney W. Grimes  * xdr routines for mount rpc's
10538fae3551SRodney W. Grimes  */
1054b352972bSXin LI static int
xdr_dir(XDR * xdrsp,char * dirp)105533924ad4SWarner Losh xdr_dir(XDR *xdrsp, char *dirp)
10568fae3551SRodney W. Grimes {
10570775314bSDoug Rabson 	return (xdr_string(xdrsp, &dirp, MNTPATHLEN));
10588fae3551SRodney W. Grimes }
10598fae3551SRodney W. Grimes 
1060b352972bSXin LI static int
xdr_fh(XDR * xdrsp,struct nfhret * np)106133924ad4SWarner Losh xdr_fh(XDR *xdrsp, struct nfhret *np)
10628fae3551SRodney W. Grimes {
10633d438ad6SDavid E. O'Brien 	int i;
1064a62dc406SDoug Rabson 	long auth, authcnt, authfnd = 0;
1065a62dc406SDoug Rabson 
1066a62dc406SDoug Rabson 	if (!xdr_u_long(xdrsp, &np->stat))
10678fae3551SRodney W. Grimes 		return (0);
10688fae3551SRodney W. Grimes 	if (np->stat)
10698fae3551SRodney W. Grimes 		return (1);
1070a62dc406SDoug Rabson 	switch (np->vers) {
1071a62dc406SDoug Rabson 	case 1:
10720775314bSDoug Rabson 		np->fhsize = NFS_FHSIZE;
10730775314bSDoug Rabson 		return (xdr_opaque(xdrsp, (caddr_t)np->nfh, NFS_FHSIZE));
1074a62dc406SDoug Rabson 	case 3:
1075a62dc406SDoug Rabson 		if (!xdr_long(xdrsp, &np->fhsize))
1076a62dc406SDoug Rabson 			return (0);
10770775314bSDoug Rabson 		if (np->fhsize <= 0 || np->fhsize > NFS3_FHSIZE)
1078a62dc406SDoug Rabson 			return (0);
1079a62dc406SDoug Rabson 		if (!xdr_opaque(xdrsp, (caddr_t)np->nfh, np->fhsize))
1080a62dc406SDoug Rabson 			return (0);
1081a62dc406SDoug Rabson 		if (!xdr_long(xdrsp, &authcnt))
1082a62dc406SDoug Rabson 			return (0);
1083a62dc406SDoug Rabson 		for (i = 0; i < authcnt; i++) {
1084a62dc406SDoug Rabson 			if (!xdr_long(xdrsp, &auth))
1085a62dc406SDoug Rabson 				return (0);
10864b5bc283SCraig Rodrigues 			if (np->auth == -1) {
10874b5bc283SCraig Rodrigues 				np->auth = auth;
1088a62dc406SDoug Rabson 				authfnd++;
10894b5bc283SCraig Rodrigues 			} else if (auth == np->auth) {
10904b5bc283SCraig Rodrigues 				authfnd++;
10914b5bc283SCraig Rodrigues 			}
1092a62dc406SDoug Rabson 		}
1093a62dc406SDoug Rabson 		/*
1094a62dc406SDoug Rabson 		 * Some servers, such as DEC's OSF/1 return a nil authenticator
1095a62dc406SDoug Rabson 		 * list to indicate RPCAUTH_UNIX.
1096a62dc406SDoug Rabson 		 */
10974b5bc283SCraig Rodrigues 		if (authcnt == 0 && np->auth == -1)
10984b5bc283SCraig Rodrigues 			np->auth = AUTH_SYS;
10994b5bc283SCraig Rodrigues 		if (!authfnd && (authcnt > 0 || np->auth != AUTH_SYS))
1100a62dc406SDoug Rabson 			np->stat = EAUTH;
1101a62dc406SDoug Rabson 		return (1);
110280c7cc1cSPedro F. Giffuni 	}
1103a62dc406SDoug Rabson 	return (0);
11048fae3551SRodney W. Grimes }
11058fae3551SRodney W. Grimes 
1106b352972bSXin LI static void
usage(void)1107b352972bSXin LI usage(void)
11088fae3551SRodney W. Grimes {
110946fc8f78SPhilippe Charnier 	(void)fprintf(stderr, "%s\n%s\n%s\n%s\n",
111086ce6a83SRobert Watson "usage: mount_nfs [-23bcdiLlNPsTU] [-a maxreadahead] [-D deadthresh]",
11118d646af5SRuslan Ermilov "                 [-g maxgroups] [-I readdirsize] [-o options] [-R retrycnt]",
11128d646af5SRuslan Ermilov "                 [-r readsize] [-t timeout] [-w writesize] [-x retrans]",
11138d646af5SRuslan Ermilov "                 rhost:path node");
11148fae3551SRodney W. Grimes 	exit(1);
11158fae3551SRodney W. Grimes }
1116