Subversion Repositories Tewi

Rev

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