@@ -44,7 +44,7 @@ Oid binary_upgrade_next_pg_authid_oid = InvalidOid;
44
44
45
45
46
46
/* GUC parameter */
47
- int Password_encryption = PASSWORD_TYPE_MD5 ;
47
+ extern bool Password_encryption ;
48
48
49
49
/* Hook to check passwords in CreateRole() and AlterRole() */
50
50
check_password_hook_type check_password_hook = NULL ;
@@ -55,8 +55,6 @@ 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 );
60
58
61
59
62
60
/* Check if current user has createrole privileges */
@@ -66,48 +64,6 @@ have_createrole_privilege(void)
66
64
return has_createrole_privilege (GetUserId ());
67
65
}
68
66
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
- }
111
67
112
68
/*
113
69
* CREATE ROLE
@@ -124,8 +80,8 @@ CreateRole(ParseState *pstate, CreateRoleStmt *stmt)
124
80
ListCell * item ;
125
81
ListCell * option ;
126
82
char * password = NULL ; /* user password */
127
- int password_type = Password_encryption ; /* encrypt password? */
128
- char * encrypted_passwd ;
83
+ bool encrypt_password = Password_encryption ; /* encrypt password? */
84
+ char encrypted_password [ MD5_PASSWD_LEN + 1 ] ;
129
85
bool issuper = false; /* Make the user a superuser? */
130
86
bool inherit = true; /* Auto inherit privileges? */
131
87
bool createrole = false; /* Can this user create roles? */
@@ -184,9 +140,9 @@ CreateRole(ParseState *pstate, CreateRoleStmt *stmt)
184
140
parser_errposition (pstate , defel -> location )));
185
141
dpassword = defel ;
186
142
if (strcmp (defel -> defname , "encryptedPassword" ) == 0 )
187
- password_type = PASSWORD_TYPE_MD5 ;
143
+ encrypt_password = true ;
188
144
else if (strcmp (defel -> defname , "unencryptedPassword" ) == 0 )
189
- password_type = PASSWORD_TYPE_PLAINTEXT ;
145
+ encrypt_password = false ;
190
146
}
191
147
else if (strcmp (defel -> defname , "sysid" ) == 0 )
192
148
{
@@ -437,13 +393,17 @@ CreateRole(ParseState *pstate, CreateRoleStmt *stmt)
437
393
438
394
if (password )
439
395
{
440
- encrypted_passwd = encrypt_password (password ,
441
- stmt -> role ,
442
- password_type );
443
-
444
- new_record [Anum_pg_authid_rolpassword - 1 ] =
445
- CStringGetTextDatum (encrypted_passwd );
446
- pfree (encrypted_passwd );
396
+ if (!encrypt_password || isMD5 (password ))
397
+ new_record [Anum_pg_authid_rolpassword - 1 ] =
398
+ CStringGetTextDatum (password );
399
+ else
400
+ {
401
+ if (!pg_md5_encrypt (password , stmt -> role , strlen (stmt -> role ),
402
+ encrypted_password ))
403
+ elog (ERROR , "password encryption failed" );
404
+ new_record [Anum_pg_authid_rolpassword - 1 ] =
405
+ CStringGetTextDatum (encrypted_password );
406
+ }
447
407
}
448
408
else
449
409
new_record_nulls [Anum_pg_authid_rolpassword - 1 ] = true;
@@ -545,8 +505,8 @@ AlterRole(AlterRoleStmt *stmt)
545
505
ListCell * option ;
546
506
char * rolename = NULL ;
547
507
char * password = NULL ; /* user password */
548
- int password_type = Password_encryption ; /* encrypt password? */
549
- char * encrypted_passwd ;
508
+ bool encrypt_password = Password_encryption ; /* encrypt password? */
509
+ char encrypted_password [ MD5_PASSWD_LEN + 1 ] ;
550
510
int issuper = -1 ; /* Make the user a superuser? */
551
511
int inherit = -1 ; /* Auto inherit privileges? */
552
512
int createrole = -1 ; /* Can this user create roles? */
@@ -590,9 +550,9 @@ AlterRole(AlterRoleStmt *stmt)
590
550
errmsg ("conflicting or redundant options" )));
591
551
dpassword = defel ;
592
552
if (strcmp (defel -> defname , "encryptedPassword" ) == 0 )
593
- password_type = PASSWORD_TYPE_MD5 ;
553
+ encrypt_password = true ;
594
554
else if (strcmp (defel -> defname , "unencryptedPassword" ) == 0 )
595
- password_type = PASSWORD_TYPE_PLAINTEXT ;
555
+ encrypt_password = false ;
596
556
}
597
557
else if (strcmp (defel -> defname , "superuser" ) == 0 )
598
558
{
@@ -844,14 +804,18 @@ AlterRole(AlterRoleStmt *stmt)
844
804
/* password */
845
805
if (password )
846
806
{
847
- encrypted_passwd = encrypt_password (password ,
848
- rolename ,
849
- password_type );
850
-
851
- new_record [Anum_pg_authid_rolpassword - 1 ] =
852
- CStringGetTextDatum (encrypted_passwd );
807
+ if (!encrypt_password || isMD5 (password ))
808
+ new_record [Anum_pg_authid_rolpassword - 1 ] =
809
+ CStringGetTextDatum (password );
810
+ else
811
+ {
812
+ if (!pg_md5_encrypt (password , rolename , strlen (rolename ),
813
+ encrypted_password ))
814
+ elog (ERROR , "password encryption failed" );
815
+ new_record [Anum_pg_authid_rolpassword - 1 ] =
816
+ CStringGetTextDatum (encrypted_password );
817
+ }
853
818
new_record_repl [Anum_pg_authid_rolpassword - 1 ] = true;
854
- pfree (encrypted_passwd );
855
819
}
856
820
857
821
/* unset password */
0 commit comments