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

Last change on this file since 123 was 123, checked in by Nishi, on Sep 22, 2024 at 10:43:47 PM

add ShowPort/HidePort

  • Property svn:keywords set to Id
File size: 10.1 KB
RevLine 
[4]1/* $Id: config.c 123 2024-09-22 13:43:47Z nishi $ */
2
[16]3#define SOURCE
4
[4]5#include "tw_config.h"
[17]6#include "tw_module.h"
[4]7
8#include <stdio.h>
[7]9#include <stdint.h>
[4]10#include <stdlib.h>
11#include <string.h>
[12]12#include <unistd.h>
[4]13
[20]14#ifdef __MINGW32__
15#include <winsock2.h>
16#endif
17
[4]18#include <cm_string.h>
19#include <cm_log.h>
20
[6]21struct tw_config config;
22
[12]23struct tw_config_entry* tw_vhost_match(const char* name, int port) {
24 int i;
25 for(i = 0; i < config.vhost_count; i++) {
[13]26 if(strcmp(config.vhosts[i].name, name) == 0 && (config.vhosts[i].port == -1 ? 1 : config.vhosts[i].port == port)) {
[12]27 return &config.vhosts[i];
28 }
29 }
30 return &config.root;
31}
32
[22]33bool tw_permission_allowed(const char* path, SOCKADDR addr, struct tw_http_request req, struct tw_config_entry* vhost) {
[21]34 int i;
35 bool found = false;
36 bool pathstart = false;
37 bool perm = false;
38again:
[22]39 for(i = 0; i < vhost->dir_count; i++) {
[21]40 struct tw_dir_entry* e = &vhost->dirs[i];
41 pathstart = false;
[22]42 if(strlen(path) >= strlen(e->dir)) {
[21]43 pathstart = true;
44 int j;
[22]45 for(j = 0; path[j] != 0 && e->dir[j] != 0; j++) {
46 if(path[j] != e->dir[j]) {
[21]47 pathstart = false;
48 break;
49 }
50 }
51 }
52 char* noslash = cm_strdup(e->dir);
53 noslash[strlen(noslash) - 1] = 0;
[22]54 if(strcmp(e->dir, path) == 0 || strcmp(noslash, path) == 0 || pathstart) {
[21]55 found = true;
[22]56 if(strcmp(e->name, "all") == 0) {
[21]57 perm = e->type == TW_DIR_ALLOW;
58 }
59 }
60 free(noslash);
61 }
[22]62 if(!found && vhost != &config.root) {
[21]63 vhost = &config.root;
64 goto again;
65 }
66 return perm;
67}
68
[7]69void tw_config_init(void) {
70 int i;
71 for(i = 0; i < MAX_PORTS + 1; i++) {
72 config.ports[i] = -1;
73 }
[12]74 for(i = 0; i < MAX_VHOSTS; i++) {
75 config.vhosts[i].sslkey = NULL;
76 config.vhosts[i].sslcert = NULL;
[19]77 config.vhosts[i].root = NULL;
[12]78 }
79 config.root.sslkey = NULL;
80 config.root.sslcert = NULL;
[19]81 config.root.root = NULL;
[21]82 config.root.mime_count = 0;
83 config.root.dir_count = 0;
[22]84 config.root.icon_count = 0;
[24]85 config.root.index_count = 0;
[33]86 config.root.readme_count = 0;
[123]87 config.root.hideport = 0;
[12]88 config.vhost_count = 0;
[18]89 config.module_count = 0;
90 config.extension = NULL;
[17]91 config.server_root = cm_strdup(PREFIX);
[12]92 gethostname(config.hostname, 1024);
[7]93}
[6]94
95int tw_config_read(const char* path) {
[4]96 cm_log("Config", "Reading %s", path);
97 char cbuf[2];
98 cbuf[1] = 0;
[6]99 int ln = 0;
[4]100 FILE* f = fopen(path, "r");
[6]101 if(f != NULL) {
[4]102 char* line = malloc(1);
103 line[0] = 0;
[6]104 int stop = 0;
[12]105 struct tw_config_entry* current = &config.root;
[6]106 char* vhost = NULL;
[21]107 char* dir = NULL;
[6]108 while(stop == 0) {
[4]109 int c = fread(cbuf, 1, 1, f);
[6]110 if(cbuf[0] == '\n' || c <= 0) {
111 ln++;
[4]112 char* l = cm_trim(line);
[6]113 if(strlen(l) > 0 && l[0] != '#') {
[5]114 char** r = cm_split(l, " \t");
115 int i;
[6]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;
[5]121 }
122 }
[21]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 }
[6]173 } else if(cm_strcaseequ(r[0], "BeginVirtualHost")) {
174 if(vhost != NULL) {
[12]175 cm_log("Config", "Already in virtual host section at line %d", ln);
[6]176 stop = 1;
177 } else {
178 if(r[1] == NULL) {
[12]179 cm_log("Config", "Missing virtual host at line %d", ln);
[6]180 stop = 1;
181 } else {
182 vhost = cm_strdup(r[1]);
[12]183 current = &config.vhosts[config.vhost_count++];
[21]184 current->dir_count = 0;
185 current->mime_count = 0;
[22]186 current->icon_count = 0;
[24]187 current->index_count = 0;
[33]188 current->readme_count = 0;
[123]189 current->hideport = -1;
[12]190 int i;
191 current->name = cm_strdup(vhost);
[13]192 current->port = -1;
[12]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 }
[6]200 }
201 }
202 } else if(cm_strcaseequ(r[0], "EndVirtualHost")) {
203 if(vhost == NULL) {
[12]204 cm_log("Config", "Not in virtual host section at line %d", ln);
[6]205 stop = 1;
206 } else {
207 free(vhost);
208 vhost = NULL;
[12]209 current = &config.root;
[6]210 }
[7]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 }
[123]221 } else if(cm_strcaseequ(r[0], "HidePort")) {
222 current->hideport = 1;
223 } else if(cm_strcaseequ(r[0], "ShowPort")) {
224 current->hideport = 0;
[12]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 }
[61]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 }
[19]248 } else if(cm_strcaseequ(r[0], "DocumentRoot")) {
249 if(r[1] == NULL) {
250 cm_log("Config", "Missing path at line %d", ln);
251 stop = 1;
252 } else {
253 if(current->root != NULL) free(current->root);
[21]254 current->root = cm_strdup(strcmp(r[1], "/") == 0 ? "" : r[1]);
[19]255 }
[17]256 } else if(cm_strcaseequ(r[0], "ServerRoot")) {
257 if(r[1] == NULL) {
258 cm_log("Config", "Missing path at line %d", ln);
259 stop = 1;
260 } else {
261 if(config.server_root != NULL) free(config.server_root);
262 config.server_root = cm_strdup(r[1]);
263 }
[21]264 } else if(cm_strcaseequ(r[0], "MIMEType")) {
265 if(r[1] == NULL) {
266 cm_log("Config", "Missing extension at line %d", ln);
267 stop = 1;
[22]268 } else if(r[2] == NULL) {
[21]269 cm_log("Config", "Missing MIME at line %d", ln);
270 stop = 1;
271 } else {
272 struct tw_mime_entry* e = &current->mimes[current->mime_count++];
273 e->ext = cm_strdup(r[1]);
274 e->mime = cm_strdup(r[2]);
275 }
[22]276 } else if(cm_strcaseequ(r[0], "Icon")) {
277 if(r[1] == NULL) {
278 cm_log("Config", "Missing MIME at line %d", ln);
279 stop = 1;
280 } else if(r[2] == NULL) {
281 cm_log("Config", "Missing path at line %d", ln);
282 stop = 1;
283 } else {
284 struct tw_icon_entry* e = &current->icons[current->icon_count++];
285 e->mime = cm_strdup(r[1]);
286 e->icon = cm_strdup(r[2]);
287 }
[17]288 } else if(cm_strcaseequ(r[0], "LoadModule")) {
289 for(i = 1; r[i] != NULL; i++) {
290 void* mod = tw_module_load(r[i]);
291 if(mod != NULL) {
[18]292 config.modules[config.module_count++] = mod;
[17]293 if(tw_module_init(mod) != 0) {
294 stop = 1;
295 break;
296 }
297 } else {
298 stop = 1;
299 break;
300 }
301 }
[24]302 } else if(cm_strcaseequ(r[0], "DirectoryIndex")) {
303 for(i = 1; r[i] != NULL; i++) {
304 current->indexes[current->index_count++] = cm_strdup(r[i]);
305 }
[33]306 } else if(cm_strcaseequ(r[0], "Readme")) {
307 for(i = 1; r[i] != NULL; i++) {
308 current->readmes[current->readme_count++] = cm_strdup(r[i]);
309 }
[6]310 } else {
[39]311 stop = 1;
[6]312 if(r[0] != NULL) {
[39]313 int argc;
314 for(argc = 0; r[argc] != NULL; argc++)
315 ;
316 stop = 0;
317 int i;
318 bool called = false;
319 struct tw_tool tools;
320 tw_init_tools(&tools);
321 for(i = 0; i < config.module_count; i++) {
322 tw_mod_config_t mod_config = (tw_mod_config_t)tw_module_symbol(config.modules[i], "mod_config");
323 int resp;
324 if(mod_config != NULL && (resp = mod_config(&tools, r, argc)) == TW_CONFIG_PARSED) {
325 called = true;
326 break;
327 }
328 if(resp == TW_CONFIG_ERROR) {
329 stop = 1;
330 called = true;
331 break;
332 }
333 }
334 if(!called) {
335 cm_log("Config", "Unknown directive `%s' at line %d", r[0], ln);
336 stop = 1;
337 }
[6]338 }
[5]339 }
340 for(i = 0; r[i] != NULL; i++) free(r[i]);
341 free(r);
[4]342 }
343 free(l);
344 free(line);
345 line = malloc(1);
346 line[0] = 0;
347 if(c <= 0) break;
[6]348 } else if(cbuf[0] != '\r') {
[4]349 char* tmp = line;
350 line = cm_strcat(tmp, cbuf);
351 free(tmp);
352 }
353 }
354 free(line);
355 fclose(f);
[6]356 return stop;
357 } else {
[5]358 cm_log("Config", "Could not open the file");
[4]359 return 1;
360 }
361}
Note: See TracBrowser for help on using the repository browser.