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 38 static 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 45 static char sccsid[] = "@(#)mount_nfs.c 8.11 (Berkeley) 5/4/95"; 46 #endif 47 static const char rcsid[] = 48 "$FreeBSD$"; 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 #include <nfs/rpcv2.h> 62 #include <nfs/nfsproto.h> 63 #include <nfsclient/nfs.h> 64 #include <nfsclient/nfsargs.h> 65 66 #include <arpa/inet.h> 67 68 #include <ctype.h> 69 #include <err.h> 70 #include <errno.h> 71 #include <fcntl.h> 72 #include <netdb.h> 73 #include <stdio.h> 74 #include <stdlib.h> 75 #include <string.h> 76 #include <strings.h> 77 #include <sysexits.h> 78 #include <unistd.h> 79 80 #include "mntopts.h" 81 #include "mounttab.h" 82 83 #define ALTF_BG 0x1 84 #define ALTF_NOCONN 0x2 85 #define ALTF_DUMBTIMR 0x4 86 #define ALTF_INTR 0x8 87 #define ALTF_NFSV3 0x20 88 #define ALTF_RDIRPLUS 0x40 89 #define ALTF_MNTUDP 0x80 90 #define ALTF_RESVPORT 0x100 91 #define ALTF_SEQPACKET 0x200 92 #define ALTF_SOFT 0x800 93 #define ALTF_TCP 0x1000 94 #define ALTF_PORT 0x2000 95 #define ALTF_NFSV2 0x4000 96 #define ALTF_ACREGMIN 0x8000 97 #define ALTF_ACREGMAX 0x10000 98 #define ALTF_ACDIRMIN 0x20000 99 #define ALTF_ACDIRMAX 0x40000 100 #define ALTF_NOLOCKD 0x80000 101 #define ALTF_NOINET4 0x100000 102 #define ALTF_NOINET6 0x200000 103 104 struct mntopt mopts[] = { 105 MOPT_STDOPTS, 106 MOPT_FORCE, 107 MOPT_UPDATE, 108 MOPT_ASYNC, 109 { "bg", 0, ALTF_BG, 1 }, 110 { "conn", 1, ALTF_NOCONN, 1 }, 111 { "dumbtimer", 0, ALTF_DUMBTIMR, 1 }, 112 { "intr", 0, ALTF_INTR, 1 }, 113 { "nfsv3", 0, ALTF_NFSV3, 1 }, 114 { "rdirplus", 0, ALTF_RDIRPLUS, 1 }, 115 { "mntudp", 0, ALTF_MNTUDP, 1 }, 116 { "resvport", 0, ALTF_RESVPORT, 1 }, 117 { "soft", 0, ALTF_SOFT, 1 }, 118 { "tcp", 0, ALTF_TCP, 1 }, 119 { "port=", 0, ALTF_PORT, 1 }, 120 { "nfsv2", 0, ALTF_NFSV2, 1 }, 121 { "acregmin=", 0, ALTF_ACREGMIN, 1 }, 122 { "acregmax=", 0, ALTF_ACREGMAX, 1 }, 123 { "acdirmin=", 0, ALTF_ACDIRMIN, 1 }, 124 { "acdirmax=", 0, ALTF_ACDIRMAX, 1 }, 125 { "lockd", 1, ALTF_NOLOCKD, 1 }, 126 { "inet4", 1, ALTF_NOINET4, 1 }, 127 { "inet6", 1, ALTF_NOINET6, 1 }, 128 { NULL } 129 }; 130 131 struct nfs_args nfsdefargs = { 132 NFS_ARGSVERSION, 133 NULL, 134 sizeof (struct sockaddr_in), 135 SOCK_DGRAM, 136 0, 137 NULL, 138 0, 139 NFSMNT_RESVPORT, 140 NFS_WSIZE, 141 NFS_RSIZE, 142 NFS_READDIRSIZE, 143 10, 144 NFS_RETRANS, 145 NFS_MAXGRPS, 146 NFS_DEFRAHEAD, 147 0, /* was: NQ_DEFLEASE */ 148 NFS_MAXDEADTHRESH, /* was: NQ_DEADTHRESH */ 149 NULL, 150 /* args version 4 */ 151 NFS_MINATTRTIMO, 152 NFS_MAXATTRTIMO, 153 NFS_MINDIRATTRTIMO, 154 NFS_MAXDIRATTRTIMO, 155 }; 156 157 /* Table for af,sotype -> netid conversions. */ 158 struct nc_protos { 159 char *netid; 160 int af; 161 int sotype; 162 } nc_protos[] = { 163 {"udp", AF_INET, SOCK_DGRAM}, 164 {"tcp", AF_INET, SOCK_STREAM}, 165 {"udp6", AF_INET6, SOCK_DGRAM}, 166 {"tcp6", AF_INET6, SOCK_STREAM}, 167 {NULL} 168 }; 169 170 struct nfhret { 171 u_long stat; 172 long vers; 173 long auth; 174 long fhsize; 175 u_char nfh[NFSX_V3FHMAX]; 176 }; 177 #define BGRND 1 178 #define ISBGRND 2 179 #define OF_NOINET4 4 180 #define OF_NOINET6 8 181 int retrycnt = -1; 182 int opflags = 0; 183 int nfsproto = IPPROTO_UDP; 184 int mnttcp_ok = 1; 185 char *portspec = NULL; /* Server nfs port; NULL means look up via rpcbind. */ 186 enum mountmode { 187 ANY, 188 V2, 189 V3 190 } mountmode = ANY; 191 192 /* Return codes for nfs_tryproto. */ 193 enum tryret { 194 TRYRET_SUCCESS, 195 TRYRET_TIMEOUT, /* No response received. */ 196 TRYRET_REMOTEERR, /* Error received from remote server. */ 197 TRYRET_LOCALERR /* Local failure. */ 198 }; 199 200 int getnfsargs(char *, struct nfs_args *); 201 /* void set_rpc_maxgrouplist(int); */ 202 struct netconfig *getnetconf_cached(const char *netid); 203 char *netidbytype(int af, int sotype); 204 void usage(void) __dead2; 205 int xdr_dir(XDR *, char *); 206 int xdr_fh(XDR *, struct nfhret *); 207 enum tryret nfs_tryproto(struct nfs_args *nfsargsp, struct addrinfo *ai, 208 char *hostp, char *spec, char **errstr); 209 enum tryret returncode(enum clnt_stat stat, struct rpc_err *rpcerr); 210 211 /* 212 * Used to set mount flags with getmntopts. Call with dir=TRUE to 213 * initialize altflags from the current mount flags. Call with 214 * dir=FALSE to update mount flags with the new value of altflags after 215 * the call to getmntopts. 216 */ 217 static void 218 set_flags(int* altflags, int* nfsflags, int dir) 219 { 220 #define F2(af, nf) \ 221 if (dir) { \ 222 if (*nfsflags & NFSMNT_##nf) \ 223 *altflags |= ALTF_##af; \ 224 else \ 225 *altflags &= ~ALTF_##af; \ 226 } else { \ 227 if (*altflags & ALTF_##af) \ 228 *nfsflags |= NFSMNT_##nf; \ 229 else \ 230 *nfsflags &= ~NFSMNT_##nf; \ 231 } 232 #define F(f) F2(f,f) 233 234 F(NOCONN); 235 F(DUMBTIMR); 236 F2(INTR, INT); 237 F(RDIRPLUS); 238 F(RESVPORT); 239 F(SOFT); 240 F(ACREGMIN); 241 F(ACREGMAX); 242 F(ACDIRMIN); 243 F(ACDIRMAX); 244 F(NOLOCKD); 245 246 #undef F 247 #undef F2 248 } 249 250 int 251 main(argc, argv) 252 int argc; 253 char *argv[]; 254 { 255 int c; 256 struct nfs_args *nfsargsp; 257 struct nfs_args nfsargs; 258 int mntflags, altflags, num; 259 char *name, *p, *spec; 260 char mntpath[MAXPATHLEN]; 261 262 mntflags = 0; 263 altflags = 0; 264 nfsargs = nfsdefargs; 265 nfsargsp = &nfsargs; 266 while ((c = getopt(argc, argv, 267 "23a:bcdD:g:I:iLl:No:PpR:r:sTt:w:x:U")) != -1) 268 switch (c) { 269 case '2': 270 mountmode = V2; 271 break; 272 case '3': 273 mountmode = V3; 274 break; 275 case 'a': 276 num = strtol(optarg, &p, 10); 277 if (*p || num < 0) 278 errx(1, "illegal -a value -- %s", optarg); 279 nfsargsp->readahead = num; 280 nfsargsp->flags |= NFSMNT_READAHEAD; 281 break; 282 case 'b': 283 opflags |= BGRND; 284 break; 285 case 'c': 286 nfsargsp->flags |= NFSMNT_NOCONN; 287 break; 288 case 'D': 289 num = strtol(optarg, &p, 10); 290 if (*p || num <= 0) 291 errx(1, "illegal -D value -- %s", optarg); 292 nfsargsp->deadthresh = num; 293 nfsargsp->flags |= NFSMNT_DEADTHRESH; 294 break; 295 case 'd': 296 nfsargsp->flags |= NFSMNT_DUMBTIMR; 297 break; 298 #if 0 /* XXXX */ 299 case 'g': 300 num = strtol(optarg, &p, 10); 301 if (*p || num <= 0) 302 errx(1, "illegal -g value -- %s", optarg); 303 set_rpc_maxgrouplist(num); 304 nfsargsp->maxgrouplist = num; 305 nfsargsp->flags |= NFSMNT_MAXGRPS; 306 break; 307 #endif 308 case 'I': 309 num = strtol(optarg, &p, 10); 310 if (*p || num <= 0) 311 errx(1, "illegal -I value -- %s", optarg); 312 nfsargsp->readdirsize = num; 313 nfsargsp->flags |= NFSMNT_READDIRSIZE; 314 break; 315 case 'i': 316 nfsargsp->flags |= NFSMNT_INT; 317 break; 318 case 'L': 319 nfsargsp->flags |= NFSMNT_NOLOCKD; 320 break; 321 case 'l': 322 nfsargsp->flags |= NFSMNT_RDIRPLUS; 323 break; 324 case 'N': 325 nfsargsp->flags &= ~NFSMNT_RESVPORT; 326 break; 327 case 'o': 328 altflags = 0; 329 set_flags(&altflags, &nfsargsp->flags, TRUE); 330 if (mountmode == V2) 331 altflags |= ALTF_NFSV2; 332 else if (mountmode == V3) 333 altflags |= ALTF_NFSV3; 334 getmntopts(optarg, mopts, &mntflags, &altflags); 335 set_flags(&altflags, &nfsargsp->flags, FALSE); 336 /* 337 * Handle altflags which don't map directly to 338 * mount flags. 339 */ 340 if (altflags & ALTF_BG) 341 opflags |= BGRND; 342 if (altflags & ALTF_NOINET4) 343 opflags |= OF_NOINET4; 344 if (altflags & ALTF_NOINET6) 345 opflags |= OF_NOINET6; 346 if (altflags & ALTF_MNTUDP) 347 mnttcp_ok = 0; 348 if (altflags & ALTF_TCP) { 349 nfsargsp->sotype = SOCK_STREAM; 350 nfsproto = IPPROTO_TCP; 351 } 352 if (altflags & ALTF_PORT) { 353 /* 354 * XXX Converting from a string to an int 355 * and back again is silly, and we should 356 * allow /etc/services names. 357 */ 358 p = strstr(optarg, "port="); 359 if (p) { 360 asprintf(&portspec, "%d", 361 atoi(p + 5)); 362 if (portspec == NULL) 363 err(1, "asprintf"); 364 } 365 } 366 mountmode = ANY; 367 if (altflags & ALTF_NFSV2) 368 mountmode = V2; 369 if (altflags & ALTF_NFSV3) 370 mountmode = V3; 371 if (altflags & ALTF_ACREGMIN) { 372 p = strstr(optarg, "acregmin="); 373 if (p) 374 nfsargsp->acregmin = atoi(p + 9); 375 } 376 if (altflags & ALTF_ACREGMAX) { 377 p = strstr(optarg, "acregmax="); 378 if (p) 379 nfsargsp->acregmax = atoi(p + 9); 380 } 381 if (altflags & ALTF_ACDIRMIN) { 382 p = strstr(optarg, "acdirmin="); 383 if (p) 384 nfsargsp->acdirmin = atoi(p + 9); 385 } 386 if (altflags & ALTF_ACDIRMAX) { 387 p = strstr(optarg, "acdirmax="); 388 if (p) 389 nfsargsp->acdirmax = atoi(p + 9); 390 } 391 break; 392 case 'P': 393 /* obsolete for NFSMNT_RESVPORT, now default */ 394 break; 395 case 'R': 396 num = strtol(optarg, &p, 10); 397 if (*p || num < 0) 398 errx(1, "illegal -R value -- %s", optarg); 399 retrycnt = num; 400 break; 401 case 'r': 402 num = strtol(optarg, &p, 10); 403 if (*p || num <= 0) 404 errx(1, "illegal -r value -- %s", optarg); 405 nfsargsp->rsize = num; 406 nfsargsp->flags |= NFSMNT_RSIZE; 407 break; 408 case 's': 409 nfsargsp->flags |= NFSMNT_SOFT; 410 break; 411 case 'T': 412 nfsargsp->sotype = SOCK_STREAM; 413 nfsproto = IPPROTO_TCP; 414 break; 415 case 't': 416 num = strtol(optarg, &p, 10); 417 if (*p || num <= 0) 418 errx(1, "illegal -t value -- %s", optarg); 419 nfsargsp->timeo = num; 420 nfsargsp->flags |= NFSMNT_TIMEO; 421 break; 422 case 'w': 423 num = strtol(optarg, &p, 10); 424 if (*p || num <= 0) 425 errx(1, "illegal -w value -- %s", optarg); 426 nfsargsp->wsize = num; 427 nfsargsp->flags |= NFSMNT_WSIZE; 428 break; 429 case 'x': 430 num = strtol(optarg, &p, 10); 431 if (*p || num <= 0) 432 errx(1, "illegal -x value -- %s", optarg); 433 nfsargsp->retrans = num; 434 nfsargsp->flags |= NFSMNT_RETRANS; 435 break; 436 case 'U': 437 mnttcp_ok = 0; 438 break; 439 default: 440 usage(); 441 break; 442 } 443 argc -= optind; 444 argv += optind; 445 446 if (argc != 2) { 447 usage(); 448 /* NOTREACHED */ 449 } 450 451 spec = *argv++; 452 name = *argv; 453 454 if (retrycnt == -1) 455 /* The default is to keep retrying forever. */ 456 retrycnt = 0; 457 if (!getnfsargs(spec, nfsargsp)) 458 exit(1); 459 460 /* resolve the mountpoint with realpath(3) */ 461 (void)checkpath(name, mntpath); 462 463 if (mount("nfs", mntpath, mntflags, nfsargsp)) 464 err(1, "%s", mntpath); 465 466 exit(0); 467 } 468 469 int 470 getnfsargs(spec, nfsargsp) 471 char *spec; 472 struct nfs_args *nfsargsp; 473 { 474 struct addrinfo hints, *ai_nfs, *ai; 475 enum tryret ret; 476 int ecode, speclen, remoteerr; 477 char *hostp, *delimp, *errstr; 478 size_t len; 479 static char nam[MNAMELEN + 1]; 480 481 if ((delimp = strrchr(spec, ':')) != NULL) { 482 hostp = spec; 483 spec = delimp + 1; 484 } else if ((delimp = strrchr(spec, '@')) != NULL) { 485 warnx("path@server syntax is deprecated, use server:path"); 486 hostp = delimp + 1; 487 } else { 488 warnx("no <host>:<dirpath> nfs-name"); 489 return (0); 490 } 491 *delimp = '\0'; 492 493 /* 494 * If there has been a trailing slash at mounttime it seems 495 * that some mountd implementations fail to remove the mount 496 * entries from their mountlist while unmounting. 497 */ 498 for (speclen = strlen(spec); 499 speclen > 1 && spec[speclen - 1] == '/'; 500 speclen--) 501 spec[speclen - 1] = '\0'; 502 if (strlen(hostp) + strlen(spec) + 1 > MNAMELEN) { 503 warnx("%s:%s: %s", hostp, spec, strerror(ENAMETOOLONG)); 504 return (0); 505 } 506 /* Make both '@' and ':' notations equal */ 507 if (*hostp != '\0') { 508 len = strlen(hostp); 509 memmove(nam, hostp, len); 510 nam[len] = ':'; 511 memmove(nam + len + 1, spec, speclen); 512 nam[len + speclen + 1] = '\0'; 513 } 514 515 /* 516 * Handle an internet host address. 517 */ 518 memset(&hints, 0, sizeof hints); 519 hints.ai_flags = AI_NUMERICHOST; 520 hints.ai_socktype = nfsargsp->sotype; 521 if (getaddrinfo(hostp, portspec, &hints, &ai_nfs) != 0) { 522 hints.ai_flags = 0; 523 if ((ecode = getaddrinfo(hostp, portspec, &hints, &ai_nfs)) 524 != 0) { 525 if (portspec == NULL) 526 errx(1, "%s: %s", hostp, gai_strerror(ecode)); 527 else 528 errx(1, "%s:%s: %s", hostp, portspec, 529 gai_strerror(ecode)); 530 return (0); 531 } 532 } 533 534 ret = TRYRET_LOCALERR; 535 for (;;) { 536 /* 537 * Try each entry returned by getaddrinfo(). Note the 538 * occurence of remote errors by setting `remoteerr'. 539 */ 540 remoteerr = 0; 541 for (ai = ai_nfs; ai != NULL; ai = ai->ai_next) { 542 if ((ai->ai_family == AF_INET6) && 543 (opflags & OF_NOINET6)) 544 continue; 545 if ((ai->ai_family == AF_INET) && 546 (opflags & OF_NOINET4)) 547 continue; 548 ret = nfs_tryproto(nfsargsp, ai, hostp, spec, &errstr); 549 if (ret == TRYRET_SUCCESS) 550 break; 551 if (ret != TRYRET_LOCALERR) 552 remoteerr = 1; 553 if ((opflags & ISBGRND) == 0) 554 fprintf(stderr, "%s\n", errstr); 555 } 556 if (ret == TRYRET_SUCCESS) 557 break; 558 559 /* Exit if all errors were local. */ 560 if (!remoteerr) 561 exit(1); 562 563 /* 564 * If retrycnt == 0, we are to keep retrying forever. 565 * Otherwise decrement it, and exit if it hits zero. 566 */ 567 if (retrycnt != 0 && --retrycnt == 0) 568 exit(1); 569 570 if ((opflags & (BGRND | ISBGRND)) == BGRND) { 571 warnx("Cannot immediately mount %s:%s, backgrounding", 572 hostp, spec); 573 opflags |= ISBGRND; 574 if (daemon(0, 0) != 0) 575 err(1, "daemon"); 576 } 577 sleep(60); 578 } 579 freeaddrinfo(ai_nfs); 580 nfsargsp->hostname = nam; 581 /* Add mounted file system to PATH_MOUNTTAB */ 582 if (!add_mtab(hostp, spec)) 583 warnx("can't update %s for %s:%s", PATH_MOUNTTAB, hostp, spec); 584 return (1); 585 } 586 587 /* 588 * Try to set up the NFS arguments according to the address 589 * family, protocol (and possibly port) specified in `ai'. 590 * 591 * Returns TRYRET_SUCCESS if successful, or: 592 * TRYRET_TIMEOUT The server did not respond. 593 * TRYRET_REMOTEERR The server reported an error. 594 * TRYRET_LOCALERR Local failure. 595 * 596 * In all error cases, *errstr will be set to a statically-allocated string 597 * describing the error. 598 */ 599 enum tryret 600 nfs_tryproto(struct nfs_args *nfsargsp, struct addrinfo *ai, char *hostp, 601 char *spec, char **errstr) 602 { 603 static char errbuf[256]; 604 struct sockaddr_storage nfs_ss; 605 struct netbuf nfs_nb; 606 struct nfhret nfhret; 607 struct timeval try; 608 struct rpc_err rpcerr; 609 CLIENT *clp; 610 struct netconfig *nconf, *nconf_mnt; 611 char *netid, *netid_mnt; 612 int doconnect, nfsvers, mntvers; 613 enum clnt_stat stat; 614 enum mountmode trymntmode; 615 616 trymntmode = mountmode; 617 errbuf[0] = '\0'; 618 *errstr = errbuf; 619 620 if ((netid = netidbytype(ai->ai_family, nfsargsp->sotype)) == NULL) { 621 snprintf(errbuf, sizeof errbuf, 622 "af %d sotype %d not supported", ai->ai_family, 623 nfsargsp->sotype); 624 return (TRYRET_LOCALERR); 625 } 626 if ((nconf = getnetconf_cached(netid)) == NULL) { 627 snprintf(errbuf, sizeof errbuf, "%s: %s", netid, nc_sperror()); 628 return (TRYRET_LOCALERR); 629 } 630 /* The RPCPROG_MNT netid may be different. */ 631 if (mnttcp_ok) { 632 netid_mnt = netid; 633 nconf_mnt = nconf; 634 } else { 635 if ((netid_mnt = netidbytype(ai->ai_family, SOCK_DGRAM)) 636 == NULL) { 637 snprintf(errbuf, sizeof errbuf, 638 "af %d sotype SOCK_DGRAM not supported", 639 ai->ai_family); 640 return (TRYRET_LOCALERR); 641 } 642 if ((nconf_mnt = getnetconf_cached(netid_mnt)) == NULL) { 643 snprintf(errbuf, sizeof errbuf, "%s: %s", netid_mnt, 644 nc_sperror()); 645 return (TRYRET_LOCALERR); 646 } 647 } 648 649 tryagain: 650 if (trymntmode == V2) { 651 nfsvers = 2; 652 mntvers = 1; 653 } else { 654 nfsvers = 3; 655 mntvers = 3; 656 } 657 658 if (portspec != NULL) { 659 /* `ai' contains the complete nfsd sockaddr. */ 660 nfs_nb.buf = ai->ai_addr; 661 nfs_nb.len = nfs_nb.maxlen = ai->ai_addrlen; 662 } else { 663 /* Ask the remote rpcbind. */ 664 nfs_nb.buf = &nfs_ss; 665 nfs_nb.len = nfs_nb.maxlen = sizeof nfs_ss; 666 667 if (!rpcb_getaddr(RPCPROG_NFS, nfsvers, nconf, &nfs_nb, 668 hostp)) { 669 if (rpc_createerr.cf_stat == RPC_PROGVERSMISMATCH && 670 trymntmode == ANY) { 671 trymntmode = V2; 672 goto tryagain; 673 } 674 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", 675 netid, hostp, spec, 676 clnt_spcreateerror("RPCPROG_NFS")); 677 return (returncode(rpc_createerr.cf_stat, 678 &rpc_createerr.cf_error)); 679 } 680 } 681 682 /* Check that the server (nfsd) responds on the port we have chosen. */ 683 clp = clnt_tli_create(RPC_ANYFD, nconf, &nfs_nb, RPCPROG_NFS, nfsvers, 684 0, 0); 685 if (clp == NULL) { 686 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid, 687 hostp, spec, clnt_spcreateerror("nfsd: RPCPROG_NFS")); 688 return (returncode(rpc_createerr.cf_stat, 689 &rpc_createerr.cf_error)); 690 } 691 if (nfsargsp->sotype == SOCK_DGRAM && 692 !(nfsargsp->flags & NFSMNT_NOCONN)) { 693 /* 694 * Use connect(), to match what the kernel does. This 695 * catches cases where the server responds from the 696 * wrong source address. 697 */ 698 doconnect = 1; 699 if (!clnt_control(clp, CLSET_CONNECT, (char *)&doconnect)) { 700 clnt_destroy(clp); 701 snprintf(errbuf, sizeof errbuf, 702 "[%s] %s:%s: CLSET_CONNECT failed", netid, hostp, 703 spec); 704 return (TRYRET_LOCALERR); 705 } 706 } 707 708 try.tv_sec = 10; 709 try.tv_usec = 0; 710 stat = clnt_call(clp, NFSPROC_NULL, (xdrproc_t)xdr_void, NULL, 711 (xdrproc_t)xdr_void, NULL, 712 try); 713 if (stat != RPC_SUCCESS) { 714 if (stat == RPC_PROGVERSMISMATCH && trymntmode == ANY) { 715 clnt_destroy(clp); 716 trymntmode = V2; 717 goto tryagain; 718 } 719 clnt_geterr(clp, &rpcerr); 720 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid, 721 hostp, spec, clnt_sperror(clp, "NFSPROC_NULL")); 722 clnt_destroy(clp); 723 return (returncode(stat, &rpcerr)); 724 } 725 clnt_destroy(clp); 726 727 /* Send the RPCMNT_MOUNT RPC to get the root filehandle. */ 728 try.tv_sec = 10; 729 try.tv_usec = 0; 730 clp = clnt_tp_create(hostp, RPCPROG_MNT, mntvers, nconf_mnt); 731 if (clp == NULL) { 732 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid_mnt, 733 hostp, spec, clnt_spcreateerror("RPCMNT: clnt_create")); 734 return (returncode(rpc_createerr.cf_stat, 735 &rpc_createerr.cf_error)); 736 } 737 clp->cl_auth = authsys_create_default(); 738 nfhret.auth = RPCAUTH_UNIX; 739 nfhret.vers = mntvers; 740 stat = clnt_call(clp, RPCMNT_MOUNT, (xdrproc_t)xdr_dir, spec, 741 (xdrproc_t)xdr_fh, &nfhret, 742 try); 743 auth_destroy(clp->cl_auth); 744 if (stat != RPC_SUCCESS) { 745 if (stat == RPC_PROGVERSMISMATCH && trymntmode == ANY) { 746 clnt_destroy(clp); 747 trymntmode = V2; 748 goto tryagain; 749 } 750 clnt_geterr(clp, &rpcerr); 751 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid_mnt, 752 hostp, spec, clnt_sperror(clp, "RPCPROG_MNT")); 753 clnt_destroy(clp); 754 return (returncode(stat, &rpcerr)); 755 } 756 clnt_destroy(clp); 757 758 if (nfhret.stat != 0) { 759 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid_mnt, 760 hostp, spec, strerror(nfhret.stat)); 761 return (TRYRET_REMOTEERR); 762 } 763 764 /* 765 * Store the filehandle and server address in nfsargsp, making 766 * sure to copy any locally allocated structures. 767 */ 768 nfsargsp->addrlen = nfs_nb.len; 769 nfsargsp->addr = malloc(nfsargsp->addrlen); 770 nfsargsp->fhsize = nfhret.fhsize; 771 nfsargsp->fh = malloc(nfsargsp->fhsize); 772 if (nfsargsp->addr == NULL || nfsargsp->fh == NULL) 773 err(1, "malloc"); 774 bcopy(nfs_nb.buf, nfsargsp->addr, nfsargsp->addrlen); 775 bcopy(nfhret.nfh, nfsargsp->fh, nfsargsp->fhsize); 776 777 if (nfsvers == 3) 778 nfsargsp->flags |= NFSMNT_NFSV3; 779 else 780 nfsargsp->flags &= ~NFSMNT_NFSV3; 781 782 return (TRYRET_SUCCESS); 783 } 784 785 786 /* 787 * Catagorise a RPC return status and error into an `enum tryret' 788 * return code. 789 */ 790 enum tryret 791 returncode(enum clnt_stat stat, struct rpc_err *rpcerr) 792 { 793 switch (stat) { 794 case RPC_TIMEDOUT: 795 return (TRYRET_TIMEOUT); 796 case RPC_PMAPFAILURE: 797 case RPC_PROGNOTREGISTERED: 798 case RPC_PROGVERSMISMATCH: 799 /* XXX, these can be local or remote. */ 800 case RPC_CANTSEND: 801 case RPC_CANTRECV: 802 return (TRYRET_REMOTEERR); 803 case RPC_SYSTEMERROR: 804 switch (rpcerr->re_errno) { 805 case ETIMEDOUT: 806 return (TRYRET_TIMEOUT); 807 case ENOMEM: 808 break; 809 default: 810 return (TRYRET_REMOTEERR); 811 } 812 /* FALLTHROUGH */ 813 default: 814 break; 815 } 816 return (TRYRET_LOCALERR); 817 } 818 819 /* 820 * Look up a netid based on an address family and socket type. 821 * `af' is the address family, and `sotype' is SOCK_DGRAM or SOCK_STREAM. 822 * 823 * XXX there should be a library function for this. 824 */ 825 char * 826 netidbytype(int af, int sotype) { 827 struct nc_protos *p; 828 829 for (p = nc_protos; p->netid != NULL; p++) { 830 if (af != p->af || sotype != p->sotype) 831 continue; 832 return (p->netid); 833 } 834 return (NULL); 835 } 836 837 /* 838 * Look up a netconfig entry based on a netid, and cache the result so 839 * that we don't need to remember to call freenetconfigent(). 840 * 841 * Otherwise it behaves just like getnetconfigent(), so nc_*error() 842 * work on failure. 843 */ 844 struct netconfig * 845 getnetconf_cached(const char *netid) { 846 static struct nc_entry { 847 struct netconfig *nconf; 848 struct nc_entry *next; 849 } *head; 850 struct nc_entry *p; 851 struct netconfig *nconf; 852 853 for (p = head; p != NULL; p = p->next) 854 if (strcmp(netid, p->nconf->nc_netid) == 0) 855 return (p->nconf); 856 857 if ((nconf = getnetconfigent(netid)) == NULL) 858 return (NULL); 859 if ((p = malloc(sizeof(*p))) == NULL) 860 err(1, "malloc"); 861 p->nconf = nconf; 862 p->next = head; 863 head = p; 864 865 return (p->nconf); 866 } 867 868 /* 869 * xdr routines for mount rpc's 870 */ 871 int 872 xdr_dir(xdrsp, dirp) 873 XDR *xdrsp; 874 char *dirp; 875 { 876 return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN)); 877 } 878 879 int 880 xdr_fh(xdrsp, np) 881 XDR *xdrsp; 882 struct nfhret *np; 883 { 884 int i; 885 long auth, authcnt, authfnd = 0; 886 887 if (!xdr_u_long(xdrsp, &np->stat)) 888 return (0); 889 if (np->stat) 890 return (1); 891 switch (np->vers) { 892 case 1: 893 np->fhsize = NFSX_V2FH; 894 return (xdr_opaque(xdrsp, (caddr_t)np->nfh, NFSX_V2FH)); 895 case 3: 896 if (!xdr_long(xdrsp, &np->fhsize)) 897 return (0); 898 if (np->fhsize <= 0 || np->fhsize > NFSX_V3FHMAX) 899 return (0); 900 if (!xdr_opaque(xdrsp, (caddr_t)np->nfh, np->fhsize)) 901 return (0); 902 if (!xdr_long(xdrsp, &authcnt)) 903 return (0); 904 for (i = 0; i < authcnt; i++) { 905 if (!xdr_long(xdrsp, &auth)) 906 return (0); 907 if (auth == np->auth) 908 authfnd++; 909 } 910 /* 911 * Some servers, such as DEC's OSF/1 return a nil authenticator 912 * list to indicate RPCAUTH_UNIX. 913 */ 914 if (!authfnd && (authcnt > 0 || np->auth != RPCAUTH_UNIX)) 915 np->stat = EAUTH; 916 return (1); 917 }; 918 return (0); 919 } 920 921 void 922 usage() 923 { 924 (void)fprintf(stderr, "%s\n%s\n%s\n%s\n", 925 "usage: mount_nfs [-23KNPTUbcdilqs] [-D deadthresh] [-I readdirsize]", 926 " [-R retrycnt] [-a maxreadahead]", 927 " [-g maxgroups] [-m realm] [-o options] [-r readsize]", 928 " [-t timeout] [-w writesize] [-x retrans] rhost:path node"); 929 exit(1); 930 } 931