PostgreSQL Source Code  git master
fe-secure.c File Reference
#include "postgres_fe.h"
#include <signal.h>
#include <fcntl.h>
#include <ctype.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <pthread.h>
#include "fe-auth.h"
#include "libpq-fe.h"
#include "libpq-int.h"
Include dependency graph for fe-secure.c:

Go to the source code of this file.

Data Structures

struct  sigpipe_info
 

Macros

#define SIGPIPE_MASKED(conn)   ((conn)->sigpipe_so || (conn)->sigpipe_flag)
 
#define DECLARE_SIGPIPE_INFO(spinfo)   struct sigpipe_info spinfo
 
#define DISABLE_SIGPIPE(conn, spinfo, failaction)
 
#define REMEMBER_EPIPE(spinfo, cond)
 
#define RESTORE_SIGPIPE(conn, spinfo)
 

Functions

int PQsslInUse (PGconn *conn)
 
void PQinitSSL (int do_init)
 
void PQinitOpenSSL (int do_ssl, int do_crypto)
 
int pqsecure_initialize (PGconn *conn, bool do_ssl, bool do_crypto)
 
PostgresPollingStatusType pqsecure_open_client (PGconn *conn)
 
void pqsecure_close (PGconn *conn)
 
ssize_t pqsecure_read (PGconn *conn, void *ptr, size_t len)
 
ssize_t pqsecure_raw_read (PGconn *conn, void *ptr, size_t len)
 
ssize_t pqsecure_write (PGconn *conn, const void *ptr, size_t len)
 
ssize_t pqsecure_raw_write (PGconn *conn, const void *ptr, size_t len)
 
void * PQgetssl (PGconn *conn)
 
void * PQsslStruct (PGconn *conn, const char *struct_name)
 
const char * PQsslAttribute (PGconn *conn, const char *attribute_name)
 
const char *const * PQsslAttributeNames (PGconn *conn)
 
PQsslKeyPassHook_OpenSSL_type PQgetSSLKeyPassHook_OpenSSL (void)
 
void PQsetSSLKeyPassHook_OpenSSL (PQsslKeyPassHook_OpenSSL_type hook)
 
int PQdefaultSSLKeyPassHook_OpenSSL (char *buf, int size, PGconn *conn)
 
void * PQgetgssctx (PGconn *conn)
 
int PQgssEncInUse (PGconn *conn)
 
int pq_block_sigpipe (sigset_t *osigset, bool *sigpipe_pending)
 
void pq_reset_sigpipe (sigset_t *osigset, bool sigpipe_pending, bool got_epipe)
 

Macro Definition Documentation

◆ DECLARE_SIGPIPE_INFO

#define DECLARE_SIGPIPE_INFO (   spinfo)    struct sigpipe_info spinfo

Definition at line 64 of file fe-secure.c.

◆ DISABLE_SIGPIPE

#define DISABLE_SIGPIPE (   conn,
  spinfo,
  failaction 
)
Value:
do { \
(spinfo).got_epipe = false; \
{ \
if (pq_block_sigpipe(&(spinfo).oldsigmask, \
&(spinfo).sigpipe_pending) < 0) \
failaction; \
} \
} while (0)
#define SIGPIPE_MASKED(conn)
Definition: fe-secure.c:55
int pq_block_sigpipe(sigset_t *osigset, bool *sigpipe_pending)
Definition: fe-secure.c:519
PGconn * conn
Definition: streamutil.c:55

Definition at line 66 of file fe-secure.c.

◆ REMEMBER_EPIPE

#define REMEMBER_EPIPE (   spinfo,
  cond 
)
Value:
do { \
if (cond) \
(spinfo).got_epipe = true; \
} while (0)

Definition at line 77 of file fe-secure.c.

◆ RESTORE_SIGPIPE

#define RESTORE_SIGPIPE (   conn,
  spinfo 
)
Value:
do { \
pq_reset_sigpipe(&(spinfo).oldsigmask, (spinfo).sigpipe_pending, \
(spinfo).got_epipe); \
} while (0)

Definition at line 83 of file fe-secure.c.

◆ SIGPIPE_MASKED

#define SIGPIPE_MASKED (   conn)    ((conn)->sigpipe_so || (conn)->sigpipe_flag)

Definition at line 55 of file fe-secure.c.

Function Documentation

◆ pq_block_sigpipe()

int pq_block_sigpipe ( sigset_t *  osigset,
bool sigpipe_pending 
)

Definition at line 519 of file fe-secure.c.

520 {
521  sigset_t sigpipe_sigset;
522  sigset_t sigset;
523 
524  sigemptyset(&sigpipe_sigset);
525  sigaddset(&sigpipe_sigset, SIGPIPE);
526 
527  /* Block SIGPIPE and save previous mask for later reset */
528  SOCK_ERRNO_SET(pthread_sigmask(SIG_BLOCK, &sigpipe_sigset, osigset));
529  if (SOCK_ERRNO)
530  return -1;
531 
532  /* We can have a pending SIGPIPE only if it was blocked before */
533  if (sigismember(osigset, SIGPIPE))
534  {
535  /* Is there a pending SIGPIPE? */
536  if (sigpending(&sigset) != 0)
537  return -1;
538 
539  if (sigismember(&sigset, SIGPIPE))
540  *sigpipe_pending = true;
541  else
542  *sigpipe_pending = false;
543  }
544  else
545  *sigpipe_pending = false;
546 
547  return 0;
548 }
#define SOCK_ERRNO
Definition: libpq-int.h:934
#define SOCK_ERRNO_SET(e)
Definition: libpq-int.h:936
#define SIGPIPE
Definition: win32_port.h:173

References SIGPIPE, SOCK_ERRNO, and SOCK_ERRNO_SET.

Referenced by PQprint().

◆ pq_reset_sigpipe()

void pq_reset_sigpipe ( sigset_t *  osigset,
bool  sigpipe_pending,
bool  got_epipe 
)

Definition at line 569 of file fe-secure.c.

570 {
571  int save_errno = SOCK_ERRNO;
572  int signo;
573  sigset_t sigset;
574 
575  /* Clear SIGPIPE only if none was pending */
576  if (got_epipe && !sigpipe_pending)
577  {
578  if (sigpending(&sigset) == 0 &&
579  sigismember(&sigset, SIGPIPE))
580  {
581  sigset_t sigpipe_sigset;
582 
583  sigemptyset(&sigpipe_sigset);
584  sigaddset(&sigpipe_sigset, SIGPIPE);
585 
586  sigwait(&sigpipe_sigset, &signo);
587  }
588  }
589 
590  /* Restore saved block mask */
591  pthread_sigmask(SIG_SETMASK, osigset, NULL);
592 
593  SOCK_ERRNO_SET(save_errno);
594 }

References SIGPIPE, SOCK_ERRNO, and SOCK_ERRNO_SET.

Referenced by PQprint().

◆ PQdefaultSSLKeyPassHook_OpenSSL()

int PQdefaultSSLKeyPassHook_OpenSSL ( char *  buf,
int  size,
PGconn conn 
)

Definition at line 488 of file fe-secure.c.

489 {
490  return 0;
491 }

◆ PQgetgssctx()

void* PQgetgssctx ( PGconn conn)

Definition at line 498 of file fe-secure.c.

499 {
500  return NULL;
501 }

◆ PQgetssl()

void* PQgetssl ( PGconn conn)

Definition at line 443 of file fe-secure.c.

444 {
445  return NULL;
446 }

◆ PQgetSSLKeyPassHook_OpenSSL()

PQsslKeyPassHook_OpenSSL_type PQgetSSLKeyPassHook_OpenSSL ( void  )

Definition at line 476 of file fe-secure.c.

477 {
478  return NULL;
479 }

◆ PQgssEncInUse()

int PQgssEncInUse ( PGconn conn)

Definition at line 504 of file fe-secure.c.

505 {
506  return 0;
507 }

◆ PQinitOpenSSL()

void PQinitOpenSSL ( int  do_ssl,
int  do_crypto 
)

Definition at line 127 of file fe-secure.c.

128 {
129 #ifdef USE_SSL
130  pgtls_init_library(do_ssl, do_crypto);
131 #endif
132 }
void pgtls_init_library(bool do_ssl, int do_crypto)

References pgtls_init_library().

◆ PQinitSSL()

void PQinitSSL ( int  do_init)

Definition at line 115 of file fe-secure.c.

116 {
117 #ifdef USE_SSL
119 #endif
120 }
static void do_init(void)
Definition: pg_ctl.c:894

References do_init(), and pgtls_init_library().

◆ pqsecure_close()

void pqsecure_close ( PGconn conn)

Definition at line 167 of file fe-secure.c.

168 {
169 #ifdef USE_SSL
170  pgtls_close(conn);
171 #endif
172 }
void pgtls_close(PGconn *conn)

References conn, and pgtls_close().

Referenced by pqDropConnection().

◆ pqsecure_initialize()

int pqsecure_initialize ( PGconn conn,
bool  do_ssl,
bool  do_crypto 
)

Definition at line 138 of file fe-secure.c.

139 {
140  int r = 0;
141 
142 #ifdef USE_SSL
143  r = pgtls_init(conn, do_ssl, do_crypto);
144 #endif
145 
146  return r;
147 }
int pgtls_init(PGconn *conn, bool do_ssl, bool do_crypto)

References conn, and pgtls_init().

Referenced by PQconnectPoll().

◆ pqsecure_open_client()

PostgresPollingStatusType pqsecure_open_client ( PGconn conn)

Definition at line 153 of file fe-secure.c.

154 {
155 #ifdef USE_SSL
156  return pgtls_open_client(conn);
157 #else
158  /* shouldn't get here */
159  return PGRES_POLLING_FAILED;
160 #endif
161 }
PostgresPollingStatusType pgtls_open_client(PGconn *conn)
@ PGRES_POLLING_FAILED
Definition: libpq-fe.h:90

References conn, PGRES_POLLING_FAILED, and pgtls_open_client().

Referenced by PQconnectPoll().

◆ pqsecure_raw_read()

ssize_t pqsecure_raw_read ( PGconn conn,
void *  ptr,
size_t  len 
)

Definition at line 208 of file fe-secure.c.

209 {
210  ssize_t n;
211  int result_errno = 0;
212  char sebuf[PG_STRERROR_R_BUFLEN];
213 
214  SOCK_ERRNO_SET(0);
215 
216  n = recv(conn->sock, ptr, len, 0);
217 
218  if (n < 0)
219  {
220  result_errno = SOCK_ERRNO;
221 
222  /* Set error message if appropriate */
223  switch (result_errno)
224  {
225 #ifdef EAGAIN
226  case EAGAIN:
227 #endif
228 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
229  case EWOULDBLOCK:
230 #endif
231  case EINTR:
232  /* no error message, caller is expected to retry */
233  break;
234 
235  case EPIPE:
236  case ECONNRESET:
237  libpq_append_conn_error(conn, "server closed the connection unexpectedly\n"
238  "\tThis probably means the server terminated abnormally\n"
239  "\tbefore or while processing the request.");
240  break;
241 
242  case 0:
243  /* If errno didn't get set, treat it as regular EOF */
244  n = 0;
245  break;
246 
247  default:
248  libpq_append_conn_error(conn, "could not receive data from server: %s",
249  SOCK_STRERROR(result_errno,
250  sebuf, sizeof(sebuf)));
251  break;
252  }
253  }
254 
255  /* ensure we return the intended errno to caller */
256  SOCK_ERRNO_SET(result_errno);
257 
258  return n;
259 }
void libpq_append_conn_error(PGconn *conn, const char *fmt,...)
Definition: fe-misc.c:1324
#define SOCK_STRERROR
Definition: libpq-int.h:935
const void size_t len
#define PG_STRERROR_R_BUFLEN
Definition: port.h:256
pgsocket sock
Definition: libpq-int.h:475
#define EINTR
Definition: win32_port.h:374
#define EWOULDBLOCK
Definition: win32_port.h:380
#define recv(s, buf, len, flags)
Definition: win32_port.h:496
#define ECONNRESET
Definition: win32_port.h:384
#define EAGAIN
Definition: win32_port.h:372

References conn, EAGAIN, ECONNRESET, EINTR, EWOULDBLOCK, len, libpq_append_conn_error(), PG_STRERROR_R_BUFLEN, recv, pg_conn::sock, SOCK_ERRNO, SOCK_ERRNO_SET, and SOCK_STRERROR.

Referenced by gss_read(), my_sock_read(), pg_GSS_read(), and pqsecure_read().

◆ pqsecure_raw_write()

ssize_t pqsecure_raw_write ( PGconn conn,
const void *  ptr,
size_t  len 
)

Definition at line 331 of file fe-secure.c.

332 {
333  ssize_t n;
334  int flags = 0;
335  int result_errno = 0;
336  char msgbuf[1024];
337  char sebuf[PG_STRERROR_R_BUFLEN];
338 
339  DECLARE_SIGPIPE_INFO(spinfo);
340 
341  /*
342  * If we already had a write failure, we will never again try to send data
343  * on that connection. Even if the kernel would let us, we've probably
344  * lost message boundary sync with the server. conn->write_failed
345  * therefore persists until the connection is reset, and we just discard
346  * all data presented to be written.
347  */
348  if (conn->write_failed)
349  return len;
350 
351 #ifdef MSG_NOSIGNAL
352  if (conn->sigpipe_flag)
353  flags |= MSG_NOSIGNAL;
354 
355 retry_masked:
356 #endif /* MSG_NOSIGNAL */
357 
358  DISABLE_SIGPIPE(conn, spinfo, return -1);
359 
360  n = send(conn->sock, ptr, len, flags);
361 
362  if (n < 0)
363  {
364  result_errno = SOCK_ERRNO;
365 
366  /*
367  * If we see an EINVAL, it may be because MSG_NOSIGNAL isn't available
368  * on this machine. So, clear sigpipe_flag so we don't try the flag
369  * again, and retry the send().
370  */
371 #ifdef MSG_NOSIGNAL
372  if (flags != 0 && result_errno == EINVAL)
373  {
374  conn->sigpipe_flag = false;
375  flags = 0;
376  goto retry_masked;
377  }
378 #endif /* MSG_NOSIGNAL */
379 
380  /* Set error message if appropriate */
381  switch (result_errno)
382  {
383 #ifdef EAGAIN
384  case EAGAIN:
385 #endif
386 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
387  case EWOULDBLOCK:
388 #endif
389  case EINTR:
390  /* no error message, caller is expected to retry */
391  break;
392 
393  case EPIPE:
394  /* Set flag for EPIPE */
395  REMEMBER_EPIPE(spinfo, true);
396 
397  /* FALL THRU */
398 
399  case ECONNRESET:
400  conn->write_failed = true;
401  /* Store error message in conn->write_err_msg, if possible */
402  /* (strdup failure is OK, we'll cope later) */
403  snprintf(msgbuf, sizeof(msgbuf),
404  libpq_gettext("server closed the connection unexpectedly\n"
405  "\tThis probably means the server terminated abnormally\n"
406  "\tbefore or while processing the request."));
407  /* keep newline out of translated string */
408  strlcat(msgbuf, "\n", sizeof(msgbuf));
409  conn->write_err_msg = strdup(msgbuf);
410  /* Now claim the write succeeded */
411  n = len;
412  break;
413 
414  default:
415  conn->write_failed = true;
416  /* Store error message in conn->write_err_msg, if possible */
417  /* (strdup failure is OK, we'll cope later) */
418  snprintf(msgbuf, sizeof(msgbuf),
419  libpq_gettext("could not send data to server: %s"),
420  SOCK_STRERROR(result_errno,
421  sebuf, sizeof(sebuf)));
422  /* keep newline out of translated string */
423  strlcat(msgbuf, "\n", sizeof(msgbuf));
424  conn->write_err_msg = strdup(msgbuf);
425  /* Now claim the write succeeded */
426  n = len;
427  break;
428  }
429  }
430 
431  RESTORE_SIGPIPE(conn, spinfo);
432 
433  /* ensure we return the intended errno to caller */
434  SOCK_ERRNO_SET(result_errno);
435 
436  return n;
437 }
#define REMEMBER_EPIPE(spinfo, cond)
Definition: fe-secure.c:77
#define DISABLE_SIGPIPE(conn, spinfo, failaction)
Definition: fe-secure.c:66
#define DECLARE_SIGPIPE_INFO(spinfo)
Definition: fe-secure.c:64
#define RESTORE_SIGPIPE(conn, spinfo)
Definition: fe-secure.c:83
#define libpq_gettext(x)
Definition: libpq-int.h:913
#define snprintf
Definition: port.h:238
size_t strlcat(char *dst, const char *src, size_t siz)
Definition: strlcat.c:33
char * write_err_msg
Definition: libpq-int.h:487
bool sigpipe_flag
Definition: libpq-int.h:485
bool write_failed
Definition: libpq-int.h:486
#define send(s, buf, len, flags)
Definition: win32_port.h:497

References conn, DECLARE_SIGPIPE_INFO, DISABLE_SIGPIPE, EAGAIN, ECONNRESET, EINTR, EWOULDBLOCK, len, libpq_gettext, PG_STRERROR_R_BUFLEN, REMEMBER_EPIPE, RESTORE_SIGPIPE, send, pg_conn::sigpipe_flag, snprintf, pg_conn::sock, SOCK_ERRNO, SOCK_ERRNO_SET, SOCK_STRERROR, strlcat(), pg_conn::write_err_msg, and pg_conn::write_failed.

Referenced by my_sock_write(), pg_GSS_write(), pqsecure_open_gss(), and pqsecure_write().

◆ pqsecure_read()

ssize_t pqsecure_read ( PGconn conn,
void *  ptr,
size_t  len 
)

Definition at line 182 of file fe-secure.c.

183 {
184  ssize_t n;
185 
186 #ifdef USE_SSL
187  if (conn->ssl_in_use)
188  {
189  n = pgtls_read(conn, ptr, len);
190  }
191  else
192 #endif
193 #ifdef ENABLE_GSS
194  if (conn->gssenc)
195  {
196  n = pg_GSS_read(conn, ptr, len);
197  }
198  else
199 #endif
200  {
201  n = pqsecure_raw_read(conn, ptr, len);
202  }
203 
204  return n;
205 }
ssize_t pg_GSS_read(PGconn *conn, void *ptr, size_t len)
ssize_t pgtls_read(PGconn *conn, void *ptr, size_t len)
ssize_t pqsecure_raw_read(PGconn *conn, void *ptr, size_t len)
Definition: fe-secure.c:208
bool ssl_in_use
Definition: libpq-int.h:568

References conn, len, pg_GSS_read(), pgtls_read(), pqsecure_raw_read(), and pg_conn::ssl_in_use.

Referenced by pqReadData().

◆ pqsecure_write()

ssize_t pqsecure_write ( PGconn conn,
const void *  ptr,
size_t  len 
)

Definition at line 282 of file fe-secure.c.

283 {
284  ssize_t n;
285 
286 #ifdef USE_SSL
287  if (conn->ssl_in_use)
288  {
289  n = pgtls_write(conn, ptr, len);
290  }
291  else
292 #endif
293 #ifdef ENABLE_GSS
294  if (conn->gssenc)
295  {
296  n = pg_GSS_write(conn, ptr, len);
297  }
298  else
299 #endif
300  {
301  n = pqsecure_raw_write(conn, ptr, len);
302  }
303 
304  return n;
305 }
ssize_t pg_GSS_write(PGconn *conn, const void *ptr, size_t len)
ssize_t pgtls_write(PGconn *conn, const void *ptr, size_t len)
ssize_t pqsecure_raw_write(PGconn *conn, const void *ptr, size_t len)
Definition: fe-secure.c:331

References conn, len, pg_GSS_write(), pgtls_write(), pqsecure_raw_write(), and pg_conn::ssl_in_use.

Referenced by pqSendSome().

◆ PQsetSSLKeyPassHook_OpenSSL()

void PQsetSSLKeyPassHook_OpenSSL ( PQsslKeyPassHook_OpenSSL_type  hook)

Definition at line 482 of file fe-secure.c.

483 {
484  return;
485 }

◆ PQsslAttribute()

const char* PQsslAttribute ( PGconn conn,
const char *  attribute_name 
)

Definition at line 455 of file fe-secure.c.

456 {
457  return NULL;
458 }

◆ PQsslAttributeNames()

const char* const* PQsslAttributeNames ( PGconn conn)

Definition at line 461 of file fe-secure.c.

462 {
463  static const char *const result[] = {NULL};
464 
465  return result;
466 }

◆ PQsslInUse()

int PQsslInUse ( PGconn conn)

Definition at line 103 of file fe-secure.c.

104 {
105  if (!conn)
106  return 0;
107  return conn->ssl_in_use;
108 }

References conn, and pg_conn::ssl_in_use.

Referenced by printSSLInfo().

◆ PQsslStruct()

void* PQsslStruct ( PGconn conn,
const char *  struct_name 
)

Definition at line 449 of file fe-secure.c.

450 {
451  return NULL;
452 }