Subversion Repositories Tewi

Rev

Rev 13 | Rev 17 | 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 16 2024-09-13 15:09:52Z nishi $ */
2
 
16 nishi 3
#define SOURCE
4
 
4 nishi 5
#include "tw_config.h"
6
 
7
#include <stdio.h>
7 nishi 8
#include <stdint.h>
4 nishi 9
#include <stdlib.h>
10
#include <string.h>
12 nishi 11
#include <unistd.h>
4 nishi 12
 
13
#include <cm_string.h>
14
#include <cm_log.h>
15
 
6 nishi 16
struct tw_config config;
17
 
12 nishi 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++) {
13 nishi 21
		if(strcmp(config.vhosts[i].name, name) == 0 && (config.vhosts[i].port == -1 ? 1 : config.vhosts[i].port == port)) {
12 nishi 22
			return &config.vhosts[i];
23
		}
24
	}
25
	return &config.root;
26
}
27
 
7 nishi 28
void tw_config_init(void) {
29
	int i;
30
	for(i = 0; i < MAX_PORTS + 1; i++) {
31
		config.ports[i] = -1;
32
	}
12 nishi 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);
7 nishi 41
}
6 nishi 42
 
43
int tw_config_read(const char* path) {
4 nishi 44
	cm_log("Config", "Reading %s", path);
45
	char cbuf[2];
46
	cbuf[1] = 0;
6 nishi 47
	int ln = 0;
4 nishi 48
	FILE* f = fopen(path, "r");
6 nishi 49
	if(f != NULL) {
4 nishi 50
		char* line = malloc(1);
51
		line[0] = 0;
6 nishi 52
		int stop = 0;
12 nishi 53
		struct tw_config_entry* current = &config.root;
6 nishi 54
		char* vhost = NULL;
55
		while(stop == 0) {
4 nishi 56
			int c = fread(cbuf, 1, 1, f);
6 nishi 57
			if(cbuf[0] == '\n' || c <= 0) {
58
				ln++;
4 nishi 59
				char* l = cm_trim(line);
6 nishi 60
				if(strlen(l) > 0 && l[0] != '#') {
5 nishi 61
					char** r = cm_split(l, " \t");
62
					int i;
6 nishi 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;
5 nishi 68
							}
69
						}
6 nishi 70
					} else if(cm_strcaseequ(r[0], "BeginVirtualHost")) {
71
						if(vhost != NULL) {
12 nishi 72
							cm_log("Config", "Already in virtual host section at line %d", ln);
6 nishi 73
							stop = 1;
74
						} else {
75
							if(r[1] == NULL) {
12 nishi 76
								cm_log("Config", "Missing virtual host at line %d", ln);
6 nishi 77
								stop = 1;
78
							} else {
79
								vhost = cm_strdup(r[1]);
12 nishi 80
								current = &config.vhosts[config.vhost_count++];
81
								int i;
82
								current->name = cm_strdup(vhost);
13 nishi 83
								current->port = -1;
12 nishi 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
								}
6 nishi 91
							}
92
						}
93
					} else if(cm_strcaseequ(r[0], "EndVirtualHost")) {
94
						if(vhost == NULL) {
12 nishi 95
							cm_log("Config", "Not in virtual host section at line %d", ln);
6 nishi 96
							stop = 1;
97
						} else {
98
							free(vhost);
99
							vhost = NULL;
12 nishi 100
							current = &config.root;
6 nishi 101
						}
7 nishi 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
						}
12 nishi 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
						}
6 nishi 128
					} else {
129
						if(r[0] != NULL) {
130
							cm_log("Config", "Unknown directive `%s' at line %d", r[0], ln);
131
						}
132
						stop = 1;
5 nishi 133
					}
134
					for(i = 0; r[i] != NULL; i++) free(r[i]);
135
					free(r);
4 nishi 136
				}
137
				free(l);
138
				free(line);
139
				line = malloc(1);
140
				line[0] = 0;
141
				if(c <= 0) break;
6 nishi 142
			} else if(cbuf[0] != '\r') {
4 nishi 143
				char* tmp = line;
144
				line = cm_strcat(tmp, cbuf);
145
				free(tmp);
146
			}
147
		}
148
		free(line);
149
		fclose(f);
6 nishi 150
		return stop;
151
	} else {
5 nishi 152
		cm_log("Config", "Could not open the file");
4 nishi 153
		return 1;
154
	}
155
}