2011-01-05 19:01:07 +03:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2014-04-06 04:22:47 +04:00
|
|
|
* Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2011-01-05 19:01:07 +03: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.
|
|
|
|
*
|
|
|
|
* 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 19:23:27 +04:00
|
|
|
|
2013-01-06 22:06:49 +04:00
|
|
|
#include "curl_setup.h"
|
2011-01-05 19:01:07 +03:00
|
|
|
|
|
|
|
#ifdef USE_WINDOWS_SSPI
|
|
|
|
|
2014-04-06 04:22:47 +04:00
|
|
|
#if !defined(CURL_DISABLE_HTTP) && defined(USE_HTTP_NEGOTIATE)
|
2011-01-05 19:01:07 +03:00
|
|
|
|
2013-01-04 05:50:28 +04:00
|
|
|
#include "urldata.h"
|
|
|
|
#include "sendf.h"
|
|
|
|
#include "rawstr.h"
|
|
|
|
#include "warnless.h"
|
2011-01-05 19:01:07 +03:00
|
|
|
#include "curl_base64.h"
|
2013-01-04 05:50:28 +04:00
|
|
|
#include "http_negotiate.h"
|
2011-01-05 19:01:07 +03:00
|
|
|
#include "curl_memory.h"
|
2012-06-15 20:05:11 +04:00
|
|
|
#include "curl_multibyte.h"
|
2011-01-05 19:01:07 +03:00
|
|
|
|
|
|
|
#define _MPRINTF_REPLACE /* use our functions only */
|
|
|
|
#include <curl/mprintf.h>
|
|
|
|
|
|
|
|
/* The last #include file should be: */
|
2013-01-04 05:50:28 +04:00
|
|
|
#include "memdebug.h"
|
2011-01-05 19:01:07 +03:00
|
|
|
|
|
|
|
static int
|
2011-06-29 14:26:23 +04:00
|
|
|
get_gss_name(struct connectdata *conn, bool proxy,
|
|
|
|
struct negotiatedata *neg_ctx)
|
2011-01-05 19:01:07 +03:00
|
|
|
{
|
|
|
|
const char* service;
|
|
|
|
size_t length;
|
|
|
|
|
2011-06-29 14:26:23 +04:00
|
|
|
if(proxy && !conn->proxy.name)
|
|
|
|
/* proxy auth requested but no given proxy name, error out! */
|
|
|
|
return -1;
|
|
|
|
|
2011-01-05 19:01:07 +03:00
|
|
|
/* GSSAPI implementation by Globus (known as GSI) requires the name to be
|
|
|
|
of form "<service>/<fqdn>" instead of <service>@<fqdn> (ie. slash instead
|
|
|
|
of at-sign). Also GSI servers are often identified as 'host' not 'khttp'.
|
|
|
|
Change following lines if you want to use GSI */
|
|
|
|
|
|
|
|
/* IIS uses the <service>@<fqdn> form but uses 'http' as the service name,
|
|
|
|
and SSPI then generates an NTLM token. When using <service>/<fqdn> a
|
|
|
|
Kerberos token is generated. */
|
|
|
|
|
|
|
|
if(neg_ctx->gss)
|
|
|
|
service = "KHTTP";
|
|
|
|
else
|
|
|
|
service = "HTTP";
|
|
|
|
|
|
|
|
length = strlen(service) + 1 + strlen(proxy ? conn->proxy.name :
|
|
|
|
conn->host.name) + 1;
|
|
|
|
if(length + 1 > sizeof(neg_ctx->server_name))
|
|
|
|
return EMSGSIZE;
|
|
|
|
|
2011-06-29 14:26:23 +04:00
|
|
|
snprintf(neg_ctx->server_name, sizeof(neg_ctx->server_name), "%s/%s",
|
2011-01-05 19:01:07 +03:00
|
|
|
service, proxy ? conn->proxy.name : conn->host.name);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* returning zero (0) means success, everything else is treated as "failure"
|
|
|
|
with no care exactly what the failure was */
|
|
|
|
int Curl_input_negotiate(struct connectdata *conn, bool proxy,
|
|
|
|
const char *header)
|
|
|
|
{
|
|
|
|
struct negotiatedata *neg_ctx = proxy?&conn->data->state.proxyneg:
|
|
|
|
&conn->data->state.negotiate;
|
2011-08-24 10:07:36 +04:00
|
|
|
BYTE *input_token = 0;
|
2011-01-05 19:01:07 +03:00
|
|
|
SecBufferDesc out_buff_desc;
|
|
|
|
SecBuffer out_sec_buff;
|
|
|
|
SecBufferDesc in_buff_desc;
|
|
|
|
SecBuffer in_sec_buff;
|
2012-06-16 21:20:50 +04:00
|
|
|
unsigned long context_attributes;
|
2011-01-05 19:01:07 +03:00
|
|
|
TimeStamp lifetime;
|
2012-06-16 21:20:50 +04:00
|
|
|
TCHAR *sname;
|
2011-01-05 19:01:07 +03:00
|
|
|
int ret;
|
|
|
|
size_t len = 0, input_token_len = 0;
|
|
|
|
bool gss = FALSE;
|
|
|
|
const char* protocol;
|
2011-08-24 10:07:36 +04:00
|
|
|
CURLcode error;
|
2011-01-05 19:01:07 +03:00
|
|
|
|
|
|
|
if(checkprefix("GSS-Negotiate", header)) {
|
|
|
|
protocol = "GSS-Negotiate";
|
|
|
|
gss = TRUE;
|
|
|
|
}
|
|
|
|
else if(checkprefix("Negotiate", header)) {
|
|
|
|
protocol = "Negotiate";
|
|
|
|
gss = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if(neg_ctx->context) {
|
|
|
|
if(neg_ctx->gss != gss) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
neg_ctx->protocol = protocol;
|
|
|
|
neg_ctx->gss = gss;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(neg_ctx->context && neg_ctx->status == SEC_E_OK) {
|
2011-04-19 17:54:13 +04:00
|
|
|
/* We finished successfully our part of authentication, but server
|
2011-01-05 19:01:07 +03:00
|
|
|
* rejected it (since we're again here). Exit with an error since we
|
|
|
|
* can't invent anything better */
|
|
|
|
Curl_cleanup_negotiate(conn->data);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-08-14 02:05:05 +04:00
|
|
|
if(0 == strlen(neg_ctx->server_name)) {
|
|
|
|
ret = get_gss_name(conn, proxy, neg_ctx);
|
|
|
|
if(ret)
|
|
|
|
return ret;
|
|
|
|
}
|
2011-01-05 19:01:07 +03:00
|
|
|
|
2011-05-16 13:19:03 +04:00
|
|
|
if(!neg_ctx->output_token) {
|
2011-01-05 19:01:07 +03:00
|
|
|
PSecPkgInfo SecurityPackage;
|
2012-06-16 21:20:50 +04:00
|
|
|
ret = s_pSecFn->QuerySecurityPackageInfo((TCHAR *) TEXT("Negotiate"),
|
2011-01-14 14:39:54 +03:00
|
|
|
&SecurityPackage);
|
2011-04-20 17:17:42 +04:00
|
|
|
if(ret != SEC_E_OK)
|
2011-01-05 19:01:07 +03:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Allocate input and output buffers according to the max token size
|
|
|
|
as indicated by the security package */
|
|
|
|
neg_ctx->max_token_length = SecurityPackage->cbMaxToken;
|
2011-06-02 21:42:24 +04:00
|
|
|
neg_ctx->output_token = malloc(neg_ctx->max_token_length);
|
2011-01-05 19:01:07 +03:00
|
|
|
s_pSecFn->FreeContextBuffer(SecurityPackage);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Obtain the input token, if any */
|
|
|
|
header += strlen(neg_ctx->protocol);
|
|
|
|
while(*header && ISSPACE(*header))
|
|
|
|
header++;
|
|
|
|
|
|
|
|
len = strlen(header);
|
2011-05-16 13:19:03 +04:00
|
|
|
if(!len) {
|
|
|
|
/* first call in a new negotation, we have to acquire credentials,
|
2011-01-05 19:01:07 +03:00
|
|
|
and allocate memory for the context */
|
|
|
|
|
2011-06-02 21:42:24 +04:00
|
|
|
neg_ctx->credentials = malloc(sizeof(CredHandle));
|
|
|
|
neg_ctx->context = malloc(sizeof(CtxtHandle));
|
2011-01-05 19:01:07 +03:00
|
|
|
|
2011-04-23 01:01:30 +04:00
|
|
|
if(!neg_ctx->credentials || !neg_ctx->context)
|
2011-01-05 19:01:07 +03:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
neg_ctx->status =
|
2012-06-15 23:50:57 +04:00
|
|
|
s_pSecFn->AcquireCredentialsHandle(NULL,
|
2012-06-16 21:20:50 +04:00
|
|
|
(TCHAR *) TEXT("Negotiate"),
|
2011-01-05 19:01:07 +03:00
|
|
|
SECPKG_CRED_OUTBOUND, NULL, NULL,
|
|
|
|
NULL, NULL, neg_ctx->credentials,
|
|
|
|
&lifetime);
|
2011-04-23 01:01:30 +04:00
|
|
|
if(neg_ctx->status != SEC_E_OK)
|
2011-01-05 19:01:07 +03:00
|
|
|
return -1;
|
|
|
|
}
|
2011-05-16 13:19:03 +04:00
|
|
|
else {
|
|
|
|
input_token = malloc(neg_ctx->max_token_length);
|
|
|
|
if(!input_token)
|
|
|
|
return -1;
|
|
|
|
|
2011-08-24 10:07:36 +04:00
|
|
|
error = Curl_base64_decode(header,
|
|
|
|
(unsigned char **)&input_token,
|
|
|
|
&input_token_len);
|
|
|
|
if(error || input_token_len == 0)
|
2011-05-16 13:19:03 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2011-01-05 19:01:07 +03:00
|
|
|
|
|
|
|
/* prepare the output buffers, and input buffers if present */
|
|
|
|
out_buff_desc.ulVersion = 0;
|
|
|
|
out_buff_desc.cBuffers = 1;
|
|
|
|
out_buff_desc.pBuffers = &out_sec_buff;
|
|
|
|
|
2012-01-13 16:34:43 +04:00
|
|
|
out_sec_buff.cbBuffer = curlx_uztoul(neg_ctx->max_token_length);
|
2011-01-05 19:01:07 +03:00
|
|
|
out_sec_buff.BufferType = SECBUFFER_TOKEN;
|
|
|
|
out_sec_buff.pvBuffer = neg_ctx->output_token;
|
|
|
|
|
|
|
|
|
2011-04-20 17:17:42 +04:00
|
|
|
if(input_token) {
|
2011-01-05 19:01:07 +03:00
|
|
|
in_buff_desc.ulVersion = 0;
|
|
|
|
in_buff_desc.cBuffers = 1;
|
2012-01-02 01:36:32 +04:00
|
|
|
in_buff_desc.pBuffers = &in_sec_buff;
|
2011-01-05 19:01:07 +03:00
|
|
|
|
2012-01-13 16:34:43 +04:00
|
|
|
in_sec_buff.cbBuffer = curlx_uztoul(input_token_len);
|
2011-01-05 19:01:07 +03:00
|
|
|
in_sec_buff.BufferType = SECBUFFER_TOKEN;
|
|
|
|
in_sec_buff.pvBuffer = input_token;
|
|
|
|
}
|
|
|
|
|
2012-07-06 00:16:15 +04:00
|
|
|
sname = Curl_convert_UTF8_to_tchar(neg_ctx->server_name);
|
2012-06-15 21:39:18 +04:00
|
|
|
if(!sname)
|
2012-06-15 20:05:11 +04:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
2011-01-05 19:01:07 +03:00
|
|
|
neg_ctx->status = s_pSecFn->InitializeSecurityContext(
|
|
|
|
neg_ctx->credentials,
|
|
|
|
input_token ? neg_ctx->context : 0,
|
2012-06-15 20:05:11 +04:00
|
|
|
sname,
|
2011-01-05 19:01:07 +03:00
|
|
|
ISC_REQ_CONFIDENTIALITY,
|
|
|
|
0,
|
|
|
|
SECURITY_NATIVE_DREP,
|
|
|
|
input_token ? &in_buff_desc : 0,
|
|
|
|
0,
|
|
|
|
neg_ctx->context,
|
|
|
|
&out_buff_desc,
|
|
|
|
&context_attributes,
|
|
|
|
&lifetime);
|
|
|
|
|
2012-07-06 00:16:15 +04:00
|
|
|
Curl_unicodefree(sname);
|
2012-06-15 20:05:11 +04:00
|
|
|
|
2011-04-23 01:01:30 +04:00
|
|
|
if(GSS_ERROR(neg_ctx->status))
|
2011-01-05 19:01:07 +03:00
|
|
|
return -1;
|
|
|
|
|
2011-04-23 01:01:30 +04:00
|
|
|
if(neg_ctx->status == SEC_I_COMPLETE_NEEDED ||
|
|
|
|
neg_ctx->status == SEC_I_COMPLETE_AND_CONTINUE) {
|
2011-01-05 19:01:07 +03:00
|
|
|
neg_ctx->status = s_pSecFn->CompleteAuthToken(neg_ctx->context,
|
|
|
|
&out_buff_desc);
|
2011-04-23 01:01:30 +04:00
|
|
|
if(GSS_ERROR(neg_ctx->status))
|
2011-01-05 19:01:07 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
neg_ctx->output_token_length = out_sec_buff.cbBuffer;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CURLcode Curl_output_negotiate(struct connectdata *conn, bool proxy)
|
|
|
|
{
|
|
|
|
struct negotiatedata *neg_ctx = proxy?&conn->data->state.proxyneg:
|
|
|
|
&conn->data->state.negotiate;
|
|
|
|
char *encoded = NULL;
|
2011-08-24 10:07:36 +04:00
|
|
|
size_t len = 0;
|
2011-01-05 19:01:07 +03:00
|
|
|
char *userp;
|
2011-08-24 10:07:36 +04:00
|
|
|
CURLcode error;
|
2011-01-05 19:01:07 +03:00
|
|
|
|
2011-08-24 10:07:36 +04:00
|
|
|
error = Curl_base64_encode(conn->data,
|
|
|
|
(const char*)neg_ctx->output_token,
|
|
|
|
neg_ctx->output_token_length,
|
|
|
|
&encoded, &len);
|
|
|
|
if(error)
|
|
|
|
return error;
|
2011-01-05 19:01:07 +03:00
|
|
|
|
|
|
|
if(len == 0)
|
2011-08-24 10:07:36 +04:00
|
|
|
return CURLE_REMOTE_ACCESS_DENIED;
|
2011-01-05 19:01:07 +03:00
|
|
|
|
|
|
|
userp = aprintf("%sAuthorization: %s %s\r\n", proxy ? "Proxy-" : "",
|
|
|
|
neg_ctx->protocol, encoded);
|
|
|
|
|
|
|
|
if(proxy)
|
|
|
|
conn->allocptr.proxyuserpwd = userp;
|
|
|
|
else
|
|
|
|
conn->allocptr.userpwd = userp;
|
|
|
|
free(encoded);
|
|
|
|
return (userp == NULL) ? CURLE_OUT_OF_MEMORY : CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cleanup(struct negotiatedata *neg_ctx)
|
|
|
|
{
|
|
|
|
if(neg_ctx->context) {
|
|
|
|
s_pSecFn->DeleteSecurityContext(neg_ctx->context);
|
|
|
|
free(neg_ctx->context);
|
|
|
|
neg_ctx->context = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(neg_ctx->credentials) {
|
|
|
|
s_pSecFn->FreeCredentialsHandle(neg_ctx->credentials);
|
|
|
|
free(neg_ctx->credentials);
|
|
|
|
neg_ctx->credentials = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(neg_ctx->output_token) {
|
|
|
|
free(neg_ctx->output_token);
|
|
|
|
neg_ctx->output_token = 0;
|
|
|
|
}
|
2011-05-16 13:19:03 +04:00
|
|
|
|
|
|
|
neg_ctx->max_token_length = 0;
|
2011-01-05 19:01:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Curl_cleanup_negotiate(struct SessionHandle *data)
|
|
|
|
{
|
|
|
|
cleanup(&data->state.negotiate);
|
|
|
|
cleanup(&data->state.proxyneg);
|
|
|
|
}
|
|
|
|
|
2014-04-06 04:22:47 +04:00
|
|
|
#endif /* !CURL_DISABLE_HTTP && USE_HTTP_NEGOTIATE */
|
2011-01-05 19:01:07 +03:00
|
|
|
|
2014-04-06 04:22:47 +04:00
|
|
|
#endif /* USE_WINDOWS_SSPI */
|