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