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