Subversion Repositories Tewi

Rev

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