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