49 const char *logdetail);
63 const char **logdetail);
71#define IDENT_USERNAME_MAX 512
91#ifdef HAVE_PAM_PAM_APPL_H
92#include <pam/pam_appl.h>
94#ifdef HAVE_SECURITY_PAM_APPL_H
95#include <security/pam_appl.h>
98#define PGSQL_PAM_SERVICE "postgresql"
101#ifdef _PAM_LEGACY_NONCONST
104#define PG_PAM_CONST const
143#define LDAP_DEPRECATED 1
153#ifndef LDAP_OPT_DIAGNOSTIC_MESSAGE
154#define LDAP_OPT_DIAGNOSTIC_MESSAGE LDAP_OPT_ERROR_STRING
257 switch (
port->hba->auth_method)
301 errstr =
gettext_noop(
"authentication failed for user \"%s\": invalid authentication method");
306 port->hba->sourcefile,
port->hba->linenumber,
351 (
errmsg(
"authentication identifier set more than once"),
352 errdetail_log(
"previous identifier: \"%s\"; new identifier: \"%s\"",
362 errmsg(
"connection authenticated: identity=\"%s\" method=%s "
366 port->hba->sourcefile,
port->hba->linenumber));
379 const char *logdetail =
NULL;
411 errmsg(
"client certificates can only be checked if a root certificate store is available")));
419 if (!
port->peer_cert_valid)
422 errmsg(
"connection requires a valid client certificate")));
428 switch (
port->hba->auth_method)
453 (
port->gss &&
port->gss->enc) ?
_(
"GSS encryption") :
456 port->ssl_in_use ?
_(
"SSL encryption") :
464 errmsg(
"pg_hba.conf rejects replication connection for host \"%s\", user \"%s\", %s",
471 errmsg(
"pg_hba.conf rejects connection for host \"%s\", user \"%s\", database \"%s\", %s",
499 (
port->gss &&
port->gss->enc) ?
_(
"GSS encryption") :
502 port->ssl_in_use ?
_(
"SSL encryption") :
506#define HOSTNAME_LOOKUP_DETAIL(port) \
507 (port->remote_hostname ? \
508 (port->remote_hostname_resolv == +1 ? \
509 errdetail_log("Client IP address resolved to \"%s\", forward lookup matches.", \
510 port->remote_hostname) : \
511 port->remote_hostname_resolv == 0 ? \
512 errdetail_log("Client IP address resolved to \"%s\", forward lookup not checked.", \
513 port->remote_hostname) : \
514 port->remote_hostname_resolv == -1 ? \
515 errdetail_log("Client IP address resolved to \"%s\", forward lookup does not match.", \
516 port->remote_hostname) : \
517 port->remote_hostname_resolv == -2 ? \
518 errdetail_log("Could not translate client host name \"%s\" to IP address: %s.", \
519 port->remote_hostname, \
520 gai_strerror(port->remote_hostname_errcode)) : \
522 : (port->remote_hostname_resolv == -2 ? \
523 errdetail_log("Could not resolve client IP address to a host name: %s.", \
524 gai_strerror(port->remote_hostname_errcode)) : \
531 errmsg(
"no pg_hba.conf entry for replication connection from host \"%s\", user \"%s\", %s",
539 errmsg(
"no pg_hba.conf entry for host \"%s\", user \"%s\", database \"%s\", %s",
554 port->gss->auth =
true;
661 errmsg(
"connection authenticated: user=\"%s\" method=%s "
664 port->hba->sourcefile,
port->hba->linenumber));
668 (*ClientAuthentication_hook) (
port, status);
733 errmsg(
"expected password response, got message type %d",
754 errmsg(
"invalid password packet size")));
772 errmsg(
"empty password returned by client")));
906 (
errmsg(
"could not generate random MD5 salt")));
946 detail =
pstrdup(
_(
"MD5 password support is deprecated and will be removed in a future release of PostgreSQL."));
982 errmsg(
"could not set environment: %m")));
1000 port->gss->delegated_creds =
false;
1021 errmsg(
"expected GSS response, got message type %d",
1039 elog(
DEBUG4,
"processing received GSS token of length %zu",
1057 elog(
DEBUG5,
"gss_accept_sec_context major: %u, "
1058 "minor: %u, outlen: %zu, outflags: %x",
1067 port->gss->delegated_creds =
true;
1070 if (
port->gss->outbuf.length != 0)
1075 elog(
DEBUG4,
"sending GSS response token of length %zu",
1076 port->gss->outbuf.length);
1079 port->gss->outbuf.value,
port->gss->outbuf.length);
1165 if (!
port->hba->include_realm)
1183 "GSSAPI realm (%s) and configured realm (%s) don't match",
1184 cp,
port->hba->krb_realm);
1193 "GSSAPI did not return realm but realm matching was requested");
1299 errmsg(
"expected SSPI response, got message type %d",
1331 outbuf.cBuffers = 1;
1335 elog(
DEBUG4,
"processing received SSPI token of length %u",
1336 (
unsigned int)
buf.len);
1351 if (outbuf.cBuffers > 0 && outbuf.pBuffers[0].cbBuffer > 0)
1356 elog(
DEBUG4,
"sending SSPI response token of length %u",
1357 (
unsigned int) outbuf.pBuffers[0].cbBuffer);
1359 port->gss->outbuf.length = outbuf.pBuffers[0].cbBuffer;
1360 port->gss->outbuf.value = outbuf.pBuffers[0].pvBuffer;
1363 port->gss->outbuf.value,
port->gss->outbuf.length);
1377 _(
"could not accept SSPI security context"), r);
1391 (
errmsg(
"out of memory")));
1418 _(
"could not get token from SSPI security context"), r);
1429 (
errmsg_internal(
"could not get token information buffer size: error code %lu",
1435 (
errmsg(
"out of memory")));
1452 if (!
port->hba->compat_realm)
1455 domainname,
sizeof(domainname),
1456 port->hba->upn_username);
1469 if (
port->hba->compat_realm)
1492 "SSPI domain (%s) and configured domain (%s) don't match",
1493 domainname,
port->hba->krb_realm);
1505 if (
port->hba->include_realm)
1554 errmsg(
"could not translate name")));
1568 if (!res || p ==
NULL)
1573 errmsg(
"could not translate name")));
1586 errmsg(
"realm name too long")));
1591 strcpy(domainname, p + 1);
1601 errmsg(
"translated account name too long")));
1627 return c ==
' ' ||
c ==
'\t';
1743 remote_port,
sizeof(remote_port),
1754 hints.ai_protocol = 0;
1755 hints.ai_addrlen = 0;
1770 hints.ai_protocol = 0;
1771 hints.ai_addrlen = 0;
1789 errmsg(
"could not create socket for Ident connection: %m")));
1804 errmsg(
"could not bind to local address \"%s\": %m",
1816 errmsg(
"could not connect to Ident server at address \"%s\", port %s: %m",
1838 errmsg(
"could not send query to Ident server at address \"%s\", port %s: %m",
1855 errmsg(
"could not receive response from Ident server at address \"%s\", port %s: %m",
1865 (
errmsg(
"invalidly formatted response from Ident server: \"%s\"",
1921 errmsg(
"peer authentication is not supported on this platform")));
1925 errmsg(
"could not get peer credentials: %m")));
1935 errmsg(
"could not look up local user ID %ld: %m", (
long) uid));
1941 errmsg(
"local user with ID %ld does not exist", (
long) uid));
2006 errmsg(
"out of memory")));
2041 (
errmsg(
"error from underlying PAM layer: %s",
2052 (
errmsg(
"unsupported PAM conversation %d/\"%s\"",
2054 msg[
i]->msg ? msg[
i]->msg :
"(none)")));
2099 if (
port->hba->pamservice &&
port->hba->pamservice[0] !=
'\0')
2109 (
errmsg(
"could not create PAM authenticator: %s",
2120 (
errmsg(
"pam_set_item(PAM_USER) failed: %s",
2131 if (
port->hba->pam_use_hostname)
2152 (
errmsg(
"pam_set_item(PAM_RHOST) failed: %s",
2164 (
errmsg(
"pam_set_item(PAM_CONV) failed: %s",
2177 (
errmsg(
"pam_authenticate failed: %s",
2190 (
errmsg(
"pam_acct_mgmt failed: %s",
2201 (
errmsg(
"could not release PAM authenticator: %s",
2281 (
errmsg(
"could not initialize LDAP: error code %lu",
2287#ifdef HAVE_LDAP_INITIALIZE
2310 if (!
port->hba->ldapserver ||
port->hba->ldapserver[0] ==
'\0')
2318 (
errmsg(
"could not extract domain name from ldapbasedn")));
2326 (
errmsg(
"LDAP authentication could not find DNS SRV records for \"%s\"",
2328 (
errhint(
"Set an LDAP server name explicitly."))));
2341 p =
port->hba->ldapserver;
2380 (
errmsg(
"could not initialize LDAP: %s",
2390 (
errmsg(
"ldaps not supported with this LDAP library")));
2398 (
errmsg(
"could not initialize LDAP: %m")));
2408 (
errmsg(
"could not set LDAP protocol version: %s",
2415 if (
port->hba->ldaptls)
2424 (
errmsg(
"could not start LDAP TLS session: %s",
2436#define LPH_USERNAME "$username"
2437#define LPH_USERNAME_LEN (sizeof(LPH_USERNAME) - 1)
2440#ifndef LDAP_NO_ATTRS
2441#define LDAP_NO_ATTRS "1.1"
2446#define LDAPS_PORT 636
2465 while (*pattern !=
'\0')
2489 const char *server_name;
2491#ifdef HAVE_LDAP_INITIALIZE
2497 if ((!
port->hba->ldapserver ||
port->hba->ldapserver[0] ==
'\0') &&
2498 (!
port->hba->ldapbasedn ||
port->hba->ldapbasedn[0] ==
'\0'))
2501 (
errmsg(
"LDAP server not specified, and no ldapbasedn")));
2505 if (!
port->hba->ldapserver ||
port->hba->ldapserver[0] ==
'\0')
2508 (
errmsg(
"LDAP server not specified")));
2517 server_name =
port->hba->ldapserver ?
port->hba->ldapserver :
"";
2519 if (
port->hba->ldapport == 0)
2521 if (
port->hba->ldapscheme !=
NULL &&
2541 if (
port->hba->ldapbasedn)
2561 for (
c =
port->user_name; *
c;
c++)
2570 (
errmsg(
"invalid character in user name for LDAP authentication")));
2582 port->hba->ldapbinddn ?
port->hba->ldapbinddn :
"",
2587 (
errmsg(
"could not perform initial LDAP bind for ldapbinddn \"%s\" on server \"%s\": %s",
2588 port->hba->ldapbinddn ?
port->hba->ldapbinddn :
"",
2598 if (
port->hba->ldapsearchfilter)
2600 else if (
port->hba->ldapsearchattribute)
2601 filter =
psprintf(
"(%s=%s)",
port->hba->ldapsearchattribute,
port->user_name);
2607 port->hba->ldapbasedn,
2608 port->hba->ldapscope,
2617 (
errmsg(
"could not search LDAP for filter \"%s\" on server \"%s\": %s",
2633 (
errmsg(
"LDAP user \"%s\" does not exist",
port->user_name),
2634 errdetail(
"LDAP search for filter \"%s\" on server \"%s\" returned no entries.",
2635 filter, server_name)));
2638 (
errmsg(
"LDAP user \"%s\" is not unique",
port->user_name),
2639 errdetail_plural(
"LDAP search for filter \"%s\" on server \"%s\" returned %d entry.",
2640 "LDAP search for filter \"%s\" on server \"%s\" returned %d entries.",
2642 filter, server_name, count)));
2659 (
errmsg(
"could not get dn for the first entry matching \"%s\" on server \"%s\": %s",
2660 filter, server_name,
2677 port->hba->ldapprefix ?
port->hba->ldapprefix :
"",
2679 port->hba->ldapsuffix ?
port->hba->ldapsuffix :
"");
2686 (
errmsg(
"LDAP login failed for user \"%s\" on server \"%s\": %s",
2718 errdetail(
"LDAP diagnostics: %s", message);
2742 switch (
port->hba->clientcertname)
2756 (
errmsg(
"certificate authentication failed for user \"%s\": client certificate contains no user name",
2777 (
errmsg(
"certificate authentication failed for user \"%s\": unable to retrieve subject DN",
2796 switch (
port->hba->clientcertname)
2800 (
errmsg(
"certificate validation (clientcert=verify-full) failed for user \"%s\": DN mismatch",
2805 (
errmsg(
"certificate validation (clientcert=verify-full) failed for user \"%s\": CN mismatch",
const pg_be_sasl_mech pg_be_oauth_mech
int CheckSASLAuth(const pg_be_sasl_mech *mech, Port *port, char *shadow_pass, const char **logdetail, bool *abandoned)
const pg_be_sasl_mech pg_be_scram_mech
void sendAuthRequest(Port *port, AuthRequest areq, const void *extradata, int extralen)
static int CheckPWChallengeAuth(Port *port, const char **logdetail)
static int auth_peer(Port *port)
static void auth_failed(Port *port, int elevel, int status, const char *logdetail)
char * pg_krb_server_keyfile
#define IDENT_USERNAME_MAX
bool pg_krb_caseins_users
static char * recv_password_packet(Port *port)
bool pg_gss_accept_delegation
static void queue_md5_password_warning(void)
ClientAuthentication_hook_type ClientAuthentication_hook
void ClientAuthentication(Port *port)
static int CheckMD5Auth(Port *port, char *shadow_pass, const char **logdetail)
void set_authn_id(Port *port, const char *id)
static bool is_ident_whitespace(const char c)
static int ident_inet(Port *port)
static bool md5_password_warning_enabled(void)
static bool interpret_ident_response(const char *ident_response, char *ident_user)
#define HOSTNAME_LOOKUP_DETAIL(port)
static int CheckPasswordAuth(Port *port, const char **logdetail)
PGDLLIMPORT auth_password_hook_typ ldap_password_hook
void(* ClientAuthentication_hook_type)(Port *, int)
char *(* auth_password_hook_typ)(char *input)
#define PG_MAX_AUTH_TOKEN_LENGTH
@ LOG_CONNECTION_AUTHENTICATION
void pg_store_delegated_credential(gss_cred_id_t cred)
void pg_GSS_error(const char *errmsg, OM_uint32 maj_stat, OM_uint32 min_stat)
bool secure_loaded_verify_locations(void)
#define unconstify(underlying_type, expr)
#define Assert(condition)
memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets))
int md5_crypt_verify(const char *role, const char *shadow_pass, const char *client_pass, const uint8 *md5_salt, int md5_salt_len, const char **logdetail)
int plain_crypt_verify(const char *role, const char *shadow_pass, const char *client_pass, const char **logdetail)
char * get_role_password(const char *role, const char **logdetail)
bool md5_password_warnings
PasswordType get_password_type(const char *shadow_pass)
int errcode_for_socket_access(void)
int errcode(int sqlerrcode)
int int errdetail_internal(const char *fmt,...) pg_attribute_printf(1
int errhint(const char *fmt,...) pg_attribute_printf(1
int errdetail(const char *fmt,...) pg_attribute_printf(1
int int errmsg_internal(const char *fmt,...) pg_attribute_printf(1
int int int errdetail_log(const char *fmt,...) pg_attribute_printf(1
#define FATAL_CLIENT_ONLY
#define ereport(elevel,...)
int errdetail_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...) pg_attribute_printf(1
#define ERRCODE_PROTOCOL_VIOLATION
#define ERRCODE_INVALID_PASSWORD
int check_usermap(const char *usermap_name, const char *pg_user, const char *system_user, bool case_insensitive)
void hba_getauthmethod(Port *port)
const char * hba_authname(UserAuth auth_method)
void pg_freeaddrinfo_all(int hint_ai_family, struct addrinfo *ai)
int pg_getnameinfo_all(const struct sockaddr_storage *addr, int salen, char *node, int nodelen, char *service, int servicelen, int flags)
int pg_getaddrinfo_all(const char *hostname, const char *servname, const struct addrinfo *hintp, struct addrinfo **result)
char * MemoryContextStrdup(MemoryContext context, const char *string)
void * MemoryContextAllocZero(MemoryContext context, Size size)
char * pstrdup(const char *in)
void pfree(void *pointer)
MemoryContext TopMemoryContext
#define CHECK_FOR_INTERRUPTS()
ClientConnectionInfo MyClientConnectionInfo
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
static char buf[DEFAULT_XLOG_SEG_SIZE]
bool pg_strong_random(void *buf, size_t len)
int pg_strcasecmp(const char *s1, const char *s2)
int getpeereid(int sock, uid_t *uid, gid_t *gid)
void StoreConnectionWarning(char *msg, char *detail, ConnectionWarningFilter filter)
int pq_getmessage(StringInfo s, int maxlen)
void pq_startmsgread(void)
#define PqMsg_GSSResponse
#define PqMsg_AuthenticationRequest
#define AUTH_REQ_PASSWORD
#define AUTH_REQ_GSS_CONT
#define PqMsg_PasswordMessage
#define AUTH_REQ_SASL_FIN
char * psprintf(const char *fmt,...)
const char * gai_strerror(int errcode)
void appendStringInfo(StringInfo str, const char *fmt,...)
void appendBinaryStringInfo(StringInfo str, const void *data, int datalen)
void appendStringInfoString(StringInfo str, const char *s)
void appendStringInfoChar(StringInfo str, char ch)
void initStringInfo(StringInfo str)
#define bind(s, addr, addrlen)
#define recv(s, buf, len, flags)
#define send(s, buf, len, flags)
#define socket(af, type, protocol)
#define connect(s, name, namelen)
static void static void static void warning(const char *const string,...) pg_attribute_printf(1