PostgreSQL Source Code  git master
fe-auth.c File Reference
#include "postgres_fe.h"
#include <unistd.h>
#include <fcntl.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <netdb.h>
#include <pwd.h>
#include "common/md5.h"
#include "common/scram-common.h"
#include "libpq-fe.h"
#include "fe-auth.h"
Include dependency graph for fe-auth.c:

Go to the source code of this file.

Macros

#define MAX_ALGORITHM_NAME_LEN   50
 

Functions

static int pg_SASL_init (PGconn *conn, int payloadlen)
 
static int pg_SASL_continue (PGconn *conn, int payloadlen, bool final)
 
static int pg_local_sendauth (PGconn *conn)
 
static int pg_password_sendauth (PGconn *conn, const char *password, AuthRequest areq)
 
int pg_fe_sendauth (AuthRequest areq, int payloadlen, PGconn *conn)
 
char * pg_fe_getauthname (PQExpBuffer errorMessage)
 
char * PQencryptPassword (const char *passwd, const char *user)
 
char * PQencryptPasswordConn (PGconn *conn, const char *passwd, const char *user, const char *algorithm)
 

Macro Definition Documentation

◆ MAX_ALGORITHM_NAME_LEN

#define MAX_ALGORITHM_NAME_LEN   50

Referenced by PQencryptPasswordConn().

Function Documentation

◆ pg_fe_getauthname()

char* pg_fe_getauthname ( PQExpBuffer  errorMessage)

Definition at line 1043 of file fe-auth.c.

References libpq_gettext, name, pglock_thread, pgunlock_thread, pqGetpwuid(), pqStrerror(), printfPQExpBuffer(), and username.

Referenced by connectOptions2(), and conninfo_add_defaults().

1044 {
1045  char *result = NULL;
1046  const char *name = NULL;
1047 
1048 #ifdef WIN32
1049  /* Microsoft recommends buffer size of UNLEN+1, where UNLEN = 256 */
1050  char username[256 + 1];
1051  DWORD namesize = sizeof(username);
1052 #else
1053  uid_t user_id = geteuid();
1054  char pwdbuf[BUFSIZ];
1055  struct passwd pwdstr;
1056  struct passwd *pw = NULL;
1057  int pwerr;
1058 #endif
1059 
1060  /*
1061  * Some users are using configure --enable-thread-safety-force, so we
1062  * might as well do the locking within our library to protect
1063  * pqGetpwuid(). In fact, application developers can use getpwuid() in
1064  * their application if they use the locking call we provide, or install
1065  * their own locking function using PQregisterThreadLock().
1066  */
1067  pglock_thread();
1068 
1069 #ifdef WIN32
1070  if (GetUserName(username, &namesize))
1071  name = username;
1072  else if (errorMessage)
1073  printfPQExpBuffer(errorMessage,
1074  libpq_gettext("user name lookup failure: error code %lu\n"),
1075  GetLastError());
1076 #else
1077  pwerr = pqGetpwuid(user_id, &pwdstr, pwdbuf, sizeof(pwdbuf), &pw);
1078  if (pw != NULL)
1079  name = pw->pw_name;
1080  else if (errorMessage)
1081  {
1082  if (pwerr != 0)
1083  printfPQExpBuffer(errorMessage,
1084  libpq_gettext("could not look up local user ID %d: %s\n"),
1085  (int) user_id,
1086  pqStrerror(pwerr, pwdbuf, sizeof(pwdbuf)));
1087  else
1088  printfPQExpBuffer(errorMessage,
1089  libpq_gettext("local user with ID %d does not exist\n"),
1090  (int) user_id);
1091  }
1092 #endif
1093 
1094  if (name)
1095  {
1096  result = strdup(name);
1097  if (result == NULL && errorMessage)
1098  printfPQExpBuffer(errorMessage,
1099  libpq_gettext("out of memory\n"));
1100  }
1101 
1102  pgunlock_thread();
1103 
1104  return result;
1105 }
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
char * pqStrerror(int errnum, char *strerrbuf, size_t buflen)
Definition: thread.c:61
#define pglock_thread()
Definition: libpq-int.h:564
static char * username
Definition: initdb.c:132
int uid_t
Definition: win32_port.h:241
const char * name
Definition: encode.c:521
int pqGetpwuid(uid_t uid, struct passwd *resultbuf, char *buffer, size_t buflen, struct passwd **result)
Definition: thread.c:95
#define pgunlock_thread()
Definition: libpq-int.h:565
#define libpq_gettext(x)
Definition: libpq-int.h:760

◆ pg_fe_sendauth()

int pg_fe_sendauth ( AuthRequest  areq,
int  payloadlen,
PGconn conn 
)

Definition at line 836 of file fe-auth.c.

References AUTH_REQ_CRYPT, AUTH_REQ_GSS, AUTH_REQ_GSS_CONT, AUTH_REQ_KRB4, AUTH_REQ_KRB5, AUTH_REQ_MD5, AUTH_REQ_OK, AUTH_REQ_PASSWORD, AUTH_REQ_SASL, AUTH_REQ_SASL_CONT, AUTH_REQ_SASL_FIN, AUTH_REQ_SCM_CREDS, AUTH_REQ_SSPI, pg_conn::connhost, pg_conn::errorMessage, PQExpBufferData::len, libpq_gettext, password, pg_conn_host::password, pg_conn::password_needed, pg_local_sendauth(), pg_password_sendauth(), pg_SASL_continue(), pg_SASL_init(), pg_strcasecmp(), pglock_thread, pg_conn::pgpass, pgunlock_thread, PQnoPasswordSupplied, printfPQExpBuffer(), pg_conn::sasl_state, STATUS_ERROR, STATUS_OK, and pg_conn::whichhost.

Referenced by PQconnectPoll().

837 {
838  switch (areq)
839  {
840  case AUTH_REQ_OK:
841  break;
842 
843  case AUTH_REQ_KRB4:
845  libpq_gettext("Kerberos 4 authentication not supported\n"));
846  return STATUS_ERROR;
847 
848  case AUTH_REQ_KRB5:
850  libpq_gettext("Kerberos 5 authentication not supported\n"));
851  return STATUS_ERROR;
852 
853 #if defined(ENABLE_GSS) || defined(ENABLE_SSPI)
854  case AUTH_REQ_GSS:
855 #if !defined(ENABLE_SSPI)
856  /* no native SSPI, so use GSSAPI library for it */
857  case AUTH_REQ_SSPI:
858 #endif
859  {
860  int r;
861 
862  pglock_thread();
863 
864  /*
865  * If we have both GSS and SSPI support compiled in, use SSPI
866  * support by default. This is overridable by a connection
867  * string parameter. Note that when using SSPI we still leave
868  * the negotiate parameter off, since we want SSPI to use the
869  * GSSAPI kerberos protocol. For actual SSPI negotiate
870  * protocol, we use AUTH_REQ_SSPI.
871  */
872 #if defined(ENABLE_GSS) && defined(ENABLE_SSPI)
873  if (conn->gsslib && (pg_strcasecmp(conn->gsslib, "gssapi") == 0))
874  r = pg_GSS_startup(conn, payloadlen);
875  else
876  r = pg_SSPI_startup(conn, 0, payloadlen);
877 #elif defined(ENABLE_GSS) && !defined(ENABLE_SSPI)
878  r = pg_GSS_startup(conn, payloadlen);
879 #elif !defined(ENABLE_GSS) && defined(ENABLE_SSPI)
880  r = pg_SSPI_startup(conn, 0, payloadlen);
881 #endif
882  if (r != STATUS_OK)
883  {
884  /* Error message already filled in. */
885  pgunlock_thread();
886  return STATUS_ERROR;
887  }
888  pgunlock_thread();
889  }
890  break;
891 
892  case AUTH_REQ_GSS_CONT:
893  {
894  int r;
895 
896  pglock_thread();
897 #if defined(ENABLE_GSS) && defined(ENABLE_SSPI)
898  if (conn->usesspi)
899  r = pg_SSPI_continue(conn, payloadlen);
900  else
901  r = pg_GSS_continue(conn, payloadlen);
902 #elif defined(ENABLE_GSS) && !defined(ENABLE_SSPI)
903  r = pg_GSS_continue(conn, payloadlen);
904 #elif !defined(ENABLE_GSS) && defined(ENABLE_SSPI)
905  r = pg_SSPI_continue(conn, payloadlen);
906 #endif
907  if (r != STATUS_OK)
908  {
909  /* Error message already filled in. */
910  pgunlock_thread();
911  return STATUS_ERROR;
912  }
913  pgunlock_thread();
914  }
915  break;
916 #else /* defined(ENABLE_GSS) || defined(ENABLE_SSPI) */
917  /* No GSSAPI *or* SSPI support */
918  case AUTH_REQ_GSS:
919  case AUTH_REQ_GSS_CONT:
921  libpq_gettext("GSSAPI authentication not supported\n"));
922  return STATUS_ERROR;
923 #endif /* defined(ENABLE_GSS) || defined(ENABLE_SSPI) */
924 
925 #ifdef ENABLE_SSPI
926  case AUTH_REQ_SSPI:
927 
928  /*
929  * SSPI has it's own startup message so libpq can decide which
930  * method to use. Indicate to pg_SSPI_startup that we want SSPI
931  * negotiation instead of Kerberos.
932  */
933  pglock_thread();
934  if (pg_SSPI_startup(conn, 1, payloadlen) != STATUS_OK)
935  {
936  /* Error message already filled in. */
937  pgunlock_thread();
938  return STATUS_ERROR;
939  }
940  pgunlock_thread();
941  break;
942 #else
943 
944  /*
945  * No SSPI support. However, if we have GSSAPI but not SSPI
946  * support, AUTH_REQ_SSPI will have been handled in the codepath
947  * for AUTH_REQ_GSSAPI above, so don't duplicate the case label in
948  * that case.
949  */
950 #if !defined(ENABLE_GSS)
951  case AUTH_REQ_SSPI:
953  libpq_gettext("SSPI authentication not supported\n"));
954  return STATUS_ERROR;
955 #endif /* !define(ENABLE_GSSAPI) */
956 #endif /* ENABLE_SSPI */
957 
958 
959  case AUTH_REQ_CRYPT:
961  libpq_gettext("Crypt authentication not supported\n"));
962  return STATUS_ERROR;
963 
964  case AUTH_REQ_MD5:
965  case AUTH_REQ_PASSWORD:
966  {
967  char *password;
968 
969  conn->password_needed = true;
970  password = conn->connhost[conn->whichhost].password;
971  if (password == NULL)
972  password = conn->pgpass;
973  if (password == NULL || password[0] == '\0')
974  {
977  return STATUS_ERROR;
978  }
979  if (pg_password_sendauth(conn, password, areq) != STATUS_OK)
980  {
982  "fe_sendauth: error sending password authentication\n");
983  return STATUS_ERROR;
984  }
985  break;
986  }
987 
988  case AUTH_REQ_SASL:
989 
990  /*
991  * The request contains the name (as assigned by IANA) of the
992  * authentication mechanism.
993  */
994  if (pg_SASL_init(conn, payloadlen) != STATUS_OK)
995  {
996  /* pg_SASL_init already set the error message */
997  return STATUS_ERROR;
998  }
999  break;
1000 
1001  case AUTH_REQ_SASL_CONT:
1002  case AUTH_REQ_SASL_FIN:
1003  if (conn->sasl_state == NULL)
1004  {
1006  "fe_sendauth: invalid authentication request from server: AUTH_REQ_SASL_CONT without AUTH_REQ_SASL\n");
1007  return STATUS_ERROR;
1008  }
1009  if (pg_SASL_continue(conn, payloadlen,
1010  (areq == AUTH_REQ_SASL_FIN)) != STATUS_OK)
1011  {
1012  /* Use error message, if set already */
1013  if (conn->errorMessage.len == 0)
1015  "fe_sendauth: error in SASL authentication\n");
1016  return STATUS_ERROR;
1017  }
1018  break;
1019 
1020  case AUTH_REQ_SCM_CREDS:
1021  if (pg_local_sendauth(conn) != STATUS_OK)
1022  return STATUS_ERROR;
1023  break;
1024 
1025  default:
1027  libpq_gettext("authentication method %u not supported\n"), areq);
1028  return STATUS_ERROR;
1029  }
1030 
1031  return STATUS_OK;
1032 }
static char password[100]
Definition: streamutil.c:54
#define AUTH_REQ_SSPI
Definition: pqcomm.h:174
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
static int pg_local_sendauth(PGconn *conn)
Definition: fe-auth.c:708
#define AUTH_REQ_SASL_FIN
Definition: pqcomm.h:177
bool password_needed
Definition: libpq-int.h:408
#define AUTH_REQ_OK
Definition: pqcomm.h:165
#define AUTH_REQ_GSS
Definition: pqcomm.h:172
static int pg_SASL_init(PGconn *conn, int payloadlen)
Definition: fe-auth.c:486
static int pg_password_sendauth(PGconn *conn, const char *password, AuthRequest areq)
Definition: fe-auth.c:760
#define STATUS_ERROR
Definition: c.h:1009
#define pglock_thread()
Definition: libpq-int.h:564
#define PQnoPasswordSupplied
Definition: libpq-fe.h:512
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
#define AUTH_REQ_MD5
Definition: pqcomm.h:170
static int pg_SASL_continue(PGconn *conn, int payloadlen, bool final)
Definition: fe-auth.c:638
pg_conn_host * connhost
Definition: libpq-int.h:398
void * sasl_state
Definition: libpq-int.h:455
#define AUTH_REQ_CRYPT
Definition: pqcomm.h:169
#define STATUS_OK
Definition: c.h:1008
#define AUTH_REQ_PASSWORD
Definition: pqcomm.h:168
PQExpBufferData errorMessage
Definition: libpq-int.h:494
#define AUTH_REQ_KRB5
Definition: pqcomm.h:167
#define AUTH_REQ_SASL_CONT
Definition: pqcomm.h:176
#define AUTH_REQ_KRB4
Definition: pqcomm.h:166
char * pgpass
Definition: libpq-int.h:344
#define AUTH_REQ_SASL
Definition: pqcomm.h:175
#define AUTH_REQ_GSS_CONT
Definition: pqcomm.h:173
#define pgunlock_thread()
Definition: libpq-int.h:565
int whichhost
Definition: libpq-int.h:397
#define libpq_gettext(x)
Definition: libpq-int.h:760
#define AUTH_REQ_SCM_CREDS
Definition: pqcomm.h:171
char * password
Definition: libpq-int.h:312

◆ pg_local_sendauth()

static int pg_local_sendauth ( PGconn conn)
static

Definition at line 708 of file fe-auth.c.

References buf, pg_conn::errorMessage, libpq_gettext, pqStrerror(), printfPQExpBuffer(), pg_conn::sock, STATUS_ERROR, and STATUS_OK.

Referenced by pg_fe_sendauth().

709 {
710 #ifdef HAVE_STRUCT_CMSGCRED
711  char buf;
712  struct iovec iov;
713  struct msghdr msg;
714  struct cmsghdr *cmsg;
715  union
716  {
717  struct cmsghdr hdr;
718  unsigned char buf[CMSG_SPACE(sizeof(struct cmsgcred))];
719  } cmsgbuf;
720 
721  /*
722  * The backend doesn't care what we send here, but it wants exactly one
723  * character to force recvmsg() to block and wait for us.
724  */
725  buf = '\0';
726  iov.iov_base = &buf;
727  iov.iov_len = 1;
728 
729  memset(&msg, 0, sizeof(msg));
730  msg.msg_iov = &iov;
731  msg.msg_iovlen = 1;
732 
733  /* We must set up a message that will be filled in by kernel */
734  memset(&cmsgbuf, 0, sizeof(cmsgbuf));
735  msg.msg_control = &cmsgbuf.buf;
736  msg.msg_controllen = sizeof(cmsgbuf.buf);
737  cmsg = CMSG_FIRSTHDR(&msg);
738  cmsg->cmsg_len = CMSG_LEN(sizeof(struct cmsgcred));
739  cmsg->cmsg_level = SOL_SOCKET;
740  cmsg->cmsg_type = SCM_CREDS;
741 
742  if (sendmsg(conn->sock, &msg, 0) == -1)
743  {
744  char sebuf[256];
745 
747  "pg_local_sendauth: sendmsg: %s\n",
748  pqStrerror(errno, sebuf, sizeof(sebuf)));
749  return STATUS_ERROR;
750  }
751  return STATUS_OK;
752 #else
754  libpq_gettext("SCM_CRED authentication method not supported\n"));
755  return STATUS_ERROR;
756 #endif
757 }
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
char * pqStrerror(int errnum, char *strerrbuf, size_t buflen)
Definition: thread.c:61
#define STATUS_ERROR
Definition: c.h:1009
static char * buf
Definition: pg_test_fsync.c:67
#define STATUS_OK
Definition: c.h:1008
pgsocket sock
Definition: libpq-int.h:401
PQExpBufferData errorMessage
Definition: libpq-int.h:494
#define libpq_gettext(x)
Definition: libpq-int.h:760

◆ pg_password_sendauth()

static int pg_password_sendauth ( PGconn conn,
const char *  password,
AuthRequest  areq 
)
static

Definition at line 760 of file fe-auth.c.

References AUTH_REQ_MD5, AUTH_REQ_PASSWORD, pg_conn::errorMessage, free, libpq_gettext, malloc, MD5_PASSWD_LEN, password, pg_md5_encrypt(), PG_PROTOCOL_MAJOR, pg_conn::pguser, pqGetnchar(), pqPacketSend(), printfPQExpBuffer(), pg_conn::pversion, and STATUS_ERROR.

Referenced by pg_fe_sendauth().

761 {
762  int ret;
763  char *crypt_pwd = NULL;
764  const char *pwd_to_send;
765  char md5Salt[4];
766 
767  /* Read the salt from the AuthenticationMD5 message. */
768  if (areq == AUTH_REQ_MD5)
769  {
770  if (pqGetnchar(md5Salt, 4, conn))
771  return STATUS_ERROR; /* shouldn't happen */
772  }
773 
774  /* Encrypt the password if needed. */
775 
776  switch (areq)
777  {
778  case AUTH_REQ_MD5:
779  {
780  char *crypt_pwd2;
781 
782  /* Allocate enough space for two MD5 hashes */
783  crypt_pwd = malloc(2 * (MD5_PASSWD_LEN + 1));
784  if (!crypt_pwd)
785  {
787  libpq_gettext("out of memory\n"));
788  return STATUS_ERROR;
789  }
790 
791  crypt_pwd2 = crypt_pwd + MD5_PASSWD_LEN + 1;
792  if (!pg_md5_encrypt(password, conn->pguser,
793  strlen(conn->pguser), crypt_pwd2))
794  {
795  free(crypt_pwd);
796  return STATUS_ERROR;
797  }
798  if (!pg_md5_encrypt(crypt_pwd2 + strlen("md5"), md5Salt,
799  4, crypt_pwd))
800  {
801  free(crypt_pwd);
802  return STATUS_ERROR;
803  }
804 
805  pwd_to_send = crypt_pwd;
806  break;
807  }
808  case AUTH_REQ_PASSWORD:
809  pwd_to_send = password;
810  break;
811  default:
812  return STATUS_ERROR;
813  }
814  /* Packet has a message type as of protocol 3.0 */
815  if (PG_PROTOCOL_MAJOR(conn->pversion) >= 3)
816  ret = pqPacketSend(conn, 'p', pwd_to_send, strlen(pwd_to_send) + 1);
817  else
818  ret = pqPacketSend(conn, 0, pwd_to_send, strlen(pwd_to_send) + 1);
819  if (crypt_pwd)
820  free(crypt_pwd);
821  return ret;
822 }
static char password[100]
Definition: streamutil.c:54
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
#define STATUS_ERROR
Definition: c.h:1009
int pqGetnchar(char *s, size_t len, PGconn *conn)
Definition: fe-misc.c:195
#define PG_PROTOCOL_MAJOR(v)
Definition: pqcomm.h:104
#define AUTH_REQ_MD5
Definition: pqcomm.h:170
#define malloc(a)
Definition: header.h:50
bool pg_md5_encrypt(const char *passwd, const char *salt, size_t salt_len, char *buf)
Definition: md5.c:323
char * pguser
Definition: libpq-int.h:343
#define AUTH_REQ_PASSWORD
Definition: pqcomm.h:168
PQExpBufferData errorMessage
Definition: libpq-int.h:494
#define MD5_PASSWD_LEN
Definition: md5.h:19
#define free(a)
Definition: header.h:65
ProtocolVersion pversion
Definition: libpq-int.h:405
int pqPacketSend(PGconn *conn, char pack_type, const void *buf, size_t buf_len)
Definition: fe-connect.c:3983
#define libpq_gettext(x)
Definition: libpq-int.h:760

◆ pg_SASL_continue()

static int pg_SASL_continue ( PGconn conn,
int  payloadlen,
bool  final 
)
static

Definition at line 638 of file fe-auth.c.

References pg_conn::errorMessage, free, libpq_gettext, malloc, output(), pg_fe_scram_exchange(), pqGetnchar(), pqPacketSend(), printfPQExpBuffer(), pg_conn::sasl_state, STATUS_ERROR, STATUS_OK, and success.

Referenced by pg_fe_sendauth().

639 {
640  char *output;
641  int outputlen;
642  bool done;
643  bool success;
644  int res;
645  char *challenge;
646 
647  /* Read the SASL challenge from the AuthenticationSASLContinue message. */
648  challenge = malloc(payloadlen + 1);
649  if (!challenge)
650  {
652  libpq_gettext("out of memory allocating SASL buffer (%d)\n"),
653  payloadlen);
654  return STATUS_ERROR;
655  }
656 
657  if (pqGetnchar(challenge, payloadlen, conn))
658  {
659  free(challenge);
660  return STATUS_ERROR;
661  }
662  /* For safety and convenience, ensure the buffer is NULL-terminated. */
663  challenge[payloadlen] = '\0';
664 
666  challenge, payloadlen,
667  &output, &outputlen,
668  &done, &success);
669  free(challenge); /* don't need the input anymore */
670 
671  if (final && !done)
672  {
673  if (outputlen != 0)
674  free(output);
675 
677  libpq_gettext("AuthenticationSASLFinal received from server, but SASL authentication was not completed\n"));
678  return STATUS_ERROR;
679  }
680  if (outputlen != 0)
681  {
682  /*
683  * Send the SASL response to the server.
684  */
685  res = pqPacketSend(conn, 'p', output, outputlen);
686  free(output);
687 
688  if (res != STATUS_OK)
689  return STATUS_ERROR;
690  }
691 
692  if (done && !success)
693  return STATUS_ERROR;
694 
695  return STATUS_OK;
696 }
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
static void output(uint64 loop_count)
#define STATUS_ERROR
Definition: c.h:1009
void pg_fe_scram_exchange(void *opaq, char *input, int inputlen, char **output, int *outputlen, bool *done, bool *success)
int pqGetnchar(char *s, size_t len, PGconn *conn)
Definition: fe-misc.c:195
#define malloc(a)
Definition: header.h:50
static bool success
void * sasl_state
Definition: libpq-int.h:455
#define STATUS_OK
Definition: c.h:1008
PQExpBufferData errorMessage
Definition: libpq-int.h:494
#define free(a)
Definition: header.h:65
int pqPacketSend(PGconn *conn, char pack_type, const void *buf, size_t buf_len)
Definition: fe-connect.c:3983
#define libpq_gettext(x)
Definition: libpq-int.h:760

◆ pg_SASL_init()

static int pg_SASL_init ( PGconn conn,
int  payloadlen 
)
static

Definition at line 486 of file fe-auth.c.

References pg_conn::connhost, PQExpBufferData::data, error(), pg_conn::errorMessage, free, initPQExpBuffer(), libpq_gettext, password, pg_conn_host::password, pg_conn::password_needed, pg_fe_scram_exchange(), pg_fe_scram_init(), pg_conn::pgpass, PQExpBufferDataBroken, pqFlush(), pqGets(), PQnoPasswordSupplied, pqPutInt(), pqPutMsgEnd(), pqPutMsgStart(), pqPutnchar(), pqPuts(), printfPQExpBuffer(), pg_conn::sasl_state, pg_conn::scram_channel_binding, SCRAM_SHA_256_NAME, SCRAM_SHA_256_PLUS_NAME, pg_conn::ssl_in_use, STATUS_ERROR, STATUS_OK, success, termPQExpBuffer(), and pg_conn::whichhost.

Referenced by pg_fe_sendauth().

487 {
488  char *initialresponse = NULL;
489  int initialresponselen;
490  bool done;
491  bool success;
492  const char *selected_mechanism;
493  PQExpBufferData mechanism_buf;
494  char *password;
495 
496  initPQExpBuffer(&mechanism_buf);
497 
498  if (conn->sasl_state)
499  {
501  libpq_gettext("duplicate SASL authentication request\n"));
502  goto error;
503  }
504 
505  /*
506  * Parse the list of SASL authentication mechanisms in the
507  * AuthenticationSASL message, and select the best mechanism that we
508  * support. SCRAM-SHA-256-PLUS and SCRAM-SHA-256 are the only ones
509  * supported at the moment, listed by order of decreasing importance.
510  */
511  selected_mechanism = NULL;
512  for (;;)
513  {
514  if (pqGets(&mechanism_buf, conn))
515  {
517  "fe_sendauth: invalid authentication request from server: invalid list of authentication mechanisms\n");
518  goto error;
519  }
520  if (PQExpBufferDataBroken(mechanism_buf))
521  goto oom_error;
522 
523  /* An empty string indicates end of list */
524  if (mechanism_buf.data[0] == '\0')
525  break;
526 
527  /*
528  * Select the mechanism to use. Pick SCRAM-SHA-256-PLUS over anything
529  * else if a channel binding type is set. Pick SCRAM-SHA-256 if
530  * nothing else has already been picked. If we add more mechanisms, a
531  * more refined priority mechanism might become necessary.
532  */
533  if (conn->ssl_in_use &&
534  conn->scram_channel_binding &&
535  strlen(conn->scram_channel_binding) > 0 &&
536  strcmp(mechanism_buf.data, SCRAM_SHA_256_PLUS_NAME) == 0)
537  selected_mechanism = SCRAM_SHA_256_PLUS_NAME;
538  else if (strcmp(mechanism_buf.data, SCRAM_SHA_256_NAME) == 0 &&
539  !selected_mechanism)
540  selected_mechanism = SCRAM_SHA_256_NAME;
541  }
542 
543  if (!selected_mechanism)
544  {
546  libpq_gettext("none of the server's SASL authentication mechanisms are supported\n"));
547  goto error;
548  }
549 
550  /*
551  * Now that the SASL mechanism has been chosen for the exchange,
552  * initialize its state information.
553  */
554 
555  /*
556  * First, select the password to use for the exchange, complaining if
557  * there isn't one. Currently, all supported SASL mechanisms require a
558  * password, so we can just go ahead here without further distinction.
559  */
560  conn->password_needed = true;
561  password = conn->connhost[conn->whichhost].password;
562  if (password == NULL)
563  password = conn->pgpass;
564  if (password == NULL || password[0] == '\0')
565  {
568  goto error;
569  }
570 
571  /*
572  * Initialize the SASL state information with all the information gathered
573  * during the initial exchange.
574  *
575  * Note: Only tls-unique is supported for the moment.
576  */
577  conn->sasl_state = pg_fe_scram_init(conn,
578  password,
579  selected_mechanism);
580  if (!conn->sasl_state)
581  goto oom_error;
582 
583  /* Get the mechanism-specific Initial Client Response, if any */
585  NULL, -1,
586  &initialresponse, &initialresponselen,
587  &done, &success);
588 
589  if (done && !success)
590  goto error;
591 
592  /*
593  * Build a SASLInitialResponse message, and send it.
594  */
595  if (pqPutMsgStart('p', true, conn))
596  goto error;
597  if (pqPuts(selected_mechanism, conn))
598  goto error;
599  if (initialresponse)
600  {
601  if (pqPutInt(initialresponselen, 4, conn))
602  goto error;
603  if (pqPutnchar(initialresponse, initialresponselen, conn))
604  goto error;
605  }
606  if (pqPutMsgEnd(conn))
607  goto error;
608  if (pqFlush(conn))
609  goto error;
610 
611  termPQExpBuffer(&mechanism_buf);
612  if (initialresponse)
613  free(initialresponse);
614 
615  return STATUS_OK;
616 
617 error:
618  termPQExpBuffer(&mechanism_buf);
619  if (initialresponse)
620  free(initialresponse);
621  return STATUS_ERROR;
622 
623 oom_error:
624  termPQExpBuffer(&mechanism_buf);
625  if (initialresponse)
626  free(initialresponse);
628  libpq_gettext("out of memory\n"));
629  return STATUS_ERROR;
630 }
int pqFlush(PGconn *conn)
Definition: fe-misc.c:961
static char password[100]
Definition: streamutil.c:54
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
static void error(void)
Definition: sql-dyntest.c:147
#define SCRAM_SHA_256_NAME
Definition: scram-common.h:19
int pqGets(PQExpBuffer buf, PGconn *conn)
Definition: fe-misc.c:163
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:128
bool password_needed
Definition: libpq-int.h:408
int pqPutMsgStart(char msg_type, bool force_len, PGconn *conn)
Definition: fe-misc.c:519
#define STATUS_ERROR
Definition: c.h:1009
#define PQnoPasswordSupplied
Definition: libpq-fe.h:512
void pg_fe_scram_exchange(void *opaq, char *input, int inputlen, char **output, int *outputlen, bool *done, bool *success)
int pqPutInt(int value, size_t bytes, PGconn *conn)
Definition: fe-misc.c:307
pg_conn_host * connhost
Definition: libpq-int.h:398
static bool success
bool ssl_in_use
Definition: libpq-int.h:458
void * sasl_state
Definition: libpq-int.h:455
#define SCRAM_SHA_256_PLUS_NAME
Definition: scram-common.h:20
int pqPuts(const char *s, PGconn *conn)
Definition: fe-misc.c:179
#define STATUS_OK
Definition: c.h:1008
char * scram_channel_binding
Definition: libpq-int.h:352
PQExpBufferData errorMessage
Definition: libpq-int.h:494
#define free(a)
Definition: header.h:65
#define PQExpBufferDataBroken(buf)
Definition: pqexpbuffer.h:67
int pqPutnchar(const char *s, size_t len, PGconn *conn)
Definition: fe-misc.c:246
char * pgpass
Definition: libpq-int.h:344
int pqPutMsgEnd(PGconn *conn)
Definition: fe-misc.c:587
void * pg_fe_scram_init(PGconn *conn, const char *password, const char *sasl_mechanism)
Definition: fe-auth-scram.c:81
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
int whichhost
Definition: libpq-int.h:397
#define libpq_gettext(x)
Definition: libpq-int.h:760
char * password
Definition: libpq-int.h:312

◆ PQencryptPassword()

char* PQencryptPassword ( const char *  passwd,
const char *  user 
)

Definition at line 1117 of file fe-auth.c.

References free, malloc, MD5_PASSWD_LEN, and pg_md5_encrypt().

1118 {
1119  char *crypt_pwd;
1120 
1121  crypt_pwd = malloc(MD5_PASSWD_LEN + 1);
1122  if (!crypt_pwd)
1123  return NULL;
1124 
1125  if (!pg_md5_encrypt(passwd, user, strlen(user), crypt_pwd))
1126  {
1127  free(crypt_pwd);
1128  return NULL;
1129  }
1130 
1131  return crypt_pwd;
1132 }
#define malloc(a)
Definition: header.h:50
bool pg_md5_encrypt(const char *passwd, const char *salt, size_t salt_len, char *buf)
Definition: md5.c:323
#define MD5_PASSWD_LEN
Definition: md5.h:19
#define free(a)
Definition: header.h:65
static char * user
Definition: pg_regress.c:93

◆ PQencryptPasswordConn()

char* PQencryptPasswordConn ( PGconn conn,
const char *  passwd,
const char *  user,
const char *  algorithm 
)

Definition at line 1159 of file fe-auth.c.

References pg_conn::errorMessage, free, libpq_gettext, malloc, MAX_ALGORITHM_NAME_LEN, MD5_PASSWD_LEN, pg_fe_scram_build_verifier(), pg_md5_encrypt(), PGRES_TUPLES_OK, PQclear(), PQexec(), PQgetvalue(), PQnfields(), PQntuples(), PQresultStatus(), printfPQExpBuffer(), and val.

Referenced by exec_command_password(), and main().

1161 {
1162 #define MAX_ALGORITHM_NAME_LEN 50
1163  char algobuf[MAX_ALGORITHM_NAME_LEN + 1];
1164  char *crypt_pwd = NULL;
1165 
1166  if (!conn)
1167  return NULL;
1168 
1169  /* If no algorithm was given, ask the server. */
1170  if (algorithm == NULL)
1171  {
1172  PGresult *res;
1173  char *val;
1174 
1175  res = PQexec(conn, "show password_encryption");
1176  if (res == NULL)
1177  {
1178  /* PQexec() should've set conn->errorMessage already */
1179  return NULL;
1180  }
1181  if (PQresultStatus(res) != PGRES_TUPLES_OK)
1182  {
1183  /* PQexec() should've set conn->errorMessage already */
1184  PQclear(res);
1185  return NULL;
1186  }
1187  if (PQntuples(res) != 1 || PQnfields(res) != 1)
1188  {
1189  PQclear(res);
1191  libpq_gettext("unexpected shape of result set returned for SHOW\n"));
1192  return NULL;
1193  }
1194  val = PQgetvalue(res, 0, 0);
1195 
1196  if (strlen(val) > MAX_ALGORITHM_NAME_LEN)
1197  {
1198  PQclear(res);
1200  libpq_gettext("password_encryption value too long\n"));
1201  return NULL;
1202  }
1203  strcpy(algobuf, val);
1204  PQclear(res);
1205 
1206  algorithm = algobuf;
1207  }
1208 
1209  /*
1210  * Also accept "on" and "off" as aliases for "md5", because
1211  * password_encryption was a boolean before PostgreSQL 10. We refuse to
1212  * send the password in plaintext even if it was "off".
1213  */
1214  if (strcmp(algorithm, "on") == 0 ||
1215  strcmp(algorithm, "off") == 0)
1216  algorithm = "md5";
1217 
1218  /*
1219  * Ok, now we know what algorithm to use
1220  */
1221  if (strcmp(algorithm, "scram-sha-256") == 0)
1222  {
1223  crypt_pwd = pg_fe_scram_build_verifier(passwd);
1224  }
1225  else if (strcmp(algorithm, "md5") == 0)
1226  {
1227  crypt_pwd = malloc(MD5_PASSWD_LEN + 1);
1228  if (crypt_pwd)
1229  {
1230  if (!pg_md5_encrypt(passwd, user, strlen(user), crypt_pwd))
1231  {
1232  free(crypt_pwd);
1233  crypt_pwd = NULL;
1234  }
1235  }
1236  }
1237  else
1238  {
1240  libpq_gettext("unrecognized password encryption algorithm \"%s\"\n"),
1241  algorithm);
1242  return NULL;
1243  }
1244 
1245  if (!crypt_pwd)
1247  libpq_gettext("out of memory\n"));
1248 
1249  return crypt_pwd;
1250 }
int PQnfields(const PGresult *res)
Definition: fe-exec.c:2732
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3118
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2724
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2647
#define malloc(a)
Definition: header.h:50
#define MAX_ALGORITHM_NAME_LEN
bool pg_md5_encrypt(const char *passwd, const char *salt, size_t salt_len, char *buf)
Definition: md5.c:323
PQExpBufferData errorMessage
Definition: libpq-int.h:494
void PQclear(PGresult *res)
Definition: fe-exec.c:671
#define MD5_PASSWD_LEN
Definition: md5.h:19
#define free(a)
Definition: header.h:65
char * pg_fe_scram_build_verifier(const char *password)
static char * user
Definition: pg_regress.c:93
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1897
long val
Definition: informix.c:689
#define libpq_gettext(x)
Definition: libpq-int.h:760