[4] | 1 | /* $Id: config.c 17 2024-09-13 17:41:07Z 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 |
|
---|
| 14 | #include <cm_string.h>
|
---|
| 15 | #include <cm_log.h>
|
---|
| 16 |
|
---|
[6] | 17 | struct tw_config config;
|
---|
| 18 |
|
---|
[12] | 19 | struct tw_config_entry* tw_vhost_match(const char* name, int port) {
|
---|
| 20 | int i;
|
---|
| 21 | for(i = 0; i < config.vhost_count; i++) {
|
---|
[13] | 22 | if(strcmp(config.vhosts[i].name, name) == 0 && (config.vhosts[i].port == -1 ? 1 : config.vhosts[i].port == port)) {
|
---|
[12] | 23 | return &config.vhosts[i];
|
---|
| 24 | }
|
---|
| 25 | }
|
---|
| 26 | return &config.root;
|
---|
| 27 | }
|
---|
| 28 |
|
---|
[7] | 29 | void tw_config_init(void) {
|
---|
| 30 | int i;
|
---|
| 31 | for(i = 0; i < MAX_PORTS + 1; i++) {
|
---|
| 32 | config.ports[i] = -1;
|
---|
| 33 | }
|
---|
[12] | 34 | for(i = 0; i < MAX_VHOSTS; i++) {
|
---|
| 35 | config.vhosts[i].sslkey = NULL;
|
---|
| 36 | config.vhosts[i].sslcert = NULL;
|
---|
| 37 | }
|
---|
| 38 | config.root.sslkey = NULL;
|
---|
| 39 | config.root.sslcert = NULL;
|
---|
| 40 | config.vhost_count = 0;
|
---|
[17] | 41 | config.server_root = cm_strdup(PREFIX);
|
---|
[12] | 42 | gethostname(config.hostname, 1024);
|
---|
[7] | 43 | }
|
---|
[6] | 44 |
|
---|
| 45 | int tw_config_read(const char* path) {
|
---|
[4] | 46 | cm_log("Config", "Reading %s", path);
|
---|
| 47 | char cbuf[2];
|
---|
| 48 | cbuf[1] = 0;
|
---|
[6] | 49 | int ln = 0;
|
---|
[4] | 50 | FILE* f = fopen(path, "r");
|
---|
[6] | 51 | if(f != NULL) {
|
---|
[4] | 52 | char* line = malloc(1);
|
---|
| 53 | line[0] = 0;
|
---|
[6] | 54 | int stop = 0;
|
---|
[12] | 55 | struct tw_config_entry* current = &config.root;
|
---|
[6] | 56 | char* vhost = NULL;
|
---|
| 57 | while(stop == 0) {
|
---|
[4] | 58 | int c = fread(cbuf, 1, 1, f);
|
---|
[6] | 59 | if(cbuf[0] == '\n' || c <= 0) {
|
---|
| 60 | ln++;
|
---|
[4] | 61 | char* l = cm_trim(line);
|
---|
[6] | 62 | if(strlen(l) > 0 && l[0] != '#') {
|
---|
[5] | 63 | char** r = cm_split(l, " \t");
|
---|
| 64 | int i;
|
---|
[6] | 65 | if(cm_strcaseequ(r[0], "Include") || cm_strcaseequ(r[0], "IncludeOptional")) {
|
---|
| 66 | for(i = 1; r[i] != NULL; i++) {
|
---|
| 67 | if(tw_config_read(r[i]) != 0 && cm_strcaseequ(r[0], "Include")) {
|
---|
| 68 | stop = 1;
|
---|
| 69 | break;
|
---|
[5] | 70 | }
|
---|
| 71 | }
|
---|
[6] | 72 | } else if(cm_strcaseequ(r[0], "BeginVirtualHost")) {
|
---|
| 73 | if(vhost != NULL) {
|
---|
[12] | 74 | cm_log("Config", "Already in virtual host section at line %d", ln);
|
---|
[6] | 75 | stop = 1;
|
---|
| 76 | } else {
|
---|
| 77 | if(r[1] == NULL) {
|
---|
[12] | 78 | cm_log("Config", "Missing virtual host at line %d", ln);
|
---|
[6] | 79 | stop = 1;
|
---|
| 80 | } else {
|
---|
| 81 | vhost = cm_strdup(r[1]);
|
---|
[12] | 82 | current = &config.vhosts[config.vhost_count++];
|
---|
| 83 | int i;
|
---|
| 84 | current->name = cm_strdup(vhost);
|
---|
[13] | 85 | current->port = -1;
|
---|
[12] | 86 | for(i = 0; vhost[i] != 0; i++) {
|
---|
| 87 | if(vhost[i] == ':') {
|
---|
| 88 | current->name[i] = 0;
|
---|
| 89 | current->port = atoi(current->name + i + 1);
|
---|
| 90 | break;
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
[6] | 93 | }
|
---|
| 94 | }
|
---|
| 95 | } else if(cm_strcaseequ(r[0], "EndVirtualHost")) {
|
---|
| 96 | if(vhost == NULL) {
|
---|
[12] | 97 | cm_log("Config", "Not in virtual host section at line %d", ln);
|
---|
[6] | 98 | stop = 1;
|
---|
| 99 | } else {
|
---|
| 100 | free(vhost);
|
---|
| 101 | vhost = NULL;
|
---|
[12] | 102 | current = &config.root;
|
---|
[6] | 103 | }
|
---|
[7] | 104 | } else if(cm_strcaseequ(r[0], "Listen") || cm_strcaseequ(r[0], "ListenSSL")) {
|
---|
| 105 | for(i = 1; r[i] != NULL; i++) {
|
---|
| 106 | uint64_t port = atoi(r[i]);
|
---|
| 107 | cm_log("Config", "Going to listen at port %d%s", (int)port, cm_strcaseequ(r[0], "ListenSSL") ? " with SSL" : "");
|
---|
| 108 | if(cm_strcaseequ(r[0], "ListenSSL")) port |= (1ULL << 32);
|
---|
| 109 | int j;
|
---|
| 110 | for(j = 0; config.ports[j] != -1; j++)
|
---|
| 111 | ;
|
---|
| 112 | config.ports[j] = port;
|
---|
| 113 | }
|
---|
[12] | 114 | } else if(cm_strcaseequ(r[0], "SSLKey")) {
|
---|
| 115 | if(r[1] == NULL) {
|
---|
| 116 | cm_log("Config", "Missing path at line %d", ln);
|
---|
| 117 | stop = 1;
|
---|
| 118 | } else {
|
---|
| 119 | if(current->sslkey != NULL) free(current->sslkey);
|
---|
| 120 | current->sslkey = cm_strdup(r[1]);
|
---|
| 121 | }
|
---|
| 122 | } else if(cm_strcaseequ(r[0], "SSLCertificate")) {
|
---|
| 123 | if(r[1] == NULL) {
|
---|
| 124 | cm_log("Config", "Missing path at line %d", ln);
|
---|
| 125 | stop = 1;
|
---|
| 126 | } else {
|
---|
| 127 | if(current->sslcert != NULL) free(current->sslcert);
|
---|
| 128 | current->sslcert = cm_strdup(r[1]);
|
---|
| 129 | }
|
---|
[17] | 130 | } else if(cm_strcaseequ(r[0], "ServerRoot")) {
|
---|
| 131 | if(r[1] == NULL) {
|
---|
| 132 | cm_log("Config", "Missing path at line %d", ln);
|
---|
| 133 | stop = 1;
|
---|
| 134 | } else {
|
---|
| 135 | if(config.server_root != NULL) free(config.server_root);
|
---|
| 136 | config.server_root = cm_strdup(r[1]);
|
---|
| 137 | }
|
---|
| 138 | } else if(cm_strcaseequ(r[0], "LoadModule")) {
|
---|
| 139 | for(i = 1; r[i] != NULL; i++) {
|
---|
| 140 | void* mod = tw_module_load(r[i]);
|
---|
| 141 | if(mod != NULL) {
|
---|
| 142 | if(tw_module_init(mod) != 0) {
|
---|
| 143 | stop = 1;
|
---|
| 144 | break;
|
---|
| 145 | }
|
---|
| 146 | } else {
|
---|
| 147 | stop = 1;
|
---|
| 148 | break;
|
---|
| 149 | }
|
---|
| 150 | }
|
---|
[6] | 151 | } else {
|
---|
| 152 | if(r[0] != NULL) {
|
---|
| 153 | cm_log("Config", "Unknown directive `%s' at line %d", r[0], ln);
|
---|
| 154 | }
|
---|
| 155 | stop = 1;
|
---|
[5] | 156 | }
|
---|
| 157 | for(i = 0; r[i] != NULL; i++) free(r[i]);
|
---|
| 158 | free(r);
|
---|
[4] | 159 | }
|
---|
| 160 | free(l);
|
---|
| 161 | free(line);
|
---|
| 162 | line = malloc(1);
|
---|
| 163 | line[0] = 0;
|
---|
| 164 | if(c <= 0) break;
|
---|
[6] | 165 | } else if(cbuf[0] != '\r') {
|
---|
[4] | 166 | char* tmp = line;
|
---|
| 167 | line = cm_strcat(tmp, cbuf);
|
---|
| 168 | free(tmp);
|
---|
| 169 | }
|
---|
| 170 | }
|
---|
| 171 | free(line);
|
---|
| 172 | fclose(f);
|
---|
[6] | 173 | return stop;
|
---|
| 174 | } else {
|
---|
[5] | 175 | cm_log("Config", "Could not open the file");
|
---|
[4] | 176 | return 1;
|
---|
| 177 | }
|
---|
| 178 | }
|
---|