/
oncp.c
1574 lines (1382 loc) · 42.7 KB
/
oncp.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 © 2008-2015 Intel Corporation.
*
* Author: David Woodhouse <dwmw2@infradead.org>
*
* 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 <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <stdarg.h>
#include <sys/types.h>
#ifndef _WIN32
#include <sys/wait.h>
#endif
#include <libxml/HTMLparser.h>
#include <libxml/HTMLtree.h>
#include "openconnect-internal.h"
/* XX: This is actually a lot of duplication with the CSTP version. */
void oncp_common_headers(struct openconnect_info *vpninfo, struct oc_text_buf *buf)
{
http_common_headers(vpninfo, buf);
buf_append(buf, "Connection: close\r\n");
// buf_append(buf, "Content-Length: 256\r\n");
buf_append(buf, "NCP-Version: 3\r\n");
// buf_append(buf, "Accept-Encoding: gzip\r\n");
}
static xmlNodePtr htmlnode_next(xmlNodePtr top, xmlNodePtr node)
{
if (node->children)
return node->children;
while (!node->next) {
node = node->parent;
if (!node || node == top)
return NULL;
}
return node->next;
}
static int parse_input_node(struct openconnect_info *vpninfo, struct oc_auth_form *form,
xmlNodePtr node, const char *submit_button)
{
const char *type = (const char *)xmlGetProp(node, (unsigned char *)"type");
struct oc_form_opt **p = &form->opts;
struct oc_form_opt *opt;
if (!type)
return -EINVAL;
opt = calloc(1, sizeof(*opt));
if (!opt)
return -ENOMEM;
if (!strcasecmp(type, "hidden")) {
opt->type = OC_FORM_OPT_HIDDEN;
xmlnode_get_prop(node, "name", &opt->name);
xmlnode_get_prop(node, "value", &opt->_value);
/* XXX: Handle tz_offset / tz */
} else if (!strcasecmp(type, "password")) {
opt->type = OC_FORM_OPT_PASSWORD;
xmlnode_get_prop(node, "name", &opt->name);
asprintf(&opt->label, "%s:", opt->name);
} else if (!strcasecmp(type, "text")) {
opt->type = OC_FORM_OPT_TEXT;
xmlnode_get_prop(node, "name", &opt->name);
asprintf(&opt->label, "%s:", opt->name);
} else if (!strcasecmp(type, "submit")) {
xmlnode_get_prop(node, "name", &opt->name);
if (!opt->name || strcmp(opt->name, submit_button)) {
vpn_progress(vpninfo, PRG_DEBUG,
_("Ignoring unknown form submit item '%s'\n"),
opt->name);
free_opt(opt);
return -EINVAL;
}
xmlnode_get_prop(node, "value", &opt->_value);
opt->type = OC_FORM_OPT_HIDDEN;
} else {
vpn_progress(vpninfo, PRG_DEBUG,
_("Ignoring unknown form input type '%s'\n"),
type);
free_opt(opt);
return -EINVAL;
}
/* Append to the existing list */
while (*p)
p = &(*p)->next;
*p = opt;
return 0;
}
static int parse_select_node(struct openconnect_info *vpninfo, struct oc_auth_form *form,
xmlNodePtr node)
{
xmlNodePtr child;
struct oc_form_opt_select *opt;
struct oc_choice *choice;
opt = calloc(1, sizeof(*opt));
if (!opt)
return -ENOMEM;
xmlnode_get_prop(node, "name", &opt->form.name);
opt->form.label = strdup(opt->form.name);
opt->form.type = OC_FORM_OPT_SELECT;
for (child = node->children; child; child = child->next) {
if (!child->name || strcasecmp((const char *)child->name, "option"))
continue;
choice = calloc(1, sizeof(*choice));
if (!choice) {
free_opt((void *)choice);
return -ENOMEM;
}
xmlnode_get_prop(node, "name", &choice->name);
choice->label = (char *)xmlNodeGetContent(child);
choice->name = strdup(choice->label);
realloc_inplace(opt->choices, sizeof(opt->choices[0]) * (opt->nr_choices+1));
if (!opt->choices) {
opt->nr_choices = 0;
free_opt((void *)opt);
return -ENOMEM;
}
opt->choices[opt->nr_choices++] = choice;
}
/* Prepend to the existing list */
opt->form.next = form->opts;
form->opts = &opt->form;
return 0;
}
static struct oc_auth_form *parse_form_node(struct openconnect_info *vpninfo,
xmlNodePtr node, const char *submit_button)
{
struct oc_auth_form *form = calloc(1, sizeof(*form));
xmlNodePtr child;
if (!form)
return NULL;
xmlnode_get_prop(node, "method", &form->method);
xmlnode_get_prop(node, "action", &form->action);
if (!form->method || strcasecmp(form->method, "POST") ||
!form->action || !form->action[0]) {
vpn_progress(vpninfo, PRG_ERR,
_("Cannot handle form method='%s', action='%s'\n"),
form->method, form->action);
free(form);
return NULL;
}
xmlnode_get_prop(node, "name", &form->auth_id);
form->banner = strdup(form->auth_id);
for (child = htmlnode_next(node, node); child && child != node; child = htmlnode_next(node, child)) {
if (!child->name)
continue;
if (!strcasecmp((char *)child->name, "input"))
parse_input_node(vpninfo, form, child, submit_button);
else if (!strcasecmp((char *)child->name, "select")) {
parse_select_node(vpninfo, form, child);
/* Skip its children */
while (child->children)
child = child->last;
}
}
return form;
}
static int oncp_https_submit(struct openconnect_info *vpninfo,
struct oc_text_buf *req_buf, xmlDocPtr *doc)
{
int ret;
char *form_buf = NULL;
struct oc_text_buf *url;
if (req_buf && req_buf->pos)
ret =do_https_request(vpninfo, "POST",
"application/x-www-form-urlencoded",
req_buf, &form_buf, 2);
else
ret = do_https_request(vpninfo, "GET", NULL, NULL,
&form_buf, 2);
if (ret < 0)
return ret;
url = buf_alloc();
buf_append(url, "https://%s", vpninfo->hostname);
if (vpninfo->port != 443)
buf_append(url, ":%d", vpninfo->port);
buf_append(url, "/");
if (vpninfo->urlpath)
buf_append(url, "%s", vpninfo->urlpath);
if (buf_error(url)) {
free(form_buf);
return buf_free(url);
}
*doc = htmlReadMemory(form_buf, ret, url->data, NULL,
HTML_PARSE_RECOVER|HTML_PARSE_NOERROR|HTML_PARSE_NOWARNING|HTML_PARSE_NONET);
buf_free(url);
free(form_buf);
if (!*doc) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to parse HTML document\n"));
return -EINVAL;
}
return 0;
}
static xmlNodePtr find_form_node(xmlDocPtr doc)
{
xmlNodePtr root, node;
for (root = node = xmlDocGetRootElement(doc); node; node = htmlnode_next(root, node)) {
if (node->name && !strcasecmp((char *)node->name, "form"))
return node;
}
return NULL;
}
static int check_cookie_success(struct openconnect_info *vpninfo)
{
const char *dslast = NULL, *dsfirst = NULL, *dsurl = NULL, *dsid = NULL;
struct oc_vpn_option *cookie;
struct oc_text_buf *buf;
for (cookie = vpninfo->cookies; cookie; cookie = cookie->next) {
if (!strcmp(cookie->option, "DSFirstAccess"))
dsfirst = cookie->value;
else if (!strcmp(cookie->option, "DSLastAccess"))
dslast = cookie->value;
else if (!strcmp(cookie->option, "DSID"))
dsid = cookie->value;
else if (!strcmp(cookie->option, "DSSignInUrl"))
dsurl = cookie->value;
}
if (!dsid)
return -ENOENT;
buf = buf_alloc();
if (vpninfo->tncc_fd != -1) {
buf_append(buf, "setcookie\n");
buf_append(buf, "Cookie=%s\n", dsid);
if (buf_error(buf))
return buf_free(buf);
send(vpninfo->tncc_fd, buf->data, buf->pos, 0);
buf_truncate(buf);
}
/* XXX: Do these need escaping? Could they theoreetically have semicolons in? */
buf_append(buf, "DSID=%s", dsid);
if (dsfirst)
buf_append(buf, "; DSFirst=%s", dsfirst);
if (dslast)
buf_append(buf, "; DSLast=%s", dslast);
if (dsurl)
buf_append(buf, "; DSSignInUrl=%s", dsurl);
if (buf_error(buf))
return buf_free(buf);
free(vpninfo->cookie);
vpninfo->cookie = buf->data;
buf->data = NULL;
buf_free(buf);
return 0;
}
#ifdef _WIN32
static int tncc_preauth(struct openconnect_info *vpninfo)
{
vpn_progress(vpninfo, PRG_ERR,
_("TNCC support not implemented yet on Windows\n"));
return -EOPNOTSUPP;
}
#else
static int tncc_preauth(struct openconnect_info *vpninfo)
{
int sockfd[2];
pid_t pid;
struct oc_text_buf *buf;
struct oc_vpn_option *cookie;
const char *dspreauth = NULL, *dssignin = "null";
char recvbuf[1024], *p;
int len;
for (cookie = vpninfo->cookies; cookie; cookie = cookie->next) {
if (!strcmp(cookie->option, "DSPREAUTH"))
dspreauth = cookie->value;
else if (!strcmp(cookie->option, "DSSIGNIN"))
dssignin = cookie->value;
}
if (!dspreauth) {
vpn_progress(vpninfo, PRG_ERR,
_("No DSPREAUTH cookie; not attempting TNCC\n"));
return -EINVAL;
}
buf = buf_alloc();
buf_append(buf, "start\n");
buf_append(buf, "IC=%s\n", vpninfo->hostname);
buf_append(buf, "Cookie=%s\n", dspreauth);
buf_append(buf, "DSSIGNIN=%s\n", dssignin);
if (buf_error(buf)) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to allocate memory for communication with TNCC\n"));
return buf_free(buf);
}
#ifdef SOCK_CLOEXEC
if (socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, sockfd))
#endif
{
if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, sockfd))
return -errno;
set_fd_cloexec(sockfd[0]);
set_fd_cloexec(sockfd[1]);
}
pid = fork();
if (pid == -1) {
buf_free(buf);
return -errno;
}
if (!pid) {
int i;
/* Fork again to detach grandchild */
if (fork())
exit(1);
close(sockfd[1]);
/* The duplicated fd does not have O_CLOEXEC */
dup2(sockfd[0], 0);
/* We really don't want anything going to stdout */
dup2(1, 2);
for (i = 3; i < 1024 ; i++)
close(i);
execl(vpninfo->csd_wrapper, vpninfo->csd_wrapper, vpninfo->hostname, NULL);
fprintf(stderr, _("Failed to exec TNCC script %s: %s\n"),
vpninfo->csd_wrapper, strerror(errno));
exit(1);
}
waitpid(pid, NULL, 0);
close(sockfd[0]);
if (send(sockfd[1], buf->data, buf->pos, 0) != buf->pos) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to send start command to TNCC\n"));
buf_free(buf);
close(sockfd[1]);
return -EIO;
}
buf_free(buf);
vpn_progress(vpninfo, PRG_DEBUG,
_("Sent start; waiting for response from TNCC\n"));
len = recv(sockfd[1], recvbuf, sizeof(recvbuf) - 1, 0);
if (len < 0) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to read response from TNCC\n"));
close(sockfd[1]);
return -EIO;
}
recvbuf[len] = 0;
p = strchr(recvbuf, '\n');
if (!p) {
invalid_response:
vpn_progress(vpninfo, PRG_ERR,
_("Received invalid response from TNCC\n"));
print_response:
vpn_progress(vpninfo, PRG_TRACE, _("TNCC response: -->\n%s\n<--\n"),
recvbuf);
close(sockfd[1]);
return -EINVAL;
}
*p = 0;
if (strcmp(recvbuf, "200")) {
vpn_progress(vpninfo, PRG_ERR,
_("Received unsuccessful %s response from TNCC\n"),
recvbuf);
goto print_response;
}
p = strchr(p + 1, '\n');
if (!p)
goto invalid_response;
dspreauth = p + 1;
p = strchr(p + 1, '\n');
if (!p)
goto invalid_response;
*p = 0;
vpn_progress(vpninfo, PRG_DEBUG,
_("Got new DSPREAUTH cookie from TNCC: %s\n"),
dspreauth);
http_add_cookie(vpninfo, "DSPREAUTH", dspreauth, 1);
vpninfo->tncc_fd = sockfd[1];
return 0;
}
#endif
int oncp_obtain_cookie(struct openconnect_info *vpninfo)
{
int ret;
struct oc_text_buf *resp_buf = NULL;
xmlDocPtr doc = NULL;
xmlNodePtr node;
struct oc_auth_form *form = NULL;
char *form_id = NULL;
int try_tncc = !!vpninfo->csd_wrapper;
resp_buf = buf_alloc();
if (buf_error(resp_buf))
return -ENOMEM;
while (1) {
ret = oncp_https_submit(vpninfo, resp_buf, &doc);
if (ret || !check_cookie_success(vpninfo))
break;
buf_truncate(resp_buf);
node = find_form_node(doc);
if (!node) {
if (try_tncc) {
try_tncc = 0;
ret = tncc_preauth(vpninfo);
if (ret)
return ret;
goto tncc_done;
}
vpn_progress(vpninfo, PRG_ERR,
_("Failed to find or parse web form in login page\n"));
ret = -EINVAL;
break;
}
form_id = (char *)xmlGetProp(node, (unsigned char *)"name");
if (!form_id) {
vpn_progress(vpninfo, PRG_ERR,
_("Encountered form with no ID\n"));
goto dump_form;
} else if (!strcmp(form_id, "frmLogin")) {
form = parse_form_node(vpninfo, node, "btnSubmit");
if (!form) {
ret = -EINVAL;
break;
}
} else if (!strcmp(form_id, "frmDefender") ||
!strcmp(form_id, "frmNextToken")) {
vpn_progress(vpninfo, PRG_ERR,
_("No support for %s form type yet\n"),
form_id);
ret = -EINVAL;
break;
} else if (!strcmp(form_id, "frmConfirmation")) {
form = parse_form_node(vpninfo, node, "btnContinue");
if (!form) {
ret = -EINVAL;
break;
}
/* XXX: Actually ask the user? */
goto form_done;
} else {
vpn_progress(vpninfo, PRG_ERR,
_("Unknown form ID '%s'\n"),
form_id);
dump_form:
fprintf(stderr, _("Dumping unknown HTML form:\n"));
htmlNodeDumpFileFormat(stderr, node->doc, node, NULL, 1);
ret = -EINVAL;
break;
}
ret = process_auth_form(vpninfo, form);
if (ret)
goto out;
form_done:
append_form_opts(vpninfo, form, resp_buf);
ret = buf_error(resp_buf);
if (ret)
break;
vpninfo->redirect_url = form->action;
form->action = NULL;
free_auth_form(form);
form = NULL;
handle_redirect(vpninfo);
tncc_done:
xmlFreeDoc(doc);
doc = NULL;
}
out:
if (doc)
xmlFreeDoc(doc);
free(form_id);
if (form)
free_auth_form(form);
buf_free(resp_buf);
return ret;
}
static int parse_cookie(struct openconnect_info *vpninfo)
{
char *p = vpninfo->cookie;
/* We currenly expect the "cookie" to be contain multiple cookies:
* DSSignInUrl=/; DSID=xxx; DSFirstAccess=xxx; DSLastAccess=xxx
* Process those into vpninfo->cookies unless we already had them
* (in which case they'll may be newer. */
while (p && *p) {
char *semicolon = strchr(p, ';');
char *equals;
if (semicolon)
*semicolon = 0;
equals = strchr(p, '=');
if (!equals) {
vpn_progress(vpninfo, PRG_ERR, _("Invalid cookie '%s'\n"), p);
return -EINVAL;
}
*equals = 0;
http_add_cookie(vpninfo, p, equals+1, 0);
*equals = '=';
p = semicolon;
if (p) {
*p = ';';
p++;
while (*p && isspace(*p))
p++;
}
}
return 0;
}
static void buf_append_be16(struct oc_text_buf *buf, uint16_t val)
{
unsigned char b[2];
b[0] = val >> 8;
b[1] = val & 0xff;
buf_append_bytes(buf, b, 2);
}
static void buf_append_le16(struct oc_text_buf *buf, uint16_t val)
{
unsigned char b[2];
b[0] = val & 0xff;
b[1] = val >> 8;
buf_append_bytes(buf, b, 2);
}
static void buf_append_tlv(struct oc_text_buf *buf, uint16_t val, uint32_t len, void *data)
{
unsigned char b[6];
b[0] = val >> 8;
b[1] = val;
b[2] = len >> 24;
b[3] = len >> 16;
b[4] = len >> 8;
b[5] = len;
buf_append_bytes(buf, b, 6);
if (len)
buf_append_bytes(buf, data, len);
}
static void buf_append_tlv_be32(struct oc_text_buf *buf, uint16_t val, uint32_t data)
{
unsigned char d[4];
d[0] = data >> 24;
d[1] = data >> 16;
d[2] = data >> 8;
d[3] = data;
buf_append_tlv(buf, val, 4, d);
}
static void buf_hexdump(struct openconnect_info *vpninfo, struct oc_text_buf *buf)
{
char linebuf[80];
int i;
for (i = 0; i < buf->pos; i++) {
if (i % 16 == 0) {
if (i)
vpn_progress(vpninfo, PRG_DEBUG, "%s\n", linebuf);
sprintf(linebuf, "%04x:", i);
}
sprintf(linebuf + strlen(linebuf), " %02x", (unsigned char)buf->data[i]);
}
vpn_progress(vpninfo, PRG_DEBUG, "%s\n", linebuf);
}
static const char authpkt_head[] = { 0x00, 0x04, 0x00, 0x00, 0x00 };
static const char authpkt_tail[] = { 0xbb, 0x01, 0x00, 0x00, 0x00, 0x00 };
#define GRP_ATTR(g, a) (((g) << 16) | (a))
#define TLV_BE32(data) ((data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3])
#define TLV_BE16(data) ((data[0] << 8) + data[1])
/* We behave like CSTP — create a linked list in vpninfo->cstp_options
* with the strings containing the information we got from the server,
* and oc_ip_info contains const copies of those pointers. */
static const char *add_option(struct openconnect_info *vpninfo, const char *opt,
const char *val, int val_len)
{
struct oc_vpn_option *new = malloc(sizeof(*new));
if (!new)
return NULL;
new->option = strdup(opt);
if (!new->option) {
free(new);
return NULL;
}
if (val_len >= 0)
new->value = strndup(val, val_len);
else
new->value = strdup(val);
if (!new->value) {
free(new->option);
free(new);
return NULL;
}
new->next = vpninfo->cstp_options;
vpninfo->cstp_options = new;
return new->value;
}
static int process_attr(struct openconnect_info *vpninfo, int group, int attr,
unsigned char *data, int attrlen)
{
char buf[80];
int i;
switch(GRP_ATTR(group, attr)) {
case GRP_ATTR(6, 2):
if (attrlen != 4) {
badlen:
vpn_progress(vpninfo, PRG_ERR,
_("Unexpected length %d for TLV %d/%d\n"),
attrlen, group, attr);
return -EINVAL;
goto badlen;
}
vpninfo->ip_info.mtu = TLV_BE32(data);
vpn_progress(vpninfo, PRG_DEBUG,
_("Received MTU %d from server\n"),
vpninfo->ip_info.mtu);
break;
case GRP_ATTR(2, 1):
if (attrlen != 4)
goto badlen;
snprintf(buf, sizeof(buf), "%d.%d.%d.%d", data[0], data[1], data[2], data[3]);
vpn_progress(vpninfo, PRG_DEBUG, _("Received DNS server %s\n"), buf);
for (i = 0; i < 3; i++) {
if (!vpninfo->ip_info.dns[i]) {
vpninfo->ip_info.dns[i] = add_option(vpninfo, "DNS", buf, -1);
break;
}
}
break;
case GRP_ATTR(2, 2):
vpn_progress(vpninfo, PRG_DEBUG, _("Received DNS search domain %.*s\n"),
attrlen, (char *)data);
vpninfo->ip_info.domain = add_option(vpninfo, "search", (char *)data, attrlen);
break;
case GRP_ATTR(1, 1):
if (attrlen != 4)
goto badlen;
snprintf(buf, sizeof(buf), "%d.%d.%d.%d", data[0], data[1], data[2], data[3]);
vpn_progress(vpninfo, PRG_DEBUG, _("Received internal IP address %s\n"), buf);
vpninfo->ip_info.addr = add_option(vpninfo, "ipaddr", buf, -1);
break;
case GRP_ATTR(1, 2):
if (attrlen != 4)
goto badlen;
snprintf(buf, sizeof(buf), "%d.%d.%d.%d", data[0], data[1], data[2], data[3]);
vpn_progress(vpninfo, PRG_DEBUG, _("Received netmask %s\n"), buf);
vpninfo->ip_info.netmask = add_option(vpninfo, "netmask", buf, -1);
break;
case GRP_ATTR(1, 3):
if (attrlen != 4)
goto badlen;
snprintf(buf, sizeof(buf), "%d.%d.%d.%d", data[0], data[1], data[2], data[3]);
vpn_progress(vpninfo, PRG_DEBUG, _("Received internal gateway address %s\n"), buf);
/* Hm, what are we supposed to do with this? It's a tunnel;
having a gateway is meaningless. */
add_option(vpninfo, "ipaddr", buf, -1);
break;
case GRP_ATTR(4, 1):
if (attrlen != 4)
goto badlen;
snprintf(buf, sizeof(buf), "%d.%d.%d.%d", data[0], data[1], data[2], data[3]);
vpn_progress(vpninfo, PRG_DEBUG, _("Received WINS server %s\n"), buf);
for (i = 0; i < 3; i++) {
if (!vpninfo->ip_info.nbns[i]) {
vpninfo->ip_info.nbns[i] = add_option(vpninfo, "WINS", buf, -1);
break;
}
}
break;
case GRP_ATTR(8, 1): {
const char *enctype;
if (attrlen != 1)
goto badlen;
if (data[0] == 0x02)
enctype = "AES-128";
else if (data[0] == 0x05)
enctype = "AES-256";
else
enctype = "unknown";
vpn_progress(vpninfo, PRG_DEBUG, _("ESP encryption: 0x%02x (%s)\n"),
data[0], enctype);
vpninfo->esp_enc = data[0];
break;
}
case GRP_ATTR(8, 2): {
const char *mactype;
if (attrlen != 1)
goto badlen;
if (data[0] == 0x01)
mactype = "MD5";
else if (data[0] == 0x02)
mactype = "SHA1";
else
mactype = "unknown";
vpn_progress(vpninfo, PRG_DEBUG, _("ESP HMAC: 0x%02x (%s)\n"),
data[0], mactype);
vpninfo->esp_hmac = data[0];
break;
}
case GRP_ATTR(8, 3):
if (attrlen != 1)
goto badlen;
vpninfo->esp_compr = data[0];
vpn_progress(vpninfo, PRG_DEBUG, _("ESP compression: %d\n"), data[0]);
break;
case GRP_ATTR(8, 4):
if (attrlen != 2)
goto badlen;
i = TLV_BE16(data);
udp_sockaddr(vpninfo, i);
vpn_progress(vpninfo, PRG_DEBUG, _("ESP port: %d\n"), i);
break;
case GRP_ATTR(8, 5):
if (attrlen != 4)
goto badlen;
vpninfo->esp_lifetime_bytes = TLV_BE32(data);
vpn_progress(vpninfo, PRG_DEBUG, _("ESP key lifetime: %u bytes\n"),
vpninfo->esp_lifetime_bytes);
break;
case GRP_ATTR(8, 6):
if (attrlen != 4)
goto badlen;
vpninfo->esp_lifetime_seconds = TLV_BE32(data);
vpn_progress(vpninfo, PRG_DEBUG, _("ESP key lifetime: %u seconds\n"),
vpninfo->esp_lifetime_seconds);
break;
case GRP_ATTR(8, 9):
if (attrlen != 4)
goto badlen;
vpninfo->esp_ssl_fallback = TLV_BE32(data);
vpn_progress(vpninfo, PRG_DEBUG, _("ESP to SSL fallback: %u seconds\n"),
vpninfo->esp_ssl_fallback);
break;
case GRP_ATTR(8, 10):
if (attrlen != 4)
goto badlen;
vpninfo->esp_replay_protect = TLV_BE32(data);
vpn_progress(vpninfo, PRG_DEBUG, _("ESP replay protection: %d\n"),
TLV_BE32(data));
break;
case GRP_ATTR(7, 1):
if (attrlen != 4)
goto badlen;
memcpy(&vpninfo->esp_out.spi, data, 4);
vpn_progress(vpninfo, PRG_DEBUG, _("ESP SPI (outbound): %x\n"),
TLV_BE32(data));
break;
case GRP_ATTR(7, 2):
if (attrlen != 0x40)
goto badlen;
memcpy(vpninfo->esp_out.secrets, data, 0x40);
vpn_progress(vpninfo, PRG_DEBUG, _("%d bytes of ESP secrets\n"),
attrlen);
break;
default:
buf[0] = 0;
for (i=0; i < 16 && i < attrlen; i++)
sprintf(buf + strlen(buf), " %02x", data[i]);
if (attrlen > 16)
sprintf(buf + strlen(buf), "...");
vpn_progress(vpninfo, PRG_DEBUG,
_("Unknown TLV group %d attr %d len %d:%s\n"),
group, attr, attrlen, buf);
}
return 0;
}
static void put_len16(struct oc_text_buf *buf, int where)
{
int len = buf->pos - where;
buf->data[where - 1] = len;
buf->data[where - 2] = len >> 8;
}
static void put_len32(struct oc_text_buf *buf, int where)
{
int len = buf->pos - where;
buf->data[where - 1] = len;
buf->data[where - 2] = len >> 8;
buf->data[where - 3] = len >> 16;
buf->data[where - 4] = len >> 24;
}
/* We don't know what these are so just hope they never change */
static const unsigned char kmp_head[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
static const unsigned char kmp_tail[] = { 0x01, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00 };
static const unsigned char kmp_tail_out[] = { 0x01, 0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00, 0x00 };
static const unsigned char data_hdr[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x2c, 0x01, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00 };
static const unsigned char esp_kmp_hdr[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x2e,
0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, /* KMP header */
0x00, 0x56, /* KMP length */
0x00, 0x07, 0x00, 0x00, 0x00, 0x50, /* TLV group 7 */
0x00, 0x01, 0x00, 0x00, 0x00, 0x04, /* Attr 1 (SPI) */
};
/* Followed by 4 bytes of SPI */
static const unsigned char esp_kmp_part2[] = {
0x00, 0x02, 0x00, 0x00, 0x00, 0x40, /* Attr 2 (secrets) */
};
/* And now 0x40 bytes of random secret for encryption and HMAC key */
static const struct pkt esp_enable_pkt = {
.oncp_hdr = {
0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x2f, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x0d
},
.data = {
0x00, 0x06, 0x00, 0x00, 0x00, 0x07, /* Group 6, len 7 */
0x00, 0x01, 0x00, 0x00, 0x00, 0x01, /* Attr 1, len 1 */
0x01
},
.len = 13
};
int oncp_connect(struct openconnect_info *vpninfo)
{
int ret, ofs, kmp, kmpend, kmplen, attr, attrlen, group, grouplen, groupend;
struct oc_text_buf *reqbuf;
unsigned char bytes[1024];
/* XXX: We should do what cstp_connect() does to check that configuration
hasn't changed on a reconnect. */
if (!vpninfo->cookies) {
ret = parse_cookie(vpninfo);
if (ret)
return ret;
}
ret = openconnect_open_https(vpninfo);
if (ret)
return ret;
reqbuf = buf_alloc();
buf_append(reqbuf, "POST /dana/js?prot=1&svc=1 HTTP/1.1\r\n");
oncp_common_headers(vpninfo, reqbuf);
buf_append(reqbuf, "\r\n");
if (buf_error(reqbuf)) {
vpn_progress(vpninfo, PRG_ERR,
_("Error creating oNCP negotiation request\n"));
ret = buf_error(reqbuf);
goto out;
}
ret = vpninfo->ssl_write(vpninfo, reqbuf->data, reqbuf->pos);
if (ret < 0)
goto out;
/* The server is fairly weird. It sends Connection: close which would
* indicate an HTTP 1.0-style body, but doesn't seem to actually close
* the connection. So tell process_http_response() it was a CONNECT
* request, since we don't care about the body anyway, and then close
* the connection for ourselves. */
ret = process_http_response(vpninfo, 1, NULL, reqbuf);
openconnect_close_https(vpninfo, 0);
if (ret < 0) {
/* We'll already have complained about whatever offended us */
goto out;
}
if (ret != 200) {
vpn_progress(vpninfo, PRG_ERR,
_("Unexpected %d result from server\n"),
ret);
ret = -EINVAL;
goto out;
}
/* Now the second request. We should reduce the duplication
here but let's not overthink it for now; we should see what
the authentication requests are going to look like, and make
do_https_request() or a new helper function work for those
too. */
ret = openconnect_open_https(vpninfo);
if (ret)
goto out;
buf_truncate(reqbuf);
buf_append(reqbuf, "POST /dana/js?prot=1&svc=4 HTTP/1.1\r\n");
oncp_common_headers(vpninfo, reqbuf);
buf_append(reqbuf, "\r\n");
if (buf_error(reqbuf)) {
vpn_progress(vpninfo, PRG_ERR,
_("Error creating oNCP negotiation request\n"));
ret = buf_error(reqbuf);
goto out;
}
ret = vpninfo->ssl_write(vpninfo, reqbuf->data, reqbuf->pos);
if (ret < 0)