1/* $NetBSD: umount.c,v 1.53 2020/04/23 04:21:13 christos Exp $ */ 2 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#include <sys/cdefs.h> 33#ifndef lint 34__COPYRIGHT("@(#) Copyright (c) 1980, 1989, 1993\ 35 The Regents of the University of California. All rights reserved."); 36#endif /* not lint */ 37 38#ifndef lint 39#if 0 40static char sccsid[] = "@(#)umount.c 8.8 (Berkeley) 5/8/95"; 41#else 42__RCSID("$NetBSD: umount.c,v 1.53 2020/04/23 04:21:13 christos Exp $"); 43#endif 44#endif /* not lint */ 45 46#include <sys/param.h> 47#include <sys/stat.h> 48#include <sys/mount.h> 49#include <sys/time.h> 50#ifndef SMALL 51#include <sys/socket.h> 52 53#include <netdb.h> 54#include <rpc/rpc.h> 55#include <rpc/pmap_clnt.h> 56#include <rpc/pmap_prot.h> 57#include <nfs/rpcv2.h> 58#include <nfs/nfsmount.h> 59#endif /* !SMALL */ 60 61#include <err.h> 62#include <errno.h> 63#include <fstab.h> 64#include <stdio.h> 65#include <stdlib.h> 66#include <string.h> 67#include <unistd.h> 68#include <util.h> 69 70typedef enum { MNTANY, MNTON, MNTFROM } mntwhat; 71 72#ifndef SMALL 73#include "mountprog.h" 74 75static int fake, verbose; 76static char *nfshost; 77static struct addrinfo *nfshost_ai = NULL; 78 79static int namematch(const struct addrinfo *); 80static int sacmp(const struct sockaddr *, const struct sockaddr *); 81static int xdr_dir(XDR *, char *); 82static const char *getmntproto(const char *); 83#endif /* !SMALL */ 84 85static int fflag; 86static char *getmntname(const char *, mntwhat, char **); 87static int umountfs(const char *, const char **, int); 88static void usage(void) __dead; 89 90int 91main(int argc, char *argv[]) 92{ 93 int ch, errs, all = 0, raw = 0; 94 char mntfromname[MAXPATHLEN]; 95#ifndef SMALL 96 int mnts; 97 struct statvfs *mntbuf; 98 struct addrinfo hints; 99#endif /* SMALL */ 100 const char **typelist = NULL; 101 102#ifdef SMALL 103#define OPTS "fR" 104#else 105#define OPTS "AaFfh:Rt:v" 106#endif 107 while ((ch = getopt(argc, argv, OPTS)) != -1) 108 switch (ch) { 109 case 'f': 110 fflag = MNT_FORCE; 111 break; 112 case 'R': 113 raw = 1; 114 break; 115#ifndef SMALL 116 case 'A': 117 case 'a': 118 all = 1; 119 break; 120 case 'F': 121 fake = 1; 122 break; 123 case 'h': /* -h implies -A. */ 124 all = 1; 125 nfshost = optarg; 126 break; 127 case 't': 128 if (typelist != NULL) 129 errx(1, "only one -t option may be specified."); 130 typelist = makevfslist(optarg); 131 break; 132 case 'v': 133 verbose = 1; 134 break; 135#endif /* !SMALL */ 136 default: 137 usage(); 138 /* NOTREACHED */ 139 } 140 argc -= optind; 141 argv += optind; 142 143 if ((argc == 0 && !all) || (argc != 0 && all) || (all && raw)) 144 usage(); 145 146#ifndef SMALL 147 /* -h implies "-t nfs" if no -t flag. */ 148 if ((nfshost != NULL) && (typelist == NULL)) 149 typelist = makevfslist("nfs"); 150 151 if (nfshost != NULL) { 152 memset(&hints, 0, sizeof hints); 153 if (getaddrinfo(nfshost, NULL, &hints, &nfshost_ai) != 0) { 154 nfshost_ai = NULL; 155 } 156 } 157 158 errs = 0; 159 if (all) { 160 if ((mnts = getmntinfo(&mntbuf, ST_NOWAIT)) == 0) { 161 warn("getmntinfo"); 162 errs = 1; 163 } 164 for (errs = 0, mnts--; mnts > 0; mnts--) { 165 if (checkvfsname(mntbuf[mnts].f_fstypename, typelist)) 166 continue; 167 if (umountfs(mntbuf[mnts].f_mntonname, typelist, 168 1) != 0) 169 errs = 1; 170 } 171 } else 172#endif /* !SMALL */ 173 for (errs = 0; *argv != NULL; ++argv) { 174 if (getfsspecname(mntfromname, sizeof(mntfromname), 175 *argv) == NULL) 176 err(EXIT_FAILURE, "%s", mntfromname); 177 if (umountfs(mntfromname, typelist, raw) != 0) 178 errs = 1; 179 } 180 return errs; 181} 182 183static int 184umountfs(const char *name, const char **typelist, int raw) 185{ 186#ifndef SMALL 187 enum clnt_stat clnt_stat; 188 struct timeval try; 189 CLIENT *clp; 190 char *hostp = NULL; 191 struct addrinfo *ai = NULL, hints; 192 const char *proto = NULL; 193#endif /* !SMALL */ 194 const char *mntpt; 195 char *type, rname[MAXPATHLEN], umountprog[MAXPATHLEN]; 196 mntwhat what; 197 struct stat sb; 198 199 if (raw) { 200 mntpt = name; 201 } else { 202 203 what = MNTANY; 204 if (realpath(name, rname) != NULL) { 205 name = rname; 206 207 if (stat(name, &sb) == 0) { 208 if (S_ISBLK(sb.st_mode)) 209 what = MNTON; 210 else if (S_ISDIR(sb.st_mode)) 211 what = MNTFROM; 212 } 213 } 214#ifdef SMALL 215 else { 216 warn("%s", name); 217 return 1; 218 } 219#endif /* SMALL */ 220 mntpt = name; 221 222 switch (what) { 223 case MNTON: 224 if ((mntpt = getmntname(name, MNTON, &type)) == NULL) { 225 warnx("%s: not currently mounted", name); 226 return (1); 227 } 228 break; 229 case MNTFROM: 230 if ((name = getmntname(mntpt, MNTFROM, &type)) == NULL) { 231 warnx("%s: not currently mounted", mntpt); 232 return (1); 233 } 234 break; 235 default: 236 if ((name = getmntname(mntpt, MNTFROM, &type)) == NULL) { 237 name = mntpt; 238 if ((mntpt = getmntname(name, MNTON, &type)) == NULL) { 239 warnx("%s: not currently mounted", name); 240 return 1; 241 } 242 } 243 } 244 245#ifndef SMALL 246 if (checkvfsname(type, typelist)) 247 return 1; 248 249 (void)memset(&hints, 0, sizeof hints); 250 if (!strncmp(type, MOUNT_NFS, 251 sizeof(((struct statvfs *)NULL)->f_fstypename))) { 252 char *delimp; 253 proto = getmntproto(mntpt); 254 /* look for host:mountpoint */ 255 if ((delimp = strrchr(name, ':')) != NULL) { 256 int len = delimp - name; 257 hostp = malloc(len + 1); 258 if (hostp == NULL) 259 return 1; 260 memcpy(hostp, name, len); 261 hostp[len] = 0; 262 name += len + 1; 263 if (getaddrinfo(hostp, NULL, &hints, &ai) != 0) 264 ai = NULL; 265 } 266 } 267 268 if (!namematch(ai)) 269 return 1; 270#endif /* ! SMALL */ 271 snprintf(umountprog, sizeof(umountprog), "umount_%s", type); 272 } 273 274#ifndef SMALL 275 if (verbose) { 276 (void)printf("%s: unmount from %s\n", name, mntpt); 277 /* put this before the test of FAKE */ 278 if (!raw) { 279 (void)printf("Trying unmount program %s\n", 280 umountprog); 281 } 282 } 283 if (fake) 284 return 0; 285#endif /* ! SMALL */ 286 287 if (!raw) { 288 /* 289 * The only options that need to be passed on are -f 290 * and -v. 291 */ 292 char *args[3]; 293 unsigned nargs = 0; 294 295 args[nargs++] = umountprog; 296 if (fflag == MNT_FORCE) { 297 args[nargs++] = __UNCONST("-f"); 298 } 299#ifndef SMALL 300 if (verbose) { 301 args[nargs++] = __UNCONST("-v"); 302 } 303#endif 304 execvp(umountprog, args); 305 if (errno != ENOENT) { 306 warn("%s: execvp", umountprog); 307 } 308 } 309 310#ifndef SMALL 311 if (verbose) 312 (void)printf("(No separate unmount program.)\n"); 313#endif 314 315 if (unmount(mntpt, fflag) == -1) { 316 warn("%s", mntpt); 317 return 1; 318 } 319 320#ifndef SMALL 321 if (ai != NULL && !(fflag & MNT_FORCE)) { 322 clp = clnt_create(hostp, RPCPROG_MNT, RPCMNT_VER1, proto); 323 if (clp == NULL) { 324 clnt_pcreateerror("Cannot MNT PRC"); 325 return 1; 326 } 327 clp->cl_auth = authsys_create_default(); 328 try.tv_sec = 20; 329 try.tv_usec = 0; 330 clnt_stat = clnt_call(clp, RPCMNT_UMOUNT, xdr_dir, 331 __UNCONST(name), xdr_void, NULL, try); 332 if (clnt_stat != RPC_SUCCESS) { 333 clnt_perror(clp, "Bad MNT RPC"); 334 return 1; 335 } 336 auth_destroy(clp->cl_auth); 337 clnt_destroy(clp); 338 } 339#endif /* ! SMALL */ 340 return 0; 341} 342 343static char * 344getmntname(const char *name, mntwhat what, char **type) 345{ 346 static struct statvfs *mntbuf; 347 static int mntsize; 348 static char mntfromname[MAXPATHLEN]; 349 int i; 350 351 if (mntbuf == NULL && 352 (mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) { 353 warn("getmntinfo"); 354 return (NULL); 355 } 356 for (i = mntsize - 1; i >= 0; i--) { 357 if ((what == MNTON) && !strcmp(mntbuf[i].f_mntfromname, name)) { 358 if (type) 359 *type = mntbuf[i].f_fstypename; 360 return (mntbuf[i].f_mntonname); 361 } 362 if ((what == MNTFROM) && !strcmp(mntbuf[i].f_mntonname, name)) { 363 if (type) 364 *type = mntbuf[i].f_fstypename; 365 if (getfsspecname(mntfromname, sizeof(mntfromname), 366 mntbuf[i].f_mntfromname) == NULL) 367 err(EXIT_FAILURE, "%s", mntfromname); 368 return mntfromname; 369 } 370 } 371 return (NULL); 372} 373 374#ifndef SMALL 375static int 376sacmp(const struct sockaddr *sa1, const struct sockaddr *sa2) 377{ 378 const void *p1, *p2; 379 size_t len; 380 381 if (sa1->sa_family != sa2->sa_family) 382 return 1; 383 384 switch (sa1->sa_family) { 385 case AF_INET: 386 p1 = &((const struct sockaddr_in *)sa1)->sin_addr; 387 p2 = &((const struct sockaddr_in *)sa2)->sin_addr; 388 len = 4; 389 break; 390 case AF_INET6: 391 p1 = &((const struct sockaddr_in6 *)sa1)->sin6_addr; 392 p2 = &((const struct sockaddr_in6 *)sa2)->sin6_addr; 393 len = 16; 394 if (((const struct sockaddr_in6 *)sa1)->sin6_scope_id != 395 ((const struct sockaddr_in6 *)sa2)->sin6_scope_id) 396 return 1; 397 break; 398 default: 399 return 1; 400 } 401 402 return memcmp(p1, p2, len); 403} 404 405static int 406namematch(const struct addrinfo *ai) 407{ 408 struct addrinfo *aip; 409 410 if (nfshost == NULL || nfshost_ai == NULL) 411 return (1); 412 413 while (ai != NULL) { 414 aip = nfshost_ai; 415 while (aip != NULL) { 416 if (sacmp(ai->ai_addr, aip->ai_addr) == 0) 417 return 1; 418 aip = aip->ai_next; 419 } 420 ai = ai->ai_next; 421 } 422 423 return 0; 424} 425 426/* 427 * xdr routines for mount rpc's 428 */ 429static int 430xdr_dir(XDR *xdrsp, char *dirp) 431{ 432 return xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN); 433} 434 435static const char * 436getmntproto(const char *name) 437{ 438 struct nfs_args nfsargs; 439 struct sockaddr_storage ss; 440 441 nfsargs.sotype = SOCK_DGRAM; 442 nfsargs.addr = (struct sockaddr *)&ss; 443 nfsargs.addrlen = sizeof(ss); 444 (void)mount("nfs", name, MNT_GETARGS, &nfsargs, sizeof(nfsargs)); 445 return nfsargs.sotype == SOCK_STREAM ? "tcp" : "udp"; 446} 447#endif /* !SMALL */ 448 449static void 450usage(void) 451{ 452#ifdef SMALL 453 (void)fprintf(stderr, 454 "Usage: %s [-fR] special | node\n", getprogname()); 455#else 456 (void)fprintf(stderr, 457 "Usage: %s [-fvFR] [-t fstypelist] special | node\n" 458 "\t %s -a[fvF] [-h host] [-t fstypelist]\n", getprogname(), 459 getprogname()); 460#endif /* SMALL */ 461 exit(1); 462} 463