PostgreSQL Source Code  git master
be-secure.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * be-secure.c
4  * functions related to setting up a secure connection to the frontend.
5  * Secure connections are expected to provide confidentiality,
6  * message integrity and endpoint authentication.
7  *
8  *
9  * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
10  * Portions Copyright (c) 1994, Regents of the University of California
11  *
12  *
13  * IDENTIFICATION
14  * src/backend/libpq/be-secure.c
15  *
16  *-------------------------------------------------------------------------
17  */
18 
19 #include "postgres.h"
20 
21 #include <signal.h>
22 #include <fcntl.h>
23 #include <ctype.h>
24 #include <sys/socket.h>
25 #include <netdb.h>
26 #include <netinet/in.h>
27 #include <netinet/tcp.h>
28 #include <arpa/inet.h>
29 
30 #include "libpq/libpq.h"
31 #include "miscadmin.h"
32 #include "tcop/tcopprot.h"
33 #include "utils/wait_event.h"
34 
44 
45 #ifdef USE_SSL
46 bool ssl_loaded_verify_locations = false;
47 #endif
48 
49 /* GUC variable controlling SSL cipher list */
50 char *SSLCipherSuites = NULL;
51 
52 /* GUC variable for default ECHD curve. */
54 
55 /* GUC variable: if false, prefer client ciphers */
57 
60 
61 /* ------------------------------------------------------------ */
62 /* Procedures common to all secure sessions */
63 /* ------------------------------------------------------------ */
64 
65 /*
66  * Initialize global context.
67  *
68  * If isServerStart is true, report any errors as FATAL (so we don't return).
69  * Otherwise, log errors at LOG level and return -1 to indicate trouble,
70  * preserving the old SSL state if any. Returns 0 if OK.
71  */
72 int
73 secure_initialize(bool isServerStart)
74 {
75 #ifdef USE_SSL
76  return be_tls_init(isServerStart);
77 #else
78  return 0;
79 #endif
80 }
81 
82 /*
83  * Destroy global context, if any.
84  */
85 void
87 {
88 #ifdef USE_SSL
90 #endif
91 }
92 
93 /*
94  * Indicate if we have loaded the root CA store to verify certificates
95  */
96 bool
98 {
99 #ifdef USE_SSL
100  return ssl_loaded_verify_locations;
101 #else
102  return false;
103 #endif
104 }
105 
106 /*
107  * Attempt to negotiate secure session.
108  */
109 int
111 {
112  int r = 0;
113 
114 #ifdef USE_SSL
116 
117  ereport(DEBUG2,
118  (errmsg_internal("SSL connection from DN:\"%s\" CN:\"%s\"",
119  port->peer_dn ? port->peer_dn : "(anonymous)",
120  port->peer_cn ? port->peer_cn : "(anonymous)")));
121 #endif
122 
123  return r;
124 }
125 
126 /*
127  * Close secure session.
128  */
129 void
131 {
132 #ifdef USE_SSL
133  if (port->ssl_in_use)
135 #endif
136 }
137 
138 /*
139  * Read data from a secure connection.
140  */
141 ssize_t
142 secure_read(Port *port, void *ptr, size_t len)
143 {
144  ssize_t n;
145  int waitfor;
146 
147  /* Deal with any already-pending interrupt condition. */
149 
150 retry:
151 #ifdef USE_SSL
152  waitfor = 0;
153  if (port->ssl_in_use)
154  {
155  n = be_tls_read(port, ptr, len, &waitfor);
156  }
157  else
158 #endif
159 #ifdef ENABLE_GSS
160  if (port->gss && port->gss->enc)
161  {
162  n = be_gssapi_read(port, ptr, len);
163  waitfor = WL_SOCKET_READABLE;
164  }
165  else
166 #endif
167  {
168  n = secure_raw_read(port, ptr, len);
169  waitfor = WL_SOCKET_READABLE;
170  }
171 
172  /* In blocking mode, wait until the socket is ready */
173  if (n < 0 && !port->noblock && (errno == EWOULDBLOCK || errno == EAGAIN))
174  {
175  WaitEvent event;
176 
177  Assert(waitfor);
178 
180 
181  WaitEventSetWait(FeBeWaitSet, -1 /* no timeout */ , &event, 1,
182  WAIT_EVENT_CLIENT_READ);
183 
184  /*
185  * If the postmaster has died, it's not safe to continue running,
186  * because it is the postmaster's job to kill us if some other backend
187  * exits uncleanly. Moreover, we won't run very well in this state;
188  * helper processes like walwriter and the bgwriter will exit, so
189  * performance may be poor. Finally, if we don't exit, pg_ctl will be
190  * unable to restart the postmaster without manual intervention, so no
191  * new connections can be accepted. Exiting clears the deck for a
192  * postmaster restart.
193  *
194  * (Note that we only make this check when we would otherwise sleep on
195  * our latch. We might still continue running for a while if the
196  * postmaster is killed in mid-query, or even through multiple queries
197  * if we never have to wait for read. We don't want to burn too many
198  * cycles checking for this very rare condition, and this should cause
199  * us to exit quickly in most cases.)
200  */
201  if (event.events & WL_POSTMASTER_DEATH)
202  ereport(FATAL,
203  (errcode(ERRCODE_ADMIN_SHUTDOWN),
204  errmsg("terminating connection due to unexpected postmaster exit")));
205 
206  /* Handle interrupt. */
207  if (event.events & WL_LATCH_SET)
208  {
211 
212  /*
213  * We'll retry the read. Most likely it will return immediately
214  * because there's still no data available, and we'll wait for the
215  * socket to become ready again.
216  */
217  }
218  goto retry;
219  }
220 
221  /*
222  * Process interrupts that happened during a successful (or non-blocking,
223  * or hard-failed) read.
224  */
226 
227  return n;
228 }
229 
230 ssize_t
231 secure_raw_read(Port *port, void *ptr, size_t len)
232 {
233  ssize_t n;
234 
235  /*
236  * Try to read from the socket without blocking. If it succeeds we're
237  * done, otherwise we'll wait for the socket using the latch mechanism.
238  */
239 #ifdef WIN32
240  pgwin32_noblock = true;
241 #endif
242  n = recv(port->sock, ptr, len, 0);
243 #ifdef WIN32
244  pgwin32_noblock = false;
245 #endif
246 
247  return n;
248 }
249 
250 
251 /*
252  * Write data to a secure connection.
253  */
254 ssize_t
255 secure_write(Port *port, void *ptr, size_t len)
256 {
257  ssize_t n;
258  int waitfor;
259 
260  /* Deal with any already-pending interrupt condition. */
262 
263 retry:
264  waitfor = 0;
265 #ifdef USE_SSL
266  if (port->ssl_in_use)
267  {
268  n = be_tls_write(port, ptr, len, &waitfor);
269  }
270  else
271 #endif
272 #ifdef ENABLE_GSS
273  if (port->gss && port->gss->enc)
274  {
275  n = be_gssapi_write(port, ptr, len);
276  waitfor = WL_SOCKET_WRITEABLE;
277  }
278  else
279 #endif
280  {
281  n = secure_raw_write(port, ptr, len);
282  waitfor = WL_SOCKET_WRITEABLE;
283  }
284 
285  if (n < 0 && !port->noblock && (errno == EWOULDBLOCK || errno == EAGAIN))
286  {
287  WaitEvent event;
288 
289  Assert(waitfor);
290 
292 
293  WaitEventSetWait(FeBeWaitSet, -1 /* no timeout */ , &event, 1,
294  WAIT_EVENT_CLIENT_WRITE);
295 
296  /* See comments in secure_read. */
297  if (event.events & WL_POSTMASTER_DEATH)
298  ereport(FATAL,
299  (errcode(ERRCODE_ADMIN_SHUTDOWN),
300  errmsg("terminating connection due to unexpected postmaster exit")));
301 
302  /* Handle interrupt. */
303  if (event.events & WL_LATCH_SET)
304  {
307 
308  /*
309  * We'll retry the write. Most likely it will return immediately
310  * because there's still no buffer space available, and we'll wait
311  * for the socket to become ready again.
312  */
313  }
314  goto retry;
315  }
316 
317  /*
318  * Process interrupts that happened during a successful (or non-blocking,
319  * or hard-failed) write.
320  */
322 
323  return n;
324 }
325 
326 ssize_t
327 secure_raw_write(Port *port, const void *ptr, size_t len)
328 {
329  ssize_t n;
330 
331 #ifdef WIN32
332  pgwin32_noblock = true;
333 #endif
334  n = send(port->sock, ptr, len, 0);
335 #ifdef WIN32
336  pgwin32_noblock = false;
337 #endif
338 
339  return n;
340 }
ssize_t be_gssapi_read(Port *port, void *ptr, size_t len)
ssize_t be_gssapi_write(Port *port, void *ptr, size_t len)
void be_tls_destroy(void)
int be_tls_init(bool isServerStart)
int be_tls_open_server(Port *port)
void be_tls_close(Port *port)
ssize_t be_tls_read(Port *port, void *ptr, size_t len, int *waitfor)
ssize_t be_tls_write(Port *port, void *ptr, size_t len, int *waitfor)
void secure_destroy(void)
Definition: be-secure.c:86
char * ssl_crl_dir
Definition: be-secure.c:40
char * ssl_dh_params_file
Definition: be-secure.c:41
int ssl_min_protocol_version
Definition: be-secure.c:58
ssize_t secure_raw_read(Port *port, void *ptr, size_t len)
Definition: be-secure.c:231
int secure_initialize(bool isServerStart)
Definition: be-secure.c:73
char * ssl_cert_file
Definition: be-secure.c:36
bool SSLPreferServerCiphers
Definition: be-secure.c:56
char * ssl_library
Definition: be-secure.c:35
int ssl_max_protocol_version
Definition: be-secure.c:59
char * ssl_passphrase_command
Definition: be-secure.c:42
ssize_t secure_write(Port *port, void *ptr, size_t len)
Definition: be-secure.c:255
int secure_open_server(Port *port)
Definition: be-secure.c:110
bool ssl_passphrase_command_supports_reload
Definition: be-secure.c:43
char * SSLCipherSuites
Definition: be-secure.c:50
bool secure_loaded_verify_locations(void)
Definition: be-secure.c:97
char * SSLECDHCurve
Definition: be-secure.c:53
char * ssl_key_file
Definition: be-secure.c:37
void secure_close(Port *port)
Definition: be-secure.c:130
ssize_t secure_read(Port *port, void *ptr, size_t len)
Definition: be-secure.c:142
ssize_t secure_raw_write(Port *port, const void *ptr, size_t len)
Definition: be-secure.c:327
char * ssl_crl_file
Definition: be-secure.c:39
char * ssl_ca_file
Definition: be-secure.c:38
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1159
int errcode(int sqlerrcode)
Definition: elog.c:859
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#define FATAL
Definition: elog.h:41
#define DEBUG2
Definition: elog.h:29
#define ereport(elevel,...)
Definition: elog.h:149
struct Latch * MyLatch
Definition: globals.c:60
void ModifyWaitEvent(WaitEventSet *set, int pos, uint32 events, Latch *latch)
Definition: latch.c:1049
int WaitEventSetWait(WaitEventSet *set, long timeout, WaitEvent *occurred_events, int nevents, uint32 wait_event_info)
Definition: latch.c:1424
void ResetLatch(Latch *latch)
Definition: latch.c:724
#define WL_SOCKET_READABLE
Definition: latch.h:128
#define WL_LATCH_SET
Definition: latch.h:127
#define WL_POSTMASTER_DEATH
Definition: latch.h:131
#define WL_SOCKET_WRITEABLE
Definition: latch.h:129
@ PG_TLS1_2_VERSION
Definition: libpq.h:131
@ PG_TLS_ANY
Definition: libpq.h:128
#define FeBeWaitSetSocketPos
Definition: libpq.h:63
Assert(fmt[strlen(fmt) - 1] !='\n')
const void size_t len
static int port
Definition: pg_regress.c:116
void ProcessClientReadInterrupt(bool blocked)
Definition: postgres.c:509
void ProcessClientWriteInterrupt(bool blocked)
Definition: postgres.c:555
WaitEventSet * FeBeWaitSet
Definition: pqcomm.c:163
int pgwin32_noblock
Definition: socket.c:28
Definition: libpq-be.h:133
uint32 events
Definition: latch.h:155
#define EWOULDBLOCK
Definition: win32_port.h:380
#define recv(s, buf, len, flags)
Definition: win32_port.h:496
#define send(s, buf, len, flags)
Definition: win32_port.h:497
#define EAGAIN
Definition: win32_port.h:372