libpq-be.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*-------------------------------------------------------------------------
  2. *
  3. * libpq_be.h
  4. * This file contains definitions for structures and externs used
  5. * by the postmaster during client authentication.
  6. *
  7. * Note that this is backend-internal and is NOT exported to clients.
  8. * Structs that need to be client-visible are in pqcomm.h.
  9. *
  10. *
  11. * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
  12. * Portions Copyright (c) 1994, Regents of the University of California
  13. *
  14. * src/include/libpq/libpq-be.h
  15. *
  16. *-------------------------------------------------------------------------
  17. */
  18. #ifndef LIBPQ_BE_H
  19. #define LIBPQ_BE_H
  20. #ifdef HAVE_SYS_TIME_H
  21. #include <sys/time.h>
  22. #endif
  23. #ifdef USE_OPENSSL
  24. #include <openssl/ssl.h>
  25. #include <openssl/err.h>
  26. #endif
  27. #ifdef HAVE_NETINET_TCP_H
  28. #include <netinet/tcp.h>
  29. #endif
  30. #ifdef ENABLE_GSS
  31. #if defined(HAVE_GSSAPI_H)
  32. #include <gssapi.h>
  33. #else
  34. #include <gssapi/gssapi.h>
  35. #endif /* HAVE_GSSAPI_H */
  36. /*
  37. * GSSAPI brings in headers that set a lot of things in the global namespace on win32,
  38. * that doesn't match the msvc build. It gives a bunch of compiler warnings that we ignore,
  39. * but also defines a symbol that simply does not exist. Undefine it again.
  40. */
  41. #ifdef WIN32_ONLY_COMPILER
  42. #undef HAVE_GETADDRINFO
  43. #endif
  44. #endif /* ENABLE_GSS */
  45. #ifdef ENABLE_SSPI
  46. #define SECURITY_WIN32
  47. #if defined(WIN32) && !defined(WIN32_ONLY_COMPILER)
  48. #include <ntsecapi.h>
  49. #endif
  50. #include <security.h>
  51. #undef SECURITY_WIN32
  52. #ifndef ENABLE_GSS
  53. /*
  54. * Define a fake structure compatible with GSSAPI on Unix.
  55. */
  56. typedef struct
  57. {
  58. void *value;
  59. int length;
  60. } gss_buffer_desc;
  61. #endif
  62. #endif /* ENABLE_SSPI */
  63. #include "datatype/timestamp.h"
  64. #include "libpq/hba.h"
  65. #include "libpq/pqcomm.h"
  66. typedef enum CAC_state
  67. {
  68. CAC_OK, CAC_STARTUP, CAC_SHUTDOWN, CAC_RECOVERY, CAC_TOOMANY,
  69. CAC_WAITBACKUP
  70. } CAC_state;
  71. /*
  72. * GSSAPI specific state information
  73. */
  74. #if defined(ENABLE_GSS) | defined(ENABLE_SSPI)
  75. typedef struct
  76. {
  77. gss_buffer_desc outbuf; /* GSSAPI output token buffer */
  78. #ifdef ENABLE_GSS
  79. gss_cred_id_t cred; /* GSSAPI connection cred's */
  80. gss_ctx_id_t ctx; /* GSSAPI connection context */
  81. gss_name_t name; /* GSSAPI client name */
  82. #endif
  83. } pg_gssinfo;
  84. #endif
  85. /*
  86. * This is used by the postmaster in its communication with frontends. It
  87. * contains all state information needed during this communication before the
  88. * backend is run. The Port structure is kept in malloc'd memory and is
  89. * still available when a backend is running (see MyProcPort). The data
  90. * it points to must also be malloc'd, or else palloc'd in TopMemoryContext,
  91. * so that it survives into PostgresMain execution!
  92. *
  93. * remote_hostname is set if we did a successful reverse lookup of the
  94. * client's IP address during connection setup.
  95. * remote_hostname_resolv tracks the state of hostname verification:
  96. * +1 = remote_hostname is known to resolve to client's IP address
  97. * -1 = remote_hostname is known NOT to resolve to client's IP address
  98. * 0 = we have not done the forward DNS lookup yet
  99. * -2 = there was an error in name resolution
  100. * If reverse lookup of the client IP address fails, remote_hostname will be
  101. * left NULL while remote_hostname_resolv is set to -2. If reverse lookup
  102. * succeeds but forward lookup fails, remote_hostname_resolv is also set to -2
  103. * (the case is distinguishable because remote_hostname isn't NULL). In
  104. * either of the -2 cases, remote_hostname_errcode saves the lookup return
  105. * code for possible later use with gai_strerror.
  106. */
  107. typedef struct Port
  108. {
  109. pgsocket sock; /* File descriptor */
  110. bool noblock; /* is the socket in non-blocking mode? */
  111. ProtocolVersion proto; /* FE/BE protocol version */
  112. SockAddr laddr; /* local addr (postmaster) */
  113. SockAddr raddr; /* remote addr (client) */
  114. char *remote_host; /* name (or ip addr) of remote host */
  115. char *remote_hostname;/* name (not ip addr) of remote host, if
  116. * available */
  117. int remote_hostname_resolv; /* see above */
  118. int remote_hostname_errcode; /* see above */
  119. char *remote_port; /* text rep of remote port */
  120. CAC_state canAcceptConnections; /* postmaster connection status */
  121. /*
  122. * Information that needs to be saved from the startup packet and passed
  123. * into backend execution. "char *" fields are NULL if not set.
  124. * guc_options points to a List of alternating option names and values.
  125. */
  126. char *database_name;
  127. char *user_name;
  128. char *cmdline_options;
  129. List *guc_options;
  130. /*
  131. * Information that needs to be held during the authentication cycle.
  132. */
  133. HbaLine *hba;
  134. char md5Salt[4]; /* Password salt */
  135. /*
  136. * Information that really has no business at all being in struct Port,
  137. * but since it gets used by elog.c in the same way as database_name and
  138. * other members of this struct, we may as well keep it here.
  139. */
  140. TimestampTz SessionStartTime; /* backend start time */
  141. /*
  142. * TCP keepalive settings.
  143. *
  144. * default values are 0 if AF_UNIX or not yet known; current values are 0
  145. * if AF_UNIX or using the default. Also, -1 in a default value means we
  146. * were unable to find out the default (getsockopt failed).
  147. */
  148. int default_keepalives_idle;
  149. int default_keepalives_interval;
  150. int default_keepalives_count;
  151. int keepalives_idle;
  152. int keepalives_interval;
  153. int keepalives_count;
  154. #if defined(ENABLE_GSS) || defined(ENABLE_SSPI)
  155. /*
  156. * If GSSAPI is supported, store GSSAPI information. Otherwise, store a
  157. * NULL pointer to make sure offsets in the struct remain the same.
  158. */
  159. pg_gssinfo *gss;
  160. #else
  161. void *gss;
  162. #endif
  163. /*
  164. * SSL structures.
  165. */
  166. bool ssl_in_use;
  167. char *peer_cn;
  168. bool peer_cert_valid;
  169. /*
  170. * OpenSSL structures. (Keep these last so that the locations of other
  171. * fields are the same whether or not you build with OpenSSL.)
  172. */
  173. #ifdef USE_OPENSSL
  174. SSL *ssl;
  175. X509 *peer;
  176. unsigned long count;
  177. #endif
  178. } Port;
  179. #ifdef USE_SSL
  180. /*
  181. * These functions are implemented by the glue code specific to each
  182. * SSL implementation (e.g. be-secure-openssl.c)
  183. */
  184. extern void be_tls_init(void);
  185. extern int be_tls_open_server(Port *port);
  186. extern void be_tls_close(Port *port);
  187. extern ssize_t be_tls_read(Port *port, void *ptr, size_t len, int *waitfor);
  188. extern ssize_t be_tls_write(Port *port, void *ptr, size_t len, int *waitfor);
  189. extern int be_tls_get_cipher_bits(Port *port);
  190. extern bool be_tls_get_compression(Port *port);
  191. extern void be_tls_get_version(Port *port, char *ptr, size_t len);
  192. extern void be_tls_get_cipher(Port *port, char *ptr, size_t len);
  193. extern void be_tls_get_peerdn_name(Port *port, char *ptr, size_t len);
  194. #endif
  195. extern ProtocolVersion FrontendProtocol;
  196. /* TCP keepalives configuration. These are no-ops on an AF_UNIX socket. */
  197. extern int pq_getkeepalivesidle(Port *port);
  198. extern int pq_getkeepalivesinterval(Port *port);
  199. extern int pq_getkeepalivescount(Port *port);
  200. extern int pq_setkeepalivesidle(int idle, Port *port);
  201. extern int pq_setkeepalivesinterval(int interval, Port *port);
  202. extern int pq_setkeepalivescount(int count, Port *port);
  203. #endif /* LIBPQ_BE_H */