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