Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vpwfa-001g9c-2r for pgsql-hackers@arkaria.postgresql.org; Tue, 10 Feb 2026 22:57:24 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vpweZ-001Mxw-0g for pgsql-hackers@arkaria.postgresql.org; Tue, 10 Feb 2026 22:56:20 +0000 Received: from makus.postgresql.org ([72.32.157.229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vpweY-001Mxm-24 for pgsql-hackers@lists.postgresql.org; Tue, 10 Feb 2026 22:56:19 +0000 Received: from mail-vk1-xa35.google.com ([2607:f8b0:4864:20::a35]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1vpweT-0000000028G-1Mmw for pgsql-hackers@postgresql.org; Tue, 10 Feb 2026 22:56:18 +0000 Received: by mail-vk1-xa35.google.com with SMTP id 71dfb90a1353d-5634feea416so3147459e0c.2 for ; Tue, 10 Feb 2026 14:56:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1770764173; x=1771368973; darn=postgresql.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=VAgTLseFCNtazhPairCFsy5f+8pkkUawx9Jz8VDB+Cc=; b=WP9qeq6VqjLoCJQbithSF56gnNXHxY9r8EAYAY7ANdqTBbrBrsE9vx41LwKcp1SSUj n63pAgoZkxlETjUxv06bs9t5Ke9z/iq2AT4YC8P2rkpAvEirR2CnU7af+pFVVfD4WmA5 Dfl8ejJdCfkwEf6iCd5t3jq4uyAEHdMJ8fz3Sd1OwE1EpMfcSBh4a48V9g7TkJLiEUNv yEVETAu3ot8jUxBw/ZA1Qc7y6CVxHH3YviFVUlsNfbVmYB9pFIQq+2MkLSMNsXbSp5Ik iuNWKuizgYoWTqAarhlgFzZdrmtjY0gXgEobVS1HCF6eQoiwrlNUIAMefa+xr8yfxePh cgpw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770764173; x=1771368973; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=VAgTLseFCNtazhPairCFsy5f+8pkkUawx9Jz8VDB+Cc=; b=V5VLdNi/+c2DI0maO1Bor+1PmWqow9CIaTHNIEFiLDH5AeOrJwD4owTgoU+I5JvZMm 8a+sLdR/8Bs0yXBJNMsWNTqldjSNVKt8V1qospSCS4dT1N0//iUO0CW8nE40Le1BA94b V5yW8tOpWk2dvaYsbuv7l11GDx4AvgHuVcAQn7bGpITWVF83jeTMcZVCVuESzmX9lv4U 2Y5h7XxIIsh+2v8XkMem1GDY4yjlSQUHAaPaSyy+qaVUaI8UjlxctHT9Iwippzm95Vi/ MlSruVOV2/Fy3NZvcN7boiFT8b5HjNMH/PB57VEym3Ak3uc3RX8/j60RC1q8x7tA7vnK e0iQ== X-Forwarded-Encrypted: i=1; AJvYcCXlP5NXbSfGP8JJmRvlisWQntp37Sp1E0hTUAsT1GfLfUWPpSZnvtIxfCGI4Ms0PHDfmk3onwk0FOVvTpr7@postgresql.org X-Gm-Message-State: AOJu0Ywkt40qpnVNSk49cP6oBm+W6YaM+xKcBNlLekLSF9WJ3B62ATfb FNNpQHM2M5zteGANfMaZ2UCfkU6Ny2nc2Ws209EChUwHyaG1yOLIwn5slH2YlA== X-Gm-Gg: AZuq6aLXz4LHCLapuU4bZVQ68w8rIkMUlEMgVSUa3fNxWfiOgnA5F5mur0LWxSIzqvS viwOWjBvF8j5uw68R4mBaefp/N9IOo48ACxE01csyeSxru0HHUegjMidaneKTqOBxtSldtzgsFb PjTb45s2LTZ8LcI53yQbGr5AXSco9nCq5uDbVviOlYSMwgRFwpwvGqCikpOjEFZ6VP+A+nMKUzB KP3Zmwag2/27bc10e2eJPbotKgoODm3EyAsHqU2ysX04J5f3Ec66OeSp8NQRJMj04tDpdzK0DZe oBGeRLWNmEn7Qklisu1ufOzmVc7acWfU4A6lQFwLxEDCE8enRmig4eVFDRESryCBEb7cEM58CXB 5T5uANCeZ00hl56ngsJZCosMsvNxAm5bzMqaQswqGKlztgFfNhl/7t28pL4vU4CBwLZgnQT4u/K x0BUKT6tALV2l7MxRM8MjTOJTYZP7adcXuG5zizESqfNCJMtMxf2H3qZ/t4wMoSFzUIHOyg0P/D bEQoCVV52ckiKs3LJHKcxNe/Ak= X-Received: by 2002:a05:6830:6d4e:b0:7cf:dc2d:8508 with SMTP id 46e09a7af769-7d4997f0163mr2177065a34.1.1770763720503; Tue, 10 Feb 2026 14:48:40 -0800 (PST) Received: from nathan (162-195-168-172.lightspeed.stlsmo.sbcglobal.net. [162.195.168.172]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-7d470914a1bsm8365286a34.27.2026.02.10.14.48.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Feb 2026 14:48:39 -0800 (PST) Date: Tue, 10 Feb 2026 16:48:37 -0600 From: Nathan Bossart To: Zsolt Parragi Cc: Chao Li , Euler Taveira , Peter Eisentraut , Gilles Darold , japin , Yuefei Shi , songjinzhou , pgsql-hackers , Andrew Dunstan , Tom Lane , liu xiaohui , Steven Niu Subject: Re: Pasword expiration warning Message-ID: References: <1f4d0b80-3fea-4f4d-8d33-a652ea23e90b@eisentraut.org> <5798afb7-b10c-436f-987e-14fb625df7d0@app.fastmail.com> <6E83A384-89B0-4141-887F-E54C02B2CACE@gmail.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="i0TBiZztyZNae5M6" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --i0TBiZztyZNae5M6 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Here's what I'm planning to commit tomorrow. -- nathan --i0TBiZztyZNae5M6 Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v17-0001-Add-password-expiration-warnings.patch From a9ecf3aad93146d4b850ecf35cc0005cf9347351 Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Tue, 10 Feb 2026 15:08:16 -0600 Subject: [PATCH v17 1/1] Add password expiration warnings. This commit adds a new parameter called password_expiration_warning_threshold that controls when the server begins emitting imminent-password-expiration warnings upon successful password authentication. By default, this parameter is set to 7 days, but this functionality can be disabled by setting it to 0. This patch also introduces a new "connection warning" infrastructure that can be reused elsewhere. For example, we may want to warn about the use of MD5 passwords for a couple of releases before removing MD5 password support. Author: Gilles Darold Co-authored-by: Nathan Bossart Reviewed-by: Japin Li Reviewed-by: songjinzhou Reviewed-by: liu xiaohui Reviewed-by: Yuefei Shi Reviewed-by: Steven Niu Reviewed-by: Soumya S Murali Reviewed-by: Euler Taveira Reviewed-by: Zsolt Parragi Reviewed-by: Chao Li Reviewed-by: Greg Sabino Mullane Reviewed-by: Peter Eisentraut Discussion: https://postgr.es/m/129bcfbf-47a6-e58a-190a-62fc21a17d03%40migops.com --- doc/src/sgml/config.sgml | 22 ++++++ src/backend/libpq/crypt.c | 72 +++++++++++++++++-- src/backend/utils/init/postinit.c | 69 ++++++++++++++++++ src/backend/utils/misc/guc_parameters.dat | 10 +++ src/backend/utils/misc/postgresql.conf.sample | 3 +- src/include/libpq/crypt.h | 3 + src/include/miscadmin.h | 1 + src/test/authentication/t/001_password.pl | 34 +++++++++ 8 files changed, 208 insertions(+), 6 deletions(-) diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml index 37342986969..a4170e3770b 100644 --- a/doc/src/sgml/config.sgml +++ b/doc/src/sgml/config.sgml @@ -1157,6 +1157,28 @@ include_dir 'conf.d' + + password_expiration_warning_threshold (integer) + + password_expiration_warning_threshold configuration parameter + + + + + When this parameter is greater than zero, the server will emit a + WARNING upon successful password authentication if + less than this amount of time remains until the authenticated role's + password expires. Note that a role's password only expires if a date + was specified in a VALID UNTIL clause for + CREATE ROLE or ALTER ROLE. If + this value is specified without units, it is taken as seconds. The + default is 7 days. This parameter can only be set in the + postgresql.conf file or on the server command + line. + + + + md5_password_warnings (boolean) diff --git a/src/backend/libpq/crypt.c b/src/backend/libpq/crypt.c index 52722060451..1a1e30c310f 100644 --- a/src/backend/libpq/crypt.c +++ b/src/backend/libpq/crypt.c @@ -20,10 +20,15 @@ #include "common/scram-common.h" #include "libpq/crypt.h" #include "libpq/scram.h" +#include "miscadmin.h" #include "utils/builtins.h" +#include "utils/memutils.h" #include "utils/syscache.h" #include "utils/timestamp.h" +/* Threshold for password expiration warnings. */ +int password_expiration_warning_threshold = 604800; + /* Enables deprecation warnings for MD5 passwords. */ bool md5_password_warnings = true; @@ -71,13 +76,70 @@ get_role_password(const char *role, const char **logdetail) ReleaseSysCache(roleTup); /* - * Password OK, but check to be sure we are not past rolvaliduntil + * Password OK, but check to be sure we are not past rolvaliduntil or + * password_expiration_warning_threshold. */ - if (!isnull && vuntil < GetCurrentTimestamp()) + if (!isnull) { - *logdetail = psprintf(_("User \"%s\" has an expired password."), - role); - return NULL; + int64 expire_time = vuntil - GetCurrentTimestamp(); + + /* + * If we're past rolvaliduntil, the connection attempt should fail, so + * update logdetail and return NULL. + */ + if (expire_time < 0) + { + *logdetail = psprintf(_("User \"%s\" has an expired password."), + role); + return NULL; + } + + /* + * If we're past the warning threshold, the connection attempt should + * succeed, but we still want to emit a warning. To do so, we queue + * the warning message using StoreConnectionWarning() so that it will + * be emitted at the end of InitPostgres(), and we return normally. + */ + if (expire_time / USECS_PER_SEC < password_expiration_warning_threshold) + { + MemoryContext oldcontext; + int days; + int hours; + int minutes; + char *warning; + char *detail; + + oldcontext = MemoryContextSwitchTo(TopMemoryContext); + + days = expire_time / USECS_PER_DAY; + hours = (expire_time % USECS_PER_DAY) / USECS_PER_HOUR; + minutes = (expire_time % USECS_PER_HOUR) / USECS_PER_MINUTE; + + warning = pstrdup(_("role password will expire soon")); + + if (days > 0) + detail = psprintf(ngettext("The password for role \"%s\" will expire in %d day.", + "The password for role \"%s\" will expire in %d days.", + days), + role, days); + else if (hours > 0) + detail = psprintf(ngettext("The password for role \"%s\" will expire in %d hour.", + "The password for role \"%s\" will expire in %d hours.", + hours), + role, hours); + else if (minutes > 0) + detail = psprintf(ngettext("The password for role \"%s\" will expire in %d minute.", + "The password for role \"%s\" will expire in %d minutes.", + minutes), + role, minutes); + else + detail = psprintf(_("The password for role \"%s\" will expire in less than 1 minute."), + role); + + StoreConnectionWarning(warning, detail); + + MemoryContextSwitchTo(oldcontext); + } } return shadow_pass; diff --git a/src/backend/utils/init/postinit.c b/src/backend/utils/init/postinit.c index 3f401faf3de..363af4c762e 100644 --- a/src/backend/utils/init/postinit.c +++ b/src/backend/utils/init/postinit.c @@ -70,6 +70,13 @@ #include "utils/syscache.h" #include "utils/timeout.h" +/* has this backend called EmitConnectionWarnings()? */ +static bool ConnectionWarningsEmitted; + +/* content of warnings to send upon successful pasword authentication */ +static List *ConnectionWarningMessages; +static List *ConnectionWarningDetails; + static HeapTuple GetDatabaseTuple(const char *dbname); static HeapTuple GetDatabaseTupleByOid(Oid dboid); static void PerformAuthentication(Port *port); @@ -85,6 +92,7 @@ static void ClientCheckTimeoutHandler(void); static bool ThereIsAtLeastOneRole(void); static void process_startup_options(Port *port, bool am_superuser); static void process_settings(Oid databaseid, Oid roleid); +static void EmitConnectionWarnings(void); /*** InitPostgres support ***/ @@ -987,6 +995,9 @@ InitPostgres(const char *in_dbname, Oid dboid, /* close the transaction we started above */ CommitTransactionCommand(); + /* send any WARNINGs we've accumulated during initialization */ + EmitConnectionWarnings(); + return; } @@ -1232,6 +1243,9 @@ InitPostgres(const char *in_dbname, Oid dboid, /* close the transaction we started above */ if (!bootstrap) CommitTransactionCommand(); + + /* send any WARNINGs we've accumulated during initialization */ + EmitConnectionWarnings(); } /* @@ -1446,3 +1460,58 @@ ThereIsAtLeastOneRole(void) return result; } + +/* + * Stores a warning message to be sent later via EmitConnectionWarnings(). + * Both msg and detail must be non-NULL. + * + * NB: Caller should ensure the strings are allocated in a long-lived context + * like TopMemoryContext. + */ +void +StoreConnectionWarning(char *msg, char *detail) +{ + MemoryContext oldcontext; + + Assert(msg); + Assert(detail); + + if (ConnectionWarningsEmitted) + elog(ERROR, "StoreConnectionWarning() called after EmitConnectionWarnings()"); + + oldcontext = MemoryContextSwitchTo(TopMemoryContext); + + ConnectionWarningMessages = lappend(ConnectionWarningMessages, msg); + ConnectionWarningDetails = lappend(ConnectionWarningDetails, detail); + + MemoryContextSwitchTo(oldcontext); +} + +/* + * Sends the warning messages saved via StoreConnectionWarning() and frees the + * strings and lists. + * + * NB: This can only be called once per backend. + */ +static void +EmitConnectionWarnings(void) +{ + ListCell *lc_msg; + ListCell *lc_detail; + + if (ConnectionWarningsEmitted) + elog(ERROR, "EmitConnectionWarnings() called more than once"); + else + ConnectionWarningsEmitted = true; + + forboth(lc_msg, ConnectionWarningMessages, + lc_detail, ConnectionWarningDetails) + { + ereport(WARNING, + (errmsg("%s", (char *) lfirst(lc_msg)), + errdetail("%s", (char *) lfirst(lc_detail)))); + } + + list_free_deep(ConnectionWarningMessages); + list_free_deep(ConnectionWarningDetails); +} diff --git a/src/backend/utils/misc/guc_parameters.dat b/src/backend/utils/misc/guc_parameters.dat index 762b8efe6b0..2037b8f7c35 100644 --- a/src/backend/utils/misc/guc_parameters.dat +++ b/src/backend/utils/misc/guc_parameters.dat @@ -2251,6 +2251,16 @@ options => 'password_encryption_options', }, +{ name => 'password_expiration_warning_threshold', type => 'int', context => 'PGC_SIGHUP', group => 'CONN_AUTH_AUTH', + short_desc => 'Threshold for password expiration warnings.', + long_desc => '0 means not to emit these warnings.', + flags => 'GUC_UNIT_S', + variable => 'password_expiration_warning_threshold', + boot_val => '604800', + min => '0', + max => 'INT_MAX', +}, + { name => 'plan_cache_mode', type => 'enum', context => 'PGC_USERSET', group => 'QUERY_TUNING_OTHER', short_desc => 'Controls the planner\'s selection of custom or generic plan.', long_desc => 'Prepared statements can have custom and generic plans, and the planner will attempt to choose which is better. This can be set to override the default behavior.', diff --git a/src/backend/utils/misc/postgresql.conf.sample b/src/backend/utils/misc/postgresql.conf.sample index 6e82c8e055d..7d4f77ba483 100644 --- a/src/backend/utils/misc/postgresql.conf.sample +++ b/src/backend/utils/misc/postgresql.conf.sample @@ -96,7 +96,8 @@ #authentication_timeout = 1min # 1s-600s #password_encryption = scram-sha-256 # scram-sha-256 or (deprecated) md5 #scram_iterations = 4096 -#md5_password_warnings = on # display md5 deprecation warnings? +#password_expiration_warning_threshold = 7d # threshold for expiration warnings +#md5_password_warnings = on # display md5 deprecation warnings? #oauth_validator_libraries = '' # comma-separated list of trusted validator modules # GSSAPI using Kerberos diff --git a/src/include/libpq/crypt.h b/src/include/libpq/crypt.h index f01886e1098..ebef0d0f78c 100644 --- a/src/include/libpq/crypt.h +++ b/src/include/libpq/crypt.h @@ -25,6 +25,9 @@ */ #define MAX_ENCRYPTED_PASSWORD_LEN (512) +/* Threshold for password expiration warnings. */ +extern PGDLLIMPORT int password_expiration_warning_threshold; + /* Enables deprecation warnings for MD5 passwords. */ extern PGDLLIMPORT bool md5_password_warnings; diff --git a/src/include/miscadmin.h b/src/include/miscadmin.h index db559b39c4d..f16f35659b9 100644 --- a/src/include/miscadmin.h +++ b/src/include/miscadmin.h @@ -507,6 +507,7 @@ extern void InitPostgres(const char *in_dbname, Oid dboid, bits32 flags, char *out_dbname); extern void BaseInit(void); +extern void StoreConnectionWarning(char *msg, char *detail); /* in utils/init/miscinit.c */ extern PGDLLIMPORT bool IgnoreSystemIndexes; diff --git a/src/test/authentication/t/001_password.pl b/src/test/authentication/t/001_password.pl index f4d65ba7bae..0ec9aa9f4e8 100644 --- a/src/test/authentication/t/001_password.pl +++ b/src/test/authentication/t/001_password.pl @@ -68,8 +68,24 @@ $node->init; $node->append_conf('postgresql.conf', "log_connections = on\n"); # Needed to allow connect_fails to inspect postmaster log: $node->append_conf('postgresql.conf', "log_min_messages = debug2"); +$node->append_conf('postgresql.conf', "password_expiration_warning_threshold = '1100d'"); $node->start; +# Set up roles for password_expiration_warning_threshold test +my $current_year = 1900 + ${ [ localtime(time) ] }[5]; +my $expire_year = $current_year - 1; +$node->safe_psql( + 'postgres', + "CREATE ROLE expired LOGIN VALID UNTIL '$expire_year-01-01' PASSWORD 'pass'"); +$expire_year = $current_year + 2; +$node->safe_psql( + 'postgres', + "CREATE ROLE expiration_warnings LOGIN VALID UNTIL '$expire_year-01-01' PASSWORD 'pass'"); +$expire_year = $current_year + 5; +$node->safe_psql( + 'postgres', + "CREATE ROLE no_warnings LOGIN VALID UNTIL '$expire_year-01-01' PASSWORD 'pass'"); + # Test behavior of log_connections GUC # # There wasn't another test file where these tests obviously fit, and we don't @@ -531,6 +547,24 @@ $node->connect_fails( qr/authentication method requirement "!password,!md5,!scram-sha-256" failed: server requested SCRAM-SHA-256 authentication/ ); +# Test password_expiration_warning_threshold +$node->connect_fails( + "user=expired dbname=postgres", + "connection fails due to expired password", + expected_stderr => + qr/password authentication failed for user "expired"/ +); +$node->connect_ok( + "user=expiration_warnings dbname=postgres", + "connection succeeds with password expiration warning", + expected_stderr => + qr/role password will expire soon/ +); +$node->connect_ok( + "user=no_warnings dbname=postgres", + "connection succeeds with no password expiration warning" +); + # Test SYSTEM_USER <> NULL with parallel workers. $node->safe_psql( 'postgres', -- 2.50.1 (Apple Git-155) --i0TBiZztyZNae5M6--