mount_nfs.c revision 25348
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.21 1997/04/18 16:23:10 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 571/* 572 * Return RPC_SUCCESS if server responds. 573 */ 574enum clnt_stat 575pingnfsserver(addr, version, sotype) 576 struct sockaddr_in *addr; 577 int version; 578 int sotype; 579{ 580 struct sockaddr_in sin; 581 int tport; 582 CLIENT *clp; 583 int so = RPC_ANYSOCK; 584 enum clnt_stat stat; 585 struct timeval pertry, try; 586 587 sin = *addr; 588 589 if ((tport = port_no ? port_no : 590 pmap_getport(&sin, RPCPROG_NFS, version, nfsproto)) == 0) { 591 return rpc_createerr.cf_stat; 592 } 593 594 sin.sin_port = htons(tport); 595 596 pertry.tv_sec = 10; 597 pertry.tv_usec = 0; 598 if (sotype == SOCK_STREAM) 599 clp = clnttcp_create(&sin, RPCPROG_NFS, version, 600 &so, 0, 0); 601 else 602 clp = clntudp_create(&sin, RPCPROG_NFS, version, 603 pertry, &so); 604 if (clp == NULL) 605 return rpc_createerr.cf_stat; 606 607 try.tv_sec = 10; 608 try.tv_usec = 0; 609 stat = clnt_call(clp, NFSPROC_NULL, 610 xdr_void, NULL, xdr_void, NULL, try); 611 612 clnt_destroy(clp); 613 614 return stat; 615} 616 617int 618getnfsargs(spec, nfsargsp) 619 char *spec; 620 struct nfs_args *nfsargsp; 621{ 622 register CLIENT *clp; 623 struct hostent *hp; 624 static struct sockaddr_in saddr; 625#ifdef ISO 626 static struct sockaddr_iso isoaddr; 627 struct iso_addr *isop; 628 int isoflag = 0; 629#endif 630 struct timeval pertry, try; 631 enum clnt_stat clnt_stat; 632 int so = RPC_ANYSOCK, i, nfsvers, mntvers, orgcnt; 633 char *hostp, *delimp; 634#ifdef NFSKERB 635 char *cp; 636#endif 637 u_short tport; 638 static struct nfhret nfhret; 639 static char nam[MNAMELEN + 1]; 640 641 strncpy(nam, spec, MNAMELEN); 642 nam[MNAMELEN] = '\0'; 643 if ((delimp = strchr(spec, '@')) != NULL) { 644 hostp = delimp + 1; 645 } else if ((delimp = strchr(spec, ':')) != NULL) { 646 hostp = spec; 647 spec = delimp + 1; 648 } else { 649 warnx("no <host>:<dirpath> or <dirpath>@<host> spec"); 650 return (0); 651 } 652 *delimp = '\0'; 653 /* 654 * DUMB!! Until the mount protocol works on iso transport, we must 655 * supply both an iso and an inet address for the host. 656 */ 657#ifdef ISO 658 if (!strncmp(hostp, "iso=", 4)) { 659 u_short isoport; 660 661 hostp += 4; 662 isoflag++; 663 if ((delimp = strchr(hostp, '+')) == NULL) { 664 warnx("no iso+inet address"); 665 return (0); 666 } 667 *delimp = '\0'; 668 if ((isop = iso_addr(hostp)) == NULL) { 669 warnx("bad ISO address"); 670 return (0); 671 } 672 memset(&isoaddr, 0, sizeof (isoaddr)); 673 memmove(&isoaddr.siso_addr, isop, sizeof (struct iso_addr)); 674 isoaddr.siso_len = sizeof (isoaddr); 675 isoaddr.siso_family = AF_ISO; 676 isoaddr.siso_tlen = 2; 677 isoport = htons(NFS_PORT); 678 memmove(TSEL(&isoaddr), &isoport, isoaddr.siso_tlen); 679 hostp = delimp + 1; 680 } 681#endif /* ISO */ 682 683 /* 684 * Handle an internet host address and reverse resolve it if 685 * doing Kerberos. 686 */ 687 if (isdigit(*hostp)) { 688 if ((saddr.sin_addr.s_addr = inet_addr(hostp)) == -1) { 689 warnx("bad net address %s", hostp); 690 return (0); 691 } 692 } else if ((hp = gethostbyname(hostp)) != NULL) 693 memmove(&saddr.sin_addr, hp->h_addr, hp->h_length); 694 else { 695 warnx("can't get net id for host"); 696 return (0); 697 } 698#ifdef NFSKERB 699 if ((nfsargsp->flags & NFSMNT_KERB)) { 700 if ((hp = gethostbyaddr((char *)&saddr.sin_addr.s_addr, 701 sizeof (u_long), AF_INET)) == (struct hostent *)0) { 702 warnx("can't reverse resolve net address"); 703 return (0); 704 } 705 memmove(&saddr.sin_addr, hp->h_addr, hp->h_length); 706 strncpy(inst, hp->h_name, INST_SZ); 707 inst[INST_SZ - 1] = '\0'; 708 if (cp = strchr(inst, '.')) 709 *cp = '\0'; 710 } 711#endif /* NFSKERB */ 712 713 orgcnt = retrycnt; 714tryagain: 715 if (mountmode == ANY || mountmode == V3) { 716 nfsvers = 3; 717 mntvers = 3; 718 nfsargsp->flags |= NFSMNT_NFSV3; 719 } else { 720 nfsvers = 2; 721 mntvers = 1; 722 nfsargsp->flags &= ~NFSMNT_NFSV3; 723 } 724 nfhret.stat = EACCES; /* Mark not yet successful */ 725 while (retrycnt > 0) { 726 saddr.sin_family = AF_INET; 727 saddr.sin_port = htons(PMAPPORT); 728 if ((tport = port_no ? port_no : 729 pmap_getport(&saddr, RPCPROG_NFS, 730 nfsvers, nfsproto)) == 0) { 731 if ((opflags & ISBGRND) == 0) 732 clnt_pcreateerror("NFS Portmap"); 733 } else { 734 /* 735 * First ping the nfs server to see if it supports 736 * the version of the protocol we want to use. 737 */ 738 clnt_stat = pingnfsserver(&saddr, nfsvers, 739 nfsargsp->sotype); 740 if (clnt_stat == RPC_PROGVERSMISMATCH) { 741 if (mountmode == ANY) { 742 mountmode = V2; 743 goto tryagain; 744 } else { 745 errx(1, "Can't contact NFS server"); 746 } 747 } 748 saddr.sin_port = 0; 749 pertry.tv_sec = 10; 750 pertry.tv_usec = 0; 751 if (mnttcp_ok && nfsargsp->sotype == SOCK_STREAM) 752 clp = clnttcp_create(&saddr, RPCPROG_MNT, mntvers, 753 &so, 0, 0); 754 else 755 clp = clntudp_create(&saddr, RPCPROG_MNT, mntvers, 756 pertry, &so); 757 if (clp == NULL) { 758 if ((opflags & ISBGRND) == 0) 759 clnt_pcreateerror("Cannot MNT RPC"); 760 } else { 761 clp->cl_auth = authunix_create_default(); 762 try.tv_sec = 10; 763 try.tv_usec = 0; 764 if (nfsargsp->flags & NFSMNT_KERB) 765 nfhret.auth = RPCAUTH_KERB4; 766 else 767 nfhret.auth = RPCAUTH_UNIX; 768 nfhret.vers = mntvers; 769 clnt_stat = clnt_call(clp, RPCMNT_MOUNT, 770 xdr_dir, spec, xdr_fh, &nfhret, try); 771 if (clnt_stat != RPC_SUCCESS) { 772 if (clnt_stat == RPC_PROGVERSMISMATCH) { 773 if (mountmode == ANY) { 774 mountmode = V2; 775 goto tryagain; 776 } else { 777 errx(1, "%s", 778 clnt_sperror(clp, "MNT RPC")); 779 } 780 } 781 if ((opflags & ISBGRND) == 0) 782 warnx("%s", clnt_sperror(clp, 783 "bad MNT RPC")); 784 } else { 785 auth_destroy(clp->cl_auth); 786 clnt_destroy(clp); 787 retrycnt = 0; 788 } 789 } 790 } 791 if (--retrycnt > 0) { 792 if (opflags & BGRND) { 793 opflags &= ~BGRND; 794 if (i = fork()) { 795 if (i == -1) 796 err(1, "nqnfs 2"); 797 exit(0); 798 } 799 (void) setsid(); 800 (void) close(STDIN_FILENO); 801 (void) close(STDOUT_FILENO); 802 (void) close(STDERR_FILENO); 803 (void) chdir("/"); 804 opflags |= ISBGRND; 805 } 806 sleep(60); 807 } 808 } 809 if (nfhret.stat) { 810 if (opflags & ISBGRND) 811 exit(1); 812 warnx("can't access %s: %s", spec, strerror(nfhret.stat)); 813 return (0); 814 } 815 saddr.sin_port = htons(tport); 816#ifdef ISO 817 if (isoflag) { 818 nfsargsp->addr = (struct sockaddr *) &isoaddr; 819 nfsargsp->addrlen = sizeof (isoaddr); 820 } else 821#endif /* ISO */ 822 { 823 nfsargsp->addr = (struct sockaddr *) &saddr; 824 nfsargsp->addrlen = sizeof (saddr); 825 } 826 nfsargsp->fh = nfhret.nfh; 827 nfsargsp->fhsize = nfhret.fhsize; 828 nfsargsp->hostname = nam; 829 return (1); 830} 831 832/* 833 * xdr routines for mount rpc's 834 */ 835int 836xdr_dir(xdrsp, dirp) 837 XDR *xdrsp; 838 char *dirp; 839{ 840 return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN)); 841} 842 843int 844xdr_fh(xdrsp, np) 845 XDR *xdrsp; 846 register struct nfhret *np; 847{ 848 register int i; 849 long auth, authcnt, authfnd = 0; 850 851 if (!xdr_u_long(xdrsp, &np->stat)) 852 return (0); 853 if (np->stat) 854 return (1); 855 switch (np->vers) { 856 case 1: 857 np->fhsize = NFSX_V2FH; 858 return (xdr_opaque(xdrsp, (caddr_t)np->nfh, NFSX_V2FH)); 859 case 3: 860 if (!xdr_long(xdrsp, &np->fhsize)) 861 return (0); 862 if (np->fhsize <= 0 || np->fhsize > NFSX_V3FHMAX) 863 return (0); 864 if (!xdr_opaque(xdrsp, (caddr_t)np->nfh, np->fhsize)) 865 return (0); 866 if (!xdr_long(xdrsp, &authcnt)) 867 return (0); 868 for (i = 0; i < authcnt; i++) { 869 if (!xdr_long(xdrsp, &auth)) 870 return (0); 871 if (auth == np->auth) 872 authfnd++; 873 } 874 /* 875 * Some servers, such as DEC's OSF/1 return a nil authenticator 876 * list to indicate RPCAUTH_UNIX. 877 */ 878 if (!authfnd && (authcnt > 0 || np->auth != RPCAUTH_UNIX)) 879 np->stat = EAUTH; 880 return (1); 881 }; 882 return (0); 883} 884 885void 886usage() 887{ 888 (void)fprintf(stderr, "\ 889usage: mount_nfs [-23KPTUbcdilqs] [-D deadthresh] [-I readdirsize]\n\ 890 [-L leaseterm] [-R retrycnt] [-a maxreadahead] [-g maxgroups]\n\ 891 [-m realm] [-o options] [-r readsize] [-t timeout] [-w writesize]\n\ 892 [-x retrans] rhost:path node\n"); 893 exit(1); 894} 895