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