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