[4] | 1 | /* $Id: config.c 138 2024-09-23 11:12:46Z 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] | 21 | struct tw_config config;
|
---|
| 22 |
|
---|
[12] | 23 | struct 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] | 33 | bool 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;
|
---|
| 38 | again:
|
---|
[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] | 69 | void 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);
|
---|
[128] | 92 | config.server_admin = cm_strdup(SERVER_ADMIN);
|
---|
[12] | 93 | gethostname(config.hostname, 1024);
|
---|
[7] | 94 | }
|
---|
[6] | 95 |
|
---|
| 96 | int tw_config_read(const char* path) {
|
---|
[4] | 97 | cm_log("Config", "Reading %s", path);
|
---|
| 98 | char cbuf[2];
|
---|
| 99 | cbuf[1] = 0;
|
---|
[6] | 100 | int ln = 0;
|
---|
[4] | 101 | FILE* f = fopen(path, "r");
|
---|
[6] | 102 | if(f != NULL) {
|
---|
[4] | 103 | char* line = malloc(1);
|
---|
| 104 | line[0] = 0;
|
---|
[6] | 105 | int stop = 0;
|
---|
[12] | 106 | struct tw_config_entry* current = &config.root;
|
---|
[6] | 107 | char* vhost = NULL;
|
---|
[21] | 108 | char* dir = NULL;
|
---|
[6] | 109 | while(stop == 0) {
|
---|
[4] | 110 | int c = fread(cbuf, 1, 1, f);
|
---|
[6] | 111 | if(cbuf[0] == '\n' || c <= 0) {
|
---|
| 112 | ln++;
|
---|
[4] | 113 | char* l = cm_trim(line);
|
---|
[6] | 114 | if(strlen(l) > 0 && l[0] != '#') {
|
---|
[5] | 115 | char** r = cm_split(l, " \t");
|
---|
| 116 | int i;
|
---|
[6] | 117 | if(cm_strcaseequ(r[0], "Include") || cm_strcaseequ(r[0], "IncludeOptional")) {
|
---|
| 118 | for(i = 1; r[i] != NULL; i++) {
|
---|
| 119 | if(tw_config_read(r[i]) != 0 && cm_strcaseequ(r[0], "Include")) {
|
---|
| 120 | stop = 1;
|
---|
| 121 | break;
|
---|
[5] | 122 | }
|
---|
| 123 | }
|
---|
[21] | 124 | } else if(cm_strcaseequ(r[0], "BeginDirectory")) {
|
---|
| 125 | if(dir != NULL) {
|
---|
| 126 | cm_log("Config", "Already in directory section at line %d", ln);
|
---|
| 127 | stop = 1;
|
---|
| 128 | } else {
|
---|
| 129 | if(r[1] == NULL) {
|
---|
| 130 | cm_log("Config", "Missing directory at line %d", ln);
|
---|
| 131 | stop = 1;
|
---|
| 132 | } else {
|
---|
| 133 | dir = cm_strcat(r[1], r[1][strlen(r[1]) - 1] == '/' ? "" : "/");
|
---|
| 134 | }
|
---|
| 135 | }
|
---|
| 136 | } else if(cm_strcaseequ(r[0], "EndDirectory")) {
|
---|
| 137 | if(dir == NULL) {
|
---|
| 138 | cm_log("Config", "Not in directory section at line %d", ln);
|
---|
| 139 | stop = 1;
|
---|
| 140 | } else {
|
---|
| 141 | free(dir);
|
---|
| 142 | dir = NULL;
|
---|
| 143 | }
|
---|
| 144 | } else if(cm_strcaseequ(r[0], "Allow")) {
|
---|
| 145 | if(dir == NULL) {
|
---|
| 146 | cm_log("Config", "Not in directory section at line %d", ln);
|
---|
| 147 | stop = 1;
|
---|
| 148 | } else {
|
---|
| 149 | if(r[1] == NULL) {
|
---|
| 150 | cm_log("Config", "Missing argument at line %d", ln);
|
---|
| 151 | stop = 1;
|
---|
| 152 | } else {
|
---|
| 153 | struct tw_dir_entry* e = ¤t->dirs[current->dir_count++];
|
---|
| 154 | e->name = cm_strdup(r[1]);
|
---|
| 155 | e->dir = cm_strdup(dir);
|
---|
| 156 | e->type = TW_DIR_ALLOW;
|
---|
| 157 | }
|
---|
| 158 | }
|
---|
| 159 | } else if(cm_strcaseequ(r[0], "Deny")) {
|
---|
| 160 | if(dir == NULL) {
|
---|
| 161 | cm_log("Config", "Not in directory section at line %d", ln);
|
---|
| 162 | stop = 1;
|
---|
| 163 | } else {
|
---|
| 164 | if(r[1] == NULL) {
|
---|
| 165 | cm_log("Config", "Missing argument at line %d", ln);
|
---|
| 166 | stop = 1;
|
---|
| 167 | } else {
|
---|
| 168 | struct tw_dir_entry* e = ¤t->dirs[current->dir_count++];
|
---|
| 169 | e->name = cm_strdup(r[1]);
|
---|
| 170 | e->dir = cm_strdup(dir);
|
---|
| 171 | e->type = TW_DIR_DENY;
|
---|
| 172 | }
|
---|
| 173 | }
|
---|
[6] | 174 | } else if(cm_strcaseequ(r[0], "BeginVirtualHost")) {
|
---|
| 175 | if(vhost != NULL) {
|
---|
[12] | 176 | cm_log("Config", "Already in virtual host section at line %d", ln);
|
---|
[6] | 177 | stop = 1;
|
---|
| 178 | } else {
|
---|
| 179 | if(r[1] == NULL) {
|
---|
[12] | 180 | cm_log("Config", "Missing virtual host at line %d", ln);
|
---|
[6] | 181 | stop = 1;
|
---|
| 182 | } else {
|
---|
| 183 | vhost = cm_strdup(r[1]);
|
---|
[12] | 184 | current = &config.vhosts[config.vhost_count++];
|
---|
[21] | 185 | current->dir_count = 0;
|
---|
| 186 | current->mime_count = 0;
|
---|
[22] | 187 | current->icon_count = 0;
|
---|
[24] | 188 | current->index_count = 0;
|
---|
[33] | 189 | current->readme_count = 0;
|
---|
[123] | 190 | current->hideport = -1;
|
---|
[12] | 191 | int i;
|
---|
| 192 | current->name = cm_strdup(vhost);
|
---|
[13] | 193 | current->port = -1;
|
---|
[12] | 194 | for(i = 0; vhost[i] != 0; i++) {
|
---|
| 195 | if(vhost[i] == ':') {
|
---|
| 196 | current->name[i] = 0;
|
---|
| 197 | current->port = atoi(current->name + i + 1);
|
---|
| 198 | break;
|
---|
| 199 | }
|
---|
| 200 | }
|
---|
[6] | 201 | }
|
---|
| 202 | }
|
---|
| 203 | } else if(cm_strcaseequ(r[0], "EndVirtualHost")) {
|
---|
| 204 | if(vhost == NULL) {
|
---|
[12] | 205 | cm_log("Config", "Not in virtual host section at line %d", ln);
|
---|
[6] | 206 | stop = 1;
|
---|
| 207 | } else {
|
---|
| 208 | free(vhost);
|
---|
| 209 | vhost = NULL;
|
---|
[12] | 210 | current = &config.root;
|
---|
[6] | 211 | }
|
---|
[7] | 212 | } else if(cm_strcaseequ(r[0], "Listen") || cm_strcaseequ(r[0], "ListenSSL")) {
|
---|
| 213 | for(i = 1; r[i] != NULL; i++) {
|
---|
| 214 | uint64_t port = atoi(r[i]);
|
---|
| 215 | cm_log("Config", "Going to listen at port %d%s", (int)port, cm_strcaseequ(r[0], "ListenSSL") ? " with SSL" : "");
|
---|
| 216 | if(cm_strcaseequ(r[0], "ListenSSL")) port |= (1ULL << 32);
|
---|
| 217 | int j;
|
---|
| 218 | for(j = 0; config.ports[j] != -1; j++)
|
---|
| 219 | ;
|
---|
| 220 | config.ports[j] = port;
|
---|
| 221 | }
|
---|
[123] | 222 | } else if(cm_strcaseequ(r[0], "HidePort")) {
|
---|
| 223 | current->hideport = 1;
|
---|
| 224 | } else if(cm_strcaseequ(r[0], "ShowPort")) {
|
---|
| 225 | current->hideport = 0;
|
---|
[12] | 226 | } else if(cm_strcaseequ(r[0], "SSLKey")) {
|
---|
| 227 | if(r[1] == NULL) {
|
---|
| 228 | cm_log("Config", "Missing path at line %d", ln);
|
---|
| 229 | stop = 1;
|
---|
| 230 | } else {
|
---|
| 231 | if(current->sslkey != NULL) free(current->sslkey);
|
---|
| 232 | current->sslkey = cm_strdup(r[1]);
|
---|
| 233 | }
|
---|
| 234 | } else if(cm_strcaseequ(r[0], "SSLCertificate")) {
|
---|
| 235 | if(r[1] == NULL) {
|
---|
| 236 | cm_log("Config", "Missing path at line %d", ln);
|
---|
| 237 | stop = 1;
|
---|
| 238 | } else {
|
---|
| 239 | if(current->sslcert != NULL) free(current->sslcert);
|
---|
| 240 | current->sslcert = cm_strdup(r[1]);
|
---|
| 241 | }
|
---|
[61] | 242 | } else if(cm_strcaseequ(r[0], "ServerRoot")) {
|
---|
| 243 | if(r[1] == NULL) {
|
---|
| 244 | cm_log("Config", "Missing path at line %d", ln);
|
---|
| 245 | stop = 1;
|
---|
| 246 | } else {
|
---|
| 247 | chdir(r[1]);
|
---|
[127] | 248 | free(config.server_root);
|
---|
| 249 | config.server_root = cm_strdup(r[1]);
|
---|
[61] | 250 | }
|
---|
[128] | 251 | } else if(cm_strcaseequ(r[0], "ServerAdmin")) {
|
---|
| 252 | if(r[1] == NULL) {
|
---|
| 253 | cm_log("Config", "Missing email at line %d", ln);
|
---|
| 254 | stop = 1;
|
---|
| 255 | } else {
|
---|
| 256 | free(config.server_admin);
|
---|
| 257 | config.server_admin = cm_strdup(r[1]);
|
---|
| 258 | }
|
---|
[19] | 259 | } else if(cm_strcaseequ(r[0], "DocumentRoot")) {
|
---|
| 260 | if(r[1] == NULL) {
|
---|
| 261 | cm_log("Config", "Missing path at line %d", ln);
|
---|
| 262 | stop = 1;
|
---|
| 263 | } else {
|
---|
| 264 | if(current->root != NULL) free(current->root);
|
---|
[21] | 265 | current->root = cm_strdup(strcmp(r[1], "/") == 0 ? "" : r[1]);
|
---|
[19] | 266 | }
|
---|
[17] | 267 | } else if(cm_strcaseequ(r[0], "ServerRoot")) {
|
---|
| 268 | if(r[1] == NULL) {
|
---|
| 269 | cm_log("Config", "Missing path at line %d", ln);
|
---|
| 270 | stop = 1;
|
---|
| 271 | } else {
|
---|
| 272 | if(config.server_root != NULL) free(config.server_root);
|
---|
| 273 | config.server_root = cm_strdup(r[1]);
|
---|
| 274 | }
|
---|
[21] | 275 | } else if(cm_strcaseequ(r[0], "MIMEType")) {
|
---|
| 276 | if(r[1] == NULL) {
|
---|
| 277 | cm_log("Config", "Missing extension at line %d", ln);
|
---|
| 278 | stop = 1;
|
---|
[22] | 279 | } else if(r[2] == NULL) {
|
---|
[21] | 280 | cm_log("Config", "Missing MIME at line %d", ln);
|
---|
| 281 | stop = 1;
|
---|
| 282 | } else {
|
---|
| 283 | struct tw_mime_entry* e = ¤t->mimes[current->mime_count++];
|
---|
| 284 | e->ext = cm_strdup(r[1]);
|
---|
| 285 | e->mime = cm_strdup(r[2]);
|
---|
| 286 | }
|
---|
[22] | 287 | } else if(cm_strcaseequ(r[0], "Icon")) {
|
---|
| 288 | if(r[1] == NULL) {
|
---|
| 289 | cm_log("Config", "Missing MIME at line %d", ln);
|
---|
| 290 | stop = 1;
|
---|
| 291 | } else if(r[2] == NULL) {
|
---|
| 292 | cm_log("Config", "Missing path at line %d", ln);
|
---|
| 293 | stop = 1;
|
---|
| 294 | } else {
|
---|
| 295 | struct tw_icon_entry* e = ¤t->icons[current->icon_count++];
|
---|
| 296 | e->mime = cm_strdup(r[1]);
|
---|
| 297 | e->icon = cm_strdup(r[2]);
|
---|
| 298 | }
|
---|
[17] | 299 | } else if(cm_strcaseequ(r[0], "LoadModule")) {
|
---|
| 300 | for(i = 1; r[i] != NULL; i++) {
|
---|
| 301 | void* mod = tw_module_load(r[i]);
|
---|
| 302 | if(mod != NULL) {
|
---|
[18] | 303 | config.modules[config.module_count++] = mod;
|
---|
[17] | 304 | if(tw_module_init(mod) != 0) {
|
---|
| 305 | stop = 1;
|
---|
| 306 | break;
|
---|
| 307 | }
|
---|
| 308 | } else {
|
---|
[127] | 309 | cm_log("Config", "Could not load the module at line %d", ln);
|
---|
[17] | 310 | stop = 1;
|
---|
| 311 | break;
|
---|
| 312 | }
|
---|
| 313 | }
|
---|
[24] | 314 | } else if(cm_strcaseequ(r[0], "DirectoryIndex")) {
|
---|
| 315 | for(i = 1; r[i] != NULL; i++) {
|
---|
| 316 | current->indexes[current->index_count++] = cm_strdup(r[i]);
|
---|
| 317 | }
|
---|
[33] | 318 | } else if(cm_strcaseequ(r[0], "Readme")) {
|
---|
| 319 | for(i = 1; r[i] != NULL; i++) {
|
---|
| 320 | current->readmes[current->readme_count++] = cm_strdup(r[i]);
|
---|
| 321 | }
|
---|
[6] | 322 | } else {
|
---|
[39] | 323 | stop = 1;
|
---|
[6] | 324 | if(r[0] != NULL) {
|
---|
[39] | 325 | int argc;
|
---|
| 326 | for(argc = 0; r[argc] != NULL; argc++)
|
---|
| 327 | ;
|
---|
| 328 | stop = 0;
|
---|
| 329 | int i;
|
---|
| 330 | bool called = false;
|
---|
| 331 | struct tw_tool tools;
|
---|
| 332 | tw_init_tools(&tools);
|
---|
| 333 | for(i = 0; i < config.module_count; i++) {
|
---|
| 334 | tw_mod_config_t mod_config = (tw_mod_config_t)tw_module_symbol(config.modules[i], "mod_config");
|
---|
| 335 | int resp;
|
---|
| 336 | if(mod_config != NULL && (resp = mod_config(&tools, r, argc)) == TW_CONFIG_PARSED) {
|
---|
| 337 | called = true;
|
---|
| 338 | break;
|
---|
| 339 | }
|
---|
| 340 | if(resp == TW_CONFIG_ERROR) {
|
---|
| 341 | stop = 1;
|
---|
| 342 | called = true;
|
---|
| 343 | break;
|
---|
| 344 | }
|
---|
| 345 | }
|
---|
| 346 | if(!called) {
|
---|
| 347 | cm_log("Config", "Unknown directive `%s' at line %d", r[0], ln);
|
---|
| 348 | stop = 1;
|
---|
| 349 | }
|
---|
[6] | 350 | }
|
---|
[5] | 351 | }
|
---|
| 352 | for(i = 0; r[i] != NULL; i++) free(r[i]);
|
---|
| 353 | free(r);
|
---|
[4] | 354 | }
|
---|
| 355 | free(l);
|
---|
| 356 | free(line);
|
---|
| 357 | line = malloc(1);
|
---|
| 358 | line[0] = 0;
|
---|
| 359 | if(c <= 0) break;
|
---|
[6] | 360 | } else if(cbuf[0] != '\r') {
|
---|
[4] | 361 | char* tmp = line;
|
---|
| 362 | line = cm_strcat(tmp, cbuf);
|
---|
| 363 | free(tmp);
|
---|
| 364 | }
|
---|
| 365 | }
|
---|
| 366 | free(line);
|
---|
| 367 | fclose(f);
|
---|
[6] | 368 | return stop;
|
---|
| 369 | } else {
|
---|
[5] | 370 | cm_log("Config", "Could not open the file");
|
---|
[4] | 371 | return 1;
|
---|
| 372 | }
|
---|
| 373 | }
|
---|