2003-06-10 20:22:19 +08:00
|
|
|
/***************************************************************************
|
2004-06-24 19:54:11 +08:00
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
2003-06-10 20:22:19 +08:00
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2014-07-22 17:00:43 +08:00
|
|
|
* Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2003-06-10 20:22:19 +08: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 19:54:11 +08:00
|
|
|
*
|
2003-06-10 20:22:19 +08: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 23:23:27 +08:00
|
|
|
|
2013-01-07 02:06:49 +08:00
|
|
|
#include "curl_setup.h"
|
2003-06-10 20:22:19 +08:00
|
|
|
|
2003-09-19 20:56:22 +08:00
|
|
|
#ifdef HAVE_GSSAPI
|
2014-10-27 00:24:07 +08:00
|
|
|
|
|
|
|
#if !defined(CURL_DISABLE_HTTP) && defined(USE_SPNEGO)
|
|
|
|
|
2008-04-07 17:26:30 +08:00
|
|
|
#ifdef HAVE_OLD_GSSMIT
|
2003-09-19 20:56:22 +08:00
|
|
|
#define GSS_C_NT_HOSTBASED_SERVICE gss_nt_service_name
|
2011-07-29 05:03:07 +08:00
|
|
|
#define NCOMPAT 1
|
2003-09-19 20:56:22 +08:00
|
|
|
#endif
|
2003-06-10 20:22:19 +08:00
|
|
|
|
2013-01-04 09:50:28 +08:00
|
|
|
#include "urldata.h"
|
|
|
|
#include "sendf.h"
|
2011-07-14 04:54:54 +08:00
|
|
|
#include "curl_gssapi.h"
|
2013-01-04 09:50:28 +08:00
|
|
|
#include "rawstr.h"
|
2008-08-17 08:25:38 +08:00
|
|
|
#include "curl_base64.h"
|
2013-01-04 09:50:28 +08:00
|
|
|
#include "http_negotiate.h"
|
2009-04-21 19:46:16 +08:00
|
|
|
#include "curl_memory.h"
|
2013-01-04 09:50:28 +08:00
|
|
|
#include "url.h"
|
2003-06-10 20:22:19 +08:00
|
|
|
|
|
|
|
#define _MPRINTF_REPLACE /* use our functions only */
|
|
|
|
#include <curl/mprintf.h>
|
|
|
|
|
|
|
|
/* The last #include file should be: */
|
2013-01-04 09:50:28 +08:00
|
|
|
#include "memdebug.h"
|
2003-06-10 20:22:19 +08:00
|
|
|
|
|
|
|
static int
|
2007-09-21 19:05:31 +08:00
|
|
|
get_gss_name(struct connectdata *conn, bool proxy, gss_name_t *server)
|
2003-06-10 20:22:19 +08:00
|
|
|
{
|
|
|
|
OM_uint32 major_status, minor_status;
|
|
|
|
gss_buffer_desc token = GSS_C_EMPTY_BUFFER;
|
|
|
|
char name[2048];
|
2014-07-21 15:53:44 +08:00
|
|
|
const char* service = "HTTP";
|
2003-09-12 06:21:11 +08:00
|
|
|
|
2007-11-21 07:02:58 +08:00
|
|
|
token.length = strlen(service) + 1 + strlen(proxy ? conn->proxy.name :
|
|
|
|
conn->host.name) + 1;
|
2007-11-05 17:45:09 +08:00
|
|
|
if(token.length + 1 > sizeof(name))
|
2003-06-10 20:22:19 +08:00
|
|
|
return EMSGSIZE;
|
2004-06-24 19:54:11 +08:00
|
|
|
|
2007-11-21 07:02:58 +08:00
|
|
|
snprintf(name, sizeof(name), "%s@%s", service, proxy ? conn->proxy.name :
|
|
|
|
conn->host.name);
|
2003-07-23 19:28:59 +08:00
|
|
|
|
2003-06-10 20:22:19 +08:00
|
|
|
token.value = (void *) name;
|
|
|
|
major_status = gss_import_name(&minor_status,
|
|
|
|
&token,
|
|
|
|
GSS_C_NT_HOSTBASED_SERVICE,
|
|
|
|
server);
|
2003-09-12 06:21:11 +08:00
|
|
|
|
2003-06-10 20:22:19 +08:00
|
|
|
return GSS_ERROR(major_status) ? -1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-04-20 21:17:42 +08:00
|
|
|
log_gss_error(struct connectdata *conn, OM_uint32 error_status,
|
|
|
|
const char *prefix)
|
2003-06-10 20:22:19 +08: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-21 07:17:08 +08: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 17:45:09 +08:00
|
|
|
if(sizeof(buf) > len + status_string.length + 1) {
|
2004-06-24 19:54:11 +08:00
|
|
|
snprintf(buf + len, sizeof(buf) - len,
|
|
|
|
": %s", (char*) status_string.value);
|
2003-06-10 20:22:19 +08:00
|
|
|
len += status_string.length;
|
|
|
|
}
|
|
|
|
gss_release_buffer(&min_stat, &status_string);
|
2007-11-05 17:45:09 +08:00
|
|
|
} while(!GSS_ERROR(maj_stat) && msg_ctx != 0);
|
2003-06-10 20:22:19 +08:00
|
|
|
|
2012-01-17 04:14:05 +08:00
|
|
|
infof(conn->data, "%s\n", buf);
|
2003-06-10 20:22:19 +08:00
|
|
|
}
|
|
|
|
|
2008-04-12 19:50:51 +08:00
|
|
|
/* returning zero (0) means success, everything else is treated as "failure"
|
|
|
|
with no care exactly what the failure was */
|
2007-11-21 07:02:58 +08:00
|
|
|
int Curl_input_negotiate(struct connectdata *conn, bool proxy,
|
|
|
|
const char *header)
|
2004-06-24 19:54:11 +08:00
|
|
|
{
|
2011-07-20 01:10:43 +08:00
|
|
|
struct SessionHandle *data = conn->data;
|
|
|
|
struct negotiatedata *neg_ctx = proxy?&data->state.proxyneg:
|
|
|
|
&data->state.negotiate;
|
2013-04-04 07:57:25 +08:00
|
|
|
OM_uint32 major_status, minor_status, discard_st;
|
2003-06-10 20:22:19 +08:00
|
|
|
gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
|
|
|
|
gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
|
|
|
|
int ret;
|
2011-08-24 14:07:36 +08:00
|
|
|
size_t len;
|
|
|
|
size_t rawlen = 0;
|
2014-10-27 00:34:44 +08:00
|
|
|
CURLcode result;
|
2003-06-10 20:22:19 +08:00
|
|
|
|
2007-11-05 17:45:09 +08:00
|
|
|
if(neg_ctx->context && neg_ctx->status == GSS_S_COMPLETE) {
|
2011-04-19 21:54:13 +08:00
|
|
|
/* We finished successfully our part of authentication, but server
|
2003-06-10 20:22:19 +08:00
|
|
|
* rejected it (since we're again here). Exit with an error since we
|
|
|
|
* can't invent anything better */
|
2011-07-20 01:10:43 +08:00
|
|
|
Curl_cleanup_negotiate(data);
|
2003-06-10 20:22:19 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-11-05 17:45:09 +08:00
|
|
|
if(neg_ctx->server_name == NULL &&
|
2007-09-21 19:05:31 +08:00
|
|
|
(ret = get_gss_name(conn, proxy, &neg_ctx->server_name)))
|
2003-06-10 20:22:19 +08:00
|
|
|
return ret;
|
|
|
|
|
2014-07-21 15:53:44 +08:00
|
|
|
header += strlen("Negotiate");
|
2006-10-18 05:32:56 +08:00
|
|
|
while(*header && ISSPACE(*header))
|
2003-06-10 20:22:19 +08:00
|
|
|
header++;
|
|
|
|
|
|
|
|
len = strlen(header);
|
2007-11-05 17:45:09 +08:00
|
|
|
if(len > 0) {
|
2014-10-27 00:34:44 +08:00
|
|
|
result = Curl_base64_decode(header, (unsigned char **)&input_token.value,
|
|
|
|
&rawlen);
|
|
|
|
if(result || rawlen == 0)
|
2003-06-10 20:22:19 +08:00
|
|
|
return -1;
|
2003-06-26 19:25:23 +08:00
|
|
|
input_token.length = rawlen;
|
2003-09-19 20:56:22 +08:00
|
|
|
|
2013-04-04 07:57:25 +08:00
|
|
|
DEBUGASSERT(input_token.value != NULL);
|
2003-06-10 20:22:19 +08:00
|
|
|
}
|
|
|
|
|
2011-07-20 01:10:43 +08:00
|
|
|
major_status = Curl_gss_init_sec_context(data,
|
|
|
|
&minor_status,
|
2011-06-10 23:16:06 +08:00
|
|
|
&neg_ctx->context,
|
|
|
|
neg_ctx->server_name,
|
2014-07-24 00:56:19 +08:00
|
|
|
&Curl_spnego_mech_oid,
|
2011-06-10 23:16:06 +08:00
|
|
|
GSS_C_NO_CHANNEL_BINDINGS,
|
|
|
|
&input_token,
|
|
|
|
&output_token,
|
|
|
|
NULL);
|
2013-04-04 07:57:25 +08:00
|
|
|
Curl_safefree(input_token.value);
|
|
|
|
|
2003-06-10 20:22:19 +08:00
|
|
|
neg_ctx->status = major_status;
|
2007-11-05 17:45:09 +08:00
|
|
|
if(GSS_ERROR(major_status)) {
|
2013-04-04 07:57:25 +08: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 20:22:19 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-04-04 07:57:25 +08:00
|
|
|
if(!output_token.value || !output_token.length) {
|
|
|
|
if(output_token.value)
|
|
|
|
gss_release_buffer(&discard_st, &output_token);
|
2003-06-10 20:22:19 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
neg_ctx->output_token = output_token;
|
|
|
|
return 0;
|
|
|
|
}
|
2004-06-24 19:54:11 +08:00
|
|
|
|
2003-06-10 20:22:19 +08:00
|
|
|
|
2007-09-21 19:05:31 +08:00
|
|
|
CURLcode Curl_output_negotiate(struct connectdata *conn, bool proxy)
|
2004-06-24 19:54:11 +08:00
|
|
|
{
|
2007-11-21 07:17:08 +08:00
|
|
|
struct negotiatedata *neg_ctx = proxy?&conn->data->state.proxyneg:
|
|
|
|
&conn->data->state.negotiate;
|
2003-06-10 20:22:19 +08:00
|
|
|
char *encoded = NULL;
|
2011-08-24 14:07:36 +08:00
|
|
|
size_t len = 0;
|
2010-08-17 04:19:38 +08:00
|
|
|
char *userp;
|
2014-10-27 00:34:44 +08:00
|
|
|
CURLcode result;
|
2013-04-04 07:57:25 +08:00
|
|
|
OM_uint32 discard_st;
|
2003-09-19 20:56:22 +08:00
|
|
|
|
2014-10-27 00:34:44 +08:00
|
|
|
result = Curl_base64_encode(conn->data,
|
|
|
|
neg_ctx->output_token.value,
|
|
|
|
neg_ctx->output_token.length,
|
|
|
|
&encoded, &len);
|
|
|
|
if(result) {
|
2013-04-04 07:57:25 +08:00
|
|
|
gss_release_buffer(&discard_st, &neg_ctx->output_token);
|
2013-04-04 18:09:39 +08:00
|
|
|
neg_ctx->output_token.value = NULL;
|
|
|
|
neg_ctx->output_token.length = 0;
|
2014-10-27 00:34:44 +08:00
|
|
|
return result;
|
2011-08-24 14:07:36 +08:00
|
|
|
}
|
2003-09-19 20:56:22 +08:00
|
|
|
|
2013-04-04 07:57:25 +08:00
|
|
|
if(!encoded || !len) {
|
|
|
|
gss_release_buffer(&discard_st, &neg_ctx->output_token);
|
2013-04-04 18:09:39 +08:00
|
|
|
neg_ctx->output_token.value = NULL;
|
|
|
|
neg_ctx->output_token.length = 0;
|
2011-08-24 14:07:36 +08:00
|
|
|
return CURLE_REMOTE_ACCESS_DENIED;
|
|
|
|
}
|
2003-06-10 20:22:19 +08:00
|
|
|
|
2014-07-21 15:53:44 +08:00
|
|
|
userp = aprintf("%sAuthorization: Negotiate %s\r\n", proxy ? "Proxy-" : "",
|
|
|
|
encoded);
|
2013-04-04 07:57:25 +08:00
|
|
|
if(proxy) {
|
|
|
|
Curl_safefree(conn->allocptr.proxyuserpwd);
|
2010-08-17 04:19:38 +08:00
|
|
|
conn->allocptr.proxyuserpwd = userp;
|
2013-04-04 07:57:25 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
Curl_safefree(conn->allocptr.userpwd);
|
2010-08-17 04:19:38 +08:00
|
|
|
conn->allocptr.userpwd = userp;
|
2013-04-04 07:57:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Curl_safefree(encoded);
|
|
|
|
|
2010-08-17 04:19:38 +08:00
|
|
|
return (userp == NULL) ? CURLE_OUT_OF_MEMORY : CURLE_OK;
|
2003-06-10 20:22:19 +08:00
|
|
|
}
|
|
|
|
|
2007-11-21 07:17:08 +08:00
|
|
|
static void cleanup(struct negotiatedata *neg_ctx)
|
2004-06-24 19:54:11 +08:00
|
|
|
{
|
2003-06-10 20:22:19 +08:00
|
|
|
OM_uint32 minor_status;
|
2007-11-05 17:45:09 +08:00
|
|
|
if(neg_ctx->context != GSS_C_NO_CONTEXT)
|
2003-06-10 20:22:19 +08:00
|
|
|
gss_delete_sec_context(&minor_status, &neg_ctx->context, GSS_C_NO_BUFFER);
|
|
|
|
|
2013-04-04 07:57:25 +08:00
|
|
|
if(neg_ctx->output_token.value)
|
2003-06-10 20:22:19 +08:00
|
|
|
gss_release_buffer(&minor_status, &neg_ctx->output_token);
|
|
|
|
|
2007-11-05 17:45:09 +08:00
|
|
|
if(neg_ctx->server_name != GSS_C_NO_NAME)
|
2003-06-10 20:22:19 +08:00
|
|
|
gss_release_name(&minor_status, &neg_ctx->server_name);
|
2004-06-24 19:54:11 +08:00
|
|
|
|
2003-06-10 20:22:19 +08:00
|
|
|
memset(neg_ctx, 0, sizeof(*neg_ctx));
|
|
|
|
}
|
|
|
|
|
2007-11-21 07:17:08 +08:00
|
|
|
void Curl_cleanup_negotiate(struct SessionHandle *data)
|
|
|
|
{
|
|
|
|
cleanup(&data->state.negotiate);
|
|
|
|
cleanup(&data->state.proxyneg);
|
|
|
|
}
|
|
|
|
|
2014-10-27 00:24:07 +08:00
|
|
|
#endif /* !CURL_DISABLE_HTTP && USE_SPNEGO */
|
2003-06-10 20:22:19 +08:00
|
|
|
|
2014-10-27 00:24:07 +08:00
|
|
|
#endif /* HAVE_GSSAPI */
|