PostgreSQL Source Code  git master
fe-secure.c File Reference
#include "postgres_fe.h"
#include <signal.h>
#include <fcntl.h>
#include <ctype.h>
#include "libpq-fe.h"
#include "fe-auth.h"
#include "libpq-int.h"
#include <sys/socket.h>
#include <unistd.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/stat.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)
 
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)
 

Macro Definition Documentation

◆ DECLARE_SIGPIPE_INFO

#define DECLARE_SIGPIPE_INFO (   spinfo)    pqsigfunc spinfo = NULL

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

Referenced by pqsecure_raw_write().

◆ DISABLE_SIGPIPE

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

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

Referenced by pqsecure_raw_write().

◆ REMEMBER_EPIPE

#define REMEMBER_EPIPE (   spinfo,
  cond 
)

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

Referenced by pqsecure_raw_write().

◆ RESTORE_SIGPIPE

#define RESTORE_SIGPIPE (   conn,
  spinfo 
)
Value:
do { \
pqsignal(SIGPIPE, spinfo); \
} while (0)
#define SIGPIPE
Definition: win32_port.h:168
#define SIGPIPE_MASKED(conn)
Definition: fe-secure.c:65
PGconn * conn
Definition: streamutil.c:55

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

Referenced by pqsecure_raw_write().

◆ SIGPIPE_MASKED

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

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

Function Documentation

◆ PQgetssl()

void* PQgetssl ( PGconn conn)

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

397 {
398  return NULL;
399 }

◆ PQinitOpenSSL()

void PQinitOpenSSL ( int  do_ssl,
int  do_crypto 
)

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

References pgtls_init_library().

158 {
159 #ifdef USE_SSL
160  pgtls_init_library(do_ssl, do_crypto);
161 #endif
162 }
void pgtls_init_library(bool do_ssl, int do_crypto)

◆ PQinitSSL()

void PQinitSSL ( int  do_init)

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

References pgtls_init_library().

146 {
147 #ifdef USE_SSL
149 #endif
150 }
static void do_init(void)
Definition: pg_ctl.c:752
void pgtls_init_library(bool do_ssl, int do_crypto)

◆ pqsecure_close()

void pqsecure_close ( PGconn conn)

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

References pgtls_close(), and pg_conn::ssl_in_use.

Referenced by pqDropConnection().

198 {
199 #ifdef USE_SSL
200  if (conn->ssl_in_use)
201  pgtls_close(conn);
202 #endif
203 }
bool ssl_in_use
Definition: libpq-int.h:458
void pgtls_close(PGconn *conn)

◆ pqsecure_initialize()

int pqsecure_initialize ( PGconn conn)

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

References pgtls_init().

Referenced by PQconnectPoll().

169 {
170  int r = 0;
171 
172 #ifdef USE_SSL
173  r = pgtls_init(conn);
174 #endif
175 
176  return r;
177 }
int pgtls_init(PGconn *conn)

◆ pqsecure_open_client()

PostgresPollingStatusType pqsecure_open_client ( PGconn conn)

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

References PGRES_POLLING_FAILED, and pgtls_open_client().

Referenced by PQconnectPoll().

184 {
185 #ifdef USE_SSL
186  return pgtls_open_client(conn);
187 #else
188  /* shouldn't get here */
189  return PGRES_POLLING_FAILED;
190 #endif
191 }
PostgresPollingStatusType pgtls_open_client(PGconn *conn)

◆ pqsecure_raw_read()

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

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

References EAGAIN, ECONNRESET, EINTR, pg_conn::errorMessage, EWOULDBLOCK, libpq_gettext, printfPQExpBuffer(), recv, pg_conn::sock, SOCK_ERRNO, SOCK_ERRNO_SET, and SOCK_STRERROR.

Referenced by my_sock_read(), and pqsecure_read().

233 {
234  ssize_t n;
235  int result_errno = 0;
236  char sebuf[256];
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 #ifdef ECONNRESET
258  case ECONNRESET:
261  "server closed the connection unexpectedly\n"
262  "\tThis probably means the server terminated abnormally\n"
263  "\tbefore or while processing the request.\n"));
264  break;
265 #endif
266 
267  default:
269  libpq_gettext("could not receive data from server: %s\n"),
270  SOCK_STRERROR(result_errno,
271  sebuf, sizeof(sebuf)));
272  break;
273  }
274  }
275 
276  /* ensure we return the intended errno to caller */
277  SOCK_ERRNO_SET(result_errno);
278 
279  return n;
280 }
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
#define EAGAIN
Definition: win32_port.h:332
#define recv(s, buf, len, flags)
Definition: win32_port.h:448
#define SOCK_STRERROR
Definition: libpq-int.h:774
#define SOCK_ERRNO
Definition: libpq-int.h:773
#define SOCK_ERRNO_SET(e)
Definition: libpq-int.h:775
pgsocket sock
Definition: libpq-int.h:401
PQExpBufferData errorMessage
Definition: libpq-int.h:494
#define ECONNRESET
Definition: win32_port.h:344
#define EWOULDBLOCK
Definition: win32_port.h:340
#define EINTR
Definition: win32_port.h:334
#define libpq_gettext(x)
Definition: libpq-int.h:760

◆ pqsecure_raw_write()

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

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

References DECLARE_SIGPIPE_INFO, DISABLE_SIGPIPE, EAGAIN, ECONNRESET, EINTR, pg_conn::errorMessage, EWOULDBLOCK, libpq_gettext, printfPQExpBuffer(), REMEMBER_EPIPE, RESTORE_SIGPIPE, send, pg_conn::sigpipe_flag, pg_conn::sock, SOCK_ERRNO, SOCK_ERRNO_SET, and SOCK_STRERROR.

Referenced by my_sock_write(), and pqsecure_write().

310 {
311  ssize_t n;
312  int flags = 0;
313  int result_errno = 0;
314  char sebuf[256];
315 
316  DECLARE_SIGPIPE_INFO(spinfo);
317 
318 #ifdef MSG_NOSIGNAL
319  if (conn->sigpipe_flag)
320  flags |= MSG_NOSIGNAL;
321 
322 retry_masked:
323 #endif /* MSG_NOSIGNAL */
324 
325  DISABLE_SIGPIPE(conn, spinfo, return -1);
326 
327  n = send(conn->sock, ptr, len, flags);
328 
329  if (n < 0)
330  {
331  result_errno = SOCK_ERRNO;
332 
333  /*
334  * If we see an EINVAL, it may be because MSG_NOSIGNAL isn't available
335  * on this machine. So, clear sigpipe_flag so we don't try the flag
336  * again, and retry the send().
337  */
338 #ifdef MSG_NOSIGNAL
339  if (flags != 0 && result_errno == EINVAL)
340  {
341  conn->sigpipe_flag = false;
342  flags = 0;
343  goto retry_masked;
344  }
345 #endif /* MSG_NOSIGNAL */
346 
347  /* Set error message if appropriate */
348  switch (result_errno)
349  {
350 #ifdef EAGAIN
351  case EAGAIN:
352 #endif
353 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
354  case EWOULDBLOCK:
355 #endif
356  case EINTR:
357  /* no error message, caller is expected to retry */
358  break;
359 
360  case EPIPE:
361  /* Set flag for EPIPE */
362  REMEMBER_EPIPE(spinfo, true);
363  /* FALL THRU */
364 
365 #ifdef ECONNRESET
366  case ECONNRESET:
367 #endif
370  "server closed the connection unexpectedly\n"
371  "\tThis probably means the server terminated abnormally\n"
372  "\tbefore or while processing the request.\n"));
373  break;
374 
375  default:
377  libpq_gettext("could not send data to server: %s\n"),
378  SOCK_STRERROR(result_errno,
379  sebuf, sizeof(sebuf)));
380  break;
381  }
382  }
383 
384  RESTORE_SIGPIPE(conn, spinfo);
385 
386  /* ensure we return the intended errno to caller */
387  SOCK_ERRNO_SET(result_errno);
388 
389  return n;
390 }
bool sigpipe_flag
Definition: libpq-int.h:411
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
#define EAGAIN
Definition: win32_port.h:332
#define DECLARE_SIGPIPE_INFO(spinfo)
Definition: fe-secure.c:103
#define SOCK_STRERROR
Definition: libpq-int.h:774
#define SOCK_ERRNO
Definition: libpq-int.h:773
#define RESTORE_SIGPIPE(conn, spinfo)
Definition: fe-secure.c:113
#define SOCK_ERRNO_SET(e)
Definition: libpq-int.h:775
pgsocket sock
Definition: libpq-int.h:401
#define REMEMBER_EPIPE(spinfo, cond)
Definition: fe-secure.c:111
PQExpBufferData errorMessage
Definition: libpq-int.h:494
#define ECONNRESET
Definition: win32_port.h:344
#define EWOULDBLOCK
Definition: win32_port.h:340
#define DISABLE_SIGPIPE(conn, spinfo, failaction)
Definition: fe-secure.c:105
#define EINTR
Definition: win32_port.h:334
#define libpq_gettext(x)
Definition: libpq-int.h:760
#define send(s, buf, len, flags)
Definition: win32_port.h:449

◆ pqsecure_read()

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

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

References pgtls_read(), pqsecure_raw_read(), and pg_conn::ssl_in_use.

Referenced by pqReadData().

214 {
215  ssize_t n;
216 
217 #ifdef USE_SSL
218  if (conn->ssl_in_use)
219  {
220  n = pgtls_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 pqsecure_raw_read(PGconn *conn, void *ptr, size_t len)
Definition: fe-secure.c:232
bool ssl_in_use
Definition: libpq-int.h:458
ssize_t pgtls_read(PGconn *conn, void *ptr, size_t len)

◆ pqsecure_write()

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

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

References pgtls_write(), pqsecure_raw_write(), and pg_conn::ssl_in_use.

Referenced by pqSendSome().

291 {
292  ssize_t n;
293 
294 #ifdef USE_SSL
295  if (conn->ssl_in_use)
296  {
297  n = pgtls_write(conn, ptr, len);
298  }
299  else
300 #endif
301  {
302  n = pqsecure_raw_write(conn, ptr, len);
303  }
304 
305  return n;
306 }
bool ssl_in_use
Definition: libpq-int.h:458
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:309

◆ PQsslAttribute()

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

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

References snprintf(), and SSL_get_current_compression.

409 {
410  return NULL;
411 }

◆ PQsslAttributeNames()

const char* const* PQsslAttributeNames ( PGconn conn)

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

References SIGPIPE, sigwait(), SOCK_ERRNO, and SOCK_ERRNO_SET.

415 {
416  static const char *const result[] = {NULL};
417 
418  return result;
419 }

◆ PQsslInUse()

int PQsslInUse ( PGconn conn)

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

References pg_conn::ssl_in_use.

Referenced by printSSLInfo().

134 {
135  if (!conn)
136  return 0;
137  return conn->ssl_in_use;
138 }
bool ssl_in_use
Definition: libpq-int.h:458

◆ PQsslStruct()

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

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

403 {
404  return NULL;
405 }