22#if defined(HAVE_SYS_EPOLL_H)
24#include <sys/timerfd.h>
25#elif defined(HAVE_SYS_EVENT_H)
28#error libpq-oauth is not supported on this platform
36#ifdef USE_DYNAMIC_OAUTH
53#define conn_errorMessage(CONN) (&CONN->errorMessage)
54#define conn_oauth_client_id(CONN) (CONN->oauth_client_id)
55#define conn_oauth_client_secret(CONN) (CONN->oauth_client_secret)
56#define conn_oauth_discovery_uri(CONN) (CONN->oauth_discovery_uri)
57#define conn_oauth_issuer_id(CONN) (CONN->oauth_issuer_id)
58#define conn_oauth_scope(CONN) (CONN->oauth_scope)
59#define conn_sasl_state(CONN) (CONN->sasl_state)
61#define set_conn_altsock(CONN, VAL) do { CONN->altsock = VAL; } while (0)
62#define set_conn_oauth_token(CONN, VAL) do { CONN->oauth_token = VAL; } while (0)
67#if defined(USE_DYNAMIC_OAUTH) && defined(LIBPQ_INT_H)
68#error do not rely on libpq-int.h in dynamic builds of libpq-oauth
83#define MAX_OAUTH_RESPONSE_SIZE (256 * 1024)
97#define MAX_OAUTH_NESTING_LEVEL 16
308 "libcurl easy handle removal failed: %s",
328 "libcurl multi handle cleanup failed: %s",
341 if (
actx->timerfd >= 0)
360 if (
state->async_ctx)
375#define actx_error(ACTX, FMT, ...) \
376 appendPQExpBuffer(&(ACTX)->errbuf, libpq_gettext(FMT), ##__VA_ARGS__)
378#define actx_error_internal(ACTX, FMT, ...) \
379 appendPQExpBuffer(&(ACTX)->errbuf, FMT, ##__VA_ARGS__)
381#define actx_error_str(ACTX, S) \
382 appendPQExpBufferStr(&(ACTX)->errbuf, S)
389#define CHECK_MSETOPT(ACTX, OPT, VAL, FAILACTION) \
391 struct async_ctx *_actx = (ACTX); \
392 CURLMcode _setopterr = curl_multi_setopt(_actx->curlm, OPT, VAL); \
394 actx_error(_actx, "failed to set %s on OAuth connection: %s",\
395 #OPT, curl_multi_strerror(_setopterr)); \
400#define CHECK_SETOPT(ACTX, OPT, VAL, FAILACTION) \
402 struct async_ctx *_actx = (ACTX); \
403 CURLcode _setopterr = curl_easy_setopt(_actx->curl, OPT, VAL); \
405 actx_error(_actx, "failed to set %s on OAuth connection: %s",\
406 #OPT, curl_easy_strerror(_setopterr)); \
411#define CHECK_GETINFO(ACTX, INFO, OUT, FAILACTION) \
413 struct async_ctx *_actx = (ACTX); \
414 CURLcode _getinfoerr = curl_easy_getinfo(_actx->curl, INFO, OUT); \
416 actx_error(_actx, "failed to get %s from OAuth response: %s",\
417 #INFO, curl_easy_strerror(_getinfoerr)); \
452#define PG_OAUTH_REQUIRED true
453#define PG_OAUTH_OPTIONAL false
465#define oauth_parse_set_error(ctx, fmt, ...) \
466 appendPQExpBuffer((ctx)->errbuf, libpq_gettext(fmt), ##__VA_ARGS__)
468#define oauth_parse_set_error_internal(ctx, fmt, ...) \
469 appendPQExpBuffer((ctx)->errbuf, fmt, ##__VA_ARGS__)
547 "internal error: started field \"%s\" before field \"%s\" was finished",
599 "internal error: field \"%s\" still active at end of object",
655 "internal error: found unexpected array end while parsing field \"%s\"",
710 "internal error: scalar target found at nesting level %d",
720 "internal error: scalar field \"%s\" would be assigned twice",
742 "internal error: array member found at nesting level %d",
800 switch (content_type[
i])
816 actx_error(
actx,
"unexpected content type: \"%s\"", content_type);
988 return actx->debugging ? 0 : 1;
1109 if (
err->error_description)
1125 ?
gettext_noop(
"provider rejected the oauth_client_secret")
1126 :
gettext_noop(
"provider requires client authentication, and no oauth_client_secret is set"));
1183#if defined(HAVE_SYS_EPOLL_H)
1194 if (
actx->timerfd < 0)
1207#elif defined(HAVE_SYS_EVENT_H)
1222 if (
actx->timerfd < 0)
1230#error setup_multiplexer is not implemented on this platform
1244#if defined(HAVE_SYS_EPOLL_H)
1300#elif defined(HAVE_SYS_EVENT_H)
1362 for (
int i = 0;
i < res; ++
i)
1388#error register_socket is not implemented on this platform
1407#if defined(HAVE_SYS_EPOLL_H)
1410#elif defined(HAVE_SYS_EVENT_H)
1435#error comb_multiplexer is not implemented on this platform
1455#if defined(HAVE_SYS_EPOLL_H)
1469 spec.it_value.tv_nsec = 1;
1474 spec.it_value.tv_nsec = (
timeout % 1000) * 1000000;
1484#elif defined(HAVE_SYS_EVENT_H)
1540#error set_timer is not implemented on this platform
1552#if defined(HAVE_SYS_EPOLL_H) || defined(HAVE_SYS_EVENT_H)
1565#error timer_expired is not implemented on this platform
1663 for (
int i = 0;
i < size;
i++)
1673 if (
c >= 0x20 &&
c <= 0x7E)
1678 && (
c ==
'\r' ||
c ==
'\n'))
1762 if (
actx->debugging)
1784#if CURL_AT_LEAST_VERSION(7, 85, 0)
1786 const char *
protos =
"https";
1787 const char *
const unsafe =
"https,http";
1794 if (
actx->debugging)
1808 if (
actx->debugging)
1924#ifndef CURL_IGNORE_DEPRECATION
1925#define CURL_IGNORE_DEPRECATION(x) x
1932#define PG_CURL_IGNORE_DEPRECATION(x) CURL_IGNORE_DEPRECATION(x;)
2001 if (
actx->errbuf.len == 0)
2022 actx_error(
actx,
"no result was retrieved for the finished handle");
2169 if (!
actx->provider.grant_types_supported)
2188 actx->provider.grant_types_supported =
temp;
2227 "the issuer identifier (%s) does not match oauth_issuer (%s)",
2235#define HTTPS_SCHEME "https://"
2236#define OAUTH_GRANT_TYPE_DEVICE_CODE "urn:ietf:params:oauth:grant-type:device_code"
2254 "issuer \"%s\" does not provide a device authorization endpoint",
2274 if (!
actx->debugging)
2280 "device authorization endpoint \"%s\" must use HTTPS",
2289 "token endpoint \"%s\" must use HTTPS",
2312 if (oauth_client_secret)
2353 actx->used_basic_auth =
true;
2364 actx->used_basic_auth =
false;
2398 if (oauth_scope && oauth_scope[0])
2478 const char *device_code =
actx->authz.device_code;
2566 if (
tok.access_token)
2580 if (
strcmp(
err->error,
"authorization_pending") != 0 &&
2591 if (
strcmp(
err->error,
"slow_down") == 0)
2595 actx->authz.interval += 5;
2620 .user_code =
actx->authz.user_code,
2621 .verification_uri_complete =
actx->authz.verification_uri_complete,
2622 .expires_in =
actx->authz.expires_in,
2669#if HAVE_THREADSAFE_CURL_GLOBAL_INIT
2673#if !HAVE_THREADSAFE_CURL_GLOBAL_INIT
2695 "curl_global_init previously failed during OAuth setup");
2714 "curl_global_init failed during OAuth setup");
2719#if HAVE_THREADSAFE_CURL_GLOBAL_INIT
2736 "\tCurl initialization was reported thread-safe when libpq\n"
2737 "\twas compiled, but the currently installed version of\n"
2738 "\tlibcurl reports that it is not. Recompile libpq against\n"
2739 "\tthe installed version of libcurl.");
2748#if !HAVE_THREADSAFE_CURL_GLOBAL_INIT
2773 char *oauth_token =
NULL;
2779 if (!
state->async_ctx)
2943 if (!
actx->user_prompted)
2952 actx->user_prompted =
true;
2990 }
while (!oauth_token && !
actx->running);
3010 if (
actx->curl_err[0])
3040 bool sigpipe_pending;
3079 actx->dbg_num_calls++;
3082 actx->dbg_num_calls);
static void cleanup(void)
#define Assert(condition)
#define fprintf(file, fmt, msg)
void err(int eval, const char *fmt,...)
PQauthDataHook_type PQgetAuthDataHook(void)
int PQsocketPoll(int sock, int forRead, int forWrite, pg_usec_time_t end_time)
JsonParseErrorType pg_parse_json(JsonLexContext *lex, const JsonSemAction *sem)
JsonLexContext * makeJsonLexContextCstringLen(JsonLexContext *lex, const char *json, size_t len, int encoding, bool need_escapes)
void setJsonLexContextOwnsTokens(JsonLexContext *lex, bool owned_by_context)
char * json_errdetail(JsonParseErrorType error, JsonLexContext *lex)
void freeJsonLexContext(JsonLexContext *lex)
int(* PQauthDataHook_type)(PGauthData type, PGconn *conn, void *data)
PostgresPollingStatusType
@ PQAUTHDATA_PROMPT_OAUTH_DEVICE
PostgresPollingStatusType pg_fe_run_oauth_flow(PGconn *conn)
static bool drain_timer_events(struct async_ctx *actx, bool *was_expired)
static char * urlencode(const char *s)
static bool setup_multiplexer(struct async_ctx *actx)
static bool finish_token_request(struct async_ctx *actx, struct token *tok)
static JsonParseErrorType oauth_json_array_end(void *state)
static void append_urlencoded(PQExpBuffer buf, const char *s)
static bool start_token_request(struct async_ctx *actx, PGconn *conn)
static bool initialize_curl(PGconn *conn)
#define MAX_OAUTH_RESPONSE_SIZE
static bool parse_token_error(struct async_ctx *actx, struct token_error *err)
void pg_fe_cleanup_oauth_flow(PGconn *conn)
static bool add_client_identification(struct async_ctx *actx, PQExpBuffer reqbody, PGconn *conn)
static int parse_interval(struct async_ctx *actx, const char *interval_str)
static void free_provider(struct provider *provider)
static void build_urlencoded(PQExpBuffer buf, const char *key, const char *value)
#define PG_CURL_IGNORE_DEPRECATION(x)
#define conn_oauth_issuer_id(CONN)
#define oauth_parse_set_error_internal(ctx, fmt,...)
static void record_token_error(struct async_ctx *actx, const struct token_error *err)
static bool parse_device_authz(struct async_ctx *actx, struct device_authz *authz)
static void report_type_mismatch(struct oauth_parse *ctx)
static int register_socket(CURL *curl, curl_socket_t socket, int what, void *ctx, void *socketp)
#define PG_OAUTH_OPTIONAL
static bool set_timer(struct async_ctx *actx, long timeout)
#define actx_error_internal(ACTX, FMT,...)
static bool parse_access_token(struct async_ctx *actx, struct token *tok)
static int timer_expired(struct async_ctx *actx)
static PostgresPollingStatusType drive_request(struct async_ctx *actx)
static bool start_device_authz(struct async_ctx *actx, PGconn *conn)
static bool prompt_user(struct async_ctx *actx, PGconn *conn)
#define CHECK_MSETOPT(ACTX, OPT, VAL, FAILACTION)
static bool finish_discovery(struct async_ctx *actx)
static double parse_json_number(const char *s)
#define conn_oauth_discovery_uri(CONN)
static bool start_discovery(struct async_ctx *actx, const char *discovery_uri)
static JsonParseErrorType oauth_json_object_field_start(void *state, char *name, bool isnull)
static JsonParseErrorType oauth_json_scalar(void *state, char *token, JsonTokenType type)
static void free_token_error(struct token_error *err)
#define actx_error_str(ACTX, S)
static bool finish_device_authz(struct async_ctx *actx)
#define conn_oauth_scope(CONN)
static size_t append_data(char *buf, size_t size, size_t nmemb, void *userdata)
#define CHECK_SETOPT(ACTX, OPT, VAL, FAILACTION)
static PostgresPollingStatusType pg_fe_run_oauth_flow_impl(PGconn *conn)
static bool parse_oauth_json(struct async_ctx *actx, const struct json_field *fields)
#define MAX_OAUTH_NESTING_LEVEL
#define OAUTH_GRANT_TYPE_DEVICE_CODE
#define conn_oauth_client_id(CONN)
static JsonParseErrorType oauth_json_array_start(void *state)
static JsonParseErrorType oauth_json_object_end(void *state)
#define set_conn_altsock(CONN, VAL)
static int debug_callback(CURL *handle, curl_infotype type, char *data, size_t size, void *clientp)
static void free_token(struct token *tok)
#define oauth_parse_set_error(ctx, fmt,...)
static bool comb_multiplexer(struct async_ctx *actx)
@ OAUTH_STEP_DEVICE_AUTHORIZATION
@ OAUTH_STEP_WAIT_INTERVAL
@ OAUTH_STEP_TOKEN_REQUEST
static int register_timer(CURLM *curlm, long timeout, void *ctx)
#define conn_oauth_client_secret(CONN)
#define set_conn_oauth_token(CONN, VAL)
#define CHECK_GETINFO(ACTX, INFO, OUT, FAILACTION)
static bool check_content_type(struct async_ctx *actx, const char *type)
static bool check_issuer(struct async_ctx *actx, PGconn *conn)
#define actx_error(ACTX, FMT,...)
static bool parse_provider(struct async_ctx *actx, struct provider *provider)
static bool start_request(struct async_ctx *actx)
static int parse_expires_in(struct async_ctx *actx, const char *expires_in_str)
static void free_async_ctx(PGconn *conn, struct async_ctx *actx)
static void free_device_authz(struct device_authz *authz)
#define conn_sasl_state(CONN)
#define conn_errorMessage(CONN)
static bool handle_token_response(struct async_ctx *actx, char **token)
static JsonParseErrorType oauth_json_object_start(void *state)
#define PG_OAUTH_REQUIRED
static bool check_for_device_flow(struct async_ctx *actx)
static bool setup_curl_handles(struct async_ctx *actx)
void pq_reset_sigpipe(sigset_t *osigset, bool sigpipe_pending, bool got_epipe)
int pq_block_sigpipe(sigset_t *osigset, bool *sigpipe_pending)
void libpq_append_conn_error(PGconn *conn, const char *fmt,...)
bool oauth_unsafe_debugging_enabled(void)
#define pgunlock_thread()
static char buf[DEFAULT_XLOG_SEG_SIZE]
int pg_strncasecmp(const char *s1, const char *s2, size_t n)
void initPQExpBuffer(PQExpBuffer str)
void resetPQExpBuffer(PQExpBuffer str)
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
void appendBinaryPQExpBuffer(PQExpBuffer str, const char *data, size_t datalen)
void appendPQExpBufferChar(PQExpBuffer str, char ch)
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
void termPQExpBuffer(PQExpBuffer str)
#define PQExpBufferBroken(str)
#define PQExpBufferDataBroken(buf)
json_struct_action array_end
json_struct_action object_start
json_ofield_action object_field_start
json_scalar_action scalar
json_struct_action array_start
json_struct_action object_end
const char * verification_uri
struct device_authz authz
PQExpBufferData work_data
char curl_err[CURL_ERROR_SIZE]
struct curl_slist * headers
char * verification_uri_complete
struct curl_slist ** array
const struct json_field * active
const struct json_field * fields
char * device_authorization_endpoint
struct curl_slist * grant_types_supported
int pg_encoding_verifymbstr(int encoding, const char *mbstr, int len)
#define socket(af, type, protocol)