vauth: move auth structs to conn meta data

Remove structs for negotiate, krb5, ntlm and gsasl from connectdata and
store them as connection meta data with auto cleanup.

De-complexify sasl mech selection by moving code into static functions.

Closes #17557
This commit is contained in:
Stefan Eissing 2025-06-09 15:09:28 +02:00 committed by Daniel Stenberg
parent 05ffeeda0c
commit ab650379a8
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
16 changed files with 598 additions and 396 deletions

View file

@ -31,6 +31,7 @@
#include "../strcase.h"
#include "../curlx/multibyte.h"
#include "../curl_printf.h"
#include "../url.h"
/* The last #include files should be: */
#include "../curl_memory.h"
@ -160,3 +161,117 @@ bool Curl_auth_allowed_to_host(struct Curl_easy *data)
(data->state.first_remote_port == conn->remote_port) &&
(data->state.first_remote_protocol == conn->handler->protocol));
}
#ifdef USE_NTLM
static void ntlm_conn_dtor(void *key, size_t klen, void *entry)
{
struct ntlmdata *ntlm = entry;
(void)key;
(void)klen;
DEBUGASSERT(ntlm);
Curl_auth_cleanup_ntlm(ntlm);
free(ntlm);
}
struct ntlmdata *Curl_auth_ntlm_get(struct connectdata *conn, bool proxy)
{
const char *key = proxy ? CURL_META_NTLM_PROXY_CONN :
CURL_META_NTLM_CONN;
struct ntlmdata *ntlm = Curl_conn_meta_get(conn, key);
if(!ntlm) {
ntlm = calloc(1, sizeof(*ntlm));
if(!ntlm ||
Curl_conn_meta_set(conn, key, ntlm, ntlm_conn_dtor))
return NULL;
}
return ntlm;
}
void Curl_auth_ntlm_remove(struct connectdata *conn, bool proxy)
{
Curl_conn_meta_remove(conn, proxy ?
CURL_META_NTLM_PROXY_CONN : CURL_META_NTLM_CONN);
}
#endif /* USE_NTLM */
#ifdef USE_KERBEROS5
static void krb5_conn_dtor(void *key, size_t klen, void *entry)
{
struct kerberos5data *krb5 = entry;
(void)key;
(void)klen;
DEBUGASSERT(krb5);
Curl_auth_cleanup_gssapi(krb5);
free(krb5);
}
struct kerberos5data *Curl_auth_krb5_get(struct connectdata *conn)
{
struct kerberos5data *krb5 = Curl_conn_meta_get(conn, CURL_META_KRB5_CONN);
if(!krb5) {
krb5 = calloc(1, sizeof(*krb5));
if(!krb5 ||
Curl_conn_meta_set(conn, CURL_META_KRB5_CONN, krb5, krb5_conn_dtor))
return NULL;
}
return krb5;
}
#endif /* USE_KERBEROS5 */
#ifdef USE_GSASL
static void gsasl_conn_dtor(void *key, size_t klen, void *entry)
{
struct gsasldata *gsasl = entry;
(void)key;
(void)klen;
DEBUGASSERT(gsasl);
Curl_auth_gsasl_cleanup(gsasl);
free(gsasl);
}
struct gsasldata *Curl_auth_gsasl_get(struct connectdata *conn)
{
struct gsasldata *gsasl = Curl_conn_meta_get(conn, CURL_META_GSASL_CONN);
if(!gsasl) {
gsasl = calloc(1, sizeof(*gsasl));
if(!gsasl ||
Curl_conn_meta_set(conn, CURL_META_GSASL_CONN, gsasl, gsasl_conn_dtor))
return NULL;
}
return gsasl;
}
#endif /* USE_GSASL */
#ifdef USE_SPNEGO
static void nego_conn_dtor(void *key, size_t klen, void *entry)
{
struct negotiatedata *nego = entry;
(void)key;
(void)klen;
DEBUGASSERT(nego);
Curl_auth_cleanup_spnego(nego);
free(nego);
}
struct negotiatedata *Curl_auth_nego_get(struct connectdata *conn, bool proxy)
{
const char *key = proxy ? CURL_META_NEGO_PROXY_CONN :
CURL_META_NEGO_CONN;
struct negotiatedata *nego = Curl_conn_meta_get(conn, key);
if(!nego) {
nego = calloc(1, sizeof(*nego));
if(!nego ||
Curl_conn_meta_set(conn, key, nego, nego_conn_dtor))
return NULL;
}
return nego;
}
#endif /* USE_SPNEGO */

View file

@ -27,8 +27,10 @@
#include <curl/curl.h>
#include "../bufref.h"
#include "../curlx/dynbuf.h"
struct Curl_easy;
struct connectdata;
#if !defined(CURL_DISABLE_DIGEST_AUTH)
struct digestdata;
@ -38,10 +40,6 @@ struct digestdata;
struct ntlmdata;
#endif
#if defined(USE_KERBEROS5)
struct kerberos5data;
#endif
#if (defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)) && defined(USE_SPNEGO)
struct negotiatedata;
#endif
@ -50,7 +48,8 @@ struct negotiatedata;
struct gsasldata;
#endif
#if defined(USE_WINDOWS_SSPI)
#ifdef USE_WINDOWS_SSPI
#include "../curl_sspi.h"
#define GSS_ERROR(status) ((status) & 0x80000000)
#endif
@ -120,6 +119,18 @@ void Curl_auth_digest_cleanup(struct digestdata *digest);
#endif /* !CURL_DISABLE_DIGEST_AUTH */
#ifdef USE_GSASL
/* meta key for storing GSASL meta at connection */
#define CURL_META_GSASL_CONN "meta:auth:gsasl:conn"
#include <gsasl.h>
struct gsasldata {
Gsasl *ctx;
Gsasl_session *client;
};
struct gsasldata *Curl_auth_gsasl_get(struct connectdata *conn);
/* This is used to evaluate if MECH is supported by gsasl */
bool Curl_auth_gsasl_is_supported(struct Curl_easy *data,
const char *mech,
@ -141,9 +152,46 @@ void Curl_auth_gsasl_cleanup(struct gsasldata *digest);
#endif
#if defined(USE_NTLM)
/* meta key for storing NTML meta at connection */
#define CURL_META_NTLM_CONN "meta:auth:ntml:conn"
/* meta key for storing NTML-PROXY meta at connection */
#define CURL_META_NTLM_PROXY_CONN "meta:auth:ntml-proxy:conn"
struct ntlmdata {
#ifdef USE_WINDOWS_SSPI
/* The sslContext is used for the Schannel bindings. The
* api is available on the Windows 7 SDK and later.
*/
#ifdef SECPKG_ATTR_ENDPOINT_BINDINGS
CtxtHandle *sslContext;
#endif
CredHandle *credentials;
CtxtHandle *context;
SEC_WINNT_AUTH_IDENTITY identity;
SEC_WINNT_AUTH_IDENTITY *p_identity;
size_t token_max;
BYTE *output_token;
BYTE *input_token;
size_t input_token_len;
TCHAR *spn;
#else
unsigned int flags;
unsigned char nonce[8];
unsigned int target_info_len;
void *target_info; /* TargetInfo received in the NTLM type-2 message */
#endif
};
/* This is used to evaluate if NTLM is supported */
bool Curl_auth_is_ntlm_supported(void);
struct ntlmdata *Curl_auth_ntlm_get(struct connectdata *conn, bool proxy);
void Curl_auth_ntlm_remove(struct connectdata *conn, bool proxy);
/* This is used to clean up the NTLM specific data */
void Curl_auth_cleanup_ntlm(struct ntlmdata *ntlm);
/* This is used to generate a base64 encoded NTLM type-1 message */
CURLcode Curl_auth_create_ntlm_type1_message(struct Curl_easy *data,
const char *userp,
@ -165,8 +213,6 @@ CURLcode Curl_auth_create_ntlm_type3_message(struct Curl_easy *data,
struct ntlmdata *ntlm,
struct bufref *out);
/* This is used to clean up the NTLM specific data */
void Curl_auth_cleanup_ntlm(struct ntlmdata *ntlm);
#else
#define Curl_auth_is_ntlm_supported() FALSE
#endif /* USE_NTLM */
@ -184,6 +230,40 @@ CURLcode Curl_auth_create_xoauth_bearer_message(const char *user,
struct bufref *out);
#if defined(USE_KERBEROS5)
#ifdef HAVE_GSSAPI
# ifdef HAVE_GSSGNU
# include <gss.h>
# elif defined HAVE_GSSAPI_GSSAPI_H
# include <gssapi/gssapi.h>
# else
# include <gssapi.h>
# endif
# ifdef HAVE_GSSAPI_GSSAPI_GENERIC_H
# include <gssapi/gssapi_generic.h>
# endif
#endif
/* meta key for storing KRB5 meta at connection */
#define CURL_META_KRB5_CONN "meta:auth:krb5:conn"
struct kerberos5data {
#if defined(USE_WINDOWS_SSPI)
CredHandle *credentials;
CtxtHandle *context;
TCHAR *spn;
SEC_WINNT_AUTH_IDENTITY identity;
SEC_WINNT_AUTH_IDENTITY *p_identity;
size_t token_max;
BYTE *output_token;
#else
gss_ctx_id_t context;
gss_name_t spn;
#endif
};
struct kerberos5data *Curl_auth_krb5_get(struct connectdata *conn);
/* This is used to evaluate if GSSAPI (Kerberos V5) is supported */
bool Curl_auth_is_gssapi_supported(void);
@ -213,10 +293,48 @@ void Curl_auth_cleanup_gssapi(struct kerberos5data *krb5);
#define Curl_auth_is_gssapi_supported() FALSE
#endif /* USE_KERBEROS5 */
#if defined(USE_SPNEGO)
/* This is used to evaluate if SPNEGO (Negotiate) is supported */
#ifdef USE_SPNEGO
bool Curl_auth_is_spnego_supported(void);
/* meta key for storing NEGO meta at connection */
#define CURL_META_NEGO_CONN "meta:auth:nego:conn"
/* meta key for storing NEGO PROXY meta at connection */
#define CURL_META_NEGO_PROXY_CONN "meta:auth:nego-proxy:conn"
/* Struct used for Negotiate (SPNEGO) authentication */
struct negotiatedata {
#ifdef HAVE_GSSAPI
OM_uint32 status;
gss_ctx_id_t context;
gss_name_t spn;
gss_buffer_desc output_token;
struct dynbuf channel_binding_data;
#else
#ifdef USE_WINDOWS_SSPI
#ifdef SECPKG_ATTR_ENDPOINT_BINDINGS
CtxtHandle *sslContext;
#endif
DWORD status;
CredHandle *credentials;
CtxtHandle *context;
SEC_WINNT_AUTH_IDENTITY identity;
SEC_WINNT_AUTH_IDENTITY *p_identity;
TCHAR *spn;
size_t token_max;
BYTE *output_token;
size_t output_token_length;
#endif
#endif
BIT(noauthpersist);
BIT(havenoauthpersist);
BIT(havenegdata);
BIT(havemultiplerequests);
};
struct negotiatedata *
Curl_auth_nego_get(struct connectdata *conn, bool proxy);
/* This is used to decode a base64 encoded SPNEGO (Negotiate) challenge
message */
CURLcode Curl_auth_decode_spnego_message(struct Curl_easy *data,