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.8 2004/02/04 17:40:00 joerg Exp $
42 #include <sys/param.h>
43 #include <sys/mount.h>
45 #include <sys/syslog.h>
48 #include <rpc/pmap_clnt.h>
49 #include <rpc/pmap_prot.h>
51 #include <vfs/nfs/rpcv2.h>
52 #include <vfs/nfs/nfsproto.h>
53 #include <vfs/nfs/nfs.h>
54 #include <vfs/nfs/nqnfs.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_MNTUDP 0x80
80 #define ALTF_RESVPORT 0x100
81 #define ALTF_SEQPACKET 0x200
82 #define ALTF_NQNFS 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", 0, ALTF_MNTUDP, 1 },
107 { "resvport", 0, ALTF_RESVPORT, 1 },
108 { "nqnfs", 0, ALTF_NQNFS, 1 },
109 { "soft", 0, ALTF_SOFT, 1 },
110 { "tcp", 0, 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 },
120 struct nfs_args nfsdefargs = {
122 (struct sockaddr *)0,
123 sizeof (struct sockaddr_in),
151 u_char nfh[NFSX_V3FHMAX];
155 #define ISBGRND 0x0002
156 #define DIDWARN 0x0004
160 int nfsproto = IPPROTO_UDP;
171 char realm[REALM_SZ];
176 struct nfsrpc_nickverf kverf;
177 struct nfsrpc_fullblock kin, kout;
181 NFSKERBKEYSCHED_T kerb_keysched;
184 /* Return codes for nfs_tryproto. */
187 TRYRET_TIMEOUT, /* No response received. */
188 TRYRET_REMOTEERR, /* Error received from remote server. */
189 TRYRET_LOCALERR /* Local failure. */
192 int getnfsargs(char *, struct nfs_args *);
193 void set_rpc_maxgrouplist(int);
194 void usage(void) __dead2;
195 int xdr_dir(XDR *, char *);
196 int xdr_fh(XDR *, struct nfhret *);
197 enum tryret nfs_tryproto(struct nfs_args *nfsargsp, struct sockaddr_in *sin,
198 char *hostp, char *spec, char **errstr);
199 enum tryret returncode(enum clnt_stat stat, struct rpc_err *rpcerr);
202 * Used to set mount flags with getmntopts. Call with dir=TRUE to
203 * initialize altflags from the current mount flags. Call with
204 * dir=FALSE to update mount flags with the new value of altflags after
205 * the call to getmntopts.
208 set_flags(int* altflags, int* nfsflags, int dir)
212 if (*nfsflags & NFSMNT_##nf) \
213 *altflags |= ALTF_##af; \
215 *altflags &= ~ALTF_##af; \
217 if (*altflags & ALTF_##af) \
218 *nfsflags |= NFSMNT_##nf; \
220 *nfsflags &= ~NFSMNT_##nf; \
244 main(int argc, char **argv)
247 register struct nfs_args *nfsargsp;
248 struct nfs_args nfsargs;
249 struct nfsd_cargs ncd;
250 int mntflags, altflags, nfssvc_flag, num;
251 char *name, *p, *spec;
252 char mntpath[MAXPATHLEN];
259 (void)strcpy(realm, KRB_REALM);
260 if (sizeof (struct nfsrpc_nickverf) != RPCX_NICKVERF ||
261 sizeof (struct nfsrpc_fullblock) != RPCX_FULLBLOCK ||
262 ((char *)&ktick.kt) - ((char *)&ktick) != NFSX_UNSIGNED ||
263 ((char *)ktick.kt.dat) - ((char *)&ktick) != 2 * NFSX_UNSIGNED)
264 fprintf(stderr, "Yikes! NFSKERB structs not packed!!\n");
269 nfsargs = nfsdefargs;
271 while ((c = getopt(argc, argv,
272 "23a:bcdD:g:I:iKL:lm:No:PqR:r:sTt:w:x:U")) != -1)
281 num = strtol(optarg, &p, 10);
283 errx(1, "illegal -a value -- %s", optarg);
284 nfsargsp->readahead = num;
285 nfsargsp->flags |= NFSMNT_READAHEAD;
291 nfsargsp->flags |= NFSMNT_NOCONN;
294 num = strtol(optarg, &p, 10);
296 errx(1, "illegal -D value -- %s", optarg);
297 nfsargsp->deadthresh = num;
298 nfsargsp->flags |= NFSMNT_DEADTHRESH;
301 nfsargsp->flags |= NFSMNT_DUMBTIMR;
304 num = strtol(optarg, &p, 10);
306 errx(1, "illegal -g value -- %s", optarg);
307 set_rpc_maxgrouplist(num);
308 nfsargsp->maxgrouplist = num;
309 nfsargsp->flags |= NFSMNT_MAXGRPS;
312 num = strtol(optarg, &p, 10);
314 errx(1, "illegal -I value -- %s", optarg);
315 nfsargsp->readdirsize = num;
316 nfsargsp->flags |= NFSMNT_READDIRSIZE;
319 nfsargsp->flags |= NFSMNT_INT;
323 nfsargsp->flags |= NFSMNT_KERB;
327 num = strtol(optarg, &p, 10);
329 errx(1, "illegal -L value -- %s", optarg);
330 nfsargsp->leaseterm = num;
331 nfsargsp->flags |= NFSMNT_LEASETERM;
334 nfsargsp->flags |= NFSMNT_RDIRPLUS;
338 (void)strncpy(realm, optarg, REALM_SZ - 1);
339 realm[REALM_SZ - 1] = '\0';
343 nfsargsp->flags &= ~NFSMNT_RESVPORT;
347 set_flags(&altflags, &nfsargsp->flags, TRUE);
349 altflags |= ALTF_NFSV2;
350 else if (mountmode == V3)
351 altflags |= ALTF_NFSV3;
352 getmntopts(optarg, mopts, &mntflags, &altflags);
353 set_flags(&altflags, &nfsargsp->flags, FALSE);
355 * Handle altflags which don't map directly to
358 if(altflags & ALTF_BG)
360 if(altflags & ALTF_MNTUDP)
362 if(altflags & ALTF_TCP) {
363 nfsargsp->sotype = SOCK_STREAM;
364 nfsproto = IPPROTO_TCP;
366 if(altflags & ALTF_PORT)
367 port_no = atoi(strstr(optarg, "port=") + 5);
369 if(altflags & ALTF_NFSV2)
371 if(altflags & ALTF_NFSV3)
373 if(altflags & ALTF_ACREGMIN)
374 nfsargsp->acregmin = atoi(strstr(optarg,
376 if(altflags & ALTF_ACREGMAX)
377 nfsargsp->acregmax = atoi(strstr(optarg,
379 if(altflags & ALTF_ACDIRMIN)
380 nfsargsp->acdirmin = atoi(strstr(optarg,
382 if(altflags & ALTF_ACDIRMAX)
383 nfsargsp->acdirmax = atoi(strstr(optarg,
387 /* obsolete for NFSMNT_RESVPORT, now default */
391 nfsargsp->flags |= NFSMNT_NQNFS;
394 num = strtol(optarg, &p, 10);
396 errx(1, "illegal -R value -- %s", optarg);
400 num = strtol(optarg, &p, 10);
402 errx(1, "illegal -r value -- %s", optarg);
403 nfsargsp->rsize = num;
404 nfsargsp->flags |= NFSMNT_RSIZE;
407 nfsargsp->flags |= NFSMNT_SOFT;
410 nfsargsp->sotype = SOCK_STREAM;
411 nfsproto = IPPROTO_TCP;
414 num = strtol(optarg, &p, 10);
416 errx(1, "illegal -t value -- %s", optarg);
417 nfsargsp->timeo = num;
418 nfsargsp->flags |= NFSMNT_TIMEO;
421 num = strtol(optarg, &p, 10);
423 errx(1, "illegal -w value -- %s", optarg);
424 nfsargsp->wsize = num;
425 nfsargsp->flags |= NFSMNT_WSIZE;
428 num = strtol(optarg, &p, 10);
430 errx(1, "illegal -x value -- %s", optarg);
431 nfsargsp->retrans = num;
432 nfsargsp->flags |= NFSMNT_RETRANS;
453 /* The default is to keep retrying forever. */
455 if (!getnfsargs(spec, nfsargsp))
458 /* resolve the mountpoint with realpath(3) */
459 (void)checkpath(name, mntpath);
461 error = getvfsbyname("nfs", &vfc);
462 if (error && vfsisloadable("nfs")) {
464 err(EX_OSERR, "vfsload(nfs)");
465 endvfsent(); /* clear cache */
466 error = getvfsbyname("nfs", &vfc);
469 errx(EX_OSERR, "nfs filesystem is not available");
471 if (mount(vfc.vfc_name, mntpath, mntflags, nfsargsp))
472 err(1, "%s", mntpath);
473 if (nfsargsp->flags & (NFSMNT_NQNFS | NFSMNT_KERB)) {
474 if ((opflags & ISBGRND) == 0) {
475 if (daemon(0, 0) != 0)
478 openlog("mount_nfs", LOG_PID, LOG_DAEMON);
479 nfssvc_flag = NFSSVC_MNTD;
480 ncd.ncd_dirp = mntpath;
481 while (nfssvc(nfssvc_flag, (caddr_t)&ncd) < 0) {
482 if (errno != ENEEDAUTH) {
483 syslog(LOG_ERR, "nfssvc err %m");
487 NFSSVC_MNTD | NFSSVC_GOTAUTH | NFSSVC_AUTHINFAIL;
490 * Set up as ncd_authuid for the kerberos call.
491 * Must set ruid to ncd_authuid and reset the
492 * ticket name iff ncd_authuid is not the same
493 * as last time, so that the right ticket file
495 * Get the Kerberos credential structure so that
496 * we have the session key and get a ticket for
498 * For more info see the IETF Draft "Authentication
501 if (ncd.ncd_authuid != last_ruid) {
503 (void)sprintf(buf, "%s%d",
504 TKT_ROOT, ncd.ncd_authuid);
505 krb_set_tkt_string(buf);
506 last_ruid = ncd.ncd_authuid;
508 setreuid(ncd.ncd_authuid, 0);
509 kret = krb_get_cred(NFS_KERBSRV, inst, realm, &kcr);
510 if (kret == RET_NOTKT) {
511 kret = get_ad_tkt(NFS_KERBSRV, inst, realm,
513 if (kret == KSUCCESS)
514 kret = krb_get_cred(NFS_KERBSRV, inst, realm,
517 if (kret == KSUCCESS)
518 kret = krb_mk_req(&ktick.kt, NFS_KERBSRV, inst,
522 * Fill in the AKN_FULLNAME authenticator and verifier.
523 * Along with the Kerberos ticket, we need to build
524 * the timestamp verifier and encrypt it in CBC mode.
526 if (kret == KSUCCESS &&
527 ktick.kt.length <= (RPCAUTH_MAXSIZ-3*NFSX_UNSIGNED)
528 && gettimeofday(&ktv, (struct timezone *)0) == 0) {
529 ncd.ncd_authtype = RPCAUTH_KERB4;
530 ncd.ncd_authstr = (u_char *)&ktick;
531 ncd.ncd_authlen = nfsm_rndup(ktick.kt.length) +
533 ncd.ncd_verfstr = (u_char *)&kverf;
534 ncd.ncd_verflen = sizeof (kverf);
535 memmove(ncd.ncd_key, kcr.session,
536 sizeof (kcr.session));
537 kin.t1 = htonl(ktv.tv_sec);
538 kin.t2 = htonl(ktv.tv_usec);
539 kin.w1 = htonl(NFS_KERBTTL);
540 kin.w2 = htonl(NFS_KERBTTL - 1);
541 bzero((caddr_t)kivec, sizeof (kivec));
544 * Encrypt kin in CBC mode using the session
550 * Finally, fill the timestamp verifier into the
551 * authenticator and verifier.
553 ktick.kind = htonl(RPCAKN_FULLNAME);
554 kverf.kind = htonl(RPCAKN_FULLNAME);
555 NFS_KERBW1(ktick.kt) = kout.w1;
556 ktick.kt.length = htonl(ktick.kt.length);
557 kverf.verf.t1 = kout.t1;
558 kverf.verf.t2 = kout.t2;
559 kverf.verf.w2 = kout.w2;
560 nfssvc_flag = NFSSVC_MNTD | NFSSVC_GOTAUTH;
570 getnfsargs(char *spec, struct nfs_args *nfsargsp)
573 struct sockaddr_in saddr;
575 int speclen, remoteerr;
576 char *hostp, *delimp, *errstr;
581 static char nam[MNAMELEN + 1];
583 if ((delimp = strrchr(spec, ':')) != NULL) {
586 } else if ((delimp = strrchr(spec, '@')) != NULL) {
587 warnx("path@server syntax is deprecated, use server:path");
590 warnx("no <host>:<dirpath> nfs-name");
596 * If there has been a trailing slash at mounttime it seems
597 * that some mountd implementations fail to remove the mount
598 * entries from their mountlist while unmounting.
600 for (speclen = strlen(spec);
601 speclen > 1 && spec[speclen - 1] == '/';
603 spec[speclen - 1] = '\0';
604 if (strlen(hostp) + strlen(spec) + 1 > MNAMELEN) {
605 warnx("%s:%s: %s", hostp, spec, strerror(ENAMETOOLONG));
608 /* Make both '@' and ':' notations equal */
609 if (*hostp != '\0') {
611 memmove(nam, hostp, len);
613 memmove(nam + len + 1, spec, speclen);
614 nam[len + speclen + 1] = '\0';
618 * Handle an internet host address and reverse resolve it if
621 bzero(&saddr, sizeof saddr);
622 saddr.sin_family = AF_INET;
623 saddr.sin_len = sizeof saddr;
626 saddr.sin_port = htons(port_no);
632 * Adjust DNS timeouts so we do not linger in the foreground
633 * if we can be backgrounded.
635 switch(opflags & (BGRND | ISBGRND)) {
645 if (isdigit(*hostp)) {
646 if ((saddr.sin_addr.s_addr = inet_addr(hostp)) == -1) {
647 warnx("bad net address %s", hostp);
650 } else if ((hp = gethostbyname(hostp)) != NULL) {
651 memmove(&saddr.sin_addr, hp->h_addr,
652 MIN(hp->h_length, sizeof(saddr.sin_addr)));
654 warnx("can't get net id for host: %s", hostp);
659 if (haserror == 0 && (nfsargsp->flags & NFSMNT_KERB)) {
660 if ((hp = gethostbyaddr((char *)&saddr.sin_addr.s_addr,
661 sizeof (u_long), AF_INET)) == NULL) {
662 warnx("can't reverse resolve net address");
666 memmove(&saddr.sin_addr, hp->h_addr,
667 MIN(hp->h_length, sizeof(saddr.sin_addr)));
668 strncpy(inst, hp->h_name, INST_SZ);
669 inst[INST_SZ - 1] = '\0';
670 if (cp = strchr(inst, '.'))
676 * If no error occured we do not have to retry again.
677 * Otherwise try to backgruond us if possible.
682 switch(opflags & (BGRND | ISBGRND)) {
684 if (haserror != EAI_AGAIN)
686 /* recoverable error */
687 warnx("Cannot immediately mount %s:%s, backgrounding",
691 if (daemon(0, 0) != 0)
696 * Already backgrounded or cannot be backgrounded.
698 if (haserror != EAI_AGAIN)
705 if (opflags & DIDWARN) {
707 warnx("successfully resolved %s after prior failures", hostp);
710 ret = TRYRET_LOCALERR;
713 ret = nfs_tryproto(nfsargsp, &saddr, hostp, spec, &errstr);
714 if (ret == TRYRET_SUCCESS)
716 if (ret != TRYRET_LOCALERR)
718 if ((opflags & ISBGRND) == 0)
719 fprintf(stderr, "%s\n", errstr);
721 /* Exit if all errors were local. */
726 * If retrycnt == 0, we are to keep retrying forever.
727 * Otherwise decrement it, and exit if it hits zero.
729 if (retrycnt != 0 && --retrycnt == 0)
732 if ((opflags & (BGRND | ISBGRND)) == BGRND) {
733 warnx("Cannot immediately mount %s:%s, backgrounding",
737 if (daemon(0, 0) != 0)
742 if (opflags & DIDWARN) {
744 warnx("successfully mounted %s after prior failures", spec);
746 nfsargsp->hostname = nam;
747 /* Add mounted filesystem to PATH_MOUNTTAB */
748 if (!add_mtab(hostp, spec))
749 warnx("can't update %s for %s:%s", PATH_MOUNTTAB, hostp, spec);
754 * Try to set up the NFS arguments according to the address
755 * (and possibly port) specified by `sinp'.
757 * Returns TRYRET_SUCCESS if successful, or:
758 * TRYRET_TIMEOUT The server did not respond.
759 * TRYRET_REMOTEERR The server reported an error.
760 * TRYRET_LOCALERR Local failure.
762 * In all error cases, *errstr will be set to a statically-allocated string
763 * describing the error.
766 nfs_tryproto(struct nfs_args *nfsargsp, struct sockaddr_in *sinp, char *hostp,
767 char *spec, char **errstr)
769 static char errbuf[256];
770 struct sockaddr_in sin, tmpsin;
771 struct nfhret nfhret;
773 struct rpc_err rpcerr;
775 int doconnect, nfsvers, mntvers, so;
777 enum mountmode trymntmode;
779 trymntmode = mountmode;
782 sin = tmpsin = *sinp;
785 if (trymntmode == V2) {
793 /* Check that the server (nfsd) responds on the port we have chosen. */
797 if (nfsargsp->sotype == SOCK_STREAM)
798 clp = clnttcp_create(&sin, RPCPROG_NFS, nfsvers, &so, 0, 0);
800 clp = clntudp_create(&sin, RPCPROG_NFS, nfsvers, try, &so);
802 snprintf(errbuf, sizeof errbuf, "%s:%s: %s",
803 hostp, spec, clnt_spcreateerror("nfsd: RPCPROG_NFS"));
804 return (returncode(rpc_createerr.cf_stat,
805 &rpc_createerr.cf_error));
807 if (nfsargsp->sotype == SOCK_DGRAM &&
808 !(nfsargsp->flags & NFSMNT_NOCONN)) {
810 * Use connect(), to match what the kernel does. This
811 * catches cases where the server responds from the
812 * wrong source address.
815 if (!clnt_control(clp, CLSET_CONNECT, (char *)&doconnect)) {
817 snprintf(errbuf, sizeof errbuf,
818 "%s:%s: CLSET_CONNECT failed", hostp, spec);
819 return (TRYRET_LOCALERR);
825 stat = clnt_call(clp, NFSPROC_NULL, xdr_void, NULL, xdr_void, NULL,
827 if (stat != RPC_SUCCESS) {
828 if (stat == RPC_PROGVERSMISMATCH && trymntmode == ANY) {
833 clnt_geterr(clp, &rpcerr);
834 snprintf(errbuf, sizeof errbuf, "%s:%s: %s",
835 hostp, spec, clnt_sperror(clp, "NFSPROC_NULL"));
837 return (returncode(stat, &rpcerr));
841 /* Send the RPCMNT_MOUNT RPC to get the root filehandle. */
846 if (mnttcp_ok && nfsargsp->sotype == SOCK_STREAM)
847 clp = clnttcp_create(&tmpsin, RPCPROG_MNT, mntvers, &so, 0, 0);
849 clp = clntudp_create(&tmpsin, RPCPROG_MNT, mntvers, try, &so);
851 snprintf(errbuf, sizeof errbuf, "%s:%s: %s",
852 hostp, spec, clnt_spcreateerror("RPCMNT: clnt_create"));
853 return (returncode(rpc_createerr.cf_stat,
854 &rpc_createerr.cf_error));
856 clp->cl_auth = authunix_create_default();
857 if (nfsargsp->flags & NFSMNT_KERB)
858 nfhret.auth = RPCAUTH_KERB4;
860 nfhret.auth = RPCAUTH_UNIX;
861 nfhret.vers = mntvers;
862 stat = clnt_call(clp, RPCMNT_MOUNT, xdr_dir, spec, xdr_fh, &nfhret,
864 auth_destroy(clp->cl_auth);
865 if (stat != RPC_SUCCESS) {
866 if (stat == RPC_PROGVERSMISMATCH && trymntmode == ANY) {
871 clnt_geterr(clp, &rpcerr);
872 snprintf(errbuf, sizeof errbuf, "%s:%s: %s",
873 hostp, spec, clnt_sperror(clp, "RPCPROG_MNT"));
875 return (returncode(stat, &rpcerr));
879 if (nfhret.stat != 0) {
880 snprintf(errbuf, sizeof errbuf, "%s:%s: %s",
881 hostp, spec, strerror(nfhret.stat));
882 return (TRYRET_REMOTEERR);
886 * Store the filehandle and server address in nfsargsp, making
887 * sure to copy any locally allocated structures.
889 nfsargsp->addrlen = sin.sin_len;
890 nfsargsp->addr = malloc(nfsargsp->addrlen);
891 nfsargsp->fhsize = nfhret.fhsize;
892 nfsargsp->fh = malloc(nfsargsp->fhsize);
893 if (nfsargsp->addr == NULL || nfsargsp->fh == NULL)
895 bcopy(&sin, nfsargsp->addr, nfsargsp->addrlen);
896 bcopy(nfhret.nfh, nfsargsp->fh, nfsargsp->fhsize);
899 nfsargsp->flags |= NFSMNT_NFSV3;
901 nfsargsp->flags &= ~NFSMNT_NFSV3;
903 return (TRYRET_SUCCESS);
907 * Catagorise a RPC return status and error into an `enum tryret'
911 returncode(enum clnt_stat stat, struct rpc_err *rpcerr)
915 return (TRYRET_TIMEOUT);
916 case RPC_PMAPFAILURE:
917 case RPC_PROGNOTREGISTERED:
918 case RPC_PROGVERSMISMATCH:
919 /* XXX, these can be local or remote. */
922 return (TRYRET_REMOTEERR);
923 case RPC_SYSTEMERROR:
924 switch (rpcerr->re_errno) {
926 return (TRYRET_TIMEOUT);
930 return (TRYRET_REMOTEERR);
936 return (TRYRET_LOCALERR);
940 * xdr routines for mount rpc's
943 xdr_dir(XDR *xdrsp, char *dirp)
945 return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
949 xdr_fh(XDR *xdrsp, register struct nfhret *np)
952 long auth, authcnt, authfnd = 0;
954 if (!xdr_u_long(xdrsp, &np->stat))
960 np->fhsize = NFSX_V2FH;
961 return (xdr_opaque(xdrsp, (caddr_t)np->nfh, NFSX_V2FH));
963 if (!xdr_long(xdrsp, &np->fhsize))
965 if (np->fhsize <= 0 || np->fhsize > NFSX_V3FHMAX)
967 if (!xdr_opaque(xdrsp, (caddr_t)np->nfh, np->fhsize))
969 if (!xdr_long(xdrsp, &authcnt))
971 for (i = 0; i < authcnt; i++) {
972 if (!xdr_long(xdrsp, &auth))
974 if (auth == np->auth)
978 * Some servers, such as DEC's OSF/1 return a nil authenticator
979 * list to indicate RPCAUTH_UNIX.
981 if (!authfnd && (authcnt > 0 || np->auth != RPCAUTH_UNIX))
991 (void)fprintf(stderr, "%s\n%s\n%s\n%s\n",
992 "usage: mount_nfs [-23KNPTUbcdilqs] [-D deadthresh] [-I readdirsize]",
993 " [-L leaseterm] [-R retrycnt] [-a maxreadahead]",
994 " [-g maxgroups] [-m realm] [-o options] [-r readsize]",
995 " [-t timeout] [-w writesize] [-x retrans] rhost:path node");