2024-09-13 09:39:33 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
#include "tw_config.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2024-09-13 10:40:53 +00:00
|
|
|
#include <stdint.h>
|
2024-09-13 09:39:33 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <cm_string.h>
|
|
|
|
#include <cm_log.h>
|
|
|
|
|
2024-09-13 10:28:20 +00:00
|
|
|
struct tw_config config;
|
|
|
|
|
2024-09-13 10:40:53 +00:00
|
|
|
void tw_config_init(void) {
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < MAX_PORTS + 1; i++) {
|
|
|
|
config.ports[i] = -1;
|
|
|
|
}
|
|
|
|
}
|
2024-09-13 10:28:20 +00:00
|
|
|
|
|
|
|
int tw_config_read(const char* path) {
|
2024-09-13 09:39:33 +00:00
|
|
|
cm_log("Config", "Reading %s", path);
|
|
|
|
char cbuf[2];
|
|
|
|
cbuf[1] = 0;
|
2024-09-13 10:28:20 +00:00
|
|
|
int ln = 0;
|
2024-09-13 09:39:33 +00:00
|
|
|
FILE* f = fopen(path, "r");
|
2024-09-13 10:28:20 +00:00
|
|
|
if(f != NULL) {
|
2024-09-13 09:39:33 +00:00
|
|
|
char* line = malloc(1);
|
|
|
|
line[0] = 0;
|
2024-09-13 10:28:20 +00:00
|
|
|
int stop = 0;
|
|
|
|
char* vhost = NULL;
|
|
|
|
while(stop == 0) {
|
2024-09-13 09:39:33 +00:00
|
|
|
int c = fread(cbuf, 1, 1, f);
|
2024-09-13 10:28:20 +00:00
|
|
|
if(cbuf[0] == '\n' || c <= 0) {
|
|
|
|
ln++;
|
2024-09-13 09:39:33 +00:00
|
|
|
char* l = cm_trim(line);
|
2024-09-13 10:28:20 +00:00
|
|
|
if(strlen(l) > 0 && l[0] != '#') {
|
2024-09-13 10:08:00 +00:00
|
|
|
char** r = cm_split(l, " \t");
|
|
|
|
int i;
|
2024-09-13 10:28:20 +00:00
|
|
|
if(cm_strcaseequ(r[0], "Include") || cm_strcaseequ(r[0], "IncludeOptional")) {
|
|
|
|
for(i = 1; r[i] != NULL; i++) {
|
|
|
|
if(tw_config_read(r[i]) != 0 && cm_strcaseequ(r[0], "Include")) {
|
|
|
|
stop = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if(cm_strcaseequ(r[0], "BeginVirtualHost")) {
|
|
|
|
if(vhost != NULL) {
|
|
|
|
cm_log("Config", "Already in virtual host section");
|
|
|
|
stop = 1;
|
|
|
|
} else {
|
|
|
|
if(r[1] == NULL) {
|
|
|
|
cm_log("Config", "Missing virtual host");
|
|
|
|
stop = 1;
|
|
|
|
} else {
|
|
|
|
vhost = cm_strdup(r[1]);
|
2024-09-13 10:08:00 +00:00
|
|
|
}
|
|
|
|
}
|
2024-09-13 10:28:20 +00:00
|
|
|
} else if(cm_strcaseequ(r[0], "EndVirtualHost")) {
|
|
|
|
if(vhost == NULL) {
|
|
|
|
cm_log("Config", "Not in virtual host section");
|
|
|
|
stop = 1;
|
|
|
|
} else {
|
|
|
|
free(vhost);
|
|
|
|
vhost = NULL;
|
|
|
|
}
|
2024-09-13 10:40:53 +00:00
|
|
|
} else if(cm_strcaseequ(r[0], "Listen") || cm_strcaseequ(r[0], "ListenSSL")) {
|
|
|
|
for(i = 1; r[i] != NULL; i++) {
|
|
|
|
uint64_t port = atoi(r[i]);
|
|
|
|
cm_log("Config", "Going to listen at port %d%s", (int)port, cm_strcaseequ(r[0], "ListenSSL") ? " with SSL" : "");
|
|
|
|
if(cm_strcaseequ(r[0], "ListenSSL")) port |= (1ULL << 32);
|
|
|
|
int j;
|
|
|
|
for(j = 0; config.ports[j] != -1; j++)
|
|
|
|
;
|
|
|
|
config.ports[j] = port;
|
|
|
|
}
|
2024-09-13 10:28:20 +00:00
|
|
|
} else {
|
|
|
|
if(r[0] != NULL) {
|
|
|
|
cm_log("Config", "Unknown directive `%s' at line %d", r[0], ln);
|
|
|
|
}
|
|
|
|
stop = 1;
|
2024-09-13 10:08:00 +00:00
|
|
|
}
|
|
|
|
for(i = 0; r[i] != NULL; i++) free(r[i]);
|
|
|
|
free(r);
|
2024-09-13 09:39:33 +00:00
|
|
|
}
|
|
|
|
free(l);
|
|
|
|
free(line);
|
|
|
|
line = malloc(1);
|
|
|
|
line[0] = 0;
|
|
|
|
if(c <= 0) break;
|
2024-09-13 10:28:20 +00:00
|
|
|
} else if(cbuf[0] != '\r') {
|
2024-09-13 09:39:33 +00:00
|
|
|
char* tmp = line;
|
|
|
|
line = cm_strcat(tmp, cbuf);
|
|
|
|
free(tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(line);
|
|
|
|
fclose(f);
|
2024-09-13 10:28:20 +00:00
|
|
|
return stop;
|
|
|
|
} else {
|
2024-09-13 10:08:00 +00:00
|
|
|
cm_log("Config", "Could not open the file");
|
2024-09-13 09:39:33 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|