Subversion Repositories Tewi

Rev

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