1c69a34d4SMatthew Dillon /* 2c69a34d4SMatthew Dillon * Copyright (c) 1999 Martin Blapp 3c69a34d4SMatthew Dillon * All rights reserved. 4c69a34d4SMatthew Dillon * 5c69a34d4SMatthew Dillon * Redistribution and use in source and binary forms, with or without 6c69a34d4SMatthew Dillon * modification, are permitted provided that the following conditions 7c69a34d4SMatthew Dillon * are met: 8c69a34d4SMatthew Dillon * 1. Redistributions of source code must retain the above copyright 9c69a34d4SMatthew Dillon * notice, this list of conditions and the following disclaimer. 10c69a34d4SMatthew Dillon * 2. Redistributions in binary form must reproduce the above copyright 11c69a34d4SMatthew Dillon * notice, this list of conditions and the following disclaimer in the 12c69a34d4SMatthew Dillon * documentation and/or other materials provided with the distribution. 13c69a34d4SMatthew Dillon * 14c69a34d4SMatthew Dillon * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15c69a34d4SMatthew Dillon * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16c69a34d4SMatthew Dillon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17c69a34d4SMatthew Dillon * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18c69a34d4SMatthew Dillon * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19c69a34d4SMatthew Dillon * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20c69a34d4SMatthew Dillon * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21c69a34d4SMatthew Dillon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22c69a34d4SMatthew Dillon * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23c69a34d4SMatthew Dillon * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24c69a34d4SMatthew Dillon * SUCH DAMAGE. 25c69a34d4SMatthew Dillon * 26c69a34d4SMatthew Dillon */ 27c69a34d4SMatthew Dillon 28c69a34d4SMatthew Dillon #ifndef lint 29c69a34d4SMatthew Dillon static const char rcsid[] = 30c69a34d4SMatthew Dillon "$FreeBSD$"; 31c69a34d4SMatthew Dillon #endif /* not lint */ 32c69a34d4SMatthew Dillon 33c69a34d4SMatthew Dillon #include <sys/syslog.h> 34c69a34d4SMatthew Dillon 35c69a34d4SMatthew Dillon #include <rpc/rpc.h> 36c69a34d4SMatthew Dillon #include <nfs/rpcv2.h> 37c69a34d4SMatthew Dillon 38c69a34d4SMatthew Dillon #include <err.h> 39c69a34d4SMatthew Dillon #include <errno.h> 40afe1ef24SIan Dowse #include <limits.h> 41c69a34d4SMatthew Dillon #include <stdio.h> 42c69a34d4SMatthew Dillon #include <stdlib.h> 43c69a34d4SMatthew Dillon #include <string.h> 44c69a34d4SMatthew Dillon #include <unistd.h> 45c69a34d4SMatthew Dillon 46c69a34d4SMatthew Dillon #include "mounttab.h" 47c69a34d4SMatthew Dillon 48c69a34d4SMatthew Dillon struct mtablist *mtabhead; 49c69a34d4SMatthew Dillon 50afe1ef24SIan Dowse static void badline(char *field, char *bad); 51afe1ef24SIan Dowse 52c69a34d4SMatthew Dillon /* 53c69a34d4SMatthew Dillon * Add an entry to PATH_MOUNTTAB for each mounted NFS filesystem, 54c69a34d4SMatthew Dillon * so the client can notify the NFS server even after reboot. 55c69a34d4SMatthew Dillon */ 56c69a34d4SMatthew Dillon int 57c69a34d4SMatthew Dillon add_mtab(char *hostp, char *dirp) { 58c69a34d4SMatthew Dillon FILE *mtabfile; 59c69a34d4SMatthew Dillon 60c69a34d4SMatthew Dillon if ((mtabfile = fopen(PATH_MOUNTTAB, "a")) == NULL) 61c69a34d4SMatthew Dillon return (0); 62c69a34d4SMatthew Dillon else { 6397f1ce75SBruce Evans fprintf(mtabfile, "%ld\t%s\t%s\n", 64afe1ef24SIan Dowse (long)time(NULL), hostp, dirp); 65c69a34d4SMatthew Dillon fclose(mtabfile); 66c69a34d4SMatthew Dillon return (1); 67c69a34d4SMatthew Dillon } 68c69a34d4SMatthew Dillon } 69c69a34d4SMatthew Dillon 70c69a34d4SMatthew Dillon /* 71c69a34d4SMatthew Dillon * Read mounttab line for line and return struct mtablist. 72c69a34d4SMatthew Dillon */ 73c69a34d4SMatthew Dillon int 74afe1ef24SIan Dowse read_mtab() { 75afe1ef24SIan Dowse struct mtablist **mtabpp, *mtabp; 76c69a34d4SMatthew Dillon char *hostp, *dirp, *cp; 77c69a34d4SMatthew Dillon char str[STRSIZ]; 78afe1ef24SIan Dowse char *timep, *endp; 79c69a34d4SMatthew Dillon time_t time; 80afe1ef24SIan Dowse u_long ultmp; 81c69a34d4SMatthew Dillon FILE *mtabfile; 82c69a34d4SMatthew Dillon 83c69a34d4SMatthew Dillon if ((mtabfile = fopen(PATH_MOUNTTAB, "r")) == NULL) { 84c69a34d4SMatthew Dillon if (errno == ENOENT) 85c69a34d4SMatthew Dillon return (0); 86c69a34d4SMatthew Dillon else { 87c69a34d4SMatthew Dillon syslog(LOG_ERR, "can't open %s", PATH_MOUNTTAB); 88c69a34d4SMatthew Dillon return (0); 89c69a34d4SMatthew Dillon } 90c69a34d4SMatthew Dillon } 91c69a34d4SMatthew Dillon time = 0; 92c69a34d4SMatthew Dillon mtabpp = &mtabhead; 93c69a34d4SMatthew Dillon while (fgets(str, STRSIZ, mtabfile) != NULL) { 94c69a34d4SMatthew Dillon cp = str; 95c69a34d4SMatthew Dillon errno = 0; 96c69a34d4SMatthew Dillon if (*cp == '#' || *cp == ' ' || *cp == '\n') 97c69a34d4SMatthew Dillon continue; 98c69a34d4SMatthew Dillon timep = strsep(&cp, " \t\n"); 99afe1ef24SIan Dowse if (timep == NULL || *timep == '\0') { 100afe1ef24SIan Dowse badline("time", timep); 101c69a34d4SMatthew Dillon continue; 102c69a34d4SMatthew Dillon } 103c69a34d4SMatthew Dillon hostp = strsep(&cp, " \t\n"); 104afe1ef24SIan Dowse if (hostp == NULL || *hostp == '\0') { 105afe1ef24SIan Dowse badline("host", hostp); 106c69a34d4SMatthew Dillon continue; 107c69a34d4SMatthew Dillon } 108c69a34d4SMatthew Dillon dirp = strsep(&cp, " \t\n"); 109afe1ef24SIan Dowse if (dirp == NULL || *dirp == '\0') { 110afe1ef24SIan Dowse badline("dir", dirp); 111c69a34d4SMatthew Dillon continue; 112c69a34d4SMatthew Dillon } 113afe1ef24SIan Dowse ultmp = strtoul(timep, &endp, 10); 114afe1ef24SIan Dowse if (ultmp == ULONG_MAX || *endp != '\0') { 115afe1ef24SIan Dowse badline("time", timep); 116c69a34d4SMatthew Dillon continue; 117c69a34d4SMatthew Dillon } 118afe1ef24SIan Dowse time = ultmp; 119c69a34d4SMatthew Dillon if ((mtabp = malloc(sizeof (struct mtablist))) == NULL) { 120c69a34d4SMatthew Dillon syslog(LOG_ERR, "malloc"); 121c69a34d4SMatthew Dillon fclose(mtabfile); 122c69a34d4SMatthew Dillon return (0); 123c69a34d4SMatthew Dillon } 124c69a34d4SMatthew Dillon mtabp->mtab_time = time; 125c69a34d4SMatthew Dillon memmove(mtabp->mtab_host, hostp, RPCMNT_NAMELEN); 126c69a34d4SMatthew Dillon mtabp->mtab_host[RPCMNT_NAMELEN - 1] = '\0'; 127c69a34d4SMatthew Dillon memmove(mtabp->mtab_dirp, dirp, RPCMNT_PATHLEN); 128c69a34d4SMatthew Dillon mtabp->mtab_dirp[RPCMNT_PATHLEN - 1] = '\0'; 129c69a34d4SMatthew Dillon mtabp->mtab_next = (struct mtablist *)NULL; 130c69a34d4SMatthew Dillon *mtabpp = mtabp; 131c69a34d4SMatthew Dillon mtabpp = &mtabp->mtab_next; 132c69a34d4SMatthew Dillon } 133c69a34d4SMatthew Dillon fclose(mtabfile); 134c69a34d4SMatthew Dillon return (1); 135c69a34d4SMatthew Dillon } 136c69a34d4SMatthew Dillon 137c69a34d4SMatthew Dillon /* 138c69a34d4SMatthew Dillon * Rewrite PATH_MOUNTTAB from scratch and skip bad entries. 139c69a34d4SMatthew Dillon * Unlink PATH_MOUNTAB if no entry is left. 140c69a34d4SMatthew Dillon */ 141c69a34d4SMatthew Dillon int 142afe1ef24SIan Dowse write_mtab(int verbose) { 143afe1ef24SIan Dowse struct mtablist *mtabp, *mp; 144c69a34d4SMatthew Dillon FILE *mtabfile; 145c69a34d4SMatthew Dillon int line; 146c69a34d4SMatthew Dillon 147c69a34d4SMatthew Dillon if ((mtabfile = fopen(PATH_MOUNTTAB, "w")) == NULL) { 148c69a34d4SMatthew Dillon syslog(LOG_ERR, "can't write to %s", PATH_MOUNTTAB); 149c69a34d4SMatthew Dillon return (0); 150c69a34d4SMatthew Dillon } 151c69a34d4SMatthew Dillon line = 0; 152c69a34d4SMatthew Dillon for (mtabp = mtabhead; mtabp != NULL; mtabp = mtabp->mtab_next) { 153afe1ef24SIan Dowse if (mtabp->mtab_host[0] == '\0') 154afe1ef24SIan Dowse continue; 155afe1ef24SIan Dowse /* Skip if a later (hence more recent) entry is identical. */ 156afe1ef24SIan Dowse for (mp = mtabp->mtab_next; mp != NULL; mp = mp->mtab_next) 157afe1ef24SIan Dowse if (strcmp(mtabp->mtab_host, mp->mtab_host) == 0 && 158afe1ef24SIan Dowse strcmp(mtabp->mtab_dirp, mp->mtab_dirp) == 0) 159afe1ef24SIan Dowse break; 160afe1ef24SIan Dowse if (mp != NULL) 161afe1ef24SIan Dowse continue; 162afe1ef24SIan Dowse 16397f1ce75SBruce Evans fprintf(mtabfile, "%ld\t%s\t%s\n", 16497f1ce75SBruce Evans (long)mtabp->mtab_time, mtabp->mtab_host, 16597f1ce75SBruce Evans mtabp->mtab_dirp); 166afe1ef24SIan Dowse if (verbose) 167afe1ef24SIan Dowse warnx("write mounttab entry %s:%s", 168ab80d6faSBrian Feldman mtabp->mtab_host, mtabp->mtab_dirp); 169c69a34d4SMatthew Dillon line++; 170c69a34d4SMatthew Dillon } 171c69a34d4SMatthew Dillon fclose(mtabfile); 172c69a34d4SMatthew Dillon if (line == 0) { 173c69a34d4SMatthew Dillon if (unlink(PATH_MOUNTTAB) == -1) { 174c69a34d4SMatthew Dillon syslog(LOG_ERR, "can't remove %s", PATH_MOUNTTAB); 175c69a34d4SMatthew Dillon return (0); 176c69a34d4SMatthew Dillon } 177c69a34d4SMatthew Dillon } 178c69a34d4SMatthew Dillon return (1); 179c69a34d4SMatthew Dillon } 180c69a34d4SMatthew Dillon 181c69a34d4SMatthew Dillon /* 182c69a34d4SMatthew Dillon * Mark the entries as clean where RPC calls have been done successfully. 183c69a34d4SMatthew Dillon */ 184c69a34d4SMatthew Dillon void 185afe1ef24SIan Dowse clean_mtab(char *hostp, char *dirp, int verbose) { 186c69a34d4SMatthew Dillon struct mtablist *mtabp; 187c69a34d4SMatthew Dillon char *host; 188c69a34d4SMatthew Dillon 189afe1ef24SIan Dowse /* Copy hostp in case it points to an entry that we are zeroing out. */ 190c69a34d4SMatthew Dillon host = strdup(hostp); 191c69a34d4SMatthew Dillon for (mtabp = mtabhead; mtabp != NULL; mtabp = mtabp->mtab_next) { 192afe1ef24SIan Dowse if (strcmp(mtabp->mtab_host, hostp) != 0) 193afe1ef24SIan Dowse continue; 194afe1ef24SIan Dowse if (dirp != NULL && strcmp(mtabp->mtab_dirp, dirp) != 0) 195afe1ef24SIan Dowse continue; 196afe1ef24SIan Dowse 197afe1ef24SIan Dowse if (verbose) 198afe1ef24SIan Dowse warnx("delete mounttab entry%s %s:%s", 199afe1ef24SIan Dowse (dirp == NULL) ? " by host" : "", 200afe1ef24SIan Dowse mtabp->mtab_host, mtabp->mtab_dirp); 201c69a34d4SMatthew Dillon bzero(mtabp->mtab_host, RPCMNT_NAMELEN); 202c69a34d4SMatthew Dillon } 203c69a34d4SMatthew Dillon free(host); 204c69a34d4SMatthew Dillon } 205c69a34d4SMatthew Dillon 206c69a34d4SMatthew Dillon /* 207c69a34d4SMatthew Dillon * Free struct mtablist mtab. 208c69a34d4SMatthew Dillon */ 209c69a34d4SMatthew Dillon void 210c69a34d4SMatthew Dillon free_mtab() { 211c69a34d4SMatthew Dillon struct mtablist *mtabp; 212c69a34d4SMatthew Dillon 213afe1ef24SIan Dowse while ((mtabp = mtabhead) != NULL) { 214afe1ef24SIan Dowse mtabhead = mtabhead->mtab_next; 215c69a34d4SMatthew Dillon free(mtabp); 216c69a34d4SMatthew Dillon } 217c69a34d4SMatthew Dillon } 218c69a34d4SMatthew Dillon 219c69a34d4SMatthew Dillon /* 220c69a34d4SMatthew Dillon * Print bad lines to syslog. 221c69a34d4SMatthew Dillon */ 222afe1ef24SIan Dowse static void 223afe1ef24SIan Dowse badline(char *field, char *bad) { 224afe1ef24SIan Dowse syslog(LOG_ERR, "bad mounttab %s field '%s'", field, 225afe1ef24SIan Dowse (bad == NULL) ? "<null>" : bad); 226c69a34d4SMatthew Dillon } 227