6
6
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
7
7
* Portions Copyright (c) 1994, Regents of the University of California
8
8
*
9
- * $PostgreSQL: pgsql/src/backend/commands/user.c,v 1.139 2004/03/16 05:05:57 momjian Exp $
9
+ * $PostgreSQL: pgsql/src/backend/commands/user.c,v 1.140 2004/05/06 16:59:16 momjian Exp $
10
10
*
11
11
*-------------------------------------------------------------------------
12
12
*/
@@ -959,8 +959,8 @@ AlterUserSet(AlterUserSetStmt *stmt)
959
959
(errcode (ERRCODE_UNDEFINED_OBJECT ),
960
960
errmsg ("user \"%s\" does not exist" , stmt -> user )));
961
961
962
- if (!(superuser ()
963
- || ((Form_pg_shadow ) GETSTRUCT (oldtuple ))-> usesysid == GetUserId ()))
962
+ if (!(superuser () ||
963
+ ((Form_pg_shadow ) GETSTRUCT (oldtuple ))-> usesysid == GetUserId ()))
964
964
ereport (ERROR ,
965
965
(errcode (ERRCODE_INSUFFICIENT_PRIVILEGE ),
966
966
errmsg ("permission denied" )));
@@ -1157,16 +1157,25 @@ DropUser(DropUserStmt *stmt)
1157
1157
void
1158
1158
RenameUser (const char * oldname , const char * newname )
1159
1159
{
1160
- HeapTuple tup ;
1160
+ HeapTuple oldtuple ,
1161
+ newtuple ;
1162
+ TupleDesc dsc ;
1161
1163
Relation rel ;
1162
-
1164
+ Datum datum ;
1165
+ bool isnull ;
1166
+ Datum repl_val [Natts_pg_shadow ];
1167
+ char repl_null [Natts_pg_shadow ];
1168
+ char repl_repl [Natts_pg_shadow ];
1169
+ int i ;
1170
+
1163
1171
/* ExclusiveLock because we need to update the password file */
1164
1172
rel = heap_openr (ShadowRelationName , ExclusiveLock );
1173
+ dsc = RelationGetDescr (rel );
1165
1174
1166
- tup = SearchSysCacheCopy (SHADOWNAME ,
1175
+ oldtuple = SearchSysCache (SHADOWNAME ,
1167
1176
CStringGetDatum (oldname ),
1168
1177
0 , 0 , 0 );
1169
- if (!HeapTupleIsValid (tup ))
1178
+ if (!HeapTupleIsValid (oldtuple ))
1170
1179
ereport (ERROR ,
1171
1180
(errcode (ERRCODE_UNDEFINED_OBJECT ),
1172
1181
errmsg ("user \"%s\" does not exist" , oldname )));
@@ -1177,7 +1186,7 @@ RenameUser(const char *oldname, const char *newname)
1177
1186
* not be an actual problem besides a little confusion, so think about
1178
1187
* this and decide.
1179
1188
*/
1180
- if (((Form_pg_shadow ) GETSTRUCT (tup ))-> usesysid == GetSessionUserId ())
1189
+ if (((Form_pg_shadow ) GETSTRUCT (oldtuple ))-> usesysid == GetSessionUserId ())
1181
1190
ereport (ERROR ,
1182
1191
(errcode (ERRCODE_FEATURE_NOT_SUPPORTED ),
1183
1192
errmsg ("session user may not be renamed" )));
@@ -1196,13 +1205,33 @@ RenameUser(const char *oldname, const char *newname)
1196
1205
(errcode (ERRCODE_INSUFFICIENT_PRIVILEGE ),
1197
1206
errmsg ("must be superuser to rename users" )));
1198
1207
1199
- /* rename */
1200
- namestrcpy (& (((Form_pg_shadow ) GETSTRUCT (tup ))-> usename ), newname );
1201
- simple_heap_update (rel , & tup -> t_self , tup );
1202
- CatalogUpdateIndexes (rel , tup );
1208
+ for (i = 0 ; i < Natts_pg_shadow ; i ++ )
1209
+ repl_repl [i ] = ' ' ;
1210
+
1211
+ repl_repl [Anum_pg_shadow_usename - 1 ] = 'r' ;
1212
+ repl_val [Anum_pg_shadow_usename - 1 ] = DirectFunctionCall1 (namein ,
1213
+ CStringGetDatum (newname ));
1214
+ repl_null [Anum_pg_shadow_usename - 1 ] = ' ' ;
1203
1215
1216
+ datum = heap_getattr (oldtuple , Anum_pg_shadow_passwd , dsc , & isnull );
1217
+
1218
+ if (!isnull && isMD5 (DatumGetCString (DirectFunctionCall1 (textout , datum ))))
1219
+ {
1220
+ /* MD5 uses the username as salt, so just clear it on a rename */
1221
+ repl_repl [Anum_pg_shadow_passwd - 1 ] = 'r' ;
1222
+ repl_null [Anum_pg_shadow_passwd - 1 ] = 'n' ;
1223
+
1224
+ ereport (NOTICE ,
1225
+ (errmsg ("MD5 password cleared because of user rename" )));
1226
+ }
1227
+
1228
+ newtuple = heap_modifytuple (oldtuple , rel , repl_val , repl_null , repl_repl );
1229
+ simple_heap_update (rel , & oldtuple -> t_self , newtuple );
1230
+
1231
+ CatalogUpdateIndexes (rel , newtuple );
1232
+
1233
+ ReleaseSysCache (oldtuple );
1204
1234
heap_close (rel , NoLock );
1205
- heap_freetuple (tup );
1206
1235
1207
1236
user_file_update_needed = true;
1208
1237
}
0 commit comments