2003-06-10 08:22:19 -04:00
|
|
|
/***************************************************************************
|
2004-06-24 07:54:11 -04:00
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
2003-06-10 08:22:19 -04:00
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2015-01-09 15:29:26 -05:00
|
|
|
* Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2003-06-10 08:22:19 -04:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2016-02-02 18:19:02 -05:00
|
|
|
* are also available at https://curl.haxx.se/docs/copyright.html.
|
2004-06-24 07:54:11 -04:00
|
|
|
*
|
2003-06-10 08:22:19 -04:00
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
2011-07-26 11:23:27 -04:00
|
|
|
|
2013-01-06 13:06:49 -05:00
|
|
|
#include "curl_setup.h"
|
2003-06-10 08:22:19 -04:00
|
|
|
|
2015-01-09 15:29:26 -05:00
|
|
|
#if defined(HAVE_GSSAPI) && !defined(CURL_DISABLE_HTTP) && defined(USE_SPNEGO)
|
2003-06-10 08:22:19 -04:00
|
|
|
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "urldata.h"
|
|
|
|
#include "sendf.h"
|
2011-07-13 16:54:54 -04:00
|
|
|
#include "curl_gssapi.h"
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "rawstr.h"
|
2008-08-16 20:25:38 -04:00
|
|
|
#include "curl_base64.h"
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "http_negotiate.h"
|
2015-09-12 06:48:24 -04:00
|
|
|
#include "vauth/vauth.h"
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "url.h"
|
2015-03-03 06:36:18 -05:00
|
|
|
#include "curl_printf.h"
|
2003-06-10 08:22:19 -04:00
|
|
|
|
2015-03-24 18:12:03 -04:00
|
|
|
/* The last #include files should be: */
|
|
|
|
#include "curl_memory.h"
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "memdebug.h"
|
2003-06-10 08:22:19 -04:00
|
|
|
|
2015-01-17 06:56:27 -05:00
|
|
|
CURLcode Curl_input_negotiate(struct connectdata *conn, bool proxy,
|
|
|
|
const char *header)
|
2004-06-24 07:54:11 -04:00
|
|
|
{
|
2011-07-19 13:10:43 -04:00
|
|
|
struct SessionHandle *data = conn->data;
|
|
|
|
struct negotiatedata *neg_ctx = proxy?&data->state.proxyneg:
|
|
|
|
&data->state.negotiate;
|
2013-04-03 19:57:25 -04:00
|
|
|
OM_uint32 major_status, minor_status, discard_st;
|
2015-01-18 12:36:59 -05:00
|
|
|
gss_buffer_desc spn_token = GSS_C_EMPTY_BUFFER;
|
2003-06-10 08:22:19 -04:00
|
|
|
gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
|
|
|
|
gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
|
2011-08-24 02:07:36 -04:00
|
|
|
size_t len;
|
|
|
|
size_t rawlen = 0;
|
2014-10-26 12:34:44 -04:00
|
|
|
CURLcode result;
|
2003-06-10 08:22:19 -04:00
|
|
|
|
2007-11-05 04:45:09 -05:00
|
|
|
if(neg_ctx->context && neg_ctx->status == GSS_S_COMPLETE) {
|
2011-04-19 09:54:13 -04:00
|
|
|
/* We finished successfully our part of authentication, but server
|
2003-06-10 08:22:19 -04:00
|
|
|
* rejected it (since we're again here). Exit with an error since we
|
|
|
|
* can't invent anything better */
|
2011-07-19 13:10:43 -04:00
|
|
|
Curl_cleanup_negotiate(data);
|
2015-01-17 06:56:27 -05:00
|
|
|
return CURLE_LOGIN_DENIED;
|
2003-06-10 08:22:19 -04:00
|
|
|
}
|
|
|
|
|
2015-01-18 12:36:59 -05:00
|
|
|
if(!neg_ctx->server_name) {
|
|
|
|
/* Generate our SPN */
|
2015-03-29 08:52:31 -04:00
|
|
|
char *spn = Curl_sasl_build_gssapi_spn(
|
|
|
|
proxy ? data->set.str[STRING_PROXY_SERVICE_NAME] :
|
|
|
|
data->set.str[STRING_SERVICE_NAME],
|
|
|
|
proxy ? conn->proxy.name : conn->host.name);
|
2015-01-18 12:36:59 -05:00
|
|
|
if(!spn)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
/* Populate the SPN structure */
|
|
|
|
spn_token.value = spn;
|
|
|
|
spn_token.length = strlen(spn);
|
|
|
|
|
|
|
|
/* Import the SPN */
|
|
|
|
major_status = gss_import_name(&minor_status, &spn_token,
|
|
|
|
GSS_C_NT_HOSTBASED_SERVICE,
|
|
|
|
&neg_ctx->server_name);
|
|
|
|
if(GSS_ERROR(major_status)) {
|
|
|
|
Curl_gss_log_error(data, minor_status, "gss_import_name() failed: ");
|
|
|
|
|
2015-03-16 10:01:15 -04:00
|
|
|
free(spn);
|
2015-01-18 12:36:59 -05:00
|
|
|
|
2015-01-17 06:56:27 -05:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2015-01-18 12:36:59 -05:00
|
|
|
}
|
|
|
|
|
2015-03-16 10:01:15 -04:00
|
|
|
free(spn);
|
2015-01-18 12:36:59 -05:00
|
|
|
}
|
2003-06-10 08:22:19 -04:00
|
|
|
|
2014-07-21 03:53:44 -04:00
|
|
|
header += strlen("Negotiate");
|
2006-10-17 17:32:56 -04:00
|
|
|
while(*header && ISSPACE(*header))
|
2003-06-10 08:22:19 -04:00
|
|
|
header++;
|
|
|
|
|
|
|
|
len = strlen(header);
|
2007-11-05 04:45:09 -05:00
|
|
|
if(len > 0) {
|
2014-10-26 12:34:44 -04:00
|
|
|
result = Curl_base64_decode(header, (unsigned char **)&input_token.value,
|
|
|
|
&rawlen);
|
2015-01-17 06:56:27 -05:00
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
|
2015-01-17 06:59:44 -05:00
|
|
|
if(!rawlen) {
|
|
|
|
infof(data, "Negotiate handshake failure (empty challenge message)\n");
|
|
|
|
|
2015-01-17 06:56:27 -05:00
|
|
|
return CURLE_BAD_CONTENT_ENCODING;
|
2015-01-17 06:59:44 -05:00
|
|
|
}
|
2015-01-17 06:56:27 -05:00
|
|
|
|
2003-06-26 07:25:23 -04:00
|
|
|
input_token.length = rawlen;
|
2003-09-19 08:56:22 -04:00
|
|
|
|
2013-04-03 19:57:25 -04:00
|
|
|
DEBUGASSERT(input_token.value != NULL);
|
2003-06-10 08:22:19 -04:00
|
|
|
}
|
|
|
|
|
2011-07-19 13:10:43 -04:00
|
|
|
major_status = Curl_gss_init_sec_context(data,
|
|
|
|
&minor_status,
|
2011-06-10 11:16:06 -04:00
|
|
|
&neg_ctx->context,
|
|
|
|
neg_ctx->server_name,
|
2014-07-23 12:56:19 -04:00
|
|
|
&Curl_spnego_mech_oid,
|
2011-06-10 11:16:06 -04:00
|
|
|
GSS_C_NO_CHANNEL_BINDINGS,
|
|
|
|
&input_token,
|
|
|
|
&output_token,
|
2014-12-04 16:59:20 -05:00
|
|
|
TRUE,
|
2011-06-10 11:16:06 -04:00
|
|
|
NULL);
|
2013-04-03 19:57:25 -04:00
|
|
|
Curl_safefree(input_token.value);
|
|
|
|
|
2003-06-10 08:22:19 -04:00
|
|
|
neg_ctx->status = major_status;
|
2007-11-05 04:45:09 -05:00
|
|
|
if(GSS_ERROR(major_status)) {
|
2013-04-03 19:57:25 -04:00
|
|
|
if(output_token.value)
|
|
|
|
gss_release_buffer(&discard_st, &output_token);
|
2014-12-02 17:21:58 -05:00
|
|
|
Curl_gss_log_error(conn->data, minor_status,
|
|
|
|
"gss_init_sec_context() failed: ");
|
2015-01-17 06:56:27 -05:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2003-06-10 08:22:19 -04:00
|
|
|
}
|
|
|
|
|
2013-04-03 19:57:25 -04:00
|
|
|
if(!output_token.value || !output_token.length) {
|
|
|
|
if(output_token.value)
|
|
|
|
gss_release_buffer(&discard_st, &output_token);
|
2015-01-17 06:56:27 -05:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2003-06-10 08:22:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
neg_ctx->output_token = output_token;
|
2004-06-24 07:54:11 -04:00
|
|
|
|
2015-01-17 06:56:27 -05:00
|
|
|
return CURLE_OK;
|
|
|
|
}
|
2003-06-10 08:22:19 -04:00
|
|
|
|
2007-09-21 07:05:31 -04:00
|
|
|
CURLcode Curl_output_negotiate(struct connectdata *conn, bool proxy)
|
2004-06-24 07:54:11 -04:00
|
|
|
{
|
2007-11-20 18:17:08 -05:00
|
|
|
struct negotiatedata *neg_ctx = proxy?&conn->data->state.proxyneg:
|
|
|
|
&conn->data->state.negotiate;
|
2003-06-10 08:22:19 -04:00
|
|
|
char *encoded = NULL;
|
2011-08-24 02:07:36 -04:00
|
|
|
size_t len = 0;
|
2010-08-16 16:19:38 -04:00
|
|
|
char *userp;
|
2014-10-26 12:34:44 -04:00
|
|
|
CURLcode result;
|
2013-04-03 19:57:25 -04:00
|
|
|
OM_uint32 discard_st;
|
2003-09-19 08:56:22 -04:00
|
|
|
|
2014-10-26 12:34:44 -04:00
|
|
|
result = Curl_base64_encode(conn->data,
|
|
|
|
neg_ctx->output_token.value,
|
|
|
|
neg_ctx->output_token.length,
|
|
|
|
&encoded, &len);
|
|
|
|
if(result) {
|
2013-04-03 19:57:25 -04:00
|
|
|
gss_release_buffer(&discard_st, &neg_ctx->output_token);
|
2013-04-04 06:09:39 -04:00
|
|
|
neg_ctx->output_token.value = NULL;
|
|
|
|
neg_ctx->output_token.length = 0;
|
2014-10-26 12:34:44 -04:00
|
|
|
return result;
|
2011-08-24 02:07:36 -04:00
|
|
|
}
|
2003-09-19 08:56:22 -04:00
|
|
|
|
2013-04-03 19:57:25 -04:00
|
|
|
if(!encoded || !len) {
|
|
|
|
gss_release_buffer(&discard_st, &neg_ctx->output_token);
|
2013-04-04 06:09:39 -04:00
|
|
|
neg_ctx->output_token.value = NULL;
|
|
|
|
neg_ctx->output_token.length = 0;
|
2011-08-24 02:07:36 -04:00
|
|
|
return CURLE_REMOTE_ACCESS_DENIED;
|
|
|
|
}
|
2003-06-10 08:22:19 -04:00
|
|
|
|
2014-07-21 03:53:44 -04:00
|
|
|
userp = aprintf("%sAuthorization: Negotiate %s\r\n", proxy ? "Proxy-" : "",
|
|
|
|
encoded);
|
2013-04-03 19:57:25 -04:00
|
|
|
if(proxy) {
|
|
|
|
Curl_safefree(conn->allocptr.proxyuserpwd);
|
2010-08-16 16:19:38 -04:00
|
|
|
conn->allocptr.proxyuserpwd = userp;
|
2013-04-03 19:57:25 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
Curl_safefree(conn->allocptr.userpwd);
|
2010-08-16 16:19:38 -04:00
|
|
|
conn->allocptr.userpwd = userp;
|
2013-04-03 19:57:25 -04:00
|
|
|
}
|
|
|
|
|
2015-03-16 10:01:15 -04:00
|
|
|
free(encoded);
|
2013-04-03 19:57:25 -04:00
|
|
|
|
2010-08-16 16:19:38 -04:00
|
|
|
return (userp == NULL) ? CURLE_OUT_OF_MEMORY : CURLE_OK;
|
2003-06-10 08:22:19 -04:00
|
|
|
}
|
|
|
|
|
2007-11-20 18:17:08 -05:00
|
|
|
static void cleanup(struct negotiatedata *neg_ctx)
|
2004-06-24 07:54:11 -04:00
|
|
|
{
|
2003-06-10 08:22:19 -04:00
|
|
|
OM_uint32 minor_status;
|
2007-11-05 04:45:09 -05:00
|
|
|
if(neg_ctx->context != GSS_C_NO_CONTEXT)
|
2003-06-10 08:22:19 -04:00
|
|
|
gss_delete_sec_context(&minor_status, &neg_ctx->context, GSS_C_NO_BUFFER);
|
|
|
|
|
2013-04-03 19:57:25 -04:00
|
|
|
if(neg_ctx->output_token.value)
|
2003-06-10 08:22:19 -04:00
|
|
|
gss_release_buffer(&minor_status, &neg_ctx->output_token);
|
|
|
|
|
2007-11-05 04:45:09 -05:00
|
|
|
if(neg_ctx->server_name != GSS_C_NO_NAME)
|
2003-06-10 08:22:19 -04:00
|
|
|
gss_release_name(&minor_status, &neg_ctx->server_name);
|
2004-06-24 07:54:11 -04:00
|
|
|
|
2003-06-10 08:22:19 -04:00
|
|
|
memset(neg_ctx, 0, sizeof(*neg_ctx));
|
|
|
|
}
|
|
|
|
|
2007-11-20 18:17:08 -05:00
|
|
|
void Curl_cleanup_negotiate(struct SessionHandle *data)
|
|
|
|
{
|
|
|
|
cleanup(&data->state.negotiate);
|
|
|
|
cleanup(&data->state.proxyneg);
|
|
|
|
}
|
|
|
|
|
2015-01-09 15:29:26 -05:00
|
|
|
#endif /* HAVE_GSSAPI && !CURL_DISABLE_HTTP && USE_SPNEGO */
|