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 "libpq/libpq.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "tcop/tcopprot.h"
#include "utils/memutils.h"
#include "storage/ipc.h"
#include "storage/proc.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_cert_file
 
char * ssl_key_file
 
char * ssl_ca_file
 
char * ssl_crl_file
 
char * ssl_dh_params_file
 
char * ssl_passphrase_command
 
bool ssl_passphrase_command_supports_reload
 
char * SSLCipherSuites = NULL
 
char * SSLECDHCurve
 
bool SSLPreferServerCiphers
 

Function Documentation

◆ secure_close()

void secure_close ( Port port)

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

References be_tls_close(), and Port::ssl_in_use.

Referenced by ConnFree(), and socket_close().

131 {
132 #ifdef USE_SSL
133  if (port->ssl_in_use)
134  be_tls_close(port);
135 #endif
136 }
bool ssl_in_use
Definition: libpq-be.h:181
void be_tls_close(Port *port)

◆ secure_destroy()

void secure_destroy ( void  )

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

References be_tls_destroy().

Referenced by SIGHUP_handler().

88 {
89 #ifdef USE_SSL
91 #endif
92 }
void be_tls_destroy(void)

◆ secure_initialize()

int secure_initialize ( bool  isServerStart)

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

References be_tls_init().

Referenced by BackendRun(), PostmasterMain(), and SIGHUP_handler().

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)

◆ secure_loaded_verify_locations()

bool secure_loaded_verify_locations ( void  )

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

References ssl_loaded_verify_locations.

Referenced by ClientAuthentication().

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

◆ secure_open_server()

int secure_open_server ( Port port)

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

References be_tls_open_server(), DEBUG2, ereport, errmsg(), and Port::peer_cn.

Referenced by ProcessStartupPacket().

112 {
113  int r = 0;
114 
115 #ifdef USE_SSL
116  r = be_tls_open_server(port);
117 
118  ereport(DEBUG2,
119  (errmsg("SSL connection from \"%s\"",
120  port->peer_cn ? port->peer_cn : "(anonymous)")));
121 #endif
122 
123  return r;
124 }
char * peer_cn
Definition: libpq-be.h:182
#define DEBUG2
Definition: elog.h:24
#define ereport(elevel, rest)
Definition: elog.h:122
int be_tls_open_server(Port *port)
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ secure_raw_read()

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

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

References pgwin32_noblock, recv, and Port::sock.

Referenced by my_sock_read(), and secure_read().

222 {
223  ssize_t n;
224 
225  /*
226  * Try to read from the socket without blocking. If it succeeds we're
227  * done, otherwise we'll wait for the socket using the latch mechanism.
228  */
229 #ifdef WIN32
230  pgwin32_noblock = true;
231 #endif
232  n = recv(port->sock, ptr, len, 0);
233 #ifdef WIN32
234  pgwin32_noblock = false;
235 #endif
236 
237  return n;
238 }
int pgwin32_noblock
Definition: socket.c:28
#define recv(s, buf, len, flags)
Definition: win32_port.h:448
pgsocket sock
Definition: libpq-be.h:118

◆ secure_raw_write()

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

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

References pgwin32_noblock, send, and Port::sock.

Referenced by my_sock_write(), and secure_write().

308 {
309  ssize_t n;
310 
311 #ifdef WIN32
312  pgwin32_noblock = true;
313 #endif
314  n = send(port->sock, ptr, len, 0);
315 #ifdef WIN32
316  pgwin32_noblock = false;
317 #endif
318 
319  return n;
320 }
int pgwin32_noblock
Definition: socket.c:28
pgsocket sock
Definition: libpq-be.h:118
#define send(s, buf, len, flags)
Definition: win32_port.h:449

◆ secure_read()

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

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

References Assert, be_tls_read(), EAGAIN, ereport, errcode(), errmsg(), WaitEvent::events, EWOULDBLOCK, FATAL, FeBeWaitSet, ModifyWaitEvent(), MyLatch, ProcessClientReadInterrupt(), ResetLatch(), secure_raw_read(), Port::ssl_in_use, WAIT_EVENT_CLIENT_READ, WaitEventSetWait(), WL_LATCH_SET, WL_POSTMASTER_DEATH, and WL_SOCKET_READABLE.

Referenced by pq_getbyte_if_available(), and pq_recvbuf().

143 {
144  ssize_t n;
145  int waitfor;
146 
147 retry:
148 #ifdef USE_SSL
149  waitfor = 0;
150  if (port->ssl_in_use)
151  {
152  n = be_tls_read(port, ptr, len, &waitfor);
153  }
154  else
155 #endif
156  {
157  n = secure_raw_read(port, ptr, len);
158  waitfor = WL_SOCKET_READABLE;
159  }
160 
161  /* In blocking mode, wait until the socket is ready */
162  if (n < 0 && !port->noblock && (errno == EWOULDBLOCK || errno == EAGAIN))
163  {
164  WaitEvent event;
165 
166  Assert(waitfor);
167 
168  ModifyWaitEvent(FeBeWaitSet, 0, waitfor, NULL);
169 
170  WaitEventSetWait(FeBeWaitSet, -1 /* no timeout */ , &event, 1,
172 
173  /*
174  * If the postmaster has died, it's not safe to continue running,
175  * because it is the postmaster's job to kill us if some other backend
176  * exists uncleanly. Moreover, we won't run very well in this state;
177  * helper processes like walwriter and the bgwriter will exit, so
178  * performance may be poor. Finally, if we don't exit, pg_ctl will be
179  * unable to restart the postmaster without manual intervention, so no
180  * new connections can be accepted. Exiting clears the deck for a
181  * postmaster restart.
182  *
183  * (Note that we only make this check when we would otherwise sleep on
184  * our latch. We might still continue running for a while if the
185  * postmaster is killed in mid-query, or even through multiple queries
186  * if we never have to wait for read. We don't want to burn too many
187  * cycles checking for this very rare condition, and this should cause
188  * us to exit quickly in most cases.)
189  */
190  if (event.events & WL_POSTMASTER_DEATH)
191  ereport(FATAL,
192  (errcode(ERRCODE_ADMIN_SHUTDOWN),
193  errmsg("terminating connection due to unexpected postmaster exit")));
194 
195  /* Handle interrupt. */
196  if (event.events & WL_LATCH_SET)
197  {
200 
201  /*
202  * We'll retry the read. Most likely it will return immediately
203  * because there's still no data available, and we'll wait for the
204  * socket to become ready again.
205  */
206  }
207  goto retry;
208  }
209 
210  /*
211  * Process interrupts that happened while (or before) receiving. Note that
212  * we signal that we're not blocking, which will prevent some types of
213  * interrupts from being processed.
214  */
216 
217  return n;
218 }
ssize_t be_tls_read(Port *port, void *ptr, size_t len, int *waitfor)
#define EAGAIN
Definition: win32_port.h:332
int errcode(int sqlerrcode)
Definition: elog.c:575
void ModifyWaitEvent(WaitEventSet *set, int pos, uint32 events, Latch *latch)
Definition: latch.c:736
bool ssl_in_use
Definition: libpq-be.h:181
#define WL_SOCKET_READABLE
Definition: latch.h:125
WaitEventSet * FeBeWaitSet
Definition: pqcomm.c:186
void ResetLatch(volatile Latch *latch)
Definition: latch.c:497
#define FATAL
Definition: elog.h:52
uint32 events
Definition: latch.h:143
#define ereport(elevel, rest)
Definition: elog.h:122
#define WL_POSTMASTER_DEATH
Definition: latch.h:128
void ProcessClientReadInterrupt(bool blocked)
Definition: postgres.c:528
ssize_t secure_raw_read(Port *port, void *ptr, size_t len)
Definition: be-secure.c:221
#define Assert(condition)
Definition: c.h:699
int errmsg(const char *fmt,...)
Definition: elog.c:797
struct Latch * MyLatch
Definition: globals.c:55
#define EWOULDBLOCK
Definition: win32_port.h:340
#define WL_LATCH_SET
Definition: latch.h:124
int WaitEventSetWait(WaitEventSet *set, long timeout, WaitEvent *occurred_events, int nevents, uint32 wait_event_info)
Definition: latch.c:921

◆ secure_write()

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

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

References Assert, be_tls_write(), EAGAIN, ereport, errcode(), errmsg(), WaitEvent::events, EWOULDBLOCK, FATAL, FeBeWaitSet, ModifyWaitEvent(), MyLatch, ProcessClientWriteInterrupt(), ResetLatch(), secure_raw_write(), Port::ssl_in_use, WAIT_EVENT_CLIENT_WRITE, WaitEventSetWait(), WL_LATCH_SET, WL_POSTMASTER_DEATH, and WL_SOCKET_WRITEABLE.

Referenced by internal_flush().

246 {
247  ssize_t n;
248  int waitfor;
249 
250 retry:
251  waitfor = 0;
252 #ifdef USE_SSL
253  if (port->ssl_in_use)
254  {
255  n = be_tls_write(port, ptr, len, &waitfor);
256  }
257  else
258 #endif
259  {
260  n = secure_raw_write(port, ptr, len);
261  waitfor = WL_SOCKET_WRITEABLE;
262  }
263 
264  if (n < 0 && !port->noblock && (errno == EWOULDBLOCK || errno == EAGAIN))
265  {
266  WaitEvent event;
267 
268  Assert(waitfor);
269 
270  ModifyWaitEvent(FeBeWaitSet, 0, waitfor, NULL);
271 
272  WaitEventSetWait(FeBeWaitSet, -1 /* no timeout */ , &event, 1,
274 
275  /* See comments in secure_read. */
276  if (event.events & WL_POSTMASTER_DEATH)
277  ereport(FATAL,
278  (errcode(ERRCODE_ADMIN_SHUTDOWN),
279  errmsg("terminating connection due to unexpected postmaster exit")));
280 
281  /* Handle interrupt. */
282  if (event.events & WL_LATCH_SET)
283  {
286 
287  /*
288  * We'll retry the write. Most likely it will return immediately
289  * because there's still no data available, and we'll wait for the
290  * socket to become ready again.
291  */
292  }
293  goto retry;
294  }
295 
296  /*
297  * Process interrupts that happened while (or before) sending. Note that
298  * we signal that we're not blocking, which will prevent some types of
299  * interrupts from being processed.
300  */
302 
303  return n;
304 }
#define WL_SOCKET_WRITEABLE
Definition: latch.h:126
#define EAGAIN
Definition: win32_port.h:332
ssize_t secure_raw_write(Port *port, const void *ptr, size_t len)
Definition: be-secure.c:307
int errcode(int sqlerrcode)
Definition: elog.c:575
void ModifyWaitEvent(WaitEventSet *set, int pos, uint32 events, Latch *latch)
Definition: latch.c:736
bool ssl_in_use
Definition: libpq-be.h:181
WaitEventSet * FeBeWaitSet
Definition: pqcomm.c:186
void ResetLatch(volatile Latch *latch)
Definition: latch.c:497
#define FATAL
Definition: elog.h:52
uint32 events
Definition: latch.h:143
ssize_t be_tls_write(Port *port, void *ptr, size_t len, int *waitfor)
#define ereport(elevel, rest)
Definition: elog.h:122
#define WL_POSTMASTER_DEATH
Definition: latch.h:128
#define Assert(condition)
Definition: c.h:699
int errmsg(const char *fmt,...)
Definition: elog.c:797
struct Latch * MyLatch
Definition: globals.c:55
#define EWOULDBLOCK
Definition: win32_port.h:340
#define WL_LATCH_SET
Definition: latch.h:124
int WaitEventSetWait(WaitEventSet *set, long timeout, WaitEvent *occurred_events, int nevents, uint32 wait_event_info)
Definition: latch.c:921
void ProcessClientWriteInterrupt(bool blocked)
Definition: postgres.c:566

Variable Documentation

◆ ssl_ca_file

char* ssl_ca_file

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

Referenced by be_tls_init().

◆ ssl_cert_file

char* ssl_cert_file

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

Referenced by be_tls_init().

◆ ssl_crl_file

char* ssl_crl_file

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

Referenced by be_tls_init().

◆ ssl_dh_params_file

char* ssl_dh_params_file

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

Referenced by initialize_dh().

◆ ssl_key_file

char* ssl_key_file

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

Referenced by be_tls_init().

◆ ssl_passphrase_command

char* ssl_passphrase_command

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

Referenced by be_tls_init(), and run_ssl_passphrase_command().

◆ ssl_passphrase_command_supports_reload

bool ssl_passphrase_command_supports_reload

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

Referenced by be_tls_init().

◆ SSLCipherSuites

char* SSLCipherSuites = NULL

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

Referenced by be_tls_init().

◆ SSLECDHCurve

char* SSLECDHCurve

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

Referenced by initialize_ecdh().

◆ SSLPreferServerCiphers

bool SSLPreferServerCiphers

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

Referenced by be_tls_init().