Fix handling of previous password hooks in passwordcheck
authorMichael Paquier <[email protected]>
Thu, 1 Aug 2019 00:38:29 +0000 (09:38 +0900)
committerMichael Paquier <[email protected]>
Thu, 1 Aug 2019 00:38:29 +0000 (09:38 +0900)
When piling up loading of modules using check_password_hook_type,
loading passwordcheck would remove any trace of a previously-loaded
hook.  Unloading the module would also cause previous hooks to be
entirely gone.

Reported-by: Rafael Castro
Author: Michael Paquier
Reviewed-by: Daniel Gustafsson
Discussion: https://postgr.es/m/15932-78f48f9ef166778c@postgresql.org
Backpatch-through: 9.4

contrib/passwordcheck/passwordcheck.c

index 405896d40a9822b2c7543a08127013661608c2d2..a46403c701f4189cd45da7dd276d30ae9fa05a6c 100644 (file)
 
 PG_MODULE_MAGIC;
 
+/* Saved hook value in case of unload */
+static check_password_hook_type prev_check_password_hook = NULL;
+
 /* passwords shorter than this will be rejected */
 #define MIN_PWD_LENGTH 8
 
 extern void _PG_init(void);
+extern void _PG_fini(void);
 
 /*
  * check_password
@@ -63,6 +67,11 @@ check_password(const char *username,
    bool        pwd_has_letter,
                pwd_has_nonletter;
 
+   if (prev_check_password_hook)
+       prev_check_password_hook(username, password,
+                                password_type, validuntil_time,
+                                validuntil_null);
+
    switch (password_type)
    {
        case PASSWORD_TYPE_MD5:
@@ -144,5 +153,16 @@ void
 _PG_init(void)
 {
    /* activate password checks when the module is loaded */
+   prev_check_password_hook = check_password_hook;
    check_password_hook = check_password;
 }
+
+/*
+ * Module unload function
+ */
+void
+_PG_fini(void)
+{
+   /* uninstall hook */
+   check_password_hook = prev_check_password_hook;
+}