Skip to content

Commit c7bbe99

Browse files
committed
Fix ALTER DATABASE RENAME to allow the operation if user is a superuser
who for some reason isn't marked usecreatedb. Per report from Alexander Pravking. Also fix sloppy coding in have_createdb_privilege().
1 parent fa5e440 commit c7bbe99

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

src/backend/commands/dbcommands.c

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*
1616
*
1717
* IDENTIFICATION
18-
* $PostgreSQL: pgsql/src/backend/commands/dbcommands.c,v 1.152 2005/03/04 20:21:05 tgl Exp $
18+
* $PostgreSQL: pgsql/src/backend/commands/dbcommands.c,v 1.153 2005/03/12 21:11:50 tgl Exp $
1919
*
2020
*-------------------------------------------------------------------------
2121
*/
@@ -722,8 +722,8 @@ RenameDatabase(const char *oldname, const char *newname)
722722
aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_DATABASE,
723723
oldname);
724724

725-
/* must have createdb */
726-
if (!have_createdb_privilege())
725+
/* must have createdb rights */
726+
if (!superuser() && !have_createdb_privilege())
727727
ereport(ERROR,
728728
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
729729
errmsg("permission denied to rename database")));
@@ -883,8 +883,7 @@ AlterDatabaseOwner(const char *dbname, AclId newOwnerSysId)
883883
bool isNull;
884884
HeapTuple newtuple;
885885

886-
/* changing owner's database for someone else: must be superuser */
887-
/* note that the someone else need not have any permissions */
886+
/* must be superuser to change ownership */
888887
if (!superuser())
889888
ereport(ERROR,
890889
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
@@ -999,24 +998,22 @@ get_db_info(const char *name, Oid *dbIdP, int4 *ownerIdP,
999998
return gottuple;
1000999
}
10011000

1001+
/* Check if current user has createdb privileges */
10021002
static bool
10031003
have_createdb_privilege(void)
10041004
{
1005+
bool result = false;
10051006
HeapTuple utup;
1006-
bool retval;
10071007

10081008
utup = SearchSysCache(SHADOWSYSID,
10091009
Int32GetDatum(GetUserId()),
10101010
0, 0, 0);
1011-
1012-
if (!HeapTupleIsValid(utup))
1013-
retval = false;
1014-
else
1015-
retval = ((Form_pg_shadow) GETSTRUCT(utup))->usecreatedb;
1016-
1017-
ReleaseSysCache(utup);
1018-
1019-
return retval;
1011+
if (HeapTupleIsValid(utup))
1012+
{
1013+
result = ((Form_pg_shadow) GETSTRUCT(utup))->usecreatedb;
1014+
ReleaseSysCache(utup);
1015+
}
1016+
return result;
10201017
}
10211018

10221019
/*

0 commit comments

Comments
 (0)