PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
libpq-be.h
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * libpq-be.h
4 * This file contains definitions for structures and externs used
5 * by the postmaster during client authentication.
6 *
7 * Note that this is backend-internal and is NOT exported to clients.
8 * Structs that need to be client-visible are in pqcomm.h.
9 *
10 *
11 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
12 * Portions Copyright (c) 1994, Regents of the University of California
13 *
14 * src/include/libpq/libpq-be.h
15 *
16 *-------------------------------------------------------------------------
17 */
18#ifndef LIBPQ_BE_H
19#define LIBPQ_BE_H
20
21#include "common/scram-common.h"
22
23#include <sys/time.h>
24#ifdef USE_OPENSSL
25#include <openssl/ssl.h>
26#include <openssl/err.h>
27#endif
28#include <netinet/tcp.h>
29
30#include "libpq/pg-gssapi.h"
31
32#ifdef ENABLE_SSPI
33#define SECURITY_WIN32
34#if defined(WIN32) && !defined(_MSC_VER)
35#include <ntsecapi.h>
36#endif
37#include <security.h>
38#undef SECURITY_WIN32
39
40#ifndef ENABLE_GSS
41/*
42 * Define a fake structure compatible with GSSAPI on Unix.
43 */
44typedef struct
45{
46 void *value;
47 int length;
48} gss_buffer_desc;
49#endif
50#endif /* ENABLE_SSPI */
51
52#include "datatype/timestamp.h"
53#include "libpq/hba.h"
54#include "libpq/pqcomm.h"
55
56
57/*
58 * GSSAPI specific state information
59 */
60#if defined(ENABLE_GSS) | defined(ENABLE_SSPI)
61typedef struct
62{
63 gss_buffer_desc outbuf; /* GSSAPI output token buffer */
64#ifdef ENABLE_GSS
65 gss_cred_id_t cred; /* GSSAPI connection cred's */
66 gss_ctx_id_t ctx; /* GSSAPI connection context */
67 gss_name_t name; /* GSSAPI client name */
68 char *princ; /* GSSAPI Principal used for auth, NULL if
69 * GSSAPI auth was not used */
70 bool auth; /* GSSAPI Authentication used */
71 bool enc; /* GSSAPI encryption in use */
72 bool delegated_creds; /* GSSAPI Delegated credentials */
73#endif
74} pg_gssinfo;
75#endif
76
77/*
78 * ClientConnectionInfo includes the fields describing the client connection
79 * that are copied over to parallel workers as nothing from Port does that.
80 * The same rules apply for allocations here as for Port (everything must be
81 * malloc'd or palloc'd in TopMemoryContext).
82 *
83 * If you add a struct member here, remember to also handle serialization in
84 * SerializeClientConnectionInfo() and co.
85 */
87{
88 /*
89 * Authenticated identity. The meaning of this identifier is dependent on
90 * auth_method; it is the identity (if any) that the user presented during
91 * the authentication cycle, before they were assigned a database role.
92 * (It is effectively the "SYSTEM-USERNAME" of a pg_ident usermap --
93 * though the exact string in use may be different, depending on pg_hba
94 * options.)
95 *
96 * authn_id is NULL if the user has not actually been authenticated, for
97 * example if the "trust" auth method is in use.
98 */
99 const char *authn_id;
100
101 /*
102 * The HBA method that determined the above authn_id. This only has
103 * meaning if authn_id is not NULL; otherwise it's undefined.
104 */
107
108/*
109 * The Port structure holds state information about a client connection in a
110 * backend process. It is available in the global variable MyProcPort. The
111 * struct and all the data it points are kept in TopMemoryContext.
112 *
113 * remote_hostname is set if we did a successful reverse lookup of the
114 * client's IP address during connection setup.
115 * remote_hostname_resolv tracks the state of hostname verification:
116 * +1 = remote_hostname is known to resolve to client's IP address
117 * -1 = remote_hostname is known NOT to resolve to client's IP address
118 * 0 = we have not done the forward DNS lookup yet
119 * -2 = there was an error in name resolution
120 * If reverse lookup of the client IP address fails, remote_hostname will be
121 * left NULL while remote_hostname_resolv is set to -2. If reverse lookup
122 * succeeds but forward lookup fails, remote_hostname_resolv is also set to -2
123 * (the case is distinguishable because remote_hostname isn't NULL). In
124 * either of the -2 cases, remote_hostname_errcode saves the lookup return
125 * code for possible later use with gai_strerror.
126 */
127
128typedef struct Port
129{
130 pgsocket sock; /* File descriptor */
131 bool noblock; /* is the socket in non-blocking mode? */
132 ProtocolVersion proto; /* FE/BE protocol version */
133 SockAddr laddr; /* local addr (postmaster) */
134 SockAddr raddr; /* remote addr (client) */
135 char *remote_host; /* name (or ip addr) of remote host */
136 char *remote_hostname; /* name (not ip addr) of remote host, if
137 * available */
138 int remote_hostname_resolv; /* see above */
139 int remote_hostname_errcode; /* see above */
140 char *remote_port; /* text rep of remote port */
141
142 /* local_host is filled only if needed (see log_status_format) */
143 char local_host[64]; /* ip addr of local socket for client conn */
144
145 /*
146 * Information that needs to be saved from the startup packet and passed
147 * into backend execution. "char *" fields are NULL if not set.
148 * guc_options points to a List of alternating option names and values.
149 */
154
155 /*
156 * The startup packet application name, only used here for the "connection
157 * authorized" log message. We shouldn't use this post-startup, instead
158 * the GUC should be used as application can change it afterward.
159 */
161
162 /*
163 * Information that needs to be held during the authentication cycle.
164 */
166
167 /*
168 * TCP keepalive and user timeout settings.
169 *
170 * default values are 0 if AF_UNIX or not yet known; current values are 0
171 * if AF_UNIX or using the default. Also, -1 in a default value means we
172 * were unable to find out the default (getsockopt failed).
173 */
182
183 /*
184 * SCRAM structures.
185 */
188 bool has_scram_keys; /* true if the above two are valid */
189
190 /*
191 * GSSAPI structures.
192 */
193#if defined(ENABLE_GSS) || defined(ENABLE_SSPI)
194
195 /*
196 * If GSSAPI is supported and used on this connection, store GSSAPI
197 * information. Even when GSSAPI is not compiled in, store a NULL pointer
198 * to keep struct offsets the same (for extension ABI compatibility).
199 */
200 pg_gssinfo *gss;
201#else
202 void *gss;
203#endif
204
205 /*
206 * SSL structures.
207 */
209 char *peer_cn;
210 char *peer_dn;
214
215 /*
216 * OpenSSL structures. As with GSSAPI above, to keep struct offsets
217 * constant, NULL pointers are stored when SSL support is not enabled.
218 * (Although extensions should have no business accessing the raw_buf
219 * fields anyway.)
220 */
221#ifdef USE_OPENSSL
222 SSL *ssl;
223 X509 *peer;
224#else
225 void *ssl;
226 void *peer;
227#endif
228
229 /*
230 * This is a bit of a hack. raw_buf is data that was previously read and
231 * buffered in a higher layer but then "unread" and needs to be read again
232 * while establishing an SSL connection via the SSL library layer.
233 *
234 * There's no API to "unread", the upper layer just places the data in the
235 * Port structure in raw_buf and sets raw_buf_remaining to the amount of
236 * bytes unread and raw_buf_consumed to 0.
237 */
238 char *raw_buf;
242
243/*
244 * ClientSocket holds a socket for an accepted connection, along with the
245 * information about the remote endpoint. This is passed from postmaster to
246 * the backend process.
247 */
248typedef struct ClientSocket
249{
250 pgsocket sock; /* File descriptor */
251 SockAddr raddr; /* remote addr (client) */
253
254#ifdef USE_SSL
255/*
256 * Hardcoded DH parameters, used in ephemeral DH keying. (See also
257 * README.SSL for more details on EDH.)
258 *
259 * This is the 2048-bit DH parameter from RFC 3526. The generation of the
260 * prime is specified in RFC 2412 Appendix E, which also discusses the
261 * design choice of the generator. Note that when loaded with OpenSSL
262 * this causes DH_check() to fail on DH_NOT_SUITABLE_GENERATOR, where
263 * leaking a bit is preferred.
264 */
265#define FILE_DH2048 \
266"-----BEGIN DH PARAMETERS-----\n\
267MIIBCAKCAQEA///////////JD9qiIWjCNMTGYouA3BzRKQJOCIpnzHQCC76mOxOb\n\
268IlFKCHmONATd75UZs806QxswKwpt8l8UN0/hNW1tUcJF5IW1dmJefsb0TELppjft\n\
269awv/XLb0Brft7jhr+1qJn6WunyQRfEsf5kkoZlHs5Fs9wgB8uKFjvwWY2kg2HFXT\n\
270mmkWP6j9JM9fg2VdI9yjrZYcYvNWIIVSu57VKQdwlpZtZww1Tkq8mATxdGwIyhgh\n\
271fDKQXkYuNs474553LBgOhgObJ4Oi7Aeij7XFXfBvTFLJ3ivL9pVYFxg5lUl86pVq\n\
2725RXSJhiY+gUQFXKOWoqsqmj//////////wIBAg==\n\
273-----END DH PARAMETERS-----\n"
274
275/*
276 * These functions are implemented by the glue code specific to each
277 * SSL implementation (e.g. be-secure-openssl.c)
278 */
279
280/*
281 * Initialize global SSL context.
282 *
283 * If isServerStart is true, report any errors as FATAL (so we don't return).
284 * Otherwise, log errors at LOG level and return -1 to indicate trouble,
285 * preserving the old SSL state if any. Returns 0 if OK.
286 */
287extern int be_tls_init(bool isServerStart);
288
289/*
290 * Destroy global SSL context, if any.
291 */
292extern void be_tls_destroy(void);
293
294/*
295 * Attempt to negotiate SSL connection.
296 */
297extern int be_tls_open_server(Port *port);
298
299/*
300 * Close SSL connection.
301 */
302extern void be_tls_close(Port *port);
303
304/*
305 * Read data from a secure connection.
306 */
307extern ssize_t be_tls_read(Port *port, void *ptr, size_t len, int *waitfor);
308
309/*
310 * Write data to a secure connection.
311 */
312extern ssize_t be_tls_write(Port *port, const void *ptr, size_t len, int *waitfor);
313
314/*
315 * Return information about the SSL connection.
316 */
317extern int be_tls_get_cipher_bits(Port *port);
318extern const char *be_tls_get_version(Port *port);
319extern const char *be_tls_get_cipher(Port *port);
320extern void be_tls_get_peer_subject_name(Port *port, char *ptr, size_t len);
321extern void be_tls_get_peer_issuer_name(Port *port, char *ptr, size_t len);
322extern void be_tls_get_peer_serial(Port *port, char *ptr, size_t len);
323
324/*
325 * Get the server certificate hash for SCRAM channel binding type
326 * tls-server-end-point.
327 *
328 * The result is a palloc'd hash of the server certificate with its
329 * size, and NULL if there is no certificate available.
330 */
331extern char *be_tls_get_certificate_hash(Port *port, size_t *len);
332
333/* init hook for SSL, the default sets the password callback if appropriate */
334#ifdef USE_OPENSSL
335typedef void (*openssl_tls_init_hook_typ) (SSL_CTX *context, bool isServerStart);
336extern PGDLLIMPORT openssl_tls_init_hook_typ openssl_tls_init_hook;
337#endif
338
339#endif /* USE_SSL */
340
341#ifdef ENABLE_GSS
342/*
343 * Return information about the GSSAPI authenticated connection
344 */
345extern bool be_gssapi_get_auth(Port *port);
346extern bool be_gssapi_get_enc(Port *port);
347extern const char *be_gssapi_get_princ(Port *port);
349
350/* Read and write to a GSSAPI-encrypted connection. */
351extern ssize_t be_gssapi_read(Port *port, void *ptr, size_t len);
352extern ssize_t be_gssapi_write(Port *port, const void *ptr, size_t len);
353#endif /* ENABLE_GSS */
354
357
358/* TCP keepalives configuration. These are no-ops on an AF_UNIX socket. */
359
360extern int pq_getkeepalivesidle(Port *port);
362extern int pq_getkeepalivescount(Port *port);
363extern int pq_gettcpusertimeout(Port *port);
364
365extern int pq_setkeepalivesidle(int idle, Port *port);
367extern int pq_setkeepalivescount(int count, Port *port);
368extern int pq_settcpusertimeout(int timeout, Port *port);
369
370#endif /* LIBPQ_BE_H */
ssize_t be_gssapi_write(Port *port, const void *ptr, size_t len)
bool be_gssapi_get_auth(Port *port)
ssize_t be_gssapi_read(Port *port, void *ptr, size_t len)
bool be_gssapi_get_enc(Port *port)
const char * be_gssapi_get_princ(Port *port)
bool be_gssapi_get_delegation(Port *port)
const char * be_tls_get_version(Port *port)
ssize_t be_tls_write(Port *port, const void *ptr, size_t len, int *waitfor)
void be_tls_destroy(void)
int be_tls_init(bool isServerStart)
openssl_tls_init_hook_typ openssl_tls_init_hook
int be_tls_get_cipher_bits(Port *port)
int be_tls_open_server(Port *port)
char * be_tls_get_certificate_hash(Port *port, size_t *len)
const char * be_tls_get_cipher(Port *port)
void be_tls_get_peer_serial(Port *port, char *ptr, size_t len)
void be_tls_close(Port *port)
void be_tls_get_peer_issuer_name(Port *port, char *ptr, size_t len)
ssize_t be_tls_read(Port *port, void *ptr, size_t len, int *waitfor)
void be_tls_get_peer_subject_name(Port *port, char *ptr, size_t len)
#define PGDLLIMPORT
Definition: c.h:1291
uint8_t uint8
Definition: c.h:500
enc
UserAuth
Definition: hba.h:26
static struct @165 value
int pq_setkeepalivesinterval(int interval, Port *port)
Definition: pqcomm.c:1753
PGDLLIMPORT ProtocolVersion FrontendProtocol
Definition: globals.c:30
int pq_getkeepalivescount(Port *port)
Definition: pqcomm.c:1802
int pq_getkeepalivesinterval(Port *port)
Definition: pqcomm.c:1718
int pq_settcpusertimeout(int timeout, Port *port)
Definition: pqcomm.c:1907
int pq_setkeepalivesidle(int idle, Port *port)
Definition: pqcomm.c:1668
int pq_getkeepalivesidle(Port *port)
Definition: pqcomm.c:1633
struct Port Port
struct ClientSocket ClientSocket
PGDLLIMPORT ClientConnectionInfo MyClientConnectionInfo
Definition: miscinit.c:1069
struct ClientConnectionInfo ClientConnectionInfo
int pq_gettcpusertimeout(Port *port)
Definition: pqcomm.c:1877
int pq_setkeepalivescount(int count, Port *port)
Definition: pqcomm.c:1832
const void size_t len
static int port
Definition: pg_regress.c:115
int pgsocket
Definition: port.h:29
uint32 ProtocolVersion
Definition: pqcomm.h:99
#define SCRAM_MAX_KEY_LEN
Definition: scram-common.h:30
const char * authn_id
Definition: libpq-be.h:99
UserAuth auth_method
Definition: libpq-be.h:105
SockAddr raddr
Definition: libpq-be.h:251
pgsocket sock
Definition: libpq-be.h:250
Definition: hba.h:96
Definition: pg_list.h:54
Definition: libpq-be.h:129
bool ssl_in_use
Definition: libpq-be.h:208
char * user_name
Definition: libpq-be.h:151
ProtocolVersion proto
Definition: libpq-be.h:132
bool last_read_was_eof
Definition: libpq-be.h:213
char * raw_buf
Definition: libpq-be.h:238
char * remote_port
Definition: libpq-be.h:140
SockAddr laddr
Definition: libpq-be.h:133
char * remote_hostname
Definition: libpq-be.h:136
int remote_hostname_errcode
Definition: libpq-be.h:139
int default_keepalives_idle
Definition: libpq-be.h:174
int keepalives_idle
Definition: libpq-be.h:178
uint8 scram_ServerKey[SCRAM_MAX_KEY_LEN]
Definition: libpq-be.h:187
int keepalives_count
Definition: libpq-be.h:180
void * gss
Definition: libpq-be.h:202
int default_keepalives_interval
Definition: libpq-be.h:175
bool peer_cert_valid
Definition: libpq-be.h:211
char * database_name
Definition: libpq-be.h:150
void * peer
Definition: libpq-be.h:226
char * remote_host
Definition: libpq-be.h:135
bool alpn_used
Definition: libpq-be.h:212
pgsocket sock
Definition: libpq-be.h:130
void * ssl
Definition: libpq-be.h:225
HbaLine * hba
Definition: libpq-be.h:165
ssize_t raw_buf_remaining
Definition: libpq-be.h:240
char * peer_cn
Definition: libpq-be.h:209
int default_keepalives_count
Definition: libpq-be.h:176
char * cmdline_options
Definition: libpq-be.h:152
int keepalives_interval
Definition: libpq-be.h:179
SockAddr raddr
Definition: libpq-be.h:134
int remote_hostname_resolv
Definition: libpq-be.h:138
List * guc_options
Definition: libpq-be.h:153
char * peer_dn
Definition: libpq-be.h:210
bool noblock
Definition: libpq-be.h:131
char * application_name
Definition: libpq-be.h:160
char local_host[64]
Definition: libpq-be.h:143
int default_tcp_user_timeout
Definition: libpq-be.h:177
bool has_scram_keys
Definition: libpq-be.h:188
uint8 scram_ClientKey[SCRAM_MAX_KEY_LEN]
Definition: libpq-be.h:186
int tcp_user_timeout
Definition: libpq-be.h:181
ssize_t raw_buf_consumed
Definition: libpq-be.h:239
const char * name