2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static const char copyright[] =
36 "@(#) Copyright (c) 1992, 1993, 1994\n\
37 The Regents of the University of California. All rights reserved.\n";
41 static char sccsid[] = "@(#)mount_nfs.c 8.11 (Berkeley) 5/4/95";
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
47 #include <sys/param.h>
48 #include <sys/linker.h>
49 #include <sys/module.h>
50 #include <sys/mount.h>
51 #include <sys/socket.h>
53 #include <sys/syslog.h>
57 #include <rpc/pmap_clnt.h>
58 #include <rpc/pmap_prot.h>
59 #include <rpcsvc/nfs_prot.h>
60 #include <rpcsvc/mount.h>
62 #include <nfsclient/nfs.h>
64 #include <arpa/inet.h>
81 /* Table for af,sotype -> netid conversions. */
87 {"udp", AF_INET, SOCK_DGRAM},
88 {"tcp", AF_INET, SOCK_STREAM},
89 {"udp6", AF_INET6, SOCK_DGRAM},
90 {"tcp6", AF_INET6, SOCK_STREAM},
99 u_char nfh[NFS3_FHSIZE];
107 int nfsproto = IPPROTO_TCP;
110 char *portspec = NULL; /* Server nfs port; NULL means look up via rpcbind. */
111 struct sockaddr *addr;
116 int got_principal = 0;
125 /* Return codes for nfs_tryproto. */
128 TRYRET_TIMEOUT, /* No response received. */
129 TRYRET_REMOTEERR, /* Error received from remote server. */
130 TRYRET_LOCALERR /* Local failure. */
133 static int fallback_mount(struct iovec *iov, int iovlen, int mntflags);
134 static int sec_name_to_num(char *sec);
135 static char *sec_num_to_name(int num);
136 static int getnfsargs(char *, struct iovec **iov, int *iovlen);
137 /* void set_rpc_maxgrouplist(int); */
138 static struct netconfig *getnetconf_cached(const char *netid);
139 static const char *netidbytype(int af, int sotype);
140 static void usage(void) __dead2;
141 static int xdr_dir(XDR *, char *);
142 static int xdr_fh(XDR *, struct nfhret *);
143 static enum tryret nfs_tryproto(struct addrinfo *ai, char *hostp, char *spec,
144 char **errstr, struct iovec **iov, int *iovlen);
145 static enum tryret returncode(enum clnt_stat stat, struct rpc_err *rpcerr);
148 main(int argc, char *argv[])
152 int mntflags, num, iovlen;
154 char *name, *p, *spec, *fstype;
155 char mntpath[MAXPATHLEN], errmsg[255];
156 char hostname[MAXHOSTNAMELEN + 1], *gssname, gssn[MAXHOSTNAMELEN + 50];
161 memset(errmsg, 0, sizeof(errmsg));
164 fstype = strrchr(argv[0], '_');
166 errx(EX_USAGE, "argv[0] must end in _fstype");
170 while ((c = getopt(argc, argv,
171 "23a:bcdD:g:I:iLlNo:PR:r:sTt:w:x:U")) != -1)
180 printf("-a deprecated, use -o readahead=<value>\n");
181 build_iovec(&iov, &iovlen, "readahead", optarg, (size_t)-1);
187 printf("-c deprecated, use -o noconn\n");
188 build_iovec(&iov, &iovlen, "noconn", NULL, 0);
192 printf("-D deprecated, use -o deadthresh=<value>\n");
193 build_iovec(&iov, &iovlen, "deadthresh", optarg, (size_t)-1);
196 printf("-d deprecated, use -o dumbtimer");
197 build_iovec(&iov, &iovlen, "dumbtimer", NULL, 0);
200 printf("-g deprecated, use -o maxgroups");
201 num = strtol(optarg, &p, 10);
203 errx(1, "illegal -g value -- %s", optarg);
204 //set_rpc_maxgrouplist(num);
205 build_iovec(&iov, &iovlen, "maxgroups", optarg, (size_t)-1);
208 printf("-I deprecated, use -o readdirsize=<value>\n");
209 build_iovec(&iov, &iovlen, "readdirsize", optarg, (size_t)-1);
212 printf("-i deprecated, use -o intr\n");
213 build_iovec(&iov, &iovlen, "intr", NULL, 0);
216 printf("-L deprecated, use -o nolockd\n");
217 build_iovec(&iov, &iovlen, "nolockd", NULL, 0);
220 printf("-l deprecated, -o rdirplus\n");
221 build_iovec(&iov, &iovlen, "rdirplus", NULL, 0);
224 printf("-N deprecated, do not specify -o resvport\n");
227 int pass_flag_to_nmount;
231 char *pnextopt = NULL;
233 pass_flag_to_nmount = 1;
234 pnextopt = strchr(opt, ',');
235 if (pnextopt != NULL) {
239 pval = strchr(opt, '=');
244 if (strcmp(opt, "bg") == 0) {
246 pass_flag_to_nmount=0;
247 } else if (strcmp(opt, "fg") == 0) {
248 /* same as not specifying -o bg */
249 pass_flag_to_nmount=0;
250 } else if (strcmp(opt, "gssname") == 0) {
251 pass_flag_to_nmount = 0;
253 } else if (strcmp(opt, "mntudp") == 0) {
255 nfsproto = IPPROTO_UDP;
256 } else if (strcmp(opt, "udp") == 0) {
257 nfsproto = IPPROTO_UDP;
258 } else if (strcmp(opt, "tcp") == 0) {
259 nfsproto = IPPROTO_TCP;
260 } else if (strcmp(opt, "noinet4") == 0) {
261 pass_flag_to_nmount=0;
262 opflags |= OF_NOINET4;
263 } else if (strcmp(opt, "noinet6") == 0) {
264 pass_flag_to_nmount=0;
265 opflags |= OF_NOINET6;
266 } else if (strcmp(opt, "noconn") == 0) {
268 } else if (strcmp(opt, "nfsv2") == 0) {
269 pass_flag_to_nmount=0;
271 } else if (strcmp(opt, "nfsv3") == 0) {
273 } else if (strcmp(opt, "nfsv4") == 0) {
274 pass_flag_to_nmount=0;
277 nfsproto = IPPROTO_TCP;
278 if (portspec == NULL)
280 } else if (strcmp(opt, "port") == 0) {
281 pass_flag_to_nmount=0;
282 asprintf(&portspec, "%d",
284 if (portspec == NULL)
286 } else if (strcmp(opt, "principal") == 0) {
288 } else if (strcmp(opt, "sec") == 0) {
290 * Don't add this option to
291 * the iovec yet - we will
292 * negotiate which sec flavor
293 * to use with the remote
296 pass_flag_to_nmount=0;
297 secflavor = sec_name_to_num(val);
300 "illegal sec value -- %s",
303 } else if (strcmp(opt, "retrycnt") == 0) {
304 pass_flag_to_nmount=0;
305 num = strtol(val, &p, 10);
307 errx(1, "illegal retrycnt value -- %s", val);
309 } else if (strcmp(opt, "maxgroups") == 0) {
310 num = strtol(val, &p, 10);
312 errx(1, "illegal maxgroups value -- %s", val);
313 //set_rpc_maxgrouplist(num);
315 if (pass_flag_to_nmount)
316 build_iovec(&iov, &iovlen, opt, val,
323 /* obsolete for -o noresvport now default */
324 printf("-P deprecated, use -o noresvport\n");
325 build_iovec(&iov, &iovlen, "noresvport", NULL, 0);
328 printf("-R deprecated, use -o retrycnt=<retrycnt>\n");
329 num = strtol(optarg, &p, 10);
331 errx(1, "illegal -R value -- %s", optarg);
335 printf("-r deprecated, use -o rsize=<rsize>\n");
336 build_iovec(&iov, &iovlen, "rsize", optarg, (size_t)-1);
339 printf("-s deprecated, use -o soft\n");
340 build_iovec(&iov, &iovlen, "soft", NULL, 0);
343 nfsproto = IPPROTO_TCP;
344 printf("-T deprecated, use -o tcp\n");
347 printf("-t deprecated, use -o timeout=<value>\n");
348 build_iovec(&iov, &iovlen, "timeout", optarg, (size_t)-1);
351 printf("-w deprecated, use -o wsize=<value>\n");
352 build_iovec(&iov, &iovlen, "wsize", optarg, (size_t)-1);
355 printf("-x deprecated, use -o retrans=<value>\n");
356 build_iovec(&iov, &iovlen, "retrans", optarg, (size_t)-1);
359 printf("-U deprecated, use -o mntudp\n");
361 nfsproto = IPPROTO_UDP;
362 build_iovec(&iov, &iovlen, "mntudp", NULL, 0);
380 /* The default is to keep retrying forever. */
384 * If the experimental nfs subsystem is loaded into the kernel
385 * and the regular one is not, use it. Otherwise, use it if the
386 * fstype is set to "newnfs", either via "mount -t newnfs ..."
387 * or by specifying an nfsv4 mount.
389 if (modfind("nfscl") >= 0 && modfind("nfs") < 0) {
391 } else if (strcmp(fstype, "newnfs") == 0) {
392 if (modfind("nfscl") < 0) {
393 /* Not present in kernel, try loading it */
394 if (kldload("nfscl") < 0 ||
395 modfind("nfscl") < 0)
396 errx(1, "nfscl is not available");
401 * Add the fqdn to the gssname, as required.
403 if (gssname != NULL) {
404 if (strchr(gssname, '@') == NULL &&
405 gethostname(hostname, MAXHOSTNAMELEN) == 0) {
406 snprintf(gssn, sizeof (gssn), "%s@%s", gssname,
410 build_iovec(&iov, &iovlen, "gssname", gssname,
411 strlen(gssname) + 1);
414 if (!getnfsargs(spec, &iov, &iovlen))
417 /* resolve the mountpoint with realpath(3) */
418 (void)checkpath(name, mntpath);
420 build_iovec(&iov, &iovlen, "fstype", fstype, (size_t)-1);
421 build_iovec(&iov, &iovlen, "fspath", mntpath, (size_t)-1);
422 build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
426 * Backwards compatibility routines for older kernels.
427 * Remove this and fallback_mount() code when we do not need to support
428 * NFS mounts against older kernels which still need
429 * struct nfs_args to be passed in via nmount().
431 osversion = getosreldate();
432 if (osversion >= 702100) {
433 if (nmount(iov, iovlen, mntflags))
434 err(1, "%s, %s", mntpath, errmsg);
436 if (fallback_mount(iov, iovlen, mntflags))
437 err(1, "%s, %s", mntpath, errmsg);
444 findopt(struct iovec *iov, int iovlen, const char *name,
445 char **valuep, int *lenp)
449 for (i = 0; i < iovlen/2; i++, iov += 2) {
450 if (strcmp(name, iov[0].iov_base) == 0) {
452 *valuep = iov[1].iov_base;
454 *lenp = iov[1].iov_len;
462 copyopt(struct iovec **newiov, int *newiovlen,
463 struct iovec *iov, int iovlen, const char *name)
468 if (findopt(iov, iovlen, name, &value, &len) == 0)
469 build_iovec(newiov, newiovlen, name, value, len);
473 * XXX: This function is provided for backwards
474 * compatibility with older kernels which did not support
475 * passing NFS mount options to nmount() as individual
476 * parameters. It should be eventually be removed.
479 fallback_mount(struct iovec *iov, int iovlen, int mntflags)
481 struct nfs_args args = {
482 .version = NFS_ARGSVERSION,
484 .addrlen = sizeof (struct sockaddr_in),
485 .sotype = SOCK_STREAM,
489 .flags = NFSMNT_RESVPORT,
492 .readdirsize = NFS_READDIRSIZE,
494 .retrans = NFS_RETRANS,
495 .maxgrouplist = NFS_MAXGRPS,
496 .readahead = NFS_DEFRAHEAD,
497 .wcommitsize = 0, /* was: NQ_DEFLEASE */
498 .deadthresh = NFS_MAXDEADTHRESH, /* was: NQ_DEADTHRESH */
501 .acregmin = NFS_MINATTRTIMO,
502 .acregmax = NFS_MAXATTRTIMO,
503 .acdirmin = NFS_MINDIRATTRTIMO,
504 .acdirmax = NFS_MAXDIRATTRTIMO,
508 struct iovec *newiov;
511 if (findopt(iov, iovlen, "dumbtimer", NULL, NULL) == 0)
512 args.flags |= NFSMNT_DUMBTIMR;
513 if (findopt(iov, iovlen, "noconn", NULL, NULL) == 0)
514 args.flags |= NFSMNT_NOCONN;
515 if (findopt(iov, iovlen, "conn", NULL, NULL) == 0)
516 args.flags |= NFSMNT_NOCONN;
517 if (findopt(iov, iovlen, "nolockd", NULL, NULL) == 0)
518 args.flags |= NFSMNT_NOLOCKD;
519 if (findopt(iov, iovlen, "lockd", NULL, NULL) == 0)
520 args.flags &= ~NFSMNT_NOLOCKD;
521 if (findopt(iov, iovlen, "intr", NULL, NULL) == 0)
522 args.flags |= NFSMNT_INT;
523 if (findopt(iov, iovlen, "rdirplus", NULL, NULL) == 0)
524 args.flags |= NFSMNT_RDIRPLUS;
525 if (findopt(iov, iovlen, "resvport", NULL, NULL) == 0)
526 args.flags |= NFSMNT_RESVPORT;
527 if (findopt(iov, iovlen, "noresvport", NULL, NULL) == 0)
528 args.flags &= ~NFSMNT_RESVPORT;
529 if (findopt(iov, iovlen, "soft", NULL, NULL) == 0)
530 args.flags |= NFSMNT_SOFT;
531 if (findopt(iov, iovlen, "hard", NULL, NULL) == 0)
532 args.flags &= ~NFSMNT_SOFT;
533 if (findopt(iov, iovlen, "mntudp", NULL, NULL) == 0)
534 args.sotype = SOCK_DGRAM;
535 if (findopt(iov, iovlen, "udp", NULL, NULL) == 0)
536 args.sotype = SOCK_DGRAM;
537 if (findopt(iov, iovlen, "tcp", NULL, NULL) == 0)
538 args.sotype = SOCK_STREAM;
539 if (findopt(iov, iovlen, "nfsv3", NULL, NULL) == 0)
540 args.flags |= NFSMNT_NFSV3;
541 if (findopt(iov, iovlen, "readdirsize", &opt, NULL) == 0) {
543 errx(1, "illegal readdirsize");
545 ret = sscanf(opt, "%d", &args.readdirsize);
546 if (ret != 1 || args.readdirsize <= 0) {
547 errx(1, "illegal readdirsize: %s", opt);
549 args.flags |= NFSMNT_READDIRSIZE;
551 if (findopt(iov, iovlen, "readahead", &opt, NULL) == 0) {
553 errx(1, "illegal readahead");
555 ret = sscanf(opt, "%d", &args.readahead);
556 if (ret != 1 || args.readahead <= 0) {
557 errx(1, "illegal readahead: %s", opt);
559 args.flags |= NFSMNT_READAHEAD;
561 if (findopt(iov, iovlen, "wsize", &opt, NULL) == 0) {
563 errx(1, "illegal wsize");
565 ret = sscanf(opt, "%d", &args.wsize);
566 if (ret != 1 || args.wsize <= 0) {
567 errx(1, "illegal wsize: %s", opt);
569 args.flags |= NFSMNT_WSIZE;
571 if (findopt(iov, iovlen, "rsize", &opt, NULL) == 0) {
573 errx(1, "illegal rsize");
575 ret = sscanf(opt, "%d", &args.rsize);
576 if (ret != 1 || args.rsize <= 0) {
577 errx(1, "illegal wsize: %s", opt);
579 args.flags |= NFSMNT_RSIZE;
581 if (findopt(iov, iovlen, "retrans", &opt, NULL) == 0) {
583 errx(1, "illegal retrans");
585 ret = sscanf(opt, "%d", &args.retrans);
586 if (ret != 1 || args.retrans <= 0) {
587 errx(1, "illegal retrans: %s", opt);
589 args.flags |= NFSMNT_RETRANS;
591 if (findopt(iov, iovlen, "acregmin", &opt, NULL) == 0) {
592 ret = sscanf(opt, "%d", &args.acregmin);
593 if (ret != 1 || args.acregmin < 0) {
594 errx(1, "illegal acregmin: %s", opt);
596 args.flags |= NFSMNT_ACREGMIN;
598 if (findopt(iov, iovlen, "acregmax", &opt, NULL) == 0) {
599 ret = sscanf(opt, "%d", &args.acregmax);
600 if (ret != 1 || args.acregmax < 0) {
601 errx(1, "illegal acregmax: %s", opt);
603 args.flags |= NFSMNT_ACREGMAX;
605 if (findopt(iov, iovlen, "acdirmin", &opt, NULL) == 0) {
606 ret = sscanf(opt, "%d", &args.acdirmin);
607 if (ret != 1 || args.acdirmin < 0) {
608 errx(1, "illegal acdirmin: %s", opt);
610 args.flags |= NFSMNT_ACDIRMIN;
612 if (findopt(iov, iovlen, "acdirmax", &opt, NULL) == 0) {
613 ret = sscanf(opt, "%d", &args.acdirmax);
614 if (ret != 1 || args.acdirmax < 0) {
615 errx(1, "illegal acdirmax: %s", opt);
617 args.flags |= NFSMNT_ACDIRMAX;
619 if (findopt(iov, iovlen, "wcommitsize", &opt, NULL) == 0) {
620 ret = sscanf(opt, "%d", &args.wcommitsize);
621 if (ret != 1 || args.wcommitsize < 0) {
622 errx(1, "illegal wcommitsize: %s", opt);
624 args.flags |= NFSMNT_WCOMMITSIZE;
626 if (findopt(iov, iovlen, "deadthresh", &opt, NULL) == 0) {
627 ret = sscanf(opt, "%d", &args.deadthresh);
628 if (ret != 1 || args.deadthresh <= 0) {
629 errx(1, "illegal deadthresh: %s", opt);
631 args.flags |= NFSMNT_DEADTHRESH;
633 if (findopt(iov, iovlen, "timeout", &opt, NULL) == 0) {
634 ret = sscanf(opt, "%d", &args.timeo);
635 if (ret != 1 || args.timeo <= 0) {
636 errx(1, "illegal timeout: %s", opt);
638 args.flags |= NFSMNT_TIMEO;
640 if (findopt(iov, iovlen, "maxgroups", &opt, NULL) == 0) {
641 ret = sscanf(opt, "%d", &args.maxgrouplist);
642 if (ret != 1 || args.timeo <= 0) {
643 errx(1, "illegal maxgroups: %s", opt);
645 args.flags |= NFSMNT_MAXGRPS;
647 if (findopt(iov, iovlen, "addr", &opt,
648 &args.addrlen) == 0) {
649 args.addr = (struct sockaddr *) opt;
651 if (findopt(iov, iovlen, "fh", &opt, &args.fhsize) == 0) {
654 if (findopt(iov, iovlen, "hostname", &args.hostname,
657 if (args.hostname == NULL) {
658 errx(1, "Invalid hostname");
664 build_iovec(&newiov, &newiovlen, "nfs_args", &args, sizeof(args));
665 copyopt(&newiov, &newiovlen, iov, iovlen, "fstype");
666 copyopt(&newiov, &newiovlen, iov, iovlen, "fspath");
667 copyopt(&newiov, &newiovlen, iov, iovlen, "errmsg");
669 return nmount(newiov, newiovlen, mntflags);
673 sec_name_to_num(char *sec)
675 if (!strcmp(sec, "krb5"))
676 return (RPCSEC_GSS_KRB5);
677 if (!strcmp(sec, "krb5i"))
678 return (RPCSEC_GSS_KRB5I);
679 if (!strcmp(sec, "krb5p"))
680 return (RPCSEC_GSS_KRB5P);
681 if (!strcmp(sec, "sys"))
687 sec_num_to_name(int flavor)
690 case RPCSEC_GSS_KRB5:
692 case RPCSEC_GSS_KRB5I:
694 case RPCSEC_GSS_KRB5P:
703 getnfsargs(char *spec, struct iovec **iov, int *iovlen)
705 struct addrinfo hints, *ai_nfs, *ai;
707 int ecode, speclen, remoteerr, offset, have_bracket = 0;
708 char *hostp, *delimp, *errstr;
710 static char nam[MNAMELEN + 1], pname[MAXHOSTNAMELEN + 5];
712 if (*spec == '[' && (delimp = strchr(spec + 1, ']')) != NULL &&
713 *(delimp + 1) == ':') {
717 } else if ((delimp = strrchr(spec, ':')) != NULL) {
720 } else if ((delimp = strrchr(spec, '@')) != NULL) {
721 warnx("path@server syntax is deprecated, use server:path");
724 warnx("no <host>:<dirpath> nfs-name");
730 * If there has been a trailing slash at mounttime it seems
731 * that some mountd implementations fail to remove the mount
732 * entries from their mountlist while unmounting.
734 for (speclen = strlen(spec);
735 speclen > 1 && spec[speclen - 1] == '/';
737 spec[speclen - 1] = '\0';
738 if (strlen(hostp) + strlen(spec) + 1 > MNAMELEN) {
739 warnx("%s:%s: %s", hostp, spec, strerror(ENAMETOOLONG));
742 /* Make both '@' and ':' notations equal */
743 if (*hostp != '\0') {
748 memmove(nam + offset, hostp, len);
750 nam[len + offset++] = ']';
751 nam[len + offset++] = ':';
752 memmove(nam + len + offset, spec, speclen);
753 nam[len + speclen + offset] = '\0';
757 * Handle an internet host address.
759 memset(&hints, 0, sizeof hints);
760 hints.ai_flags = AI_NUMERICHOST;
761 if (nfsproto == IPPROTO_TCP)
762 hints.ai_socktype = SOCK_STREAM;
763 else if (nfsproto == IPPROTO_UDP)
764 hints.ai_socktype = SOCK_DGRAM;
766 if (getaddrinfo(hostp, portspec, &hints, &ai_nfs) != 0) {
767 hints.ai_flags = AI_CANONNAME;
768 if ((ecode = getaddrinfo(hostp, portspec, &hints, &ai_nfs))
770 if (portspec == NULL)
771 errx(1, "%s: %s", hostp, gai_strerror(ecode));
773 errx(1, "%s:%s: %s", hostp, portspec,
774 gai_strerror(ecode));
779 * For a Kerberized nfs mount where the "principal"
780 * argument has not been set, add it here.
782 if (got_principal == 0 && secflavor >= 0 &&
783 secflavor != AUTH_SYS && ai_nfs->ai_canonname != NULL) {
784 snprintf(pname, sizeof (pname), "nfs@%s",
785 ai_nfs->ai_canonname);
786 build_iovec(iov, iovlen, "principal", pname,
791 ret = TRYRET_LOCALERR;
794 * Try each entry returned by getaddrinfo(). Note the
795 * occurence of remote errors by setting `remoteerr'.
798 for (ai = ai_nfs; ai != NULL; ai = ai->ai_next) {
799 if ((ai->ai_family == AF_INET6) &&
800 (opflags & OF_NOINET6))
802 if ((ai->ai_family == AF_INET) &&
803 (opflags & OF_NOINET4))
805 ret = nfs_tryproto(ai, hostp, spec, &errstr, iov,
807 if (ret == TRYRET_SUCCESS)
809 if (ret != TRYRET_LOCALERR)
811 if ((opflags & ISBGRND) == 0)
812 fprintf(stderr, "%s\n", errstr);
814 if (ret == TRYRET_SUCCESS)
817 /* Exit if all errors were local. */
822 * If retrycnt == 0, we are to keep retrying forever.
823 * Otherwise decrement it, and exit if it hits zero.
825 if (retrycnt != 0 && --retrycnt == 0)
828 if ((opflags & (BGRND | ISBGRND)) == BGRND) {
829 warnx("Cannot immediately mount %s:%s, backgrounding",
832 if (daemon(0, 0) != 0)
837 freeaddrinfo(ai_nfs);
839 build_iovec(iov, iovlen, "hostname", nam, (size_t)-1);
840 /* Add mounted file system to PATH_MOUNTTAB */
841 if (!add_mtab(hostp, spec))
842 warnx("can't update %s for %s:%s", PATH_MOUNTTAB, hostp, spec);
847 * Try to set up the NFS arguments according to the address
848 * family, protocol (and possibly port) specified in `ai'.
850 * Returns TRYRET_SUCCESS if successful, or:
851 * TRYRET_TIMEOUT The server did not respond.
852 * TRYRET_REMOTEERR The server reported an error.
853 * TRYRET_LOCALERR Local failure.
855 * In all error cases, *errstr will be set to a statically-allocated string
856 * describing the error.
859 nfs_tryproto(struct addrinfo *ai, char *hostp, char *spec, char **errstr,
860 struct iovec **iov, int *iovlen)
862 static char errbuf[256];
863 struct sockaddr_storage nfs_ss;
864 struct netbuf nfs_nb;
865 struct nfhret nfhret;
867 struct rpc_err rpcerr;
869 struct netconfig *nconf, *nconf_mnt;
870 const char *netid, *netid_mnt;
872 int doconnect, nfsvers, mntvers, sotype;
874 enum mountmode trymntmode;
876 trymntmode = mountmode;
880 if (nfsproto == IPPROTO_TCP)
881 sotype = SOCK_STREAM;
882 else if (nfsproto == IPPROTO_UDP)
885 if ((netid = netidbytype(ai->ai_family, sotype)) == NULL) {
886 snprintf(errbuf, sizeof errbuf,
887 "af %d sotype %d not supported", ai->ai_family, sotype);
888 return (TRYRET_LOCALERR);
890 if ((nconf = getnetconf_cached(netid)) == NULL) {
891 snprintf(errbuf, sizeof errbuf, "%s: %s", netid, nc_sperror());
892 return (TRYRET_LOCALERR);
894 /* The RPCPROG_MNT netid may be different. */
899 if ((netid_mnt = netidbytype(ai->ai_family, SOCK_DGRAM))
901 snprintf(errbuf, sizeof errbuf,
902 "af %d sotype SOCK_DGRAM not supported",
904 return (TRYRET_LOCALERR);
906 if ((nconf_mnt = getnetconf_cached(netid_mnt)) == NULL) {
907 snprintf(errbuf, sizeof errbuf, "%s: %s", netid_mnt,
909 return (TRYRET_LOCALERR);
914 if (trymntmode == V4) {
916 } else if (trymntmode == V2) {
924 if (portspec != NULL) {
925 /* `ai' contains the complete nfsd sockaddr. */
926 nfs_nb.buf = ai->ai_addr;
927 nfs_nb.len = nfs_nb.maxlen = ai->ai_addrlen;
929 /* Ask the remote rpcbind. */
930 nfs_nb.buf = &nfs_ss;
931 nfs_nb.len = nfs_nb.maxlen = sizeof nfs_ss;
933 if (!rpcb_getaddr(NFS_PROGRAM, nfsvers, nconf, &nfs_nb,
935 if (rpc_createerr.cf_stat == RPC_PROGVERSMISMATCH &&
940 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s",
942 clnt_spcreateerror("RPCPROG_NFS"));
943 return (returncode(rpc_createerr.cf_stat,
944 &rpc_createerr.cf_error));
948 /* Check that the server (nfsd) responds on the port we have chosen. */
949 clp = clnt_tli_create(RPC_ANYFD, nconf, &nfs_nb, NFS_PROGRAM, nfsvers,
952 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid,
953 hostp, spec, clnt_spcreateerror("nfsd: RPCPROG_NFS"));
954 return (returncode(rpc_createerr.cf_stat,
955 &rpc_createerr.cf_error));
957 if (sotype == SOCK_DGRAM && noconn == 0) {
959 * Use connect(), to match what the kernel does. This
960 * catches cases where the server responds from the
961 * wrong source address.
964 if (!clnt_control(clp, CLSET_CONNECT, (char *)&doconnect)) {
966 snprintf(errbuf, sizeof errbuf,
967 "[%s] %s:%s: CLSET_CONNECT failed", netid, hostp,
969 return (TRYRET_LOCALERR);
975 stat = clnt_call(clp, NFSPROC_NULL, (xdrproc_t)xdr_void, NULL,
976 (xdrproc_t)xdr_void, NULL, try);
977 if (stat != RPC_SUCCESS) {
978 if (stat == RPC_PROGVERSMISMATCH && trymntmode == ANY) {
983 clnt_geterr(clp, &rpcerr);
984 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid,
985 hostp, spec, clnt_sperror(clp, "NFSPROC_NULL"));
987 return (returncode(stat, &rpcerr));
992 * For NFSv4, there is no mount protocol.
994 if (trymntmode == V4) {
996 * Store the server address in nfsargsp, making
997 * sure to copy any locally allocated structures.
999 addrlen = nfs_nb.len;
1000 addr = malloc(addrlen);
1003 bcopy(nfs_nb.buf, addr, addrlen);
1005 build_iovec(iov, iovlen, "addr", addr, addrlen);
1006 secname = sec_num_to_name(secflavor);
1007 if (secname != NULL)
1008 build_iovec(iov, iovlen, "sec", secname, (size_t)-1);
1009 build_iovec(iov, iovlen, "nfsv4", NULL, 0);
1010 build_iovec(iov, iovlen, "dirpath", spec, (size_t)-1);
1012 return (TRYRET_SUCCESS);
1015 /* Send the MOUNTPROC_MNT RPC to get the root filehandle. */
1018 clp = clnt_tp_create(hostp, MOUNTPROG, mntvers, nconf_mnt);
1020 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid_mnt,
1021 hostp, spec, clnt_spcreateerror("RPCMNT: clnt_create"));
1022 return (returncode(rpc_createerr.cf_stat,
1023 &rpc_createerr.cf_error));
1025 clp->cl_auth = authsys_create_default();
1026 nfhret.auth = secflavor;
1027 nfhret.vers = mntvers;
1028 stat = clnt_call(clp, MOUNTPROC_MNT, (xdrproc_t)xdr_dir, spec,
1029 (xdrproc_t)xdr_fh, &nfhret,
1031 auth_destroy(clp->cl_auth);
1032 if (stat != RPC_SUCCESS) {
1033 if (stat == RPC_PROGVERSMISMATCH && trymntmode == ANY) {
1038 clnt_geterr(clp, &rpcerr);
1039 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid_mnt,
1040 hostp, spec, clnt_sperror(clp, "RPCPROG_MNT"));
1042 return (returncode(stat, &rpcerr));
1046 if (nfhret.stat != 0) {
1047 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid_mnt,
1048 hostp, spec, strerror(nfhret.stat));
1049 return (TRYRET_REMOTEERR);
1053 * Store the filehandle and server address in nfsargsp, making
1054 * sure to copy any locally allocated structures.
1056 addrlen = nfs_nb.len;
1057 addr = malloc(addrlen);
1058 fhsize = nfhret.fhsize;
1059 fh = malloc(fhsize);
1060 if (addr == NULL || fh == NULL)
1062 bcopy(nfs_nb.buf, addr, addrlen);
1063 bcopy(nfhret.nfh, fh, fhsize);
1065 build_iovec(iov, iovlen, "addr", addr, addrlen);
1066 build_iovec(iov, iovlen, "fh", fh, fhsize);
1067 secname = sec_num_to_name(nfhret.auth);
1069 build_iovec(iov, iovlen, "sec", secname, (size_t)-1);
1071 build_iovec(iov, iovlen, "nfsv3", NULL, 0);
1073 return (TRYRET_SUCCESS);
1077 * Catagorise a RPC return status and error into an `enum tryret'
1081 returncode(enum clnt_stat stat, struct rpc_err *rpcerr)
1085 return (TRYRET_TIMEOUT);
1086 case RPC_PMAPFAILURE:
1087 case RPC_PROGNOTREGISTERED:
1088 case RPC_PROGVERSMISMATCH:
1089 /* XXX, these can be local or remote. */
1092 return (TRYRET_REMOTEERR);
1093 case RPC_SYSTEMERROR:
1094 switch (rpcerr->re_errno) {
1096 return (TRYRET_TIMEOUT);
1100 return (TRYRET_REMOTEERR);
1106 return (TRYRET_LOCALERR);
1110 * Look up a netid based on an address family and socket type.
1111 * `af' is the address family, and `sotype' is SOCK_DGRAM or SOCK_STREAM.
1113 * XXX there should be a library function for this.
1116 netidbytype(int af, int sotype)
1118 struct nc_protos *p;
1120 for (p = nc_protos; p->netid != NULL; p++) {
1121 if (af != p->af || sotype != p->sotype)
1129 * Look up a netconfig entry based on a netid, and cache the result so
1130 * that we don't need to remember to call freenetconfigent().
1132 * Otherwise it behaves just like getnetconfigent(), so nc_*error()
1135 static struct netconfig *
1136 getnetconf_cached(const char *netid)
1138 static struct nc_entry {
1139 struct netconfig *nconf;
1140 struct nc_entry *next;
1143 struct netconfig *nconf;
1145 for (p = head; p != NULL; p = p->next)
1146 if (strcmp(netid, p->nconf->nc_netid) == 0)
1149 if ((nconf = getnetconfigent(netid)) == NULL)
1151 if ((p = malloc(sizeof(*p))) == NULL)
1161 * xdr routines for mount rpc's
1164 xdr_dir(XDR *xdrsp, char *dirp)
1166 return (xdr_string(xdrsp, &dirp, MNTPATHLEN));
1170 xdr_fh(XDR *xdrsp, struct nfhret *np)
1173 long auth, authcnt, authfnd = 0;
1175 if (!xdr_u_long(xdrsp, &np->stat))
1181 np->fhsize = NFS_FHSIZE;
1182 return (xdr_opaque(xdrsp, (caddr_t)np->nfh, NFS_FHSIZE));
1184 if (!xdr_long(xdrsp, &np->fhsize))
1186 if (np->fhsize <= 0 || np->fhsize > NFS3_FHSIZE)
1188 if (!xdr_opaque(xdrsp, (caddr_t)np->nfh, np->fhsize))
1190 if (!xdr_long(xdrsp, &authcnt))
1192 for (i = 0; i < authcnt; i++) {
1193 if (!xdr_long(xdrsp, &auth))
1195 if (np->auth == -1) {
1198 } else if (auth == np->auth) {
1203 * Some servers, such as DEC's OSF/1 return a nil authenticator
1204 * list to indicate RPCAUTH_UNIX.
1206 if (authcnt == 0 && np->auth == -1)
1207 np->auth = AUTH_SYS;
1208 if (!authfnd && (authcnt > 0 || np->auth != AUTH_SYS))
1218 (void)fprintf(stderr, "%s\n%s\n%s\n%s\n",
1219 "usage: mount_nfs [-23bcdiLlNPsTU] [-a maxreadahead] [-D deadthresh]",
1220 " [-g maxgroups] [-I readdirsize] [-o options] [-R retrycnt]",
1221 " [-r readsize] [-t timeout] [-w writesize] [-x retrans]",
1222 " rhost:path node");