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