Subversion Repositories Tewi

Rev

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

Rev 20 Rev 21
Line 1... Line 1...
1
/* $Id: config.c 20 2024-09-14 09:59:15Z nishi $ */
1
/* $Id: config.c 21 2024-09-14 12:39:39Z 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"
Line 28... Line 28...
28
		}
28
		}
29
	}
29
	}
30
	return &config.root;
30
	return &config.root;
31
}
31
}
32
 
32
 
-
 
33
bool tw_permission_allowed(const char* path, SOCKADDR addr, struct tw_http_request req, struct tw_config_entry* vhost){
-
 
34
	int i;
-
 
35
	bool found = false;
-
 
36
	bool pathstart = false;
-
 
37
	bool perm = false;
-
 
38
again:
-
 
39
	for(i = 0; i < vhost->dir_count; i++){
-
 
40
		struct tw_dir_entry* e = &vhost->dirs[i];
-
 
41
		pathstart = false;
-
 
42
		if(strlen(path) >= strlen(e->dir)){
-
 
43
			pathstart = true;
-
 
44
			int j;
-
 
45
			for(j = 0; path[j] != 0 && e->dir[j] != 0; j++){
-
 
46
				if(path[j] != e->dir[j]){
-
 
47
					pathstart = false;
-
 
48
					break;
-
 
49
				}
-
 
50
			}
-
 
51
		}
-
 
52
		char* noslash = cm_strdup(e->dir);
-
 
53
		noslash[strlen(noslash) - 1] = 0;
-
 
54
		if(strcmp(e->dir, path) == 0 || strcmp(noslash, path) == 0 || pathstart){
-
 
55
			found = true;
-
 
56
			if(strcmp(e->name, "all") == 0){
-
 
57
				perm = e->type == TW_DIR_ALLOW;
-
 
58
			}
-
 
59
		}
-
 
60
		free(noslash);
-
 
61
	}
-
 
62
	if(!found && vhost != &config.root){
-
 
63
		vhost = &config.root;
-
 
64
		goto again;
-
 
65
	}
-
 
66
	return perm;
-
 
67
}
-
 
68
 
33
void tw_config_init(void) {
69
void tw_config_init(void) {
34
	int i;
70
	int i;
35
	for(i = 0; i < MAX_PORTS + 1; i++) {
71
	for(i = 0; i < MAX_PORTS + 1; i++) {
36
		config.ports[i] = -1;
72
		config.ports[i] = -1;
37
	}
73
	}
Line 41... Line 77...
41
		config.vhosts[i].root = NULL;
77
		config.vhosts[i].root = NULL;
42
	}
78
	}
43
	config.root.sslkey = NULL;
79
	config.root.sslkey = NULL;
44
	config.root.sslcert = NULL;
80
	config.root.sslcert = NULL;
45
	config.root.root = NULL;
81
	config.root.root = NULL;
-
 
82
	config.root.mime_count = 0;
-
 
83
	config.root.dir_count = 0;
46
	config.vhost_count = 0;
84
	config.vhost_count = 0;
47
	config.module_count = 0;
85
	config.module_count = 0;
48
	config.extension = NULL;
86
	config.extension = NULL;
49
	config.server_root = cm_strdup(PREFIX);
87
	config.server_root = cm_strdup(PREFIX);
50
	gethostname(config.hostname, 1024);
88
	gethostname(config.hostname, 1024);
Line 60... Line 98...
60
		char* line = malloc(1);
98
		char* line = malloc(1);
61
		line[0] = 0;
99
		line[0] = 0;
62
		int stop = 0;
100
		int stop = 0;
63
		struct tw_config_entry* current = &config.root;
101
		struct tw_config_entry* current = &config.root;
64
		char* vhost = NULL;
102
		char* vhost = NULL;
-
 
103
		char* dir = NULL;
65
		while(stop == 0) {
104
		while(stop == 0) {
66
			int c = fread(cbuf, 1, 1, f);
105
			int c = fread(cbuf, 1, 1, f);
67
			if(cbuf[0] == '\n' || c <= 0) {
106
			if(cbuf[0] == '\n' || c <= 0) {
68
				ln++;
107
				ln++;
69
				char* l = cm_trim(line);
108
				char* l = cm_trim(line);
Line 75... Line 114...
75
							if(tw_config_read(r[i]) != 0 && cm_strcaseequ(r[0], "Include")) {
114
							if(tw_config_read(r[i]) != 0 && cm_strcaseequ(r[0], "Include")) {
76
								stop = 1;
115
								stop = 1;
77
								break;
116
								break;
78
							}
117
							}
79
						}
118
						}
-
 
119
					} else if(cm_strcaseequ(r[0], "BeginDirectory")) {
-
 
120
						if(dir != NULL) {
-
 
121
							cm_log("Config", "Already in directory section at line %d", ln);
-
 
122
							stop = 1;
-
 
123
						} else {
-
 
124
							if(r[1] == NULL) {
-
 
125
								cm_log("Config", "Missing directory at line %d", ln);
-
 
126
								stop = 1;
-
 
127
							} else {
-
 
128
								dir = cm_strcat(r[1], r[1][strlen(r[1]) - 1] == '/' ? "" : "/");
-
 
129
							}
-
 
130
						}
-
 
131
					} else if(cm_strcaseequ(r[0], "EndDirectory")) {
-
 
132
						if(dir == NULL) {
-
 
133
							cm_log("Config", "Not in directory section at line %d", ln);
-
 
134
							stop = 1;
-
 
135
						} else {
-
 
136
							free(dir);
-
 
137
							dir = NULL;
-
 
138
						}
-
 
139
					} else if(cm_strcaseequ(r[0], "Allow")) {
-
 
140
						if(dir == NULL) {
-
 
141
							cm_log("Config", "Not in directory section at line %d", ln);
-
 
142
							stop = 1;
-
 
143
						} else {
-
 
144
							if(r[1] == NULL) {
-
 
145
								cm_log("Config", "Missing argument at line %d", ln);
-
 
146
								stop = 1;
-
 
147
							} else {
-
 
148
								struct tw_dir_entry* e = &current->dirs[current->dir_count++];
-
 
149
								e->name = cm_strdup(r[1]);
-
 
150
								e->dir = cm_strdup(dir);
-
 
151
								e->type = TW_DIR_ALLOW;
-
 
152
							}
-
 
153
						}
-
 
154
					} else if(cm_strcaseequ(r[0], "Deny")) {
-
 
155
						if(dir == NULL) {
-
 
156
							cm_log("Config", "Not in directory section at line %d", ln);
-
 
157
							stop = 1;
-
 
158
						} else {
-
 
159
							if(r[1] == NULL) {
-
 
160
								cm_log("Config", "Missing argument at line %d", ln);
-
 
161
								stop = 1;
-
 
162
							} else {
-
 
163
								struct tw_dir_entry* e = &current->dirs[current->dir_count++];
-
 
164
								e->name = cm_strdup(r[1]);
-
 
165
								e->dir = cm_strdup(dir);
-
 
166
								e->type = TW_DIR_DENY;
-
 
167
							}
-
 
168
						}
80
					} else if(cm_strcaseequ(r[0], "BeginVirtualHost")) {
169
					} else if(cm_strcaseequ(r[0], "BeginVirtualHost")) {
81
						if(vhost != NULL) {
170
						if(vhost != NULL) {
82
							cm_log("Config", "Already in virtual host section at line %d", ln);
171
							cm_log("Config", "Already in virtual host section at line %d", ln);
83
							stop = 1;
172
							stop = 1;
84
						} else {
173
						} else {
Line 86... Line 175...
86
								cm_log("Config", "Missing virtual host at line %d", ln);
175
								cm_log("Config", "Missing virtual host at line %d", ln);
87
								stop = 1;
176
								stop = 1;
88
							} else {
177
							} else {
89
								vhost = cm_strdup(r[1]);
178
								vhost = cm_strdup(r[1]);
90
								current = &config.vhosts[config.vhost_count++];
179
								current = &config.vhosts[config.vhost_count++];
-
 
180
								current->dir_count = 0;
-
 
181
								current->mime_count = 0;
91
								int i;
182
								int i;
92
								current->name = cm_strdup(vhost);
183
								current->name = cm_strdup(vhost);
93
								current->port = -1;
184
								current->port = -1;
94
								for(i = 0; vhost[i] != 0; i++) {
185
								for(i = 0; vhost[i] != 0; i++) {
95
									if(vhost[i] == ':') {
186
									if(vhost[i] == ':') {
Line 139... Line 230...
139
						if(r[1] == NULL) {
230
						if(r[1] == NULL) {
140
							cm_log("Config", "Missing path at line %d", ln);
231
							cm_log("Config", "Missing path at line %d", ln);
141
							stop = 1;
232
							stop = 1;
142
						} else {
233
						} else {
143
							if(current->root != NULL) free(current->root);
234
							if(current->root != NULL) free(current->root);
144
							current->root = cm_strdup(r[1]);
235
							current->root = cm_strdup(strcmp(r[1], "/") == 0 ? "" : r[1]);
145
						}
236
						}
146
					} else if(cm_strcaseequ(r[0], "ServerRoot")) {
237
					} else if(cm_strcaseequ(r[0], "ServerRoot")) {
147
						if(r[1] == NULL) {
238
						if(r[1] == NULL) {
148
							cm_log("Config", "Missing path at line %d", ln);
239
							cm_log("Config", "Missing path at line %d", ln);
149
							stop = 1;
240
							stop = 1;
150
						} else {
241
						} else {
151
							if(config.server_root != NULL) free(config.server_root);
242
							if(config.server_root != NULL) free(config.server_root);
152
							config.server_root = cm_strdup(r[1]);
243
							config.server_root = cm_strdup(r[1]);
153
						}
244
						}
-
 
245
					} else if(cm_strcaseequ(r[0], "MIMEType")) {
-
 
246
						if(r[1] == NULL) {
-
 
247
							cm_log("Config", "Missing extension at line %d", ln);
-
 
248
							stop = 1;
-
 
249
						}else if(r[2] == NULL) {
-
 
250
							cm_log("Config", "Missing MIME at line %d", ln);
-
 
251
							stop = 1;
-
 
252
						} else {
-
 
253
							struct tw_mime_entry* e = &current->mimes[current->mime_count++];
-
 
254
							e->ext = cm_strdup(r[1]);
-
 
255
							e->mime = cm_strdup(r[2]);
-
 
256
						}
154
					} else if(cm_strcaseequ(r[0], "LoadModule")) {
257
					} else if(cm_strcaseequ(r[0], "LoadModule")) {
155
						for(i = 1; r[i] != NULL; i++) {
258
						for(i = 1; r[i] != NULL; i++) {
156
							void* mod = tw_module_load(r[i]);
259
							void* mod = tw_module_load(r[i]);
157
							if(mod != NULL) {
260
							if(mod != NULL) {
158
								config.modules[config.module_count++] = mod;
261
								config.modules[config.module_count++] = mod;