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