Subversion Repositories Tewi

Rev

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