Skip to content

Commit f263925

Browse files
committed
Add feature "Disallow password change" to disallow users to change
their password. This behavior is enabled by setting: credcheck.disallow_password_change=on It returns the following message when a user tries to change its password: ERROR: you are not allowed to change your password. Thanks to Jacute for the reature request.
1 parent eb1036c commit f263925

2 files changed

Lines changed: 35 additions & 7 deletions

File tree

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- [Authentication failure ban](#authentication-failure-ban)
99
- [Authentication delay](#authentication-delay)
1010
- [Force password change](#force-password-change)
11+
- [Disallow password change](#disallow-password-change)
1112
- [Warning before password expire](#warning-before-password-expire)
1213
- [Examples](#examples)
1314
- [Limitations](#limitations)
@@ -466,6 +467,19 @@ You can also force any user to change his password at any time using:
466467
ALTER USER user1 SET credcheck_internal.force_change_password = true;
467468
```
468469

470+
### [Disallow password change](#disallow-password-change)
471+
472+
This feature disallow users to change their password. This behavior is enabled by setting `credcheck.disallow_password_change` to `true`. For example:
473+
474+
```
475+
$ psql -h localhost -d test -U user1
476+
psql (17.5 (Ubuntu 17.5-1.pgdg24.04+1))
477+
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, compression: off, ALPN: postgresql)
478+
Type "help" for help.
479+
480+
test=> ALTER ROLE 'user1' PASSWORD 'My-New-Pass#123';
481+
ERROR: you are not allowed to change your password.
482+
469483
### [Warning before password expire](#warning-before-password-expire)
470484
471485
To send a warning to the user N days before his password expires you can set the number of days

credcheck.c

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ static int password_valid_max = 0;
248248
static int auth_delay_milliseconds = 0;
249249
static bool password_change_first_login = false;
250250
static bool force_change_password = false;
251+
static bool disallow_change_password = false;
251252

252253
#if PG_VERSION_NUM >= 120000
253254
/*
@@ -870,6 +871,11 @@ password_guc()
870871
NULL, &force_change_password, false, PGC_SUSET, 0,
871872
NULL, NULL, NULL);
872873

874+
DefineCustomBoolVariable("credcheck.disallow_change_password",
875+
gettext_noop("prevent users to change their password"),
876+
NULL, &disallow_change_password, false, PGC_SUSET, 0,
877+
NULL, NULL, NULL);
878+
873879
}
874880

875881
#if PG_VERSION_NUM >= 120000
@@ -1451,15 +1457,23 @@ cc_ProcessUtility(PEL_PROCESSUTILITY_PROTO)
14511457
{
14521458
Node *parsetree = pstmt->utilityStmt;
14531459

1454-
/* at first login we don't allow anything else than password change */
1455-
/*
1456-
* When first login we don't allow anything else than password change.
1457-
* Command \password issue a "show password_encryption" query after
1458-
* change password prompts, allow it. The \password command will be
1459-
* rejected anyway because it uses encrypted password.
1460-
*/
14611460
if (!is_in_whitelist(MyProcPort->user_name, username_whitelist))
14621461
{
1462+
/*
1463+
* When disallow_change_password is enable we return
1464+
* an error to any usertrying to change his password.
1465+
*/
1466+
if (nodeTag(parsetree) == T_AlterRoleStmt && disallow_change_password)
1467+
ereport(ERROR,
1468+
(errcode(ERRCODE_INVALID_AUTHORIZATION_SPECIFICATION),
1469+
errmsg(gettext_noop("you are not allowed to change your password."))));
1470+
1471+
/*
1472+
* When first login we don't allow anything else than password change.
1473+
* Command \password issue a "show password_encryption" query after
1474+
* change password prompts, allow it. The \password command will be
1475+
* rejected anyway because it uses encrypted password.
1476+
*/
14631477
if (nodeTag(parsetree) != T_AlterRoleStmt && force_change_password
14641478
&& strcmp(debug_query_string, "show password_encryption") != 0)
14651479
ereport(ERROR,

0 commit comments

Comments
 (0)