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 <arpa/inet.h>
#include <sys/stat.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.

Macros

#define SIGPIPE_MASKED(conn)   ((conn)->sigpipe_so || (conn)->sigpipe_flag)
 
#define DECLARE_SIGPIPE_INFO(spinfo)   pqsigfunc spinfo = NULL
 
#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)
 

Macro Definition Documentation

◆ DECLARE_SIGPIPE_INFO

#define DECLARE_SIGPIPE_INFO (   spinfo)    pqsigfunc spinfo = NULL

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

◆ DISABLE_SIGPIPE

#define DISABLE_SIGPIPE (   conn,
  spinfo,
  failaction 
)
Value:
do { \
spinfo = pqsignal(SIGPIPE, SIG_IGN); \
} while (0)
#define SIGPIPE_MASKED(conn)
Definition: fe-secure.c:59
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:180
PGconn * conn
Definition: streamutil.c:54
#define SIGPIPE
Definition: win32_port.h:172
#define SIG_IGN
Definition: win32_port.h:164

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

◆ REMEMBER_EPIPE

#define REMEMBER_EPIPE (   spinfo,
  cond 
)

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

◆ RESTORE_SIGPIPE

#define RESTORE_SIGPIPE (   conn,
  spinfo 
)
Value:
do { \
pqsignal(SIGPIPE, spinfo); \
} while (0)

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

◆ SIGPIPE_MASKED

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

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

Function Documentation

◆ PQdefaultSSLKeyPassHook_OpenSSL()

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

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

504 {
505  return 0;
506 }

◆ PQgetgssctx()

void* PQgetgssctx ( PGconn conn)

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

514 {
515  return NULL;
516 }

◆ PQgetssl()

void* PQgetssl ( PGconn conn)

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

459 {
460  return NULL;
461 }

◆ PQgetSSLKeyPassHook_OpenSSL()

PQsslKeyPassHook_OpenSSL_type PQgetSSLKeyPassHook_OpenSSL ( void  )

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

492 {
493  return NULL;
494 }

◆ PQgssEncInUse()

int PQgssEncInUse ( PGconn conn)

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

520 {
521  return 0;
522 }

◆ PQinitOpenSSL()

void PQinitOpenSSL ( int  do_ssl,
int  do_crypto 
)

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

152 {
153 #ifdef USE_SSL
154  pgtls_init_library(do_ssl, do_crypto);
155 #endif
156 }
void pgtls_init_library(bool do_ssl, int do_crypto)

References pgtls_init_library().

◆ PQinitSSL()

void PQinitSSL ( int  do_init)

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

140 {
141 #ifdef USE_SSL
143 #endif
144 }
static void do_init(void)
Definition: pg_ctl.c:901

References do_init(), and pgtls_init_library().

◆ pqsecure_close()

void pqsecure_close ( PGconn conn)

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

192 {
193 #ifdef USE_SSL
194  pgtls_close(conn);
195 #endif
196 }
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 162 of file fe-secure.c.

163 {
164  int r = 0;
165 
166 #ifdef USE_SSL
167  r = pgtls_init(conn, do_ssl, do_crypto);
168 #endif
169 
170  return r;
171 }
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 177 of file fe-secure.c.

178 {
179 #ifdef USE_SSL
180  return pgtls_open_client(conn);
181 #else
182  /* shouldn't get here */
183  return PGRES_POLLING_FAILED;
184 #endif
185 }
PostgresPollingStatusType pgtls_open_client(PGconn *conn)
@ PGRES_POLLING_FAILED
Definition: libpq-fe.h:86

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 232 of file fe-secure.c.

233 {
234  ssize_t n;
235  int result_errno = 0;
236  char sebuf[PG_STRERROR_R_BUFLEN];
237 
238  n = recv(conn->sock, ptr, len, 0);
239 
240  if (n < 0)
241  {
242  result_errno = SOCK_ERRNO;
243 
244  /* Set error message if appropriate */
245  switch (result_errno)
246  {
247 #ifdef EAGAIN
248  case EAGAIN:
249 #endif
250 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
251  case EWOULDBLOCK:
252 #endif
253  case EINTR:
254  /* no error message, caller is expected to retry */
255  break;
256 
257  case EPIPE:
258  case ECONNRESET:
260  libpq_gettext("server closed the connection unexpectedly\n"
261  "\tThis probably means the server terminated abnormally\n"
262  "\tbefore or while processing the request.\n"));
263  break;
264 
265  default:
267  libpq_gettext("could not receive data from server: %s\n"),
268  SOCK_STRERROR(result_errno,
269  sebuf, sizeof(sebuf)));
270  break;
271  }
272  }
273 
274  /* ensure we return the intended errno to caller */
275  SOCK_ERRNO_SET(result_errno);
276 
277  return n;
278 }
#define libpq_gettext(x)
Definition: libpq-int.h:878
#define SOCK_STRERROR
Definition: libpq-int.h:892
#define SOCK_ERRNO
Definition: libpq-int.h:891
#define SOCK_ERRNO_SET(e)
Definition: libpq-int.h:893
const void size_t len
#define PG_STRERROR_R_BUFLEN
Definition: port.h:243
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
pgsocket sock
Definition: libpq-int.h:444
PQExpBufferData errorMessage
Definition: libpq-int.h:585
#define EINTR
Definition: win32_port.h:351
#define EWOULDBLOCK
Definition: win32_port.h:357
#define recv(s, buf, len, flags)
Definition: win32_port.h:475
#define ECONNRESET
Definition: win32_port.h:361
#define EAGAIN
Definition: win32_port.h:349

References appendPQExpBuffer(), appendPQExpBufferStr(), conn, EAGAIN, ECONNRESET, EINTR, pg_conn::errorMessage, EWOULDBLOCK, len, libpq_gettext, 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 350 of file fe-secure.c.

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

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, 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 206 of file fe-secure.c.

207 {
208  ssize_t n;
209 
210 #ifdef USE_SSL
211  if (conn->ssl_in_use)
212  {
213  n = pgtls_read(conn, ptr, len);
214  }
215  else
216 #endif
217 #ifdef ENABLE_GSS
218  if (conn->gssenc)
219  {
220  n = pg_GSS_read(conn, ptr, len);
221  }
222  else
223 #endif
224  {
225  n = pqsecure_raw_read(conn, ptr, len);
226  }
227 
228  return n;
229 }
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:232
bool ssl_in_use
Definition: libpq-int.h:517

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 301 of file fe-secure.c.

302 {
303  ssize_t n;
304 
305 #ifdef USE_SSL
306  if (conn->ssl_in_use)
307  {
308  n = pgtls_write(conn, ptr, len);
309  }
310  else
311 #endif
312 #ifdef ENABLE_GSS
313  if (conn->gssenc)
314  {
315  n = pg_GSS_write(conn, ptr, len);
316  }
317  else
318 #endif
319  {
320  n = pqsecure_raw_write(conn, ptr, len);
321  }
322 
323  return n;
324 }
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:350

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 497 of file fe-secure.c.

498 {
499  return;
500 }

◆ PQsslAttribute()

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

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

471 {
472  return NULL;
473 }

◆ PQsslAttributeNames()

const char* const* PQsslAttributeNames ( PGconn conn)

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

477 {
478  static const char *const result[] = {NULL};
479 
480  return result;
481 }

◆ PQsslInUse()

int PQsslInUse ( PGconn conn)

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

128 {
129  if (!conn)
130  return 0;
131  return conn->ssl_in_use;
132 }

References conn, and pg_conn::ssl_in_use.

Referenced by printSSLInfo().

◆ PQsslStruct()

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

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

465 {
466  return NULL;
467 }