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 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#) Copyright (c) 1992, 1993, 1994 The Regents of the University of California. All rights reserved.
37 * @(#)mount_nfs.c 8.11 (Berkeley) 5/4/95
38 * $FreeBSD: src/sbin/mount_nfs/mount_nfs.c,v 1.36.2.6 2003/05/13 14:45:40 trhodes Exp $
39 * $DragonFly: src/sbin/mount_nfs/mount_nfs.c,v 1.14 2008/07/14 22:22:40 dillon Exp $
42 #include <sys/param.h>
43 #include <sys/mount.h>
44 #include <sys/socket.h>
46 #include <sys/syslog.h>
49 #include <rpc/pmap_clnt.h>
50 #include <rpc/pmap_prot.h>
52 #include <vfs/nfs/rpcv2.h>
53 #include <vfs/nfs/nfsproto.h>
54 #include <vfs/nfs/nfs.h>
56 #include <arpa/inet.h>
73 #define ALTF_NOCONN 0x2
74 #define ALTF_DUMBTIMR 0x4
76 #define ALTF_KERB 0x10
77 #define ALTF_NFSV3 0x20
78 #define ALTF_RDIRPLUS 0x40
79 #define ALTF_CACHE 0x80
80 #define ALTF_RESVPORT 0x100
81 #define ALTF_SEQPACKET 0x200
82 #define ALTF_UNUSED400 0x400
83 #define ALTF_SOFT 0x800
84 #define ALTF_TCP 0x1000
85 #define ALTF_PORT 0x2000
86 #define ALTF_NFSV2 0x4000
87 #define ALTF_ACREGMIN 0x8000
88 #define ALTF_ACREGMAX 0x10000
89 #define ALTF_ACDIRMIN 0x20000
90 #define ALTF_ACDIRMAX 0x40000
92 struct mntopt mopts[] = {
97 { "bg", 0, ALTF_BG, 1 },
98 { "conn", 1, ALTF_NOCONN, 1 },
99 { "dumbtimer", 0, ALTF_DUMBTIMR, 1 },
100 { "intr", 0, ALTF_INTR, 1 },
102 { "kerb", 0, ALTF_KERB, 1 },
104 { "nfsv3", 0, ALTF_NFSV3, 1 },
105 { "rdirplus", 0, ALTF_RDIRPLUS, 1 },
106 { "mntudp", 1, ALTF_TCP, 1 },
107 { "resvport", 0, ALTF_RESVPORT, 1 },
108 { "soft", 0, ALTF_SOFT, 1 },
109 { "tcp", 0, ALTF_TCP, 1 },
110 { "udp", 1, ALTF_TCP, 1 },
111 { "port=", 0, ALTF_PORT, 1 },
112 { "nfsv2", 0, ALTF_NFSV2, 1 },
113 { "acregmin=", 0, ALTF_ACREGMIN, 1 },
114 { "acregmax=", 0, ALTF_ACREGMAX, 1 },
115 { "acdirmin=", 0, ALTF_ACDIRMIN, 1 },
116 { "acdirmax=", 0, ALTF_ACDIRMAX, 1 },
117 { "cache", 0, ALTF_CACHE, 1 },
121 struct nfs_args nfsdefargs = {
124 sizeof (struct sockaddr_in),
147 /* Table for af,sotype -> netid conversions. */
153 {"udp", AF_INET, SOCK_DGRAM},
154 {"tcp", AF_INET, SOCK_STREAM},
155 {"udp6", AF_INET6, SOCK_DGRAM},
156 {"tcp6", AF_INET6, SOCK_STREAM},
165 u_char nfh[NFSX_V3FHMAX];
172 char *portspec = NULL; /* Server nfs port; NULL means look up via rpcbind. */
181 char realm[REALM_SZ];
186 struct nfsrpc_nickverf kverf;
187 struct nfsrpc_fullblock kin, kout;
191 NFSKERBKEYSCHED_T kerb_keysched;
194 /* Return codes for nfs_tryproto. */
197 TRYRET_TIMEOUT, /* No response received. */
198 TRYRET_REMOTEERR, /* Error received from remote server. */
199 TRYRET_LOCALERR /* Local failure. */
203 void set_rpc_maxgrouplist(int);
205 static struct netconfig *getnetconf_cached(const char *);
206 static const char *netidbytype(int, int);
207 static int getnfsargs(char *, struct nfs_args *);
208 static void usage(void) __dead2;
209 static int xdr_dir(XDR *, char *);
210 static int xdr_fh(XDR *, struct nfhret *);
212 nfs_tryproto(struct nfs_args *, struct addrinfo *,
213 char *, char *, char **);
215 returncode(enum clnt_stat, struct rpc_err *);
218 * Used to set mount flags with getmntopts. Call with dir=TRUE to
219 * initialize altflags from the current mount flags. Call with
220 * dir=FALSE to update mount flags with the new value of altflags after
221 * the call to getmntopts.
224 set_flags(int* altflags, int* nfsflags, int dir)
228 if (*nfsflags & NFSMNT_##nf) \
229 *altflags |= ALTF_##af; \
231 *altflags &= ~ALTF_##af; \
233 if (*altflags & ALTF_##af) \
234 *nfsflags |= NFSMNT_##nf; \
236 *nfsflags &= ~NFSMNT_##nf; \
262 main(int argc, char **argv)
265 struct nfs_args *nfsargsp;
266 struct nfs_args nfsargs;
267 struct nfsd_cargs ncd;
268 int mntflags, altflags, nfssvc_flag, num;
269 char *name, *p, *spec;
270 char mntpath[MAXPATHLEN];
277 strcpy(realm, KRB_REALM);
278 if (sizeof (struct nfsrpc_nickverf) != RPCX_NICKVERF ||
279 sizeof (struct nfsrpc_fullblock) != RPCX_FULLBLOCK ||
280 ((char *)&ktick.kt) - ((char *)&ktick) != NFSX_UNSIGNED ||
281 ((char *)ktick.kt.dat) - ((char *)&ktick) != 2 * NFSX_UNSIGNED)
282 fprintf(stderr, "Yikes! NFSKERB structs not packed!!\n");
286 altflags = ALTF_TCP | ALTF_RDIRPLUS;
287 nfsargs = nfsdefargs;
289 if (altflags & ALTF_TCP) {
290 nfsproto = IPPROTO_TCP;
291 nfsargsp->sotype = SOCK_STREAM;
293 nfsproto = IPPROTO_UDP;
294 nfsargsp->sotype = SOCK_DGRAM;
296 while ((c = getopt(argc, argv,
297 "23a:bcdD:g:I:iKlm:No:PR:r:sTt:w:x:U")) != -1)
306 num = strtol(optarg, &p, 10);
308 errx(1, "illegal -a value -- %s", optarg);
309 nfsargsp->readahead = num;
310 nfsargsp->flags |= NFSMNT_READAHEAD;
316 nfsargsp->flags |= NFSMNT_NOCONN;
319 num = strtol(optarg, &p, 10);
321 errx(1, "illegal -D value -- %s", optarg);
322 nfsargsp->deadthresh = num;
323 nfsargsp->flags |= NFSMNT_DEADTHRESH;
326 nfsargsp->flags |= NFSMNT_DUMBTIMR;
330 num = strtol(optarg, &p, 10);
332 errx(1, "illegal -g value -- %s", optarg);
333 set_rpc_maxgrouplist(num);
334 nfsargsp->maxgrouplist = num;
335 nfsargsp->flags |= NFSMNT_MAXGRPS;
339 num = strtol(optarg, &p, 10);
341 errx(1, "illegal -I value -- %s", optarg);
342 nfsargsp->readdirsize = num;
343 nfsargsp->flags |= NFSMNT_READDIRSIZE;
346 nfsargsp->flags |= NFSMNT_INT;
350 nfsargsp->flags |= NFSMNT_KERB;
354 nfsargsp->flags |= NFSMNT_RDIRPLUS;
358 strncpy(realm, optarg, REALM_SZ - 1);
359 realm[REALM_SZ - 1] = '\0';
363 nfsargsp->flags &= ~NFSMNT_RESVPORT;
366 set_flags(&altflags, &nfsargsp->flags, TRUE);
367 altflags |= ALTF_RDIRPLUS;
368 if (nfsproto == IPPROTO_TCP)
369 altflags |= ALTF_TCP;
371 altflags |= ALTF_NFSV2;
372 else if (mountmode == V3)
373 altflags |= ALTF_NFSV3;
374 getmntopts(optarg, mopts, &mntflags, &altflags);
375 set_flags(&altflags, &nfsargsp->flags, FALSE);
377 * Handle altflags which don't map directly to
380 if(altflags & ALTF_BG)
382 if (altflags & ALTF_TCP) {
383 nfsproto = IPPROTO_TCP;
384 nfsargsp->sotype = SOCK_STREAM;
386 nfsproto = IPPROTO_UDP;
387 nfsargsp->sotype = SOCK_DGRAM;
389 if(altflags & ALTF_PORT) {
391 * XXX Converting from a string to an int
392 * and back again is silly, and we should
393 * allow /etc/services names.
395 asprintf(&portspec, "%d",
396 atoi(strstr(optarg, "port=") + 5));
397 if (portspec == NULL)
401 if(altflags & ALTF_NFSV2)
403 if(altflags & ALTF_NFSV3)
405 if(altflags & ALTF_ACREGMIN)
406 nfsargsp->acregmin = atoi(strstr(optarg,
408 if(altflags & ALTF_ACREGMAX)
409 nfsargsp->acregmax = atoi(strstr(optarg,
411 if(altflags & ALTF_ACDIRMIN)
412 nfsargsp->acdirmin = atoi(strstr(optarg,
414 if(altflags & ALTF_ACDIRMAX)
415 nfsargsp->acdirmax = atoi(strstr(optarg,
419 /* obsolete for NFSMNT_RESVPORT, now default */
422 num = strtol(optarg, &p, 10);
424 errx(1, "illegal -R value -- %s", optarg);
428 num = strtol(optarg, &p, 10);
430 errx(1, "illegal -r value -- %s", optarg);
431 nfsargsp->rsize = num;
432 nfsargsp->flags |= NFSMNT_RSIZE;
435 nfsargsp->flags |= NFSMNT_SOFT;
438 nfsargsp->sotype = SOCK_STREAM;
439 nfsproto = IPPROTO_TCP;
440 altflags |= ALTF_TCP;
443 num = strtol(optarg, &p, 10);
445 errx(1, "illegal -t value -- %s", optarg);
446 nfsargsp->timeo = num;
447 nfsargsp->flags |= NFSMNT_TIMEO;
450 num = strtol(optarg, &p, 10);
452 errx(1, "illegal -w value -- %s", optarg);
453 nfsargsp->wsize = num;
454 nfsargsp->flags |= NFSMNT_WSIZE;
457 num = strtol(optarg, &p, 10);
459 errx(1, "illegal -x value -- %s", optarg);
460 nfsargsp->retrans = num;
461 nfsargsp->flags |= NFSMNT_RETRANS;
464 nfsargsp->sotype = SOCK_DGRAM;
465 nfsproto = IPPROTO_UDP;
466 altflags &= ~ALTF_TCP;
484 /* The default is to keep retrying forever. */
486 if (!getnfsargs(spec, nfsargsp))
489 /* resolve the mountpoint with realpath(3) */
490 checkpath(name, mntpath);
492 error = getvfsbyname("nfs", &vfc);
493 if (error && vfsisloadable("nfs")) {
495 err(EX_OSERR, "vfsload(nfs)");
496 endvfsent(); /* clear cache */
497 error = getvfsbyname("nfs", &vfc);
500 errx(EX_OSERR, "nfs filesystem is not available");
502 if (mount(vfc.vfc_name, mntpath, mntflags, nfsargsp))
503 err(1, "%s", mntpath);
504 if (nfsargsp->flags & NFSMNT_KERB) {
505 if ((opflags & ISBGRND) == 0) {
506 if (daemon(0, 0) != 0)
509 openlog("mount_nfs", LOG_PID, LOG_DAEMON);
510 nfssvc_flag = NFSSVC_MNTD;
511 ncd.ncd_dirp = mntpath;
512 while (nfssvc(nfssvc_flag, (caddr_t)&ncd) < 0) {
513 if (errno != ENEEDAUTH) {
514 syslog(LOG_ERR, "nfssvc err %m");
518 NFSSVC_MNTD | NFSSVC_GOTAUTH | NFSSVC_AUTHINFAIL;
521 * Set up as ncd_authuid for the kerberos call.
522 * Must set ruid to ncd_authuid and reset the
523 * ticket name iff ncd_authuid is not the same
524 * as last time, so that the right ticket file
526 * Get the Kerberos credential structure so that
527 * we have the session key and get a ticket for
529 * For more info see the IETF Draft "Authentication
532 if (ncd.ncd_authuid != last_ruid) {
534 sprintf(buf, "%s%d", TKT_ROOT, ncd.ncd_authuid);
535 krb_set_tkt_string(buf);
536 last_ruid = ncd.ncd_authuid;
538 setreuid(ncd.ncd_authuid, 0);
539 kret = krb_get_cred(NFS_KERBSRV, inst, realm, &kcr);
540 if (kret == RET_NOTKT) {
541 kret = get_ad_tkt(NFS_KERBSRV, inst, realm,
543 if (kret == KSUCCESS)
544 kret = krb_get_cred(NFS_KERBSRV, inst, realm,
547 if (kret == KSUCCESS)
548 kret = krb_mk_req(&ktick.kt, NFS_KERBSRV, inst,
552 * Fill in the AKN_FULLNAME authenticator and verifier.
553 * Along with the Kerberos ticket, we need to build
554 * the timestamp verifier and encrypt it in CBC mode.
556 if (kret == KSUCCESS &&
557 ktick.kt.length <= (RPCAUTH_MAXSIZ-3*NFSX_UNSIGNED)
558 && gettimeofday(&ktv, NULL) == 0) {
559 ncd.ncd_authtype = RPCAUTH_KERB4;
560 ncd.ncd_authstr = (u_char *)&ktick;
561 ncd.ncd_authlen = nfsm_rndup(ktick.kt.length) +
563 ncd.ncd_verfstr = (u_char *)&kverf;
564 ncd.ncd_verflen = sizeof (kverf);
565 memmove(ncd.ncd_key, kcr.session,
566 sizeof (kcr.session));
567 kin.t1 = htonl(ktv.tv_sec);
568 kin.t2 = htonl(ktv.tv_usec);
569 kin.w1 = htonl(NFS_KERBTTL);
570 kin.w2 = htonl(NFS_KERBTTL - 1);
571 bzero((caddr_t)kivec, sizeof (kivec));
574 * Encrypt kin in CBC mode using the session
580 * Finally, fill the timestamp verifier into the
581 * authenticator and verifier.
583 ktick.kind = htonl(RPCAKN_FULLNAME);
584 kverf.kind = htonl(RPCAKN_FULLNAME);
585 NFS_KERBW1(ktick.kt) = kout.w1;
586 ktick.kt.length = htonl(ktick.kt.length);
587 kverf.verf.t1 = kout.t1;
588 kverf.verf.t2 = kout.t2;
589 kverf.verf.w2 = kout.w2;
590 nfssvc_flag = NFSSVC_MNTD | NFSSVC_GOTAUTH;
600 getnfsargs(char *spec, struct nfs_args *nfsargsp)
602 struct addrinfo hints, *ai_nfs, *ai;
604 char host[NI_MAXHOST], serv[NI_MAXSERV];
607 int ecode, speclen, remoteerr;
608 char *hostp, *delimp, *errstr;
613 static char nam[MNAMELEN + 1];
615 if ((delimp = strrchr(spec, ':')) != NULL) {
618 } else if ((delimp = strrchr(spec, '@')) != NULL) {
619 warnx("path@server syntax is deprecated, use server:path");
622 warnx("no <host>:<dirpath> nfs-name");
628 * If there has been a trailing slash at mounttime it seems
629 * that some mountd implementations fail to remove the mount
630 * entries from their mountlist while unmounting.
632 for (speclen = strlen(spec);
633 speclen > 1 && spec[speclen - 1] == '/';
635 spec[speclen - 1] = '\0';
636 if (strlen(hostp) + strlen(spec) + 1 > MNAMELEN) {
637 warnx("%s:%s: %s", hostp, spec, strerror(ENAMETOOLONG));
640 /* Make both '@' and ':' notations equal */
641 if (*hostp != '\0') {
643 memmove(nam, hostp, len);
645 memmove(nam + len + 1, spec, speclen);
646 nam[len + speclen + 1] = '\0';
650 * Handle an internet host address and reverse resolve it if
653 memset(&hints, 0, sizeof hints);
654 hints.ai_flags = AI_NUMERICHOST;
655 hints.ai_socktype = nfsargsp->sotype;
656 if (getaddrinfo(hostp, portspec, &hints, &ai_nfs) == 0) {
658 if ((nfsargsp->flags & NFSMNT_KERB)) {
660 if (getnameinfo(ai_nfs->ai_addr, ai_nfs->ai_addrlen,
661 host, sizeof host, serv, sizeof serv, 0) != 0) {
662 warnx("can't reverse resolve net address");
670 if ((ecode = getaddrinfo(hostp, portspec, &hints, &ai_nfs))
672 if (portspec == NULL)
673 errx(1, "%s: %s", hostp, gai_strerror(ecode));
675 errx(1, "%s:%s: %s", hostp, portspec,
676 gai_strerror(ecode));
681 if (nfsargsp->flags & NFSMNT_KERB) {
682 strncpy(inst, hp->h_name, INST_SZ);
683 inst[INST_SZ - 1] = '\0';
684 if (cp = strchr(inst, '.'))
689 ret = TRYRET_LOCALERR;
692 * Try each entry returned by getaddrinfo(). Note the
693 * occurence of remote errors by setting `remoteerr'.
696 for (ai = ai_nfs; ai != NULL; ai = ai->ai_next) {
697 ret = nfs_tryproto(nfsargsp, ai, hostp, spec, &errstr);
698 if (ret == TRYRET_SUCCESS)
700 if (ret != TRYRET_LOCALERR)
702 if ((opflags & ISBGRND) == 0)
703 fprintf(stderr, "%s\n", errstr);
705 if (ret == TRYRET_SUCCESS)
708 /* Exit if all errors were local. */
713 * If retrycnt == 0, we are to keep retrying forever.
714 * Otherwise decrement it, and exit if it hits zero.
716 if (retrycnt != 0 && --retrycnt == 0)
719 if ((opflags & (BGRND | ISBGRND)) == BGRND) {
720 warnx("Cannot immediately mount %s:%s, backgrounding",
723 if (daemon(0, 0) != 0)
728 freeaddrinfo(ai_nfs);
729 nfsargsp->hostname = nam;
730 /* Add mounted filesystem to PATH_MOUNTTAB */
731 if (!add_mtab(hostp, spec))
732 warnx("can't update %s for %s:%s", PATH_MOUNTTAB, hostp, spec);
737 * Try to set up the NFS arguments according to the address
738 * family, protocol (and possibly port) specified in `ai'.
740 * Returns TRYRET_SUCCESS if successful, or:
741 * TRYRET_TIMEOUT The server did not respond.
742 * TRYRET_REMOTEERR The server reported an error.
743 * TRYRET_LOCALERR Local failure.
745 * In all error cases, *errstr will be set to a statically-allocated string
746 * describing the error.
749 nfs_tryproto(struct nfs_args *nfsargsp, struct addrinfo *ai, char *hostp,
750 char *spec, char **errstr)
752 static char errbuf[256];
753 struct sockaddr_storage nfs_ss;
754 struct netbuf nfs_nb;
755 struct nfhret nfhret;
757 struct rpc_err rpcerr;
759 struct netconfig *nconf, *nconf_mnt;
760 const char *netid, *netid_mnt;
761 int doconnect, nfsvers, mntvers;
762 enum clnt_stat status;
763 enum mountmode trymntmode;
765 trymntmode = mountmode;
769 if ((netid = netidbytype(ai->ai_family, nfsargsp->sotype)) == NULL) {
770 snprintf(errbuf, sizeof errbuf,
771 "af %d sotype %d not supported", ai->ai_family,
773 return (TRYRET_LOCALERR);
775 if ((nconf = getnetconf_cached(netid)) == NULL) {
776 snprintf(errbuf, sizeof errbuf, "%s: %s", netid, nc_sperror());
777 return (TRYRET_LOCALERR);
784 if (trymntmode == V2) {
792 if (portspec != NULL) {
793 /* `ai' contains the complete nfsd sockaddr. */
794 nfs_nb.buf = ai->ai_addr;
795 nfs_nb.len = nfs_nb.maxlen = ai->ai_addrlen;
797 /* Ask the remote rpcbind. */
798 nfs_nb.buf = &nfs_ss;
799 nfs_nb.len = nfs_nb.maxlen = sizeof nfs_ss;
801 if (!rpcb_getaddr(RPCPROG_NFS, nfsvers, nconf, &nfs_nb,
803 if (rpc_createerr.cf_stat == RPC_PROGVERSMISMATCH &&
808 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s",
810 clnt_spcreateerror("RPCPROG_NFS"));
811 return (returncode(rpc_createerr.cf_stat,
812 &rpc_createerr.cf_error));
816 /* Check that the server (nfsd) responds on the port we have chosen. */
817 clp = clnt_tli_create(RPC_ANYFD, nconf, &nfs_nb, RPCPROG_NFS, nfsvers,
820 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid,
821 hostp, spec, clnt_spcreateerror("nfsd: RPCPROG_NFS"));
822 return (returncode(rpc_createerr.cf_stat,
823 &rpc_createerr.cf_error));
825 if (nfsargsp->sotype == SOCK_DGRAM &&
826 !(nfsargsp->flags & NFSMNT_NOCONN)) {
828 * Use connect(), to match what the kernel does. This
829 * catches cases where the server responds from the
830 * wrong source address.
833 if (!clnt_control(clp, CLSET_CONNECT, (char *)&doconnect)) {
835 snprintf(errbuf, sizeof errbuf,
836 "[%s] %s:%s: CLSET_CONNECT failed", netid, hostp,
838 return (TRYRET_LOCALERR);
844 status = clnt_call(clp, NFSPROC_NULL, (xdrproc_t)xdr_void, NULL,
845 (xdrproc_t)xdr_void, NULL, try);
846 if (status != RPC_SUCCESS) {
847 if (status == RPC_PROGVERSMISMATCH && trymntmode == ANY) {
852 clnt_geterr(clp, &rpcerr);
853 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid,
854 hostp, spec, clnt_sperror(clp, "NFSPROC_NULL"));
856 return (returncode(status, &rpcerr));
860 /* Send the RPCMNT_MOUNT RPC to get the root filehandle. */
863 clp = clnt_tp_create(hostp, RPCPROG_MNT, mntvers, nconf_mnt);
865 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid_mnt,
866 hostp, spec, clnt_spcreateerror("RPCMNT: clnt_create"));
867 return (returncode(rpc_createerr.cf_stat,
868 &rpc_createerr.cf_error));
870 clp->cl_auth = authsys_create_default();
871 if (nfsargsp->flags & NFSMNT_KERB)
872 nfhret.auth = RPCAUTH_KERB4;
874 nfhret.auth = RPCAUTH_UNIX;
875 nfhret.vers = mntvers;
876 status = clnt_call(clp, RPCMNT_MOUNT, (xdrproc_t)xdr_dir, spec,
877 (xdrproc_t)xdr_fh, &nfhret, try);
878 auth_destroy(clp->cl_auth);
879 if (status != RPC_SUCCESS) {
880 if (status == RPC_PROGVERSMISMATCH && trymntmode == ANY) {
885 clnt_geterr(clp, &rpcerr);
886 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid_mnt,
887 hostp, spec, clnt_sperror(clp, "RPCPROG_MNT"));
889 return (returncode(status, &rpcerr));
893 if (nfhret.stat != 0) {
894 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid_mnt,
895 hostp, spec, strerror(nfhret.stat));
896 return (TRYRET_REMOTEERR);
900 * Store the filehandle and server address in nfsargsp, making
901 * sure to copy any locally allocated structures.
903 nfsargsp->addrlen = nfs_nb.len;
904 nfsargsp->addr = malloc(nfsargsp->addrlen);
905 nfsargsp->fhsize = nfhret.fhsize;
906 nfsargsp->fh = malloc(nfsargsp->fhsize);
907 if (nfsargsp->addr == NULL || nfsargsp->fh == NULL)
909 bcopy(nfs_nb.buf, nfsargsp->addr, nfsargsp->addrlen);
910 bcopy(nfhret.nfh, nfsargsp->fh, nfsargsp->fhsize);
913 nfsargsp->flags |= NFSMNT_NFSV3;
915 nfsargsp->flags &= ~NFSMNT_NFSV3;
917 return (TRYRET_SUCCESS);
922 * Catagorise a RPC return status and error into an `enum tryret'
926 returncode(enum clnt_stat status, struct rpc_err *rpcerr)
930 return (TRYRET_TIMEOUT);
931 case RPC_PMAPFAILURE:
932 case RPC_PROGNOTREGISTERED:
933 case RPC_PROGVERSMISMATCH:
934 /* XXX, these can be local or remote. */
937 return (TRYRET_REMOTEERR);
938 case RPC_SYSTEMERROR:
939 switch (rpcerr->re_errno) {
941 return (TRYRET_TIMEOUT);
945 return (TRYRET_REMOTEERR);
951 return (TRYRET_LOCALERR);
955 * Look up a netid based on an address family and socket type.
956 * `af' is the address family, and `sotype' is SOCK_DGRAM or SOCK_STREAM.
958 * XXX there should be a library function for this.
961 netidbytype(int af, int sotype)
965 for (p = nc_protos; p->netid != NULL; p++) {
966 if (af != p->af || sotype != p->sotype)
974 * Look up a netconfig entry based on a netid, and cache the result so
975 * that we don't need to remember to call freenetconfigent().
977 * Otherwise it behaves just like getnetconfigent(), so nc_*error()
980 static struct netconfig *
981 getnetconf_cached(const char *netid)
983 static struct nc_entry {
984 struct netconfig *nconf;
985 struct nc_entry *next;
988 struct netconfig *nconf;
990 for (p = head; p != NULL; p = p->next)
991 if (strcmp(netid, p->nconf->nc_netid) == 0)
994 if ((nconf = getnetconfigent(netid)) == NULL)
996 if ((p = malloc(sizeof(*p))) == NULL)
1006 * xdr routines for mount rpc's
1009 xdr_dir(XDR *xdrsp, char *dirp)
1011 return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
1015 xdr_fh(XDR *xdrsp, struct nfhret *np)
1018 long auth, authcnt, authfnd = 0;
1020 if (!xdr_u_long(xdrsp, &np->stat))
1026 np->fhsize = NFSX_V2FH;
1027 return (xdr_opaque(xdrsp, (caddr_t)np->nfh, NFSX_V2FH));
1029 if (!xdr_long(xdrsp, &np->fhsize))
1031 if (np->fhsize <= 0 || np->fhsize > NFSX_V3FHMAX)
1033 if (!xdr_opaque(xdrsp, (caddr_t)np->nfh, np->fhsize))
1035 if (!xdr_long(xdrsp, &authcnt))
1037 for (i = 0; i < authcnt; i++) {
1038 if (!xdr_long(xdrsp, &auth))
1040 if (auth == np->auth)
1044 * Some servers, such as DEC's OSF/1 return a nil authenticator
1045 * list to indicate RPCAUTH_UNIX.
1047 if (!authfnd && (authcnt > 0 || np->auth != RPCAUTH_UNIX))
1057 fprintf(stderr, "%s\n%s\n%s\n%s\n",
1058 "usage: mount_nfs [-23KNPTUbcdils] [-D deadthresh] [-I readdirsize]",
1059 " [-R retrycnt] [-a maxreadahead]",
1060 " [-g maxgroups] [-m realm] [-o options] [-r readsize]",
1061 " [-t timeout] [-w writesize] [-x retrans] rhost:path node");