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