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

Last change on this file since 127 was 127, checked in by Nishi, on Sep 23, 2024 at 6:39:28 PM

fix some stuff

  • Property svn:keywords set to Id
File size: 10.2 KB
Line 
1/* $Id: config.c 127 2024-09-23 09:39:28Z 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 config.vhosts[i].sslkey = NULL;
76 config.vhosts[i].sslcert = NULL;
77 config.vhosts[i].root = NULL;
78 }
79 config.root.sslkey = NULL;
80 config.root.sslcert = NULL;
81 config.root.root = NULL;
82 config.root.mime_count = 0;
83 config.root.dir_count = 0;
84 config.root.icon_count = 0;
85 config.root.index_count = 0;
86 config.root.readme_count = 0;
87 config.root.hideport = 0;
88 config.vhost_count = 0;
89 config.module_count = 0;
90 config.extension = NULL;
91 config.server_root = cm_strdup(PREFIX);
92 gethostname(config.hostname, 1024);
93}
94
95int tw_config_read(const char* path) {
96 cm_log("Config", "Reading %s", path);
97 char cbuf[2];
98 cbuf[1] = 0;
99 int ln = 0;
100 FILE* f = fopen(path, "r");
101 if(f != NULL) {
102 char* line = malloc(1);
103 line[0] = 0;
104 int stop = 0;
105 struct tw_config_entry* current = &config.root;
106 char* vhost = NULL;
107 char* dir = NULL;
108 while(stop == 0) {
109 int c = fread(cbuf, 1, 1, f);
110 if(cbuf[0] == '\n' || c <= 0) {
111 ln++;
112 char* l = cm_trim(line);
113 if(strlen(l) > 0 && l[0] != '#') {
114 char** r = cm_split(l, " \t");
115 int i;
116 if(cm_strcaseequ(r[0], "Include") || cm_strcaseequ(r[0], "IncludeOptional")) {
117 for(i = 1; r[i] != NULL; i++) {
118 if(tw_config_read(r[i]) != 0 && cm_strcaseequ(r[0], "Include")) {
119 stop = 1;
120 break;
121 }
122 }
123 } else if(cm_strcaseequ(r[0], "BeginDirectory")) {
124 if(dir != NULL) {
125 cm_log("Config", "Already in directory section at line %d", ln);
126 stop = 1;
127 } else {
128 if(r[1] == NULL) {
129 cm_log("Config", "Missing directory at line %d", ln);
130 stop = 1;
131 } else {
132 dir = cm_strcat(r[1], r[1][strlen(r[1]) - 1] == '/' ? "" : "/");
133 }
134 }
135 } else if(cm_strcaseequ(r[0], "EndDirectory")) {
136 if(dir == NULL) {
137 cm_log("Config", "Not in directory section at line %d", ln);
138 stop = 1;
139 } else {
140 free(dir);
141 dir = NULL;
142 }
143 } else if(cm_strcaseequ(r[0], "Allow")) {
144 if(dir == NULL) {
145 cm_log("Config", "Not in directory section at line %d", ln);
146 stop = 1;
147 } else {
148 if(r[1] == NULL) {
149 cm_log("Config", "Missing argument at line %d", ln);
150 stop = 1;
151 } else {
152 struct tw_dir_entry* e = &current->dirs[current->dir_count++];
153 e->name = cm_strdup(r[1]);
154 e->dir = cm_strdup(dir);
155 e->type = TW_DIR_ALLOW;
156 }
157 }
158 } else if(cm_strcaseequ(r[0], "Deny")) {
159 if(dir == NULL) {
160 cm_log("Config", "Not in directory section at line %d", ln);
161 stop = 1;
162 } else {
163 if(r[1] == NULL) {
164 cm_log("Config", "Missing argument at line %d", ln);
165 stop = 1;
166 } else {
167 struct tw_dir_entry* e = &current->dirs[current->dir_count++];
168 e->name = cm_strdup(r[1]);
169 e->dir = cm_strdup(dir);
170 e->type = TW_DIR_DENY;
171 }
172 }
173 } else if(cm_strcaseequ(r[0], "BeginVirtualHost")) {
174 if(vhost != NULL) {
175 cm_log("Config", "Already in virtual host section at line %d", ln);
176 stop = 1;
177 } else {
178 if(r[1] == NULL) {
179 cm_log("Config", "Missing virtual host at line %d", ln);
180 stop = 1;
181 } else {
182 vhost = cm_strdup(r[1]);
183 current = &config.vhosts[config.vhost_count++];
184 current->dir_count = 0;
185 current->mime_count = 0;
186 current->icon_count = 0;
187 current->index_count = 0;
188 current->readme_count = 0;
189 current->hideport = -1;
190 int i;
191 current->name = cm_strdup(vhost);
192 current->port = -1;
193 for(i = 0; vhost[i] != 0; i++) {
194 if(vhost[i] == ':') {
195 current->name[i] = 0;
196 current->port = atoi(current->name + i + 1);
197 break;
198 }
199 }
200 }
201 }
202 } else if(cm_strcaseequ(r[0], "EndVirtualHost")) {
203 if(vhost == NULL) {
204 cm_log("Config", "Not in virtual host section at line %d", ln);
205 stop = 1;
206 } else {
207 free(vhost);
208 vhost = NULL;
209 current = &config.root;
210 }
211 } else if(cm_strcaseequ(r[0], "Listen") || cm_strcaseequ(r[0], "ListenSSL")) {
212 for(i = 1; r[i] != NULL; i++) {
213 uint64_t port = atoi(r[i]);
214 cm_log("Config", "Going to listen at port %d%s", (int)port, cm_strcaseequ(r[0], "ListenSSL") ? " with SSL" : "");
215 if(cm_strcaseequ(r[0], "ListenSSL")) port |= (1ULL << 32);
216 int j;
217 for(j = 0; config.ports[j] != -1; j++)
218 ;
219 config.ports[j] = port;
220 }
221 } else if(cm_strcaseequ(r[0], "HidePort")) {
222 current->hideport = 1;
223 } else if(cm_strcaseequ(r[0], "ShowPort")) {
224 current->hideport = 0;
225 } else if(cm_strcaseequ(r[0], "SSLKey")) {
226 if(r[1] == NULL) {
227 cm_log("Config", "Missing path at line %d", ln);
228 stop = 1;
229 } else {
230 if(current->sslkey != NULL) free(current->sslkey);
231 current->sslkey = cm_strdup(r[1]);
232 }
233 } else if(cm_strcaseequ(r[0], "SSLCertificate")) {
234 if(r[1] == NULL) {
235 cm_log("Config", "Missing path at line %d", ln);
236 stop = 1;
237 } else {
238 if(current->sslcert != NULL) free(current->sslcert);
239 current->sslcert = cm_strdup(r[1]);
240 }
241 } else if(cm_strcaseequ(r[0], "ServerRoot")) {
242 if(r[1] == NULL) {
243 cm_log("Config", "Missing path at line %d", ln);
244 stop = 1;
245 } else {
246 chdir(r[1]);
247 free(config.server_root);
248 config.server_root = cm_strdup(r[1]);
249 }
250 } else if(cm_strcaseequ(r[0], "DocumentRoot")) {
251 if(r[1] == NULL) {
252 cm_log("Config", "Missing path at line %d", ln);
253 stop = 1;
254 } else {
255 if(current->root != NULL) free(current->root);
256 current->root = cm_strdup(strcmp(r[1], "/") == 0 ? "" : r[1]);
257 }
258 } else if(cm_strcaseequ(r[0], "ServerRoot")) {
259 if(r[1] == NULL) {
260 cm_log("Config", "Missing path at line %d", ln);
261 stop = 1;
262 } else {
263 if(config.server_root != NULL) free(config.server_root);
264 config.server_root = cm_strdup(r[1]);
265 }
266 } else if(cm_strcaseequ(r[0], "MIMEType")) {
267 if(r[1] == NULL) {
268 cm_log("Config", "Missing extension at line %d", ln);
269 stop = 1;
270 } else if(r[2] == NULL) {
271 cm_log("Config", "Missing MIME at line %d", ln);
272 stop = 1;
273 } else {
274 struct tw_mime_entry* e = &current->mimes[current->mime_count++];
275 e->ext = cm_strdup(r[1]);
276 e->mime = cm_strdup(r[2]);
277 }
278 } else if(cm_strcaseequ(r[0], "Icon")) {
279 if(r[1] == NULL) {
280 cm_log("Config", "Missing MIME at line %d", ln);
281 stop = 1;
282 } else if(r[2] == NULL) {
283 cm_log("Config", "Missing path at line %d", ln);
284 stop = 1;
285 } else {
286 struct tw_icon_entry* e = &current->icons[current->icon_count++];
287 e->mime = cm_strdup(r[1]);
288 e->icon = cm_strdup(r[2]);
289 }
290 } else if(cm_strcaseequ(r[0], "LoadModule")) {
291 for(i = 1; r[i] != NULL; i++) {
292 void* mod = tw_module_load(r[i]);
293 if(mod != NULL) {
294 config.modules[config.module_count++] = mod;
295 if(tw_module_init(mod) != 0) {
296 stop = 1;
297 break;
298 }
299 } else {
300 cm_log("Config", "Could not load the module at line %d", ln);
301 stop = 1;
302 break;
303 }
304 }
305 } else if(cm_strcaseequ(r[0], "DirectoryIndex")) {
306 for(i = 1; r[i] != NULL; i++) {
307 current->indexes[current->index_count++] = cm_strdup(r[i]);
308 }
309 } else if(cm_strcaseequ(r[0], "Readme")) {
310 for(i = 1; r[i] != NULL; i++) {
311 current->readmes[current->readme_count++] = cm_strdup(r[i]);
312 }
313 } else {
314 stop = 1;
315 if(r[0] != NULL) {
316 int argc;
317 for(argc = 0; r[argc] != NULL; argc++)
318 ;
319 stop = 0;
320 int i;
321 bool called = false;
322 struct tw_tool tools;
323 tw_init_tools(&tools);
324 for(i = 0; i < config.module_count; i++) {
325 tw_mod_config_t mod_config = (tw_mod_config_t)tw_module_symbol(config.modules[i], "mod_config");
326 int resp;
327 if(mod_config != NULL && (resp = mod_config(&tools, r, argc)) == TW_CONFIG_PARSED) {
328 called = true;
329 break;
330 }
331 if(resp == TW_CONFIG_ERROR) {
332 stop = 1;
333 called = true;
334 break;
335 }
336 }
337 if(!called) {
338 cm_log("Config", "Unknown directive `%s' at line %d", r[0], ln);
339 stop = 1;
340 }
341 }
342 }
343 for(i = 0; r[i] != NULL; i++) free(r[i]);
344 free(r);
345 }
346 free(l);
347 free(line);
348 line = malloc(1);
349 line[0] = 0;
350 if(c <= 0) break;
351 } else if(cbuf[0] != '\r') {
352 char* tmp = line;
353 line = cm_strcat(tmp, cbuf);
354 free(tmp);
355 }
356 }
357 free(line);
358 fclose(f);
359 return stop;
360 } else {
361 cm_log("Config", "Could not open the file");
362 return 1;
363 }
364}
Note: See TracBrowser for help on using the repository browser.