xref: /freebsd/usr.sbin/rpc.umntall/mounttab.c (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
11de7b4b8SPedro F. Giffuni /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni  *
4c69a34d4SMatthew Dillon  * Copyright (c) 1999 Martin Blapp
5c69a34d4SMatthew Dillon  * All rights reserved.
6c69a34d4SMatthew Dillon  *
7c69a34d4SMatthew Dillon  * Redistribution and use in source and binary forms, with or without
8c69a34d4SMatthew Dillon  * modification, are permitted provided that the following conditions
9c69a34d4SMatthew Dillon  * are met:
10c69a34d4SMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
11c69a34d4SMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
12c69a34d4SMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
13c69a34d4SMatthew Dillon  *    notice, this list of conditions and the following disclaimer in the
14c69a34d4SMatthew Dillon  *    documentation and/or other materials provided with the distribution.
15c69a34d4SMatthew Dillon  *
16c69a34d4SMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17c69a34d4SMatthew Dillon  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18c69a34d4SMatthew Dillon  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19c69a34d4SMatthew Dillon  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20c69a34d4SMatthew Dillon  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21c69a34d4SMatthew Dillon  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22c69a34d4SMatthew Dillon  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23c69a34d4SMatthew Dillon  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24c69a34d4SMatthew Dillon  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25c69a34d4SMatthew Dillon  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26c69a34d4SMatthew Dillon  * SUCH DAMAGE.
27c69a34d4SMatthew Dillon  *
28c69a34d4SMatthew Dillon  */
29c69a34d4SMatthew Dillon 
30b728350eSDavid E. O'Brien #include <sys/cdefs.h>
31c69a34d4SMatthew Dillon #include <sys/syslog.h>
32c69a34d4SMatthew Dillon 
33c69a34d4SMatthew Dillon #include <rpc/rpc.h>
340775314bSDoug Rabson #include <rpcsvc/mount.h>
35c69a34d4SMatthew Dillon 
36c69a34d4SMatthew Dillon #include <err.h>
37c69a34d4SMatthew Dillon #include <errno.h>
38afe1ef24SIan Dowse #include <limits.h>
39c69a34d4SMatthew Dillon #include <stdio.h>
40c69a34d4SMatthew Dillon #include <stdlib.h>
41c69a34d4SMatthew Dillon #include <string.h>
42c69a34d4SMatthew Dillon #include <unistd.h>
43c69a34d4SMatthew Dillon 
44c69a34d4SMatthew Dillon #include "mounttab.h"
45c69a34d4SMatthew Dillon 
46c69a34d4SMatthew Dillon struct mtablist *mtabhead;
47c69a34d4SMatthew Dillon 
487f471a32SXin LI static void badline(const char *field, const char *bad);
49afe1ef24SIan Dowse 
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
add_mtab(char * hostp,char * dirp)55ca25fa25SEd Schouten add_mtab(char *hostp, char *dirp)
56ca25fa25SEd Schouten {
57c69a34d4SMatthew Dillon 	FILE *mtabfile;
58c69a34d4SMatthew Dillon 
59c69a34d4SMatthew Dillon 	if ((mtabfile = fopen(PATH_MOUNTTAB, "a")) == NULL)
60c69a34d4SMatthew Dillon 		return (0);
61c69a34d4SMatthew Dillon 	else {
6297f1ce75SBruce Evans 		fprintf(mtabfile, "%ld\t%s\t%s\n",
63afe1ef24SIan Dowse 		    (long)time(NULL), 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
read_mtab(void)73ca25fa25SEd Schouten read_mtab(void)
74ca25fa25SEd Schouten {
75afe1ef24SIan Dowse 	struct mtablist **mtabpp, *mtabp;
76c69a34d4SMatthew Dillon 	char *hostp, *dirp, *cp;
77c69a34d4SMatthew Dillon 	char str[STRSIZ];
78afe1ef24SIan Dowse 	char *timep, *endp;
797f471a32SXin LI 	time_t actiontime;
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 	}
917f471a32SXin LI 	actiontime = 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 		}
1187f471a32SXin LI 		actiontime = 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 		}
1247f471a32SXin LI 		mtabp->mtab_time = actiontime;
1250775314bSDoug Rabson 		memmove(mtabp->mtab_host, hostp, MNTNAMLEN);
1260775314bSDoug Rabson 		mtabp->mtab_host[MNTNAMLEN - 1] = '\0';
1270775314bSDoug Rabson 		memmove(mtabp->mtab_dirp, dirp, MNTPATHLEN);
1280775314bSDoug Rabson 		mtabp->mtab_dirp[MNTPATHLEN - 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
write_mtab(int verbose)142ca25fa25SEd Schouten write_mtab(int verbose)
143ca25fa25SEd Schouten {
144afe1ef24SIan Dowse 	struct mtablist *mtabp, *mp;
145c69a34d4SMatthew Dillon 	FILE *mtabfile;
146c69a34d4SMatthew Dillon 	int line;
147c69a34d4SMatthew Dillon 
148c69a34d4SMatthew Dillon 	if ((mtabfile = fopen(PATH_MOUNTTAB, "w")) == NULL) {
149c69a34d4SMatthew Dillon 		syslog(LOG_ERR, "can't write to %s", PATH_MOUNTTAB);
150c69a34d4SMatthew Dillon 			return (0);
151c69a34d4SMatthew Dillon 	}
152c69a34d4SMatthew Dillon 	line = 0;
153c69a34d4SMatthew Dillon 	for (mtabp = mtabhead; mtabp != NULL; mtabp = mtabp->mtab_next) {
154afe1ef24SIan Dowse 		if (mtabp->mtab_host[0] == '\0')
155afe1ef24SIan Dowse 			continue;
156afe1ef24SIan Dowse 		/* Skip if a later (hence more recent) entry is identical. */
157afe1ef24SIan Dowse 		for (mp = mtabp->mtab_next; mp != NULL; mp = mp->mtab_next)
158afe1ef24SIan Dowse 			if (strcmp(mtabp->mtab_host, mp->mtab_host) == 0 &&
159afe1ef24SIan Dowse 			    strcmp(mtabp->mtab_dirp, mp->mtab_dirp) == 0)
160afe1ef24SIan Dowse 				break;
161afe1ef24SIan Dowse 		if (mp != NULL)
162afe1ef24SIan Dowse 			continue;
163afe1ef24SIan Dowse 
16497f1ce75SBruce Evans 		fprintf(mtabfile, "%ld\t%s\t%s\n",
16597f1ce75SBruce Evans 		    (long)mtabp->mtab_time, mtabp->mtab_host,
16697f1ce75SBruce Evans 		    mtabp->mtab_dirp);
167afe1ef24SIan Dowse 		if (verbose)
168afe1ef24SIan Dowse 			warnx("write mounttab entry %s:%s",
169ab80d6faSBrian Feldman 			    mtabp->mtab_host, mtabp->mtab_dirp);
170c69a34d4SMatthew Dillon 		line++;
171c69a34d4SMatthew Dillon 	}
172c69a34d4SMatthew Dillon 	fclose(mtabfile);
173c69a34d4SMatthew Dillon 	if (line == 0) {
174c69a34d4SMatthew Dillon 		if (unlink(PATH_MOUNTTAB) == -1) {
175c69a34d4SMatthew Dillon 			syslog(LOG_ERR, "can't remove %s", PATH_MOUNTTAB);
176c69a34d4SMatthew Dillon 			return (0);
177c69a34d4SMatthew Dillon 		}
178c69a34d4SMatthew Dillon 	}
179c69a34d4SMatthew Dillon 	return (1);
180c69a34d4SMatthew Dillon }
181c69a34d4SMatthew Dillon 
182c69a34d4SMatthew Dillon /*
183c69a34d4SMatthew Dillon  * Mark the entries as clean where RPC calls have been done successfully.
184c69a34d4SMatthew Dillon  */
185c69a34d4SMatthew Dillon void
clean_mtab(char * hostp,char * dirp,int verbose)186ca25fa25SEd Schouten clean_mtab(char *hostp, char *dirp, int verbose)
187ca25fa25SEd Schouten {
188c69a34d4SMatthew Dillon 	struct mtablist *mtabp;
189c69a34d4SMatthew Dillon 	char *host;
190c69a34d4SMatthew Dillon 
191afe1ef24SIan Dowse 	/* Copy hostp in case it points to an entry that we are zeroing out. */
192c69a34d4SMatthew Dillon 	host = strdup(hostp);
193c69a34d4SMatthew Dillon 	for (mtabp = mtabhead; mtabp != NULL; mtabp = mtabp->mtab_next) {
194146e669bSIan Dowse 		if (strcmp(mtabp->mtab_host, host) != 0)
195afe1ef24SIan Dowse 			continue;
196afe1ef24SIan Dowse 		if (dirp != NULL && strcmp(mtabp->mtab_dirp, dirp) != 0)
197afe1ef24SIan Dowse 			continue;
198afe1ef24SIan Dowse 
199afe1ef24SIan Dowse 		if (verbose)
200afe1ef24SIan Dowse 			warnx("delete mounttab entry%s %s:%s",
201afe1ef24SIan Dowse 			    (dirp == NULL) ? " by host" : "",
202afe1ef24SIan Dowse 			    mtabp->mtab_host, mtabp->mtab_dirp);
2030775314bSDoug Rabson 		bzero(mtabp->mtab_host, MNTNAMLEN);
204c69a34d4SMatthew Dillon 	}
205c69a34d4SMatthew Dillon 	free(host);
206c69a34d4SMatthew Dillon }
207c69a34d4SMatthew Dillon 
208c69a34d4SMatthew Dillon /*
209c69a34d4SMatthew Dillon  * Free struct mtablist mtab.
210c69a34d4SMatthew Dillon  */
211c69a34d4SMatthew Dillon void
free_mtab(void)212ca25fa25SEd Schouten free_mtab(void)
213ca25fa25SEd Schouten {
214c69a34d4SMatthew Dillon 	struct mtablist *mtabp;
215c69a34d4SMatthew Dillon 
216afe1ef24SIan Dowse 	while ((mtabp = mtabhead) != NULL) {
217afe1ef24SIan Dowse 		mtabhead = mtabhead->mtab_next;
218c69a34d4SMatthew Dillon 		free(mtabp);
219c69a34d4SMatthew Dillon 	}
220c69a34d4SMatthew Dillon }
221c69a34d4SMatthew Dillon 
222c69a34d4SMatthew Dillon /*
223c69a34d4SMatthew Dillon  * Print bad lines to syslog.
224c69a34d4SMatthew Dillon  */
225afe1ef24SIan Dowse static void
badline(const char * field,const char * bad)226ca25fa25SEd Schouten badline(const char *field, const char *bad)
227ca25fa25SEd Schouten {
228afe1ef24SIan Dowse 	syslog(LOG_ERR, "bad mounttab %s field '%s'", field,
229afe1ef24SIan Dowse 	    (bad == NULL) ? "<null>" : bad);
230c69a34d4SMatthew Dillon }
231