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