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 1024 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().

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

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

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

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

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

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

742 {
743  int ret;
744  char *crypt_pwd = NULL;
745  const char *pwd_to_send;
746  char md5Salt[4];
747 
748  /* Read the salt from the AuthenticationMD5 message. */
749  if (areq == AUTH_REQ_MD5)
750  {
751  if (pqGetnchar(md5Salt, 4, conn))
752  return STATUS_ERROR; /* shouldn't happen */
753  }
754 
755  /* Encrypt the password if needed. */
756 
757  switch (areq)
758  {
759  case AUTH_REQ_MD5:
760  {
761  char *crypt_pwd2;
762 
763  /* Allocate enough space for two MD5 hashes */
764  crypt_pwd = malloc(2 * (MD5_PASSWD_LEN + 1));
765  if (!crypt_pwd)
766  {
768  libpq_gettext("out of memory\n"));
769  return STATUS_ERROR;
770  }
771 
772  crypt_pwd2 = crypt_pwd + MD5_PASSWD_LEN + 1;
773  if (!pg_md5_encrypt(password, conn->pguser,
774  strlen(conn->pguser), crypt_pwd2))
775  {
776  free(crypt_pwd);
777  return STATUS_ERROR;
778  }
779  if (!pg_md5_encrypt(crypt_pwd2 + strlen("md5"), md5Salt,
780  4, crypt_pwd))
781  {
782  free(crypt_pwd);
783  return STATUS_ERROR;
784  }
785 
786  pwd_to_send = crypt_pwd;
787  break;
788  }
789  case AUTH_REQ_PASSWORD:
790  pwd_to_send = password;
791  break;
792  default:
793  return STATUS_ERROR;
794  }
795  /* Packet has a message type as of protocol 3.0 */
796  if (PG_PROTOCOL_MAJOR(conn->pversion) >= 3)
797  ret = pqPacketSend(conn, 'p', pwd_to_send, strlen(pwd_to_send) + 1);
798  else
799  ret = pqPacketSend(conn, 0, pwd_to_send, strlen(pwd_to_send) + 1);
800  if (crypt_pwd)
801  free(crypt_pwd);
802  return ret;
803 }
static char password[100]
Definition: streamutil.c:42
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:343
#define AUTH_REQ_PASSWORD
Definition: pqcomm.h:168
PQExpBufferData errorMessage
Definition: libpq-int.h:491
#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:3975
#define libpq_gettext(x)
Definition: libpq-int.h:682
static int pg_SASL_continue ( PGconn conn,
int  payloadlen,
bool  final 
)
static

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

620 {
621  char *output;
622  int outputlen;
623  bool done;
624  bool success;
625  int res;
626  char *challenge;
627 
628  /* Read the SASL challenge from the AuthenticationSASLContinue message. */
629  challenge = malloc(payloadlen + 1);
630  if (!challenge)
631  {
633  libpq_gettext("out of memory allocating SASL buffer (%d)\n"),
634  payloadlen);
635  return STATUS_ERROR;
636  }
637 
638  if (pqGetnchar(challenge, payloadlen, conn))
639  {
640  free(challenge);
641  return STATUS_ERROR;
642  }
643  /* For safety and convenience, ensure the buffer is NULL-terminated. */
644  challenge[payloadlen] = '\0';
645 
647  challenge, payloadlen,
648  &output, &outputlen,
649  &done, &success, &conn->errorMessage);
650  free(challenge); /* don't need the input anymore */
651 
652  if (final && !done)
653  {
654  if (outputlen != 0)
655  free(output);
656 
658  libpq_gettext("AuthenticationSASLFinal received from server, but SASL authentication was not completed\n"));
659  return STATUS_ERROR;
660  }
661  if (outputlen != 0)
662  {
663  /*
664  * Send the SASL response to the server.
665  */
666  res = pqPacketSend(conn, 'p', output, outputlen);
667  free(output);
668 
669  if (res != STATUS_OK)
670  return STATUS_ERROR;
671  }
672 
673  if (done && !success)
674  return STATUS_ERROR;
675 
676  return STATUS_OK;
677 }
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:454
#define STATUS_OK
Definition: c.h:975
PQExpBufferData errorMessage
Definition: libpq-int.h:491
#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:3975
#define libpq_gettext(x)
Definition: libpq-int.h:682
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, 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().

487 {
488  char *initialresponse = NULL;
489  int initialresponselen;
490  bool done;
491  bool success;
492  const char *selected_mechanism;
493  PQExpBufferData mechanism_buf;
494 
495  initPQExpBuffer(&mechanism_buf);
496 
497  if (conn->sasl_state)
498  {
500  libpq_gettext("duplicate SASL authentication request\n"));
501  goto error;
502  }
503 
504  /*
505  * Parse the list of SASL authentication mechanisms in the
506  * AuthenticationSASL message, and select the best mechanism that we
507  * support. (Only SCRAM-SHA-256 is supported at the moment.)
508  */
509  selected_mechanism = NULL;
510  for (;;)
511  {
512  if (pqGets(&mechanism_buf, conn))
513  {
515  "fe_sendauth: invalid authentication request from server: invalid list of authentication mechanisms\n");
516  goto error;
517  }
518  if (PQExpBufferDataBroken(mechanism_buf))
519  goto oom_error;
520 
521  /* An empty string indicates end of list */
522  if (mechanism_buf.data[0] == '\0')
523  break;
524 
525  /*
526  * If we have already selected a mechanism, just skip through the rest
527  * of the list.
528  */
529  if (selected_mechanism)
530  continue;
531 
532  /*
533  * Do we support this mechanism?
534  */
535  if (strcmp(mechanism_buf.data, SCRAM_SHA256_NAME) == 0)
536  {
537  char *password;
538 
539  conn->password_needed = true;
540  password = conn->connhost[conn->whichhost].password;
541  if (password == NULL)
542  password = conn->pgpass;
543  if (password == NULL || password[0] == '\0')
544  {
547  goto error;
548  }
549 
550  conn->sasl_state = pg_fe_scram_init(conn->pguser, password);
551  if (!conn->sasl_state)
552  goto oom_error;
553  selected_mechanism = SCRAM_SHA256_NAME;
554  }
555  }
556 
557  if (!selected_mechanism)
558  {
560  libpq_gettext("none of the server's SASL authentication mechanisms are supported\n"));
561  goto error;
562  }
563 
564  /* Get the mechanism-specific Initial Client Response, if any */
566  NULL, -1,
567  &initialresponse, &initialresponselen,
568  &done, &success, &conn->errorMessage);
569 
570  if (done && !success)
571  goto error;
572 
573  /*
574  * Build a SASLInitialResponse message, and send it.
575  */
576  if (pqPutMsgStart('p', true, conn))
577  goto error;
578  if (pqPuts(selected_mechanism, conn))
579  goto error;
580  if (initialresponse)
581  {
582  if (pqPutInt(initialresponselen, 4, conn))
583  goto error;
584  if (pqPutnchar(initialresponse, initialresponselen, conn))
585  goto error;
586  }
587  if (pqPutMsgEnd(conn))
588  goto error;
589  if (pqFlush(conn))
590  goto error;
591 
592  termPQExpBuffer(&mechanism_buf);
593  if (initialresponse)
594  free(initialresponse);
595 
596  return STATUS_OK;
597 
598 error:
599  termPQExpBuffer(&mechanism_buf);
600  if (initialresponse)
601  free(initialresponse);
602  return STATUS_ERROR;
603 
604 oom_error:
605  termPQExpBuffer(&mechanism_buf);
606  if (initialresponse)
607  free(initialresponse);
609  libpq_gettext("out of memory\n"));
610  return STATUS_ERROR;
611 }
int pqFlush(PGconn *conn)
Definition: fe-misc.c:963
static char password[100]
Definition: streamutil.c:42
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:407
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:454
#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:343
void * pg_fe_scram_init(const char *username, const char *password)
Definition: fe-auth-scram.c:84
PQExpBufferData errorMessage
Definition: libpq-int.h:491
#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:344
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:682
char * password
Definition: libpq-int.h:312
char* PQencryptPassword ( const char *  passwd,
const char *  user 
)

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

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

1099 {
1100  char *crypt_pwd;
1101 
1102  crypt_pwd = malloc(MD5_PASSWD_LEN + 1);
1103  if (!crypt_pwd)
1104  return NULL;
1105 
1106  if (!pg_md5_encrypt(passwd, user, strlen(user), crypt_pwd))
1107  {
1108  free(crypt_pwd);
1109  return NULL;
1110  }
1111 
1112  return crypt_pwd;
1113 }
#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 1140 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().

1142 {
1143 #define MAX_ALGORITHM_NAME_LEN 50
1144  char algobuf[MAX_ALGORITHM_NAME_LEN + 1];
1145  char *crypt_pwd = NULL;
1146 
1147  if (!conn)
1148  return NULL;
1149 
1150  /* If no algorithm was given, ask the server. */
1151  if (algorithm == NULL)
1152  {
1153  PGresult *res;
1154  char *val;
1155 
1156  res = PQexec(conn, "show password_encryption");
1157  if (res == NULL)
1158  {
1159  /* PQexec() should've set conn->errorMessage already */
1160  return NULL;
1161  }
1162  if (PQresultStatus(res) != PGRES_TUPLES_OK)
1163  {
1164  /* PQexec() should've set conn->errorMessage already */
1165  PQclear(res);
1166  return NULL;
1167  }
1168  if (PQntuples(res) != 1 || PQnfields(res) != 1)
1169  {
1170  PQclear(res);
1172  libpq_gettext("unexpected shape of result set returned for SHOW\n"));
1173  return NULL;
1174  }
1175  val = PQgetvalue(res, 0, 0);
1176 
1177  if (strlen(val) > MAX_ALGORITHM_NAME_LEN)
1178  {
1179  PQclear(res);
1181  libpq_gettext("password_encryption value too long\n"));
1182  return NULL;
1183  }
1184  strcpy(algobuf, val);
1185  PQclear(res);
1186 
1187  algorithm = algobuf;
1188  }
1189 
1190  /*
1191  * Also accept "on" and "off" as aliases for "md5", because
1192  * password_encryption was a boolean before PostgreSQL 10. We refuse to
1193  * send the password in plaintext even if it was "off".
1194  */
1195  if (strcmp(algorithm, "on") == 0 ||
1196  strcmp(algorithm, "off") == 0)
1197  algorithm = "md5";
1198 
1199  /*
1200  * Ok, now we know what algorithm to use
1201  */
1202  if (strcmp(algorithm, "scram-sha-256") == 0)
1203  {
1204  crypt_pwd = pg_fe_scram_build_verifier(passwd);
1205  }
1206  else if (strcmp(algorithm, "md5") == 0)
1207  {
1208  crypt_pwd = malloc(MD5_PASSWD_LEN + 1);
1209  if (crypt_pwd)
1210  {
1211  if (!pg_md5_encrypt(passwd, user, strlen(user), crypt_pwd))
1212  {
1213  free(crypt_pwd);
1214  crypt_pwd = NULL;
1215  }
1216  }
1217  }
1218  else
1219  {
1221  libpq_gettext("unrecognized password encryption algorithm \"%s\"\n"),
1222  algorithm);
1223  return NULL;
1224  }
1225 
1226  if (!crypt_pwd)
1228  libpq_gettext("out of memory\n"));
1229 
1230  return crypt_pwd;
1231 }
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:491
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:682