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/injection_point.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 166 of file be-secure.c.

167 {
168 #ifdef USE_SSL
169  if (port->ssl_in_use)
171 #endif
172 }
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 87 of file be-secure.c.

88 {
89 #ifdef USE_SSL
91 #endif
92 }
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 74 of file be-secure.c.

75 {
76 #ifdef USE_SSL
77  return be_tls_init(isServerStart);
78 #else
79  return 0;
80 #endif
81 }
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 98 of file be-secure.c.

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

Referenced by ClientAuthentication().

◆ secure_open_server()

int secure_open_server ( Port port)

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

112 {
113 #ifdef USE_SSL
114  int r = 0;
115  ssize_t len;
116 
117  /* push unencrypted buffered data back through SSL setup */
119  if (len > 0)
120  {
121  char *buf = palloc(len);
122 
123  pq_startmsgread();
124  if (pq_getbytes(buf, len) == EOF)
125  return STATUS_ERROR; /* shouldn't be possible */
126  pq_endmsgread();
127  port->raw_buf = buf;
128  port->raw_buf_remaining = len;
129  port->raw_buf_consumed = 0;
130  }
132 
133  INJECTION_POINT("backend-ssl-startup");
134 
136 
137  if (port->raw_buf_remaining > 0)
138  {
139  /*
140  * This shouldn't be possible -- it would mean the client sent
141  * encrypted data before we established a session key...
142  */
143  elog(LOG, "buffered unencrypted data remains after negotiating SSL connection");
144  return STATUS_ERROR;
145  }
146  if (port->raw_buf != NULL)
147  {
148  pfree(port->raw_buf);
149  port->raw_buf = NULL;
150  }
151 
152  ereport(DEBUG2,
153  (errmsg_internal("SSL connection from DN:\"%s\" CN:\"%s\"",
154  port->peer_dn ? port->peer_dn : "(anonymous)",
155  port->peer_cn ? port->peer_cn : "(anonymous)")));
156  return r;
157 #else
158  return 0;
159 #endif
160 }
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:1157
#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
#define INJECTION_POINT(name)
void pfree(void *pointer)
Definition: mcxt.c:1521
void * palloc(Size size)
Definition: mcxt.c:1317
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:1063
ssize_t pq_buffer_remaining_data(void)
Definition: pqcomm.c:1127
void pq_endmsgread(void)
Definition: pqcomm.c:1165
void pq_startmsgread(void)
Definition: pqcomm.c:1141

References Assert, be_tls_open_server(), buf, DEBUG2, elog, ereport, errmsg_internal(), INJECTION_POINT, 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 267 of file be-secure.c.

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

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 376 of file be-secure.c.

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

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 178 of file be-secure.c.

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

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

Referenced by be_tls_init().

◆ ssl_cert_file

char* ssl_cert_file

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

Referenced by be_tls_init().

◆ ssl_crl_dir

char* ssl_crl_dir

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

Referenced by be_tls_init().

◆ ssl_crl_file

char* ssl_crl_file

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

Referenced by be_tls_init().

◆ ssl_dh_params_file

char* ssl_dh_params_file

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

Referenced by initialize_dh().

◆ ssl_key_file

char* ssl_key_file

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

Referenced by be_tls_init(), and check_ssl_key_file_permissions().

◆ ssl_library

char* ssl_library

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

◆ ssl_max_protocol_version

int ssl_max_protocol_version = PG_TLS_ANY

Definition at line 60 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 59 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 43 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 44 of file be-secure.c.

Referenced by default_openssl_tls_init().

◆ SSLCipherSuites

char* SSLCipherSuites = NULL

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

Referenced by be_tls_init().

◆ SSLECDHCurve

char* SSLECDHCurve

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

Referenced by initialize_ecdh().

◆ SSLPreferServerCiphers

bool SSLPreferServerCiphers

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

Referenced by be_tls_init().