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 1213 of file fe-auth.c.

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

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 1168 of file fe-auth.c.

1169 {
1170  char *result = NULL;
1171  const char *name = NULL;
1172 
1173 #ifdef WIN32
1174  /* Microsoft recommends buffer size of UNLEN+1, where UNLEN = 256 */
1175  char username[256 + 1];
1176  DWORD namesize = sizeof(username);
1177 #else
1178  char pwdbuf[BUFSIZ];
1179 #endif
1180 
1181 #ifdef WIN32
1182  if (GetUserName(username, &namesize))
1183  name = username;
1184  else if (errorMessage)
1185  libpq_append_error(errorMessage,
1186  "user name lookup failure: error code %lu",
1187  GetLastError());
1188 #else
1189  if (pg_get_user_name(user_id, pwdbuf, sizeof(pwdbuf)))
1190  name = pwdbuf;
1191  else if (errorMessage)
1192  appendPQExpBuffer(errorMessage, "%s\n", pwdbuf);
1193 #endif
1194 
1195  if (name)
1196  {
1197  result = strdup(name);
1198  if (result == NULL && errorMessage)
1199  libpq_append_error(errorMessage, "out of memory");
1200  }
1201 
1202  return result;
1203 }
void libpq_append_error(PQExpBuffer errorMessage, const char *fmt,...)
Definition: fe-misc.c:1296
const char * username
Definition: pgbench.c:296
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:899
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 960 of file fe-auth.c.

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

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