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

Last change on this file since 163 was 161, checked in by Nishi, on Sep 25, 2024 at 10:35:04 PM

adding chroot

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