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
|
/*
* 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 "openconnect-internal.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>
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, 0);
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;
}
static int bearer_authorization(struct openconnect_info *vpninfo, int proxy,
struct http_auth_state *auth_state,
struct oc_text_buf *hdrbuf)
{
const char *bearer_token = vpninfo->bearer_token;
if (proxy) {
return -EINVAL;
}
if (!bearer_token)
return -EINVAL;
if (auth_state->state == AUTH_IN_PROGRESS) {
auth_state->state = AUTH_FAILED;
return -EAGAIN;
}
buf_append(hdrbuf, "Authorization: Bearer %s\r\n", bearer_token);
vpn_progress(vpninfo, PRG_INFO, _("Attempting HTTP Bearer authentication to server '%s'\n"),
vpninfo->hostname);
auth_state->state = AUTH_IN_PROGRESS;
return 0;
}
#if !defined(HAVE_GSSAPI) && !defined(_WIN32)
static int no_gssapi_authorization(struct openconnect_info *vpninfo, int proxy,
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 },
{ AUTH_TYPE_BEARER, "Bearer", bearer_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 < ARRAY_SIZE(auth_methods); 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];
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;
}
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"));
if (vpninfo->retry_on_auth_fail) {
/* Try again without the X-Support-HTTP-Auth: header */
vpninfo->try_http_auth = 0;
return 0;
}
return -ENOENT;
}
/* Returns non-zero if it matched */
static int handle_auth_proto(struct openconnect_info *vpninfo,
struct http_auth_state *auth_states,
struct auth_method *method, char *hdr)
{
struct http_auth_state *auth = &auth_states[method->state_index];
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 < ARRAY_SIZE(auth_methods); i++) {
/* Return once we've found a match */
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;
if (!strcasecmp(hdr, "X-HTTP-Auth-Support") &&
!strcasecmp(val, "fallback")) {
vpninfo->retry_on_auth_fail = 1;
return 0;
}
if (strcasecmp(hdr, "WWW-Authenticate"))
return 0;
for (i = 0; i < ARRAY_SIZE(auth_methods); i++) {
/* Return once we've found a match */
if (handle_auth_proto(vpninfo, vpninfo->http_auth, &auth_methods[i], val))
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 < ARRAY_SIZE(auth_methods); 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)
continue;
if (reset || auth->state == AUTH_AVAILABLE)
auth->state = AUTH_UNSEEN;
}
}
static int set_authmethods(struct openconnect_info *vpninfo, struct http_auth_state *auth_states,
const char *methods)
{
int i, len;
const char *p;
for (i = 0; i < ARRAY_SIZE(auth_methods); i++)
auth_states[auth_methods[i].state_index].state = AUTH_DISABLED;
while (methods) {
p = strchr(methods, ',');
if (p) {
len = p - methods;
p++;
} else
len = strlen(methods);
for (i = 0; i < ARRAY_SIZE(auth_methods); i++) {
if (strprefix_match(methods, len, auth_methods[i].name) ||
(auth_methods[i].state_index == AUTH_TYPE_GSSAPI &&
strprefix_match(methods, len, "gssapi"))) {
auth_states[auth_methods[i].state_index].state = AUTH_UNSEEN;
break;
}
}
methods = p;
}
return 0;
}
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);
}
|