-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.c
245 lines (213 loc) · 6.56 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/* Copyright (c) 2020 J. von Rotz <jr@vrtz.ch>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <libconfig.h>
#include "tls.h"
#include "gemini.h"
#include "log.h"
#include "config.h"
#include "vhost.h"
static volatile int keepRunning = 1;
VHOST *vhost;
unsigned int vhostcount = 0;
void initWorker(int client, const char *server_root) {
char document_root[MAXBUF];
SSL *ssl;
VHOST *select_vhost;
SSL_CTX_set_tlsext_servername_callback(vhost->ctx, sni_cb);
ssl = SSL_new(vhost->ctx); /* Use first vhost as default context */
SSL_set_fd(ssl, client);
if (SSL_accept(ssl) <= 0) {
ERR_print_errors_fp(stderr);
} else {
select_vhost = get_current_vhost();
if(select_vhost == NULL) {
fprintf(stderr, "Cannot get current vhost\n");
return;
}
snprintf(document_root, MAXBUF-1, "%s/%s", server_root, select_vhost->docroot);
handle_request(ssl, document_root, select_vhost->defaultdocument, select_vhost->accesslog, select_vhost->errorlog);
}
SSL_shutdown(ssl);
SSL_free(ssl);
close(client);
}
void usage(char *progname) {
fprintf(stderr, "Usage: %s -c config [-t]\n\n", progname);
fprintf(stderr, "\t-c config\n\t\tPath to configuration file\n\n");
fprintf(stderr, "\t-t\n\t\tTest and print configuration\n\n");
exit(EXIT_FAILURE);
}
/* Currently not needed */
/*
void intHandler(int signal) {
keepRunning = 0;
}
*/
int main(int argc, char **argv) {
int sock;
int pid;
int client;
int opt;
uint len;
struct sockaddr_in addr;
struct sockaddr_in6 addr6;
const char *configpath = NULL;
int run_tests = 0;
char accesslog_path[MAXBUF];
char errorlog_path[MAXBUF];
GLOBALCONF *global;
VHOSTLIST *vhostlist;
VHOSTCONF *vhcp;
VHOST *vhp;
VHOST *tempvhp;
config_t cfg;
unsigned int i;
int j;
while((opt = getopt(argc, argv, "c:t")) != -1) {
switch(opt) {
case 'c':
configpath = optarg;
break;
case 't':
run_tests = 1;
break;
default:
usage(argv[0]);
}
}
if(configpath == NULL || strcmp(configpath, "") == 0)
usage(argv[0]);
if (run_tests) {
testprintconfig(configpath);
exit(EXIT_SUCCESS);
}
/* signal(SIGINT, intHandler); */
/* Prepare configuration */
if (init_geminid_config(configpath, &cfg, &global, &vhostlist) < 0) {
config_destroy(&cfg);
fprintf(stderr, "Cannot parse config.\n");
exit(EXIT_FAILURE);
}
if(global == NULL) {
config_destroy(&cfg);
fprintf(stderr, "Cannot parse config.\n");
exit(EXIT_FAILURE);
}
if(vhostlist->count < 1) {
config_destroy(&cfg);
fprintf(stderr, "No vhosts defined.\n");
exit(EXIT_FAILURE);
}
init_openssl();
/* Global configuration */
if (log_setup(&(LOGCONFIG){.use_local_time = global->loglocaltime, .time_format = global->logtimeformat})) {
fprintf(stderr, "Error setting up logging system\n");
exit(EXIT_FAILURE);
}
/* Configuration per virtual host */
vhostcount = vhostlist->count;
vhost = calloc(vhostcount, sizeof(VHOST));
if(vhost == NULL) {
fprintf(stderr, "Error allocating vhost struct\n");
exit(EXIT_FAILURE);
}
vhcp = vhostlist->vhost;
vhp = vhost;
for(i=0; i < vhostcount; i++) {
snprintf(accesslog_path, MAXBUF-1, "%s/%s", global->logdir, vhcp->accesslog);
snprintf(errorlog_path, MAXBUF-1, "%s/%s", global->logdir, vhcp->errorlog);
tempvhp = create_vhost(vhcp->name, vhcp->docroot, vhcp->index, accesslog_path, errorlog_path, vhcp->cert, vhcp->key);
if(tempvhp == NULL) {
fprintf(stderr, "Error allocating vhost struct\n");
exit(EXIT_FAILURE);
}
*vhp = *tempvhp;
free(tempvhp);
/* Print configuration settings */
fprintf(stderr, "serverroot: %s\nlogdir: %s\nhostname: %s\naccess_log_path: %s\nerror_log_path: %s\ndefault_document: %s\nlisten_port: %d\ndocument_root: %s\npublic key: %s\nprivate key: %s\n\n\n", global->serverroot, global->logdir, vhcp->name, accesslog_path, errorlog_path, vhcp->index, global->port, vhcp->docroot, vhcp->cert, vhcp->key);
vhcp++;
vhp++;
}
j = set_current_vhost(vhost);
if(j < 0) {
fprintf(stderr, "Cannot set current (default) vhost\n");
exit(EXIT_FAILURE);
}
if(global->ipv6_enable)
sock = create_socket6(global->port);
else
sock = create_socket(global->port);
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &(int){ 1 }, sizeof(int));
/* Auto-reap zombies for now.
* Maybe refine that with a real signal handler later
* to get exit codes from children.
*/
signal(SIGCHLD, SIG_IGN);
while(keepRunning) {
if(global->ipv6_enable) {
len = sizeof(addr6);
client = accept(sock, (struct sockaddr*)&addr6, &len);
} else {
len = sizeof(addr);
client = accept(sock, (struct sockaddr*)&addr, &len);
}
if (client < 0) {
perror("Unable to accept");
continue;
}
if((pid = fork()) == 0) {
// Child
close(sock);
initWorker(client, global->serverroot);
exit(0);
} else if (pid > 0) {
// Parent
close(client);
} else {
// Failed
perror("Unable to fork");
}
}
destroy_vhost(vhost, vhostcount);
close(sock);
cleanup_openssl();
free(global);
free(vhostlist);
config_destroy(&cfg);
}