forked from dlenski/openconnect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth-globalprotect.c
482 lines (424 loc) · 15.6 KB
/
auth-globalprotect.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
/*
* OpenConnect (SSL + DTLS) VPN client
*
* Author: Dan Lenski <dlenski@gmail.com>
*
* 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 <errno.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include "openconnect-internal.h"
void gpst_common_headers(struct openconnect_info *vpninfo, struct oc_text_buf *buf)
{
http_common_headers(vpninfo, buf);
}
/* The GlobalProtect auth form always has two visible fields:
* 1) username
* 2) one secret value:
* - normal account password
* - "challenge" (2FA) password, along with form name in auth_id
* - cookie from external authentication flow (INSTEAD OF password)
*
* This function steals the value of auth_id and prompt and username for
* use in the auth form; pw_or_cookie_field is NOT stolen.
*/
static struct oc_auth_form *auth_form(struct openconnect_info *vpninfo, char *prompt, char *auth_id, char *username, char *pw_or_cookie_field)
{
static struct oc_auth_form *form;
static struct oc_form_opt *opt, *opt2;
form = calloc(1, sizeof(*form));
if (!form)
return NULL;
form->message = prompt ? : strdup(_("Please enter your username and password"));
form->auth_id = auth_id ? : strdup("_default");
opt = form->opts = calloc(1, sizeof(*opt));
if (!opt)
return NULL;
opt->name=strdup("user");
opt->label=strdup(_("Username: "));
if (username) {
opt->type = OC_FORM_OPT_HIDDEN;
opt->_value = username;
} else {
opt->type = OC_FORM_OPT_TEXT;
opt->flags = OC_FORM_OPT_FILL_USERNAME;
}
opt2 = opt->next = calloc(1, sizeof(*opt));
if (!opt2)
return NULL;
opt2->name = strdup(pw_or_cookie_field ? : "passwd");
asprintf(&opt2->label, "%s: ", auth_id ? _("Challenge") : (pw_or_cookie_field ? : _("Password")));
opt2->type = vpninfo->token_mode!=OC_TOKEN_MODE_NONE ? OC_FORM_OPT_TOKEN : OC_FORM_OPT_PASSWORD;
opt2->flags = OC_FORM_OPT_FILL_PASSWORD;
form->opts = opt;
return form;
}
/* Return value:
* < 0, on error
* = 0, on success; *form is populated
*/
struct gp_login_arg { const char *opt; int save:1; int show:1; int warn_missing:1; int err_missing:1; const char *check; };
static const struct gp_login_arg gp_login_args[] = {
[0] = { .opt="unknown-arg0", .show=1 },
[1] = { .opt="authcookie", .save=1, .err_missing=1 },
[2] = { .opt="persistent-cookie", .warn_missing=1 }, /* 40 hex digits; persists across sessions */
[3] = { .opt="portal", .save=1, .warn_missing=1 },
[4] = { .opt="user", .save=1, .err_missing=1 },
[5] = { .opt="authentication-source", .show=1 }, /* LDAP-auth, AUTH-RADIUS_RSA_OTP, etc. */
[6] = { .opt="configuration", .warn_missing=1 }, /* usually vsys1 (sometimes vsys2, etc.) */
[7] = { .opt="domain", .save=1, .warn_missing=1 },
[8] = { .opt="unknown-arg8", .show=1 },
[9] = { .opt="unknown-arg9", .show=1 },
[10] = { .opt="unknown-arg10", .show=1 },
[11] = { .opt="unknown-arg11", .show=1 },
[12] = { .opt="connection-type", .err_missing=1, .check="tunnel" },
[13] = { .opt="password-expiration-days", .show=1 }, /* days until password expires, if not -1 */
[14] = { .opt="clientVer", .err_missing=1, .check="4100" },
[15] = { .opt="preferred-ip", .save=1 },
};
const int gp_login_nargs = (sizeof(gp_login_args)/sizeof(*gp_login_args));
static int parse_login_xml(struct openconnect_info *vpninfo, xmlNode *xml_node)
{
struct oc_text_buf *cookie = buf_alloc();
const char *value = NULL;
const struct gp_login_arg *arg;
if (!xmlnode_is_named(xml_node, "jnlp"))
goto err_out;
xml_node = xml_node->children;
while (xml_node && xml_node->type != XML_ELEMENT_NODE)
xml_node = xml_node->next;
if (!xmlnode_is_named(xml_node, "application-desc"))
goto err_out;
xml_node = xml_node->children;
for (arg=gp_login_args; arg<gp_login_args+gp_login_nargs; arg++) {
if (!arg->opt)
continue;
while (xml_node && xml_node->type != XML_ELEMENT_NODE)
xml_node = xml_node->next;
if (!xml_node)
value = NULL;
else if (!xmlnode_is_named(xml_node, "argument"))
goto err_out;
else {
value = (const char *)xmlNodeGetContent(xml_node);
if (value && (!strlen(value) || !strcmp(value, "(null)") || !strcmp(value, "-1"))) {
free((void *)value);
value = NULL;
}
xml_node = xml_node->next;
}
if (arg->check && (value==NULL || strcmp(value, arg->check))) {
vpn_progress(vpninfo, arg->err_missing ? PRG_ERR : PRG_DEBUG,
_("GlobalProtect login returned %s=%s (expected %s)\n"), arg->opt, value, arg->check);
if (arg->err_missing) goto err_out;
} else if ((arg->err_missing || arg->warn_missing) && value==NULL) {
vpn_progress(vpninfo, arg->err_missing ? PRG_ERR : PRG_DEBUG,
_("GlobalProtect login returned empty or missing %s\n"), arg->opt);
if (arg->err_missing) goto err_out;
} else if (value && arg->show) {
vpn_progress(vpninfo, PRG_INFO,
_("GlobalProtect login returned %s=%s\n"), arg->opt, value);
}
if (value && arg->save)
append_opt(cookie, arg->opt, value);
free((void *)value);
}
vpninfo->cookie = strdup(cookie->data);
return buf_free(cookie);
err_out:
free((void *)value);
buf_free(cookie);
return -EINVAL;
}
static int parse_portal_xml(struct openconnect_info *vpninfo, xmlNode *xml_node)
{
struct oc_auth_form *form;
xmlNode *x = NULL;
struct oc_form_opt_select *opt;
struct oc_text_buf *buf = NULL;
int max_choices = 0, result;
char *portal = NULL;
form = calloc(1, sizeof(*form));
if (!form)
return -ENOMEM;
form->message = strdup(_("Please select GlobalProtect gateway."));
form->auth_id = strdup("_portal");
opt = form->authgroup_opt = calloc(1, sizeof(*opt));
if (!opt) {
result = -ENOMEM;
goto out;
}
opt->form.type = OC_FORM_OPT_SELECT;
opt->form.name = strdup("gateway");
opt->form.label = strdup(_("GATEWAY:"));
form->opts = (void *)opt;
/* The portal contains a ton of stuff, but basically none of it is useful to a VPN client
* that wishes to give control to the client user, as opposed to the VPN administrator.
* The exception is the list of gateways in policy/gateways/external/list
*/
if (xmlnode_is_named(xml_node, "policy")) {
for (x = xml_node->children, xml_node = NULL; x; x = x->next) {
if (xmlnode_is_named(x, "portal-name"))
portal = (char *)xmlNodeGetContent(x);
else if (xmlnode_is_named(x, "gateways"))
xml_node = x;
}
}
if (xml_node) {
for (xml_node = xml_node->children; xml_node; xml_node = xml_node->next)
if (xmlnode_is_named(xml_node, "external"))
for (xml_node = xml_node->children; xml_node; xml_node = xml_node->next)
if (xmlnode_is_named(xml_node, "list"))
goto gateways;
}
result = -EINVAL;
goto out;
gateways:
if (vpninfo->write_new_config) {
buf = buf_alloc();
buf_append(buf, "<GPPortal>\n <ServerList>\n");
if (portal) {
buf_append(buf, " <HostEntry><HostName>");
buf_append_xmlescaped(buf, portal);
buf_append(buf, "</HostName><HostAddress>%s", vpninfo->hostname);
if (vpninfo->port!=443)
buf_append(buf, ":%d", vpninfo->port);
buf_append(buf, "/global-protect</HostAddress></HostEntry>\n");
}
}
/* first, count the number of gateways */
for (x = xml_node->children; x; x = x->next)
if (xmlnode_is_named(x, "entry"))
max_choices++;
opt->choices = calloc(max_choices, sizeof(opt->choices[0]));
if (!opt->choices) {
result = -ENOMEM;
goto out;
}
/* each entry looks like <entry name="host[:443]"><description>Label</description></entry> */
vpn_progress(vpninfo, PRG_INFO, _("%d gateway servers available:\n"), max_choices);
for (xml_node = xml_node->children; xml_node; xml_node = xml_node->next) {
if (xmlnode_is_named(xml_node, "entry")) {
struct oc_choice *choice = calloc(1, sizeof(*choice));
if (!choice) {
result = -ENOMEM;
goto out;
}
xmlnode_get_prop(xml_node, "name", &choice->name);
for (x = xml_node->children; x; x=x->next)
if (xmlnode_is_named(x, "description")) {
choice->label = (char *)xmlNodeGetContent(x);
if (vpninfo->write_new_config) {
buf_append(buf, " <HostEntry><HostName>");
buf_append_xmlescaped(buf, choice->label);
buf_append(buf, "</HostName><HostAddress>%s/ssl-vpn</HostAddress></HostEntry>\n",
choice->name);
}
}
opt->choices[opt->nr_choices++] = choice;
vpn_progress(vpninfo, PRG_INFO, _(" %s (%s)\n"),
choice->label, choice->name);
}
}
if (vpninfo->write_new_config) {
buf_append(buf, " </ServerList>\n</GPPortal>\n");
if ((result = buf_error(buf)))
goto out;
if ((result = vpninfo->write_new_config(vpninfo, buf->data, buf->pos)))
goto out;
}
/* process auth form to select gateway */
result = process_auth_form(vpninfo, form);
if (result != OC_FORM_RESULT_NEWGROUP)
goto out;
/* redirect to the gateway (no-op if it's the same host) */
free(vpninfo->redirect_url);
if (asprintf(&vpninfo->redirect_url, "https://%s", vpninfo->authgroup) == 0) {
result = -ENOMEM;
goto out;
}
result = handle_redirect(vpninfo);
out:
buf_free(buf);
free(portal);
free_auth_form(form);
return result;
}
static int gpst_login(struct openconnect_info *vpninfo, int portal, const char *pw_or_cookie_field)
{
int result;
struct oc_auth_form *form = NULL;
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;
char *prompt=NULL, *auth_id=NULL, *username=NULL;
#ifdef HAVE_LIBSTOKEN
/* Step 1: Unlock software token (if applicable) */
if (vpninfo->token_mode == OC_TOKEN_MODE_STOKEN) {
result = prepare_stoken(vpninfo);
if (result)
goto out;
}
#endif
form = auth_form(vpninfo, prompt, auth_id, username, pw_or_cookie_field);
if (!form)
return -ENOMEM;
/* Ask the user to fill in the auth form; repeat as necessary */
for (;;) {
/* process auth form (username and password or challenge) */
result = process_auth_form(vpninfo, form);
if (result)
goto out;
reuse_whole_form:
buf_truncate(request_body);
/* generate token code if specified */
result = do_gen_tokencode(vpninfo, form);
if (result) {
vpn_progress(vpninfo, PRG_ERR, _("Failed to generate OTP tokencode; disabling token\n"));
vpninfo->token_bypassed = 1;
goto out;
}
/* submit gateway login (ssl-vpn/login.esp) or portal config (global-protect/getconfig.esp) request */
buf_truncate(request_body);
buf_append(request_body, "jnlpReady=jnlpReady&ok=Login&direct=yes&clientVer=4100&prot=https:");
if (!strcmp(vpninfo->platname, "win"))
append_opt(request_body, "clientos", "Windows");
else
append_opt(request_body, "clientos", vpninfo->platname);
append_opt(request_body, "server", vpninfo->hostname);
append_opt(request_body, "computer", vpninfo->localname);
if (vpninfo->ip_info.addr)
append_opt(request_body, "preferred-ip", vpninfo->ip_info.addr);
if (form->auth_id && form->auth_id[0]!='_')
append_opt(request_body, "inputStr", form->auth_id);
append_form_opts(vpninfo, form, request_body);
if ((result = buf_error(request_body)))
goto out;
orig_path = vpninfo->urlpath;
vpninfo->urlpath = strdup(portal ? "global-protect/getconfig.esp" : "ssl-vpn/login.esp");
result = do_https_request(vpninfo, method, request_body_type, request_body,
&xml_buf, 0);
free(vpninfo->urlpath);
vpninfo->urlpath = orig_path;
/* Result could be either a JavaScript challenge or XML */
result = gpst_xml_or_error(vpninfo, result, xml_buf,
portal ? parse_portal_xml : parse_login_xml, &prompt, &auth_id);
if (result == -EAGAIN) {
reuse_username:
/* Steal and reuse username from first form */
username = form->opts ? form->opts->_value : NULL;
form->opts->_value = NULL;
free_auth_form(form);
form = auth_form(vpninfo, prompt, auth_id, username, pw_or_cookie_field);
prompt = auth_id = username = NULL;
if (!form)
return -ENOMEM;
} else if (portal && result == 0) {
/* Portal login succeeded; reuse same credentials to login to gateway,
* unless it was a challenge auth form, in which case we only
* reuse the username.
*/
portal = 0;
if (form->auth_id && form->auth_id[0] != '_')
goto reuse_username;
else
goto reuse_whole_form;
} else if (result == -EACCES) {
/* Invalid username/password; reuse same form, but blank */
nuke_opt_values(form->opts);
} else
break;
}
out:
free_auth_form(form);
buf_free(request_body);
free(xml_buf);
return result;
}
int gpst_obtain_cookie(struct openconnect_info *vpninfo)
{
char *pw_or_cookie_field = NULL;
int result;
/* An alternate password/secret field may be specified in the "URL path". Known possibilities:
* /portal:portal-userauthcookie
* /gateway:prelogin-cookie
*/
if (vpninfo->urlpath
&& (pw_or_cookie_field = strrchr(vpninfo->urlpath, ':'))!=NULL) {
*pw_or_cookie_field = '\0';
pw_or_cookie_field++;
}
if (vpninfo->urlpath && (!strcmp(vpninfo->urlpath, "portal") || !strncmp(vpninfo->urlpath, "global-protect", 14))) {
/* assume the server is a portal */
return gpst_login(vpninfo, 1, pw_or_cookie_field);
} else if (vpninfo->urlpath && (!strcmp(vpninfo->urlpath, "gateway") || !strncmp(vpninfo->urlpath, "ssl-vpn", 7))) {
/* assume the server is a gateway */
return gpst_login(vpninfo, 0, pw_or_cookie_field);
} else {
/* first try handling it as a gateway, then a portal */
result = gpst_login(vpninfo, 0, pw_or_cookie_field);
if (result == -EEXIST) {
result = gpst_login(vpninfo, 1, pw_or_cookie_field);
if (result == -EEXIST)
vpn_progress(vpninfo, PRG_ERR, _("Server is neither a GlobalProtect portal nor a gateway.\n"));
}
return result;
}
}
int gpst_bye(struct openconnect_info *vpninfo, const char *reason)
{
char *orig_path;
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;
/* In order to logout successfully, the client must send not only
* the session's authcookie, but also the portal, user, computer,
* and domain matching the values sent with the getconfig request.
*
* You read that right: the client must send a bunch of irrelevant
* non-secret values in its logout request. If they're wrong or
* missing, the logout will fail and the authcookie will remain
* valid -- which is a security hole.
*
* Don't blame me. I didn't design this.
*/
append_opt(request_body, "computer", vpninfo->localname);
buf_append(request_body, "&%s", vpninfo->cookie);
if ((result = buf_error(request_body)))
goto out;
/* We need to close and reopen the HTTPS connection (to kill
* the tunnel session) and submit a new HTTPS request to
* logout.
*/
orig_path = vpninfo->urlpath;
vpninfo->urlpath = strdup("ssl-vpn/logout.esp");
openconnect_close_https(vpninfo, 0);
result = do_https_request(vpninfo, method, request_body_type, request_body,
&xml_buf, 0);
free(vpninfo->urlpath);
vpninfo->urlpath = orig_path;
/* logout.esp returns HTTP status 200 and <response status="success"> when
* successful, and all manner of malformed junk when unsuccessful.
*/
result = gpst_xml_or_error(vpninfo, result, xml_buf, NULL, NULL, NULL);
if (result < 0)
vpn_progress(vpninfo, PRG_ERR, _("Logout failed.\n"));
else
vpn_progress(vpninfo, PRG_INFO, _("Logout successful\n"));
out:
buf_free(request_body);
free(xml_buf);
return result;
}