[4] | 1 | /* $Id: config.c 61 2024-09-18 12:45:02Z 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;
|
---|
[12] | 87 | config.vhost_count = 0;
|
---|
[18] | 88 | config.module_count = 0;
|
---|
| 89 | config.extension = NULL;
|
---|
[17] | 90 | config.server_root = cm_strdup(PREFIX);
|
---|
[12] | 91 | gethostname(config.hostname, 1024);
|
---|
[7] | 92 | }
|
---|
[6] | 93 |
|
---|
| 94 | int tw_config_read(const char* path) {
|
---|
[4] | 95 | cm_log("Config", "Reading %s", path);
|
---|
| 96 | char cbuf[2];
|
---|
| 97 | cbuf[1] = 0;
|
---|
[6] | 98 | int ln = 0;
|
---|
[4] | 99 | FILE* f = fopen(path, "r");
|
---|
[6] | 100 | if(f != NULL) {
|
---|
[4] | 101 | char* line = malloc(1);
|
---|
| 102 | line[0] = 0;
|
---|
[6] | 103 | int stop = 0;
|
---|
[12] | 104 | struct tw_config_entry* current = &config.root;
|
---|
[6] | 105 | char* vhost = NULL;
|
---|
[21] | 106 | char* dir = NULL;
|
---|
[6] | 107 | while(stop == 0) {
|
---|
[4] | 108 | int c = fread(cbuf, 1, 1, f);
|
---|
[6] | 109 | if(cbuf[0] == '\n' || c <= 0) {
|
---|
| 110 | ln++;
|
---|
[4] | 111 | char* l = cm_trim(line);
|
---|
[6] | 112 | if(strlen(l) > 0 && l[0] != '#') {
|
---|
[5] | 113 | char** r = cm_split(l, " \t");
|
---|
| 114 | int i;
|
---|
[6] | 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;
|
---|
[5] | 120 | }
|
---|
| 121 | }
|
---|
[21] | 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 = ¤t->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 = ¤t->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 | }
|
---|
[6] | 172 | } else if(cm_strcaseequ(r[0], "BeginVirtualHost")) {
|
---|
| 173 | if(vhost != NULL) {
|
---|
[12] | 174 | cm_log("Config", "Already in virtual host section at line %d", ln);
|
---|
[6] | 175 | stop = 1;
|
---|
| 176 | } else {
|
---|
| 177 | if(r[1] == NULL) {
|
---|
[12] | 178 | cm_log("Config", "Missing virtual host at line %d", ln);
|
---|
[6] | 179 | stop = 1;
|
---|
| 180 | } else {
|
---|
| 181 | vhost = cm_strdup(r[1]);
|
---|
[12] | 182 | current = &config.vhosts[config.vhost_count++];
|
---|
[21] | 183 | current->dir_count = 0;
|
---|
| 184 | current->mime_count = 0;
|
---|
[22] | 185 | current->icon_count = 0;
|
---|
[24] | 186 | current->index_count = 0;
|
---|
[33] | 187 | current->readme_count = 0;
|
---|
[12] | 188 | int i;
|
---|
| 189 | current->name = cm_strdup(vhost);
|
---|
[13] | 190 | current->port = -1;
|
---|
[12] | 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 | }
|
---|
[6] | 198 | }
|
---|
| 199 | }
|
---|
| 200 | } else if(cm_strcaseequ(r[0], "EndVirtualHost")) {
|
---|
| 201 | if(vhost == NULL) {
|
---|
[12] | 202 | cm_log("Config", "Not in virtual host section at line %d", ln);
|
---|
[6] | 203 | stop = 1;
|
---|
| 204 | } else {
|
---|
| 205 | free(vhost);
|
---|
| 206 | vhost = NULL;
|
---|
[12] | 207 | current = &config.root;
|
---|
[6] | 208 | }
|
---|
[7] | 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 | }
|
---|
[12] | 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 | }
|
---|
[61] | 235 | } else if(cm_strcaseequ(r[0], "ServerRoot")) {
|
---|
| 236 | if(r[1] == NULL) {
|
---|
| 237 | cm_log("Config", "Missing path at line %d", ln);
|
---|
| 238 | stop = 1;
|
---|
| 239 | } else {
|
---|
| 240 | chdir(r[1]);
|
---|
| 241 | }
|
---|
[19] | 242 | } else if(cm_strcaseequ(r[0], "DocumentRoot")) {
|
---|
| 243 | if(r[1] == NULL) {
|
---|
| 244 | cm_log("Config", "Missing path at line %d", ln);
|
---|
| 245 | stop = 1;
|
---|
| 246 | } else {
|
---|
| 247 | if(current->root != NULL) free(current->root);
|
---|
[21] | 248 | current->root = cm_strdup(strcmp(r[1], "/") == 0 ? "" : r[1]);
|
---|
[19] | 249 | }
|
---|
[17] | 250 | } else if(cm_strcaseequ(r[0], "ServerRoot")) {
|
---|
| 251 | if(r[1] == NULL) {
|
---|
| 252 | cm_log("Config", "Missing path at line %d", ln);
|
---|
| 253 | stop = 1;
|
---|
| 254 | } else {
|
---|
| 255 | if(config.server_root != NULL) free(config.server_root);
|
---|
| 256 | config.server_root = cm_strdup(r[1]);
|
---|
| 257 | }
|
---|
[21] | 258 | } else if(cm_strcaseequ(r[0], "MIMEType")) {
|
---|
| 259 | if(r[1] == NULL) {
|
---|
| 260 | cm_log("Config", "Missing extension at line %d", ln);
|
---|
| 261 | stop = 1;
|
---|
[22] | 262 | } else if(r[2] == NULL) {
|
---|
[21] | 263 | cm_log("Config", "Missing MIME at line %d", ln);
|
---|
| 264 | stop = 1;
|
---|
| 265 | } else {
|
---|
| 266 | struct tw_mime_entry* e = ¤t->mimes[current->mime_count++];
|
---|
| 267 | e->ext = cm_strdup(r[1]);
|
---|
| 268 | e->mime = cm_strdup(r[2]);
|
---|
| 269 | }
|
---|
[22] | 270 | } else if(cm_strcaseequ(r[0], "Icon")) {
|
---|
| 271 | if(r[1] == NULL) {
|
---|
| 272 | cm_log("Config", "Missing MIME at line %d", ln);
|
---|
| 273 | stop = 1;
|
---|
| 274 | } else if(r[2] == NULL) {
|
---|
| 275 | cm_log("Config", "Missing path at line %d", ln);
|
---|
| 276 | stop = 1;
|
---|
| 277 | } else {
|
---|
| 278 | struct tw_icon_entry* e = ¤t->icons[current->icon_count++];
|
---|
| 279 | e->mime = cm_strdup(r[1]);
|
---|
| 280 | e->icon = cm_strdup(r[2]);
|
---|
| 281 | }
|
---|
[17] | 282 | } else if(cm_strcaseequ(r[0], "LoadModule")) {
|
---|
| 283 | for(i = 1; r[i] != NULL; i++) {
|
---|
| 284 | void* mod = tw_module_load(r[i]);
|
---|
| 285 | if(mod != NULL) {
|
---|
[18] | 286 | config.modules[config.module_count++] = mod;
|
---|
[17] | 287 | if(tw_module_init(mod) != 0) {
|
---|
| 288 | stop = 1;
|
---|
| 289 | break;
|
---|
| 290 | }
|
---|
| 291 | } else {
|
---|
| 292 | stop = 1;
|
---|
| 293 | break;
|
---|
| 294 | }
|
---|
| 295 | }
|
---|
[24] | 296 | } else if(cm_strcaseequ(r[0], "DirectoryIndex")) {
|
---|
| 297 | for(i = 1; r[i] != NULL; i++) {
|
---|
| 298 | current->indexes[current->index_count++] = cm_strdup(r[i]);
|
---|
| 299 | }
|
---|
[33] | 300 | } else if(cm_strcaseequ(r[0], "Readme")) {
|
---|
| 301 | for(i = 1; r[i] != NULL; i++) {
|
---|
| 302 | current->readmes[current->readme_count++] = cm_strdup(r[i]);
|
---|
| 303 | }
|
---|
[6] | 304 | } else {
|
---|
[39] | 305 | stop = 1;
|
---|
[6] | 306 | if(r[0] != NULL) {
|
---|
[39] | 307 | int argc;
|
---|
| 308 | for(argc = 0; r[argc] != NULL; argc++)
|
---|
| 309 | ;
|
---|
| 310 | stop = 0;
|
---|
| 311 | int i;
|
---|
| 312 | bool called = false;
|
---|
| 313 | struct tw_tool tools;
|
---|
| 314 | tw_init_tools(&tools);
|
---|
| 315 | for(i = 0; i < config.module_count; i++) {
|
---|
| 316 | tw_mod_config_t mod_config = (tw_mod_config_t)tw_module_symbol(config.modules[i], "mod_config");
|
---|
| 317 | int resp;
|
---|
| 318 | if(mod_config != NULL && (resp = mod_config(&tools, r, argc)) == TW_CONFIG_PARSED) {
|
---|
| 319 | called = true;
|
---|
| 320 | break;
|
---|
| 321 | }
|
---|
| 322 | if(resp == TW_CONFIG_ERROR) {
|
---|
| 323 | stop = 1;
|
---|
| 324 | called = true;
|
---|
| 325 | break;
|
---|
| 326 | }
|
---|
| 327 | }
|
---|
| 328 | if(!called) {
|
---|
| 329 | cm_log("Config", "Unknown directive `%s' at line %d", r[0], ln);
|
---|
| 330 | stop = 1;
|
---|
| 331 | }
|
---|
[6] | 332 | }
|
---|
[5] | 333 | }
|
---|
| 334 | for(i = 0; r[i] != NULL; i++) free(r[i]);
|
---|
| 335 | free(r);
|
---|
[4] | 336 | }
|
---|
| 337 | free(l);
|
---|
| 338 | free(line);
|
---|
| 339 | line = malloc(1);
|
---|
| 340 | line[0] = 0;
|
---|
| 341 | if(c <= 0) break;
|
---|
[6] | 342 | } else if(cbuf[0] != '\r') {
|
---|
[4] | 343 | char* tmp = line;
|
---|
| 344 | line = cm_strcat(tmp, cbuf);
|
---|
| 345 | free(tmp);
|
---|
| 346 | }
|
---|
| 347 | }
|
---|
| 348 | free(line);
|
---|
| 349 | fclose(f);
|
---|
[6] | 350 | return stop;
|
---|
| 351 | } else {
|
---|
[5] | 352 | cm_log("Config", "Could not open the file");
|
---|
[4] | 353 | return 1;
|
---|
| 354 | }
|
---|
| 355 | }
|
---|