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