49 const char *logdetail);
61 const char **logdetail);
69#define IDENT_USERNAME_MAX 512
89#ifdef HAVE_PAM_PAM_APPL_H
90#include <pam/pam_appl.h>
92#ifdef HAVE_SECURITY_PAM_APPL_H
93#include <security/pam_appl.h>
96#define PGSQL_PAM_SERVICE "postgresql"
99#ifdef _PAM_LEGACY_NONCONST
102#define PG_PAM_CONST const
141#define LDAP_DEPRECATED 1
151#ifndef LDAP_OPT_DIAGNOSTIC_MESSAGE
152#define LDAP_OPT_DIAGNOSTIC_MESSAGE LDAP_OPT_ERROR_STRING
255 switch (
port->hba->auth_method)
299 errstr =
gettext_noop(
"authentication failed for user \"%s\": invalid authentication method");
304 port->hba->sourcefile,
port->hba->linenumber,
349 (
errmsg(
"authentication identifier set more than once"),
350 errdetail_log(
"previous identifier: \"%s\"; new identifier: \"%s\"",
360 errmsg(
"connection authenticated: identity=\"%s\" method=%s "
364 port->hba->sourcefile,
port->hba->linenumber));
377 const char *logdetail =
NULL;
409 errmsg(
"client certificates can only be checked if a root certificate store is available")));
417 if (!
port->peer_cert_valid)
420 errmsg(
"connection requires a valid client certificate")));
426 switch (
port->hba->auth_method)
451 (
port->gss &&
port->gss->enc) ?
_(
"GSS encryption") :
454 port->ssl_in_use ?
_(
"SSL encryption") :
462 errmsg(
"pg_hba.conf rejects replication connection for host \"%s\", user \"%s\", %s",
469 errmsg(
"pg_hba.conf rejects connection for host \"%s\", user \"%s\", database \"%s\", %s",
497 (
port->gss &&
port->gss->enc) ?
_(
"GSS encryption") :
500 port->ssl_in_use ?
_(
"SSL encryption") :
504#define HOSTNAME_LOOKUP_DETAIL(port) \
505 (port->remote_hostname ? \
506 (port->remote_hostname_resolv == +1 ? \
507 errdetail_log("Client IP address resolved to \"%s\", forward lookup matches.", \
508 port->remote_hostname) : \
509 port->remote_hostname_resolv == 0 ? \
510 errdetail_log("Client IP address resolved to \"%s\", forward lookup not checked.", \
511 port->remote_hostname) : \
512 port->remote_hostname_resolv == -1 ? \
513 errdetail_log("Client IP address resolved to \"%s\", forward lookup does not match.", \
514 port->remote_hostname) : \
515 port->remote_hostname_resolv == -2 ? \
516 errdetail_log("Could not translate client host name \"%s\" to IP address: %s.", \
517 port->remote_hostname, \
518 gai_strerror(port->remote_hostname_errcode)) : \
520 : (port->remote_hostname_resolv == -2 ? \
521 errdetail_log("Could not resolve client IP address to a host name: %s.", \
522 gai_strerror(port->remote_hostname_errcode)) : \
529 errmsg(
"no pg_hba.conf entry for replication connection from host \"%s\", user \"%s\", %s",
537 errmsg(
"no pg_hba.conf entry for host \"%s\", user \"%s\", database \"%s\", %s",
552 port->gss->auth =
true;
659 errmsg(
"connection authenticated: user=\"%s\" method=%s "
662 port->hba->sourcefile,
port->hba->linenumber));
666 (*ClientAuthentication_hook) (
port, status);
731 errmsg(
"expected password response, got message type %d",
752 errmsg(
"invalid password packet size")));
770 errmsg(
"empty password returned by client")));
898 (
errmsg(
"could not generate random MD5 salt")));
949 errmsg(
"could not set environment: %m")));
967 port->gss->delegated_creds =
false;
988 errmsg(
"expected GSS response, got message type %d",
1006 elog(
DEBUG4,
"processing received GSS token of length %zu",
1024 elog(
DEBUG5,
"gss_accept_sec_context major: %u, "
1025 "minor: %u, outlen: %zu, outflags: %x",
1034 port->gss->delegated_creds =
true;
1037 if (
port->gss->outbuf.length != 0)
1042 elog(
DEBUG4,
"sending GSS response token of length %zu",
1043 port->gss->outbuf.length);
1046 port->gss->outbuf.value,
port->gss->outbuf.length);
1132 if (!
port->hba->include_realm)
1150 "GSSAPI realm (%s) and configured realm (%s) don't match",
1151 cp,
port->hba->krb_realm);
1160 "GSSAPI did not return realm but realm matching was requested");
1266 errmsg(
"expected SSPI response, got message type %d",
1298 outbuf.cBuffers = 1;
1302 elog(
DEBUG4,
"processing received SSPI token of length %u",
1303 (
unsigned int)
buf.len);
1318 if (outbuf.cBuffers > 0 && outbuf.pBuffers[0].cbBuffer > 0)
1323 elog(
DEBUG4,
"sending SSPI response token of length %u",
1324 (
unsigned int) outbuf.pBuffers[0].cbBuffer);
1326 port->gss->outbuf.length = outbuf.pBuffers[0].cbBuffer;
1327 port->gss->outbuf.value = outbuf.pBuffers[0].pvBuffer;
1330 port->gss->outbuf.value,
port->gss->outbuf.length);
1344 _(
"could not accept SSPI security context"), r);
1358 (
errmsg(
"out of memory")));
1385 _(
"could not get token from SSPI security context"), r);
1396 (
errmsg_internal(
"could not get token information buffer size: error code %lu",
1402 (
errmsg(
"out of memory")));
1419 if (!
port->hba->compat_realm)
1422 domainname,
sizeof(domainname),
1423 port->hba->upn_username);
1436 if (
port->hba->compat_realm)
1459 "SSPI domain (%s) and configured domain (%s) don't match",
1460 domainname,
port->hba->krb_realm);
1472 if (
port->hba->include_realm)
1521 errmsg(
"could not translate name")));
1535 if (!res || p ==
NULL)
1540 errmsg(
"could not translate name")));
1553 errmsg(
"realm name too long")));
1558 strcpy(domainname, p + 1);
1568 errmsg(
"translated account name too long")));
1594 return c ==
' ' ||
c ==
'\t';
1710 remote_port,
sizeof(remote_port),
1721 hints.ai_protocol = 0;
1722 hints.ai_addrlen = 0;
1737 hints.ai_protocol = 0;
1738 hints.ai_addrlen = 0;
1756 errmsg(
"could not create socket for Ident connection: %m")));
1771 errmsg(
"could not bind to local address \"%s\": %m",
1783 errmsg(
"could not connect to Ident server at address \"%s\", port %s: %m",
1805 errmsg(
"could not send query to Ident server at address \"%s\", port %s: %m",
1822 errmsg(
"could not receive response from Ident server at address \"%s\", port %s: %m",
1832 (
errmsg(
"invalidly formatted response from Ident server: \"%s\"",
1888 errmsg(
"peer authentication is not supported on this platform")));
1892 errmsg(
"could not get peer credentials: %m")));
1902 errmsg(
"could not look up local user ID %ld: %m", (
long) uid));
1908 errmsg(
"local user with ID %ld does not exist", (
long) uid));
1973 errmsg(
"out of memory")));
2008 (
errmsg(
"error from underlying PAM layer: %s",
2019 (
errmsg(
"unsupported PAM conversation %d/\"%s\"",
2021 msg[
i]->msg ? msg[
i]->msg :
"(none)")));
2066 if (
port->hba->pamservice &&
port->hba->pamservice[0] !=
'\0')
2076 (
errmsg(
"could not create PAM authenticator: %s",
2087 (
errmsg(
"pam_set_item(PAM_USER) failed: %s",
2098 if (
port->hba->pam_use_hostname)
2119 (
errmsg(
"pam_set_item(PAM_RHOST) failed: %s",
2131 (
errmsg(
"pam_set_item(PAM_CONV) failed: %s",
2144 (
errmsg(
"pam_authenticate failed: %s",
2157 (
errmsg(
"pam_acct_mgmt failed: %s",
2168 (
errmsg(
"could not release PAM authenticator: %s",
2248 (
errmsg(
"could not initialize LDAP: error code %lu",
2254#ifdef HAVE_LDAP_INITIALIZE
2277 if (!
port->hba->ldapserver ||
port->hba->ldapserver[0] ==
'\0')
2285 (
errmsg(
"could not extract domain name from ldapbasedn")));
2293 (
errmsg(
"LDAP authentication could not find DNS SRV records for \"%s\"",
2295 (
errhint(
"Set an LDAP server name explicitly."))));
2308 p =
port->hba->ldapserver;
2347 (
errmsg(
"could not initialize LDAP: %s",
2357 (
errmsg(
"ldaps not supported with this LDAP library")));
2365 (
errmsg(
"could not initialize LDAP: %m")));
2375 (
errmsg(
"could not set LDAP protocol version: %s",
2382 if (
port->hba->ldaptls)
2391 (
errmsg(
"could not start LDAP TLS session: %s",
2403#define LPH_USERNAME "$username"
2404#define LPH_USERNAME_LEN (sizeof(LPH_USERNAME) - 1)
2407#ifndef LDAP_NO_ATTRS
2408#define LDAP_NO_ATTRS "1.1"
2413#define LDAPS_PORT 636
2432 while (*pattern !=
'\0')
2456 const char *server_name;
2458#ifdef HAVE_LDAP_INITIALIZE
2464 if ((!
port->hba->ldapserver ||
port->hba->ldapserver[0] ==
'\0') &&
2465 (!
port->hba->ldapbasedn ||
port->hba->ldapbasedn[0] ==
'\0'))
2468 (
errmsg(
"LDAP server not specified, and no ldapbasedn")));
2472 if (!
port->hba->ldapserver ||
port->hba->ldapserver[0] ==
'\0')
2475 (
errmsg(
"LDAP server not specified")));
2484 server_name =
port->hba->ldapserver ?
port->hba->ldapserver :
"";
2486 if (
port->hba->ldapport == 0)
2488 if (
port->hba->ldapscheme !=
NULL &&
2508 if (
port->hba->ldapbasedn)
2528 for (
c =
port->user_name; *
c;
c++)
2537 (
errmsg(
"invalid character in user name for LDAP authentication")));
2549 port->hba->ldapbinddn ?
port->hba->ldapbinddn :
"",
2554 (
errmsg(
"could not perform initial LDAP bind for ldapbinddn \"%s\" on server \"%s\": %s",
2555 port->hba->ldapbinddn ?
port->hba->ldapbinddn :
"",
2565 if (
port->hba->ldapsearchfilter)
2567 else if (
port->hba->ldapsearchattribute)
2568 filter =
psprintf(
"(%s=%s)",
port->hba->ldapsearchattribute,
port->user_name);
2574 port->hba->ldapbasedn,
2575 port->hba->ldapscope,
2584 (
errmsg(
"could not search LDAP for filter \"%s\" on server \"%s\": %s",
2600 (
errmsg(
"LDAP user \"%s\" does not exist",
port->user_name),
2601 errdetail(
"LDAP search for filter \"%s\" on server \"%s\" returned no entries.",
2602 filter, server_name)));
2605 (
errmsg(
"LDAP user \"%s\" is not unique",
port->user_name),
2606 errdetail_plural(
"LDAP search for filter \"%s\" on server \"%s\" returned %d entry.",
2607 "LDAP search for filter \"%s\" on server \"%s\" returned %d entries.",
2609 filter, server_name, count)));
2626 (
errmsg(
"could not get dn for the first entry matching \"%s\" on server \"%s\": %s",
2627 filter, server_name,
2644 port->hba->ldapprefix ?
port->hba->ldapprefix :
"",
2646 port->hba->ldapsuffix ?
port->hba->ldapsuffix :
"");
2653 (
errmsg(
"LDAP login failed for user \"%s\" on server \"%s\": %s",
2685 errdetail(
"LDAP diagnostics: %s", message);
2709 switch (
port->hba->clientcertname)
2723 (
errmsg(
"certificate authentication failed for user \"%s\": client certificate contains no user name",
2744 (
errmsg(
"certificate authentication failed for user \"%s\": unable to retrieve subject DN",
2763 switch (
port->hba->clientcertname)
2767 (
errmsg(
"certificate validation (clientcert=verify-full) failed for user \"%s\": DN mismatch",
2772 (
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
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 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)
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 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)
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)