Subversion Repositories Tewi

Rev

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