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

Last change on this file since 22 was 22, checked in by Nishi, on Sep 14, 2024 at 10:25:38 PM

icon works

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