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