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

Last change on this file since 312 was 312, checked in by Nishi, on Oct 14, 2024 at 3:17:37 AM

works on os2

  • Property svn:keywords set to Id
File size: 15.9 KB
Line 
1/* $Id: config.c 312 2024-10-13 18:17:37Z nishi $ */
2
3#define SOURCE
4
5#include "../config.h"
6
7#include <stdio.h>
8#include <stdint.h>
9#include <stdlib.h>
10#include <string.h>
11#include <sys/stat.h>
12
13#if !defined(_MSC_VER) && !defined(__BORLANDC__)
14#include <unistd.h>
15#endif
16
17#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || (defined(__WATCOMC__) && !defined(__OS2__) && !defined(__WATCOMC__))
18#ifdef USE_WINSOCK1
19#include <winsock.h>
20#else
21#include <winsock2.h>
22#endif
23#endif
24
25#include <cm_string.h>
26#include <cm_log.h>
27
28#ifdef __OS2__
29#include <types.h>
30#include <netinet/in.h>
31#include <tcpustd.h>
32#endif
33
34#include "tw_config.h"
35#include "tw_module.h"
36
37struct tw_config config;
38
39struct tw_config_entry* tw_vhost_match(const char* name, int port) {
40 int i;
41 for(i = 0; i < config.vhost_count; i++) {
42 if(strcmp(config.vhosts[i].name, name) == 0 && (config.vhosts[i].port == -1 ? 1 : config.vhosts[i].port == port)) {
43 return &config.vhosts[i];
44 }
45 }
46 return &config.root;
47}
48
49bool tw_permission_allowed(const char* path, SOCKADDR addr, struct tw_http_request req, struct tw_config_entry* vhost) {
50 int i;
51 bool found = false;
52 bool pathstart = false;
53 bool perm = false;
54again:
55 for(i = 0; i < vhost->dir_count; i++) {
56 char* noslash;
57 struct tw_dir_entry* e = &vhost->dirs[i];
58 pathstart = false;
59 if(strlen(path) >= strlen(e->dir)) {
60 int j;
61 pathstart = true;
62 for(j = 0; path[j] != 0 && e->dir[j] != 0; j++) {
63 if(path[j] != e->dir[j]) {
64 pathstart = false;
65 break;
66 }
67 }
68 }
69 noslash = cm_strdup(e->dir);
70 noslash[strlen(noslash) - 1] = 0;
71 if(strcmp(e->dir, path) == 0 || strcmp(noslash, path) == 0 || pathstart) {
72 found = true;
73 if(strcmp(e->name, "all") == 0) {
74 perm = e->type == TW_DIR_ALLOW;
75 }
76 }
77 free(noslash);
78 }
79 if(!found && vhost != &config.root) {
80 vhost = &config.root;
81 goto again;
82 }
83 return perm;
84}
85
86void tw_config_init(void) {
87 int i;
88 for(i = 0; i < MAX_PORTS + 1; i++) {
89 config.ports[i] = -1;
90 }
91 for(i = 0; i < MAX_VHOSTS; i++) {
92#ifndef NO_SSL
93 config.vhosts[i].sslkey = NULL;
94 config.vhosts[i].sslcert = NULL;
95#endif
96 config.vhosts[i].root = NULL;
97#ifdef HAS_CHROOT
98 config.vhosts[i].chroot_path = NULL;
99#endif
100 }
101#ifndef NO_SSL
102 config.root.sslkey = NULL;
103 config.root.sslcert = NULL;
104#endif
105 config.root.root = NULL;
106 config.root.mime_count = 0;
107 config.root.dir_count = 0;
108 config.root.icon_count = 0;
109 config.root.index_count = 0;
110 config.root.readme_count = 0;
111 config.root.hideport = 0;
112#ifdef HAS_CHROOT
113 config.root.chroot_path = NULL;
114#endif
115 config.vhost_count = 0;
116 config.module_count = 0;
117 config.extension = NULL;
118 config.server_root = cm_strdup(PREFIX);
119 config.server_admin = cm_strdup(SERVER_ADMIN);
120 config.defined[0] = NULL;
121#if defined(_PSP)
122 strcpy(config.hostname, "psp");
123#elif defined(__PPU__)
124 strcpy(config.hostname, "ps3");
125#elif defined(__ps2sdk__)
126 strcpy(config.hostname, "ps2");
127#else
128 gethostname(config.hostname, 1024);
129#endif
130#ifdef HAS_CHROOT
131 tw_add_define("HAS_CHROOT");
132#endif
133#ifndef NO_SSL
134 tw_add_define("HAS_SSL");
135#endif
136}
137
138int tw_config_read(const char* path) {
139 char cbuf[2];
140 int ln = 0;
141 int ifbr = 0;
142 int ignore = -1;
143 int portcount;
144 FILE* f;
145 cm_log("Config", "Reading %s", path);
146 f = fopen(path, "r");
147 cbuf[1] = 0;
148 if(f != NULL) {
149 char* line = malloc(1);
150 int stop = 0;
151 struct tw_config_entry* current = &config.root;
152 char* vhost = NULL;
153 char* dir = NULL;
154 line[0] = 0;
155 while(stop == 0) {
156 int c = fread(cbuf, 1, 1, f);
157 if(cbuf[0] == '\n' || c <= 0) {
158 char* l = cm_trim(line);
159 ln++;
160 if(strlen(l) > 0 && l[0] != '#') {
161 char** r = cm_split(l, " \t");
162 int i;
163 if(ignore != -1 && ifbr >= ignore) {
164 if(cm_strcaseequ(r[0], "EndIf")) ifbr--;
165 if(ifbr == 0) {
166 ignore = -1;
167 }
168 } else if(cm_strcaseequ(r[0], "Include") || cm_strcaseequ(r[0], "IncludeOptional")) {
169 for(i = 1; r[i] != NULL; i++) {
170 if(tw_config_read(r[i]) != 0 && cm_strcaseequ(r[0], "Include")) {
171 stop = 1;
172 break;
173 }
174 }
175 } else if(cm_strcaseequ(r[0], "Define")) {
176 if(r[1] == NULL) {
177 cm_log("Config", "Missing name at line %d", ln);
178 stop = 1;
179 } else {
180 tw_add_define(r[1]);
181 }
182 } else if(cm_strcaseequ(r[0], "Undefine")) {
183 if(r[1] == NULL) {
184 cm_log("Config", "Missing name at line %d", ln);
185 stop = 1;
186 } else {
187 tw_delete_define(r[1]);
188 }
189 } else if(cm_strcaseequ(r[0], "BeginDirectory")) {
190 if(dir != NULL) {
191 cm_log("Config", "Already in directory section at line %d", ln);
192 stop = 1;
193 } else {
194 if(r[1] == NULL) {
195 cm_log("Config", "Missing directory at line %d", ln);
196 stop = 1;
197 } else {
198 dir = cm_strcat(r[1], r[1][strlen(r[1]) - 1] == '/' ? "" : "/");
199 }
200 }
201 } else if(cm_strcaseequ(r[0], "EndDirectory")) {
202 if(dir == NULL) {
203 cm_log("Config", "Not in directory section at line %d", ln);
204 stop = 1;
205 } else {
206 free(dir);
207 dir = NULL;
208 }
209 } else if(cm_strcaseequ(r[0], "Allow")) {
210 if(dir == NULL) {
211 cm_log("Config", "Not in directory section at line %d", ln);
212 stop = 1;
213 } else {
214 if(r[1] == NULL) {
215 cm_log("Config", "Missing argument at line %d", ln);
216 stop = 1;
217 } else {
218 struct tw_dir_entry* e = &current->dirs[current->dir_count++];
219 e->name = cm_strdup(r[1]);
220 e->dir = cm_strdup(dir);
221 e->type = TW_DIR_ALLOW;
222 }
223 }
224 } else if(cm_strcaseequ(r[0], "Deny")) {
225 if(dir == NULL) {
226 cm_log("Config", "Not in directory section at line %d", ln);
227 stop = 1;
228 } else {
229 if(r[1] == NULL) {
230 cm_log("Config", "Missing argument at line %d", ln);
231 stop = 1;
232 } else {
233 struct tw_dir_entry* e = &current->dirs[current->dir_count++];
234 e->name = cm_strdup(r[1]);
235 e->dir = cm_strdup(dir);
236 e->type = TW_DIR_DENY;
237 }
238 }
239 } else if(cm_strcaseequ(r[0], "BeginVirtualHost")) {
240 if(vhost != NULL) {
241 cm_log("Config", "Already in virtual host section at line %d", ln);
242 stop = 1;
243 } else {
244 if(r[1] == NULL) {
245 cm_log("Config", "Missing virtual host at line %d", ln);
246 stop = 1;
247 } else {
248 int i;
249 vhost = cm_strdup(r[1]);
250 current = &config.vhosts[config.vhost_count++];
251 current->dir_count = 0;
252 current->mime_count = 0;
253 current->icon_count = 0;
254 current->index_count = 0;
255 current->readme_count = 0;
256 current->hideport = -1;
257 current->name = cm_strdup(vhost);
258 current->port = -1;
259 for(i = 0; vhost[i] != 0; i++) {
260 if(vhost[i] == ':') {
261 current->name[i] = 0;
262 current->port = atoi(current->name + i + 1);
263 break;
264 }
265 }
266 }
267 }
268 } else if(cm_strcaseequ(r[0], "EndVirtualHost")) {
269 if(vhost == NULL) {
270 cm_log("Config", "Not in virtual host section at line %d", ln);
271 stop = 1;
272 } else {
273 free(vhost);
274 vhost = NULL;
275 current = &config.root;
276 }
277 } else if(cm_strcaseequ(r[0], "Listen")
278#ifndef NO_SSL
279 || cm_strcaseequ(r[0], "ListenSSL")
280#endif
281 ) {
282 for(i = 1; r[i] != NULL; i++) {
283#if defined(_MSC_VER) || defined(__BORLANDC__)
284 uint32_t port = atoi(r[i]);
285#else
286 uint64_t port = atoi(r[i]);
287#endif
288 int j;
289 cm_log("Config", "Going to listen at port %d%s", (int)port, cm_strcaseequ(r[0], "ListenSSL") ? " with SSL" : "");
290#if defined(_MSC_VER) || defined(__BORLANDC__)
291 if(cm_strcaseequ(r[0], "ListenSSL")) port |= (1UL << 31);
292#else
293 if(cm_strcaseequ(r[0], "ListenSSL")) port |= (1ULL << 31);
294#endif
295 for(j = 0; config.ports[j] != -1; j++)
296 ;
297 config.ports[j] = port;
298 }
299 } else if(cm_strcaseequ(r[0], "HidePort")) {
300 current->hideport = 1;
301 } else if(cm_strcaseequ(r[0], "ShowPort")) {
302 current->hideport = 0;
303#ifndef NO_SSL
304 } else if(cm_strcaseequ(r[0], "SSLKey")) {
305 if(r[1] == NULL) {
306 cm_log("Config", "Missing path at line %d", ln);
307 stop = 1;
308 } else {
309 if(current->sslkey != NULL) free(current->sslkey);
310 current->sslkey = cm_strdup(r[1]);
311 }
312 } else if(cm_strcaseequ(r[0], "SSLCertificate")) {
313 if(r[1] == NULL) {
314 cm_log("Config", "Missing path at line %d", ln);
315 stop = 1;
316 } else {
317 if(current->sslcert != NULL) free(current->sslcert);
318 current->sslcert = cm_strdup(r[1]);
319 }
320#endif
321#ifdef HAS_CHROOT
322 } else if(cm_strcaseequ(r[0], "ChrootDirectory")) {
323 if(r[1] == NULL) {
324 cm_log("Config", "Missing path at line %d", ln);
325 stop = 1;
326 } else {
327 if(current->chroot_path != NULL) free(current->chroot_path);
328 current->chroot_path = cm_strdup(r[1]);
329 }
330#endif
331 } else if(cm_strcaseequ(r[0], "ForceLog")) {
332 if(r[1] == NULL) {
333 cm_log("Config", "Missing log at line %d", ln);
334 stop = 1;
335 } else {
336 cm_force_log(r[1]);
337 }
338 } else if(cm_strcaseequ(r[0], "EndIf")) {
339 if(ifbr == 0) {
340 cm_log("Config", "Missing BeginIf at line %d", ln);
341 stop = 1;
342 }
343 ifbr--;
344 } else if(cm_strcaseequ(r[0], "BeginIf") || cm_strcaseequ(r[0], "BeginIfNot")) {
345 if(r[1] == NULL) {
346 cm_log("Config", "Missing condition type at line %d", ln);
347 } else {
348 bool ign = false;
349 ifbr++;
350 if(cm_strcaseequ(r[1], "False")) {
351 ign = true;
352 } else if(cm_strcaseequ(r[1], "True")) {
353 } else if(cm_strcaseequ(r[1], "Defined")) {
354 if(r[2] == NULL) {
355 cm_log("Config", "Missing name at line %d", ln);
356 stop = 1;
357 } else {
358 int i;
359 bool fndit = false;
360 for(i = 0; config.defined[i] != NULL; i++) {
361 if(strcmp(config.defined[i], r[2]) == 0) {
362 fndit = true;
363 break;
364 }
365 }
366 if(!fndit) {
367 ign = true;
368 }
369 }
370 } else {
371 cm_log("Config", "Unknown condition type at line %d", ln);
372 stop = 1;
373 }
374 if(cm_strcaseequ(r[0], "BeginIfNot")) ign = !ign;
375 if(ign) {
376 ignore = ifbr - 1;
377 }
378 }
379 } else if(cm_strcaseequ(r[0], "ServerRoot")) {
380 if(r[1] == NULL) {
381 cm_log("Config", "Missing path at line %d", ln);
382 stop = 1;
383 } else {
384 chdir(r[1]);
385 free(config.server_root);
386 config.server_root = cm_strdup(r[1]);
387 }
388 } else if(cm_strcaseequ(r[0], "ServerAdmin")) {
389 if(r[1] == NULL) {
390 cm_log("Config", "Missing email at line %d", ln);
391 stop = 1;
392 } else {
393 free(config.server_admin);
394 config.server_admin = cm_strdup(r[1]);
395 }
396 } else if(cm_strcaseequ(r[0], "DocumentRoot")) {
397 if(r[1] == NULL) {
398 cm_log("Config", "Missing path at line %d", ln);
399 stop = 1;
400 } else {
401 if(current->root != NULL) free(current->root);
402 current->root = cm_strdup(strcmp(r[1], "/") == 0 ? "" : r[1]);
403 }
404 } else if(cm_strcaseequ(r[0], "MIMEType")) {
405 if(r[1] == NULL) {
406 cm_log("Config", "Missing extension at line %d", ln);
407 stop = 1;
408 } else if(r[2] == NULL) {
409 cm_log("Config", "Missing MIME at line %d", ln);
410 stop = 1;
411 } else {
412 struct tw_mime_entry* e = &current->mimes[current->mime_count++];
413 e->ext = cm_strdup(r[1]);
414 e->mime = cm_strdup(r[2]);
415 }
416 } else if(cm_strcaseequ(r[0], "MIMEFile")) {
417 if(r[1] == NULL) {
418 cm_log("Config", "Missing path at line %d", ln);
419 stop = 1;
420 } else {
421 FILE* mimefile = fopen(r[1], "r");
422 if(mimefile == NULL) {
423 cm_log("Config", "Could not load the file at line %d", ln);
424 stop = 1;
425 } else {
426 char* line = malloc(1);
427 int i;
428 struct stat st;
429 char* buf;
430 int incr = 0;
431 stat(r[1], &st);
432
433 buf = malloc(st.st_size + 1);
434 fread(buf, st.st_size, 1, mimefile);
435
436 for(i = 0;; i++) {
437 if(buf[i] == '\n' || buf[i] == 0) {
438 char oldc = buf[i];
439 char* line;
440 buf[i] = 0;
441
442 line = buf + incr;
443
444 if(strlen(line) > 0 && line[0] != '#') {
445 int j;
446 for(j = 0; line[j] != 0; j++) {
447 if(line[j] == ' ' || line[j] == '\t') {
448 line[j] = 0;
449 j++;
450 for(; line[j] != 0; j++) {
451 if(line[j] != ' ' && line[j] != '\t') {
452 char* name = line;
453 char* mimes = line + j;
454 int k = 0;
455 int incr2 = 0;
456 for(k = 0;; k++) {
457 if(mimes[k] == ' ' || mimes[k] == 0) {
458 char oldc2 = mimes[k];
459 struct tw_mime_entry* e;
460 mimes[k] = 0;
461
462 e = &current->mimes[current->mime_count++];
463 e->ext = cm_strcat(".", mimes + incr2);
464 e->mime = cm_strdup(name);
465 if(current->mime_count == MAX_MIME) {
466 cm_log("Config", "Too many MIME types, cannot handle");
467 stop = 1;
468 break;
469 }
470
471 incr2 = k + 1;
472 if(oldc2 == 0) break;
473 }
474 }
475 break;
476 }
477 }
478 break;
479 }
480 if(stop) break;
481 }
482 }
483
484 incr = i + 1;
485 if(oldc == 0) break;
486 }
487 }
488
489 free(buf);
490
491 fclose(mimefile);
492 }
493 }
494 } else if(cm_strcaseequ(r[0], "Icon")) {
495 if(r[1] == NULL) {
496 cm_log("Config", "Missing MIME at line %d", ln);
497 stop = 1;
498 } else if(r[2] == NULL) {
499 cm_log("Config", "Missing path at line %d", ln);
500 stop = 1;
501 } else {
502 struct tw_icon_entry* e = &current->icons[current->icon_count++];
503 e->mime = cm_strdup(r[1]);
504 e->icon = cm_strdup(r[2]);
505 }
506 } else if(cm_strcaseequ(r[0], "LoadModule")) {
507 for(i = 1; r[i] != NULL; i++) {
508 void* mod = tw_module_load(r[i]);
509 if(mod != NULL) {
510 config.modules[config.module_count++] = mod;
511 if(tw_module_init(mod) != 0) {
512 stop = 1;
513 break;
514 }
515 } else {
516 cm_log("Config", "Could not load the module at line %d", ln);
517 stop = 1;
518 break;
519 }
520 }
521 } else if(cm_strcaseequ(r[0], "DirectoryIndex")) {
522 for(i = 1; r[i] != NULL; i++) {
523 current->indexes[current->index_count++] = cm_strdup(r[i]);
524 }
525 } else if(cm_strcaseequ(r[0], "ReadmeFile") || cm_strcaseequ(r[0], "Readme")) {
526 if(cm_strcaseequ(r[0], "Readme")) {
527 cm_force_log("NOTE: Readme directive is deprecated.");
528 }
529 for(i = 1; r[i] != NULL; i++) {
530 current->readmes[current->readme_count++] = cm_strdup(r[i]);
531 }
532 } else {
533 stop = 1;
534 if(r[0] != NULL) {
535 int argc;
536 int i;
537 bool called = false;
538 struct tw_tool tools;
539 for(argc = 0; r[argc] != NULL; argc++)
540 ;
541 stop = 0;
542 tw_init_tools(&tools);
543 for(i = 0; i < config.module_count; i++) {
544 tw_mod_config_t mod_config = (tw_mod_config_t)tw_module_symbol(config.modules[i], "mod_config");
545 int resp;
546 if(mod_config != NULL && (resp = mod_config(&tools, r, argc)) == TW_CONFIG_PARSED) {
547 called = true;
548 break;
549 }
550 if(resp == TW_CONFIG_ERROR) {
551 stop = 1;
552 called = true;
553 break;
554 }
555 }
556 if(!called) {
557 cm_log("Config", "Unknown directive `%s' at line %d", r[0], ln);
558 stop = 1;
559 }
560 }
561 }
562 for(i = 0; r[i] != NULL; i++) free(r[i]);
563 free(r);
564 }
565 free(l);
566 free(line);
567 line = malloc(1);
568 line[0] = 0;
569 if(c <= 0) break;
570 } else if(cbuf[0] != '\r') {
571 char* tmp = line;
572 line = cm_strcat(tmp, cbuf);
573 free(tmp);
574 }
575 }
576 free(line);
577 fclose(f);
578 for(portcount = 0; config.ports[portcount] != -1; portcount++)
579 ;
580 if(portcount == 0) {
581 return 1;
582 } else {
583 return stop;
584 }
585 } else {
586 cm_log("Config", "Could not open the file");
587 return 1;
588 }
589}
Note: See TracBrowser for help on using the repository browser.