Subversion Repositories Tewi

Rev

Rev 18 | Rev 20 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4 nishi 1
/* $Id: config.c 19 2024-09-14 00:51:41Z nishi $ */
2
 
16 nishi 3
#define SOURCE
4
 
4 nishi 5
#include "tw_config.h"
17 nishi 6
#include "tw_module.h"
4 nishi 7
 
8
#include <stdio.h>
7 nishi 9
#include <stdint.h>
4 nishi 10
#include <stdlib.h>
11
#include <string.h>
12 nishi 12
#include <unistd.h>
4 nishi 13
 
14
#include <cm_string.h>
15
#include <cm_log.h>
16
 
6 nishi 17
struct tw_config config;
18
 
12 nishi 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 nishi 22
		if(strcmp(config.vhosts[i].name, name) == 0 && (config.vhosts[i].port == -1 ? 1 : config.vhosts[i].port == port)) {
12 nishi 23
			return &config.vhosts[i];
24
		}
25
	}
26
	return &config.root;
27
}
28
 
7 nishi 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 nishi 34
	for(i = 0; i < MAX_VHOSTS; i++) {
35
		config.vhosts[i].sslkey = NULL;
36
		config.vhosts[i].sslcert = NULL;
19 nishi 37
		config.vhosts[i].root = NULL;
12 nishi 38
	}
39
	config.root.sslkey = NULL;
40
	config.root.sslcert = NULL;
19 nishi 41
	config.root.root = NULL;
12 nishi 42
	config.vhost_count = 0;
18 nishi 43
	config.module_count = 0;
44
	config.extension = NULL;
17 nishi 45
	config.server_root = cm_strdup(PREFIX);
12 nishi 46
	gethostname(config.hostname, 1024);
7 nishi 47
}
6 nishi 48
 
49
int tw_config_read(const char* path) {
4 nishi 50
	cm_log("Config", "Reading %s", path);
51
	char cbuf[2];
52
	cbuf[1] = 0;
6 nishi 53
	int ln = 0;
4 nishi 54
	FILE* f = fopen(path, "r");
6 nishi 55
	if(f != NULL) {
4 nishi 56
		char* line = malloc(1);
57
		line[0] = 0;
6 nishi 58
		int stop = 0;
12 nishi 59
		struct tw_config_entry* current = &config.root;
6 nishi 60
		char* vhost = NULL;
61
		while(stop == 0) {
4 nishi 62
			int c = fread(cbuf, 1, 1, f);
6 nishi 63
			if(cbuf[0] == '\n' || c <= 0) {
64
				ln++;
4 nishi 65
				char* l = cm_trim(line);
6 nishi 66
				if(strlen(l) > 0 && l[0] != '#') {
5 nishi 67
					char** r = cm_split(l, " \t");
68
					int i;
6 nishi 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;
5 nishi 74
							}
75
						}
6 nishi 76
					} else if(cm_strcaseequ(r[0], "BeginVirtualHost")) {
77
						if(vhost != NULL) {
12 nishi 78
							cm_log("Config", "Already in virtual host section at line %d", ln);
6 nishi 79
							stop = 1;
80
						} else {
81
							if(r[1] == NULL) {
12 nishi 82
								cm_log("Config", "Missing virtual host at line %d", ln);
6 nishi 83
								stop = 1;
84
							} else {
85
								vhost = cm_strdup(r[1]);
12 nishi 86
								current = &config.vhosts[config.vhost_count++];
87
								int i;
88
								current->name = cm_strdup(vhost);
13 nishi 89
								current->port = -1;
12 nishi 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
								}
6 nishi 97
							}
98
						}
99
					} else if(cm_strcaseequ(r[0], "EndVirtualHost")) {
100
						if(vhost == NULL) {
12 nishi 101
							cm_log("Config", "Not in virtual host section at line %d", ln);
6 nishi 102
							stop = 1;
103
						} else {
104
							free(vhost);
105
							vhost = NULL;
12 nishi 106
							current = &config.root;
6 nishi 107
						}
7 nishi 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
						}
12 nishi 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
						}
19 nishi 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
						}
17 nishi 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) {
18 nishi 154
								config.modules[config.module_count++] = mod;
17 nishi 155
								if(tw_module_init(mod) != 0) {
156
									stop = 1;
157
									break;
158
								}
159
							} else {
160
								stop = 1;
161
								break;
162
							}
163
						}
6 nishi 164
					} else {
165
						if(r[0] != NULL) {
166
							cm_log("Config", "Unknown directive `%s' at line %d", r[0], ln);
167
						}
168
						stop = 1;
5 nishi 169
					}
170
					for(i = 0; r[i] != NULL; i++) free(r[i]);
171
					free(r);
4 nishi 172
				}
173
				free(l);
174
				free(line);
175
				line = malloc(1);
176
				line[0] = 0;
177
				if(c <= 0) break;
6 nishi 178
			} else if(cbuf[0] != '\r') {
4 nishi 179
				char* tmp = line;
180
				line = cm_strcat(tmp, cbuf);
181
				free(tmp);
182
			}
183
		}
184
		free(line);
185
		fclose(f);
6 nishi 186
		return stop;
187
	} else {
5 nishi 188
		cm_log("Config", "Could not open the file");
4 nishi 189
		return 1;
190
	}
191
}