Subversion Repositories Tewi

Rev

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