mount_nfs.c revision 25004
1/* 2 * Copyright (c) 1992, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Rick Macklem at The University of Guelph. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 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. 23 * 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 34 * SUCH DAMAGE. 35 */ 36 37#ifndef lint 38static char copyright[] = 39"@(#) Copyright (c) 1992, 1993, 1994\n\ 40 The Regents of the University of California. All rights reserved.\n"; 41#endif /* not lint */ 42 43#ifndef lint 44/* 45static char sccsid[] = "@(#)mount_nfs.c 8.11 (Berkeley) 5/4/95"; 46*/ 47static const char rcsid[] = 48 "$Id: mount_nfs.c,v 1.20 1997/04/02 11:30:44 dfr Exp $"; 49#endif /* not lint */ 50 51#include <sys/param.h> 52#include <sys/mount.h> 53#include <sys/socket.h> 54#include <sys/socketvar.h> 55#include <sys/stat.h> 56#include <sys/syslog.h> 57 58#include <rpc/rpc.h> 59#include <rpc/pmap_clnt.h> 60#include <rpc/pmap_prot.h> 61 62#ifdef ISO 63#include <netiso/iso.h> 64#endif 65 66#ifdef NFSKERB 67#include <kerberosIV/des.h> 68#include <kerberosIV/krb.h> 69#endif 70 71#include <nfs/rpcv2.h> 72#include <nfs/nfsproto.h> 73#define KERNEL 74#include <nfs/nfs.h> 75#undef KERNEL 76#include <nfs/nqnfs.h> 77 78#include <arpa/inet.h> 79 80#include <ctype.h> 81#include <err.h> 82#include <errno.h> 83#include <fcntl.h> 84#include <netdb.h> 85#include <signal.h> 86#include <stdio.h> 87#include <stdlib.h> 88#include <strings.h> 89#include <sysexits.h> 90#include <unistd.h> 91 92#include "mntopts.h" 93 94#define ALTF_BG 0x1 95#define ALTF_NOCONN 0x2 96#define ALTF_DUMBTIMR 0x4 97#define ALTF_INTR 0x8 98#define ALTF_KERB 0x10 99#define ALTF_NFSV3 0x20 100#define ALTF_RDIRPLUS 0x40 101#define ALTF_MNTUDP 0x80 102#define ALTF_RESVPORT 0x100 103#define ALTF_SEQPACKET 0x200 104#define ALTF_NQNFS 0x400 105#define ALTF_SOFT 0x800 106#define ALTF_TCP 0x1000 107#define ALTF_PORT 0x2000 108#define ALTF_NFSV2 0x4000 109 110struct mntopt mopts[] = { 111 MOPT_STDOPTS, 112 MOPT_FORCE, 113 MOPT_UPDATE, 114 { "bg", 0, ALTF_BG, 1 }, 115 { "conn", 1, ALTF_NOCONN, 1 }, 116 { "dumbtimer", 0, ALTF_DUMBTIMR, 1 }, 117 { "intr", 0, ALTF_INTR, 1 }, 118#ifdef NFSKERB 119 { "kerb", 0, ALTF_KERB, 1 }, 120#endif 121 { "nfsv3", 0, ALTF_NFSV3, 1 }, 122 { "rdirplus", 0, ALTF_RDIRPLUS, 1 }, 123 { "mntudp", 0, ALTF_MNTUDP, 1 }, 124 { "resvport", 0, ALTF_RESVPORT, 1 }, 125#ifdef ISO 126 { "seqpacket", 0, ALTF_SEQPACKET, 1 }, 127#endif 128 { "nqnfs", 0, ALTF_NQNFS, 1 }, 129 { "soft", 0, ALTF_SOFT, 1 }, 130 { "tcp", 0, ALTF_TCP, 1 }, 131 { "port=", 0, ALTF_PORT, 1 }, 132 { "nfsv2", 0, ALTF_NFSV2, 1 }, 133 { NULL } 134}; 135 136struct nfs_args nfsdefargs = { 137 NFS_ARGSVERSION, 138 (struct sockaddr *)0, 139 sizeof (struct sockaddr_in), 140 SOCK_DGRAM, 141 0, 142 (u_char *)0, 143 0, 144 NFSMNT_RESVPORT, 145 NFS_WSIZE, 146 NFS_RSIZE, 147 NFS_READDIRSIZE, 148 10, 149 NFS_RETRANS, 150 NFS_MAXGRPS, 151 NFS_DEFRAHEAD, 152 NQ_DEFLEASE, 153 NQ_DEADTHRESH, 154 (char *)0, 155}; 156 157struct nfhret { 158 u_long stat; 159 long vers; 160 long auth; 161 long fhsize; 162 u_char nfh[NFSX_V3FHMAX]; 163}; 164#define DEF_RETRY 10000 165#define BGRND 1 166#define ISBGRND 2 167int retrycnt = DEF_RETRY; 168int opflags = 0; 169int nfsproto = IPPROTO_UDP; 170int mnttcp_ok = 1; 171u_short port_no = 0; 172enum { 173 ANY, 174 V2, 175 V3 176} mountmode = ANY; 177 178#ifdef NFSKERB 179char inst[INST_SZ]; 180char realm[REALM_SZ]; 181struct { 182 u_long kind; 183 KTEXT_ST kt; 184} ktick; 185struct nfsrpc_nickverf kverf; 186struct nfsrpc_fullblock kin, kout; 187NFSKERBKEY_T kivec; 188CREDENTIALS kcr; 189struct timeval ktv; 190NFSKERBKEYSCHED_T kerb_keysched; 191#endif 192 193int getnfsargs __P((char *, struct nfs_args *)); 194#ifdef ISO 195struct iso_addr *iso_addr __P((const char *)); 196#endif 197void set_rpc_maxgrouplist __P((int)); 198void usage __P((void)) __dead2; 199int xdr_dir __P((XDR *, char *)); 200int xdr_fh __P((XDR *, struct nfhret *)); 201 202/* 203 * Used to set mount flags with getmntopts. Call with dir=TRUE to 204 * initialise altflags from the current mount flags. Call with 205 * dir=FALSE to update mount flags with the new value of altflags after 206 * the call to getmntopts. 207 */ 208static void 209setflags(int* altflags, int* nfsflags, int dir) 210{ 211#define F2(af, nf) \ 212 if (dir) { \ 213 if (*nfsflags & NFSMNT_##nf) \ 214 *altflags |= ALTF_##af; \ 215 else \ 216 *altflags &= ~ALTF_##af; \ 217 } else { \ 218 if (*altflags & ALTF_##af) \ 219 *nfsflags |= NFSMNT_##nf; \ 220 else \ 221 *nfsflags &= ~NFSMNT_##nf; \ 222 } 223#define F(f) F2(f,f) 224 225 F(NOCONN); 226 F(DUMBTIMR); 227 F2(INTR, INT); 228#ifdef NFSKERB 229 F(KERB); 230#endif 231 F(RDIRPLUS); 232 F(RESVPORT); 233 F(NQNFS); 234 F(SOFT); 235 236#undef F 237#undef F2 238} 239 240int 241main(argc, argv) 242 int argc; 243 char *argv[]; 244{ 245 register int c; 246 register struct nfs_args *nfsargsp; 247 struct nfs_args nfsargs; 248 struct nfsd_cargs ncd; 249 int mntflags, altflags, i, nfssvc_flag, num; 250 char *name, *p, *spec; 251 struct vfsconf vfc; 252 int error = 0; 253#ifdef NFSKERB 254 uid_t last_ruid; 255 256 last_ruid = -1; 257 (void)strcpy(realm, KRB_REALM); 258 if (sizeof (struct nfsrpc_nickverf) != RPCX_NICKVERF || 259 sizeof (struct nfsrpc_fullblock) != RPCX_FULLBLOCK || 260 ((char *)&ktick.kt) - ((char *)&ktick) != NFSX_UNSIGNED || 261 ((char *)ktick.kt.dat) - ((char *)&ktick) != 2 * NFSX_UNSIGNED) 262 fprintf(stderr, "Yikes! NFSKERB structs not packed!!\n"); 263#endif /* NFSKERB */ 264 retrycnt = DEF_RETRY; 265 266 mntflags = 0; 267 altflags = 0; 268 nfsargs = nfsdefargs; 269 nfsargsp = &nfsargs; 270 while ((c = getopt(argc, argv, 271 "23a:bcdD:g:I:iKL:lm:o:PpqR:r:sTt:w:x:U")) != -1) 272 switch (c) { 273 case '2': 274 mountmode = V2; 275 break; 276 case '3': 277 mountmode = V3; 278 break; 279 case 'a': 280 num = strtol(optarg, &p, 10); 281 if (*p || num < 0) 282 errx(1, "illegal -a value -- %s", optarg); 283 nfsargsp->readahead = num; 284 nfsargsp->flags |= NFSMNT_READAHEAD; 285 break; 286 case 'b': 287 opflags |= BGRND; 288 break; 289 case 'c': 290 nfsargsp->flags |= NFSMNT_NOCONN; 291 break; 292 case 'D': 293 num = strtol(optarg, &p, 10); 294 if (*p || num <= 0) 295 errx(1, "illegal -D value -- %s", optarg); 296 nfsargsp->deadthresh = num; 297 nfsargsp->flags |= NFSMNT_DEADTHRESH; 298 break; 299 case 'd': 300 nfsargsp->flags |= NFSMNT_DUMBTIMR; 301 break; 302 case 'g': 303 num = strtol(optarg, &p, 10); 304 if (*p || num <= 0) 305 errx(1, "illegal -g value -- %s", optarg); 306#ifdef __FreeBSD__ 307 set_rpc_maxgrouplist(num); 308#endif 309 nfsargsp->maxgrouplist = num; 310 nfsargsp->flags |= NFSMNT_MAXGRPS; 311 break; 312 case 'I': 313 num = strtol(optarg, &p, 10); 314 if (*p || num <= 0) 315 errx(1, "illegal -I value -- %s", optarg); 316 nfsargsp->readdirsize = num; 317 nfsargsp->flags |= NFSMNT_READDIRSIZE; 318 break; 319 case 'i': 320 nfsargsp->flags |= NFSMNT_INT; 321 break; 322#ifdef NFSKERB 323 case 'K': 324 nfsargsp->flags |= NFSMNT_KERB; 325 break; 326#endif 327 case 'L': 328 num = strtol(optarg, &p, 10); 329 if (*p || num < 2) 330 errx(1, "illegal -L value -- %s", optarg); 331 nfsargsp->leaseterm = num; 332 nfsargsp->flags |= NFSMNT_LEASETERM; 333 break; 334 case 'l': 335 nfsargsp->flags |= NFSMNT_RDIRPLUS; 336 break; 337#ifdef NFSKERB 338 case 'm': 339 (void)strncpy(realm, optarg, REALM_SZ - 1); 340 realm[REALM_SZ - 1] = '\0'; 341 break; 342#endif 343 case 'o': 344 altflags = 0; 345 setflags(&altflags, &nfsargsp->flags, TRUE); 346 if (mountmode == V2) 347 altflags |= ALTF_NFSV2; 348 else if (mountmode == V3) 349 altflags |= ALTF_NFSV3; 350 getmntopts(optarg, mopts, &mntflags, &altflags); 351 setflags(&altflags, &nfsargsp->flags, FALSE); 352 /* 353 * Handle altflags which don't map directly to 354 * mount flags. 355 */ 356 if(altflags & ALTF_BG) 357 opflags |= BGRND; 358 if(altflags & ALTF_MNTUDP) 359 mnttcp_ok = 0; 360#ifdef ISO 361 if(altflags & ALTF_SEQPACKET) 362 nfsargsp->sotype = SOCK_SEQPACKET; 363#endif 364 if(altflags & ALTF_TCP) { 365 nfsargsp->sotype = SOCK_STREAM; 366 nfsproto = IPPROTO_TCP; 367 } 368 if(altflags & ALTF_PORT) 369 port_no = atoi(strstr(optarg, "port=") + 5); 370 mountmode = ANY; 371 if(altflags & ALTF_NFSV2) 372 mountmode = V2; 373 if(altflags & ALTF_NFSV3) 374 mountmode = V3; 375 break; 376 case 'P': 377 nfsargsp->flags |= NFSMNT_RESVPORT; 378 break; 379#ifdef ISO 380 case 'p': 381 nfsargsp->sotype = SOCK_SEQPACKET; 382 break; 383#endif 384 case 'q': 385 mountmode = V3; 386 nfsargsp->flags |= NFSMNT_NQNFS; 387 break; 388 case 'R': 389 num = strtol(optarg, &p, 10); 390 if (*p || num <= 0) 391 errx(1, "illegal -R value -- %s", optarg); 392 retrycnt = num; 393 break; 394 case 'r': 395 num = strtol(optarg, &p, 10); 396 if (*p || num <= 0) 397 errx(1, "illegal -r value -- %s", optarg); 398 nfsargsp->rsize = num; 399 nfsargsp->flags |= NFSMNT_RSIZE; 400 break; 401 case 's': 402 nfsargsp->flags |= NFSMNT_SOFT; 403 break; 404 case 'T': 405 nfsargsp->sotype = SOCK_STREAM; 406 nfsproto = IPPROTO_TCP; 407 break; 408 case 't': 409 num = strtol(optarg, &p, 10); 410 if (*p || num <= 0) 411 errx(1, "illegal -t value -- %s", optarg); 412 nfsargsp->timeo = num; 413 nfsargsp->flags |= NFSMNT_TIMEO; 414 break; 415 case 'w': 416 num = strtol(optarg, &p, 10); 417 if (*p || num <= 0) 418 errx(1, "illegal -w value -- %s", optarg); 419 nfsargsp->wsize = num; 420 nfsargsp->flags |= NFSMNT_WSIZE; 421 break; 422 case 'x': 423 num = strtol(optarg, &p, 10); 424 if (*p || num <= 0) 425 errx(1, "illegal -x value -- %s", optarg); 426 nfsargsp->retrans = num; 427 nfsargsp->flags |= NFSMNT_RETRANS; 428 break; 429 case 'U': 430 mnttcp_ok = 0; 431 break; 432 default: 433 usage(); 434 break; 435 } 436 argc -= optind; 437 argv += optind; 438 439 if (argc != 2) { 440 usage(); 441 /* NOTREACHED */ 442 } 443 444 spec = *argv++; 445 name = *argv; 446 447 if (!getnfsargs(spec, nfsargsp)) 448 exit(1); 449 450#ifdef __FreeBSD__ 451 error = getvfsbyname("nfs", &vfc); 452 if (error && vfsisloadable("nfs")) { 453 if(vfsload("nfs")) 454 err(EX_OSERR, "vfsload(nfs)"); 455 endvfsent(); /* clear cache */ 456 error = getvfsbyname("nfs", &vfc); 457 } 458 if (error) 459 errx(EX_OSERR, "nfs filesystem is not available"); 460 461 if (mount(vfc.vfc_name, name, mntflags, nfsargsp)) 462 err(1, "%s", name); 463#else 464 if (mount("nfs", name, mntflags, nfsargsp)) 465 err(1, "%s", name); 466#endif 467 if (nfsargsp->flags & (NFSMNT_NQNFS | NFSMNT_KERB)) { 468 if ((opflags & ISBGRND) == 0) { 469 if (i = fork()) { 470 if (i == -1) 471 err(1, "nqnfs 1"); 472 exit(0); 473 } 474 (void) setsid(); 475 (void) close(STDIN_FILENO); 476 (void) close(STDOUT_FILENO); 477 (void) close(STDERR_FILENO); 478 (void) chdir("/"); 479 } 480 openlog("mount_nfs:", LOG_PID, LOG_DAEMON); 481 nfssvc_flag = NFSSVC_MNTD; 482 ncd.ncd_dirp = name; 483 while (nfssvc(nfssvc_flag, (caddr_t)&ncd) < 0) { 484 if (errno != ENEEDAUTH) { 485 syslog(LOG_ERR, "nfssvc err %m"); 486 continue; 487 } 488 nfssvc_flag = 489 NFSSVC_MNTD | NFSSVC_GOTAUTH | NFSSVC_AUTHINFAIL; 490#ifdef NFSKERB 491 /* 492 * Set up as ncd_authuid for the kerberos call. 493 * Must set ruid to ncd_authuid and reset the 494 * ticket name iff ncd_authuid is not the same 495 * as last time, so that the right ticket file 496 * is found. 497 * Get the Kerberos credential structure so that 498 * we have the seesion key and get a ticket for 499 * this uid. 500 * For more info see the IETF Draft "Authentication 501 * in ONC RPC". 502 */ 503 if (ncd.ncd_authuid != last_ruid) { 504 char buf[512]; 505 (void)sprintf(buf, "%s%d", 506 TKT_ROOT, ncd.ncd_authuid); 507 krb_set_tkt_string(buf); 508 last_ruid = ncd.ncd_authuid; 509 } 510 setreuid(ncd.ncd_authuid, 0); 511 kret = krb_get_cred(NFS_KERBSRV, inst, realm, &kcr); 512 if (kret == RET_NOTKT) { 513 kret = get_ad_tkt(NFS_KERBSRV, inst, realm, 514 DEFAULT_TKT_LIFE); 515 if (kret == KSUCCESS) 516 kret = krb_get_cred(NFS_KERBSRV, inst, realm, 517 &kcr); 518 } 519 if (kret == KSUCCESS) 520 kret = krb_mk_req(&ktick.kt, NFS_KERBSRV, inst, 521 realm, 0); 522 523 /* 524 * Fill in the AKN_FULLNAME authenticator and verfier. 525 * Along with the Kerberos ticket, we need to build 526 * the timestamp verifier and encrypt it in CBC mode. 527 */ 528 if (kret == KSUCCESS && 529 ktick.kt.length <= (RPCAUTH_MAXSIZ-3*NFSX_UNSIGNED) 530 && gettimeofday(&ktv, (struct timezone *)0) == 0) { 531 ncd.ncd_authtype = RPCAUTH_KERB4; 532 ncd.ncd_authstr = (u_char *)&ktick; 533 ncd.ncd_authlen = nfsm_rndup(ktick.kt.length) + 534 3 * NFSX_UNSIGNED; 535 ncd.ncd_verfstr = (u_char *)&kverf; 536 ncd.ncd_verflen = sizeof (kverf); 537 memmove(ncd.ncd_key, kcr.session, 538 sizeof (kcr.session)); 539 kin.t1 = htonl(ktv.tv_sec); 540 kin.t2 = htonl(ktv.tv_usec); 541 kin.w1 = htonl(NFS_KERBTTL); 542 kin.w2 = htonl(NFS_KERBTTL - 1); 543 bzero((caddr_t)kivec, sizeof (kivec)); 544 545 /* 546 * Encrypt kin in CBC mode using the session 547 * key in kcr. 548 */ 549 XXX 550 551 /* 552 * Finally, fill the timestamp verifier into the 553 * authenticator and verifier. 554 */ 555 ktick.kind = htonl(RPCAKN_FULLNAME); 556 kverf.kind = htonl(RPCAKN_FULLNAME); 557 NFS_KERBW1(ktick.kt) = kout.w1; 558 ktick.kt.length = htonl(ktick.kt.length); 559 kverf.verf.t1 = kout.t1; 560 kverf.verf.t2 = kout.t2; 561 kverf.verf.w2 = kout.w2; 562 nfssvc_flag = NFSSVC_MNTD | NFSSVC_GOTAUTH; 563 } 564 setreuid(0, 0); 565#endif /* NFSKERB */ 566 } 567 } 568 exit(0); 569} 570 571int 572getnfsargs(spec, nfsargsp) 573 char *spec; 574 struct nfs_args *nfsargsp; 575{ 576 register CLIENT *clp; 577 struct hostent *hp; 578 static struct sockaddr_in saddr; 579#ifdef ISO 580 static struct sockaddr_iso isoaddr; 581 struct iso_addr *isop; 582 int isoflag = 0; 583#endif 584 struct timeval pertry, try; 585 enum clnt_stat clnt_stat; 586 int so = RPC_ANYSOCK, i, nfsvers, mntvers, orgcnt; 587 char *hostp, *delimp; 588#ifdef NFSKERB 589 char *cp; 590#endif 591 u_short tport; 592 static struct nfhret nfhret; 593 static char nam[MNAMELEN + 1]; 594 595 strncpy(nam, spec, MNAMELEN); 596 nam[MNAMELEN] = '\0'; 597 if ((delimp = strchr(spec, '@')) != NULL) { 598 hostp = delimp + 1; 599 } else if ((delimp = strchr(spec, ':')) != NULL) { 600 hostp = spec; 601 spec = delimp + 1; 602 } else { 603 warnx("no <host>:<dirpath> or <dirpath>@<host> spec"); 604 return (0); 605 } 606 *delimp = '\0'; 607 /* 608 * DUMB!! Until the mount protocol works on iso transport, we must 609 * supply both an iso and an inet address for the host. 610 */ 611#ifdef ISO 612 if (!strncmp(hostp, "iso=", 4)) { 613 u_short isoport; 614 615 hostp += 4; 616 isoflag++; 617 if ((delimp = strchr(hostp, '+')) == NULL) { 618 warnx("no iso+inet address"); 619 return (0); 620 } 621 *delimp = '\0'; 622 if ((isop = iso_addr(hostp)) == NULL) { 623 warnx("bad ISO address"); 624 return (0); 625 } 626 memset(&isoaddr, 0, sizeof (isoaddr)); 627 memmove(&isoaddr.siso_addr, isop, sizeof (struct iso_addr)); 628 isoaddr.siso_len = sizeof (isoaddr); 629 isoaddr.siso_family = AF_ISO; 630 isoaddr.siso_tlen = 2; 631 isoport = htons(NFS_PORT); 632 memmove(TSEL(&isoaddr), &isoport, isoaddr.siso_tlen); 633 hostp = delimp + 1; 634 } 635#endif /* ISO */ 636 637 /* 638 * Handle an internet host address and reverse resolve it if 639 * doing Kerberos. 640 */ 641 if (isdigit(*hostp)) { 642 if ((saddr.sin_addr.s_addr = inet_addr(hostp)) == -1) { 643 warnx("bad net address %s", hostp); 644 return (0); 645 } 646 } else if ((hp = gethostbyname(hostp)) != NULL) 647 memmove(&saddr.sin_addr, hp->h_addr, hp->h_length); 648 else { 649 warnx("can't get net id for host"); 650 return (0); 651 } 652#ifdef NFSKERB 653 if ((nfsargsp->flags & NFSMNT_KERB)) { 654 if ((hp = gethostbyaddr((char *)&saddr.sin_addr.s_addr, 655 sizeof (u_long), AF_INET)) == (struct hostent *)0) { 656 warnx("can't reverse resolve net address"); 657 return (0); 658 } 659 memmove(&saddr.sin_addr, hp->h_addr, hp->h_length); 660 strncpy(inst, hp->h_name, INST_SZ); 661 inst[INST_SZ - 1] = '\0'; 662 if (cp = strchr(inst, '.')) 663 *cp = '\0'; 664 } 665#endif /* NFSKERB */ 666 667 orgcnt = retrycnt; 668tryagain: 669 if (mountmode == ANY || mountmode == V3) { 670 nfsvers = 3; 671 mntvers = 3; 672 nfsargsp->flags |= NFSMNT_NFSV3; 673 } else { 674 nfsvers = 2; 675 mntvers = 1; 676 nfsargsp->flags &= ~NFSMNT_NFSV3; 677 } 678 nfhret.stat = EACCES; /* Mark not yet successful */ 679 while (retrycnt > 0) { 680 saddr.sin_family = AF_INET; 681 saddr.sin_port = htons(PMAPPORT); 682 if ((tport = port_no ? port_no : 683 pmap_getport(&saddr, RPCPROG_NFS, 684 nfsvers, nfsproto)) == 0) { 685 if ((opflags & ISBGRND) == 0) 686 clnt_pcreateerror("NFS Portmap"); 687 } else { 688 saddr.sin_port = 0; 689 pertry.tv_sec = 10; 690 pertry.tv_usec = 0; 691 if (mnttcp_ok && nfsargsp->sotype == SOCK_STREAM) 692 clp = clnttcp_create(&saddr, RPCPROG_MNT, mntvers, 693 &so, 0, 0); 694 else 695 clp = clntudp_create(&saddr, RPCPROG_MNT, mntvers, 696 pertry, &so); 697 if (clp == NULL) { 698 if ((opflags & ISBGRND) == 0) 699 clnt_pcreateerror("Cannot MNT RPC"); 700 } else { 701 clp->cl_auth = authunix_create_default(); 702 try.tv_sec = 10; 703 try.tv_usec = 0; 704 if (nfsargsp->flags & NFSMNT_KERB) 705 nfhret.auth = RPCAUTH_KERB4; 706 else 707 nfhret.auth = RPCAUTH_UNIX; 708 nfhret.vers = mntvers; 709 clnt_stat = clnt_call(clp, RPCMNT_MOUNT, 710 xdr_dir, spec, xdr_fh, &nfhret, try); 711 if (clnt_stat != RPC_SUCCESS) { 712 if (clnt_stat == RPC_PROGVERSMISMATCH) { 713 if (mountmode == ANY) { 714 mountmode = V2; 715 goto tryagain; 716 } else { 717 errx(1, "%s", 718 clnt_sperror(clp, "MNT RPC")); 719 } 720 } 721 if ((opflags & ISBGRND) == 0) 722 warnx("%s", clnt_sperror(clp, 723 "bad MNT RPC")); 724 } else { 725 auth_destroy(clp->cl_auth); 726 clnt_destroy(clp); 727 retrycnt = 0; 728 } 729 } 730 } 731 if (--retrycnt > 0) { 732 if (opflags & BGRND) { 733 opflags &= ~BGRND; 734 if (i = fork()) { 735 if (i == -1) 736 err(1, "nqnfs 2"); 737 exit(0); 738 } 739 (void) setsid(); 740 (void) close(STDIN_FILENO); 741 (void) close(STDOUT_FILENO); 742 (void) close(STDERR_FILENO); 743 (void) chdir("/"); 744 opflags |= ISBGRND; 745 } 746 sleep(60); 747 } 748 } 749 if (nfhret.stat) { 750 if (opflags & ISBGRND) 751 exit(1); 752 warnx("can't access %s: %s", spec, strerror(nfhret.stat)); 753 return (0); 754 } 755 saddr.sin_port = htons(tport); 756#ifdef ISO 757 if (isoflag) { 758 nfsargsp->addr = (struct sockaddr *) &isoaddr; 759 nfsargsp->addrlen = sizeof (isoaddr); 760 } else 761#endif /* ISO */ 762 { 763 nfsargsp->addr = (struct sockaddr *) &saddr; 764 nfsargsp->addrlen = sizeof (saddr); 765 } 766 nfsargsp->fh = nfhret.nfh; 767 nfsargsp->fhsize = nfhret.fhsize; 768 nfsargsp->hostname = nam; 769 return (1); 770} 771 772/* 773 * xdr routines for mount rpc's 774 */ 775int 776xdr_dir(xdrsp, dirp) 777 XDR *xdrsp; 778 char *dirp; 779{ 780 return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN)); 781} 782 783int 784xdr_fh(xdrsp, np) 785 XDR *xdrsp; 786 register struct nfhret *np; 787{ 788 register int i; 789 long auth, authcnt, authfnd = 0; 790 791 if (!xdr_u_long(xdrsp, &np->stat)) 792 return (0); 793 if (np->stat) 794 return (1); 795 switch (np->vers) { 796 case 1: 797 np->fhsize = NFSX_V2FH; 798 return (xdr_opaque(xdrsp, (caddr_t)np->nfh, NFSX_V2FH)); 799 case 3: 800 if (!xdr_long(xdrsp, &np->fhsize)) 801 return (0); 802 if (np->fhsize <= 0 || np->fhsize > NFSX_V3FHMAX) 803 return (0); 804 if (!xdr_opaque(xdrsp, (caddr_t)np->nfh, np->fhsize)) 805 return (0); 806 if (!xdr_long(xdrsp, &authcnt)) 807 return (0); 808 for (i = 0; i < authcnt; i++) { 809 if (!xdr_long(xdrsp, &auth)) 810 return (0); 811 if (auth == np->auth) 812 authfnd++; 813 } 814 /* 815 * Some servers, such as DEC's OSF/1 return a nil authenticator 816 * list to indicate RPCAUTH_UNIX. 817 */ 818 if (!authfnd && (authcnt > 0 || np->auth != RPCAUTH_UNIX)) 819 np->stat = EAUTH; 820 return (1); 821 }; 822 return (0); 823} 824 825void 826usage() 827{ 828 (void)fprintf(stderr, "\ 829usage: mount_nfs [-23KPTUbcdilqs] [-D deadthresh] [-I readdirsize]\n\ 830 [-L leaseterm] [-R retrycnt] [-a maxreadahead] [-g maxgroups]\n\ 831 [-m realm] [-o options] [-r readsize] [-t timeout] [-w writesize]\n\ 832 [-x retrans] rhost:path node\n"); 833 exit(1); 834} 835