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