xref: /freebsd/usr.sbin/rpc.umntall/mounttab.c (revision c69a34d486ea44d0c900900b00f460a2ab261030)
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>
40c69a34d4SMatthew Dillon #include <stdio.h>
41c69a34d4SMatthew Dillon #include <stdlib.h>
42c69a34d4SMatthew Dillon #include <string.h>
43c69a34d4SMatthew Dillon #include <unistd.h>
44c69a34d4SMatthew Dillon 
45c69a34d4SMatthew Dillon #include "mounttab.h"
46c69a34d4SMatthew Dillon 
47c69a34d4SMatthew Dillon int verbose;
48c69a34d4SMatthew Dillon struct mtablist *mtabhead;
49c69a34d4SMatthew Dillon 
50c69a34d4SMatthew Dillon /*
51c69a34d4SMatthew Dillon  * Add an entry to PATH_MOUNTTAB for each mounted NFS filesystem,
52c69a34d4SMatthew Dillon  * so the client can notify the NFS server even after reboot.
53c69a34d4SMatthew Dillon  */
54c69a34d4SMatthew Dillon int
55c69a34d4SMatthew Dillon add_mtab(char *hostp, char *dirp) {
56c69a34d4SMatthew Dillon 	FILE *mtabfile;
57c69a34d4SMatthew Dillon 	time_t *now;
58c69a34d4SMatthew Dillon 
59c69a34d4SMatthew Dillon 	now = NULL;
60c69a34d4SMatthew Dillon 	if ((mtabfile = fopen(PATH_MOUNTTAB, "a")) == NULL)
61c69a34d4SMatthew Dillon 		return (0);
62c69a34d4SMatthew Dillon 	else {
63c69a34d4SMatthew Dillon 		fprintf(mtabfile, "%ld\t%s\t%s\n", time(now), hostp, dirp);
64c69a34d4SMatthew Dillon 		fclose(mtabfile);
65c69a34d4SMatthew Dillon 		return (1);
66c69a34d4SMatthew Dillon 	}
67c69a34d4SMatthew Dillon }
68c69a34d4SMatthew Dillon 
69c69a34d4SMatthew Dillon /*
70c69a34d4SMatthew Dillon  * Read mounttab line for line and return struct mtablist.
71c69a34d4SMatthew Dillon  */
72c69a34d4SMatthew Dillon int
73c69a34d4SMatthew Dillon read_mtab(struct mtablist *mtabp) {
74c69a34d4SMatthew Dillon 	struct mtablist **mtabpp;
75c69a34d4SMatthew Dillon 	char *hostp, *dirp, *cp;
76c69a34d4SMatthew Dillon 	char str[STRSIZ];
77c69a34d4SMatthew Dillon 	char *timep;
78c69a34d4SMatthew Dillon 	time_t time;
79c69a34d4SMatthew Dillon 	FILE *mtabfile;
80c69a34d4SMatthew Dillon 
81c69a34d4SMatthew Dillon 	if ((mtabfile = fopen(PATH_MOUNTTAB, "r")) == NULL) {
82c69a34d4SMatthew Dillon 		if (errno == ENOENT)
83c69a34d4SMatthew Dillon 			return (0);
84c69a34d4SMatthew Dillon 		else {
85c69a34d4SMatthew Dillon 			syslog(LOG_ERR, "can't open %s", PATH_MOUNTTAB);
86c69a34d4SMatthew Dillon 			return (0);
87c69a34d4SMatthew Dillon 		}
88c69a34d4SMatthew Dillon 	}
89c69a34d4SMatthew Dillon 	time = 0;
90c69a34d4SMatthew Dillon 	mtabpp = &mtabhead;
91c69a34d4SMatthew Dillon 	while (fgets(str, STRSIZ, mtabfile) != NULL) {
92c69a34d4SMatthew Dillon 		cp = str;
93c69a34d4SMatthew Dillon 		errno = 0;
94c69a34d4SMatthew Dillon 		if (*cp == '#' || *cp == ' ' || *cp == '\n')
95c69a34d4SMatthew Dillon 			continue;
96c69a34d4SMatthew Dillon 		timep = strsep(&cp, " \t\n");
97c69a34d4SMatthew Dillon 		if (timep == NULL || *timep == ' ' || *timep == '\n') {
98c69a34d4SMatthew Dillon 			badline(timep);
99c69a34d4SMatthew Dillon 			continue;
100c69a34d4SMatthew Dillon 		}
101c69a34d4SMatthew Dillon 		hostp = strsep(&cp, " \t\n");
102c69a34d4SMatthew Dillon 		if (hostp == NULL || *hostp == ' ' || *hostp == '\n') {
103c69a34d4SMatthew Dillon 			badline(hostp);
104c69a34d4SMatthew Dillon 			continue;
105c69a34d4SMatthew Dillon 		}
106c69a34d4SMatthew Dillon 		dirp = strsep(&cp, " \t\n");
107c69a34d4SMatthew Dillon 		if (dirp == NULL || *dirp == ' ' || *dirp == '\n') {
108c69a34d4SMatthew Dillon 			badline(dirp);
109c69a34d4SMatthew Dillon 			continue;
110c69a34d4SMatthew Dillon 		}
111c69a34d4SMatthew Dillon 		time = strtoul(timep, (char **)NULL, 10);
112c69a34d4SMatthew Dillon 		if (errno == ERANGE) {
113c69a34d4SMatthew Dillon 			badline(timep);
114c69a34d4SMatthew Dillon 			continue;
115c69a34d4SMatthew Dillon 		}
116c69a34d4SMatthew Dillon 		if ((mtabp = malloc(sizeof (struct mtablist))) == NULL) {
117c69a34d4SMatthew Dillon 			syslog(LOG_ERR, "malloc");
118c69a34d4SMatthew Dillon 			fclose(mtabfile);
119c69a34d4SMatthew Dillon 			return (0);
120c69a34d4SMatthew Dillon 		}
121c69a34d4SMatthew Dillon 		mtabp->mtab_time = time;
122c69a34d4SMatthew Dillon 		memmove(mtabp->mtab_host, hostp, RPCMNT_NAMELEN);
123c69a34d4SMatthew Dillon 		mtabp->mtab_host[RPCMNT_NAMELEN - 1] = '\0';
124c69a34d4SMatthew Dillon 		memmove(mtabp->mtab_dirp, dirp, RPCMNT_PATHLEN);
125c69a34d4SMatthew Dillon 		mtabp->mtab_dirp[RPCMNT_PATHLEN - 1] = '\0';
126c69a34d4SMatthew Dillon 		mtabp->mtab_next = (struct mtablist *)NULL;
127c69a34d4SMatthew Dillon 		*mtabpp = mtabp;
128c69a34d4SMatthew Dillon 		mtabpp = &mtabp->mtab_next;
129c69a34d4SMatthew Dillon 	}
130c69a34d4SMatthew Dillon 	fclose(mtabfile);
131c69a34d4SMatthew Dillon 	return (1);
132c69a34d4SMatthew Dillon }
133c69a34d4SMatthew Dillon 
134c69a34d4SMatthew Dillon /*
135c69a34d4SMatthew Dillon  * Rewrite PATH_MOUNTTAB from scratch and skip bad entries.
136c69a34d4SMatthew Dillon  * Unlink PATH_MOUNTAB if no entry is left.
137c69a34d4SMatthew Dillon  */
138c69a34d4SMatthew Dillon int
139c69a34d4SMatthew Dillon write_mtab() {
140c69a34d4SMatthew Dillon 	struct mtablist *mtabp;
141c69a34d4SMatthew Dillon 	FILE *mtabfile;
142c69a34d4SMatthew Dillon 	int line;
143c69a34d4SMatthew Dillon 
144c69a34d4SMatthew Dillon 	if ((mtabfile = fopen(PATH_MOUNTTAB, "w")) == NULL) {
145c69a34d4SMatthew Dillon 		syslog(LOG_ERR, "can't write to %s", PATH_MOUNTTAB);
146c69a34d4SMatthew Dillon 			return (0);
147c69a34d4SMatthew Dillon 	}
148c69a34d4SMatthew Dillon 	line = 0;
149c69a34d4SMatthew Dillon 	for (mtabp = mtabhead; mtabp != NULL; mtabp = mtabp->mtab_next) {
150c69a34d4SMatthew Dillon 		if (mtabp->mtab_host != NULL &&
151c69a34d4SMatthew Dillon 		    strlen(mtabp->mtab_host) > 0) {
152c69a34d4SMatthew Dillon 			fprintf(mtabfile, "%ld\t%s\t%s\n", mtabp->mtab_time,
153c69a34d4SMatthew Dillon 			    mtabp->mtab_host, mtabp->mtab_dirp);
154c69a34d4SMatthew Dillon 			line++;
155c69a34d4SMatthew Dillon 		}
156c69a34d4SMatthew Dillon 	}
157c69a34d4SMatthew Dillon 	fclose(mtabfile);
158c69a34d4SMatthew Dillon 	if (line == 0) {
159c69a34d4SMatthew Dillon 		if (unlink(PATH_MOUNTTAB) == -1) {
160c69a34d4SMatthew Dillon 			syslog(LOG_ERR, "can't remove %s", PATH_MOUNTTAB);
161c69a34d4SMatthew Dillon 			return (0);
162c69a34d4SMatthew Dillon 		}
163c69a34d4SMatthew Dillon 	}
164c69a34d4SMatthew Dillon 	return (1);
165c69a34d4SMatthew Dillon }
166c69a34d4SMatthew Dillon 
167c69a34d4SMatthew Dillon /*
168c69a34d4SMatthew Dillon  * Mark the entries as clean where RPC calls have been done successfully.
169c69a34d4SMatthew Dillon  */
170c69a34d4SMatthew Dillon void
171c69a34d4SMatthew Dillon clean_mtab(char *hostp, char *dirp) {
172c69a34d4SMatthew Dillon 	struct mtablist *mtabp;
173c69a34d4SMatthew Dillon 	char *host;
174c69a34d4SMatthew Dillon 
175c69a34d4SMatthew Dillon 	host = strdup(hostp);
176c69a34d4SMatthew Dillon 	for (mtabp = mtabhead; mtabp != NULL; mtabp = mtabp->mtab_next) {
177c69a34d4SMatthew Dillon 		if (mtabp->mtab_host != NULL &&
178c69a34d4SMatthew Dillon 		    strcmp(mtabp->mtab_host, host) == 0) {
179c69a34d4SMatthew Dillon 			if (dirp == NULL) {
180c69a34d4SMatthew Dillon 				if (verbose) {
181c69a34d4SMatthew Dillon 					warnx("entries deleted for "
182c69a34d4SMatthew Dillon 					    "host %s", host);
183c69a34d4SMatthew Dillon 				}
184c69a34d4SMatthew Dillon 				bzero(mtabp->mtab_host, RPCMNT_NAMELEN);
185c69a34d4SMatthew Dillon 			} else {
186c69a34d4SMatthew Dillon 				if (strcmp(mtabp->mtab_dirp, dirp) == 0) {
187c69a34d4SMatthew Dillon 					if (verbose) {
188c69a34d4SMatthew Dillon 						warnx("entry deleted for "
189c69a34d4SMatthew Dillon 						    "%s:%s", host, dirp);
190c69a34d4SMatthew Dillon 					}
191c69a34d4SMatthew Dillon 					bzero(mtabp->mtab_host, RPCMNT_NAMELEN);
192c69a34d4SMatthew Dillon 				}
193c69a34d4SMatthew Dillon 			}
194c69a34d4SMatthew Dillon 		}
195c69a34d4SMatthew Dillon 	}
196c69a34d4SMatthew Dillon 	free(host);
197c69a34d4SMatthew Dillon }
198c69a34d4SMatthew Dillon 
199c69a34d4SMatthew Dillon /*
200c69a34d4SMatthew Dillon  * Free struct mtablist mtab.
201c69a34d4SMatthew Dillon  */
202c69a34d4SMatthew Dillon void
203c69a34d4SMatthew Dillon free_mtab() {
204c69a34d4SMatthew Dillon 	struct mtablist *mtabp;
205c69a34d4SMatthew Dillon 	struct mtablist *mtab_next;
206c69a34d4SMatthew Dillon 
207c69a34d4SMatthew Dillon 	for (mtabp = mtabhead; mtabp != NULL; mtabp = mtab_next) {
208c69a34d4SMatthew Dillon 		mtab_next = mtabp->mtab_next;
209c69a34d4SMatthew Dillon 		free(mtabp);
210c69a34d4SMatthew Dillon 		mtabp = mtab_next;
211c69a34d4SMatthew Dillon 	}
212c69a34d4SMatthew Dillon }
213c69a34d4SMatthew Dillon 
214c69a34d4SMatthew Dillon /*
215c69a34d4SMatthew Dillon  * Print bad lines to syslog.
216c69a34d4SMatthew Dillon  */
217c69a34d4SMatthew Dillon void
218c69a34d4SMatthew Dillon badline(char *bad) {
219c69a34d4SMatthew Dillon 
220c69a34d4SMatthew Dillon 	syslog(LOG_ERR, "skipped bad line in mounttab with entry %s", bad);
221c69a34d4SMatthew Dillon }
222