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 * 4. 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 33 #if 0 34 #ifndef lint 35 static const char copyright[] = 36 "@(#) Copyright (c) 1992, 1993, 1994\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38 #endif /* not lint */ 39 40 #ifndef lint 41 static char sccsid[] = "@(#)mount_nfs.c 8.11 (Berkeley) 5/4/95"; 42 #endif /* not lint */ 43 #endif 44 #include <sys/cdefs.h> 45 __FBSDID("$FreeBSD$"); 46 47 #include <sys/param.h> 48 #include <sys/linker.h> 49 #include <sys/module.h> 50 #include <sys/mount.h> 51 #include <sys/socket.h> 52 #include <sys/stat.h> 53 #include <sys/syslog.h> 54 #include <sys/uio.h> 55 56 #include <rpc/rpc.h> 57 #include <rpc/pmap_clnt.h> 58 #include <rpc/pmap_prot.h> 59 #include <rpcsvc/nfs_prot.h> 60 #include <rpcsvc/mount.h> 61 62 #include <nfsclient/nfs.h> 63 64 #include <arpa/inet.h> 65 66 #include <ctype.h> 67 #include <err.h> 68 #include <errno.h> 69 #include <fcntl.h> 70 #include <netdb.h> 71 #include <stdio.h> 72 #include <stdlib.h> 73 #include <string.h> 74 #include <strings.h> 75 #include <sysexits.h> 76 #include <unistd.h> 77 78 #include "mntopts.h" 79 #include "mounttab.h" 80 81 /* Table for af,sotype -> netid conversions. */ 82 struct nc_protos { 83 const char *netid; 84 int af; 85 int sotype; 86 } nc_protos[] = { 87 {"udp", AF_INET, SOCK_DGRAM}, 88 {"tcp", AF_INET, SOCK_STREAM}, 89 {"udp6", AF_INET6, SOCK_DGRAM}, 90 {"tcp6", AF_INET6, SOCK_STREAM}, 91 {NULL, 0, 0} 92 }; 93 94 struct nfhret { 95 u_long stat; 96 long vers; 97 long auth; 98 long fhsize; 99 u_char nfh[NFS3_FHSIZE]; 100 }; 101 #define BGRND 1 102 #define ISBGRND 2 103 #define OF_NOINET4 4 104 #define OF_NOINET6 8 105 int retrycnt = -1; 106 int opflags = 0; 107 int nfsproto = IPPROTO_TCP; 108 int mnttcp_ok = 1; 109 int noconn = 0; 110 char *portspec = NULL; /* Server nfs port; NULL means look up via rpcbind. */ 111 struct sockaddr *addr; 112 int addrlen = 0; 113 u_char *fh = NULL; 114 int fhsize = 0; 115 int secflavor = -1; 116 int got_principal = 0; 117 118 enum mountmode { 119 ANY, 120 V2, 121 V3, 122 V4 123 } mountmode = ANY; 124 125 /* Return codes for nfs_tryproto. */ 126 enum tryret { 127 TRYRET_SUCCESS, 128 TRYRET_TIMEOUT, /* No response received. */ 129 TRYRET_REMOTEERR, /* Error received from remote server. */ 130 TRYRET_LOCALERR /* Local failure. */ 131 }; 132 133 static int fallback_mount(struct iovec *iov, int iovlen); 134 static int sec_name_to_num(char *sec); 135 static char *sec_num_to_name(int num); 136 static int getnfsargs(char *, struct iovec **iov, int *iovlen); 137 /* void set_rpc_maxgrouplist(int); */ 138 static struct netconfig *getnetconf_cached(const char *netid); 139 static const char *netidbytype(int af, int sotype); 140 static void usage(void) __dead2; 141 static int xdr_dir(XDR *, char *); 142 static int xdr_fh(XDR *, struct nfhret *); 143 static enum tryret nfs_tryproto(struct addrinfo *ai, char *hostp, char *spec, 144 char **errstr, struct iovec **iov, int *iovlen); 145 static enum tryret returncode(enum clnt_stat stat, struct rpc_err *rpcerr); 146 147 int 148 main(int argc, char *argv[]) 149 { 150 int c; 151 struct iovec *iov; 152 int num, iovlen; 153 int osversion; 154 char *name, *p, *spec, *fstype; 155 char mntpath[MAXPATHLEN], errmsg[255]; 156 char hostname[MAXHOSTNAMELEN + 1], *gssname, gssn[MAXHOSTNAMELEN + 50]; 157 158 iov = NULL; 159 iovlen = 0; 160 memset(errmsg, 0, sizeof(errmsg)); 161 gssname = NULL; 162 163 fstype = strrchr(argv[0], '_'); 164 if (fstype == NULL) 165 errx(EX_USAGE, "argv[0] must end in _fstype"); 166 167 ++fstype; 168 169 while ((c = getopt(argc, argv, 170 "23a:bcdD:g:I:iLlNo:PR:r:sTt:w:x:U")) != -1) 171 switch (c) { 172 case '2': 173 mountmode = V2; 174 break; 175 case '3': 176 mountmode = V3; 177 break; 178 case 'a': 179 printf("-a deprecated, use -o readahead=<value>\n"); 180 build_iovec(&iov, &iovlen, "readahead", optarg, (size_t)-1); 181 break; 182 case 'b': 183 opflags |= BGRND; 184 break; 185 case 'c': 186 printf("-c deprecated, use -o noconn\n"); 187 build_iovec(&iov, &iovlen, "noconn", NULL, 0); 188 noconn = 1; 189 break; 190 case 'D': 191 printf("-D deprecated, use -o deadthresh=<value>\n"); 192 build_iovec(&iov, &iovlen, "deadthresh", optarg, (size_t)-1); 193 break; 194 case 'd': 195 printf("-d deprecated, use -o dumbtimer"); 196 build_iovec(&iov, &iovlen, "dumbtimer", NULL, 0); 197 break; 198 case 'g': 199 printf("-g deprecated, use -o maxgroups"); 200 num = strtol(optarg, &p, 10); 201 if (*p || num <= 0) 202 errx(1, "illegal -g value -- %s", optarg); 203 //set_rpc_maxgrouplist(num); 204 build_iovec(&iov, &iovlen, "maxgroups", optarg, (size_t)-1); 205 break; 206 case 'I': 207 printf("-I deprecated, use -o readdirsize=<value>\n"); 208 build_iovec(&iov, &iovlen, "readdirsize", optarg, (size_t)-1); 209 break; 210 case 'i': 211 printf("-i deprecated, use -o intr\n"); 212 build_iovec(&iov, &iovlen, "intr", NULL, 0); 213 break; 214 case 'L': 215 printf("-L deprecated, use -o nolockd\n"); 216 build_iovec(&iov, &iovlen, "nolockd", NULL, 0); 217 break; 218 case 'l': 219 printf("-l deprecated, -o rdirplus\n"); 220 build_iovec(&iov, &iovlen, "rdirplus", NULL, 0); 221 break; 222 case 'N': 223 printf("-N deprecated, do not specify -o resvport\n"); 224 break; 225 case 'o': { 226 int pass_flag_to_nmount; 227 char *opt = optarg; 228 while (opt) { 229 char *pval = NULL; 230 char *pnextopt = NULL; 231 char *val = ""; 232 pass_flag_to_nmount = 1; 233 pnextopt = strchr(opt, ','); 234 if (pnextopt != NULL) { 235 *pnextopt = '\0'; 236 pnextopt++; 237 } 238 pval = strchr(opt, '='); 239 if (pval != NULL) { 240 *pval = '\0'; 241 val = pval + 1; 242 } 243 if (strcmp(opt, "bg") == 0) { 244 opflags |= BGRND; 245 pass_flag_to_nmount=0; 246 } else if (strcmp(opt, "fg") == 0) { 247 /* same as not specifying -o bg */ 248 pass_flag_to_nmount=0; 249 } else if (strcmp(opt, "gssname") == 0) { 250 pass_flag_to_nmount = 0; 251 gssname = val; 252 } else if (strcmp(opt, "mntudp") == 0) { 253 mnttcp_ok = 0; 254 nfsproto = IPPROTO_UDP; 255 } else if (strcmp(opt, "udp") == 0) { 256 nfsproto = IPPROTO_UDP; 257 } else if (strcmp(opt, "tcp") == 0) { 258 nfsproto = IPPROTO_TCP; 259 } else if (strcmp(opt, "noinet4") == 0) { 260 pass_flag_to_nmount=0; 261 opflags |= OF_NOINET4; 262 } else if (strcmp(opt, "noinet6") == 0) { 263 pass_flag_to_nmount=0; 264 opflags |= OF_NOINET6; 265 } else if (strcmp(opt, "noconn") == 0) { 266 noconn = 1; 267 } else if (strcmp(opt, "nfsv2") == 0) { 268 pass_flag_to_nmount=0; 269 mountmode = V2; 270 } else if (strcmp(opt, "nfsv3") == 0) { 271 mountmode = V3; 272 } else if (strcmp(opt, "nfsv4") == 0) { 273 pass_flag_to_nmount=0; 274 mountmode = V4; 275 fstype = "nfs"; 276 nfsproto = IPPROTO_TCP; 277 if (portspec == NULL) 278 portspec = "2049"; 279 } else if (strcmp(opt, "port") == 0) { 280 pass_flag_to_nmount=0; 281 asprintf(&portspec, "%d", 282 atoi(val)); 283 if (portspec == NULL) 284 err(1, "asprintf"); 285 } else if (strcmp(opt, "principal") == 0) { 286 got_principal = 1; 287 } else if (strcmp(opt, "sec") == 0) { 288 /* 289 * Don't add this option to 290 * the iovec yet - we will 291 * negotiate which sec flavor 292 * to use with the remote 293 * mountd. 294 */ 295 pass_flag_to_nmount=0; 296 secflavor = sec_name_to_num(val); 297 if (secflavor < 0) { 298 errx(1, 299 "illegal sec value -- %s", 300 val); 301 } 302 } else if (strcmp(opt, "retrycnt") == 0) { 303 pass_flag_to_nmount=0; 304 num = strtol(val, &p, 10); 305 if (*p || num < 0) 306 errx(1, "illegal retrycnt value -- %s", val); 307 retrycnt = num; 308 } else if (strcmp(opt, "maxgroups") == 0) { 309 num = strtol(val, &p, 10); 310 if (*p || num <= 0) 311 errx(1, "illegal maxgroups value -- %s", val); 312 //set_rpc_maxgrouplist(num); 313 } 314 if (pass_flag_to_nmount) 315 build_iovec(&iov, &iovlen, opt, val, 316 strlen(val) + 1); 317 opt = pnextopt; 318 } 319 } 320 break; 321 case 'P': 322 /* obsolete for -o noresvport now default */ 323 printf("-P deprecated, use -o noresvport\n"); 324 build_iovec(&iov, &iovlen, "noresvport", NULL, 0); 325 break; 326 case 'R': 327 printf("-R deprecated, use -o retrycnt=<retrycnt>\n"); 328 num = strtol(optarg, &p, 10); 329 if (*p || num < 0) 330 errx(1, "illegal -R value -- %s", optarg); 331 retrycnt = num; 332 break; 333 case 'r': 334 printf("-r deprecated, use -o rsize=<rsize>\n"); 335 build_iovec(&iov, &iovlen, "rsize", optarg, (size_t)-1); 336 break; 337 case 's': 338 printf("-s deprecated, use -o soft\n"); 339 build_iovec(&iov, &iovlen, "soft", NULL, 0); 340 break; 341 case 'T': 342 nfsproto = IPPROTO_TCP; 343 printf("-T deprecated, use -o tcp\n"); 344 break; 345 case 't': 346 printf("-t deprecated, use -o timeout=<value>\n"); 347 build_iovec(&iov, &iovlen, "timeout", optarg, (size_t)-1); 348 break; 349 case 'w': 350 printf("-w deprecated, use -o wsize=<value>\n"); 351 build_iovec(&iov, &iovlen, "wsize", optarg, (size_t)-1); 352 break; 353 case 'x': 354 printf("-x deprecated, use -o retrans=<value>\n"); 355 build_iovec(&iov, &iovlen, "retrans", optarg, (size_t)-1); 356 break; 357 case 'U': 358 printf("-U deprecated, use -o mntudp\n"); 359 mnttcp_ok = 0; 360 nfsproto = IPPROTO_UDP; 361 build_iovec(&iov, &iovlen, "mntudp", NULL, 0); 362 break; 363 default: 364 usage(); 365 break; 366 } 367 argc -= optind; 368 argv += optind; 369 370 if (argc != 2) { 371 usage(); 372 /* NOTREACHED */ 373 } 374 375 spec = *argv++; 376 name = *argv; 377 378 if (retrycnt == -1) 379 /* The default is to keep retrying forever. */ 380 retrycnt = 0; 381 382 /* 383 * If the fstye is "oldnfs", run the old NFS client unless the 384 * "nfsv4" option was specified. 385 */ 386 if (strcmp(fstype, "nfs") == 0) { 387 if (modfind("nfscl") < 0) { 388 /* Not present in kernel, try loading it */ 389 if (kldload("nfscl") < 0 || 390 modfind("nfscl") < 0) 391 errx(1, "nfscl is not available"); 392 } 393 } 394 395 /* 396 * Add the fqdn to the gssname, as required. 397 */ 398 if (gssname != NULL) { 399 if (strchr(gssname, '@') == NULL && 400 gethostname(hostname, MAXHOSTNAMELEN) == 0) { 401 snprintf(gssn, sizeof (gssn), "%s@%s", gssname, 402 hostname); 403 gssname = gssn; 404 } 405 build_iovec(&iov, &iovlen, "gssname", gssname, 406 strlen(gssname) + 1); 407 } 408 409 if (!getnfsargs(spec, &iov, &iovlen)) 410 exit(1); 411 412 /* resolve the mountpoint with realpath(3) */ 413 if (checkpath(name, mntpath) != 0) 414 err(1, "%s", mntpath); 415 416 build_iovec(&iov, &iovlen, "fstype", fstype, (size_t)-1); 417 build_iovec(&iov, &iovlen, "fspath", mntpath, (size_t)-1); 418 build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg)); 419 420 /* 421 * XXX: 422 * Backwards compatibility routines for older kernels. 423 * Remove this and fallback_mount() code when we do not need to support 424 * NFS mounts against older kernels which still need 425 * struct nfs_args to be passed in via nmount(). 426 */ 427 osversion = getosreldate(); 428 if (osversion >= 702100) { 429 if (nmount(iov, iovlen, 0)) 430 err(1, "%s, %s", mntpath, errmsg); 431 } else { 432 if (fallback_mount(iov, iovlen)) 433 err(1, "%s, %s", mntpath, errmsg); 434 } 435 436 exit(0); 437 } 438 439 static int 440 findopt(struct iovec *iov, int iovlen, const char *name, 441 char **valuep, int *lenp) 442 { 443 int i; 444 445 for (i = 0; i < iovlen/2; i++, iov += 2) { 446 if (strcmp(name, iov[0].iov_base) == 0) { 447 if (valuep) 448 *valuep = iov[1].iov_base; 449 if (lenp) 450 *lenp = iov[1].iov_len; 451 return (0); 452 } 453 } 454 return (ENOENT); 455 } 456 457 static void 458 copyopt(struct iovec **newiov, int *newiovlen, 459 struct iovec *iov, int iovlen, const char *name) 460 { 461 char *value; 462 int len; 463 464 if (findopt(iov, iovlen, name, &value, &len) == 0) 465 build_iovec(newiov, newiovlen, name, value, len); 466 } 467 468 /* 469 * XXX: This function is provided for backwards 470 * compatibility with older kernels which did not support 471 * passing NFS mount options to nmount() as individual 472 * parameters. It should be eventually be removed. 473 */ 474 static int 475 fallback_mount(struct iovec *iov, int iovlen) 476 { 477 struct nfs_args args = { 478 .version = NFS_ARGSVERSION, 479 .addr = NULL, 480 .addrlen = sizeof (struct sockaddr_in), 481 .sotype = SOCK_STREAM, 482 .proto = 0, 483 .fh = NULL, 484 .fhsize = 0, 485 .flags = NFSMNT_RESVPORT, 486 .wsize = NFS_WSIZE, 487 .rsize = NFS_RSIZE, 488 .readdirsize = NFS_READDIRSIZE, 489 .timeo = 10, 490 .retrans = NFS_RETRANS, 491 .maxgrouplist = NFS_MAXGRPS, 492 .readahead = NFS_DEFRAHEAD, 493 .wcommitsize = 0, /* was: NQ_DEFLEASE */ 494 .deadthresh = NFS_MAXDEADTHRESH, /* was: NQ_DEADTHRESH */ 495 .hostname = NULL, 496 /* args version 4 */ 497 .acregmin = NFS_MINATTRTIMO, 498 .acregmax = NFS_MAXATTRTIMO, 499 .acdirmin = NFS_MINDIRATTRTIMO, 500 .acdirmax = NFS_MAXDIRATTRTIMO, 501 }; 502 int ret; 503 char *opt; 504 struct iovec *newiov; 505 int newiovlen; 506 507 if (findopt(iov, iovlen, "dumbtimer", NULL, NULL) == 0) 508 args.flags |= NFSMNT_DUMBTIMR; 509 if (findopt(iov, iovlen, "noconn", NULL, NULL) == 0) 510 args.flags |= NFSMNT_NOCONN; 511 if (findopt(iov, iovlen, "conn", NULL, NULL) == 0) 512 args.flags |= NFSMNT_NOCONN; 513 if (findopt(iov, iovlen, "nolockd", NULL, NULL) == 0) 514 args.flags |= NFSMNT_NOLOCKD; 515 if (findopt(iov, iovlen, "lockd", NULL, NULL) == 0) 516 args.flags &= ~NFSMNT_NOLOCKD; 517 if (findopt(iov, iovlen, "intr", NULL, NULL) == 0) 518 args.flags |= NFSMNT_INT; 519 if (findopt(iov, iovlen, "rdirplus", NULL, NULL) == 0) 520 args.flags |= NFSMNT_RDIRPLUS; 521 if (findopt(iov, iovlen, "resvport", NULL, NULL) == 0) 522 args.flags |= NFSMNT_RESVPORT; 523 if (findopt(iov, iovlen, "noresvport", NULL, NULL) == 0) 524 args.flags &= ~NFSMNT_RESVPORT; 525 if (findopt(iov, iovlen, "soft", NULL, NULL) == 0) 526 args.flags |= NFSMNT_SOFT; 527 if (findopt(iov, iovlen, "hard", NULL, NULL) == 0) 528 args.flags &= ~NFSMNT_SOFT; 529 if (findopt(iov, iovlen, "mntudp", NULL, NULL) == 0) 530 args.sotype = SOCK_DGRAM; 531 if (findopt(iov, iovlen, "udp", NULL, NULL) == 0) 532 args.sotype = SOCK_DGRAM; 533 if (findopt(iov, iovlen, "tcp", NULL, NULL) == 0) 534 args.sotype = SOCK_STREAM; 535 if (findopt(iov, iovlen, "nfsv3", NULL, NULL) == 0) 536 args.flags |= NFSMNT_NFSV3; 537 if (findopt(iov, iovlen, "readdirsize", &opt, NULL) == 0) { 538 if (opt == NULL) { 539 errx(1, "illegal readdirsize"); 540 } 541 ret = sscanf(opt, "%d", &args.readdirsize); 542 if (ret != 1 || args.readdirsize <= 0) { 543 errx(1, "illegal readdirsize: %s", opt); 544 } 545 args.flags |= NFSMNT_READDIRSIZE; 546 } 547 if (findopt(iov, iovlen, "readahead", &opt, NULL) == 0) { 548 if (opt == NULL) { 549 errx(1, "illegal readahead"); 550 } 551 ret = sscanf(opt, "%d", &args.readahead); 552 if (ret != 1 || args.readahead <= 0) { 553 errx(1, "illegal readahead: %s", opt); 554 } 555 args.flags |= NFSMNT_READAHEAD; 556 } 557 if (findopt(iov, iovlen, "wsize", &opt, NULL) == 0) { 558 if (opt == NULL) { 559 errx(1, "illegal wsize"); 560 } 561 ret = sscanf(opt, "%d", &args.wsize); 562 if (ret != 1 || args.wsize <= 0) { 563 errx(1, "illegal wsize: %s", opt); 564 } 565 args.flags |= NFSMNT_WSIZE; 566 } 567 if (findopt(iov, iovlen, "rsize", &opt, NULL) == 0) { 568 if (opt == NULL) { 569 errx(1, "illegal rsize"); 570 } 571 ret = sscanf(opt, "%d", &args.rsize); 572 if (ret != 1 || args.rsize <= 0) { 573 errx(1, "illegal wsize: %s", opt); 574 } 575 args.flags |= NFSMNT_RSIZE; 576 } 577 if (findopt(iov, iovlen, "retrans", &opt, NULL) == 0) { 578 if (opt == NULL) { 579 errx(1, "illegal retrans"); 580 } 581 ret = sscanf(opt, "%d", &args.retrans); 582 if (ret != 1 || args.retrans <= 0) { 583 errx(1, "illegal retrans: %s", opt); 584 } 585 args.flags |= NFSMNT_RETRANS; 586 } 587 if (findopt(iov, iovlen, "acregmin", &opt, NULL) == 0) { 588 ret = sscanf(opt, "%d", &args.acregmin); 589 if (ret != 1 || args.acregmin < 0) { 590 errx(1, "illegal acregmin: %s", opt); 591 } 592 args.flags |= NFSMNT_ACREGMIN; 593 } 594 if (findopt(iov, iovlen, "acregmax", &opt, NULL) == 0) { 595 ret = sscanf(opt, "%d", &args.acregmax); 596 if (ret != 1 || args.acregmax < 0) { 597 errx(1, "illegal acregmax: %s", opt); 598 } 599 args.flags |= NFSMNT_ACREGMAX; 600 } 601 if (findopt(iov, iovlen, "acdirmin", &opt, NULL) == 0) { 602 ret = sscanf(opt, "%d", &args.acdirmin); 603 if (ret != 1 || args.acdirmin < 0) { 604 errx(1, "illegal acdirmin: %s", opt); 605 } 606 args.flags |= NFSMNT_ACDIRMIN; 607 } 608 if (findopt(iov, iovlen, "acdirmax", &opt, NULL) == 0) { 609 ret = sscanf(opt, "%d", &args.acdirmax); 610 if (ret != 1 || args.acdirmax < 0) { 611 errx(1, "illegal acdirmax: %s", opt); 612 } 613 args.flags |= NFSMNT_ACDIRMAX; 614 } 615 if (findopt(iov, iovlen, "wcommitsize", &opt, NULL) == 0) { 616 ret = sscanf(opt, "%d", &args.wcommitsize); 617 if (ret != 1 || args.wcommitsize < 0) { 618 errx(1, "illegal wcommitsize: %s", opt); 619 } 620 args.flags |= NFSMNT_WCOMMITSIZE; 621 } 622 if (findopt(iov, iovlen, "deadthresh", &opt, NULL) == 0) { 623 ret = sscanf(opt, "%d", &args.deadthresh); 624 if (ret != 1 || args.deadthresh <= 0) { 625 errx(1, "illegal deadthresh: %s", opt); 626 } 627 args.flags |= NFSMNT_DEADTHRESH; 628 } 629 if (findopt(iov, iovlen, "timeout", &opt, NULL) == 0) { 630 ret = sscanf(opt, "%d", &args.timeo); 631 if (ret != 1 || args.timeo <= 0) { 632 errx(1, "illegal timeout: %s", opt); 633 } 634 args.flags |= NFSMNT_TIMEO; 635 } 636 if (findopt(iov, iovlen, "maxgroups", &opt, NULL) == 0) { 637 ret = sscanf(opt, "%d", &args.maxgrouplist); 638 if (ret != 1 || args.timeo <= 0) { 639 errx(1, "illegal maxgroups: %s", opt); 640 } 641 args.flags |= NFSMNT_MAXGRPS; 642 } 643 if (findopt(iov, iovlen, "addr", &opt, 644 &args.addrlen) == 0) { 645 args.addr = (struct sockaddr *) opt; 646 } 647 if (findopt(iov, iovlen, "fh", &opt, &args.fhsize) == 0) { 648 args.fh = opt; 649 } 650 if (findopt(iov, iovlen, "hostname", &args.hostname, 651 NULL) == 0) { 652 } 653 if (args.hostname == NULL) { 654 errx(1, "Invalid hostname"); 655 } 656 657 newiov = NULL; 658 newiovlen = 0; 659 660 build_iovec(&newiov, &newiovlen, "nfs_args", &args, sizeof(args)); 661 copyopt(&newiov, &newiovlen, iov, iovlen, "fstype"); 662 copyopt(&newiov, &newiovlen, iov, iovlen, "fspath"); 663 copyopt(&newiov, &newiovlen, iov, iovlen, "errmsg"); 664 665 return nmount(newiov, newiovlen, 0); 666 } 667 668 static int 669 sec_name_to_num(char *sec) 670 { 671 if (!strcmp(sec, "krb5")) 672 return (RPCSEC_GSS_KRB5); 673 if (!strcmp(sec, "krb5i")) 674 return (RPCSEC_GSS_KRB5I); 675 if (!strcmp(sec, "krb5p")) 676 return (RPCSEC_GSS_KRB5P); 677 if (!strcmp(sec, "sys")) 678 return (AUTH_SYS); 679 return (-1); 680 } 681 682 static char * 683 sec_num_to_name(int flavor) 684 { 685 switch (flavor) { 686 case RPCSEC_GSS_KRB5: 687 return ("krb5"); 688 case RPCSEC_GSS_KRB5I: 689 return ("krb5i"); 690 case RPCSEC_GSS_KRB5P: 691 return ("krb5p"); 692 case AUTH_SYS: 693 return ("sys"); 694 } 695 return (NULL); 696 } 697 698 static int 699 getnfsargs(char *spec, struct iovec **iov, int *iovlen) 700 { 701 struct addrinfo hints, *ai_nfs, *ai; 702 enum tryret ret; 703 int ecode, speclen, remoteerr, offset, have_bracket = 0; 704 char *hostp, *delimp, *errstr; 705 size_t len; 706 static char nam[MNAMELEN + 1], pname[MAXHOSTNAMELEN + 5]; 707 708 if (*spec == '[' && (delimp = strchr(spec + 1, ']')) != NULL && 709 *(delimp + 1) == ':') { 710 hostp = spec + 1; 711 spec = delimp + 2; 712 have_bracket = 1; 713 } else if ((delimp = strrchr(spec, ':')) != NULL) { 714 hostp = spec; 715 spec = delimp + 1; 716 } else if ((delimp = strrchr(spec, '@')) != NULL) { 717 warnx("path@server syntax is deprecated, use server:path"); 718 hostp = delimp + 1; 719 } else { 720 warnx("no <host>:<dirpath> nfs-name"); 721 return (0); 722 } 723 *delimp = '\0'; 724 725 /* 726 * If there has been a trailing slash at mounttime it seems 727 * that some mountd implementations fail to remove the mount 728 * entries from their mountlist while unmounting. 729 */ 730 for (speclen = strlen(spec); 731 speclen > 1 && spec[speclen - 1] == '/'; 732 speclen--) 733 spec[speclen - 1] = '\0'; 734 if (strlen(hostp) + strlen(spec) + 1 > MNAMELEN) { 735 warnx("%s:%s: %s", hostp, spec, strerror(ENAMETOOLONG)); 736 return (0); 737 } 738 /* Make both '@' and ':' notations equal */ 739 if (*hostp != '\0') { 740 len = strlen(hostp); 741 offset = 0; 742 if (have_bracket) 743 nam[offset++] = '['; 744 memmove(nam + offset, hostp, len); 745 if (have_bracket) 746 nam[len + offset++] = ']'; 747 nam[len + offset++] = ':'; 748 memmove(nam + len + offset, spec, speclen); 749 nam[len + speclen + offset] = '\0'; 750 } 751 752 /* 753 * Handle an internet host address. 754 */ 755 memset(&hints, 0, sizeof hints); 756 hints.ai_flags = AI_NUMERICHOST; 757 if (nfsproto == IPPROTO_TCP) 758 hints.ai_socktype = SOCK_STREAM; 759 else if (nfsproto == IPPROTO_UDP) 760 hints.ai_socktype = SOCK_DGRAM; 761 762 if (getaddrinfo(hostp, portspec, &hints, &ai_nfs) != 0) { 763 hints.ai_flags = AI_CANONNAME; 764 if ((ecode = getaddrinfo(hostp, portspec, &hints, &ai_nfs)) 765 != 0) { 766 if (portspec == NULL) 767 errx(1, "%s: %s", hostp, gai_strerror(ecode)); 768 else 769 errx(1, "%s:%s: %s", hostp, portspec, 770 gai_strerror(ecode)); 771 return (0); 772 } 773 774 /* 775 * For a Kerberized nfs mount where the "principal" 776 * argument has not been set, add it here. 777 */ 778 if (got_principal == 0 && secflavor >= 0 && 779 secflavor != AUTH_SYS && ai_nfs->ai_canonname != NULL) { 780 snprintf(pname, sizeof (pname), "nfs@%s", 781 ai_nfs->ai_canonname); 782 build_iovec(iov, iovlen, "principal", pname, 783 strlen(pname) + 1); 784 } 785 } 786 787 ret = TRYRET_LOCALERR; 788 for (;;) { 789 /* 790 * Try each entry returned by getaddrinfo(). Note the 791 * occurrence of remote errors by setting `remoteerr'. 792 */ 793 remoteerr = 0; 794 for (ai = ai_nfs; ai != NULL; ai = ai->ai_next) { 795 if ((ai->ai_family == AF_INET6) && 796 (opflags & OF_NOINET6)) 797 continue; 798 if ((ai->ai_family == AF_INET) && 799 (opflags & OF_NOINET4)) 800 continue; 801 ret = nfs_tryproto(ai, hostp, spec, &errstr, iov, 802 iovlen); 803 if (ret == TRYRET_SUCCESS) 804 break; 805 if (ret != TRYRET_LOCALERR) 806 remoteerr = 1; 807 if ((opflags & ISBGRND) == 0) 808 fprintf(stderr, "%s\n", errstr); 809 } 810 if (ret == TRYRET_SUCCESS) 811 break; 812 813 /* Exit if all errors were local. */ 814 if (!remoteerr) 815 exit(1); 816 817 /* 818 * If retrycnt == 0, we are to keep retrying forever. 819 * Otherwise decrement it, and exit if it hits zero. 820 */ 821 if (retrycnt != 0 && --retrycnt == 0) 822 exit(1); 823 824 if ((opflags & (BGRND | ISBGRND)) == BGRND) { 825 warnx("Cannot immediately mount %s:%s, backgrounding", 826 hostp, spec); 827 opflags |= ISBGRND; 828 if (daemon(0, 0) != 0) 829 err(1, "daemon"); 830 } 831 sleep(60); 832 } 833 freeaddrinfo(ai_nfs); 834 835 build_iovec(iov, iovlen, "hostname", nam, (size_t)-1); 836 /* Add mounted file system to PATH_MOUNTTAB */ 837 if (!add_mtab(hostp, spec)) 838 warnx("can't update %s for %s:%s", PATH_MOUNTTAB, hostp, spec); 839 return (1); 840 } 841 842 /* 843 * Try to set up the NFS arguments according to the address 844 * family, protocol (and possibly port) specified in `ai'. 845 * 846 * Returns TRYRET_SUCCESS if successful, or: 847 * TRYRET_TIMEOUT The server did not respond. 848 * TRYRET_REMOTEERR The server reported an error. 849 * TRYRET_LOCALERR Local failure. 850 * 851 * In all error cases, *errstr will be set to a statically-allocated string 852 * describing the error. 853 */ 854 static enum tryret 855 nfs_tryproto(struct addrinfo *ai, char *hostp, char *spec, char **errstr, 856 struct iovec **iov, int *iovlen) 857 { 858 static char errbuf[256]; 859 struct sockaddr_storage nfs_ss; 860 struct netbuf nfs_nb; 861 struct nfhret nfhret; 862 struct timeval try; 863 struct rpc_err rpcerr; 864 CLIENT *clp; 865 struct netconfig *nconf, *nconf_mnt; 866 const char *netid, *netid_mnt; 867 char *secname; 868 int doconnect, nfsvers, mntvers, sotype; 869 enum clnt_stat stat; 870 enum mountmode trymntmode; 871 872 sotype = 0; 873 trymntmode = mountmode; 874 errbuf[0] = '\0'; 875 *errstr = errbuf; 876 877 if (nfsproto == IPPROTO_TCP) 878 sotype = SOCK_STREAM; 879 else if (nfsproto == IPPROTO_UDP) 880 sotype = SOCK_DGRAM; 881 882 if ((netid = netidbytype(ai->ai_family, sotype)) == NULL) { 883 snprintf(errbuf, sizeof errbuf, 884 "af %d sotype %d not supported", ai->ai_family, sotype); 885 return (TRYRET_LOCALERR); 886 } 887 if ((nconf = getnetconf_cached(netid)) == NULL) { 888 snprintf(errbuf, sizeof errbuf, "%s: %s", netid, nc_sperror()); 889 return (TRYRET_LOCALERR); 890 } 891 /* The RPCPROG_MNT netid may be different. */ 892 if (mnttcp_ok) { 893 netid_mnt = netid; 894 nconf_mnt = nconf; 895 } else { 896 if ((netid_mnt = netidbytype(ai->ai_family, SOCK_DGRAM)) 897 == NULL) { 898 snprintf(errbuf, sizeof errbuf, 899 "af %d sotype SOCK_DGRAM not supported", 900 ai->ai_family); 901 return (TRYRET_LOCALERR); 902 } 903 if ((nconf_mnt = getnetconf_cached(netid_mnt)) == NULL) { 904 snprintf(errbuf, sizeof errbuf, "%s: %s", netid_mnt, 905 nc_sperror()); 906 return (TRYRET_LOCALERR); 907 } 908 } 909 910 tryagain: 911 if (trymntmode == V4) { 912 nfsvers = 4; 913 } else if (trymntmode == V2) { 914 nfsvers = 2; 915 mntvers = 1; 916 } else { 917 nfsvers = 3; 918 mntvers = 3; 919 } 920 921 if (portspec != NULL) { 922 /* `ai' contains the complete nfsd sockaddr. */ 923 nfs_nb.buf = ai->ai_addr; 924 nfs_nb.len = nfs_nb.maxlen = ai->ai_addrlen; 925 } else { 926 /* Ask the remote rpcbind. */ 927 nfs_nb.buf = &nfs_ss; 928 nfs_nb.len = nfs_nb.maxlen = sizeof nfs_ss; 929 930 if (!rpcb_getaddr(NFS_PROGRAM, nfsvers, nconf, &nfs_nb, 931 hostp)) { 932 if (rpc_createerr.cf_stat == RPC_PROGVERSMISMATCH && 933 trymntmode == ANY) { 934 trymntmode = V2; 935 goto tryagain; 936 } 937 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", 938 netid, hostp, spec, 939 clnt_spcreateerror("RPCPROG_NFS")); 940 return (returncode(rpc_createerr.cf_stat, 941 &rpc_createerr.cf_error)); 942 } 943 } 944 945 /* Check that the server (nfsd) responds on the port we have chosen. */ 946 clp = clnt_tli_create(RPC_ANYFD, nconf, &nfs_nb, NFS_PROGRAM, nfsvers, 947 0, 0); 948 if (clp == NULL) { 949 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid, 950 hostp, spec, clnt_spcreateerror("nfsd: RPCPROG_NFS")); 951 return (returncode(rpc_createerr.cf_stat, 952 &rpc_createerr.cf_error)); 953 } 954 if (sotype == SOCK_DGRAM && noconn == 0) { 955 /* 956 * Use connect(), to match what the kernel does. This 957 * catches cases where the server responds from the 958 * wrong source address. 959 */ 960 doconnect = 1; 961 if (!clnt_control(clp, CLSET_CONNECT, (char *)&doconnect)) { 962 clnt_destroy(clp); 963 snprintf(errbuf, sizeof errbuf, 964 "[%s] %s:%s: CLSET_CONNECT failed", netid, hostp, 965 spec); 966 return (TRYRET_LOCALERR); 967 } 968 } 969 970 try.tv_sec = 10; 971 try.tv_usec = 0; 972 stat = clnt_call(clp, NFSPROC_NULL, (xdrproc_t)xdr_void, NULL, 973 (xdrproc_t)xdr_void, NULL, try); 974 if (stat != RPC_SUCCESS) { 975 if (stat == RPC_PROGVERSMISMATCH && trymntmode == ANY) { 976 clnt_destroy(clp); 977 trymntmode = V2; 978 goto tryagain; 979 } 980 clnt_geterr(clp, &rpcerr); 981 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid, 982 hostp, spec, clnt_sperror(clp, "NFSPROC_NULL")); 983 clnt_destroy(clp); 984 return (returncode(stat, &rpcerr)); 985 } 986 clnt_destroy(clp); 987 988 /* 989 * For NFSv4, there is no mount protocol. 990 */ 991 if (trymntmode == V4) { 992 /* 993 * Store the server address in nfsargsp, making 994 * sure to copy any locally allocated structures. 995 */ 996 addrlen = nfs_nb.len; 997 addr = malloc(addrlen); 998 if (addr == NULL) 999 err(1, "malloc"); 1000 bcopy(nfs_nb.buf, addr, addrlen); 1001 1002 build_iovec(iov, iovlen, "addr", addr, addrlen); 1003 secname = sec_num_to_name(secflavor); 1004 if (secname != NULL) 1005 build_iovec(iov, iovlen, "sec", secname, (size_t)-1); 1006 build_iovec(iov, iovlen, "nfsv4", NULL, 0); 1007 build_iovec(iov, iovlen, "dirpath", spec, (size_t)-1); 1008 1009 return (TRYRET_SUCCESS); 1010 } 1011 1012 /* Send the MOUNTPROC_MNT RPC to get the root filehandle. */ 1013 try.tv_sec = 10; 1014 try.tv_usec = 0; 1015 clp = clnt_tp_create(hostp, MOUNTPROG, mntvers, nconf_mnt); 1016 if (clp == NULL) { 1017 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid_mnt, 1018 hostp, spec, clnt_spcreateerror("RPCMNT: clnt_create")); 1019 return (returncode(rpc_createerr.cf_stat, 1020 &rpc_createerr.cf_error)); 1021 } 1022 clp->cl_auth = authsys_create_default(); 1023 nfhret.auth = secflavor; 1024 nfhret.vers = mntvers; 1025 stat = clnt_call(clp, MOUNTPROC_MNT, (xdrproc_t)xdr_dir, spec, 1026 (xdrproc_t)xdr_fh, &nfhret, 1027 try); 1028 auth_destroy(clp->cl_auth); 1029 if (stat != RPC_SUCCESS) { 1030 if (stat == RPC_PROGVERSMISMATCH && trymntmode == ANY) { 1031 clnt_destroy(clp); 1032 trymntmode = V2; 1033 goto tryagain; 1034 } 1035 clnt_geterr(clp, &rpcerr); 1036 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid_mnt, 1037 hostp, spec, clnt_sperror(clp, "RPCPROG_MNT")); 1038 clnt_destroy(clp); 1039 return (returncode(stat, &rpcerr)); 1040 } 1041 clnt_destroy(clp); 1042 1043 if (nfhret.stat != 0) { 1044 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid_mnt, 1045 hostp, spec, strerror(nfhret.stat)); 1046 return (TRYRET_REMOTEERR); 1047 } 1048 1049 /* 1050 * Store the filehandle and server address in nfsargsp, making 1051 * sure to copy any locally allocated structures. 1052 */ 1053 addrlen = nfs_nb.len; 1054 addr = malloc(addrlen); 1055 fhsize = nfhret.fhsize; 1056 fh = malloc(fhsize); 1057 if (addr == NULL || fh == NULL) 1058 err(1, "malloc"); 1059 bcopy(nfs_nb.buf, addr, addrlen); 1060 bcopy(nfhret.nfh, fh, fhsize); 1061 1062 build_iovec(iov, iovlen, "addr", addr, addrlen); 1063 build_iovec(iov, iovlen, "fh", fh, fhsize); 1064 secname = sec_num_to_name(nfhret.auth); 1065 if (secname) 1066 build_iovec(iov, iovlen, "sec", secname, (size_t)-1); 1067 if (nfsvers == 3) 1068 build_iovec(iov, iovlen, "nfsv3", NULL, 0); 1069 1070 return (TRYRET_SUCCESS); 1071 } 1072 1073 /* 1074 * Catagorise a RPC return status and error into an `enum tryret' 1075 * return code. 1076 */ 1077 static enum tryret 1078 returncode(enum clnt_stat stat, struct rpc_err *rpcerr) 1079 { 1080 switch (stat) { 1081 case RPC_TIMEDOUT: 1082 return (TRYRET_TIMEOUT); 1083 case RPC_PMAPFAILURE: 1084 case RPC_PROGNOTREGISTERED: 1085 case RPC_PROGVERSMISMATCH: 1086 /* XXX, these can be local or remote. */ 1087 case RPC_CANTSEND: 1088 case RPC_CANTRECV: 1089 return (TRYRET_REMOTEERR); 1090 case RPC_SYSTEMERROR: 1091 switch (rpcerr->re_errno) { 1092 case ETIMEDOUT: 1093 return (TRYRET_TIMEOUT); 1094 case ENOMEM: 1095 break; 1096 default: 1097 return (TRYRET_REMOTEERR); 1098 } 1099 /* FALLTHROUGH */ 1100 default: 1101 break; 1102 } 1103 return (TRYRET_LOCALERR); 1104 } 1105 1106 /* 1107 * Look up a netid based on an address family and socket type. 1108 * `af' is the address family, and `sotype' is SOCK_DGRAM or SOCK_STREAM. 1109 * 1110 * XXX there should be a library function for this. 1111 */ 1112 static const char * 1113 netidbytype(int af, int sotype) 1114 { 1115 struct nc_protos *p; 1116 1117 for (p = nc_protos; p->netid != NULL; p++) { 1118 if (af != p->af || sotype != p->sotype) 1119 continue; 1120 return (p->netid); 1121 } 1122 return (NULL); 1123 } 1124 1125 /* 1126 * Look up a netconfig entry based on a netid, and cache the result so 1127 * that we don't need to remember to call freenetconfigent(). 1128 * 1129 * Otherwise it behaves just like getnetconfigent(), so nc_*error() 1130 * work on failure. 1131 */ 1132 static struct netconfig * 1133 getnetconf_cached(const char *netid) 1134 { 1135 static struct nc_entry { 1136 struct netconfig *nconf; 1137 struct nc_entry *next; 1138 } *head; 1139 struct nc_entry *p; 1140 struct netconfig *nconf; 1141 1142 for (p = head; p != NULL; p = p->next) 1143 if (strcmp(netid, p->nconf->nc_netid) == 0) 1144 return (p->nconf); 1145 1146 if ((nconf = getnetconfigent(netid)) == NULL) 1147 return (NULL); 1148 if ((p = malloc(sizeof(*p))) == NULL) 1149 err(1, "malloc"); 1150 p->nconf = nconf; 1151 p->next = head; 1152 head = p; 1153 1154 return (p->nconf); 1155 } 1156 1157 /* 1158 * xdr routines for mount rpc's 1159 */ 1160 static int 1161 xdr_dir(XDR *xdrsp, char *dirp) 1162 { 1163 return (xdr_string(xdrsp, &dirp, MNTPATHLEN)); 1164 } 1165 1166 static int 1167 xdr_fh(XDR *xdrsp, struct nfhret *np) 1168 { 1169 int i; 1170 long auth, authcnt, authfnd = 0; 1171 1172 if (!xdr_u_long(xdrsp, &np->stat)) 1173 return (0); 1174 if (np->stat) 1175 return (1); 1176 switch (np->vers) { 1177 case 1: 1178 np->fhsize = NFS_FHSIZE; 1179 return (xdr_opaque(xdrsp, (caddr_t)np->nfh, NFS_FHSIZE)); 1180 case 3: 1181 if (!xdr_long(xdrsp, &np->fhsize)) 1182 return (0); 1183 if (np->fhsize <= 0 || np->fhsize > NFS3_FHSIZE) 1184 return (0); 1185 if (!xdr_opaque(xdrsp, (caddr_t)np->nfh, np->fhsize)) 1186 return (0); 1187 if (!xdr_long(xdrsp, &authcnt)) 1188 return (0); 1189 for (i = 0; i < authcnt; i++) { 1190 if (!xdr_long(xdrsp, &auth)) 1191 return (0); 1192 if (np->auth == -1) { 1193 np->auth = auth; 1194 authfnd++; 1195 } else if (auth == np->auth) { 1196 authfnd++; 1197 } 1198 } 1199 /* 1200 * Some servers, such as DEC's OSF/1 return a nil authenticator 1201 * list to indicate RPCAUTH_UNIX. 1202 */ 1203 if (authcnt == 0 && np->auth == -1) 1204 np->auth = AUTH_SYS; 1205 if (!authfnd && (authcnt > 0 || np->auth != AUTH_SYS)) 1206 np->stat = EAUTH; 1207 return (1); 1208 }; 1209 return (0); 1210 } 1211 1212 static void 1213 usage(void) 1214 { 1215 (void)fprintf(stderr, "%s\n%s\n%s\n%s\n", 1216 "usage: mount_nfs [-23bcdiLlNPsTU] [-a maxreadahead] [-D deadthresh]", 1217 " [-g maxgroups] [-I readdirsize] [-o options] [-R retrycnt]", 1218 " [-r readsize] [-t timeout] [-w writesize] [-x retrans]", 1219 " rhost:path node"); 1220 exit(1); 1221 } 1222