Subversion Repositories Tewi

Rev

Rev 127 | Rev 138 | 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 128 2024-09-23 10:19:19Z 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>
12 nishi 12
#include <unistd.h>
4 nishi 13
 
20 nishi 14
#ifdef __MINGW32__
15
#include <winsock2.h>
16
#endif
17
 
4 nishi 18
#include <cm_string.h>
19
#include <cm_log.h>
20
 
6 nishi 21
struct tw_config config;
22
 
12 nishi 23
struct tw_config_entry* tw_vhost_match(const char* name, int port) {
24
	int i;
25
	for(i = 0; i < config.vhost_count; i++) {
13 nishi 26
		if(strcmp(config.vhosts[i].name, name) == 0 && (config.vhosts[i].port == -1 ? 1 : config.vhosts[i].port == port)) {
12 nishi 27
			return &config.vhosts[i];
28
		}
29
	}
30
	return &config.root;
31
}
32
 
22 nishi 33
bool tw_permission_allowed(const char* path, SOCKADDR addr, struct tw_http_request req, struct tw_config_entry* vhost) {
21 nishi 34
	int i;
35
	bool found = false;
36
	bool pathstart = false;
37
	bool perm = false;
38
again:
22 nishi 39
	for(i = 0; i < vhost->dir_count; i++) {
21 nishi 40
		struct tw_dir_entry* e = &vhost->dirs[i];
41
		pathstart = false;
22 nishi 42
		if(strlen(path) >= strlen(e->dir)) {
21 nishi 43
			pathstart = true;
44
			int j;
22 nishi 45
			for(j = 0; path[j] != 0 && e->dir[j] != 0; j++) {
46
				if(path[j] != e->dir[j]) {
21 nishi 47
					pathstart = false;
48
					break;
49
				}
50
			}
51
		}
52
		char* noslash = cm_strdup(e->dir);
53
		noslash[strlen(noslash) - 1] = 0;
22 nishi 54
		if(strcmp(e->dir, path) == 0 || strcmp(noslash, path) == 0 || pathstart) {
21 nishi 55
			found = true;
22 nishi 56
			if(strcmp(e->name, "all") == 0) {
21 nishi 57
				perm = e->type == TW_DIR_ALLOW;
58
			}
59
		}
60
		free(noslash);
61
	}
22 nishi 62
	if(!found && vhost != &config.root) {
21 nishi 63
		vhost = &config.root;
64
		goto again;
65
	}
66
	return perm;
67
}
68
 
7 nishi 69
void tw_config_init(void) {
70
	int i;
71
	for(i = 0; i < MAX_PORTS + 1; i++) {
72
		config.ports[i] = -1;
73
	}
12 nishi 74
	for(i = 0; i < MAX_VHOSTS; i++) {
75
		config.vhosts[i].sslkey = NULL;
76
		config.vhosts[i].sslcert = NULL;
19 nishi 77
		config.vhosts[i].root = NULL;
12 nishi 78
	}
79
	config.root.sslkey = NULL;
80
	config.root.sslcert = NULL;
19 nishi 81
	config.root.root = NULL;
21 nishi 82
	config.root.mime_count = 0;
83
	config.root.dir_count = 0;
22 nishi 84
	config.root.icon_count = 0;
24 nishi 85
	config.root.index_count = 0;
33 nishi 86
	config.root.readme_count = 0;
123 nishi 87
	config.root.hideport = 0;
12 nishi 88
	config.vhost_count = 0;
18 nishi 89
	config.module_count = 0;
90
	config.extension = NULL;
17 nishi 91
	config.server_root = cm_strdup(PREFIX);
128 nishi 92
	config.server_admin = cm_strdup(SERVER_ADMIN);
12 nishi 93
	gethostname(config.hostname, 1024);
128 nishi 94
	chdir(config.server_root);
7 nishi 95
}
6 nishi 96
 
97
int tw_config_read(const char* path) {
4 nishi 98
	cm_log("Config", "Reading %s", path);
99
	char cbuf[2];
100
	cbuf[1] = 0;
6 nishi 101
	int ln = 0;
4 nishi 102
	FILE* f = fopen(path, "r");
6 nishi 103
	if(f != NULL) {
4 nishi 104
		char* line = malloc(1);
105
		line[0] = 0;
6 nishi 106
		int stop = 0;
12 nishi 107
		struct tw_config_entry* current = &config.root;
6 nishi 108
		char* vhost = NULL;
21 nishi 109
		char* dir = NULL;
6 nishi 110
		while(stop == 0) {
4 nishi 111
			int c = fread(cbuf, 1, 1, f);
6 nishi 112
			if(cbuf[0] == '\n' || c <= 0) {
113
				ln++;
4 nishi 114
				char* l = cm_trim(line);
6 nishi 115
				if(strlen(l) > 0 && l[0] != '#') {
5 nishi 116
					char** r = cm_split(l, " \t");
117
					int i;
6 nishi 118
					if(cm_strcaseequ(r[0], "Include") || cm_strcaseequ(r[0], "IncludeOptional")) {
119
						for(i = 1; r[i] != NULL; i++) {
120
							if(tw_config_read(r[i]) != 0 && cm_strcaseequ(r[0], "Include")) {
121
								stop = 1;
122
								break;
5 nishi 123
							}
124
						}
21 nishi 125
					} else if(cm_strcaseequ(r[0], "BeginDirectory")) {
126
						if(dir != NULL) {
127
							cm_log("Config", "Already in directory section at line %d", ln);
128
							stop = 1;
129
						} else {
130
							if(r[1] == NULL) {
131
								cm_log("Config", "Missing directory at line %d", ln);
132
								stop = 1;
133
							} else {
134
								dir = cm_strcat(r[1], r[1][strlen(r[1]) - 1] == '/' ? "" : "/");
135
							}
136
						}
137
					} else if(cm_strcaseequ(r[0], "EndDirectory")) {
138
						if(dir == NULL) {
139
							cm_log("Config", "Not in directory section at line %d", ln);
140
							stop = 1;
141
						} else {
142
							free(dir);
143
							dir = NULL;
144
						}
145
					} else if(cm_strcaseequ(r[0], "Allow")) {
146
						if(dir == NULL) {
147
							cm_log("Config", "Not in directory section at line %d", ln);
148
							stop = 1;
149
						} else {
150
							if(r[1] == NULL) {
151
								cm_log("Config", "Missing argument at line %d", ln);
152
								stop = 1;
153
							} else {
154
								struct tw_dir_entry* e = &current->dirs[current->dir_count++];
155
								e->name = cm_strdup(r[1]);
156
								e->dir = cm_strdup(dir);
157
								e->type = TW_DIR_ALLOW;
158
							}
159
						}
160
					} else if(cm_strcaseequ(r[0], "Deny")) {
161
						if(dir == NULL) {
162
							cm_log("Config", "Not in directory section at line %d", ln);
163
							stop = 1;
164
						} else {
165
							if(r[1] == NULL) {
166
								cm_log("Config", "Missing argument at line %d", ln);
167
								stop = 1;
168
							} else {
169
								struct tw_dir_entry* e = &current->dirs[current->dir_count++];
170
								e->name = cm_strdup(r[1]);
171
								e->dir = cm_strdup(dir);
172
								e->type = TW_DIR_DENY;
173
							}
174
						}
6 nishi 175
					} else if(cm_strcaseequ(r[0], "BeginVirtualHost")) {
176
						if(vhost != NULL) {
12 nishi 177
							cm_log("Config", "Already in virtual host section at line %d", ln);
6 nishi 178
							stop = 1;
179
						} else {
180
							if(r[1] == NULL) {
12 nishi 181
								cm_log("Config", "Missing virtual host at line %d", ln);
6 nishi 182
								stop = 1;
183
							} else {
184
								vhost = cm_strdup(r[1]);
12 nishi 185
								current = &config.vhosts[config.vhost_count++];
21 nishi 186
								current->dir_count = 0;
187
								current->mime_count = 0;
22 nishi 188
								current->icon_count = 0;
24 nishi 189
								current->index_count = 0;
33 nishi 190
								current->readme_count = 0;
123 nishi 191
								current->hideport = -1;
12 nishi 192
								int i;
193
								current->name = cm_strdup(vhost);
13 nishi 194
								current->port = -1;
12 nishi 195
								for(i = 0; vhost[i] != 0; i++) {
196
									if(vhost[i] == ':') {
197
										current->name[i] = 0;
198
										current->port = atoi(current->name + i + 1);
199
										break;
200
									}
201
								}
6 nishi 202
							}
203
						}
204
					} else if(cm_strcaseequ(r[0], "EndVirtualHost")) {
205
						if(vhost == NULL) {
12 nishi 206
							cm_log("Config", "Not in virtual host section at line %d", ln);
6 nishi 207
							stop = 1;
208
						} else {
209
							free(vhost);
210
							vhost = NULL;
12 nishi 211
							current = &config.root;
6 nishi 212
						}
7 nishi 213
					} else if(cm_strcaseequ(r[0], "Listen") || cm_strcaseequ(r[0], "ListenSSL")) {
214
						for(i = 1; r[i] != NULL; i++) {
215
							uint64_t port = atoi(r[i]);
216
							cm_log("Config", "Going to listen at port %d%s", (int)port, cm_strcaseequ(r[0], "ListenSSL") ? " with SSL" : "");
217
							if(cm_strcaseequ(r[0], "ListenSSL")) port |= (1ULL << 32);
218
							int j;
219
							for(j = 0; config.ports[j] != -1; j++)
220
								;
221
							config.ports[j] = port;
222
						}
123 nishi 223
					} else if(cm_strcaseequ(r[0], "HidePort")) {
224
						current->hideport = 1;
225
					} else if(cm_strcaseequ(r[0], "ShowPort")) {
226
						current->hideport = 0;
12 nishi 227
					} else if(cm_strcaseequ(r[0], "SSLKey")) {
228
						if(r[1] == NULL) {
229
							cm_log("Config", "Missing path at line %d", ln);
230
							stop = 1;
231
						} else {
232
							if(current->sslkey != NULL) free(current->sslkey);
233
							current->sslkey = cm_strdup(r[1]);
234
						}
235
					} else if(cm_strcaseequ(r[0], "SSLCertificate")) {
236
						if(r[1] == NULL) {
237
							cm_log("Config", "Missing path at line %d", ln);
238
							stop = 1;
239
						} else {
240
							if(current->sslcert != NULL) free(current->sslcert);
241
							current->sslcert = cm_strdup(r[1]);
242
						}
61 nishi 243
					} else if(cm_strcaseequ(r[0], "ServerRoot")) {
244
						if(r[1] == NULL) {
245
							cm_log("Config", "Missing path at line %d", ln);
246
							stop = 1;
247
						} else {
248
							chdir(r[1]);
127 nishi 249
							free(config.server_root);
250
							config.server_root = cm_strdup(r[1]);
61 nishi 251
						}
128 nishi 252
					} else if(cm_strcaseequ(r[0], "ServerAdmin")) {
253
						if(r[1] == NULL) {
254
							cm_log("Config", "Missing email at line %d", ln);
255
							stop = 1;
256
						} else {
257
							free(config.server_admin);
258
							config.server_admin = cm_strdup(r[1]);
259
						}
19 nishi 260
					} else if(cm_strcaseequ(r[0], "DocumentRoot")) {
261
						if(r[1] == NULL) {
262
							cm_log("Config", "Missing path at line %d", ln);
263
							stop = 1;
264
						} else {
265
							if(current->root != NULL) free(current->root);
21 nishi 266
							current->root = cm_strdup(strcmp(r[1], "/") == 0 ? "" : r[1]);
19 nishi 267
						}
17 nishi 268
					} else if(cm_strcaseequ(r[0], "ServerRoot")) {
269
						if(r[1] == NULL) {
270
							cm_log("Config", "Missing path at line %d", ln);
271
							stop = 1;
272
						} else {
273
							if(config.server_root != NULL) free(config.server_root);
274
							config.server_root = cm_strdup(r[1]);
275
						}
21 nishi 276
					} else if(cm_strcaseequ(r[0], "MIMEType")) {
277
						if(r[1] == NULL) {
278
							cm_log("Config", "Missing extension at line %d", ln);
279
							stop = 1;
22 nishi 280
						} else if(r[2] == NULL) {
21 nishi 281
							cm_log("Config", "Missing MIME at line %d", ln);
282
							stop = 1;
283
						} else {
284
							struct tw_mime_entry* e = &current->mimes[current->mime_count++];
285
							e->ext = cm_strdup(r[1]);
286
							e->mime = cm_strdup(r[2]);
287
						}
22 nishi 288
					} else if(cm_strcaseequ(r[0], "Icon")) {
289
						if(r[1] == NULL) {
290
							cm_log("Config", "Missing MIME at line %d", ln);
291
							stop = 1;
292
						} else if(r[2] == NULL) {
293
							cm_log("Config", "Missing path at line %d", ln);
294
							stop = 1;
295
						} else {
296
							struct tw_icon_entry* e = &current->icons[current->icon_count++];
297
							e->mime = cm_strdup(r[1]);
298
							e->icon = cm_strdup(r[2]);
299
						}
17 nishi 300
					} else if(cm_strcaseequ(r[0], "LoadModule")) {
301
						for(i = 1; r[i] != NULL; i++) {
302
							void* mod = tw_module_load(r[i]);
303
							if(mod != NULL) {
18 nishi 304
								config.modules[config.module_count++] = mod;
17 nishi 305
								if(tw_module_init(mod) != 0) {
306
									stop = 1;
307
									break;
308
								}
309
							} else {
127 nishi 310
								cm_log("Config", "Could not load the module at line %d", ln);
17 nishi 311
								stop = 1;
312
								break;
313
							}
314
						}
24 nishi 315
					} else if(cm_strcaseequ(r[0], "DirectoryIndex")) {
316
						for(i = 1; r[i] != NULL; i++) {
317
							current->indexes[current->index_count++] = cm_strdup(r[i]);
318
						}
33 nishi 319
					} else if(cm_strcaseequ(r[0], "Readme")) {
320
						for(i = 1; r[i] != NULL; i++) {
321
							current->readmes[current->readme_count++] = cm_strdup(r[i]);
322
						}
6 nishi 323
					} else {
39 nishi 324
						stop = 1;
6 nishi 325
						if(r[0] != NULL) {
39 nishi 326
							int argc;
327
							for(argc = 0; r[argc] != NULL; argc++)
328
								;
329
							stop = 0;
330
							int i;
331
							bool called = false;
332
							struct tw_tool tools;
333
							tw_init_tools(&tools);
334
							for(i = 0; i < config.module_count; i++) {
335
								tw_mod_config_t mod_config = (tw_mod_config_t)tw_module_symbol(config.modules[i], "mod_config");
336
								int resp;
337
								if(mod_config != NULL && (resp = mod_config(&tools, r, argc)) == TW_CONFIG_PARSED) {
338
									called = true;
339
									break;
340
								}
341
								if(resp == TW_CONFIG_ERROR) {
342
									stop = 1;
343
									called = true;
344
									break;
345
								}
346
							}
347
							if(!called) {
348
								cm_log("Config", "Unknown directive `%s' at line %d", r[0], ln);
349
								stop = 1;
350
							}
6 nishi 351
						}
5 nishi 352
					}
353
					for(i = 0; r[i] != NULL; i++) free(r[i]);
354
					free(r);
4 nishi 355
				}
356
				free(l);
357
				free(line);
358
				line = malloc(1);
359
				line[0] = 0;
360
				if(c <= 0) break;
6 nishi 361
			} else if(cbuf[0] != '\r') {
4 nishi 362
				char* tmp = line;
363
				line = cm_strcat(tmp, cbuf);
364
				free(tmp);
365
			}
366
		}
367
		free(line);
368
		fclose(f);
6 nishi 369
		return stop;
370
	} else {
5 nishi 371
		cm_log("Config", "Could not open the file");
4 nishi 372
		return 1;
373
	}
374
}