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

Last change on this file since 36 was 33, checked in by Nishi, on Sep 16, 2024 at 9:52:47 PM

can show readme on index now

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