summaryrefslogtreecommitdiffstats
path: root/src/config.c
blob: ad2a8ac99ec4152aede3d6e141c6b396f0945c84 (plain) (blame)
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
#include <assert.h>
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "config.h"
#include "ini.h"

struct gmnisrv_host *
gmnisrv_config_get_host(struct gmnisrv_config *conf, const char *hostname)
{
	struct gmnisrv_host *host = conf->hosts;
	while (host) {
		if (strcmp(host->hostname, hostname) == 0) {
			return host;
		}
		host = host->next;
	}
	return NULL;
}

struct gmnisrv_route *
gmnisrv_host_get_route(struct gmnisrv_host *host,
		enum gmnisrv_routing routing, const char *spec)
{
	struct gmnisrv_route *route = host->routes;
	while (route) {
		if (route->routing == routing
				&& strcmp(route->spec, spec) == 0) {
			return route;
		}
		route = route->next;
	}
	return NULL;
}

static int
parse_listen(struct gmnisrv_config *conf, const char *value)
{
	char listen[4096];
	assert(strlen(value) < sizeof(listen) - 1);
	strcpy(listen, value);

	char *tok = strtok(listen, " ");
	while (tok) {
		char *port = tok;
		struct gmnisrv_bind *bind =
			calloc(1, sizeof(struct gmnisrv_bind));
		assert(bind);
		bind->port = 1965;
		bind->name = strdup(tok);

		if (tok[0] == '[') {
			bind->family = AF_INET6;
			char *e = strchr(tok, ']');
			if (!e) {
				fprintf(stderr,
					"Error: invalid address specification %s\n",
					tok);
				return 0;
			}
			*e = '\0';
			port = e + 1;
			++tok;
		} else {
			bind->family = AF_INET;
		}

		port = strchr(port, ':');
		if (port) {
			*port = '\0';
			++port;

			char *endptr;
			bind->port = strtoul(port, &endptr, 10);
			if (*endptr) {
				fprintf(stderr, "Error: invalid port %s\n", port);
				return 0;
			}
			if (bind->port == 0 || bind->port > 65535) {
				fprintf(stderr, "Error: invalid port %s\n", port);
				return 0;
			}
		}

		int r = inet_pton(bind->family, tok, bind->addr);
		if (r != 1) {
			fprintf(stderr,
				"Error: invalid address specification %s\n",
				tok);
			return 0;
		}

		bind->next = conf->binds;
		conf->binds = bind;
		tok = strtok(NULL, " ");
	}

	return 1;
}

static int
conf_ini_handler(void *user, const char *section,
	const char *name, const char *value)
{
	struct gmnisrv_config *conf = (struct gmnisrv_config *)user;
	struct {
		char *section;
		char *name;
		char **value;
	} strvars[] = {
		{ ":tls", "store", &conf->tls.store },
		{ ":tls", "organization", &conf->tls.organization },
	};
	struct {
		char *section;
		char *name;
		int (*fn)(struct gmnisrv_config *conf, const char *value);
	} fnvars[] = {
		{ "", "listen", &parse_listen, }
	};

	for (size_t i = 0; i < sizeof(strvars) / sizeof(strvars[0]); ++i) {
		if (strcmp(strvars[i].section, section) != 0) {
			continue;
		}
		if (strcmp(strvars[i].name, name) != 0) {
			continue;
		}
		*strvars[i].value = strdup(value);
		return 1;
	}

	for (size_t i = 0; i < sizeof(fnvars) / sizeof(fnvars[0]); ++i) {
		if (strcmp(fnvars[i].section, section) != 0) {
			continue;
		}
		if (strcmp(fnvars[i].name, name) != 0) {
			continue;
		}
		return fnvars[i].fn(conf, value);
	}

	if (section[0] == '\0' || section[0] == ':') {
		fprintf(stderr, "Unknown config option [%s]%s\n", section, name);
		return 0;
	}

	const char *spec;
	char hostname[1024 + 1];
	enum gmnisrv_routing routing;
	size_t hostln = strcspn(section, ":~");
	switch (section[hostln]) {
	case '\0':
		routing = ROUTE_PATH;
		spec = "/";
		break;
	case ':':
		routing = ROUTE_PATH;
		spec = &section[hostln + 1];
		break;
	case '~':
		routing = ROUTE_REGEX;
		spec = &section[hostln + 1];
		break;
	}
	assert(hostln < sizeof(hostname));
	strncpy(hostname, section, hostln);
	hostname[hostln] = '\0';

	struct gmnisrv_host *host = gmnisrv_config_get_host(conf, hostname);
	if (!host) {
		host = calloc(1, sizeof(struct gmnisrv_host));
		assert(host);
		host->hostname = strdup(section);
		host->next = conf->hosts;
		conf->hosts = host;
	}

	struct gmnisrv_route *route =
		gmnisrv_host_get_route(host, routing, spec);
	if (!route) {
		route = calloc(1, sizeof(struct gmnisrv_route));
		assert(route);
		route->spec = strdup(spec);
		route->routing = routing;
		route->next = host->routes;
		host->routes = route;

		switch (route->routing) {
		case ROUTE_PATH:
			route->path = strdup(spec);
			break;
		case ROUTE_REGEX:
			assert(0); // TODO
		}
	}

	struct {
		char *name;
		char **value;
	} route_strvars[] = {
		{ "root", &route->root },
		{ "index", &route->index },
	};
	struct {
		char *name;
		bool *value;
	} route_bvars[] = {
		{ "autoindex", &route->autoindex },
		{ "cgi", &route->cgi },
	};

	for (size_t i = 0; i < sizeof(route_strvars) / sizeof(route_strvars[0]); ++i) {
		if (strcmp(route_strvars[i].name, name) != 0) {
			continue;
		}
		*route_strvars[i].value = strdup(value);
		return 1;
	}

	for (size_t i = 0; i < sizeof(route_bvars) / sizeof(route_bvars[0]); ++i) {
		if (strcmp(route_bvars[i].name, name) != 0) {
			continue;
		}
		*route_bvars[i].value =
			strcasecmp(value, "yes") == 0 ||
			strcasecmp(value, "true") == 0 ||
			strcasecmp(value, "on") == 0;
		return 1;
	}

	fprintf(stderr, "Unknown config option [%s]%s\n", section, name);
	return 0;
}

static int
validate_config(struct gmnisrv_config *conf)
{
	if (!conf->binds) {
		fprintf(stderr, "Error: config missing listen directive\n");
		return 1;
	}
	if (!conf->hosts) {
		fprintf(stderr, "Error: config defines no hosts\n");
		return 1;
	}
	if (!conf->tls.store) {
		fprintf(stderr, "Error: config defines no certificate store\n");
		return 1;
	}
	return 0;
}

int
load_config(struct gmnisrv_config *conf, const char *path)
{
	FILE *f = fopen(path, "r");
	if (!f) {
		fprintf(stderr, "Error opening %s: %s\n",
				path, strerror(errno));
		return 1;
	}

	int n = ini_parse_file(f, conf_ini_handler, conf);
	fclose(f);
	if (n != 0) {
		fprintf(stderr, "at %s:%d\n", path, n);
		return 1;
	}

	return validate_config(conf);
}

void
config_finish(struct gmnisrv_config *conf)
{
	free(conf->tls.store);
	free(conf->tls.organization);
	struct gmnisrv_bind *bind = conf->binds;
	while (bind) {
		struct gmnisrv_bind *next = bind->next;
		free(bind->name);
		free(bind);
		bind = next;
	}

	struct gmnisrv_host *host = conf->hosts;
	while (host) {
		struct gmnisrv_host *next = host->next;
		free(host->hostname);

		struct gmnisrv_route *route = host->routes;
		while (route) {
			switch (route->routing) {
			case ROUTE_PATH:
				free(route->path);
				break;
			case ROUTE_REGEX:
				assert(0); // TODO
			}

			struct gmnisrv_route *rnext = route->next;
			free(route->spec);
			free(route->root);
			free(route->index);
			free(route);
			route = rnext;
		}

		free(host);
		host = next;
	}
}