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