1 /* 2 * copyright (c) 2003 3 * the regents of the university of michigan 4 * all rights reserved 5 * 6 * permission is granted to use, copy, create derivative works and redistribute 7 * this software and such derivative works for any purpose, so long as the name 8 * of the university of michigan is not used in any advertising or publicity 9 * pertaining to the use or distribution of this software without specific, 10 * written prior authorization. if the above copyright notice or any other 11 * identification of the university of michigan is included in any copy of any 12 * portion of this software, then the disclaimer below must also be included. 13 * 14 * this software is provided as is, without representation from the university 15 * of michigan as to its fitness for any purpose, and without warranty by the 16 * university of michigan of any kind, either express or implied, including 17 * without limitation the implied warranties of merchantability and fitness for 18 * a particular purpose. the regents of the university of michigan shall not be 19 * liable for any damages, including special, indirect, incidental, or 20 * consequential damages, with respect to any claim arising out of or in 21 * connection with the use of the software, even if it has been or is hereafter 22 * advised of the possibility of such damages. 23 */ 24 25 /* 26 * Copyright (c) 1992, 1993, 1994 27 * The Regents of the University of California. All rights reserved. 28 * 29 * This code is derived from software contributed to Berkeley by 30 * Rick Macklem at The University of Guelph. 31 * 32 * Redistribution and use in source and binary forms, with or without 33 * modification, are permitted provided that the following conditions 34 * are met: 35 * 1. Redistributions of source code must retain the above copyright 36 * notice, this list of conditions and the following disclaimer. 37 * 2. Redistributions in binary form must reproduce the above copyright 38 * notice, this list of conditions and the following disclaimer in the 39 * documentation and/or other materials provided with the distribution. 40 * 4. Neither the name of the University nor the names of its contributors 41 * may be used to endorse or promote products derived from this software 42 * without specific prior written permission. 43 * 44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 54 * SUCH DAMAGE. 55 */ 56 57 #if 0 58 #ifndef lint 59 static const char copyright[] = 60 "@(#) Copyright (c) 1992, 1993, 1994\n\ 61 The Regents of the University of California. All rights reserved.\n"; 62 #endif /* not lint */ 63 64 #ifndef lint 65 static char sccsid[] = "@(#)mount_nfs.c 8.11 (Berkeley) 5/4/95"; 66 #endif /* not lint */ 67 #endif 68 #include <sys/cdefs.h> 69 __FBSDID("$FreeBSD$"); 70 71 #include <sys/param.h> 72 #include <sys/mount.h> 73 #include <sys/socket.h> 74 #include <sys/stat.h> 75 #include <sys/syslog.h> 76 #include <sys/uio.h> 77 78 #include <rpc/rpc.h> 79 #include <rpc/pmap_clnt.h> 80 #include <rpc/pmap_prot.h> 81 82 #include <nfs/rpcv2.h> 83 #include <nfs/nfsproto.h> 84 #include <nfsclient/nfs.h> 85 #include <nfsclient/nfsargs.h> 86 87 #include <arpa/inet.h> 88 89 #include <ctype.h> 90 #include <err.h> 91 #include <errno.h> 92 #include <fcntl.h> 93 #include <netdb.h> 94 #include <stdio.h> 95 #include <stdlib.h> 96 #include <string.h> 97 #include <strings.h> 98 #include <sysexits.h> 99 #include <unistd.h> 100 101 #include "mntopts.h" 102 #include "mounttab.h" 103 104 #define ALTF_BG 0x1 105 #define ALTF_NOCONN 0x2 106 #define ALTF_DUMBTIMR 0x4 107 #define ALTF_INTR 0x8 108 #define ALTF_NFSV3 0x20 109 #define ALTF_RDIRPLUS 0x40 110 #define ALTF_MNTUDP 0x80 111 #define ALTF_RESVPORT 0x100 112 #define ALTF_SEQPACKET 0x200 113 #define ALTF_SOFT 0x800 114 #define ALTF_TCP 0x1000 115 #define ALTF_PORT 0x2000 116 #define ALTF_NFSV2 0x4000 117 #define ALTF_ACREGMIN 0x8000 118 #define ALTF_ACREGMAX 0x10000 119 #define ALTF_ACDIRMIN 0x20000 120 #define ALTF_ACDIRMAX 0x40000 121 #define ALTF_NOLOCKD 0x80000 122 #define ALTF_NOINET4 0x100000 123 #define ALTF_NOINET6 0x200000 124 125 struct mntopt mopts[] = { 126 MOPT_STDOPTS, 127 MOPT_FORCE, 128 MOPT_UPDATE, 129 MOPT_ASYNC, 130 { "bg", 0, ALTF_BG, 1 }, 131 { "fg", 1, ALTF_BG, 1 }, 132 { "conn", 1, ALTF_NOCONN, 1 }, 133 { "dumbtimer", 0, ALTF_DUMBTIMR, 1 }, 134 { "intr", 0, ALTF_INTR, 1 }, 135 { "nfsv3", 0, ALTF_NFSV3, 1 }, 136 { "rdirplus", 0, ALTF_RDIRPLUS, 1 }, 137 { "mntudp", 0, ALTF_MNTUDP, 1 }, 138 { "resvport", 0, ALTF_RESVPORT, 1 }, 139 { "soft", 0, ALTF_SOFT, 1 }, 140 { "hard", 1, ALTF_SOFT, 1 }, 141 { "tcp", 0, ALTF_TCP, 1 }, 142 { "port=", 0, ALTF_PORT, 1 }, 143 { "nfsv2", 0, ALTF_NFSV2, 1 }, 144 { "acregmin=", 0, ALTF_ACREGMIN, 1 }, 145 { "acregmax=", 0, ALTF_ACREGMAX, 1 }, 146 { "acdirmin=", 0, ALTF_ACDIRMIN, 1 }, 147 { "acdirmax=", 0, ALTF_ACDIRMAX, 1 }, 148 { "lockd", 1, ALTF_NOLOCKD, 1 }, 149 { "inet4", 1, ALTF_NOINET4, 1 }, 150 { "inet6", 1, ALTF_NOINET6, 1 }, 151 MOPT_END 152 }; 153 154 struct nfs_args nfsdefargs = { 155 NFS_ARGSVERSION, 156 NULL, 157 sizeof (struct sockaddr_in), 158 SOCK_STREAM, 159 0, 160 NULL, 161 0, 162 NFSMNT_RESVPORT, 163 NFS_WSIZE, 164 NFS_RSIZE, 165 NFS_READDIRSIZE, 166 10, 167 NFS_RETRANS, 168 NFS_MAXGRPS, 169 NFS_DEFRAHEAD, 170 0, /* was: NQ_DEFLEASE */ 171 NFS_MAXDEADTHRESH, /* was: NQ_DEADTHRESH */ 172 NULL, 173 /* args version 4 */ 174 NFS_MINATTRTIMO, 175 NFS_MAXATTRTIMO, 176 NFS_MINDIRATTRTIMO, 177 NFS_MAXDIRATTRTIMO, 178 }; 179 180 /* Table for af,sotype -> netid conversions. */ 181 struct nc_protos { 182 char *netid; 183 int af; 184 int sotype; 185 } nc_protos[] = { 186 {"udp", AF_INET, SOCK_DGRAM}, 187 {"tcp", AF_INET, SOCK_STREAM}, 188 {"udp6", AF_INET6, SOCK_DGRAM}, 189 {"tcp6", AF_INET6, SOCK_STREAM}, 190 {NULL, 0, 0} 191 }; 192 193 struct nfhret { 194 u_long stat; 195 long vers; 196 long auth; 197 long fhsize; 198 u_char nfh[NFSX_V3FHMAX]; 199 }; 200 #define BGRND 1 201 #define ISBGRND 2 202 #define OF_NOINET4 4 203 #define OF_NOINET6 8 204 int retrycnt = -1; 205 int opflags = 0; 206 int nfsproto = IPPROTO_UDP; 207 int mnttcp_ok = 1; 208 char *portspec = NULL; /* Server nfs port; NULL means look up via rpcbind. */ 209 enum mountmode { 210 ANY, 211 V2, 212 V3, 213 V4 214 } mountmode = ANY; 215 216 /* Return codes for nfs_tryproto. */ 217 enum tryret { 218 TRYRET_SUCCESS, 219 TRYRET_TIMEOUT, /* No response received. */ 220 TRYRET_REMOTEERR, /* Error received from remote server. */ 221 TRYRET_LOCALERR /* Local failure. */ 222 }; 223 224 int getnfsargs(char *, struct nfs_args *); 225 int getnfs4args(char *, struct nfs_args *); 226 /* void set_rpc_maxgrouplist(int); */ 227 struct netconfig *getnetconf_cached(const char *netid); 228 char *netidbytype(int af, int sotype); 229 void usage(void) __dead2; 230 int xdr_dir(XDR *, char *); 231 int xdr_fh(XDR *, struct nfhret *); 232 enum tryret nfs_tryproto(struct nfs_args *nfsargsp, struct addrinfo *ai, 233 char *hostp, char *spec, char **errstr); 234 enum tryret nfs4_tryproto(struct nfs_args *nfsargsp, struct addrinfo *ai, 235 char *hostp, char *spec, char **errstr); 236 enum tryret returncode(enum clnt_stat stat, struct rpc_err *rpcerr); 237 238 /* 239 * Used to set mount flags with getmntopts. Call with dir=TRUE to 240 * initialize altflags from the current mount flags. Call with 241 * dir=FALSE to update mount flags with the new value of altflags after 242 * the call to getmntopts. 243 */ 244 static void 245 set_flags(int* altflags, int* nfsflags, int dir) 246 { 247 #define F2(af, nf) \ 248 if (dir) { \ 249 if (*nfsflags & NFSMNT_##nf) \ 250 *altflags |= ALTF_##af; \ 251 else \ 252 *altflags &= ~ALTF_##af; \ 253 } else { \ 254 if (*altflags & ALTF_##af) \ 255 *nfsflags |= NFSMNT_##nf; \ 256 else \ 257 *nfsflags &= ~NFSMNT_##nf; \ 258 } 259 #define F(f) F2(f,f) 260 261 F(NOCONN); 262 F(DUMBTIMR); 263 F2(INTR, INT); 264 F(RDIRPLUS); 265 F(RESVPORT); 266 F(SOFT); 267 F(ACREGMIN); 268 F(ACREGMAX); 269 F(ACDIRMIN); 270 F(ACDIRMAX); 271 F(NOLOCKD); 272 273 #undef F 274 #undef F2 275 } 276 277 int 278 main(int argc, char *argv[]) 279 { 280 int c; 281 struct nfs_args *nfsargsp; 282 struct nfs_args nfsargs; 283 struct iovec *iov; 284 int mntflags, altflags, num; 285 int iovlen; 286 char *name, *p, *spec, *fstype; 287 char mntpath[MAXPATHLEN], errmsg[255]; 288 289 mntflags = 0; 290 altflags = 0; 291 nfsargs = nfsdefargs; 292 nfsargsp = &nfsargs; 293 iov = NULL; 294 iovlen = 0; 295 memset(errmsg, 0, sizeof(errmsg)); 296 297 fstype = strrchr(argv[0], '_'); 298 if (fstype == NULL) 299 errx(EX_USAGE, "argv[0] must end in _fstype"); 300 301 ++fstype; 302 303 if (strcmp(fstype, "nfs4") == 0) { 304 nfsproto = IPPROTO_TCP; 305 portspec = "2049"; 306 nfsdefargs.sotype = SOCK_STREAM; 307 mountmode = V4; 308 } 309 310 while ((c = getopt(argc, argv, 311 "234a:bcdD:g:I:iLlNo:PR:r:sTt:w:x:U")) != -1) 312 switch (c) { 313 case '2': 314 mountmode = V2; 315 break; 316 case '3': 317 mountmode = V3; 318 break; 319 case '4': 320 mountmode = V4; 321 fstype = "nfs4"; 322 break; 323 case 'a': 324 num = strtol(optarg, &p, 10); 325 if (*p || num < 0) 326 errx(1, "illegal -a value -- %s", optarg); 327 nfsargsp->readahead = num; 328 nfsargsp->flags |= NFSMNT_READAHEAD; 329 break; 330 case 'b': 331 opflags |= BGRND; 332 break; 333 case 'c': 334 nfsargsp->flags |= NFSMNT_NOCONN; 335 break; 336 case 'D': 337 num = strtol(optarg, &p, 10); 338 if (*p || num <= 0) 339 errx(1, "illegal -D value -- %s", optarg); 340 nfsargsp->deadthresh = num; 341 nfsargsp->flags |= NFSMNT_DEADTHRESH; 342 break; 343 case 'd': 344 nfsargsp->flags |= NFSMNT_DUMBTIMR; 345 break; 346 #if 0 /* XXXX */ 347 case 'g': 348 num = strtol(optarg, &p, 10); 349 if (*p || num <= 0) 350 errx(1, "illegal -g value -- %s", optarg); 351 set_rpc_maxgrouplist(num); 352 nfsargsp->maxgrouplist = num; 353 nfsargsp->flags |= NFSMNT_MAXGRPS; 354 break; 355 #endif 356 case 'I': 357 num = strtol(optarg, &p, 10); 358 if (*p || num <= 0) 359 errx(1, "illegal -I value -- %s", optarg); 360 nfsargsp->readdirsize = num; 361 nfsargsp->flags |= NFSMNT_READDIRSIZE; 362 break; 363 case 'i': 364 nfsargsp->flags |= NFSMNT_INT; 365 break; 366 case 'L': 367 nfsargsp->flags |= NFSMNT_NOLOCKD; 368 break; 369 case 'l': 370 nfsargsp->flags |= NFSMNT_RDIRPLUS; 371 break; 372 case 'N': 373 nfsargsp->flags &= ~NFSMNT_RESVPORT; 374 break; 375 case 'o': 376 altflags = 0; 377 set_flags(&altflags, &nfsargsp->flags, TRUE); 378 if (mountmode == V2) 379 altflags |= ALTF_NFSV2; 380 else if (mountmode == V3) 381 altflags |= ALTF_NFSV3; 382 getmntopts(optarg, mopts, &mntflags, &altflags); 383 set_flags(&altflags, &nfsargsp->flags, FALSE); 384 /* 385 * Handle altflags which don't map directly to 386 * mount flags. 387 */ 388 if (altflags & ALTF_BG) 389 opflags |= BGRND; 390 if (altflags & ALTF_NOINET4) 391 opflags |= OF_NOINET4; 392 if (altflags & ALTF_NOINET6) 393 opflags |= OF_NOINET6; 394 if (altflags & ALTF_MNTUDP) { 395 mnttcp_ok = 0; 396 nfsargsp->sotype = SOCK_DGRAM; 397 nfsproto = IPPROTO_UDP; 398 } 399 if (altflags & ALTF_TCP) { 400 nfsargsp->sotype = SOCK_STREAM; 401 nfsproto = IPPROTO_TCP; 402 } 403 if (altflags & ALTF_PORT) { 404 /* 405 * XXX Converting from a string to an int 406 * and back again is silly, and we should 407 * allow /etc/services names. 408 */ 409 p = strstr(optarg, "port="); 410 if (p) { 411 asprintf(&portspec, "%d", 412 atoi(p + 5)); 413 if (portspec == NULL) 414 err(1, "asprintf"); 415 } 416 } 417 mountmode = ANY; 418 if (altflags & ALTF_NFSV2) 419 mountmode = V2; 420 if (altflags & ALTF_NFSV3) 421 mountmode = V3; 422 if (altflags & ALTF_ACREGMIN) { 423 p = strstr(optarg, "acregmin="); 424 if (p) 425 nfsargsp->acregmin = atoi(p + 9); 426 } 427 if (altflags & ALTF_ACREGMAX) { 428 p = strstr(optarg, "acregmax="); 429 if (p) 430 nfsargsp->acregmax = atoi(p + 9); 431 } 432 if (altflags & ALTF_ACDIRMIN) { 433 p = strstr(optarg, "acdirmin="); 434 if (p) 435 nfsargsp->acdirmin = atoi(p + 9); 436 } 437 if (altflags & ALTF_ACDIRMAX) { 438 p = strstr(optarg, "acdirmax="); 439 if (p) 440 nfsargsp->acdirmax = atoi(p + 9); 441 } 442 break; 443 case 'P': 444 /* obsolete for NFSMNT_RESVPORT, now default */ 445 break; 446 case 'R': 447 num = strtol(optarg, &p, 10); 448 if (*p || num < 0) 449 errx(1, "illegal -R value -- %s", optarg); 450 retrycnt = num; 451 break; 452 case 'r': 453 num = strtol(optarg, &p, 10); 454 if (*p || num <= 0) 455 errx(1, "illegal -r value -- %s", optarg); 456 nfsargsp->rsize = num; 457 nfsargsp->flags |= NFSMNT_RSIZE; 458 break; 459 case 's': 460 nfsargsp->flags |= NFSMNT_SOFT; 461 break; 462 case 'T': 463 nfsargsp->sotype = SOCK_STREAM; 464 nfsproto = IPPROTO_TCP; 465 break; 466 case 't': 467 num = strtol(optarg, &p, 10); 468 if (*p || num <= 0) 469 errx(1, "illegal -t value -- %s", optarg); 470 nfsargsp->timeo = num; 471 nfsargsp->flags |= NFSMNT_TIMEO; 472 break; 473 case 'w': 474 num = strtol(optarg, &p, 10); 475 if (*p || num <= 0) 476 errx(1, "illegal -w value -- %s", optarg); 477 nfsargsp->wsize = num; 478 nfsargsp->flags |= NFSMNT_WSIZE; 479 break; 480 case 'x': 481 num = strtol(optarg, &p, 10); 482 if (*p || num <= 0) 483 errx(1, "illegal -x value -- %s", optarg); 484 nfsargsp->retrans = num; 485 nfsargsp->flags |= NFSMNT_RETRANS; 486 break; 487 case 'U': 488 mnttcp_ok = 0; 489 nfsargsp->sotype = SOCK_DGRAM; 490 nfsproto = IPPROTO_UDP; 491 break; 492 default: 493 usage(); 494 break; 495 } 496 argc -= optind; 497 argv += optind; 498 499 if (argc != 2) { 500 usage(); 501 /* NOTREACHED */ 502 } 503 504 spec = *argv++; 505 name = *argv; 506 507 if (retrycnt == -1) 508 /* The default is to keep retrying forever. */ 509 retrycnt = 0; 510 511 if (mountmode == V4) { 512 if (!getnfs4args(spec, nfsargsp)) 513 exit(1); 514 } else { 515 if (!getnfsargs(spec, nfsargsp)) 516 exit(1); 517 } 518 519 /* resolve the mountpoint with realpath(3) */ 520 (void)checkpath(name, mntpath); 521 522 build_iovec(&iov, &iovlen, "nfs_args", nfsargsp, sizeof(*nfsargsp)); 523 build_iovec(&iov, &iovlen, "fstype", fstype, (size_t)-1); 524 build_iovec(&iov, &iovlen, "fspath", mntpath, (size_t)-1); 525 build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg)); 526 527 if (nmount(iov, iovlen, mntflags)) 528 err(1, "%s, %s", mntpath, errmsg); 529 530 exit(0); 531 } 532 533 int 534 getnfsargs(char *spec, struct nfs_args *nfsargsp) 535 { 536 struct addrinfo hints, *ai_nfs, *ai; 537 enum tryret ret; 538 int ecode, speclen, remoteerr; 539 char *hostp, *delimp, *errstr; 540 size_t len; 541 static char nam[MNAMELEN + 1]; 542 543 if ((delimp = strrchr(spec, ':')) != NULL) { 544 hostp = spec; 545 spec = delimp + 1; 546 } else if ((delimp = strrchr(spec, '@')) != NULL) { 547 warnx("path@server syntax is deprecated, use server:path"); 548 hostp = delimp + 1; 549 } else { 550 warnx("no <host>:<dirpath> nfs-name"); 551 return (0); 552 } 553 *delimp = '\0'; 554 555 /* 556 * If there has been a trailing slash at mounttime it seems 557 * that some mountd implementations fail to remove the mount 558 * entries from their mountlist while unmounting. 559 */ 560 for (speclen = strlen(spec); 561 speclen > 1 && spec[speclen - 1] == '/'; 562 speclen--) 563 spec[speclen - 1] = '\0'; 564 if (strlen(hostp) + strlen(spec) + 1 > MNAMELEN) { 565 warnx("%s:%s: %s", hostp, spec, strerror(ENAMETOOLONG)); 566 return (0); 567 } 568 /* Make both '@' and ':' notations equal */ 569 if (*hostp != '\0') { 570 len = strlen(hostp); 571 memmove(nam, hostp, len); 572 nam[len] = ':'; 573 memmove(nam + len + 1, spec, speclen); 574 nam[len + speclen + 1] = '\0'; 575 } 576 577 /* 578 * Handle an internet host address. 579 */ 580 memset(&hints, 0, sizeof hints); 581 hints.ai_flags = AI_NUMERICHOST; 582 hints.ai_socktype = nfsargsp->sotype; 583 if (getaddrinfo(hostp, portspec, &hints, &ai_nfs) != 0) { 584 hints.ai_flags = 0; 585 if ((ecode = getaddrinfo(hostp, portspec, &hints, &ai_nfs)) 586 != 0) { 587 if (portspec == NULL) 588 errx(1, "%s: %s", hostp, gai_strerror(ecode)); 589 else 590 errx(1, "%s:%s: %s", hostp, portspec, 591 gai_strerror(ecode)); 592 return (0); 593 } 594 } 595 596 ret = TRYRET_LOCALERR; 597 for (;;) { 598 /* 599 * Try each entry returned by getaddrinfo(). Note the 600 * occurence of remote errors by setting `remoteerr'. 601 */ 602 remoteerr = 0; 603 for (ai = ai_nfs; ai != NULL; ai = ai->ai_next) { 604 if ((ai->ai_family == AF_INET6) && 605 (opflags & OF_NOINET6)) 606 continue; 607 if ((ai->ai_family == AF_INET) && 608 (opflags & OF_NOINET4)) 609 continue; 610 ret = nfs_tryproto(nfsargsp, ai, hostp, spec, &errstr); 611 if (ret == TRYRET_SUCCESS) 612 break; 613 if (ret != TRYRET_LOCALERR) 614 remoteerr = 1; 615 if ((opflags & ISBGRND) == 0) 616 fprintf(stderr, "%s\n", errstr); 617 } 618 if (ret == TRYRET_SUCCESS) 619 break; 620 621 /* Exit if all errors were local. */ 622 if (!remoteerr) 623 exit(1); 624 625 /* 626 * If retrycnt == 0, we are to keep retrying forever. 627 * Otherwise decrement it, and exit if it hits zero. 628 */ 629 if (retrycnt != 0 && --retrycnt == 0) 630 exit(1); 631 632 if ((opflags & (BGRND | ISBGRND)) == BGRND) { 633 warnx("Cannot immediately mount %s:%s, backgrounding", 634 hostp, spec); 635 opflags |= ISBGRND; 636 if (daemon(0, 0) != 0) 637 err(1, "daemon"); 638 } 639 sleep(60); 640 } 641 freeaddrinfo(ai_nfs); 642 nfsargsp->hostname = nam; 643 /* Add mounted file system to PATH_MOUNTTAB */ 644 if (!add_mtab(hostp, spec)) 645 warnx("can't update %s for %s:%s", PATH_MOUNTTAB, hostp, spec); 646 return (1); 647 } 648 649 650 int 651 getnfs4args(char *spec, struct nfs_args *nfsargsp) 652 { 653 struct addrinfo hints, *ai_nfs, *ai; 654 enum tryret ret; 655 int ecode, speclen, remoteerr; 656 char *hostp, *delimp, *errstr; 657 size_t len; 658 static char nam[MNAMELEN + 1]; 659 660 if ((delimp = strrchr(spec, ':')) != NULL) { 661 hostp = spec; 662 spec = delimp + 1; 663 } else if ((delimp = strrchr(spec, '@')) != NULL) { 664 warnx("path@server syntax is deprecated, use server:path"); 665 hostp = delimp + 1; 666 } else { 667 warnx("no <host>:<dirpath> nfs-name"); 668 return (0); 669 } 670 *delimp = '\0'; 671 672 /* 673 * If there has been a trailing slash at mounttime it seems 674 * that some mountd implementations fail to remove the mount 675 * entries from their mountlist while unmounting. 676 */ 677 for (speclen = strlen(spec); 678 speclen > 1 && spec[speclen - 1] == '/'; 679 speclen--) 680 spec[speclen - 1] = '\0'; 681 if (strlen(hostp) + strlen(spec) + 1 > MNAMELEN) { 682 warnx("%s:%s: %s", hostp, spec, strerror(ENAMETOOLONG)); 683 return (0); 684 } 685 /* Make both '@' and ':' notations equal */ 686 if (*hostp != '\0') { 687 len = strlen(hostp); 688 memmove(nam, hostp, len); 689 nam[len] = ':'; 690 memmove(nam + len + 1, spec, speclen); 691 nam[len + speclen + 1] = '\0'; 692 } 693 694 /* 695 * Handle an internet host address. 696 */ 697 memset(&hints, 0, sizeof hints); 698 hints.ai_flags = AI_NUMERICHOST; 699 hints.ai_socktype = nfsargsp->sotype; 700 if (getaddrinfo(hostp, portspec, &hints, &ai_nfs) != 0) { 701 hints.ai_flags = 0; 702 if ((ecode = getaddrinfo(hostp, portspec, &hints, &ai_nfs)) 703 != 0) { 704 if (portspec == NULL) 705 errx(1, "%s: %s", hostp, gai_strerror(ecode)); 706 else 707 errx(1, "%s:%s: %s", hostp, portspec, 708 gai_strerror(ecode)); 709 return (0); 710 } 711 } 712 713 ret = TRYRET_LOCALERR; 714 for (;;) { 715 /* 716 * Try each entry returned by getaddrinfo(). Note the 717 * occurence of remote errors by setting `remoteerr'. 718 */ 719 remoteerr = 0; 720 for (ai = ai_nfs; ai != NULL; ai = ai->ai_next) { 721 if ((ai->ai_family == AF_INET6) && 722 (opflags & OF_NOINET6)) 723 continue; 724 if ((ai->ai_family == AF_INET) && 725 (opflags & OF_NOINET4)) 726 continue; 727 ret = nfs4_tryproto(nfsargsp, ai, hostp, spec, &errstr); 728 if (ret == TRYRET_SUCCESS) 729 break; 730 if (ret != TRYRET_LOCALERR) 731 remoteerr = 1; 732 if ((opflags & ISBGRND) == 0) 733 fprintf(stderr, "%s\n", errstr); 734 } 735 if (ret == TRYRET_SUCCESS) 736 break; 737 738 /* Exit if all errors were local. */ 739 if (!remoteerr) 740 exit(1); 741 742 /* 743 * If retrycnt == 0, we are to keep retrying forever. 744 * Otherwise decrement it, and exit if it hits zero. 745 */ 746 if (retrycnt != 0 && --retrycnt == 0) 747 exit(1); 748 749 if ((opflags & (BGRND | ISBGRND)) == BGRND) { 750 warnx("Cannot immediately mount %s:%s, backgrounding", 751 hostp, spec); 752 opflags |= ISBGRND; 753 if (daemon(0, 0) != 0) 754 err(1, "daemon"); 755 } 756 sleep(60); 757 } 758 freeaddrinfo(ai_nfs); 759 nfsargsp->hostname = nam; 760 /* Add mounted file system to PATH_MOUNTTAB */ 761 if (!add_mtab(hostp, spec)) 762 warnx("can't update %s for %s:%s", PATH_MOUNTTAB, hostp, spec); 763 return (1); 764 } 765 766 /* 767 * Try to set up the NFS arguments according to the address 768 * family, protocol (and possibly port) specified in `ai'. 769 * 770 * Returns TRYRET_SUCCESS if successful, or: 771 * TRYRET_TIMEOUT The server did not respond. 772 * TRYRET_REMOTEERR The server reported an error. 773 * TRYRET_LOCALERR Local failure. 774 * 775 * In all error cases, *errstr will be set to a statically-allocated string 776 * describing the error. 777 */ 778 enum tryret 779 nfs_tryproto(struct nfs_args *nfsargsp, struct addrinfo *ai, char *hostp, 780 char *spec, char **errstr) 781 { 782 static char errbuf[256]; 783 struct sockaddr_storage nfs_ss; 784 struct netbuf nfs_nb; 785 struct nfhret nfhret; 786 struct timeval try; 787 struct rpc_err rpcerr; 788 CLIENT *clp; 789 struct netconfig *nconf, *nconf_mnt; 790 char *netid, *netid_mnt; 791 int doconnect, nfsvers, mntvers; 792 enum clnt_stat stat; 793 enum mountmode trymntmode; 794 795 trymntmode = mountmode; 796 errbuf[0] = '\0'; 797 *errstr = errbuf; 798 799 if ((netid = netidbytype(ai->ai_family, nfsargsp->sotype)) == NULL) { 800 snprintf(errbuf, sizeof errbuf, 801 "af %d sotype %d not supported", ai->ai_family, 802 nfsargsp->sotype); 803 return (TRYRET_LOCALERR); 804 } 805 if ((nconf = getnetconf_cached(netid)) == NULL) { 806 snprintf(errbuf, sizeof errbuf, "%s: %s", netid, nc_sperror()); 807 return (TRYRET_LOCALERR); 808 } 809 /* The RPCPROG_MNT netid may be different. */ 810 if (mnttcp_ok) { 811 netid_mnt = netid; 812 nconf_mnt = nconf; 813 } else { 814 if ((netid_mnt = netidbytype(ai->ai_family, SOCK_DGRAM)) 815 == NULL) { 816 snprintf(errbuf, sizeof errbuf, 817 "af %d sotype SOCK_DGRAM not supported", 818 ai->ai_family); 819 return (TRYRET_LOCALERR); 820 } 821 if ((nconf_mnt = getnetconf_cached(netid_mnt)) == NULL) { 822 snprintf(errbuf, sizeof errbuf, "%s: %s", netid_mnt, 823 nc_sperror()); 824 return (TRYRET_LOCALERR); 825 } 826 } 827 828 tryagain: 829 if (trymntmode == V2) { 830 nfsvers = 2; 831 mntvers = 1; 832 } else { 833 nfsvers = 3; 834 mntvers = 3; 835 } 836 837 if (portspec != NULL) { 838 /* `ai' contains the complete nfsd sockaddr. */ 839 nfs_nb.buf = ai->ai_addr; 840 nfs_nb.len = nfs_nb.maxlen = ai->ai_addrlen; 841 } else { 842 /* Ask the remote rpcbind. */ 843 nfs_nb.buf = &nfs_ss; 844 nfs_nb.len = nfs_nb.maxlen = sizeof nfs_ss; 845 846 if (!rpcb_getaddr(RPCPROG_NFS, nfsvers, nconf, &nfs_nb, 847 hostp)) { 848 if (rpc_createerr.cf_stat == RPC_PROGVERSMISMATCH && 849 trymntmode == ANY) { 850 trymntmode = V2; 851 goto tryagain; 852 } 853 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", 854 netid, hostp, spec, 855 clnt_spcreateerror("RPCPROG_NFS")); 856 return (returncode(rpc_createerr.cf_stat, 857 &rpc_createerr.cf_error)); 858 } 859 } 860 861 /* Check that the server (nfsd) responds on the port we have chosen. */ 862 clp = clnt_tli_create(RPC_ANYFD, nconf, &nfs_nb, RPCPROG_NFS, nfsvers, 863 0, 0); 864 if (clp == NULL) { 865 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid, 866 hostp, spec, clnt_spcreateerror("nfsd: RPCPROG_NFS")); 867 return (returncode(rpc_createerr.cf_stat, 868 &rpc_createerr.cf_error)); 869 } 870 if (nfsargsp->sotype == SOCK_DGRAM && 871 !(nfsargsp->flags & NFSMNT_NOCONN)) { 872 /* 873 * Use connect(), to match what the kernel does. This 874 * catches cases where the server responds from the 875 * wrong source address. 876 */ 877 doconnect = 1; 878 if (!clnt_control(clp, CLSET_CONNECT, (char *)&doconnect)) { 879 clnt_destroy(clp); 880 snprintf(errbuf, sizeof errbuf, 881 "[%s] %s:%s: CLSET_CONNECT failed", netid, hostp, 882 spec); 883 return (TRYRET_LOCALERR); 884 } 885 } 886 887 try.tv_sec = 10; 888 try.tv_usec = 0; 889 stat = clnt_call(clp, NFSPROC_NULL, (xdrproc_t)xdr_void, NULL, 890 (xdrproc_t)xdr_void, NULL, 891 try); 892 if (stat != RPC_SUCCESS) { 893 if (stat == RPC_PROGVERSMISMATCH && trymntmode == ANY) { 894 clnt_destroy(clp); 895 trymntmode = V2; 896 goto tryagain; 897 } 898 clnt_geterr(clp, &rpcerr); 899 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid, 900 hostp, spec, clnt_sperror(clp, "NFSPROC_NULL")); 901 clnt_destroy(clp); 902 return (returncode(stat, &rpcerr)); 903 } 904 clnt_destroy(clp); 905 906 /* Send the RPCMNT_MOUNT RPC to get the root filehandle. */ 907 try.tv_sec = 10; 908 try.tv_usec = 0; 909 clp = clnt_tp_create(hostp, RPCPROG_MNT, mntvers, nconf_mnt); 910 if (clp == NULL) { 911 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid_mnt, 912 hostp, spec, clnt_spcreateerror("RPCMNT: clnt_create")); 913 return (returncode(rpc_createerr.cf_stat, 914 &rpc_createerr.cf_error)); 915 } 916 clp->cl_auth = authsys_create_default(); 917 nfhret.auth = RPCAUTH_UNIX; 918 nfhret.vers = mntvers; 919 stat = clnt_call(clp, RPCMNT_MOUNT, (xdrproc_t)xdr_dir, spec, 920 (xdrproc_t)xdr_fh, &nfhret, 921 try); 922 auth_destroy(clp->cl_auth); 923 if (stat != RPC_SUCCESS) { 924 if (stat == RPC_PROGVERSMISMATCH && trymntmode == ANY) { 925 clnt_destroy(clp); 926 trymntmode = V2; 927 goto tryagain; 928 } 929 clnt_geterr(clp, &rpcerr); 930 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid_mnt, 931 hostp, spec, clnt_sperror(clp, "RPCPROG_MNT")); 932 clnt_destroy(clp); 933 return (returncode(stat, &rpcerr)); 934 } 935 clnt_destroy(clp); 936 937 if (nfhret.stat != 0) { 938 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid_mnt, 939 hostp, spec, strerror(nfhret.stat)); 940 return (TRYRET_REMOTEERR); 941 } 942 943 /* 944 * Store the filehandle and server address in nfsargsp, making 945 * sure to copy any locally allocated structures. 946 */ 947 nfsargsp->addrlen = nfs_nb.len; 948 nfsargsp->addr = malloc(nfsargsp->addrlen); 949 nfsargsp->fhsize = nfhret.fhsize; 950 nfsargsp->fh = malloc(nfsargsp->fhsize); 951 if (nfsargsp->addr == NULL || nfsargsp->fh == NULL) 952 err(1, "malloc"); 953 bcopy(nfs_nb.buf, nfsargsp->addr, nfsargsp->addrlen); 954 bcopy(nfhret.nfh, nfsargsp->fh, nfsargsp->fhsize); 955 956 if (nfsvers == 3) 957 nfsargsp->flags |= NFSMNT_NFSV3; 958 else 959 nfsargsp->flags &= ~NFSMNT_NFSV3; 960 961 return (TRYRET_SUCCESS); 962 } 963 964 965 /* 966 * Try to set up the NFS arguments according to the address 967 * family, protocol (and possibly port) specified in `ai'. 968 * 969 * Returns TRYRET_SUCCESS if successful, or: 970 * TRYRET_TIMEOUT The server did not respond. 971 * TRYRET_REMOTEERR The server reported an error. 972 * TRYRET_LOCALERR Local failure. 973 * 974 * In all error cases, *errstr will be set to a statically-allocated string 975 * describing the error. 976 */ 977 enum tryret 978 nfs4_tryproto(struct nfs_args *nfsargsp, struct addrinfo *ai, char *hostp, 979 char *spec, char **errstr) 980 { 981 static char errbuf[256]; 982 struct sockaddr_storage nfs_ss; 983 struct netbuf nfs_nb; 984 struct netconfig *nconf; 985 char *netid; 986 int nfsvers; 987 988 errbuf[0] = '\0'; 989 *errstr = errbuf; 990 991 if ((netid = netidbytype(ai->ai_family, nfsargsp->sotype)) == NULL) { 992 snprintf(errbuf, sizeof errbuf, 993 "af %d sotype %d not supported", ai->ai_family, 994 nfsargsp->sotype); 995 return (TRYRET_LOCALERR); 996 } 997 if ((nconf = getnetconf_cached(netid)) == NULL) { 998 snprintf(errbuf, sizeof errbuf, "%s: %s", netid, nc_sperror()); 999 return (TRYRET_LOCALERR); 1000 } 1001 1002 nfsvers = 4; 1003 1004 if (portspec != NULL && atoi(portspec) != 0) { 1005 /* `ai' contains the complete nfsd sockaddr. */ 1006 nfs_nb.buf = ai->ai_addr; 1007 nfs_nb.len = nfs_nb.maxlen = ai->ai_addrlen; 1008 } else { 1009 /* Ask the remote rpcbind. */ 1010 nfs_nb.buf = &nfs_ss; 1011 nfs_nb.len = nfs_nb.maxlen = sizeof nfs_ss; 1012 1013 if (!rpcb_getaddr(RPCPROG_NFS, nfsvers, nconf, &nfs_nb, 1014 hostp)) { 1015 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", 1016 netid, hostp, spec, 1017 clnt_spcreateerror("RPCPROG_NFS")); 1018 return (returncode(rpc_createerr.cf_stat, 1019 &rpc_createerr.cf_error)); 1020 } 1021 } 1022 1023 /* 1024 * Store the filehandle and server address in nfsargsp, making 1025 * sure to copy any locally allocated structures. 1026 */ 1027 nfsargsp->addrlen = nfs_nb.len; 1028 nfsargsp->addr = malloc(nfsargsp->addrlen); 1029 1030 if (nfsargsp->addr == NULL) 1031 err(1, "malloc"); 1032 bcopy(nfs_nb.buf, nfsargsp->addr, nfsargsp->addrlen); 1033 1034 /* XXX hack */ 1035 nfsargsp->flags |= (NFSMNT_NFSV3 | NFSMNT_NFSV4); 1036 1037 return (TRYRET_SUCCESS); 1038 } 1039 1040 /* 1041 * Catagorise a RPC return status and error into an `enum tryret' 1042 * return code. 1043 */ 1044 enum tryret 1045 returncode(enum clnt_stat stat, struct rpc_err *rpcerr) 1046 { 1047 switch (stat) { 1048 case RPC_TIMEDOUT: 1049 return (TRYRET_TIMEOUT); 1050 case RPC_PMAPFAILURE: 1051 case RPC_PROGNOTREGISTERED: 1052 case RPC_PROGVERSMISMATCH: 1053 /* XXX, these can be local or remote. */ 1054 case RPC_CANTSEND: 1055 case RPC_CANTRECV: 1056 return (TRYRET_REMOTEERR); 1057 case RPC_SYSTEMERROR: 1058 switch (rpcerr->re_errno) { 1059 case ETIMEDOUT: 1060 return (TRYRET_TIMEOUT); 1061 case ENOMEM: 1062 break; 1063 default: 1064 return (TRYRET_REMOTEERR); 1065 } 1066 /* FALLTHROUGH */ 1067 default: 1068 break; 1069 } 1070 return (TRYRET_LOCALERR); 1071 } 1072 1073 /* 1074 * Look up a netid based on an address family and socket type. 1075 * `af' is the address family, and `sotype' is SOCK_DGRAM or SOCK_STREAM. 1076 * 1077 * XXX there should be a library function for this. 1078 */ 1079 char * 1080 netidbytype(int af, int sotype) 1081 { 1082 struct nc_protos *p; 1083 1084 for (p = nc_protos; p->netid != NULL; p++) { 1085 if (af != p->af || sotype != p->sotype) 1086 continue; 1087 return (p->netid); 1088 } 1089 return (NULL); 1090 } 1091 1092 /* 1093 * Look up a netconfig entry based on a netid, and cache the result so 1094 * that we don't need to remember to call freenetconfigent(). 1095 * 1096 * Otherwise it behaves just like getnetconfigent(), so nc_*error() 1097 * work on failure. 1098 */ 1099 struct netconfig * 1100 getnetconf_cached(const char *netid) 1101 { 1102 static struct nc_entry { 1103 struct netconfig *nconf; 1104 struct nc_entry *next; 1105 } *head; 1106 struct nc_entry *p; 1107 struct netconfig *nconf; 1108 1109 for (p = head; p != NULL; p = p->next) 1110 if (strcmp(netid, p->nconf->nc_netid) == 0) 1111 return (p->nconf); 1112 1113 if ((nconf = getnetconfigent(netid)) == NULL) 1114 return (NULL); 1115 if ((p = malloc(sizeof(*p))) == NULL) 1116 err(1, "malloc"); 1117 p->nconf = nconf; 1118 p->next = head; 1119 head = p; 1120 1121 return (p->nconf); 1122 } 1123 1124 /* 1125 * xdr routines for mount rpc's 1126 */ 1127 int 1128 xdr_dir(XDR *xdrsp, char *dirp) 1129 { 1130 return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN)); 1131 } 1132 1133 int 1134 xdr_fh(XDR *xdrsp, struct nfhret *np) 1135 { 1136 int i; 1137 long auth, authcnt, authfnd = 0; 1138 1139 if (!xdr_u_long(xdrsp, &np->stat)) 1140 return (0); 1141 if (np->stat) 1142 return (1); 1143 switch (np->vers) { 1144 case 1: 1145 np->fhsize = NFSX_V2FH; 1146 return (xdr_opaque(xdrsp, (caddr_t)np->nfh, NFSX_V2FH)); 1147 case 3: 1148 if (!xdr_long(xdrsp, &np->fhsize)) 1149 return (0); 1150 if (np->fhsize <= 0 || np->fhsize > NFSX_V3FHMAX) 1151 return (0); 1152 if (!xdr_opaque(xdrsp, (caddr_t)np->nfh, np->fhsize)) 1153 return (0); 1154 if (!xdr_long(xdrsp, &authcnt)) 1155 return (0); 1156 for (i = 0; i < authcnt; i++) { 1157 if (!xdr_long(xdrsp, &auth)) 1158 return (0); 1159 if (auth == np->auth) 1160 authfnd++; 1161 } 1162 /* 1163 * Some servers, such as DEC's OSF/1 return a nil authenticator 1164 * list to indicate RPCAUTH_UNIX. 1165 */ 1166 if (!authfnd && (authcnt > 0 || np->auth != RPCAUTH_UNIX)) 1167 np->stat = EAUTH; 1168 return (1); 1169 }; 1170 return (0); 1171 } 1172 1173 void 1174 usage() 1175 { 1176 (void)fprintf(stderr, "%s\n%s\n%s\n%s\n", 1177 "usage: mount_nfs [-234bcdiLlNPsTU] [-a maxreadahead] [-D deadthresh]", 1178 " [-g maxgroups] [-I readdirsize] [-o options] [-R retrycnt]", 1179 " [-r readsize] [-t timeout] [-w writesize] [-x retrans]", 1180 " rhost:path node"); 1181 exit(1); 1182 } 1183