Subversion Repositories Tewi

Rev

Rev 156 | Rev 174 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4 nishi 1
/* $Id: config.c 161 2024-09-25 13:35:04Z nishi $ */
2
 
16 nishi 3
#define SOURCE
4
 
4 nishi 5
#include "tw_config.h"
17 nishi 6
#include "tw_module.h"
4 nishi 7
 
8
#include <stdio.h>
7 nishi 9
#include <stdint.h>
4 nishi 10
#include <stdlib.h>
11
#include <string.h>
12 nishi 12
#include <unistd.h>
4 nishi 13
 
20 nishi 14
#ifdef __MINGW32__
15
#include <winsock2.h>
16
#endif
17
 
4 nishi 18
#include <cm_string.h>
19
#include <cm_log.h>
20
 
6 nishi 21
struct tw_config config;
22
 
12 nishi 23
struct tw_config_entry* tw_vhost_match(const char* name, int port) {
24
	int i;
25
	for(i = 0; i < config.vhost_count; i++) {
13 nishi 26
		if(strcmp(config.vhosts[i].name, name) == 0 && (config.vhosts[i].port == -1 ? 1 : config.vhosts[i].port == port)) {
12 nishi 27
			return &config.vhosts[i];
28
		}
29
	}
30
	return &config.root;
31
}
32
 
22 nishi 33
bool tw_permission_allowed(const char* path, SOCKADDR addr, struct tw_http_request req, struct tw_config_entry* vhost) {
21 nishi 34
	int i;
35
	bool found = false;
36
	bool pathstart = false;
37
	bool perm = false;
38
again:
22 nishi 39
	for(i = 0; i < vhost->dir_count; i++) {
21 nishi 40
		struct tw_dir_entry* e = &vhost->dirs[i];
41
		pathstart = false;
22 nishi 42
		if(strlen(path) >= strlen(e->dir)) {
21 nishi 43
			pathstart = true;
44
			int j;
22 nishi 45
			for(j = 0; path[j] != 0 && e->dir[j] != 0; j++) {
46
				if(path[j] != e->dir[j]) {
21 nishi 47
					pathstart = false;
48
					break;
49
				}
50
			}
51
		}
52
		char* noslash = cm_strdup(e->dir);
53
		noslash[strlen(noslash) - 1] = 0;
22 nishi 54
		if(strcmp(e->dir, path) == 0 || strcmp(noslash, path) == 0 || pathstart) {
21 nishi 55
			found = true;
22 nishi 56
			if(strcmp(e->name, "all") == 0) {
21 nishi 57
				perm = e->type == TW_DIR_ALLOW;
58
			}
59
		}
60
		free(noslash);
61
	}
22 nishi 62
	if(!found && vhost != &config.root) {
21 nishi 63
		vhost = &config.root;
64
		goto again;
65
	}
66
	return perm;
67
}
68
 
7 nishi 69
void tw_config_init(void) {
70
	int i;
71
	for(i = 0; i < MAX_PORTS + 1; i++) {
72
		config.ports[i] = -1;
73
	}
12 nishi 74
	for(i = 0; i < MAX_VHOSTS; i++) {
156 nishi 75
#ifndef NO_SSL
12 nishi 76
		config.vhosts[i].sslkey = NULL;
77
		config.vhosts[i].sslcert = NULL;
156 nishi 78
#endif
19 nishi 79
		config.vhosts[i].root = NULL;
156 nishi 80
#ifdef HAS_CHROOT
81
		config.vhosts[i].chroot_path = NULL;
82
#endif
12 nishi 83
	}
156 nishi 84
#ifndef NO_SSL
12 nishi 85
	config.root.sslkey = NULL;
86
	config.root.sslcert = NULL;
156 nishi 87
#endif
19 nishi 88
	config.root.root = NULL;
21 nishi 89
	config.root.mime_count = 0;
90
	config.root.dir_count = 0;
22 nishi 91
	config.root.icon_count = 0;
24 nishi 92
	config.root.index_count = 0;
33 nishi 93
	config.root.readme_count = 0;
123 nishi 94
	config.root.hideport = 0;
156 nishi 95
#ifdef HAS_CHROOT
96
	config.root.chroot_path = NULL;
97
#endif
12 nishi 98
	config.vhost_count = 0;
18 nishi 99
	config.module_count = 0;
100
	config.extension = NULL;
17 nishi 101
	config.server_root = cm_strdup(PREFIX);
128 nishi 102
	config.server_admin = cm_strdup(SERVER_ADMIN);
156 nishi 103
	config.defined[0] = NULL;
12 nishi 104
	gethostname(config.hostname, 1024);
161 nishi 105
#ifdef HAS_CHROOT
106
	tw_add_define("HAS_CHROOT");
107
#endif
7 nishi 108
}
6 nishi 109
 
110
int tw_config_read(const char* path) {
4 nishi 111
	cm_log("Config", "Reading %s", path);
112
	char cbuf[2];
113
	cbuf[1] = 0;
6 nishi 114
	int ln = 0;
156 nishi 115
	int ifbr = 0;
116
	int ignore = -1;
4 nishi 117
	FILE* f = fopen(path, "r");
6 nishi 118
	if(f != NULL) {
4 nishi 119
		char* line = malloc(1);
120
		line[0] = 0;
6 nishi 121
		int stop = 0;
12 nishi 122
		struct tw_config_entry* current = &config.root;
6 nishi 123
		char* vhost = NULL;
21 nishi 124
		char* dir = NULL;
6 nishi 125
		while(stop == 0) {
4 nishi 126
			int c = fread(cbuf, 1, 1, f);
6 nishi 127
			if(cbuf[0] == '\n' || c <= 0) {
128
				ln++;
4 nishi 129
				char* l = cm_trim(line);
6 nishi 130
				if(strlen(l) > 0 && l[0] != '#') {
5 nishi 131
					char** r = cm_split(l, " \t");
132
					int i;
156 nishi 133
					if(ignore != -1 && ifbr >= ignore) {
134
						if(cm_strcaseequ(r[0], "EndIf")) ifbr--;
135
						if(ifbr == 0) {
136
							ignore = -1;
137
						}
138
					} else if(cm_strcaseequ(r[0], "Include") || cm_strcaseequ(r[0], "IncludeOptional")) {
6 nishi 139
						for(i = 1; r[i] != NULL; i++) {
140
							if(tw_config_read(r[i]) != 0 && cm_strcaseequ(r[0], "Include")) {
141
								stop = 1;
142
								break;
5 nishi 143
							}
144
						}
156 nishi 145
					} else if(cm_strcaseequ(r[0], "Define")) {
146
						if(r[1] == NULL) {
147
							cm_log("Config", "Missing name at line %d", ln);
148
							stop = 1;
149
						} else {
150
							tw_add_define(r[1]);
151
						}
152
					} else if(cm_strcaseequ(r[0], "Undefine")) {
153
						if(r[1] == NULL) {
154
							cm_log("Config", "Missing name at line %d", ln);
155
							stop = 1;
156
						} else {
157
							tw_delete_define(r[1]);
158
						}
21 nishi 159
					} else if(cm_strcaseequ(r[0], "BeginDirectory")) {
160
						if(dir != NULL) {
161
							cm_log("Config", "Already in directory section at line %d", ln);
162
							stop = 1;
163
						} else {
164
							if(r[1] == NULL) {
165
								cm_log("Config", "Missing directory at line %d", ln);
166
								stop = 1;
167
							} else {
168
								dir = cm_strcat(r[1], r[1][strlen(r[1]) - 1] == '/' ? "" : "/");
169
							}
170
						}
171
					} else if(cm_strcaseequ(r[0], "EndDirectory")) {
172
						if(dir == NULL) {
173
							cm_log("Config", "Not in directory section at line %d", ln);
174
							stop = 1;
175
						} else {
176
							free(dir);
177
							dir = NULL;
178
						}
179
					} else if(cm_strcaseequ(r[0], "Allow")) {
180
						if(dir == NULL) {
181
							cm_log("Config", "Not in directory section at line %d", ln);
182
							stop = 1;
183
						} else {
184
							if(r[1] == NULL) {
185
								cm_log("Config", "Missing argument at line %d", ln);
186
								stop = 1;
187
							} else {
188
								struct tw_dir_entry* e = &current->dirs[current->dir_count++];
189
								e->name = cm_strdup(r[1]);
190
								e->dir = cm_strdup(dir);
191
								e->type = TW_DIR_ALLOW;
192
							}
193
						}
194
					} else if(cm_strcaseequ(r[0], "Deny")) {
195
						if(dir == NULL) {
196
							cm_log("Config", "Not in directory section at line %d", ln);
197
							stop = 1;
198
						} else {
199
							if(r[1] == NULL) {
200
								cm_log("Config", "Missing argument at line %d", ln);
201
								stop = 1;
202
							} else {
203
								struct tw_dir_entry* e = &current->dirs[current->dir_count++];
204
								e->name = cm_strdup(r[1]);
205
								e->dir = cm_strdup(dir);
206
								e->type = TW_DIR_DENY;
207
							}
208
						}
6 nishi 209
					} else if(cm_strcaseequ(r[0], "BeginVirtualHost")) {
210
						if(vhost != NULL) {
12 nishi 211
							cm_log("Config", "Already in virtual host section at line %d", ln);
6 nishi 212
							stop = 1;
213
						} else {
214
							if(r[1] == NULL) {
12 nishi 215
								cm_log("Config", "Missing virtual host at line %d", ln);
6 nishi 216
								stop = 1;
217
							} else {
218
								vhost = cm_strdup(r[1]);
12 nishi 219
								current = &config.vhosts[config.vhost_count++];
21 nishi 220
								current->dir_count = 0;
221
								current->mime_count = 0;
22 nishi 222
								current->icon_count = 0;
24 nishi 223
								current->index_count = 0;
33 nishi 224
								current->readme_count = 0;
123 nishi 225
								current->hideport = -1;
12 nishi 226
								int i;
227
								current->name = cm_strdup(vhost);
13 nishi 228
								current->port = -1;
12 nishi 229
								for(i = 0; vhost[i] != 0; i++) {
230
									if(vhost[i] == ':') {
231
										current->name[i] = 0;
232
										current->port = atoi(current->name + i + 1);
233
										break;
234
									}
235
								}
6 nishi 236
							}
237
						}
238
					} else if(cm_strcaseequ(r[0], "EndVirtualHost")) {
239
						if(vhost == NULL) {
12 nishi 240
							cm_log("Config", "Not in virtual host section at line %d", ln);
6 nishi 241
							stop = 1;
242
						} else {
243
							free(vhost);
244
							vhost = NULL;
12 nishi 245
							current = &config.root;
6 nishi 246
						}
7 nishi 247
					} else if(cm_strcaseequ(r[0], "Listen") || cm_strcaseequ(r[0], "ListenSSL")) {
248
						for(i = 1; r[i] != NULL; i++) {
249
							uint64_t port = atoi(r[i]);
250
							cm_log("Config", "Going to listen at port %d%s", (int)port, cm_strcaseequ(r[0], "ListenSSL") ? " with SSL" : "");
251
							if(cm_strcaseequ(r[0], "ListenSSL")) port |= (1ULL << 32);
252
							int j;
253
							for(j = 0; config.ports[j] != -1; j++)
254
								;
255
							config.ports[j] = port;
256
						}
123 nishi 257
					} else if(cm_strcaseequ(r[0], "HidePort")) {
258
						current->hideport = 1;
259
					} else if(cm_strcaseequ(r[0], "ShowPort")) {
260
						current->hideport = 0;
156 nishi 261
#ifndef NO_SSL
12 nishi 262
					} else if(cm_strcaseequ(r[0], "SSLKey")) {
263
						if(r[1] == NULL) {
264
							cm_log("Config", "Missing path at line %d", ln);
265
							stop = 1;
266
						} else {
267
							if(current->sslkey != NULL) free(current->sslkey);
268
							current->sslkey = cm_strdup(r[1]);
269
						}
270
					} else if(cm_strcaseequ(r[0], "SSLCertificate")) {
271
						if(r[1] == NULL) {
272
							cm_log("Config", "Missing path at line %d", ln);
273
							stop = 1;
274
						} else {
275
							if(current->sslcert != NULL) free(current->sslcert);
276
							current->sslcert = cm_strdup(r[1]);
277
						}
156 nishi 278
#endif
161 nishi 279
#ifdef HAS_CHROOT
280
					} else if(cm_strcaseequ(r[0], "ChrootDirectory")) {
281
						if(r[1] == NULL) {
282
							cm_log("Config", "Missing path at line %d", ln);
283
							stop = 1;
284
						} else {
285
							if(current->chroot_path != NULL) free(current->chroot_path);
286
							current->chroot_path = cm_strdup(r[1]);
287
						}
288
#endif
156 nishi 289
					} else if(cm_strcaseequ(r[0], "ForceLog")) {
290
						if(r[1] == NULL) {
291
							cm_log("Config", "Missing log at line %d", ln);
292
							stop = 1;
293
						} else {
294
							cm_force_log(r[1]);
295
						}
296
					} else if(cm_strcaseequ(r[0], "EndIf")) {
297
						if(ifbr == 0) {
298
							cm_log("Config", "Missing BeginIf at line %d", ln);
299
							stop = 1;
300
						}
301
						ifbr--;
302
					} else if(cm_strcaseequ(r[0], "BeginIf") || cm_strcaseequ(r[0], "BeginIfNot")) {
303
						if(r[1] == NULL) {
304
							cm_log("Config", "Missing condition type at line %d", ln);
305
						} else {
306
							ifbr++;
307
							bool ign = false;
308
							if(cm_strcaseequ(r[1], "False")) {
309
								ign = true;
310
							} else if(cm_strcaseequ(r[1], "True")) {
311
							} else if(cm_strcaseequ(r[1], "Defined")) {
312
								if(r[2] == NULL) {
313
									cm_log("Config", "Missing name at line %d", ln);
314
									stop = 1;
315
								} else {
316
									int i;
317
									bool fndit = false;
318
									for(i = 0; config.defined[i] != NULL; i++) {
319
										if(strcmp(config.defined[i], r[2]) == 0) {
320
											fndit = true;
321
											break;
322
										}
323
									}
324
									if(!fndit) {
325
										ign = true;
326
									}
327
								}
328
							} else {
329
								cm_log("Config", "Unknown condition type at line %d", ln);
330
								stop = 1;
331
							}
332
							if(cm_strcaseequ(r[0], "BeginIfNot")) ign = !ign;
333
							if(ign) {
334
								ignore = ifbr - 1;
335
							}
336
						}
61 nishi 337
					} else if(cm_strcaseequ(r[0], "ServerRoot")) {
338
						if(r[1] == NULL) {
339
							cm_log("Config", "Missing path at line %d", ln);
340
							stop = 1;
341
						} else {
342
							chdir(r[1]);
127 nishi 343
							free(config.server_root);
344
							config.server_root = cm_strdup(r[1]);
61 nishi 345
						}
128 nishi 346
					} else if(cm_strcaseequ(r[0], "ServerAdmin")) {
347
						if(r[1] == NULL) {
348
							cm_log("Config", "Missing email at line %d", ln);
349
							stop = 1;
350
						} else {
351
							free(config.server_admin);
352
							config.server_admin = cm_strdup(r[1]);
353
						}
19 nishi 354
					} else if(cm_strcaseequ(r[0], "DocumentRoot")) {
355
						if(r[1] == NULL) {
356
							cm_log("Config", "Missing path at line %d", ln);
357
							stop = 1;
358
						} else {
359
							if(current->root != NULL) free(current->root);
21 nishi 360
							current->root = cm_strdup(strcmp(r[1], "/") == 0 ? "" : r[1]);
19 nishi 361
						}
21 nishi 362
					} else if(cm_strcaseequ(r[0], "MIMEType")) {
363
						if(r[1] == NULL) {
364
							cm_log("Config", "Missing extension at line %d", ln);
365
							stop = 1;
22 nishi 366
						} else if(r[2] == NULL) {
21 nishi 367
							cm_log("Config", "Missing MIME at line %d", ln);
368
							stop = 1;
369
						} else {
370
							struct tw_mime_entry* e = &current->mimes[current->mime_count++];
371
							e->ext = cm_strdup(r[1]);
372
							e->mime = cm_strdup(r[2]);
373
						}
22 nishi 374
					} else if(cm_strcaseequ(r[0], "Icon")) {
375
						if(r[1] == NULL) {
376
							cm_log("Config", "Missing MIME at line %d", ln);
377
							stop = 1;
378
						} else if(r[2] == NULL) {
379
							cm_log("Config", "Missing path at line %d", ln);
380
							stop = 1;
381
						} else {
382
							struct tw_icon_entry* e = &current->icons[current->icon_count++];
383
							e->mime = cm_strdup(r[1]);
384
							e->icon = cm_strdup(r[2]);
385
						}
17 nishi 386
					} else if(cm_strcaseequ(r[0], "LoadModule")) {
387
						for(i = 1; r[i] != NULL; i++) {
388
							void* mod = tw_module_load(r[i]);
389
							if(mod != NULL) {
18 nishi 390
								config.modules[config.module_count++] = mod;
17 nishi 391
								if(tw_module_init(mod) != 0) {
392
									stop = 1;
393
									break;
394
								}
395
							} else {
127 nishi 396
								cm_log("Config", "Could not load the module at line %d", ln);
17 nishi 397
								stop = 1;
398
								break;
399
							}
400
						}
24 nishi 401
					} else if(cm_strcaseequ(r[0], "DirectoryIndex")) {
402
						for(i = 1; r[i] != NULL; i++) {
403
							current->indexes[current->index_count++] = cm_strdup(r[i]);
404
						}
33 nishi 405
					} else if(cm_strcaseequ(r[0], "Readme")) {
406
						for(i = 1; r[i] != NULL; i++) {
407
							current->readmes[current->readme_count++] = cm_strdup(r[i]);
408
						}
6 nishi 409
					} else {
39 nishi 410
						stop = 1;
6 nishi 411
						if(r[0] != NULL) {
39 nishi 412
							int argc;
413
							for(argc = 0; r[argc] != NULL; argc++)
414
								;
415
							stop = 0;
416
							int i;
417
							bool called = false;
418
							struct tw_tool tools;
419
							tw_init_tools(&tools);
420
							for(i = 0; i < config.module_count; i++) {
421
								tw_mod_config_t mod_config = (tw_mod_config_t)tw_module_symbol(config.modules[i], "mod_config");
422
								int resp;
423
								if(mod_config != NULL && (resp = mod_config(&tools, r, argc)) == TW_CONFIG_PARSED) {
424
									called = true;
425
									break;
426
								}
427
								if(resp == TW_CONFIG_ERROR) {
428
									stop = 1;
429
									called = true;
430
									break;
431
								}
432
							}
433
							if(!called) {
434
								cm_log("Config", "Unknown directive `%s' at line %d", r[0], ln);
435
								stop = 1;
436
							}
6 nishi 437
						}
5 nishi 438
					}
439
					for(i = 0; r[i] != NULL; i++) free(r[i]);
440
					free(r);
4 nishi 441
				}
442
				free(l);
443
				free(line);
444
				line = malloc(1);
445
				line[0] = 0;
446
				if(c <= 0) break;
6 nishi 447
			} else if(cbuf[0] != '\r') {
4 nishi 448
				char* tmp = line;
449
				line = cm_strcat(tmp, cbuf);
450
				free(tmp);
451
			}
452
		}
453
		free(line);
454
		fclose(f);
6 nishi 455
		return stop;
456
	} else {
5 nishi 457
		cm_log("Config", "Could not open the file");
4 nishi 458
		return 1;
459
	}
460
}