@@ -44,7 +44,7 @@ Oid binary_upgrade_next_pg_authid_oid = InvalidOid;
44
44
45
45
46
46
/* GUC parameter */
47
- extern bool Password_encryption ;
47
+ int Password_encryption = PASSWORD_TYPE_MD5 ;
48
48
49
49
/* Hook to check passwords in CreateRole() and AlterRole() */
50
50
check_password_hook_type check_password_hook = NULL ;
@@ -55,6 +55,8 @@ static void AddRoleMems(const char *rolename, Oid roleid,
55
55
static void DelRoleMems (const char * rolename , Oid roleid ,
56
56
List * memberSpecs , List * memberIds ,
57
57
bool admin_opt );
58
+ static char * encrypt_password (char * passwd , char * rolname ,
59
+ int passwd_type );
58
60
59
61
60
62
/* Check if current user has createrole privileges */
@@ -64,6 +66,48 @@ have_createrole_privilege(void)
64
66
return has_createrole_privilege (GetUserId ());
65
67
}
66
68
69
+ /*
70
+ * Encrypt a password if necessary for insertion in pg_authid.
71
+ *
72
+ * If a password is found as already MD5-encrypted, no error is raised
73
+ * to ease the dump and reload of such data. Returns a palloc'ed string
74
+ * holding the encrypted password.
75
+ */
76
+ static char *
77
+ encrypt_password (char * password , char * rolname , int passwd_type )
78
+ {
79
+ char * res ;
80
+
81
+ Assert (password != NULL );
82
+
83
+ /*
84
+ * If a password is already identified as MD5-encrypted, it is used
85
+ * as such. If the password given is not encrypted, adapt it depending
86
+ * on the type wanted by the caller of this routine.
87
+ */
88
+ if (isMD5 (password ))
89
+ res = pstrdup (password );
90
+ else
91
+ {
92
+ switch (passwd_type )
93
+ {
94
+ case PASSWORD_TYPE_PLAINTEXT :
95
+ res = pstrdup (password );
96
+ break ;
97
+ case PASSWORD_TYPE_MD5 :
98
+ res = (char * ) palloc (MD5_PASSWD_LEN + 1 );
99
+ if (!pg_md5_encrypt (password , rolname ,
100
+ strlen (rolname ),
101
+ res ))
102
+ elog (ERROR , "password encryption failed" );
103
+ break ;
104
+ default :
105
+ Assert (0 ); /* should not come here */
106
+ }
107
+ }
108
+
109
+ return res ;
110
+ }
67
111
68
112
/*
69
113
* CREATE ROLE
@@ -80,8 +124,8 @@ CreateRole(CreateRoleStmt *stmt)
80
124
ListCell * item ;
81
125
ListCell * option ;
82
126
char * password = NULL ; /* user password */
83
- bool encrypt_password = Password_encryption ; /* encrypt password? */
84
- char encrypted_password [ MD5_PASSWD_LEN + 1 ] ;
127
+ int password_type = Password_encryption ; /* encrypt password? */
128
+ char * encrypted_passwd ;
85
129
bool issuper = false; /* Make the user a superuser? */
86
130
bool inherit = true; /* Auto inherit privileges? */
87
131
bool createrole = false; /* Can this user create roles? */
@@ -139,9 +183,9 @@ CreateRole(CreateRoleStmt *stmt)
139
183
errmsg ("conflicting or redundant options" )));
140
184
dpassword = defel ;
141
185
if (strcmp (defel -> defname , "encryptedPassword" ) == 0 )
142
- encrypt_password = true ;
186
+ password_type = PASSWORD_TYPE_MD5 ;
143
187
else if (strcmp (defel -> defname , "unencryptedPassword" ) == 0 )
144
- encrypt_password = false ;
188
+ password_type = PASSWORD_TYPE_PLAINTEXT ;
145
189
}
146
190
else if (strcmp (defel -> defname , "sysid" ) == 0 )
147
191
{
@@ -380,17 +424,13 @@ CreateRole(CreateRoleStmt *stmt)
380
424
381
425
if (password )
382
426
{
383
- if (!encrypt_password || isMD5 (password ))
384
- new_record [Anum_pg_authid_rolpassword - 1 ] =
385
- CStringGetTextDatum (password );
386
- else
387
- {
388
- if (!pg_md5_encrypt (password , stmt -> role , strlen (stmt -> role ),
389
- encrypted_password ))
390
- elog (ERROR , "password encryption failed" );
391
- new_record [Anum_pg_authid_rolpassword - 1 ] =
392
- CStringGetTextDatum (encrypted_password );
393
- }
427
+ encrypted_passwd = encrypt_password (password ,
428
+ stmt -> role ,
429
+ password_type );
430
+
431
+ new_record [Anum_pg_authid_rolpassword - 1 ] =
432
+ CStringGetTextDatum (encrypted_passwd );
433
+ pfree (encrypted_passwd );
394
434
}
395
435
else
396
436
new_record_nulls [Anum_pg_authid_rolpassword - 1 ] = true;
@@ -492,8 +532,8 @@ AlterRole(AlterRoleStmt *stmt)
492
532
ListCell * option ;
493
533
char * rolename = NULL ;
494
534
char * password = NULL ; /* user password */
495
- bool encrypt_password = Password_encryption ; /* encrypt password? */
496
- char encrypted_password [ MD5_PASSWD_LEN + 1 ] ;
535
+ int password_type = Password_encryption ; /* encrypt password? */
536
+ char * encrypted_passwd ;
497
537
int issuper = -1 ; /* Make the user a superuser? */
498
538
int inherit = -1 ; /* Auto inherit privileges? */
499
539
int createrole = -1 ; /* Can this user create roles? */
@@ -537,9 +577,9 @@ AlterRole(AlterRoleStmt *stmt)
537
577
errmsg ("conflicting or redundant options" )));
538
578
dpassword = defel ;
539
579
if (strcmp (defel -> defname , "encryptedPassword" ) == 0 )
540
- encrypt_password = true ;
580
+ password_type = PASSWORD_TYPE_MD5 ;
541
581
else if (strcmp (defel -> defname , "unencryptedPassword" ) == 0 )
542
- encrypt_password = false ;
582
+ password_type = PASSWORD_TYPE_PLAINTEXT ;
543
583
}
544
584
else if (strcmp (defel -> defname , "superuser" ) == 0 )
545
585
{
@@ -791,18 +831,14 @@ AlterRole(AlterRoleStmt *stmt)
791
831
/* password */
792
832
if (password )
793
833
{
794
- if (!encrypt_password || isMD5 (password ))
795
- new_record [Anum_pg_authid_rolpassword - 1 ] =
796
- CStringGetTextDatum (password );
797
- else
798
- {
799
- if (!pg_md5_encrypt (password , rolename , strlen (rolename ),
800
- encrypted_password ))
801
- elog (ERROR , "password encryption failed" );
802
- new_record [Anum_pg_authid_rolpassword - 1 ] =
803
- CStringGetTextDatum (encrypted_password );
804
- }
834
+ encrypted_passwd = encrypt_password (password ,
835
+ rolename ,
836
+ password_type );
837
+
838
+ new_record [Anum_pg_authid_rolpassword - 1 ] =
839
+ CStringGetTextDatum (encrypted_passwd );
805
840
new_record_repl [Anum_pg_authid_rolpassword - 1 ] = true;
841
+ pfree (encrypted_passwd );
806
842
}
807
843
808
844
/* unset password */
0 commit comments