From: liulang <lang.liu@esgyn.cn>
To: pgsql-bugs@lists.postgresql.org
Subject: lost status 'STATUS_EOF' for authentication when using 'MD5' or 'scram-sha-256'
Date: Tue, 2 Jan 2024 18:31:00 +0800
Message-ID: <b725238c-539d-cb09-2bff-b5e6cb2c069c@esgyn.cn> (raw)
Hello:
I'm an extension writer and I wrote an extension to lock users who
tried to enter the wrong password too much. This extension is hooking
'ClientAuthentication_hook' and checking the hook's 'status' parameter
to count the times of wrong password. It’s work fine when auth is
'password', but get double count when auth is'MD5'or'scram-sha-256'.
problem reappearance:
1. *create an user without password*
2. set pg_hba.conf with ‘MD5’ or ‘scram-sha-256’
3. use psql without -W or -w or .pgpass file or env PGPASSFILE
4. It's ok when client is pgadmin or another
5. all of version pg can reappearance
when I read the source, find an incorret logical way on
CheckPWChallengeAuth at src/backend/libpq/auth.c
Like
static int
CheckPWChallengeAuth(Port *port, char **logdetail)
{
...
/* the CheckMD5Auth or CheckSCRAMAuth returns STATUS_EOF because
psql is not provide password,
* It will prompt user to type and send the password to other
backend process next time.
* The current backend will exit normally.
*/
if (port->hba->auth_method == uaMD5 && pwtype == PASSWORD_TYPE_MD5)
auth_result = CheckMD5Auth(port, shadow_pass, logdetail);
else
auth_result = CheckSCRAMAuth(port, shadow_pass, logdetail);
...
/*
* If get_role_password() returned error, return error, even if the
* authentication succeeded.
*/
/* The get_role_password returns NULL when the user without password */
if (!shadow_pass)
{
Assert(auth_result != STATUS_OK);
/* lost STATUS_EOF */
*return STATUS_ERROR;*
}
...
}
The above code does not affect the database execution,but
ClientAuthentication_hook will be confused whether the password is
incorrect or not currently entered?
so.. The CheckPWChallengeAuth should returns STATUS_EOF when It is, I think.
Try to change the code
static int
CheckPWChallengeAuth(Port *port, char **logdetail)
{
if (port->hba->auth_method == uaMD5 && pwtype == PASSWORD_TYPE_MD5)
auth_result = CheckMD5Auth(port, shadow_pass, logdetail);
else
auth_result = CheckSCRAMAuth(port, shadow_pass, logdetail);
...
if (STATUS_EOF == auth_result)
{
*/* do nothing */*
}
else if (!shadow_pass)
{
Assert(auth_result != STATUS_OK);
return STATUS_ERROR;
}
else if (auth_result == STATUS_OK)
set_authn_id(port, port->user_name);
return auth_result;
}
I don't know if this is right, please point out. thanks!
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: lang.liu@esgyn.cn
Subject: Re: lost status 'STATUS_EOF' for authentication when using 'MD5' or 'scram-sha-256'
In-Reply-To: <b725238c-539d-cb09-2bff-b5e6cb2c069c@esgyn.cn>
References: <b725238c-539d-cb09-2bff-b5e6cb2c069c@esgyn.cn>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox