PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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 "libpq-fe.h"
#include "libpq/scram.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

#define MAX_ALGORITHM_NAME_LEN   50

Referenced by PQencryptPasswordConn().

Function Documentation

char* pg_fe_getauthname ( PQExpBuffer  errorMessage)

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

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

Referenced by connectOptions2(), and conninfo_add_defaults().

1015 {
1016  char *result = NULL;
1017  const char *name = NULL;
1018 
1019 #ifdef WIN32
1020  /* Microsoft recommends buffer size of UNLEN+1, where UNLEN = 256 */
1021  char username[256 + 1];
1022  DWORD namesize = sizeof(username);
1023 #else
1024  uid_t user_id = geteuid();
1025  char pwdbuf[BUFSIZ];
1026  struct passwd pwdstr;
1027  struct passwd *pw = NULL;
1028  int pwerr;
1029 #endif
1030 
1031  /*
1032  * Some users are using configure --enable-thread-safety-force, so we
1033  * might as well do the locking within our library to protect
1034  * pqGetpwuid(). In fact, application developers can use getpwuid() in
1035  * their application if they use the locking call we provide, or install
1036  * their own locking function using PQregisterThreadLock().
1037  */
1038  pglock_thread();
1039 
1040 #ifdef WIN32
1041  if (GetUserName(username, &namesize))
1042  name = username;
1043  else if (errorMessage)
1044  printfPQExpBuffer(errorMessage,
1045  libpq_gettext("user name lookup failure: error code %lu\n"),
1046  GetLastError());
1047 #else
1048  pwerr = pqGetpwuid(user_id, &pwdstr, pwdbuf, sizeof(pwdbuf), &pw);
1049  if (pw != NULL)
1050  name = pw->pw_name;
1051  else if (errorMessage)
1052  {
1053  if (pwerr != 0)
1054  printfPQExpBuffer(errorMessage,
1055  libpq_gettext("could not look up local user ID %d: %s\n"),
1056  (int) user_id,
1057  pqStrerror(pwerr, pwdbuf, sizeof(pwdbuf)));
1058  else
1059  printfPQExpBuffer(errorMessage,
1060  libpq_gettext("local user with ID %d does not exist\n"),
1061  (int) user_id);
1062  }
1063 #endif
1064 
1065  if (name)
1066  {
1067  result = strdup(name);
1068  if (result == NULL && errorMessage)
1069  printfPQExpBuffer(errorMessage,
1070  libpq_gettext("out of memory\n"));
1071  }
1072 
1073  pgunlock_thread();
1074 
1075  return result;
1076 }
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
int uid_t
Definition: win32.h:250
char * pqStrerror(int errnum, char *strerrbuf, size_t buflen)
Definition: thread.c:61
#define pglock_thread()
Definition: libpq-int.h:562
return result
Definition: formatting.c:1632
static char * username
Definition: initdb.c:131
#define NULL
Definition: c.h:229
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:563
#define libpq_gettext(x)
Definition: libpq-int.h:683
int pg_fe_sendauth ( AuthRequest  areq,
int  payloadlen,
PGconn conn 
)

Definition at line 807 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, NULL, 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().

808 {
809  switch (areq)
810  {
811  case AUTH_REQ_OK:
812  break;
813 
814  case AUTH_REQ_KRB4:
816  libpq_gettext("Kerberos 4 authentication not supported\n"));
817  return STATUS_ERROR;
818 
819  case AUTH_REQ_KRB5:
821  libpq_gettext("Kerberos 5 authentication not supported\n"));
822  return STATUS_ERROR;
823 
824 #if defined(ENABLE_GSS) || defined(ENABLE_SSPI)
825  case AUTH_REQ_GSS:
826 #if !defined(ENABLE_SSPI)
827  /* no native SSPI, so use GSSAPI library for it */
828  case AUTH_REQ_SSPI:
829 #endif
830  {
831  int r;
832 
833  pglock_thread();
834 
835  /*
836  * If we have both GSS and SSPI support compiled in, use SSPI
837  * support by default. This is overridable by a connection
838  * string parameter. Note that when using SSPI we still leave
839  * the negotiate parameter off, since we want SSPI to use the
840  * GSSAPI kerberos protocol. For actual SSPI negotiate
841  * protocol, we use AUTH_REQ_SSPI.
842  */
843 #if defined(ENABLE_GSS) && defined(ENABLE_SSPI)
844  if (conn->gsslib && (pg_strcasecmp(conn->gsslib, "gssapi") == 0))
845  r = pg_GSS_startup(conn, payloadlen);
846  else
847  r = pg_SSPI_startup(conn, 0, payloadlen);
848 #elif defined(ENABLE_GSS) && !defined(ENABLE_SSPI)
849  r = pg_GSS_startup(conn, payloadlen);
850 #elif !defined(ENABLE_GSS) && defined(ENABLE_SSPI)
851  r = pg_SSPI_startup(conn, 0, payloadlen);
852 #endif
853  if (r != STATUS_OK)
854  {
855  /* Error message already filled in. */
856  pgunlock_thread();
857  return STATUS_ERROR;
858  }
859  pgunlock_thread();
860  }
861  break;
862 
863  case AUTH_REQ_GSS_CONT:
864  {
865  int r;
866 
867  pglock_thread();
868 #if defined(ENABLE_GSS) && defined(ENABLE_SSPI)
869  if (conn->usesspi)
870  r = pg_SSPI_continue(conn, payloadlen);
871  else
872  r = pg_GSS_continue(conn, payloadlen);
873 #elif defined(ENABLE_GSS) && !defined(ENABLE_SSPI)
874  r = pg_GSS_continue(conn, payloadlen);
875 #elif !defined(ENABLE_GSS) && defined(ENABLE_SSPI)
876  r = pg_SSPI_continue(conn, payloadlen);
877 #endif
878  if (r != STATUS_OK)
879  {
880  /* Error message already filled in. */
881  pgunlock_thread();
882  return STATUS_ERROR;
883  }
884  pgunlock_thread();
885  }
886  break;
887 #else /* defined(ENABLE_GSS) || defined(ENABLE_SSPI) */
888  /* No GSSAPI *or* SSPI support */
889  case AUTH_REQ_GSS:
890  case AUTH_REQ_GSS_CONT:
892  libpq_gettext("GSSAPI authentication not supported\n"));
893  return STATUS_ERROR;
894 #endif /* defined(ENABLE_GSS) || defined(ENABLE_SSPI) */
895 
896 #ifdef ENABLE_SSPI
897  case AUTH_REQ_SSPI:
898 
899  /*
900  * SSPI has it's own startup message so libpq can decide which
901  * method to use. Indicate to pg_SSPI_startup that we want SSPI
902  * negotiation instead of Kerberos.
903  */
904  pglock_thread();
905  if (pg_SSPI_startup(conn, 1, payloadlen) != STATUS_OK)
906  {
907  /* Error message already filled in. */
908  pgunlock_thread();
909  return STATUS_ERROR;
910  }
911  pgunlock_thread();
912  break;
913 #else
914 
915  /*
916  * No SSPI support. However, if we have GSSAPI but not SSPI
917  * support, AUTH_REQ_SSPI will have been handled in the codepath
918  * for AUTH_REQ_GSSAPI above, so don't duplicate the case label in
919  * that case.
920  */
921 #if !defined(ENABLE_GSS)
922  case AUTH_REQ_SSPI:
924  libpq_gettext("SSPI authentication not supported\n"));
925  return STATUS_ERROR;
926 #endif /* !define(ENABLE_GSSAPI) */
927 #endif /* ENABLE_SSPI */
928 
929 
930  case AUTH_REQ_CRYPT:
932  libpq_gettext("Crypt authentication not supported\n"));
933  return STATUS_ERROR;
934 
935  case AUTH_REQ_MD5:
936  case AUTH_REQ_PASSWORD:
937  {
938  char *password;
939 
940  conn->password_needed = true;
941  password = conn->connhost[conn->whichhost].password;
942  if (password == NULL)
943  password = conn->pgpass;
944  if (password == NULL || password[0] == '\0')
945  {
948  return STATUS_ERROR;
949  }
950  if (pg_password_sendauth(conn, password, areq) != STATUS_OK)
951  {
953  "fe_sendauth: error sending password authentication\n");
954  return STATUS_ERROR;
955  }
956  break;
957  }
958 
959  case AUTH_REQ_SASL:
960 
961  /*
962  * The request contains the name (as assigned by IANA) of the
963  * authentication mechanism.
964  */
965  if (pg_SASL_init(conn, payloadlen) != STATUS_OK)
966  {
967  /* pg_SASL_init already set the error message */
968  return STATUS_ERROR;
969  }
970  break;
971 
972  case AUTH_REQ_SASL_CONT:
973  case AUTH_REQ_SASL_FIN:
974  if (conn->sasl_state == NULL)
975  {
977  "fe_sendauth: invalid authentication request from server: AUTH_REQ_SASL_CONT without AUTH_REQ_SASL\n");
978  return STATUS_ERROR;
979  }
980  if (pg_SASL_continue(conn, payloadlen,
981  (areq == AUTH_REQ_SASL_FIN)) != STATUS_OK)
982  {
983  /* Use error message, if set already */
984  if (conn->errorMessage.len == 0)
986  "fe_sendauth: error in SASL authentication\n");
987  return STATUS_ERROR;
988  }
989  break;
990 
991  case AUTH_REQ_SCM_CREDS:
992  if (pg_local_sendauth(conn) != STATUS_OK)
993  return STATUS_ERROR;
994  break;
995 
996  default:
998  libpq_gettext("authentication method %u not supported\n"), areq);
999  return STATUS_ERROR;
1000  }
1001 
1002  return STATUS_OK;
1003 }
static char password[100]
Definition: streamutil.c:41
#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:679
#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:476
static int pg_password_sendauth(PGconn *conn, const char *password, AuthRequest areq)
Definition: fe-auth.c:731
#define STATUS_ERROR
Definition: c.h:976
#define pglock_thread()
Definition: libpq-int.h:562
#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:609
pg_conn_host * connhost
Definition: libpq-int.h:397
void * sasl_state
Definition: libpq-int.h:455
#define AUTH_REQ_CRYPT
Definition: pqcomm.h:169
#define STATUS_OK
Definition: c.h:975
#define AUTH_REQ_PASSWORD
Definition: pqcomm.h:168
PQExpBufferData errorMessage
Definition: libpq-int.h:492
#define AUTH_REQ_KRB5
Definition: pqcomm.h:167
#define AUTH_REQ_SASL_CONT
Definition: pqcomm.h:176
#define NULL
Definition: c.h:229
#define AUTH_REQ_KRB4
Definition: pqcomm.h:166
char * pgpass
Definition: libpq-int.h:343
#define AUTH_REQ_SASL
Definition: pqcomm.h:175
#define AUTH_REQ_GSS_CONT
Definition: pqcomm.h:173
#define pgunlock_thread()
Definition: libpq-int.h:563
int whichhost
Definition: libpq-int.h:396
#define libpq_gettext(x)
Definition: libpq-int.h:683
#define AUTH_REQ_SCM_CREDS
Definition: pqcomm.h:171
char * password
Definition: libpq-int.h:311
static int pg_local_sendauth ( PGconn conn)
static

Definition at line 679 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().

680 {
681 #ifdef HAVE_STRUCT_CMSGCRED
682  char buf;
683  struct iovec iov;
684  struct msghdr msg;
685  struct cmsghdr *cmsg;
686  union
687  {
688  struct cmsghdr hdr;
689  unsigned char buf[CMSG_SPACE(sizeof(struct cmsgcred))];
690  } cmsgbuf;
691 
692  /*
693  * The backend doesn't care what we send here, but it wants exactly one
694  * character to force recvmsg() to block and wait for us.
695  */
696  buf = '\0';
697  iov.iov_base = &buf;
698  iov.iov_len = 1;
699 
700  memset(&msg, 0, sizeof(msg));
701  msg.msg_iov = &iov;
702  msg.msg_iovlen = 1;
703 
704  /* We must set up a message that will be filled in by kernel */
705  memset(&cmsgbuf, 0, sizeof(cmsgbuf));
706  msg.msg_control = &cmsgbuf.buf;
707  msg.msg_controllen = sizeof(cmsgbuf.buf);
708  cmsg = CMSG_FIRSTHDR(&msg);
709  cmsg->cmsg_len = CMSG_LEN(sizeof(struct cmsgcred));
710  cmsg->cmsg_level = SOL_SOCKET;
711  cmsg->cmsg_type = SCM_CREDS;
712 
713  if (sendmsg(conn->sock, &msg, 0) == -1)
714  {
715  char sebuf[256];
716 
718  "pg_local_sendauth: sendmsg: %s\n",
719  pqStrerror(errno, sebuf, sizeof(sebuf)));
720  return STATUS_ERROR;
721  }
722  return STATUS_OK;
723 #else
725  libpq_gettext("SCM_CRED authentication method not supported\n"));
726  return STATUS_ERROR;
727 #endif
728 }
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:976
static char * buf
Definition: pg_test_fsync.c:66
#define STATUS_OK
Definition: c.h:975
pgsocket sock
Definition: libpq-int.h:400
PQExpBufferData errorMessage
Definition: libpq-int.h:492
#define libpq_gettext(x)
Definition: libpq-int.h:683
static int pg_password_sendauth ( PGconn conn,
const char *  password,
AuthRequest  areq 
)
static

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

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

Referenced by pg_fe_sendauth().

732 {
733  int ret;
734  char *crypt_pwd = NULL;
735  const char *pwd_to_send;
736  char md5Salt[4];
737 
738  /* Read the salt from the AuthenticationMD5 message. */
739  if (areq == AUTH_REQ_MD5)
740  {
741  if (pqGetnchar(md5Salt, 4, conn))
742  return STATUS_ERROR; /* shouldn't happen */
743  }
744 
745  /* Encrypt the password if needed. */
746 
747  switch (areq)
748  {
749  case AUTH_REQ_MD5:
750  {
751  char *crypt_pwd2;
752 
753  /* Allocate enough space for two MD5 hashes */
754  crypt_pwd = malloc(2 * (MD5_PASSWD_LEN + 1));
755  if (!crypt_pwd)
756  {
758  libpq_gettext("out of memory\n"));
759  return STATUS_ERROR;
760  }
761 
762  crypt_pwd2 = crypt_pwd + MD5_PASSWD_LEN + 1;
763  if (!pg_md5_encrypt(password, conn->pguser,
764  strlen(conn->pguser), crypt_pwd2))
765  {
766  free(crypt_pwd);
767  return STATUS_ERROR;
768  }
769  if (!pg_md5_encrypt(crypt_pwd2 + strlen("md5"), md5Salt,
770  4, crypt_pwd))
771  {
772  free(crypt_pwd);
773  return STATUS_ERROR;
774  }
775 
776  pwd_to_send = crypt_pwd;
777  break;
778  }
779  case AUTH_REQ_PASSWORD:
780  pwd_to_send = password;
781  break;
782  default:
783  return STATUS_ERROR;
784  }
785  /* Packet has a message type as of protocol 3.0 */
786  if (PG_PROTOCOL_MAJOR(conn->pversion) >= 3)
787  ret = pqPacketSend(conn, 'p', pwd_to_send, strlen(pwd_to_send) + 1);
788  else
789  ret = pqPacketSend(conn, 0, pwd_to_send, strlen(pwd_to_send) + 1);
790  if (crypt_pwd)
791  free(crypt_pwd);
792  return ret;
793 }
static char password[100]
Definition: streamutil.c:41
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
#define STATUS_ERROR
Definition: c.h:976
int pqGetnchar(char *s, size_t len, PGconn *conn)
Definition: fe-misc.c:197
#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:342
#define AUTH_REQ_PASSWORD
Definition: pqcomm.h:168
PQExpBufferData errorMessage
Definition: libpq-int.h:492
#define MD5_PASSWD_LEN
Definition: md5.h:19
#define free(a)
Definition: header.h:65
#define NULL
Definition: c.h:229
ProtocolVersion pversion
Definition: libpq-int.h:404
int pqPacketSend(PGconn *conn, char pack_type, const void *buf, size_t buf_len)
Definition: fe-connect.c:3866
#define libpq_gettext(x)
Definition: libpq-int.h:683
static int pg_SASL_continue ( PGconn conn,
int  payloadlen,
bool  final 
)
static

Definition at line 609 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().

610 {
611  char *output;
612  int outputlen;
613  bool done;
614  bool success;
615  int res;
616  char *challenge;
617 
618  /* Read the SASL challenge from the AuthenticationSASLContinue message. */
619  challenge = malloc(payloadlen + 1);
620  if (!challenge)
621  {
623  libpq_gettext("out of memory allocating SASL buffer (%d)\n"),
624  payloadlen);
625  return STATUS_ERROR;
626  }
627 
628  if (pqGetnchar(challenge, payloadlen, conn))
629  {
630  free(challenge);
631  return STATUS_ERROR;
632  }
633  /* For safety and convenience, ensure the buffer is NULL-terminated. */
634  challenge[payloadlen] = '\0';
635 
637  challenge, payloadlen,
638  &output, &outputlen,
639  &done, &success, &conn->errorMessage);
640  free(challenge); /* don't need the input anymore */
641 
642  if (final && !done)
643  {
644  if (outputlen != 0)
645  free(output);
646 
648  libpq_gettext("AuthenticationSASLFinal received from server, but SASL authentication was not completed\n"));
649  return STATUS_ERROR;
650  }
651  if (outputlen != 0)
652  {
653  /*
654  * Send the SASL response to the server.
655  */
656  res = pqPacketSend(conn, 'p', output, outputlen);
657  free(output);
658 
659  if (res != STATUS_OK)
660  return STATUS_ERROR;
661  }
662 
663  if (done && !success)
664  return STATUS_ERROR;
665 
666  return STATUS_OK;
667 }
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
static void output(uint64 loop_count)
#define STATUS_ERROR
Definition: c.h:976
int pqGetnchar(char *s, size_t len, PGconn *conn)
Definition: fe-misc.c:197
#define malloc(a)
Definition: header.h:50
void pg_fe_scram_exchange(void *opaq, char *input, int inputlen, char **output, int *outputlen, bool *done, bool *success, PQExpBuffer errorMessage)
static bool success
Definition: pg_basebackup.c:96
void * sasl_state
Definition: libpq-int.h:455
#define STATUS_OK
Definition: c.h:975
PQExpBufferData errorMessage
Definition: libpq-int.h:492
#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:3866
#define libpq_gettext(x)
Definition: libpq-int.h:683
static int pg_SASL_init ( PGconn conn,
int  payloadlen 
)
static

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

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

Referenced by pg_fe_sendauth().

477 {
478  char *initialresponse = NULL;
479  int initialresponselen;
480  bool done;
481  bool success;
482  const char *selected_mechanism;
483  PQExpBufferData mechanism_buf;
484 
485  initPQExpBuffer(&mechanism_buf);
486 
487  if (conn->sasl_state)
488  {
490  libpq_gettext("duplicate SASL authentication request\n"));
491  goto error;
492  }
493 
494  /*
495  * Parse the list of SASL authentication mechanisms in the
496  * AuthenticationSASL message, and select the best mechanism that we
497  * support. (Only SCRAM-SHA-256 is supported at the moment.)
498  */
499  selected_mechanism = NULL;
500  for (;;)
501  {
502  if (pqGets(&mechanism_buf, conn))
503  {
505  "fe_sendauth: invalid authentication request from server: invalid list of authentication mechanisms\n");
506  goto error;
507  }
508  if (PQExpBufferDataBroken(mechanism_buf))
509  goto oom_error;
510 
511  /* An empty string indicates end of list */
512  if (mechanism_buf.data[0] == '\0')
513  break;
514 
515  /*
516  * If we have already selected a mechanism, just skip through the rest
517  * of the list.
518  */
519  if (selected_mechanism)
520  continue;
521 
522  /*
523  * Do we support this mechanism?
524  */
525  if (strcmp(mechanism_buf.data, SCRAM_SHA256_NAME) == 0)
526  {
527  char *password;
528 
529  conn->password_needed = true;
530  password = conn->connhost[conn->whichhost].password;
531  if (password == NULL)
532  password = conn->pgpass;
533  if (password == NULL || password[0] == '\0')
534  {
537  goto error;
538  }
539 
540  conn->sasl_state = pg_fe_scram_init(conn->pguser, password);
541  if (!conn->sasl_state)
542  goto oom_error;
543  selected_mechanism = SCRAM_SHA256_NAME;
544  }
545  }
546 
547  if (!selected_mechanism)
548  {
550  libpq_gettext("none of the server's SASL authentication mechanisms are supported\n"));
551  goto error;
552  }
553 
554  /* Get the mechanism-specific Initial Client Response, if any */
556  NULL, -1,
557  &initialresponse, &initialresponselen,
558  &done, &success, &conn->errorMessage);
559 
560  if (done && !success)
561  goto error;
562 
563  /*
564  * Build a SASLInitialResponse message, and send it.
565  */
566  if (pqPutMsgStart('p', true, conn))
567  goto error;
568  if (pqPuts(selected_mechanism, conn))
569  goto error;
570  if (initialresponse)
571  {
572  if (pqPutInt(initialresponselen, 4, conn))
573  goto error;
574  if (pqPutnchar(initialresponse, initialresponselen, conn))
575  goto error;
576  }
577  if (pqPutMsgEnd(conn))
578  goto error;
579  if (pqFlush(conn))
580  goto error;
581 
582  termPQExpBuffer(&mechanism_buf);
583  if (initialresponse)
584  free(initialresponse);
585 
586  return STATUS_OK;
587 
588 error:
589  termPQExpBuffer(&mechanism_buf);
590  if (initialresponse)
591  free(initialresponse);
592  return STATUS_ERROR;
593 
594 oom_error:
595  termPQExpBuffer(&mechanism_buf);
596  if (initialresponse)
597  free(initialresponse);
599  libpq_gettext("out of memory\n"));
600  return STATUS_ERROR;
601 }
int pqFlush(PGconn *conn)
Definition: fe-misc.c:963
static char password[100]
Definition: streamutil.c:41
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
static void error(void)
Definition: sql-dyntest.c:147
int pqGets(PQExpBuffer buf, PGconn *conn)
Definition: fe-misc.c:165
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:521
#define STATUS_ERROR
Definition: c.h:976
#define PQnoPasswordSupplied
Definition: libpq-fe.h:512
int pqPutInt(int value, size_t bytes, PGconn *conn)
Definition: fe-misc.c:309
void pg_fe_scram_exchange(void *opaq, char *input, int inputlen, char **output, int *outputlen, bool *done, bool *success, PQExpBuffer errorMessage)
pg_conn_host * connhost
Definition: libpq-int.h:397
static bool success
Definition: pg_basebackup.c:96
void * sasl_state
Definition: libpq-int.h:455
#define SCRAM_SHA256_NAME
Definition: scram.h:17
int pqPuts(const char *s, PGconn *conn)
Definition: fe-misc.c:181
#define STATUS_OK
Definition: c.h:975
char * pguser
Definition: libpq-int.h:342
void * pg_fe_scram_init(const char *username, const char *password)
Definition: fe-auth-scram.c:84
PQExpBufferData errorMessage
Definition: libpq-int.h:492
#define free(a)
Definition: header.h:65
#define PQExpBufferDataBroken(buf)
Definition: pqexpbuffer.h:67
#define NULL
Definition: c.h:229
int pqPutnchar(const char *s, size_t len, PGconn *conn)
Definition: fe-misc.c:248
char * pgpass
Definition: libpq-int.h:343
int pqPutMsgEnd(PGconn *conn)
Definition: fe-misc.c:589
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
int whichhost
Definition: libpq-int.h:396
#define libpq_gettext(x)
Definition: libpq-int.h:683
char * password
Definition: libpq-int.h:311
char* PQencryptPassword ( const char *  passwd,
const char *  user 
)

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

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

1089 {
1090  char *crypt_pwd;
1091 
1092  crypt_pwd = malloc(MD5_PASSWD_LEN + 1);
1093  if (!crypt_pwd)
1094  return NULL;
1095 
1096  if (!pg_md5_encrypt(passwd, user, strlen(user), crypt_pwd))
1097  {
1098  free(crypt_pwd);
1099  return NULL;
1100  }
1101 
1102  return crypt_pwd;
1103 }
#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
#define NULL
Definition: c.h:229
static char * user
Definition: pg_regress.c:92
char* PQencryptPasswordConn ( PGconn conn,
const char *  passwd,
const char *  user,
const char *  algorithm 
)

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

References pg_conn::errorMessage, free, libpq_gettext, malloc, MAX_ALGORITHM_NAME_LEN, MD5_PASSWD_LEN, NULL, 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().

1132 {
1133 #define MAX_ALGORITHM_NAME_LEN 50
1134  char algobuf[MAX_ALGORITHM_NAME_LEN + 1];
1135  char *crypt_pwd = NULL;
1136 
1137  if (!conn)
1138  return NULL;
1139 
1140  /* If no algorithm was given, ask the server. */
1141  if (algorithm == NULL)
1142  {
1143  PGresult *res;
1144  char *val;
1145 
1146  res = PQexec(conn, "show password_encryption");
1147  if (res == NULL)
1148  {
1149  /* PQexec() should've set conn->errorMessage already */
1150  return NULL;
1151  }
1152  if (PQresultStatus(res) != PGRES_TUPLES_OK)
1153  {
1154  /* PQexec() should've set conn->errorMessage already */
1155  PQclear(res);
1156  return NULL;
1157  }
1158  if (PQntuples(res) != 1 || PQnfields(res) != 1)
1159  {
1160  PQclear(res);
1162  libpq_gettext("unexpected shape of result set returned for SHOW\n"));
1163  return NULL;
1164  }
1165  val = PQgetvalue(res, 0, 0);
1166 
1167  if (strlen(val) > MAX_ALGORITHM_NAME_LEN)
1168  {
1169  PQclear(res);
1171  libpq_gettext("password_encryption value too long\n"));
1172  return NULL;
1173  }
1174  strcpy(algobuf, val);
1175  PQclear(res);
1176 
1177  algorithm = algobuf;
1178  }
1179 
1180  /*
1181  * Also accept "on" and "off" as aliases for "md5", because
1182  * password_encryption was a boolean before PostgreSQL 10. We refuse to
1183  * send the password in plaintext even if it was "off".
1184  */
1185  if (strcmp(algorithm, "on") == 0 ||
1186  strcmp(algorithm, "off") == 0)
1187  algorithm = "md5";
1188 
1189  /*
1190  * Ok, now we know what algorithm to use
1191  */
1192  if (strcmp(algorithm, "scram-sha-256") == 0)
1193  {
1194  crypt_pwd = pg_fe_scram_build_verifier(passwd);
1195  }
1196  else if (strcmp(algorithm, "md5") == 0)
1197  {
1198  crypt_pwd = malloc(MD5_PASSWD_LEN + 1);
1199  if (crypt_pwd)
1200  {
1201  if (!pg_md5_encrypt(passwd, user, strlen(user), crypt_pwd))
1202  {
1203  free(crypt_pwd);
1204  crypt_pwd = NULL;
1205  }
1206  }
1207  }
1208  else
1209  {
1211  libpq_gettext("unknown password encryption algorithm\n"));
1212  return NULL;
1213  }
1214 
1215  if (!crypt_pwd)
1217  libpq_gettext("out of memory\n"));
1218 
1219  return crypt_pwd;
1220 }
int PQnfields(const PGresult *res)
Definition: fe-exec.c:2681
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:3067
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2673
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2596
#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:492
void PQclear(PGresult *res)
Definition: fe-exec.c:650
#define MD5_PASSWD_LEN
Definition: md5.h:19
#define free(a)
Definition: header.h:65
#define NULL
Definition: c.h:229
char * pg_fe_scram_build_verifier(const char *password)
static char * user
Definition: pg_regress.c:92
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1846
long val
Definition: informix.c:689
#define libpq_gettext(x)
Definition: libpq-int.h:683