mount_nfs.c revision 74462
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 const 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#if 0 45static char sccsid[] = "@(#)mount_nfs.c 8.11 (Berkeley) 5/4/95"; 46#endif 47static const char rcsid[] = 48 "$FreeBSD: head/sbin/mount_nfs/mount_nfs.c 74462 2001-03-19 12:50:13Z alfred $"; 49#endif /* not lint */ 50 51#include <sys/param.h> 52#include <sys/mount.h> 53#include <sys/socket.h> 54#include <sys/stat.h> 55#include <sys/syslog.h> 56 57#include <rpc/rpc.h> 58#include <rpc/pmap_clnt.h> 59#include <rpc/pmap_prot.h> 60 61#ifdef NFSKERB 62#include <kerberosIV/des.h> 63#include <kerberosIV/krb.h> 64#endif 65 66#include <nfs/rpcv2.h> 67#include <nfs/nfsproto.h> 68#include <nfs/nfs.h> 69#include <nfs/nqnfs.h> 70 71#include <arpa/inet.h> 72 73#include <ctype.h> 74#include <err.h> 75#include <errno.h> 76#include <fcntl.h> 77#include <netdb.h> 78#include <stdio.h> 79#include <stdlib.h> 80#include <strings.h> 81#include <sysexits.h> 82#include <unistd.h> 83 84#include "mntopts.h" 85#include "mounttab.h" 86 87#define ALTF_BG 0x1 88#define ALTF_NOCONN 0x2 89#define ALTF_DUMBTIMR 0x4 90#define ALTF_INTR 0x8 91#define ALTF_KERB 0x10 92#define ALTF_NFSV3 0x20 93#define ALTF_RDIRPLUS 0x40 94#define ALTF_MNTUDP 0x80 95#define ALTF_RESVPORT 0x100 96#define ALTF_SEQPACKET 0x200 97#define ALTF_NQNFS 0x400 98#define ALTF_SOFT 0x800 99#define ALTF_TCP 0x1000 100#define ALTF_PORT 0x2000 101#define ALTF_NFSV2 0x4000 102#define ALTF_ACREGMIN 0x8000 103#define ALTF_ACREGMAX 0x10000 104#define ALTF_ACDIRMIN 0x20000 105#define ALTF_ACDIRMAX 0x40000 106 107struct mntopt mopts[] = { 108 MOPT_STDOPTS, 109 MOPT_FORCE, 110 MOPT_UPDATE, 111 MOPT_ASYNC, 112 { "bg", 0, ALTF_BG, 1 }, 113 { "conn", 1, ALTF_NOCONN, 1 }, 114 { "dumbtimer", 0, ALTF_DUMBTIMR, 1 }, 115 { "intr", 0, ALTF_INTR, 1 }, 116#ifdef NFSKERB 117 { "kerb", 0, ALTF_KERB, 1 }, 118#endif 119 { "nfsv3", 0, ALTF_NFSV3, 1 }, 120 { "rdirplus", 0, ALTF_RDIRPLUS, 1 }, 121 { "mntudp", 0, ALTF_MNTUDP, 1 }, 122 { "resvport", 0, ALTF_RESVPORT, 1 }, 123 { "nqnfs", 0, ALTF_NQNFS, 1 }, 124 { "soft", 0, ALTF_SOFT, 1 }, 125 { "tcp", 0, ALTF_TCP, 1 }, 126 { "port=", 0, ALTF_PORT, 1 }, 127 { "nfsv2", 0, ALTF_NFSV2, 1 }, 128 { "acregmin=", 0, ALTF_ACREGMIN, 1 }, 129 { "acregmax=", 0, ALTF_ACREGMAX, 1 }, 130 { "acdirmin=", 0, ALTF_ACDIRMIN, 1 }, 131 { "acdirmax=", 0, ALTF_ACDIRMAX, 1 }, 132 { NULL } 133}; 134 135struct nfs_args nfsdefargs = { 136 NFS_ARGSVERSION, 137 (struct sockaddr *)0, 138 sizeof (struct sockaddr_in), 139 SOCK_DGRAM, 140 0, 141 (u_char *)0, 142 0, 143 NFSMNT_RESVPORT, 144 NFS_WSIZE, 145 NFS_RSIZE, 146 NFS_READDIRSIZE, 147 10, 148 NFS_RETRANS, 149 NFS_MAXGRPS, 150 NFS_DEFRAHEAD, 151 NQ_DEFLEASE, 152 NQ_DEADTHRESH, 153 (char *)0, 154 /* args version 4 */ 155 NFS_MINATTRTIMO, 156 NFS_MAXATTRTIMO, 157 NFS_MINDIRATTRTIMO, 158 NFS_MAXDIRATTRTIMO, 159}; 160 161struct nfhret { 162 u_long stat; 163 long vers; 164 long auth; 165 long fhsize; 166 u_char nfh[NFSX_V3FHMAX]; 167}; 168#define DEF_RETRY 10000 169#define BGRND 1 170#define ISBGRND 2 171int retrycnt = DEF_RETRY; 172int opflags = 0; 173int nfsproto = IPPROTO_UDP; 174int mnttcp_ok = 1; 175u_short port_no = 0; 176enum { 177 ANY, 178 V2, 179 V3 180} mountmode = ANY; 181 182#ifdef NFSKERB 183char inst[INST_SZ]; 184char realm[REALM_SZ]; 185struct { 186 u_long kind; 187 KTEXT_ST kt; 188} ktick; 189struct nfsrpc_nickverf kverf; 190struct nfsrpc_fullblock kin, kout; 191NFSKERBKEY_T kivec; 192CREDENTIALS kcr; 193struct timeval ktv; 194NFSKERBKEYSCHED_T kerb_keysched; 195#endif 196 197int getnfsargs __P((char *, struct nfs_args *)); 198/* void 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 * initialize 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 210set_flags(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 char mntpath[MAXPATHLEN]; 253 struct vfsconf vfc; 254 int error = 0; 255#ifdef NFSKERB 256 uid_t last_ruid; 257 258 last_ruid = -1; 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"); 265#endif /* NFSKERB */ 266 retrycnt = DEF_RETRY; 267 268 mntflags = 0; 269 altflags = 0; 270 nfsargs = nfsdefargs; 271 nfsargsp = &nfsargs; 272 while ((c = getopt(argc, argv, 273 "23a:bcdD:g:I:iKL:lm:No:PpqR:r:sTt:w:x:U")) != -1) 274 switch (c) { 275 case '2': 276 mountmode = V2; 277 break; 278 case '3': 279 mountmode = V3; 280 break; 281 case 'a': 282 num = strtol(optarg, &p, 10); 283 if (*p || num < 0) 284 errx(1, "illegal -a value -- %s", optarg); 285 nfsargsp->readahead = num; 286 nfsargsp->flags |= NFSMNT_READAHEAD; 287 break; 288 case 'b': 289 opflags |= BGRND; 290 break; 291 case 'c': 292 nfsargsp->flags |= NFSMNT_NOCONN; 293 break; 294 case 'D': 295 num = strtol(optarg, &p, 10); 296 if (*p || num <= 0) 297 errx(1, "illegal -D value -- %s", optarg); 298 nfsargsp->deadthresh = num; 299 nfsargsp->flags |= NFSMNT_DEADTHRESH; 300 break; 301 case 'd': 302 nfsargsp->flags |= NFSMNT_DUMBTIMR; 303 break; 304#if 0 /* XXXX */ 305 case 'g': 306 num = strtol(optarg, &p, 10); 307 if (*p || num <= 0) 308 errx(1, "illegal -g value -- %s", optarg); 309 set_rpc_maxgrouplist(num); 310 nfsargsp->maxgrouplist = num; 311 nfsargsp->flags |= NFSMNT_MAXGRPS; 312 break; 313#endif 314 case 'I': 315 num = strtol(optarg, &p, 10); 316 if (*p || num <= 0) 317 errx(1, "illegal -I value -- %s", optarg); 318 nfsargsp->readdirsize = num; 319 nfsargsp->flags |= NFSMNT_READDIRSIZE; 320 break; 321 case 'i': 322 nfsargsp->flags |= NFSMNT_INT; 323 break; 324#ifdef NFSKERB 325 case 'K': 326 nfsargsp->flags |= NFSMNT_KERB; 327 break; 328#endif 329 case 'L': 330 num = strtol(optarg, &p, 10); 331 if (*p || num < 2) 332 errx(1, "illegal -L value -- %s", optarg); 333 nfsargsp->leaseterm = num; 334 nfsargsp->flags |= NFSMNT_LEASETERM; 335 break; 336 case 'l': 337 nfsargsp->flags |= NFSMNT_RDIRPLUS; 338 break; 339#ifdef NFSKERB 340 case 'm': 341 (void)strncpy(realm, optarg, REALM_SZ - 1); 342 realm[REALM_SZ - 1] = '\0'; 343 break; 344#endif 345 case 'N': 346 nfsargsp->flags &= ~NFSMNT_RESVPORT; 347 break; 348 case 'o': 349 altflags = 0; 350 set_flags(&altflags, &nfsargsp->flags, TRUE); 351 if (mountmode == V2) 352 altflags |= ALTF_NFSV2; 353 else if (mountmode == V3) 354 altflags |= ALTF_NFSV3; 355 getmntopts(optarg, mopts, &mntflags, &altflags); 356 set_flags(&altflags, &nfsargsp->flags, FALSE); 357 /* 358 * Handle altflags which don't map directly to 359 * mount flags. 360 */ 361 if(altflags & ALTF_BG) 362 opflags |= BGRND; 363 if(altflags & ALTF_MNTUDP) 364 mnttcp_ok = 0; 365 if(altflags & ALTF_TCP) { 366 nfsargsp->sotype = SOCK_STREAM; 367 nfsproto = IPPROTO_TCP; 368 } 369 if(altflags & ALTF_PORT) 370 port_no = atoi(strstr(optarg, "port=") + 5); 371 mountmode = ANY; 372 if(altflags & ALTF_NFSV2) 373 mountmode = V2; 374 if(altflags & ALTF_NFSV3) 375 mountmode = V3; 376 if(altflags & ALTF_ACREGMIN) 377 nfsargsp->acregmin = atoi(strstr(optarg, 378 "acregmin=") + 9); 379 if(altflags & ALTF_ACREGMAX) 380 nfsargsp->acregmax = atoi(strstr(optarg, 381 "acregmax=") + 9); 382 if(altflags & ALTF_ACDIRMIN) 383 nfsargsp->acdirmin = atoi(strstr(optarg, 384 "acdirmin=") + 9); 385 if(altflags & ALTF_ACDIRMAX) 386 nfsargsp->acdirmax = atoi(strstr(optarg, 387 "acdirmax=") + 9); 388 break; 389 case 'P': 390 /* obsolete for NFSMNT_RESVPORT, now default */ 391 break; 392 case 'q': 393 mountmode = V3; 394 nfsargsp->flags |= NFSMNT_NQNFS; 395 break; 396 case 'R': 397 num = strtol(optarg, &p, 10); 398 if (*p || num <= 0) 399 errx(1, "illegal -R value -- %s", optarg); 400 retrycnt = num; 401 break; 402 case 'r': 403 num = strtol(optarg, &p, 10); 404 if (*p || num <= 0) 405 errx(1, "illegal -r value -- %s", optarg); 406 nfsargsp->rsize = num; 407 nfsargsp->flags |= NFSMNT_RSIZE; 408 break; 409 case 's': 410 nfsargsp->flags |= NFSMNT_SOFT; 411 break; 412 case 'T': 413 nfsargsp->sotype = SOCK_STREAM; 414 nfsproto = IPPROTO_TCP; 415 break; 416 case 't': 417 num = strtol(optarg, &p, 10); 418 if (*p || num <= 0) 419 errx(1, "illegal -t value -- %s", optarg); 420 nfsargsp->timeo = num; 421 nfsargsp->flags |= NFSMNT_TIMEO; 422 break; 423 case 'w': 424 num = strtol(optarg, &p, 10); 425 if (*p || num <= 0) 426 errx(1, "illegal -w value -- %s", optarg); 427 nfsargsp->wsize = num; 428 nfsargsp->flags |= NFSMNT_WSIZE; 429 break; 430 case 'x': 431 num = strtol(optarg, &p, 10); 432 if (*p || num <= 0) 433 errx(1, "illegal -x value -- %s", optarg); 434 nfsargsp->retrans = num; 435 nfsargsp->flags |= NFSMNT_RETRANS; 436 break; 437 case 'U': 438 mnttcp_ok = 0; 439 break; 440 default: 441 usage(); 442 break; 443 } 444 argc -= optind; 445 argv += optind; 446 447 if (argc != 2) { 448 usage(); 449 /* NOTREACHED */ 450 } 451 452 spec = *argv++; 453 name = *argv; 454 455 if (!getnfsargs(spec, nfsargsp)) 456 exit(1); 457 458 /* resolve the mountpoint with realpath(3) */ 459 (void)checkpath(name, mntpath); 460 461 error = getvfsbyname("nfs", &vfc); 462 if (error && vfsisloadable("nfs")) { 463 if(vfsload("nfs")) 464 err(EX_OSERR, "vfsload(nfs)"); 465 endvfsent(); /* clear cache */ 466 error = getvfsbyname("nfs", &vfc); 467 } 468 if (error) 469 errx(EX_OSERR, "nfs filesystem is not available"); 470 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 ((i = fork())) { 476 if (i == -1) 477 err(1, "nqnfs 1"); 478 exit(0); 479 } 480 (void) setsid(); 481 (void) close(STDIN_FILENO); 482 (void) close(STDOUT_FILENO); 483 (void) close(STDERR_FILENO); 484 (void) chdir("/"); 485 } 486 openlog("mount_nfs:", LOG_PID, LOG_DAEMON); 487 nfssvc_flag = NFSSVC_MNTD; 488 ncd.ncd_dirp = mntpath; 489 while (nfssvc(nfssvc_flag, (caddr_t)&ncd) < 0) { 490 if (errno != ENEEDAUTH) { 491 syslog(LOG_ERR, "nfssvc err %m"); 492 continue; 493 } 494 nfssvc_flag = 495 NFSSVC_MNTD | NFSSVC_GOTAUTH | NFSSVC_AUTHINFAIL; 496#ifdef NFSKERB 497 /* 498 * Set up as ncd_authuid for the kerberos call. 499 * Must set ruid to ncd_authuid and reset the 500 * ticket name iff ncd_authuid is not the same 501 * as last time, so that the right ticket file 502 * is found. 503 * Get the Kerberos credential structure so that 504 * we have the session key and get a ticket for 505 * this uid. 506 * For more info see the IETF Draft "Authentication 507 * in ONC RPC". 508 */ 509 if (ncd.ncd_authuid != last_ruid) { 510 char buf[512]; 511 (void)sprintf(buf, "%s%d", 512 TKT_ROOT, ncd.ncd_authuid); 513 krb_set_tkt_string(buf); 514 last_ruid = ncd.ncd_authuid; 515 } 516 setreuid(ncd.ncd_authuid, 0); 517 kret = krb_get_cred(NFS_KERBSRV, inst, realm, &kcr); 518 if (kret == RET_NOTKT) { 519 kret = get_ad_tkt(NFS_KERBSRV, inst, realm, 520 DEFAULT_TKT_LIFE); 521 if (kret == KSUCCESS) 522 kret = krb_get_cred(NFS_KERBSRV, inst, realm, 523 &kcr); 524 } 525 if (kret == KSUCCESS) 526 kret = krb_mk_req(&ktick.kt, NFS_KERBSRV, inst, 527 realm, 0); 528 529 /* 530 * Fill in the AKN_FULLNAME authenticator and verifier. 531 * Along with the Kerberos ticket, we need to build 532 * the timestamp verifier and encrypt it in CBC mode. 533 */ 534 if (kret == KSUCCESS && 535 ktick.kt.length <= (RPCAUTH_MAXSIZ-3*NFSX_UNSIGNED) 536 && gettimeofday(&ktv, (struct timezone *)0) == 0) { 537 ncd.ncd_authtype = RPCAUTH_KERB4; 538 ncd.ncd_authstr = (u_char *)&ktick; 539 ncd.ncd_authlen = nfsm_rndup(ktick.kt.length) + 540 3 * NFSX_UNSIGNED; 541 ncd.ncd_verfstr = (u_char *)&kverf; 542 ncd.ncd_verflen = sizeof (kverf); 543 memmove(ncd.ncd_key, kcr.session, 544 sizeof (kcr.session)); 545 kin.t1 = htonl(ktv.tv_sec); 546 kin.t2 = htonl(ktv.tv_usec); 547 kin.w1 = htonl(NFS_KERBTTL); 548 kin.w2 = htonl(NFS_KERBTTL - 1); 549 bzero((caddr_t)kivec, sizeof (kivec)); 550 551 /* 552 * Encrypt kin in CBC mode using the session 553 * key in kcr. 554 */ 555 XXX 556 557 /* 558 * Finally, fill the timestamp verifier into the 559 * authenticator and verifier. 560 */ 561 ktick.kind = htonl(RPCAKN_FULLNAME); 562 kverf.kind = htonl(RPCAKN_FULLNAME); 563 NFS_KERBW1(ktick.kt) = kout.w1; 564 ktick.kt.length = htonl(ktick.kt.length); 565 kverf.verf.t1 = kout.t1; 566 kverf.verf.t2 = kout.t2; 567 kverf.verf.w2 = kout.w2; 568 nfssvc_flag = NFSSVC_MNTD | NFSSVC_GOTAUTH; 569 } 570 setreuid(0, 0); 571#endif /* NFSKERB */ 572 } 573 } 574 exit(0); 575} 576 577int 578getnfsargs(spec, nfsargsp) 579 char *spec; 580 struct nfs_args *nfsargsp; 581{ 582 CLIENT *clp; 583 struct addrinfo hints, *ai_nfs, *ai; 584 int ecode; 585 char host[NI_MAXHOST], serv[NI_MAXSERV]; 586 static struct netbuf nfs_nb; 587 static struct sockaddr_storage nfs_ss; 588 struct netconfig *nconf; 589 char *netid; 590 struct timeval pertry, try; 591 enum clnt_stat clnt_stat; 592 int so, i, nfsvers, mntvers, orgcnt, speclen; 593 char *hostp, *delimp; 594#ifdef NFSKERB 595 char *cp; 596#endif 597 size_t len; 598 static struct nfhret nfhret; 599 static char nam[MNAMELEN + 1]; 600 601 so = i = 0; 602 if ((delimp = strrchr(spec, ':')) != NULL) { 603 hostp = spec; 604 spec = delimp + 1; 605 } else if ((delimp = strrchr(spec, '@')) != NULL) { 606 warnx("path@server syntax is deprecated, use server:path"); 607 hostp = delimp + 1; 608 } else { 609 warnx("no <host>:<dirpath> nfs-name"); 610 return (0); 611 } 612 613 *delimp = '\0'; 614 615 /* 616 * If there has been a trailing slash at mounttime it seems 617 * that some mountd implementations fail to remove the mount 618 * entries from their mountlist while unmounting. 619 */ 620 for (speclen = strlen(spec); 621 speclen > 1 && spec[speclen - 1] == '/'; 622 speclen--) 623 spec[speclen - 1] = '\0'; 624 if (strlen(hostp) + strlen(spec) + 1 > MNAMELEN) { 625 warnx("%s:%s: %s", hostp, spec, strerror(ENAMETOOLONG)); 626 return (0); 627 } 628 /* Make both '@' and ':' notations equal */ 629 if (*hostp != '\0') { 630 len = strlen(hostp); 631 memmove(nam, hostp, len); 632 nam[len] = ':'; 633 memmove(nam + len + 1, spec, speclen); 634 nam[len + speclen + 1] = '\0'; 635 } 636 637 /* 638 * Handle an internet host address and reverse resolve it if 639 * doing Kerberos. 640 */ 641 memset(&hints, 0, sizeof hints); 642 hints.ai_flags = AI_NUMERICHOST; 643 hints.ai_socktype = nfsargsp->sotype; 644 if (getaddrinfo(hostp, "nfs", &hints, &ai_nfs) == 0) { 645 if ((nfsargsp->flags & NFSMNT_KERB)) { 646 hints.ai_flags = 0; 647 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, host, 648 sizeof host, serv, sizeof serv, 0) != 0) { 649 warnx("can't reverse resolve net address"); 650 return (0); 651 } 652 hostp = host; 653 } 654 } else { 655 hints.ai_flags = 0; 656 if ((ecode = getaddrinfo(hostp, "nfs", &hints, &ai_nfs)) != 0) { 657 warnx("can't get net id for host/nfs: %s", 658 gai_strerror(ecode)); 659 return (0); 660 } 661 } 662#ifdef NFSKERB 663 if (nfsargsp->flags & NFSMNT_KERB) { 664 strncpy(inst, hp->h_name, INST_SZ); 665 inst[INST_SZ - 1] = '\0'; 666 if (cp = strchr(inst, '.')) 667 *cp = '\0'; 668 } 669#endif /* NFSKERB */ 670 671 orgcnt = retrycnt; 672 673 if (mountmode == ANY || mountmode == V3) { 674 nfsvers = 3; 675 mntvers = 3; 676 nfsargsp->flags |= NFSMNT_NFSV3; 677 } else { 678 nfsvers = 2; 679 mntvers = 1; 680 nfsargsp->flags &= ~NFSMNT_NFSV3; 681 } 682 nfhret.stat = EACCES; /* Mark not yet successful */ 683 ai = ai_nfs; 684 while (ai != NULL) { 685 /* 686 * XXX. Nead a generic (family, type, proto) -> nconf interface. 687 * __rpc_*2nconf exist, maybe they should be exported. 688 */ 689 if (nfsargsp->sotype == SOCK_STREAM) { 690 if (ai->ai_family == AF_INET6) 691 netid = "tcp6"; 692 else 693 netid = "tcp"; 694 } else { 695 if (ai->ai_family == AF_INET6) 696 netid = "udp6"; 697 else 698 netid = "udp"; 699 } 700 701 nconf = getnetconfigent(netid); 702 703tryagain: 704 retrycnt = orgcnt; 705 706 while (retrycnt > 0) { 707 nfs_nb.buf = &nfs_ss; 708 nfs_nb.maxlen = sizeof nfs_ss; 709 if (!rpcb_getaddr(RPCPROG_NFS, nfsvers, nconf, 710 &nfs_nb, hostp)){ 711 if (rpc_createerr.cf_stat == RPC_SYSTEMERROR) { 712 nfhret.stat = rpc_createerr.cf_error.re_errno; 713 break; 714 } 715 if (rpc_createerr.cf_stat == RPC_UNKNOWNPROTO) { 716 nfhret.stat = EPROTONOSUPPORT; 717 break; 718 } 719 if ((opflags & ISBGRND) == 0) 720 clnt_pcreateerror( 721 "mount_nfs: rpcbind on server"); 722 } else { 723 pertry.tv_sec = 10; 724 pertry.tv_usec = 0; 725 /* 726 * XXX relies on clnt_tcp_create to bind 727 * to a reserved socket. 728 */ 729 clp = clnt_tp_create(hostp, RPCPROG_MNT, mntvers, 730 mnttcp_ok ? nconf : getnetconfigent("udp")); 731 if (clp == NULL) { 732 if ((opflags & ISBGRND) == 0) 733 clnt_pcreateerror("Cannot MNT RPC"); 734 } else { 735 CLNT_CONTROL(clp, CLSET_RETRY_TIMEOUT, 736 (char *)&pertry); 737 clp->cl_auth = authsys_create_default(); 738 try.tv_sec = 10; 739 try.tv_usec = 0; 740 if (nfsargsp->flags & NFSMNT_KERB) 741 nfhret.auth = RPCAUTH_KERB4; 742 else 743 nfhret.auth = RPCAUTH_UNIX; 744 nfhret.vers = mntvers; 745 clnt_stat = clnt_call(clp, RPCMNT_MOUNT, 746 xdr_dir, spec, xdr_fh, &nfhret, try); 747 if (clnt_stat != RPC_SUCCESS) { 748 if (clnt_stat == RPC_PROGVERSMISMATCH) { 749 if (mountmode == ANY) { 750 mountmode = V2; 751 goto tryagain; 752 } else { 753 errx(1, "%s", 754 clnt_sperror(clp, "MNT RPC")); 755 } 756 } 757 if ((opflags & ISBGRND) == 0) 758 warnx("%s", clnt_sperror(clp, 759 "bad MNT RPC")); 760 } else { 761 retrycnt = 0; 762 } 763 auth_destroy(clp->cl_auth); 764 clnt_destroy(clp); 765 so = RPC_ANYSOCK; 766 } 767 } 768 if (--retrycnt > 0) { 769 if (opflags & BGRND) { 770 warnx("Cannot immediately mount %s:%s, " 771 "backgrounding", hostp, spec); 772 opflags &= ~BGRND; 773 if ((i = fork())) { 774 if (i == -1) 775 err(1, "nqnfs 2"); 776 exit(0); 777 } 778 (void) setsid(); 779 (void) close(STDIN_FILENO); 780 (void) close(STDOUT_FILENO); 781 (void) close(STDERR_FILENO); 782 (void) chdir("/"); 783 opflags |= ISBGRND; 784 } 785 sleep(60); 786 } 787 } 788 if (nfhret.stat == 0) 789 break; 790 ai = ai->ai_next; 791 } 792 freeaddrinfo(ai_nfs); 793 if (nfhret.stat) { 794 if (opflags & ISBGRND) 795 exit(1); 796 warnx("can't access %s: %s", spec, strerror(nfhret.stat)); 797 return (0); 798 } 799 { 800 nfsargsp->addr = (struct sockaddr *) nfs_nb.buf; 801 nfsargsp->addrlen = nfs_nb.len; 802 } 803 nfsargsp->fh = nfhret.nfh; 804 nfsargsp->fhsize = nfhret.fhsize; 805 nfsargsp->hostname = nam; 806 /* Add mounted filesystem to PATH_MOUNTTAB */ 807 if (!add_mtab(hostp, spec)) 808 warnx("can't update %s for %s:%s", PATH_MOUNTTAB, hostp, spec); 809 return (1); 810} 811 812/* 813 * xdr routines for mount rpc's 814 */ 815int 816xdr_dir(xdrsp, dirp) 817 XDR *xdrsp; 818 char *dirp; 819{ 820 return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN)); 821} 822 823int 824xdr_fh(xdrsp, np) 825 XDR *xdrsp; 826 register struct nfhret *np; 827{ 828 register int i; 829 long auth, authcnt, authfnd = 0; 830 831 if (!xdr_u_long(xdrsp, &np->stat)) 832 return (0); 833 if (np->stat) 834 return (1); 835 switch (np->vers) { 836 case 1: 837 np->fhsize = NFSX_V2FH; 838 return (xdr_opaque(xdrsp, (caddr_t)np->nfh, NFSX_V2FH)); 839 case 3: 840 if (!xdr_long(xdrsp, &np->fhsize)) 841 return (0); 842 if (np->fhsize <= 0 || np->fhsize > NFSX_V3FHMAX) 843 return (0); 844 if (!xdr_opaque(xdrsp, (caddr_t)np->nfh, np->fhsize)) 845 return (0); 846 if (!xdr_long(xdrsp, &authcnt)) 847 return (0); 848 for (i = 0; i < authcnt; i++) { 849 if (!xdr_long(xdrsp, &auth)) 850 return (0); 851 if (auth == np->auth) 852 authfnd++; 853 } 854 /* 855 * Some servers, such as DEC's OSF/1 return a nil authenticator 856 * list to indicate RPCAUTH_UNIX. 857 */ 858 if (!authfnd && (authcnt > 0 || np->auth != RPCAUTH_UNIX)) 859 np->stat = EAUTH; 860 return (1); 861 }; 862 return (0); 863} 864 865void 866usage() 867{ 868 (void)fprintf(stderr, "%s\n%s\n%s\n%s\n", 869"usage: mount_nfs [-23KNPTUbcdilqs] [-D deadthresh] [-I readdirsize]", 870" [-L leaseterm] [-R retrycnt] [-a maxreadahead]", 871" [-g maxgroups] [-m realm] [-o options] [-r readsize]", 872" [-t timeout] [-w writesize] [-x retrans] rhost:path node"); 873 exit(1); 874} 875