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  * $FreeBSD: src/usr.sbin/rpc.umntall/rpc.umntall.c,v 1.3.2.1 2001/12/13 01:27:20 iedowse Exp $
27  * $DragonFly: src/usr.sbin/rpc.umntall/rpc.umntall.c,v 1.3 2003/11/09 07:11:02 dillon Exp $
28  */
29 
30 #include <sys/param.h>
31 #include <sys/ucred.h>
32 #include <sys/mount.h>
33 
34 #include <rpc/rpc.h>
35 #include <nfs/rpcv2.h>
36 
37 #include <err.h>
38 #include <netdb.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <resolv.h>
44 
45 #include "mounttab.h"
46 
47 int verbose;
48 int fastopt;
49 
50 static int do_umount (char *, char *);
51 static int do_umntall (char *);
52 static int is_mounted (char *, char *);
53 static void usage (void);
54 static struct hostent *gethostbyname_quick(const char *name);
55 
56 int	xdr_dir (XDR *, char *);
57 
58 int
59 main(int argc, char **argv) {
60 	int ch, keep, success, pathlen;
61 	time_t expire, now;
62 	char *host, *path;
63 	struct mtablist *mtab;
64 
65 	expire = 0;
66 	host = path = NULL;
67 	success = keep = verbose = 0;
68 	while ((ch = getopt(argc, argv, "h:kp:vfe:")) != -1)
69 		switch (ch) {
70 		case 'h':
71 			host = optarg;
72 			break;
73 		case 'e':
74 			expire = atoi(optarg);
75 			break;
76 		case 'k':
77 			keep = 1;
78 			break;
79 		case 'p':
80 			path = optarg;
81 			break;
82 		case 'v':
83 			verbose = 1;
84 			break;
85 		case 'f':
86 			fastopt = 1;
87 			break;
88 		case '?':
89 			usage();
90 		default:
91 		}
92 	argc -= optind;
93 	argv += optind;
94 
95 	/* Default expiretime is one day */
96 	if (expire == 0)
97 		expire = 86400;
98 	time(&now);
99 
100 	/* Read PATH_MOUNTTAB. */
101 	if (!read_mtab()) {
102 		if (verbose)
103 			warnx("no mounttab entries (%s does not exist)",
104 			    PATH_MOUNTTAB);
105 		mtabhead = NULL;
106 	}
107 
108 	if (host == NULL && path == NULL) {
109 		/* Check each entry and do any necessary unmount RPCs. */
110 		for (mtab = mtabhead; mtab != NULL; mtab = mtab->mtab_next) {
111 			if (*mtab->mtab_host == '\0')
112 				continue;
113 			if (mtab->mtab_time + expire < now) {
114 				/* Clear expired entry. */
115 				if (verbose)
116 					warnx("remove expired entry %s:%s",
117 					    mtab->mtab_host, mtab->mtab_dirp);
118 				bzero(mtab->mtab_host,
119 				    sizeof(mtab->mtab_host));
120 				continue;
121 			}
122 			if (keep && is_mounted(mtab->mtab_host,
123 			    mtab->mtab_dirp)) {
124 				if (verbose)
125 					warnx("skip entry %s:%s",
126 					    mtab->mtab_host, mtab->mtab_dirp);
127 				continue;
128 			}
129 			if (do_umount(mtab->mtab_host, mtab->mtab_dirp)) {
130 				if (verbose)
131 					warnx("umount RPC for %s:%s succeeded",
132 					    mtab->mtab_host, mtab->mtab_dirp);
133 				/* Remove all entries for this host + path. */
134 				clean_mtab(mtab->mtab_host, mtab->mtab_dirp,
135 				    verbose);
136 			}
137 		}
138 		success = 1;
139 	} else {
140 		if (host == NULL && path != NULL)
141 			/* Missing hostname. */
142 			usage();
143 		if (path == NULL) {
144 			/* Do a RPC UMNTALL for this specific host */
145 			success = do_umntall(host);
146 			if (verbose && success)
147 				warnx("umntall RPC for %s succeeded", host);
148 		} else {
149 			/* Do a RPC UMNTALL for this specific mount */
150 			for (pathlen = strlen(path);
151 			    pathlen > 1 && path[pathlen - 1] == '/'; pathlen--)
152 				path[pathlen - 1] = '\0';
153 			success = do_umount(host, path);
154 			if (verbose && success)
155 				warnx("umount RPC for %s:%s succeeded", host,
156 				    path);
157 		}
158 		/* If successful, remove any corresponding mounttab entries. */
159 		if (success)
160 			clean_mtab(host, path, verbose);
161 	}
162 	/* Write and unlink PATH_MOUNTTAB if necessary */
163 	if (success)
164 		success = write_mtab(verbose);
165 	free_mtab();
166 	exit (success ? 0 : 1);
167 }
168 
169 /*
170  * Send a RPC_MNT UMNTALL request to hostname.
171  * XXX This works for all mountd implementations,
172  * but produces a RPC IOERR on non FreeBSD systems.
173  */
174 int
175 do_umntall(char *hostname) {
176 	enum clnt_stat clnt_stat;
177 	struct hostent *hp;
178 	struct sockaddr_in saddr;
179 	struct timeval pertry, try;
180 	int so;
181 	CLIENT *clp;
182 
183 	if ((hp = gethostbyname_quick(hostname)) == NULL) {
184 		warnx("gethostbyname(%s) failed", hostname);
185 		return (0);
186 	}
187 	memset(&saddr, 0, sizeof(saddr));
188 	saddr.sin_family = AF_INET;
189 	saddr.sin_port = 0;
190 	memmove(&saddr.sin_addr, hp->h_addr, MIN(hp->h_length,
191 	    sizeof(saddr.sin_addr)));
192 	pertry.tv_sec = 3;
193 	pertry.tv_usec = 0;
194 	if (fastopt)
195 	    pmap_getport_timeout(NULL, &pertry);
196 	so = RPC_ANYSOCK;
197 	clp = clntudp_create(&saddr, RPCPROG_MNT, RPCMNT_VER1, pertry, &so);
198 	if (clp == NULL) {
199 		warnx("%s: %s", hostname, clnt_spcreateerror("RPCPROG_MNT"));
200 		return (0);
201 	}
202 	clp->cl_auth = authunix_create_default();
203 	try.tv_sec = 3;
204 	try.tv_usec = 0;
205 	clnt_stat = clnt_call(clp, RPCMNT_UMNTALL, xdr_void, (caddr_t)0,
206 	    xdr_void, (caddr_t)0, try);
207 	if (clnt_stat != RPC_SUCCESS)
208 		warnx("%s: %s", hostname, clnt_sperror(clp, "RPCMNT_UMNTALL"));
209 	auth_destroy(clp->cl_auth);
210 	clnt_destroy(clp);
211 	return (clnt_stat == RPC_SUCCESS);
212 }
213 
214 /*
215  * Send a RPC_MNT UMOUNT request for dirp to hostname.
216  */
217 int
218 do_umount(char *hostname, char *dirp) {
219 	enum clnt_stat clnt_stat;
220 	struct hostent *hp;
221 	struct sockaddr_in saddr;
222 	struct timeval pertry, try;
223 	CLIENT *clp;
224 	int so;
225 
226 	if ((hp = gethostbyname_quick(hostname)) == NULL) {
227 		warnx("gethostbyname(%s) failed", hostname);
228 		return (0);
229 	}
230 	memset(&saddr, 0, sizeof(saddr));
231 	saddr.sin_family = AF_INET;
232 	saddr.sin_port = 0;
233 	memmove(&saddr.sin_addr, hp->h_addr, MIN(hp->h_length,
234 	    sizeof(saddr.sin_addr)));
235 	pertry.tv_sec = 3;
236 	pertry.tv_usec = 0;
237 	if (fastopt)
238 	    pmap_getport_timeout(NULL, &pertry);
239 	so = RPC_ANYSOCK;
240 	clp = clntudp_create(&saddr, RPCPROG_MNT, RPCMNT_VER1, pertry, &so);
241 	if (clp  == NULL) {
242 		warnx("%s: %s", hostname, clnt_spcreateerror("RPCPROG_MNT"));
243 		return (0);
244 	}
245 	clp->cl_auth = authunix_create_default();
246 	try.tv_sec = 3;
247 	try.tv_usec = 0;
248 	clnt_stat = clnt_call(clp, RPCMNT_UMOUNT, xdr_dir, dirp,
249 	    xdr_void, (caddr_t)0, try);
250 	if (clnt_stat != RPC_SUCCESS)
251 		warnx("%s: %s", hostname, clnt_sperror(clp, "RPCMNT_UMOUNT"));
252 	auth_destroy(clp->cl_auth);
253 	clnt_destroy(clp);
254 	return (clnt_stat == RPC_SUCCESS);
255 }
256 
257 /*
258  * Check if the entry is still/already mounted.
259  */
260 int
261 is_mounted(char *hostname, char *dirp) {
262 	struct statfs *mntbuf;
263 	char name[MNAMELEN + 1];
264 	size_t bufsize;
265 	int mntsize, i;
266 
267 	if (strlen(hostname) + strlen(dirp) >= MNAMELEN)
268 		return (0);
269 	snprintf(name, sizeof(name), "%s:%s", hostname, dirp);
270 	mntsize = getfsstat(NULL, 0, MNT_NOWAIT);
271 	if (mntsize <= 0)
272 		return (0);
273 	bufsize = (mntsize + 1) * sizeof(struct statfs);
274 	if ((mntbuf = malloc(bufsize)) == NULL)
275 		err(1, "malloc");
276 	mntsize = getfsstat(mntbuf, (long)bufsize, MNT_NOWAIT);
277 	for (i = mntsize - 1; i >= 0; i--) {
278 		if (strcmp(mntbuf[i].f_mntfromname, name) == 0) {
279 			free(mntbuf);
280 			return (1);
281 		}
282 	}
283 	free(mntbuf);
284 	return (0);
285 }
286 
287 /*
288  * rpc.umntall is often called at boot.  If the network or target host has
289  * issues we want to limit resolver stalls so rpc.umntall doesn't stall
290  * the boot sequence forever.
291  */
292 struct hostent *
293 gethostbyname_quick(const char *name)
294 {
295     struct hostent *he;
296     int save_retrans;
297     int save_retry;
298 
299     save_retrans = _res.retrans;
300     save_retry = _res.retry;
301     if (fastopt) {
302 	_res.retrans = 1;
303 	_res.retry = 2;
304     }
305     he = gethostbyname(name);
306     if (fastopt) {
307 	_res.retrans = save_retrans;
308 	_res.retry = save_retry;
309     }
310     return(he);
311 }
312 
313 /*
314  * xdr routines for mount rpc's
315  */
316 int
317 xdr_dir(XDR *xdrsp, char *dirp) {
318 	return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
319 }
320 
321 static void
322 usage() {
323 	(void)fprintf(stderr, "%s\n",
324 	    "usage: rpc.umntall [-kv] [-e expire] [-h host] [-p path]");
325 	exit(1);
326 }
327