mirror of
https://github.com/postgres/postgres.git
synced 2025-06-16 06:01:02 +03:00
Refactor SASL code with a generic interface for its mechanisms
The code of SCRAM and SASL have been tightly linked together since SCRAM exists in the core code, making hard to apprehend the addition of new SASL mechanisms, but these are by design different facilities, with SCRAM being an option for SASL. This refactors the code related to both so as the backend and the frontend use a set of callbacks for SASL mechanisms, documenting while on it what is expected by anybody adding a new SASL mechanism. The separation between both layers is neat, using two sets of callbacks for the frontend and the backend to mark the frontier between both facilities. The shape of the callbacks is now directly inspired from the routines used by SCRAM, so the code change is straight-forward, and the SASL code is moved into its own set of files. These will likely change depending on how and if new SASL mechanisms get added in the future. Author: Jacob Champion Reviewed-by: Michael Paquier Discussion: https://postgr.es/m/3d2a6f5d50e741117d6baf83eb67ebf1a8a35a11.camel@vmware.com
This commit is contained in:
130
src/interfaces/libpq/fe-auth-sasl.h
Normal file
130
src/interfaces/libpq/fe-auth-sasl.h
Normal file
@ -0,0 +1,130 @@
|
||||
/*-------------------------------------------------------------------------
|
||||
*
|
||||
* fe-auth-sasl.h
|
||||
* Defines the SASL mechanism interface for libpq.
|
||||
*
|
||||
* Each SASL mechanism defines a frontend and a backend callback structure.
|
||||
* This is not part of the public API for applications.
|
||||
*
|
||||
* See src/include/libpq/sasl.h for the backend counterpart.
|
||||
*
|
||||
* Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
|
||||
* Portions Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
* src/interfaces/libpq/fe-auth-sasl.h
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
#ifndef FE_AUTH_SASL_H
|
||||
#define FE_AUTH_SASL_H
|
||||
|
||||
#include "libpq-fe.h"
|
||||
|
||||
/*
|
||||
* Frontend SASL mechanism callbacks.
|
||||
*
|
||||
* To implement a frontend mechanism, declare a pg_be_sasl_mech struct with
|
||||
* appropriate callback implementations, then hook it into conn->sasl during
|
||||
* pg_SASL_init()'s mechanism negotiation.
|
||||
*/
|
||||
typedef struct pg_fe_sasl_mech
|
||||
{
|
||||
/*-------
|
||||
* init()
|
||||
*
|
||||
* Initializes mechanism-specific state for a connection. This
|
||||
* callback must return a pointer to its allocated state, which will
|
||||
* be passed as-is as the first argument to the other callbacks.
|
||||
* the free() callback is called to release any state resources.
|
||||
*
|
||||
* If state allocation fails, the implementation should return NULL to
|
||||
* fail the authentication exchange.
|
||||
*
|
||||
* Input parameters:
|
||||
*
|
||||
* conn: The connection to the server
|
||||
*
|
||||
* password: The user's supplied password for the current connection
|
||||
*
|
||||
* mech: The mechanism name in use, for implementations that may
|
||||
* advertise more than one name (such as *-PLUS variants).
|
||||
*-------
|
||||
*/
|
||||
void *(*init) (PGconn *conn, const char *password, const char *mech);
|
||||
|
||||
/*--------
|
||||
* exchange()
|
||||
*
|
||||
* Produces a client response to a server challenge. As a special case
|
||||
* for client-first SASL mechanisms, exchange() is called with a NULL
|
||||
* server response once at the start of the authentication exchange to
|
||||
* generate an initial response.
|
||||
*
|
||||
* Input parameters:
|
||||
*
|
||||
* state: The opaque mechanism state returned by init()
|
||||
*
|
||||
* input: The challenge data sent by the server, or NULL when
|
||||
* generating a client-first initial response (that is, when
|
||||
* the server expects the client to send a message to start
|
||||
* the exchange). This is guaranteed to be null-terminated
|
||||
* for safety, but SASL allows embedded nulls in challenges,
|
||||
* so mechanisms must be careful to check inputlen.
|
||||
*
|
||||
* inputlen: The length of the challenge data sent by the server, or -1
|
||||
* during client-first initial response generation.
|
||||
*
|
||||
* Output parameters, to be set by the callback function:
|
||||
*
|
||||
* output: A malloc'd buffer containing the client's response to
|
||||
* the server, or NULL if the exchange should be aborted.
|
||||
* (*success should be set to false in the latter case.)
|
||||
*
|
||||
* outputlen: The length of the client response buffer, or zero if no
|
||||
* data should be sent due to an exchange failure
|
||||
*
|
||||
* done: Set to true if the SASL exchange should not continue,
|
||||
* because the exchange is either complete or failed
|
||||
*
|
||||
* success: Set to true if the SASL exchange completed successfully.
|
||||
* Ignored if *done is false.
|
||||
*--------
|
||||
*/
|
||||
void (*exchange) (void *state, char *input, int inputlen,
|
||||
char **output, int *outputlen,
|
||||
bool *done, bool *success);
|
||||
|
||||
/*--------
|
||||
* channel_bound()
|
||||
*
|
||||
* Returns true if the connection has an established channel binding. A
|
||||
* mechanism implementation must ensure that a SASL exchange has actually
|
||||
* been completed, in addition to checking that channel binding is in use.
|
||||
*
|
||||
* Mechanisms that do not implement channel binding may simply return
|
||||
* false.
|
||||
*
|
||||
* Input parameters:
|
||||
*
|
||||
* state: The opaque mechanism state returned by init()
|
||||
*--------
|
||||
*/
|
||||
bool (*channel_bound) (void *state);
|
||||
|
||||
/*--------
|
||||
* free()
|
||||
*
|
||||
* Frees the state allocated by init(). This is called when the connection
|
||||
* is dropped, not when the exchange is completed.
|
||||
*
|
||||
* Input parameters:
|
||||
*
|
||||
* state: The opaque mechanism state returned by init()
|
||||
*--------
|
||||
*/
|
||||
void (*free) (void *state);
|
||||
|
||||
} pg_fe_sasl_mech;
|
||||
|
||||
#endif /* FE_AUTH_SASL_H */
|
@ -21,6 +21,22 @@
|
||||
#include "fe-auth.h"
|
||||
|
||||
|
||||
/* The exported SCRAM callback mechanism. */
|
||||
static void *scram_init(PGconn *conn, const char *password,
|
||||
const char *sasl_mechanism);
|
||||
static void scram_exchange(void *opaq, char *input, int inputlen,
|
||||
char **output, int *outputlen,
|
||||
bool *done, bool *success);
|
||||
static bool scram_channel_bound(void *opaq);
|
||||
static void scram_free(void *opaq);
|
||||
|
||||
const pg_fe_sasl_mech pg_scram_mech = {
|
||||
scram_init,
|
||||
scram_exchange,
|
||||
scram_channel_bound,
|
||||
scram_free
|
||||
};
|
||||
|
||||
/*
|
||||
* Status of exchange messages used for SCRAM authentication via the
|
||||
* SASL protocol.
|
||||
@ -72,10 +88,10 @@ static bool calculate_client_proof(fe_scram_state *state,
|
||||
/*
|
||||
* Initialize SCRAM exchange status.
|
||||
*/
|
||||
void *
|
||||
pg_fe_scram_init(PGconn *conn,
|
||||
const char *password,
|
||||
const char *sasl_mechanism)
|
||||
static void *
|
||||
scram_init(PGconn *conn,
|
||||
const char *password,
|
||||
const char *sasl_mechanism)
|
||||
{
|
||||
fe_scram_state *state;
|
||||
char *prep_password;
|
||||
@ -128,8 +144,8 @@ pg_fe_scram_init(PGconn *conn,
|
||||
* Note that the caller must also ensure that the exchange was actually
|
||||
* successful.
|
||||
*/
|
||||
bool
|
||||
pg_fe_scram_channel_bound(void *opaq)
|
||||
static bool
|
||||
scram_channel_bound(void *opaq)
|
||||
{
|
||||
fe_scram_state *state = (fe_scram_state *) opaq;
|
||||
|
||||
@ -152,8 +168,8 @@ pg_fe_scram_channel_bound(void *opaq)
|
||||
/*
|
||||
* Free SCRAM exchange status
|
||||
*/
|
||||
void
|
||||
pg_fe_scram_free(void *opaq)
|
||||
static void
|
||||
scram_free(void *opaq)
|
||||
{
|
||||
fe_scram_state *state = (fe_scram_state *) opaq;
|
||||
|
||||
@ -188,10 +204,10 @@ pg_fe_scram_free(void *opaq)
|
||||
/*
|
||||
* Exchange a SCRAM message with backend.
|
||||
*/
|
||||
void
|
||||
pg_fe_scram_exchange(void *opaq, char *input, int inputlen,
|
||||
char **output, int *outputlen,
|
||||
bool *done, bool *success)
|
||||
static void
|
||||
scram_exchange(void *opaq, char *input, int inputlen,
|
||||
char **output, int *outputlen,
|
||||
bool *done, bool *success)
|
||||
{
|
||||
fe_scram_state *state = (fe_scram_state *) opaq;
|
||||
PGconn *conn = state->conn;
|
||||
|
@ -41,6 +41,7 @@
|
||||
#include "common/md5.h"
|
||||
#include "common/scram-common.h"
|
||||
#include "fe-auth.h"
|
||||
#include "fe-auth-sasl.h"
|
||||
#include "libpq-fe.h"
|
||||
|
||||
#ifdef ENABLE_GSS
|
||||
@ -482,7 +483,10 @@ pg_SASL_init(PGconn *conn, int payloadlen)
|
||||
* channel_binding is not disabled.
|
||||
*/
|
||||
if (conn->channel_binding[0] != 'd') /* disable */
|
||||
{
|
||||
selected_mechanism = SCRAM_SHA_256_PLUS_NAME;
|
||||
conn->sasl = &pg_scram_mech;
|
||||
}
|
||||
#else
|
||||
/*
|
||||
* The client does not support channel binding. If it is
|
||||
@ -516,7 +520,10 @@ pg_SASL_init(PGconn *conn, int payloadlen)
|
||||
}
|
||||
else if (strcmp(mechanism_buf.data, SCRAM_SHA_256_NAME) == 0 &&
|
||||
!selected_mechanism)
|
||||
{
|
||||
selected_mechanism = SCRAM_SHA_256_NAME;
|
||||
conn->sasl = &pg_scram_mech;
|
||||
}
|
||||
}
|
||||
|
||||
if (!selected_mechanism)
|
||||
@ -555,20 +562,22 @@ pg_SASL_init(PGconn *conn, int payloadlen)
|
||||
goto error;
|
||||
}
|
||||
|
||||
Assert(conn->sasl);
|
||||
|
||||
/*
|
||||
* Initialize the SASL state information with all the information gathered
|
||||
* during the initial exchange.
|
||||
*
|
||||
* Note: Only tls-unique is supported for the moment.
|
||||
*/
|
||||
conn->sasl_state = pg_fe_scram_init(conn,
|
||||
conn->sasl_state = conn->sasl->init(conn,
|
||||
password,
|
||||
selected_mechanism);
|
||||
if (!conn->sasl_state)
|
||||
goto oom_error;
|
||||
|
||||
/* Get the mechanism-specific Initial Client Response, if any */
|
||||
pg_fe_scram_exchange(conn->sasl_state,
|
||||
conn->sasl->exchange(conn->sasl_state,
|
||||
NULL, -1,
|
||||
&initialresponse, &initialresponselen,
|
||||
&done, &success);
|
||||
@ -649,7 +658,7 @@ pg_SASL_continue(PGconn *conn, int payloadlen, bool final)
|
||||
/* For safety and convenience, ensure the buffer is NULL-terminated. */
|
||||
challenge[payloadlen] = '\0';
|
||||
|
||||
pg_fe_scram_exchange(conn->sasl_state,
|
||||
conn->sasl->exchange(conn->sasl_state,
|
||||
challenge, payloadlen,
|
||||
&output, &outputlen,
|
||||
&done, &success);
|
||||
@ -664,6 +673,7 @@ pg_SASL_continue(PGconn *conn, int payloadlen, bool final)
|
||||
libpq_gettext("AuthenticationSASLFinal received from server, but SASL authentication was not completed\n"));
|
||||
return STATUS_ERROR;
|
||||
}
|
||||
|
||||
if (outputlen != 0)
|
||||
{
|
||||
/*
|
||||
@ -830,7 +840,7 @@ check_expected_areq(AuthRequest areq, PGconn *conn)
|
||||
case AUTH_REQ_SASL_FIN:
|
||||
break;
|
||||
case AUTH_REQ_OK:
|
||||
if (!pg_fe_scram_channel_bound(conn->sasl_state))
|
||||
if (!conn->sasl || !conn->sasl->channel_bound(conn->sasl_state))
|
||||
{
|
||||
appendPQExpBufferStr(&conn->errorMessage,
|
||||
libpq_gettext("channel binding required, but server authenticated client without channel binding\n"));
|
||||
|
@ -22,15 +22,8 @@
|
||||
extern int pg_fe_sendauth(AuthRequest areq, int payloadlen, PGconn *conn);
|
||||
extern char *pg_fe_getauthname(PQExpBuffer errorMessage);
|
||||
|
||||
/* Prototypes for functions in fe-auth-scram.c */
|
||||
extern void *pg_fe_scram_init(PGconn *conn,
|
||||
const char *password,
|
||||
const char *sasl_mechanism);
|
||||
extern bool pg_fe_scram_channel_bound(void *opaq);
|
||||
extern void pg_fe_scram_free(void *opaq);
|
||||
extern void pg_fe_scram_exchange(void *opaq, char *input, int inputlen,
|
||||
char **output, int *outputlen,
|
||||
bool *done, bool *success);
|
||||
/* Mechanisms in fe-auth-scram.c */
|
||||
extern const pg_fe_sasl_mech pg_scram_mech;
|
||||
extern char *pg_fe_scram_build_secret(const char *password);
|
||||
|
||||
#endif /* FE_AUTH_H */
|
||||
|
@ -516,11 +516,7 @@ pqDropConnection(PGconn *conn, bool flushInput)
|
||||
#endif
|
||||
if (conn->sasl_state)
|
||||
{
|
||||
/*
|
||||
* XXX: if support for more authentication mechanisms is added, this
|
||||
* needs to call the right 'free' function.
|
||||
*/
|
||||
pg_fe_scram_free(conn->sasl_state);
|
||||
conn->sasl->free(conn->sasl_state);
|
||||
conn->sasl_state = NULL;
|
||||
}
|
||||
}
|
||||
|
@ -41,6 +41,7 @@
|
||||
#include "getaddrinfo.h"
|
||||
#include "libpq/pqcomm.h"
|
||||
/* include stuff found in fe only */
|
||||
#include "fe-auth-sasl.h"
|
||||
#include "pqexpbuffer.h"
|
||||
|
||||
#ifdef ENABLE_GSS
|
||||
@ -500,6 +501,7 @@ struct pg_conn
|
||||
PGresult *next_result; /* next result (used in single-row mode) */
|
||||
|
||||
/* Assorted state for SASL, SSL, GSS, etc */
|
||||
const pg_fe_sasl_mech *sasl;
|
||||
void *sasl_state;
|
||||
|
||||
/* SSL structures */
|
||||
|
Reference in New Issue
Block a user