aboutsummaryrefslogtreecommitdiff
path: root/src/config.c
blob: d382c95743126e82b95faa8e35c21ccad803960f (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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/* This file is part of Ping903
   Copyright (C) 2020 Sergey Poznyakoff
  
   Ping903 is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.
  
   Ping903 is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
  
   You should have received a copy of the GNU General Public License
   along with Ping903.  If not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include "ping903.h"
#include "json.h"
#include "defs.h"

static int
get_num(char const *str, unsigned long *retval,
	char const *file, unsigned line)
{
	unsigned long n;
	char *p;
	errno = 0;
	n = strtoul(str, &p, 10);
	if (errno || *p) {
		error("%s:%d: invalid numeric value", file, line);
		return -1;
	}
	*retval = n;
	return 0;
}

static int
cf_ip_list(int mode, union cf_callback_arg *arg, void *data)
{
	FILE *fp;
	char buf[1024];
	unsigned ln = 0;
	int skip_to_eol = 0;
	int ret = CF_RET_OK;
	struct addrinfo hints;

	if (mode != CF_PARSE)
		return CF_RET_IGNORE;
	
	fp = fopen(arg->input.val, "r");
	if (!fp) {
		error("%s:%d: can't open file %s: %s",
		      arg->input.file, arg->input.line,
		      arg->input.val, strerror(errno));
		exit(1);
	}
	
	memset(&hints, 0, sizeof(hints));
	hints.ai_family = AF_INET;
	hints.ai_protocol = IPPROTO_TCP;

	while (fgets(buf, sizeof(buf), fp)) {
		int len;
		char *p;
		struct addrinfo *res;
		int rc;
		
		ln++;
		len = strlen(buf);
		if (len == 0)
			continue;

		if (skip_to_eol) {
			skip_to_eol = buf[len-1] != '\n';
			continue;
		}
		
		if (buf[len-1] == '\n')
			buf[--len] = 0;
		else if (!feof(fp)) {
			error("%s:%d: line too long", arg->input.val, ln);
			skip_to_eol = 1;
			ret = CF_RET_FAIL;
			continue;
		}

		while (len > 0 && isspace(buf[len-1]))
			buf[--len] = 0;

		if (len == 0)
			continue;
		for (p = buf; (*p == ' ' || *p == '\t'); p++)
			;
		if (*p == 0 || *p == '#')
			continue;

		rc = getaddrinfo(p, NULL, &hints, &res);
		if (rc) {
			error("%s:%d: invalid IP address", arg->input.val, ln);
			ret = CF_RET_FAIL;
			continue;
		}
		if (hostaddr_count == hostaddr_max) {
			hostaddr = e2nrealloc(hostaddr,
					      &hostaddr_max,
					      sizeof(hostaddr[0]));
		}
		memset(&hostaddr[hostaddr_count], 0, sizeof(hostaddr[0]));
		hostaddr[hostaddr_count].name = estrdup(p);
		hostaddr[hostaddr_count].addr = emalloc(res->ai_addrlen);
		memcpy(hostaddr[hostaddr_count].addr, res->ai_addr, res->ai_addrlen);
		hostaddr[hostaddr_count].addrlen = res->ai_addrlen;
		hostaddr_count++;
		freeaddrinfo(res);
	}
	fclose(fp);
	return ret;
}

struct cf_stmt {
	char const *kw;
	int type;
	void *data;
	CF_CALLBACK callback;
};

struct cf_stmt statements[] = {
	{ "listen",             STMT_T_STRING,    &httpd_addr },
	{ "pidfile",            STMT_T_STRING,    &pidfile },
	{ "trusted-ip",         STMT_T_CALLBACK,  NULL, cf_trusted_ip },
	{ "probe-interval",     STMT_T_ULONG,     &probe_interval },
	{ "ping-interval",      STMT_T_ULONG,     &ping_interval },
	{ "ping-count",         STMT_T_ULONG,     &ping_count },
	{ "tolerance",          STMT_T_ULONG,     &ping_tolerance },
	{ "ip-list",            STMT_T_CALLBACK,  NULL, cf_ip_list },
	{ "data-length",        STMT_T_ULONG,     &data_length },
	{ "syslog-facility",    STMT_T_CALLBACK,  NULL, cf_syslog_facility },
	{ "access-log",         STMT_T_BOOL,      &httpd_access_log },
	{ "access-log-verbose", STMT_T_BOOL,      &httpd_log_verbose },
	{ NULL }
};

static struct cf_stmt *
find_stmt(char const *kw)
{
	struct cf_stmt *cf;
	for (cf = statements; cf->kw; cf++) {
		if (strcmp(cf->kw, kw) == 0)
			return cf;
	}
	return NULL;
}

static int
stmt_parse(char const *kw, char const *val, char const *file, unsigned line)
{
	struct cf_stmt *cf = find_stmt(kw);
	union cf_callback_arg arg;
	
	if (!cf) {
		error("%s:%d: unrecognized keyword", file, line);
		return -1;
	}

	switch (cf->type) {
	case STMT_T_STRING:
		*(char**)cf->data = estrdup(val);
		break;
		
	case STMT_T_ULONG:
		return get_num(val, cf->data, file, line);
			
	case STMT_T_BOOL: {
		static char *t_val[] = { "1", "t", "true", "yes", "on", NULL };
		static char *f_val[] = { "0", "f", "nil", "false", "no", "off", NULL };
		int i;
		for (i = 0; t_val[i]; i++) {
			if (strcasecmp(t_val[i], val) == 0) {
				*(int*)cf->data = 1;
				return 0;
			}
		}
		for (i = 0; f_val[i]; i++) {
			if (strcasecmp(f_val[i], val) == 0) {
				*(int*)cf->data = 0;
				return 0;
			}
		}
		error("%s:%d: not a valid boolean value", file, line);
		return -1;
	}
		
	case STMT_T_CALLBACK:
		arg.input.val = val;
		arg.input.file = file;
		arg.input.line = line;
		return cf->callback(CF_PARSE, &arg, cf->data);

	default:
		abort();
	}
	return 0;
}

int
readconfig(char const *file)
{
	FILE *fp;
	char buf[1024];
	unsigned ln = 0;
	int skip_to_eol = 0;
	int ec = 0;
	
	fp = fopen(file, "r");
	if (!fp) {
		error("can't open file %s: %s", file,
		      strerror(errno));
		exit(1);
	}
	
        while (fgets(buf, sizeof(buf), fp)) {
		int len;
		char *kw, *p;
		
		ln++;
		len = strlen(buf);
		if (len == 0)
			continue;

		if (skip_to_eol) {
			skip_to_eol = buf[len-1] != '\n';
			continue;
		}
		
		if (buf[len-1] == '\n')
			buf[--len] = 0;
		else if (!feof(fp)) {
			error("%s:%d: line too long", file, ln);
			skip_to_eol = 1;
			ec = 1;
			continue;
		}

		while (len > 0 && isspace(buf[len-1]))
			buf[--len] = 0;

		if (len == 0)
			continue;

		for (p = buf; (*p == ' ' || *p == '\t'); p++)
			;
		if (*p == 0 || *p == '#')
			continue;
		kw = p;

		while (*p && isascii(*p) && (isalnum(*p) || *p == '_' || *p == '-'))
		     p++;
		if (*p)
			*p++ = 0;
		while (*p == ' ' || *p == '\t')
			p++;
		
		if (!*p) {
			error("%s:%d: malformed statement", file, ln);
			ec = 1;
			continue;
		}

		if (stmt_parse(kw, p, file, ln)) {
			ec = 1;
		}
	}
	fclose(fp);

	if ((double) ping_count * ping_interval >= probe_interval) {
		error("configuration error: ping_count * ping_interval >= probe_interval");
		ec = 1;
	}
	
	return ec;
}

struct json_value *
config_to_json(void)
{
	struct cf_stmt *cf;
	struct json_value *obj = json_new_object();
	struct json_value *vn;
	union cf_callback_arg arg;

	if (!obj)
		goto err;

	for (cf = statements; cf->kw; cf++) {
		switch (cf->type) {
		case STMT_T_STRING:
			if (*(char**)cf->data == NULL)
				vn = json_new_null();
			else
				vn = json_new_string(*(char**)cf->data);
			break;

		case STMT_T_ULONG:
			vn = json_new_number(*(unsigned long*)cf->data);
			break;

		case STMT_T_BOOL:
			vn = json_new_bool(*(int*)cf->data);
			break;
		
		case STMT_T_CALLBACK:
			arg.output = NULL;
			switch (cf->callback(CF_SERIALIZE, &arg, cf->data)) {
			case CF_RET_OK:
				vn = arg.output;
				break;

			case CF_RET_IGNORE:
				continue;
				
			default:
				goto err;
			}
		}
		if (json_object_set(obj, cf->kw, vn)) {
			json_value_free(vn);
			goto err;
		}
	} 
	return obj;
  err:
	json_value_free(obj);
	return NULL;
}

Return to:

Send suggestions and report system problems to the System administrator.