Subversion Repositories Tewi

Rev

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