summaryrefslogtreecommitdiff
path: root/support/nfs/rmtab.c
blob: 154b26fa340248eb9b1fa8ff1e89f917cf9c489f (plain)
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
/*
 * support/nfs/rmtab.c
 *
 * Handling for rmtab.
 *
 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include "nfslib.h"

/*
 * Colons in incoming IPv6 presentation addresses have to
 * replaced with another character, since rmtab already
 * uses colons to delineate fields.
 *
 * Use a printable character, but one that would never be
 * found in a presentation address or domain name
 */
#define IPV6_COLON	';'

#define LINELEN		(2048)

static FILE	*rmfp = NULL;

struct state_paths rmtab;

int
setrmtabent(char *type)
{
	if (rmfp)
		fclose(rmfp);
	rmfp = fsetrmtabent(rmtab.statefn, type);
	return (rmfp != NULL);
}

FILE *
fsetrmtabent(char *fname, char *type)
{
	int	readonly = !strcmp(type, "r");
	FILE	*fp;

	if (!fname)
		return NULL;
	if ((fp = fopen(fname, type)) == NULL) {
		xlog(L_ERROR, "can't open %s for %sing", fname,
				readonly ? "read" : "writ");
		return NULL;
	}
	return fp;
}

struct rmtabent *
getrmtabent(int log, long *pos)
{
	return fgetrmtabent(rmfp, log, pos);
}

struct rmtabent *
fgetrmtabent(FILE *fp, int log, long *pos)
{
	static struct rmtabent	re;
	char		*count, *host, *path, *c;
	static char	buf[LINELEN];

	errno = 0;
	if (!fp)
		return NULL;
	do {
		if (pos)
			*pos = ftell (fp);
		if (fgets(buf, sizeof(buf)-1, fp) == NULL)
			return NULL;
		host = buf;
		if ((path = strchr(host, '\n')) != NULL)
			*path = '\0';
		if (!(path = strchr(host, ':'))) {
			if (log)
				xlog(L_ERROR, "malformed entry in rmtab file");
			errno = EINVAL;
			return NULL;
		}
		*path++ = '\0';
		count = strchr(path, ':');
		if (count) {
			*count++ = '\0';
			re.r_count = strtol (count, NULL, 0);
		}
		else
			re.r_count = 1;
	} while (0);

	strncpy(re.r_client, host, sizeof (re.r_client) - 1);
	re.r_client[sizeof (re.r_client) - 1] = '\0';
	for (c = re.r_client; *c != '\0'; c++)
		if (*c == IPV6_COLON)
			*c = ':';

	strncpy(re.r_path, path, sizeof (re.r_path) - 1);
	re.r_path[sizeof (re.r_path) - 1] = '\0';

	return &re;
}

void
putrmtabent(struct rmtabent *rep, long *pos)
{
	fputrmtabent(rmfp, rep, pos);
}

void
fputrmtabent(FILE *fp, struct rmtabent *rep, long *pos)
{
	static char	buf[LINELEN];
	char		*c;

	if (!fp || (pos && fseek (fp, *pos, SEEK_SET) != 0))
		return;

	/*
	 * To avoid confusing the token parser in fgetrmtabent(),
	 * convert colons in incoming IPv6 presentation addresses
	 * to semicolons.
	 */
	if (strlen(rep->r_client) > sizeof(buf)) {
		xlog(L_ERROR, "client name too large");
		return;
	}
	strncpy(buf, rep->r_client, sizeof(buf));
	for (c = buf; *c != '\0'; c++)
		if (*c == ':')
			*c = IPV6_COLON;

	(void)fprintf(fp, "%s:%s:0x%.8x\n", buf, rep->r_path, rep->r_count);
}

void
endrmtabent(void)
{
	fendrmtabent(rmfp);
	rmfp = NULL;
}

void
fendrmtabent(FILE *fp)
{
	if (fp)
		fclose(fp);
}

void
rewindrmtabent(void)
{
	if (rmfp)
		rewind(rmfp);
}

void
frewindrmtabent(FILE *fp)
{
	if (fp)
		rewind (fp);
}