PostgreSQL Source Code git master
Loading...
Searching...
No Matches
auth.c File Reference
#include "postgres.h"
#include <sys/param.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <pwd.h>
#include <unistd.h>
#include "commands/user.h"
#include "common/ip.h"
#include "common/md5.h"
#include "libpq/auth.h"
#include "libpq/crypt.h"
#include "libpq/libpq.h"
#include "libpq/oauth.h"
#include "libpq/pqformat.h"
#include "libpq/sasl.h"
#include "libpq/scram.h"
#include "miscadmin.h"
#include "port/pg_bswap.h"
#include "postmaster/postmaster.h"
#include "replication/walsender.h"
#include "storage/ipc.h"
#include "tcop/backend_startup.h"
#include "utils/memutils.h"
Include dependency graph for auth.c:

Go to the source code of this file.

Macros

#define IDENT_USERNAME_MAX   512
 
#define IDENT_PORT   113
 
#define HOSTNAME_LOOKUP_DETAIL(port)
 

Functions

static void auth_failed (Port *port, int elevel, int status, const char *logdetail)
 
static charrecv_password_packet (Port *port)
 
static int CheckPasswordAuth (Port *port, const char **logdetail)
 
static int CheckPWChallengeAuth (Port *port, const char **logdetail)
 
static int CheckMD5Auth (Port *port, char *shadow_pass, const char **logdetail)
 
static int ident_inet (Port *port)
 
static int auth_peer (Port *port)
 
void set_authn_id (Port *port, const char *id)
 
void ClientAuthentication (Port *port)
 
void sendAuthRequest (Port *port, AuthRequest areq, const void *extradata, int extralen)
 
static bool is_ident_whitespace (const char c)
 
static bool interpret_ident_response (const char *ident_response, char *ident_user)
 

Variables

charpg_krb_server_keyfile
 
bool pg_krb_caseins_users
 
bool pg_gss_accept_delegation
 
ClientAuthentication_hook_type ClientAuthentication_hook = NULL
 

Macro Definition Documentation

◆ HOSTNAME_LOOKUP_DETAIL

#define HOSTNAME_LOOKUP_DETAIL (   port)
Value:
(port->remote_hostname ? \
(port->remote_hostname_resolv == +1 ? \
errdetail_log("Client IP address resolved to \"%s\", forward lookup matches.", \
port->remote_hostname) : \
port->remote_hostname_resolv == 0 ? \
errdetail_log("Client IP address resolved to \"%s\", forward lookup not checked.", \
port->remote_hostname) : \
port->remote_hostname_resolv == -1 ? \
errdetail_log("Client IP address resolved to \"%s\", forward lookup does not match.", \
port->remote_hostname) : \
port->remote_hostname_resolv == -2 ? \
errdetail_log("Could not translate client host name \"%s\" to IP address: %s.", \
port->remote_hostname, \
gai_strerror(port->remote_hostname_errcode)) : \
0) \
: (port->remote_hostname_resolv == -2 ? \
errdetail_log("Could not resolve client IP address to a host name: %s.", \
gai_strerror(port->remote_hostname_errcode)) : \
0))
int int int errdetail_log(const char *fmt,...) pg_attribute_printf(1
static int port
Definition pg_regress.c:117
static int fb(int x)
const char * gai_strerror(int errcode)

◆ IDENT_PORT

#define IDENT_PORT   113

Definition at line 72 of file auth.c.

◆ IDENT_USERNAME_MAX

#define IDENT_USERNAME_MAX   512

Definition at line 69 of file auth.c.

Function Documentation

◆ auth_failed()

static void auth_failed ( Port port,
int  elevel,
int  status,
const char logdetail 
)
static

Definition at line 234 of file auth.c.

235{
236 const char *errstr;
237 char *cdetail;
239
240 Assert(elevel >= FATAL); /* we must exit here */
241
242 /*
243 * If we failed due to EOF from client, just quit; there's no point in
244 * trying to send a message to the client, and not much point in logging
245 * the failure in the postmaster log. (Logging the failure might be
246 * desirable, were it not for the fact that libpq closes the connection
247 * unceremoniously if challenged for a password when it hasn't got one to
248 * send. We'll get a useless log entry for every psql connection under
249 * password auth, even if it's perfectly successful, if we log STATUS_EOF
250 * events.)
251 */
252 if (status == STATUS_EOF)
253 proc_exit(0);
254
255 switch (port->hba->auth_method)
256 {
257 case uaReject:
258 case uaImplicitReject:
259 errstr = gettext_noop("authentication failed for user \"%s\": host rejected");
260 break;
261 case uaTrust:
262 errstr = gettext_noop("\"trust\" authentication failed for user \"%s\"");
263 break;
264 case uaIdent:
265 errstr = gettext_noop("Ident authentication failed for user \"%s\"");
266 break;
267 case uaPeer:
268 errstr = gettext_noop("Peer authentication failed for user \"%s\"");
269 break;
270 case uaPassword:
271 case uaMD5:
272 case uaSCRAM:
273 errstr = gettext_noop("password authentication failed for user \"%s\"");
274 /* We use it to indicate if a .pgpass password failed. */
276 break;
277 case uaGSS:
278 errstr = gettext_noop("GSSAPI authentication failed for user \"%s\"");
279 break;
280 case uaSSPI:
281 errstr = gettext_noop("SSPI authentication failed for user \"%s\"");
282 break;
283 case uaPAM:
284 errstr = gettext_noop("PAM authentication failed for user \"%s\"");
285 break;
286 case uaBSD:
287 errstr = gettext_noop("BSD authentication failed for user \"%s\"");
288 break;
289 case uaLDAP:
290 errstr = gettext_noop("LDAP authentication failed for user \"%s\"");
291 break;
292 case uaCert:
293 errstr = gettext_noop("certificate authentication failed for user \"%s\"");
294 break;
295 case uaOAuth:
296 errstr = gettext_noop("OAuth bearer authentication failed for user \"%s\"");
297 break;
298 default:
299 errstr = gettext_noop("authentication failed for user \"%s\": invalid authentication method");
300 break;
301 }
302
303 cdetail = psprintf(_("Connection matched file \"%s\" line %d: \"%s\""),
304 port->hba->sourcefile, port->hba->linenumber,
305 port->hba->rawline);
306 if (logdetail)
307 logdetail = psprintf("%s\n%s", logdetail, cdetail);
308 else
309 logdetail = cdetail;
310
311 ereport(elevel,
313 errmsg(errstr, port->user_name),
314 logdetail ? errdetail_log("%s", logdetail) : 0));
315
316 /* doesn't return */
318}
#define gettext_noop(x)
Definition c.h:1285
#define Assert(condition)
Definition c.h:943
#define STATUS_EOF
Definition c.h:1260
#define pg_unreachable()
Definition c.h:367
int errcode(int sqlerrcode)
Definition elog.c:874
#define _(x)
Definition elog.c:95
#define FATAL
Definition elog.h:42
#define ereport(elevel,...)
Definition elog.h:152
#define ERRCODE_INVALID_PASSWORD
Definition fe-connect.c:93
@ uaBSD
Definition hba.h:37
@ uaLDAP
Definition hba.h:38
@ uaPeer
Definition hba.h:40
@ uaPAM
Definition hba.h:36
@ uaPassword
Definition hba.h:31
@ uaCert
Definition hba.h:39
@ uaMD5
Definition hba.h:32
@ uaReject
Definition hba.h:27
@ uaGSS
Definition hba.h:34
@ uaSCRAM
Definition hba.h:33
@ uaImplicitReject
Definition hba.h:28
@ uaIdent
Definition hba.h:30
@ uaOAuth
Definition hba.h:41
@ uaTrust
Definition hba.h:29
@ uaSSPI
Definition hba.h:35
void proc_exit(int code)
Definition ipc.c:105
static char * errmsg
char * psprintf(const char *fmt,...)
Definition psprintf.c:43

References _, Assert, ereport, errcode(), ERRCODE_INVALID_PASSWORD, errdetail_log(), errmsg, FATAL, fb(), gettext_noop, pg_unreachable, port, proc_exit(), psprintf(), STATUS_EOF, uaBSD, uaCert, uaGSS, uaIdent, uaImplicitReject, uaLDAP, uaMD5, uaOAuth, uaPAM, uaPassword, uaPeer, uaReject, uaSCRAM, uaSSPI, and uaTrust.

Referenced by ClientAuthentication().

◆ auth_peer()

static int auth_peer ( Port port)
static

Definition at line 1870 of file auth.c.

1871{
1872 uid_t uid;
1873 gid_t gid;
1874#ifndef WIN32
1875 struct passwd pwbuf;
1876 struct passwd *pw;
1877 char buf[1024];
1878 int rc;
1879 int ret;
1880#endif
1881
1882 if (getpeereid(port->sock, &uid, &gid) != 0)
1883 {
1884 /* Provide special error message if getpeereid is a stub */
1885 if (errno == ENOSYS)
1886 ereport(LOG,
1888 errmsg("peer authentication is not supported on this platform")));
1889 else
1890 ereport(LOG,
1892 errmsg("could not get peer credentials: %m")));
1893 return STATUS_ERROR;
1894 }
1895
1896#ifndef WIN32
1897 rc = getpwuid_r(uid, &pwbuf, buf, sizeof buf, &pw);
1898 if (rc != 0)
1899 {
1900 errno = rc;
1901 ereport(LOG,
1902 errmsg("could not look up local user ID %ld: %m", (long) uid));
1903 return STATUS_ERROR;
1904 }
1905 else if (!pw)
1906 {
1907 ereport(LOG,
1908 errmsg("local user with ID %ld does not exist", (long) uid));
1909 return STATUS_ERROR;
1910 }
1911
1912 /*
1913 * Make a copy of static getpw*() result area; this is our authenticated
1914 * identity. Set it before calling check_usermap, because authentication
1915 * has already succeeded and we want the log file to reflect that.
1916 */
1917 set_authn_id(port, pw->pw_name);
1918
1919 ret = check_usermap(port->hba->usermap, port->user_name,
1921
1922 return ret;
1923#else
1924 /* should have failed with ENOSYS above */
1925 Assert(false);
1926 return STATUS_ERROR;
1927#endif
1928}
void set_authn_id(Port *port, const char *id)
Definition auth.c:336
#define STATUS_ERROR
Definition c.h:1259
int errcode_for_socket_access(void)
Definition elog.c:976
#define LOG
Definition elog.h:32
int check_usermap(const char *usermap_name, const char *pg_user, const char *system_user, bool case_insensitive)
Definition hba.c:2791
ClientConnectionInfo MyClientConnectionInfo
Definition miscinit.c:1020
static char buf[DEFAULT_XLOG_SEG_SIZE]
int getpeereid(int sock, uid_t *uid, gid_t *gid)
Definition getpeereid.c:33
const char * authn_id
Definition libpq-be.h:99
int gid_t
Definition win32_port.h:235
int uid_t
Definition win32_port.h:234

References Assert, ClientConnectionInfo::authn_id, buf, check_usermap(), ereport, errcode(), errcode_for_socket_access(), errmsg, fb(), getpeereid(), LOG, MyClientConnectionInfo, port, set_authn_id(), and STATUS_ERROR.

Referenced by ClientAuthentication().

◆ CheckMD5Auth()

static int CheckMD5Auth ( Port port,
char shadow_pass,
const char **  logdetail 
)
static

Definition at line 888 of file auth.c.

889{
890 uint8 md5Salt[4]; /* Password salt */
891 char *passwd;
892 int result;
893
894 /* include the salt to use for computing the response */
895 if (!pg_strong_random(md5Salt, 4))
896 {
897 ereport(LOG,
898 (errmsg("could not generate random MD5 salt")));
899 return STATUS_ERROR;
900 }
901
903
905 if (passwd == NULL)
906 return STATUS_EOF; /* client wouldn't send password */
907
908 if (shadow_pass)
910 md5Salt, 4, logdetail);
911 else
913
914 pfree(passwd);
915
916 return result;
917}
void sendAuthRequest(Port *port, AuthRequest areq, const void *extradata, int extralen)
Definition auth.c:682
static char * recv_password_packet(Port *port)
Definition auth.c:712
uint8_t uint8
Definition c.h:622
uint32 result
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)
Definition crypt.c:265
void pfree(void *pointer)
Definition mcxt.c:1616
bool pg_strong_random(void *buf, size_t len)
#define AUTH_REQ_MD5
Definition protocol.h:101

References AUTH_REQ_MD5, ereport, errmsg, fb(), LOG, md5_crypt_verify(), pfree(), pg_strong_random(), port, recv_password_packet(), result, sendAuthRequest(), STATUS_EOF, and STATUS_ERROR.

Referenced by CheckPWChallengeAuth().

◆ CheckPasswordAuth()

static int CheckPasswordAuth ( Port port,
const char **  logdetail 
)
static

Definition at line 793 of file auth.c.

794{
795 char *passwd;
796 int result;
797 char *shadow_pass;
798
800
802 if (passwd == NULL)
803 return STATUS_EOF; /* client wouldn't send password */
804
805 shadow_pass = get_role_password(port->user_name, logdetail);
806 if (shadow_pass)
807 {
809 logdetail);
810 }
811 else
813
814 if (shadow_pass)
816 pfree(passwd);
817
818 if (result == STATUS_OK)
819 set_authn_id(port, port->user_name);
820
821 return result;
822}
#define STATUS_OK
Definition c.h:1258
int plain_crypt_verify(const char *role, const char *shadow_pass, const char *client_pass, const char **logdetail)
Definition crypt.c:336
char * get_role_password(const char *role, const char **logdetail)
Definition crypt.c:43
#define AUTH_REQ_PASSWORD
Definition protocol.h:99

References AUTH_REQ_PASSWORD, fb(), get_role_password(), pfree(), plain_crypt_verify(), port, recv_password_packet(), result, sendAuthRequest(), set_authn_id(), STATUS_EOF, STATUS_ERROR, and STATUS_OK.

Referenced by ClientAuthentication().

◆ CheckPWChallengeAuth()

static int CheckPWChallengeAuth ( Port port,
const char **  logdetail 
)
static

Definition at line 828 of file auth.c.

829{
830 int auth_result;
831 char *shadow_pass;
833
834 Assert(port->hba->auth_method == uaSCRAM ||
835 port->hba->auth_method == uaMD5);
836
837 /* First look up the user's password. */
838 shadow_pass = get_role_password(port->user_name, logdetail);
839
840 /*
841 * If the user does not exist, or has no password or it's expired, we
842 * still go through the motions of authentication, to avoid revealing to
843 * the client that the user didn't exist. If 'md5' is allowed, we choose
844 * whether to use 'md5' or 'scram-sha-256' authentication based on current
845 * password_encryption setting. The idea is that most genuine users
846 * probably have a password of that type, and if we pretend that this user
847 * had a password of that type, too, it "blends in" best.
848 */
849 if (!shadow_pass)
851 else
853
854 /*
855 * If 'md5' authentication is allowed, decide whether to perform 'md5' or
856 * 'scram-sha-256' authentication based on the type of password the user
857 * has. If it's an MD5 hash, we must do MD5 authentication, and if it's a
858 * SCRAM secret, we must do SCRAM authentication.
859 *
860 * If MD5 authentication is not allowed, always use SCRAM. If the user
861 * had an MD5 password, CheckSASLAuth() with the SCRAM mechanism will
862 * fail.
863 */
864 if (port->hba->auth_method == uaMD5 && pwtype == PASSWORD_TYPE_MD5)
866 else
868 logdetail, NULL /* can't abandon SCRAM */ );
869
870 if (shadow_pass)
872 else
873 {
874 /*
875 * If get_role_password() returned error, authentication better not
876 * have succeeded.
877 */
879 }
880
881 if (auth_result == STATUS_OK)
882 set_authn_id(port, port->user_name);
883
884 return auth_result;
885}
int CheckSASLAuth(const pg_be_sasl_mech *mech, Port *port, char *shadow_pass, const char **logdetail, bool *abandoned)
Definition auth-sasl.c:50
const pg_be_sasl_mech pg_be_scram_mech
Definition auth-scram.c:114
static int CheckMD5Auth(Port *port, char *shadow_pass, const char **logdetail)
Definition auth.c:888
PasswordType get_password_type(const char *shadow_pass)
Definition crypt.c:153
PasswordType
Definition crypt.h:44
@ PASSWORD_TYPE_MD5
Definition crypt.h:46
int Password_encryption
Definition user.c:86

References Assert, CheckMD5Auth(), CheckSASLAuth(), fb(), get_password_type(), get_role_password(), Password_encryption, PASSWORD_TYPE_MD5, pfree(), pg_be_scram_mech, port, set_authn_id(), STATUS_OK, uaMD5, and uaSCRAM.

Referenced by ClientAuthentication().

◆ ClientAuthentication()

void ClientAuthentication ( Port port)

Definition at line 374 of file auth.c.

375{
376 int status = STATUS_ERROR;
377 const char *logdetail = NULL;
378
379 /*
380 * "Abandoned" is a SASL-specific state similar to STATUS_EOF, in that we
381 * don't want to generate any server logs. But it's caused by an in-band
382 * client action that requires a server response, not an out-of-band
383 * connection closure, so we can't just proc_exit() like we do with
384 * STATUS_EOF.
385 */
386 bool abandoned = false;
387
388 /*
389 * Get the authentication method to use for this frontend/database
390 * combination. Note: we do not parse the file at this point; this has
391 * already been done elsewhere. hba.c dropped an error message into the
392 * server logfile if parsing the hba config file failed.
393 */
395
397
398 /*
399 * This is the first point where we have access to the hba record for the
400 * current connection, so perform any verifications based on the hba
401 * options field that should be done *before* the authentication here.
402 */
403 if (port->hba->clientcert != clientCertOff)
404 {
405 /* If we haven't loaded a root certificate store, fail */
409 errmsg("client certificates can only be checked if a root certificate store is available")));
410
411 /*
412 * If we loaded a root certificate store, and if a certificate is
413 * present on the client, then it has been verified against our root
414 * certificate store, and the connection would have been aborted
415 * already if it didn't verify ok.
416 */
417 if (!port->peer_cert_valid)
420 errmsg("connection requires a valid client certificate")));
421 }
422
423 /*
424 * Now proceed to do the actual authentication check
425 */
426 switch (port->hba->auth_method)
427 {
428 case uaReject:
429
430 /*
431 * An explicit "reject" entry in pg_hba.conf. This report exposes
432 * the fact that there's an explicit reject entry, which is
433 * perhaps not so desirable from a security standpoint; but the
434 * message for an implicit reject could confuse the DBA a lot when
435 * the true situation is a match to an explicit reject. And we
436 * don't want to change the message for an implicit reject. As
437 * noted below, the additional information shown here doesn't
438 * expose anything not known to an attacker.
439 */
440 {
441 char hostinfo[NI_MAXHOST];
442 const char *encryption_state;
443
444 pg_getnameinfo_all(&port->raddr.addr, port->raddr.salen,
445 hostinfo, sizeof(hostinfo),
446 NULL, 0,
448
450#ifdef ENABLE_GSS
451 (port->gss && port->gss->enc) ? _("GSS encryption") :
452#endif
453#ifdef USE_SSL
454 port->ssl_in_use ? _("SSL encryption") :
455#endif
456 _("no encryption");
457
461 /* translator: last %s describes encryption state */
462 errmsg("pg_hba.conf rejects replication connection for host \"%s\", user \"%s\", %s",
463 hostinfo, port->user_name,
465 else
468 /* translator: last %s describes encryption state */
469 errmsg("pg_hba.conf rejects connection for host \"%s\", user \"%s\", database \"%s\", %s",
470 hostinfo, port->user_name,
471 port->database_name,
473 break;
474 }
475
476 case uaImplicitReject:
477
478 /*
479 * No matching entry, so tell the user we fell through.
480 *
481 * NOTE: the extra info reported here is not a security breach,
482 * because all that info is known at the frontend and must be
483 * assumed known to bad guys. We're merely helping out the less
484 * clueful good guys.
485 */
486 {
487 char hostinfo[NI_MAXHOST];
488 const char *encryption_state;
489
490 pg_getnameinfo_all(&port->raddr.addr, port->raddr.salen,
491 hostinfo, sizeof(hostinfo),
492 NULL, 0,
494
496#ifdef ENABLE_GSS
497 (port->gss && port->gss->enc) ? _("GSS encryption") :
498#endif
499#ifdef USE_SSL
500 port->ssl_in_use ? _("SSL encryption") :
501#endif
502 _("no encryption");
503
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)) : \
519 0) \
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)) : \
523 0))
524
528 /* translator: last %s describes encryption state */
529 errmsg("no pg_hba.conf entry for replication connection from host \"%s\", user \"%s\", %s",
530 hostinfo, port->user_name,
533 else
536 /* translator: last %s describes encryption state */
537 errmsg("no pg_hba.conf entry for host \"%s\", user \"%s\", database \"%s\", %s",
538 hostinfo, port->user_name,
539 port->database_name,
542 break;
543 }
544
545 case uaGSS:
546#ifdef ENABLE_GSS
547 /* We might or might not have the gss workspace already */
548 if (port->gss == NULL)
549 port->gss = (pg_gssinfo *)
551 sizeof(pg_gssinfo));
552 port->gss->auth = true;
553
554 /*
555 * If GSS state was set up while enabling encryption, we can just
556 * check the client's principal. Otherwise, ask for it.
557 */
558 if (port->gss->enc)
559 status = pg_GSS_checkauth(port);
560 else
561 {
563 status = pg_GSS_recvauth(port);
564 }
565#else
566 Assert(false);
567#endif
568 break;
569
570 case uaSSPI:
571#ifdef ENABLE_SSPI
572 if (port->gss == NULL)
573 port->gss = (pg_gssinfo *)
575 sizeof(pg_gssinfo));
577 status = pg_SSPI_recvauth(port);
578#else
579 Assert(false);
580#endif
581 break;
582
583 case uaPeer:
584 status = auth_peer(port);
585 break;
586
587 case uaIdent:
588 status = ident_inet(port);
589 break;
590
591 case uaMD5:
592 case uaSCRAM:
593 status = CheckPWChallengeAuth(port, &logdetail);
594 break;
595
596 case uaPassword:
597 status = CheckPasswordAuth(port, &logdetail);
598 break;
599
600 case uaPAM:
601#ifdef USE_PAM
602 status = CheckPAMAuth(port, port->user_name, "");
603#else
604 Assert(false);
605#endif /* USE_PAM */
606 break;
607
608 case uaBSD:
609#ifdef USE_BSD_AUTH
610 status = CheckBSDAuth(port, port->user_name);
611#else
612 Assert(false);
613#endif /* USE_BSD_AUTH */
614 break;
615
616 case uaLDAP:
617#ifdef USE_LDAP
618 status = CheckLDAPAuth(port);
619#else
620 Assert(false);
621#endif
622 break;
623 case uaCert:
624 /* uaCert will be treated as if clientcert=verify-full (uaTrust) */
625 case uaTrust:
626 status = STATUS_OK;
627 break;
628 case uaOAuth:
629 status = CheckSASLAuth(&pg_be_oauth_mech, port, NULL, &logdetail,
630 &abandoned);
631 break;
632 }
633
634 if ((status == STATUS_OK && port->hba->clientcert == clientCertFull)
635 || port->hba->auth_method == uaCert)
636 {
637 /*
638 * Make sure we only check the certificate if we use the cert method
639 * or verify-full option.
640 */
641#ifdef USE_SSL
642 status = CheckCertAuth(port);
643#else
644 Assert(false);
645#endif
646 }
647
649 status == STATUS_OK &&
651 {
652 /*
653 * Normally, if log_connections is set, the call to set_authn_id()
654 * will log the connection. However, if that function is never
655 * called, perhaps because the trust method is in use, then we handle
656 * the logging here instead.
657 */
658 ereport(LOG,
659 errmsg("connection authenticated: user=\"%s\" method=%s "
660 "(%s:%d)",
661 port->user_name, hba_authname(port->hba->auth_method),
662 port->hba->sourcefile, port->hba->linenumber));
663 }
664
666 (*ClientAuthentication_hook) (port, status);
667
668 if (status == STATUS_OK)
670 else
673 status,
674 logdetail);
675}
const pg_be_sasl_mech pg_be_oauth_mech
Definition auth-oauth.c:54
static int CheckPWChallengeAuth(Port *port, const char **logdetail)
Definition auth.c:828
static int auth_peer(Port *port)
Definition auth.c:1870
static void auth_failed(Port *port, int elevel, int status, const char *logdetail)
Definition auth.c:234
ClientAuthentication_hook_type ClientAuthentication_hook
Definition auth.c:217
static int ident_inet(Port *port)
Definition auth.c:1685
#define HOSTNAME_LOOKUP_DETAIL(port)
static int CheckPasswordAuth(Port *port, const char **logdetail)
Definition auth.c:793
uint32 log_connections
@ LOG_CONNECTION_AUTHENTICATION
bool secure_loaded_verify_locations(void)
Definition be-secure.c:103
#define FATAL_CLIENT_ONLY
Definition elog.h:43
void hba_getauthmethod(Port *port)
Definition hba.c:2935
const char * hba_authname(UserAuth auth_method)
Definition hba.c:2948
@ clientCertOff
Definition hba.h:69
@ clientCertFull
Definition hba.h:71
int pg_getnameinfo_all(const struct sockaddr_storage *addr, int salen, char *node, int nodelen, char *service, int servicelen, int flags)
Definition ip.c:117
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition mcxt.c:1266
MemoryContext TopMemoryContext
Definition mcxt.c:166
#define CHECK_FOR_INTERRUPTS()
Definition miscadmin.h:125
#define AUTH_REQ_SSPI
Definition protocol.h:105
#define AUTH_REQ_GSS
Definition protocol.h:103
#define AUTH_REQ_OK
Definition protocol.h:96
bool am_walsender
Definition walsender.c:135
bool am_db_walsender
Definition walsender.c:138

References _, am_db_walsender, am_walsender, Assert, auth_failed(), auth_peer(), AUTH_REQ_GSS, AUTH_REQ_OK, AUTH_REQ_SSPI, ClientConnectionInfo::authn_id, CHECK_FOR_INTERRUPTS, CheckPasswordAuth(), CheckPWChallengeAuth(), CheckSASLAuth(), ClientAuthentication_hook, clientCertFull, clientCertOff, ereport, errcode(), errmsg, FATAL, FATAL_CLIENT_ONLY, fb(), hba_authname(), hba_getauthmethod(), HOSTNAME_LOOKUP_DETAIL, ident_inet(), LOG, LOG_CONNECTION_AUTHENTICATION, log_connections, MemoryContextAllocZero(), MyClientConnectionInfo, pg_be_oauth_mech, pg_getnameinfo_all(), port, secure_loaded_verify_locations(), sendAuthRequest(), STATUS_ERROR, STATUS_OK, TopMemoryContext, uaBSD, uaCert, uaGSS, uaIdent, uaImplicitReject, uaLDAP, uaMD5, uaOAuth, uaPAM, uaPassword, uaPeer, uaReject, uaSCRAM, uaSSPI, and uaTrust.

Referenced by PerformAuthentication().

◆ ident_inet()

static int ident_inet ( Port port)
static

Definition at line 1685 of file auth.c.

1686{
1687 const SockAddr remote_addr = port->raddr;
1688 const SockAddr local_addr = port->laddr;
1690 pgsocket sock_fd = PGINVALID_SOCKET; /* for talking to Ident server */
1691 int rc; /* Return code from a locally called function */
1692 bool ident_return;
1694 char remote_port[NI_MAXSERV];
1696 char local_port[NI_MAXSERV];
1697 char ident_port[NI_MAXSERV];
1698 char ident_query[80];
1700 struct addrinfo *ident_serv = NULL,
1701 *la = NULL,
1702 hints;
1703
1704 /*
1705 * Might look a little weird to first convert it to text and then back to
1706 * sockaddr, but it's protocol independent.
1707 */
1710 remote_port, sizeof(remote_port),
1713 local_addr_s, sizeof(local_addr_s),
1714 local_port, sizeof(local_port),
1716
1717 snprintf(ident_port, sizeof(ident_port), "%d", IDENT_PORT);
1718 hints.ai_flags = AI_NUMERICHOST;
1719 hints.ai_family = remote_addr.addr.ss_family;
1720 hints.ai_socktype = SOCK_STREAM;
1721 hints.ai_protocol = 0;
1722 hints.ai_addrlen = 0;
1723 hints.ai_canonname = NULL;
1724 hints.ai_addr = NULL;
1725 hints.ai_next = NULL;
1727 if (rc || !ident_serv)
1728 {
1729 /* we don't expect this to happen */
1730 ident_return = false;
1731 goto ident_inet_done;
1732 }
1733
1734 hints.ai_flags = AI_NUMERICHOST;
1735 hints.ai_family = local_addr.addr.ss_family;
1736 hints.ai_socktype = SOCK_STREAM;
1737 hints.ai_protocol = 0;
1738 hints.ai_addrlen = 0;
1739 hints.ai_canonname = NULL;
1740 hints.ai_addr = NULL;
1741 hints.ai_next = NULL;
1743 if (rc || !la)
1744 {
1745 /* we don't expect this to happen */
1746 ident_return = false;
1747 goto ident_inet_done;
1748 }
1749
1750 sock_fd = socket(ident_serv->ai_family, ident_serv->ai_socktype,
1751 ident_serv->ai_protocol);
1753 {
1754 ereport(LOG,
1756 errmsg("could not create socket for Ident connection: %m")));
1757 ident_return = false;
1758 goto ident_inet_done;
1759 }
1760
1761 /*
1762 * Bind to the address which the client originally contacted, otherwise
1763 * the ident server won't be able to match up the right connection. This
1764 * is necessary if the PostgreSQL server is running on an IP alias.
1765 */
1766 rc = bind(sock_fd, la->ai_addr, la->ai_addrlen);
1767 if (rc != 0)
1768 {
1769 ereport(LOG,
1771 errmsg("could not bind to local address \"%s\": %m",
1772 local_addr_s)));
1773 ident_return = false;
1774 goto ident_inet_done;
1775 }
1776
1777 rc = connect(sock_fd, ident_serv->ai_addr,
1778 ident_serv->ai_addrlen);
1779 if (rc != 0)
1780 {
1781 ereport(LOG,
1783 errmsg("could not connect to Ident server at address \"%s\", port %s: %m",
1785 ident_return = false;
1786 goto ident_inet_done;
1787 }
1788
1789 /* The query we send to the Ident server */
1790 snprintf(ident_query, sizeof(ident_query), "%s,%s\r\n",
1791 remote_port, local_port);
1792
1793 /* loop in case send is interrupted */
1794 do
1795 {
1797
1799 } while (rc < 0 && errno == EINTR);
1800
1801 if (rc < 0)
1802 {
1803 ereport(LOG,
1805 errmsg("could not send query to Ident server at address \"%s\", port %s: %m",
1807 ident_return = false;
1808 goto ident_inet_done;
1809 }
1810
1811 do
1812 {
1814
1815 rc = recv(sock_fd, ident_response, sizeof(ident_response) - 1, 0);
1816 } while (rc < 0 && errno == EINTR);
1817
1818 if (rc < 0)
1819 {
1820 ereport(LOG,
1822 errmsg("could not receive response from Ident server at address \"%s\", port %s: %m",
1824 ident_return = false;
1825 goto ident_inet_done;
1826 }
1827
1828 ident_response[rc] = '\0';
1830 if (!ident_return)
1831 ereport(LOG,
1832 (errmsg("invalidly formatted response from Ident server: \"%s\"",
1833 ident_response)));
1834
1838 if (ident_serv)
1840 if (la)
1841 pg_freeaddrinfo_all(local_addr.addr.ss_family, la);
1842
1843 if (ident_return)
1844 {
1845 /*
1846 * Success! Store the identity, then check the usermap. Note that
1847 * setting the authenticated identity is done before checking the
1848 * usermap, because at this point authentication has succeeded.
1849 */
1851 return check_usermap(port->hba->usermap, port->user_name, ident_user, false);
1852 }
1853 return STATUS_ERROR;
1854}
#define IDENT_USERNAME_MAX
Definition auth.c:69
#define IDENT_PORT
Definition auth.c:72
static bool interpret_ident_response(const char *ident_response, char *ident_user)
Definition auth.c:1604
void pg_freeaddrinfo_all(int hint_ai_family, struct addrinfo *ai)
Definition ip.c:85
int pg_getaddrinfo_all(const char *hostname, const char *servname, const struct addrinfo *hintp, struct addrinfo **result)
Definition ip.c:56
int pgsocket
Definition port.h:29
#define snprintf
Definition port.h:260
#define PGINVALID_SOCKET
Definition port.h:31
#define closesocket
Definition port.h:397
#define bind(s, addr, addrlen)
Definition win32_port.h:496
#define EINTR
Definition win32_port.h:361
#define recv(s, buf, len, flags)
Definition win32_port.h:501
#define send(s, buf, len, flags)
Definition win32_port.h:502
#define socket(af, type, protocol)
Definition win32_port.h:495
#define connect(s, name, namelen)
Definition win32_port.h:499

References bind, CHECK_FOR_INTERRUPTS, check_usermap(), closesocket, connect, EINTR, ereport, errcode_for_socket_access(), errmsg, fb(), IDENT_PORT, IDENT_USERNAME_MAX, interpret_ident_response(), LOG, pg_freeaddrinfo_all(), pg_getaddrinfo_all(), pg_getnameinfo_all(), PGINVALID_SOCKET, port, recv, send, set_authn_id(), snprintf, socket, and STATUS_ERROR.

Referenced by ClientAuthentication().

◆ interpret_ident_response()

static bool interpret_ident_response ( const char ident_response,
char ident_user 
)
static

Definition at line 1604 of file auth.c.

1606{
1607 const char *cursor = ident_response; /* Cursor into *ident_response */
1608
1609 /*
1610 * Ident's response, in the telnet tradition, should end in crlf (\r\n).
1611 */
1612 if (strlen(ident_response) < 2)
1613 return false;
1614 else if (ident_response[strlen(ident_response) - 2] != '\r')
1615 return false;
1616 else
1617 {
1618 while (*cursor != ':' && *cursor != '\r')
1619 cursor++; /* skip port field */
1620
1621 if (*cursor != ':')
1622 return false;
1623 else
1624 {
1625 /* We're positioned to colon before response type field */
1626 char response_type[80];
1627 int i; /* Index into *response_type */
1628
1629 cursor++; /* Go over colon */
1630 while (is_ident_whitespace(*cursor))
1631 cursor++; /* skip blanks */
1632 i = 0;
1633 while (*cursor != ':' && *cursor != '\r' && !is_ident_whitespace(*cursor) &&
1634 i < (int) (sizeof(response_type) - 1))
1635 response_type[i++] = *cursor++;
1636 response_type[i] = '\0';
1637 while (is_ident_whitespace(*cursor))
1638 cursor++; /* skip blanks */
1639 if (strcmp(response_type, "USERID") != 0)
1640 return false;
1641 else
1642 {
1643 /*
1644 * It's a USERID response. Good. "cursor" should be pointing
1645 * to the colon that precedes the operating system type.
1646 */
1647 if (*cursor != ':')
1648 return false;
1649 else
1650 {
1651 cursor++; /* Go over colon */
1652 /* Skip over operating system field. */
1653 while (*cursor != ':' && *cursor != '\r')
1654 cursor++;
1655 if (*cursor != ':')
1656 return false;
1657 else
1658 {
1659 cursor++; /* Go over colon */
1660 while (is_ident_whitespace(*cursor))
1661 cursor++; /* skip blanks */
1662 /* Rest of line is user name. Copy it over. */
1663 i = 0;
1664 while (*cursor != '\r' && i < IDENT_USERNAME_MAX)
1665 ident_user[i++] = *cursor++;
1666 ident_user[i] = '\0';
1667 return true;
1668 }
1669 }
1670 }
1671 }
1672 }
1673}
static bool is_ident_whitespace(const char c)
Definition auth.c:1592
int i
Definition isn.c:77
Definition type.h:138

References fb(), i, IDENT_USERNAME_MAX, and is_ident_whitespace().

Referenced by ident_inet().

◆ is_ident_whitespace()

static bool is_ident_whitespace ( const char  c)
static

Definition at line 1592 of file auth.c.

1593{
1594 return c == ' ' || c == '\t';
1595}
char * c

Referenced by interpret_ident_response().

◆ recv_password_packet()

static char * recv_password_packet ( Port port)
static

Definition at line 712 of file auth.c.

713{
715 int mtype;
716
718
719 /* Expect 'p' message type */
720 mtype = pq_getbyte();
722 {
723 /*
724 * If the client just disconnects without offering a password, don't
725 * make a log entry. This is legal per protocol spec and in fact
726 * commonly done by psql, so complaining just clutters the log.
727 */
728 if (mtype != EOF)
731 errmsg("expected password response, got message type %d",
732 mtype)));
733 return NULL; /* EOF or bad message type */
734 }
735
737 if (pq_getmessage(&buf, PG_MAX_AUTH_TOKEN_LENGTH)) /* receive password */
738 {
739 /* EOF - pq_getmessage already logged a suitable message */
740 pfree(buf.data);
741 return NULL;
742 }
743
744 /*
745 * Apply sanity check: password packet length should agree with length of
746 * contained string. Note it is safe to use strlen here because
747 * StringInfo is guaranteed to have an appended '\0'.
748 */
749 if (strlen(buf.data) + 1 != buf.len)
752 errmsg("invalid password packet size")));
753
754 /*
755 * Don't allow an empty password. Libpq treats an empty password the same
756 * as no password at all, and won't even try to authenticate. But other
757 * clients might, so allowing it would be confusing.
758 *
759 * Note that this only catches an empty password sent by the client in
760 * plaintext. There's also a check in CREATE/ALTER USER that prevents an
761 * empty string from being stored as a user's password in the first place.
762 * We rely on that for MD5 and SCRAM authentication, but we still need
763 * this check here, to prevent an empty password from being used with
764 * authentication methods that check the password against an external
765 * system, like PAM and LDAP.
766 */
767 if (buf.len == 1)
770 errmsg("empty password returned by client")));
771
772 /* Do not echo password to logs, for security. */
773 elog(DEBUG5, "received password packet");
774
775 /*
776 * Return the received string. Note we do not attempt to do any
777 * character-set conversion on it; since we don't yet know the client's
778 * encoding, there wouldn't be much point.
779 */
780 return buf.data;
781}
#define PG_MAX_AUTH_TOKEN_LENGTH
Definition auth.h:33
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
#define DEBUG5
Definition elog.h:27
#define ERRCODE_PROTOCOL_VIOLATION
Definition fe-connect.c:96
int pq_getmessage(StringInfo s, int maxlen)
Definition pqcomm.c:1204
int pq_getbyte(void)
Definition pqcomm.c:964
void pq_startmsgread(void)
Definition pqcomm.c:1142
#define PqMsg_PasswordMessage
Definition protocol.h:31
void initStringInfo(StringInfo str)
Definition stringinfo.c:97

References buf, DEBUG5, elog, ereport, errcode(), ERRCODE_INVALID_PASSWORD, ERRCODE_PROTOCOL_VIOLATION, errmsg, ERROR, fb(), initStringInfo(), pfree(), PG_MAX_AUTH_TOKEN_LENGTH, pq_getbyte(), pq_getmessage(), pq_startmsgread(), and PqMsg_PasswordMessage.

Referenced by CheckMD5Auth(), and CheckPasswordAuth().

◆ sendAuthRequest()

void sendAuthRequest ( Port port,
AuthRequest  areq,
const void extradata,
int  extralen 
)

Definition at line 682 of file auth.c.

683{
685
687
690 if (extralen > 0)
692
694
695 /*
696 * Flush message so client will see it, except for AUTH_REQ_OK and
697 * AUTH_REQ_SASL_FIN, which need not be sent until we are ready for
698 * queries.
699 */
701 pq_flush();
702
704}
int32_t int32
Definition c.h:620
#define pq_flush()
Definition libpq.h:49
void pq_sendbytes(StringInfo buf, const void *data, int datalen)
Definition pqformat.c:126
void pq_endmessage(StringInfo buf)
Definition pqformat.c:296
void pq_beginmessage(StringInfo buf, char msgtype)
Definition pqformat.c:88
static void pq_sendint32(StringInfo buf, uint32 i)
Definition pqformat.h:144
#define PqMsg_AuthenticationRequest
Definition protocol.h:50
#define AUTH_REQ_SASL_FIN
Definition protocol.h:108

References AUTH_REQ_OK, AUTH_REQ_SASL_FIN, buf, CHECK_FOR_INTERRUPTS, fb(), pq_beginmessage(), pq_endmessage(), pq_flush, pq_sendbytes(), pq_sendint32(), and PqMsg_AuthenticationRequest.

Referenced by CheckMD5Auth(), CheckPasswordAuth(), CheckSASLAuth(), and ClientAuthentication().

◆ set_authn_id()

void set_authn_id ( Port port,
const char id 
)

Definition at line 336 of file auth.c.

337{
338 Assert(id);
339
341 {
342 /*
343 * An existing authn_id should never be overwritten; that means two
344 * authentication providers are fighting (or one is fighting itself).
345 * Don't leak any authn details to the client, but don't let the
346 * connection continue, either.
347 */
349 (errmsg("authentication identifier set more than once"),
350 errdetail_log("previous identifier: \"%s\"; new identifier: \"%s\"",
352 }
353
355 MyClientConnectionInfo.auth_method = port->hba->auth_method;
356
358 {
359 ereport(LOG,
360 errmsg("connection authenticated: identity=\"%s\" method=%s "
361 "(%s:%d)",
364 port->hba->sourcefile, port->hba->linenumber));
365 }
366}
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition mcxt.c:1768

References Assert, ClientConnectionInfo::auth_method, ClientConnectionInfo::authn_id, ereport, errdetail_log(), errmsg, FATAL, hba_authname(), LOG, LOG_CONNECTION_AUTHENTICATION, log_connections, MemoryContextStrdup(), MyClientConnectionInfo, port, and TopMemoryContext.

Referenced by auth_peer(), CheckPasswordAuth(), CheckPWChallengeAuth(), ident_inet(), and validate().

Variable Documentation

◆ ClientAuthentication_hook

ClientAuthentication_hook_type ClientAuthentication_hook = NULL

Definition at line 217 of file auth.c.

Referenced by _PG_init(), ClientAuthentication(), and sepgsql_init_client_label().

◆ pg_gss_accept_delegation

bool pg_gss_accept_delegation

Definition at line 176 of file auth.c.

Referenced by secure_open_gssapi().

◆ pg_krb_caseins_users

bool pg_krb_caseins_users

Definition at line 175 of file auth.c.

◆ pg_krb_server_keyfile

char* pg_krb_server_keyfile

Definition at line 174 of file auth.c.

Referenced by secure_open_gssapi().