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

Last change on this file since 21 was 21, checked in by Nishi, on Sep 14, 2024 at 9:39:39 PM

can send file now

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