rpc.umntall.c revision 53494
1/*
2 * Copyright (c) 1999 Martin Blapp
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 */
27
28#ifndef lint
29static const char rcsid[] =
30  "$FreeBSD: head/usr.sbin/rpc.umntall/rpc.umntall.c 53494 1999-11-21 08:06:00Z dillon $";
31#endif /* not lint */
32
33#include <sys/param.h>
34#include <sys/ucred.h>
35#include <sys/mount.h>
36
37#include <rpc/rpc.h>
38#include <nfs/rpcv2.h>
39
40#include <err.h>
41#include <netdb.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <string.h>
45#include <unistd.h>
46
47#include "mounttab.h"
48
49int verbose;
50
51static int do_umount (char *, char *);
52static int do_umntall (char *);
53static int is_mounted (char *, char *);
54static void usage (void);
55int	xdr_dir (XDR *, char *);
56
57struct mtablist *mtabhead;
58
59int
60main(int argc, char **argv) {
61	int ch, keep, success, pathlen;
62	time_t expire, *now;
63	char *host, *path;
64	struct mtablist *mtab;
65
66	mtab = NULL;
67	now = NULL;
68	expire = 0;
69	host = path = '\0';
70	success = keep = verbose = 0;
71	while ((ch = getopt(argc, argv, "h:kp:ve:")) != -1)
72		switch (ch) {
73		case 'h':
74			host = optarg;
75			break;
76		case 'e':
77			expire = (time_t)optarg;
78			break;
79		case 'k':
80			keep = 1;
81			break;
82		case 'p':
83			path = optarg;
84			break;
85		case 'v':
86			verbose = 1;
87			break;
88		case '?':
89			usage();
90		default:
91		}
92	argc -= optind;
93	argv += optind;
94
95	/* Ignore SIGINT and SIGQUIT during shutdown */
96	signal(SIGINT, SIG_IGN);
97	signal(SIGQUIT, SIG_IGN);
98
99	/* Default expiretime is two days */
100	if (expire == 0)
101		expire = 172800;
102	/*
103	 * Read PATH_MOUNTTAB and check each entry
104	 * and do finally the unmounts.
105	 */
106	if (host == NULL && path == NULL) {
107	    	if (!read_mtab(mtab)) {
108			if (verbose)
109				warnx("nothing to do, remove %s",
110				    PATH_MOUNTTAB);
111		}
112		for (mtab = mtabhead; mtab != NULL; mtab = mtab->mtab_next) {
113			if (*mtab->mtab_host != '\0' &&
114			    (do_umntall(mtab->mtab_host) ||
115			    mtab->mtab_time <= (time(now) - expire))) {
116				if (keep && is_mounted(mtab->mtab_host,
117				    mtab->mtab_dirp)) {
118					if (verbose) {
119						warnx("skipping entry %s:%s",
120						    mtab->mtab_host,
121						    mtab->mtab_dirp);
122					}
123				} else
124					clean_mtab(mtab->mtab_host, NULL);
125			}
126		}
127	/* Only do a RPC UMNTALL for this specific host */
128	} else if (host != NULL && path == NULL) {
129		if (!do_umntall(host))
130			exit(1);
131		else
132			success = 1;
133	/* Someone forgot to enter a hostname */
134	} else if (host == NULL && path != NULL)
135		usage();
136	/* Only do a RPC UMOUNT for this specific mount */
137	else {
138		for (pathlen = strlen(path);
139		    pathlen > 1 && path[pathlen - 1] == '/'; pathlen--)
140			path[pathlen - 1] = '\0';
141		if (!do_umount(host, path))
142			exit(1);
143		else
144			success = 1;
145	}
146	/* Write and unlink PATH_MOUNTTAB if necessary */
147	if (success) {
148		if (verbose)
149			warnx("UMOUNT RPC successfully sent to %s", host);
150		if (read_mtab(mtab)) {
151			mtab = mtabhead;
152			clean_mtab(host, path);
153		}
154	}
155	if (!write_mtab()) {
156		free_mtab();
157		exit(1);
158	}
159	free_mtab();
160	exit(0);
161}
162
163/*
164 * Send a RPC_MNT UMNTALL request to hostname.
165 */
166int
167do_umntall(char *hostname) {
168	enum clnt_stat clnt_stat;
169	struct hostent *hp;
170	struct sockaddr_in saddr;
171	struct timeval pertry, try;
172	int so;
173	CLIENT *clp;
174
175	if ((hp = gethostbyname(hostname)) == NULL) {
176		warnx("gethostbyname(%s) failed", hostname);
177		return (0);
178	}
179	memset(&saddr, 0, sizeof(saddr));
180	saddr.sin_family = AF_INET;
181	saddr.sin_port = 0;
182	memmove(&saddr.sin_addr, hp->h_addr, MIN(hp->h_length,
183	    sizeof(saddr.sin_addr)));
184	pertry.tv_sec = 3;
185	pertry.tv_usec = 0;
186	so = RPC_ANYSOCK;
187	if ((clp = clntudp_create(&saddr, RPCPROG_MNT, RPCMNT_VER1,
188	    pertry, &so)) == NULL) {
189		clnt_pcreateerror("Cannot send MNT PRC");
190		return (0);
191	}
192	clp->cl_auth = authunix_create_default();
193	try.tv_sec = 3;
194	try.tv_usec = 0;
195	clnt_stat = clnt_call(clp, RPCMNT_UMNTALL, xdr_void, (caddr_t)0,
196	    xdr_void, (caddr_t)0, try);
197	if (clnt_stat != RPC_SUCCESS) {
198		clnt_perror(clp, "Bad MNT RPC");
199		return (0);
200	} else
201		return (1);
202}
203
204/*
205 * Send a RPC_MNT UMOUNT request for dirp to hostname.
206 */
207int
208do_umount(char *hostname, char *dirp) {
209	enum clnt_stat clnt_stat;
210	struct hostent *hp;
211	struct sockaddr_in saddr;
212	struct timeval pertry, try;
213	CLIENT *clp;
214	int so;
215
216	if ((hp = gethostbyname(hostname)) == NULL) {
217		warnx("gethostbyname(%s) failed", hostname);
218		return (0);
219	}
220	memset(&saddr, 0, sizeof(saddr));
221	saddr.sin_family = AF_INET;
222	saddr.sin_port = 0;
223	memmove(&saddr.sin_addr, hp->h_addr, MIN(hp->h_length,
224	    sizeof(saddr.sin_addr)));
225	pertry.tv_sec = 3;
226	pertry.tv_usec = 0;
227	so = RPC_ANYSOCK;
228	if ((clp = clntudp_create(&saddr, RPCPROG_MNT, RPCMNT_VER1,
229	    pertry, &so)) == NULL) {
230		clnt_pcreateerror("Cannot send MNT PRC");
231		return (0);
232	}
233	clp->cl_auth = authunix_create_default();
234	try.tv_sec = 3;
235	try.tv_usec = 0;
236	clnt_stat = clnt_call(clp, RPCMNT_UMOUNT, xdr_dir, dirp,
237	    xdr_void, (caddr_t)0, try);
238	if (clnt_stat != RPC_SUCCESS) {
239		clnt_perror(clp, "Bad MNT RPC");
240		return (0);
241	}
242	return (1);
243}
244
245/*
246 * Check if the entry is still/already mounted.
247 */
248int
249is_mounted(char *hostname, char *dirp) {
250	struct statfs *mntbuf;
251	char name[MNAMELEN + 1];
252	size_t bufsize, hostlen, dirlen;
253	int mntsize, i;
254
255	hostlen = strlen(hostname);
256	dirlen = strlen(dirp);
257	if ((hostlen + dirlen) >= MNAMELEN)
258		return (0);
259	memmove(name, hostname, hostlen);
260	name[hostlen] = ':';
261	memmove(name + hostlen + 1, dirp, dirlen);
262	name[hostlen + dirlen + 1] = '\0';
263	mntsize = getfsstat(NULL, 0, MNT_NOWAIT);
264	if (mntsize <= 0)
265		return (0);
266	bufsize = (mntsize + 1) * sizeof(struct statfs);
267	if ((mntbuf = malloc(bufsize)) == NULL)
268		err(1, "malloc");
269	mntsize = getfsstat(mntbuf, (long)bufsize, MNT_NOWAIT);
270	for (i = mntsize - 1; i >= 0; i--) {
271		if (strcmp(mntbuf[i].f_mntfromname, name) == 0) {
272			free(mntbuf);
273			return (1);
274		}
275	}
276	free(mntbuf);
277	return (0);
278}
279
280/*
281 * xdr routines for mount rpc's
282 */
283int
284xdr_dir(XDR *xdrsp, char *dirp) {
285
286	return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
287}
288
289static void
290usage() {
291
292	(void)fprintf(stderr, "%s\n",
293	    "usage: rpc.umntall [-h host] [-k] [-p path] [-t expire] [-v]");
294	exit(1);
295}
296