1 /* 2 * Copyright (c) 1992, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Rick Macklem at The University of Guelph. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#) Copyright (c) 1992, 1993, 1994 The Regents of the University of California. All rights reserved. 37 * @(#)mount_nfs.c 8.11 (Berkeley) 5/4/95 38 * $FreeBSD: src/sbin/mount_nfs/mount_nfs.c,v 1.36.2.6 2003/05/13 14:45:40 trhodes Exp $ 39 * $DragonFly: src/sbin/mount_nfs/mount_nfs.c,v 1.9 2004/12/18 21:43:39 swildner Exp $ 40 */ 41 42 #include <sys/param.h> 43 #include <sys/mount.h> 44 #include <sys/stat.h> 45 #include <sys/syslog.h> 46 47 #include <rpc/rpc.h> 48 #include <rpc/pmap_clnt.h> 49 #include <rpc/pmap_prot.h> 50 51 #include <vfs/nfs/rpcv2.h> 52 #include <vfs/nfs/nfsproto.h> 53 #include <vfs/nfs/nfs.h> 54 #include <vfs/nfs/nqnfs.h> 55 56 #include <arpa/inet.h> 57 58 #include <ctype.h> 59 #include <err.h> 60 #include <errno.h> 61 #include <netdb.h> 62 #include <stdio.h> 63 #include <stdlib.h> 64 #include <strings.h> 65 #include <sysexits.h> 66 #include <unistd.h> 67 #include <resolv.h> 68 69 #include "mntopts.h" 70 #include "mounttab.h" 71 72 #define ALTF_BG 0x1 73 #define ALTF_NOCONN 0x2 74 #define ALTF_DUMBTIMR 0x4 75 #define ALTF_INTR 0x8 76 #define ALTF_KERB 0x10 77 #define ALTF_NFSV3 0x20 78 #define ALTF_RDIRPLUS 0x40 79 #define ALTF_MNTUDP 0x80 80 #define ALTF_RESVPORT 0x100 81 #define ALTF_SEQPACKET 0x200 82 #define ALTF_NQNFS 0x400 83 #define ALTF_SOFT 0x800 84 #define ALTF_TCP 0x1000 85 #define ALTF_PORT 0x2000 86 #define ALTF_NFSV2 0x4000 87 #define ALTF_ACREGMIN 0x8000 88 #define ALTF_ACREGMAX 0x10000 89 #define ALTF_ACDIRMIN 0x20000 90 #define ALTF_ACDIRMAX 0x40000 91 92 struct mntopt mopts[] = { 93 MOPT_STDOPTS, 94 MOPT_FORCE, 95 MOPT_UPDATE, 96 MOPT_ASYNC, 97 { "bg", 0, ALTF_BG, 1 }, 98 { "conn", 1, ALTF_NOCONN, 1 }, 99 { "dumbtimer", 0, ALTF_DUMBTIMR, 1 }, 100 { "intr", 0, ALTF_INTR, 1 }, 101 #ifdef NFSKERB 102 { "kerb", 0, ALTF_KERB, 1 }, 103 #endif 104 { "nfsv3", 0, ALTF_NFSV3, 1 }, 105 { "rdirplus", 0, ALTF_RDIRPLUS, 1 }, 106 { "mntudp", 0, ALTF_MNTUDP, 1 }, 107 { "resvport", 0, ALTF_RESVPORT, 1 }, 108 { "nqnfs", 0, ALTF_NQNFS, 1 }, 109 { "soft", 0, ALTF_SOFT, 1 }, 110 { "tcp", 0, ALTF_TCP, 1 }, 111 { "port=", 0, ALTF_PORT, 1 }, 112 { "nfsv2", 0, ALTF_NFSV2, 1 }, 113 { "acregmin=", 0, ALTF_ACREGMIN, 1 }, 114 { "acregmax=", 0, ALTF_ACREGMAX, 1 }, 115 { "acdirmin=", 0, ALTF_ACDIRMIN, 1 }, 116 { "acdirmax=", 0, ALTF_ACDIRMAX, 1 }, 117 { NULL } 118 }; 119 120 struct nfs_args nfsdefargs = { 121 NFS_ARGSVERSION, 122 (struct sockaddr *)0, 123 sizeof (struct sockaddr_in), 124 SOCK_DGRAM, 125 0, 126 (u_char *)0, 127 0, 128 NFSMNT_RESVPORT, 129 NFS_WSIZE, 130 NFS_RSIZE, 131 NFS_READDIRSIZE, 132 10, 133 NFS_RETRANS, 134 NFS_MAXGRPS, 135 NFS_DEFRAHEAD, 136 NQ_DEFLEASE, 137 NQ_DEADTHRESH, 138 (char *)0, 139 /* args version 4 */ 140 NFS_MINATTRTIMO, 141 NFS_MAXATTRTIMO, 142 NFS_MINDIRATTRTIMO, 143 NFS_MAXDIRATTRTIMO, 144 }; 145 146 struct nfhret { 147 u_long stat; 148 long vers; 149 long auth; 150 long fhsize; 151 u_char nfh[NFSX_V3FHMAX]; 152 }; 153 154 #define BGRND 0x0001 155 #define ISBGRND 0x0002 156 #define DIDWARN 0x0004 157 158 int retrycnt = -1; 159 int opflags = 0; 160 int nfsproto = IPPROTO_UDP; 161 int mnttcp_ok = 1; 162 u_short port_no = 0; 163 enum mountmode { 164 ANY, 165 V2, 166 V3 167 } mountmode = ANY; 168 169 #ifdef NFSKERB 170 char inst[INST_SZ]; 171 char realm[REALM_SZ]; 172 struct { 173 u_long kind; 174 KTEXT_ST kt; 175 } ktick; 176 struct nfsrpc_nickverf kverf; 177 struct nfsrpc_fullblock kin, kout; 178 NFSKERBKEY_T kivec; 179 CREDENTIALS kcr; 180 struct timeval ktv; 181 NFSKERBKEYSCHED_T kerb_keysched; 182 #endif 183 184 /* Return codes for nfs_tryproto. */ 185 enum tryret { 186 TRYRET_SUCCESS, 187 TRYRET_TIMEOUT, /* No response received. */ 188 TRYRET_REMOTEERR, /* Error received from remote server. */ 189 TRYRET_LOCALERR /* Local failure. */ 190 }; 191 192 int getnfsargs(char *, struct nfs_args *); 193 void set_rpc_maxgrouplist(int); 194 void usage(void) __dead2; 195 int xdr_dir(XDR *, char *); 196 int xdr_fh(XDR *, struct nfhret *); 197 enum tryret nfs_tryproto(struct nfs_args *nfsargsp, struct sockaddr_in *sin, 198 char *hostp, char *spec, char **errstr); 199 enum tryret returncode(enum clnt_stat stat, struct rpc_err *rpcerr); 200 201 /* 202 * Used to set mount flags with getmntopts. Call with dir=TRUE to 203 * initialize altflags from the current mount flags. Call with 204 * dir=FALSE to update mount flags with the new value of altflags after 205 * the call to getmntopts. 206 */ 207 static void 208 set_flags(int* altflags, int* nfsflags, int dir) 209 { 210 #define F2(af, nf) \ 211 if (dir) { \ 212 if (*nfsflags & NFSMNT_##nf) \ 213 *altflags |= ALTF_##af; \ 214 else \ 215 *altflags &= ~ALTF_##af; \ 216 } else { \ 217 if (*altflags & ALTF_##af) \ 218 *nfsflags |= NFSMNT_##nf; \ 219 else \ 220 *nfsflags &= ~NFSMNT_##nf; \ 221 } 222 #define F(f) F2(f,f) 223 224 F(NOCONN); 225 F(DUMBTIMR); 226 F2(INTR, INT); 227 #ifdef NFSKERB 228 F(KERB); 229 #endif 230 F(RDIRPLUS); 231 F(RESVPORT); 232 F(NQNFS); 233 F(SOFT); 234 F(ACREGMIN); 235 F(ACREGMAX); 236 F(ACDIRMIN); 237 F(ACDIRMAX); 238 239 #undef F 240 #undef F2 241 } 242 243 int 244 main(int argc, char **argv) 245 { 246 register int c; 247 register struct nfs_args *nfsargsp; 248 struct nfs_args nfsargs; 249 struct nfsd_cargs ncd; 250 int mntflags, altflags, nfssvc_flag, num; 251 char *name, *p, *spec; 252 char mntpath[MAXPATHLEN]; 253 struct vfsconf vfc; 254 int error = 0; 255 #ifdef NFSKERB 256 uid_t last_ruid; 257 258 last_ruid = -1; 259 strcpy(realm, KRB_REALM); 260 if (sizeof (struct nfsrpc_nickverf) != RPCX_NICKVERF || 261 sizeof (struct nfsrpc_fullblock) != RPCX_FULLBLOCK || 262 ((char *)&ktick.kt) - ((char *)&ktick) != NFSX_UNSIGNED || 263 ((char *)ktick.kt.dat) - ((char *)&ktick) != 2 * NFSX_UNSIGNED) 264 fprintf(stderr, "Yikes! NFSKERB structs not packed!!\n"); 265 #endif /* NFSKERB */ 266 267 mntflags = 0; 268 altflags = 0; 269 nfsargs = nfsdefargs; 270 nfsargsp = &nfsargs; 271 while ((c = getopt(argc, argv, 272 "23a:bcdD:g:I:iKL:lm:No:PqR:r:sTt:w:x:U")) != -1) 273 switch (c) { 274 case '2': 275 mountmode = V2; 276 break; 277 case '3': 278 mountmode = V3; 279 break; 280 case 'a': 281 num = strtol(optarg, &p, 10); 282 if (*p || num < 0) 283 errx(1, "illegal -a value -- %s", optarg); 284 nfsargsp->readahead = num; 285 nfsargsp->flags |= NFSMNT_READAHEAD; 286 break; 287 case 'b': 288 opflags |= BGRND; 289 break; 290 case 'c': 291 nfsargsp->flags |= NFSMNT_NOCONN; 292 break; 293 case 'D': 294 num = strtol(optarg, &p, 10); 295 if (*p || num <= 0) 296 errx(1, "illegal -D value -- %s", optarg); 297 nfsargsp->deadthresh = num; 298 nfsargsp->flags |= NFSMNT_DEADTHRESH; 299 break; 300 case 'd': 301 nfsargsp->flags |= NFSMNT_DUMBTIMR; 302 break; 303 case 'g': 304 num = strtol(optarg, &p, 10); 305 if (*p || num <= 0) 306 errx(1, "illegal -g value -- %s", optarg); 307 set_rpc_maxgrouplist(num); 308 nfsargsp->maxgrouplist = num; 309 nfsargsp->flags |= NFSMNT_MAXGRPS; 310 break; 311 case 'I': 312 num = strtol(optarg, &p, 10); 313 if (*p || num <= 0) 314 errx(1, "illegal -I value -- %s", optarg); 315 nfsargsp->readdirsize = num; 316 nfsargsp->flags |= NFSMNT_READDIRSIZE; 317 break; 318 case 'i': 319 nfsargsp->flags |= NFSMNT_INT; 320 break; 321 #ifdef NFSKERB 322 case 'K': 323 nfsargsp->flags |= NFSMNT_KERB; 324 break; 325 #endif 326 case 'L': 327 num = strtol(optarg, &p, 10); 328 if (*p || num < 2) 329 errx(1, "illegal -L value -- %s", optarg); 330 nfsargsp->leaseterm = num; 331 nfsargsp->flags |= NFSMNT_LEASETERM; 332 break; 333 case 'l': 334 nfsargsp->flags |= NFSMNT_RDIRPLUS; 335 break; 336 #ifdef NFSKERB 337 case 'm': 338 strncpy(realm, optarg, REALM_SZ - 1); 339 realm[REALM_SZ - 1] = '\0'; 340 break; 341 #endif 342 case 'N': 343 nfsargsp->flags &= ~NFSMNT_RESVPORT; 344 break; 345 case 'o': 346 altflags = 0; 347 set_flags(&altflags, &nfsargsp->flags, TRUE); 348 if (mountmode == V2) 349 altflags |= ALTF_NFSV2; 350 else if (mountmode == V3) 351 altflags |= ALTF_NFSV3; 352 getmntopts(optarg, mopts, &mntflags, &altflags); 353 set_flags(&altflags, &nfsargsp->flags, FALSE); 354 /* 355 * Handle altflags which don't map directly to 356 * mount flags. 357 */ 358 if(altflags & ALTF_BG) 359 opflags |= BGRND; 360 if(altflags & ALTF_MNTUDP) 361 mnttcp_ok = 0; 362 if(altflags & ALTF_TCP) { 363 nfsargsp->sotype = SOCK_STREAM; 364 nfsproto = IPPROTO_TCP; 365 } 366 if(altflags & ALTF_PORT) 367 port_no = atoi(strstr(optarg, "port=") + 5); 368 mountmode = ANY; 369 if(altflags & ALTF_NFSV2) 370 mountmode = V2; 371 if(altflags & ALTF_NFSV3) 372 mountmode = V3; 373 if(altflags & ALTF_ACREGMIN) 374 nfsargsp->acregmin = atoi(strstr(optarg, 375 "acregmin=") + 9); 376 if(altflags & ALTF_ACREGMAX) 377 nfsargsp->acregmax = atoi(strstr(optarg, 378 "acregmax=") + 9); 379 if(altflags & ALTF_ACDIRMIN) 380 nfsargsp->acdirmin = atoi(strstr(optarg, 381 "acdirmin=") + 9); 382 if(altflags & ALTF_ACDIRMAX) 383 nfsargsp->acdirmax = atoi(strstr(optarg, 384 "acdirmax=") + 9); 385 break; 386 case 'P': 387 /* obsolete for NFSMNT_RESVPORT, now default */ 388 break; 389 case 'q': 390 mountmode = V3; 391 nfsargsp->flags |= NFSMNT_NQNFS; 392 break; 393 case 'R': 394 num = strtol(optarg, &p, 10); 395 if (*p || num < 0) 396 errx(1, "illegal -R value -- %s", optarg); 397 retrycnt = num; 398 break; 399 case 'r': 400 num = strtol(optarg, &p, 10); 401 if (*p || num <= 0) 402 errx(1, "illegal -r value -- %s", optarg); 403 nfsargsp->rsize = num; 404 nfsargsp->flags |= NFSMNT_RSIZE; 405 break; 406 case 's': 407 nfsargsp->flags |= NFSMNT_SOFT; 408 break; 409 case 'T': 410 nfsargsp->sotype = SOCK_STREAM; 411 nfsproto = IPPROTO_TCP; 412 break; 413 case 't': 414 num = strtol(optarg, &p, 10); 415 if (*p || num <= 0) 416 errx(1, "illegal -t value -- %s", optarg); 417 nfsargsp->timeo = num; 418 nfsargsp->flags |= NFSMNT_TIMEO; 419 break; 420 case 'w': 421 num = strtol(optarg, &p, 10); 422 if (*p || num <= 0) 423 errx(1, "illegal -w value -- %s", optarg); 424 nfsargsp->wsize = num; 425 nfsargsp->flags |= NFSMNT_WSIZE; 426 break; 427 case 'x': 428 num = strtol(optarg, &p, 10); 429 if (*p || num <= 0) 430 errx(1, "illegal -x value -- %s", optarg); 431 nfsargsp->retrans = num; 432 nfsargsp->flags |= NFSMNT_RETRANS; 433 break; 434 case 'U': 435 mnttcp_ok = 0; 436 break; 437 default: 438 usage(); 439 break; 440 } 441 argc -= optind; 442 argv += optind; 443 444 if (argc != 2) { 445 usage(); 446 /* NOTREACHED */ 447 } 448 449 spec = *argv++; 450 name = *argv; 451 452 if (retrycnt == -1) 453 /* The default is to keep retrying forever. */ 454 retrycnt = 0; 455 if (!getnfsargs(spec, nfsargsp)) 456 exit(1); 457 458 /* resolve the mountpoint with realpath(3) */ 459 checkpath(name, mntpath); 460 461 error = getvfsbyname("nfs", &vfc); 462 if (error && vfsisloadable("nfs")) { 463 if(vfsload("nfs")) 464 err(EX_OSERR, "vfsload(nfs)"); 465 endvfsent(); /* clear cache */ 466 error = getvfsbyname("nfs", &vfc); 467 } 468 if (error) 469 errx(EX_OSERR, "nfs filesystem is not available"); 470 471 if (mount(vfc.vfc_name, mntpath, mntflags, nfsargsp)) 472 err(1, "%s", mntpath); 473 if (nfsargsp->flags & (NFSMNT_NQNFS | NFSMNT_KERB)) { 474 if ((opflags & ISBGRND) == 0) { 475 if (daemon(0, 0) != 0) 476 err(1, "daemon"); 477 } 478 openlog("mount_nfs", LOG_PID, LOG_DAEMON); 479 nfssvc_flag = NFSSVC_MNTD; 480 ncd.ncd_dirp = mntpath; 481 while (nfssvc(nfssvc_flag, (caddr_t)&ncd) < 0) { 482 if (errno != ENEEDAUTH) { 483 syslog(LOG_ERR, "nfssvc err %m"); 484 continue; 485 } 486 nfssvc_flag = 487 NFSSVC_MNTD | NFSSVC_GOTAUTH | NFSSVC_AUTHINFAIL; 488 #ifdef NFSKERB 489 /* 490 * Set up as ncd_authuid for the kerberos call. 491 * Must set ruid to ncd_authuid and reset the 492 * ticket name iff ncd_authuid is not the same 493 * as last time, so that the right ticket file 494 * is found. 495 * Get the Kerberos credential structure so that 496 * we have the session key and get a ticket for 497 * this uid. 498 * For more info see the IETF Draft "Authentication 499 * in ONC RPC". 500 */ 501 if (ncd.ncd_authuid != last_ruid) { 502 char buf[512]; 503 sprintf(buf, "%s%d", TKT_ROOT, ncd.ncd_authuid); 504 krb_set_tkt_string(buf); 505 last_ruid = ncd.ncd_authuid; 506 } 507 setreuid(ncd.ncd_authuid, 0); 508 kret = krb_get_cred(NFS_KERBSRV, inst, realm, &kcr); 509 if (kret == RET_NOTKT) { 510 kret = get_ad_tkt(NFS_KERBSRV, inst, realm, 511 DEFAULT_TKT_LIFE); 512 if (kret == KSUCCESS) 513 kret = krb_get_cred(NFS_KERBSRV, inst, realm, 514 &kcr); 515 } 516 if (kret == KSUCCESS) 517 kret = krb_mk_req(&ktick.kt, NFS_KERBSRV, inst, 518 realm, 0); 519 520 /* 521 * Fill in the AKN_FULLNAME authenticator and verifier. 522 * Along with the Kerberos ticket, we need to build 523 * the timestamp verifier and encrypt it in CBC mode. 524 */ 525 if (kret == KSUCCESS && 526 ktick.kt.length <= (RPCAUTH_MAXSIZ-3*NFSX_UNSIGNED) 527 && gettimeofday(&ktv, (struct timezone *)0) == 0) { 528 ncd.ncd_authtype = RPCAUTH_KERB4; 529 ncd.ncd_authstr = (u_char *)&ktick; 530 ncd.ncd_authlen = nfsm_rndup(ktick.kt.length) + 531 3 * NFSX_UNSIGNED; 532 ncd.ncd_verfstr = (u_char *)&kverf; 533 ncd.ncd_verflen = sizeof (kverf); 534 memmove(ncd.ncd_key, kcr.session, 535 sizeof (kcr.session)); 536 kin.t1 = htonl(ktv.tv_sec); 537 kin.t2 = htonl(ktv.tv_usec); 538 kin.w1 = htonl(NFS_KERBTTL); 539 kin.w2 = htonl(NFS_KERBTTL - 1); 540 bzero((caddr_t)kivec, sizeof (kivec)); 541 542 /* 543 * Encrypt kin in CBC mode using the session 544 * key in kcr. 545 */ 546 XXX 547 548 /* 549 * Finally, fill the timestamp verifier into the 550 * authenticator and verifier. 551 */ 552 ktick.kind = htonl(RPCAKN_FULLNAME); 553 kverf.kind = htonl(RPCAKN_FULLNAME); 554 NFS_KERBW1(ktick.kt) = kout.w1; 555 ktick.kt.length = htonl(ktick.kt.length); 556 kverf.verf.t1 = kout.t1; 557 kverf.verf.t2 = kout.t2; 558 kverf.verf.w2 = kout.w2; 559 nfssvc_flag = NFSSVC_MNTD | NFSSVC_GOTAUTH; 560 } 561 setreuid(0, 0); 562 #endif /* NFSKERB */ 563 } 564 } 565 exit(0); 566 } 567 568 int 569 getnfsargs(char *spec, struct nfs_args *nfsargsp) 570 { 571 struct hostent *hp; 572 struct sockaddr_in saddr; 573 enum tryret ret; 574 int speclen, remoteerr; 575 char *hostp, *delimp, *errstr; 576 #ifdef NFSKERB 577 char *cp; 578 #endif 579 size_t len; 580 static char nam[MNAMELEN + 1]; 581 582 if ((delimp = strrchr(spec, ':')) != NULL) { 583 hostp = spec; 584 spec = delimp + 1; 585 } else if ((delimp = strrchr(spec, '@')) != NULL) { 586 warnx("path@server syntax is deprecated, use server:path"); 587 hostp = delimp + 1; 588 } else { 589 warnx("no <host>:<dirpath> nfs-name"); 590 return (0); 591 } 592 *delimp = '\0'; 593 594 /* 595 * If there has been a trailing slash at mounttime it seems 596 * that some mountd implementations fail to remove the mount 597 * entries from their mountlist while unmounting. 598 */ 599 for (speclen = strlen(spec); 600 speclen > 1 && spec[speclen - 1] == '/'; 601 speclen--) 602 spec[speclen - 1] = '\0'; 603 if (strlen(hostp) + strlen(spec) + 1 > MNAMELEN) { 604 warnx("%s:%s: %s", hostp, spec, strerror(ENAMETOOLONG)); 605 return (0); 606 } 607 /* Make both '@' and ':' notations equal */ 608 if (*hostp != '\0') { 609 len = strlen(hostp); 610 memmove(nam, hostp, len); 611 nam[len] = ':'; 612 memmove(nam + len + 1, spec, speclen); 613 nam[len + speclen + 1] = '\0'; 614 } 615 616 /* 617 * Handle an internet host address and reverse resolve it if 618 * doing Kerberos. 619 */ 620 bzero(&saddr, sizeof saddr); 621 saddr.sin_family = AF_INET; 622 saddr.sin_len = sizeof saddr; 623 624 if (port_no != 0) 625 saddr.sin_port = htons(port_no); 626 627 for (;;) { 628 int haserror = 0; 629 630 /* 631 * Adjust DNS timeouts so we do not linger in the foreground 632 * if we can be backgrounded. 633 */ 634 switch(opflags & (BGRND | ISBGRND)) { 635 case BGRND: 636 _res.retry = 1; 637 _res.retrans = 1; 638 break; 639 case BGRND|ISBGRND: 640 _res.retry = 3; 641 _res.retrans = 3; 642 break; 643 } 644 if (isdigit(*hostp)) { 645 if ((saddr.sin_addr.s_addr = inet_addr(hostp)) == -1) { 646 warnx("bad net address %s", hostp); 647 haserror = EAI_FAIL; 648 } 649 } else if ((hp = gethostbyname(hostp)) != NULL) { 650 memmove(&saddr.sin_addr, hp->h_addr, 651 MIN(hp->h_length, sizeof(saddr.sin_addr))); 652 } else { 653 warnx("can't get net id for host: %s", hostp); 654 opflags |= DIDWARN; 655 haserror = h_errno; 656 } 657 #ifdef NFSKERB 658 if (haserror == 0 && (nfsargsp->flags & NFSMNT_KERB)) { 659 if ((hp = gethostbyaddr((char *)&saddr.sin_addr.s_addr, 660 sizeof (u_long), AF_INET)) == NULL) { 661 warnx("can't reverse resolve net address"); 662 opflags |= DIDWARN; 663 haserror = h_errno; 664 } else { 665 memmove(&saddr.sin_addr, hp->h_addr, 666 MIN(hp->h_length, sizeof(saddr.sin_addr))); 667 strncpy(inst, hp->h_name, INST_SZ); 668 inst[INST_SZ - 1] = '\0'; 669 if (cp = strchr(inst, '.')) 670 *cp = '\0'; 671 } 672 } 673 #endif /* NFSKERB */ 674 /* 675 * If no error occured we do not have to retry again. 676 * Otherwise try to backgruond us if possible. 677 */ 678 if (haserror == 0) 679 break; 680 681 switch(opflags & (BGRND | ISBGRND)) { 682 case BGRND: 683 if (haserror != EAI_AGAIN) 684 return(0); 685 /* recoverable error */ 686 warnx("Cannot immediately mount %s:%s, backgrounding", 687 hostp, spec); 688 opflags |= DIDWARN; 689 opflags |= ISBGRND; 690 if (daemon(0, 0) != 0) 691 err(1, "daemon"); 692 break; 693 default: 694 /* 695 * Already backgrounded or cannot be backgrounded. 696 */ 697 if (haserror != EAI_AGAIN) 698 return(0); 699 break; 700 } 701 sleep(20); 702 endhostent(); 703 } 704 if (opflags & DIDWARN) { 705 opflags &= ~DIDWARN; 706 warnx("successfully resolved %s after prior failures", hostp); 707 } 708 709 ret = TRYRET_LOCALERR; 710 for (;;) { 711 remoteerr = 0; 712 ret = nfs_tryproto(nfsargsp, &saddr, hostp, spec, &errstr); 713 if (ret == TRYRET_SUCCESS) 714 break; 715 if (ret != TRYRET_LOCALERR) 716 remoteerr = 1; 717 if ((opflags & ISBGRND) == 0) 718 fprintf(stderr, "%s\n", errstr); 719 720 /* Exit if all errors were local. */ 721 if (!remoteerr) 722 exit(1); 723 724 /* 725 * If retrycnt == 0, we are to keep retrying forever. 726 * Otherwise decrement it, and exit if it hits zero. 727 */ 728 if (retrycnt != 0 && --retrycnt == 0) 729 exit(1); 730 731 if ((opflags & (BGRND | ISBGRND)) == BGRND) { 732 warnx("Cannot immediately mount %s:%s, backgrounding", 733 hostp, spec); 734 opflags |= ISBGRND; 735 opflags |= DIDWARN; 736 if (daemon(0, 0) != 0) 737 err(1, "daemon"); 738 } 739 sleep(20); 740 } 741 if (opflags & DIDWARN) { 742 opflags &= ~DIDWARN; 743 warnx("successfully mounted %s after prior failures", spec); 744 } 745 nfsargsp->hostname = nam; 746 /* Add mounted filesystem to PATH_MOUNTTAB */ 747 if (!add_mtab(hostp, spec)) 748 warnx("can't update %s for %s:%s", PATH_MOUNTTAB, hostp, spec); 749 return (1); 750 } 751 752 /* 753 * Try to set up the NFS arguments according to the address 754 * (and possibly port) specified by `sinp'. 755 * 756 * Returns TRYRET_SUCCESS if successful, or: 757 * TRYRET_TIMEOUT The server did not respond. 758 * TRYRET_REMOTEERR The server reported an error. 759 * TRYRET_LOCALERR Local failure. 760 * 761 * In all error cases, *errstr will be set to a statically-allocated string 762 * describing the error. 763 */ 764 enum tryret 765 nfs_tryproto(struct nfs_args *nfsargsp, struct sockaddr_in *sinp, char *hostp, 766 char *spec, char **errstr) 767 { 768 static char errbuf[256]; 769 struct sockaddr_in sin, tmpsin; 770 struct nfhret nfhret; 771 struct timeval try; 772 struct rpc_err rpcerr; 773 CLIENT *clp; 774 int doconnect, nfsvers, mntvers, so; 775 enum clnt_stat stat; 776 enum mountmode trymntmode; 777 778 trymntmode = mountmode; 779 errbuf[0] = '\0'; 780 *errstr = errbuf; 781 sin = tmpsin = *sinp; 782 783 tryagain: 784 if (trymntmode == V2) { 785 nfsvers = 2; 786 mntvers = 1; 787 } else { 788 nfsvers = 3; 789 mntvers = 3; 790 } 791 792 /* Check that the server (nfsd) responds on the port we have chosen. */ 793 try.tv_sec = 10; 794 try.tv_usec = 0; 795 so = RPC_ANYSOCK; 796 if (nfsargsp->sotype == SOCK_STREAM) 797 clp = clnttcp_create(&sin, RPCPROG_NFS, nfsvers, &so, 0, 0); 798 else 799 clp = clntudp_create(&sin, RPCPROG_NFS, nfsvers, try, &so); 800 if (clp == NULL) { 801 snprintf(errbuf, sizeof errbuf, "%s:%s: %s", 802 hostp, spec, clnt_spcreateerror("nfsd: RPCPROG_NFS")); 803 return (returncode(rpc_createerr.cf_stat, 804 &rpc_createerr.cf_error)); 805 } 806 if (nfsargsp->sotype == SOCK_DGRAM && 807 !(nfsargsp->flags & NFSMNT_NOCONN)) { 808 /* 809 * Use connect(), to match what the kernel does. This 810 * catches cases where the server responds from the 811 * wrong source address. 812 */ 813 doconnect = 1; 814 if (!clnt_control(clp, CLSET_CONNECT, (char *)&doconnect)) { 815 clnt_destroy(clp); 816 snprintf(errbuf, sizeof errbuf, 817 "%s:%s: CLSET_CONNECT failed", hostp, spec); 818 return (TRYRET_LOCALERR); 819 } 820 } 821 822 try.tv_sec = 10; 823 try.tv_usec = 0; 824 stat = clnt_call(clp, NFSPROC_NULL, xdr_void, NULL, xdr_void, NULL, 825 try); 826 if (stat != RPC_SUCCESS) { 827 if (stat == RPC_PROGVERSMISMATCH && trymntmode == ANY) { 828 clnt_destroy(clp); 829 trymntmode = V2; 830 goto tryagain; 831 } 832 clnt_geterr(clp, &rpcerr); 833 snprintf(errbuf, sizeof errbuf, "%s:%s: %s", 834 hostp, spec, clnt_sperror(clp, "NFSPROC_NULL")); 835 clnt_destroy(clp); 836 return (returncode(stat, &rpcerr)); 837 } 838 clnt_destroy(clp); 839 840 /* Send the RPCMNT_MOUNT RPC to get the root filehandle. */ 841 tmpsin.sin_port = 0; 842 try.tv_sec = 10; 843 try.tv_usec = 0; 844 so = RPC_ANYSOCK; 845 if (mnttcp_ok && nfsargsp->sotype == SOCK_STREAM) 846 clp = clnttcp_create(&tmpsin, RPCPROG_MNT, mntvers, &so, 0, 0); 847 else 848 clp = clntudp_create(&tmpsin, RPCPROG_MNT, mntvers, try, &so); 849 if (clp == NULL) { 850 snprintf(errbuf, sizeof errbuf, "%s:%s: %s", 851 hostp, spec, clnt_spcreateerror("RPCMNT: clnt_create")); 852 return (returncode(rpc_createerr.cf_stat, 853 &rpc_createerr.cf_error)); 854 } 855 clp->cl_auth = authunix_create_default(); 856 if (nfsargsp->flags & NFSMNT_KERB) 857 nfhret.auth = RPCAUTH_KERB4; 858 else 859 nfhret.auth = RPCAUTH_UNIX; 860 nfhret.vers = mntvers; 861 stat = clnt_call(clp, RPCMNT_MOUNT, xdr_dir, spec, xdr_fh, &nfhret, 862 try); 863 auth_destroy(clp->cl_auth); 864 if (stat != RPC_SUCCESS) { 865 if (stat == RPC_PROGVERSMISMATCH && trymntmode == ANY) { 866 clnt_destroy(clp); 867 trymntmode = V2; 868 goto tryagain; 869 } 870 clnt_geterr(clp, &rpcerr); 871 snprintf(errbuf, sizeof errbuf, "%s:%s: %s", 872 hostp, spec, clnt_sperror(clp, "RPCPROG_MNT")); 873 clnt_destroy(clp); 874 return (returncode(stat, &rpcerr)); 875 } 876 clnt_destroy(clp); 877 878 if (nfhret.stat != 0) { 879 snprintf(errbuf, sizeof errbuf, "%s:%s: %s", 880 hostp, spec, strerror(nfhret.stat)); 881 return (TRYRET_REMOTEERR); 882 } 883 884 /* 885 * Store the filehandle and server address in nfsargsp, making 886 * sure to copy any locally allocated structures. 887 */ 888 nfsargsp->addrlen = sin.sin_len; 889 nfsargsp->addr = malloc(nfsargsp->addrlen); 890 nfsargsp->fhsize = nfhret.fhsize; 891 nfsargsp->fh = malloc(nfsargsp->fhsize); 892 if (nfsargsp->addr == NULL || nfsargsp->fh == NULL) 893 err(1, "malloc"); 894 bcopy(&sin, nfsargsp->addr, nfsargsp->addrlen); 895 bcopy(nfhret.nfh, nfsargsp->fh, nfsargsp->fhsize); 896 897 if (nfsvers == 3) 898 nfsargsp->flags |= NFSMNT_NFSV3; 899 else 900 nfsargsp->flags &= ~NFSMNT_NFSV3; 901 902 return (TRYRET_SUCCESS); 903 } 904 905 /* 906 * Catagorise a RPC return status and error into an `enum tryret' 907 * return code. 908 */ 909 enum tryret 910 returncode(enum clnt_stat stat, struct rpc_err *rpcerr) 911 { 912 switch (stat) { 913 case RPC_TIMEDOUT: 914 return (TRYRET_TIMEOUT); 915 case RPC_PMAPFAILURE: 916 case RPC_PROGNOTREGISTERED: 917 case RPC_PROGVERSMISMATCH: 918 /* XXX, these can be local or remote. */ 919 case RPC_CANTSEND: 920 case RPC_CANTRECV: 921 return (TRYRET_REMOTEERR); 922 case RPC_SYSTEMERROR: 923 switch (rpcerr->re_errno) { 924 case ETIMEDOUT: 925 return (TRYRET_TIMEOUT); 926 case ENOMEM: 927 break; 928 default: 929 return (TRYRET_REMOTEERR); 930 } 931 /* FALLTHROUGH */ 932 default: 933 break; 934 } 935 return (TRYRET_LOCALERR); 936 } 937 938 /* 939 * xdr routines for mount rpc's 940 */ 941 int 942 xdr_dir(XDR *xdrsp, char *dirp) 943 { 944 return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN)); 945 } 946 947 int 948 xdr_fh(XDR *xdrsp, register struct nfhret *np) 949 { 950 register int i; 951 long auth, authcnt, authfnd = 0; 952 953 if (!xdr_u_long(xdrsp, &np->stat)) 954 return (0); 955 if (np->stat) 956 return (1); 957 switch (np->vers) { 958 case 1: 959 np->fhsize = NFSX_V2FH; 960 return (xdr_opaque(xdrsp, (caddr_t)np->nfh, NFSX_V2FH)); 961 case 3: 962 if (!xdr_long(xdrsp, &np->fhsize)) 963 return (0); 964 if (np->fhsize <= 0 || np->fhsize > NFSX_V3FHMAX) 965 return (0); 966 if (!xdr_opaque(xdrsp, (caddr_t)np->nfh, np->fhsize)) 967 return (0); 968 if (!xdr_long(xdrsp, &authcnt)) 969 return (0); 970 for (i = 0; i < authcnt; i++) { 971 if (!xdr_long(xdrsp, &auth)) 972 return (0); 973 if (auth == np->auth) 974 authfnd++; 975 } 976 /* 977 * Some servers, such as DEC's OSF/1 return a nil authenticator 978 * list to indicate RPCAUTH_UNIX. 979 */ 980 if (!authfnd && (authcnt > 0 || np->auth != RPCAUTH_UNIX)) 981 np->stat = EAUTH; 982 return (1); 983 }; 984 return (0); 985 } 986 987 void 988 usage(void) 989 { 990 fprintf(stderr, "%s\n%s\n%s\n%s\n", 991 "usage: mount_nfs [-23KNPTUbcdilqs] [-D deadthresh] [-I readdirsize]", 992 " [-L leaseterm] [-R retrycnt] [-a maxreadahead]", 993 " [-g maxgroups] [-m realm] [-o options] [-r readsize]", 994 " [-t timeout] [-w writesize] [-x retrans] rhost:path node"); 995 exit(1); 996 } 997