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