PostgreSQL Source Code  git master
be-secure.c File Reference
#include "postgres.h"
#include <signal.h>
#include <fcntl.h>
#include <ctype.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include "libpq/libpq.h"
#include "miscadmin.h"
#include "tcop/tcopprot.h"
#include "utils/wait_event.h"
Include dependency graph for be-secure.c:

Go to the source code of this file.

Functions

int secure_initialize (bool isServerStart)
 
void secure_destroy (void)
 
bool secure_loaded_verify_locations (void)
 
int secure_open_server (Port *port)
 
void secure_close (Port *port)
 
ssize_t secure_read (Port *port, void *ptr, size_t len)
 
ssize_t secure_raw_read (Port *port, void *ptr, size_t len)
 
ssize_t secure_write (Port *port, void *ptr, size_t len)
 
ssize_t secure_raw_write (Port *port, const void *ptr, size_t len)
 

Variables

char * ssl_library
 
char * ssl_cert_file
 
char * ssl_key_file
 
char * ssl_ca_file
 
char * ssl_crl_file
 
char * ssl_crl_dir
 
char * ssl_dh_params_file
 
char * ssl_passphrase_command
 
bool ssl_passphrase_command_supports_reload
 
char * SSLCipherSuites = NULL
 
char * SSLECDHCurve
 
bool SSLPreferServerCiphers
 
int ssl_min_protocol_version = PG_TLS1_2_VERSION
 
int ssl_max_protocol_version = PG_TLS_ANY
 

Function Documentation

◆ secure_close()

void secure_close ( Port port)

Definition at line 163 of file be-secure.c.

164 {
165 #ifdef USE_SSL
166  if (port->ssl_in_use)
168 #endif
169 }
void be_tls_close(Port *port)
static int port
Definition: pg_regress.c:116

References be_tls_close(), and port.

Referenced by socket_close().

◆ secure_destroy()

void secure_destroy ( void  )

Definition at line 86 of file be-secure.c.

87 {
88 #ifdef USE_SSL
90 #endif
91 }
void be_tls_destroy(void)

References be_tls_destroy().

Referenced by process_pm_reload_request().

◆ secure_initialize()

int secure_initialize ( bool  isServerStart)

Definition at line 73 of file be-secure.c.

74 {
75 #ifdef USE_SSL
76  return be_tls_init(isServerStart);
77 #else
78  return 0;
79 #endif
80 }
int be_tls_init(bool isServerStart)

References be_tls_init().

Referenced by BackendMain(), PostmasterMain(), and process_pm_reload_request().

◆ secure_loaded_verify_locations()

bool secure_loaded_verify_locations ( void  )

Definition at line 97 of file be-secure.c.

98 {
99 #ifdef USE_SSL
100  return ssl_loaded_verify_locations;
101 #else
102  return false;
103 #endif
104 }

Referenced by ClientAuthentication().

◆ secure_open_server()

int secure_open_server ( Port port)

Definition at line 110 of file be-secure.c.

111 {
112 #ifdef USE_SSL
113  int r = 0;
114  ssize_t len;
115 
116  /* push unencrypted buffered data back through SSL setup */
118  if (len > 0)
119  {
120  char *buf = palloc(len);
121 
122  pq_startmsgread();
123  if (pq_getbytes(buf, len) == EOF)
124  return STATUS_ERROR; /* shouldn't be possible */
125  pq_endmsgread();
126  port->raw_buf = buf;
127  port->raw_buf_remaining = len;
128  port->raw_buf_consumed = 0;
129  }
131 
133 
134  if (port->raw_buf_remaining > 0)
135  {
136  /*
137  * This shouldn't be possible -- it would mean the client sent
138  * encrypted data before we established a session key...
139  */
140  elog(LOG, "buffered unencrypted data remains after negotiating SSL connection");
141  return STATUS_ERROR;
142  }
143  if (port->raw_buf != NULL)
144  {
145  pfree(port->raw_buf);
146  port->raw_buf = NULL;
147  }
148 
149  ereport(DEBUG2,
150  (errmsg_internal("SSL connection from DN:\"%s\" CN:\"%s\"",
151  port->peer_dn ? port->peer_dn : "(anonymous)",
152  port->peer_cn ? port->peer_cn : "(anonymous)")));
153  return r;
154 #else
155  return 0;
156 #endif
157 }
int be_tls_open_server(Port *port)
#define Assert(condition)
Definition: c.h:858
#define STATUS_ERROR
Definition: c.h:1170
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1159
#define LOG
Definition: elog.h:31
#define DEBUG2
Definition: elog.h:29
#define elog(elevel,...)
Definition: elog.h:224
#define ereport(elevel,...)
Definition: elog.h:149
void pfree(void *pointer)
Definition: mcxt.c:1520
void * palloc(Size size)
Definition: mcxt.c:1316
const void size_t len
static char * buf
Definition: pg_test_fsync.c:73
int pq_getbytes(char *s, size_t len)
Definition: pqcomm.c:1062
ssize_t pq_buffer_remaining_data(void)
Definition: pqcomm.c:1126
void pq_endmsgread(void)
Definition: pqcomm.c:1164
void pq_startmsgread(void)
Definition: pqcomm.c:1140

References Assert, be_tls_open_server(), buf, DEBUG2, elog, ereport, errmsg_internal(), len, LOG, palloc(), pfree(), port, pq_buffer_remaining_data(), pq_endmsgread(), pq_getbytes(), pq_startmsgread(), and STATUS_ERROR.

Referenced by ProcessSSLStartup(), and ProcessStartupPacket().

◆ secure_raw_read()

ssize_t secure_raw_read ( Port port,
void *  ptr,
size_t  len 
)

Definition at line 264 of file be-secure.c.

265 {
266  ssize_t n;
267 
268  /* Read from the "unread" buffered data first. c.f. libpq-be.h */
269  if (port->raw_buf_remaining > 0)
270  {
271  /* consume up to len bytes from the raw_buf */
272  if (len > port->raw_buf_remaining)
273  len = port->raw_buf_remaining;
274  Assert(port->raw_buf);
275  memcpy(ptr, port->raw_buf + port->raw_buf_consumed, len);
276  port->raw_buf_consumed += len;
277  port->raw_buf_remaining -= len;
278  return len;
279  }
280 
281  /*
282  * Try to read from the socket without blocking. If it succeeds we're
283  * done, otherwise we'll wait for the socket using the latch mechanism.
284  */
285 #ifdef WIN32
286  pgwin32_noblock = true;
287 #endif
288  n = recv(port->sock, ptr, len, 0);
289 #ifdef WIN32
290  pgwin32_noblock = false;
291 #endif
292 
293  return n;
294 }
int pgwin32_noblock
Definition: socket.c:28
#define recv(s, buf, len, flags)
Definition: win32_port.h:496

References Assert, len, pgwin32_noblock, port, and recv.

Referenced by be_gssapi_read(), my_sock_read(), read_or_wait(), and secure_read().

◆ secure_raw_write()

ssize_t secure_raw_write ( Port port,
const void *  ptr,
size_t  len 
)

Definition at line 373 of file be-secure.c.

374 {
375  ssize_t n;
376 
377 #ifdef WIN32
378  pgwin32_noblock = true;
379 #endif
380  n = send(port->sock, ptr, len, 0);
381 #ifdef WIN32
382  pgwin32_noblock = false;
383 #endif
384 
385  return n;
386 }
#define send(s, buf, len, flags)
Definition: win32_port.h:497

References len, pgwin32_noblock, port, and send.

Referenced by be_gssapi_write(), my_sock_write(), secure_open_gssapi(), and secure_write().

◆ secure_read()

ssize_t secure_read ( Port port,
void *  ptr,
size_t  len 
)

Definition at line 175 of file be-secure.c.

176 {
177  ssize_t n;
178  int waitfor;
179 
180  /* Deal with any already-pending interrupt condition. */
182 
183 retry:
184 #ifdef USE_SSL
185  waitfor = 0;
186  if (port->ssl_in_use)
187  {
188  n = be_tls_read(port, ptr, len, &waitfor);
189  }
190  else
191 #endif
192 #ifdef ENABLE_GSS
193  if (port->gss && port->gss->enc)
194  {
195  n = be_gssapi_read(port, ptr, len);
196  waitfor = WL_SOCKET_READABLE;
197  }
198  else
199 #endif
200  {
201  n = secure_raw_read(port, ptr, len);
202  waitfor = WL_SOCKET_READABLE;
203  }
204 
205  /* In blocking mode, wait until the socket is ready */
206  if (n < 0 && !port->noblock && (errno == EWOULDBLOCK || errno == EAGAIN))
207  {
208  WaitEvent event;
209 
210  Assert(waitfor);
211 
213 
214  WaitEventSetWait(FeBeWaitSet, -1 /* no timeout */ , &event, 1,
215  WAIT_EVENT_CLIENT_READ);
216 
217  /*
218  * If the postmaster has died, it's not safe to continue running,
219  * because it is the postmaster's job to kill us if some other backend
220  * exits uncleanly. Moreover, we won't run very well in this state;
221  * helper processes like walwriter and the bgwriter will exit, so
222  * performance may be poor. Finally, if we don't exit, pg_ctl will be
223  * unable to restart the postmaster without manual intervention, so no
224  * new connections can be accepted. Exiting clears the deck for a
225  * postmaster restart.
226  *
227  * (Note that we only make this check when we would otherwise sleep on
228  * our latch. We might still continue running for a while if the
229  * postmaster is killed in mid-query, or even through multiple queries
230  * if we never have to wait for read. We don't want to burn too many
231  * cycles checking for this very rare condition, and this should cause
232  * us to exit quickly in most cases.)
233  */
234  if (event.events & WL_POSTMASTER_DEATH)
235  ereport(FATAL,
236  (errcode(ERRCODE_ADMIN_SHUTDOWN),
237  errmsg("terminating connection due to unexpected postmaster exit")));
238 
239  /* Handle interrupt. */
240  if (event.events & WL_LATCH_SET)
241  {
244 
245  /*
246  * We'll retry the read. Most likely it will return immediately
247  * because there's still no data available, and we'll wait for the
248  * socket to become ready again.
249  */
250  }
251  goto retry;
252  }
253 
254  /*
255  * Process interrupts that happened during a successful (or non-blocking,
256  * or hard-failed) read.
257  */
259 
260  return n;
261 }
ssize_t be_gssapi_read(Port *port, void *ptr, size_t len)
ssize_t be_tls_read(Port *port, void *ptr, size_t len, int *waitfor)
ssize_t secure_raw_read(Port *port, void *ptr, size_t len)
Definition: be-secure.c:264
int errcode(int sqlerrcode)
Definition: elog.c:859
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#define FATAL
Definition: elog.h:41
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 FeBeWaitSetSocketPos
Definition: libpq.h:63
void ProcessClientReadInterrupt(bool blocked)
Definition: postgres.c:509
WaitEventSet * FeBeWaitSet
Definition: pqcomm.c:165
uint32 events
Definition: latch.h:155
#define EWOULDBLOCK
Definition: win32_port.h:380
#define EAGAIN
Definition: win32_port.h:372

References Assert, be_gssapi_read(), be_tls_read(), EAGAIN, ereport, errcode(), errmsg(), WaitEvent::events, EWOULDBLOCK, FATAL, FeBeWaitSet, FeBeWaitSetSocketPos, len, ModifyWaitEvent(), MyLatch, port, ProcessClientReadInterrupt(), ResetLatch(), secure_raw_read(), WaitEventSetWait(), WL_LATCH_SET, WL_POSTMASTER_DEATH, and WL_SOCKET_READABLE.

Referenced by pq_getbyte_if_available(), and pq_recvbuf().

◆ secure_write()

ssize_t secure_write ( Port port,
void *  ptr,
size_t  len 
)

Definition at line 301 of file be-secure.c.

302 {
303  ssize_t n;
304  int waitfor;
305 
306  /* Deal with any already-pending interrupt condition. */
308 
309 retry:
310  waitfor = 0;
311 #ifdef USE_SSL
312  if (port->ssl_in_use)
313  {
314  n = be_tls_write(port, ptr, len, &waitfor);
315  }
316  else
317 #endif
318 #ifdef ENABLE_GSS
319  if (port->gss && port->gss->enc)
320  {
321  n = be_gssapi_write(port, ptr, len);
322  waitfor = WL_SOCKET_WRITEABLE;
323  }
324  else
325 #endif
326  {
327  n = secure_raw_write(port, ptr, len);
328  waitfor = WL_SOCKET_WRITEABLE;
329  }
330 
331  if (n < 0 && !port->noblock && (errno == EWOULDBLOCK || errno == EAGAIN))
332  {
333  WaitEvent event;
334 
335  Assert(waitfor);
336 
338 
339  WaitEventSetWait(FeBeWaitSet, -1 /* no timeout */ , &event, 1,
340  WAIT_EVENT_CLIENT_WRITE);
341 
342  /* See comments in secure_read. */
343  if (event.events & WL_POSTMASTER_DEATH)
344  ereport(FATAL,
345  (errcode(ERRCODE_ADMIN_SHUTDOWN),
346  errmsg("terminating connection due to unexpected postmaster exit")));
347 
348  /* Handle interrupt. */
349  if (event.events & WL_LATCH_SET)
350  {
353 
354  /*
355  * We'll retry the write. Most likely it will return immediately
356  * because there's still no buffer space available, and we'll wait
357  * for the socket to become ready again.
358  */
359  }
360  goto retry;
361  }
362 
363  /*
364  * Process interrupts that happened during a successful (or non-blocking,
365  * or hard-failed) write.
366  */
368 
369  return n;
370 }
ssize_t be_gssapi_write(Port *port, void *ptr, size_t len)
ssize_t be_tls_write(Port *port, void *ptr, size_t len, int *waitfor)
ssize_t secure_raw_write(Port *port, const void *ptr, size_t len)
Definition: be-secure.c:373
#define WL_SOCKET_WRITEABLE
Definition: latch.h:129
void ProcessClientWriteInterrupt(bool blocked)
Definition: postgres.c:555

References Assert, be_gssapi_write(), be_tls_write(), EAGAIN, ereport, errcode(), errmsg(), WaitEvent::events, EWOULDBLOCK, FATAL, FeBeWaitSet, FeBeWaitSetSocketPos, len, ModifyWaitEvent(), MyLatch, port, ProcessClientWriteInterrupt(), ResetLatch(), secure_raw_write(), WaitEventSetWait(), WL_LATCH_SET, WL_POSTMASTER_DEATH, and WL_SOCKET_WRITEABLE.

Referenced by internal_flush_buffer(), and ProcessStartupPacket().

Variable Documentation

◆ ssl_ca_file

char* ssl_ca_file

Definition at line 38 of file be-secure.c.

Referenced by be_tls_init().

◆ ssl_cert_file

char* ssl_cert_file

Definition at line 36 of file be-secure.c.

Referenced by be_tls_init().

◆ ssl_crl_dir

char* ssl_crl_dir

Definition at line 40 of file be-secure.c.

Referenced by be_tls_init().

◆ ssl_crl_file

char* ssl_crl_file

Definition at line 39 of file be-secure.c.

Referenced by be_tls_init().

◆ ssl_dh_params_file

char* ssl_dh_params_file

Definition at line 41 of file be-secure.c.

Referenced by initialize_dh().

◆ ssl_key_file

char* ssl_key_file

Definition at line 37 of file be-secure.c.

Referenced by be_tls_init(), and check_ssl_key_file_permissions().

◆ ssl_library

char* ssl_library

Definition at line 35 of file be-secure.c.

◆ ssl_max_protocol_version

int ssl_max_protocol_version = PG_TLS_ANY

Definition at line 59 of file be-secure.c.

Referenced by be_tls_init(), and be_tls_open_server().

◆ ssl_min_protocol_version

int ssl_min_protocol_version = PG_TLS1_2_VERSION

Definition at line 58 of file be-secure.c.

Referenced by be_tls_init(), and be_tls_open_server().

◆ ssl_passphrase_command

char* ssl_passphrase_command

Definition at line 42 of file be-secure.c.

Referenced by default_openssl_tls_init(), run_ssl_passphrase_command(), and set_rot13().

◆ ssl_passphrase_command_supports_reload

bool ssl_passphrase_command_supports_reload

Definition at line 43 of file be-secure.c.

Referenced by default_openssl_tls_init().

◆ SSLCipherSuites

char* SSLCipherSuites = NULL

Definition at line 50 of file be-secure.c.

Referenced by be_tls_init().

◆ SSLECDHCurve

char* SSLECDHCurve

Definition at line 53 of file be-secure.c.

Referenced by initialize_ecdh().

◆ SSLPreferServerCiphers

bool SSLPreferServerCiphers

Definition at line 56 of file be-secure.c.

Referenced by be_tls_init().