PostgreSQL Source Code git master
Loading...
Searching...
No Matches
oauth.h File Reference
#include "libpq/libpq-be.h"
#include "libpq/sasl.h"
Include dependency graph for oauth.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ValidatorModuleState
 
struct  ValidatorModuleResult
 
struct  OAuthValidatorCallbacks
 

Macros

#define PG_OAUTH_VALIDATOR_MAGIC   0x20250220
 

Typedefs

typedef struct ValidatorModuleState ValidatorModuleState
 
typedef struct ValidatorModuleResult ValidatorModuleResult
 
typedef void(* ValidatorStartupCB) (ValidatorModuleState *state)
 
typedef void(* ValidatorShutdownCB) (ValidatorModuleState *state)
 
typedef bool(* ValidatorValidateCB) (const ValidatorModuleState *state, const char *token, const char *role, ValidatorModuleResult *result)
 
typedef struct OAuthValidatorCallbacks OAuthValidatorCallbacks
 
typedef const OAuthValidatorCallbacks *(* OAuthValidatorModuleInit) (void)
 

Functions

void RegisterOAuthHBAOptions (ValidatorModuleState *state, int num, const char *opts[])
 
const charGetOAuthHBAOption (const ValidatorModuleState *state, const char *optname)
 
PGDLLEXPORT const OAuthValidatorCallbacks_PG_oauth_validator_module_init (void)
 
bool check_oauth_validator (HbaLine *hbaline, int elevel, char **err_msg)
 
bool valid_oauth_hba_option_name (const char *name)
 

Variables

PGDLLIMPORT charoauth_validator_libraries_string
 
PGDLLIMPORT const pg_be_sasl_mech pg_be_oauth_mech
 

Macro Definition Documentation

◆ PG_OAUTH_VALIDATOR_MAGIC

#define PG_OAUTH_VALIDATOR_MAGIC   0x20250220

Definition at line 88 of file oauth.h.

Typedef Documentation

◆ OAuthValidatorCallbacks

◆ OAuthValidatorModuleInit

typedef const OAuthValidatorCallbacks *(* OAuthValidatorModuleInit) (void)

Definition at line 115 of file oauth.h.

◆ ValidatorModuleResult

◆ ValidatorModuleState

◆ ValidatorShutdownCB

typedef void(* ValidatorShutdownCB) (ValidatorModuleState *state)

Definition at line 77 of file oauth.h.

◆ ValidatorStartupCB

typedef void(* ValidatorStartupCB) (ValidatorModuleState *state)

Definition at line 76 of file oauth.h.

◆ ValidatorValidateCB

Definition at line 78 of file oauth.h.

Function Documentation

◆ _PG_oauth_validator_module_init()

PGDLLEXPORT const OAuthValidatorCallbacks * _PG_oauth_validator_module_init ( void  )
extern

Definition at line 35 of file fail_validator.c.

36{
37 return &validator_callbacks;
38}
static const OAuthValidatorCallbacks validator_callbacks

References validator_callbacks, validator_callbacks, and validator_callbacks.

◆ check_oauth_validator()

bool check_oauth_validator ( HbaLine hbaline,
int  elevel,
char **  err_msg 
)
extern

Definition at line 857 of file auth-oauth.c.

858{
859 int line_num = hbaline->linenumber;
860 const char *file_name = hbaline->sourcefile;
861 char *rawstring;
862 List *elemlist = NIL;
863
864 *err_msg = NULL;
865
867 {
868 ereport(elevel,
870 errmsg("oauth_validator_libraries must be set for authentication method %s",
871 "oauth"),
872 errcontext("line %d of configuration file \"%s\"",
873 line_num, file_name));
874 *err_msg = psprintf("oauth_validator_libraries must be set for authentication method %s",
875 "oauth");
876 return false;
877 }
878
879 /* SplitDirectoriesString needs a modifiable copy */
881
883 {
884 /* syntax error in list */
885 ereport(elevel,
887 errmsg("invalid list syntax in parameter \"%s\"",
888 "oauth_validator_libraries"));
889 *err_msg = psprintf("invalid list syntax in parameter \"%s\"",
890 "oauth_validator_libraries");
891 goto done;
892 }
893
894 if (!hbaline->oauth_validator)
895 {
896 if (elemlist->length == 1)
897 {
898 hbaline->oauth_validator = pstrdup(linitial(elemlist));
899 goto done;
900 }
901
902 ereport(elevel,
904 errmsg("authentication method \"oauth\" requires argument \"validator\" to be set when oauth_validator_libraries contains multiple options"),
905 errcontext("line %d of configuration file \"%s\"",
906 line_num, file_name));
907 *err_msg = "authentication method \"oauth\" requires argument \"validator\" to be set when oauth_validator_libraries contains multiple options";
908 goto done;
909 }
910
911 foreach_ptr(char, allowed, elemlist)
912 {
913 if (strcmp(allowed, hbaline->oauth_validator) == 0)
914 goto done;
915 }
916
917 ereport(elevel,
919 errmsg("validator \"%s\" is not permitted by %s",
920 hbaline->oauth_validator, "oauth_validator_libraries"),
921 errcontext("line %d of configuration file \"%s\"",
922 line_num, file_name));
923 *err_msg = psprintf("validator \"%s\" is not permitted by %s",
924 hbaline->oauth_validator, "oauth_validator_libraries");
925
926done:
929
930 return (*err_msg == NULL);
931}
char * oauth_validator_libraries_string
Definition auth-oauth.c:35
int errcode(int sqlerrcode)
Definition elog.c:874
#define errcontext
Definition elog.h:200
#define ereport(elevel,...)
Definition elog.h:152
void list_free_deep(List *list)
Definition list.c:1560
char * pstrdup(const char *in)
Definition mcxt.c:1781
void pfree(void *pointer)
Definition mcxt.c:1616
static char * errmsg
#define NIL
Definition pg_list.h:68
#define foreach_ptr(type, var, lst)
Definition pg_list.h:501
#define linitial(l)
Definition pg_list.h:178
static int fb(int x)
char * psprintf(const char *fmt,...)
Definition psprintf.c:43
Definition pg_list.h:54
bool SplitDirectoriesString(char *rawstring, char separator, List **namelist)
Definition varlena.c:2950

References ereport, errcode(), errcontext, errmsg, fb(), foreach_ptr, linitial, list_free_deep(), NIL, oauth_validator_libraries_string, pfree(), psprintf(), pstrdup(), and SplitDirectoriesString().

Referenced by parse_hba_line().

◆ GetOAuthHBAOption()

const char * GetOAuthHBAOption ( const ValidatorModuleState state,
const char optname 
)
extern

Definition at line 1090 of file auth-oauth.c.

1091{
1092 HbaLine *hba = MyProcPort->hba;
1093 ListCell *lc_k;
1094 ListCell *lc_v;
1095 const char *ret = NULL;
1096
1098 {
1099 /*
1100 * Prevent the startup_cb from retrieving HBA options that it has just
1101 * registered. This probably seems strange -- why refuse to hand out
1102 * information we already know? -- but this lets us reserve the
1103 * ability to perform the startup_cb call earlier, before we know
1104 * which HBA line is matched by a connection, without breaking this
1105 * API.
1106 */
1107 return NULL;
1108 }
1109
1110 if (!state || !hba)
1111 {
1112 Assert(false);
1113 return NULL;
1114 }
1115
1117
1119 {
1120 const char *key = lfirst(lc_k);
1121 const char *val = lfirst(lc_v);
1122
1123 if (strcmp(key, optname) == 0)
1124 {
1125 /*
1126 * Don't return yet -- when regular HBA options are specified more
1127 * than once, the last one wins. Do the same for these options.
1128 */
1129 ret = val;
1130 }
1131 }
1132
1133 return ret;
1134}
static bool ValidatorOptionsChecked
Definition auth-oauth.c:51
#define Assert(condition)
Definition c.h:943
struct Port * MyProcPort
Definition globals.c:53
long val
Definition informix.c:689
#define lfirst(lc)
Definition pg_list.h:172
static int list_length(const List *l)
Definition pg_list.h:152
#define forboth(cell1, list1, cell2, list2)
Definition pg_list.h:550
Definition hba.h:95
List * oauth_opt_vals
Definition hba.h:135
List * oauth_opt_keys
Definition hba.h:134
HbaLine * hba
Definition libpq-be.h:165

References Assert, fb(), forboth, Port::hba, lfirst, list_length(), MyProcPort, HbaLine::oauth_opt_keys, HbaLine::oauth_opt_vals, val, and ValidatorOptionsChecked.

Referenced by validate_token(), and validator_startup().

◆ RegisterOAuthHBAOptions()

void RegisterOAuthHBAOptions ( ValidatorModuleState state,
int  num,
const char opts[] 
)
extern

Definition at line 949 of file auth-oauth.c.

951{
952 MemoryContext oldcontext;
953
954 if (!state)
955 {
956 Assert(false);
957 return;
958 }
959
961
962 for (int i = 0; i < num; i++)
963 {
965 {
966 /*
967 * The user can't set this option in the HBA, so GetOAuthHBAOption
968 * would always return NULL.
969 */
971 errmsg("HBA option name \"%s\" is invalid and will be ignored",
972 opts[i]),
973 /* translator: the second %s is a function name */
974 errcontext("validator module \"%s\", in call to %s",
976 "RegisterOAuthHBAOptions"));
977 continue;
978 }
979
981 }
982
983 MemoryContextSwitchTo(oldcontext);
984
985 /*
986 * Wait to validate the HBA against the registered options until later
987 * (see check_validator_hba_options()).
988 *
989 * Delaying allows the validator to make multiple registration calls, to
990 * append to the list; it lets us make the check in a place where we can
991 * report the error without leaking details to the client; and it avoids
992 * exporting the order of operations between HBA matching and the
993 * startup_cb call as an API guarantee. (The last issue may become
994 * relevant with a threaded model.)
995 */
996}
static MemoryContext ValidatorMemoryContext
Definition auth-oauth.c:49
static List * ValidatorOptions
Definition auth-oauth.c:50
bool valid_oauth_hba_option_name(const char *name)
#define WARNING
Definition elog.h:37
int i
Definition isn.c:77
List * lappend(List *list, void *datum)
Definition list.c:339
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:124
static AmcheckOptions opts
Definition pg_amcheck.c:112
char * oauth_validator
Definition hba.h:132

References Assert, ereport, errcontext, errmsg, Port::hba, i, lappend(), MemoryContextSwitchTo(), MyProcPort, HbaLine::oauth_validator, opts, pstrdup(), valid_oauth_hba_option_name(), ValidatorMemoryContext, ValidatorOptions, and WARNING.

Referenced by validator_startup().

◆ valid_oauth_hba_option_name()

bool valid_oauth_hba_option_name ( const char name)
extern

Definition at line 1003 of file auth-oauth.c.

1004{
1005 /*
1006 * This list is not incredibly principled, since the goal is just to bound
1007 * compatibility guarantees for our HBA parser. Alphanumerics seem
1008 * obviously fine, and it's difficult to argue against the punctuation
1009 * that's already included in some HBA option names and identifiers.
1010 */
1011 static const char *name_allowed_set =
1012 "abcdefghijklmnopqrstuvwxyz"
1013 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1014 "0123456789_-";
1015
1016 size_t span;
1017
1018 if (!name[0])
1019 return false;
1020
1022 return name[span] == '\0';
1023}
const char * name

References fb(), and name.

Referenced by parse_hba_auth_opt(), and RegisterOAuthHBAOptions().

Variable Documentation

◆ oauth_validator_libraries_string

PGDLLIMPORT char* oauth_validator_libraries_string
extern

Definition at line 35 of file auth-oauth.c.

Referenced by check_oauth_validator().

◆ pg_be_oauth_mech

PGDLLIMPORT const pg_be_sasl_mech pg_be_oauth_mech
extern

Definition at line 54 of file auth-oauth.c.

54 {
55 .get_mechanisms = oauth_get_mechanisms,
56 .init = oauth_init,
57 .exchange = oauth_exchange,
58
59 .max_message_length = PG_MAX_AUTH_TOKEN_LENGTH,
60};
static void * oauth_init(Port *port, const char *selected_mech, const char *shadow_pass)
Definition auth-oauth.c:109
static void oauth_get_mechanisms(Port *port, StringInfo buf)
Definition auth-oauth.c:96
static int oauth_exchange(void *opaq, const char *input, int inputlen, char **output, int *outputlen, const char **logdetail)
Definition auth-oauth.c:144
#define PG_MAX_AUTH_TOKEN_LENGTH
Definition auth.h:33

Referenced by ClientAuthentication().