Subversion Repositories Tewi

Rev

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