PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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

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)
 
int PQsslInUse (PGconn *conn)
 
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

#define DECLARE_SIGPIPE_INFO (   spinfo)    pqsigfunc spinfo = NULL

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

Referenced by pqsecure_raw_write().

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

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

Referenced by pqsecure_raw_write().

#define REMEMBER_EPIPE (   spinfo,
  cond 
)

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

Referenced by pqsecure_raw_write().

#define RESTORE_SIGPIPE (   conn,
  spinfo 
)
Value:
do { \
pqsignal(SIGPIPE, spinfo); \
} while (0)
#define SIGPIPE_MASKED(conn)
Definition: fe-secure.c:65
PGconn * conn
Definition: streamutil.c:42
#define SIGPIPE
Definition: win32.h:193
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:168

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

Referenced by pqsecure_raw_write().

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

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

Function Documentation

void* PQgetssl ( PGconn conn)

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

References NULL.

395 {
396  return NULL;
397 }
#define NULL
Definition: c.h:229
void PQinitOpenSSL ( int  do_ssl,
int  do_crypto 
)

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

References pgtls_init_library().

150 {
151 #ifdef USE_SSL
152  pgtls_init_library(do_ssl, do_crypto);
153 #endif
154 }
void pgtls_init_library(bool do_ssl, int do_crypto)
void PQinitSSL ( int  do_init)

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

References pgtls_init_library().

138 {
139 #ifdef USE_SSL
141 #endif
142 }
static void do_init(void)
Definition: pg_ctl.c:837
void pgtls_init_library(bool do_ssl, int do_crypto)
void pqsecure_close ( PGconn conn)

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

References pgtls_close().

Referenced by pqDropConnection().

190 {
191 #ifdef USE_SSL
192  if (conn->ssl_in_use)
193  pgtls_close(conn);
194 #endif
195 }
void pgtls_close(PGconn *conn)
int pqsecure_initialize ( PGconn conn)

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

References pgtls_init().

Referenced by PQconnectPoll().

161 {
162  int r = 0;
163 
164 #ifdef USE_SSL
165  r = pgtls_init(conn);
166 #endif
167 
168  return r;
169 }
int pgtls_init(PGconn *conn)
PostgresPollingStatusType pqsecure_open_client ( PGconn conn)

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

References PGRES_POLLING_FAILED, and pgtls_open_client().

Referenced by PQconnectPoll().

176 {
177 #ifdef USE_SSL
178  return pgtls_open_client(conn);
179 #else
180  /* shouldn't get here */
181  return PGRES_POLLING_FAILED;
182 #endif
183 }
PostgresPollingStatusType pgtls_open_client(PGconn *conn)
ssize_t pqsecure_raw_read ( PGconn conn,
void *  ptr,
size_t  len 
)

Definition at line 224 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().

225 {
226  ssize_t n;
227  int result_errno = 0;
228  char sebuf[256];
229 
230  n = recv(conn->sock, ptr, len, 0);
231 
232  if (n < 0)
233  {
234  result_errno = SOCK_ERRNO;
235 
236  /* Set error message if appropriate */
237  switch (result_errno)
238  {
239 #ifdef EAGAIN
240  case EAGAIN:
241 #endif
242 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
243  case EWOULDBLOCK:
244 #endif
245  case EINTR:
246  /* no error message, caller is expected to retry */
247  break;
248 
249 #ifdef ECONNRESET
250  case ECONNRESET:
253  "server closed the connection unexpectedly\n"
254  "\tThis probably means the server terminated abnormally\n"
255  "\tbefore or while processing the request.\n"));
256  break;
257 #endif
258 
259  default:
261  libpq_gettext("could not receive data from server: %s\n"),
262  SOCK_STRERROR(result_errno,
263  sebuf, sizeof(sebuf)));
264  break;
265  }
266  }
267 
268  /* ensure we return the intended errno to caller */
269  SOCK_ERRNO_SET(result_errno);
270 
271  return n;
272 }
#define EWOULDBLOCK
Definition: win32.h:291
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
#define recv(s, buf, len, flags)
Definition: win32.h:375
#define ECONNRESET
Definition: win32.h:295
#define EAGAIN
Definition: win32.h:283
#define SOCK_STRERROR
Definition: libpq-int.h:697
#define SOCK_ERRNO
Definition: libpq-int.h:696
#define SOCK_ERRNO_SET(e)
Definition: libpq-int.h:698
pgsocket sock
Definition: libpq-int.h:400
#define EINTR
Definition: win32.h:285
PQExpBufferData errorMessage
Definition: libpq-int.h:492
#define libpq_gettext(x)
Definition: libpq-int.h:683
ssize_t pqsecure_raw_write ( PGconn conn,
const void *  ptr,
size_t  len 
)

Definition at line 301 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().

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

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

References pgtls_read(), and pqsecure_raw_read().

Referenced by pqReadData().

206 {
207  ssize_t n;
208 
209 #ifdef USE_SSL
210  if (conn->ssl_in_use)
211  {
212  n = pgtls_read(conn, ptr, len);
213  }
214  else
215 #endif
216  {
217  n = pqsecure_raw_read(conn, ptr, len);
218  }
219 
220  return n;
221 }
ssize_t pqsecure_raw_read(PGconn *conn, void *ptr, size_t len)
Definition: fe-secure.c:224
ssize_t pgtls_read(PGconn *conn, void *ptr, size_t len)
ssize_t pqsecure_write ( PGconn conn,
const void *  ptr,
size_t  len 
)

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

References pgtls_write(), and pqsecure_raw_write().

Referenced by pqSendSome().

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  {
294  n = pqsecure_raw_write(conn, ptr, len);
295  }
296 
297  return n;
298 }
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:301
const char* PQsslAttribute ( PGconn conn,
const char *  attribute_name 
)

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

References NULL, snprintf(), and SSL_get_current_compression.

407 {
408  return NULL;
409 }
#define NULL
Definition: c.h:229
const char* const* PQsslAttributeNames ( PGconn conn)

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

References NULL, and result.

413 {
414  static const char *const result[] = {NULL};
415 
416  return result;
417 }
return result
Definition: formatting.c:1618
#define NULL
Definition: c.h:229
int PQsslInUse ( PGconn conn)

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

389 {
390  return 0;
391 }
void* PQsslStruct ( PGconn conn,
const char *  struct_name 
)

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

References NULL.

401 {
402  return NULL;
403 }
#define NULL
Definition: c.h:229