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

Last change on this file since 140 was 138, checked in by Nishi, on Sep 23, 2024 at 8:12:46 PM

do not chdir

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