Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit d6f8a76

Browse files
committed
Cause ALTER OWNER commands to update the object's ACL, replacing references
to the old owner with the new owner. This is not necessarily right, but it's sure a lot more likely to be what the user wants than doing nothing. Christopher Kings-Lynne, some rework by Tom Lane.
1 parent 35ff782 commit d6f8a76

File tree

7 files changed

+293
-45
lines changed

7 files changed

+293
-45
lines changed

src/backend/commands/dbcommands.c

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*
1111
* IDENTIFICATION
12-
* $PostgreSQL: pgsql/src/backend/commands/dbcommands.c,v 1.138 2004/08/01 06:19:22 momjian Exp $
12+
* $PostgreSQL: pgsql/src/backend/commands/dbcommands.c,v 1.139 2004/08/01 20:30:48 tgl Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -768,8 +768,7 @@ AlterDatabaseSet(AlterDatabaseSetStmt *stmt)
768768
void
769769
AlterDatabaseOwner(const char *dbname, AclId newOwnerSysId)
770770
{
771-
HeapTuple tuple,
772-
newtuple;
771+
HeapTuple tuple;
773772
Relation rel;
774773
ScanKeyData scankey;
775774
SysScanDesc scan;
@@ -788,26 +787,56 @@ AlterDatabaseOwner(const char *dbname, AclId newOwnerSysId)
788787
(errcode(ERRCODE_UNDEFINED_DATABASE),
789788
errmsg("database \"%s\" does not exist", dbname)));
790789

791-
newtuple = heap_copytuple(tuple);
792-
datForm = (Form_pg_database) GETSTRUCT(newtuple);
790+
datForm = (Form_pg_database) GETSTRUCT(tuple);
793791

794792
/*
795793
* If the new owner is the same as the existing owner, consider the
796794
* command to have succeeded. This is to be consistent with other objects.
797795
*/
798796
if (datForm->datdba != newOwnerSysId)
799797
{
798+
Datum repl_val[Natts_pg_database];
799+
char repl_null[Natts_pg_database];
800+
char repl_repl[Natts_pg_database];
801+
Acl *newAcl;
802+
Datum aclDatum;
803+
bool isNull;
804+
HeapTuple newtuple;
805+
800806
/* changing owner's database for someone else: must be superuser */
801807
/* note that the someone else need not have any permissions */
802808
if (!superuser())
803809
ereport(ERROR,
804810
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
805811
errmsg("must be superuser to change owner")));
806812

807-
/* change owner */
808-
datForm->datdba = newOwnerSysId;
813+
memset(repl_null, ' ', sizeof(repl_null));
814+
memset(repl_repl, ' ', sizeof(repl_repl));
815+
816+
repl_repl[Anum_pg_database_datdba - 1] = 'r';
817+
repl_val[Anum_pg_database_datdba - 1] = Int32GetDatum(newOwnerSysId);
818+
819+
/*
820+
* Determine the modified ACL for the new owner. This is only
821+
* necessary when the ACL is non-null.
822+
*/
823+
aclDatum = heap_getattr(tuple,
824+
Anum_pg_database_datacl,
825+
RelationGetDescr(rel),
826+
&isNull);
827+
if (!isNull)
828+
{
829+
newAcl = aclnewowner(DatumGetAclP(aclDatum),
830+
datForm->datdba, newOwnerSysId);
831+
repl_repl[Anum_pg_database_datacl - 1] = 'r';
832+
repl_val[Anum_pg_database_datacl - 1] = PointerGetDatum(newAcl);
833+
}
834+
835+
newtuple = heap_modifytuple(tuple, rel, repl_val, repl_null, repl_repl);
809836
simple_heap_update(rel, &newtuple->t_self, newtuple);
810837
CatalogUpdateIndexes(rel, newtuple);
838+
839+
heap_freetuple(newtuple);
811840
}
812841

813842
systable_endscan(scan);

src/backend/commands/functioncmds.c

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
*
1212
* IDENTIFICATION
13-
* $PostgreSQL: pgsql/src/backend/commands/functioncmds.c,v 1.49 2004/06/25 21:55:53 tgl Exp $
13+
* $PostgreSQL: pgsql/src/backend/commands/functioncmds.c,v 1.50 2004/08/01 20:30:48 tgl Exp $
1414
*
1515
* DESCRIPTION
1616
* These routines take the parse tree and pick out the
@@ -738,7 +738,7 @@ AlterFunctionOwner(List *name, List *argtypes, AclId newOwnerSysId)
738738

739739
procOid = LookupFuncNameTypeNames(name, argtypes, false);
740740

741-
tup = SearchSysCacheCopy(PROCOID,
741+
tup = SearchSysCache(PROCOID,
742742
ObjectIdGetDatum(procOid),
743743
0, 0, 0);
744744
if (!HeapTupleIsValid(tup)) /* should not happen */
@@ -758,22 +758,51 @@ AlterFunctionOwner(List *name, List *argtypes, AclId newOwnerSysId)
758758
*/
759759
if (procForm->proowner != newOwnerSysId)
760760
{
761+
Datum repl_val[Natts_pg_proc];
762+
char repl_null[Natts_pg_proc];
763+
char repl_repl[Natts_pg_proc];
764+
Acl *newAcl;
765+
Datum aclDatum;
766+
bool isNull;
767+
HeapTuple newtuple;
768+
761769
/* Otherwise, must be superuser to change object ownership */
762770
if (!superuser())
763771
ereport(ERROR,
764772
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
765773
errmsg("must be superuser to change owner")));
766774

767-
/* Modify the owner --- okay to scribble on tup because it's a copy */
768-
procForm->proowner = newOwnerSysId;
775+
memset(repl_null, ' ', sizeof(repl_null));
776+
memset(repl_repl, ' ', sizeof(repl_repl));
777+
778+
repl_repl[Anum_pg_proc_proowner - 1] = 'r';
779+
repl_val[Anum_pg_proc_proowner - 1] = Int32GetDatum(newOwnerSysId);
780+
781+
/*
782+
* Determine the modified ACL for the new owner. This is only
783+
* necessary when the ACL is non-null.
784+
*/
785+
aclDatum = SysCacheGetAttr(PROCOID, tup,
786+
Anum_pg_proc_proacl,
787+
&isNull);
788+
if (!isNull)
789+
{
790+
newAcl = aclnewowner(DatumGetAclP(aclDatum),
791+
procForm->proowner, newOwnerSysId);
792+
repl_repl[Anum_pg_proc_proacl - 1] = 'r';
793+
repl_val[Anum_pg_proc_proacl - 1] = PointerGetDatum(newAcl);
794+
}
795+
796+
newtuple = heap_modifytuple(tup, rel, repl_val, repl_null, repl_repl);
769797

770-
simple_heap_update(rel, &tup->t_self, tup);
798+
simple_heap_update(rel, &newtuple->t_self, newtuple);
799+
CatalogUpdateIndexes(rel, newtuple);
771800

772-
CatalogUpdateIndexes(rel, tup);
801+
heap_freetuple(newtuple);
773802
}
774803

804+
ReleaseSysCache(tup);
775805
heap_close(rel, NoLock);
776-
heap_freetuple(tup);
777806
}
778807

779808

src/backend/commands/schemacmds.c

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/commands/schemacmds.c,v 1.20 2004/06/25 21:55:53 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/commands/schemacmds.c,v 1.21 2004/08/01 20:30:48 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -320,7 +320,7 @@ AlterSchemaOwner(const char *name, AclId newOwnerSysId)
320320

321321
rel = heap_openr(NamespaceRelationName, RowExclusiveLock);
322322

323-
tup = SearchSysCacheCopy(NAMESPACENAME,
323+
tup = SearchSysCache(NAMESPACENAME,
324324
CStringGetDatum(name),
325325
0, 0, 0);
326326
if (!HeapTupleIsValid(tup))
@@ -335,20 +335,49 @@ AlterSchemaOwner(const char *name, AclId newOwnerSysId)
335335
*/
336336
if (nspForm->nspowner != newOwnerSysId)
337337
{
338+
Datum repl_val[Natts_pg_namespace];
339+
char repl_null[Natts_pg_namespace];
340+
char repl_repl[Natts_pg_namespace];
341+
Acl *newAcl;
342+
Datum aclDatum;
343+
bool isNull;
344+
HeapTuple newtuple;
345+
338346
/* Otherwise, must be superuser to change object ownership */
339347
if (!superuser())
340348
ereport(ERROR,
341349
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
342350
errmsg("must be superuser to change owner")));
343351

344-
/* Modify the owner --- okay to scribble on tup because it's a copy */
345-
nspForm->nspowner = newOwnerSysId;
352+
memset(repl_null, ' ', sizeof(repl_null));
353+
memset(repl_repl, ' ', sizeof(repl_repl));
346354

347-
simple_heap_update(rel, &tup->t_self, tup);
355+
repl_repl[Anum_pg_namespace_nspowner - 1] = 'r';
356+
repl_val[Anum_pg_namespace_nspowner - 1] = Int32GetDatum(newOwnerSysId);
348357

349-
CatalogUpdateIndexes(rel, tup);
350-
}
358+
/*
359+
* Determine the modified ACL for the new owner. This is only
360+
* necessary when the ACL is non-null.
361+
*/
362+
aclDatum = SysCacheGetAttr(NAMESPACENAME, tup,
363+
Anum_pg_namespace_nspacl,
364+
&isNull);
365+
if (!isNull)
366+
{
367+
newAcl = aclnewowner(DatumGetAclP(aclDatum),
368+
nspForm->nspowner, newOwnerSysId);
369+
repl_repl[Anum_pg_namespace_nspacl - 1] = 'r';
370+
repl_val[Anum_pg_namespace_nspacl - 1] = PointerGetDatum(newAcl);
371+
}
372+
373+
newtuple = heap_modifytuple(tup, rel, repl_val, repl_null, repl_repl);
374+
375+
simple_heap_update(rel, &newtuple->t_self, newtuple);
376+
CatalogUpdateIndexes(rel, newtuple);
351377

378+
heap_freetuple(newtuple);
379+
}
380+
381+
ReleaseSysCache(tup);
352382
heap_close(rel, NoLock);
353-
heap_freetuple(tup);
354383
}

src/backend/commands/tablecmds.c

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.122 2004/07/21 22:31:21 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.123 2004/08/01 20:30:48 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -5115,7 +5115,7 @@ ATExecChangeOwner(Oid relationOid, int32 newOwnerSysId)
51155115
/* Get its pg_class tuple, too */
51165116
class_rel = heap_openr(RelationRelationName, RowExclusiveLock);
51175117

5118-
tuple = SearchSysCacheCopy(RELOID,
5118+
tuple = SearchSysCache(RELOID,
51195119
ObjectIdGetDatum(relationOid),
51205120
0, 0, 0);
51215121
if (!HeapTupleIsValid(tuple))
@@ -5145,21 +5145,47 @@ ATExecChangeOwner(Oid relationOid, int32 newOwnerSysId)
51455145
*/
51465146
if (tuple_class->relowner != newOwnerSysId)
51475147
{
5148+
Datum repl_val[Natts_pg_class];
5149+
char repl_null[Natts_pg_class];
5150+
char repl_repl[Natts_pg_class];
5151+
Acl *newAcl;
5152+
Datum aclDatum;
5153+
bool isNull;
5154+
HeapTuple newtuple;
5155+
51485156
/* Otherwise, check that we are the superuser */
51495157
if (!superuser())
51505158
ereport(ERROR,
51515159
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
51525160
errmsg("must be superuser to change owner")));
51535161

5162+
memset(repl_null, ' ', sizeof(repl_null));
5163+
memset(repl_repl, ' ', sizeof(repl_repl));
5164+
5165+
repl_repl[Anum_pg_class_relowner - 1] = 'r';
5166+
repl_val[Anum_pg_class_relowner - 1] = Int32GetDatum(newOwnerSysId);
5167+
51545168
/*
5155-
* Okay, this is a valid tuple: change its ownership and write to the
5156-
* heap.
5169+
* Determine the modified ACL for the new owner. This is only
5170+
* necessary when the ACL is non-null.
51575171
*/
5158-
tuple_class->relowner = newOwnerSysId;
5159-
simple_heap_update(class_rel, &tuple->t_self, tuple);
5172+
aclDatum = SysCacheGetAttr(RELOID, tuple,
5173+
Anum_pg_class_relacl,
5174+
&isNull);
5175+
if (!isNull)
5176+
{
5177+
newAcl = aclnewowner(DatumGetAclP(aclDatum),
5178+
tuple_class->relowner, newOwnerSysId);
5179+
repl_repl[Anum_pg_class_relacl - 1] = 'r';
5180+
repl_val[Anum_pg_class_relacl - 1] = PointerGetDatum(newAcl);
5181+
}
51605182

5161-
/* Keep the catalog indexes up to date */
5162-
CatalogUpdateIndexes(class_rel, tuple);
5183+
newtuple = heap_modifytuple(tuple, class_rel, repl_val, repl_null, repl_repl);
5184+
5185+
simple_heap_update(class_rel, &newtuple->t_self, newtuple);
5186+
CatalogUpdateIndexes(class_rel, newtuple);
5187+
5188+
heap_freetuple(newtuple);
51635189

51645190
/*
51655191
* If we are operating on a table, also change the ownership of any
@@ -5190,7 +5216,7 @@ ATExecChangeOwner(Oid relationOid, int32 newOwnerSysId)
51905216
}
51915217
}
51925218

5193-
heap_freetuple(tuple);
5219+
ReleaseSysCache(tuple);
51945220
heap_close(class_rel, RowExclusiveLock);
51955221
relation_close(target_rel, NoLock);
51965222
}

src/backend/commands/tablespace.c

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
*
4646
*
4747
* IDENTIFICATION
48-
* $PostgreSQL: pgsql/src/backend/commands/tablespace.c,v 1.6 2004/07/11 19:52:49 tgl Exp $
48+
* $PostgreSQL: pgsql/src/backend/commands/tablespace.c,v 1.7 2004/08/01 20:30:48 tgl Exp $
4949
*
5050
*-------------------------------------------------------------------------
5151
*/
@@ -757,7 +757,6 @@ AlterTableSpaceOwner(const char *name, AclId newOwnerSysId)
757757
HeapScanDesc scandesc;
758758
Form_pg_tablespace spcForm;
759759
HeapTuple tup;
760-
HeapTuple newtuple;
761760

762761
/* Search pg_tablespace */
763762
rel = heap_openr(TableSpaceRelationName, RowExclusiveLock);
@@ -773,25 +772,56 @@ AlterTableSpaceOwner(const char *name, AclId newOwnerSysId)
773772
(errcode(ERRCODE_UNDEFINED_OBJECT),
774773
errmsg("tablespace \"%s\" does not exist", name)));
775774

776-
newtuple = heap_copytuple(tup);
777-
spcForm = (Form_pg_tablespace) GETSTRUCT(newtuple);
775+
spcForm = (Form_pg_tablespace) GETSTRUCT(tup);
778776

779777
/*
780778
* If the new owner is the same as the existing owner, consider the
781779
* command to have succeeded. This is for dump restoration purposes.
782780
*/
783781
if (spcForm->spcowner != newOwnerSysId)
784782
{
783+
Datum repl_val[Natts_pg_tablespace];
784+
char repl_null[Natts_pg_tablespace];
785+
char repl_repl[Natts_pg_tablespace];
786+
Acl *newAcl;
787+
Datum aclDatum;
788+
bool isNull;
789+
HeapTuple newtuple;
790+
785791
/* Otherwise, must be superuser to change object ownership */
786792
if (!superuser())
787793
ereport(ERROR,
788794
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
789795
errmsg("must be superuser to change owner")));
790796

791-
/* Modify the owner */
792-
spcForm->spcowner = newOwnerSysId;
797+
memset(repl_null, ' ', sizeof(repl_null));
798+
memset(repl_repl, ' ', sizeof(repl_repl));
799+
800+
repl_repl[Anum_pg_tablespace_spcowner - 1] = 'r';
801+
repl_val[Anum_pg_tablespace_spcowner - 1] = Int32GetDatum(newOwnerSysId);
802+
803+
/*
804+
* Determine the modified ACL for the new owner. This is only
805+
* necessary when the ACL is non-null.
806+
*/
807+
aclDatum = heap_getattr(tup,
808+
Anum_pg_tablespace_spcacl,
809+
RelationGetDescr(rel),
810+
&isNull);
811+
if (!isNull)
812+
{
813+
newAcl = aclnewowner(DatumGetAclP(aclDatum),
814+
spcForm->spcowner, newOwnerSysId);
815+
repl_repl[Anum_pg_tablespace_spcacl - 1] = 'r';
816+
repl_val[Anum_pg_tablespace_spcacl - 1] = PointerGetDatum(newAcl);
817+
}
818+
819+
newtuple = heap_modifytuple(tup, rel, repl_val, repl_null, repl_repl);
820+
793821
simple_heap_update(rel, &newtuple->t_self, newtuple);
794822
CatalogUpdateIndexes(rel, newtuple);
823+
824+
heap_freetuple(newtuple);
795825
}
796826

797827
heap_endscan(scandesc);

0 commit comments

Comments
 (0)