1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1980, 1989, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #ifndef lint 33 static const char copyright[] = 34 "@(#) Copyright (c) 1980, 1989, 1993\n\ 35 The Regents of the University of California. All rights reserved.\n"; 36 #endif /* not lint */ 37 38 #ifndef lint 39 #if 0 40 static char sccsid[] = "@(#)umount.c 8.8 (Berkeley) 5/8/95"; 41 #endif 42 #endif /* not lint */ 43 44 #include <sys/param.h> 45 #include <sys/mount.h> 46 #include <sys/socket.h> 47 #include <sys/stat.h> 48 49 #include <netdb.h> 50 #include <rpc/rpc.h> 51 #include <rpcsvc/mount.h> 52 #include <nfs/nfssvc.h> 53 54 #include <ctype.h> 55 #include <err.h> 56 #include <errno.h> 57 #include <fstab.h> 58 #include <stdio.h> 59 #include <stdlib.h> 60 #include <string.h> 61 #include <unistd.h> 62 63 #include "mounttab.h" 64 65 typedef enum { FIND, REMOVE, CHECKUNIQUE } dowhat; 66 67 static struct addrinfo *nfshost_ai = NULL; 68 static int fflag, vflag; 69 static char *nfshost; 70 71 struct statfs *checkmntlist(char *); 72 int checkvfsname (const char *, char **); 73 struct statfs *getmntentry(const char *fromname, const char *onname, 74 fsid_t *fsid, dowhat what); 75 char **makevfslist (const char *); 76 size_t mntinfo (struct statfs **); 77 int namematch (struct addrinfo *); 78 int parsehexfsid(const char *hex, fsid_t *fsid); 79 int sacmp (void *, void *); 80 int umountall (char **); 81 int checkname (char *, char **); 82 int umountfs(struct statfs *sfs); 83 void usage (void); 84 int xdr_dir (XDR *, char *); 85 86 int 87 main(int argc, char *argv[]) 88 { 89 int all, errs, ch, mntsize, error, nfsforce, ret; 90 char **typelist = NULL; 91 struct statfs *mntbuf, *sfs; 92 struct addrinfo hints; 93 94 nfsforce = all = errs = 0; 95 while ((ch = getopt(argc, argv, "AaF:fh:Nnt:v")) != -1) 96 switch (ch) { 97 case 'A': 98 all = 2; 99 break; 100 case 'a': 101 all = 1; 102 break; 103 case 'F': 104 setfstab(optarg); 105 break; 106 case 'f': 107 fflag |= MNT_FORCE; 108 break; 109 case 'h': /* -h implies -A. */ 110 all = 2; 111 nfshost = optarg; 112 break; 113 case 'N': 114 nfsforce = 1; 115 break; 116 case 'n': 117 fflag |= MNT_NONBUSY; 118 break; 119 case 't': 120 if (typelist != NULL) 121 err(1, "only one -t option may be specified"); 122 typelist = makevfslist(optarg); 123 break; 124 case 'v': 125 vflag = 1; 126 break; 127 default: 128 usage(); 129 /* NOTREACHED */ 130 } 131 argc -= optind; 132 argv += optind; 133 134 if ((fflag & MNT_FORCE) != 0 && (fflag & MNT_NONBUSY) != 0) 135 err(1, "-f and -n are mutually exclusive"); 136 137 if ((argc == 0 && !all) || (argc != 0 && all)) 138 usage(); 139 140 if (nfsforce != 0 && (argc == 0 || nfshost != NULL || typelist != NULL)) 141 usage(); 142 143 /* -h implies "-t nfs" if no -t flag. */ 144 if ((nfshost != NULL) && (typelist == NULL)) 145 typelist = makevfslist("nfs"); 146 147 if (nfshost != NULL) { 148 memset(&hints, 0, sizeof hints); 149 error = getaddrinfo(nfshost, NULL, &hints, &nfshost_ai); 150 if (error) 151 errx(1, "%s: %s", nfshost, gai_strerror(error)); 152 } 153 154 switch (all) { 155 case 2: 156 if ((mntsize = mntinfo(&mntbuf)) <= 0) 157 break; 158 /* 159 * We unmount the nfs-mounts in the reverse order 160 * that they were mounted. 161 */ 162 for (errs = 0, mntsize--; mntsize > 0; mntsize--) { 163 sfs = &mntbuf[mntsize]; 164 if (checkvfsname(sfs->f_fstypename, typelist)) 165 continue; 166 if (strcmp(sfs->f_mntonname, "/dev") == 0) 167 continue; 168 if (umountfs(sfs) != 0) 169 errs = 1; 170 } 171 free(mntbuf); 172 break; 173 case 1: 174 if (setfsent() == 0) 175 err(1, "%s", getfstab()); 176 errs = umountall(typelist); 177 break; 178 case 0: 179 for (errs = 0; *argv != NULL; ++argv) 180 if (nfsforce != 0) { 181 /* 182 * First do the nfssvc() syscall to shut down 183 * the mount point and then do the forced 184 * dismount. 185 */ 186 ret = nfssvc(NFSSVC_FORCEDISM, *argv); 187 if (ret >= 0) 188 ret = unmount(*argv, MNT_FORCE); 189 if (ret < 0) { 190 warn("%s", *argv); 191 errs = 1; 192 } 193 } else if (checkname(*argv, typelist) != 0) 194 errs = 1; 195 break; 196 } 197 exit(errs); 198 } 199 200 int 201 umountall(char **typelist) 202 { 203 struct xvfsconf vfc; 204 struct fstab *fs; 205 int rval; 206 char *cp; 207 static int firstcall = 1; 208 209 if ((fs = getfsent()) != NULL) 210 firstcall = 0; 211 else if (firstcall) 212 errx(1, "fstab reading failure"); 213 else 214 return (0); 215 do { 216 /* Ignore the root. */ 217 if (strcmp(fs->fs_file, "/") == 0) 218 continue; 219 /* 220 * !!! 221 * Historic practice: ignore unknown FSTAB_* fields. 222 */ 223 if (strcmp(fs->fs_type, FSTAB_RW) && 224 strcmp(fs->fs_type, FSTAB_RO) && 225 strcmp(fs->fs_type, FSTAB_RQ)) 226 continue; 227 /* Ignore unknown file system types. */ 228 if (getvfsbyname(fs->fs_vfstype, &vfc) == -1) 229 continue; 230 if (checkvfsname(fs->fs_vfstype, typelist)) 231 continue; 232 233 /* 234 * We want to unmount the file systems in the reverse order 235 * that they were mounted. So, we save off the file name 236 * in some allocated memory, and then call recursively. 237 */ 238 if ((cp = malloc((size_t)strlen(fs->fs_file) + 1)) == NULL) 239 err(1, "malloc failed"); 240 (void)strcpy(cp, fs->fs_file); 241 rval = umountall(typelist); 242 rval = checkname(cp, typelist) || rval; 243 free(cp); 244 return (rval); 245 } while ((fs = getfsent()) != NULL); 246 return (0); 247 } 248 249 /* 250 * Do magic checks on mountpoint/device/fsid, and then call unmount(2). 251 */ 252 int 253 checkname(char *mntname, char **typelist) 254 { 255 char buf[MAXPATHLEN]; 256 struct statfs sfsbuf; 257 struct stat sb; 258 struct statfs *sfs; 259 char *delimp; 260 dev_t dev; 261 int len; 262 263 /* 264 * 1. Check if the name exists in the mounttable. 265 */ 266 sfs = checkmntlist(mntname); 267 /* 268 * 2. Remove trailing slashes if there are any. After that 269 * we look up the name in the mounttable again. 270 */ 271 if (sfs == NULL) { 272 len = strlen(mntname); 273 while (len > 1 && mntname[len - 1] == '/') 274 mntname[--len] = '\0'; 275 sfs = checkmntlist(mntname); 276 } 277 /* 278 * 3. Check if the deprecated NFS syntax with an '@' has been used 279 * and translate it to the ':' syntax. Look up the name in the 280 * mount table again. 281 */ 282 if (sfs == NULL && (delimp = strrchr(mntname, '@')) != NULL) { 283 snprintf(buf, sizeof(buf), "%s:%.*s", delimp + 1, 284 (int)(delimp - mntname), mntname); 285 len = strlen(buf); 286 while (len > 1 && buf[len - 1] == '/') 287 buf[--len] = '\0'; 288 sfs = checkmntlist(buf); 289 } 290 /* 291 * 4. Resort to a statfs(2) call. This is the last check so that 292 * hung NFS filesystems for example can be unmounted without 293 * potentially blocking forever in statfs() as long as the 294 * filesystem is specified unambiguously. This covers all the 295 * hard cases such as symlinks and mismatches between the 296 * mount list and reality. 297 * We also do this if an ambiguous mount point was specified. 298 */ 299 if (sfs == NULL || (getmntentry(NULL, mntname, NULL, FIND) != NULL && 300 getmntentry(NULL, mntname, NULL, CHECKUNIQUE) == NULL)) { 301 if (statfs(mntname, &sfsbuf) != 0) { 302 warn("%s: statfs", mntname); 303 } else if (stat(mntname, &sb) != 0) { 304 warn("%s: stat", mntname); 305 } else if (S_ISDIR(sb.st_mode)) { 306 /* Check that `mntname' is the root directory. */ 307 dev = sb.st_dev; 308 snprintf(buf, sizeof(buf), "%s/..", mntname); 309 if (stat(buf, &sb) != 0) { 310 warn("%s: stat", buf); 311 } else if (sb.st_dev == dev) { 312 warnx("%s: not a file system root directory", 313 mntname); 314 return (1); 315 } else 316 sfs = &sfsbuf; 317 } 318 } 319 if (sfs == NULL) { 320 warnx("%s: unknown file system", mntname); 321 return (1); 322 } 323 if (checkvfsname(sfs->f_fstypename, typelist)) 324 return (1); 325 return (umountfs(sfs)); 326 } 327 328 /* 329 * NFS stuff and unmount(2) call 330 */ 331 int 332 umountfs(struct statfs *sfs) 333 { 334 char fsidbuf[64]; 335 enum clnt_stat clnt_stat; 336 struct timeval try; 337 struct addrinfo *ai, hints; 338 int do_rpc; 339 CLIENT *clp; 340 char *nfsdirname, *orignfsdirname; 341 char *hostp, *delimp; 342 char buf[1024]; 343 struct nfscl_dumpmntopts dumpmntopts; 344 const char *proto_ptr = NULL; 345 346 ai = NULL; 347 do_rpc = 0; 348 hostp = NULL; 349 nfsdirname = delimp = orignfsdirname = NULL; 350 memset(&hints, 0, sizeof hints); 351 352 if (strcmp(sfs->f_fstypename, "nfs") == 0) { 353 if ((nfsdirname = strdup(sfs->f_mntfromname)) == NULL) 354 err(1, "strdup"); 355 orignfsdirname = nfsdirname; 356 if (*nfsdirname == '[' && 357 (delimp = strchr(nfsdirname + 1, ']')) != NULL && 358 *(delimp + 1) == ':') { 359 hostp = nfsdirname + 1; 360 nfsdirname = delimp + 2; 361 } else if ((delimp = strrchr(nfsdirname, ':')) != NULL) { 362 hostp = nfsdirname; 363 nfsdirname = delimp + 1; 364 } 365 if (hostp != NULL) { 366 *delimp = '\0'; 367 getaddrinfo(hostp, NULL, &hints, &ai); 368 if (ai == NULL) { 369 warnx("can't get net id for host"); 370 } 371 } 372 373 /* 374 * Check if we have to start the rpc-call later. 375 * If there are still identical nfs-names mounted, 376 * we skip the rpc-call. Obviously this has to 377 * happen before unmount(2), but it should happen 378 * after the previous namecheck. 379 * A non-NULL return means that this is the last 380 * mount from mntfromname that is still mounted. 381 */ 382 if (getmntentry(sfs->f_mntfromname, NULL, NULL, 383 CHECKUNIQUE) != NULL) { 384 do_rpc = 1; 385 proto_ptr = "udp"; 386 /* 387 * Try and find out whether this NFS mount is NFSv4 and 388 * what protocol is being used. If this fails, the 389 * default is NFSv2,3 and use UDP for the Unmount RPC. 390 */ 391 dumpmntopts.ndmnt_fname = sfs->f_mntonname; 392 dumpmntopts.ndmnt_buf = buf; 393 dumpmntopts.ndmnt_blen = sizeof(buf); 394 if (nfssvc(NFSSVC_DUMPMNTOPTS, &dumpmntopts) >= 0) { 395 if (strstr(buf, "nfsv4,") != NULL) 396 do_rpc = 0; 397 else if (strstr(buf, ",tcp,") != NULL) 398 proto_ptr = "tcp"; 399 } 400 } 401 } 402 403 if (!namematch(ai)) { 404 free(orignfsdirname); 405 return (1); 406 } 407 /* First try to unmount using the file system ID. */ 408 snprintf(fsidbuf, sizeof(fsidbuf), "FSID:%d:%d", sfs->f_fsid.val[0], 409 sfs->f_fsid.val[1]); 410 if (unmount(fsidbuf, fflag | MNT_BYFSID) != 0) { 411 /* XXX, non-root users get a zero fsid, so don't warn. */ 412 if (errno != ENOENT || sfs->f_fsid.val[0] != 0 || 413 sfs->f_fsid.val[1] != 0) 414 warn("unmount of %s failed", sfs->f_mntonname); 415 if (errno != ENOENT) { 416 free(orignfsdirname); 417 return (1); 418 } 419 /* Compatibility for old kernels. */ 420 if (sfs->f_fsid.val[0] != 0 || sfs->f_fsid.val[1] != 0) 421 warnx("retrying using path instead of file system ID"); 422 if (unmount(sfs->f_mntonname, fflag) != 0) { 423 warn("unmount of %s failed", sfs->f_mntonname); 424 free(orignfsdirname); 425 return (1); 426 } 427 } 428 /* Mark this file system as unmounted. */ 429 getmntentry(NULL, NULL, &sfs->f_fsid, REMOVE); 430 if (vflag) 431 (void)printf("%s: unmount from %s\n", sfs->f_mntfromname, 432 sfs->f_mntonname); 433 /* 434 * Report to mountd-server which nfsname 435 * has been unmounted. 436 */ 437 if (ai != NULL && !(fflag & MNT_FORCE) && do_rpc) { 438 clp = clnt_create(hostp, MOUNTPROG, MOUNTVERS3, proto_ptr); 439 if (clp == NULL) { 440 warnx("%s: %s", hostp, 441 clnt_spcreateerror("MOUNTPROG")); 442 free(orignfsdirname); 443 return (1); 444 } 445 clp->cl_auth = authsys_create_default(); 446 try.tv_sec = 20; 447 try.tv_usec = 0; 448 clnt_stat = clnt_call(clp, MOUNTPROC_UMNT, (xdrproc_t)xdr_dir, 449 nfsdirname, (xdrproc_t)xdr_void, (caddr_t)0, try); 450 if (clnt_stat != RPC_SUCCESS) { 451 warnx("%s: %s", hostp, 452 clnt_sperror(clp, "RPCMNT_UMOUNT")); 453 free(orignfsdirname); 454 return (1); 455 } 456 /* 457 * Remove the unmounted entry from /var/db/mounttab. 458 */ 459 if (read_mtab()) { 460 clean_mtab(hostp, nfsdirname, vflag); 461 if(!write_mtab(vflag)) 462 warnx("cannot remove mounttab entry %s:%s", 463 hostp, nfsdirname); 464 free_mtab(); 465 } 466 auth_destroy(clp->cl_auth); 467 clnt_destroy(clp); 468 } 469 free(orignfsdirname); 470 return (0); 471 } 472 473 struct statfs * 474 getmntentry(const char *fromname, const char *onname, fsid_t *fsid, dowhat what) 475 { 476 static struct statfs *mntbuf; 477 static size_t mntsize = 0; 478 static int *mntcheck = NULL; 479 struct statfs *sfs, *foundsfs; 480 int i, count; 481 482 if (mntsize <= 0) { 483 if ((mntsize = mntinfo(&mntbuf)) <= 0) 484 return (NULL); 485 } 486 if (mntcheck == NULL) { 487 if ((mntcheck = calloc(mntsize + 1, sizeof(int))) == NULL) 488 err(1, "calloc"); 489 } 490 /* 491 * We want to get the file systems in the reverse order 492 * that they were mounted. Unmounted file systems are marked 493 * in a table called 'mntcheck'. 494 */ 495 count = 0; 496 foundsfs = NULL; 497 for (i = mntsize - 1; i >= 0; i--) { 498 if (mntcheck[i]) 499 continue; 500 sfs = &mntbuf[i]; 501 if (fromname != NULL && strcmp(sfs->f_mntfromname, 502 fromname) != 0) 503 continue; 504 if (onname != NULL && strcmp(sfs->f_mntonname, onname) != 0) 505 continue; 506 if (fsid != NULL && fsidcmp(&sfs->f_fsid, fsid) != 0) 507 continue; 508 509 switch (what) { 510 case CHECKUNIQUE: 511 foundsfs = sfs; 512 count++; 513 continue; 514 case REMOVE: 515 mntcheck[i] = 1; 516 break; 517 default: 518 break; 519 } 520 return (sfs); 521 } 522 523 if (what == CHECKUNIQUE && count == 1) 524 return (foundsfs); 525 return (NULL); 526 } 527 528 int 529 sacmp(void *sa1, void *sa2) 530 { 531 void *p1, *p2; 532 int len; 533 534 if (((struct sockaddr *)sa1)->sa_family != 535 ((struct sockaddr *)sa2)->sa_family) 536 return (1); 537 538 switch (((struct sockaddr *)sa1)->sa_family) { 539 case AF_INET: 540 p1 = &((struct sockaddr_in *)sa1)->sin_addr; 541 p2 = &((struct sockaddr_in *)sa2)->sin_addr; 542 len = 4; 543 break; 544 case AF_INET6: 545 p1 = &((struct sockaddr_in6 *)sa1)->sin6_addr; 546 p2 = &((struct sockaddr_in6 *)sa2)->sin6_addr; 547 len = 16; 548 if (((struct sockaddr_in6 *)sa1)->sin6_scope_id != 549 ((struct sockaddr_in6 *)sa2)->sin6_scope_id) 550 return (1); 551 break; 552 default: 553 return (1); 554 } 555 556 return memcmp(p1, p2, len); 557 } 558 559 int 560 namematch(struct addrinfo *ai) 561 { 562 struct addrinfo *aip; 563 564 if (nfshost == NULL || nfshost_ai == NULL) 565 return (1); 566 567 while (ai != NULL) { 568 aip = nfshost_ai; 569 while (aip != NULL) { 570 if (sacmp(ai->ai_addr, aip->ai_addr) == 0) 571 return (1); 572 aip = aip->ai_next; 573 } 574 ai = ai->ai_next; 575 } 576 577 return (0); 578 } 579 580 struct statfs * 581 checkmntlist(char *mntname) 582 { 583 struct statfs *sfs; 584 fsid_t fsid; 585 586 sfs = NULL; 587 if (parsehexfsid(mntname, &fsid) == 0) 588 sfs = getmntentry(NULL, NULL, &fsid, FIND); 589 if (sfs == NULL) 590 sfs = getmntentry(NULL, mntname, NULL, FIND); 591 if (sfs == NULL) 592 sfs = getmntentry(mntname, NULL, NULL, FIND); 593 return (sfs); 594 } 595 596 size_t 597 mntinfo(struct statfs **mntbuf) 598 { 599 static struct statfs *origbuf; 600 size_t bufsize; 601 int mntsize; 602 603 mntsize = getfsstat(NULL, 0, MNT_NOWAIT); 604 if (mntsize <= 0) 605 return (0); 606 bufsize = (mntsize + 1) * sizeof(struct statfs); 607 if ((origbuf = malloc(bufsize)) == NULL) 608 err(1, "malloc"); 609 mntsize = getfsstat(origbuf, (long)bufsize, MNT_NOWAIT); 610 *mntbuf = origbuf; 611 return (mntsize); 612 } 613 614 /* 615 * Convert a hexadecimal filesystem ID to an fsid_t. 616 * Returns 0 on success. 617 */ 618 int 619 parsehexfsid(const char *hex, fsid_t *fsid) 620 { 621 char hexbuf[3]; 622 int i; 623 624 if (strlen(hex) != sizeof(*fsid) * 2) 625 return (-1); 626 hexbuf[2] = '\0'; 627 for (i = 0; i < (int)sizeof(*fsid); i++) { 628 hexbuf[0] = hex[i * 2]; 629 hexbuf[1] = hex[i * 2 + 1]; 630 if (!isxdigit(hexbuf[0]) || !isxdigit(hexbuf[1])) 631 return (-1); 632 ((u_char *)fsid)[i] = strtol(hexbuf, NULL, 16); 633 } 634 return (0); 635 } 636 637 /* 638 * xdr routines for mount rpc's 639 */ 640 int 641 xdr_dir(XDR *xdrsp, char *dirp) 642 { 643 644 return (xdr_string(xdrsp, &dirp, MNTPATHLEN)); 645 } 646 647 void 648 usage(void) 649 { 650 651 (void)fprintf(stderr, "%s\n%s\n", 652 "usage: umount [-fNnv] special ... | node ... | fsid ...", 653 " umount -a | -A [-F fstab] [-fnv] [-h host] [-t type]"); 654 exit(1); 655 } 656