Skip to content

Commit 9d227ef

Browse files
author
Commitfest Bot
committed
[CF 5792] v2 - pg_upgrade: warn about roles with md5 passwords
This branch was automatically generated by a robot using patches from an email thread registered at: https://commitfest.postgresql.org/patch/5792 The branch will be overwritten each time a new patch version is posted to the thread, and also periodically to check for bitrot caused by changes on the master branch. Patch(es): https://www.postgresql.org/message-id/aD8sXgfJeIGLc7-t@nathan Author(s): Nathan Bossart
2 parents 73bdcfa + 33b30b7 commit 9d227ef

File tree

5 files changed

+88
-0
lines changed

5 files changed

+88
-0
lines changed

src/backend/libpq/crypt.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
/* Enables deprecation warnings for MD5 passwords. */
2828
bool md5_password_warnings = true;
2929

30+
static bool used_md5_auth = false;
31+
3032
/*
3133
* Fetch stored password for a user, for authentication.
3234
*
@@ -210,6 +212,8 @@ md5_crypt_verify(const char *role, const char *shadow_pass,
210212

211213
Assert(md5_salt_len > 0);
212214

215+
used_md5_auth = true;
216+
213217
if (get_password_type(shadow_pass) != PASSWORD_TYPE_MD5)
214218
{
215219
/* incompatible password hash format. */
@@ -242,6 +246,19 @@ md5_crypt_verify(const char *role, const char *shadow_pass,
242246
return retval;
243247
}
244248

249+
void
250+
warn_if_md5_auth(void)
251+
{
252+
if (md5_password_warnings && used_md5_auth)
253+
ereport(WARNING,
254+
(errcode(ERRCODE_WARNING_DEPRECATED_FEATURE),
255+
errmsg("authenticated with an MD5-encrypted password"),
256+
errdetail("MD5 password support is deprecated and will be removed in a future release of PostgreSQL."),
257+
errhint("Refer to the PostgreSQL documentation for details about migrating to another password type.")));
258+
259+
used_md5_auth = false;
260+
}
261+
245262
/*
246263
* Check given password for given user, and return STATUS_OK or STATUS_ERROR.
247264
*

src/backend/utils/init/postinit.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "catalog/pg_db_role_setting.h"
3535
#include "catalog/pg_tablespace.h"
3636
#include "libpq/auth.h"
37+
#include "libpq/crypt.h"
3738
#include "libpq/libpq-be.h"
3839
#include "mb/pg_wchar.h"
3940
#include "miscadmin.h"
@@ -1234,6 +1235,8 @@ InitPostgres(const char *in_dbname, Oid dboid,
12341235
/* close the transaction we started above */
12351236
if (!bootstrap)
12361237
CommitTransactionCommand();
1238+
1239+
warn_if_md5_auth();
12371240
}
12381241

12391242
/*

src/bin/pg_upgrade/check.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ static void check_new_cluster_logical_replication_slots(void);
3131
static void check_new_cluster_subscription_configuration(void);
3232
static void check_old_cluster_for_valid_slots(void);
3333
static void check_old_cluster_subscription_state(void);
34+
static void check_for_md5_passwords(ClusterInfo *cluster);
3435

3536
/*
3637
* DataTypesUsageChecks - definitions of data type checks for the old cluster
@@ -685,6 +686,12 @@ check_and_dump_old_cluster(void)
685686
if (GET_MAJOR_VERSION(old_cluster.major_version) <= 905)
686687
check_for_pg_role_prefix(&old_cluster);
687688

689+
/*
690+
* MD5 password support is deprecated. Warn if any roles have MD5
691+
* passwords.
692+
*/
693+
check_for_md5_passwords(&old_cluster);
694+
688695
/*
689696
* While not a check option, we do this now because this is the only time
690697
* the old server is running.
@@ -2272,3 +2279,62 @@ check_old_cluster_subscription_state(void)
22722279
else
22732280
check_ok();
22742281
}
2282+
2283+
/*
2284+
* check_for_md5_passwords()
2285+
*
2286+
* As of v18, MD5 password support is marked as deprecated and to-be-removed in
2287+
* a future major release.
2288+
*/
2289+
static void
2290+
check_for_md5_passwords(ClusterInfo *cluster)
2291+
{
2292+
PGresult *res;
2293+
PGconn *conn = connectToServer(cluster, "template1");
2294+
int ntups;
2295+
int i_roloid;
2296+
int i_rolname;
2297+
FILE *script = NULL;
2298+
char output_path[MAXPGPATH];
2299+
2300+
prep_status("Checking for roles with MD5 passwords");
2301+
2302+
snprintf(output_path, sizeof(output_path), "%s/%s",
2303+
log_opts.basedir,
2304+
"roles_with_md5_passwords.txt");
2305+
2306+
res = executeQueryOrDie(conn,
2307+
"SELECT oid AS roloid, rolname "
2308+
"FROM pg_catalog.pg_authid "
2309+
"WHERE rolpassword ~ '^md5'");
2310+
2311+
ntups = PQntuples(res);
2312+
i_roloid = PQfnumber(res, "roloid");
2313+
i_rolname = PQfnumber(res, "rolname");
2314+
for (int rowno = 0; rowno < ntups; rowno++)
2315+
{
2316+
if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
2317+
pg_fatal("could not open file \"%s\": %m", output_path);
2318+
fprintf(script, "%s (oid=%s)\n",
2319+
PQgetvalue(res, rowno, i_rolname),
2320+
PQgetvalue(res, rowno, i_roloid));
2321+
}
2322+
2323+
PQclear(res);
2324+
2325+
PQfinish(conn);
2326+
2327+
if (script)
2328+
{
2329+
fclose(script);
2330+
report_status(PG_WARNING, "warning");
2331+
pg_log(PG_WARNING,
2332+
"Your installation contains roles with MD5 passwords.\n"
2333+
"Support for MD5-encrypted passwords is deprecated and will be\n"
2334+
"removed in a future release of PostgreSQL. A list of roles\n"
2335+
"with MD5 passwords is in the file:\n"
2336+
" %s", output_path);
2337+
}
2338+
else
2339+
check_ok();
2340+
}

src/include/libpq/crypt.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ extern char *get_role_password(const char *role, const char **logdetail);
5353
extern int md5_crypt_verify(const char *role, const char *shadow_pass,
5454
const char *client_pass, const uint8 *md5_salt,
5555
int md5_salt_len, const char **logdetail);
56+
extern void warn_if_md5_auth(void);
5657
extern int plain_crypt_verify(const char *role, const char *shadow_pass,
5758
const char *client_pass,
5859
const char **logdetail);

src/test/authentication/t/001_password.pl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ sub test_conn
6868
$node->append_conf('postgresql.conf', "log_connections = on\n");
6969
# Needed to allow connect_fails to inspect postmaster log:
7070
$node->append_conf('postgresql.conf', "log_min_messages = debug2");
71+
$node->append_conf('postgresql.conf', "md5_password_warnings = off");
7172
$node->start;
7273

7374
# Test behavior of log_connections GUC

0 commit comments

Comments
 (0)