1 /* $OpenBSD: mount_nfs.c,v 1.55 2020/01/22 06:24:08 tedu Exp $ */ 2 /* $NetBSD: mount_nfs.c,v 1.12.4.1 1996/05/25 22:48:05 fvdl Exp $ */ 3 4 /* 5 * Copyright (c) 1992, 1993, 1994 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * Rick Macklem at The University of Guelph. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/types.h> 37 #include <sys/mount.h> 38 #include <sys/socket.h> 39 #include <sys/stat.h> 40 #include <syslog.h> 41 42 #include <rpc/rpc.h> 43 #include <rpc/pmap_clnt.h> 44 #include <rpc/pmap_prot.h> 45 46 #include <nfs/rpcv2.h> 47 #include <nfs/nfsproto.h> 48 #include <nfs/nfs.h> 49 50 #include <arpa/inet.h> 51 52 #include <ctype.h> 53 #include <err.h> 54 #include <errno.h> 55 #include <fcntl.h> 56 #include <netdb.h> 57 #include <signal.h> 58 #include <stdio.h> 59 #include <stdlib.h> 60 #include <string.h> 61 #include <unistd.h> 62 #include <limits.h> 63 64 #include "mntopts.h" 65 66 #define ALTF_BG 0x1 67 #define ALTF_NOCONN 0x2 68 #define ALTF_DUMBTIMR 0x4 69 #define ALTF_INTR 0x8 70 #define ALTF_NFSV3 0x20 71 #define ALTF_RDIRPLUS 0x40 72 #define ALTF_MNTUDP 0x80 73 #define ALTF_RESVPORT 0x100 74 #define ALTF_SEQPACKET 0x200 75 #define ALTF_SOFT 0x800 76 #define ALTF_TCP 0x1000 77 #define ALTF_PORT 0x2000 78 #define ALTF_NFSV2 0x4000 79 #define ALTF_NOAC 0x8000 80 #define ALTF_ACREGMIN 0x10000 81 #define ALTF_ACREGMAX 0x20000 82 #define ALTF_ACDIRMIN 0x40000 83 #define ALTF_ACDIRMAX 0x80000 84 85 const struct mntopt mopts[] = { 86 MOPT_STDOPTS, 87 MOPT_WXALLOWED, 88 MOPT_FORCE, 89 MOPT_UPDATE, 90 MOPT_SYNC, 91 { "bg", ALTF_BG, 0 }, 92 { "conn", ALTF_NOCONN, MFLAG_INVERSE }, 93 { "dumbtimer", ALTF_DUMBTIMR, 0 }, 94 { "intr", ALTF_INTR, 0 }, 95 { "nfsv3", ALTF_NFSV3, 0 }, 96 { "rdirplus", ALTF_RDIRPLUS, 0 }, 97 { "mntudp", ALTF_MNTUDP, 0 }, 98 { "resvport", ALTF_RESVPORT, 0 }, 99 { "soft", ALTF_SOFT, 0 }, 100 { "tcp", ALTF_TCP, 0 }, 101 { "port", ALTF_PORT, MFLAG_INTVAL }, 102 { "nfsv2", ALTF_NFSV2, 0 }, 103 { "ac", ALTF_NOAC, MFLAG_INVERSE }, 104 { "acregmin", ALTF_ACREGMIN, MFLAG_INTVAL }, 105 { "acregmax", ALTF_ACREGMAX, MFLAG_INTVAL }, 106 { "acdirmin", ALTF_ACDIRMIN, MFLAG_INTVAL }, 107 { "acdirmax", ALTF_ACDIRMAX, MFLAG_INTVAL }, 108 { NULL } 109 }; 110 111 struct nfs_args nfsdefargs = { 112 NFS_ARGSVERSION, 113 NULL, 114 sizeof (struct sockaddr_in), 115 SOCK_DGRAM, 116 0, 117 NULL, 118 0, 119 NFSMNT_NFSV3, 120 NFS_WSIZE, 121 NFS_RSIZE, 122 NFS_READDIRSIZE, 123 10, 124 NFS_RETRANS, 125 NFS_MAXGRPS, 126 NFS_DEFRAHEAD, 127 0, 128 0, 129 NULL, 130 0, 131 0, 132 0, 133 0 134 }; 135 136 struct nfhret { 137 u_long stat; 138 long vers; 139 long auth; 140 long fhsize; 141 u_char nfh[NFSX_V3FHMAX]; 142 }; 143 #define DEF_RETRY 10000 144 #define BGRND 1 145 #define ISBGRND 2 146 int retrycnt; 147 int opflags = 0; 148 int nfsproto = IPPROTO_UDP; 149 int mnttcp_ok = 1; 150 u_short port_no = 0; 151 int force2 = 0; 152 int force3 = 0; 153 154 int getnfsargs(char *, struct nfs_args *); 155 void set_rpc_maxgrouplist(int); 156 __dead void usage(void); 157 int xdr_dir(XDR *, char *); 158 int xdr_fh(XDR *, struct nfhret *); 159 160 int 161 main(int argc, char *argv[]) 162 { 163 int c; 164 struct nfs_args *nfsargsp; 165 struct nfs_args nfsargs; 166 int mntflags, num; 167 char name[PATH_MAX], *options = NULL, *spec; 168 const char *p; 169 union mntval value; 170 171 retrycnt = DEF_RETRY; 172 173 mntflags = 0; 174 nfsargs = nfsdefargs; 175 nfsargsp = &nfsargs; 176 while ((c = getopt(argc, argv, 177 "23a:bcdD:g:I:iL:lo:PR:r:sTt:w:x:U")) != -1) 178 switch (c) { 179 case '3': 180 if (force2) 181 errx(1, "-2 and -3 are mutually exclusive"); 182 force3 = 1; 183 break; 184 case '2': 185 if (force3) 186 errx(1, "-2 and -3 are mutually exclusive"); 187 force2 = 1; 188 nfsargsp->flags &= ~NFSMNT_NFSV3; 189 break; 190 case 'a': 191 num = (int) strtonum(optarg, 0, 4, &p); 192 if (p) 193 errx(1, "illegal -a value %s: %s", optarg, p); 194 nfsargsp->readahead = num; 195 nfsargsp->flags |= NFSMNT_READAHEAD; 196 break; 197 case 'b': 198 opflags |= BGRND; 199 break; 200 case 'c': 201 nfsargsp->flags |= NFSMNT_NOCONN; 202 break; 203 case 'D': 204 /* backward compatibility */ 205 break; 206 case 'd': 207 nfsargsp->flags |= NFSMNT_DUMBTIMR; 208 break; 209 case 'g': 210 num = (int) strtonum(optarg, 1, NGROUPS_MAX, &p); 211 if (p) 212 errx(1, "illegal -g value %s: %s", optarg, p); 213 set_rpc_maxgrouplist(num); 214 nfsargsp->maxgrouplist = num; 215 nfsargsp->flags |= NFSMNT_MAXGRPS; 216 break; 217 case 'I': 218 num = (int) strtonum(optarg, 1, INT_MAX, &p); 219 if (p) 220 errx(1, "illegal -I value %s: %s", optarg, p); 221 nfsargsp->readdirsize = num; 222 nfsargsp->flags |= NFSMNT_READDIRSIZE; 223 break; 224 case 'i': 225 nfsargsp->flags |= NFSMNT_INT; 226 break; 227 case 'L': 228 /* backward compatibility */ 229 break; 230 case 'l': 231 nfsargsp->flags |= NFSMNT_RDIRPLUS; 232 break; 233 case 'o': 234 options = optarg; 235 while (options != NULL) { 236 switch (getmntopt(&options, &value, mopts, 237 &mntflags)) { 238 case ALTF_BG: 239 opflags |= BGRND; 240 break; 241 case ALTF_NOCONN: 242 nfsargsp->flags |= NFSMNT_NOCONN; 243 break; 244 case ALTF_DUMBTIMR: 245 nfsargsp->flags |= NFSMNT_DUMBTIMR; 246 break; 247 case ALTF_INTR: 248 nfsargsp->flags |= NFSMNT_INT; 249 break; 250 case ALTF_NFSV3: 251 if (force2) 252 errx(1, 253 "conflicting version options"); 254 force3 = 1; 255 break; 256 case ALTF_NFSV2: 257 if (force3) 258 errx(1, 259 "conflicting version options"); 260 force2 = 1; 261 nfsargsp->flags &= ~NFSMNT_NFSV3; 262 break; 263 case ALTF_RDIRPLUS: 264 nfsargsp->flags |= NFSMNT_RDIRPLUS; 265 break; 266 case ALTF_MNTUDP: 267 mnttcp_ok = 0; 268 break; 269 case ALTF_RESVPORT: 270 nfsargsp->flags |= NFSMNT_RESVPORT; 271 break; 272 case ALTF_SOFT: 273 nfsargsp->flags |= NFSMNT_SOFT; 274 break; 275 case ALTF_TCP: 276 nfsargsp->sotype = SOCK_STREAM; 277 nfsproto = IPPROTO_TCP; 278 break; 279 case ALTF_PORT: 280 port_no = value.ival; 281 break; 282 case ALTF_NOAC: 283 nfsargsp->flags |= (NFSMNT_ACREGMIN | 284 NFSMNT_ACREGMAX | NFSMNT_ACDIRMIN | 285 NFSMNT_ACDIRMAX); 286 nfsargsp->acregmin = 0; 287 nfsargsp->acregmax = 0; 288 nfsargsp->acdirmin = 0; 289 nfsargsp->acdirmax = 0; 290 break; 291 case ALTF_ACREGMIN: 292 nfsargsp->flags |= NFSMNT_ACREGMIN; 293 nfsargsp->acregmin = value.ival; 294 break; 295 case ALTF_ACREGMAX: 296 nfsargsp->flags |= NFSMNT_ACREGMAX; 297 nfsargsp->acregmax = value.ival; 298 break; 299 case ALTF_ACDIRMIN: 300 nfsargsp->flags |= NFSMNT_ACDIRMIN; 301 nfsargsp->acdirmin = value.ival; 302 break; 303 case ALTF_ACDIRMAX: 304 nfsargsp->flags |= NFSMNT_ACDIRMAX; 305 nfsargsp->acdirmax = value.ival; 306 break; 307 } 308 } 309 break; 310 case 'P': 311 /* backward compatibility */ 312 break; 313 case 'R': 314 num = (int) strtonum(optarg, 1, INT_MAX, &p); 315 if (p) 316 errx(1, "illegal -R value %s: %s", optarg, p); 317 retrycnt = num; 318 break; 319 case 'r': 320 num = (int) strtonum(optarg, 1, INT_MAX, &p); 321 if (p) 322 errx(1, "illegal -r value %s: %s", optarg, p); 323 nfsargsp->rsize = num; 324 nfsargsp->flags |= NFSMNT_RSIZE; 325 break; 326 case 's': 327 nfsargsp->flags |= NFSMNT_SOFT; 328 break; 329 case 'T': 330 nfsargsp->sotype = SOCK_STREAM; 331 nfsproto = IPPROTO_TCP; 332 break; 333 case 't': 334 num = (int) strtonum(optarg, 1, INT_MAX, &p); 335 if (p) 336 errx(1, "illegal -t value %s: %s", optarg, p); 337 nfsargsp->timeo = num; 338 nfsargsp->flags |= NFSMNT_TIMEO; 339 break; 340 case 'w': 341 num = (int) strtonum(optarg, 1, INT_MAX, &p); 342 if (p) 343 errx(1, "illegal -w value %s: %s", optarg, p); 344 nfsargsp->wsize = num; 345 nfsargsp->flags |= NFSMNT_WSIZE; 346 break; 347 case 'x': 348 num = (int) strtonum(optarg, 1, INT_MAX, &p); 349 if (p) 350 errx(1, "illegal -x value %s: %s", optarg, p); 351 nfsargsp->retrans = num; 352 nfsargsp->flags |= NFSMNT_RETRANS; 353 break; 354 case 'U': 355 mnttcp_ok = 0; 356 break; 357 default: 358 usage(); 359 /* NOTREACHED */ 360 } 361 argc -= optind; 362 argv += optind; 363 364 if (argc != 2) 365 usage(); 366 367 spec = *argv++; 368 if (realpath(*argv, name) == NULL) 369 err(1, "realpath %s", *argv); 370 371 if (!getnfsargs(spec, nfsargsp)) 372 exit(1); 373 if (mount(MOUNT_NFS, name, mntflags, nfsargsp)) { 374 if (errno == EOPNOTSUPP) 375 errx(1, "%s: Filesystem not supported by kernel", name); 376 else 377 err(1, "%s", name); 378 } 379 exit(0); 380 } 381 382 int 383 getnfsargs(char *spec, struct nfs_args *nfsargsp) 384 { 385 CLIENT *clp; 386 struct hostent *hp; 387 static struct sockaddr_in saddr; 388 struct timeval pertry, try; 389 enum clnt_stat clnt_stat; 390 int so = RPC_ANYSOCK, i, nfsvers, mntvers, orgcnt; 391 char *hostp, *delimp; 392 u_short tport; 393 static struct nfhret nfhret; 394 static char nam[MNAMELEN + 1]; 395 396 if (strlcpy(nam, spec, sizeof(nam)) >= sizeof(nam)) { 397 errx(1, "hostname too long"); 398 } 399 400 if ((delimp = strchr(spec, '@')) != NULL) { 401 hostp = delimp + 1; 402 } else if ((delimp = strchr(spec, ':')) != NULL) { 403 hostp = spec; 404 spec = delimp + 1; 405 } else { 406 warnx("no <host>:<dirpath> or <dirpath>@<host> spec"); 407 return (0); 408 } 409 *delimp = '\0'; 410 411 /* 412 * Handle an internet host address 413 */ 414 if (inet_aton(hostp, &saddr.sin_addr) == 0) { 415 hp = gethostbyname(hostp); 416 if (hp == NULL) { 417 warnx("can't resolve address for host %s", hostp); 418 return (0); 419 } 420 memcpy(&saddr.sin_addr, hp->h_addr, hp->h_length); 421 } 422 423 if (force2) { 424 nfsvers = NFS_VER2; 425 mntvers = RPCMNT_VER1; 426 } else { 427 nfsvers = NFS_VER3; 428 mntvers = RPCMNT_VER3; 429 } 430 orgcnt = retrycnt; 431 tryagain: 432 nfhret.stat = EACCES; /* Mark not yet successful */ 433 while (retrycnt > 0) { 434 saddr.sin_family = AF_INET; 435 saddr.sin_port = htons(PMAPPORT); 436 if ((tport = port_no ? port_no : pmap_getport(&saddr, 437 RPCPROG_NFS, nfsvers, nfsargsp->sotype == SOCK_STREAM ? 438 IPPROTO_TCP : IPPROTO_UDP)) == 0) { 439 if ((opflags & ISBGRND) == 0) 440 clnt_pcreateerror("NFS Portmap"); 441 } else { 442 saddr.sin_port = 0; 443 pertry.tv_sec = 10; 444 pertry.tv_usec = 0; 445 if (mnttcp_ok && nfsargsp->sotype == SOCK_STREAM) 446 clp = clnttcp_create(&saddr, RPCPROG_MNT, mntvers, 447 &so, 0, 0); 448 else 449 clp = clntudp_create(&saddr, RPCPROG_MNT, mntvers, 450 pertry, &so); 451 if (clp == NULL) { 452 if ((opflags & ISBGRND) == 0) 453 clnt_pcreateerror("Cannot MNT RPC"); 454 } else { 455 clp->cl_auth = authunix_create_default(); 456 try.tv_sec = 10; 457 try.tv_usec = 0; 458 nfhret.auth = RPCAUTH_UNIX; 459 nfhret.vers = mntvers; 460 clnt_stat = clnt_call(clp, RPCMNT_MOUNT, 461 xdr_dir, spec, xdr_fh, &nfhret, try); 462 if (clnt_stat != RPC_SUCCESS) { 463 if (clnt_stat == RPC_PROGVERSMISMATCH) { 464 if (nfsvers == NFS_VER3 && 465 !force3) { 466 retrycnt = orgcnt; 467 nfsvers = NFS_VER2; 468 mntvers = RPCMNT_VER1; 469 nfsargsp->flags &= 470 ~NFSMNT_NFSV3; 471 goto tryagain; 472 } else { 473 warnx("%s", 474 clnt_sperror(clp, 475 "MNT RPC")); 476 } 477 } 478 if ((opflags & ISBGRND) == 0) 479 warnx("%s", clnt_sperror(clp, 480 "bad MNT RPC")); 481 } else { 482 auth_destroy(clp->cl_auth); 483 clnt_destroy(clp); 484 retrycnt = 0; 485 } 486 } 487 } 488 if (--retrycnt > 0) { 489 if (opflags & BGRND) { 490 opflags &= ~BGRND; 491 if ((i = fork())) { 492 if (i == -1) 493 err(1, "fork"); 494 exit(0); 495 } 496 (void) setsid(); 497 (void) close(STDIN_FILENO); 498 (void) close(STDOUT_FILENO); 499 (void) close(STDERR_FILENO); 500 (void) chdir("/"); 501 opflags |= ISBGRND; 502 } 503 sleep(60); 504 } 505 } 506 if (nfhret.stat) { 507 if (opflags & ISBGRND) 508 exit(1); 509 warnc(nfhret.stat, "can't access %s", spec); 510 return (0); 511 } 512 saddr.sin_port = htons(tport); 513 nfsargsp->addr = (struct sockaddr *) &saddr; 514 nfsargsp->addrlen = sizeof (saddr); 515 nfsargsp->fh = nfhret.nfh; 516 nfsargsp->fhsize = nfhret.fhsize; 517 nfsargsp->hostname = nam; 518 return (1); 519 } 520 521 /* 522 * xdr routines for mount rpc's 523 */ 524 int 525 xdr_dir(XDR *xdrsp, char *dirp) 526 { 527 return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN)); 528 } 529 530 int 531 xdr_fh(XDR *xdrsp, struct nfhret *np) 532 { 533 int i; 534 long auth, authcnt, authfnd = 0; 535 536 if (!xdr_u_long(xdrsp, &np->stat)) 537 return (0); 538 if (np->stat) 539 return (1); 540 switch (np->vers) { 541 case 1: 542 np->fhsize = NFSX_V2FH; 543 return (xdr_opaque(xdrsp, (caddr_t)np->nfh, NFSX_V2FH)); 544 case 3: 545 if (!xdr_long(xdrsp, &np->fhsize)) 546 return (0); 547 if (np->fhsize <= 0 || np->fhsize > NFSX_V3FHMAX) 548 return (0); 549 if (!xdr_opaque(xdrsp, (caddr_t)np->nfh, np->fhsize)) 550 return (0); 551 if (!xdr_long(xdrsp, &authcnt)) 552 return (0); 553 for (i = 0; i < authcnt; i++) { 554 if (!xdr_long(xdrsp, &auth)) 555 return (0); 556 if (auth == np->auth) 557 authfnd++; 558 } 559 /* 560 * Some servers, such as DEC's OSF/1 return a nil authenticator 561 * list to indicate RPCAUTH_UNIX. 562 */ 563 if (!authfnd && (authcnt > 0 || np->auth != RPCAUTH_UNIX)) 564 np->stat = EAUTH; 565 return (1); 566 } 567 return (0); 568 } 569 570 __dead void 571 usage(void) 572 { 573 extern char *__progname; 574 575 (void)fprintf(stderr, 576 "usage: %s [-23bcdilsTU] [-a maxreadahead] [-g maxgroups]\n" 577 "\t[-I readdirsize] [-o options] [-R retrycnt] [-r readsize]\n" 578 "\t[-t timeout] [-w writesize] [-x retrans] rhost:path node\n", 579 __progname); 580 exit(1); 581 } 582