18fae3551SRodney W. Grimes /* 28fae3551SRodney W. Grimes * Copyright (c) 1992, 1993, 1994 38fae3551SRodney W. Grimes * The Regents of the University of California. All rights reserved. 48fae3551SRodney W. Grimes * 58fae3551SRodney W. Grimes * This code is derived from software contributed to Berkeley by 68fae3551SRodney W. Grimes * Rick Macklem at The University of Guelph. 78fae3551SRodney W. Grimes * 88fae3551SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 98fae3551SRodney W. Grimes * modification, are permitted provided that the following conditions 108fae3551SRodney W. Grimes * are met: 118fae3551SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 128fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 138fae3551SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 148fae3551SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 158fae3551SRodney W. Grimes * documentation and/or other materials provided with the distribution. 16fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 178fae3551SRodney W. Grimes * may be used to endorse or promote products derived from this software 188fae3551SRodney W. Grimes * without specific prior written permission. 198fae3551SRodney W. Grimes * 208fae3551SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 218fae3551SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 228fae3551SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 238fae3551SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 248fae3551SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 258fae3551SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 268fae3551SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 278fae3551SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 288fae3551SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 298fae3551SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 308fae3551SRodney W. Grimes * SUCH DAMAGE. 318fae3551SRodney W. Grimes */ 328fae3551SRodney W. Grimes 33c69284caSDavid E. O'Brien #if 0 348fae3551SRodney W. Grimes #ifndef lint 3546fc8f78SPhilippe Charnier static const char copyright[] = 368fae3551SRodney W. Grimes "@(#) Copyright (c) 1992, 1993, 1994\n\ 378fae3551SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 388fae3551SRodney W. Grimes #endif /* not lint */ 398fae3551SRodney W. Grimes 408fae3551SRodney W. Grimes #ifndef lint 414a4c5285SPeter Wemm static char sccsid[] = "@(#)mount_nfs.c 8.11 (Berkeley) 5/4/95"; 428fae3551SRodney W. Grimes #endif /* not lint */ 43c69284caSDavid E. O'Brien #endif 44c69284caSDavid E. O'Brien #include <sys/cdefs.h> 45c69284caSDavid E. O'Brien __FBSDID("$FreeBSD$"); 468fae3551SRodney W. Grimes 478fae3551SRodney W. Grimes #include <sys/param.h> 48011981fdSRick Macklem #include <sys/linker.h> 49011981fdSRick Macklem #include <sys/module.h> 508fae3551SRodney W. Grimes #include <sys/mount.h> 518360efbdSAlfred Perlstein #include <sys/socket.h> 528fae3551SRodney W. Grimes #include <sys/stat.h> 538fae3551SRodney W. Grimes #include <sys/syslog.h> 54c5aa1dc8SCraig Rodrigues #include <sys/uio.h> 558fae3551SRodney W. Grimes 568fae3551SRodney W. Grimes #include <rpc/rpc.h> 578fae3551SRodney W. Grimes #include <rpc/pmap_clnt.h> 588fae3551SRodney W. Grimes #include <rpc/pmap_prot.h> 590775314bSDoug Rabson #include <rpcsvc/nfs_prot.h> 600775314bSDoug Rabson #include <rpcsvc/mount.h> 618fae3551SRodney W. Grimes 6291196234SPeter Wemm #include <nfsclient/nfs.h> 638fae3551SRodney W. Grimes 648fae3551SRodney W. Grimes #include <arpa/inet.h> 658fae3551SRodney W. Grimes 668fae3551SRodney W. Grimes #include <ctype.h> 678fae3551SRodney W. Grimes #include <err.h> 688fae3551SRodney W. Grimes #include <errno.h> 698360efbdSAlfred Perlstein #include <fcntl.h> 708fae3551SRodney W. Grimes #include <netdb.h> 718fae3551SRodney W. Grimes #include <stdio.h> 728fae3551SRodney W. Grimes #include <stdlib.h> 73c04affffSMatthew N. Dodd #include <string.h> 748fae3551SRodney W. Grimes #include <strings.h> 755e074e31SGarrett Wollman #include <sysexits.h> 768fae3551SRodney W. Grimes #include <unistd.h> 778fae3551SRodney W. Grimes 788fae3551SRodney W. Grimes #include "mntopts.h" 79a69497d7SMatthew Dillon #include "mounttab.h" 808fae3551SRodney W. Grimes 81a3d8f94bSIan Dowse /* Table for af,sotype -> netid conversions. */ 826575d184SEdward Tomasz Napierala static struct nc_protos { 834b5bc283SCraig Rodrigues const char *netid; 84a3d8f94bSIan Dowse int af; 85a3d8f94bSIan Dowse int sotype; 86a3d8f94bSIan Dowse } nc_protos[] = { 87a3d8f94bSIan Dowse {"udp", AF_INET, SOCK_DGRAM}, 88a3d8f94bSIan Dowse {"tcp", AF_INET, SOCK_STREAM}, 89a3d8f94bSIan Dowse {"udp6", AF_INET6, SOCK_DGRAM}, 90a3d8f94bSIan Dowse {"tcp6", AF_INET6, SOCK_STREAM}, 91b535b298SCraig Rodrigues {NULL, 0, 0} 92a3d8f94bSIan Dowse }; 93a3d8f94bSIan Dowse 948fae3551SRodney W. Grimes struct nfhret { 958fae3551SRodney W. Grimes u_long stat; 96a62dc406SDoug Rabson long vers; 97a62dc406SDoug Rabson long auth; 98a62dc406SDoug Rabson long fhsize; 990775314bSDoug Rabson u_char nfh[NFS3_FHSIZE]; 1008fae3551SRodney W. Grimes }; 1018fae3551SRodney W. Grimes #define BGRND 1 1028fae3551SRodney W. Grimes #define ISBGRND 2 103302f15f9SMatthew N. Dodd #define OF_NOINET4 4 104302f15f9SMatthew N. Dodd #define OF_NOINET6 8 1056575d184SEdward Tomasz Napierala static int retrycnt = -1; 1066575d184SEdward Tomasz Napierala static int opflags = 0; 1076575d184SEdward Tomasz Napierala static int nfsproto = IPPROTO_TCP; 1086575d184SEdward Tomasz Napierala static int mnttcp_ok = 1; 1096575d184SEdward Tomasz Napierala static int noconn = 0; 1106575d184SEdward Tomasz Napierala /* The 'portspec' is the server nfs port; NULL means look up via rpcbind. */ 1116575d184SEdward Tomasz Napierala static const char *portspec = NULL; 1126575d184SEdward Tomasz Napierala static struct sockaddr *addr; 1136575d184SEdward Tomasz Napierala static int addrlen = 0; 1146575d184SEdward Tomasz Napierala static u_char *fh = NULL; 1156575d184SEdward Tomasz Napierala static int fhsize = 0; 1166575d184SEdward Tomasz Napierala static int secflavor = -1; 1176575d184SEdward Tomasz Napierala static int got_principal = 0; 1184b5bc283SCraig Rodrigues 1196575d184SEdward Tomasz Napierala static enum mountmode { 1202cd1c32cSDoug Rabson ANY, 1212cd1c32cSDoug Rabson V2, 1220c269d1fSCraig Rodrigues V3, 123011981fdSRick Macklem V4 1242cd1c32cSDoug Rabson } mountmode = ANY; 1258fae3551SRodney W. Grimes 126317d5933SIan Dowse /* Return codes for nfs_tryproto. */ 127317d5933SIan Dowse enum tryret { 128317d5933SIan Dowse TRYRET_SUCCESS, 129317d5933SIan Dowse TRYRET_TIMEOUT, /* No response received. */ 130317d5933SIan Dowse TRYRET_REMOTEERR, /* Error received from remote server. */ 131317d5933SIan Dowse TRYRET_LOCALERR /* Local failure. */ 132317d5933SIan Dowse }; 133317d5933SIan Dowse 1346575d184SEdward Tomasz Napierala static int sec_name_to_num(const char *sec); 1356575d184SEdward Tomasz Napierala static const char *sec_num_to_name(int num); 136b352972bSXin LI static int getnfsargs(char *, struct iovec **iov, int *iovlen); 13785429990SWarner Losh /* void set_rpc_maxgrouplist(int); */ 138b352972bSXin LI static struct netconfig *getnetconf_cached(const char *netid); 139b352972bSXin LI static const char *netidbytype(int af, int sotype); 140b352972bSXin LI static void usage(void) __dead2; 141b352972bSXin LI static int xdr_dir(XDR *, char *); 142b352972bSXin LI static int xdr_fh(XDR *, struct nfhret *); 143b352972bSXin LI static enum tryret nfs_tryproto(struct addrinfo *ai, char *hostp, char *spec, 1444b5bc283SCraig Rodrigues char **errstr, struct iovec **iov, int *iovlen); 145b352972bSXin LI static enum tryret returncode(enum clnt_stat stat, struct rpc_err *rpcerr); 1468fae3551SRodney W. Grimes 1478fae3551SRodney W. Grimes int 14833924ad4SWarner Losh main(int argc, char *argv[]) 1498fae3551SRodney W. Grimes { 1503d438ad6SDavid E. O'Brien int c; 151c5aa1dc8SCraig Rodrigues struct iovec *iov; 1527039dfb3SJung-uk Kim int num, iovlen; 1536575d184SEdward Tomasz Napierala char *mntname, *p, *spec, *tmp; 154a6b26402SCraig Rodrigues char mntpath[MAXPATHLEN], errmsg[255]; 1556575d184SEdward Tomasz Napierala char hostname[MAXHOSTNAMELEN + 1], gssn[MAXHOSTNAMELEN + 50]; 1566575d184SEdward Tomasz Napierala const char *fstype, *gssname; 1578fae3551SRodney W. Grimes 158c5aa1dc8SCraig Rodrigues iov = NULL; 159c5aa1dc8SCraig Rodrigues iovlen = 0; 160a6b26402SCraig Rodrigues memset(errmsg, 0, sizeof(errmsg)); 161011981fdSRick Macklem gssname = NULL; 162c5aa1dc8SCraig Rodrigues 163412ffff0SCraig Rodrigues fstype = strrchr(argv[0], '_'); 164412ffff0SCraig Rodrigues if (fstype == NULL) 165412ffff0SCraig Rodrigues errx(EX_USAGE, "argv[0] must end in _fstype"); 166412ffff0SCraig Rodrigues 167412ffff0SCraig Rodrigues ++fstype; 168412ffff0SCraig Rodrigues 1698fae3551SRodney W. Grimes while ((c = getopt(argc, argv, 17086ce6a83SRobert Watson "23a:bcdD:g:I:iLlNo:PR:r:sTt:w:x:U")) != -1) 1718fae3551SRodney W. Grimes switch (c) { 1722cd1c32cSDoug Rabson case '2': 1732cd1c32cSDoug Rabson mountmode = V2; 1742cd1c32cSDoug Rabson break; 175a62dc406SDoug Rabson case '3': 1762cd1c32cSDoug Rabson mountmode = V3; 177a62dc406SDoug Rabson break; 1788fae3551SRodney W. Grimes case 'a': 179baa0988aSJaakko Heinonen printf("-a deprecated, use -o readahead=<value>\n"); 1804b5bc283SCraig Rodrigues build_iovec(&iov, &iovlen, "readahead", optarg, (size_t)-1); 1818fae3551SRodney W. Grimes break; 1828fae3551SRodney W. Grimes case 'b': 1838fae3551SRodney W. Grimes opflags |= BGRND; 1848fae3551SRodney W. Grimes break; 1858fae3551SRodney W. Grimes case 'c': 1864b5bc283SCraig Rodrigues printf("-c deprecated, use -o noconn\n"); 1874b5bc283SCraig Rodrigues build_iovec(&iov, &iovlen, "noconn", NULL, 0); 1884b5bc283SCraig Rodrigues noconn = 1; 1898fae3551SRodney W. Grimes break; 1908fae3551SRodney W. Grimes case 'D': 1914b5bc283SCraig Rodrigues printf("-D deprecated, use -o deadthresh=<value>\n"); 1924b5bc283SCraig Rodrigues build_iovec(&iov, &iovlen, "deadthresh", optarg, (size_t)-1); 1938fae3551SRodney W. Grimes break; 1948fae3551SRodney W. Grimes case 'd': 1954b5bc283SCraig Rodrigues printf("-d deprecated, use -o dumbtimer"); 1964b5bc283SCraig Rodrigues build_iovec(&iov, &iovlen, "dumbtimer", NULL, 0); 1978fae3551SRodney W. Grimes break; 1988fae3551SRodney W. Grimes case 'g': 1994b5bc283SCraig Rodrigues printf("-g deprecated, use -o maxgroups"); 2008fae3551SRodney W. Grimes num = strtol(optarg, &p, 10); 2018fae3551SRodney W. Grimes if (*p || num <= 0) 2028fae3551SRodney W. Grimes errx(1, "illegal -g value -- %s", optarg); 2034b5bc283SCraig Rodrigues //set_rpc_maxgrouplist(num); 2044b5bc283SCraig Rodrigues build_iovec(&iov, &iovlen, "maxgroups", optarg, (size_t)-1); 2058fae3551SRodney W. Grimes break; 206a62dc406SDoug Rabson case 'I': 2074b5bc283SCraig Rodrigues printf("-I deprecated, use -o readdirsize=<value>\n"); 2084b5bc283SCraig Rodrigues build_iovec(&iov, &iovlen, "readdirsize", optarg, (size_t)-1); 209a62dc406SDoug Rabson break; 2108fae3551SRodney W. Grimes case 'i': 2114b5bc283SCraig Rodrigues printf("-i deprecated, use -o intr\n"); 2124b5bc283SCraig Rodrigues build_iovec(&iov, &iovlen, "intr", NULL, 0); 2138fae3551SRodney W. Grimes break; 21413190d87SAlfred Perlstein case 'L': 215d97326d0SSimon L. B. Nielsen printf("-L deprecated, use -o nolockd\n"); 2164b5bc283SCraig Rodrigues build_iovec(&iov, &iovlen, "nolockd", NULL, 0); 21713190d87SAlfred Perlstein break; 2188fae3551SRodney W. Grimes case 'l': 2194b5bc283SCraig Rodrigues printf("-l deprecated, -o rdirplus\n"); 2204b5bc283SCraig Rodrigues build_iovec(&iov, &iovlen, "rdirplus", NULL, 0); 2218fae3551SRodney W. Grimes break; 222cc75b131SJoerg Wunsch case 'N': 2234b5bc283SCraig Rodrigues printf("-N deprecated, do not specify -o resvport\n"); 224cc75b131SJoerg Wunsch break; 2254b5bc283SCraig Rodrigues case 'o': { 2264b5bc283SCraig Rodrigues int pass_flag_to_nmount; 2274b5bc283SCraig Rodrigues char *opt = optarg; 2284b5bc283SCraig Rodrigues while (opt) { 2294b5bc283SCraig Rodrigues char *pval = NULL; 2304b5bc283SCraig Rodrigues char *pnextopt = NULL; 2316575d184SEdward Tomasz Napierala const char *val = ""; 2324b5bc283SCraig Rodrigues pass_flag_to_nmount = 1; 2334b5bc283SCraig Rodrigues pnextopt = strchr(opt, ','); 234bdb40379SJaakko Heinonen if (pnextopt != NULL) { 235bdb40379SJaakko Heinonen *pnextopt = '\0'; 236bdb40379SJaakko Heinonen pnextopt++; 237bdb40379SJaakko Heinonen } 238bdb40379SJaakko Heinonen pval = strchr(opt, '='); 2394b5bc283SCraig Rodrigues if (pval != NULL) { 2404b5bc283SCraig Rodrigues *pval = '\0'; 2414b5bc283SCraig Rodrigues val = pval + 1; 2424b5bc283SCraig Rodrigues } 2434b5bc283SCraig Rodrigues if (strcmp(opt, "bg") == 0) { 2443fa88decSGarrett Wollman opflags |= BGRND; 2454b5bc283SCraig Rodrigues pass_flag_to_nmount=0; 2464b5bc283SCraig Rodrigues } else if (strcmp(opt, "fg") == 0) { 2474b5bc283SCraig Rodrigues /* same as not specifying -o bg */ 2484b5bc283SCraig Rodrigues pass_flag_to_nmount=0; 249011981fdSRick Macklem } else if (strcmp(opt, "gssname") == 0) { 250011981fdSRick Macklem pass_flag_to_nmount = 0; 251011981fdSRick Macklem gssname = val; 2524b5bc283SCraig Rodrigues } else if (strcmp(opt, "mntudp") == 0) { 253a62dc406SDoug Rabson mnttcp_ok = 0; 254bf005f32SKris Kennaway nfsproto = IPPROTO_UDP; 2554b5bc283SCraig Rodrigues } else if (strcmp(opt, "udp") == 0) { 2564b5bc283SCraig Rodrigues nfsproto = IPPROTO_UDP; 2574b5bc283SCraig Rodrigues } else if (strcmp(opt, "tcp") == 0) { 258a62dc406SDoug Rabson nfsproto = IPPROTO_TCP; 2594b5bc283SCraig Rodrigues } else if (strcmp(opt, "noinet4") == 0) { 2604b5bc283SCraig Rodrigues pass_flag_to_nmount=0; 2614b5bc283SCraig Rodrigues opflags |= OF_NOINET4; 2624b5bc283SCraig Rodrigues } else if (strcmp(opt, "noinet6") == 0) { 2634b5bc283SCraig Rodrigues pass_flag_to_nmount=0; 2644b5bc283SCraig Rodrigues opflags |= OF_NOINET6; 2654b5bc283SCraig Rodrigues } else if (strcmp(opt, "noconn") == 0) { 2664b5bc283SCraig Rodrigues noconn = 1; 2674b5bc283SCraig Rodrigues } else if (strcmp(opt, "nfsv2") == 0) { 2684b5bc283SCraig Rodrigues pass_flag_to_nmount=0; 2694b5bc283SCraig Rodrigues mountmode = V2; 2704b5bc283SCraig Rodrigues } else if (strcmp(opt, "nfsv3") == 0) { 2714b5bc283SCraig Rodrigues mountmode = V3; 272011981fdSRick Macklem } else if (strcmp(opt, "nfsv4") == 0) { 273011981fdSRick Macklem pass_flag_to_nmount=0; 274011981fdSRick Macklem mountmode = V4; 2754309e17aSRick Macklem fstype = "nfs"; 276011981fdSRick Macklem nfsproto = IPPROTO_TCP; 277011981fdSRick Macklem if (portspec == NULL) 278011981fdSRick Macklem portspec = "2049"; 2794b5bc283SCraig Rodrigues } else if (strcmp(opt, "port") == 0) { 2804b5bc283SCraig Rodrigues pass_flag_to_nmount=0; 2816575d184SEdward Tomasz Napierala asprintf(&tmp, "%d", atoi(val)); 2826575d184SEdward Tomasz Napierala if (tmp == NULL) 283317d5933SIan Dowse err(1, "asprintf"); 2846575d184SEdward Tomasz Napierala portspec = tmp; 285011981fdSRick Macklem } else if (strcmp(opt, "principal") == 0) { 286011981fdSRick Macklem got_principal = 1; 2875a06ac35SEdward Tomasz Napierala } else if (strcmp(opt, "proto") == 0) { 2885a06ac35SEdward Tomasz Napierala pass_flag_to_nmount=0; 2895a06ac35SEdward Tomasz Napierala if (strcmp(val, "tcp") == 0) { 2905a06ac35SEdward Tomasz Napierala nfsproto = IPPROTO_TCP; 2915a06ac35SEdward Tomasz Napierala opflags |= OF_NOINET6; 2925a06ac35SEdward Tomasz Napierala build_iovec(&iov, &iovlen, 2935a06ac35SEdward Tomasz Napierala "tcp", NULL, 0); 2945a06ac35SEdward Tomasz Napierala } else if (strcmp(val, "udp") == 0) { 2955a06ac35SEdward Tomasz Napierala mnttcp_ok = 0; 2965a06ac35SEdward Tomasz Napierala nfsproto = IPPROTO_UDP; 2975a06ac35SEdward Tomasz Napierala opflags |= OF_NOINET6; 2985a06ac35SEdward Tomasz Napierala build_iovec(&iov, &iovlen, 2995a06ac35SEdward Tomasz Napierala "udp", NULL, 0); 3005a06ac35SEdward Tomasz Napierala } else if (strcmp(val, "tcp6") == 0) { 3015a06ac35SEdward Tomasz Napierala nfsproto = IPPROTO_TCP; 3025a06ac35SEdward Tomasz Napierala opflags |= OF_NOINET4; 3035a06ac35SEdward Tomasz Napierala build_iovec(&iov, &iovlen, 3045a06ac35SEdward Tomasz Napierala "tcp", NULL, 0); 3055a06ac35SEdward Tomasz Napierala } else if (strcmp(val, "udp6") == 0) { 3065a06ac35SEdward Tomasz Napierala mnttcp_ok = 0; 3075a06ac35SEdward Tomasz Napierala nfsproto = IPPROTO_UDP; 3085a06ac35SEdward Tomasz Napierala opflags |= OF_NOINET4; 3095a06ac35SEdward Tomasz Napierala build_iovec(&iov, &iovlen, 3105a06ac35SEdward Tomasz Napierala "udp", NULL, 0); 3115a06ac35SEdward Tomasz Napierala } else { 3125a06ac35SEdward Tomasz Napierala errx(1, 3135a06ac35SEdward Tomasz Napierala "illegal proto value -- %s", 3145a06ac35SEdward Tomasz Napierala val); 3155a06ac35SEdward Tomasz Napierala } 316a9148abdSDoug Rabson } else if (strcmp(opt, "sec") == 0) { 317a9148abdSDoug Rabson /* 318a9148abdSDoug Rabson * Don't add this option to 319a9148abdSDoug Rabson * the iovec yet - we will 320a9148abdSDoug Rabson * negotiate which sec flavor 321a9148abdSDoug Rabson * to use with the remote 322a9148abdSDoug Rabson * mountd. 323a9148abdSDoug Rabson */ 324a9148abdSDoug Rabson pass_flag_to_nmount=0; 325a9148abdSDoug Rabson secflavor = sec_name_to_num(val); 326a9148abdSDoug Rabson if (secflavor < 0) { 327a9148abdSDoug Rabson errx(1, 328a9148abdSDoug Rabson "illegal sec value -- %s", 329a9148abdSDoug Rabson val); 330a9148abdSDoug Rabson } 3314b5bc283SCraig Rodrigues } else if (strcmp(opt, "retrycnt") == 0) { 3324b5bc283SCraig Rodrigues pass_flag_to_nmount=0; 3334b5bc283SCraig Rodrigues num = strtol(val, &p, 10); 3344b5bc283SCraig Rodrigues if (*p || num < 0) 3354b5bc283SCraig Rodrigues errx(1, "illegal retrycnt value -- %s", val); 3364b5bc283SCraig Rodrigues retrycnt = num; 3374b5bc283SCraig Rodrigues } else if (strcmp(opt, "maxgroups") == 0) { 3384b5bc283SCraig Rodrigues num = strtol(val, &p, 10); 3394b5bc283SCraig Rodrigues if (*p || num <= 0) 3404b5bc283SCraig Rodrigues errx(1, "illegal maxgroups value -- %s", val); 3414b5bc283SCraig Rodrigues //set_rpc_maxgrouplist(num); 342528c159fSBjoern A. Zeeb } else if (strcmp(opt, "vers") == 0) { 343528c159fSBjoern A. Zeeb num = strtol(val, &p, 10); 344528c159fSBjoern A. Zeeb if (*p || num <= 0) 345528c159fSBjoern A. Zeeb errx(1, "illegal vers value -- " 346528c159fSBjoern A. Zeeb "%s", val); 347528c159fSBjoern A. Zeeb switch (num) { 348528c159fSBjoern A. Zeeb case 2: 349528c159fSBjoern A. Zeeb mountmode = V2; 350528c159fSBjoern A. Zeeb break; 351528c159fSBjoern A. Zeeb case 3: 352528c159fSBjoern A. Zeeb mountmode = V3; 353528c159fSBjoern A. Zeeb build_iovec(&iov, &iovlen, 354528c159fSBjoern A. Zeeb "nfsv3", NULL, 0); 355528c159fSBjoern A. Zeeb break; 356528c159fSBjoern A. Zeeb case 4: 357528c159fSBjoern A. Zeeb mountmode = V4; 358528c159fSBjoern A. Zeeb fstype = "nfs"; 359528c159fSBjoern A. Zeeb nfsproto = IPPROTO_TCP; 360528c159fSBjoern A. Zeeb if (portspec == NULL) 361528c159fSBjoern A. Zeeb portspec = "2049"; 362528c159fSBjoern A. Zeeb break; 363528c159fSBjoern A. Zeeb default: 364528c159fSBjoern A. Zeeb errx(1, "illegal nfs version " 365528c159fSBjoern A. Zeeb "value -- %s", val); 366528c159fSBjoern A. Zeeb } 367528c159fSBjoern A. Zeeb pass_flag_to_nmount=0; 368317d5933SIan Dowse } 3696575d184SEdward Tomasz Napierala if (pass_flag_to_nmount) { 3706575d184SEdward Tomasz Napierala build_iovec(&iov, &iovlen, opt, 3716575d184SEdward Tomasz Napierala __DECONST(void *, val), 3724b5bc283SCraig Rodrigues strlen(val) + 1); 3736575d184SEdward Tomasz Napierala } 3744b5bc283SCraig Rodrigues opt = pnextopt; 3754db56604SPeter Wemm } 3764db56604SPeter Wemm } 3778fae3551SRodney W. Grimes break; 3788fae3551SRodney W. Grimes case 'P': 3794b5bc283SCraig Rodrigues /* obsolete for -o noresvport now default */ 3804b5bc283SCraig Rodrigues printf("-P deprecated, use -o noresvport\n"); 3814b5bc283SCraig Rodrigues build_iovec(&iov, &iovlen, "noresvport", NULL, 0); 3828fae3551SRodney W. Grimes break; 3838fae3551SRodney W. Grimes case 'R': 3844b5bc283SCraig Rodrigues printf("-R deprecated, use -o retrycnt=<retrycnt>\n"); 3858fae3551SRodney W. Grimes num = strtol(optarg, &p, 10); 386e16873daSIan Dowse if (*p || num < 0) 3878fae3551SRodney W. Grimes errx(1, "illegal -R value -- %s", optarg); 3888fae3551SRodney W. Grimes retrycnt = num; 3898fae3551SRodney W. Grimes break; 3908fae3551SRodney W. Grimes case 'r': 3914b5bc283SCraig Rodrigues printf("-r deprecated, use -o rsize=<rsize>\n"); 3924b5bc283SCraig Rodrigues build_iovec(&iov, &iovlen, "rsize", optarg, (size_t)-1); 3938fae3551SRodney W. Grimes break; 3948fae3551SRodney W. Grimes case 's': 3954b5bc283SCraig Rodrigues printf("-s deprecated, use -o soft\n"); 3964b5bc283SCraig Rodrigues build_iovec(&iov, &iovlen, "soft", NULL, 0); 3978fae3551SRodney W. Grimes break; 3988fae3551SRodney W. Grimes case 'T': 399a62dc406SDoug Rabson nfsproto = IPPROTO_TCP; 4004b5bc283SCraig Rodrigues printf("-T deprecated, use -o tcp\n"); 4018fae3551SRodney W. Grimes break; 4028fae3551SRodney W. Grimes case 't': 4034b5bc283SCraig Rodrigues printf("-t deprecated, use -o timeout=<value>\n"); 4044b5bc283SCraig Rodrigues build_iovec(&iov, &iovlen, "timeout", optarg, (size_t)-1); 4058fae3551SRodney W. Grimes break; 4068fae3551SRodney W. Grimes case 'w': 4074b5bc283SCraig Rodrigues printf("-w deprecated, use -o wsize=<value>\n"); 4084b5bc283SCraig Rodrigues build_iovec(&iov, &iovlen, "wsize", optarg, (size_t)-1); 4098fae3551SRodney W. Grimes break; 4108fae3551SRodney W. Grimes case 'x': 4114b5bc283SCraig Rodrigues printf("-x deprecated, use -o retrans=<value>\n"); 4124b5bc283SCraig Rodrigues build_iovec(&iov, &iovlen, "retrans", optarg, (size_t)-1); 4138fae3551SRodney W. Grimes break; 414a62dc406SDoug Rabson case 'U': 4154b5bc283SCraig Rodrigues printf("-U deprecated, use -o mntudp\n"); 416a62dc406SDoug Rabson mnttcp_ok = 0; 4170c269d1fSCraig Rodrigues nfsproto = IPPROTO_UDP; 4184b5bc283SCraig Rodrigues build_iovec(&iov, &iovlen, "mntudp", NULL, 0); 419a62dc406SDoug Rabson break; 4208fae3551SRodney W. Grimes default: 4218fae3551SRodney W. Grimes usage(); 4228fae3551SRodney W. Grimes break; 4238fae3551SRodney W. Grimes } 4248fae3551SRodney W. Grimes argc -= optind; 4258fae3551SRodney W. Grimes argv += optind; 4268fae3551SRodney W. Grimes 4274a4c5285SPeter Wemm if (argc != 2) { 4282f21d07aSDavid Greenman usage(); 4294a4c5285SPeter Wemm /* NOTREACHED */ 4304a4c5285SPeter Wemm } 4318fae3551SRodney W. Grimes 4328fae3551SRodney W. Grimes spec = *argv++; 4336575d184SEdward Tomasz Napierala mntname = *argv; 4348fae3551SRodney W. Grimes 435e16873daSIan Dowse if (retrycnt == -1) 4362bc53e11SIan Dowse /* The default is to keep retrying forever. */ 4372bc53e11SIan Dowse retrycnt = 0; 4380c269d1fSCraig Rodrigues 439011981fdSRick Macklem /* 4404309e17aSRick Macklem * If the fstye is "oldnfs", run the old NFS client unless the 4414309e17aSRick Macklem * "nfsv4" option was specified. 442011981fdSRick Macklem */ 4434309e17aSRick Macklem if (strcmp(fstype, "nfs") == 0) { 444011981fdSRick Macklem if (modfind("nfscl") < 0) { 445011981fdSRick Macklem /* Not present in kernel, try loading it */ 446011981fdSRick Macklem if (kldload("nfscl") < 0 || 447011981fdSRick Macklem modfind("nfscl") < 0) 448011981fdSRick Macklem errx(1, "nfscl is not available"); 449011981fdSRick Macklem } 450011981fdSRick Macklem } 451011981fdSRick Macklem 452011981fdSRick Macklem /* 453011981fdSRick Macklem * Add the fqdn to the gssname, as required. 454011981fdSRick Macklem */ 455011981fdSRick Macklem if (gssname != NULL) { 456011981fdSRick Macklem if (strchr(gssname, '@') == NULL && 457011981fdSRick Macklem gethostname(hostname, MAXHOSTNAMELEN) == 0) { 458011981fdSRick Macklem snprintf(gssn, sizeof (gssn), "%s@%s", gssname, 459011981fdSRick Macklem hostname); 460011981fdSRick Macklem gssname = gssn; 461011981fdSRick Macklem } 4626575d184SEdward Tomasz Napierala build_iovec(&iov, &iovlen, "gssname", 4636575d184SEdward Tomasz Napierala __DECONST(void *, gssname), strlen(gssname) + 1); 464011981fdSRick Macklem } 465011981fdSRick Macklem 4664b5bc283SCraig Rodrigues if (!getnfsargs(spec, &iov, &iovlen)) 4678fae3551SRodney W. Grimes exit(1); 468d599144dSGarrett Wollman 46973dd3167SPoul-Henning Kamp /* resolve the mountpoint with realpath(3) */ 4706575d184SEdward Tomasz Napierala if (checkpath(mntname, mntpath) != 0) 471d3250014SJaakko Heinonen err(1, "%s", mntpath); 47273dd3167SPoul-Henning Kamp 4736575d184SEdward Tomasz Napierala build_iovec(&iov, &iovlen, "fstype", 4746575d184SEdward Tomasz Napierala __DECONST(void *, fstype), (size_t)-1); 475c5aa1dc8SCraig Rodrigues build_iovec(&iov, &iovlen, "fspath", mntpath, (size_t)-1); 476a6b26402SCraig Rodrigues build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg)); 477c5aa1dc8SCraig Rodrigues 4787039dfb3SJung-uk Kim if (nmount(iov, iovlen, 0)) 479773e5651SConrad Meyer err(1, "nmount: %s%s%s", mntpath, errmsg[0] ? ", " : "", 480773e5651SConrad Meyer errmsg); 481a62dc406SDoug Rabson 4828fae3551SRodney W. Grimes exit(0); 4838fae3551SRodney W. Grimes } 4848fae3551SRodney W. Grimes 4854b5bc283SCraig Rodrigues static int 4866575d184SEdward Tomasz Napierala sec_name_to_num(const char *sec) 487a9148abdSDoug Rabson { 488a9148abdSDoug Rabson if (!strcmp(sec, "krb5")) 489a9148abdSDoug Rabson return (RPCSEC_GSS_KRB5); 490a9148abdSDoug Rabson if (!strcmp(sec, "krb5i")) 491a9148abdSDoug Rabson return (RPCSEC_GSS_KRB5I); 492a9148abdSDoug Rabson if (!strcmp(sec, "krb5p")) 493a9148abdSDoug Rabson return (RPCSEC_GSS_KRB5P); 494a9148abdSDoug Rabson if (!strcmp(sec, "sys")) 495a9148abdSDoug Rabson return (AUTH_SYS); 496a9148abdSDoug Rabson return (-1); 497a9148abdSDoug Rabson } 498a9148abdSDoug Rabson 4996575d184SEdward Tomasz Napierala static const char * 500a9148abdSDoug Rabson sec_num_to_name(int flavor) 501a9148abdSDoug Rabson { 502a9148abdSDoug Rabson switch (flavor) { 503a9148abdSDoug Rabson case RPCSEC_GSS_KRB5: 504a9148abdSDoug Rabson return ("krb5"); 505a9148abdSDoug Rabson case RPCSEC_GSS_KRB5I: 506a9148abdSDoug Rabson return ("krb5i"); 507a9148abdSDoug Rabson case RPCSEC_GSS_KRB5P: 508a9148abdSDoug Rabson return ("krb5p"); 509a9148abdSDoug Rabson case AUTH_SYS: 510a9148abdSDoug Rabson return ("sys"); 511a9148abdSDoug Rabson } 512a9148abdSDoug Rabson return (NULL); 513a9148abdSDoug Rabson } 514a9148abdSDoug Rabson 515b352972bSXin LI static int 5164b5bc283SCraig Rodrigues getnfsargs(char *spec, struct iovec **iov, int *iovlen) 5178fae3551SRodney W. Grimes { 5188360efbdSAlfred Perlstein struct addrinfo hints, *ai_nfs, *ai; 519317d5933SIan Dowse enum tryret ret; 520a505d435SHajimu UMEMOTO int ecode, speclen, remoteerr, offset, have_bracket = 0; 521317d5933SIan Dowse char *hostp, *delimp, *errstr; 52266a84ea7SBrian Feldman size_t len; 523011981fdSRick Macklem static char nam[MNAMELEN + 1], pname[MAXHOSTNAMELEN + 5]; 5248fae3551SRodney W. Grimes 525a505d435SHajimu UMEMOTO if (*spec == '[' && (delimp = strchr(spec + 1, ']')) != NULL && 526a505d435SHajimu UMEMOTO *(delimp + 1) == ':') { 527a505d435SHajimu UMEMOTO hostp = spec + 1; 528a505d435SHajimu UMEMOTO spec = delimp + 2; 529a505d435SHajimu UMEMOTO have_bracket = 1; 530a505d435SHajimu UMEMOTO } else if ((delimp = strrchr(spec, ':')) != NULL) { 5318fae3551SRodney W. Grimes hostp = spec; 5328fae3551SRodney W. Grimes spec = delimp + 1; 53373dd3167SPoul-Henning Kamp } else if ((delimp = strrchr(spec, '@')) != NULL) { 53473dd3167SPoul-Henning Kamp warnx("path@server syntax is deprecated, use server:path"); 53573dd3167SPoul-Henning Kamp hostp = delimp + 1; 5368fae3551SRodney W. Grimes } else { 53773dd3167SPoul-Henning Kamp warnx("no <host>:<dirpath> nfs-name"); 5388fae3551SRodney W. Grimes return (0); 5398fae3551SRodney W. Grimes } 5408fae3551SRodney W. Grimes *delimp = '\0'; 54173dd3167SPoul-Henning Kamp 54273dd3167SPoul-Henning Kamp /* 54373dd3167SPoul-Henning Kamp * If there has been a trailing slash at mounttime it seems 54473dd3167SPoul-Henning Kamp * that some mountd implementations fail to remove the mount 54573dd3167SPoul-Henning Kamp * entries from their mountlist while unmounting. 54673dd3167SPoul-Henning Kamp */ 54766a84ea7SBrian Feldman for (speclen = strlen(spec); 54866a84ea7SBrian Feldman speclen > 1 && spec[speclen - 1] == '/'; 54966a84ea7SBrian Feldman speclen--) 55073dd3167SPoul-Henning Kamp spec[speclen - 1] = '\0'; 55173dd3167SPoul-Henning Kamp if (strlen(hostp) + strlen(spec) + 1 > MNAMELEN) { 55273dd3167SPoul-Henning Kamp warnx("%s:%s: %s", hostp, spec, strerror(ENAMETOOLONG)); 55373dd3167SPoul-Henning Kamp return (0); 55473dd3167SPoul-Henning Kamp } 55573dd3167SPoul-Henning Kamp /* Make both '@' and ':' notations equal */ 55666a84ea7SBrian Feldman if (*hostp != '\0') { 55766a84ea7SBrian Feldman len = strlen(hostp); 558a505d435SHajimu UMEMOTO offset = 0; 559a505d435SHajimu UMEMOTO if (have_bracket) 560a505d435SHajimu UMEMOTO nam[offset++] = '['; 561a505d435SHajimu UMEMOTO memmove(nam + offset, hostp, len); 562a505d435SHajimu UMEMOTO if (have_bracket) 563a505d435SHajimu UMEMOTO nam[len + offset++] = ']'; 564a505d435SHajimu UMEMOTO nam[len + offset++] = ':'; 565a505d435SHajimu UMEMOTO memmove(nam + len + offset, spec, speclen); 566a505d435SHajimu UMEMOTO nam[len + speclen + offset] = '\0'; 56766a84ea7SBrian Feldman } 5688fae3551SRodney W. Grimes 5698fae3551SRodney W. Grimes /* 57091196234SPeter Wemm * Handle an internet host address. 5718fae3551SRodney W. Grimes */ 5728360efbdSAlfred Perlstein memset(&hints, 0, sizeof hints); 5738360efbdSAlfred Perlstein hints.ai_flags = AI_NUMERICHOST; 5744b5bc283SCraig Rodrigues if (nfsproto == IPPROTO_TCP) 5754b5bc283SCraig Rodrigues hints.ai_socktype = SOCK_STREAM; 5764b5bc283SCraig Rodrigues else if (nfsproto == IPPROTO_UDP) 5774b5bc283SCraig Rodrigues hints.ai_socktype = SOCK_DGRAM; 5784b5bc283SCraig Rodrigues 57991196234SPeter Wemm if (getaddrinfo(hostp, portspec, &hints, &ai_nfs) != 0) { 580011981fdSRick Macklem hints.ai_flags = AI_CANONNAME; 581317d5933SIan Dowse if ((ecode = getaddrinfo(hostp, portspec, &hints, &ai_nfs)) 582317d5933SIan Dowse != 0) { 583317d5933SIan Dowse if (portspec == NULL) 584317d5933SIan Dowse errx(1, "%s: %s", hostp, gai_strerror(ecode)); 585317d5933SIan Dowse else 586317d5933SIan Dowse errx(1, "%s:%s: %s", hostp, portspec, 5878360efbdSAlfred Perlstein gai_strerror(ecode)); 5888360efbdSAlfred Perlstein return (0); 5898360efbdSAlfred Perlstein } 590011981fdSRick Macklem 591011981fdSRick Macklem /* 592011981fdSRick Macklem * For a Kerberized nfs mount where the "principal" 593011981fdSRick Macklem * argument has not been set, add it here. 594011981fdSRick Macklem */ 595c088e62eSRick Macklem if (got_principal == 0 && secflavor != AUTH_SYS && 596c088e62eSRick Macklem ai_nfs->ai_canonname != NULL) { 597011981fdSRick Macklem snprintf(pname, sizeof (pname), "nfs@%s", 598011981fdSRick Macklem ai_nfs->ai_canonname); 599011981fdSRick Macklem build_iovec(iov, iovlen, "principal", pname, 600011981fdSRick Macklem strlen(pname) + 1); 601011981fdSRick Macklem } 6028360efbdSAlfred Perlstein } 6038fae3551SRodney W. Grimes 604317d5933SIan Dowse ret = TRYRET_LOCALERR; 605e16873daSIan Dowse for (;;) { 606deffdffaSAndrey A. Chernov /* 607317d5933SIan Dowse * Try each entry returned by getaddrinfo(). Note the 6084b85a12fSUlrich Spörlein * occurrence of remote errors by setting `remoteerr'. 609deffdffaSAndrey A. Chernov */ 610317d5933SIan Dowse remoteerr = 0; 611317d5933SIan Dowse for (ai = ai_nfs; ai != NULL; ai = ai->ai_next) { 612302f15f9SMatthew N. Dodd if ((ai->ai_family == AF_INET6) && 613302f15f9SMatthew N. Dodd (opflags & OF_NOINET6)) 614302f15f9SMatthew N. Dodd continue; 615302f15f9SMatthew N. Dodd if ((ai->ai_family == AF_INET) && 616302f15f9SMatthew N. Dodd (opflags & OF_NOINET4)) 617302f15f9SMatthew N. Dodd continue; 6184b5bc283SCraig Rodrigues ret = nfs_tryproto(ai, hostp, spec, &errstr, iov, 6194b5bc283SCraig Rodrigues iovlen); 620317d5933SIan Dowse if (ret == TRYRET_SUCCESS) 621317d5933SIan Dowse break; 622317d5933SIan Dowse if (ret != TRYRET_LOCALERR) 623317d5933SIan Dowse remoteerr = 1; 624317d5933SIan Dowse if ((opflags & ISBGRND) == 0) 625317d5933SIan Dowse fprintf(stderr, "%s\n", errstr); 626deffdffaSAndrey A. Chernov } 627317d5933SIan Dowse if (ret == TRYRET_SUCCESS) 628317d5933SIan Dowse break; 629deffdffaSAndrey A. Chernov 630e16873daSIan Dowse /* Exit if all errors were local. */ 631e16873daSIan Dowse if (!remoteerr) 632deffdffaSAndrey A. Chernov exit(1); 633317d5933SIan Dowse 634e16873daSIan Dowse /* 635e16873daSIan Dowse * If retrycnt == 0, we are to keep retrying forever. 636e16873daSIan Dowse * Otherwise decrement it, and exit if it hits zero. 637e16873daSIan Dowse */ 638e16873daSIan Dowse if (retrycnt != 0 && --retrycnt == 0) 639317d5933SIan Dowse exit(1); 640317d5933SIan Dowse 641317d5933SIan Dowse if ((opflags & (BGRND | ISBGRND)) == BGRND) { 642317d5933SIan Dowse warnx("Cannot immediately mount %s:%s, backgrounding", 643317d5933SIan Dowse hostp, spec); 644317d5933SIan Dowse opflags |= ISBGRND; 645317d5933SIan Dowse if (daemon(0, 0) != 0) 646317d5933SIan Dowse err(1, "daemon"); 6478fae3551SRodney W. Grimes } 6488fae3551SRodney W. Grimes sleep(60); 6498fae3551SRodney W. Grimes } 6508360efbdSAlfred Perlstein freeaddrinfo(ai_nfs); 6514b5bc283SCraig Rodrigues 6524b5bc283SCraig Rodrigues build_iovec(iov, iovlen, "hostname", nam, (size_t)-1); 653a69497d7SMatthew Dillon /* Add mounted file system to PATH_MOUNTTAB */ 654a69497d7SMatthew Dillon if (!add_mtab(hostp, spec)) 655a69497d7SMatthew Dillon warnx("can't update %s for %s:%s", PATH_MOUNTTAB, hostp, spec); 6568fae3551SRodney W. Grimes return (1); 6578fae3551SRodney W. Grimes } 6588fae3551SRodney W. Grimes 6598fae3551SRodney W. Grimes /* 660317d5933SIan Dowse * Try to set up the NFS arguments according to the address 661317d5933SIan Dowse * family, protocol (and possibly port) specified in `ai'. 662317d5933SIan Dowse * 663317d5933SIan Dowse * Returns TRYRET_SUCCESS if successful, or: 664317d5933SIan Dowse * TRYRET_TIMEOUT The server did not respond. 665317d5933SIan Dowse * TRYRET_REMOTEERR The server reported an error. 666317d5933SIan Dowse * TRYRET_LOCALERR Local failure. 667317d5933SIan Dowse * 668317d5933SIan Dowse * In all error cases, *errstr will be set to a statically-allocated string 669317d5933SIan Dowse * describing the error. 670317d5933SIan Dowse */ 671b352972bSXin LI static enum tryret 6724b5bc283SCraig Rodrigues nfs_tryproto(struct addrinfo *ai, char *hostp, char *spec, char **errstr, 6734b5bc283SCraig Rodrigues struct iovec **iov, int *iovlen) 674317d5933SIan Dowse { 675317d5933SIan Dowse static char errbuf[256]; 676317d5933SIan Dowse struct sockaddr_storage nfs_ss; 677317d5933SIan Dowse struct netbuf nfs_nb; 678317d5933SIan Dowse struct nfhret nfhret; 679317d5933SIan Dowse struct timeval try; 680317d5933SIan Dowse struct rpc_err rpcerr; 681317d5933SIan Dowse CLIENT *clp; 682317d5933SIan Dowse struct netconfig *nconf, *nconf_mnt; 6836575d184SEdward Tomasz Napierala const char *netid, *netid_mnt, *secname; 6844b5bc283SCraig Rodrigues int doconnect, nfsvers, mntvers, sotype; 6856575d184SEdward Tomasz Napierala enum clnt_stat clntstat; 686317d5933SIan Dowse enum mountmode trymntmode; 687317d5933SIan Dowse 688d0654c85SKevin Lo sotype = 0; 689317d5933SIan Dowse trymntmode = mountmode; 690317d5933SIan Dowse errbuf[0] = '\0'; 691317d5933SIan Dowse *errstr = errbuf; 692317d5933SIan Dowse 6934b5bc283SCraig Rodrigues if (nfsproto == IPPROTO_TCP) 6944b5bc283SCraig Rodrigues sotype = SOCK_STREAM; 6954b5bc283SCraig Rodrigues else if (nfsproto == IPPROTO_UDP) 6964b5bc283SCraig Rodrigues sotype = SOCK_DGRAM; 6974b5bc283SCraig Rodrigues 6984b5bc283SCraig Rodrigues if ((netid = netidbytype(ai->ai_family, sotype)) == NULL) { 699a3d8f94bSIan Dowse snprintf(errbuf, sizeof errbuf, 7004b5bc283SCraig Rodrigues "af %d sotype %d not supported", ai->ai_family, sotype); 701a3d8f94bSIan Dowse return (TRYRET_LOCALERR); 702a3d8f94bSIan Dowse } 703a3d8f94bSIan Dowse if ((nconf = getnetconf_cached(netid)) == NULL) { 704317d5933SIan Dowse snprintf(errbuf, sizeof errbuf, "%s: %s", netid, nc_sperror()); 705317d5933SIan Dowse return (TRYRET_LOCALERR); 706317d5933SIan Dowse } 707317d5933SIan Dowse /* The RPCPROG_MNT netid may be different. */ 708317d5933SIan Dowse if (mnttcp_ok) { 709ba33efd9SIan Dowse netid_mnt = netid; 710317d5933SIan Dowse nconf_mnt = nconf; 711317d5933SIan Dowse } else { 712a3d8f94bSIan Dowse if ((netid_mnt = netidbytype(ai->ai_family, SOCK_DGRAM)) 713a3d8f94bSIan Dowse == NULL) { 714a3d8f94bSIan Dowse snprintf(errbuf, sizeof errbuf, 715a3d8f94bSIan Dowse "af %d sotype SOCK_DGRAM not supported", 716a3d8f94bSIan Dowse ai->ai_family); 717a3d8f94bSIan Dowse return (TRYRET_LOCALERR); 718a3d8f94bSIan Dowse } 719a3d8f94bSIan Dowse if ((nconf_mnt = getnetconf_cached(netid_mnt)) == NULL) { 720ba33efd9SIan Dowse snprintf(errbuf, sizeof errbuf, "%s: %s", netid_mnt, 721317d5933SIan Dowse nc_sperror()); 722317d5933SIan Dowse return (TRYRET_LOCALERR); 723317d5933SIan Dowse } 724317d5933SIan Dowse } 725317d5933SIan Dowse 726317d5933SIan Dowse tryagain: 727011981fdSRick Macklem if (trymntmode == V4) { 728011981fdSRick Macklem nfsvers = 4; 7296575d184SEdward Tomasz Napierala mntvers = 3; /* Workaround for GCC. */ 730011981fdSRick Macklem } else if (trymntmode == V2) { 731317d5933SIan Dowse nfsvers = 2; 732317d5933SIan Dowse mntvers = 1; 733317d5933SIan Dowse } else { 734317d5933SIan Dowse nfsvers = 3; 735317d5933SIan Dowse mntvers = 3; 736317d5933SIan Dowse } 737317d5933SIan Dowse 738317d5933SIan Dowse if (portspec != NULL) { 739317d5933SIan Dowse /* `ai' contains the complete nfsd sockaddr. */ 740317d5933SIan Dowse nfs_nb.buf = ai->ai_addr; 741317d5933SIan Dowse nfs_nb.len = nfs_nb.maxlen = ai->ai_addrlen; 742317d5933SIan Dowse } else { 743317d5933SIan Dowse /* Ask the remote rpcbind. */ 744317d5933SIan Dowse nfs_nb.buf = &nfs_ss; 745317d5933SIan Dowse nfs_nb.len = nfs_nb.maxlen = sizeof nfs_ss; 746317d5933SIan Dowse 7470775314bSDoug Rabson if (!rpcb_getaddr(NFS_PROGRAM, nfsvers, nconf, &nfs_nb, 748317d5933SIan Dowse hostp)) { 749317d5933SIan Dowse if (rpc_createerr.cf_stat == RPC_PROGVERSMISMATCH && 750317d5933SIan Dowse trymntmode == ANY) { 751317d5933SIan Dowse trymntmode = V2; 752317d5933SIan Dowse goto tryagain; 753317d5933SIan Dowse } 754317d5933SIan Dowse snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", 755317d5933SIan Dowse netid, hostp, spec, 756317d5933SIan Dowse clnt_spcreateerror("RPCPROG_NFS")); 757317d5933SIan Dowse return (returncode(rpc_createerr.cf_stat, 758317d5933SIan Dowse &rpc_createerr.cf_error)); 759317d5933SIan Dowse } 760317d5933SIan Dowse } 761317d5933SIan Dowse 762317d5933SIan Dowse /* Check that the server (nfsd) responds on the port we have chosen. */ 7630775314bSDoug Rabson clp = clnt_tli_create(RPC_ANYFD, nconf, &nfs_nb, NFS_PROGRAM, nfsvers, 764317d5933SIan Dowse 0, 0); 765317d5933SIan Dowse if (clp == NULL) { 766317d5933SIan Dowse snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid, 767317d5933SIan Dowse hostp, spec, clnt_spcreateerror("nfsd: RPCPROG_NFS")); 768317d5933SIan Dowse return (returncode(rpc_createerr.cf_stat, 769317d5933SIan Dowse &rpc_createerr.cf_error)); 770317d5933SIan Dowse } 7714b5bc283SCraig Rodrigues if (sotype == SOCK_DGRAM && noconn == 0) { 772eca1c24eSIan Dowse /* 773eca1c24eSIan Dowse * Use connect(), to match what the kernel does. This 774eca1c24eSIan Dowse * catches cases where the server responds from the 775eca1c24eSIan Dowse * wrong source address. 776eca1c24eSIan Dowse */ 777eca1c24eSIan Dowse doconnect = 1; 778eca1c24eSIan Dowse if (!clnt_control(clp, CLSET_CONNECT, (char *)&doconnect)) { 779eca1c24eSIan Dowse clnt_destroy(clp); 780eca1c24eSIan Dowse snprintf(errbuf, sizeof errbuf, 781eca1c24eSIan Dowse "[%s] %s:%s: CLSET_CONNECT failed", netid, hostp, 782eca1c24eSIan Dowse spec); 783eca1c24eSIan Dowse return (TRYRET_LOCALERR); 784eca1c24eSIan Dowse } 785eca1c24eSIan Dowse } 786eca1c24eSIan Dowse 787317d5933SIan Dowse try.tv_sec = 10; 788317d5933SIan Dowse try.tv_usec = 0; 7896575d184SEdward Tomasz Napierala clntstat = clnt_call(clp, NFSPROC_NULL, (xdrproc_t)xdr_void, NULL, 790011981fdSRick Macklem (xdrproc_t)xdr_void, NULL, try); 7916575d184SEdward Tomasz Napierala if (clntstat != RPC_SUCCESS) { 7926575d184SEdward Tomasz Napierala if (clntstat == RPC_PROGVERSMISMATCH && trymntmode == ANY) { 793317d5933SIan Dowse clnt_destroy(clp); 794317d5933SIan Dowse trymntmode = V2; 795317d5933SIan Dowse goto tryagain; 796317d5933SIan Dowse } 797317d5933SIan Dowse clnt_geterr(clp, &rpcerr); 798317d5933SIan Dowse snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid, 799317d5933SIan Dowse hostp, spec, clnt_sperror(clp, "NFSPROC_NULL")); 800317d5933SIan Dowse clnt_destroy(clp); 8016575d184SEdward Tomasz Napierala return (returncode(clntstat, &rpcerr)); 802317d5933SIan Dowse } 803317d5933SIan Dowse clnt_destroy(clp); 804317d5933SIan Dowse 805011981fdSRick Macklem /* 806011981fdSRick Macklem * For NFSv4, there is no mount protocol. 807011981fdSRick Macklem */ 808011981fdSRick Macklem if (trymntmode == V4) { 809011981fdSRick Macklem /* 810011981fdSRick Macklem * Store the server address in nfsargsp, making 811011981fdSRick Macklem * sure to copy any locally allocated structures. 812011981fdSRick Macklem */ 813011981fdSRick Macklem addrlen = nfs_nb.len; 814011981fdSRick Macklem addr = malloc(addrlen); 815011981fdSRick Macklem if (addr == NULL) 816011981fdSRick Macklem err(1, "malloc"); 817011981fdSRick Macklem bcopy(nfs_nb.buf, addr, addrlen); 818011981fdSRick Macklem 819011981fdSRick Macklem build_iovec(iov, iovlen, "addr", addr, addrlen); 820011981fdSRick Macklem secname = sec_num_to_name(secflavor); 8216575d184SEdward Tomasz Napierala if (secname != NULL) { 8226575d184SEdward Tomasz Napierala build_iovec(iov, iovlen, "sec", 8236575d184SEdward Tomasz Napierala __DECONST(void *, secname), (size_t)-1); 8246575d184SEdward Tomasz Napierala } 825011981fdSRick Macklem build_iovec(iov, iovlen, "nfsv4", NULL, 0); 826011981fdSRick Macklem build_iovec(iov, iovlen, "dirpath", spec, (size_t)-1); 827011981fdSRick Macklem 828011981fdSRick Macklem return (TRYRET_SUCCESS); 829011981fdSRick Macklem } 830011981fdSRick Macklem 8310775314bSDoug Rabson /* Send the MOUNTPROC_MNT RPC to get the root filehandle. */ 832317d5933SIan Dowse try.tv_sec = 10; 833317d5933SIan Dowse try.tv_usec = 0; 8340775314bSDoug Rabson clp = clnt_tp_create(hostp, MOUNTPROG, mntvers, nconf_mnt); 835317d5933SIan Dowse if (clp == NULL) { 836ba33efd9SIan Dowse snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid_mnt, 837317d5933SIan Dowse hostp, spec, clnt_spcreateerror("RPCMNT: clnt_create")); 838317d5933SIan Dowse return (returncode(rpc_createerr.cf_stat, 839317d5933SIan Dowse &rpc_createerr.cf_error)); 840317d5933SIan Dowse } 841317d5933SIan Dowse clp->cl_auth = authsys_create_default(); 842a9148abdSDoug Rabson nfhret.auth = secflavor; 843317d5933SIan Dowse nfhret.vers = mntvers; 8446575d184SEdward Tomasz Napierala clntstat = clnt_call(clp, MOUNTPROC_MNT, (xdrproc_t)xdr_dir, spec, 845c04affffSMatthew N. Dodd (xdrproc_t)xdr_fh, &nfhret, 846317d5933SIan Dowse try); 847317d5933SIan Dowse auth_destroy(clp->cl_auth); 8486575d184SEdward Tomasz Napierala if (clntstat != RPC_SUCCESS) { 8496575d184SEdward Tomasz Napierala if (clntstat == RPC_PROGVERSMISMATCH && trymntmode == ANY) { 850317d5933SIan Dowse clnt_destroy(clp); 851317d5933SIan Dowse trymntmode = V2; 852317d5933SIan Dowse goto tryagain; 853317d5933SIan Dowse } 854317d5933SIan Dowse clnt_geterr(clp, &rpcerr); 855ba33efd9SIan Dowse snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid_mnt, 856317d5933SIan Dowse hostp, spec, clnt_sperror(clp, "RPCPROG_MNT")); 857317d5933SIan Dowse clnt_destroy(clp); 8586575d184SEdward Tomasz Napierala return (returncode(clntstat, &rpcerr)); 859317d5933SIan Dowse } 860317d5933SIan Dowse clnt_destroy(clp); 861317d5933SIan Dowse 862317d5933SIan Dowse if (nfhret.stat != 0) { 863ba33efd9SIan Dowse snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid_mnt, 864317d5933SIan Dowse hostp, spec, strerror(nfhret.stat)); 865317d5933SIan Dowse return (TRYRET_REMOTEERR); 866317d5933SIan Dowse } 867317d5933SIan Dowse 868317d5933SIan Dowse /* 869317d5933SIan Dowse * Store the filehandle and server address in nfsargsp, making 870317d5933SIan Dowse * sure to copy any locally allocated structures. 871317d5933SIan Dowse */ 8724b5bc283SCraig Rodrigues addrlen = nfs_nb.len; 8734b5bc283SCraig Rodrigues addr = malloc(addrlen); 8744b5bc283SCraig Rodrigues fhsize = nfhret.fhsize; 8754b5bc283SCraig Rodrigues fh = malloc(fhsize); 8764b5bc283SCraig Rodrigues if (addr == NULL || fh == NULL) 877317d5933SIan Dowse err(1, "malloc"); 8784b5bc283SCraig Rodrigues bcopy(nfs_nb.buf, addr, addrlen); 8794b5bc283SCraig Rodrigues bcopy(nfhret.nfh, fh, fhsize); 880317d5933SIan Dowse 8814b5bc283SCraig Rodrigues build_iovec(iov, iovlen, "addr", addr, addrlen); 8824b5bc283SCraig Rodrigues build_iovec(iov, iovlen, "fh", fh, fhsize); 883a9148abdSDoug Rabson secname = sec_num_to_name(nfhret.auth); 8846575d184SEdward Tomasz Napierala if (secname) { 8856575d184SEdward Tomasz Napierala build_iovec(iov, iovlen, "sec", 8866575d184SEdward Tomasz Napierala __DECONST(void *, secname), (size_t)-1); 8876575d184SEdward Tomasz Napierala } 888317d5933SIan Dowse if (nfsvers == 3) 8894b5bc283SCraig Rodrigues build_iovec(iov, iovlen, "nfsv3", NULL, 0); 890317d5933SIan Dowse 891317d5933SIan Dowse return (TRYRET_SUCCESS); 892317d5933SIan Dowse } 893317d5933SIan Dowse 8940c269d1fSCraig Rodrigues /* 895317d5933SIan Dowse * Catagorise a RPC return status and error into an `enum tryret' 896317d5933SIan Dowse * return code. 897317d5933SIan Dowse */ 898b352972bSXin LI static enum tryret 8996575d184SEdward Tomasz Napierala returncode(enum clnt_stat clntstat, struct rpc_err *rpcerr) 900317d5933SIan Dowse { 9016575d184SEdward Tomasz Napierala 9026575d184SEdward Tomasz Napierala switch (clntstat) { 903317d5933SIan Dowse case RPC_TIMEDOUT: 904317d5933SIan Dowse return (TRYRET_TIMEOUT); 905317d5933SIan Dowse case RPC_PMAPFAILURE: 906317d5933SIan Dowse case RPC_PROGNOTREGISTERED: 907317d5933SIan Dowse case RPC_PROGVERSMISMATCH: 908eca1c24eSIan Dowse /* XXX, these can be local or remote. */ 909eca1c24eSIan Dowse case RPC_CANTSEND: 910eca1c24eSIan Dowse case RPC_CANTRECV: 911317d5933SIan Dowse return (TRYRET_REMOTEERR); 912317d5933SIan Dowse case RPC_SYSTEMERROR: 913317d5933SIan Dowse switch (rpcerr->re_errno) { 914317d5933SIan Dowse case ETIMEDOUT: 915317d5933SIan Dowse return (TRYRET_TIMEOUT); 916317d5933SIan Dowse case ENOMEM: 917317d5933SIan Dowse break; 918317d5933SIan Dowse default: 919317d5933SIan Dowse return (TRYRET_REMOTEERR); 920317d5933SIan Dowse } 921317d5933SIan Dowse /* FALLTHROUGH */ 922317d5933SIan Dowse default: 923317d5933SIan Dowse break; 924317d5933SIan Dowse } 925317d5933SIan Dowse return (TRYRET_LOCALERR); 926317d5933SIan Dowse } 927317d5933SIan Dowse 928317d5933SIan Dowse /* 929a3d8f94bSIan Dowse * Look up a netid based on an address family and socket type. 930a3d8f94bSIan Dowse * `af' is the address family, and `sotype' is SOCK_DGRAM or SOCK_STREAM. 931a3d8f94bSIan Dowse * 932a3d8f94bSIan Dowse * XXX there should be a library function for this. 933a3d8f94bSIan Dowse */ 934b352972bSXin LI static const char * 93533924ad4SWarner Losh netidbytype(int af, int sotype) 93633924ad4SWarner Losh { 937a3d8f94bSIan Dowse struct nc_protos *p; 938a3d8f94bSIan Dowse 939a3d8f94bSIan Dowse for (p = nc_protos; p->netid != NULL; p++) { 940a3d8f94bSIan Dowse if (af != p->af || sotype != p->sotype) 941a3d8f94bSIan Dowse continue; 942a3d8f94bSIan Dowse return (p->netid); 943a3d8f94bSIan Dowse } 944a3d8f94bSIan Dowse return (NULL); 945a3d8f94bSIan Dowse } 946a3d8f94bSIan Dowse 947a3d8f94bSIan Dowse /* 948a3d8f94bSIan Dowse * Look up a netconfig entry based on a netid, and cache the result so 949a3d8f94bSIan Dowse * that we don't need to remember to call freenetconfigent(). 950a3d8f94bSIan Dowse * 951a3d8f94bSIan Dowse * Otherwise it behaves just like getnetconfigent(), so nc_*error() 952a3d8f94bSIan Dowse * work on failure. 953a3d8f94bSIan Dowse */ 954b352972bSXin LI static struct netconfig * 95533924ad4SWarner Losh getnetconf_cached(const char *netid) 95633924ad4SWarner Losh { 957a3d8f94bSIan Dowse static struct nc_entry { 958a3d8f94bSIan Dowse struct netconfig *nconf; 959a3d8f94bSIan Dowse struct nc_entry *next; 960a3d8f94bSIan Dowse } *head; 961a3d8f94bSIan Dowse struct nc_entry *p; 962a3d8f94bSIan Dowse struct netconfig *nconf; 963a3d8f94bSIan Dowse 964a3d8f94bSIan Dowse for (p = head; p != NULL; p = p->next) 965a3d8f94bSIan Dowse if (strcmp(netid, p->nconf->nc_netid) == 0) 966a3d8f94bSIan Dowse return (p->nconf); 967a3d8f94bSIan Dowse 968a3d8f94bSIan Dowse if ((nconf = getnetconfigent(netid)) == NULL) 969a3d8f94bSIan Dowse return (NULL); 970a3d8f94bSIan Dowse if ((p = malloc(sizeof(*p))) == NULL) 971a3d8f94bSIan Dowse err(1, "malloc"); 972a3d8f94bSIan Dowse p->nconf = nconf; 973a3d8f94bSIan Dowse p->next = head; 974a3d8f94bSIan Dowse head = p; 975a3d8f94bSIan Dowse 976a3d8f94bSIan Dowse return (p->nconf); 977a3d8f94bSIan Dowse } 978a3d8f94bSIan Dowse 979a3d8f94bSIan Dowse /* 9808fae3551SRodney W. Grimes * xdr routines for mount rpc's 9818fae3551SRodney W. Grimes */ 982b352972bSXin LI static int 98333924ad4SWarner Losh xdr_dir(XDR *xdrsp, char *dirp) 9848fae3551SRodney W. Grimes { 9850775314bSDoug Rabson return (xdr_string(xdrsp, &dirp, MNTPATHLEN)); 9868fae3551SRodney W. Grimes } 9878fae3551SRodney W. Grimes 988b352972bSXin LI static int 98933924ad4SWarner Losh xdr_fh(XDR *xdrsp, struct nfhret *np) 9908fae3551SRodney W. Grimes { 9913d438ad6SDavid E. O'Brien int i; 992a62dc406SDoug Rabson long auth, authcnt, authfnd = 0; 993a62dc406SDoug Rabson 994a62dc406SDoug Rabson if (!xdr_u_long(xdrsp, &np->stat)) 9958fae3551SRodney W. Grimes return (0); 9968fae3551SRodney W. Grimes if (np->stat) 9978fae3551SRodney W. Grimes return (1); 998a62dc406SDoug Rabson switch (np->vers) { 999a62dc406SDoug Rabson case 1: 10000775314bSDoug Rabson np->fhsize = NFS_FHSIZE; 10010775314bSDoug Rabson return (xdr_opaque(xdrsp, (caddr_t)np->nfh, NFS_FHSIZE)); 1002a62dc406SDoug Rabson case 3: 1003a62dc406SDoug Rabson if (!xdr_long(xdrsp, &np->fhsize)) 1004a62dc406SDoug Rabson return (0); 10050775314bSDoug Rabson if (np->fhsize <= 0 || np->fhsize > NFS3_FHSIZE) 1006a62dc406SDoug Rabson return (0); 1007a62dc406SDoug Rabson if (!xdr_opaque(xdrsp, (caddr_t)np->nfh, np->fhsize)) 1008a62dc406SDoug Rabson return (0); 1009a62dc406SDoug Rabson if (!xdr_long(xdrsp, &authcnt)) 1010a62dc406SDoug Rabson return (0); 1011a62dc406SDoug Rabson for (i = 0; i < authcnt; i++) { 1012a62dc406SDoug Rabson if (!xdr_long(xdrsp, &auth)) 1013a62dc406SDoug Rabson return (0); 10144b5bc283SCraig Rodrigues if (np->auth == -1) { 10154b5bc283SCraig Rodrigues np->auth = auth; 1016a62dc406SDoug Rabson authfnd++; 10174b5bc283SCraig Rodrigues } else if (auth == np->auth) { 10184b5bc283SCraig Rodrigues authfnd++; 10194b5bc283SCraig Rodrigues } 1020a62dc406SDoug Rabson } 1021a62dc406SDoug Rabson /* 1022a62dc406SDoug Rabson * Some servers, such as DEC's OSF/1 return a nil authenticator 1023a62dc406SDoug Rabson * list to indicate RPCAUTH_UNIX. 1024a62dc406SDoug Rabson */ 10254b5bc283SCraig Rodrigues if (authcnt == 0 && np->auth == -1) 10264b5bc283SCraig Rodrigues np->auth = AUTH_SYS; 10274b5bc283SCraig Rodrigues if (!authfnd && (authcnt > 0 || np->auth != AUTH_SYS)) 1028a62dc406SDoug Rabson np->stat = EAUTH; 1029a62dc406SDoug Rabson return (1); 103080c7cc1cSPedro F. Giffuni } 1031a62dc406SDoug Rabson return (0); 10328fae3551SRodney W. Grimes } 10338fae3551SRodney W. Grimes 1034b352972bSXin LI static void 1035b352972bSXin LI usage(void) 10368fae3551SRodney W. Grimes { 103746fc8f78SPhilippe Charnier (void)fprintf(stderr, "%s\n%s\n%s\n%s\n", 103886ce6a83SRobert Watson "usage: mount_nfs [-23bcdiLlNPsTU] [-a maxreadahead] [-D deadthresh]", 10398d646af5SRuslan Ermilov " [-g maxgroups] [-I readdirsize] [-o options] [-R retrycnt]", 10408d646af5SRuslan Ermilov " [-r readsize] [-t timeout] [-w writesize] [-x retrans]", 10418d646af5SRuslan Ermilov " rhost:path node"); 10428fae3551SRodney W. Grimes exit(1); 10438fae3551SRodney W. Grimes } 1044