Skip to content

Latest commit

 

History

History
402 lines (344 loc) · 10.2 KB

http-auth.c

File metadata and controls

402 lines (344 loc) · 10.2 KB
 
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
/*
* 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 <stdarg.h>
#include "openconnect-internal.h"
/* Ick. Yet another wheel to reinvent. But although we could pull it
in from OpenSSL, we can't from GnuTLS */
static inline int b64_char(char c)
{
if (c >= 'A' && c <= 'Z')
return c - 'A';
if (c >= 'a' && c <= 'z')
return c - 'a' + 26;
if (c >= '0' && c <= '9')
return c - '0' + 52;
if (c == '+')
return 62;
if (c == '/')
return 63;
return -1;
}
void *openconnect_base64_decode(int *ret_len, const char *in)
{
unsigned char *buf;
int b[4];
int len = strlen(in);
if (len & 3) {
*ret_len = -EINVAL;
return NULL;
}
len = (len * 3) / 4;
buf = malloc(len);
if (!buf) {
*ret_len = -ENOMEM;
return NULL;
}
len = 0;
while (*in) {
if (!in[1] || !in[2] || !in[3])
goto err;
b[0] = b64_char(in[0]);
b[1] = b64_char(in[1]);
if (b[0] < 0 || b[1] < 0)
goto err;
buf[len++] = (b[0] << 2) | (b[1] >> 4);
if (in[2] == '=') {
if (in[3] != '=' || in[4] != 0)
goto err;
break;
}
b[2] = b64_char(in[2]);
if (b[2] < 0)
goto err;
buf[len++] = (b[1] << 4) | (b[2] >> 2);
if (in[3] == '=') {
if (in[4] != 0)
goto err;
break;
}
b[3] = b64_char(in[3]);
if (b[3] < 0)
goto err;
buf[len++] = (b[2] << 6) | b[3];
in += 4;
}
*ret_len = len;
return buf;
err:
free(buf);
*ret_len = EINVAL;
return NULL;
}
static const char b64_table[] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
};
void buf_append_base64(struct oc_text_buf *buf, const void *bytes, int len)
{
const unsigned char *in = bytes;
int hibits;
if (!buf || buf->error)
return;
if (buf_ensure_space(buf, (4 * (len + 2) / 3) + 1))
return;
while (len > 0) {
buf->data[buf->pos++] = b64_table[in[0] >> 2];
hibits = (in[0] << 4) & 0x30;
if (len == 1) {
buf->data[buf->pos++] = b64_table[hibits];
buf->data[buf->pos++] = '=';
buf->data[buf->pos++] = '=';
break;
}
buf->data[buf->pos++] = b64_table[hibits | (in[1] >> 4)];
hibits = (in[1] << 2) & 0x3c;
if (len == 2) {
buf->data[buf->pos++] = b64_table[hibits];
buf->data[buf->pos++] = '=';
break;
}
buf->data[buf->pos++] = b64_table[hibits | (in[2] >> 6)];
buf->data[buf->pos++] = b64_table[in[2] & 0x3f];
in += 3;
len -= 3;
}
buf->data[buf->pos] = 0;
}
static int basic_authorization(struct openconnect_info *vpninfo, int proxy,
struct http_auth_state *auth_state,
struct oc_text_buf *hdrbuf)
{
struct oc_text_buf *text;
const char *user, *pass;
if (proxy) {
user = vpninfo->proxy_user;
pass = vpninfo->proxy_pass;
} else {
/* Need to parse this out of the URL */
return -EINVAL;
}
if (!user || !pass)
return -EINVAL;
if (auth_state->state == AUTH_IN_PROGRESS) {
auth_state->state = AUTH_FAILED;
return -EAGAIN;
}
text = buf_alloc();
buf_append(text, "%s:%s", user, pass);
if (buf_error(text))
return buf_free(text);
buf_append(hdrbuf, "%sAuthorization: Basic ", proxy ? "Proxy-" : "");
buf_append_base64(hdrbuf, text->data, text->pos);
buf_append(hdrbuf, "\r\n");
memset(text->data, 0, text->pos);
buf_free(text);
if (proxy)
vpn_progress(vpninfo, PRG_INFO, _("Attempting HTTP Basic authentication to proxy\n"));
else
vpn_progress(vpninfo, PRG_INFO, _("Attempting HTTP Basic authentication to server '%s'\n"),
vpninfo->hostname);
auth_state->state = AUTH_IN_PROGRESS;
return 0;
}
#if !defined(HAVE_GSSAPI) && !defined(_WIN32)
Mar 15, 2015
Mar 15, 2015
194
static int no_gssapi_authorization(struct openconnect_info *vpninfo, int proxy,
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
struct http_auth_state *auth_state,
struct oc_text_buf *hdrbuf)
{
/* This comes last so just complain. We're about to bail. */
vpn_progress(vpninfo, PRG_ERR,
_("This version of OpenConnect was built without GSSAPI support\n"));
auth_state->state = AUTH_FAILED;
return -ENOENT;
}
#endif
struct auth_method {
int state_index;
const char *name;
int (*authorization)(struct openconnect_info *, int, struct http_auth_state *, struct oc_text_buf *);
void (*cleanup)(struct openconnect_info *, struct http_auth_state *);
} auth_methods[] = {
#if defined(HAVE_GSSAPI) || defined(_WIN32)
{ AUTH_TYPE_GSSAPI, "Negotiate", gssapi_authorization, cleanup_gssapi_auth },
#endif
{ AUTH_TYPE_NTLM, "NTLM", ntlm_authorization, cleanup_ntlm_auth },
{ AUTH_TYPE_DIGEST, "Digest", digest_authorization, NULL },
{ AUTH_TYPE_BASIC, "Basic", basic_authorization, NULL },
#if !defined(HAVE_GSSAPI) && !defined(_WIN32)
{ AUTH_TYPE_GSSAPI, "Negotiate", no_gssapi_authorization, NULL }
#endif
};
/* Generate Proxy-Authorization: header for request if appropriate */
int gen_authorization_hdr(struct openconnect_info *vpninfo, int proxy,
struct oc_text_buf *buf)
{
int ret;
int i;
for (i = 0; i < sizeof(auth_methods) / sizeof(auth_methods[0]); i++) {
struct http_auth_state *auth_state;
if (proxy)
auth_state = &vpninfo->proxy_auth[auth_methods[i].state_index];
else
auth_state = &vpninfo->http_auth[auth_methods[i].state_index];
Feb 24, 2015
Feb 24, 2015
236
237
238
239
240
241
242
243
244
245
246
247
248
249
if (auth_state->state == AUTH_DEFAULT_DISABLED) {
if (proxy)
vpn_progress(vpninfo, PRG_ERR,
_("Proxy requested Basic authentication which is disabled by default\n"));
else
vpn_progress(vpninfo, PRG_ERR,
_("Server '%s' requested Basic authentication which is disabled by default\n"),
vpninfo->hostname);
auth_state->state = AUTH_FAILED;
return -EINVAL;
}
250
251
252
253
254
255
256
if (auth_state->state > AUTH_UNSEEN) {
ret = auth_methods[i].authorization(vpninfo, proxy, auth_state, buf);
if (ret == -EAGAIN || !ret)
return ret;
}
}
vpn_progress(vpninfo, PRG_INFO, _("No more authentication methods to try\n"));
Feb 24, 2015
Feb 24, 2015
257
258
259
260
261
262
if (vpninfo->retry_on_auth_fail) {
/* Try again without the X-Support-HTTP-Auth: header */
vpninfo->try_http_auth = 0;
return 0;
}
263
264
265
266
267
return -ENOENT;
}
/* Returns non-zero if it matched */
static int handle_auth_proto(struct openconnect_info *vpninfo,
Feb 24, 2015
Feb 24, 2015
268
struct http_auth_state *auth_states,
269
270
struct auth_method *method, char *hdr)
{
Feb 24, 2015
Feb 24, 2015
271
struct http_auth_state *auth = &auth_states[method->state_index];
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
int l = strlen(method->name);
if (auth->state <= AUTH_FAILED)
return 0;
if (strncmp(method->name, hdr, l))
return 0;
if (hdr[l] != ' ' && hdr[l] != 0)
return 0;
if (auth->state == AUTH_UNSEEN)
auth->state = AUTH_AVAILABLE;
free(auth->challenge);
if (hdr[l])
auth->challenge = strdup(hdr + l + 1);
else
auth->challenge = NULL;
return 1;
}
int proxy_auth_hdrs(struct openconnect_info *vpninfo, char *hdr, char *val)
{
int i;
if (!strcasecmp(hdr, "Proxy-Connection") ||
!strcasecmp(hdr, "Connection")) {
if (!strcasecmp(val, "close"))
vpninfo->proxy_close_during_auth = 1;
return 0;
}
if (strcasecmp(hdr, "Proxy-Authenticate"))
return 0;
for (i = 0; i < sizeof(auth_methods) / sizeof(auth_methods[0]); i++) {
/* Return once we've found a match */
Feb 24, 2015
Feb 24, 2015
310
311
312
313
314
315
316
317
318
319
320
if (handle_auth_proto(vpninfo, vpninfo->proxy_auth, &auth_methods[i], val))
return 0;
}
return 0;
}
int http_auth_hdrs(struct openconnect_info *vpninfo, char *hdr, char *val)
{
int i;
Feb 24, 2015
Feb 24, 2015
321
322
323
324
325
326
if (!strcasecmp(hdr, "X-HTTP-Auth-Support") &&
!strcasecmp(val, "fallback")) {
vpninfo->retry_on_auth_fail = 1;
return 0;
}
Feb 24, 2015
Feb 24, 2015
327
328
329
330
331
332
if (strcasecmp(hdr, "WWW-Authenticate"))
return 0;
for (i = 0; i < sizeof(auth_methods) / sizeof(auth_methods[0]); i++) {
/* Return once we've found a match */
if (handle_auth_proto(vpninfo, vpninfo->http_auth, &auth_methods[i], val))
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
return 0;
}
return 0;
}
void clear_auth_states(struct openconnect_info *vpninfo,
struct http_auth_state *auth_states, int reset)
{
int i;
for (i = 0; i < sizeof(auth_methods) / sizeof(auth_methods[0]); i++) {
struct http_auth_state *auth = &auth_states[auth_methods[i].state_index];
/* The 'reset' argument is set when we're connected successfully,
to fully reset the state to allow another connection to start
again. Otherwise, we need to remember which auth methods have
been tried and should not be attempted again. */
if (reset && auth_methods[i].cleanup)
auth_methods[i].cleanup(vpninfo, auth);
free(auth->challenge);
auth->challenge = NULL;
/* If it *failed* don't try it again even next time */
if (auth->state <= AUTH_FAILED)
Feb 24, 2015
Feb 24, 2015
358
continue;
359
360
361
362
363
if (reset || auth->state == AUTH_AVAILABLE)
auth->state = AUTH_UNSEEN;
}
}
Feb 24, 2015
Feb 24, 2015
364
365
static int set_authmethods(struct openconnect_info *vpninfo, struct http_auth_state *auth_states,
const char *methods)
366
367
368
369
370
{
int i, len;
const char *p;
for (i = 0; i < sizeof(auth_methods) / sizeof(auth_methods[0]); i++)
Feb 24, 2015
Feb 24, 2015
371
auth_states[auth_methods[i].state_index].state = AUTH_DISABLED;
372
373
374
375
376
377
378
379
380
381
382
383
384
while (methods) {
p = strchr(methods, ',');
if (p) {
len = p - methods;
p++;
} else
len = strlen(methods);
for (i = 0; i < sizeof(auth_methods) / sizeof(auth_methods[0]); i++) {
if (strprefix_match(methods, len, auth_methods[i].name) ||
(auth_methods[i].state_index == AUTH_TYPE_GSSAPI &&
strprefix_match(methods, len, "gssapi"))) {
Feb 24, 2015
Feb 24, 2015
385
auth_states[auth_methods[i].state_index].state = AUTH_UNSEEN;
386
387
388
389
390
391
392
393
break;
}
}
methods = p;
}
return 0;
}
Feb 24, 2015
Feb 24, 2015
394
395
396
397
398
399
400
401
402
int openconnect_set_http_auth(struct openconnect_info *vpninfo, const char *methods)
{
return set_authmethods(vpninfo, vpninfo->http_auth, methods);
}
int openconnect_set_proxy_auth(struct openconnect_info *vpninfo, const char *methods)
{
return set_authmethods(vpninfo, vpninfo->proxy_auth, methods);
}