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