PostgreSQL Source Code git master
Loading...
Searching...
No Matches
oauth-utils.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * oauth-utils.c
4 *
5 * "Glue" helpers providing a copy of some internal APIs from libpq. At
6 * some point in the future, we might be able to deduplicate.
7 *
8 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
9 * Portions Copyright (c) 1994, Regents of the University of California
10 *
11 * IDENTIFICATION
12 * src/interfaces/libpq-oauth/oauth-utils.c
13 *
14 *-------------------------------------------------------------------------
15 */
16
17#include "postgres_fe.h"
18
19#include <signal.h>
20
21#include "oauth-utils.h"
22
23#ifndef USE_DYNAMIC_OAUTH
24#error oauth-utils.c is not supported in static builds
25#endif
26
27#ifdef LIBPQ_INT_H
28#error do not rely on libpq-int.h in dynamic builds of libpq-oauth
29#endif
30
31/*
32 * Function pointers set by libpq_oauth_init().
33 */
34
37
38/*-
39 * Initializes libpq-oauth by setting necessary callbacks.
40 *
41 * The current implementation relies on libpq_gettext to translate error
42 * messages using libpq's message domain, so libpq injects it here. We also use
43 * this chance to initialize our threadlock.
44 */
45void
51
52#ifdef ENABLE_NLS
53
54/*
55 * A shim that defers to the actual libpq_gettext().
56 */
57char *
58libpq_gettext(const char *msgid)
59{
61 {
62 /*
63 * Possible if the libpq build didn't enable NLS but the libpq-oauth
64 * build did. That's an odd mismatch, but we can handle it.
65 *
66 * Note that callers of libpq_gettext() have to treat the return value
67 * as if it were const, because builds without NLS simply pass through
68 * their argument.
69 */
70 return unconstify(char *, msgid);
71 }
72
74}
75
76#endif /* ENABLE_NLS */
77
78/*
79 * Returns true if the PGOAUTHDEBUG=UNSAFE flag is set in the environment.
80 */
81bool
83{
84 const char *env = getenv("PGOAUTHDEBUG");
85
86 return (env && strcmp(env, "UNSAFE") == 0);
87}
88
89/*
90 * Duplicate SOCK_ERRNO* definitions from libpq-int.h, for use by
91 * pq_block/reset_sigpipe().
92 */
93#ifdef WIN32
94#define SOCK_ERRNO (WSAGetLastError())
95#define SOCK_ERRNO_SET(e) WSASetLastError(e)
96#else
97#define SOCK_ERRNO errno
98#define SOCK_ERRNO_SET(e) (errno = (e))
99#endif
100
101/*
102 * Block SIGPIPE for this thread. This is a copy of libpq's internal API.
103 */
104int
105pq_block_sigpipe(sigset_t *osigset, bool *sigpipe_pending)
106{
109
112
113 /* Block SIGPIPE and save previous mask for later reset */
115 if (SOCK_ERRNO)
116 return -1;
117
118 /* We can have a pending SIGPIPE only if it was blocked before */
120 {
121 /* Is there a pending SIGPIPE? */
122 if (sigpending(&sigset) != 0)
123 return -1;
124
126 *sigpipe_pending = true;
127 else
128 *sigpipe_pending = false;
129 }
130 else
131 *sigpipe_pending = false;
132
133 return 0;
134}
135
136/*
137 * Discard any pending SIGPIPE and reset the signal mask. This is a copy of
138 * libpq's internal API.
139 */
140void
141pq_reset_sigpipe(sigset_t *osigset, bool sigpipe_pending, bool got_epipe)
142{
144 int signo;
146
147 /* Clear SIGPIPE only if none was pending */
148 if (got_epipe && !sigpipe_pending)
149 {
150 if (sigpending(&sigset) == 0 &&
152 {
154
157
159 }
160 }
161
162 /* Restore saved block mask */
164
166}
#define unconstify(underlying_type, expr)
Definition c.h:1327
pgthreadlock_t PQgetThreadLock(void)
void(* pgthreadlock_t)(int acquire)
Definition libpq-fe.h:477
void pq_reset_sigpipe(sigset_t *osigset, bool sigpipe_pending, bool got_epipe)
void libpq_oauth_init(libpq_gettext_func gettext_impl)
Definition oauth-utils.c:46
#define SOCK_ERRNO
Definition oauth-utils.c:97
int pq_block_sigpipe(sigset_t *osigset, bool *sigpipe_pending)
static libpq_gettext_func libpq_gettext_impl
Definition oauth-utils.c:36
pgthreadlock_t pg_g_threadlock
Definition oauth-utils.c:35
#define SOCK_ERRNO_SET(e)
Definition oauth-utils.c:98
bool oauth_unsafe_debugging_enabled(void)
Definition oauth-utils.c:82
#define libpq_gettext(x)
Definition oauth-utils.h:45
char *(* libpq_gettext_func)(const char *msgid)
Definition oauth-utils.h:21
static int fb(int x)
#define SIGPIPE
Definition win32_port.h:163