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 * SSLCipherList = 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 167 of file be-secure.c.

168{
169#ifdef USE_SSL
170 if (port->ssl_in_use)
172#endif
173}
void be_tls_close(Port *port)
static int port
Definition: pg_regress.c:115

References be_tls_close(), and port.

Referenced by socket_close().

◆ secure_destroy()

void secure_destroy ( void  )

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

89{
90#ifdef USE_SSL
92#endif
93}
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 75 of file be-secure.c.

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

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

Referenced by ClientAuthentication().

◆ secure_open_server()

int secure_open_server ( Port port)

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

113{
114#ifdef USE_SSL
115 int r = 0;
116 ssize_t len;
117
118 /* push unencrypted buffered data back through SSL setup */
120 if (len > 0)
121 {
122 char *buf = palloc(len);
123
125 if (pq_getbytes(buf, len) == EOF)
126 return STATUS_ERROR; /* shouldn't be possible */
128 port->raw_buf = buf;
129 port->raw_buf_remaining = len;
130 port->raw_buf_consumed = 0;
131 }
133
134 INJECTION_POINT("backend-ssl-startup");
135
137
138 if (port->raw_buf_remaining > 0)
139 {
140 /*
141 * This shouldn't be possible -- it would mean the client sent
142 * encrypted data before we established a session key...
143 */
144 elog(LOG, "buffered unencrypted data remains after negotiating SSL connection");
145 return STATUS_ERROR;
146 }
147 if (port->raw_buf != NULL)
148 {
149 pfree(port->raw_buf);
150 port->raw_buf = NULL;
151 }
152
154 (errmsg_internal("SSL connection from DN:\"%s\" CN:\"%s\"",
155 port->peer_dn ? port->peer_dn : "(anonymous)",
156 port->peer_cn ? port->peer_cn : "(anonymous)")));
157 return r;
158#else
159 return 0;
160#endif
161}
int be_tls_open_server(Port *port)
#define Assert(condition)
Definition: c.h:815
#define STATUS_ERROR
Definition: c.h:1127
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:225
#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:72
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 268 of file be-secure.c.

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

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

Referenced by be_gssapi_read(), port_bio_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 377 of file be-secure.c.

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

References len, pgwin32_noblock, port, and send.

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

◆ secure_read()

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

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

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

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

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

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 61 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 60 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().

◆ SSLCipherList

char* SSLCipherList = NULL

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

Referenced by be_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 55 of file be-secure.c.

Referenced by initialize_ecdh().

◆ SSLPreferServerCiphers

bool SSLPreferServerCiphers

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

Referenced by be_tls_init().