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
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2014-07-22 05:00:43 -04:00
|
|
|
* Copyright (C) 1998 - 2014, 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
|
|
|
|
* are also available at http://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
|
|
|
|
2003-09-19 08:56:22 -04:00
|
|
|
#ifdef HAVE_GSSAPI
|
2014-10-26 12:24:07 -04:00
|
|
|
|
|
|
|
#if !defined(CURL_DISABLE_HTTP) && defined(USE_SPNEGO)
|
|
|
|
|
2008-04-07 05:26:30 -04:00
|
|
|
#ifdef HAVE_OLD_GSSMIT
|
2003-09-19 08:56:22 -04:00
|
|
|
#define GSS_C_NT_HOSTBASED_SERVICE gss_nt_service_name
|
2011-07-28 17:03:07 -04:00
|
|
|
#define NCOMPAT 1
|
2003-09-19 08:56:22 -04:00
|
|
|
#endif
|
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"
|
2009-04-21 07:46:16 -04:00
|
|
|
#include "curl_memory.h"
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "url.h"
|
2003-06-10 08:22:19 -04:00
|
|
|
|
|
|
|
#define _MPRINTF_REPLACE /* use our functions only */
|
|
|
|
#include <curl/mprintf.h>
|
|
|
|
|
|
|
|
/* The last #include file should be: */
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "memdebug.h"
|
2003-06-10 08:22:19 -04:00
|
|
|
|
|
|
|
static int
|
2007-09-21 07:05:31 -04:00
|
|
|
get_gss_name(struct connectdata *conn, bool proxy, gss_name_t *server)
|
2003-06-10 08:22:19 -04:00
|
|
|
{
|
|
|
|
OM_uint32 major_status, minor_status;
|
|
|
|
gss_buffer_desc token = GSS_C_EMPTY_BUFFER;
|
|
|
|
char name[2048];
|
2014-07-21 03:53:44 -04:00
|
|
|
const char* service = "HTTP";
|
2003-09-11 18:21:11 -04:00
|
|
|
|
2007-11-20 18:02:58 -05:00
|
|
|
token.length = strlen(service) + 1 + strlen(proxy ? conn->proxy.name :
|
|
|
|
conn->host.name) + 1;
|
2007-11-05 04:45:09 -05:00
|
|
|
if(token.length + 1 > sizeof(name))
|
2003-06-10 08:22:19 -04:00
|
|
|
return EMSGSIZE;
|
2004-06-24 07:54:11 -04:00
|
|
|
|
2007-11-20 18:02:58 -05:00
|
|
|
snprintf(name, sizeof(name), "%s@%s", service, proxy ? conn->proxy.name :
|
|
|
|
conn->host.name);
|
2003-07-23 07:28:59 -04:00
|
|
|
|
2003-06-10 08:22:19 -04:00
|
|
|
token.value = (void *) name;
|
|
|
|
major_status = gss_import_name(&minor_status,
|
|
|
|
&token,
|
|
|
|
GSS_C_NT_HOSTBASED_SERVICE,
|
|
|
|
server);
|
2003-09-11 18:21:11 -04:00
|
|
|
|
2003-06-10 08:22:19 -04:00
|
|
|
return GSS_ERROR(major_status) ? -1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-04-20 09:17:42 -04:00
|
|
|
log_gss_error(struct connectdata *conn, OM_uint32 error_status,
|
|
|
|
const char *prefix)
|
2003-06-10 08:22:19 -04:00
|
|
|
{
|
|
|
|
OM_uint32 maj_stat, min_stat;
|
|
|
|
OM_uint32 msg_ctx = 0;
|
|
|
|
gss_buffer_desc status_string;
|
|
|
|
char buf[1024];
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
snprintf(buf, sizeof(buf), "%s", prefix);
|
|
|
|
len = strlen(buf);
|
|
|
|
do {
|
2007-11-20 18:17:08 -05:00
|
|
|
maj_stat = gss_display_status(&min_stat,
|
|
|
|
error_status,
|
|
|
|
GSS_C_MECH_CODE,
|
|
|
|
GSS_C_NO_OID,
|
|
|
|
&msg_ctx,
|
|
|
|
&status_string);
|
2007-11-05 04:45:09 -05:00
|
|
|
if(sizeof(buf) > len + status_string.length + 1) {
|
2004-06-24 07:54:11 -04:00
|
|
|
snprintf(buf + len, sizeof(buf) - len,
|
|
|
|
": %s", (char*) status_string.value);
|
2003-06-10 08:22:19 -04:00
|
|
|
len += status_string.length;
|
|
|
|
}
|
|
|
|
gss_release_buffer(&min_stat, &status_string);
|
2007-11-05 04:45:09 -05:00
|
|
|
} while(!GSS_ERROR(maj_stat) && msg_ctx != 0);
|
2003-06-10 08:22:19 -04:00
|
|
|
|
2012-01-16 15:14:05 -05:00
|
|
|
infof(conn->data, "%s\n", buf);
|
2003-06-10 08:22:19 -04:00
|
|
|
}
|
|
|
|
|
2008-04-12 07:50:51 -04:00
|
|
|
/* returning zero (0) means success, everything else is treated as "failure"
|
|
|
|
with no care exactly what the failure was */
|
2007-11-20 18:02:58 -05:00
|
|
|
int 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;
|
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;
|
|
|
|
int ret;
|
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);
|
2003-06-10 08:22:19 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-11-05 04:45:09 -05:00
|
|
|
if(neg_ctx->server_name == NULL &&
|
2007-09-21 07:05:31 -04:00
|
|
|
(ret = get_gss_name(conn, proxy, &neg_ctx->server_name)))
|
2003-06-10 08:22:19 -04:00
|
|
|
return ret;
|
|
|
|
|
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);
|
|
|
|
if(result || rawlen == 0)
|
2003-06-10 08:22:19 -04:00
|
|
|
return -1;
|
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,
|
|
|
|
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);
|
|
|
|
log_gss_error(conn, minor_status, "gss_init_sec_context() failed: ");
|
2003-06-10 08:22:19 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
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);
|
2003-06-10 08:22:19 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
neg_ctx->output_token = output_token;
|
|
|
|
return 0;
|
|
|
|
}
|
2004-06-24 07:54:11 -04:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
Curl_safefree(encoded);
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-10-26 12:24:07 -04:00
|
|
|
#endif /* !CURL_DISABLE_HTTP && USE_SPNEGO */
|
2003-06-10 08:22:19 -04:00
|
|
|
|
2014-10-26 12:24:07 -04:00
|
|
|
#endif /* HAVE_GSSAPI */
|