-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
325 lines (271 loc) · 9.12 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#define _DEFAULT_SOURCE
#include <arpa/inet.h>
#include <netinet/in.h>
#include <openssl/ssl.h>
#include <getopt.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
#include "config.h"
#include "request.h"
#include "response.h"
#include "file.h"
struct gem_config cfg;
static void usage(char *argv[]);
static void startup_check(void);
static void log_connection(int client_fd);
int main(int argc, char *argv[]) {
SSL_CTX *ctx;
SSL *ssl;
struct sockaddr_in addr;
struct timeval timeout;
pid_t pid;
int fd, client;
int enable = 1;
int opt, err;
int hset = 0, dset = 0;
int pset = 0, iset = 0;
int kset = 0, cset = 0;
struct gem_uri uri = {0};
char buffer[GEM_URI_MAXSIZ + 1] = {0};
/* Force line-buffering */
setvbuf(stdout, NULL, _IOLBF, 0);
setvbuf(stderr, NULL, _IOLBF, 0);
while ((opt = getopt(argc, argv, "k:c:h:p:d:i:eav")) != -1) {
switch (opt) {
case 'k':
strncpy(cfg.key_path, optarg, GEM_CFG_SIZ);
kset = 1;
break;
case 'c':
strncpy(cfg.crt_path, optarg, GEM_CFG_SIZ);
cset = 1;
break;
case 'h':
strncpy(cfg.hostname, optarg, GEM_CFG_SIZ);
hset = 1;
break;
case 'p':
cfg.port = atoi(optarg);
pset = 1;
break;
case 'd':
strncpy(cfg.docroot, optarg, GEM_CFG_SIZ);
dset = 1;
break;
case 'i':
strncpy(cfg.index, optarg, GEM_CFG_SIZ);
iset = 1;
break;
case 'e':
cfg.enumerate = 1;
break;
case 'a':
cfg.diffhost = 1;
break;
case 'v':
cfg.verbose = 1;
break;
default:
usage(argv);
}
}
/* check user input */
/* port provided as 0 or atoi() failed */
if (!cfg.port) {
if (pset) {
fprintf(stderr, "invalid port\n");
usage(argv);
}
cfg.port = 1965;
}
/* doc root not set */
if (!dset) {
fprintf(stderr, "Error: no document root path specified\n");
usage(argv);
}
/* hostname not set */
if (!hset) {
strncpy(cfg.hostname, "localhost", GEM_CFG_SIZ);
}
/* index file not set */
if (!iset) {
strncpy(cfg.index, "index.gmi", GEM_CFG_SIZ);
}
if (cfg_validate(&cfg)) {
usage(argv);
}
/* If not explicitly set when calling program, use defaults.. */
if (!kset) {
strcpy(cfg.key_path, PRIVATE_KEY_PATH);
}
if (!cset) {
strcpy(cfg.crt_path, PUBLIC_KEY_PATH);
}
/* check ssl/tls files */
startup_check();
/* timeout CLIENT sockets after 10 seconds */
timeout.tv_sec = 10;
timeout.tv_usec = 0;
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) {
perror("socket()");
exit(1);
}
/* useful for quick restarts */
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof enable) < 0) {
perror("setsockopt(SO_REUSEADDR)");
close(fd);
exit(1);
}
addr.sin_family = AF_INET;
addr.sin_port = htons((unsigned short)cfg.port);
addr.sin_addr.s_addr = INADDR_ANY;
if (bind(fd, (struct sockaddr *)&addr, sizeof addr) < 0) {
perror("bind()");
close(fd);
exit(1);
}
/* prevent zombie processes */
signal(SIGCHLD, SIG_IGN);
while(1) {
listen(fd, 10);
client = accept(fd, NULL, NULL);
if (cfg.verbose) log_connection(client);
/* timeout on receive */
if (setsockopt(client, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof timeout) < 0) {
perror("setsockopt(SO_RCVTIMEO)");
close(client);
continue;
}
/* timeout on send */
if (setsockopt(client, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof timeout) < 0) {
perror("setsockopt(SO_SNDTIMEO)");
close(client);
continue;
}
pid = fork();
if (pid == 0) {
memset(buffer, 0, GEM_URI_MAXSIZ);
memset(&uri, 0, sizeof (uri));
ctx = SSL_CTX_new(TLS_server_method());
ssl = SSL_new(ctx);
SSL_set_fd(ssl, client);
if (SSL_use_certificate_chain_file(ssl, cfg.crt_path) != 1) {
perror("SSL_use_certificate_chain_file");
goto CLOSE_CONNECTION;
}
if (SSL_use_PrivateKey_file(ssl, cfg.key_path, SSL_FILETYPE_PEM) != 1) {
perror("SSL_use_PrivateKey_file");
goto CLOSE_CONNECTION;
}
if (SSL_accept(ssl) != 1) {
perror("SSL_accept");
goto CLOSE_CONNECTION;
}
if (SSL_read(ssl, buffer, GEM_URI_MAXSIZ) <= 0) {
perror("SSL_read");
goto CLOSE_CONNECTION;
}
/* chroot "/" to docroot */
if (chroot(cfg.docroot)) {
perror("chroot");
goto CLOSE_CONNECTION;
}
if (chdir("/") != 0) {
goto CLOSE_CONNECTION;
}
if (cfg.verbose) printf("data received: %s\n", buffer);
request_parse(buffer, &uri);
if (cfg.verbose) request_print_uri(&uri);
if (uri.error != 0) {
if (cfg.verbose) printf("(1) E=%d\n", uri.error);
resp_error(RESP_STATUS_BAD_REQUEST, ssl);
goto CLOSE_CONNECTION;
}
request_validate_uri(&uri);
if (uri.error != 0) {
if (cfg.verbose) printf("(2) E=%d\n", uri.error);
switch (uri.error) {
case REQUEST_ERR_WRONG_SCHEME:
case REQUEST_ERR_WRONG_DOMAIN:
case REQUEST_ERR_PORT:
resp_error(RESP_STATUS_PROXY_REFUSED, ssl);
break;
case REQUEST_ERR_PATH:
case REQUEST_ERR_SCHEME:
case REQUEST_ERR_DOMAIN:
resp_error(RESP_STATUS_BAD_REQUEST, ssl);
break;
}
goto CLOSE_CONNECTION;
}
if ((err = resp_serve_file(&uri, ssl))) {
switch(err) {
case RESP_FILE_NOT_FOUND:
if (cfg.verbose) puts("file not found");
resp_error(RESP_STATUS_NOT_FOUND, ssl);
break;
case RESP_FILE_TRANSFER:
if (cfg.verbose) puts("file transfer failed");
break;
case RESP_FILE_PATH_TOO_LONG:
if (cfg.verbose) puts("provided file path too long");
break;
default:
if (cfg.verbose) printf("unknown error: %d\n", err);
}
goto CLOSE_CONNECTION;
}
if (cfg.verbose) puts("OK\n");
CLOSE_CONNECTION:
SSL_shutdown(ssl);
SSL_free(ssl);
SSL_CTX_free(ctx);
close(client);
_exit(0);
} else if (pid == -1) {
perror("fork");
exit(1);
}
}
exit(0);
}
/* print usage and exit */
static void usage(char *argv[]) {
fprintf(stderr, "Usage:\n%s [OPTIONS]\n", argv[0]);
fprintf(stderr, "\t-c [pub cert path] ex: -c \"gem.crt\" (tls/server.crt default)\n");
fprintf(stderr, "\t-k [priv key path] ex: -k \"gem.key\" (tls/server.key default)\n");
fprintf(stderr, "\t-h [HOSTNAME] ex: -h \"example.com\" (localhost default)\n");
fprintf(stderr, "\t-p [PORT] ex: -p 1965 (default)\n");
fprintf(stderr, "\t-d [DOC ROOT] ex: -d \"/var/gemini\"\n");
fprintf(stderr, "\t-i [INDEX FILE] ex: -i \"index.gmi\" (default)\n");
fprintf(stderr, "\t-e enumerate directories without an index file\n");
fprintf(stderr, "\t-a permit requests with a different hostname\n");
fprintf(stderr, "\t-v print request information\n");
exit(1);
}
static void startup_check(void) {
if (!file_exists(cfg.crt_path) || !file_exists(cfg.key_path)) {
fprintf(stderr, "unable to find ssl keys\n");
fprintf(stderr, "Public key: %s\n", cfg.crt_path);
fprintf(stderr, "Private key: %s\n", cfg.key_path);
exit (1);
}
}
/* log remote ipv4 */
static void log_connection(int client_fd) {
struct sockaddr_in client_addr;
socklen_t client_addr_len = sizeof(client_addr);
char client_ip[INET_ADDRSTRLEN];
if (getpeername(client_fd, (struct sockaddr *)&client_addr, &client_addr_len) == -1) {
return;
}
inet_ntop(AF_INET, &client_addr.sin_addr, client_ip, sizeof(client_ip));
printf("Remote: %s\n", client_ip);
}