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

Last change on this file since 31 was 24, checked in by Nishi, on Sep 14, 2024 at 11:09:58 PM

redirects properly now

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