-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth-juniper.c
627 lines (551 loc) · 16.5 KB
/
auth-juniper.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
/*
* OpenConnect (SSL + DTLS) VPN client
*
* Copyright © 2008-2015 Intel Corporation.
*
* Author: David Woodhouse <[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.
*/
/*
* Grateful thanks to Tiebing Zhang, who did much of the hard work
* of analysing and decoding the protocol.
*/
#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, "Content-Length: 256\r\n");
buf_append(buf, "NCP-Version: 3\r\n");
// buf_append(buf, "Accept-Encoding: gzip\r\n");
}
static int oncp_can_gen_tokencode(struct openconnect_info *vpninfo,
struct oc_auth_form *form,
struct oc_form_opt *opt)
{
if (vpninfo->token_mode == OC_TOKEN_MODE_NONE ||
vpninfo->token_bypassed)
return -EINVAL;
if (opt->type == OC_FORM_OPT_PASSWORD &&
(!strcmp(form->auth_id, "frmLogin") ||
!strcmp(form->auth_id, "loginForm"))) {
/* XX: The first occurence of a password input field in frmLogin is likely to be a password,
* not token, input. However, if we have already added a password input field to this form,
* then a second one is likely to hold a token.
*/
struct oc_form_opt *p;
for (p = form->opts; p; p = p->next) {
if (p->type == OC_FORM_OPT_PASSWORD)
goto okay;
}
return -EINVAL;
}
if (strcmp(form->auth_id, "frmDefender") &&
strcmp(form->auth_id, "frmNextToken") &&
strcmp(form->auth_id, "frmTotpToken") &&
strcmp(form->auth_id, "loginForm"))
return -EINVAL;
okay:
return can_gen_tokencode(vpninfo, form, opt);
}
int oncp_send_tncc_command(struct openconnect_info *vpninfo, int start)
{
const char *dspreauth = vpninfo->csd_token, *dsurl = vpninfo->csd_starturl ? : "null";
struct oc_text_buf *buf;
buf = buf_alloc();
if (start) {
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", dsurl);
} else {
buf_append(buf, "setcookie\n");
buf_append(buf, "Cookie=%s\n", dspreauth);
}
if (buf_error(buf)) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to allocate memory for communication with TNCC\n"));
return buf_free(buf);
}
if (cancellable_send(vpninfo, vpninfo->tncc_fd, buf->data, buf->pos) != buf->pos) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to send command to TNCC\n"));
buf_free(buf);
return -EIO;
}
/* Mainloop timers need to know the last Trojan was invoked */
vpninfo->last_trojan = time(NULL);
return buf_free(buf);
}
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;
else if (!strcmp(cookie->option, "DSSIGNIN")) {
free(vpninfo->csd_starturl);
vpninfo->csd_starturl = strdup(cookie->value);
} else if (!strcmp(cookie->option, "DSPREAUTH")) {
free(vpninfo->csd_token);
vpninfo->csd_token = strdup(cookie->value);
}
}
if (!dsid)
return -ENOENT;
if (vpninfo->tncc_fd != -1) {
/* update TNCC once we get a DSID cookie */
oncp_send_tncc_command(vpninfo, 0);
}
/* XXX: Do these need escaping? Could they theoreetically have semicolons in? */
buf = buf_alloc();
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;
const char *dspreauth = vpninfo->csd_token;
char recvbuf[1024];
int len, count, ret;
if (!dspreauth) {
vpn_progress(vpninfo, PRG_ERR,
_("No DSPREAUTH cookie; not attempting TNCC\n"));
return -EINVAL;
}
vpn_progress(vpninfo, PRG_INFO,
_("Trying to run TNCC/Host Checker Trojan script '%s'.\n"),
vpninfo->csd_wrapper);
#ifdef SOCK_CLOEXEC
if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sockfd))
#endif
{
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfd))
return -errno;
set_fd_cloexec(sockfd[0]);
set_fd_cloexec(sockfd[1]);
}
pid = fork();
if (pid == -1) {
close(sockfd[0]);
close(sockfd[1]);
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 our stdout.
Redirect the child's stdout, to our stderr. */
dup2(2, 1);
/* And close everything else.*/
for (i = 3; i < 1024 ; i++)
close(i);
if (setenv("TNCC_SHA256", openconnect_get_peer_cert_hash(vpninfo)+11, 1)) /* remove initial 'pin-sha256:' */
goto out;
if (setenv("TNCC_HOSTNAME", vpninfo->localname, 1))
goto out;
if (!vpninfo->trojan_interval) {
char is[32];
snprintf(is, 32, "%d", vpninfo->trojan_interval);
if (setenv("TNCC_INTERVAL", is, 1))
goto out;
}
execl(vpninfo->csd_wrapper, vpninfo->csd_wrapper, vpninfo->hostname, NULL);
out:
fprintf(stderr, _("Failed to exec TNCC script %s: %s\n"),
vpninfo->csd_wrapper, strerror(errno));
exit(1);
}
waitpid(pid, NULL, 0);
close(sockfd[0]);
vpninfo->tncc_fd = sockfd[1];
ret = oncp_send_tncc_command(vpninfo, 1);
if (ret < 0) {
err:
close(vpninfo->tncc_fd);
vpninfo->tncc_fd = -1;
return ret;
}
vpn_progress(vpninfo, PRG_DEBUG,
_("Sent start; waiting for response from TNCC\n"));
/* First line: HTTP-like response code. */
len = cancellable_gets(vpninfo, sockfd[1], recvbuf, sizeof(recvbuf));
if (len < 0) {
respfail:
vpn_progress(vpninfo, PRG_ERR,
_("Failed to read response from TNCC\n"));
ret = -EIO;
goto err;
}
if (strcmp(recvbuf, "200")) {
vpn_progress(vpninfo, PRG_ERR,
_("Received unsuccessful %s response from TNCC\n"),
recvbuf);
ret = -EINVAL;
goto err;
}
vpn_progress(vpninfo, PRG_TRACE, _("TNCC response 200 OK\n"));
/* We're not sure what the second line is. We ignore it. */
len = cancellable_gets(vpninfo, sockfd[1], recvbuf, sizeof(recvbuf));
if (len < 0)
goto respfail;
vpn_progress(vpninfo, PRG_TRACE, _("Second line of TNCC response: '%s'\n"),
recvbuf);
/* Third line is the DSPREAUTH cookie */
len = cancellable_gets(vpninfo, sockfd[1], recvbuf, sizeof(recvbuf));
if (len < 0)
goto respfail;
vpn_progress(vpninfo, PRG_DEBUG,
_("Got new DSPREAUTH cookie from TNCC: %s\n"),
recvbuf);
http_add_cookie(vpninfo, "DSPREAUTH", recvbuf, 1);
/* Fourth line, if present, is the interval to rerun TNCC */
len = cancellable_gets(vpninfo, sockfd[1], recvbuf, sizeof(recvbuf));
if (len < 0)
goto respfail;
if (len > 0) {
int interval = atoi(recvbuf);
if (interval != 0) {
vpninfo->trojan_interval = interval;
vpn_progress(vpninfo, PRG_DEBUG,
_("Got reauth interval from TNCC: %d seconds\n"),
interval);
}
}
count = 0;
do {
len = cancellable_gets(vpninfo, sockfd[1], recvbuf,
sizeof(recvbuf));
if (len < 0)
goto respfail;
if (len > 0)
vpn_progress(vpninfo, PRG_DEBUG,
_("Unexpected non-empty line from TNCC "
"after DSPREAUTH cookie: '%s'\n"),
recvbuf);
} while (len && (count++ < 10));
if (len > 0) {
vpn_progress(vpninfo, PRG_ERR,
_("Too many non-empty lines from TNCC after "
"DSPREAUTH cookie\n"));
goto respfail;
}
return 0;
}
#endif
static struct oc_auth_form *parse_roles_table_node(xmlNodePtr node)
{
struct oc_auth_form *form;
xmlNodePtr table_itr;
xmlNodePtr row_itr;
xmlNodePtr data_itr;
struct oc_form_opt_select *opt;
struct oc_choice *choice;
form = calloc(1, sizeof(*form));
if (!form)
return NULL;
form->auth_id = strdup("frmSelectRoles");
if (!form->auth_id) {
free(form);
return NULL;
};
opt = calloc(1, sizeof(*opt));
if (!opt) {
free_auth_form(form);
return NULL;
}
form->opts = &opt->form;
opt->form.label = strdup("frmSelectRoles");
opt->form.name = strdup("frmSelectRoles");
opt->form.type = OC_FORM_OPT_SELECT;
form->authgroup_opt = opt; /* XX: --authgroup also sets realm field (see parse_select_node in auth-html.c) */
for (table_itr = node->children; table_itr; table_itr = table_itr->next) {
if (!table_itr->name || strcasecmp((const char *)table_itr->name, "tr"))
continue;
for (row_itr = table_itr->children; row_itr; row_itr = row_itr->next) {
if (!row_itr->name || strcasecmp((const char *)row_itr->name, "td"))
continue;
for (data_itr = row_itr->children; data_itr; data_itr = data_itr->next) {
struct oc_choice **new_choices;
char *role_link = NULL;
char *role_name = NULL;
if (!data_itr->name || strcasecmp((const char *)data_itr->name, "a"))
continue;
// Discovered <a> tag with role selection.
role_link = (char *)xmlGetProp(data_itr, (unsigned char *)"href");
if (!role_link)
continue;
role_name = (char *)xmlNodeGetContent(data_itr);
if (!role_name) {
// some weird case?
free(role_link);
continue;
}
choice = calloc(1, sizeof(*choice));
if (!choice) {
free(role_name);
free(role_link);
free_auth_form(form);
return NULL;
}
choice->label = role_name;
choice->name = role_link;
new_choices = realloc(opt->choices, sizeof(opt->choices[0]) * (opt->nr_choices+1));
if (!new_choices) {
free(choice);
free(role_name);
free(role_link);
free_auth_form(form);
return NULL;
}
opt->choices = new_choices;
opt->choices[opt->nr_choices++] = choice;
}
}
}
return form;
}
static struct oc_auth_form *parse_roles_form_node(xmlNodePtr node)
{
struct oc_auth_form *form = NULL;
xmlNodePtr child;
// Set form->action here as a redirect url with keys and ids.
for (child = htmlnode_dive(node, node); child && child != node;
child = htmlnode_dive(node, child)) {
if (child->name && !strcasecmp((char *)child->name, "table")) {
char *table_id = (char *)xmlGetProp(child, (unsigned char *)"id");
if (table_id) {
if (!strcmp(table_id, "TABLE_SelectRole_1"))
form = parse_roles_table_node(child);
free(table_id);
if (form)
break;
}
}
}
return form;
}
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_name = NULL, *form_id = NULL;
int try_tncc = !!vpninfo->csd_wrapper;
resp_buf = buf_alloc();
if (buf_error(resp_buf)) {
ret = buf_error(resp_buf);
goto out;
}
while (1) {
char *form_buf = NULL;
int role_select = 0;
char *url;
if (resp_buf && resp_buf->pos)
ret = do_https_request(vpninfo, "POST",
"application/x-www-form-urlencoded",
resp_buf, &form_buf, 2);
else
ret = do_https_request(vpninfo, "GET", NULL, NULL,
&form_buf, 2);
/* After login, the server will redirect the "browser" to a landing page.
* https://kb.pulsesecure.net/articles/Pulse_Security_Advisories/SA44784
* turned some of those landing pages into a 403 but we don't *care*
* about that as long as we have the cookie we wanted. So check for
* cookie success *before* checking 'ret'. */
if (!check_cookie_success(vpninfo)) {
free(form_buf);
ret = 0;
break;
}
if (ret < 0)
break;
url = internal_get_url(vpninfo);
if (!url) {
free(form_buf);
ret = -ENOMEM;
break;
}
doc = htmlReadMemory(form_buf, ret, url, NULL,
HTML_PARSE_RECOVER|HTML_PARSE_NOERROR|HTML_PARSE_NOWARNING|HTML_PARSE_NONET);
free(url);
free(form_buf);
if (!doc) {
vpn_progress(vpninfo, PRG_ERR,
_("Failed to parse HTML document\n"));
ret = -EINVAL;
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;
}
free(form_name);
free(form_id);
form_name = (char *)xmlGetProp(node, (unsigned char *)"name");
form_id = (char *)xmlGetProp(node, (unsigned char *)"id");
if (!form_name && !form_id) {
vpn_progress(vpninfo, PRG_ERR,
_("Encountered form with no 'name' or 'id'\n"));
goto dump_form;
} else if (form_name && !strcmp(form_name, "frmLogin")) {
form = parse_form_node(vpninfo, node, "btnSubmit", oncp_can_gen_tokencode);
} else if (form_id && !strcmp(form_id, "loginForm")) {
form = parse_form_node(vpninfo, node, "submitButton", oncp_can_gen_tokencode);
} else if ((form_name && !strcmp(form_name, "frmDefender")) ||
(form_name && !strcmp(form_name, "frmNextToken"))) {
form = parse_form_node(vpninfo, node, "btnAction", oncp_can_gen_tokencode);
} else if (form_name && !strcmp(form_name, "frmConfirmation")) {
form = parse_form_node(vpninfo, node, "btnContinue", oncp_can_gen_tokencode);
if (!form) {
ret = -EINVAL;
break;
}
/* XXX: Actually ask the user? */
goto form_done;
} else if (form_name && !strcmp(form_name, "frmSelectRoles")) {
form = parse_roles_form_node(node);
role_select = 1;
} else if (form_name && !strcmp(form_name, "frmTotpToken")) {
form = parse_form_node(vpninfo, node, "totpactionEnter", oncp_can_gen_tokencode);
} else if ((form_name && !strcmp(form_name, "hiddenform")) ||
(form_id && !strcmp(form_id, "formSAMLSSO"))) {
form = parse_form_node(vpninfo, node, "submit", oncp_can_gen_tokencode);
} else {
char *form_action = (char *)xmlGetProp(node, (unsigned char *)"action");
if (form_action && strstr(form_action, "remediate.cgi")) {
vpn_progress(vpninfo, PRG_ERR,
_("Form action (%s) likely indicates that TNCC/Host Checker failed.\n"),
form_action);
}
free(form_action);
vpn_progress(vpninfo, PRG_ERR,
_("Unknown form (name '%s', id '%s')\n"),
form_name, form_id);
dump_form:
fprintf(stderr, _("Dumping unknown HTML form:\n"));
htmlNodeDumpFileFormat(stderr, node->doc, node, NULL, 1);
ret = -EINVAL;
break;
}
if (!form) {
ret = -EINVAL;
break;
}
do {
ret = process_auth_form(vpninfo, form);
} while (ret == OC_FORM_RESULT_NEWGROUP);
if (ret)
goto out;
ret = do_gen_tokencode(vpninfo, form);
if (ret) {
vpn_progress(vpninfo, PRG_ERR, _("Failed to generate OTP tokencode; disabling token\n"));
vpninfo->token_bypassed = 1;
goto out;
}
/* frmSelectRoles is special; it's actually *links*, not a form. So
* we need to process it differently... */
if (role_select) {
vpninfo->redirect_url = strdup(form->opts[0]._value);
goto do_redirect;
}
form_done:
append_form_opts(vpninfo, form, resp_buf);
ret = buf_error(resp_buf);
if (ret)
break;
if (form->action) {
vpninfo->redirect_url = form->action;
form->action = NULL;
}
do_redirect:
free_auth_form(form);
form = NULL;
if (vpninfo->redirect_url)
handle_redirect(vpninfo);
tncc_done:
xmlFreeDoc(doc);
doc = NULL;
}
out:
if (doc)
xmlFreeDoc(doc);
free(form_name);
free(form_id);
if (form)
free_auth_form(form);
buf_free(resp_buf);
return ret;
}