source: Main/trunk/Server/config.c@ 157

Last change on this file since 157 was 156, checked in by Nishi, on Sep 25, 2024 at 9:28:10 PM

release 1.07

  • Property svn:keywords set to Id
File size: 12.6 KB
Line 
1/* $Id: config.c 156 2024-09-25 12:28:10Z nishi $ */
2
3#define SOURCE
4
5#include "tw_config.h"
6#include "tw_module.h"
7
8#include <stdio.h>
9#include <stdint.h>
10#include <stdlib.h>
11#include <string.h>
12#include <unistd.h>
13
14#ifdef __MINGW32__
15#include <winsock2.h>
16#endif
17
18#include <cm_string.h>
19#include <cm_log.h>
20
21struct tw_config config;
22
23struct tw_config_entry* tw_vhost_match(const char* name, int port) {
24 int i;
25 for(i = 0; i < config.vhost_count; i++) {
26 if(strcmp(config.vhosts[i].name, name) == 0 && (config.vhosts[i].port == -1 ? 1 : config.vhosts[i].port == port)) {
27 return &config.vhosts[i];
28 }
29 }
30 return &config.root;
31}
32
33bool 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;
38again:
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
69void tw_config_init(void) {
70 int i;
71 for(i = 0; i < MAX_PORTS + 1; i++) {
72 config.ports[i] = -1;
73 }
74 for(i = 0; i < MAX_VHOSTS; i++) {
75#ifndef NO_SSL
76 config.vhosts[i].sslkey = NULL;
77 config.vhosts[i].sslcert = NULL;
78#endif
79 config.vhosts[i].root = NULL;
80#ifdef HAS_CHROOT
81 config.vhosts[i].chroot_path = NULL;
82#endif
83 }
84#ifndef NO_SSL
85 config.root.sslkey = NULL;
86 config.root.sslcert = NULL;
87#endif
88 config.root.root = NULL;
89 config.root.mime_count = 0;
90 config.root.dir_count = 0;
91 config.root.icon_count = 0;
92 config.root.index_count = 0;
93 config.root.readme_count = 0;
94 config.root.hideport = 0;
95#ifdef HAS_CHROOT
96 config.root.chroot_path = NULL;
97#endif
98 config.vhost_count = 0;
99 config.module_count = 0;
100 config.extension = NULL;
101 config.server_root = cm_strdup(PREFIX);
102 config.server_admin = cm_strdup(SERVER_ADMIN);
103 config.defined[0] = NULL;
104 gethostname(config.hostname, 1024);
105}
106
107int tw_config_read(const char* path) {
108 cm_log("Config", "Reading %s", path);
109 char cbuf[2];
110 cbuf[1] = 0;
111 int ln = 0;
112 int ifbr = 0;
113 int ignore = -1;
114 FILE* f = fopen(path, "r");
115 if(f != NULL) {
116 char* line = malloc(1);
117 line[0] = 0;
118 int stop = 0;
119 struct tw_config_entry* current = &config.root;
120 char* vhost = NULL;
121 char* dir = NULL;
122 while(stop == 0) {
123 int c = fread(cbuf, 1, 1, f);
124 if(cbuf[0] == '\n' || c <= 0) {
125 ln++;
126 char* l = cm_trim(line);
127 if(strlen(l) > 0 && l[0] != '#') {
128 char** r = cm_split(l, " \t");
129 int i;
130 if(ignore != -1 && ifbr >= ignore) {
131 if(cm_strcaseequ(r[0], "EndIf")) ifbr--;
132 if(ifbr == 0) {
133 ignore = -1;
134 }
135 } else if(cm_strcaseequ(r[0], "Include") || cm_strcaseequ(r[0], "IncludeOptional")) {
136 for(i = 1; r[i] != NULL; i++) {
137 if(tw_config_read(r[i]) != 0 && cm_strcaseequ(r[0], "Include")) {
138 stop = 1;
139 break;
140 }
141 }
142 } else if(cm_strcaseequ(r[0], "Define")) {
143 if(r[1] == NULL) {
144 cm_log("Config", "Missing name at line %d", ln);
145 stop = 1;
146 } else {
147 tw_add_define(r[1]);
148 }
149 } else if(cm_strcaseequ(r[0], "Undefine")) {
150 if(r[1] == NULL) {
151 cm_log("Config", "Missing name at line %d", ln);
152 stop = 1;
153 } else {
154 tw_delete_define(r[1]);
155 }
156 } else if(cm_strcaseequ(r[0], "BeginDirectory")) {
157 if(dir != NULL) {
158 cm_log("Config", "Already in directory section at line %d", ln);
159 stop = 1;
160 } else {
161 if(r[1] == NULL) {
162 cm_log("Config", "Missing directory at line %d", ln);
163 stop = 1;
164 } else {
165 dir = cm_strcat(r[1], r[1][strlen(r[1]) - 1] == '/' ? "" : "/");
166 }
167 }
168 } else if(cm_strcaseequ(r[0], "EndDirectory")) {
169 if(dir == NULL) {
170 cm_log("Config", "Not in directory section at line %d", ln);
171 stop = 1;
172 } else {
173 free(dir);
174 dir = NULL;
175 }
176 } else if(cm_strcaseequ(r[0], "Allow")) {
177 if(dir == NULL) {
178 cm_log("Config", "Not in directory section at line %d", ln);
179 stop = 1;
180 } else {
181 if(r[1] == NULL) {
182 cm_log("Config", "Missing argument at line %d", ln);
183 stop = 1;
184 } else {
185 struct tw_dir_entry* e = &current->dirs[current->dir_count++];
186 e->name = cm_strdup(r[1]);
187 e->dir = cm_strdup(dir);
188 e->type = TW_DIR_ALLOW;
189 }
190 }
191 } else if(cm_strcaseequ(r[0], "Deny")) {
192 if(dir == NULL) {
193 cm_log("Config", "Not in directory section at line %d", ln);
194 stop = 1;
195 } else {
196 if(r[1] == NULL) {
197 cm_log("Config", "Missing argument at line %d", ln);
198 stop = 1;
199 } else {
200 struct tw_dir_entry* e = &current->dirs[current->dir_count++];
201 e->name = cm_strdup(r[1]);
202 e->dir = cm_strdup(dir);
203 e->type = TW_DIR_DENY;
204 }
205 }
206 } else if(cm_strcaseequ(r[0], "BeginVirtualHost")) {
207 if(vhost != NULL) {
208 cm_log("Config", "Already in virtual host section at line %d", ln);
209 stop = 1;
210 } else {
211 if(r[1] == NULL) {
212 cm_log("Config", "Missing virtual host at line %d", ln);
213 stop = 1;
214 } else {
215 vhost = cm_strdup(r[1]);
216 current = &config.vhosts[config.vhost_count++];
217 current->dir_count = 0;
218 current->mime_count = 0;
219 current->icon_count = 0;
220 current->index_count = 0;
221 current->readme_count = 0;
222 current->hideport = -1;
223 int i;
224 current->name = cm_strdup(vhost);
225 current->port = -1;
226 for(i = 0; vhost[i] != 0; i++) {
227 if(vhost[i] == ':') {
228 current->name[i] = 0;
229 current->port = atoi(current->name + i + 1);
230 break;
231 }
232 }
233 }
234 }
235 } else if(cm_strcaseequ(r[0], "EndVirtualHost")) {
236 if(vhost == NULL) {
237 cm_log("Config", "Not in virtual host section at line %d", ln);
238 stop = 1;
239 } else {
240 free(vhost);
241 vhost = NULL;
242 current = &config.root;
243 }
244 } else if(cm_strcaseequ(r[0], "Listen") || cm_strcaseequ(r[0], "ListenSSL")) {
245 for(i = 1; r[i] != NULL; i++) {
246 uint64_t port = atoi(r[i]);
247 cm_log("Config", "Going to listen at port %d%s", (int)port, cm_strcaseequ(r[0], "ListenSSL") ? " with SSL" : "");
248 if(cm_strcaseequ(r[0], "ListenSSL")) port |= (1ULL << 32);
249 int j;
250 for(j = 0; config.ports[j] != -1; j++)
251 ;
252 config.ports[j] = port;
253 }
254 } else if(cm_strcaseequ(r[0], "HidePort")) {
255 current->hideport = 1;
256 } else if(cm_strcaseequ(r[0], "ShowPort")) {
257 current->hideport = 0;
258#ifndef NO_SSL
259 } else if(cm_strcaseequ(r[0], "SSLKey")) {
260 if(r[1] == NULL) {
261 cm_log("Config", "Missing path at line %d", ln);
262 stop = 1;
263 } else {
264 if(current->sslkey != NULL) free(current->sslkey);
265 current->sslkey = cm_strdup(r[1]);
266 }
267 } else if(cm_strcaseequ(r[0], "SSLCertificate")) {
268 if(r[1] == NULL) {
269 cm_log("Config", "Missing path at line %d", ln);
270 stop = 1;
271 } else {
272 if(current->sslcert != NULL) free(current->sslcert);
273 current->sslcert = cm_strdup(r[1]);
274 }
275#endif
276 } else if(cm_strcaseequ(r[0], "ForceLog")) {
277 if(r[1] == NULL) {
278 cm_log("Config", "Missing log at line %d", ln);
279 stop = 1;
280 } else {
281 cm_force_log(r[1]);
282 }
283 } else if(cm_strcaseequ(r[0], "EndIf")) {
284 if(ifbr == 0) {
285 cm_log("Config", "Missing BeginIf at line %d", ln);
286 stop = 1;
287 }
288 ifbr--;
289 } else if(cm_strcaseequ(r[0], "BeginIf") || cm_strcaseequ(r[0], "BeginIfNot")) {
290 if(r[1] == NULL) {
291 cm_log("Config", "Missing condition type at line %d", ln);
292 } else {
293 ifbr++;
294 bool ign = false;
295 if(cm_strcaseequ(r[1], "False")) {
296 ign = true;
297 } else if(cm_strcaseequ(r[1], "True")) {
298 } else if(cm_strcaseequ(r[1], "Defined")) {
299 if(r[2] == NULL) {
300 cm_log("Config", "Missing name at line %d", ln);
301 stop = 1;
302 } else {
303 int i;
304 bool fndit = false;
305 for(i = 0; config.defined[i] != NULL; i++) {
306 if(strcmp(config.defined[i], r[2]) == 0) {
307 fndit = true;
308 break;
309 }
310 }
311 if(!fndit) {
312 ign = true;
313 }
314 }
315 } else {
316 cm_log("Config", "Unknown condition type at line %d", ln);
317 stop = 1;
318 }
319 if(cm_strcaseequ(r[0], "BeginIfNot")) ign = !ign;
320 if(ign) {
321 ignore = ifbr - 1;
322 }
323 }
324 } else if(cm_strcaseequ(r[0], "ServerRoot")) {
325 if(r[1] == NULL) {
326 cm_log("Config", "Missing path at line %d", ln);
327 stop = 1;
328 } else {
329 chdir(r[1]);
330 free(config.server_root);
331 config.server_root = cm_strdup(r[1]);
332 }
333 } else if(cm_strcaseequ(r[0], "ServerAdmin")) {
334 if(r[1] == NULL) {
335 cm_log("Config", "Missing email at line %d", ln);
336 stop = 1;
337 } else {
338 free(config.server_admin);
339 config.server_admin = cm_strdup(r[1]);
340 }
341 } else if(cm_strcaseequ(r[0], "DocumentRoot")) {
342 if(r[1] == NULL) {
343 cm_log("Config", "Missing path at line %d", ln);
344 stop = 1;
345 } else {
346 if(current->root != NULL) free(current->root);
347 current->root = cm_strdup(strcmp(r[1], "/") == 0 ? "" : r[1]);
348 }
349 } else if(cm_strcaseequ(r[0], "ServerRoot")) {
350 if(r[1] == NULL) {
351 cm_log("Config", "Missing path at line %d", ln);
352 stop = 1;
353 } else {
354 if(config.server_root != NULL) free(config.server_root);
355 config.server_root = cm_strdup(r[1]);
356 }
357 } else if(cm_strcaseequ(r[0], "MIMEType")) {
358 if(r[1] == NULL) {
359 cm_log("Config", "Missing extension at line %d", ln);
360 stop = 1;
361 } else if(r[2] == NULL) {
362 cm_log("Config", "Missing MIME at line %d", ln);
363 stop = 1;
364 } else {
365 struct tw_mime_entry* e = &current->mimes[current->mime_count++];
366 e->ext = cm_strdup(r[1]);
367 e->mime = cm_strdup(r[2]);
368 }
369 } else if(cm_strcaseequ(r[0], "Icon")) {
370 if(r[1] == NULL) {
371 cm_log("Config", "Missing MIME at line %d", ln);
372 stop = 1;
373 } else if(r[2] == NULL) {
374 cm_log("Config", "Missing path at line %d", ln);
375 stop = 1;
376 } else {
377 struct tw_icon_entry* e = &current->icons[current->icon_count++];
378 e->mime = cm_strdup(r[1]);
379 e->icon = cm_strdup(r[2]);
380 }
381 } else if(cm_strcaseequ(r[0], "LoadModule")) {
382 for(i = 1; r[i] != NULL; i++) {
383 void* mod = tw_module_load(r[i]);
384 if(mod != NULL) {
385 config.modules[config.module_count++] = mod;
386 if(tw_module_init(mod) != 0) {
387 stop = 1;
388 break;
389 }
390 } else {
391 cm_log("Config", "Could not load the module at line %d", ln);
392 stop = 1;
393 break;
394 }
395 }
396 } else if(cm_strcaseequ(r[0], "DirectoryIndex")) {
397 for(i = 1; r[i] != NULL; i++) {
398 current->indexes[current->index_count++] = cm_strdup(r[i]);
399 }
400 } else if(cm_strcaseequ(r[0], "Readme")) {
401 for(i = 1; r[i] != NULL; i++) {
402 current->readmes[current->readme_count++] = cm_strdup(r[i]);
403 }
404 } else {
405 stop = 1;
406 if(r[0] != NULL) {
407 int argc;
408 for(argc = 0; r[argc] != NULL; argc++)
409 ;
410 stop = 0;
411 int i;
412 bool called = false;
413 struct tw_tool tools;
414 tw_init_tools(&tools);
415 for(i = 0; i < config.module_count; i++) {
416 tw_mod_config_t mod_config = (tw_mod_config_t)tw_module_symbol(config.modules[i], "mod_config");
417 int resp;
418 if(mod_config != NULL && (resp = mod_config(&tools, r, argc)) == TW_CONFIG_PARSED) {
419 called = true;
420 break;
421 }
422 if(resp == TW_CONFIG_ERROR) {
423 stop = 1;
424 called = true;
425 break;
426 }
427 }
428 if(!called) {
429 cm_log("Config", "Unknown directive `%s' at line %d", r[0], ln);
430 stop = 1;
431 }
432 }
433 }
434 for(i = 0; r[i] != NULL; i++) free(r[i]);
435 free(r);
436 }
437 free(l);
438 free(line);
439 line = malloc(1);
440 line[0] = 0;
441 if(c <= 0) break;
442 } else if(cbuf[0] != '\r') {
443 char* tmp = line;
444 line = cm_strcat(tmp, cbuf);
445 free(tmp);
446 }
447 }
448 free(line);
449 fclose(f);
450 return stop;
451 } else {
452 cm_log("Config", "Could not open the file");
453 return 1;
454 }
455}
Note: See TracBrowser for help on using the repository browser.