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

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

References pg_fe_getusername().

Referenced by connectOptions2(), and conninfo_add_defaults().

◆ pg_fe_getusername()

char* pg_fe_getusername ( uid_t  user_id,
PQExpBuffer  errorMessage 
)

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

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

899 {
900  char *prep_password;
901  pg_saslprep_rc rc;
902  char saltbuf[SCRAM_DEFAULT_SALT_LEN];
903  char *result;
904 
905  /*
906  * Normalize the password with SASLprep. If that doesn't work, because
907  * the password isn't valid UTF-8 or contains prohibited characters, just
908  * proceed with the original password. (See comments at the top of
909  * auth-scram.c.)
910  */
911  rc = pg_saslprep(password, &prep_password);
912  if (rc == SASLPREP_OOM)
913  {
914  *errstr = libpq_gettext("out of memory");
915  return NULL;
916  }
917  if (rc == SASLPREP_SUCCESS)
918  password = (const char *) prep_password;
919 
920  /* Generate a random salt */
922  {
923  *errstr = libpq_gettext("could not generate random salt");
924  free(prep_password);
925  return NULL;
926  }
927 
931  errstr);
932 
933  free(prep_password);
934 
935  return result;
936 }
@ PG_SHA256
Definition: cryptohash.h:24
#define free(a)
Definition: header.h:65
#define libpq_gettext(x)
Definition: libpq-int.h:901
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:53
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 962 of file fe-auth.c.

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

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

Referenced by pg_SASL_init().