-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpst.c
1524 lines (1352 loc) · 50.4 KB
/
gpst.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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* OpenConnect (SSL + DTLS) VPN client
*
* Copyright © 2016-2017 Daniel Lenski
*
* Author: Daniel Lenski <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 2.1, as published by the Free Software Foundation.
*
* 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
* Lesser General Public License for more details.
*/
#include <config.h>
#include "openconnect-internal.h"
#ifdef HAVE_LZ4
#include <lz4.h>
#endif
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#ifdef _WIN32
#include "win32-ipicmp.h"
#else
#include <sys/wait.h>
/* The BSDs require the first two headers before netinet/ip.h
* (Linux and macOS already #include them within netinet/ip.h)
*/
#include <netinet/in_systm.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <netinet/ip6.h>
#include <netinet/icmp6.h>
#endif
#if defined(__linux__)
/* For TCP_INFO */
# include <linux/tcp.h>
#endif
#include <time.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
/*
* Data packets are encapsulated in the SSL stream as follows:
*
* 0000: Magic "\x1a\x2b\x3c\x4d"
* 0004: Big-endian EtherType (0x0800 for IPv4)
* 0006: Big-endian 16-bit length (not including 16-byte header)
* 0008: Always "\x01\0\0\0\0\0\0\0"
* 0010: data payload
*/
/* Strange initialisers here to work around GCC PR#10676 (which was
* fixed in GCC 4.6 but it takes a while for some systems to catch
* up. */
static const struct pkt dpd_pkt = {
.next = NULL,
{ .gpst.hdr = { 0x1a, 0x2b, 0x3c, 0x4d } }
};
static int filter_opts(struct oc_text_buf *buf, const char *query, const char *incexc, int include)
{
const char *f, *endf, *eq;
const char *found, *comma;
for (f = query; *f; f=(*endf) ? endf+1 : endf) {
endf = strchrnul(f, '&');
eq = strchr(f, '=');
if (!eq || eq > endf)
eq = endf;
for (found = incexc; *found; found=(*comma) ? comma+1 : comma) {
comma = strchrnul(found, ',');
if (!strncmp(found, f, MAX(comma-found, eq-f)))
break;
}
if ((include && *found) || (!include && !*found)) {
if (buf->pos && buf->data[buf->pos-1] != '?' && buf->data[buf->pos-1] != '&')
buf_append(buf, "&");
buf_append_bytes(buf, f, (int)(endf-f));
}
}
return buf_error(buf);
}
/* Parse this JavaScript-y mess:
"var respStatus = \"Challenge|Error\";\n"
"var respMsg = \"<prompt>\";\n"
"thisForm.inputStr.value = "<inputStr>";\n"
*/
static int parse_javascript(char *buf, char **prompt, char **inputStr)
{
const char *start, *end = buf;
int status;
const char *pre_status = "var respStatus = \"",
*pre_prompt = "var respMsg = \"",
*pre_inputStr = "thisForm.inputStr.value = \"";
/* Status */
while (isspace(*end))
end++;
if (strncmp(end, pre_status, strlen(pre_status)))
goto err;
start = end+strlen(pre_status);
end = strchr(start, '\n');
if (!end || end[-1] != ';' || end[-2] != '"')
goto err;
if (!strncmp(start, "Challenge", 8)) status = 0;
else if (!strncmp(start, "Error", 5)) status = 1;
else goto err;
/* Prompt */
while (isspace(*end))
end++;
if (strncmp(end, pre_prompt, strlen(pre_prompt)))
goto err;
start = end+strlen(pre_prompt);
end = strchr(start, '\n');
if (!end || end[-1] != ';' || end[-2] != '"' || (end<start+2))
goto err;
if (prompt)
*prompt = strndup(start, end-start-2);
/* inputStr */
while (isspace(*end))
end++;
if (strncmp(end, pre_inputStr, strlen(pre_inputStr)))
goto err2;
start = end+strlen(pre_inputStr);
end = strchr(start, '\n');
if (!end || end[-1] != ';' || end[-2] != '"' || (end<start+2))
goto err2;
if (inputStr)
*inputStr = strndup(start, end-start-2);
while (isspace(*end))
end++;
if (*end != '\0')
goto err3;
return status;
err3:
if (inputStr) free(*inputStr);
err2:
if (prompt) free(*prompt);
err:
return -EINVAL;
}
int gpst_xml_or_error(struct openconnect_info *vpninfo, char *response,
int (*xml_cb)(struct openconnect_info *, xmlNode *xml_node, void *cb_data),
int (*challenge_cb)(struct openconnect_info *, char *prompt, char *inputStr, void *cb_data),
void *cb_data)
{
xmlDocPtr xml_doc;
xmlNode *xml_node;
char *err = NULL;
char *prompt = NULL, *inputStr = NULL;
int result = -EINVAL;
if (!response) {
vpn_progress(vpninfo, PRG_ERR,
_("Empty response from server\n"));
return -EINVAL;
}
/* is it XML? */
xml_doc = xmlReadMemory(response, strlen(response), "noname.xml", NULL,
XML_PARSE_NOERROR);
if (!xml_doc) {
/* is it Javascript? */
result = parse_javascript(response, &prompt, &inputStr);
switch (result) {
case 1:
vpn_progress(vpninfo, PRG_ERR, _("%s\n"), prompt);
break;
case 0:
vpn_progress(vpninfo, PRG_INFO, _("Challenge: %s\n"), prompt);
result = challenge_cb ? challenge_cb(vpninfo, prompt, inputStr, cb_data) : -EINVAL;
break;
default:
goto bad_xml;
}
free(prompt);
free(inputStr);
goto bad_xml;
}
xml_node = xmlDocGetRootElement(xml_doc);
/* is it <response status="error"><error>..</error></response> ? */
if (xmlnode_is_named(xml_node, "response")
&& !xmlnode_match_prop(xml_node, "status", "error")) {
for (xml_node=xml_node->children; xml_node; xml_node=xml_node->next) {
if (!xmlnode_get_val(xml_node, "error", &err))
goto out;
}
goto bad_xml;
}
/* Is it <prelogin-response><status>Error</status><msg>..</msg></prelogin-response> ? */
if (xmlnode_is_named(xml_node, "prelogin-response")) {
char *s = NULL;
int has_err = 0;
xmlNode *x;
for (x=xml_node->children; x; x=x->next) {
if (!xmlnode_get_val(x, "status", &s))
has_err = strcmp(s, "Success");
else
xmlnode_get_val(x, "msg", &err);
}
free(s);
if (has_err)
goto out;
free(err);
err = NULL;
}
/* is it <challenge><user>user.name</user><inputstr>...</inputstr><respmsg>...</respmsg></challenge> */
if (xmlnode_is_named(xml_node, "challenge")) {
for (xml_node=xml_node->children; xml_node; xml_node=xml_node->next) {
xmlnode_get_val(xml_node, "inputstr", &inputStr);
xmlnode_get_val(xml_node, "respmsg", &prompt);
/* XXX: override the username passed to the next form from <user> ? */
}
result = challenge_cb ? challenge_cb(vpninfo, prompt, inputStr, cb_data) : -EINVAL;
free(prompt);
free(inputStr);
goto bad_xml;
}
/* if it's XML, invoke callback (or default to success) */
result = xml_cb ? xml_cb(vpninfo, xml_node, cb_data) : 0;
bad_xml:
if (result == -EINVAL) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to parse server response\n"));
vpn_progress(vpninfo, PRG_DEBUG,
_("Response was: %s\n"), response);
}
out:
if (err) {
if (!strcmp(err, "GlobalProtect gateway does not exist")
|| !strcmp(err, "GlobalProtect portal does not exist")) {
vpn_progress(vpninfo, PRG_DEBUG, "%s\n", err);
result = -EEXIST;
} else if (!strcmp(err, "Invalid authentication cookie") /* equivalent to custom HTTP status 512 */
|| !strcmp(err, "Portal name not found") /* cookie is bogus */
|| !strcmp(err, "Valid client certificate is required") /* equivalent to custom HTTP status 513 */
|| !strcmp(err, "Allow Automatic Restoration of SSL VPN is disabled")) {
/* Any of these errors indicates that retrying won't help us reconnect (EPERM signals this to mainloop.) */
vpn_progress(vpninfo, PRG_ERR, "%s\n", err);
result = -EPERM;
} else {
vpn_progress(vpninfo, PRG_ERR, "%s\n", err);
result = -EINVAL;
}
free(err);
}
if (xml_doc)
xmlFreeDoc(xml_doc);
return result;
}
#define ESP_HEADER_SIZE (4 /* SPI */ + 4 /* sequence number */)
#define ESP_FOOTER_SIZE (1 /* pad length */ + 1 /* next header */)
#ifdef HAVE_ESP
static int check_hmac_algo(struct openconnect_info *v, const char *s)
{
if (!strcmp(s, "sha1")) return HMAC_SHA1;
if (!strcmp(s, "md5")) return HMAC_MD5;
if (!strcmp(s, "sha256")) return HMAC_SHA256;
vpn_progress(v, PRG_ERR, _("Unknown ESP MAC algorithm: %s"), s);
return -ENOENT;
}
static int check_enc_algo(struct openconnect_info *v, const char *s)
{
if (!strcmp(s, "aes128") || !strcmp(s, "aes-128-cbc")) return ENC_AES_128_CBC;
if (!strcmp(s, "aes-256-cbc")) return ENC_AES_256_CBC;
vpn_progress(v, PRG_ERR, _("Unknown ESP encryption algorithm: %s"), s);
return -ENOENT;
}
/* Reads <KEYTAG/><bits>N</bits><val>hex digits</val></KEYTAG> and saves the
* key in dest, returning its length in bytes.
*/
static int xml_to_key(xmlNode *xml_node, unsigned char *dest, int dest_size)
{
int explen = -1, len = 0;
xmlNode *child;
char *p, *s = NULL;
for (child = xml_node->children; child; child=child->next) {
if (xmlnode_get_val(child, "bits", &s) == 0) {
explen = atoi(s);
if (explen & 0x07) goto out;
explen >>= 3;
} else if (xmlnode_get_val(child, "val", &s) == 0) {
for (p=s; p[0] && p[1]; p+=2)
if (len++ < dest_size)
*dest++ = unhex(p);
}
}
out:
free(s);
return (len == explen) ? len : -EINVAL;
}
#endif
/* Return value:
* < 0, on error
* = 0, on success; *form is populated
*/
static int gpst_parse_config_xml(struct openconnect_info *vpninfo, xmlNode *xml_node, void *cb_data)
{
xmlNode *member;
char *s = NULL, *deferred_netmask = NULL;
struct oc_split_include *inc;
int split_route_is_default_route = 0;
int n_dns = 0, esp_keys = 0, esp_v4 = 0, esp_v6 = 0;
int ret = 0;
int ii;
uint32_t esp_magic = 0;
struct in6_addr esp6_magic;
if (!xml_node || !xmlnode_is_named(xml_node, "response"))
return -EINVAL;
struct oc_vpn_option *new_opts = NULL;
struct oc_ip_info new_ip_info = {};
memset(vpninfo->esp_magic, 0, sizeof(vpninfo->esp_magic));
vpninfo->esp_replay_protect = 1;
vpninfo->ssl_times.rekey_method = REKEY_NONE;
/* Parse config */
for (xml_node = xml_node->children; xml_node; xml_node=xml_node->next) {
if (!xmlnode_get_val(xml_node, "ip-address", &s))
new_ip_info.addr = add_option_steal(&new_opts, "ipaddr", &s);
else if (!xmlnode_get_val(xml_node, "ip-address-v6", &s)) {
if (!vpninfo->disable_ipv6)
new_ip_info.addr6 = add_option_steal(&new_opts, "ipaddr6", &s);
} else if (!xmlnode_get_val(xml_node, "netmask", &deferred_netmask)) {
/* XX: GlobalProtect servers always (almost always?) send 255.255.255.255 as their netmask
* (a /32 host route), and if they want to include an actual default route (0.0.0.0/0)
* they instead put it under <access-routes/>. We defer saving the netmask until later.
*/
} else if (!xmlnode_get_val(xml_node, "mtu", &s))
new_ip_info.mtu = atoi(s);
else if (!xmlnode_get_val(xml_node, "lifetime", &s))
vpninfo->auth_expiration = time(NULL) + atol(s);
else if (!xmlnode_get_val(xml_node, "quarantine", &s)) {
if (strcmp(s, "no"))
vpn_progress(vpninfo, PRG_DEBUG,
_("WARNING: Config XML contains <quarantine> tag with value of \"%s\".\n"
" VPN connectivity may be disabled or limited.\n"), s);
} else if (!xmlnode_get_val(xml_node, "disconnect-on-idle", &s)) {
int sec = atoi(s);
vpn_progress(vpninfo, PRG_INFO, _("Idle timeout is %d minutes.\n"), sec/60);
vpninfo->idle_timeout = sec;
} else if (!xmlnode_get_val(xml_node, "ssl-tunnel-url", &s)) {
free(vpninfo->urlpath);
vpninfo->urlpath = s;
if (strcmp(s, "/ssl-tunnel-connect.sslvpn"))
vpn_progress(vpninfo, PRG_INFO, _("Non-standard SSL tunnel path: %s\n"), s);
s = NULL;
} else if (!xmlnode_get_val(xml_node, "timeout", &s)) {
int sec = atoi(s);
vpn_progress(vpninfo, PRG_INFO, _("Tunnel timeout (rekey interval) is %d minutes.\n"), sec/60);
vpninfo->ssl_times.last_rekey = time(NULL);
vpninfo->ssl_times.rekey = sec - 60;
vpninfo->ssl_times.rekey_method = REKEY_TUNNEL;
} else if (!xmlnode_get_val(xml_node, "gw-address", &s)) {
/* As remarked in oncp.c, "this is a tunnel; having a
* gateway is meaningless." See esp_send_probes_gp for the
* gory details of what this field actually means.
*/
if (vpninfo->peer_addr->sa_family == IPPROTO_IP &&
vpninfo->ip_info.gateway_addr && strcmp(s, vpninfo->ip_info.gateway_addr))
vpn_progress(vpninfo, PRG_DEBUG,
_("Gateway address in config XML (%s) differs from external gateway address (%s).\n"), s, new_ip_info.gateway_addr);
esp_magic = inet_addr(s);
esp_v4 = 1;
} else if (!xmlnode_get_val(xml_node, "gw-address-v6", &s)) {
if (vpninfo->peer_addr->sa_family == IPPROTO_IPV6 &&
vpninfo->ip_info.gateway_addr && strcmp(s, vpninfo->ip_info.gateway_addr))
vpn_progress(vpninfo, PRG_DEBUG,
_("IPv6 gateway address in config XML (%s) differs from external gateway address (%s).\n"), s, vpninfo->ip_info.gateway_addr);
inet_pton(AF_INET6, s, &esp6_magic);
esp_v6 = 1;
} else if (!xmlnode_get_val(xml_node, "connected-gw-ip", &s)) {
if (vpninfo->ip_info.gateway_addr && strcmp(s, vpninfo->ip_info.gateway_addr))
vpn_progress(vpninfo, PRG_DEBUG, _("Config XML <connected-gw-ip> address (%s) differs from external\n"
"gateway address (%s). Please report any this to\n"
"<[email protected]>, including any problems\n"
"with ESP or other apparent loss of connectivity or performance.\n"),
s, vpninfo->ip_info.gateway_addr);
} else if (xmlnode_is_named(xml_node, "dns-v6") ||
xmlnode_is_named(xml_node, "dns")) {
for (member = xml_node->children; member && n_dns<3; member=member->next) {
if (!xmlnode_get_val(member, "member", &s)) {
for (ii=0; ii<n_dns; ii++)
/* XX: frequent duplicates between <dns> and <dns-v6> */
if (!strcmp(s, new_ip_info.dns[ii]))
break;
if (ii==n_dns)
new_ip_info.dns[n_dns++] = add_option_steal(&new_opts, "DNS", &s);
}
}
} else if (xmlnode_is_named(xml_node, "wins")) {
for (ii=0, member = xml_node->children; member && ii<3; member=member->next)
if (!xmlnode_get_val(member, "member", &s))
new_ip_info.nbns[ii++] = add_option_steal(&new_opts, "WINS", &s);
} else if (xmlnode_is_named(xml_node, "dns-suffix")) {
struct oc_text_buf *domains = buf_alloc();
for (member = xml_node->children; member; member=member->next)
if (!xmlnode_get_val(member, "member", &s))
buf_append(domains, "%s ", s);
if (buf_error(domains) == 0 && domains->pos > 0) {
domains->data[domains->pos-1] = '\0';
new_ip_info.domain = add_option_steal(&new_opts, "search", &domains->data);
}
buf_free(domains);
} else if (xmlnode_is_named(xml_node, "access-routes-v6") || xmlnode_is_named(xml_node, "exclude-access-routes-v6") ||
xmlnode_is_named(xml_node, "access-routes") || xmlnode_is_named(xml_node, "exclude-access-routes")) {
for (member = xml_node->children; member; member=member->next) {
if (!xmlnode_get_val(member, "member", &s)) {
int is_inc = (xml_node->name[0] == 'a');
/* XX: if this is a default Legacy IP route jammed into the split-include
* routes, just mark it for now.
*/
if (is_inc && !strcmp(s, "0.0.0.0/0")) {
split_route_is_default_route = 1;
continue;
}
inc = malloc(sizeof(*inc));
if (!inc) {
ret = -ENOMEM;
goto err;
}
if (is_inc) {
inc->route = add_option_steal(&new_opts, "split-include", &s);
inc->next = new_ip_info.split_includes;
new_ip_info.split_includes = inc;
} else {
inc->route = add_option_steal(&new_opts, "split-exclude", &s);
inc->next = new_ip_info.split_excludes;
new_ip_info.split_excludes = inc;
}
}
}
} else if (xmlnode_is_named(xml_node, "ipsec")) {
#ifdef HAVE_ESP
if (vpninfo->dtls_state != DTLS_DISABLED) {
int c = (vpninfo->current_esp_in ^= 1);
struct esp *ei = &vpninfo->esp_in[c], *eo = &vpninfo->esp_out;
vpninfo->old_esp_maxseq = vpninfo->esp_in[c^1].seq + 32;
for (member = xml_node->children; member; member=member->next) {
if (!xmlnode_get_val(member, "udp-port", &s)) udp_sockaddr(vpninfo, atoi(s));
else if (!xmlnode_get_val(member, "enc-algo", &s)) vpninfo->esp_enc = check_enc_algo(vpninfo, s);
else if (!xmlnode_get_val(member, "hmac-algo", &s)) vpninfo->esp_hmac = check_hmac_algo(vpninfo, s);
else if (!xmlnode_get_val(member, "c2s-spi", &s)) eo->spi = htonl(strtoul(s, NULL, 16));
else if (!xmlnode_get_val(member, "s2c-spi", &s)) ei->spi = htonl(strtoul(s, NULL, 16));
else if (xmlnode_is_named(member, "ekey-c2s")) vpninfo->enc_key_len = xml_to_key(member, eo->enc_key, sizeof(eo->enc_key));
else if (xmlnode_is_named(member, "ekey-s2c")) vpninfo->enc_key_len = xml_to_key(member, ei->enc_key, sizeof(ei->enc_key));
else if (xmlnode_is_named(member, "akey-c2s")) vpninfo->hmac_key_len = xml_to_key(member, eo->hmac_key, sizeof(eo->hmac_key));
else if (xmlnode_is_named(member, "akey-s2c")) vpninfo->hmac_key_len = xml_to_key(member, ei->hmac_key, sizeof(ei->hmac_key));
else if (!xmlnode_get_val(member, "ipsec-mode", &s) && strcmp(s, "esp-tunnel"))
vpn_progress(vpninfo, PRG_ERR, _("GlobalProtect config sent ipsec-mode=%s (expected esp-tunnel)\n"), s);
}
if (!(vpninfo->esp_enc > 0 && vpninfo->esp_hmac > 0 && vpninfo->enc_key_len > 0 && vpninfo->hmac_key_len > 0))
vpn_progress(vpninfo, PRG_ERR, "Server's ESP configuration is incomplete or uses unknown algorithms.\n");
else
esp_keys = 1;
}
#else
vpn_progress(vpninfo, PRG_DEBUG, _("Ignoring ESP keys since ESP support not available in this build\n"));
#endif
} else if (xmlnode_is_named(xml_node, "need-tunnel")
|| xmlnode_is_named(xml_node, "bw-c2s")
|| xmlnode_is_named(xml_node, "bw-s2c")
|| xmlnode_is_named(xml_node, "default-gateway")
|| xmlnode_is_named(xml_node, "default-gateway-v6")
|| xmlnode_is_named(xml_node, "no-direct-access-to-local-network")
|| xmlnode_is_named(xml_node, "ip-address-preferred")
|| xmlnode_is_named(xml_node, "ip-address-v6-preferred")
|| xmlnode_is_named(xml_node, "ipv6-connection")
|| xmlnode_is_named(xml_node, "portal")
|| xmlnode_is_named(xml_node, "user")) {
/* XX: Do these have any potential value at all for routing configuration or diagnostics? */
} else if (xml_node->type == XML_ELEMENT_NODE) {
free(s);
s = (char *)xmlNodeGetContent(xml_node);
if (strchr((char *)xml_node->name, '6'))
vpn_progress(vpninfo, PRG_ERR, _("Potential IPv6-related GlobalProtect config tag <%s>: %s\n"), xml_node->name, s);
else
vpn_progress(vpninfo, PRG_DEBUG, _("Unknown GlobalProtect config tag <%s>: %s\n"), xml_node->name, s);
}
}
/* Fix the issue of a 0.0.0.0/0 "split"-include route by swapping the "split" route with the default netmask. */
if (split_route_is_default_route) {
char *original_netmask = deferred_netmask;
if ((deferred_netmask = strdup("0.0.0.0")) == NULL)
return -ENOMEM;
/* If the original netmask wasn't /32, add it as a split route */
if (new_ip_info.addr && original_netmask) {
uint32_t nm_bits = inet_addr(original_netmask);
if (nm_bits != 0xffffffff) { /* 255.255.255.255 */
struct in_addr net_addr;
inet_aton(new_ip_info.addr, &net_addr);
net_addr.s_addr &= nm_bits; /* clear host bits */
char abuf[INET_ADDRSTRLEN];
if ((inc = malloc(sizeof(*inc))) == NULL ||
asprintf(&s, "%s/%s", inet_ntop(AF_INET, &net_addr, abuf, sizeof(abuf)), original_netmask) <= 0)
return -ENOMEM;
inc->route = add_option_steal(&new_opts, "split-include", &s);
inc->next = new_ip_info.split_includes;
new_ip_info.split_includes = inc;
}
}
free(original_netmask);
}
if (deferred_netmask)
new_ip_info.netmask = add_option_steal(&new_opts, "netmask", &deferred_netmask);
/* Set 10-second DPD/keepalive (same as Windows client) unless
* overridden with --force-dpd */
if (!vpninfo->ssl_times.dpd)
vpninfo->ssl_times.dpd = 10;
vpninfo->ssl_times.keepalive = vpninfo->esp_ssl_fallback = vpninfo->ssl_times.dpd;
/* Warn about IPv6 config, if present, and ESP config, if absent */
if (new_ip_info.addr6)
vpn_progress(vpninfo, PRG_ERR,
_("GlobalProtect IPv6 support is experimental. Please report results to <[email protected]>.\n"));
#ifdef HAVE_ESP
if (esp_keys && esp_v6 && new_ip_info.addr6) {
/* We got ESP keys, an IPv6 esp_magic address, and an IPv6 address */
vpninfo->esp_magic_af = AF_INET6;
memcpy(vpninfo->esp_magic, &esp6_magic, sizeof(esp6_magic));
setup_esp_keys:
if (openconnect_setup_esp_keys(vpninfo, 0)) {
vpn_progress(vpninfo, PRG_ERR, "Failed to setup ESP keys.\n");
} else {
/* prevent race condition between esp_mainloop() and gpst_mainloop() timers */
vpninfo->dtls_times.last_rekey = time(&vpninfo->new_dtls_started);
vpninfo->delay_tunnel_reason = "awaiting GPST ESP connection";
}
} else if (esp_keys && esp_v4 && new_ip_info.addr) {
/* We got ESP keys, an IPv4 esp_magic address, and an IPv4 address */
vpninfo->esp_magic_af = AF_INET;
memcpy(vpninfo->esp_magic, &esp_magic, sizeof(esp_magic));
goto setup_esp_keys;
} else if (vpninfo->dtls_state != DTLS_DISABLED)
vpn_progress(vpninfo, PRG_ERR,
_("Did not receive ESP keys and matching gateway in GlobalProtect config; tunnel will be TLS only.\n"));
#endif
free(s);
ret = install_vpn_opts(vpninfo, new_opts, &new_ip_info);
if (ret) {
err:
free_optlist(new_opts);
free_split_routes(&new_ip_info);
}
return ret;
}
static int gpst_get_config(struct openconnect_info *vpninfo)
{
char *orig_path;
int result;
struct oc_text_buf *request_body = buf_alloc();
const char *old_addr = vpninfo->ip_info.addr;
const char *old_addr6 = vpninfo->ip_info.addr6;
const char *request_body_type = "application/x-www-form-urlencoded";
const char *method = "POST";
char *xml_buf = NULL;
/* submit getconfig request */
buf_append(request_body, "client-type=1&protocol-version=p1&app-version=5.1.5-8");
append_opt(request_body, "ipv6-support", vpninfo->disable_ipv6 ? "no" : "yes");
append_opt(request_body, "clientos", gpst_os_name(vpninfo));
append_opt(request_body, "os-version", vpninfo->platname);
append_opt(request_body, "hmac-algo", "sha1,md5,sha256");
append_opt(request_body, "enc-algo", "aes-128-cbc,aes-256-cbc");
if (old_addr || old_addr6) {
append_opt(request_body, "preferred-ip", old_addr);
append_opt(request_body, "preferred-ipv6", old_addr6);
filter_opts(request_body, vpninfo->cookie, "preferred-ip,preferred-ipv6", 0);
} else
buf_append(request_body, "&%s", vpninfo->cookie);
if ((result = buf_error(request_body)))
goto out;
orig_path = vpninfo->urlpath;
vpninfo->urlpath = strdup("ssl-vpn/getconfig.esp");
result = do_https_request(vpninfo, method, request_body_type, request_body, &xml_buf, NULL, HTTP_NO_FLAGS);
free(vpninfo->urlpath);
vpninfo->urlpath = orig_path;
/* parse getconfig result */
if (result >= 0)
result = gpst_xml_or_error(vpninfo, xml_buf, gpst_parse_config_xml, NULL, NULL);
if (result) {
/* XX: if our "cookie" is bogus (doesn't include at least 'user', 'authcookie',
* and 'portal' fields) the server will respond like this.
*/
if (result == -EINVAL && xml_buf && !strcmp(xml_buf, "errors getting SSL/VPN config"))
result = -EPERM;
goto out;
}
if (!vpninfo->ip_info.mtu) {
/* FIXME: GP gateway config always seems to be <mtu>0</mtu> */
char *no_esp_reason = NULL;
#ifdef HAVE_ESP
if (vpninfo->dtls_state == DTLS_DISABLED)
no_esp_reason = _("ESP disabled");
else if (vpninfo->dtls_state == DTLS_NOSECRET)
no_esp_reason = _("No ESP keys received");
#else
no_esp_reason = _("ESP support not available in this build");
#endif
if (!no_esp_reason)
vpninfo->ip_info.mtu = calculate_mtu(
vpninfo, 1,
ESP_HEADER_SIZE + vpninfo->hmac_out_len + MAX_IV_SIZE, /* ESP header size */
ESP_FOOTER_SIZE, /* ESP footer (contributes to payload before padding) */
16 /* blocksize for both AES-128 and AES-256 */ );
else
vpninfo->ip_info.mtu = calculate_mtu(vpninfo, 0, TLS_OVERHEAD, 0, 1);
vpn_progress(vpninfo, PRG_ERR,
_("No MTU received. Calculated %d for %s%s\n"), vpninfo->ip_info.mtu,
no_esp_reason ? "SSL tunnel. " : "ESP tunnel", no_esp_reason ? : "");
/* return -EINVAL; */
}
out:
buf_free(request_body);
free(xml_buf);
return result;
}
static int gpst_connect(struct openconnect_info *vpninfo)
{
int ret;
struct oc_text_buf *reqbuf;
static const char start_tunnel[12] = "START_TUNNEL"; /* NOT zero-terminated */
char buf[256];
/* We do NOT actually start the HTTPS tunnel if ESP is enabled and we received
* ESP keys, because the ESP keys become invalid as soon as the HTTPS tunnel
* is connected! >:-(
*/
if (vpninfo->dtls_state != DTLS_DISABLED && vpninfo->dtls_state != DTLS_NOSECRET)
return 0;
/* Connect to SSL VPN tunnel */
vpn_progress(vpninfo, PRG_DEBUG,
_("Connecting to HTTPS tunnel endpoint ...\n"));
ret = openconnect_open_https(vpninfo);
if (ret)
return ret;
reqbuf = buf_alloc();
buf_append(reqbuf, "GET %s?", vpninfo->urlpath);
filter_opts(reqbuf, vpninfo->cookie, "user,authcookie", 1);
buf_append(reqbuf, " HTTP/1.1\r\n\r\n");
if ((ret = buf_error(reqbuf)))
goto out;
if (vpninfo->dump_http_traffic)
dump_buf(vpninfo, '>', reqbuf->data);
vpninfo->ssl_write(vpninfo, reqbuf->data, reqbuf->pos);
if ((ret = vpninfo->ssl_read(vpninfo, buf, 12)) < 0) {
if (ret == -EINTR)
goto out;
vpn_progress(vpninfo, PRG_ERR,
_("Error fetching GET-tunnel HTTPS response.\n"));
ret = -EINVAL;
goto out;
}
if (!strncmp(buf, start_tunnel, sizeof(start_tunnel))) {
ret = 0;
} else if (ret==0) {
vpn_progress(vpninfo, PRG_ERR,
_("Gateway disconnected immediately after GET-tunnel request.\n"));
ret = -EPIPE;
} else {
if (ret==sizeof(start_tunnel)) {
ret = vpninfo->ssl_gets(vpninfo, buf+sizeof(start_tunnel), sizeof(buf)-sizeof(start_tunnel));
ret = (ret>0 ? ret : 0) + sizeof(start_tunnel);
}
int status = check_http_status(buf, ret);
/* XX: GP servers return 502 when they don't like the cookie */
if (status == 502)
ret = -EPERM;
else {
vpn_progress(vpninfo, PRG_ERR, _("Got unexpected HTTP response: %.*s\n"),
ret, buf);
ret = -EINVAL;
}
}
if (ret < 0)
openconnect_close_https(vpninfo, 0);
else {
monitor_fd_new(vpninfo, ssl);
monitor_read_fd(vpninfo, ssl);
monitor_except_fd(vpninfo, ssl);
vpninfo->ssl_times.last_rx = vpninfo->ssl_times.last_tx = time(NULL);
/* connecting the HTTPS tunnel totally invalidates the ESP keys,
hence shutdown */
if (vpninfo->proto->udp_shutdown)
vpninfo->proto->udp_shutdown(vpninfo);
}
out:
buf_free(reqbuf);
return ret;
}
static int parse_hip_report_check(struct openconnect_info *vpninfo, xmlNode *xml_node, void *cb_data)
{
char *s = NULL;
int result = -EINVAL;
if (!xml_node || !xmlnode_is_named(xml_node, "response"))
goto out;
for (xml_node = xml_node->children; xml_node; xml_node=xml_node->next) {
if (!xmlnode_get_val(xml_node, "hip-report-needed", &s)) {
if (!strcmp(s, "no"))
result = 0;
else if (!strcmp(s, "yes"))
result = -EAGAIN;
else
result = -EINVAL;
goto out;
}
}
out:
free(s);
return result;
}
/* Unlike CSD, the HIP security checker runs during the connection
* phase, not during the authentication phase.
*
* The HIP security checker will (probably) ask us to resubmit the
* HIP report if either of the following changes:
* - Client IP address
* - Client HIP report md5sum
*
* I'm not sure what the md5sum is computed over in the official
* client, but it doesn't really matter.
*
* We just need an identifier for the combination of the local host
* and the VPN gateway which won't change when our IP address
* or authcookie are changed.
*/
static int build_csd_token(struct openconnect_info *vpninfo)
{
struct oc_text_buf *buf;
unsigned char md5[16];
int i;
if (vpninfo->csd_token)
return 0;
vpninfo->csd_token = malloc(MD5_SIZE * 2 + 1);
if (!vpninfo->csd_token)
return -ENOMEM;
/* use cookie (excluding volatile authcookie and preferred-ip/ipv6) to build md5sum */
buf = buf_alloc();
filter_opts(buf, vpninfo->cookie, "authcookie,preferred-ip,preferred-ipv6", 0);
if (buf_error(buf))
goto out;
/* save as csd_token */
openconnect_md5(md5, buf->data, buf->pos);
for (i=0; i < MD5_SIZE; i++)
sprintf(&vpninfo->csd_token[i*2], "%02x", md5[i]);
out:
return buf_free(buf);
}
/* check if HIP report is needed (to ssl-vpn/hipreportcheck.esp) or submit HIP report contents (to ssl-vpn/hipreport.esp) */
static int check_or_submit_hip_report(struct openconnect_info *vpninfo, const char *report)
{
int result;
struct oc_text_buf *request_body = buf_alloc();
const char *request_body_type = "application/x-www-form-urlencoded";
const char *method = "POST";
char *xml_buf=NULL, *orig_path;
/* cookie gives us these fields: authcookie, portal, user, domain, computer, and (maybe the unnecessary) preferred-ip/ipv6 */
buf_append(request_body, "client-role=global-protect-full&%s", vpninfo->cookie);
if (vpninfo->ip_info.addr)
append_opt(request_body, "client-ip", vpninfo->ip_info.addr);
if (vpninfo->ip_info.addr6)
append_opt(request_body, "client-ipv6", vpninfo->ip_info.addr6);
if (report) {
/* XML report contains many characters requiring URL-encoding (%xx) */
buf_ensure_space(request_body, strlen(report)*3);
append_opt(request_body, "report", report);
} else {
result = build_csd_token(vpninfo);
if (result)
goto out;
append_opt(request_body, "md5", vpninfo->csd_token);
}
if ((result = buf_error(request_body)))
goto out;
orig_path = vpninfo->urlpath;
vpninfo->urlpath = strdup(report ? "ssl-vpn/hipreport.esp" : "ssl-vpn/hipreportcheck.esp");
result = do_https_request(vpninfo, method, request_body_type, request_body, &xml_buf, NULL, HTTP_NO_FLAGS);
free(vpninfo->urlpath);
vpninfo->urlpath = orig_path;
if (result >= 0)
result = gpst_xml_or_error(vpninfo, xml_buf, report ? NULL : parse_hip_report_check, NULL, NULL);
out:
buf_free(request_body);
free(xml_buf);
return result;
}
static int run_hip_script(struct openconnect_info *vpninfo)
{
#if !defined(_WIN32) && !defined(__native_client__)
int pipefd[2];
int ret;
pid_t child;
#endif
if (!vpninfo->csd_wrapper) {
/* Only warn once */
if (!vpninfo->last_trojan) {
vpn_progress(vpninfo, PRG_ERR,
_("WARNING: Server asked us to submit HIP report with md5sum %s.\n"
" VPN connectivity may be disabled or limited without HIP report submission.\n %s\n"),
vpninfo->csd_token,
#if defined(_WIN32) || defined(__native_client__)
_("However, running the HIP report submission script on this platform is not yet implemented.")
#else
_("You need to provide a --csd-wrapper argument with the HIP report submission script.")
#endif
);
/* XXX: Many GlobalProtect VPNs work fine despite allegedly requiring HIP report submission */
}
return 0;
}
#if defined(_WIN32) || defined(__native_client__)
vpn_progress(vpninfo, PRG_ERR,
_("Error: Running the 'HIP Report' script on this platform is not yet implemented.\n"));
return -EPERM;
#else
vpn_progress(vpninfo, PRG_INFO,
_("Trying to run HIP Trojan script '%s'.\n"),
vpninfo->csd_wrapper);
#ifdef __linux__
if (pipe2(pipefd, O_CLOEXEC))
#endif
{
if (pipe(pipefd)) {
vpn_progress(vpninfo, PRG_ERR, _("Failed to create pipe for HIP script\n"));
return -EPERM;
}
set_fd_cloexec(pipefd[0]);
set_fd_cloexec(pipefd[1]);
}
child = fork();
if (child == -1) {
vpn_progress(vpninfo, PRG_ERR, _("Failed to fork for HIP script\n"));
return -EPERM;
} else if (child > 0) {
/* in parent: read report from child */
struct oc_text_buf *report_buf = buf_alloc();
char b[256];
int i, status;
close(pipefd[1]);
buf_truncate(report_buf);
while ((i = read(pipefd[0], b, sizeof(b))) > 0)
buf_append_bytes(report_buf, b, i);
waitpid(child, &status, 0);
if (!WIFEXITED(status)) {
vpn_progress(vpninfo, PRG_ERR,
_("HIP script '%s' exited abnormally\n"),
vpninfo->csd_wrapper);
ret = -EINVAL;
} else if (WEXITSTATUS(status) != 0) {
vpn_progress(vpninfo, PRG_ERR,
_("HIP script '%s' returned non-zero status: %d\n"),
vpninfo->csd_wrapper, WEXITSTATUS(status));
ret = -EINVAL;
} else {
vpn_progress(vpninfo, PRG_INFO,
_("HIP script '%s' completed successfully (report is %d bytes).\n"),
vpninfo->csd_wrapper, report_buf->pos);
ret = check_or_submit_hip_report(vpninfo, report_buf->data);
if (ret < 0)
vpn_progress(vpninfo, PRG_ERR, _("HIP report submission failed.\n"));
else {
vpn_progress(vpninfo, PRG_INFO, _("HIP report submitted successfully.\n"));
ret = 0;
}
}
buf_free(report_buf);
return ret;
} else {
/* in child: run HIP script */
const char *hip_argv[32];
int i = 0;
close(pipefd[0]);
/* The duplicated fd does not have O_CLOEXEC */
dup2(pipefd[1], 1);
if (set_csd_user(vpninfo) < 0)
exit(1);
hip_argv[i++] = openconnect_utf8_to_legacy(vpninfo, vpninfo->csd_wrapper);
hip_argv[i++] = "--cookie";
hip_argv[i++] = vpninfo->cookie;
if (vpninfo->ip_info.addr) {
hip_argv[i++] = "--client-ip";
hip_argv[i++] = vpninfo->ip_info.addr;
}
if (vpninfo->ip_info.addr6) {
hip_argv[i++] = "--client-ipv6";
hip_argv[i++] = vpninfo->ip_info.addr6;
}
hip_argv[i++] = "--md5";
hip_argv[i++] = vpninfo->csd_token;
hip_argv[i++] = "--client-os";
hip_argv[i++] = gpst_os_name(vpninfo);
hip_argv[i++] = NULL;
execv(hip_argv[0], (char **)hip_argv);
vpn_progress(vpninfo, PRG_ERR,
_("Failed to exec HIP script %s\n"), hip_argv[0]);
exit(1);