Subversion Repositories Tewi

Rev

Rev 189 | Rev 215 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 189 Rev 212
Line 1... Line 1...
1
/* $Id: config.c 189 2024-09-29 00:37:00Z nishi $ */
1
/* $Id: config.c 212 2024-10-02 17:44:55Z nishi $ */
2
 
2
 
3
#define SOURCE
3
#define SOURCE
4
 
4
 
5
#include "tw_config.h"
5
#include "tw_config.h"
6
#include "tw_module.h"
6
#include "tw_module.h"
7
 
7
 
8
#include <stdio.h>
8
#include <stdio.h>
9
#include <stdint.h>
9
#include <stdint.h>
10
#include <stdlib.h>
10
#include <stdlib.h>
11
#include <string.h>
11
#include <string.h>
-
 
12
 
-
 
13
#ifndef _MSC_VER
12
#include <unistd.h>
14
#include <unistd.h>
-
 
15
#endif
13
 
16
 
14
#ifdef __MINGW32__
17
#if defined(__MINGW32__) || defined(_MSC_VER)
15
#include <winsock2.h>
18
#include <winsock2.h>
16
#endif
19
#endif
17
 
20
 
18
#include <cm_string.h>
21
#include <cm_string.h>
19
#include <cm_log.h>
22
#include <cm_log.h>
Line 35... Line 38...
35
	bool found = false;
38
	bool found = false;
36
	bool pathstart = false;
39
	bool pathstart = false;
37
	bool perm = false;
40
	bool perm = false;
38
again:
41
again:
39
	for(i = 0; i < vhost->dir_count; i++) {
42
	for(i = 0; i < vhost->dir_count; i++) {
-
 
43
		char* noslash;
40
		struct tw_dir_entry* e = &vhost->dirs[i];
44
		struct tw_dir_entry* e = &vhost->dirs[i];
41
		pathstart = false;
45
		pathstart = false;
42
		if(strlen(path) >= strlen(e->dir)) {
46
		if(strlen(path) >= strlen(e->dir)) {
43
			pathstart = true;
-
 
44
			int j;
47
			int j;
-
 
48
			pathstart = true;
45
			for(j = 0; path[j] != 0 && e->dir[j] != 0; j++) {
49
			for(j = 0; path[j] != 0 && e->dir[j] != 0; j++) {
46
				if(path[j] != e->dir[j]) {
50
				if(path[j] != e->dir[j]) {
47
					pathstart = false;
51
					pathstart = false;
48
					break;
52
					break;
49
				}
53
				}
50
			}
54
			}
51
		}
55
		}
52
		char* noslash = cm_strdup(e->dir);
56
		noslash = cm_strdup(e->dir);
53
		noslash[strlen(noslash) - 1] = 0;
57
		noslash[strlen(noslash) - 1] = 0;
54
		if(strcmp(e->dir, path) == 0 || strcmp(noslash, path) == 0 || pathstart) {
58
		if(strcmp(e->dir, path) == 0 || strcmp(noslash, path) == 0 || pathstart) {
55
			found = true;
59
			found = true;
56
			if(strcmp(e->name, "all") == 0) {
60
			if(strcmp(e->name, "all") == 0) {
57
				perm = e->type == TW_DIR_ALLOW;
61
				perm = e->type == TW_DIR_ALLOW;
Line 117... Line 121...
117
	tw_add_define("HAS_SSL");
121
	tw_add_define("HAS_SSL");
118
#endif
122
#endif
119
}
123
}
120
 
124
 
121
int tw_config_read(const char* path) {
125
int tw_config_read(const char* path) {
122
	cm_log("Config", "Reading %s", path);
-
 
123
	char cbuf[2];
126
	char cbuf[2];
124
	cbuf[1] = 0;
-
 
125
	int ln = 0;
127
	int ln = 0;
126
	int ifbr = 0;
128
	int ifbr = 0;
127
	int ignore = -1;
129
	int ignore = -1;
-
 
130
	FILE* f;
-
 
131
	cm_log("Config", "Reading %s", path);
128
	FILE* f = fopen(path, "r");
132
	f = fopen(path, "r");
-
 
133
	cbuf[1] = 0;
129
	if(f != NULL) {
134
	if(f != NULL) {
130
		char* line = malloc(1);
135
		char* line = malloc(1);
131
		line[0] = 0;
-
 
132
		int stop = 0;
136
		int stop = 0;
133
		struct tw_config_entry* current = &config.root;
137
		struct tw_config_entry* current = &config.root;
134
		char* vhost = NULL;
138
		char* vhost = NULL;
135
		char* dir = NULL;
139
		char* dir = NULL;
-
 
140
		line[0] = 0;
136
		while(stop == 0) {
141
		while(stop == 0) {
137
			int c = fread(cbuf, 1, 1, f);
142
			int c = fread(cbuf, 1, 1, f);
138
			if(cbuf[0] == '\n' || c <= 0) {
143
			if(cbuf[0] == '\n' || c <= 0) {
139
				ln++;
-
 
140
				char* l = cm_trim(line);
144
				char* l = cm_trim(line);
-
 
145
				ln++;
141
				if(strlen(l) > 0 && l[0] != '#') {
146
				if(strlen(l) > 0 && l[0] != '#') {
142
					char** r = cm_split(l, " \t");
147
					char** r = cm_split(l, " \t");
143
					int i;
148
					int i;
144
					if(ignore != -1 && ifbr >= ignore) {
149
					if(ignore != -1 && ifbr >= ignore) {
145
						if(cm_strcaseequ(r[0], "EndIf")) ifbr--;
150
						if(cm_strcaseequ(r[0], "EndIf")) ifbr--;
Line 224... Line 229...
224
						} else {
229
						} else {
225
							if(r[1] == NULL) {
230
							if(r[1] == NULL) {
226
								cm_log("Config", "Missing virtual host at line %d", ln);
231
								cm_log("Config", "Missing virtual host at line %d", ln);
227
								stop = 1;
232
								stop = 1;
228
							} else {
233
							} else {
-
 
234
								int i;
229
								vhost = cm_strdup(r[1]);
235
								vhost = cm_strdup(r[1]);
230
								current = &config.vhosts[config.vhost_count++];
236
								current = &config.vhosts[config.vhost_count++];
231
								current->dir_count = 0;
237
								current->dir_count = 0;
232
								current->mime_count = 0;
238
								current->mime_count = 0;
233
								current->icon_count = 0;
239
								current->icon_count = 0;
234
								current->index_count = 0;
240
								current->index_count = 0;
235
								current->readme_count = 0;
241
								current->readme_count = 0;
236
								current->hideport = -1;
242
								current->hideport = -1;
237
								int i;
-
 
238
								current->name = cm_strdup(vhost);
243
								current->name = cm_strdup(vhost);
239
								current->port = -1;
244
								current->port = -1;
240
								for(i = 0; vhost[i] != 0; i++) {
245
								for(i = 0; vhost[i] != 0; i++) {
241
									if(vhost[i] == ':') {
246
									if(vhost[i] == ':') {
242
										current->name[i] = 0;
247
										current->name[i] = 0;
Line 259... Line 264...
259
#ifndef NO_SSL
264
#ifndef NO_SSL
260
						  || cm_strcaseequ(r[0], "ListenSSL")
265
						  || cm_strcaseequ(r[0], "ListenSSL")
261
#endif
266
#endif
262
					) {
267
					) {
263
						for(i = 1; r[i] != NULL; i++) {
268
						for(i = 1; r[i] != NULL; i++) {
-
 
269
#ifdef _MSC_VER
-
 
270
							uint32_t port = atoi(r[i]);
-
 
271
#else
264
							uint64_t port = atoi(r[i]);
272
							uint64_t port = atoi(r[i]);
265
							cm_log("Config", "Going to listen at port %d%s", (int)port, cm_strcaseequ(r[0], "ListenSSL") ? " with SSL" : "");
-
 
266
							if(cm_strcaseequ(r[0], "ListenSSL")) port |= (1ULL << 32);
-
 
-
 
273
#endif
267
							int j;
274
							int j;
-
 
275
							cm_log("Config", "Going to listen at port %d%s", (int)port, cm_strcaseequ(r[0], "ListenSSL") ? " with SSL" : "");
-
 
276
#ifdef _MSC_VER
-
 
277
							if(cm_strcaseequ(r[0], "ListenSSL")) port |= (1UL << 31);
-
 
278
#else
-
 
279
							if(cm_strcaseequ(r[0], "ListenSSL")) port |= (1ULL << 31);
-
 
280
#endif
268
							for(j = 0; config.ports[j] != -1; j++)
281
							for(j = 0; config.ports[j] != -1; j++)
269
								;
282
								;
270
							config.ports[j] = port;
283
							config.ports[j] = port;
271
						}
284
						}
272
					} else if(cm_strcaseequ(r[0], "HidePort")) {
285
					} else if(cm_strcaseequ(r[0], "HidePort")) {
Line 316... Line 329...
316
						ifbr--;
329
						ifbr--;
317
					} else if(cm_strcaseequ(r[0], "BeginIf") || cm_strcaseequ(r[0], "BeginIfNot")) {
330
					} else if(cm_strcaseequ(r[0], "BeginIf") || cm_strcaseequ(r[0], "BeginIfNot")) {
318
						if(r[1] == NULL) {
331
						if(r[1] == NULL) {
319
							cm_log("Config", "Missing condition type at line %d", ln);
332
							cm_log("Config", "Missing condition type at line %d", ln);
320
						} else {
333
						} else {
321
							ifbr++;
-
 
322
							bool ign = false;
334
							bool ign = false;
-
 
335
							ifbr++;
323
							if(cm_strcaseequ(r[1], "False")) {
336
							if(cm_strcaseequ(r[1], "False")) {
324
								ign = true;
337
								ign = true;
325
							} else if(cm_strcaseequ(r[1], "True")) {
338
							} else if(cm_strcaseequ(r[1], "True")) {
326
							} else if(cm_strcaseequ(r[1], "Defined")) {
339
							} else if(cm_strcaseequ(r[1], "Defined")) {
327
								if(r[2] == NULL) {
340
								if(r[2] == NULL) {
Line 426... Line 439...
426
						}
439
						}
427
					} else {
440
					} else {
428
						stop = 1;
441
						stop = 1;
429
						if(r[0] != NULL) {
442
						if(r[0] != NULL) {
430
							int argc;
443
							int argc;
431
							for(argc = 0; r[argc] != NULL; argc++)
-
 
432
								;
-
 
433
							stop = 0;
-
 
434
							int i;
444
							int i;
435
							bool called = false;
445
							bool called = false;
436
							struct tw_tool tools;
446
							struct tw_tool tools;
-
 
447
							for(argc = 0; r[argc] != NULL; argc++)
-
 
448
								;
-
 
449
							stop = 0;
437
							tw_init_tools(&tools);
450
							tw_init_tools(&tools);
438
							for(i = 0; i < config.module_count; i++) {
451
							for(i = 0; i < config.module_count; i++) {
439
								tw_mod_config_t mod_config = (tw_mod_config_t)tw_module_symbol(config.modules[i], "mod_config");
452
								tw_mod_config_t mod_config = (tw_mod_config_t)tw_module_symbol(config.modules[i], "mod_config");
440
								int resp;
453
								int resp;
441
								if(mod_config != NULL && (resp = mod_config(&tools, r, argc)) == TW_CONFIG_PARSED) {
454
								if(mod_config != NULL && (resp = mod_config(&tools, r, argc)) == TW_CONFIG_PARSED) {