mirror of
https://github.com/curl/curl.git
synced 2026-07-09 23:57:15 +03:00
Before this patch curl used the C preprocessor to override standard memory allocation symbols: malloc, calloc, strdup, realloc, free. The goal of these is to replace them with curl's debug wrappers in `CURLDEBUG` builds, another was to replace them with the wrappers calling user-defined allocators in libcurl. This solution needed a bunch of workarounds to avoid breaking external headers: it relied on include order to do the overriding last. For "unity" builds it needed to reset overrides before external includes. Also in test apps, which are always built as single source files. It also needed the `(symbol)` trick to avoid overrides in some places. This would still not fix cases where the standard symbols were macros. It was also fragile and difficult to figure out which was the actual function behind an alloc or free call in a specific piece of code. This in turn caused bugs where the wrong allocator was accidentally called. To avoid these problems, this patch replaces this solution with `curlx_`-prefixed allocator macros, and mapping them _once_ to either the libcurl wrappers, the debug wrappers or the standard ones, matching the rest of the code in libtests. This concludes the long journey to avoid redefining standard functions in the curl codebase. Note: I did not update `packages/OS400/*.c` sources. They did not `#include` `curl_setup.h`, `curl_memory.h` or `memdebug.h`, meaning the overrides were never applied to them. This may or may not have been correct. For now I suppressed the direct use of standard allocators via a local `.checksrc`. Probably they (except for `curlcl.c`) should be updated to include `curl_setup.h` and use the `curlx_` macros. This patch changes mappings in two places: - `lib/curl_threads.c` in libtests: Before this patch it mapped to libcurl allocators. After, it maps to standard allocators, like the rest of libtests code. - `units`: before this patch it mapped to standard allocators. After, it maps to libcurl allocators. Also: - drop all position-dependent `curl_memory.h` and `memdebug.h` includes, and delete the now unnecessary headers. - rename `Curl_tcsdup` macro to `curlx_tcsdup` and define like the other allocators. - map `curlx_strdup()` to `_strdup()` on Windows (was: `strdup()`). To fix warnings silenced via `_CRT_NONSTDC_NO_DEPRECATE`. - multibyte: map `curlx_convert_*()` to `_strdup()` on Windows (was: `strdup()`). - src: do not reuse the `strdup` name for the local replacement. - lib509: call `_strdup()` on Windows (was: `strdup()`). - test1132: delete test obsoleted by this patch. - CHECKSRC.md: update text for `SNPRINTF`. - checksrc: ban standard allocator symbols. Follow-up tob12da22db1#18866 Follow-up todb98daab05#18844 Follow-up to4deea9396b#18814 Follow-up to9678ff5b1b#18776 Follow-up to10bac43b87#18774 Follow-up to20142f5d06#18634 Follow-up tobf7375ecc5#18503 Follow-up to9863599d69#18502 Follow-up to3bb5e58c10#17827 Closes #19626
354 lines
11 KiB
C
354 lines
11 KiB
C
/***************************************************************************
|
|
* _ _ ____ _
|
|
* Project ___| | | | _ \| |
|
|
* / __| | | | |_) | |
|
|
* | (__| |_| | _ <| |___
|
|
* \___|\___/|_| \_\_____|
|
|
*
|
|
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
|
|
*
|
|
* 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 https://curl.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.
|
|
*
|
|
* SPDX-License-Identifier: curl
|
|
*
|
|
* RFC4178 Simple and Protected GSS-API Negotiation Mechanism
|
|
*
|
|
***************************************************************************/
|
|
|
|
#include "../curl_setup.h"
|
|
|
|
#if defined(USE_WINDOWS_SSPI) && defined(USE_SPNEGO)
|
|
|
|
#include <curl/curl.h>
|
|
|
|
#include "vauth.h"
|
|
#include "../urldata.h"
|
|
#include "../curlx/base64.h"
|
|
#include "../curlx/warnless.h"
|
|
#include "../curlx/multibyte.h"
|
|
#include "../sendf.h"
|
|
#include "../strerror.h"
|
|
|
|
/*
|
|
* Curl_auth_is_spnego_supported()
|
|
*
|
|
* This is used to evaluate if SPNEGO (Negotiate) is supported.
|
|
*
|
|
* Parameters: None
|
|
*
|
|
* Returns TRUE if Negotiate is supported by Windows SSPI.
|
|
*/
|
|
bool Curl_auth_is_spnego_supported(void)
|
|
{
|
|
PSecPkgInfo SecurityPackage;
|
|
SECURITY_STATUS status;
|
|
|
|
/* Query the security package for Negotiate */
|
|
status = Curl_pSecFn->QuerySecurityPackageInfo(
|
|
(TCHAR *)CURL_UNCONST(TEXT(SP_NAME_NEGOTIATE)),
|
|
&SecurityPackage);
|
|
|
|
/* Release the package buffer as it is not required anymore */
|
|
if(status == SEC_E_OK) {
|
|
Curl_pSecFn->FreeContextBuffer(SecurityPackage);
|
|
}
|
|
|
|
return status == SEC_E_OK;
|
|
}
|
|
|
|
/*
|
|
* Curl_auth_decode_spnego_message()
|
|
*
|
|
* This is used to decode an already encoded SPNEGO (Negotiate) challenge
|
|
* message.
|
|
*
|
|
* Parameters:
|
|
*
|
|
* data [in] - The session handle.
|
|
* user [in] - The username in the format User or Domain\User.
|
|
* password [in] - The user's password.
|
|
* service [in] - The service type such as http, smtp, pop or imap.
|
|
* host [in] - The hostname.
|
|
* chlg64 [in] - The optional base64 encoded challenge message.
|
|
* nego [in/out] - The Negotiate data struct being used and modified.
|
|
*
|
|
* Returns CURLE_OK on success.
|
|
*/
|
|
CURLcode Curl_auth_decode_spnego_message(struct Curl_easy *data,
|
|
const char *user,
|
|
const char *password,
|
|
const char *service,
|
|
const char *host,
|
|
const char *chlg64,
|
|
struct negotiatedata *nego)
|
|
{
|
|
CURLcode result = CURLE_OK;
|
|
size_t chlglen = 0;
|
|
unsigned char *chlg = NULL;
|
|
PSecPkgInfo SecurityPackage;
|
|
SecBuffer chlg_buf[2];
|
|
SecBuffer resp_buf;
|
|
SecBufferDesc chlg_desc;
|
|
SecBufferDesc resp_desc;
|
|
unsigned long attrs;
|
|
|
|
#ifdef CURL_DISABLE_VERBOSE_STRINGS
|
|
(void)data;
|
|
#endif
|
|
|
|
if(nego->context && nego->status == SEC_E_OK) {
|
|
/* We finished successfully our part of authentication, but server
|
|
* rejected it (since we are again here). Exit with an error since we
|
|
* cannot invent anything better */
|
|
Curl_auth_cleanup_spnego(nego);
|
|
return CURLE_LOGIN_DENIED;
|
|
}
|
|
|
|
if(!nego->spn) {
|
|
/* Generate our SPN */
|
|
nego->spn = Curl_auth_build_spn(service, host, NULL);
|
|
if(!nego->spn)
|
|
return CURLE_OUT_OF_MEMORY;
|
|
}
|
|
|
|
if(!nego->output_token) {
|
|
/* Query the security package for Negotiate */
|
|
nego->status = Curl_pSecFn->QuerySecurityPackageInfo(
|
|
(TCHAR *)CURL_UNCONST(TEXT(SP_NAME_NEGOTIATE)),
|
|
&SecurityPackage);
|
|
if(nego->status != SEC_E_OK) {
|
|
failf(data, "SSPI: could not get auth info");
|
|
return CURLE_AUTH_ERROR;
|
|
}
|
|
|
|
nego->token_max = SecurityPackage->cbMaxToken;
|
|
|
|
/* Release the package buffer as it is not required anymore */
|
|
Curl_pSecFn->FreeContextBuffer(SecurityPackage);
|
|
|
|
/* Allocate our output buffer */
|
|
nego->output_token = curlx_malloc(nego->token_max);
|
|
if(!nego->output_token)
|
|
return CURLE_OUT_OF_MEMORY;
|
|
}
|
|
|
|
if(!nego->credentials) {
|
|
/* Do we have credentials to use or are we using single sign-on? */
|
|
if(user && *user) {
|
|
/* Populate our identity structure */
|
|
result = Curl_create_sspi_identity(user, password, &nego->identity);
|
|
if(result)
|
|
return result;
|
|
|
|
/* Allow proper cleanup of the identity structure */
|
|
nego->p_identity = &nego->identity;
|
|
}
|
|
else
|
|
/* Use the current Windows user */
|
|
nego->p_identity = NULL;
|
|
|
|
/* Allocate our credentials handle */
|
|
nego->credentials = curlx_calloc(1, sizeof(CredHandle));
|
|
if(!nego->credentials)
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
/* Acquire our credentials handle */
|
|
nego->status = Curl_pSecFn->AcquireCredentialsHandle(NULL,
|
|
(TCHAR *)CURL_UNCONST(TEXT(SP_NAME_NEGOTIATE)),
|
|
SECPKG_CRED_OUTBOUND, NULL,
|
|
nego->p_identity, NULL, NULL,
|
|
nego->credentials, NULL);
|
|
if(nego->status != SEC_E_OK)
|
|
return CURLE_AUTH_ERROR;
|
|
|
|
/* Allocate our new context handle */
|
|
nego->context = curlx_calloc(1, sizeof(CtxtHandle));
|
|
if(!nego->context)
|
|
return CURLE_OUT_OF_MEMORY;
|
|
}
|
|
|
|
if(chlg64 && *chlg64) {
|
|
/* Decode the base-64 encoded challenge message */
|
|
if(*chlg64 != '=') {
|
|
result = curlx_base64_decode(chlg64, &chlg, &chlglen);
|
|
if(result)
|
|
return result;
|
|
}
|
|
|
|
/* Ensure we have a valid challenge message */
|
|
if(!chlg) {
|
|
infof(data, "SPNEGO handshake failure (empty challenge message)");
|
|
return CURLE_BAD_CONTENT_ENCODING;
|
|
}
|
|
|
|
/* Setup the challenge "input" security buffer */
|
|
chlg_desc.ulVersion = SECBUFFER_VERSION;
|
|
chlg_desc.cBuffers = 1;
|
|
chlg_desc.pBuffers = &chlg_buf[0];
|
|
chlg_buf[0].BufferType = SECBUFFER_TOKEN;
|
|
chlg_buf[0].pvBuffer = chlg;
|
|
chlg_buf[0].cbBuffer = curlx_uztoul(chlglen);
|
|
|
|
#ifdef SECPKG_ATTR_ENDPOINT_BINDINGS
|
|
/* ssl context comes from Schannel.
|
|
* When extended protection is used in IIS server,
|
|
* we have to pass a second SecBuffer to the SecBufferDesc
|
|
* otherwise IIS will not pass the authentication (401 response).
|
|
* Minimum supported version is Windows 7.
|
|
* https://learn.microsoft.com/security-updates/SecurityAdvisories/2009/973811
|
|
*/
|
|
if(nego->sslContext) {
|
|
SEC_CHANNEL_BINDINGS channelBindings;
|
|
SecPkgContext_Bindings pkgBindings;
|
|
pkgBindings.Bindings = &channelBindings;
|
|
nego->status = Curl_pSecFn->QueryContextAttributes(
|
|
nego->sslContext,
|
|
SECPKG_ATTR_ENDPOINT_BINDINGS,
|
|
&pkgBindings);
|
|
if(nego->status == SEC_E_OK) {
|
|
chlg_desc.cBuffers++;
|
|
chlg_buf[1].BufferType = SECBUFFER_CHANNEL_BINDINGS;
|
|
chlg_buf[1].cbBuffer = pkgBindings.BindingsLength;
|
|
chlg_buf[1].pvBuffer = pkgBindings.Bindings;
|
|
}
|
|
}
|
|
#endif
|
|
}
|
|
|
|
/* Setup the response "output" security buffer */
|
|
resp_desc.ulVersion = SECBUFFER_VERSION;
|
|
resp_desc.cBuffers = 1;
|
|
resp_desc.pBuffers = &resp_buf;
|
|
resp_buf.BufferType = SECBUFFER_TOKEN;
|
|
resp_buf.pvBuffer = nego->output_token;
|
|
resp_buf.cbBuffer = curlx_uztoul(nego->token_max);
|
|
|
|
/* Generate our challenge-response message */
|
|
nego->status =
|
|
Curl_pSecFn->InitializeSecurityContext(nego->credentials,
|
|
chlg ? nego->context : NULL,
|
|
nego->spn,
|
|
ISC_REQ_CONFIDENTIALITY,
|
|
0, SECURITY_NATIVE_DREP,
|
|
chlg ? &chlg_desc : NULL,
|
|
0, nego->context,
|
|
&resp_desc, &attrs, NULL);
|
|
|
|
/* Free the decoded challenge as it is not required anymore */
|
|
curlx_free(chlg);
|
|
|
|
if(GSS_ERROR(nego->status)) {
|
|
char buffer[STRERROR_LEN];
|
|
failf(data, "InitializeSecurityContext failed: %s",
|
|
Curl_sspi_strerror(nego->status, buffer, sizeof(buffer)));
|
|
|
|
if(nego->status == SEC_E_INSUFFICIENT_MEMORY)
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
return CURLE_AUTH_ERROR;
|
|
}
|
|
|
|
if(nego->status == SEC_I_COMPLETE_NEEDED ||
|
|
nego->status == SEC_I_COMPLETE_AND_CONTINUE) {
|
|
nego->status = Curl_pSecFn->CompleteAuthToken(nego->context, &resp_desc);
|
|
if(GSS_ERROR(nego->status)) {
|
|
char buffer[STRERROR_LEN];
|
|
failf(data, "CompleteAuthToken failed: %s",
|
|
Curl_sspi_strerror(nego->status, buffer, sizeof(buffer)));
|
|
|
|
if(nego->status == SEC_E_INSUFFICIENT_MEMORY)
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
return CURLE_AUTH_ERROR;
|
|
}
|
|
}
|
|
|
|
nego->output_token_length = resp_buf.cbBuffer;
|
|
|
|
return result;
|
|
}
|
|
|
|
/*
|
|
* Curl_auth_create_spnego_message()
|
|
*
|
|
* This is used to generate an already encoded SPNEGO (Negotiate) response
|
|
* message ready for sending to the recipient.
|
|
*
|
|
* Parameters:
|
|
*
|
|
* data [in] - The session handle.
|
|
* nego [in/out] - The Negotiate data struct being used and modified.
|
|
* outptr [in/out] - The address where a pointer to newly allocated memory
|
|
* holding the result will be stored upon completion.
|
|
* outlen [out] - The length of the output message.
|
|
*
|
|
* Returns CURLE_OK on success.
|
|
*/
|
|
CURLcode Curl_auth_create_spnego_message(struct negotiatedata *nego,
|
|
char **outptr, size_t *outlen)
|
|
{
|
|
/* Base64 encode the already generated response */
|
|
CURLcode result = curlx_base64_encode(nego->output_token,
|
|
nego->output_token_length, outptr,
|
|
outlen);
|
|
if(!result && (!*outptr || !*outlen)) {
|
|
curlx_free(*outptr);
|
|
result = CURLE_REMOTE_ACCESS_DENIED;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/*
|
|
* Curl_auth_cleanup_spnego()
|
|
*
|
|
* This is used to clean up the SPNEGO (Negotiate) specific data.
|
|
*
|
|
* Parameters:
|
|
*
|
|
* nego [in/out] - The Negotiate data struct being cleaned up.
|
|
*
|
|
*/
|
|
void Curl_auth_cleanup_spnego(struct negotiatedata *nego)
|
|
{
|
|
/* Free our security context */
|
|
if(nego->context) {
|
|
Curl_pSecFn->DeleteSecurityContext(nego->context);
|
|
curlx_free(nego->context);
|
|
nego->context = NULL;
|
|
}
|
|
|
|
/* Free our credentials handle */
|
|
if(nego->credentials) {
|
|
Curl_pSecFn->FreeCredentialsHandle(nego->credentials);
|
|
curlx_free(nego->credentials);
|
|
nego->credentials = NULL;
|
|
}
|
|
|
|
/* Free our identity */
|
|
Curl_sspi_free_identity(nego->p_identity);
|
|
nego->p_identity = NULL;
|
|
|
|
/* Free the SPN and output token */
|
|
Curl_safefree(nego->spn);
|
|
Curl_safefree(nego->output_token);
|
|
|
|
/* Reset any variables */
|
|
nego->status = 0;
|
|
nego->token_max = 0;
|
|
nego->noauthpersist = FALSE;
|
|
nego->havenoauthpersist = FALSE;
|
|
nego->havenegdata = FALSE;
|
|
nego->havemultiplerequests = FALSE;
|
|
}
|
|
|
|
#endif /* USE_WINDOWS_SSPI && USE_SPNEGO */
|