Skip to content

Commit 9a29ff4

Browse files
committed
Allow no password policy checks at all for changes done by a superuser
enabling new GUC credcheck.superuser_nocheck. Thanks to Jacute for the feature request.
1 parent f263925 commit 9a29ff4

4 files changed

Lines changed: 37 additions & 1 deletion

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ There is also the `credcheck.whitelist` GUC that can be used to set a comma sepa
102102
```
103103
credcheck.whitelist = 'admin,supuser'
104104
```
105-
will disable any credcheck policy for the user named `admin` and `supuser`.
105+
will disable any credcheck policy for password change of users named `admin` or `supuser`.
106+
107+
To disable password policy checks for changes done by a superuser, enable GUC `credcheck.superuser_nocheck`.
106108

107109
### [Examples](#examples)
108110

credcheck.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ static int auth_delay_milliseconds = 0;
249249
static bool password_change_first_login = false;
250250
static bool force_change_password = false;
251251
static bool disallow_change_password = false;
252+
static bool superuser_nocheck = false;
252253

253254
#if PG_VERSION_NUM >= 120000
254255
/*
@@ -594,6 +595,13 @@ static void password_check(const char *username, const char *password)
594595
Assert(username != NULL);
595596
Assert(password != NULL);
596597

598+
/*
599+
* no passowrd check at all if the user is superuser
600+
* and superuser_nocheck is enabled
601+
*/
602+
if (superuser() && superuser_nocheck)
603+
return;
604+
597605
/* checks has to be done by ignoring case */
598606
if (password_ignore_case)
599607
{
@@ -876,6 +884,11 @@ password_guc()
876884
NULL, &disallow_change_password, false, PGC_SUSET, 0,
877885
NULL, NULL, NULL);
878886

887+
DefineCustomBoolVariable("credcheck.superuser_nocheck",
888+
gettext_noop("don't do password check if the logged user is a superuser"),
889+
NULL, &superuser_nocheck, false, PGC_SUSET, 0,
890+
NULL, NULL, NULL);
891+
879892
}
880893

881894
#if PG_VERSION_NUM >= 120000
@@ -1295,6 +1308,7 @@ check_password(const char *username, const char *password,
12951308
#ifdef USE_CRACKLIB
12961309
const char *reason;
12971310
#endif
1311+
/* don't do any password check if the role is whitelisted */
12981312
if (is_in_whitelist((char *)username, username_whitelist))
12991313
break;
13001314

test/expected/09_su_nocheck.out

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
LOAD 'credcheck';
2+
SET credcheck.password_min_upper To 4;
3+
CREATE USER aaa PASSWORD 'DuMmY4P';
4+
-- must return an error
5+
ALTER ROLE aaa PASSWORD 'DummY2';
6+
ERROR: password does not contain the configured credcheck.password_min_upper characters (4)
7+
SET credcheck.superuser_nocheck TO on;
8+
-- no error
9+
ALTER ROLE aaa PASSWORD 'DummY2';
10+
DROP ROLE aaa;

test/sql/09_su_nocheck.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
LOAD 'credcheck';
2+
3+
SET credcheck.password_min_upper To 4;
4+
CREATE USER aaa PASSWORD 'DuMmY4P';
5+
-- must return an error
6+
ALTER ROLE aaa PASSWORD 'DummY2';
7+
SET credcheck.superuser_nocheck TO on;
8+
-- no error
9+
ALTER ROLE aaa PASSWORD 'DummY2';
10+
DROP ROLE aaa;

0 commit comments

Comments
 (0)