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

Last change on this file since 187 was 187, checked in by Nishi, on Sep 28, 2024 at 9:07:34 AM

kinda works

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