File: rmtab.c

package info (click to toggle)
nfs-server 2.2beta37-1slink.1
  • links: PTS
  • area: main
  • in suites: slink
  • size: 916 kB
  • ctags: 1,355
  • sloc: ansic: 11,214; sh: 567; makefile: 238
file content (327 lines) | stat: -rw-r--r-- 6,566 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
 * A set of support routines for /etc/rmtab file managment. These routines
 * are called from mountd.c.
 *
 * Written and Copyright by Dariush Shirazi, <dshirazi@uhl.uiowa.edu>
 *
 */

#include "nfsd.h"
#include "rmtab.h"

static char *	rmtab_gethost(struct svc_req *);
static int	rmtab_insert(char *, char *);
static void	rmtab_file(char);

/*
 * global top to linklist
 */

static mountlist rmtablist = NULL;

/*
 * rmtab_add_client -- if client+path not in the list, add them.
 */

void
rmtab_add_client(dirpath path, struct svc_req *rqstp)
{
	char		*hostname;

	hostname = rmtab_gethost(rqstp);
	if (hostname != NULL) {
		rmtab_file('r');
		if (rmtab_insert(hostname, path))
			rmtab_file('w');
	}
}

/*
 * rmtab_lst_client -- return the top pointer.
 */

mountlist *
rmtab_lst_client(void)
{
	rmtab_file('r');
	return(&rmtablist);
}

/*
 * rmtab_del_client -- delete a client+path
 */

void
rmtab_del_client(dirpath path, struct svc_req *rqstp)
{
	int		p0, p1, changed;
	char		*hostname;
	mountlist	cur, prv;

	hostname = rmtab_gethost(rqstp);
	Dprintf(D_RMTAB, "\trmtab_del path='%s' host='%s'\n", path, hostname);
	if (hostname == NULL)
		return;

	rmtab_file('r');
	changed = 0;

	for (cur = rmtablist, prv = NULL; cur; cur = cur->ml_next) {
		p0 = strcmp(cur->ml_hostname, hostname);
		p1 = strcmp(cur->ml_directory, path);
		if (p0 == 0 && p1 == 0)
			break;				/* already exists */
		prv = cur;
	}

	if (cur) {
		/*
		 * don't free both ml_hostname & ml_directory.
		 * See rmtab_insert for details.
		 */
		free(cur->ml_hostname);
		if (prv)
			prv->ml_next = cur->ml_next;
		else
			rmtablist    = cur->ml_next;
		free(cur);

		changed = 1;
	}

	if (changed)
		rmtab_file('w');
}

/*
 * rmtab_mdel_client -- delete all the entry points for a client
 */

void
rmtab_mdel_client(struct svc_req *rqstp)
{
	int		p0, changed;
	char		*hostname;
	mountlist	cur, prv, tmp;

	hostname = rmtab_gethost(rqstp);
	Dprintf(D_RMTAB, "\trmtab_mdel host='%s'\n", hostname);
	if (hostname == NULL)
		return;

	rmtab_file('r');
	changed = 0;

	prv     = NULL;
	cur     = rmtablist;
	while (cur) {
		p0 = strcmp(cur->ml_hostname, hostname);
		if (p0 == 0) {
			/*
			 * don't free both ml_hostname & ml_directory.
			 * See rmtab_insert for details.
			 */
			tmp = cur;
			cur = cur->ml_next;
			if (prv)
				prv->ml_next = cur;
			else
				rmtablist    = cur;
			free(tmp->ml_hostname);
			free(tmp);

			changed = 1;
		} else if (p0 < 0) {
			prv = cur;
			cur = cur->ml_next;
		} else
			break;				/* not found */
	}

	if (changed)
		rmtab_file('w');
}

/*
 * rmtab_gethost -- return the hostname
 */

static char *
rmtab_gethost(struct svc_req *rqstp)
{
	struct hostent *hp;
        struct in_addr addr;

	addr = svc_getcaller(rqstp->rq_xprt)->sin_addr;
	hp   = gethostbyaddr((char *) &addr, sizeof(addr), AF_INET);

	if (hp)
		return((char *) hp->h_name);

	return((char *) NULL);
}

/*
 * rmtab_insert -- a sorted link list
 */

static int
rmtab_insert(char *hostname, char *path)
{
	int		hostlen, p0, p1;
	mountlist	cur, prv;

	Dprintf(D_RMTAB, "\trmtab_insert path='%s' host='%s'\n",
				path, hostname);

	for (cur = rmtablist, prv = NULL; cur; cur = cur->ml_next) {
		p0 = strcmp(cur->ml_hostname, hostname);
		p1 = strcmp(cur->ml_directory, path);
		if (p0 > 0 || (p0 == 0 && p1 > 0))
			break;				/* insert here */
		else if (p0 == 0 && p1 == 0)
			return(0);			/* already exists */
		prv = cur;
	}

	if ((cur = (mountlist) malloc(sizeof(mountbody))) == NULL) {
		Dprintf(L_ERROR, "failed to allocate memory for mountbody\n");
		return(0);
	}
	/*
	 * since the data we are storing is really small (ie. h.x.y.z:/cur),
	 * allocate one memory unit for both and split it.
	 */
	hostlen = strlen(hostname);
	if ((cur->ml_hostname = (char *) malloc(hostlen+strlen(path)+2)) == NULL) {
		Dprintf(L_ERROR, "failed to allocate memory for mountlist buffer\n");
		free(cur);
		return(0);
	}
	cur->ml_directory = cur->ml_hostname + (hostlen + 1);

	strcpy(cur->ml_hostname, hostname);
	strcpy(cur->ml_directory, path);

	if (prv) {
		cur->ml_next = prv->ml_next;
		prv->ml_next = cur;
	} else {
		cur->ml_next = rmtablist;
		rmtablist    = cur;
	}
	return(1);
}

/*
 * rmtab_file -- read/write the mount list from/to rmtab file.
 */

static void
rmtab_file(char op)
{
	register int	c, len;
	register char	*p;
	char		buff[256], *host, *path;
	FILE		*fp;
	mountlist	cur;
	struct stat	newstat;

	static time_t 	old_st_mtime = 0;

	if (op == 'r') {				/* read&update llist */

		/*
		 * get a new stat; if file not there, create it
		 */
		if (stat(_PATH_RMTAB, &newstat)) {
			int	zappa;

			if ((zappa = creat(_PATH_RMTAB, 0644)) < 0) {
				Dprintf(L_ERROR, "failed to create '%s'\n",
								_PATH_RMTAB);
				umask(0);
				return;
			}
			close(zappa);
			umask(0);

			if (stat(_PATH_RMTAB, &newstat)) {
				Dprintf(L_ERROR, "failed to stat '%s'\n",
								_PATH_RMTAB);
				return;
			}
			old_st_mtime = newstat.st_mtime;
			return;
		}

		if (old_st_mtime == newstat.st_mtime)
			return;				/* no change */

		if ((fp = fopen(_PATH_RMTAB, "r")) == NULL) {
			Dprintf(L_ERROR, "failed to open '%s'\n", _PATH_RMTAB);
			return;
		}

		while (rmtablist) {			/* free the old list */
			cur       = rmtablist;
			rmtablist = rmtablist->ml_next;
			/*
			 * don't free both ml_hostname & ml_directory.
			 * See rmtab_insert for details.
			 */
			free(cur->ml_hostname);
			free(cur);
		}

		while (! feof(fp)) {
			/*
			 * the reason this looks worse than it should is so
			 * we don't have to do bunch of passes on the buff.
			 * (fgets,strlen,strchr...)
			 */
			p    = buff;
			host = buff;
			path = NULL;
			len  = c = 0;
			while (!feof(fp) && (c = fgetc(fp))!='\n' && len<255) {
				if (c == ':') {
					c    = '\0';
					path = p+1;
				}
				*p++ = (char) c;
				len++;
			}
			*p = '\0';

			while (!feof(fp) && c != '\n')	/* skip if line > 255 */
				c = fgetc(fp);

			if (path)			/* skip bad input */
				if (*host && *path)
					rmtab_insert(host, path);
		}
		fclose(fp);
		old_st_mtime = newstat.st_mtime;

	} else if (op == 'w') {				/* write from llist */

		if ((fp = fopen(_PATH_RMTAB, "w")) == NULL) {
			Dprintf(L_ERROR, "failed to open '%s'\n", _PATH_RMTAB);
			return;
		}
		for (cur = rmtablist; cur; cur = cur->ml_next)
			fprintf(fp, "%s:%s\n", cur->ml_hostname,
					       cur->ml_directory);
		fclose(fp);

		if (stat(_PATH_RMTAB, &newstat)) {
			Dprintf(L_ERROR, "failed to stat '%s'\n", _PATH_RMTAB);
			fclose(fp);
			return;
		}
		old_st_mtime = newstat.st_mtime;

	} else
		Dprintf(L_ERROR, "rmtab_file bad flag '%c'\n", op);
}