-
Notifications
You must be signed in to change notification settings - Fork 315
/
tunnel.c
509 lines (431 loc) · 13 KB
/
tunnel.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/*
* Copyright (C) 2015 Adrien Vergé
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give permission
* to link the code of portions of this program with the OpenSSL library under
* certain conditions as described in each individual source file, and
* distribute linked combinations including the two.
* You must obey the GNU General Public License in all respects for all of the
* code used other than OpenSSL. If you modify file(s) with this exception,
* you may extend this exception to your version of the file(s), but you are
* not obligated to do so. If you do not wish to do so, delete this exception
* statement from your version. If you delete this exception statement from
* all source files in the program, then also delete it here.
*/
#include <fcntl.h>
#include <ifaddrs.h>
#include <netdb.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <openssl/err.h>
#include <pty.h>
#include <sys/wait.h>
#include "http.h"
#include "log.h"
static int on_ppp_if_up(struct tunnel *tunnel)
{
log_info("Interface %s is UP.\n", tunnel->ppp_iface);
if (tunnel->config->set_routes) {
log_info("Setting new routes...\n");
ipv4_set_tunnel_routes(tunnel);
}
if (tunnel->config->set_dns) {
log_info("Adding VPN nameservers...\n");
ipv4_add_nameservers_to_resolv_conf(tunnel);
}
log_info("Tunnel is up and running.\n");
return 0;
}
static int on_ppp_if_down(struct tunnel *tunnel)
{
log_info("Setting ppp interface down.\n");
if (tunnel->config->set_routes) {
log_info("Restoring routes...\n");
ipv4_restore_routes(tunnel);
}
if (tunnel->config->set_dns) {
log_info("Removing VPN nameservers...\n");
ipv4_del_nameservers_from_resolv_conf(tunnel);
}
return 0;
}
static int pppd_run(struct tunnel *tunnel)
{
pid_t pid;
int amaster;
struct termios termp;
termp.c_cflag = B9600;
termp.c_cc[VTIME] = 0;
termp.c_cc[VMIN] = 1;
pid = forkpty(&amaster, NULL, &termp, NULL);
if (pid == -1) {
log_error("forkpty: %s\n", strerror(errno));
return 1;
} else if (pid == 0) {
int i = 16;
char *args[] = {
"/usr/sbin/pppd", "38400", "noipdefault", "noaccomp",
"noauth", "default-asyncmap", "nopcomp", "receive-all",
"nodefaultroute", ":1.1.1.1", "nodetach",
"lcp-max-configure", "40", "usepeerdns", "mru", "1354",
NULL, NULL, NULL,
NULL, NULL, NULL
};
if (tunnel->config->pppd_log) {
args[i++] = "debug";
args[i++] = "logfile";
args[i++] = tunnel->config->pppd_log;
}
if (tunnel->config->pppd_plugin) {
args[i++] = "plugin";
args[i++] = tunnel->config->pppd_plugin;
}
close(tunnel->ssl_socket);
if (execvp(args[0], args) == -1) {
log_error("execvp: %s\n", strerror(errno));
return 1;
}
}
// Set non-blocking
int flags;
if ((flags = fcntl(amaster, F_GETFL, 0)) == -1)
flags = 0;
if (fcntl(amaster, F_SETFL, flags | O_NONBLOCK) == -1) {
log_error("fcntl: %s\n", strerror(errno));
return 1;
}
tunnel->pppd_pid = pid;
tunnel->pppd_pty = amaster;
return 0;
}
static int pppd_terminate(struct tunnel *tunnel)
{
close(tunnel->pppd_pty);
log_debug("Waiting for pppd to exit...\n");
waitpid(tunnel->pppd_pid, NULL, 0);
return 0;
}
int ppp_interface_is_up(struct tunnel *tunnel)
{
struct ifaddrs *ifap, *ifa;
if (getifaddrs(&ifap)) {
log_error("getifaddrs: %s\n", strerror(errno));
return 0;
}
for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
if (strstr(ifa->ifa_name, "ppp") != NULL
&& ifa->ifa_flags & IFF_UP) {
strncpy(tunnel->ppp_iface, ifa->ifa_name,
ROUTE_IFACE_LEN - 1);
freeifaddrs(ifap);
return 1;
}
}
freeifaddrs(ifap);
return 0;
}
static int get_gateway_host_ip(struct tunnel *tunnel)
{
struct hostent *host = gethostbyname(tunnel->config->gateway_host);
if (host == NULL) {
log_error("gethostbyname: %s\n", strerror(h_errno));
return 1;
}
tunnel->config->gateway_ip = *((struct in_addr *)
host->h_addr_list[0]);
setenv("VPN_GATEWAY", inet_ntoa(tunnel->config->gateway_ip), 0);
return 0;
}
/*
* Establish a regular TCP connection.
*/
static int tcp_connect(struct tunnel *tunnel)
{
int ret, handle;
struct sockaddr_in server;
handle = socket(AF_INET, SOCK_STREAM, 0);
if (handle == -1) {
log_error("socket: %s\n", strerror(errno));
return -1;
}
server.sin_family = AF_INET;
server.sin_port = htons(tunnel->config->gateway_port);
server.sin_addr = tunnel->config->gateway_ip;
bzero(&(server.sin_zero), 8);
ret = connect(handle, (struct sockaddr *) &server, sizeof(server));
if (ret == -1) {
log_error("connect: %s\n", strerror(errno));
return -1;
}
return handle;
}
static int ssl_verify_cert(struct tunnel *tunnel)
{
int ret = -1;
unsigned char digest[SHA256LEN];
unsigned len;
struct x509_digest *elem;
char digest_str[SHA256STRLEN], *subject, *issuer;
char *line;
int i;
X509_NAME *subj;
char common_name[FIELD_SIZE + 1];
SSL_set_verify(tunnel->ssl_handle, SSL_VERIFY_PEER, NULL);
X509 *cert = SSL_get_peer_certificate(tunnel->ssl_handle);
if (cert == NULL) {
log_error("Unable to get gateway certificate.\n");
return 1;
}
subj = X509_get_subject_name(cert);
// Try to validate certificate using local PKI
if (subj
&& X509_NAME_get_text_by_NID(subj, NID_commonName, common_name,
FIELD_SIZE) > 0
&& strncasecmp(common_name, tunnel->config->gateway_host,
FIELD_SIZE) == 0
&& SSL_get_verify_result(tunnel->ssl_handle) == X509_V_OK) {
log_debug("Gateway certificate validation succeeded.\n");
ret = 0;
goto free_cert;
}
log_debug("Gateway certificate validation failed.\n");
// If validation failed, check if cert is in the white list
if (X509_digest(cert, EVP_sha256(), digest, &len) <= 0
|| len != SHA256LEN) {
log_error("Could not compute certificate sha256 digest.\n");
goto free_cert;
}
// Encode digest in base16
for (i = 0; i < SHA256LEN; i++)
sprintf(&digest_str[2 * i], "%02x", digest[i]);
// Is it in whitelist?
for (elem = tunnel->config->cert_whitelist; elem != NULL;
elem = elem->next)
if (memcmp(digest_str, elem->data, SHA256STRLEN) == 0)
break;
if (elem != NULL) { // break before end of loop
log_debug("Gateway certificate digest found in white list.\n");
ret = 0;
goto free_cert;
}
subject = X509_NAME_oneline(subj, NULL, 0);
issuer = X509_NAME_oneline(X509_get_issuer_name(cert), NULL, 0);
log_error("Gateway certificate validation failed, and the certificate "
"digest in not in the local whitelist. If you trust it, "
"rerun with:\n");
log_error(" --trusted-cert %s\n", digest_str);
log_error("or add this line to your config file:\n");
log_error(" trusted-cert = %s\n", digest_str);
log_error("Gateway certificate:\n");
log_error(" subject:\n");
for (line = strtok(subject, "/"); line != NULL;
line = strtok(NULL, "/"))
log_error(" %s\n", line);
log_error(" issuer:\n");
for (line = strtok(issuer, "/"); line != NULL;
line = strtok(NULL, "/"))
log_error(" %s\n", line);
log_error(" sha256 digest:\n");
log_error(" %s\n", digest_str);
free_cert:
X509_free(cert);
return ret;
}
/*
* Destroy and free the SSL connection to the gateway.
*/
static void ssl_disconnect(struct tunnel *tunnel)
{
if (!tunnel->ssl_handle)
return;
SSL_shutdown(tunnel->ssl_handle);
SSL_free(tunnel->ssl_handle);
SSL_CTX_free(tunnel->ssl_context);
close(tunnel->ssl_socket);
tunnel->ssl_handle = NULL;
tunnel->ssl_context = NULL;
}
/*
* Connects to the gateway and initiate a SSL session.
*/
int ssl_connect(struct tunnel *tunnel)
{
ssl_disconnect (tunnel);
tunnel->ssl_socket = tcp_connect(tunnel);
if (tunnel->ssl_socket == -1)
return 1;
// Register the error strings for libcrypto & libssl
SSL_load_error_strings();
// Register the available ciphers and digests
SSL_library_init();
tunnel->ssl_context = SSL_CTX_new(SSLv23_client_method());
if (tunnel->ssl_context == NULL) {
log_error("SSL_CTX_new: %s\n",
ERR_error_string(ERR_peek_last_error(), NULL));
return 1;
}
if (tunnel->config->ca_file) {
if (!SSL_CTX_load_verify_locations(
tunnel->ssl_context,
tunnel->config->ca_file, NULL)) {
log_error("SSL_CTX_load_verify_locations: %s\n",
ERR_error_string(ERR_peek_last_error(),
NULL));
return 1;
}
}
if (tunnel->config->user_cert) {
if (!SSL_CTX_use_certificate_file(
tunnel->ssl_context, tunnel->config->user_cert,
SSL_FILETYPE_PEM)) {
log_error("SSL_CTX_use_certificate_file: %s\n",
ERR_error_string(ERR_peek_last_error(),
NULL));
return 1;
}
}
if (tunnel->config->user_key) {
if (!SSL_CTX_use_PrivateKey_file(
tunnel->ssl_context, tunnel->config->user_key,
SSL_FILETYPE_PEM)) {
log_error("SSL_CTX_use_PrivateKey_file: %s\n",
ERR_error_string(ERR_peek_last_error(),
NULL));
return 1;
}
}
if (tunnel->config->user_cert && tunnel->config->user_key) {
if (!SSL_CTX_check_private_key(tunnel->ssl_context)) {
log_error("SSL_CTX_check_private_key: %s\n",
ERR_error_string(ERR_peek_last_error(), NULL));
return 1;
}
}
tunnel->ssl_handle = SSL_new(tunnel->ssl_context);
if (tunnel->ssl_handle == NULL) {
log_error("SSL_new: %s\n",
ERR_error_string(ERR_peek_last_error(), NULL));
return 1;
}
if (!SSL_set_fd(tunnel->ssl_handle, tunnel->ssl_socket)) {
log_error("SSL_set_fd: %s\n",
ERR_error_string(ERR_peek_last_error(), NULL));
return 1;
}
SSL_set_mode(tunnel->ssl_handle, SSL_MODE_AUTO_RETRY);
// Initiate SSL handshake
if (SSL_connect(tunnel->ssl_handle) != 1) {
log_error("SSL_connect: %s\n",
ERR_error_string(ERR_peek_last_error(), NULL));
return 1;
}
SSL_set_mode(tunnel->ssl_handle, SSL_MODE_AUTO_RETRY);
if (tunnel->config->verify_cert)
if (ssl_verify_cert(tunnel))
return 1;
// Disable SIGPIPE (occurs when trying to write to an already-closed
// socket).
signal(SIGPIPE, SIG_IGN);
return 0;
}
int run_tunnel(struct vpn_config *config)
{
int ret;
struct tunnel tunnel;
tunnel.config = config;
tunnel.on_ppp_if_up = on_ppp_if_up;
tunnel.on_ppp_if_down = on_ppp_if_down;
tunnel.ipv4.ns1_addr.s_addr = 0;
tunnel.ipv4.ns2_addr.s_addr = 0;
tunnel.ssl_handle = NULL;
tunnel.ssl_context = NULL;
tunnel.state = STATE_DOWN;
// Step 0: get gateway host IP
ret = get_gateway_host_ip(&tunnel);
if (ret)
goto err_tunnel;
// Step 1: open a SSL connection to the gateway
ret = ssl_connect(&tunnel);
if (ret)
goto err_tunnel;
log_info("Connected to gateway.\n");
// Step 2: connect to the HTTP interface and authenticate to get a
// cookie
ret = auth_log_in(&tunnel);
if (ret != 1) {
log_error("Could not authenticate to gateway (%s).\n",
err_http_str(ret));
ret = 1;
goto err_tunnel;
}
log_info("Authenticated.\n");
log_debug("Cookie: %s\n", config->cookie);
ret = auth_request_vpn_allocation(&tunnel);
if (ret != 1) {
log_error("VPN allocation request failed (%s).\n",
err_http_str(ret));
ret = 1;
goto err_tunnel;
}
log_info("Remote gateway has allocated a VPN.\n");
ret = ssl_connect(&tunnel);
if (ret)
goto err_tunnel;
// Step 3: get configuration
ret = auth_get_config(&tunnel);
if (ret != 1) {
log_error("Could not get VPN configuration (%s).\n",
err_http_str(ret));
ret = 1;
goto err_tunnel;
}
// Step 4: run a pppd process
ret = pppd_run(&tunnel);
if (ret)
goto err_tunnel;
// Step 5: ask gateway to start tunneling
ret = http_send(&tunnel,
"GET /remote/sslvpn-tunnel HTTP/1.1\n"
"Host: sslvpn\n"
"Cookie: %s\n\n%c",
tunnel.config->cookie, '\0');
if (ret != 1) {
log_error("Could not start tunnel (%s).\n", err_http_str(ret));
ret = 1;
goto err_start_tunnel;
}
tunnel.state = STATE_CONNECTING;
ret = 0;
// Step 6: perform io between pppd and the gateway, while tunnel is up
io_loop(&tunnel);
if (tunnel.state == STATE_UP)
if (tunnel.on_ppp_if_down != NULL)
tunnel.on_ppp_if_down(&tunnel);
tunnel.state = STATE_DISCONNECTING;
err_start_tunnel:
pppd_terminate(&tunnel);
log_info("Terminated pppd.\n");
err_tunnel:
log_info("Closed connection to gateway.\n");
tunnel.state = STATE_DOWN;
if (ssl_connect(&tunnel)) {
log_info("Could not log out.\n");
} else {
auth_log_out(&tunnel);
log_info("Logged out.\n");
}
return ret;
}