Subversion Repositories Tewi

Rev

Rev 7 | Rev 13 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 7 Rev 12
Line 1... Line 1...
1
/* $Id: config.c 7 2024-09-13 10:40:53Z nishi $ */
1
/* $Id: config.c 12 2024-09-13 13:36:03Z nishi $ */
2
 
2
 
3
#include "tw_config.h"
3
#include "tw_config.h"
4
 
4
 
5
#include <stdio.h>
5
#include <stdio.h>
6
#include <stdint.h>
6
#include <stdint.h>
7
#include <stdlib.h>
7
#include <stdlib.h>
8
#include <string.h>
8
#include <string.h>
-
 
9
#include <unistd.h>
9
 
10
 
10
#include <cm_string.h>
11
#include <cm_string.h>
11
#include <cm_log.h>
12
#include <cm_log.h>
12
 
13
 
13
struct tw_config config;
14
struct tw_config config;
14
 
15
 
-
 
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++) {
-
 
19
		if(strcmp(config.vhosts[i].name, name) == 0 && config.vhosts[i].port == port) {
-
 
20
			return &config.vhosts[i];
-
 
21
		}
-
 
22
	}
-
 
23
	return &config.root;
-
 
24
}
-
 
25
 
15
void tw_config_init(void) {
26
void tw_config_init(void) {
16
	int i;
27
	int i;
17
	for(i = 0; i < MAX_PORTS + 1; i++) {
28
	for(i = 0; i < MAX_PORTS + 1; i++) {
18
		config.ports[i] = -1;
29
		config.ports[i] = -1;
19
	}
30
	}
-
 
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);
20
}
39
}
21
 
40
 
22
int tw_config_read(const char* path) {
41
int tw_config_read(const char* path) {
23
	cm_log("Config", "Reading %s", path);
42
	cm_log("Config", "Reading %s", path);
24
	char cbuf[2];
43
	char cbuf[2];
Line 27... Line 46...
27
	FILE* f = fopen(path, "r");
46
	FILE* f = fopen(path, "r");
28
	if(f != NULL) {
47
	if(f != NULL) {
29
		char* line = malloc(1);
48
		char* line = malloc(1);
30
		line[0] = 0;
49
		line[0] = 0;
31
		int stop = 0;
50
		int stop = 0;
-
 
51
		struct tw_config_entry* current = &config.root;
32
		char* vhost = NULL;
52
		char* vhost = NULL;
33
		while(stop == 0) {
53
		while(stop == 0) {
34
			int c = fread(cbuf, 1, 1, f);
54
			int c = fread(cbuf, 1, 1, f);
35
			if(cbuf[0] == '\n' || c <= 0) {
55
			if(cbuf[0] == '\n' || c <= 0) {
36
				ln++;
56
				ln++;
Line 45... Line 65...
45
								break;
65
								break;
46
							}
66
							}
47
						}
67
						}
48
					} else if(cm_strcaseequ(r[0], "BeginVirtualHost")) {
68
					} else if(cm_strcaseequ(r[0], "BeginVirtualHost")) {
49
						if(vhost != NULL) {
69
						if(vhost != NULL) {
50
							cm_log("Config", "Already in virtual host section");
70
							cm_log("Config", "Already in virtual host section at line %d", ln);
51
							stop = 1;
71
							stop = 1;
52
						} else {
72
						} else {
53
							if(r[1] == NULL) {
73
							if(r[1] == NULL) {
54
								cm_log("Config", "Missing virtual host");
74
								cm_log("Config", "Missing virtual host at line %d", ln);
55
								stop = 1;
75
								stop = 1;
56
							} else {
76
							} else {
57
								vhost = cm_strdup(r[1]);
77
								vhost = cm_strdup(r[1]);
-
 
78
								current = &config.vhosts[config.vhost_count++];
-
 
79
								int i;
-
 
80
								current->name = cm_strdup(vhost);
-
 
81
								current->port = 80;
-
 
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
								}
58
							}
89
							}
59
						}
90
						}
60
					} else if(cm_strcaseequ(r[0], "EndVirtualHost")) {
91
					} else if(cm_strcaseequ(r[0], "EndVirtualHost")) {
61
						if(vhost == NULL) {
92
						if(vhost == NULL) {
62
							cm_log("Config", "Not in virtual host section");
93
							cm_log("Config", "Not in virtual host section at line %d", ln);
63
							stop = 1;
94
							stop = 1;
64
						} else {
95
						} else {
65
							free(vhost);
96
							free(vhost);
66
							vhost = NULL;
97
							vhost = NULL;
-
 
98
							current = &config.root;
67
						}
99
						}
68
					} else if(cm_strcaseequ(r[0], "Listen") || cm_strcaseequ(r[0], "ListenSSL")) {
100
					} else if(cm_strcaseequ(r[0], "Listen") || cm_strcaseequ(r[0], "ListenSSL")) {
69
						for(i = 1; r[i] != NULL; i++) {
101
						for(i = 1; r[i] != NULL; i++) {
70
							uint64_t port = atoi(r[i]);
102
							uint64_t port = atoi(r[i]);
71
							cm_log("Config", "Going to listen at port %d%s", (int)port, cm_strcaseequ(r[0], "ListenSSL") ? " with SSL" : "");
103
							cm_log("Config", "Going to listen at port %d%s", (int)port, cm_strcaseequ(r[0], "ListenSSL") ? " with SSL" : "");
Line 73... Line 105...
73
							int j;
105
							int j;
74
							for(j = 0; config.ports[j] != -1; j++)
106
							for(j = 0; config.ports[j] != -1; j++)
75
								;
107
								;
76
							config.ports[j] = port;
108
							config.ports[j] = port;
77
						}
109
						}
-
 
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
						}
78
					} else {
126
					} else {
79
						if(r[0] != NULL) {
127
						if(r[0] != NULL) {
80
							cm_log("Config", "Unknown directive `%s' at line %d", r[0], ln);
128
							cm_log("Config", "Unknown directive `%s' at line %d", r[0], ln);
81
						}
129
						}
82
						stop = 1;
130
						stop = 1;