@@ -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
@@ -125,7 +81,7 @@ CreateRole(CreateRoleStmt *stmt)
125
81
ListCell * option ;
126
82
char * password = NULL ; /* user password */
127
83
int password_type = Password_encryption ;
128
- char * encrypted_passwd ;
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? */
@@ -424,13 +380,17 @@ CreateRole(CreateRoleStmt *stmt)
424
380
425
381
if (password )
426
382
{
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 );
383
+ if (password_type == PASSWORD_TYPE_PLAINTEXT || 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
+ }
434
394
}
435
395
else
436
396
new_record_nulls [Anum_pg_authid_rolpassword - 1 ] = true;
@@ -533,7 +493,7 @@ AlterRole(AlterRoleStmt *stmt)
533
493
char * rolename = NULL ;
534
494
char * password = NULL ; /* user password */
535
495
int password_type = Password_encryption ;
536
- char * encrypted_passwd ;
496
+ char encrypted_password [ MD5_PASSWD_LEN + 1 ] ;
537
497
int issuper = -1 ; /* Make the user a superuser? */
538
498
int inherit = -1 ; /* Auto inherit privileges? */
539
499
int createrole = -1 ; /* Can this user create roles? */
@@ -831,14 +791,18 @@ AlterRole(AlterRoleStmt *stmt)
831
791
/* password */
832
792
if (password )
833
793
{
834
- encrypted_passwd = encrypt_password (password ,
835
- rolename ,
836
- password_type );
837
-
838
- new_record [Anum_pg_authid_rolpassword - 1 ] =
839
- CStringGetTextDatum (encrypted_passwd );
794
+ if (password_type == PASSWORD_TYPE_PLAINTEXT || 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
+ }
840
805
new_record_repl [Anum_pg_authid_rolpassword - 1 ] = true;
841
- pfree (encrypted_passwd );
842
806
}
843
807
844
808
/* unset password */
0 commit comments