rpc.umntall.c revision 53494
153494Sdillon/* 253494Sdillon * Copyright (c) 1999 Martin Blapp 353494Sdillon * All rights reserved. 453494Sdillon * 553494Sdillon * Redistribution and use in source and binary forms, with or without 653494Sdillon * modification, are permitted provided that the following conditions 753494Sdillon * are met: 853494Sdillon * 1. Redistributions of source code must retain the above copyright 953494Sdillon * notice, this list of conditions and the following disclaimer. 1053494Sdillon * 2. Redistributions in binary form must reproduce the above copyright 1153494Sdillon * notice, this list of conditions and the following disclaimer in the 1253494Sdillon * documentation and/or other materials provided with the distribution. 1353494Sdillon * 1453494Sdillon * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1553494Sdillon * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1653494Sdillon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1753494Sdillon * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1853494Sdillon * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1953494Sdillon * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2053494Sdillon * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2153494Sdillon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2253494Sdillon * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2353494Sdillon * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2453494Sdillon * SUCH DAMAGE. 2553494Sdillon * 2653494Sdillon */ 2753494Sdillon 2853494Sdillon#ifndef lint 2953494Sdillonstatic const char rcsid[] = 3053494Sdillon "$FreeBSD: head/usr.sbin/rpc.umntall/rpc.umntall.c 53494 1999-11-21 08:06:00Z dillon $"; 3153494Sdillon#endif /* not lint */ 3253494Sdillon 3353494Sdillon#include <sys/param.h> 3453494Sdillon#include <sys/ucred.h> 3553494Sdillon#include <sys/mount.h> 3653494Sdillon 3753494Sdillon#include <rpc/rpc.h> 3853494Sdillon#include <nfs/rpcv2.h> 3953494Sdillon 4053494Sdillon#include <err.h> 4153494Sdillon#include <netdb.h> 4253494Sdillon#include <stdio.h> 4353494Sdillon#include <stdlib.h> 4453494Sdillon#include <string.h> 4553494Sdillon#include <unistd.h> 4653494Sdillon 4753494Sdillon#include "mounttab.h" 4853494Sdillon 4953494Sdillonint verbose; 5053494Sdillon 5153494Sdillonstatic int do_umount (char *, char *); 5253494Sdillonstatic int do_umntall (char *); 5353494Sdillonstatic int is_mounted (char *, char *); 5453494Sdillonstatic void usage (void); 5553494Sdillonint xdr_dir (XDR *, char *); 5653494Sdillon 5753494Sdillonstruct mtablist *mtabhead; 5853494Sdillon 5953494Sdillonint 6053494Sdillonmain(int argc, char **argv) { 6153494Sdillon int ch, keep, success, pathlen; 6253494Sdillon time_t expire, *now; 6353494Sdillon char *host, *path; 6453494Sdillon struct mtablist *mtab; 6553494Sdillon 6653494Sdillon mtab = NULL; 6753494Sdillon now = NULL; 6853494Sdillon expire = 0; 6953494Sdillon host = path = '\0'; 7053494Sdillon success = keep = verbose = 0; 7153494Sdillon while ((ch = getopt(argc, argv, "h:kp:ve:")) != -1) 7253494Sdillon switch (ch) { 7353494Sdillon case 'h': 7453494Sdillon host = optarg; 7553494Sdillon break; 7653494Sdillon case 'e': 7753494Sdillon expire = (time_t)optarg; 7853494Sdillon break; 7953494Sdillon case 'k': 8053494Sdillon keep = 1; 8153494Sdillon break; 8253494Sdillon case 'p': 8353494Sdillon path = optarg; 8453494Sdillon break; 8553494Sdillon case 'v': 8653494Sdillon verbose = 1; 8753494Sdillon break; 8853494Sdillon case '?': 8953494Sdillon usage(); 9053494Sdillon default: 9153494Sdillon } 9253494Sdillon argc -= optind; 9353494Sdillon argv += optind; 9453494Sdillon 9553494Sdillon /* Ignore SIGINT and SIGQUIT during shutdown */ 9653494Sdillon signal(SIGINT, SIG_IGN); 9753494Sdillon signal(SIGQUIT, SIG_IGN); 9853494Sdillon 9953494Sdillon /* Default expiretime is two days */ 10053494Sdillon if (expire == 0) 10153494Sdillon expire = 172800; 10253494Sdillon /* 10353494Sdillon * Read PATH_MOUNTTAB and check each entry 10453494Sdillon * and do finally the unmounts. 10553494Sdillon */ 10653494Sdillon if (host == NULL && path == NULL) { 10753494Sdillon if (!read_mtab(mtab)) { 10853494Sdillon if (verbose) 10953494Sdillon warnx("nothing to do, remove %s", 11053494Sdillon PATH_MOUNTTAB); 11153494Sdillon } 11253494Sdillon for (mtab = mtabhead; mtab != NULL; mtab = mtab->mtab_next) { 11353494Sdillon if (*mtab->mtab_host != '\0' && 11453494Sdillon (do_umntall(mtab->mtab_host) || 11553494Sdillon mtab->mtab_time <= (time(now) - expire))) { 11653494Sdillon if (keep && is_mounted(mtab->mtab_host, 11753494Sdillon mtab->mtab_dirp)) { 11853494Sdillon if (verbose) { 11953494Sdillon warnx("skipping entry %s:%s", 12053494Sdillon mtab->mtab_host, 12153494Sdillon mtab->mtab_dirp); 12253494Sdillon } 12353494Sdillon } else 12453494Sdillon clean_mtab(mtab->mtab_host, NULL); 12553494Sdillon } 12653494Sdillon } 12753494Sdillon /* Only do a RPC UMNTALL for this specific host */ 12853494Sdillon } else if (host != NULL && path == NULL) { 12953494Sdillon if (!do_umntall(host)) 13053494Sdillon exit(1); 13153494Sdillon else 13253494Sdillon success = 1; 13353494Sdillon /* Someone forgot to enter a hostname */ 13453494Sdillon } else if (host == NULL && path != NULL) 13553494Sdillon usage(); 13653494Sdillon /* Only do a RPC UMOUNT for this specific mount */ 13753494Sdillon else { 13853494Sdillon for (pathlen = strlen(path); 13953494Sdillon pathlen > 1 && path[pathlen - 1] == '/'; pathlen--) 14053494Sdillon path[pathlen - 1] = '\0'; 14153494Sdillon if (!do_umount(host, path)) 14253494Sdillon exit(1); 14353494Sdillon else 14453494Sdillon success = 1; 14553494Sdillon } 14653494Sdillon /* Write and unlink PATH_MOUNTTAB if necessary */ 14753494Sdillon if (success) { 14853494Sdillon if (verbose) 14953494Sdillon warnx("UMOUNT RPC successfully sent to %s", host); 15053494Sdillon if (read_mtab(mtab)) { 15153494Sdillon mtab = mtabhead; 15253494Sdillon clean_mtab(host, path); 15353494Sdillon } 15453494Sdillon } 15553494Sdillon if (!write_mtab()) { 15653494Sdillon free_mtab(); 15753494Sdillon exit(1); 15853494Sdillon } 15953494Sdillon free_mtab(); 16053494Sdillon exit(0); 16153494Sdillon} 16253494Sdillon 16353494Sdillon/* 16453494Sdillon * Send a RPC_MNT UMNTALL request to hostname. 16553494Sdillon */ 16653494Sdillonint 16753494Sdillondo_umntall(char *hostname) { 16853494Sdillon enum clnt_stat clnt_stat; 16953494Sdillon struct hostent *hp; 17053494Sdillon struct sockaddr_in saddr; 17153494Sdillon struct timeval pertry, try; 17253494Sdillon int so; 17353494Sdillon CLIENT *clp; 17453494Sdillon 17553494Sdillon if ((hp = gethostbyname(hostname)) == NULL) { 17653494Sdillon warnx("gethostbyname(%s) failed", hostname); 17753494Sdillon return (0); 17853494Sdillon } 17953494Sdillon memset(&saddr, 0, sizeof(saddr)); 18053494Sdillon saddr.sin_family = AF_INET; 18153494Sdillon saddr.sin_port = 0; 18253494Sdillon memmove(&saddr.sin_addr, hp->h_addr, MIN(hp->h_length, 18353494Sdillon sizeof(saddr.sin_addr))); 18453494Sdillon pertry.tv_sec = 3; 18553494Sdillon pertry.tv_usec = 0; 18653494Sdillon so = RPC_ANYSOCK; 18753494Sdillon if ((clp = clntudp_create(&saddr, RPCPROG_MNT, RPCMNT_VER1, 18853494Sdillon pertry, &so)) == NULL) { 18953494Sdillon clnt_pcreateerror("Cannot send MNT PRC"); 19053494Sdillon return (0); 19153494Sdillon } 19253494Sdillon clp->cl_auth = authunix_create_default(); 19353494Sdillon try.tv_sec = 3; 19453494Sdillon try.tv_usec = 0; 19553494Sdillon clnt_stat = clnt_call(clp, RPCMNT_UMNTALL, xdr_void, (caddr_t)0, 19653494Sdillon xdr_void, (caddr_t)0, try); 19753494Sdillon if (clnt_stat != RPC_SUCCESS) { 19853494Sdillon clnt_perror(clp, "Bad MNT RPC"); 19953494Sdillon return (0); 20053494Sdillon } else 20153494Sdillon return (1); 20253494Sdillon} 20353494Sdillon 20453494Sdillon/* 20553494Sdillon * Send a RPC_MNT UMOUNT request for dirp to hostname. 20653494Sdillon */ 20753494Sdillonint 20853494Sdillondo_umount(char *hostname, char *dirp) { 20953494Sdillon enum clnt_stat clnt_stat; 21053494Sdillon struct hostent *hp; 21153494Sdillon struct sockaddr_in saddr; 21253494Sdillon struct timeval pertry, try; 21353494Sdillon CLIENT *clp; 21453494Sdillon int so; 21553494Sdillon 21653494Sdillon if ((hp = gethostbyname(hostname)) == NULL) { 21753494Sdillon warnx("gethostbyname(%s) failed", hostname); 21853494Sdillon return (0); 21953494Sdillon } 22053494Sdillon memset(&saddr, 0, sizeof(saddr)); 22153494Sdillon saddr.sin_family = AF_INET; 22253494Sdillon saddr.sin_port = 0; 22353494Sdillon memmove(&saddr.sin_addr, hp->h_addr, MIN(hp->h_length, 22453494Sdillon sizeof(saddr.sin_addr))); 22553494Sdillon pertry.tv_sec = 3; 22653494Sdillon pertry.tv_usec = 0; 22753494Sdillon so = RPC_ANYSOCK; 22853494Sdillon if ((clp = clntudp_create(&saddr, RPCPROG_MNT, RPCMNT_VER1, 22953494Sdillon pertry, &so)) == NULL) { 23053494Sdillon clnt_pcreateerror("Cannot send MNT PRC"); 23153494Sdillon return (0); 23253494Sdillon } 23353494Sdillon clp->cl_auth = authunix_create_default(); 23453494Sdillon try.tv_sec = 3; 23553494Sdillon try.tv_usec = 0; 23653494Sdillon clnt_stat = clnt_call(clp, RPCMNT_UMOUNT, xdr_dir, dirp, 23753494Sdillon xdr_void, (caddr_t)0, try); 23853494Sdillon if (clnt_stat != RPC_SUCCESS) { 23953494Sdillon clnt_perror(clp, "Bad MNT RPC"); 24053494Sdillon return (0); 24153494Sdillon } 24253494Sdillon return (1); 24353494Sdillon} 24453494Sdillon 24553494Sdillon/* 24653494Sdillon * Check if the entry is still/already mounted. 24753494Sdillon */ 24853494Sdillonint 24953494Sdillonis_mounted(char *hostname, char *dirp) { 25053494Sdillon struct statfs *mntbuf; 25153494Sdillon char name[MNAMELEN + 1]; 25253494Sdillon size_t bufsize, hostlen, dirlen; 25353494Sdillon int mntsize, i; 25453494Sdillon 25553494Sdillon hostlen = strlen(hostname); 25653494Sdillon dirlen = strlen(dirp); 25753494Sdillon if ((hostlen + dirlen) >= MNAMELEN) 25853494Sdillon return (0); 25953494Sdillon memmove(name, hostname, hostlen); 26053494Sdillon name[hostlen] = ':'; 26153494Sdillon memmove(name + hostlen + 1, dirp, dirlen); 26253494Sdillon name[hostlen + dirlen + 1] = '\0'; 26353494Sdillon mntsize = getfsstat(NULL, 0, MNT_NOWAIT); 26453494Sdillon if (mntsize <= 0) 26553494Sdillon return (0); 26653494Sdillon bufsize = (mntsize + 1) * sizeof(struct statfs); 26753494Sdillon if ((mntbuf = malloc(bufsize)) == NULL) 26853494Sdillon err(1, "malloc"); 26953494Sdillon mntsize = getfsstat(mntbuf, (long)bufsize, MNT_NOWAIT); 27053494Sdillon for (i = mntsize - 1; i >= 0; i--) { 27153494Sdillon if (strcmp(mntbuf[i].f_mntfromname, name) == 0) { 27253494Sdillon free(mntbuf); 27353494Sdillon return (1); 27453494Sdillon } 27553494Sdillon } 27653494Sdillon free(mntbuf); 27753494Sdillon return (0); 27853494Sdillon} 27953494Sdillon 28053494Sdillon/* 28153494Sdillon * xdr routines for mount rpc's 28253494Sdillon */ 28353494Sdillonint 28453494Sdillonxdr_dir(XDR *xdrsp, char *dirp) { 28553494Sdillon 28653494Sdillon return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN)); 28753494Sdillon} 28853494Sdillon 28953494Sdillonstatic void 29053494Sdillonusage() { 29153494Sdillon 29253494Sdillon (void)fprintf(stderr, "%s\n", 29353494Sdillon "usage: rpc.umntall [-h host] [-k] [-p path] [-t expire] [-v]"); 29453494Sdillon exit(1); 29553494Sdillon} 296