1997-12-04 00:34:01 +00:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-13 23:22:53 +00:00
|
|
|
* crypt.c
|
2001-11-01 18:10:48 +00:00
|
|
|
* Look into the password file and check the encrypted password with
|
|
|
|
* the one passed in from the frontend.
|
1997-12-04 00:34:01 +00:00
|
|
|
*
|
2001-11-01 18:10:48 +00:00
|
|
|
* Original coding by Todd A. Brandys
|
1997-12-30 02:26:56 +00:00
|
|
|
*
|
2010-01-02 16:58:17 +00:00
|
|
|
* Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
|
2001-11-01 18:10:48 +00:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1997-12-30 02:26:56 +00:00
|
|
|
*
|
2010-02-26 02:01:40 +00:00
|
|
|
* $PostgreSQL: pgsql/src/backend/libpq/crypt.c,v 1.81 2010/02/26 02:00:42 momjian Exp $
|
1997-12-04 00:34:01 +00:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
2001-11-01 18:10:48 +00:00
|
|
|
#include "postgres.h"
|
1997-12-04 00:34:01 +00:00
|
|
|
|
|
|
|
#include <unistd.h>
|
2001-11-02 18:39:57 +00:00
|
|
|
#ifdef HAVE_CRYPT_H
|
|
|
|
#include <crypt.h>
|
|
|
|
#endif
|
1997-12-04 00:34:01 +00:00
|
|
|
|
2009-08-29 19:26:52 +00:00
|
|
|
#include "catalog/pg_authid.h"
|
1999-07-16 05:00:38 +00:00
|
|
|
#include "libpq/crypt.h"
|
2006-06-20 19:56:52 +00:00
|
|
|
#include "libpq/md5.h"
|
2009-08-29 19:26:52 +00:00
|
|
|
#include "miscadmin.h"
|
|
|
|
#include "utils/builtins.h"
|
|
|
|
#include "utils/syscache.h"
|
1997-12-30 02:26:56 +00:00
|
|
|
|
1998-01-26 01:42:53 +00:00
|
|
|
|
1998-02-26 04:46:47 +00:00
|
|
|
int
|
2005-06-28 05:09:14 +00:00
|
|
|
md5_crypt_verify(const Port *port, const char *role, char *client_pass)
|
1998-02-26 04:46:47 +00:00
|
|
|
{
|
1999-05-25 16:15:34 +00:00
|
|
|
int retval = STATUS_ERROR;
|
2009-08-29 19:26:52 +00:00
|
|
|
char *shadow_pass,
|
|
|
|
*crypt_pwd;
|
|
|
|
TimestampTz vuntil = 0;
|
2002-12-05 18:52:43 +00:00
|
|
|
char *crypt_client_pass = client_pass;
|
2009-08-29 19:26:52 +00:00
|
|
|
HeapTuple roleTup;
|
|
|
|
Datum datum;
|
|
|
|
bool isnull;
|
|
|
|
|
|
|
|
/*
|
2010-02-26 02:01:40 +00:00
|
|
|
* Disable immediate interrupts while doing database access. (Note we
|
|
|
|
* don't bother to turn this back on if we hit one of the failure
|
2009-08-29 19:26:52 +00:00
|
|
|
* conditions, since we can expect we'll just exit right away anyway.)
|
|
|
|
*/
|
|
|
|
ImmediateInterruptOK = false;
|
2002-09-04 20:31:48 +00:00
|
|
|
|
2009-08-29 19:26:52 +00:00
|
|
|
/* Get role info from pg_authid */
|
2010-02-14 18:42:19 +00:00
|
|
|
roleTup = SearchSysCache1(AUTHNAME, PointerGetDatum(role));
|
2009-08-29 19:26:52 +00:00
|
|
|
if (!HeapTupleIsValid(roleTup))
|
2010-02-26 02:01:40 +00:00
|
|
|
return STATUS_ERROR; /* no such user */
|
1998-02-26 04:46:47 +00:00
|
|
|
|
2009-08-29 19:26:52 +00:00
|
|
|
datum = SysCacheGetAttr(AUTHNAME, roleTup,
|
|
|
|
Anum_pg_authid_rolpassword, &isnull);
|
|
|
|
if (isnull)
|
2002-04-25 00:56:36 +00:00
|
|
|
{
|
2009-08-29 19:26:52 +00:00
|
|
|
ReleaseSysCache(roleTup);
|
2010-02-26 02:01:40 +00:00
|
|
|
return STATUS_ERROR; /* user has no password */
|
2002-04-25 00:56:36 +00:00
|
|
|
}
|
2009-08-29 19:26:52 +00:00
|
|
|
shadow_pass = TextDatumGetCString(datum);
|
|
|
|
|
|
|
|
datum = SysCacheGetAttr(AUTHNAME, roleTup,
|
|
|
|
Anum_pg_authid_rolvaliduntil, &isnull);
|
|
|
|
if (!isnull)
|
|
|
|
vuntil = DatumGetTimestampTz(datum);
|
2002-09-04 20:31:48 +00:00
|
|
|
|
2009-08-29 19:26:52 +00:00
|
|
|
ReleaseSysCache(roleTup);
|
|
|
|
|
|
|
|
if (*shadow_pass == '\0')
|
2010-02-26 02:01:40 +00:00
|
|
|
return STATUS_ERROR; /* empty password */
|
2009-08-29 19:26:52 +00:00
|
|
|
|
|
|
|
/* Re-enable immediate response to SIGTERM/SIGINT/timeout interrupts */
|
|
|
|
ImmediateInterruptOK = true;
|
|
|
|
/* And don't forget to detect one that already arrived */
|
|
|
|
CHECK_FOR_INTERRUPTS();
|
1998-02-26 04:46:47 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Compare with the encrypted or plain password depending on the
|
|
|
|
* authentication method being used for this connection.
|
|
|
|
*/
|
2008-09-15 12:32:57 +00:00
|
|
|
switch (port->hba->auth_method)
|
2001-08-17 02:59:20 +00:00
|
|
|
{
|
2001-08-15 18:42:16 +00:00
|
|
|
case uaMD5:
|
2001-10-25 05:50:21 +00:00
|
|
|
crypt_pwd = palloc(MD5_PASSWD_LEN + 1);
|
2002-12-05 18:52:43 +00:00
|
|
|
if (isMD5(shadow_pass))
|
2001-08-15 18:42:16 +00:00
|
|
|
{
|
2005-08-15 02:40:36 +00:00
|
|
|
/* stored password already encrypted, only do salt */
|
2005-10-17 16:24:20 +00:00
|
|
|
if (!pg_md5_encrypt(shadow_pass + strlen("md5"),
|
|
|
|
(char *) port->md5Salt,
|
|
|
|
sizeof(port->md5Salt), crypt_pwd))
|
2001-08-15 18:42:16 +00:00
|
|
|
{
|
|
|
|
pfree(crypt_pwd);
|
|
|
|
return STATUS_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-08-15 02:40:36 +00:00
|
|
|
/* stored password is plain, double-encrypt */
|
2001-10-25 05:50:21 +00:00
|
|
|
char *crypt_pwd2 = palloc(MD5_PASSWD_LEN + 1);
|
2001-08-15 18:42:16 +00:00
|
|
|
|
2005-10-17 16:24:20 +00:00
|
|
|
if (!pg_md5_encrypt(shadow_pass,
|
|
|
|
port->user_name,
|
|
|
|
strlen(port->user_name),
|
|
|
|
crypt_pwd2))
|
2001-08-15 18:42:16 +00:00
|
|
|
{
|
|
|
|
pfree(crypt_pwd);
|
|
|
|
pfree(crypt_pwd2);
|
|
|
|
return STATUS_ERROR;
|
|
|
|
}
|
2005-10-17 16:24:20 +00:00
|
|
|
if (!pg_md5_encrypt(crypt_pwd2 + strlen("md5"),
|
|
|
|
port->md5Salt,
|
|
|
|
sizeof(port->md5Salt),
|
|
|
|
crypt_pwd))
|
2001-08-15 18:42:16 +00:00
|
|
|
{
|
|
|
|
pfree(crypt_pwd);
|
|
|
|
pfree(crypt_pwd2);
|
|
|
|
return STATUS_ERROR;
|
|
|
|
}
|
|
|
|
pfree(crypt_pwd2);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2002-12-05 18:52:43 +00:00
|
|
|
if (isMD5(shadow_pass))
|
2002-12-05 18:39:43 +00:00
|
|
|
{
|
2005-08-15 02:40:36 +00:00
|
|
|
/* Encrypt user-supplied password to match stored MD5 */
|
2002-12-05 18:52:43 +00:00
|
|
|
crypt_client_pass = palloc(MD5_PASSWD_LEN + 1);
|
2005-10-17 16:24:20 +00:00
|
|
|
if (!pg_md5_encrypt(client_pass,
|
|
|
|
port->user_name,
|
|
|
|
strlen(port->user_name),
|
|
|
|
crypt_client_pass))
|
2002-12-05 18:39:43 +00:00
|
|
|
{
|
2002-12-05 18:52:43 +00:00
|
|
|
pfree(crypt_client_pass);
|
2002-12-05 18:39:43 +00:00
|
|
|
return STATUS_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2002-12-05 18:52:43 +00:00
|
|
|
crypt_pwd = shadow_pass;
|
2001-08-15 18:42:16 +00:00
|
|
|
break;
|
|
|
|
}
|
1998-02-26 04:46:47 +00:00
|
|
|
|
2002-12-05 18:52:43 +00:00
|
|
|
if (strcmp(crypt_client_pass, crypt_pwd) == 0)
|
1998-02-26 04:46:47 +00:00
|
|
|
{
|
|
|
|
/*
|
2009-08-29 19:26:52 +00:00
|
|
|
* Password OK, now check to be sure we are not past rolvaliduntil
|
1998-02-26 04:46:47 +00:00
|
|
|
*/
|
2009-08-29 19:26:52 +00:00
|
|
|
if (isnull)
|
1998-02-26 04:46:47 +00:00
|
|
|
retval = STATUS_OK;
|
2009-08-29 19:26:52 +00:00
|
|
|
else if (vuntil < GetCurrentTimestamp())
|
|
|
|
retval = STATUS_ERROR;
|
2005-06-28 05:09:14 +00:00
|
|
|
else
|
2009-08-29 19:26:52 +00:00
|
|
|
retval = STATUS_OK;
|
1998-02-26 04:46:47 +00:00
|
|
|
}
|
|
|
|
|
2008-09-15 12:32:57 +00:00
|
|
|
if (port->hba->auth_method == uaMD5)
|
2001-08-15 18:42:16 +00:00
|
|
|
pfree(crypt_pwd);
|
2002-12-05 18:52:43 +00:00
|
|
|
if (crypt_client_pass != client_pass)
|
|
|
|
pfree(crypt_client_pass);
|
1998-02-26 04:46:47 +00:00
|
|
|
|
|
|
|
return retval;
|
1997-12-04 00:34:01 +00:00
|
|
|
}
|