PostgreSQL Source Code  git master
fe-auth.h File Reference
#include "libpq-fe.h"
#include "libpq-int.h"
Include dependency graph for fe-auth.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

int pg_fe_sendauth (AuthRequest areq, int payloadlen, PGconn *conn)
 
char * pg_fe_getusername (uid_t user_id, PQExpBuffer errorMessage)
 
char * pg_fe_getauthname (PQExpBuffer errorMessage)
 
char * pg_fe_scram_build_secret (const char *password, int iterations, const char **errstr)
 

Variables

const pg_fe_sasl_mech pg_scram_mech
 

Function Documentation

◆ pg_fe_getauthname()

char* pg_fe_getauthname ( PQExpBuffer  errorMessage)

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

1215 {
1216 #ifdef WIN32
1217  return pg_fe_getusername(0, errorMessage);
1218 #else
1219  return pg_fe_getusername(geteuid(), errorMessage);
1220 #endif
1221 }
char * pg_fe_getusername(uid_t user_id, PQExpBuffer errorMessage)
Definition: fe-auth.c:1169

References pg_fe_getusername().

Referenced by conninfo_add_defaults(), and pqConnectOptions2().

◆ pg_fe_getusername()

char* pg_fe_getusername ( uid_t  user_id,
PQExpBuffer  errorMessage 
)

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

1170 {
1171  char *result = NULL;
1172  const char *name = NULL;
1173 
1174 #ifdef WIN32
1175  /* Microsoft recommends buffer size of UNLEN+1, where UNLEN = 256 */
1176  char username[256 + 1];
1177  DWORD namesize = sizeof(username);
1178 #else
1179  char pwdbuf[BUFSIZ];
1180 #endif
1181 
1182 #ifdef WIN32
1183  if (GetUserName(username, &namesize))
1184  name = username;
1185  else if (errorMessage)
1186  libpq_append_error(errorMessage,
1187  "user name lookup failure: error code %lu",
1188  GetLastError());
1189 #else
1190  if (pg_get_user_name(user_id, pwdbuf, sizeof(pwdbuf)))
1191  name = pwdbuf;
1192  else if (errorMessage)
1193  appendPQExpBuffer(errorMessage, "%s\n", pwdbuf);
1194 #endif
1195 
1196  if (name)
1197  {
1198  result = strdup(name);
1199  if (result == NULL && errorMessage)
1200  libpq_append_error(errorMessage, "out of memory");
1201  }
1202 
1203  return result;
1204 }
void libpq_append_error(PQExpBuffer errorMessage, const char *fmt,...)
Definition: fe-misc.c:1328
static char * username
Definition: initdb.c:153
bool pg_get_user_name(uid_t user_id, char *buffer, size_t buflen)
Definition: user.c:28
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:265
const char * name

References appendPQExpBuffer(), libpq_append_error(), name, pg_get_user_name(), and username.

Referenced by pg_fe_getauthname(), and PQconnectPoll().

◆ pg_fe_scram_build_secret()

char* pg_fe_scram_build_secret ( const char *  password,
int  iterations,
const char **  errstr 
)

Definition at line 892 of file fe-auth-scram.c.

893 {
894  char *prep_password;
895  pg_saslprep_rc rc;
896  char saltbuf[SCRAM_DEFAULT_SALT_LEN];
897  char *result;
898 
899  /*
900  * Normalize the password with SASLprep. If that doesn't work, because
901  * the password isn't valid UTF-8 or contains prohibited characters, just
902  * proceed with the original password. (See comments at the top of
903  * auth-scram.c.)
904  */
905  rc = pg_saslprep(password, &prep_password);
906  if (rc == SASLPREP_OOM)
907  {
908  *errstr = libpq_gettext("out of memory");
909  return NULL;
910  }
911  if (rc == SASLPREP_SUCCESS)
912  password = (const char *) prep_password;
913 
914  /* Generate a random salt */
916  {
917  *errstr = libpq_gettext("could not generate random salt");
918  free(prep_password);
919  return NULL;
920  }
921 
925  errstr);
926 
927  free(prep_password);
928 
929  return result;
930 }
@ PG_SHA256
Definition: cryptohash.h:24
#define free(a)
Definition: header.h:65
#define libpq_gettext(x)
Definition: libpq-int.h:911
bool pg_strong_random(void *buf, size_t len)
pg_saslprep_rc pg_saslprep(const char *input, char **output)
Definition: saslprep.c:1044
pg_saslprep_rc
Definition: saslprep.h:21
@ SASLPREP_OOM
Definition: saslprep.h:23
@ SASLPREP_SUCCESS
Definition: saslprep.h:22
char * scram_build_secret(pg_cryptohash_type hash_type, int key_length, const char *salt, int saltlen, int iterations, const char *password, const char **errstr)
Definition: scram-common.c:210
#define SCRAM_DEFAULT_SALT_LEN
Definition: scram-common.h:44
#define SCRAM_SHA_256_KEY_LEN
Definition: scram-common.h:24
static char * password
Definition: streamutil.c:54
int iterations
Definition: thread-thread.c:39

References free, iterations, libpq_gettext, password, pg_saslprep(), PG_SHA256, pg_strong_random(), SASLPREP_OOM, SASLPREP_SUCCESS, scram_build_secret(), SCRAM_DEFAULT_SALT_LEN, and SCRAM_SHA_256_KEY_LEN.

Referenced by PQencryptPasswordConn().

◆ pg_fe_sendauth()

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

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

962 {
963  int oldmsglen;
964 
965  if (!check_expected_areq(areq, conn))
966  return STATUS_ERROR;
967 
968  switch (areq)
969  {
970  case AUTH_REQ_OK:
971  break;
972 
973  case AUTH_REQ_KRB4:
974  libpq_append_conn_error(conn, "Kerberos 4 authentication not supported");
975  return STATUS_ERROR;
976 
977  case AUTH_REQ_KRB5:
978  libpq_append_conn_error(conn, "Kerberos 5 authentication not supported");
979  return STATUS_ERROR;
980 
981 #if defined(ENABLE_GSS) || defined(ENABLE_SSPI)
982  case AUTH_REQ_GSS:
983 #if !defined(ENABLE_SSPI)
984  /* no native SSPI, so use GSSAPI library for it */
985  case AUTH_REQ_SSPI:
986 #endif
987  {
988  int r;
989 
990  pglock_thread();
991 
992  /*
993  * If we have both GSS and SSPI support compiled in, use SSPI
994  * support by default. This is overridable by a connection
995  * string parameter. Note that when using SSPI we still leave
996  * the negotiate parameter off, since we want SSPI to use the
997  * GSSAPI kerberos protocol. For actual SSPI negotiate
998  * protocol, we use AUTH_REQ_SSPI.
999  */
1000 #if defined(ENABLE_GSS) && defined(ENABLE_SSPI)
1001  if (conn->gsslib && (pg_strcasecmp(conn->gsslib, "gssapi") == 0))
1002  r = pg_GSS_startup(conn, payloadlen);
1003  else
1004  r = pg_SSPI_startup(conn, 0, payloadlen);
1005 #elif defined(ENABLE_GSS) && !defined(ENABLE_SSPI)
1006  r = pg_GSS_startup(conn, payloadlen);
1007 #elif !defined(ENABLE_GSS) && defined(ENABLE_SSPI)
1008  r = pg_SSPI_startup(conn, 0, payloadlen);
1009 #endif
1010  if (r != STATUS_OK)
1011  {
1012  /* Error message already filled in. */
1013  pgunlock_thread();
1014  return STATUS_ERROR;
1015  }
1016  pgunlock_thread();
1017  }
1018  break;
1019 
1020  case AUTH_REQ_GSS_CONT:
1021  {
1022  int r;
1023 
1024  pglock_thread();
1025 #if defined(ENABLE_GSS) && defined(ENABLE_SSPI)
1026  if (conn->usesspi)
1027  r = pg_SSPI_continue(conn, payloadlen);
1028  else
1029  r = pg_GSS_continue(conn, payloadlen);
1030 #elif defined(ENABLE_GSS) && !defined(ENABLE_SSPI)
1031  r = pg_GSS_continue(conn, payloadlen);
1032 #elif !defined(ENABLE_GSS) && defined(ENABLE_SSPI)
1033  r = pg_SSPI_continue(conn, payloadlen);
1034 #endif
1035  if (r != STATUS_OK)
1036  {
1037  /* Error message already filled in. */
1038  pgunlock_thread();
1039  return STATUS_ERROR;
1040  }
1041  pgunlock_thread();
1042  }
1043  break;
1044 #else /* defined(ENABLE_GSS) || defined(ENABLE_SSPI) */
1045  /* No GSSAPI *or* SSPI support */
1046  case AUTH_REQ_GSS:
1047  case AUTH_REQ_GSS_CONT:
1048  libpq_append_conn_error(conn, "GSSAPI authentication not supported");
1049  return STATUS_ERROR;
1050 #endif /* defined(ENABLE_GSS) || defined(ENABLE_SSPI) */
1051 
1052 #ifdef ENABLE_SSPI
1053  case AUTH_REQ_SSPI:
1054 
1055  /*
1056  * SSPI has its own startup message so libpq can decide which
1057  * method to use. Indicate to pg_SSPI_startup that we want SSPI
1058  * negotiation instead of Kerberos.
1059  */
1060  pglock_thread();
1061  if (pg_SSPI_startup(conn, 1, payloadlen) != STATUS_OK)
1062  {
1063  /* Error message already filled in. */
1064  pgunlock_thread();
1065  return STATUS_ERROR;
1066  }
1067  pgunlock_thread();
1068  break;
1069 #else
1070 
1071  /*
1072  * No SSPI support. However, if we have GSSAPI but not SSPI
1073  * support, AUTH_REQ_SSPI will have been handled in the codepath
1074  * for AUTH_REQ_GSS above, so don't duplicate the case label in
1075  * that case.
1076  */
1077 #if !defined(ENABLE_GSS)
1078  case AUTH_REQ_SSPI:
1079  libpq_append_conn_error(conn, "SSPI authentication not supported");
1080  return STATUS_ERROR;
1081 #endif /* !define(ENABLE_GSS) */
1082 #endif /* ENABLE_SSPI */
1083 
1084 
1085  case AUTH_REQ_CRYPT:
1086  libpq_append_conn_error(conn, "Crypt authentication not supported");
1087  return STATUS_ERROR;
1088 
1089  case AUTH_REQ_MD5:
1090  case AUTH_REQ_PASSWORD:
1091  {
1092  char *password;
1093 
1094  conn->password_needed = true;
1096  if (password == NULL)
1097  password = conn->pgpass;
1098  if (password == NULL || password[0] == '\0')
1099  {
1102  return STATUS_ERROR;
1103  }
1105  {
1107  "fe_sendauth: error sending password authentication\n");
1108  return STATUS_ERROR;
1109  }
1110 
1111  /* We expect no further authentication requests. */
1112  conn->client_finished_auth = true;
1113  break;
1114  }
1115 
1116  case AUTH_REQ_SASL:
1117 
1118  /*
1119  * The request contains the name (as assigned by IANA) of the
1120  * authentication mechanism.
1121  */
1122  if (pg_SASL_init(conn, payloadlen) != STATUS_OK)
1123  {
1124  /* pg_SASL_init already set the error message */
1125  return STATUS_ERROR;
1126  }
1127  break;
1128 
1129  case AUTH_REQ_SASL_CONT:
1130  case AUTH_REQ_SASL_FIN:
1131  if (conn->sasl_state == NULL)
1132  {
1134  "fe_sendauth: invalid authentication request from server: AUTH_REQ_SASL_CONT without AUTH_REQ_SASL\n");
1135  return STATUS_ERROR;
1136  }
1137  oldmsglen = conn->errorMessage.len;
1138  if (pg_SASL_continue(conn, payloadlen,
1139  (areq == AUTH_REQ_SASL_FIN)) != STATUS_OK)
1140  {
1141  /* Use this message if pg_SASL_continue didn't supply one */
1142  if (conn->errorMessage.len == oldmsglen)
1144  "fe_sendauth: error in SASL authentication\n");
1145  return STATUS_ERROR;
1146  }
1147  break;
1148 
1149  default:
1150  libpq_append_conn_error(conn, "authentication method %u not supported", areq);
1151  return STATUS_ERROR;
1152  }
1153 
1154  return STATUS_OK;
1155 }
#define STATUS_OK
Definition: c.h:1169
#define STATUS_ERROR
Definition: c.h:1170
static bool check_expected_areq(AuthRequest areq, PGconn *conn)
Definition: fe-auth.c:802
static int pg_SASL_continue(PGconn *conn, int payloadlen, bool final)
Definition: fe-auth.c:628
static int pg_SASL_init(PGconn *conn, int payloadlen)
Definition: fe-auth.c:422
static int pg_password_sendauth(PGconn *conn, const char *password, AuthRequest areq)
Definition: fe-auth.c:700
void libpq_append_conn_error(PGconn *conn, const char *fmt,...)
Definition: fe-misc.c:1357
#define PQnoPasswordSupplied
Definition: libpq-fe.h:599
#define pglock_thread()
Definition: libpq-int.h:694
#define pgunlock_thread()
Definition: libpq-int.h:695
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:367
#define AUTH_REQ_SSPI
Definition: protocol.h:83
#define AUTH_REQ_SASL_CONT
Definition: protocol.h:85
#define AUTH_REQ_GSS
Definition: protocol.h:81
#define AUTH_REQ_MD5
Definition: protocol.h:79
#define AUTH_REQ_KRB5
Definition: protocol.h:76
#define AUTH_REQ_OK
Definition: protocol.h:74
#define AUTH_REQ_KRB4
Definition: protocol.h:75
#define AUTH_REQ_PASSWORD
Definition: protocol.h:77
#define AUTH_REQ_GSS_CONT
Definition: protocol.h:82
#define AUTH_REQ_CRYPT
Definition: protocol.h:78
#define AUTH_REQ_SASL
Definition: protocol.h:84
#define AUTH_REQ_SASL_FIN
Definition: protocol.h:86
PGconn * conn
Definition: streamutil.c:55
char * password
Definition: libpq-int.h:355
char * pgpass
Definition: libpq-int.h:386
bool client_finished_auth
Definition: libpq-int.h:491
PQExpBufferData errorMessage
Definition: libpq-int.h:633
char * gsslib
Definition: libpq-int.h:410
void * sasl_state
Definition: libpq-int.h:558
int whichhost
Definition: libpq-int.h:455
pg_conn_host * connhost
Definition: libpq-int.h:456
bool password_needed
Definition: libpq-int.h:480

References appendPQExpBufferStr(), 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_SSPI, check_expected_areq(), pg_conn::client_finished_auth, conn, pg_conn::connhost, pg_conn::errorMessage, pg_conn::gsslib, PQExpBufferData::len, libpq_append_conn_error(), password, pg_conn_host::password, pg_conn::password_needed, pg_password_sendauth(), pg_SASL_continue(), pg_SASL_init(), pg_strcasecmp(), pglock_thread, pg_conn::pgpass, pgunlock_thread, PQnoPasswordSupplied, pg_conn::sasl_state, STATUS_ERROR, STATUS_OK, and pg_conn::whichhost.

Referenced by PQconnectPoll().

Variable Documentation

◆ pg_scram_mech

const pg_fe_sasl_mech pg_scram_mech
extern

Definition at line 32 of file fe-auth-scram.c.

Referenced by pg_SASL_init().