PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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 "storage/latch.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, const void *ptr, size_t len)
 
ssize_t secure_raw_write (Port *port, const void *ptr, size_t len)
 

Variables

charssl_library
 
charssl_cert_file
 
charssl_key_file
 
charssl_ca_file
 
charssl_crl_file
 
charssl_crl_dir
 
charssl_dh_params_file
 
charssl_passphrase_command
 
bool ssl_passphrase_command_supports_reload
 
charSSLCipherSuites = NULL
 
charSSLCipherList = NULL
 
charSSLECDHCurve
 
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 168 of file be-secure.c.

169{
170#ifdef USE_SSL
171 if (port->ssl_in_use)
173#endif
174}
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 89 of file be-secure.c.

90{
91#ifdef USE_SSL
93#endif
94}
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 76 of file be-secure.c.

77{
78#ifdef USE_SSL
80#else
81 return 0;
82#endif
83}
int be_tls_init(bool isServerStart)
static int fb(int x)

References be_tls_init(), and fb().

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

◆ secure_loaded_verify_locations()

bool secure_loaded_verify_locations ( void  )

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

101{
102#ifdef USE_SSL
104#else
105 return false;
106#endif
107}

References fb().

Referenced by ClientAuthentication().

◆ secure_open_server()

int secure_open_server ( Port port)

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

114{
115#ifdef USE_SSL
116 int r = 0;
117 ssize_t len;
118
119 /* push unencrypted buffered data back through SSL setup */
121 if (len > 0)
122 {
123 char *buf = palloc(len);
124
126 if (pq_getbytes(buf, len) == EOF)
127 return STATUS_ERROR; /* shouldn't be possible */
129 port->raw_buf = buf;
130 port->raw_buf_remaining = len;
131 port->raw_buf_consumed = 0;
132 }
134
135 INJECTION_POINT("backend-ssl-startup", NULL);
136
138
139 if (port->raw_buf_remaining > 0)
140 {
141 /*
142 * This shouldn't be possible -- it would mean the client sent
143 * encrypted data before we established a session key...
144 */
145 elog(LOG, "buffered unencrypted data remains after negotiating SSL connection");
146 return STATUS_ERROR;
147 }
148 if (port->raw_buf != NULL)
149 {
150 pfree(port->raw_buf);
151 port->raw_buf = NULL;
152 }
153
155 (errmsg_internal("SSL connection from DN:\"%s\" CN:\"%s\"",
156 port->peer_dn ? port->peer_dn : "(anonymous)",
157 port->peer_cn ? port->peer_cn : "(anonymous)")));
158 return r;
159#else
160 return 0;
161#endif
162}
int be_tls_open_server(Port *port)
#define Assert(condition)
Definition c.h:906
#define STATUS_ERROR
Definition c.h:1222
#define LOG
Definition elog.h:31
int int errmsg_internal(const char *fmt,...) pg_attribute_printf(1
#define DEBUG2
Definition elog.h:29
#define elog(elevel,...)
Definition elog.h:226
#define ereport(elevel,...)
Definition elog.h:150
#define INJECTION_POINT(name, arg)
void pfree(void *pointer)
Definition mcxt.c:1616
void * palloc(Size size)
Definition mcxt.c:1387
const void size_t len
static char buf[DEFAULT_XLOG_SEG_SIZE]
ssize_t pq_buffer_remaining_data(void)
Definition pqcomm.c:1128
int pq_getbytes(void *b, size_t len)
Definition pqcomm.c:1063
void pq_endmsgread(void)
Definition pqcomm.c:1166
void pq_startmsgread(void)
Definition pqcomm.c:1142

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

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

References Assert, fb(), 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 378 of file be-secure.c.

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

References fb(), 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 180 of file be-secure.c.

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

References Assert, be_gssapi_read(), be_tls_read(), EAGAIN, ereport, errcode(), errmsg, WaitEvent::events, EWOULDBLOCK, FATAL, fb(), 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,
const void ptr,
size_t  len 
)

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

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

References Assert, be_gssapi_write(), be_tls_write(), EAGAIN, ereport, errcode(), errmsg, WaitEvent::events, EWOULDBLOCK, FATAL, fb(), 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 40 of file be-secure.c.

Referenced by be_tls_init().

◆ ssl_cert_file

char* ssl_cert_file

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

Referenced by be_tls_init().

◆ ssl_crl_dir

char* ssl_crl_dir

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

Referenced by be_tls_init().

◆ ssl_crl_file

char* ssl_crl_file

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

Referenced by be_tls_init().

◆ ssl_dh_params_file

char* ssl_dh_params_file

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

Referenced by initialize_dh().

◆ ssl_key_file

char* ssl_key_file

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

Referenced by be_tls_init(), and check_ssl_key_file_permissions().

◆ ssl_library

char* ssl_library

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

◆ ssl_max_protocol_version

int ssl_max_protocol_version = PG_TLS_ANY

Definition at line 62 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 61 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 44 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 45 of file be-secure.c.

Referenced by default_openssl_tls_init().

◆ SSLCipherList

char* SSLCipherList = NULL

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

Referenced by be_tls_init().

◆ SSLCipherSuites

char* SSLCipherSuites = NULL

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

Referenced by be_tls_init().

◆ SSLECDHCurve

char* SSLECDHCurve

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

Referenced by initialize_ecdh().

◆ SSLPreferServerCiphers

bool SSLPreferServerCiphers

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

Referenced by be_tls_init().