xref: /freebsd/usr.sbin/rpc.umntall/rpc.umntall.c (revision eba230afba4932f02a1ca44efc797cf7499a5cb0)
11de7b4b8SPedro F. Giffuni /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni  *
4c69a34d4SMatthew Dillon  * Copyright (c) 1999 Martin Blapp
5c69a34d4SMatthew Dillon  * All rights reserved.
6c69a34d4SMatthew Dillon  *
7c69a34d4SMatthew Dillon  * Redistribution and use in source and binary forms, with or without
8c69a34d4SMatthew Dillon  * modification, are permitted provided that the following conditions
9c69a34d4SMatthew Dillon  * are met:
10c69a34d4SMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
11c69a34d4SMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
12c69a34d4SMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
13c69a34d4SMatthew Dillon  *    notice, this list of conditions and the following disclaimer in the
14c69a34d4SMatthew Dillon  *    documentation and/or other materials provided with the distribution.
15c69a34d4SMatthew Dillon  *
16c69a34d4SMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17c69a34d4SMatthew Dillon  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18c69a34d4SMatthew Dillon  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19c69a34d4SMatthew Dillon  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20c69a34d4SMatthew Dillon  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21c69a34d4SMatthew Dillon  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22c69a34d4SMatthew Dillon  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23c69a34d4SMatthew Dillon  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24c69a34d4SMatthew Dillon  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25c69a34d4SMatthew Dillon  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26c69a34d4SMatthew Dillon  * SUCH DAMAGE.
27c69a34d4SMatthew Dillon  *
28c69a34d4SMatthew Dillon  */
29c69a34d4SMatthew Dillon 
30c69a34d4SMatthew Dillon #include <sys/param.h>
31c69a34d4SMatthew Dillon #include <sys/ucred.h>
32c69a34d4SMatthew Dillon #include <sys/mount.h>
33c69a34d4SMatthew Dillon 
34c69a34d4SMatthew Dillon #include <rpc/rpc.h>
350775314bSDoug Rabson #include <rpcsvc/mount.h>
36c69a34d4SMatthew Dillon 
37c69a34d4SMatthew Dillon #include <err.h>
38c69a34d4SMatthew Dillon #include <netdb.h>
39c69a34d4SMatthew Dillon #include <stdio.h>
40c69a34d4SMatthew Dillon #include <stdlib.h>
41c69a34d4SMatthew Dillon #include <string.h>
42c69a34d4SMatthew Dillon #include <unistd.h>
43c69a34d4SMatthew Dillon 
44c69a34d4SMatthew Dillon #include "mounttab.h"
45c69a34d4SMatthew Dillon 
46c69a34d4SMatthew Dillon int verbose;
47c69a34d4SMatthew Dillon 
48c69a34d4SMatthew Dillon static int do_umount (char *, char *);
49c69a34d4SMatthew Dillon static int do_umntall (char *);
50c69a34d4SMatthew Dillon static int is_mounted (char *, char *);
51c69a34d4SMatthew Dillon static void usage (void);
52c69a34d4SMatthew Dillon int	xdr_dir (XDR *, char *);
53c69a34d4SMatthew Dillon 
54c69a34d4SMatthew Dillon int
main(int argc,char ** argv)55c69a34d4SMatthew Dillon main(int argc, char **argv) {
56c69a34d4SMatthew Dillon 	int ch, keep, success, pathlen;
57d730d3b4SIan Dowse 	time_t expire, now;
58c69a34d4SMatthew Dillon 	char *host, *path;
59c69a34d4SMatthew Dillon 	struct mtablist *mtab;
60c69a34d4SMatthew Dillon 
61c69a34d4SMatthew Dillon 	expire = 0;
62d730d3b4SIan Dowse 	host = path = NULL;
63c69a34d4SMatthew Dillon 	success = keep = verbose = 0;
64c69a34d4SMatthew Dillon 	while ((ch = getopt(argc, argv, "h:kp:ve:")) != -1)
65c69a34d4SMatthew Dillon 		switch (ch) {
66c69a34d4SMatthew Dillon 		case 'h':
67c69a34d4SMatthew Dillon 			host = optarg;
68c69a34d4SMatthew Dillon 			break;
69c69a34d4SMatthew Dillon 		case 'e':
707b2df384SJonathan Lemon 			expire = atoi(optarg);
71c69a34d4SMatthew Dillon 			break;
72c69a34d4SMatthew Dillon 		case 'k':
73c69a34d4SMatthew Dillon 			keep = 1;
74c69a34d4SMatthew Dillon 			break;
75c69a34d4SMatthew Dillon 		case 'p':
76c69a34d4SMatthew Dillon 			path = optarg;
77c69a34d4SMatthew Dillon 			break;
78c69a34d4SMatthew Dillon 		case 'v':
79c69a34d4SMatthew Dillon 			verbose = 1;
80c69a34d4SMatthew Dillon 			break;
81c69a34d4SMatthew Dillon 		case '?':
82c69a34d4SMatthew Dillon 			usage();
83c69a34d4SMatthew Dillon 		default:
84891c2f9eSWarner Losh 			break;
85c69a34d4SMatthew Dillon 		}
86c69a34d4SMatthew Dillon 	argc -= optind;
87c69a34d4SMatthew Dillon 	argv += optind;
88c69a34d4SMatthew Dillon 
89ab80d6faSBrian Feldman 	/* Default expiretime is one day */
90c69a34d4SMatthew Dillon 	if (expire == 0)
91ab80d6faSBrian Feldman 		expire = 86400;
92d730d3b4SIan Dowse 	time(&now);
93d730d3b4SIan Dowse 
94d730d3b4SIan Dowse 	/* Read PATH_MOUNTTAB. */
95afe1ef24SIan Dowse 	if (!read_mtab()) {
96c69a34d4SMatthew Dillon 		if (verbose)
97d730d3b4SIan Dowse 			warnx("no mounttab entries (%s does not exist)",
98c69a34d4SMatthew Dillon 			    PATH_MOUNTTAB);
99d730d3b4SIan Dowse 		mtabhead = NULL;
100c69a34d4SMatthew Dillon 	}
101d730d3b4SIan Dowse 
102d730d3b4SIan Dowse 	if (host == NULL && path == NULL) {
103d730d3b4SIan Dowse 		/* Check each entry and do any necessary unmount RPCs. */
104c69a34d4SMatthew Dillon 		for (mtab = mtabhead; mtab != NULL; mtab = mtab->mtab_next) {
105d730d3b4SIan Dowse 			if (*mtab->mtab_host == '\0')
106d730d3b4SIan Dowse 				continue;
107d730d3b4SIan Dowse 			if (mtab->mtab_time + expire < now) {
108d730d3b4SIan Dowse 				/* Clear expired entry. */
109d730d3b4SIan Dowse 				if (verbose)
110d730d3b4SIan Dowse 					warnx("remove expired entry %s:%s",
111d730d3b4SIan Dowse 					    mtab->mtab_host, mtab->mtab_dirp);
112d730d3b4SIan Dowse 				bzero(mtab->mtab_host,
113d730d3b4SIan Dowse 				    sizeof(mtab->mtab_host));
114d730d3b4SIan Dowse 				continue;
115d730d3b4SIan Dowse 			}
116c69a34d4SMatthew Dillon 			if (keep && is_mounted(mtab->mtab_host,
117c69a34d4SMatthew Dillon 			    mtab->mtab_dirp)) {
118d730d3b4SIan Dowse 				if (verbose)
119ab80d6faSBrian Feldman 					warnx("skip entry %s:%s",
120d730d3b4SIan Dowse 					    mtab->mtab_host, mtab->mtab_dirp);
121d730d3b4SIan Dowse 				continue;
122c69a34d4SMatthew Dillon 			}
123d730d3b4SIan Dowse 			if (do_umount(mtab->mtab_host, mtab->mtab_dirp)) {
124d730d3b4SIan Dowse 				if (verbose)
125d730d3b4SIan Dowse 					warnx("umount RPC for %s:%s succeeded",
126d730d3b4SIan Dowse 					    mtab->mtab_host, mtab->mtab_dirp);
127d730d3b4SIan Dowse 				/* Remove all entries for this host + path. */
128afe1ef24SIan Dowse 				clean_mtab(mtab->mtab_host, mtab->mtab_dirp,
129afe1ef24SIan Dowse 				    verbose);
13010cb4f4aSGreg Lehey 			}
131c69a34d4SMatthew Dillon 		}
132c69a34d4SMatthew Dillon 		success = 1;
133d730d3b4SIan Dowse 	} else {
134d730d3b4SIan Dowse 		if (host == NULL && path != NULL)
135d730d3b4SIan Dowse 			/* Missing hostname. */
136c69a34d4SMatthew Dillon 			usage();
137d730d3b4SIan Dowse 		if (path == NULL) {
138d730d3b4SIan Dowse 			/* Do a RPC UMNTALL for this specific host */
139d730d3b4SIan Dowse 			success = do_umntall(host);
140d730d3b4SIan Dowse 			if (verbose && success)
141d730d3b4SIan Dowse 				warnx("umntall RPC for %s succeeded", host);
142d730d3b4SIan Dowse 		} else {
143d730d3b4SIan Dowse 			/* Do a RPC UMNTALL for this specific mount */
144c69a34d4SMatthew Dillon 			for (pathlen = strlen(path);
145c69a34d4SMatthew Dillon 			    pathlen > 1 && path[pathlen - 1] == '/'; pathlen--)
146c69a34d4SMatthew Dillon 				path[pathlen - 1] = '\0';
147d730d3b4SIan Dowse 			success = do_umount(host, path);
148d730d3b4SIan Dowse 			if (verbose && success)
149d730d3b4SIan Dowse 				warnx("umount RPC for %s:%s succeeded", host,
150d730d3b4SIan Dowse 				    path);
151c69a34d4SMatthew Dillon 		}
152d730d3b4SIan Dowse 		/* If successful, remove any corresponding mounttab entries. */
153d730d3b4SIan Dowse 		if (success)
154afe1ef24SIan Dowse 			clean_mtab(host, path, verbose);
155c69a34d4SMatthew Dillon 	}
156d730d3b4SIan Dowse 	/* Write and unlink PATH_MOUNTTAB if necessary */
157d730d3b4SIan Dowse 	if (success)
158afe1ef24SIan Dowse 		success = write_mtab(verbose);
159c69a34d4SMatthew Dillon 	free_mtab();
160d730d3b4SIan Dowse 	exit (success ? 0 : 1);
161c69a34d4SMatthew Dillon }
162c69a34d4SMatthew Dillon 
163c69a34d4SMatthew Dillon /*
164c69a34d4SMatthew Dillon  * Send a RPC_MNT UMNTALL request to hostname.
16510cb4f4aSGreg Lehey  * XXX This works for all mountd implementations,
16610cb4f4aSGreg Lehey  * but produces a RPC IOERR on non FreeBSD systems.
167c69a34d4SMatthew Dillon  */
168c69a34d4SMatthew Dillon int
do_umntall(char * hostname)169c69a34d4SMatthew Dillon do_umntall(char *hostname) {
170c69a34d4SMatthew Dillon 	enum clnt_stat clnt_stat;
1718360efbdSAlfred Perlstein 	struct timeval try;
172c69a34d4SMatthew Dillon 	CLIENT *clp;
173c69a34d4SMatthew Dillon 
174b4bb2bf4SMaxime Henrion 	try.tv_sec = 3;
175b4bb2bf4SMaxime Henrion 	try.tv_usec = 0;
1760775314bSDoug Rabson 	clp = clnt_create_timed(hostname, MOUNTPROG, MOUNTVERS, "udp",
177b4bb2bf4SMaxime Henrion 	    &try);
1788360efbdSAlfred Perlstein 	if (clp == NULL) {
1790775314bSDoug Rabson 		warnx("%s: %s", hostname, clnt_spcreateerror("MOUNTPROG"));
180146e669bSIan Dowse 		return (0);
181c69a34d4SMatthew Dillon 	}
182c69a34d4SMatthew Dillon 	clp->cl_auth = authunix_create_default();
1830775314bSDoug Rabson 	clnt_stat = clnt_call(clp, MOUNTPROC_UMNTALL,
184422e293cSPeter Wemm 	    (xdrproc_t)xdr_void, (caddr_t)0,
185422e293cSPeter Wemm 	    (xdrproc_t)xdr_void, (caddr_t)0, try);
186146e669bSIan Dowse 	if (clnt_stat != RPC_SUCCESS)
1870775314bSDoug Rabson 		warnx("%s: %s", hostname, clnt_sperror(clp, "MOUNTPROC_UMNTALL"));
188146e669bSIan Dowse 	auth_destroy(clp->cl_auth);
189146e669bSIan Dowse 	clnt_destroy(clp);
190146e669bSIan Dowse 	return (clnt_stat == RPC_SUCCESS);
191c69a34d4SMatthew Dillon }
192c69a34d4SMatthew Dillon 
193c69a34d4SMatthew Dillon /*
194c69a34d4SMatthew Dillon  * Send a RPC_MNT UMOUNT request for dirp to hostname.
195c69a34d4SMatthew Dillon  */
196c69a34d4SMatthew Dillon int
do_umount(char * hostname,char * dirp)197c69a34d4SMatthew Dillon do_umount(char *hostname, char *dirp) {
198c69a34d4SMatthew Dillon 	enum clnt_stat clnt_stat;
1998360efbdSAlfred Perlstein 	struct timeval try;
200c69a34d4SMatthew Dillon 	CLIENT *clp;
201c69a34d4SMatthew Dillon 
202b4bb2bf4SMaxime Henrion 	try.tv_sec = 3;
203b4bb2bf4SMaxime Henrion 	try.tv_usec = 0;
2040775314bSDoug Rabson 	clp = clnt_create_timed(hostname, MOUNTPROG, MOUNTVERS, "udp",
205b4bb2bf4SMaxime Henrion 	    &try);
2068360efbdSAlfred Perlstein 	if (clp  == NULL) {
2070775314bSDoug Rabson 		warnx("%s: %s", hostname, clnt_spcreateerror("MOUNTPROG"));
208146e669bSIan Dowse 		return (0);
209c69a34d4SMatthew Dillon 	}
2108360efbdSAlfred Perlstein 	clp->cl_auth = authsys_create_default();
2110775314bSDoug Rabson 	clnt_stat = clnt_call(clp, MOUNTPROC_UMNT, (xdrproc_t)xdr_dir, dirp,
212422e293cSPeter Wemm 	    (xdrproc_t)xdr_void, (caddr_t)0, try);
213146e669bSIan Dowse 	if (clnt_stat != RPC_SUCCESS)
2140775314bSDoug Rabson 		warnx("%s: %s", hostname, clnt_sperror(clp, "MOUNTPROC_UMNT"));
215146e669bSIan Dowse 	auth_destroy(clp->cl_auth);
216146e669bSIan Dowse 	clnt_destroy(clp);
217146e669bSIan Dowse 	return (clnt_stat == RPC_SUCCESS);
218c69a34d4SMatthew Dillon }
219c69a34d4SMatthew Dillon 
220c69a34d4SMatthew Dillon /*
221c69a34d4SMatthew Dillon  * Check if the entry is still/already mounted.
222c69a34d4SMatthew Dillon  */
223c69a34d4SMatthew Dillon int
is_mounted(char * hostname,char * dirp)224c69a34d4SMatthew Dillon is_mounted(char *hostname, char *dirp) {
225c69a34d4SMatthew Dillon 	struct statfs *mntbuf;
226c69a34d4SMatthew Dillon 	char name[MNAMELEN + 1];
227d730d3b4SIan Dowse 	size_t bufsize;
228c69a34d4SMatthew Dillon 	int mntsize, i;
229c69a34d4SMatthew Dillon 
230d730d3b4SIan Dowse 	if (strlen(hostname) + strlen(dirp) >= MNAMELEN)
231c69a34d4SMatthew Dillon 		return (0);
232d730d3b4SIan Dowse 	snprintf(name, sizeof(name), "%s:%s", hostname, dirp);
233c69a34d4SMatthew Dillon 	mntsize = getfsstat(NULL, 0, MNT_NOWAIT);
234c69a34d4SMatthew Dillon 	if (mntsize <= 0)
235c69a34d4SMatthew Dillon 		return (0);
236c69a34d4SMatthew Dillon 	bufsize = (mntsize + 1) * sizeof(struct statfs);
237c69a34d4SMatthew Dillon 	if ((mntbuf = malloc(bufsize)) == NULL)
238c69a34d4SMatthew Dillon 		err(1, "malloc");
239c69a34d4SMatthew Dillon 	mntsize = getfsstat(mntbuf, (long)bufsize, MNT_NOWAIT);
240c69a34d4SMatthew Dillon 	for (i = mntsize - 1; i >= 0; i--) {
241c69a34d4SMatthew Dillon 		if (strcmp(mntbuf[i].f_mntfromname, name) == 0) {
242c69a34d4SMatthew Dillon 			free(mntbuf);
243c69a34d4SMatthew Dillon 			return (1);
244c69a34d4SMatthew Dillon 		}
245c69a34d4SMatthew Dillon 	}
246c69a34d4SMatthew Dillon 	free(mntbuf);
247c69a34d4SMatthew Dillon 	return (0);
248c69a34d4SMatthew Dillon }
249c69a34d4SMatthew Dillon 
250c69a34d4SMatthew Dillon /*
251c69a34d4SMatthew Dillon  * xdr routines for mount rpc's
252c69a34d4SMatthew Dillon  */
253c69a34d4SMatthew Dillon int
xdr_dir(XDR * xdrsp,char * dirp)254c69a34d4SMatthew Dillon xdr_dir(XDR *xdrsp, char *dirp) {
2550775314bSDoug Rabson 	return (xdr_string(xdrsp, &dirp, MNTPATHLEN));
256c69a34d4SMatthew Dillon }
257c69a34d4SMatthew Dillon 
258c69a34d4SMatthew Dillon static void
usage(void)259bd22d268SJohn Baldwin usage(void)
260bd22d268SJohn Baldwin {
261c69a34d4SMatthew Dillon 	(void)fprintf(stderr, "%s\n",
262d730d3b4SIan Dowse 	    "usage: rpc.umntall [-kv] [-e expire] [-h host] [-p path]");
263c69a34d4SMatthew Dillon 	exit(1);
264c69a34d4SMatthew Dillon }
265