gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

319 line
9.6KB

  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-2019, 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. #include <sys/time.h>
  21. #ifdef USE_OPENSSL
  22. #include <openssl/ssl.h>
  23. #include <openssl/err.h>
  24. #endif
  25. #ifdef HAVE_NETINET_TCP_H
  26. #include <netinet/tcp.h>
  27. #endif
  28. #ifdef ENABLE_GSS
  29. #if defined(HAVE_GSSAPI_H)
  30. #include <gssapi.h>
  31. #else
  32. #include <gssapi/gssapi.h>
  33. #endif /* HAVE_GSSAPI_H */
  34. /*
  35. * GSSAPI brings in headers that set a lot of things in the global namespace on win32,
  36. * that doesn't match the msvc build. It gives a bunch of compiler warnings that we ignore,
  37. * but also defines a symbol that simply does not exist. Undefine it again.
  38. */
  39. #ifdef _MSC_VER
  40. #undef HAVE_GETADDRINFO
  41. #endif
  42. #endif /* ENABLE_GSS */
  43. #ifdef ENABLE_SSPI
  44. #define SECURITY_WIN32
  45. #if defined(WIN32) && !defined(_MSC_VER)
  46. #include <ntsecapi.h>
  47. #endif
  48. #include <security.h>
  49. #undef SECURITY_WIN32
  50. #ifndef ENABLE_GSS
  51. /*
  52. * Define a fake structure compatible with GSSAPI on Unix.
  53. */
  54. typedef struct
  55. {
  56. void *value;
  57. int length;
  58. } gss_buffer_desc;
  59. #endif
  60. #endif /* ENABLE_SSPI */
  61. #include "datatype/timestamp.h"
  62. #include "libpq/hba.h"
  63. #include "libpq/pqcomm.h"
  64. typedef enum CAC_state
  65. {
  66. CAC_OK, CAC_STARTUP, CAC_SHUTDOWN, CAC_RECOVERY, CAC_TOOMANY,
  67. CAC_WAITBACKUP
  68. } CAC_state;
  69. /*
  70. * GSSAPI specific state information
  71. */
  72. #if defined(ENABLE_GSS) | defined(ENABLE_SSPI)
  73. typedef struct
  74. {
  75. gss_buffer_desc outbuf; /* GSSAPI output token buffer */
  76. #ifdef ENABLE_GSS
  77. gss_cred_id_t cred; /* GSSAPI connection cred's */
  78. gss_ctx_id_t ctx; /* GSSAPI connection context */
  79. gss_name_t name; /* GSSAPI client name */
  80. char *princ; /* GSSAPI Principal used for auth, NULL if
  81. * GSSAPI auth was not used */
  82. bool auth; /* GSSAPI Authentication used */
  83. bool enc; /* GSSAPI encryption in use */
  84. #endif
  85. } pg_gssinfo;
  86. #endif
  87. /*
  88. * This is used by the postmaster in its communication with frontends. It
  89. * contains all state information needed during this communication before the
  90. * backend is run. The Port structure is kept in malloc'd memory and is
  91. * still available when a backend is running (see MyProcPort). The data
  92. * it points to must also be malloc'd, or else palloc'd in TopMemoryContext,
  93. * so that it survives into PostgresMain execution!
  94. *
  95. * remote_hostname is set if we did a successful reverse lookup of the
  96. * client's IP address during connection setup.
  97. * remote_hostname_resolv tracks the state of hostname verification:
  98. * +1 = remote_hostname is known to resolve to client's IP address
  99. * -1 = remote_hostname is known NOT to resolve to client's IP address
  100. * 0 = we have not done the forward DNS lookup yet
  101. * -2 = there was an error in name resolution
  102. * If reverse lookup of the client IP address fails, remote_hostname will be
  103. * left NULL while remote_hostname_resolv is set to -2. If reverse lookup
  104. * succeeds but forward lookup fails, remote_hostname_resolv is also set to -2
  105. * (the case is distinguishable because remote_hostname isn't NULL). In
  106. * either of the -2 cases, remote_hostname_errcode saves the lookup return
  107. * code for possible later use with gai_strerror.
  108. */
  109. typedef struct Port
  110. {
  111. pgsocket sock; /* File descriptor */
  112. bool noblock; /* is the socket in non-blocking mode? */
  113. ProtocolVersion proto; /* FE/BE protocol version */
  114. SockAddr laddr; /* local addr (postmaster) */
  115. SockAddr raddr; /* remote addr (client) */
  116. char *remote_host; /* name (or ip addr) of remote host */
  117. char *remote_hostname; /* name (not ip addr) of remote host, if
  118. * available */
  119. int remote_hostname_resolv; /* see above */
  120. int remote_hostname_errcode; /* see above */
  121. char *remote_port; /* text rep of remote port */
  122. CAC_state canAcceptConnections; /* postmaster connection status */
  123. /*
  124. * Information that needs to be saved from the startup packet and passed
  125. * into backend execution. "char *" fields are NULL if not set.
  126. * guc_options points to a List of alternating option names and values.
  127. */
  128. char *database_name;
  129. char *user_name;
  130. char *cmdline_options;
  131. List *guc_options;
  132. /*
  133. * The startup packet application name, only used here for the "connection
  134. * authorized" log message. We shouldn't use this post-startup, instead
  135. * the GUC should be used as application can change it afterward.
  136. */
  137. char *application_name;
  138. /*
  139. * Information that needs to be held during the authentication cycle.
  140. */
  141. HbaLine *hba;
  142. /*
  143. * TCP keepalive and user timeout settings.
  144. *
  145. * default values are 0 if AF_UNIX or not yet known; current values are 0
  146. * if AF_UNIX or using the default. Also, -1 in a default value means we
  147. * were unable to find out the default (getsockopt failed).
  148. */
  149. int default_keepalives_idle;
  150. int default_keepalives_interval;
  151. int default_keepalives_count;
  152. int default_tcp_user_timeout;
  153. int keepalives_idle;
  154. int keepalives_interval;
  155. int keepalives_count;
  156. int tcp_user_timeout;
  157. /*
  158. * GSSAPI structures.
  159. */
  160. #if defined(ENABLE_GSS) || defined(ENABLE_SSPI)
  161. /*
  162. * If GSSAPI is supported, store GSSAPI information. Otherwise, store a
  163. * NULL pointer to make sure offsets in the struct remain the same.
  164. */
  165. pg_gssinfo *gss;
  166. #else
  167. void *gss;
  168. #endif
  169. /*
  170. * SSL structures.
  171. */
  172. bool ssl_in_use;
  173. char *peer_cn;
  174. bool peer_cert_valid;
  175. /*
  176. * OpenSSL structures. (Keep these last so that the locations of other
  177. * fields are the same whether or not you build with OpenSSL.)
  178. */
  179. #ifdef USE_OPENSSL
  180. SSL *ssl;
  181. X509 *peer;
  182. #endif
  183. } Port;
  184. #ifdef USE_SSL
  185. /*
  186. * Hardcoded DH parameters, used in ephemeral DH keying. (See also
  187. * README.SSL for more details on EDH.)
  188. *
  189. * If you want to create your own hardcoded DH parameters
  190. * for fun and profit, review "Assigned Number for SKIP
  191. * Protocols" (http://www.skip-vpn.org/spec/numbers.html)
  192. * for suggestions.
  193. */
  194. #define FILE_DH2048 \
  195. "-----BEGIN DH PARAMETERS-----\n\
  196. MIIBCAKCAQEA9kJXtwh/CBdyorrWqULzBej5UxE5T7bxbrlLOCDaAadWoxTpj0BV\n\
  197. 89AHxstDqZSt90xkhkn4DIO9ZekX1KHTUPj1WV/cdlJPPT2N286Z4VeSWc39uK50\n\
  198. T8X8dryDxUcwYc58yWb/Ffm7/ZFexwGq01uejaClcjrUGvC/RgBYK+X0iP1YTknb\n\
  199. zSC0neSRBzZrM2w4DUUdD3yIsxx8Wy2O9vPJI8BD8KVbGI2Ou1WMuF040zT9fBdX\n\
  200. Q6MdGGzeMyEstSr/POGxKUAYEY18hKcKctaGxAMZyAcpesqVDNmWn6vQClCbAkbT\n\
  201. CD1mpF1Bn5x8vYlLIhkmuquiXsNV6TILOwIBAg==\n\
  202. -----END DH PARAMETERS-----\n"
  203. /*
  204. * These functions are implemented by the glue code specific to each
  205. * SSL implementation (e.g. be-secure-openssl.c)
  206. */
  207. /*
  208. * Initialize global SSL context.
  209. *
  210. * If isServerStart is true, report any errors as FATAL (so we don't return).
  211. * Otherwise, log errors at LOG level and return -1 to indicate trouble,
  212. * preserving the old SSL state if any. Returns 0 if OK.
  213. */
  214. extern int be_tls_init(bool isServerStart);
  215. /*
  216. * Destroy global SSL context, if any.
  217. */
  218. extern void be_tls_destroy(void);
  219. /*
  220. * Attempt to negotiate SSL connection.
  221. */
  222. extern int be_tls_open_server(Port *port);
  223. /*
  224. * Close SSL connection.
  225. */
  226. extern void be_tls_close(Port *port);
  227. /*
  228. * Read data from a secure connection.
  229. */
  230. extern ssize_t be_tls_read(Port *port, void *ptr, size_t len, int *waitfor);
  231. /*
  232. * Write data to a secure connection.
  233. */
  234. extern ssize_t be_tls_write(Port *port, void *ptr, size_t len, int *waitfor);
  235. /*
  236. * Return information about the SSL connection.
  237. */
  238. extern int be_tls_get_cipher_bits(Port *port);
  239. extern bool be_tls_get_compression(Port *port);
  240. extern const char *be_tls_get_version(Port *port);
  241. extern const char *be_tls_get_cipher(Port *port);
  242. extern void be_tls_get_peer_subject_name(Port *port, char *ptr, size_t len);
  243. extern void be_tls_get_peer_issuer_name(Port *port, char *ptr, size_t len);
  244. extern void be_tls_get_peer_serial(Port *port, char *ptr, size_t len);
  245. /*
  246. * Get the server certificate hash for SCRAM channel binding type
  247. * tls-server-end-point.
  248. *
  249. * The result is a palloc'd hash of the server certificate with its
  250. * size, and NULL if there is no certificate available.
  251. *
  252. * This is not supported with old versions of OpenSSL that don't have
  253. * the X509_get_signature_nid() function.
  254. */
  255. #if defined(USE_OPENSSL) && defined(HAVE_X509_GET_SIGNATURE_NID)
  256. #define HAVE_BE_TLS_GET_CERTIFICATE_HASH
  257. extern char *be_tls_get_certificate_hash(Port *port, size_t *len);
  258. #endif
  259. #endif /* USE_SSL */
  260. #ifdef ENABLE_GSS
  261. /*
  262. * Return information about the GSSAPI authenticated connection
  263. */
  264. extern bool be_gssapi_get_auth(Port *port);
  265. extern bool be_gssapi_get_enc(Port *port);
  266. extern const char *be_gssapi_get_princ(Port *port);
  267. /* Read and write to a GSSAPI-encrypted connection. */
  268. extern ssize_t be_gssapi_read(Port *port, void *ptr, size_t len);
  269. extern ssize_t be_gssapi_write(Port *port, void *ptr, size_t len);
  270. #endif /* ENABLE_GSS */
  271. extern ProtocolVersion FrontendProtocol;
  272. /* TCP keepalives configuration. These are no-ops on an AF_UNIX socket. */
  273. extern int pq_getkeepalivesidle(Port *port);
  274. extern int pq_getkeepalivesinterval(Port *port);
  275. extern int pq_getkeepalivescount(Port *port);
  276. extern int pq_gettcpusertimeout(Port *port);
  277. extern int pq_setkeepalivesidle(int idle, Port *port);
  278. extern int pq_setkeepalivesinterval(int interval, Port *port);
  279. extern int pq_setkeepalivescount(int count, Port *port);
  280. extern int pq_settcpusertimeout(int timeout, Port *port);
  281. #endif /* LIBPQ_BE_H */
上海开阖软件有限公司 沪ICP备12045867号-1