2
0

libpq-be.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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-2022, 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,
  67. CAC_STARTUP,
  68. CAC_SHUTDOWN,
  69. CAC_RECOVERY,
  70. CAC_NOTCONSISTENT,
  71. CAC_TOOMANY
  72. } CAC_state;
  73. /*
  74. * GSSAPI specific state information
  75. */
  76. #if defined(ENABLE_GSS) | defined(ENABLE_SSPI)
  77. typedef struct
  78. {
  79. gss_buffer_desc outbuf; /* GSSAPI output token buffer */
  80. #ifdef ENABLE_GSS
  81. gss_cred_id_t cred; /* GSSAPI connection cred's */
  82. gss_ctx_id_t ctx; /* GSSAPI connection context */
  83. gss_name_t name; /* GSSAPI client name */
  84. char *princ; /* GSSAPI Principal used for auth, NULL if
  85. * GSSAPI auth was not used */
  86. bool auth; /* GSSAPI Authentication used */
  87. bool enc; /* GSSAPI encryption in use */
  88. #endif
  89. } pg_gssinfo;
  90. #endif
  91. /*
  92. * This is used by the postmaster in its communication with frontends. It
  93. * contains all state information needed during this communication before the
  94. * backend is run. The Port structure is kept in malloc'd memory and is
  95. * still available when a backend is running (see MyProcPort). The data
  96. * it points to must also be malloc'd, or else palloc'd in TopMemoryContext,
  97. * so that it survives into PostgresMain execution!
  98. *
  99. * remote_hostname is set if we did a successful reverse lookup of the
  100. * client's IP address during connection setup.
  101. * remote_hostname_resolv tracks the state of hostname verification:
  102. * +1 = remote_hostname is known to resolve to client's IP address
  103. * -1 = remote_hostname is known NOT to resolve to client's IP address
  104. * 0 = we have not done the forward DNS lookup yet
  105. * -2 = there was an error in name resolution
  106. * If reverse lookup of the client IP address fails, remote_hostname will be
  107. * left NULL while remote_hostname_resolv is set to -2. If reverse lookup
  108. * succeeds but forward lookup fails, remote_hostname_resolv is also set to -2
  109. * (the case is distinguishable because remote_hostname isn't NULL). In
  110. * either of the -2 cases, remote_hostname_errcode saves the lookup return
  111. * code for possible later use with gai_strerror.
  112. */
  113. typedef struct Port
  114. {
  115. pgsocket sock; /* File descriptor */
  116. bool noblock; /* is the socket in non-blocking mode? */
  117. ProtocolVersion proto; /* FE/BE protocol version */
  118. SockAddr laddr; /* local addr (postmaster) */
  119. SockAddr raddr; /* remote addr (client) */
  120. char *remote_host; /* name (or ip addr) of remote host */
  121. char *remote_hostname; /* name (not ip addr) of remote host, if
  122. * available */
  123. int remote_hostname_resolv; /* see above */
  124. int remote_hostname_errcode; /* see above */
  125. char *remote_port; /* text rep of remote port */
  126. CAC_state canAcceptConnections; /* postmaster connection status */
  127. /*
  128. * Information that needs to be saved from the startup packet and passed
  129. * into backend execution. "char *" fields are NULL if not set.
  130. * guc_options points to a List of alternating option names and values.
  131. */
  132. char *database_name;
  133. char *user_name;
  134. char *cmdline_options;
  135. List *guc_options;
  136. /*
  137. * The startup packet application name, only used here for the "connection
  138. * authorized" log message. We shouldn't use this post-startup, instead
  139. * the GUC should be used as application can change it afterward.
  140. */
  141. char *application_name;
  142. /*
  143. * Information that needs to be held during the authentication cycle.
  144. */
  145. HbaLine *hba;
  146. /*
  147. * Authenticated identity. The meaning of this identifier is dependent on
  148. * hba->auth_method; it is the identity (if any) that the user presented
  149. * during the authentication cycle, before they were assigned a database
  150. * role. (It is effectively the "SYSTEM-USERNAME" of a pg_ident usermap
  151. * -- though the exact string in use may be different, depending on pg_hba
  152. * options.)
  153. *
  154. * authn_id is NULL if the user has not actually been authenticated, for
  155. * example if the "trust" auth method is in use.
  156. */
  157. const char *authn_id;
  158. /*
  159. * TCP keepalive and user timeout settings.
  160. *
  161. * default values are 0 if AF_UNIX or not yet known; current values are 0
  162. * if AF_UNIX or using the default. Also, -1 in a default value means we
  163. * were unable to find out the default (getsockopt failed).
  164. */
  165. int default_keepalives_idle;
  166. int default_keepalives_interval;
  167. int default_keepalives_count;
  168. int default_tcp_user_timeout;
  169. int keepalives_idle;
  170. int keepalives_interval;
  171. int keepalives_count;
  172. int tcp_user_timeout;
  173. /*
  174. * GSSAPI structures.
  175. */
  176. #if defined(ENABLE_GSS) || defined(ENABLE_SSPI)
  177. /*
  178. * If GSSAPI is supported and used on this connection, store GSSAPI
  179. * information. Even when GSSAPI is not compiled in, store a NULL pointer
  180. * to keep struct offsets the same (for extension ABI compatibility).
  181. */
  182. pg_gssinfo *gss;
  183. #else
  184. void *gss;
  185. #endif
  186. /*
  187. * SSL structures.
  188. */
  189. bool ssl_in_use;
  190. char *peer_cn;
  191. char *peer_dn;
  192. bool peer_cert_valid;
  193. /*
  194. * OpenSSL structures. (Keep these last so that the locations of other
  195. * fields are the same whether or not you build with SSL enabled.)
  196. */
  197. #ifdef USE_OPENSSL
  198. SSL *ssl;
  199. X509 *peer;
  200. #endif
  201. } Port;
  202. #ifdef USE_SSL
  203. /*
  204. * Hardcoded DH parameters, used in ephemeral DH keying. (See also
  205. * README.SSL for more details on EDH.)
  206. *
  207. * This is the 2048-bit DH parameter from RFC 3526. The generation of the
  208. * prime is specified in RFC 2412 Appendix E, which also discusses the
  209. * design choice of the generator. Note that when loaded with OpenSSL
  210. * this causes DH_check() to fail on DH_NOT_SUITABLE_GENERATOR, where
  211. * leaking a bit is preferred.
  212. */
  213. #define FILE_DH2048 \
  214. "-----BEGIN DH PARAMETERS-----\n\
  215. MIIBCAKCAQEA///////////JD9qiIWjCNMTGYouA3BzRKQJOCIpnzHQCC76mOxOb\n\
  216. IlFKCHmONATd75UZs806QxswKwpt8l8UN0/hNW1tUcJF5IW1dmJefsb0TELppjft\n\
  217. awv/XLb0Brft7jhr+1qJn6WunyQRfEsf5kkoZlHs5Fs9wgB8uKFjvwWY2kg2HFXT\n\
  218. mmkWP6j9JM9fg2VdI9yjrZYcYvNWIIVSu57VKQdwlpZtZww1Tkq8mATxdGwIyhgh\n\
  219. fDKQXkYuNs474553LBgOhgObJ4Oi7Aeij7XFXfBvTFLJ3ivL9pVYFxg5lUl86pVq\n\
  220. 5RXSJhiY+gUQFXKOWoqsqmj//////////wIBAg==\n\
  221. -----END DH PARAMETERS-----\n"
  222. /*
  223. * These functions are implemented by the glue code specific to each
  224. * SSL implementation (e.g. be-secure-openssl.c)
  225. */
  226. /*
  227. * Initialize global SSL context.
  228. *
  229. * If isServerStart is true, report any errors as FATAL (so we don't return).
  230. * Otherwise, log errors at LOG level and return -1 to indicate trouble,
  231. * preserving the old SSL state if any. Returns 0 if OK.
  232. */
  233. extern int be_tls_init(bool isServerStart);
  234. /*
  235. * Destroy global SSL context, if any.
  236. */
  237. extern void be_tls_destroy(void);
  238. /*
  239. * Attempt to negotiate SSL connection.
  240. */
  241. extern int be_tls_open_server(Port *port);
  242. /*
  243. * Close SSL connection.
  244. */
  245. extern void be_tls_close(Port *port);
  246. /*
  247. * Read data from a secure connection.
  248. */
  249. extern ssize_t be_tls_read(Port *port, void *ptr, size_t len, int *waitfor);
  250. /*
  251. * Write data to a secure connection.
  252. */
  253. extern ssize_t be_tls_write(Port *port, void *ptr, size_t len, int *waitfor);
  254. /*
  255. * Return information about the SSL connection.
  256. */
  257. extern int be_tls_get_cipher_bits(Port *port);
  258. extern const char *be_tls_get_version(Port *port);
  259. extern const char *be_tls_get_cipher(Port *port);
  260. extern void be_tls_get_peer_subject_name(Port *port, char *ptr, size_t len);
  261. extern void be_tls_get_peer_issuer_name(Port *port, char *ptr, size_t len);
  262. extern void be_tls_get_peer_serial(Port *port, char *ptr, size_t len);
  263. /*
  264. * Get the server certificate hash for SCRAM channel binding type
  265. * tls-server-end-point.
  266. *
  267. * The result is a palloc'd hash of the server certificate with its
  268. * size, and NULL if there is no certificate available.
  269. *
  270. * This is not supported with old versions of OpenSSL that don't have
  271. * the X509_get_signature_nid() function.
  272. */
  273. #if defined(USE_OPENSSL) && (defined(HAVE_X509_GET_SIGNATURE_NID) || defined(HAVE_X509_GET_SIGNATURE_INFO))
  274. #define HAVE_BE_TLS_GET_CERTIFICATE_HASH
  275. extern char *be_tls_get_certificate_hash(Port *port, size_t *len);
  276. #endif
  277. /* init hook for SSL, the default sets the password callback if appropriate */
  278. #ifdef USE_OPENSSL
  279. typedef void (*openssl_tls_init_hook_typ) (SSL_CTX *context, bool isServerStart);
  280. extern PGDLLIMPORT openssl_tls_init_hook_typ openssl_tls_init_hook;
  281. #endif
  282. #endif /* USE_SSL */
  283. #ifdef ENABLE_GSS
  284. /*
  285. * Return information about the GSSAPI authenticated connection
  286. */
  287. extern bool be_gssapi_get_auth(Port *port);
  288. extern bool be_gssapi_get_enc(Port *port);
  289. extern const char *be_gssapi_get_princ(Port *port);
  290. /* Read and write to a GSSAPI-encrypted connection. */
  291. extern ssize_t be_gssapi_read(Port *port, void *ptr, size_t len);
  292. extern ssize_t be_gssapi_write(Port *port, void *ptr, size_t len);
  293. #endif /* ENABLE_GSS */
  294. extern PGDLLIMPORT ProtocolVersion FrontendProtocol;
  295. /* TCP keepalives configuration. These are no-ops on an AF_UNIX socket. */
  296. extern int pq_getkeepalivesidle(Port *port);
  297. extern int pq_getkeepalivesinterval(Port *port);
  298. extern int pq_getkeepalivescount(Port *port);
  299. extern int pq_gettcpusertimeout(Port *port);
  300. extern int pq_setkeepalivesidle(int idle, Port *port);
  301. extern int pq_setkeepalivesinterval(int interval, Port *port);
  302. extern int pq_setkeepalivescount(int count, Port *port);
  303. extern int pq_settcpusertimeout(int timeout, Port *port);
  304. #endif /* LIBPQ_BE_H */