LCOV - code coverage report
Current view: top level - src/backend/catalog - aclchk.c (source / functions) Hit Total Coverage
Test: PostgreSQL 17devel Lines: 1468 1810 81.1 %
Date: 2024-04-25 15:13:21 Functions: 54 57 94.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * aclchk.c
       4             :  *    Routines to check access control permissions.
       5             :  *
       6             :  * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
       7             :  * Portions Copyright (c) 1994, Regents of the University of California
       8             :  *
       9             :  *
      10             :  * IDENTIFICATION
      11             :  *    src/backend/catalog/aclchk.c
      12             :  *
      13             :  * NOTES
      14             :  *    See acl.h.
      15             :  *
      16             :  *    The xxx_aclmask() functions in this file are wrappers around
      17             :  *    acl.c's aclmask() function; see that for basic usage information.
      18             :  *    The wrapper functions add object-type-specific lookup capability.
      19             :  *    Generally, they will throw error if the object doesn't exist.
      20             :  *
      21             :  *    The xxx_aclmask_ext() functions add the ability to not throw
      22             :  *    error if the object doesn't exist.  If their "is_missing" argument
      23             :  *    isn't NULL, then when the object isn't found they will set
      24             :  *    *is_missing = true and return zero (no privileges) instead of
      25             :  *    throwing an error.  Caller must initialize *is_missing = false.
      26             :  *
      27             :  *    The xxx_aclcheck() functions are simplified wrappers around the
      28             :  *    corresponding xxx_aclmask() functions, simply returning ACLCHECK_OK
      29             :  *    if any of the privileges specified in "mode" are held, and otherwise
      30             :  *    a suitable error code (in practice, always ACLCHECK_NO_PRIV).
      31             :  *    Again, they will throw error if the object doesn't exist.
      32             :  *
      33             :  *    The xxx_aclcheck_ext() functions add the ability to not throw
      34             :  *    error if the object doesn't exist.  Their "is_missing" argument
      35             :  *    works similarly to the xxx_aclmask_ext() functions.
      36             :  *
      37             :  *-------------------------------------------------------------------------
      38             :  */
      39             : #include "postgres.h"
      40             : 
      41             : #include "access/genam.h"
      42             : #include "access/heapam.h"
      43             : #include "access/htup_details.h"
      44             : #include "access/sysattr.h"
      45             : #include "access/tableam.h"
      46             : #include "access/xact.h"
      47             : #include "catalog/binary_upgrade.h"
      48             : #include "catalog/catalog.h"
      49             : #include "catalog/dependency.h"
      50             : #include "catalog/indexing.h"
      51             : #include "catalog/objectaccess.h"
      52             : #include "catalog/pg_authid.h"
      53             : #include "catalog/pg_class.h"
      54             : #include "catalog/pg_database.h"
      55             : #include "catalog/pg_default_acl.h"
      56             : #include "catalog/pg_foreign_data_wrapper.h"
      57             : #include "catalog/pg_foreign_server.h"
      58             : #include "catalog/pg_init_privs.h"
      59             : #include "catalog/pg_language.h"
      60             : #include "catalog/pg_largeobject.h"
      61             : #include "catalog/pg_largeobject_metadata.h"
      62             : #include "catalog/pg_namespace.h"
      63             : #include "catalog/pg_parameter_acl.h"
      64             : #include "catalog/pg_proc.h"
      65             : #include "catalog/pg_tablespace.h"
      66             : #include "catalog/pg_type.h"
      67             : #include "commands/dbcommands.h"
      68             : #include "commands/defrem.h"
      69             : #include "commands/event_trigger.h"
      70             : #include "commands/extension.h"
      71             : #include "commands/proclang.h"
      72             : #include "commands/tablespace.h"
      73             : #include "foreign/foreign.h"
      74             : #include "miscadmin.h"
      75             : #include "nodes/makefuncs.h"
      76             : #include "parser/parse_func.h"
      77             : #include "parser/parse_type.h"
      78             : #include "utils/acl.h"
      79             : #include "utils/aclchk_internal.h"
      80             : #include "utils/builtins.h"
      81             : #include "utils/fmgroids.h"
      82             : #include "utils/guc.h"
      83             : #include "utils/lsyscache.h"
      84             : #include "utils/rel.h"
      85             : #include "utils/syscache.h"
      86             : 
      87             : /*
      88             :  * Internal format used by ALTER DEFAULT PRIVILEGES.
      89             :  */
      90             : typedef struct
      91             : {
      92             :     Oid         roleid;         /* owning role */
      93             :     Oid         nspid;          /* namespace, or InvalidOid if none */
      94             :     /* remaining fields are same as in InternalGrant: */
      95             :     bool        is_grant;
      96             :     ObjectType  objtype;
      97             :     bool        all_privs;
      98             :     AclMode     privileges;
      99             :     List       *grantees;
     100             :     bool        grant_option;
     101             :     DropBehavior behavior;
     102             : } InternalDefaultACL;
     103             : 
     104             : /*
     105             :  * When performing a binary-upgrade, pg_dump will call a function to set
     106             :  * this variable to let us know that we need to populate the pg_init_privs
     107             :  * table for the GRANT/REVOKE commands while this variable is set to true.
     108             :  */
     109             : bool        binary_upgrade_record_init_privs = false;
     110             : 
     111             : static void ExecGrantStmt_oids(InternalGrant *istmt);
     112             : static void ExecGrant_Relation(InternalGrant *istmt);
     113             : static void ExecGrant_common(InternalGrant *istmt, Oid classid, AclMode default_privs,
     114             :                              void (*object_check) (InternalGrant *istmt, HeapTuple tuple));
     115             : static void ExecGrant_Language_check(InternalGrant *istmt, HeapTuple tuple);
     116             : static void ExecGrant_Largeobject(InternalGrant *istmt);
     117             : static void ExecGrant_Type_check(InternalGrant *istmt, HeapTuple tuple);
     118             : static void ExecGrant_Parameter(InternalGrant *istmt);
     119             : 
     120             : static void SetDefaultACLsInSchemas(InternalDefaultACL *iacls, List *nspnames);
     121             : static void SetDefaultACL(InternalDefaultACL *iacls);
     122             : 
     123             : static List *objectNamesToOids(ObjectType objtype, List *objnames,
     124             :                                bool is_grant);
     125             : static List *objectsInSchemaToOids(ObjectType objtype, List *nspnames);
     126             : static List *getRelationsInNamespace(Oid namespaceId, char relkind);
     127             : static void expand_col_privileges(List *colnames, Oid table_oid,
     128             :                                   AclMode this_privileges,
     129             :                                   AclMode *col_privileges,
     130             :                                   int num_col_privileges);
     131             : static void expand_all_col_privileges(Oid table_oid, Form_pg_class classForm,
     132             :                                       AclMode this_privileges,
     133             :                                       AclMode *col_privileges,
     134             :                                       int num_col_privileges);
     135             : static AclMode string_to_privilege(const char *privname);
     136             : static const char *privilege_to_string(AclMode privilege);
     137             : static AclMode restrict_and_check_grant(bool is_grant, AclMode avail_goptions,
     138             :                                         bool all_privs, AclMode privileges,
     139             :                                         Oid objectId, Oid grantorId,
     140             :                                         ObjectType objtype, const char *objname,
     141             :                                         AttrNumber att_number, const char *colname);
     142             : static AclMode pg_aclmask(ObjectType objtype, Oid object_oid, AttrNumber attnum,
     143             :                           Oid roleid, AclMode mask, AclMaskHow how);
     144             : static AclMode object_aclmask(Oid classid, Oid objectid, Oid roleid,
     145             :                               AclMode mask, AclMaskHow how);
     146             : static AclMode object_aclmask_ext(Oid classid, Oid objectid, Oid roleid,
     147             :                                   AclMode mask, AclMaskHow how,
     148             :                                   bool *is_missing);
     149             : static AclMode pg_attribute_aclmask(Oid table_oid, AttrNumber attnum,
     150             :                                     Oid roleid, AclMode mask, AclMaskHow how);
     151             : static AclMode pg_attribute_aclmask_ext(Oid table_oid, AttrNumber attnum,
     152             :                                         Oid roleid, AclMode mask,
     153             :                                         AclMaskHow how, bool *is_missing);
     154             : static AclMode pg_class_aclmask_ext(Oid table_oid, Oid roleid,
     155             :                                     AclMode mask, AclMaskHow how,
     156             :                                     bool *is_missing);
     157             : static AclMode pg_parameter_acl_aclmask(Oid acl_oid, Oid roleid,
     158             :                                         AclMode mask, AclMaskHow how);
     159             : static AclMode pg_largeobject_aclmask_snapshot(Oid lobj_oid, Oid roleid,
     160             :                                                AclMode mask, AclMaskHow how, Snapshot snapshot);
     161             : static AclMode pg_namespace_aclmask_ext(Oid nsp_oid, Oid roleid,
     162             :                                         AclMode mask, AclMaskHow how,
     163             :                                         bool *is_missing);
     164             : static AclMode pg_type_aclmask_ext(Oid type_oid, Oid roleid,
     165             :                                    AclMode mask, AclMaskHow how,
     166             :                                    bool *is_missing);
     167             : static void recordExtensionInitPriv(Oid objoid, Oid classoid, int objsubid,
     168             :                                     Acl *new_acl);
     169             : static void recordExtensionInitPrivWorker(Oid objoid, Oid classoid, int objsubid,
     170             :                                           Acl *new_acl);
     171             : 
     172             : 
     173             : /*
     174             :  * If is_grant is true, adds the given privileges for the list of
     175             :  * grantees to the existing old_acl.  If is_grant is false, the
     176             :  * privileges for the given grantees are removed from old_acl.
     177             :  *
     178             :  * NB: the original old_acl is pfree'd.
     179             :  */
     180             : static Acl *
     181       33298 : merge_acl_with_grant(Acl *old_acl, bool is_grant,
     182             :                      bool grant_option, DropBehavior behavior,
     183             :                      List *grantees, AclMode privileges,
     184             :                      Oid grantorId, Oid ownerId)
     185             : {
     186             :     unsigned    modechg;
     187             :     ListCell   *j;
     188             :     Acl        *new_acl;
     189             : 
     190       33298 :     modechg = is_grant ? ACL_MODECHG_ADD : ACL_MODECHG_DEL;
     191             : 
     192       33298 :     new_acl = old_acl;
     193             : 
     194       66710 :     foreach(j, grantees)
     195             :     {
     196             :         AclItem     aclitem;
     197             :         Acl        *newer_acl;
     198             : 
     199       33424 :         aclitem.ai_grantee = lfirst_oid(j);
     200             : 
     201             :         /*
     202             :          * Grant options can only be granted to individual roles, not PUBLIC.
     203             :          * The reason is that if a user would re-grant a privilege that he
     204             :          * held through PUBLIC, and later the user is removed, the situation
     205             :          * is impossible to clean up.
     206             :          */
     207       33424 :         if (is_grant && grant_option && aclitem.ai_grantee == ACL_ID_PUBLIC)
     208           0 :             ereport(ERROR,
     209             :                     (errcode(ERRCODE_INVALID_GRANT_OPERATION),
     210             :                      errmsg("grant options can only be granted to roles")));
     211             : 
     212       33424 :         aclitem.ai_grantor = grantorId;
     213             : 
     214             :         /*
     215             :          * The asymmetry in the conditions here comes from the spec.  In
     216             :          * GRANT, the grant_option flag signals WITH GRANT OPTION, which means
     217             :          * to grant both the basic privilege and its grant option. But in
     218             :          * REVOKE, plain revoke revokes both the basic privilege and its grant
     219             :          * option, while REVOKE GRANT OPTION revokes only the option.
     220             :          */
     221       33424 :         ACLITEM_SET_PRIVS_GOPTIONS(aclitem,
     222             :                                    (is_grant || !grant_option) ? privileges : ACL_NO_RIGHTS,
     223             :                                    (!is_grant || grant_option) ? privileges : ACL_NO_RIGHTS);
     224             : 
     225       33424 :         newer_acl = aclupdate(new_acl, &aclitem, modechg, ownerId, behavior);
     226             : 
     227             :         /* avoid memory leak when there are many grantees */
     228       33412 :         pfree(new_acl);
     229       33412 :         new_acl = newer_acl;
     230             :     }
     231             : 
     232       33286 :     return new_acl;
     233             : }
     234             : 
     235             : /*
     236             :  * Restrict the privileges to what we can actually grant, and emit
     237             :  * the standards-mandated warning and error messages.
     238             :  */
     239             : static AclMode
     240       33138 : restrict_and_check_grant(bool is_grant, AclMode avail_goptions, bool all_privs,
     241             :                          AclMode privileges, Oid objectId, Oid grantorId,
     242             :                          ObjectType objtype, const char *objname,
     243             :                          AttrNumber att_number, const char *colname)
     244             : {
     245             :     AclMode     this_privileges;
     246             :     AclMode     whole_mask;
     247             : 
     248       33138 :     switch (objtype)
     249             :     {
     250       17794 :         case OBJECT_COLUMN:
     251       17794 :             whole_mask = ACL_ALL_RIGHTS_COLUMN;
     252       17794 :             break;
     253        7406 :         case OBJECT_TABLE:
     254        7406 :             whole_mask = ACL_ALL_RIGHTS_RELATION;
     255        7406 :             break;
     256         160 :         case OBJECT_SEQUENCE:
     257         160 :             whole_mask = ACL_ALL_RIGHTS_SEQUENCE;
     258         160 :             break;
     259         284 :         case OBJECT_DATABASE:
     260         284 :             whole_mask = ACL_ALL_RIGHTS_DATABASE;
     261         284 :             break;
     262        6556 :         case OBJECT_FUNCTION:
     263        6556 :             whole_mask = ACL_ALL_RIGHTS_FUNCTION;
     264        6556 :             break;
     265          36 :         case OBJECT_LANGUAGE:
     266          36 :             whole_mask = ACL_ALL_RIGHTS_LANGUAGE;
     267          36 :             break;
     268          80 :         case OBJECT_LARGEOBJECT:
     269          80 :             whole_mask = ACL_ALL_RIGHTS_LARGEOBJECT;
     270          80 :             break;
     271         382 :         case OBJECT_SCHEMA:
     272         382 :             whole_mask = ACL_ALL_RIGHTS_SCHEMA;
     273         382 :             break;
     274           0 :         case OBJECT_TABLESPACE:
     275           0 :             whole_mask = ACL_ALL_RIGHTS_TABLESPACE;
     276           0 :             break;
     277          94 :         case OBJECT_FDW:
     278          94 :             whole_mask = ACL_ALL_RIGHTS_FDW;
     279          94 :             break;
     280          90 :         case OBJECT_FOREIGN_SERVER:
     281          90 :             whole_mask = ACL_ALL_RIGHTS_FOREIGN_SERVER;
     282          90 :             break;
     283           0 :         case OBJECT_EVENT_TRIGGER:
     284           0 :             elog(ERROR, "grantable rights not supported for event triggers");
     285             :             /* not reached, but keep compiler quiet */
     286             :             return ACL_NO_RIGHTS;
     287         122 :         case OBJECT_TYPE:
     288         122 :             whole_mask = ACL_ALL_RIGHTS_TYPE;
     289         122 :             break;
     290         134 :         case OBJECT_PARAMETER_ACL:
     291         134 :             whole_mask = ACL_ALL_RIGHTS_PARAMETER_ACL;
     292         134 :             break;
     293           0 :         default:
     294           0 :             elog(ERROR, "unrecognized object type: %d", objtype);
     295             :             /* not reached, but keep compiler quiet */
     296             :             return ACL_NO_RIGHTS;
     297             :     }
     298             : 
     299             :     /*
     300             :      * If we found no grant options, consider whether to issue a hard error.
     301             :      * Per spec, having any privilege at all on the object will get you by
     302             :      * here.
     303             :      */
     304       33138 :     if (avail_goptions == ACL_NO_RIGHTS)
     305             :     {
     306          66 :         if (pg_aclmask(objtype, objectId, att_number, grantorId,
     307          66 :                        whole_mask | ACL_GRANT_OPTION_FOR(whole_mask),
     308             :                        ACLMASK_ANY) == ACL_NO_RIGHTS)
     309             :         {
     310          30 :             if (objtype == OBJECT_COLUMN && colname)
     311           0 :                 aclcheck_error_col(ACLCHECK_NO_PRIV, objtype, objname, colname);
     312             :             else
     313          30 :                 aclcheck_error(ACLCHECK_NO_PRIV, objtype, objname);
     314             :         }
     315             :     }
     316             : 
     317             :     /*
     318             :      * Restrict the operation to what we can actually grant or revoke, and
     319             :      * issue a warning if appropriate.  (For REVOKE this isn't quite what the
     320             :      * spec says to do: the spec seems to want a warning only if no privilege
     321             :      * bits actually change in the ACL. In practice that behavior seems much
     322             :      * too noisy, as well as inconsistent with the GRANT case.)
     323             :      */
     324       33108 :     this_privileges = privileges & ACL_OPTION_TO_PRIVS(avail_goptions);
     325       33108 :     if (is_grant)
     326             :     {
     327        9882 :         if (this_privileges == 0)
     328             :         {
     329          30 :             if (objtype == OBJECT_COLUMN && colname)
     330           0 :                 ereport(WARNING,
     331             :                         (errcode(ERRCODE_WARNING_PRIVILEGE_NOT_GRANTED),
     332             :                          errmsg("no privileges were granted for column \"%s\" of relation \"%s\"",
     333             :                                 colname, objname)));
     334             :             else
     335          30 :                 ereport(WARNING,
     336             :                         (errcode(ERRCODE_WARNING_PRIVILEGE_NOT_GRANTED),
     337             :                          errmsg("no privileges were granted for \"%s\"",
     338             :                                 objname)));
     339             :         }
     340        9852 :         else if (!all_privs && this_privileges != privileges)
     341             :         {
     342           0 :             if (objtype == OBJECT_COLUMN && colname)
     343           0 :                 ereport(WARNING,
     344             :                         (errcode(ERRCODE_WARNING_PRIVILEGE_NOT_GRANTED),
     345             :                          errmsg("not all privileges were granted for column \"%s\" of relation \"%s\"",
     346             :                                 colname, objname)));
     347             :             else
     348           0 :                 ereport(WARNING,
     349             :                         (errcode(ERRCODE_WARNING_PRIVILEGE_NOT_GRANTED),
     350             :                          errmsg("not all privileges were granted for \"%s\"",
     351             :                                 objname)));
     352             :         }
     353             :     }
     354             :     else
     355             :     {
     356       23226 :         if (this_privileges == 0)
     357             :         {
     358           6 :             if (objtype == OBJECT_COLUMN && colname)
     359           0 :                 ereport(WARNING,
     360             :                         (errcode(ERRCODE_WARNING_PRIVILEGE_NOT_REVOKED),
     361             :                          errmsg("no privileges could be revoked for column \"%s\" of relation \"%s\"",
     362             :                                 colname, objname)));
     363             :             else
     364           6 :                 ereport(WARNING,
     365             :                         (errcode(ERRCODE_WARNING_PRIVILEGE_NOT_REVOKED),
     366             :                          errmsg("no privileges could be revoked for \"%s\"",
     367             :                                 objname)));
     368             :         }
     369       23220 :         else if (!all_privs && this_privileges != privileges)
     370             :         {
     371           0 :             if (objtype == OBJECT_COLUMN && colname)
     372           0 :                 ereport(WARNING,
     373             :                         (errcode(ERRCODE_WARNING_PRIVILEGE_NOT_REVOKED),
     374             :                          errmsg("not all privileges could be revoked for column \"%s\" of relation \"%s\"",
     375             :                                 colname, objname)));
     376             :             else
     377           0 :                 ereport(WARNING,
     378             :                         (errcode(ERRCODE_WARNING_PRIVILEGE_NOT_REVOKED),
     379             :                          errmsg("not all privileges could be revoked for \"%s\"",
     380             :                                 objname)));
     381             :         }
     382             :     }
     383             : 
     384       33108 :     return this_privileges;
     385             : }
     386             : 
     387             : /*
     388             :  * Called to execute the utility commands GRANT and REVOKE
     389             :  */
     390             : void
     391       15382 : ExecuteGrantStmt(GrantStmt *stmt)
     392             : {
     393             :     InternalGrant istmt;
     394             :     ListCell   *cell;
     395             :     const char *errormsg;
     396             :     AclMode     all_privileges;
     397             : 
     398       15382 :     if (stmt->grantor)
     399             :     {
     400             :         Oid         grantor;
     401             : 
     402          18 :         grantor = get_rolespec_oid(stmt->grantor, false);
     403             : 
     404             :         /*
     405             :          * Currently, this clause is only for SQL compatibility, not very
     406             :          * interesting otherwise.
     407             :          */
     408          18 :         if (grantor != GetUserId())
     409           6 :             ereport(ERROR,
     410             :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
     411             :                      errmsg("grantor must be current user")));
     412             :     }
     413             : 
     414             :     /*
     415             :      * Turn the regular GrantStmt into the InternalGrant form.
     416             :      */
     417       15376 :     istmt.is_grant = stmt->is_grant;
     418       15376 :     istmt.objtype = stmt->objtype;
     419             : 
     420             :     /* Collect the OIDs of the target objects */
     421       15376 :     switch (stmt->targtype)
     422             :     {
     423       15346 :         case ACL_TARGET_OBJECT:
     424       30666 :             istmt.objects = objectNamesToOids(stmt->objtype, stmt->objects,
     425       15346 :                                               stmt->is_grant);
     426       15320 :             break;
     427          30 :         case ACL_TARGET_ALL_IN_SCHEMA:
     428          30 :             istmt.objects = objectsInSchemaToOids(stmt->objtype, stmt->objects);
     429          30 :             break;
     430             :             /* ACL_TARGET_DEFAULTS should not be seen here */
     431           0 :         default:
     432           0 :             elog(ERROR, "unrecognized GrantStmt.targtype: %d",
     433             :                  (int) stmt->targtype);
     434             :     }
     435             : 
     436             :     /* all_privs to be filled below */
     437             :     /* privileges to be filled below */
     438       15350 :     istmt.col_privs = NIL;      /* may get filled below */
     439       15350 :     istmt.grantees = NIL;       /* filled below */
     440       15350 :     istmt.grant_option = stmt->grant_option;
     441       15350 :     istmt.behavior = stmt->behavior;
     442             : 
     443             :     /*
     444             :      * Convert the RoleSpec list into an Oid list.  Note that at this point we
     445             :      * insert an ACL_ID_PUBLIC into the list if appropriate, so downstream
     446             :      * there shouldn't be any additional work needed to support this case.
     447             :      */
     448       30790 :     foreach(cell, stmt->grantees)
     449             :     {
     450       15446 :         RoleSpec   *grantee = (RoleSpec *) lfirst(cell);
     451             :         Oid         grantee_uid;
     452             : 
     453       15446 :         switch (grantee->roletype)
     454             :         {
     455       11896 :             case ROLESPEC_PUBLIC:
     456       11896 :                 grantee_uid = ACL_ID_PUBLIC;
     457       11896 :                 break;
     458        3550 :             default:
     459        3550 :                 grantee_uid = get_rolespec_oid(grantee, false);
     460        3544 :                 break;
     461             :         }
     462       15440 :         istmt.grantees = lappend_oid(istmt.grantees, grantee_uid);
     463             :     }
     464             : 
     465             :     /*
     466             :      * Convert stmt->privileges, a list of AccessPriv nodes, into an AclMode
     467             :      * bitmask.  Note: objtype can't be OBJECT_COLUMN.
     468             :      */
     469       15344 :     switch (stmt->objtype)
     470             :     {
     471        7790 :         case OBJECT_TABLE:
     472             : 
     473             :             /*
     474             :              * Because this might be a sequence, we test both relation and
     475             :              * sequence bits, and later do a more limited test when we know
     476             :              * the object type.
     477             :              */
     478        7790 :             all_privileges = ACL_ALL_RIGHTS_RELATION | ACL_ALL_RIGHTS_SEQUENCE;
     479        7790 :             errormsg = gettext_noop("invalid privilege type %s for relation");
     480        7790 :             break;
     481          16 :         case OBJECT_SEQUENCE:
     482          16 :             all_privileges = ACL_ALL_RIGHTS_SEQUENCE;
     483          16 :             errormsg = gettext_noop("invalid privilege type %s for sequence");
     484          16 :             break;
     485         274 :         case OBJECT_DATABASE:
     486         274 :             all_privileges = ACL_ALL_RIGHTS_DATABASE;
     487         274 :             errormsg = gettext_noop("invalid privilege type %s for database");
     488         274 :             break;
     489          26 :         case OBJECT_DOMAIN:
     490          26 :             all_privileges = ACL_ALL_RIGHTS_TYPE;
     491          26 :             errormsg = gettext_noop("invalid privilege type %s for domain");
     492          26 :             break;
     493        6444 :         case OBJECT_FUNCTION:
     494        6444 :             all_privileges = ACL_ALL_RIGHTS_FUNCTION;
     495        6444 :             errormsg = gettext_noop("invalid privilege type %s for function");
     496        6444 :             break;
     497          42 :         case OBJECT_LANGUAGE:
     498          42 :             all_privileges = ACL_ALL_RIGHTS_LANGUAGE;
     499          42 :             errormsg = gettext_noop("invalid privilege type %s for language");
     500          42 :             break;
     501          62 :         case OBJECT_LARGEOBJECT:
     502          62 :             all_privileges = ACL_ALL_RIGHTS_LARGEOBJECT;
     503          62 :             errormsg = gettext_noop("invalid privilege type %s for large object");
     504          62 :             break;
     505         286 :         case OBJECT_SCHEMA:
     506         286 :             all_privileges = ACL_ALL_RIGHTS_SCHEMA;
     507         286 :             errormsg = gettext_noop("invalid privilege type %s for schema");
     508         286 :             break;
     509          48 :         case OBJECT_PROCEDURE:
     510          48 :             all_privileges = ACL_ALL_RIGHTS_FUNCTION;
     511          48 :             errormsg = gettext_noop("invalid privilege type %s for procedure");
     512          48 :             break;
     513           6 :         case OBJECT_ROUTINE:
     514           6 :             all_privileges = ACL_ALL_RIGHTS_FUNCTION;
     515           6 :             errormsg = gettext_noop("invalid privilege type %s for routine");
     516           6 :             break;
     517           0 :         case OBJECT_TABLESPACE:
     518           0 :             all_privileges = ACL_ALL_RIGHTS_TABLESPACE;
     519           0 :             errormsg = gettext_noop("invalid privilege type %s for tablespace");
     520           0 :             break;
     521         110 :         case OBJECT_TYPE:
     522         110 :             all_privileges = ACL_ALL_RIGHTS_TYPE;
     523         110 :             errormsg = gettext_noop("invalid privilege type %s for type");
     524         110 :             break;
     525          92 :         case OBJECT_FDW:
     526          92 :             all_privileges = ACL_ALL_RIGHTS_FDW;
     527          92 :             errormsg = gettext_noop("invalid privilege type %s for foreign-data wrapper");
     528          92 :             break;
     529          76 :         case OBJECT_FOREIGN_SERVER:
     530          76 :             all_privileges = ACL_ALL_RIGHTS_FOREIGN_SERVER;
     531          76 :             errormsg = gettext_noop("invalid privilege type %s for foreign server");
     532          76 :             break;
     533          72 :         case OBJECT_PARAMETER_ACL:
     534          72 :             all_privileges = ACL_ALL_RIGHTS_PARAMETER_ACL;
     535          72 :             errormsg = gettext_noop("invalid privilege type %s for parameter");
     536          72 :             break;
     537           0 :         default:
     538           0 :             elog(ERROR, "unrecognized GrantStmt.objtype: %d",
     539             :                  (int) stmt->objtype);
     540             :             /* keep compiler quiet */
     541             :             all_privileges = ACL_NO_RIGHTS;
     542             :             errormsg = NULL;
     543             :     }
     544             : 
     545       15344 :     if (stmt->privileges == NIL)
     546             :     {
     547        2032 :         istmt.all_privs = true;
     548             : 
     549             :         /*
     550             :          * will be turned into ACL_ALL_RIGHTS_* by the internal routines
     551             :          * depending on the object type
     552             :          */
     553        2032 :         istmt.privileges = ACL_NO_RIGHTS;
     554             :     }
     555             :     else
     556             :     {
     557       13312 :         istmt.all_privs = false;
     558       13312 :         istmt.privileges = ACL_NO_RIGHTS;
     559             : 
     560       27030 :         foreach(cell, stmt->privileges)
     561             :         {
     562       13742 :             AccessPriv *privnode = (AccessPriv *) lfirst(cell);
     563             :             AclMode     priv;
     564             : 
     565             :             /*
     566             :              * If it's a column-level specification, we just set it aside in
     567             :              * col_privs for the moment; but insist it's for a relation.
     568             :              */
     569       13742 :             if (privnode->cols)
     570             :             {
     571         410 :                 if (stmt->objtype != OBJECT_TABLE)
     572           0 :                     ereport(ERROR,
     573             :                             (errcode(ERRCODE_INVALID_GRANT_OPERATION),
     574             :                              errmsg("column privileges are only valid for relations")));
     575         410 :                 istmt.col_privs = lappend(istmt.col_privs, privnode);
     576         410 :                 continue;
     577             :             }
     578             : 
     579       13332 :             if (privnode->priv_name == NULL) /* parser mistake? */
     580           0 :                 elog(ERROR, "AccessPriv node must specify privilege or columns");
     581       13332 :             priv = string_to_privilege(privnode->priv_name);
     582             : 
     583       13332 :             if (priv & ~((AclMode) all_privileges))
     584          24 :                 ereport(ERROR,
     585             :                         (errcode(ERRCODE_INVALID_GRANT_OPERATION),
     586             :                          errmsg(errormsg, privilege_to_string(priv))));
     587             : 
     588       13308 :             istmt.privileges |= priv;
     589             :         }
     590             :     }
     591             : 
     592       15320 :     ExecGrantStmt_oids(&istmt);
     593       15254 : }
     594             : 
     595             : /*
     596             :  * ExecGrantStmt_oids
     597             :  *
     598             :  * Internal entry point for granting and revoking privileges.
     599             :  */
     600             : static void
     601       15532 : ExecGrantStmt_oids(InternalGrant *istmt)
     602             : {
     603       15532 :     switch (istmt->objtype)
     604             :     {
     605        7898 :         case OBJECT_TABLE:
     606             :         case OBJECT_SEQUENCE:
     607        7898 :             ExecGrant_Relation(istmt);
     608        7892 :             break;
     609         284 :         case OBJECT_DATABASE:
     610         284 :             ExecGrant_common(istmt, DatabaseRelationId, ACL_ALL_RIGHTS_DATABASE, NULL);
     611         284 :             break;
     612         140 :         case OBJECT_DOMAIN:
     613             :         case OBJECT_TYPE:
     614         140 :             ExecGrant_common(istmt, TypeRelationId, ACL_ALL_RIGHTS_TYPE, ExecGrant_Type_check);
     615         116 :             break;
     616          94 :         case OBJECT_FDW:
     617          94 :             ExecGrant_common(istmt, ForeignDataWrapperRelationId, ACL_ALL_RIGHTS_FDW, NULL);
     618          76 :             break;
     619          90 :         case OBJECT_FOREIGN_SERVER:
     620          90 :             ExecGrant_common(istmt, ForeignServerRelationId, ACL_ALL_RIGHTS_FOREIGN_SERVER, NULL);
     621          78 :             break;
     622        6514 :         case OBJECT_FUNCTION:
     623             :         case OBJECT_PROCEDURE:
     624             :         case OBJECT_ROUTINE:
     625        6514 :             ExecGrant_common(istmt, ProcedureRelationId, ACL_ALL_RIGHTS_FUNCTION, NULL);
     626        6514 :             break;
     627          42 :         case OBJECT_LANGUAGE:
     628          42 :             ExecGrant_common(istmt, LanguageRelationId, ACL_ALL_RIGHTS_LANGUAGE, ExecGrant_Language_check);
     629          36 :             break;
     630          74 :         case OBJECT_LARGEOBJECT:
     631          74 :             ExecGrant_Largeobject(istmt);
     632          74 :             break;
     633         300 :         case OBJECT_SCHEMA:
     634         300 :             ExecGrant_common(istmt, NamespaceRelationId, ACL_ALL_RIGHTS_SCHEMA, NULL);
     635         300 :             break;
     636           0 :         case OBJECT_TABLESPACE:
     637           0 :             ExecGrant_common(istmt, TableSpaceRelationId, ACL_ALL_RIGHTS_TABLESPACE, NULL);
     638           0 :             break;
     639          96 :         case OBJECT_PARAMETER_ACL:
     640          96 :             ExecGrant_Parameter(istmt);
     641          96 :             break;
     642           0 :         default:
     643           0 :             elog(ERROR, "unrecognized GrantStmt.objtype: %d",
     644             :                  (int) istmt->objtype);
     645             :     }
     646             : 
     647             :     /*
     648             :      * Pass the info to event triggers about the just-executed GRANT.  Note
     649             :      * that we prefer to do it after actually executing it, because that gives
     650             :      * the functions a chance to adjust the istmt with privileges actually
     651             :      * granted.
     652             :      */
     653       15466 :     if (EventTriggerSupportsObjectType(istmt->objtype))
     654       15086 :         EventTriggerCollectGrant(istmt);
     655       15466 : }
     656             : 
     657             : /*
     658             :  * objectNamesToOids
     659             :  *
     660             :  * Turn a list of object names of a given type into an Oid list.
     661             :  *
     662             :  * XXX: This function doesn't take any sort of locks on the objects whose
     663             :  * names it looks up.  In the face of concurrent DDL, we might easily latch
     664             :  * onto an old version of an object, causing the GRANT or REVOKE statement
     665             :  * to fail.
     666             :  */
     667             : static List *
     668       15346 : objectNamesToOids(ObjectType objtype, List *objnames, bool is_grant)
     669             : {
     670       15346 :     List       *objects = NIL;
     671             :     ListCell   *cell;
     672             : 
     673             :     Assert(objnames != NIL);
     674             : 
     675       15346 :     switch (objtype)
     676             :     {
     677        7794 :         case OBJECT_TABLE:
     678             :         case OBJECT_SEQUENCE:
     679       15642 :             foreach(cell, objnames)
     680             :             {
     681        7848 :                 RangeVar   *relvar = (RangeVar *) lfirst(cell);
     682             :                 Oid         relOid;
     683             : 
     684        7848 :                 relOid = RangeVarGetRelid(relvar, NoLock, false);
     685        7848 :                 objects = lappend_oid(objects, relOid);
     686             :             }
     687        7794 :             break;
     688         274 :         case OBJECT_DATABASE:
     689         548 :             foreach(cell, objnames)
     690             :             {
     691         274 :                 char       *dbname = strVal(lfirst(cell));
     692             :                 Oid         dbid;
     693             : 
     694         274 :                 dbid = get_database_oid(dbname, false);
     695         274 :                 objects = lappend_oid(objects, dbid);
     696             :             }
     697         274 :             break;
     698         136 :         case OBJECT_DOMAIN:
     699             :         case OBJECT_TYPE:
     700         272 :             foreach(cell, objnames)
     701             :             {
     702         136 :                 List       *typname = (List *) lfirst(cell);
     703             :                 Oid         oid;
     704             : 
     705         136 :                 oid = typenameTypeId(NULL, makeTypeNameFromNameList(typname));
     706         136 :                 objects = lappend_oid(objects, oid);
     707             :             }
     708         136 :             break;
     709        6450 :         case OBJECT_FUNCTION:
     710       12912 :             foreach(cell, objnames)
     711             :             {
     712        6474 :                 ObjectWithArgs *func = (ObjectWithArgs *) lfirst(cell);
     713             :                 Oid         funcid;
     714             : 
     715        6474 :                 funcid = LookupFuncWithArgs(OBJECT_FUNCTION, func, false);
     716        6462 :                 objects = lappend_oid(objects, funcid);
     717             :             }
     718        6438 :             break;
     719          42 :         case OBJECT_LANGUAGE:
     720          84 :             foreach(cell, objnames)
     721             :             {
     722          42 :                 char       *langname = strVal(lfirst(cell));
     723             :                 Oid         oid;
     724             : 
     725          42 :                 oid = get_language_oid(langname, false);
     726          42 :                 objects = lappend_oid(objects, oid);
     727             :             }
     728          42 :             break;
     729          80 :         case OBJECT_LARGEOBJECT:
     730         154 :             foreach(cell, objnames)
     731             :             {
     732          86 :                 Oid         lobjOid = oidparse(lfirst(cell));
     733             : 
     734          86 :                 if (!LargeObjectExists(lobjOid))
     735          12 :                     ereport(ERROR,
     736             :                             (errcode(ERRCODE_UNDEFINED_OBJECT),
     737             :                              errmsg("large object %u does not exist",
     738             :                                     lobjOid)));
     739             : 
     740          74 :                 objects = lappend_oid(objects, lobjOid);
     741             :             }
     742          68 :             break;
     743         286 :         case OBJECT_SCHEMA:
     744         654 :             foreach(cell, objnames)
     745             :             {
     746         368 :                 char       *nspname = strVal(lfirst(cell));
     747             :                 Oid         oid;
     748             : 
     749         368 :                 oid = get_namespace_oid(nspname, false);
     750         368 :                 objects = lappend_oid(objects, oid);
     751             :             }
     752         286 :             break;
     753          42 :         case OBJECT_PROCEDURE:
     754          84 :             foreach(cell, objnames)
     755             :             {
     756          42 :                 ObjectWithArgs *func = (ObjectWithArgs *) lfirst(cell);
     757             :                 Oid         procid;
     758             : 
     759          42 :                 procid = LookupFuncWithArgs(OBJECT_PROCEDURE, func, false);
     760          42 :                 objects = lappend_oid(objects, procid);
     761             :             }
     762          42 :             break;
     763           0 :         case OBJECT_ROUTINE:
     764           0 :             foreach(cell, objnames)
     765             :             {
     766           0 :                 ObjectWithArgs *func = (ObjectWithArgs *) lfirst(cell);
     767             :                 Oid         routid;
     768             : 
     769           0 :                 routid = LookupFuncWithArgs(OBJECT_ROUTINE, func, false);
     770           0 :                 objects = lappend_oid(objects, routid);
     771             :             }
     772           0 :             break;
     773           0 :         case OBJECT_TABLESPACE:
     774           0 :             foreach(cell, objnames)
     775             :             {
     776           0 :                 char       *spcname = strVal(lfirst(cell));
     777             :                 Oid         spcoid;
     778             : 
     779           0 :                 spcoid = get_tablespace_oid(spcname, false);
     780           0 :                 objects = lappend_oid(objects, spcoid);
     781             :             }
     782           0 :             break;
     783          92 :         case OBJECT_FDW:
     784         184 :             foreach(cell, objnames)
     785             :             {
     786          92 :                 char       *fdwname = strVal(lfirst(cell));
     787          92 :                 Oid         fdwid = get_foreign_data_wrapper_oid(fdwname, false);
     788             : 
     789          92 :                 objects = lappend_oid(objects, fdwid);
     790             :             }
     791          92 :             break;
     792          76 :         case OBJECT_FOREIGN_SERVER:
     793         152 :             foreach(cell, objnames)
     794             :             {
     795          76 :                 char       *srvname = strVal(lfirst(cell));
     796          76 :                 Oid         srvid = get_foreign_server_oid(srvname, false);
     797             : 
     798          76 :                 objects = lappend_oid(objects, srvid);
     799             :             }
     800          76 :             break;
     801          74 :         case OBJECT_PARAMETER_ACL:
     802         196 :             foreach(cell, objnames)
     803             :             {
     804             :                 /*
     805             :                  * In this code we represent a GUC by the OID of its entry in
     806             :                  * pg_parameter_acl, which we have to manufacture here if it
     807             :                  * doesn't exist yet.  (That's a hack for sure, but it avoids
     808             :                  * messing with all the GRANT/REVOKE infrastructure that
     809             :                  * expects to use OIDs for object identities.)  However, if
     810             :                  * this is a REVOKE, we can instead just ignore any GUCs that
     811             :                  * don't have such an entry, as they must not have any
     812             :                  * privileges needing removal.
     813             :                  */
     814         124 :                 char       *parameter = strVal(lfirst(cell));
     815         124 :                 Oid         parameterId = ParameterAclLookup(parameter, true);
     816             : 
     817         124 :                 if (!OidIsValid(parameterId) && is_grant)
     818             :                 {
     819          68 :                     parameterId = ParameterAclCreate(parameter);
     820             : 
     821             :                     /*
     822             :                      * Prevent error when processing duplicate objects, and
     823             :                      * make this new entry visible so that ExecGrant_Parameter
     824             :                      * can update it.
     825             :                      */
     826          66 :                     CommandCounterIncrement();
     827             :                 }
     828         122 :                 if (OidIsValid(parameterId))
     829         110 :                     objects = lappend_oid(objects, parameterId);
     830             :             }
     831          72 :             break;
     832           0 :         default:
     833           0 :             elog(ERROR, "unrecognized GrantStmt.objtype: %d",
     834             :                  (int) objtype);
     835             :     }
     836             : 
     837       15320 :     return objects;
     838             : }
     839             : 
     840             : /*
     841             :  * objectsInSchemaToOids
     842             :  *
     843             :  * Find all objects of a given type in specified schemas, and make a list
     844             :  * of their Oids.  We check USAGE privilege on the schemas, but there is
     845             :  * no privilege checking on the individual objects here.
     846             :  */
     847             : static List *
     848          30 : objectsInSchemaToOids(ObjectType objtype, List *nspnames)
     849             : {
     850          30 :     List       *objects = NIL;
     851             :     ListCell   *cell;
     852             : 
     853          60 :     foreach(cell, nspnames)
     854             :     {
     855          30 :         char       *nspname = strVal(lfirst(cell));
     856             :         Oid         namespaceId;
     857             :         List       *objs;
     858             : 
     859          30 :         namespaceId = LookupExplicitNamespace(nspname, false);
     860             : 
     861          30 :         switch (objtype)
     862             :         {
     863          12 :             case OBJECT_TABLE:
     864          12 :                 objs = getRelationsInNamespace(namespaceId, RELKIND_RELATION);
     865          12 :                 objects = list_concat(objects, objs);
     866          12 :                 objs = getRelationsInNamespace(namespaceId, RELKIND_VIEW);
     867          12 :                 objects = list_concat(objects, objs);
     868          12 :                 objs = getRelationsInNamespace(namespaceId, RELKIND_MATVIEW);
     869          12 :                 objects = list_concat(objects, objs);
     870          12 :                 objs = getRelationsInNamespace(namespaceId, RELKIND_FOREIGN_TABLE);
     871          12 :                 objects = list_concat(objects, objs);
     872          12 :                 objs = getRelationsInNamespace(namespaceId, RELKIND_PARTITIONED_TABLE);
     873          12 :                 objects = list_concat(objects, objs);
     874          12 :                 break;
     875           0 :             case OBJECT_SEQUENCE:
     876           0 :                 objs = getRelationsInNamespace(namespaceId, RELKIND_SEQUENCE);
     877           0 :                 objects = list_concat(objects, objs);
     878           0 :                 break;
     879          18 :             case OBJECT_FUNCTION:
     880             :             case OBJECT_PROCEDURE:
     881             :             case OBJECT_ROUTINE:
     882             :                 {
     883             :                     ScanKeyData key[2];
     884             :                     int         keycount;
     885             :                     Relation    rel;
     886             :                     TableScanDesc scan;
     887             :                     HeapTuple   tuple;
     888             : 
     889          18 :                     keycount = 0;
     890          18 :                     ScanKeyInit(&key[keycount++],
     891             :                                 Anum_pg_proc_pronamespace,
     892             :                                 BTEqualStrategyNumber, F_OIDEQ,
     893             :                                 ObjectIdGetDatum(namespaceId));
     894             : 
     895          18 :                     if (objtype == OBJECT_FUNCTION)
     896             :                         /* includes aggregates and window functions */
     897           6 :                         ScanKeyInit(&key[keycount++],
     898             :                                     Anum_pg_proc_prokind,
     899             :                                     BTEqualStrategyNumber, F_CHARNE,
     900             :                                     CharGetDatum(PROKIND_PROCEDURE));
     901          12 :                     else if (objtype == OBJECT_PROCEDURE)
     902           6 :                         ScanKeyInit(&key[keycount++],
     903             :                                     Anum_pg_proc_prokind,
     904             :                                     BTEqualStrategyNumber, F_CHAREQ,
     905             :                                     CharGetDatum(PROKIND_PROCEDURE));
     906             : 
     907          18 :                     rel = table_open(ProcedureRelationId, AccessShareLock);
     908          18 :                     scan = table_beginscan_catalog(rel, keycount, key);
     909             : 
     910          54 :                     while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
     911             :                     {
     912          36 :                         Oid         oid = ((Form_pg_proc) GETSTRUCT(tuple))->oid;
     913             : 
     914          36 :                         objects = lappend_oid(objects, oid);
     915             :                     }
     916             : 
     917          18 :                     table_endscan(scan);
     918          18 :                     table_close(rel, AccessShareLock);
     919             :                 }
     920          18 :                 break;
     921           0 :             default:
     922             :                 /* should not happen */
     923           0 :                 elog(ERROR, "unrecognized GrantStmt.objtype: %d",
     924             :                      (int) objtype);
     925             :         }
     926             :     }
     927             : 
     928          30 :     return objects;
     929             : }
     930             : 
     931             : /*
     932             :  * getRelationsInNamespace
     933             :  *
     934             :  * Return Oid list of relations in given namespace filtered by relation kind
     935             :  */
     936             : static List *
     937          60 : getRelationsInNamespace(Oid namespaceId, char relkind)
     938             : {
     939          60 :     List       *relations = NIL;
     940             :     ScanKeyData key[2];
     941             :     Relation    rel;
     942             :     TableScanDesc scan;
     943             :     HeapTuple   tuple;
     944             : 
     945          60 :     ScanKeyInit(&key[0],
     946             :                 Anum_pg_class_relnamespace,
     947             :                 BTEqualStrategyNumber, F_OIDEQ,
     948             :                 ObjectIdGetDatum(namespaceId));
     949          60 :     ScanKeyInit(&key[1],
     950             :                 Anum_pg_class_relkind,
     951             :                 BTEqualStrategyNumber, F_CHAREQ,
     952             :                 CharGetDatum(relkind));
     953             : 
     954          60 :     rel = table_open(RelationRelationId, AccessShareLock);
     955          60 :     scan = table_beginscan_catalog(rel, 2, key);
     956             : 
     957          84 :     while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
     958             :     {
     959          24 :         Oid         oid = ((Form_pg_class) GETSTRUCT(tuple))->oid;
     960             : 
     961          24 :         relations = lappend_oid(relations, oid);
     962             :     }
     963             : 
     964          60 :     table_endscan(scan);
     965          60 :     table_close(rel, AccessShareLock);
     966             : 
     967          60 :     return relations;
     968             : }
     969             : 
     970             : 
     971             : /*
     972             :  * ALTER DEFAULT PRIVILEGES statement
     973             :  */
     974             : void
     975         160 : ExecAlterDefaultPrivilegesStmt(ParseState *pstate, AlterDefaultPrivilegesStmt *stmt)
     976             : {
     977         160 :     GrantStmt  *action = stmt->action;
     978             :     InternalDefaultACL iacls;
     979             :     ListCell   *cell;
     980         160 :     List       *rolespecs = NIL;
     981         160 :     List       *nspnames = NIL;
     982         160 :     DefElem    *drolespecs = NULL;
     983         160 :     DefElem    *dnspnames = NULL;
     984             :     AclMode     all_privileges;
     985             :     const char *errormsg;
     986             : 
     987             :     /* Deconstruct the "options" part of the statement */
     988         282 :     foreach(cell, stmt->options)
     989             :     {
     990         122 :         DefElem    *defel = (DefElem *) lfirst(cell);
     991             : 
     992         122 :         if (strcmp(defel->defname, "schemas") == 0)
     993             :         {
     994          54 :             if (dnspnames)
     995           0 :                 errorConflictingDefElem(defel, pstate);
     996          54 :             dnspnames = defel;
     997             :         }
     998          68 :         else if (strcmp(defel->defname, "roles") == 0)
     999             :         {
    1000          68 :             if (drolespecs)
    1001           0 :                 errorConflictingDefElem(defel, pstate);
    1002          68 :             drolespecs = defel;
    1003             :         }
    1004             :         else
    1005           0 :             elog(ERROR, "option \"%s\" not recognized", defel->defname);
    1006             :     }
    1007             : 
    1008         160 :     if (dnspnames)
    1009          54 :         nspnames = (List *) dnspnames->arg;
    1010         160 :     if (drolespecs)
    1011          68 :         rolespecs = (List *) drolespecs->arg;
    1012             : 
    1013             :     /* Prepare the InternalDefaultACL representation of the statement */
    1014             :     /* roleid to be filled below */
    1015             :     /* nspid to be filled in SetDefaultACLsInSchemas */
    1016         160 :     iacls.is_grant = action->is_grant;
    1017         160 :     iacls.objtype = action->objtype;
    1018             :     /* all_privs to be filled below */
    1019             :     /* privileges to be filled below */
    1020         160 :     iacls.grantees = NIL;       /* filled below */
    1021         160 :     iacls.grant_option = action->grant_option;
    1022         160 :     iacls.behavior = action->behavior;
    1023             : 
    1024             :     /*
    1025             :      * Convert the RoleSpec list into an Oid list.  Note that at this point we
    1026             :      * insert an ACL_ID_PUBLIC into the list if appropriate, so downstream
    1027             :      * there shouldn't be any additional work needed to support this case.
    1028             :      */
    1029         326 :     foreach(cell, action->grantees)
    1030             :     {
    1031         166 :         RoleSpec   *grantee = (RoleSpec *) lfirst(cell);
    1032             :         Oid         grantee_uid;
    1033             : 
    1034         166 :         switch (grantee->roletype)
    1035             :         {
    1036          40 :             case ROLESPEC_PUBLIC:
    1037          40 :                 grantee_uid = ACL_ID_PUBLIC;
    1038          40 :                 break;
    1039         126 :             default:
    1040         126 :                 grantee_uid = get_rolespec_oid(grantee, false);
    1041         126 :                 break;
    1042             :         }
    1043         166 :         iacls.grantees = lappend_oid(iacls.grantees, grantee_uid);
    1044             :     }
    1045             : 
    1046             :     /*
    1047             :      * Convert action->privileges, a list of privilege strings, into an
    1048             :      * AclMode bitmask.
    1049             :      */
    1050         160 :     switch (action->objtype)
    1051             :     {
    1052          78 :         case OBJECT_TABLE:
    1053          78 :             all_privileges = ACL_ALL_RIGHTS_RELATION;
    1054          78 :             errormsg = gettext_noop("invalid privilege type %s for relation");
    1055          78 :             break;
    1056           6 :         case OBJECT_SEQUENCE:
    1057           6 :             all_privileges = ACL_ALL_RIGHTS_SEQUENCE;
    1058           6 :             errormsg = gettext_noop("invalid privilege type %s for sequence");
    1059           6 :             break;
    1060          22 :         case OBJECT_FUNCTION:
    1061          22 :             all_privileges = ACL_ALL_RIGHTS_FUNCTION;
    1062          22 :             errormsg = gettext_noop("invalid privilege type %s for function");
    1063          22 :             break;
    1064           0 :         case OBJECT_PROCEDURE:
    1065           0 :             all_privileges = ACL_ALL_RIGHTS_FUNCTION;
    1066           0 :             errormsg = gettext_noop("invalid privilege type %s for procedure");
    1067           0 :             break;
    1068           0 :         case OBJECT_ROUTINE:
    1069           0 :             all_privileges = ACL_ALL_RIGHTS_FUNCTION;
    1070           0 :             errormsg = gettext_noop("invalid privilege type %s for routine");
    1071           0 :             break;
    1072          18 :         case OBJECT_TYPE:
    1073          18 :             all_privileges = ACL_ALL_RIGHTS_TYPE;
    1074          18 :             errormsg = gettext_noop("invalid privilege type %s for type");
    1075          18 :             break;
    1076          36 :         case OBJECT_SCHEMA:
    1077          36 :             all_privileges = ACL_ALL_RIGHTS_SCHEMA;
    1078          36 :             errormsg = gettext_noop("invalid privilege type %s for schema");
    1079          36 :             break;
    1080           0 :         default:
    1081           0 :             elog(ERROR, "unrecognized GrantStmt.objtype: %d",
    1082             :                  (int) action->objtype);
    1083             :             /* keep compiler quiet */
    1084             :             all_privileges = ACL_NO_RIGHTS;
    1085             :             errormsg = NULL;
    1086             :     }
    1087             : 
    1088         160 :     if (action->privileges == NIL)
    1089             :     {
    1090          56 :         iacls.all_privs = true;
    1091             : 
    1092             :         /*
    1093             :          * will be turned into ACL_ALL_RIGHTS_* by the internal routines
    1094             :          * depending on the object type
    1095             :          */
    1096          56 :         iacls.privileges = ACL_NO_RIGHTS;
    1097             :     }
    1098             :     else
    1099             :     {
    1100         104 :         iacls.all_privs = false;
    1101         104 :         iacls.privileges = ACL_NO_RIGHTS;
    1102             : 
    1103         208 :         foreach(cell, action->privileges)
    1104             :         {
    1105         104 :             AccessPriv *privnode = (AccessPriv *) lfirst(cell);
    1106             :             AclMode     priv;
    1107             : 
    1108         104 :             if (privnode->cols)
    1109           0 :                 ereport(ERROR,
    1110             :                         (errcode(ERRCODE_INVALID_GRANT_OPERATION),
    1111             :                          errmsg("default privileges cannot be set for columns")));
    1112             : 
    1113         104 :             if (privnode->priv_name == NULL) /* parser mistake? */
    1114           0 :                 elog(ERROR, "AccessPriv node must specify privilege");
    1115         104 :             priv = string_to_privilege(privnode->priv_name);
    1116             : 
    1117         104 :             if (priv & ~((AclMode) all_privileges))
    1118           0 :                 ereport(ERROR,
    1119             :                         (errcode(ERRCODE_INVALID_GRANT_OPERATION),
    1120             :                          errmsg(errormsg, privilege_to_string(priv))));
    1121             : 
    1122         104 :             iacls.privileges |= priv;
    1123             :         }
    1124             :     }
    1125             : 
    1126         160 :     if (rolespecs == NIL)
    1127             :     {
    1128             :         /* Set permissions for myself */
    1129          92 :         iacls.roleid = GetUserId();
    1130             : 
    1131          92 :         SetDefaultACLsInSchemas(&iacls, nspnames);
    1132             :     }
    1133             :     else
    1134             :     {
    1135             :         /* Look up the role OIDs and do permissions checks */
    1136             :         ListCell   *rolecell;
    1137             : 
    1138         136 :         foreach(rolecell, rolespecs)
    1139             :         {
    1140          68 :             RoleSpec   *rolespec = lfirst(rolecell);
    1141             : 
    1142          68 :             iacls.roleid = get_rolespec_oid(rolespec, false);
    1143             : 
    1144          68 :             if (!has_privs_of_role(GetUserId(), iacls.roleid))
    1145           0 :                 ereport(ERROR,
    1146             :                         (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
    1147             :                          errmsg("permission denied to change default privileges")));
    1148             : 
    1149          68 :             SetDefaultACLsInSchemas(&iacls, nspnames);
    1150             :         }
    1151             :     }
    1152         154 : }
    1153             : 
    1154             : /*
    1155             :  * Process ALTER DEFAULT PRIVILEGES for a list of target schemas
    1156             :  *
    1157             :  * All fields of *iacls except nspid were filled already
    1158             :  */
    1159             : static void
    1160         160 : SetDefaultACLsInSchemas(InternalDefaultACL *iacls, List *nspnames)
    1161             : {
    1162         160 :     if (nspnames == NIL)
    1163             :     {
    1164             :         /* Set database-wide permissions if no schema was specified */
    1165         106 :         iacls->nspid = InvalidOid;
    1166             : 
    1167         106 :         SetDefaultACL(iacls);
    1168             :     }
    1169             :     else
    1170             :     {
    1171             :         /* Look up the schema OIDs and set permissions for each one */
    1172             :         ListCell   *nspcell;
    1173             : 
    1174         108 :         foreach(nspcell, nspnames)
    1175             :         {
    1176          60 :             char       *nspname = strVal(lfirst(nspcell));
    1177             : 
    1178          60 :             iacls->nspid = get_namespace_oid(nspname, false);
    1179             : 
    1180             :             /*
    1181             :              * We used to insist that the target role have CREATE privileges
    1182             :              * on the schema, since without that it wouldn't be able to create
    1183             :              * an object for which these default privileges would apply.
    1184             :              * However, this check proved to be more confusing than helpful,
    1185             :              * and it also caused certain database states to not be
    1186             :              * dumpable/restorable, since revoking CREATE doesn't cause
    1187             :              * default privileges for the schema to go away.  So now, we just
    1188             :              * allow the ALTER; if the user lacks CREATE he'll find out when
    1189             :              * he tries to create an object.
    1190             :              */
    1191             : 
    1192          60 :             SetDefaultACL(iacls);
    1193             :         }
    1194             :     }
    1195         154 : }
    1196             : 
    1197             : 
    1198             : /*
    1199             :  * Create or update a pg_default_acl entry
    1200             :  */
    1201             : static void
    1202         196 : SetDefaultACL(InternalDefaultACL *iacls)
    1203             : {
    1204         196 :     AclMode     this_privileges = iacls->privileges;
    1205             :     char        objtype;
    1206             :     Relation    rel;
    1207             :     HeapTuple   tuple;
    1208             :     bool        isNew;
    1209             :     Acl        *def_acl;
    1210             :     Acl        *old_acl;
    1211             :     Acl        *new_acl;
    1212             :     HeapTuple   newtuple;
    1213             :     int         noldmembers;
    1214             :     int         nnewmembers;
    1215             :     Oid        *oldmembers;
    1216             :     Oid        *newmembers;
    1217             : 
    1218         196 :     rel = table_open(DefaultAclRelationId, RowExclusiveLock);
    1219             : 
    1220             :     /*
    1221             :      * The default for a global entry is the hard-wired default ACL for the
    1222             :      * particular object type.  The default for non-global entries is an empty
    1223             :      * ACL.  This must be so because global entries replace the hard-wired
    1224             :      * defaults, while others are added on.
    1225             :      */
    1226         196 :     if (!OidIsValid(iacls->nspid))
    1227         136 :         def_acl = acldefault(iacls->objtype, iacls->roleid);
    1228             :     else
    1229          60 :         def_acl = make_empty_acl();
    1230             : 
    1231             :     /*
    1232             :      * Convert ACL object type to pg_default_acl object type and handle
    1233             :      * all_privs option
    1234             :      */
    1235         196 :     switch (iacls->objtype)
    1236             :     {
    1237          90 :         case OBJECT_TABLE:
    1238          90 :             objtype = DEFACLOBJ_RELATION;
    1239          90 :             if (iacls->all_privs && this_privileges == ACL_NO_RIGHTS)
    1240          26 :                 this_privileges = ACL_ALL_RIGHTS_RELATION;
    1241          90 :             break;
    1242             : 
    1243          12 :         case OBJECT_SEQUENCE:
    1244          12 :             objtype = DEFACLOBJ_SEQUENCE;
    1245          12 :             if (iacls->all_privs && this_privileges == ACL_NO_RIGHTS)
    1246          12 :                 this_privileges = ACL_ALL_RIGHTS_SEQUENCE;
    1247          12 :             break;
    1248             : 
    1249          28 :         case OBJECT_FUNCTION:
    1250          28 :             objtype = DEFACLOBJ_FUNCTION;
    1251          28 :             if (iacls->all_privs && this_privileges == ACL_NO_RIGHTS)
    1252          12 :                 this_privileges = ACL_ALL_RIGHTS_FUNCTION;
    1253          28 :             break;
    1254             : 
    1255          24 :         case OBJECT_TYPE:
    1256          24 :             objtype = DEFACLOBJ_TYPE;
    1257          24 :             if (iacls->all_privs && this_privileges == ACL_NO_RIGHTS)
    1258          12 :                 this_privileges = ACL_ALL_RIGHTS_TYPE;
    1259          24 :             break;
    1260             : 
    1261          42 :         case OBJECT_SCHEMA:
    1262          42 :             if (OidIsValid(iacls->nspid))
    1263           6 :                 ereport(ERROR,
    1264             :                         (errcode(ERRCODE_INVALID_GRANT_OPERATION),
    1265             :                          errmsg("cannot use IN SCHEMA clause when using GRANT/REVOKE ON SCHEMAS")));
    1266          36 :             objtype = DEFACLOBJ_NAMESPACE;
    1267          36 :             if (iacls->all_privs && this_privileges == ACL_NO_RIGHTS)
    1268          24 :                 this_privileges = ACL_ALL_RIGHTS_SCHEMA;
    1269          36 :             break;
    1270             : 
    1271           0 :         default:
    1272           0 :             elog(ERROR, "unrecognized object type: %d",
    1273             :                  (int) iacls->objtype);
    1274             :             objtype = 0;        /* keep compiler quiet */
    1275             :             break;
    1276             :     }
    1277             : 
    1278             :     /* Search for existing row for this object type in catalog */
    1279         190 :     tuple = SearchSysCache3(DEFACLROLENSPOBJ,
    1280             :                             ObjectIdGetDatum(iacls->roleid),
    1281             :                             ObjectIdGetDatum(iacls->nspid),
    1282             :                             CharGetDatum(objtype));
    1283             : 
    1284         190 :     if (HeapTupleIsValid(tuple))
    1285             :     {
    1286             :         Datum       aclDatum;
    1287             :         bool        isNull;
    1288             : 
    1289          72 :         aclDatum = SysCacheGetAttr(DEFACLROLENSPOBJ, tuple,
    1290             :                                    Anum_pg_default_acl_defaclacl,
    1291             :                                    &isNull);
    1292          72 :         if (!isNull)
    1293          72 :             old_acl = DatumGetAclPCopy(aclDatum);
    1294             :         else
    1295           0 :             old_acl = NULL;     /* this case shouldn't happen, probably */
    1296          72 :         isNew = false;
    1297             :     }
    1298             :     else
    1299             :     {
    1300         118 :         old_acl = NULL;
    1301         118 :         isNew = true;
    1302             :     }
    1303             : 
    1304         190 :     if (old_acl != NULL)
    1305             :     {
    1306             :         /*
    1307             :          * We need the members of both old and new ACLs so we can correct the
    1308             :          * shared dependency information.  Collect data before
    1309             :          * merge_acl_with_grant throws away old_acl.
    1310             :          */
    1311          72 :         noldmembers = aclmembers(old_acl, &oldmembers);
    1312             :     }
    1313             :     else
    1314             :     {
    1315             :         /* If no or null entry, start with the default ACL value */
    1316         118 :         old_acl = aclcopy(def_acl);
    1317             :         /* There are no old member roles according to the catalogs */
    1318         118 :         noldmembers = 0;
    1319         118 :         oldmembers = NULL;
    1320             :     }
    1321             : 
    1322             :     /*
    1323             :      * Generate new ACL.  Grantor of rights is always the same as the target
    1324             :      * role.
    1325             :      */
    1326         190 :     new_acl = merge_acl_with_grant(old_acl,
    1327         190 :                                    iacls->is_grant,
    1328         190 :                                    iacls->grant_option,
    1329             :                                    iacls->behavior,
    1330             :                                    iacls->grantees,
    1331             :                                    this_privileges,
    1332             :                                    iacls->roleid,
    1333             :                                    iacls->roleid);
    1334             : 
    1335             :     /*
    1336             :      * If the result is the same as the default value, we do not need an
    1337             :      * explicit pg_default_acl entry, and should in fact remove the entry if
    1338             :      * it exists.  Must sort both arrays to compare properly.
    1339             :      */
    1340         190 :     aclitemsort(new_acl);
    1341         190 :     aclitemsort(def_acl);
    1342         190 :     if (aclequal(new_acl, def_acl))
    1343             :     {
    1344             :         /* delete old entry, if indeed there is one */
    1345          56 :         if (!isNew)
    1346             :         {
    1347             :             ObjectAddress myself;
    1348             : 
    1349             :             /*
    1350             :              * The dependency machinery will take care of removing all
    1351             :              * associated dependency entries.  We use DROP_RESTRICT since
    1352             :              * there shouldn't be anything depending on this entry.
    1353             :              */
    1354          54 :             myself.classId = DefaultAclRelationId;
    1355          54 :             myself.objectId = ((Form_pg_default_acl) GETSTRUCT(tuple))->oid;
    1356          54 :             myself.objectSubId = 0;
    1357             : 
    1358          54 :             performDeletion(&myself, DROP_RESTRICT, 0);
    1359             :         }
    1360             :     }
    1361             :     else
    1362             :     {
    1363         134 :         Datum       values[Natts_pg_default_acl] = {0};
    1364         134 :         bool        nulls[Natts_pg_default_acl] = {0};
    1365         134 :         bool        replaces[Natts_pg_default_acl] = {0};
    1366             :         Oid         defAclOid;
    1367             : 
    1368         134 :         if (isNew)
    1369             :         {
    1370             :             /* insert new entry */
    1371         116 :             defAclOid = GetNewOidWithIndex(rel, DefaultAclOidIndexId,
    1372             :                                            Anum_pg_default_acl_oid);
    1373         116 :             values[Anum_pg_default_acl_oid - 1] = ObjectIdGetDatum(defAclOid);
    1374         116 :             values[Anum_pg_default_acl_defaclrole - 1] = ObjectIdGetDatum(iacls->roleid);
    1375         116 :             values[Anum_pg_default_acl_defaclnamespace - 1] = ObjectIdGetDatum(iacls->nspid);
    1376         116 :             values[Anum_pg_default_acl_defaclobjtype - 1] = CharGetDatum(objtype);
    1377         116 :             values[Anum_pg_default_acl_defaclacl - 1] = PointerGetDatum(new_acl);
    1378             : 
    1379         116 :             newtuple = heap_form_tuple(RelationGetDescr(rel), values, nulls);
    1380         116 :             CatalogTupleInsert(rel, newtuple);
    1381             :         }
    1382             :         else
    1383             :         {
    1384          18 :             defAclOid = ((Form_pg_default_acl) GETSTRUCT(tuple))->oid;
    1385             : 
    1386             :             /* update existing entry */
    1387          18 :             values[Anum_pg_default_acl_defaclacl - 1] = PointerGetDatum(new_acl);
    1388          18 :             replaces[Anum_pg_default_acl_defaclacl - 1] = true;
    1389             : 
    1390          18 :             newtuple = heap_modify_tuple(tuple, RelationGetDescr(rel),
    1391             :                                          values, nulls, replaces);
    1392          18 :             CatalogTupleUpdate(rel, &newtuple->t_self, newtuple);
    1393             :         }
    1394             : 
    1395             :         /* these dependencies don't change in an update */
    1396         134 :         if (isNew)
    1397             :         {
    1398             :             /* dependency on role */
    1399         116 :             recordDependencyOnOwner(DefaultAclRelationId, defAclOid,
    1400             :                                     iacls->roleid);
    1401             : 
    1402             :             /* dependency on namespace */
    1403         116 :             if (OidIsValid(iacls->nspid))
    1404             :             {
    1405             :                 ObjectAddress myself,
    1406             :                             referenced;
    1407             : 
    1408          34 :                 myself.classId = DefaultAclRelationId;
    1409          34 :                 myself.objectId = defAclOid;
    1410          34 :                 myself.objectSubId = 0;
    1411             : 
    1412          34 :                 referenced.classId = NamespaceRelationId;
    1413          34 :                 referenced.objectId = iacls->nspid;
    1414          34 :                 referenced.objectSubId = 0;
    1415             : 
    1416          34 :                 recordDependencyOn(&myself, &referenced, DEPENDENCY_AUTO);
    1417             :             }
    1418             :         }
    1419             : 
    1420             :         /*
    1421             :          * Update the shared dependency ACL info
    1422             :          */
    1423         134 :         nnewmembers = aclmembers(new_acl, &newmembers);
    1424             : 
    1425         134 :         updateAclDependencies(DefaultAclRelationId,
    1426             :                               defAclOid, 0,
    1427             :                               iacls->roleid,
    1428             :                               noldmembers, oldmembers,
    1429             :                               nnewmembers, newmembers);
    1430             : 
    1431         134 :         if (isNew)
    1432         116 :             InvokeObjectPostCreateHook(DefaultAclRelationId, defAclOid, 0);
    1433             :         else
    1434          18 :             InvokeObjectPostAlterHook(DefaultAclRelationId, defAclOid, 0);
    1435             :     }
    1436             : 
    1437         190 :     if (HeapTupleIsValid(tuple))
    1438          72 :         ReleaseSysCache(tuple);
    1439             : 
    1440         190 :     table_close(rel, RowExclusiveLock);
    1441             : 
    1442             :     /* prevent error when processing duplicate objects */
    1443         190 :     CommandCounterIncrement();
    1444         190 : }
    1445             : 
    1446             : 
    1447             : /*
    1448             :  * RemoveRoleFromObjectACL
    1449             :  *
    1450             :  * Used by shdepDropOwned to remove mentions of a role in ACLs
    1451             :  */
    1452             : void
    1453         242 : RemoveRoleFromObjectACL(Oid roleid, Oid classid, Oid objid)
    1454             : {
    1455         242 :     if (classid == DefaultAclRelationId)
    1456             :     {
    1457             :         InternalDefaultACL iacls;
    1458             :         Form_pg_default_acl pg_default_acl_tuple;
    1459             :         Relation    rel;
    1460             :         ScanKeyData skey[1];
    1461             :         SysScanDesc scan;
    1462             :         HeapTuple   tuple;
    1463             : 
    1464             :         /* first fetch info needed by SetDefaultACL */
    1465          30 :         rel = table_open(DefaultAclRelationId, AccessShareLock);
    1466             : 
    1467          30 :         ScanKeyInit(&skey[0],
    1468             :                     Anum_pg_default_acl_oid,
    1469             :                     BTEqualStrategyNumber, F_OIDEQ,
    1470             :                     ObjectIdGetDatum(objid));
    1471             : 
    1472          30 :         scan = systable_beginscan(rel, DefaultAclOidIndexId, true,
    1473             :                                   NULL, 1, skey);
    1474             : 
    1475          30 :         tuple = systable_getnext(scan);
    1476             : 
    1477          30 :         if (!HeapTupleIsValid(tuple))
    1478           0 :             elog(ERROR, "could not find tuple for default ACL %u", objid);
    1479             : 
    1480          30 :         pg_default_acl_tuple = (Form_pg_default_acl) GETSTRUCT(tuple);
    1481             : 
    1482          30 :         iacls.roleid = pg_default_acl_tuple->defaclrole;
    1483          30 :         iacls.nspid = pg_default_acl_tuple->defaclnamespace;
    1484             : 
    1485          30 :         switch (pg_default_acl_tuple->defaclobjtype)
    1486             :         {
    1487           6 :             case DEFACLOBJ_RELATION:
    1488           6 :                 iacls.objtype = OBJECT_TABLE;
    1489           6 :                 break;
    1490           6 :             case DEFACLOBJ_SEQUENCE:
    1491           6 :                 iacls.objtype = OBJECT_SEQUENCE;
    1492           6 :                 break;
    1493           6 :             case DEFACLOBJ_FUNCTION:
    1494           6 :                 iacls.objtype = OBJECT_FUNCTION;
    1495           6 :                 break;
    1496           6 :             case DEFACLOBJ_TYPE:
    1497           6 :                 iacls.objtype = OBJECT_TYPE;
    1498           6 :                 break;
    1499           6 :             case DEFACLOBJ_NAMESPACE:
    1500           6 :                 iacls.objtype = OBJECT_SCHEMA;
    1501           6 :                 break;
    1502           0 :             default:
    1503             :                 /* Shouldn't get here */
    1504           0 :                 elog(ERROR, "unexpected default ACL type: %d",
    1505             :                      (int) pg_default_acl_tuple->defaclobjtype);
    1506             :                 break;
    1507             :         }
    1508             : 
    1509          30 :         systable_endscan(scan);
    1510          30 :         table_close(rel, AccessShareLock);
    1511             : 
    1512          30 :         iacls.is_grant = false;
    1513          30 :         iacls.all_privs = true;
    1514          30 :         iacls.privileges = ACL_NO_RIGHTS;
    1515          30 :         iacls.grantees = list_make1_oid(roleid);
    1516          30 :         iacls.grant_option = false;
    1517          30 :         iacls.behavior = DROP_CASCADE;
    1518             : 
    1519             :         /* Do it */
    1520          30 :         SetDefaultACL(&iacls);
    1521             :     }
    1522             :     else
    1523             :     {
    1524             :         InternalGrant istmt;
    1525             : 
    1526         212 :         switch (classid)
    1527             :         {
    1528          92 :             case RelationRelationId:
    1529             :                 /* it's OK to use TABLE for a sequence */
    1530          92 :                 istmt.objtype = OBJECT_TABLE;
    1531          92 :                 break;
    1532          10 :             case DatabaseRelationId:
    1533          10 :                 istmt.objtype = OBJECT_DATABASE;
    1534          10 :                 break;
    1535           4 :             case TypeRelationId:
    1536           4 :                 istmt.objtype = OBJECT_TYPE;
    1537           4 :                 break;
    1538          34 :             case ProcedureRelationId:
    1539          34 :                 istmt.objtype = OBJECT_ROUTINE;
    1540          34 :                 break;
    1541           0 :             case LanguageRelationId:
    1542           0 :                 istmt.objtype = OBJECT_LANGUAGE;
    1543           0 :                 break;
    1544          18 :             case LargeObjectRelationId:
    1545          18 :                 istmt.objtype = OBJECT_LARGEOBJECT;
    1546          18 :                 break;
    1547          14 :             case NamespaceRelationId:
    1548          14 :                 istmt.objtype = OBJECT_SCHEMA;
    1549          14 :                 break;
    1550           0 :             case TableSpaceRelationId:
    1551           0 :                 istmt.objtype = OBJECT_TABLESPACE;
    1552           0 :                 break;
    1553          14 :             case ForeignServerRelationId:
    1554          14 :                 istmt.objtype = OBJECT_FOREIGN_SERVER;
    1555          14 :                 break;
    1556           2 :             case ForeignDataWrapperRelationId:
    1557           2 :                 istmt.objtype = OBJECT_FDW;
    1558           2 :                 break;
    1559          24 :             case ParameterAclRelationId:
    1560          24 :                 istmt.objtype = OBJECT_PARAMETER_ACL;
    1561          24 :                 break;
    1562           0 :             default:
    1563           0 :                 elog(ERROR, "unexpected object class %u", classid);
    1564             :                 break;
    1565             :         }
    1566         212 :         istmt.is_grant = false;
    1567         212 :         istmt.objects = list_make1_oid(objid);
    1568         212 :         istmt.all_privs = true;
    1569         212 :         istmt.privileges = ACL_NO_RIGHTS;
    1570         212 :         istmt.col_privs = NIL;
    1571         212 :         istmt.grantees = list_make1_oid(roleid);
    1572         212 :         istmt.grant_option = false;
    1573         212 :         istmt.behavior = DROP_CASCADE;
    1574             : 
    1575         212 :         ExecGrantStmt_oids(&istmt);
    1576             :     }
    1577         242 : }
    1578             : 
    1579             : 
    1580             : /*
    1581             :  * expand_col_privileges
    1582             :  *
    1583             :  * OR the specified privilege(s) into per-column array entries for each
    1584             :  * specified attribute.  The per-column array is indexed starting at
    1585             :  * FirstLowInvalidHeapAttributeNumber, up to relation's last attribute.
    1586             :  */
    1587             : static void
    1588         410 : expand_col_privileges(List *colnames, Oid table_oid,
    1589             :                       AclMode this_privileges,
    1590             :                       AclMode *col_privileges,
    1591             :                       int num_col_privileges)
    1592             : {
    1593             :     ListCell   *cell;
    1594             : 
    1595        2310 :     foreach(cell, colnames)
    1596             :     {
    1597        1900 :         char       *colname = strVal(lfirst(cell));
    1598             :         AttrNumber  attnum;
    1599             : 
    1600        1900 :         attnum = get_attnum(table_oid, colname);
    1601        1900 :         if (attnum == InvalidAttrNumber)
    1602           0 :             ereport(ERROR,
    1603             :                     (errcode(ERRCODE_UNDEFINED_COLUMN),
    1604             :                      errmsg("column \"%s\" of relation \"%s\" does not exist",
    1605             :                             colname, get_rel_name(table_oid))));
    1606        1900 :         attnum -= FirstLowInvalidHeapAttributeNumber;
    1607        1900 :         if (attnum <= 0 || attnum >= num_col_privileges)
    1608           0 :             elog(ERROR, "column number out of range");    /* safety check */
    1609        1900 :         col_privileges[attnum] |= this_privileges;
    1610             :     }
    1611         410 : }
    1612             : 
    1613             : /*
    1614             :  * expand_all_col_privileges
    1615             :  *
    1616             :  * OR the specified privilege(s) into per-column array entries for each valid
    1617             :  * attribute of a relation.  The per-column array is indexed starting at
    1618             :  * FirstLowInvalidHeapAttributeNumber, up to relation's last attribute.
    1619             :  */
    1620             : static void
    1621        1516 : expand_all_col_privileges(Oid table_oid, Form_pg_class classForm,
    1622             :                           AclMode this_privileges,
    1623             :                           AclMode *col_privileges,
    1624             :                           int num_col_privileges)
    1625             : {
    1626             :     AttrNumber  curr_att;
    1627             : 
    1628             :     Assert(classForm->relnatts - FirstLowInvalidHeapAttributeNumber < num_col_privileges);
    1629        1516 :     for (curr_att = FirstLowInvalidHeapAttributeNumber + 1;
    1630       22790 :          curr_att <= classForm->relnatts;
    1631       21274 :          curr_att++)
    1632             :     {
    1633             :         HeapTuple   attTuple;
    1634             :         bool        isdropped;
    1635             : 
    1636       21274 :         if (curr_att == InvalidAttrNumber)
    1637        1516 :             continue;
    1638             : 
    1639             :         /* Views don't have any system columns at all */
    1640       19758 :         if (classForm->relkind == RELKIND_VIEW && curr_att < 0)
    1641        3816 :             continue;
    1642             : 
    1643       15942 :         attTuple = SearchSysCache2(ATTNUM,
    1644             :                                    ObjectIdGetDatum(table_oid),
    1645             :                                    Int16GetDatum(curr_att));
    1646       15942 :         if (!HeapTupleIsValid(attTuple))
    1647           0 :             elog(ERROR, "cache lookup failed for attribute %d of relation %u",
    1648             :                  curr_att, table_oid);
    1649             : 
    1650       15942 :         isdropped = ((Form_pg_attribute) GETSTRUCT(attTuple))->attisdropped;
    1651             : 
    1652       15942 :         ReleaseSysCache(attTuple);
    1653             : 
    1654             :         /* ignore dropped columns */
    1655       15942 :         if (isdropped)
    1656           6 :             continue;
    1657             : 
    1658       15936 :         col_privileges[curr_att - FirstLowInvalidHeapAttributeNumber] |= this_privileges;
    1659             :     }
    1660        1516 : }
    1661             : 
    1662             : /*
    1663             :  *  This processes attributes, but expects to be called from
    1664             :  *  ExecGrant_Relation, not directly from ExecuteGrantStmt.
    1665             :  */
    1666             : static void
    1667       17794 : ExecGrant_Attribute(InternalGrant *istmt, Oid relOid, const char *relname,
    1668             :                     AttrNumber attnum, Oid ownerId, AclMode col_privileges,
    1669             :                     Relation attRelation, const Acl *old_rel_acl)
    1670             : {
    1671             :     HeapTuple   attr_tuple;
    1672             :     Form_pg_attribute pg_attribute_tuple;
    1673             :     Acl        *old_acl;
    1674             :     Acl        *new_acl;
    1675             :     Acl        *merged_acl;
    1676             :     Datum       aclDatum;
    1677             :     bool        isNull;
    1678             :     Oid         grantorId;
    1679             :     AclMode     avail_goptions;
    1680             :     bool        need_update;
    1681             :     HeapTuple   newtuple;
    1682       17794 :     Datum       values[Natts_pg_attribute] = {0};
    1683       17794 :     bool        nulls[Natts_pg_attribute] = {0};
    1684       17794 :     bool        replaces[Natts_pg_attribute] = {0};
    1685             :     int         noldmembers;
    1686             :     int         nnewmembers;
    1687             :     Oid        *oldmembers;
    1688             :     Oid        *newmembers;
    1689             : 
    1690       17794 :     attr_tuple = SearchSysCache2(ATTNUM,
    1691             :                                  ObjectIdGetDatum(relOid),
    1692             :                                  Int16GetDatum(attnum));
    1693       17794 :     if (!HeapTupleIsValid(attr_tuple))
    1694           0 :         elog(ERROR, "cache lookup failed for attribute %d of relation %u",
    1695             :              attnum, relOid);
    1696       17794 :     pg_attribute_tuple = (Form_pg_attribute) GETSTRUCT(attr_tuple);
    1697             : 
    1698             :     /*
    1699             :      * Get working copy of existing ACL. If there's no ACL, substitute the
    1700             :      * proper default.
    1701             :      */
    1702       17794 :     aclDatum = SysCacheGetAttr(ATTNUM, attr_tuple, Anum_pg_attribute_attacl,
    1703             :                                &isNull);
    1704       17794 :     if (isNull)
    1705             :     {
    1706       17488 :         old_acl = acldefault(OBJECT_COLUMN, ownerId);
    1707             :         /* There are no old member roles according to the catalogs */
    1708       17488 :         noldmembers = 0;
    1709       17488 :         oldmembers = NULL;
    1710             :     }
    1711             :     else
    1712             :     {
    1713         306 :         old_acl = DatumGetAclPCopy(aclDatum);
    1714             :         /* Get the roles mentioned in the existing ACL */
    1715         306 :         noldmembers = aclmembers(old_acl, &oldmembers);
    1716             :     }
    1717             : 
    1718             :     /*
    1719             :      * In select_best_grantor we should consider existing table-level ACL bits
    1720             :      * as well as the per-column ACL.  Build a new ACL that is their
    1721             :      * concatenation.  (This is a bit cheap and dirty compared to merging them
    1722             :      * properly with no duplications, but it's all we need here.)
    1723             :      */
    1724       17794 :     merged_acl = aclconcat(old_rel_acl, old_acl);
    1725             : 
    1726             :     /* Determine ID to do the grant as, and available grant options */
    1727       17794 :     select_best_grantor(GetUserId(), col_privileges,
    1728             :                         merged_acl, ownerId,
    1729             :                         &grantorId, &avail_goptions);
    1730             : 
    1731       17794 :     pfree(merged_acl);
    1732             : 
    1733             :     /*
    1734             :      * Restrict the privileges to what we can actually grant, and emit the
    1735             :      * standards-mandated warning and error messages.  Note: we don't track
    1736             :      * whether the user actually used the ALL PRIVILEGES(columns) syntax for
    1737             :      * each column; we just approximate it by whether all the possible
    1738             :      * privileges are specified now.  Since the all_privs flag only determines
    1739             :      * whether a warning is issued, this seems close enough.
    1740             :      */
    1741             :     col_privileges =
    1742       17794 :         restrict_and_check_grant(istmt->is_grant, avail_goptions,
    1743             :                                  (col_privileges == ACL_ALL_RIGHTS_COLUMN),
    1744             :                                  col_privileges,
    1745             :                                  relOid, grantorId, OBJECT_COLUMN,
    1746             :                                  relname, attnum,
    1747       17794 :                                  NameStr(pg_attribute_tuple->attname));
    1748             : 
    1749             :     /*
    1750             :      * Generate new ACL.
    1751             :      */
    1752       17794 :     new_acl = merge_acl_with_grant(old_acl, istmt->is_grant,
    1753       17794 :                                    istmt->grant_option,
    1754             :                                    istmt->behavior, istmt->grantees,
    1755             :                                    col_privileges, grantorId,
    1756             :                                    ownerId);
    1757             : 
    1758             :     /*
    1759             :      * We need the members of both old and new ACLs so we can correct the
    1760             :      * shared dependency information.
    1761             :      */
    1762       17794 :     nnewmembers = aclmembers(new_acl, &newmembers);
    1763             : 
    1764             :     /* finished building new ACL value, now insert it */
    1765             : 
    1766             :     /*
    1767             :      * If the updated ACL is empty, we can set attacl to null, and maybe even
    1768             :      * avoid an update of the pg_attribute row.  This is worth testing because
    1769             :      * we'll come through here multiple times for any relation-level REVOKE,
    1770             :      * even if there were never any column GRANTs.  Note we are assuming that
    1771             :      * the "default" ACL state for columns is empty.
    1772             :      */
    1773       17794 :     if (ACL_NUM(new_acl) > 0)
    1774             :     {
    1775        1924 :         values[Anum_pg_attribute_attacl - 1] = PointerGetDatum(new_acl);
    1776        1924 :         need_update = true;
    1777             :     }
    1778             :     else
    1779             :     {
    1780       15870 :         nulls[Anum_pg_attribute_attacl - 1] = true;
    1781       15870 :         need_update = !isNull;
    1782             :     }
    1783       17794 :     replaces[Anum_pg_attribute_attacl - 1] = true;
    1784             : 
    1785       17794 :     if (need_update)
    1786             :     {
    1787        2018 :         newtuple = heap_modify_tuple(attr_tuple, RelationGetDescr(attRelation),
    1788             :                                      values, nulls, replaces);
    1789             : 
    1790        2018 :         CatalogTupleUpdate(attRelation, &newtuple->t_self, newtuple);
    1791             : 
    1792             :         /* Update initial privileges for extensions */
    1793        2018 :         recordExtensionInitPriv(relOid, RelationRelationId, attnum,
    1794        2018 :                                 ACL_NUM(new_acl) > 0 ? new_acl : NULL);
    1795             : 
    1796             :         /* Update the shared dependency ACL info */
    1797        2018 :         updateAclDependencies(RelationRelationId, relOid, attnum,
    1798             :                               ownerId,
    1799             :                               noldmembers, oldmembers,
    1800             :                               nnewmembers, newmembers);
    1801             :     }
    1802             : 
    1803       17794 :     pfree(new_acl);
    1804             : 
    1805       17794 :     ReleaseSysCache(attr_tuple);
    1806       17794 : }
    1807             : 
    1808             : /*
    1809             :  *  This processes both sequences and non-sequences.
    1810             :  */
    1811             : static void
    1812        7898 : ExecGrant_Relation(InternalGrant *istmt)
    1813             : {
    1814             :     Relation    relation;
    1815             :     Relation    attRelation;
    1816             :     ListCell   *cell;
    1817             : 
    1818        7898 :     relation = table_open(RelationRelationId, RowExclusiveLock);
    1819        7898 :     attRelation = table_open(AttributeRelationId, RowExclusiveLock);
    1820             : 
    1821       15856 :     foreach(cell, istmt->objects)
    1822             :     {
    1823        7964 :         Oid         relOid = lfirst_oid(cell);
    1824             :         Datum       aclDatum;
    1825             :         Form_pg_class pg_class_tuple;
    1826             :         bool        isNull;
    1827             :         AclMode     this_privileges;
    1828             :         AclMode    *col_privileges;
    1829             :         int         num_col_privileges;
    1830             :         bool        have_col_privileges;
    1831             :         Acl        *old_acl;
    1832             :         Acl        *old_rel_acl;
    1833             :         int         noldmembers;
    1834             :         Oid        *oldmembers;
    1835             :         Oid         ownerId;
    1836             :         HeapTuple   tuple;
    1837             :         ListCell   *cell_colprivs;
    1838             : 
    1839        7964 :         tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relOid));
    1840        7964 :         if (!HeapTupleIsValid(tuple))
    1841           0 :             elog(ERROR, "cache lookup failed for relation %u", relOid);
    1842        7964 :         pg_class_tuple = (Form_pg_class) GETSTRUCT(tuple);
    1843             : 
    1844             :         /* Not sensible to grant on an index */
    1845        7964 :         if (pg_class_tuple->relkind == RELKIND_INDEX ||
    1846        7964 :             pg_class_tuple->relkind == RELKIND_PARTITIONED_INDEX)
    1847           0 :             ereport(ERROR,
    1848             :                     (errcode(ERRCODE_WRONG_OBJECT_TYPE),
    1849             :                      errmsg("\"%s\" is an index",
    1850             :                             NameStr(pg_class_tuple->relname))));
    1851             : 
    1852             :         /* Composite types aren't tables either */
    1853        7964 :         if (pg_class_tuple->relkind == RELKIND_COMPOSITE_TYPE)
    1854           0 :             ereport(ERROR,
    1855             :                     (errcode(ERRCODE_WRONG_OBJECT_TYPE),
    1856             :                      errmsg("\"%s\" is a composite type",
    1857             :                             NameStr(pg_class_tuple->relname))));
    1858             : 
    1859             :         /* Used GRANT SEQUENCE on a non-sequence? */
    1860        7964 :         if (istmt->objtype == OBJECT_SEQUENCE &&
    1861          16 :             pg_class_tuple->relkind != RELKIND_SEQUENCE)
    1862           0 :             ereport(ERROR,
    1863             :                     (errcode(ERRCODE_WRONG_OBJECT_TYPE),
    1864             :                      errmsg("\"%s\" is not a sequence",
    1865             :                             NameStr(pg_class_tuple->relname))));
    1866             : 
    1867             :         /* Adjust the default permissions based on object type */
    1868        7964 :         if (istmt->all_privs && istmt->privileges == ACL_NO_RIGHTS)
    1869             :         {
    1870        1570 :             if (pg_class_tuple->relkind == RELKIND_SEQUENCE)
    1871          74 :                 this_privileges = ACL_ALL_RIGHTS_SEQUENCE;
    1872             :             else
    1873        1496 :                 this_privileges = ACL_ALL_RIGHTS_RELATION;
    1874             :         }
    1875             :         else
    1876        6394 :             this_privileges = istmt->privileges;
    1877             : 
    1878             :         /*
    1879             :          * The GRANT TABLE syntax can be used for sequences and non-sequences,
    1880             :          * so we have to look at the relkind to determine the supported
    1881             :          * permissions.  The OR of table and sequence permissions were already
    1882             :          * checked.
    1883             :          */
    1884        7964 :         if (istmt->objtype == OBJECT_TABLE)
    1885             :         {
    1886        7948 :             if (pg_class_tuple->relkind == RELKIND_SEQUENCE)
    1887             :             {
    1888             :                 /*
    1889             :                  * For backward compatibility, just throw a warning for
    1890             :                  * invalid sequence permissions when using the non-sequence
    1891             :                  * GRANT syntax.
    1892             :                  */
    1893         144 :                 if (this_privileges & ~((AclMode) ACL_ALL_RIGHTS_SEQUENCE))
    1894             :                 {
    1895             :                     /*
    1896             :                      * Mention the object name because the user needs to know
    1897             :                      * which operations succeeded.  This is required because
    1898             :                      * WARNING allows the command to continue.
    1899             :                      */
    1900           0 :                     ereport(WARNING,
    1901             :                             (errcode(ERRCODE_INVALID_GRANT_OPERATION),
    1902             :                              errmsg("sequence \"%s\" only supports USAGE, SELECT, and UPDATE privileges",
    1903             :                                     NameStr(pg_class_tuple->relname))));
    1904           0 :                     this_privileges &= (AclMode) ACL_ALL_RIGHTS_SEQUENCE;
    1905             :                 }
    1906             :             }
    1907             :             else
    1908             :             {
    1909        7804 :                 if (this_privileges & ~((AclMode) ACL_ALL_RIGHTS_RELATION))
    1910             :                 {
    1911             :                     /*
    1912             :                      * USAGE is the only permission supported by sequences but
    1913             :                      * not by non-sequences.  Don't mention the object name
    1914             :                      * because we didn't in the combined TABLE | SEQUENCE
    1915             :                      * check.
    1916             :                      */
    1917           0 :                     ereport(ERROR,
    1918             :                             (errcode(ERRCODE_INVALID_GRANT_OPERATION),
    1919             :                              errmsg("invalid privilege type %s for table",
    1920             :                                     "USAGE")));
    1921             :                 }
    1922             :             }
    1923             :         }
    1924             : 
    1925             :         /*
    1926             :          * Set up array in which we'll accumulate any column privilege bits
    1927             :          * that need modification.  The array is indexed such that entry [0]
    1928             :          * corresponds to FirstLowInvalidHeapAttributeNumber.
    1929             :          */
    1930        7964 :         num_col_privileges = pg_class_tuple->relnatts - FirstLowInvalidHeapAttributeNumber + 1;
    1931        7964 :         col_privileges = (AclMode *) palloc0(num_col_privileges * sizeof(AclMode));
    1932        7964 :         have_col_privileges = false;
    1933             : 
    1934             :         /*
    1935             :          * If we are revoking relation privileges that are also column
    1936             :          * privileges, we must implicitly revoke them from each column too,
    1937             :          * per SQL spec.  (We don't need to implicitly add column privileges
    1938             :          * during GRANT because the permissions-checking code always checks
    1939             :          * both relation and per-column privileges.)
    1940             :          */
    1941        7964 :         if (!istmt->is_grant &&
    1942        1572 :             (this_privileges & ACL_ALL_RIGHTS_COLUMN) != 0)
    1943             :         {
    1944        1516 :             expand_all_col_privileges(relOid, pg_class_tuple,
    1945             :                                       this_privileges & ACL_ALL_RIGHTS_COLUMN,
    1946             :                                       col_privileges,
    1947             :                                       num_col_privileges);
    1948        1516 :             have_col_privileges = true;
    1949             :         }
    1950             : 
    1951             :         /*
    1952             :          * Get owner ID and working copy of existing ACL. If there's no ACL,
    1953             :          * substitute the proper default.
    1954             :          */
    1955        7964 :         ownerId = pg_class_tuple->relowner;
    1956        7964 :         aclDatum = SysCacheGetAttr(RELOID, tuple, Anum_pg_class_relacl,
    1957             :                                    &isNull);
    1958        7964 :         if (isNull)
    1959             :         {
    1960        6874 :             switch (pg_class_tuple->relkind)
    1961             :             {
    1962          84 :                 case RELKIND_SEQUENCE:
    1963          84 :                     old_acl = acldefault(OBJECT_SEQUENCE, ownerId);
    1964          84 :                     break;
    1965        6790 :                 default:
    1966        6790 :                     old_acl = acldefault(OBJECT_TABLE, ownerId);
    1967        6790 :                     break;
    1968             :             }
    1969             :             /* There are no old member roles according to the catalogs */
    1970        6874 :             noldmembers = 0;
    1971        6874 :             oldmembers = NULL;
    1972             :         }
    1973             :         else
    1974             :         {
    1975        1090 :             old_acl = DatumGetAclPCopy(aclDatum);
    1976             :             /* Get the roles mentioned in the existing ACL */
    1977        1090 :             noldmembers = aclmembers(old_acl, &oldmembers);
    1978             :         }
    1979             : 
    1980             :         /* Need an extra copy of original rel ACL for column handling */
    1981        7964 :         old_rel_acl = aclcopy(old_acl);
    1982             : 
    1983             :         /*
    1984             :          * Handle relation-level privileges, if any were specified
    1985             :          */
    1986        7964 :         if (this_privileges != ACL_NO_RIGHTS)
    1987             :         {
    1988             :             AclMode     avail_goptions;
    1989             :             Acl        *new_acl;
    1990             :             Oid         grantorId;
    1991             :             HeapTuple   newtuple;
    1992        7566 :             Datum       values[Natts_pg_class] = {0};
    1993        7566 :             bool        nulls[Natts_pg_class] = {0};
    1994        7566 :             bool        replaces[Natts_pg_class] = {0};
    1995             :             int         nnewmembers;
    1996             :             Oid        *newmembers;
    1997             :             ObjectType  objtype;
    1998             : 
    1999             :             /* Determine ID to do the grant as, and available grant options */
    2000        7566 :             select_best_grantor(GetUserId(), this_privileges,
    2001             :                                 old_acl, ownerId,
    2002             :                                 &grantorId, &avail_goptions);
    2003             : 
    2004        7566 :             switch (pg_class_tuple->relkind)
    2005             :             {
    2006         160 :                 case RELKIND_SEQUENCE:
    2007         160 :                     objtype = OBJECT_SEQUENCE;
    2008         160 :                     break;
    2009        7406 :                 default:
    2010        7406 :                     objtype = OBJECT_TABLE;
    2011        7406 :                     break;
    2012             :             }
    2013             : 
    2014             :             /*
    2015             :              * Restrict the privileges to what we can actually grant, and emit
    2016             :              * the standards-mandated warning and error messages.
    2017             :              */
    2018             :             this_privileges =
    2019        7566 :                 restrict_and_check_grant(istmt->is_grant, avail_goptions,
    2020        7566 :                                          istmt->all_privs, this_privileges,
    2021             :                                          relOid, grantorId, objtype,
    2022        7566 :                                          NameStr(pg_class_tuple->relname),
    2023             :                                          0, NULL);
    2024             : 
    2025             :             /*
    2026             :              * Generate new ACL.
    2027             :              */
    2028        7566 :             new_acl = merge_acl_with_grant(old_acl,
    2029        7566 :                                            istmt->is_grant,
    2030        7566 :                                            istmt->grant_option,
    2031             :                                            istmt->behavior,
    2032             :                                            istmt->grantees,
    2033             :                                            this_privileges,
    2034             :                                            grantorId,
    2035             :                                            ownerId);
    2036             : 
    2037             :             /*
    2038             :              * We need the members of both old and new ACLs so we can correct
    2039             :              * the shared dependency information.
    2040             :              */
    2041        7560 :             nnewmembers = aclmembers(new_acl, &newmembers);
    2042             : 
    2043             :             /* finished building new ACL value, now insert it */
    2044        7560 :             replaces[Anum_pg_class_relacl - 1] = true;
    2045        7560 :             values[Anum_pg_class_relacl - 1] = PointerGetDatum(new_acl);
    2046             : 
    2047        7560 :             newtuple = heap_modify_tuple(tuple, RelationGetDescr(relation),
    2048             :                                          values, nulls, replaces);
    2049             : 
    2050        7560 :             CatalogTupleUpdate(relation, &newtuple->t_self, newtuple);
    2051             : 
    2052             :             /* Update initial privileges for extensions */
    2053        7560 :             recordExtensionInitPriv(relOid, RelationRelationId, 0, new_acl);
    2054             : 
    2055             :             /* Update the shared dependency ACL info */
    2056        7560 :             updateAclDependencies(RelationRelationId, relOid, 0,
    2057             :                                   ownerId,
    2058             :                                   noldmembers, oldmembers,
    2059             :                                   nnewmembers, newmembers);
    2060             : 
    2061        7560 :             pfree(new_acl);
    2062             :         }
    2063             : 
    2064             :         /*
    2065             :          * Handle column-level privileges, if any were specified or implied.
    2066             :          * We first expand the user-specified column privileges into the
    2067             :          * array, and then iterate over all nonempty array entries.
    2068             :          */
    2069        8368 :         foreach(cell_colprivs, istmt->col_privs)
    2070             :         {
    2071         410 :             AccessPriv *col_privs = (AccessPriv *) lfirst(cell_colprivs);
    2072             : 
    2073         410 :             if (col_privs->priv_name == NULL)
    2074          18 :                 this_privileges = ACL_ALL_RIGHTS_COLUMN;
    2075             :             else
    2076         392 :                 this_privileges = string_to_privilege(col_privs->priv_name);
    2077             : 
    2078         410 :             if (this_privileges & ~((AclMode) ACL_ALL_RIGHTS_COLUMN))
    2079           0 :                 ereport(ERROR,
    2080             :                         (errcode(ERRCODE_INVALID_GRANT_OPERATION),
    2081             :                          errmsg("invalid privilege type %s for column",
    2082             :                                 privilege_to_string(this_privileges))));
    2083             : 
    2084         410 :             if (pg_class_tuple->relkind == RELKIND_SEQUENCE &&
    2085           0 :                 this_privileges & ~((AclMode) ACL_SELECT))
    2086             :             {
    2087             :                 /*
    2088             :                  * The only column privilege allowed on sequences is SELECT.
    2089             :                  * This is a warning not error because we do it that way for
    2090             :                  * relation-level privileges.
    2091             :                  */
    2092           0 :                 ereport(WARNING,
    2093             :                         (errcode(ERRCODE_INVALID_GRANT_OPERATION),
    2094             :                          errmsg("sequence \"%s\" only supports SELECT column privileges",
    2095             :                                 NameStr(pg_class_tuple->relname))));
    2096             : 
    2097           0 :                 this_privileges &= (AclMode) ACL_SELECT;
    2098             :             }
    2099             : 
    2100         410 :             expand_col_privileges(col_privs->cols, relOid,
    2101             :                                   this_privileges,
    2102             :                                   col_privileges,
    2103             :                                   num_col_privileges);
    2104         410 :             have_col_privileges = true;
    2105             :         }
    2106             : 
    2107        7958 :         if (have_col_privileges)
    2108             :         {
    2109             :             AttrNumber  i;
    2110             : 
    2111       30568 :             for (i = 0; i < num_col_privileges; i++)
    2112             :             {
    2113       28660 :                 if (col_privileges[i] == ACL_NO_RIGHTS)
    2114       10866 :                     continue;
    2115       17794 :                 ExecGrant_Attribute(istmt,
    2116             :                                     relOid,
    2117       17794 :                                     NameStr(pg_class_tuple->relname),
    2118       17794 :                                     i + FirstLowInvalidHeapAttributeNumber,
    2119             :                                     ownerId,
    2120       17794 :                                     col_privileges[i],
    2121             :                                     attRelation,
    2122             :                                     old_rel_acl);
    2123             :             }
    2124             :         }
    2125             : 
    2126        7958 :         pfree(old_rel_acl);
    2127        7958 :         pfree(col_privileges);
    2128             : 
    2129        7958 :         ReleaseSysCache(tuple);
    2130             : 
    2131             :         /* prevent error when processing duplicate objects */
    2132        7958 :         CommandCounterIncrement();
    2133             :     }
    2134             : 
    2135        7892 :     table_close(attRelation, RowExclusiveLock);
    2136        7892 :     table_close(relation, RowExclusiveLock);
    2137        7892 : }
    2138             : 
    2139             : static void
    2140        7464 : ExecGrant_common(InternalGrant *istmt, Oid classid, AclMode default_privs,
    2141             :                  void (*object_check) (InternalGrant *istmt, HeapTuple tuple))
    2142             : {
    2143             :     int         cacheid;
    2144             :     Relation    relation;
    2145             :     ListCell   *cell;
    2146             : 
    2147        7464 :     if (istmt->all_privs && istmt->privileges == ACL_NO_RIGHTS)
    2148         630 :         istmt->privileges = default_privs;
    2149             : 
    2150        7464 :     cacheid = get_object_catcache_oid(classid);
    2151             : 
    2152        7464 :     relation = table_open(classid, RowExclusiveLock);
    2153             : 
    2154       14992 :     foreach(cell, istmt->objects)
    2155             :     {
    2156        7588 :         Oid         objectid = lfirst_oid(cell);
    2157             :         Datum       aclDatum;
    2158             :         Datum       nameDatum;
    2159             :         bool        isNull;
    2160             :         AclMode     avail_goptions;
    2161             :         AclMode     this_privileges;
    2162             :         Acl        *old_acl;
    2163             :         Acl        *new_acl;
    2164             :         Oid         grantorId;
    2165             :         Oid         ownerId;
    2166             :         HeapTuple   tuple;
    2167             :         HeapTuple   newtuple;
    2168        7588 :         Datum      *values = palloc0_array(Datum, RelationGetDescr(relation)->natts);
    2169        7588 :         bool       *nulls = palloc0_array(bool, RelationGetDescr(relation)->natts);
    2170        7588 :         bool       *replaces = palloc0_array(bool, RelationGetDescr(relation)->natts);
    2171             :         int         noldmembers;
    2172             :         int         nnewmembers;
    2173             :         Oid        *oldmembers;
    2174             :         Oid        *newmembers;
    2175             : 
    2176        7588 :         tuple = SearchSysCache1(cacheid, ObjectIdGetDatum(objectid));
    2177        7588 :         if (!HeapTupleIsValid(tuple))
    2178           0 :             elog(ERROR, "cache lookup failed for %s %u", get_object_class_descr(classid), objectid);
    2179             : 
    2180             :         /*
    2181             :          * Additional object-type-specific checks
    2182             :          */
    2183        7588 :         if (object_check)
    2184         182 :             object_check(istmt, tuple);
    2185             : 
    2186             :         /*
    2187             :          * Get owner ID and working copy of existing ACL. If there's no ACL,
    2188             :          * substitute the proper default.
    2189             :          */
    2190        7564 :         ownerId = DatumGetObjectId(SysCacheGetAttrNotNull(cacheid,
    2191             :                                                           tuple,
    2192        7564 :                                                           get_object_attnum_owner(classid)));
    2193        7564 :         aclDatum = SysCacheGetAttr(cacheid,
    2194             :                                    tuple,
    2195        7564 :                                    get_object_attnum_acl(classid),
    2196             :                                    &isNull);
    2197        7564 :         if (isNull)
    2198             :         {
    2199        5872 :             old_acl = acldefault(get_object_type(classid, objectid), ownerId);
    2200             :             /* There are no old member roles according to the catalogs */
    2201        5872 :             noldmembers = 0;
    2202        5872 :             oldmembers = NULL;
    2203             :         }
    2204             :         else
    2205             :         {
    2206        1692 :             old_acl = DatumGetAclPCopy(aclDatum);
    2207             :             /* Get the roles mentioned in the existing ACL */
    2208        1692 :             noldmembers = aclmembers(old_acl, &oldmembers);
    2209             :         }
    2210             : 
    2211             :         /* Determine ID to do the grant as, and available grant options */
    2212        7564 :         select_best_grantor(GetUserId(), istmt->privileges,
    2213             :                             old_acl, ownerId,
    2214             :                             &grantorId, &avail_goptions);
    2215             : 
    2216        7564 :         nameDatum = SysCacheGetAttrNotNull(cacheid, tuple,
    2217        7564 :                                            get_object_attnum_name(classid));
    2218             : 
    2219             :         /*
    2220             :          * Restrict the privileges to what we can actually grant, and emit the
    2221             :          * standards-mandated warning and error messages.
    2222             :          */
    2223             :         this_privileges =
    2224       15128 :             restrict_and_check_grant(istmt->is_grant, avail_goptions,
    2225        7564 :                                      istmt->all_privs, istmt->privileges,
    2226             :                                      objectid, grantorId, get_object_type(classid, objectid),
    2227        7564 :                                      NameStr(*DatumGetName(nameDatum)),
    2228             :                                      0, NULL);
    2229             : 
    2230             :         /*
    2231             :          * Generate new ACL.
    2232             :          */
    2233        7534 :         new_acl = merge_acl_with_grant(old_acl, istmt->is_grant,
    2234        7534 :                                        istmt->grant_option, istmt->behavior,
    2235             :                                        istmt->grantees, this_privileges,
    2236             :                                        grantorId, ownerId);
    2237             : 
    2238             :         /*
    2239             :          * We need the members of both old and new ACLs so we can correct the
    2240             :          * shared dependency information.
    2241             :          */
    2242        7528 :         nnewmembers = aclmembers(new_acl, &newmembers);
    2243             : 
    2244             :         /* finished building new ACL value, now insert it */
    2245        7528 :         replaces[get_object_attnum_acl(classid) - 1] = true;
    2246        7528 :         values[get_object_attnum_acl(classid) - 1] = PointerGetDatum(new_acl);
    2247             : 
    2248        7528 :         newtuple = heap_modify_tuple(tuple, RelationGetDescr(relation), values,
    2249             :                                      nulls, replaces);
    2250             : 
    2251        7528 :         CatalogTupleUpdate(relation, &newtuple->t_self, newtuple);
    2252             : 
    2253             :         /* Update initial privileges for extensions */
    2254        7528 :         recordExtensionInitPriv(objectid, classid, 0, new_acl);
    2255             : 
    2256             :         /* Update the shared dependency ACL info */
    2257        7528 :         updateAclDependencies(classid,
    2258             :                               objectid, 0,
    2259             :                               ownerId,
    2260             :                               noldmembers, oldmembers,
    2261             :                               nnewmembers, newmembers);
    2262             : 
    2263        7528 :         ReleaseSysCache(tuple);
    2264             : 
    2265        7528 :         pfree(new_acl);
    2266             : 
    2267             :         /* prevent error when processing duplicate objects */
    2268        7528 :         CommandCounterIncrement();
    2269             :     }
    2270             : 
    2271        7404 :     table_close(relation, RowExclusiveLock);
    2272        7404 : }
    2273             : 
    2274             : static void
    2275          42 : ExecGrant_Language_check(InternalGrant *istmt, HeapTuple tuple)
    2276             : {
    2277             :     Form_pg_language pg_language_tuple;
    2278             : 
    2279          42 :     pg_language_tuple = (Form_pg_language) GETSTRUCT(tuple);
    2280             : 
    2281          42 :     if (!pg_language_tuple->lanpltrusted)
    2282           6 :         ereport(ERROR,
    2283             :                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
    2284             :                  errmsg("language \"%s\" is not trusted",
    2285             :                         NameStr(pg_language_tuple->lanname)),
    2286             :                  errdetail("GRANT and REVOKE are not allowed on untrusted languages, "
    2287             :                            "because only superusers can use untrusted languages.")));
    2288          36 : }
    2289             : 
    2290             : static void
    2291          74 : ExecGrant_Largeobject(InternalGrant *istmt)
    2292             : {
    2293             :     Relation    relation;
    2294             :     ListCell   *cell;
    2295             : 
    2296          74 :     if (istmt->all_privs && istmt->privileges == ACL_NO_RIGHTS)
    2297          44 :         istmt->privileges = ACL_ALL_RIGHTS_LARGEOBJECT;
    2298             : 
    2299          74 :     relation = table_open(LargeObjectMetadataRelationId,
    2300             :                           RowExclusiveLock);
    2301             : 
    2302         154 :     foreach(cell, istmt->objects)
    2303             :     {
    2304          80 :         Oid         loid = lfirst_oid(cell);
    2305             :         Form_pg_largeobject_metadata form_lo_meta;
    2306             :         char        loname[NAMEDATALEN];
    2307             :         Datum       aclDatum;
    2308             :         bool        isNull;
    2309             :         AclMode     avail_goptions;
    2310             :         AclMode     this_privileges;
    2311             :         Acl        *old_acl;
    2312             :         Acl        *new_acl;
    2313             :         Oid         grantorId;
    2314             :         Oid         ownerId;
    2315             :         HeapTuple   newtuple;
    2316          80 :         Datum       values[Natts_pg_largeobject_metadata] = {0};
    2317          80 :         bool        nulls[Natts_pg_largeobject_metadata] = {0};
    2318          80 :         bool        replaces[Natts_pg_largeobject_metadata] = {0};
    2319             :         int         noldmembers;
    2320             :         int         nnewmembers;
    2321             :         Oid        *oldmembers;
    2322             :         Oid        *newmembers;
    2323             :         ScanKeyData entry[1];
    2324             :         SysScanDesc scan;
    2325             :         HeapTuple   tuple;
    2326             : 
    2327             :         /* There's no syscache for pg_largeobject_metadata */
    2328          80 :         ScanKeyInit(&entry[0],
    2329             :                     Anum_pg_largeobject_metadata_oid,
    2330             :                     BTEqualStrategyNumber, F_OIDEQ,
    2331             :                     ObjectIdGetDatum(loid));
    2332             : 
    2333          80 :         scan = systable_beginscan(relation,
    2334             :                                   LargeObjectMetadataOidIndexId, true,
    2335             :                                   NULL, 1, entry);
    2336             : 
    2337          80 :         tuple = systable_getnext(scan);
    2338          80 :         if (!HeapTupleIsValid(tuple))
    2339           0 :             elog(ERROR, "could not find tuple for large object %u", loid);
    2340             : 
    2341          80 :         form_lo_meta = (Form_pg_largeobject_metadata) GETSTRUCT(tuple);
    2342             : 
    2343             :         /*
    2344             :          * Get owner ID and working copy of existing ACL. If there's no ACL,
    2345             :          * substitute the proper default.
    2346             :          */
    2347          80 :         ownerId = form_lo_meta->lomowner;
    2348          80 :         aclDatum = heap_getattr(tuple,
    2349             :                                 Anum_pg_largeobject_metadata_lomacl,
    2350             :                                 RelationGetDescr(relation), &isNull);
    2351          80 :         if (isNull)
    2352             :         {
    2353          44 :             old_acl = acldefault(OBJECT_LARGEOBJECT, ownerId);
    2354             :             /* There are no old member roles according to the catalogs */
    2355          44 :             noldmembers = 0;
    2356          44 :             oldmembers = NULL;
    2357             :         }
    2358             :         else
    2359             :         {
    2360          36 :             old_acl = DatumGetAclPCopy(aclDatum);
    2361             :             /* Get the roles mentioned in the existing ACL */
    2362          36 :             noldmembers = aclmembers(old_acl, &oldmembers);
    2363             :         }
    2364             : 
    2365             :         /* Determine ID to do the grant as, and available grant options */
    2366          80 :         select_best_grantor(GetUserId(), istmt->privileges,
    2367             :                             old_acl, ownerId,
    2368             :                             &grantorId, &avail_goptions);
    2369             : 
    2370             :         /*
    2371             :          * Restrict the privileges to what we can actually grant, and emit the
    2372             :          * standards-mandated warning and error messages.
    2373             :          */
    2374          80 :         snprintf(loname, sizeof(loname), "large object %u", loid);
    2375             :         this_privileges =
    2376          80 :             restrict_and_check_grant(istmt->is_grant, avail_goptions,
    2377          80 :                                      istmt->all_privs, istmt->privileges,
    2378             :                                      loid, grantorId, OBJECT_LARGEOBJECT,
    2379             :                                      loname, 0, NULL);
    2380             : 
    2381             :         /*
    2382             :          * Generate new ACL.
    2383             :          */
    2384          80 :         new_acl = merge_acl_with_grant(old_acl, istmt->is_grant,
    2385          80 :                                        istmt->grant_option, istmt->behavior,
    2386             :                                        istmt->grantees, this_privileges,
    2387             :                                        grantorId, ownerId);
    2388             : 
    2389             :         /*
    2390             :          * We need the members of both old and new ACLs so we can correct the
    2391             :          * shared dependency information.
    2392             :          */
    2393          80 :         nnewmembers = aclmembers(new_acl, &newmembers);
    2394             : 
    2395             :         /* finished building new ACL value, now insert it */
    2396          80 :         replaces[Anum_pg_largeobject_metadata_lomacl - 1] = true;
    2397             :         values[Anum_pg_largeobject_metadata_lomacl - 1]
    2398          80 :             = PointerGetDatum(new_acl);
    2399             : 
    2400          80 :         newtuple = heap_modify_tuple(tuple, RelationGetDescr(relation),
    2401             :                                      values, nulls, replaces);
    2402             : 
    2403          80 :         CatalogTupleUpdate(relation, &newtuple->t_self, newtuple);
    2404             : 
    2405             :         /* Update initial privileges for extensions */
    2406          80 :         recordExtensionInitPriv(loid, LargeObjectRelationId, 0, new_acl);
    2407             : 
    2408             :         /* Update the shared dependency ACL info */
    2409          80 :         updateAclDependencies(LargeObjectRelationId,
    2410             :                               form_lo_meta->oid, 0,
    2411             :                               ownerId,
    2412             :                               noldmembers, oldmembers,
    2413             :                               nnewmembers, newmembers);
    2414             : 
    2415          80 :         systable_endscan(scan);
    2416             : 
    2417          80 :         pfree(new_acl);
    2418             : 
    2419             :         /* prevent error when processing duplicate objects */
    2420          80 :         CommandCounterIncrement();
    2421             :     }
    2422             : 
    2423          74 :     table_close(relation, RowExclusiveLock);
    2424          74 : }
    2425             : 
    2426             : static void
    2427         140 : ExecGrant_Type_check(InternalGrant *istmt, HeapTuple tuple)
    2428             : {
    2429             :     Form_pg_type pg_type_tuple;
    2430             : 
    2431         140 :     pg_type_tuple = (Form_pg_type) GETSTRUCT(tuple);
    2432             : 
    2433             :     /* Disallow GRANT on dependent types */
    2434         140 :     if (IsTrueArrayType(pg_type_tuple))
    2435           6 :         ereport(ERROR,
    2436             :                 (errcode(ERRCODE_INVALID_GRANT_OPERATION),
    2437             :                  errmsg("cannot set privileges of array types"),
    2438             :                  errhint("Set the privileges of the element type instead.")));
    2439         134 :     if (pg_type_tuple->typtype == TYPTYPE_MULTIRANGE)
    2440           6 :         ereport(ERROR,
    2441             :                 (errcode(ERRCODE_INVALID_GRANT_OPERATION),
    2442             :                  errmsg("cannot set privileges of multirange types"),
    2443             :                  errhint("Set the privileges of the range type instead.")));
    2444             : 
    2445             :     /* Used GRANT DOMAIN on a non-domain? */
    2446         128 :     if (istmt->objtype == OBJECT_DOMAIN &&
    2447          26 :         pg_type_tuple->typtype != TYPTYPE_DOMAIN)
    2448           6 :         ereport(ERROR,
    2449             :                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
    2450             :                  errmsg("\"%s\" is not a domain",
    2451             :                         NameStr(pg_type_tuple->typname))));
    2452         122 : }
    2453             : 
    2454             : static void
    2455          96 : ExecGrant_Parameter(InternalGrant *istmt)
    2456             : {
    2457             :     Relation    relation;
    2458             :     ListCell   *cell;
    2459             : 
    2460          96 :     if (istmt->all_privs && istmt->privileges == ACL_NO_RIGHTS)
    2461          42 :         istmt->privileges = ACL_ALL_RIGHTS_PARAMETER_ACL;
    2462             : 
    2463          96 :     relation = table_open(ParameterAclRelationId, RowExclusiveLock);
    2464             : 
    2465         230 :     foreach(cell, istmt->objects)
    2466             :     {
    2467         134 :         Oid         parameterId = lfirst_oid(cell);
    2468             :         Datum       nameDatum;
    2469             :         const char *parname;
    2470             :         Datum       aclDatum;
    2471             :         bool        isNull;
    2472             :         AclMode     avail_goptions;
    2473             :         AclMode     this_privileges;
    2474             :         Acl        *old_acl;
    2475             :         Acl        *new_acl;
    2476             :         Oid         grantorId;
    2477             :         Oid         ownerId;
    2478             :         HeapTuple   tuple;
    2479             :         int         noldmembers;
    2480             :         int         nnewmembers;
    2481             :         Oid        *oldmembers;
    2482             :         Oid        *newmembers;
    2483             : 
    2484         134 :         tuple = SearchSysCache1(PARAMETERACLOID, ObjectIdGetDatum(parameterId));
    2485         134 :         if (!HeapTupleIsValid(tuple))
    2486           0 :             elog(ERROR, "cache lookup failed for parameter ACL %u",
    2487             :                  parameterId);
    2488             : 
    2489             :         /* We'll need the GUC's name */
    2490         134 :         nameDatum = SysCacheGetAttrNotNull(PARAMETERACLOID, tuple,
    2491             :                                            Anum_pg_parameter_acl_parname);
    2492         134 :         parname = TextDatumGetCString(nameDatum);
    2493             : 
    2494             :         /* Treat all parameters as belonging to the bootstrap superuser. */
    2495         134 :         ownerId = BOOTSTRAP_SUPERUSERID;
    2496             : 
    2497             :         /*
    2498             :          * Get working copy of existing ACL. If there's no ACL, substitute the
    2499             :          * proper default.
    2500             :          */
    2501         134 :         aclDatum = SysCacheGetAttr(PARAMETERACLOID, tuple,
    2502             :                                    Anum_pg_parameter_acl_paracl,
    2503             :                                    &isNull);
    2504             : 
    2505         134 :         if (isNull)
    2506             :         {
    2507          66 :             old_acl = acldefault(istmt->objtype, ownerId);
    2508             :             /* There are no old member roles according to the catalogs */
    2509          66 :             noldmembers = 0;
    2510          66 :             oldmembers = NULL;
    2511             :         }
    2512             :         else
    2513             :         {
    2514          68 :             old_acl = DatumGetAclPCopy(aclDatum);
    2515             :             /* Get the roles mentioned in the existing ACL */
    2516          68 :             noldmembers = aclmembers(old_acl, &oldmembers);
    2517             :         }
    2518             : 
    2519             :         /* Determine ID to do the grant as, and available grant options */
    2520         134 :         select_best_grantor(GetUserId(), istmt->privileges,
    2521             :                             old_acl, ownerId,
    2522             :                             &grantorId, &avail_goptions);
    2523             : 
    2524             :         /*
    2525             :          * Restrict the privileges to what we can actually grant, and emit the
    2526             :          * standards-mandated warning and error messages.
    2527             :          */
    2528             :         this_privileges =
    2529         134 :             restrict_and_check_grant(istmt->is_grant, avail_goptions,
    2530         134 :                                      istmt->all_privs, istmt->privileges,
    2531             :                                      parameterId, grantorId,
    2532             :                                      OBJECT_PARAMETER_ACL,
    2533             :                                      parname,
    2534             :                                      0, NULL);
    2535             : 
    2536             :         /*
    2537             :          * Generate new ACL.
    2538             :          */
    2539         134 :         new_acl = merge_acl_with_grant(old_acl, istmt->is_grant,
    2540         134 :                                        istmt->grant_option, istmt->behavior,
    2541             :                                        istmt->grantees, this_privileges,
    2542             :                                        grantorId, ownerId);
    2543             : 
    2544             :         /*
    2545             :          * We need the members of both old and new ACLs so we can correct the
    2546             :          * shared dependency information.
    2547             :          */
    2548         134 :         nnewmembers = aclmembers(new_acl, &newmembers);
    2549             : 
    2550             :         /*
    2551             :          * If the new ACL is equal to the default, we don't need the catalog
    2552             :          * entry any longer.  Delete it rather than updating it, to avoid
    2553             :          * leaving a degenerate entry.
    2554             :          */
    2555         134 :         if (aclequal(new_acl, acldefault(istmt->objtype, ownerId)))
    2556             :         {
    2557          58 :             CatalogTupleDelete(relation, &tuple->t_self);
    2558             :         }
    2559             :         else
    2560             :         {
    2561             :             /* finished building new ACL value, now insert it */
    2562             :             HeapTuple   newtuple;
    2563          76 :             Datum       values[Natts_pg_parameter_acl] = {0};
    2564          76 :             bool        nulls[Natts_pg_parameter_acl] = {0};
    2565          76 :             bool        replaces[Natts_pg_parameter_acl] = {0};
    2566             : 
    2567          76 :             replaces[Anum_pg_parameter_acl_paracl - 1] = true;
    2568          76 :             values[Anum_pg_parameter_acl_paracl - 1] = PointerGetDatum(new_acl);
    2569             : 
    2570          76 :             newtuple = heap_modify_tuple(tuple, RelationGetDescr(relation),
    2571             :                                          values, nulls, replaces);
    2572             : 
    2573          76 :             CatalogTupleUpdate(relation, &newtuple->t_self, newtuple);
    2574             :         }
    2575             : 
    2576             :         /* Update initial privileges for extensions */
    2577         134 :         recordExtensionInitPriv(parameterId, ParameterAclRelationId, 0,
    2578             :                                 new_acl);
    2579             : 
    2580             :         /* Update the shared dependency ACL info */
    2581         134 :         updateAclDependencies(ParameterAclRelationId, parameterId, 0,
    2582             :                               ownerId,
    2583             :                               noldmembers, oldmembers,
    2584             :                               nnewmembers, newmembers);
    2585             : 
    2586         134 :         ReleaseSysCache(tuple);
    2587         134 :         pfree(new_acl);
    2588             : 
    2589             :         /* prevent error when processing duplicate objects */
    2590         134 :         CommandCounterIncrement();
    2591             :     }
    2592             : 
    2593          96 :     table_close(relation, RowExclusiveLock);
    2594          96 : }
    2595             : 
    2596             : 
    2597             : static AclMode
    2598       13828 : string_to_privilege(const char *privname)
    2599             : {
    2600       13828 :     if (strcmp(privname, "insert") == 0)
    2601         222 :         return ACL_INSERT;
    2602       13606 :     if (strcmp(privname, "select") == 0)
    2603        5914 :         return ACL_SELECT;
    2604        7692 :     if (strcmp(privname, "update") == 0)
    2605         322 :         return ACL_UPDATE;
    2606        7370 :     if (strcmp(privname, "delete") == 0)
    2607         114 :         return ACL_DELETE;
    2608        7256 :     if (strcmp(privname, "truncate") == 0)
    2609          34 :         return ACL_TRUNCATE;
    2610        7222 :     if (strcmp(privname, "references") == 0)
    2611          14 :         return ACL_REFERENCES;
    2612        7208 :     if (strcmp(privname, "trigger") == 0)
    2613           8 :         return ACL_TRIGGER;
    2614        7200 :     if (strcmp(privname, "execute") == 0)
    2615        6072 :         return ACL_EXECUTE;
    2616        1128 :     if (strcmp(privname, "usage") == 0)
    2617         602 :         return ACL_USAGE;
    2618         526 :     if (strcmp(privname, "create") == 0)
    2619         250 :         return ACL_CREATE;
    2620         276 :     if (strcmp(privname, "temporary") == 0)
    2621         158 :         return ACL_CREATE_TEMP;
    2622         118 :     if (strcmp(privname, "temp") == 0)
    2623           0 :         return ACL_CREATE_TEMP;
    2624         118 :     if (strcmp(privname, "connect") == 0)
    2625          14 :         return ACL_CONNECT;
    2626         104 :     if (strcmp(privname, "set") == 0)
    2627          48 :         return ACL_SET;
    2628          56 :     if (strcmp(privname, "alter system") == 0)
    2629          24 :         return ACL_ALTER_SYSTEM;
    2630          32 :     if (strcmp(privname, "maintain") == 0)
    2631          32 :         return ACL_MAINTAIN;
    2632           0 :     if (strcmp(privname, "rule") == 0)
    2633           0 :         return 0;               /* ignore old RULE privileges */
    2634           0 :     ereport(ERROR,
    2635             :             (errcode(ERRCODE_SYNTAX_ERROR),
    2636             :              errmsg("unrecognized privilege type \"%s\"", privname)));
    2637             :     return 0;                   /* appease compiler */
    2638             : }
    2639             : 
    2640             : static const char *
    2641          24 : privilege_to_string(AclMode privilege)
    2642             : {
    2643          24 :     switch (privilege)
    2644             :     {
    2645           6 :         case ACL_INSERT:
    2646           6 :             return "INSERT";
    2647           0 :         case ACL_SELECT:
    2648           0 :             return "SELECT";
    2649           0 :         case ACL_UPDATE:
    2650           0 :             return "UPDATE";
    2651           0 :         case ACL_DELETE:
    2652           0 :             return "DELETE";
    2653           0 :         case ACL_TRUNCATE:
    2654           0 :             return "TRUNCATE";
    2655           0 :         case ACL_REFERENCES:
    2656           0 :             return "REFERENCES";
    2657           0 :         case ACL_TRIGGER:
    2658           0 :             return "TRIGGER";
    2659           0 :         case ACL_EXECUTE:
    2660           0 :             return "EXECUTE";
    2661          18 :         case ACL_USAGE:
    2662          18 :             return "USAGE";
    2663           0 :         case ACL_CREATE:
    2664           0 :             return "CREATE";
    2665           0 :         case ACL_CREATE_TEMP:
    2666           0 :             return "TEMP";
    2667           0 :         case ACL_CONNECT:
    2668           0 :             return "CONNECT";
    2669           0 :         case ACL_SET:
    2670           0 :             return "SET";
    2671           0 :         case ACL_ALTER_SYSTEM:
    2672           0 :             return "ALTER SYSTEM";
    2673           0 :         case ACL_MAINTAIN:
    2674           0 :             return "MAINTAIN";
    2675           0 :         default:
    2676           0 :             elog(ERROR, "unrecognized privilege: %d", (int) privilege);
    2677             :     }
    2678             :     return NULL;                /* appease compiler */
    2679             : }
    2680             : 
    2681             : /*
    2682             :  * Standardized reporting of aclcheck permissions failures.
    2683             :  *
    2684             :  * Note: we do not double-quote the %s's below, because many callers
    2685             :  * supply strings that might be already quoted.
    2686             :  */
    2687             : void
    2688        2608 : aclcheck_error(AclResult aclerr, ObjectType objtype,
    2689             :                const char *objectname)
    2690             : {
    2691        2608 :     switch (aclerr)
    2692             :     {
    2693           0 :         case ACLCHECK_OK:
    2694             :             /* no error, so return to caller */
    2695           0 :             break;
    2696        2094 :         case ACLCHECK_NO_PRIV:
    2697             :             {
    2698        2094 :                 const char *msg = "???";
    2699             : 
    2700             :                 switch (objtype)
    2701             :                 {
    2702           6 :                     case OBJECT_AGGREGATE:
    2703           6 :                         msg = gettext_noop("permission denied for aggregate %s");
    2704           6 :                         break;
    2705           0 :                     case OBJECT_COLLATION:
    2706           0 :                         msg = gettext_noop("permission denied for collation %s");
    2707           0 :                         break;
    2708           0 :                     case OBJECT_COLUMN:
    2709           0 :                         msg = gettext_noop("permission denied for column %s");
    2710           0 :                         break;
    2711           0 :                     case OBJECT_CONVERSION:
    2712           0 :                         msg = gettext_noop("permission denied for conversion %s");
    2713           0 :                         break;
    2714          18 :                     case OBJECT_DATABASE:
    2715          18 :                         msg = gettext_noop("permission denied for database %s");
    2716          18 :                         break;
    2717           0 :                     case OBJECT_DOMAIN:
    2718           0 :                         msg = gettext_noop("permission denied for domain %s");
    2719           0 :                         break;
    2720           0 :                     case OBJECT_EVENT_TRIGGER:
    2721           0 :                         msg = gettext_noop("permission denied for event trigger %s");
    2722           0 :                         break;
    2723           0 :                     case OBJECT_EXTENSION:
    2724           0 :                         msg = gettext_noop("permission denied for extension %s");
    2725           0 :                         break;
    2726          44 :                     case OBJECT_FDW:
    2727          44 :                         msg = gettext_noop("permission denied for foreign-data wrapper %s");
    2728          44 :                         break;
    2729          20 :                     case OBJECT_FOREIGN_SERVER:
    2730          20 :                         msg = gettext_noop("permission denied for foreign server %s");
    2731          20 :                         break;
    2732           2 :                     case OBJECT_FOREIGN_TABLE:
    2733           2 :                         msg = gettext_noop("permission denied for foreign table %s");
    2734           2 :                         break;
    2735          90 :                     case OBJECT_FUNCTION:
    2736          90 :                         msg = gettext_noop("permission denied for function %s");
    2737          90 :                         break;
    2738          12 :                     case OBJECT_INDEX:
    2739          12 :                         msg = gettext_noop("permission denied for index %s");
    2740          12 :                         break;
    2741           8 :                     case OBJECT_LANGUAGE:
    2742           8 :                         msg = gettext_noop("permission denied for language %s");
    2743           8 :                         break;
    2744           0 :                     case OBJECT_LARGEOBJECT:
    2745           0 :                         msg = gettext_noop("permission denied for large object %s");
    2746           0 :                         break;
    2747           6 :                     case OBJECT_MATVIEW:
    2748           6 :                         msg = gettext_noop("permission denied for materialized view %s");
    2749           6 :                         break;
    2750           0 :                     case OBJECT_OPCLASS:
    2751           0 :                         msg = gettext_noop("permission denied for operator class %s");
    2752           0 :                         break;
    2753           0 :                     case OBJECT_OPERATOR:
    2754           0 :                         msg = gettext_noop("permission denied for operator %s");
    2755           0 :                         break;
    2756           0 :                     case OBJECT_OPFAMILY:
    2757           0 :                         msg = gettext_noop("permission denied for operator family %s");
    2758           0 :                         break;
    2759           0 :                     case OBJECT_PARAMETER_ACL:
    2760           0 :                         msg = gettext_noop("permission denied for parameter %s");
    2761           0 :                         break;
    2762           0 :                     case OBJECT_POLICY:
    2763           0 :                         msg = gettext_noop("permission denied for policy %s");
    2764           0 :                         break;
    2765          12 :                     case OBJECT_PROCEDURE:
    2766          12 :                         msg = gettext_noop("permission denied for procedure %s");
    2767          12 :                         break;
    2768           0 :                     case OBJECT_PUBLICATION:
    2769           0 :                         msg = gettext_noop("permission denied for publication %s");
    2770           0 :                         break;
    2771           0 :                     case OBJECT_ROUTINE:
    2772           0 :                         msg = gettext_noop("permission denied for routine %s");
    2773           0 :                         break;
    2774          14 :                     case OBJECT_SCHEMA:
    2775          14 :                         msg = gettext_noop("permission denied for schema %s");
    2776          14 :                         break;
    2777           0 :                     case OBJECT_SEQUENCE:
    2778           0 :                         msg = gettext_noop("permission denied for sequence %s");
    2779           0 :                         break;
    2780           0 :                     case OBJECT_STATISTIC_EXT:
    2781           0 :                         msg = gettext_noop("permission denied for statistics object %s");
    2782           0 :                         break;
    2783           0 :                     case OBJECT_SUBSCRIPTION:
    2784           0 :                         msg = gettext_noop("permission denied for subscription %s");
    2785           0 :                         break;
    2786        1338 :                     case OBJECT_TABLE:
    2787        1338 :                         msg = gettext_noop("permission denied for table %s");
    2788        1338 :                         break;
    2789          18 :                     case OBJECT_TABLESPACE:
    2790          18 :                         msg = gettext_noop("permission denied for tablespace %s");
    2791          18 :                         break;
    2792           0 :                     case OBJECT_TSCONFIGURATION:
    2793           0 :                         msg = gettext_noop("permission denied for text search configuration %s");
    2794           0 :                         break;
    2795           0 :                     case OBJECT_TSDICTIONARY:
    2796           0 :                         msg = gettext_noop("permission denied for text search dictionary %s");
    2797           0 :                         break;
    2798         120 :                     case OBJECT_TYPE:
    2799         120 :                         msg = gettext_noop("permission denied for type %s");
    2800         120 :                         break;
    2801         386 :                     case OBJECT_VIEW:
    2802         386 :                         msg = gettext_noop("permission denied for view %s");
    2803         386 :                         break;
    2804             :                         /* these currently aren't used */
    2805           0 :                     case OBJECT_ACCESS_METHOD:
    2806             :                     case OBJECT_AMOP:
    2807             :                     case OBJECT_AMPROC:
    2808             :                     case OBJECT_ATTRIBUTE:
    2809             :                     case OBJECT_CAST:
    2810             :                     case OBJECT_DEFAULT:
    2811             :                     case OBJECT_DEFACL:
    2812             :                     case OBJECT_DOMCONSTRAINT:
    2813             :                     case OBJECT_PUBLICATION_NAMESPACE:
    2814             :                     case OBJECT_PUBLICATION_REL:
    2815             :                     case OBJECT_ROLE:
    2816             :                     case OBJECT_RULE:
    2817             :                     case OBJECT_TABCONSTRAINT:
    2818             :                     case OBJECT_TRANSFORM:
    2819             :                     case OBJECT_TRIGGER:
    2820             :                     case OBJECT_TSPARSER:
    2821             :                     case OBJECT_TSTEMPLATE:
    2822             :                     case OBJECT_USER_MAPPING:
    2823           0 :                         elog(ERROR, "unsupported object type: %d", objtype);
    2824             :                 }
    2825             : 
    2826        2094 :                 ereport(ERROR,
    2827             :                         (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
    2828             :                          errmsg(msg, objectname)));
    2829             :                 break;
    2830             :             }
    2831         514 :         case ACLCHECK_NOT_OWNER:
    2832             :             {
    2833         514 :                 const char *msg = "???";
    2834             : 
    2835             :                 switch (objtype)
    2836             :                 {
    2837           6 :                     case OBJECT_AGGREGATE:
    2838           6 :                         msg = gettext_noop("must be owner of aggregate %s");
    2839           6 :                         break;
    2840           0 :                     case OBJECT_COLLATION:
    2841           0 :                         msg = gettext_noop("must be owner of collation %s");
    2842           0 :                         break;
    2843          18 :                     case OBJECT_CONVERSION:
    2844          18 :                         msg = gettext_noop("must be owner of conversion %s");
    2845          18 :                         break;
    2846           0 :                     case OBJECT_DATABASE:
    2847           0 :                         msg = gettext_noop("must be owner of database %s");
    2848           0 :                         break;
    2849           0 :                     case OBJECT_DOMAIN:
    2850           0 :                         msg = gettext_noop("must be owner of domain %s");
    2851           0 :                         break;
    2852           0 :                     case OBJECT_EVENT_TRIGGER:
    2853           0 :                         msg = gettext_noop("must be owner of event trigger %s");
    2854           0 :                         break;
    2855           0 :                     case OBJECT_EXTENSION:
    2856           0 :                         msg = gettext_noop("must be owner of extension %s");
    2857           0 :                         break;
    2858          18 :                     case OBJECT_FDW:
    2859          18 :                         msg = gettext_noop("must be owner of foreign-data wrapper %s");
    2860          18 :                         break;
    2861         114 :                     case OBJECT_FOREIGN_SERVER:
    2862         114 :                         msg = gettext_noop("must be owner of foreign server %s");
    2863         114 :                         break;
    2864           0 :                     case OBJECT_FOREIGN_TABLE:
    2865           0 :                         msg = gettext_noop("must be owner of foreign table %s");
    2866           0 :                         break;
    2867          42 :                     case OBJECT_FUNCTION:
    2868          42 :                         msg = gettext_noop("must be owner of function %s");
    2869          42 :                         break;
    2870          24 :                     case OBJECT_INDEX:
    2871          24 :                         msg = gettext_noop("must be owner of index %s");
    2872          24 :                         break;
    2873          12 :                     case OBJECT_LANGUAGE:
    2874          12 :                         msg = gettext_noop("must be owner of language %s");
    2875          12 :                         break;
    2876           0 :                     case OBJECT_LARGEOBJECT:
    2877           0 :                         msg = gettext_noop("must be owner of large object %s");
    2878           0 :                         break;
    2879           0 :                     case OBJECT_MATVIEW:
    2880           0 :                         msg = gettext_noop("must be owner of materialized view %s");
    2881           0 :                         break;
    2882          18 :                     case OBJECT_OPCLASS:
    2883          18 :                         msg = gettext_noop("must be owner of operator class %s");
    2884          18 :                         break;
    2885          18 :                     case OBJECT_OPERATOR:
    2886          18 :                         msg = gettext_noop("must be owner of operator %s");
    2887          18 :                         break;
    2888          18 :                     case OBJECT_OPFAMILY:
    2889          18 :                         msg = gettext_noop("must be owner of operator family %s");
    2890          18 :                         break;
    2891           6 :                     case OBJECT_PROCEDURE:
    2892           6 :                         msg = gettext_noop("must be owner of procedure %s");
    2893           6 :                         break;
    2894           6 :                     case OBJECT_PUBLICATION:
    2895           6 :                         msg = gettext_noop("must be owner of publication %s");
    2896           6 :                         break;
    2897           0 :                     case OBJECT_ROUTINE:
    2898           0 :                         msg = gettext_noop("must be owner of routine %s");
    2899           0 :                         break;
    2900           6 :                     case OBJECT_SEQUENCE:
    2901           6 :                         msg = gettext_noop("must be owner of sequence %s");
    2902           6 :                         break;
    2903           6 :                     case OBJECT_SUBSCRIPTION:
    2904           6 :                         msg = gettext_noop("must be owner of subscription %s");
    2905           6 :                         break;
    2906          70 :                     case OBJECT_TABLE:
    2907          70 :                         msg = gettext_noop("must be owner of table %s");
    2908          70 :                         break;
    2909           6 :                     case OBJECT_TYPE:
    2910           6 :                         msg = gettext_noop("must be owner of type %s");
    2911           6 :                         break;
    2912          18 :                     case OBJECT_VIEW:
    2913          18 :                         msg = gettext_noop("must be owner of view %s");
    2914          18 :                         break;
    2915          18 :                     case OBJECT_SCHEMA:
    2916          18 :                         msg = gettext_noop("must be owner of schema %s");
    2917          18 :                         break;
    2918          36 :                     case OBJECT_STATISTIC_EXT:
    2919          36 :                         msg = gettext_noop("must be owner of statistics object %s");
    2920          36 :                         break;
    2921           0 :                     case OBJECT_TABLESPACE:
    2922           0 :                         msg = gettext_noop("must be owner of tablespace %s");
    2923           0 :                         break;
    2924          18 :                     case OBJECT_TSCONFIGURATION:
    2925          18 :                         msg = gettext_noop("must be owner of text search configuration %s");
    2926          18 :                         break;
    2927          18 :                     case OBJECT_TSDICTIONARY:
    2928          18 :                         msg = gettext_noop("must be owner of text search dictionary %s");
    2929          18 :                         break;
    2930             : 
    2931             :                         /*
    2932             :                          * Special cases: For these, the error message talks
    2933             :                          * about "relation", because that's where the
    2934             :                          * ownership is attached.  See also
    2935             :                          * check_object_ownership().
    2936             :                          */
    2937          18 :                     case OBJECT_COLUMN:
    2938             :                     case OBJECT_POLICY:
    2939             :                     case OBJECT_RULE:
    2940             :                     case OBJECT_TABCONSTRAINT:
    2941             :                     case OBJECT_TRIGGER:
    2942          18 :                         msg = gettext_noop("must be owner of relation %s");
    2943          18 :                         break;
    2944             :                         /* these currently aren't used */
    2945           0 :                     case OBJECT_ACCESS_METHOD:
    2946             :                     case OBJECT_AMOP:
    2947             :                     case OBJECT_AMPROC:
    2948             :                     case OBJECT_ATTRIBUTE:
    2949             :                     case OBJECT_CAST:
    2950             :                     case OBJECT_DEFAULT:
    2951             :                     case OBJECT_DEFACL:
    2952             :                     case OBJECT_DOMCONSTRAINT:
    2953             :                     case OBJECT_PARAMETER_ACL:
    2954             :                     case OBJECT_PUBLICATION_NAMESPACE:
    2955             :                     case OBJECT_PUBLICATION_REL:
    2956             :                     case OBJECT_ROLE:
    2957             :                     case OBJECT_TRANSFORM:
    2958             :                     case OBJECT_TSPARSER:
    2959             :                     case OBJECT_TSTEMPLATE:
    2960             :                     case OBJECT_USER_MAPPING:
    2961           0 :                         elog(ERROR, "unsupported object type: %d", objtype);
    2962             :                 }
    2963             : 
    2964         514 :                 ereport(ERROR,
    2965             :                         (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
    2966             :                          errmsg(msg, objectname)));
    2967             :                 break;
    2968             :             }
    2969           0 :         default:
    2970           0 :             elog(ERROR, "unrecognized AclResult: %d", (int) aclerr);
    2971             :             break;
    2972             :     }
    2973           0 : }
    2974             : 
    2975             : 
    2976             : void
    2977           0 : aclcheck_error_col(AclResult aclerr, ObjectType objtype,
    2978             :                    const char *objectname, const char *colname)
    2979             : {
    2980           0 :     switch (aclerr)
    2981             :     {
    2982           0 :         case ACLCHECK_OK:
    2983             :             /* no error, so return to caller */
    2984           0 :             break;
    2985           0 :         case ACLCHECK_NO_PRIV:
    2986           0 :             ereport(ERROR,
    2987             :                     (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
    2988             :                      errmsg("permission denied for column \"%s\" of relation \"%s\"",
    2989             :                             colname, objectname)));
    2990             :             break;
    2991           0 :         case ACLCHECK_NOT_OWNER:
    2992             :             /* relation msg is OK since columns don't have separate owners */
    2993           0 :             aclcheck_error(aclerr, objtype, objectname);
    2994           0 :             break;
    2995           0 :         default:
    2996           0 :             elog(ERROR, "unrecognized AclResult: %d", (int) aclerr);
    2997             :             break;
    2998             :     }
    2999           0 : }
    3000             : 
    3001             : 
    3002             : /*
    3003             :  * Special common handling for types: use element type instead of array type,
    3004             :  * and format nicely
    3005             :  */
    3006             : void
    3007         120 : aclcheck_error_type(AclResult aclerr, Oid typeOid)
    3008             : {
    3009         120 :     Oid         element_type = get_element_type(typeOid);
    3010             : 
    3011         120 :     aclcheck_error(aclerr, OBJECT_TYPE, format_type_be(element_type ? element_type : typeOid));
    3012           0 : }
    3013             : 
    3014             : 
    3015             : /*
    3016             :  * Relay for the various pg_*_mask routines depending on object kind
    3017             :  */
    3018             : static AclMode
    3019          66 : pg_aclmask(ObjectType objtype, Oid object_oid, AttrNumber attnum, Oid roleid,
    3020             :            AclMode mask, AclMaskHow how)
    3021             : {
    3022          66 :     switch (objtype)
    3023             :     {
    3024           0 :         case OBJECT_COLUMN:
    3025             :             return
    3026           0 :                 pg_class_aclmask(object_oid, roleid, mask, how) |
    3027           0 :                 pg_attribute_aclmask(object_oid, attnum, roleid, mask, how);
    3028          12 :         case OBJECT_TABLE:
    3029             :         case OBJECT_SEQUENCE:
    3030          12 :             return pg_class_aclmask(object_oid, roleid, mask, how);
    3031           0 :         case OBJECT_DATABASE:
    3032           0 :             return object_aclmask(DatabaseRelationId, object_oid, roleid, mask, how);
    3033           0 :         case OBJECT_FUNCTION:
    3034           0 :             return object_aclmask(ProcedureRelationId, object_oid, roleid, mask, how);
    3035           6 :         case OBJECT_LANGUAGE:
    3036           6 :             return object_aclmask(LanguageRelationId, object_oid, roleid, mask, how);
    3037           0 :         case OBJECT_LARGEOBJECT:
    3038           0 :             return pg_largeobject_aclmask_snapshot(object_oid, roleid,
    3039             :                                                    mask, how, NULL);
    3040           0 :         case OBJECT_PARAMETER_ACL:
    3041           0 :             return pg_parameter_acl_aclmask(object_oid, roleid, mask, how);
    3042           0 :         case OBJECT_SCHEMA:
    3043           0 :             return object_aclmask(NamespaceRelationId, object_oid, roleid, mask, how);
    3044           0 :         case OBJECT_STATISTIC_EXT:
    3045           0 :             elog(ERROR, "grantable rights not supported for statistics objects");
    3046             :             /* not reached, but keep compiler quiet */
    3047             :             return ACL_NO_RIGHTS;
    3048           0 :         case OBJECT_TABLESPACE:
    3049           0 :             return object_aclmask(TableSpaceRelationId, object_oid, roleid, mask, how);
    3050          18 :         case OBJECT_FDW:
    3051          18 :             return object_aclmask(ForeignDataWrapperRelationId, object_oid, roleid, mask, how);
    3052          18 :         case OBJECT_FOREIGN_SERVER:
    3053          18 :             return object_aclmask(ForeignServerRelationId, object_oid, roleid, mask, how);
    3054           0 :         case OBJECT_EVENT_TRIGGER:
    3055           0 :             elog(ERROR, "grantable rights not supported for event triggers");
    3056             :             /* not reached, but keep compiler quiet */
    3057             :             return ACL_NO_RIGHTS;
    3058          12 :         case OBJECT_TYPE:
    3059          12 :             return object_aclmask(TypeRelationId, object_oid, roleid, mask, how);
    3060           0 :         default:
    3061           0 :             elog(ERROR, "unrecognized object type: %d",
    3062             :                  (int) objtype);
    3063             :             /* not reached, but keep compiler quiet */
    3064             :             return ACL_NO_RIGHTS;
    3065             :     }
    3066             : }
    3067             : 
    3068             : 
    3069             : /* ****************************************************************
    3070             :  * Exported routines for examining a user's privileges for various objects
    3071             :  *
    3072             :  * See aclmask() for a description of the common API for these functions.
    3073             :  *
    3074             :  * Note: we give lookup failure the full ereport treatment because the
    3075             :  * has_xxx_privilege() family of functions allow users to pass any random
    3076             :  * OID to these functions.
    3077             :  * ****************************************************************
    3078             :  */
    3079             : 
    3080             : /*
    3081             :  * Generic routine for examining a user's privileges for an object
    3082             :  */
    3083             : static AclMode
    3084          54 : object_aclmask(Oid classid, Oid objectid, Oid roleid,
    3085             :                AclMode mask, AclMaskHow how)
    3086             : {
    3087          54 :     return object_aclmask_ext(classid, objectid, roleid, mask, how, NULL);
    3088             : }
    3089             : 
    3090             : /*
    3091             :  * Generic routine for examining a user's privileges for an object,
    3092             :  * with is_missing
    3093             :  */
    3094             : static AclMode
    3095     2887122 : object_aclmask_ext(Oid classid, Oid objectid, Oid roleid,
    3096             :                    AclMode mask, AclMaskHow how,
    3097             :                    bool *is_missing)
    3098             : {
    3099             :     int         cacheid;
    3100             :     AclMode     result;
    3101             :     HeapTuple   tuple;
    3102             :     Datum       aclDatum;
    3103             :     bool        isNull;
    3104             :     Acl        *acl;
    3105             :     Oid         ownerId;
    3106             : 
    3107             :     /* Special cases */
    3108     2887122 :     switch (classid)
    3109             :     {
    3110      810128 :         case NamespaceRelationId:
    3111      810128 :             return pg_namespace_aclmask_ext(objectid, roleid, mask, how,
    3112             :                                             is_missing);
    3113      290872 :         case TypeRelationId:
    3114      290872 :             return pg_type_aclmask_ext(objectid, roleid, mask, how,
    3115             :                                        is_missing);
    3116             :     }
    3117             : 
    3118             :     /* Even more special cases */
    3119             :     Assert(classid != RelationRelationId);  /* should use pg_class_acl* */
    3120             :     Assert(classid != LargeObjectMetadataRelationId);   /* should use
    3121             :                                                          * pg_largeobject_acl* */
    3122             : 
    3123             :     /* Superusers bypass all permission checking. */
    3124     1786122 :     if (superuser_arg(roleid))
    3125     1748672 :         return mask;
    3126             : 
    3127             :     /*
    3128             :      * Get the object's ACL from its catalog
    3129             :      */
    3130             : 
    3131       37450 :     cacheid = get_object_catcache_oid(classid);
    3132             : 
    3133       37450 :     tuple = SearchSysCache1(cacheid, ObjectIdGetDatum(objectid));
    3134       37450 :     if (!HeapTupleIsValid(tuple))
    3135             :     {
    3136           0 :         if (is_missing != NULL)
    3137             :         {
    3138             :             /* return "no privileges" instead of throwing an error */
    3139           0 :             *is_missing = true;
    3140           0 :             return 0;
    3141             :         }
    3142             :         else
    3143           0 :             ereport(ERROR,
    3144             :                     (errcode(ERRCODE_UNDEFINED_OBJECT),
    3145             :                      errmsg("%s with OID %u does not exist",
    3146             :                             get_object_class_descr(classid), objectid)));
    3147             :     }
    3148             : 
    3149       37450 :     ownerId = DatumGetObjectId(SysCacheGetAttrNotNull(cacheid,
    3150             :                                                       tuple,
    3151       37450 :                                                       get_object_attnum_owner(classid)));
    3152             : 
    3153       37450 :     aclDatum = SysCacheGetAttr(cacheid, tuple, get_object_attnum_acl(classid),
    3154             :                                &isNull);
    3155       37450 :     if (isNull)
    3156             :     {
    3157             :         /* No ACL, so build default ACL */
    3158       34958 :         acl = acldefault(get_object_type(classid, objectid), ownerId);
    3159       34958 :         aclDatum = (Datum) 0;
    3160             :     }
    3161             :     else
    3162             :     {
    3163             :         /* detoast ACL if necessary */
    3164        2492 :         acl = DatumGetAclP(aclDatum);
    3165             :     }
    3166             : 
    3167       37450 :     result = aclmask(acl, roleid, ownerId, mask, how);
    3168             : 
    3169             :     /* if we have a detoasted copy, free it */
    3170       37450 :     if (acl && (Pointer) acl != DatumGetPointer(aclDatum))
    3171       37450 :         pfree(acl);
    3172             : 
    3173       37450 :     ReleaseSysCache(tuple);
    3174             : 
    3175       37450 :     return result;
    3176             : }
    3177             : 
    3178             : /*
    3179             :  * Routine for examining a user's privileges for a column
    3180             :  *
    3181             :  * Note: this considers only privileges granted specifically on the column.
    3182             :  * It is caller's responsibility to take relation-level privileges into account
    3183             :  * as appropriate.  (For the same reason, we have no special case for
    3184             :  * superuser-ness here.)
    3185             :  */
    3186             : static AclMode
    3187           0 : pg_attribute_aclmask(Oid table_oid, AttrNumber attnum, Oid roleid,
    3188             :                      AclMode mask, AclMaskHow how)
    3189             : {
    3190           0 :     return pg_attribute_aclmask_ext(table_oid, attnum, roleid,
    3191             :                                     mask, how, NULL);
    3192             : }
    3193             : 
    3194             : /*
    3195             :  * Routine for examining a user's privileges for a column, with is_missing
    3196             :  */
    3197             : static AclMode
    3198        5362 : pg_attribute_aclmask_ext(Oid table_oid, AttrNumber attnum, Oid roleid,
    3199             :                          AclMode mask, AclMaskHow how, bool *is_missing)
    3200             : {
    3201             :     AclMode     result;
    3202             :     HeapTuple   classTuple;
    3203             :     HeapTuple   attTuple;
    3204             :     Form_pg_class classForm;
    3205             :     Form_pg_attribute attributeForm;
    3206             :     Datum       aclDatum;
    3207             :     bool        isNull;
    3208             :     Acl        *acl;
    3209             :     Oid         ownerId;
    3210             : 
    3211             :     /*
    3212             :      * First, get the column's ACL from its pg_attribute entry
    3213             :      */
    3214        5362 :     attTuple = SearchSysCache2(ATTNUM,
    3215             :                                ObjectIdGetDatum(table_oid),
    3216             :                                Int16GetDatum(attnum));
    3217        5362 :     if (!HeapTupleIsValid(attTuple))
    3218             :     {
    3219          30 :         if (is_missing != NULL)
    3220             :         {
    3221             :             /* return "no privileges" instead of throwing an error */
    3222          30 :             *is_missing = true;
    3223          30 :             return 0;
    3224             :         }
    3225             :         else
    3226           0 :             ereport(ERROR,
    3227             :                     (errcode(ERRCODE_UNDEFINED_COLUMN),
    3228             :                      errmsg("attribute %d of relation with OID %u does not exist",
    3229             :                             attnum, table_oid)));
    3230             :     }
    3231             : 
    3232        5332 :     attributeForm = (Form_pg_attribute) GETSTRUCT(attTuple);
    3233             : 
    3234             :     /* Check dropped columns, too */
    3235        5332 :     if (attributeForm->attisdropped)
    3236             :     {
    3237          12 :         if (is_missing != NULL)
    3238             :         {
    3239             :             /* return "no privileges" instead of throwing an error */
    3240          12 :             *is_missing = true;
    3241          12 :             ReleaseSysCache(attTuple);
    3242          12 :             return 0;
    3243             :         }
    3244             :         else
    3245           0 :             ereport(ERROR,
    3246             :                     (errcode(ERRCODE_UNDEFINED_COLUMN),
    3247             :                      errmsg("attribute %d of relation with OID %u does not exist",
    3248             :                             attnum, table_oid)));
    3249             :     }
    3250             : 
    3251        5320 :     aclDatum = SysCacheGetAttr(ATTNUM, attTuple, Anum_pg_attribute_attacl,
    3252             :                                &isNull);
    3253             : 
    3254             :     /*
    3255             :      * Here we hard-wire knowledge that the default ACL for a column grants no
    3256             :      * privileges, so that we can fall out quickly in the very common case
    3257             :      * where attacl is null.
    3258             :      */
    3259        5320 :     if (isNull)
    3260             :     {
    3261        2826 :         ReleaseSysCache(attTuple);
    3262        2826 :         return 0;
    3263             :     }
    3264             : 
    3265             :     /*
    3266             :      * Must get the relation's ownerId from pg_class.  Since we already found
    3267             :      * a pg_attribute entry, the only likely reason for this to fail is that a
    3268             :      * concurrent DROP of the relation committed since then (which could only
    3269             :      * happen if we don't have lock on the relation).  Treat that similarly to
    3270             :      * not finding the attribute entry.
    3271             :      */
    3272        2494 :     classTuple = SearchSysCache1(RELOID, ObjectIdGetDatum(table_oid));
    3273        2494 :     if (!HeapTupleIsValid(classTuple))
    3274             :     {
    3275           0 :         ReleaseSysCache(attTuple);
    3276           0 :         if (is_missing != NULL)
    3277             :         {
    3278             :             /* return "no privileges" instead of throwing an error */
    3279           0 :             *is_missing = true;
    3280           0 :             return 0;
    3281             :         }
    3282             :         else
    3283           0 :             ereport(ERROR,
    3284             :                     (errcode(ERRCODE_UNDEFINED_TABLE),
    3285             :                      errmsg("relation with OID %u does not exist",
    3286             :                             table_oid)));
    3287             :     }
    3288        2494 :     classForm = (Form_pg_class) GETSTRUCT(classTuple);
    3289             : 
    3290        2494 :     ownerId = classForm->relowner;
    3291             : 
    3292        2494 :     ReleaseSysCache(classTuple);
    3293             : 
    3294             :     /* detoast column's ACL if necessary */
    3295        2494 :     acl = DatumGetAclP(aclDatum);
    3296             : 
    3297        2494 :     result = aclmask(acl, roleid, ownerId, mask, how);
    3298             : 
    3299             :     /* if we have a detoasted copy, free it */
    3300        2494 :     if (acl && (Pointer) acl != DatumGetPointer(aclDatum))
    3301        2494 :         pfree(acl);
    3302             : 
    3303        2494 :     ReleaseSysCache(attTuple);
    3304             : 
    3305        2494 :     return result;
    3306             : }
    3307             : 
    3308             : /*
    3309             :  * Exported routine for examining a user's privileges for a table
    3310             :  */
    3311             : AclMode
    3312      506808 : pg_class_aclmask(Oid table_oid, Oid roleid,
    3313             :                  AclMode mask, AclMaskHow how)
    3314             : {
    3315      506808 :     return pg_class_aclmask_ext(table_oid, roleid, mask, how, NULL);
    3316             : }
    3317             : 
    3318             : /*
    3319             :  * Routine for examining a user's privileges for a table, with is_missing
    3320             :  */
    3321             : static AclMode
    3322     2424740 : pg_class_aclmask_ext(Oid table_oid, Oid roleid, AclMode mask,
    3323             :                      AclMaskHow how, bool *is_missing)
    3324             : {
    3325             :     AclMode     result;
    3326             :     HeapTuple   tuple;
    3327             :     Form_pg_class classForm;
    3328             :     Datum       aclDatum;
    3329             :     bool        isNull;
    3330             :     Acl        *acl;
    3331             :     Oid         ownerId;
    3332             : 
    3333             :     /*
    3334             :      * Must get the relation's tuple from pg_class
    3335             :      */
    3336     2424740 :     tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(table_oid));
    3337     2424740 :     if (!HeapTupleIsValid(tuple))
    3338             :     {
    3339           8 :         if (is_missing != NULL)
    3340             :         {
    3341             :             /* return "no privileges" instead of throwing an error */
    3342           8 :             *is_missing = true;
    3343           8 :             return 0;
    3344             :         }
    3345             :         else
    3346           0 :             ereport(ERROR,
    3347             :                     (errcode(ERRCODE_UNDEFINED_TABLE),
    3348             :                      errmsg("relation with OID %u does not exist",
    3349             :                             table_oid)));
    3350             :     }
    3351             : 
    3352     2424732 :     classForm = (Form_pg_class) GETSTRUCT(tuple);
    3353             : 
    3354             :     /*
    3355             :      * Deny anyone permission to update a system catalog unless
    3356             :      * pg_authid.rolsuper is set.
    3357             :      *
    3358             :      * As of 7.4 we have some updatable system views; those shouldn't be
    3359             :      * protected in this way.  Assume the view rules can take care of
    3360             :      * themselves.  ACL_USAGE is if we ever have system sequences.
    3361             :      */
    3362     3088570 :     if ((mask & (ACL_INSERT | ACL_UPDATE | ACL_DELETE | ACL_TRUNCATE | ACL_USAGE)) &&
    3363      663838 :         IsSystemClass(table_oid, classForm) &&
    3364        4544 :         classForm->relkind != RELKIND_VIEW &&
    3365        4544 :         !superuser_arg(roleid))
    3366          70 :         mask &= ~(ACL_INSERT | ACL_UPDATE | ACL_DELETE | ACL_TRUNCATE | ACL_USAGE);
    3367             : 
    3368             :     /*
    3369             :      * Otherwise, superusers bypass all permission-checking.
    3370             :      */
    3371     2424732 :     if (superuser_arg(roleid))
    3372             :     {
    3373     2392432 :         ReleaseSysCache(tuple);
    3374     2392432 :         return mask;
    3375             :     }
    3376             : 
    3377             :     /*
    3378             :      * Normal case: get the relation's ACL from pg_class
    3379             :      */
    3380       32300 :     ownerId = classForm->relowner;
    3381             : 
    3382       32300 :     aclDatum = SysCacheGetAttr(RELOID, tuple, Anum_pg_class_relacl,
    3383             :                                &isNull);
    3384       32300 :     if (isNull)
    3385             :     {
    3386             :         /* No ACL, so build default ACL */
    3387        5846 :         switch (classForm->relkind)
    3388             :         {
    3389          36 :             case RELKIND_SEQUENCE:
    3390          36 :                 acl = acldefault(OBJECT_SEQUENCE, ownerId);
    3391          36 :                 break;
    3392        5810 :             default:
    3393        5810 :                 acl = acldefault(OBJECT_TABLE, ownerId);
    3394        5810 :                 break;
    3395             :         }
    3396        5846 :         aclDatum = (Datum) 0;
    3397             :     }
    3398             :     else
    3399             :     {
    3400             :         /* detoast rel's ACL if necessary */
    3401       26454 :         acl = DatumGetAclP(aclDatum);
    3402             :     }
    3403             : 
    3404       32300 :     result = aclmask(acl, roleid, ownerId, mask, how);
    3405             : 
    3406             :     /* if we have a detoasted copy, free it */
    3407       32300 :     if (acl && (Pointer) acl != DatumGetPointer(aclDatum))
    3408       32300 :         pfree(acl);
    3409             : 
    3410       32300 :     ReleaseSysCache(tuple);
    3411             : 
    3412             :     /*
    3413             :      * Check if ACL_SELECT is being checked and, if so, and not set already as
    3414             :      * part of the result, then check if the user is a member of the
    3415             :      * pg_read_all_data role, which allows read access to all relations.
    3416             :      */
    3417       34246 :     if (mask & ACL_SELECT && !(result & ACL_SELECT) &&
    3418        1946 :         has_privs_of_role(roleid, ROLE_PG_READ_ALL_DATA))
    3419          12 :         result |= ACL_SELECT;
    3420             : 
    3421             :     /*
    3422             :      * Check if ACL_INSERT, ACL_UPDATE, or ACL_DELETE is being checked and, if
    3423             :      * so, and not set already as part of the result, then check if the user
    3424             :      * is a member of the pg_write_all_data role, which allows
    3425             :      * INSERT/UPDATE/DELETE access to all relations (except system catalogs,
    3426             :      * which requires superuser, see above).
    3427             :      */
    3428       32300 :     if (mask & (ACL_INSERT | ACL_UPDATE | ACL_DELETE) &&
    3429        7268 :         !(result & (ACL_INSERT | ACL_UPDATE | ACL_DELETE)) &&
    3430        1674 :         has_privs_of_role(roleid, ROLE_PG_WRITE_ALL_DATA))
    3431          18 :         result |= (mask & (ACL_INSERT | ACL_UPDATE | ACL_DELETE));
    3432             : 
    3433             :     /*
    3434             :      * Check if ACL_MAINTAIN is being checked and, if so, and not already set
    3435             :      * as part of the result, then check if the user is a member of the
    3436             :      * pg_maintain role, which allows VACUUM, ANALYZE, CLUSTER, REFRESH
    3437             :      * MATERIALIZED VIEW, and REINDEX on all relations.
    3438             :      */
    3439       32300 :     if (mask & ACL_MAINTAIN &&
    3440        1878 :         !(result & ACL_MAINTAIN) &&
    3441         670 :         has_privs_of_role(roleid, ROLE_PG_MAINTAIN))
    3442          66 :         result |= ACL_MAINTAIN;
    3443             : 
    3444       32300 :     return result;
    3445             : }
    3446             : 
    3447             : /*
    3448             :  * Routine for examining a user's privileges for a configuration
    3449             :  * parameter (GUC), identified by GUC name.
    3450             :  */
    3451             : static AclMode
    3452         160 : pg_parameter_aclmask(const char *name, Oid roleid, AclMode mask, AclMaskHow how)
    3453             : {
    3454             :     AclMode     result;
    3455             :     char       *parname;
    3456             :     text       *partext;
    3457             :     HeapTuple   tuple;
    3458             : 
    3459             :     /* Superusers bypass all permission checking. */
    3460         160 :     if (superuser_arg(roleid))
    3461           2 :         return mask;
    3462             : 
    3463             :     /* Convert name to the form it should have in pg_parameter_acl... */
    3464         158 :     parname = convert_GUC_name_for_parameter_acl(name);
    3465         158 :     partext = cstring_to_text(parname);
    3466             : 
    3467             :     /* ... and look it up */
    3468         158 :     tuple = SearchSysCache1(PARAMETERACLNAME, PointerGetDatum(partext));
    3469             : 
    3470         158 :     if (!HeapTupleIsValid(tuple))
    3471             :     {
    3472             :         /* If no entry, GUC has no permissions for non-superusers */
    3473          70 :         result = ACL_NO_RIGHTS;
    3474             :     }
    3475             :     else
    3476             :     {
    3477             :         Datum       aclDatum;
    3478             :         bool        isNull;
    3479             :         Acl        *acl;
    3480             : 
    3481          88 :         aclDatum = SysCacheGetAttr(PARAMETERACLNAME, tuple,
    3482             :                                    Anum_pg_parameter_acl_paracl,
    3483             :                                    &isNull);
    3484          88 :         if (isNull)
    3485             :         {
    3486             :             /* No ACL, so build default ACL */
    3487           0 :             acl = acldefault(OBJECT_PARAMETER_ACL, BOOTSTRAP_SUPERUSERID);
    3488           0 :             aclDatum = (Datum) 0;
    3489             :         }
    3490             :         else
    3491             :         {
    3492             :             /* detoast ACL if necessary */
    3493          88 :             acl = DatumGetAclP(aclDatum);
    3494             :         }
    3495             : 
    3496          88 :         result = aclmask(acl, roleid, BOOTSTRAP_SUPERUSERID, mask, how);
    3497             : 
    3498             :         /* if we have a detoasted copy, free it */
    3499          88 :         if (acl && (Pointer) acl != DatumGetPointer(aclDatum))
    3500          88 :             pfree(acl);
    3501             : 
    3502          88 :         ReleaseSysCache(tuple);
    3503             :     }
    3504             : 
    3505         158 :     pfree(parname);
    3506         158 :     pfree(partext);
    3507             : 
    3508         158 :     return result;
    3509             : }
    3510             : 
    3511             : /*
    3512             :  * Routine for examining a user's privileges for a configuration
    3513             :  * parameter (GUC), identified by the OID of its pg_parameter_acl entry.
    3514             :  */
    3515             : static AclMode
    3516           0 : pg_parameter_acl_aclmask(Oid acl_oid, Oid roleid, AclMode mask, AclMaskHow how)
    3517             : {
    3518             :     AclMode     result;
    3519             :     HeapTuple   tuple;
    3520             :     Datum       aclDatum;
    3521             :     bool        isNull;
    3522             :     Acl        *acl;
    3523             : 
    3524             :     /* Superusers bypass all permission checking. */
    3525           0 :     if (superuser_arg(roleid))
    3526           0 :         return mask;
    3527             : 
    3528             :     /* Get the ACL from pg_parameter_acl */
    3529           0 :     tuple = SearchSysCache1(PARAMETERACLOID, ObjectIdGetDatum(acl_oid));
    3530           0 :     if (!HeapTupleIsValid(tuple))
    3531           0 :         ereport(ERROR,
    3532             :                 (errcode(ERRCODE_UNDEFINED_OBJECT),
    3533             :                  errmsg("parameter ACL with OID %u does not exist",
    3534             :                         acl_oid)));
    3535             : 
    3536           0 :     aclDatum = SysCacheGetAttr(PARAMETERACLOID, tuple,
    3537             :                                Anum_pg_parameter_acl_paracl,
    3538             :                                &isNull);
    3539           0 :     if (isNull)
    3540             :     {
    3541             :         /* No ACL, so build default ACL */
    3542           0 :         acl = acldefault(OBJECT_PARAMETER_ACL, BOOTSTRAP_SUPERUSERID);
    3543           0 :         aclDatum = (Datum) 0;
    3544             :     }
    3545             :     else
    3546             :     {
    3547             :         /* detoast ACL if necessary */
    3548           0 :         acl = DatumGetAclP(aclDatum);
    3549             :     }
    3550             : 
    3551           0 :     result = aclmask(acl, roleid, BOOTSTRAP_SUPERUSERID, mask, how);
    3552             : 
    3553             :     /* if we have a detoasted copy, free it */
    3554           0 :     if (acl && (Pointer) acl != DatumGetPointer(aclDatum))
    3555           0 :         pfree(acl);
    3556             : 
    3557           0 :     ReleaseSysCache(tuple);
    3558             : 
    3559           0 :     return result;
    3560             : }
    3561             : 
    3562             : /*
    3563             :  * Routine for examining a user's privileges for a largeobject
    3564             :  *
    3565             :  * When a large object is opened for reading, it is opened relative to the
    3566             :  * caller's snapshot, but when it is opened for writing, a current
    3567             :  * MVCC snapshot will be used.  See doc/src/sgml/lobj.sgml.  This function
    3568             :  * takes a snapshot argument so that the permissions check can be made
    3569             :  * relative to the same snapshot that will be used to read the underlying
    3570             :  * data.  The caller will actually pass NULL for an instantaneous MVCC
    3571             :  * snapshot, since all we do with the snapshot argument is pass it through
    3572             :  * to systable_beginscan().
    3573             :  */
    3574             : static AclMode
    3575         560 : pg_largeobject_aclmask_snapshot(Oid lobj_oid, Oid roleid,
    3576             :                                 AclMode mask, AclMaskHow how,
    3577             :                                 Snapshot snapshot)
    3578             : {
    3579             :     AclMode     result;
    3580             :     Relation    pg_lo_meta;
    3581             :     ScanKeyData entry[1];
    3582             :     SysScanDesc scan;
    3583             :     HeapTuple   tuple;
    3584             :     Datum       aclDatum;
    3585             :     bool        isNull;
    3586             :     Acl        *acl;
    3587             :     Oid         ownerId;
    3588             : 
    3589             :     /* Superusers bypass all permission checking. */
    3590         560 :     if (superuser_arg(roleid))
    3591         410 :         return mask;
    3592             : 
    3593             :     /*
    3594             :      * Get the largeobject's ACL from pg_largeobject_metadata
    3595             :      */
    3596         150 :     pg_lo_meta = table_open(LargeObjectMetadataRelationId,
    3597             :                             AccessShareLock);
    3598             : 
    3599         150 :     ScanKeyInit(&entry[0],
    3600             :                 Anum_pg_largeobject_metadata_oid,
    3601             :                 BTEqualStrategyNumber, F_OIDEQ,
    3602             :                 ObjectIdGetDatum(lobj_oid));
    3603             : 
    3604         150 :     scan = systable_beginscan(pg_lo_meta,
    3605             :                               LargeObjectMetadataOidIndexId, true,
    3606             :                               snapshot, 1, entry);
    3607             : 
    3608         150 :     tuple = systable_getnext(scan);
    3609         150 :     if (!HeapTupleIsValid(tuple))
    3610           0 :         ereport(ERROR,
    3611             :                 (errcode(ERRCODE_UNDEFINED_OBJECT),
    3612             :                  errmsg("large object %u does not exist", lobj_oid)));
    3613             : 
    3614         150 :     ownerId = ((Form_pg_largeobject_metadata) GETSTRUCT(tuple))->lomowner;
    3615             : 
    3616         150 :     aclDatum = heap_getattr(tuple, Anum_pg_largeobject_metadata_lomacl,
    3617             :                             RelationGetDescr(pg_lo_meta), &isNull);
    3618             : 
    3619         150 :     if (isNull)
    3620             :     {
    3621             :         /* No ACL, so build default ACL */
    3622          36 :         acl = acldefault(OBJECT_LARGEOBJECT, ownerId);
    3623          36 :         aclDatum = (Datum) 0;
    3624             :     }
    3625             :     else
    3626             :     {
    3627             :         /* detoast ACL if necessary */
    3628         114 :         acl = DatumGetAclP(aclDatum);
    3629             :     }
    3630             : 
    3631         150 :     result = aclmask(acl, roleid, ownerId, mask, how);
    3632             : 
    3633             :     /* if we have a detoasted copy, free it */
    3634         150 :     if (acl && (Pointer) acl != DatumGetPointer(aclDatum))
    3635         150 :         pfree(acl);
    3636             : 
    3637         150 :     systable_endscan(scan);
    3638             : 
    3639         150 :     table_close(pg_lo_meta, AccessShareLock);
    3640             : 
    3641         150 :     return result;
    3642             : }
    3643             : 
    3644             : /*
    3645             :  * Routine for examining a user's privileges for a namespace, with is_missing
    3646             :  */
    3647             : static AclMode
    3648      810128 : pg_namespace_aclmask_ext(Oid nsp_oid, Oid roleid,
    3649             :                          AclMode mask, AclMaskHow how,
    3650             :                          bool *is_missing)
    3651             : {
    3652             :     AclMode     result;
    3653             :     HeapTuple   tuple;
    3654             :     Datum       aclDatum;
    3655             :     bool        isNull;
    3656             :     Acl        *acl;
    3657             :     Oid         ownerId;
    3658             : 
    3659             :     /* Superusers bypass all permission checking. */
    3660      810128 :     if (superuser_arg(roleid))
    3661      794218 :         return mask;
    3662             : 
    3663             :     /*
    3664             :      * If we have been assigned this namespace as a temp namespace, check to
    3665             :      * make sure we have CREATE TEMP permission on the database, and if so act
    3666             :      * as though we have all standard (but not GRANT OPTION) permissions on
    3667             :      * the namespace.  If we don't have CREATE TEMP, act as though we have
    3668             :      * only USAGE (and not CREATE) rights.
    3669             :      *
    3670             :      * This may seem redundant given the check in InitTempTableNamespace, but
    3671             :      * it really isn't since current user ID may have changed since then. The
    3672             :      * upshot of this behavior is that a SECURITY DEFINER function can create
    3673             :      * temp tables that can then be accessed (if permission is granted) by
    3674             :      * code in the same session that doesn't have permissions to create temp
    3675             :      * tables.
    3676             :      *
    3677             :      * XXX Would it be safe to ereport a special error message as
    3678             :      * InitTempTableNamespace does?  Returning zero here means we'll get a
    3679             :      * generic "permission denied for schema pg_temp_N" message, which is not
    3680             :      * remarkably user-friendly.
    3681             :      */
    3682       15910 :     if (isTempNamespace(nsp_oid))
    3683             :     {
    3684         272 :         if (object_aclcheck_ext(DatabaseRelationId, MyDatabaseId, roleid,
    3685             :                                 ACL_CREATE_TEMP, is_missing) == ACLCHECK_OK)
    3686         272 :             return mask & ACL_ALL_RIGHTS_SCHEMA;
    3687             :         else
    3688           0 :             return mask & ACL_USAGE;
    3689             :     }
    3690             : 
    3691             :     /*
    3692             :      * Get the schema's ACL from pg_namespace
    3693             :      */
    3694       15638 :     tuple = SearchSysCache1(NAMESPACEOID, ObjectIdGetDatum(nsp_oid));
    3695       15638 :     if (!HeapTupleIsValid(tuple))
    3696             :     {
    3697           0 :         if (is_missing != NULL)
    3698             :         {
    3699             :             /* return "no privileges" instead of throwing an error */
    3700           0 :             *is_missing = true;
    3701           0 :             return 0;
    3702             :         }
    3703             :         else
    3704           0 :             ereport(ERROR,
    3705             :                     (errcode(ERRCODE_UNDEFINED_SCHEMA),
    3706             :                      errmsg("schema with OID %u does not exist", nsp_oid)));
    3707             :     }
    3708             : 
    3709       15638 :     ownerId = ((Form_pg_namespace) GETSTRUCT(tuple))->nspowner;
    3710             : 
    3711       15638 :     aclDatum = SysCacheGetAttr(NAMESPACEOID, tuple, Anum_pg_namespace_nspacl,
    3712             :                                &isNull);
    3713       15638 :     if (isNull)
    3714             :     {
    3715             :         /* No ACL, so build default ACL */
    3716         288 :         acl = acldefault(OBJECT_SCHEMA, ownerId);
    3717         288 :         aclDatum = (Datum) 0;
    3718             :     }
    3719             :     else
    3720             :     {
    3721             :         /* detoast ACL if necessary */
    3722       15350 :         acl = DatumGetAclP(aclDatum);
    3723             :     }
    3724             : 
    3725       15638 :     result = aclmask(acl, roleid, ownerId, mask, how);
    3726             : 
    3727             :     /* if we have a detoasted copy, free it */
    3728       15638 :     if (acl && (Pointer) acl != DatumGetPointer(aclDatum))
    3729       15638 :         pfree(acl);
    3730             : 
    3731       15638 :     ReleaseSysCache(tuple);
    3732             : 
    3733             :     /*
    3734             :      * Check if ACL_USAGE is being checked and, if so, and not set already as
    3735             :      * part of the result, then check if the user is a member of the
    3736             :      * pg_read_all_data or pg_write_all_data roles, which allow usage access
    3737             :      * to all schemas.
    3738             :      */
    3739       15676 :     if (mask & ACL_USAGE && !(result & ACL_USAGE) &&
    3740          70 :         (has_privs_of_role(roleid, ROLE_PG_READ_ALL_DATA) ||
    3741          32 :          has_privs_of_role(roleid, ROLE_PG_WRITE_ALL_DATA)))
    3742          12 :         result |= ACL_USAGE;
    3743       15638 :     return result;
    3744             : }
    3745             : 
    3746             : /*
    3747             :  * Routine for examining a user's privileges for a type, with is_missing
    3748             :  */
    3749             : static AclMode
    3750      290872 : pg_type_aclmask_ext(Oid type_oid, Oid roleid, AclMode mask, AclMaskHow how,
    3751             :                     bool *is_missing)
    3752             : {
    3753             :     AclMode     result;
    3754             :     HeapTuple   tuple;
    3755             :     Form_pg_type typeForm;
    3756             :     Datum       aclDatum;
    3757             :     bool        isNull;
    3758             :     Acl        *acl;
    3759             :     Oid         ownerId;
    3760             : 
    3761             :     /* Bypass permission checks for superusers */
    3762      290872 :     if (superuser_arg(roleid))
    3763      286636 :         return mask;
    3764             : 
    3765             :     /*
    3766             :      * Must get the type's tuple from pg_type
    3767             :      */
    3768        4236 :     tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(type_oid));
    3769        4236 :     if (!HeapTupleIsValid(tuple))
    3770             :     {
    3771           0 :         if (is_missing != NULL)
    3772             :         {
    3773             :             /* return "no privileges" instead of throwing an error */
    3774           0 :             *is_missing = true;
    3775           0 :             return 0;
    3776             :         }
    3777             :         else
    3778           0 :             ereport(ERROR,
    3779             :                     (errcode(ERRCODE_UNDEFINED_OBJECT),
    3780             :                      errmsg("type with OID %u does not exist",
    3781             :                             type_oid)));
    3782             :     }
    3783        4236 :     typeForm = (Form_pg_type) GETSTRUCT(tuple);
    3784             : 
    3785             :     /*
    3786             :      * "True" array types don't manage permissions of their own; consult the
    3787             :      * element type instead.
    3788             :      */
    3789        4236 :     if (IsTrueArrayType(typeForm))
    3790             :     {
    3791          48 :         Oid         elttype_oid = typeForm->typelem;
    3792             : 
    3793          48 :         ReleaseSysCache(tuple);
    3794             : 
    3795          48 :         tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(elttype_oid));
    3796          48 :         if (!HeapTupleIsValid(tuple))
    3797             :         {
    3798           0 :             if (is_missing != NULL)
    3799             :             {
    3800             :                 /* return "no privileges" instead of throwing an error */
    3801           0 :                 *is_missing = true;
    3802           0 :                 return 0;
    3803             :             }
    3804             :             else
    3805           0 :                 ereport(ERROR,
    3806             :                         (errcode(ERRCODE_UNDEFINED_OBJECT),
    3807             :                          errmsg("type with OID %u does not exist",
    3808             :                                 elttype_oid)));
    3809             :         }
    3810          48 :         typeForm = (Form_pg_type) GETSTRUCT(tuple);
    3811             :     }
    3812             : 
    3813             :     /*
    3814             :      * Likewise, multirange types don't manage their own permissions; consult
    3815             :      * the associated range type.  (Note we must do this after the array step
    3816             :      * to get the right answer for arrays of multiranges.)
    3817             :      */
    3818        4236 :     if (typeForm->typtype == TYPTYPE_MULTIRANGE)
    3819             :     {
    3820          12 :         Oid         rangetype = get_multirange_range(typeForm->oid);
    3821             : 
    3822          12 :         ReleaseSysCache(tuple);
    3823             : 
    3824          12 :         tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(rangetype));
    3825          12 :         if (!HeapTupleIsValid(tuple))
    3826             :         {
    3827           0 :             if (is_missing != NULL)
    3828             :             {
    3829             :                 /* return "no privileges" instead of throwing an error */
    3830           0 :                 *is_missing = true;
    3831           0 :                 return 0;
    3832             :             }
    3833             :             else
    3834           0 :                 ereport(ERROR,
    3835             :                         (errcode(ERRCODE_UNDEFINED_OBJECT),
    3836             :                          errmsg("type with OID %u does not exist",
    3837             :                                 rangetype)));
    3838             :         }
    3839          12 :         typeForm = (Form_pg_type) GETSTRUCT(tuple);
    3840             :     }
    3841             : 
    3842             :     /*
    3843             :      * Now get the type's owner and ACL from the tuple
    3844             :      */
    3845        4236 :     ownerId = typeForm->typowner;
    3846             : 
    3847        4236 :     aclDatum = SysCacheGetAttr(TYPEOID, tuple,
    3848             :                                Anum_pg_type_typacl, &isNull);
    3849        4236 :     if (isNull)
    3850             :     {
    3851             :         /* No ACL, so build default ACL */
    3852        4002 :         acl = acldefault(OBJECT_TYPE, ownerId);
    3853        4002 :         aclDatum = (Datum) 0;
    3854             :     }
    3855             :     else
    3856             :     {
    3857             :         /* detoast rel's ACL if necessary */
    3858         234 :         acl = DatumGetAclP(aclDatum);
    3859             :     }
    3860             : 
    3861        4236 :     result = aclmask(acl, roleid, ownerId, mask, how);
    3862             : 
    3863             :     /* if we have a detoasted copy, free it */
    3864        4236 :     if (acl && (Pointer) acl != DatumGetPointer(aclDatum))
    3865        4236 :         pfree(acl);
    3866             : 
    3867        4236 :     ReleaseSysCache(tuple);
    3868             : 
    3869        4236 :     return result;
    3870             : }
    3871             : 
    3872             : /*
    3873             :  * Exported generic routine for checking a user's access privileges to an object
    3874             :  */
    3875             : AclResult
    3876     2886688 : object_aclcheck(Oid classid, Oid objectid, Oid roleid, AclMode mode)
    3877             : {
    3878     2886688 :     return object_aclcheck_ext(classid, objectid, roleid, mode, NULL);
    3879             : }
    3880             : 
    3881             : /*
    3882             :  * Exported generic routine for checking a user's access privileges to an
    3883             :  * object, with is_missing
    3884             :  */
    3885             : AclResult
    3886     2887068 : object_aclcheck_ext(Oid classid, Oid objectid,
    3887             :                     Oid roleid, AclMode mode,
    3888             :                     bool *is_missing)
    3889             : {
    3890     2887068 :     if (object_aclmask_ext(classid, objectid, roleid, mode, ACLMASK_ANY,
    3891             :                            is_missing) != 0)
    3892     2886524 :         return ACLCHECK_OK;
    3893             :     else
    3894         544 :         return ACLCHECK_NO_PRIV;
    3895             : }
    3896             : 
    3897             : /*
    3898             :  * Exported routine for checking a user's access privileges to a column
    3899             :  *
    3900             :  * Returns ACLCHECK_OK if the user has any of the privileges identified by
    3901             :  * 'mode'; otherwise returns a suitable error code (in practice, always
    3902             :  * ACLCHECK_NO_PRIV).
    3903             :  *
    3904             :  * As with pg_attribute_aclmask, only privileges granted directly on the
    3905             :  * column are considered here.
    3906             :  */
    3907             : AclResult
    3908        3538 : pg_attribute_aclcheck(Oid table_oid, AttrNumber attnum,
    3909             :                       Oid roleid, AclMode mode)
    3910             : {
    3911        3538 :     return pg_attribute_aclcheck_ext(table_oid, attnum, roleid, mode, NULL);
    3912             : }
    3913             : 
    3914             : 
    3915             : /*
    3916             :  * Exported routine for checking a user's access privileges to a column,
    3917             :  * with is_missing
    3918             :  */
    3919             : AclResult
    3920        5362 : pg_attribute_aclcheck_ext(Oid table_oid, AttrNumber attnum,
    3921             :                           Oid roleid, AclMode mode, bool *is_missing)
    3922             : {
    3923        5362 :     if (pg_attribute_aclmask_ext(table_oid, attnum, roleid, mode,
    3924             :                                  ACLMASK_ANY, is_missing) != 0)
    3925        1960 :         return ACLCHECK_OK;
    3926             :     else
    3927        3402 :         return ACLCHECK_NO_PRIV;
    3928             : }
    3929             : 
    3930             : /*
    3931             :  * Exported routine for checking a user's access privileges to any/all columns
    3932             :  *
    3933             :  * If 'how' is ACLMASK_ANY, then returns ACLCHECK_OK if user has any of the
    3934             :  * privileges identified by 'mode' on any non-dropped column in the relation;
    3935             :  * otherwise returns a suitable error code (in practice, always
    3936             :  * ACLCHECK_NO_PRIV).
    3937             :  *
    3938             :  * If 'how' is ACLMASK_ALL, then returns ACLCHECK_OK if user has any of the
    3939             :  * privileges identified by 'mode' on each non-dropped column in the relation
    3940             :  * (and there must be at least one such column); otherwise returns a suitable
    3941             :  * error code (in practice, always ACLCHECK_NO_PRIV).
    3942             :  *
    3943             :  * As with pg_attribute_aclmask, only privileges granted directly on the
    3944             :  * column(s) are considered here.
    3945             :  *
    3946             :  * Note: system columns are not considered here; there are cases where that
    3947             :  * might be appropriate but there are also cases where it wouldn't.
    3948             :  */
    3949             : AclResult
    3950         162 : pg_attribute_aclcheck_all(Oid table_oid, Oid roleid, AclMode mode,
    3951             :                           AclMaskHow how)
    3952             : {
    3953         162 :     return pg_attribute_aclcheck_all_ext(table_oid, roleid, mode, how, NULL);
    3954             : }
    3955             : 
    3956             : /*
    3957             :  * Exported routine for checking a user's access privileges to any/all columns,
    3958             :  * with is_missing
    3959             :  */
    3960             : AclResult
    3961         162 : pg_attribute_aclcheck_all_ext(Oid table_oid, Oid roleid,
    3962             :                               AclMode mode, AclMaskHow how,
    3963             :                               bool *is_missing)
    3964             : {
    3965             :     AclResult   result;
    3966             :     HeapTuple   classTuple;
    3967             :     Form_pg_class classForm;
    3968             :     Oid         ownerId;
    3969             :     AttrNumber  nattrs;
    3970             :     AttrNumber  curr_att;
    3971             : 
    3972             :     /*
    3973             :      * Must fetch pg_class row to get owner ID and number of attributes.
    3974             :      */
    3975         162 :     classTuple = SearchSysCache1(RELOID, ObjectIdGetDatum(table_oid));
    3976         162 :     if (!HeapTupleIsValid(classTuple))
    3977             :     {
    3978           0 :         if (is_missing != NULL)
    3979             :         {
    3980             :             /* return "no privileges" instead of throwing an error */
    3981           0 :             *is_missing = true;
    3982           0 :             return ACLCHECK_NO_PRIV;
    3983             :         }
    3984             :         else
    3985           0 :             ereport(ERROR,
    3986             :                     (errcode(ERRCODE_UNDEFINED_TABLE),
    3987             :                      errmsg("relation with OID %u does not exist",
    3988             :                             table_oid)));
    3989             :     }
    3990         162 :     classForm = (Form_pg_class) GETSTRUCT(classTuple);
    3991             : 
    3992         162 :     ownerId = classForm->relowner;
    3993         162 :     nattrs = classForm->relnatts;
    3994             : 
    3995         162 :     ReleaseSysCache(classTuple);
    3996             : 
    3997             :     /*
    3998             :      * Initialize result in case there are no non-dropped columns.  We want to
    3999             :      * report failure in such cases for either value of 'how'.
    4000             :      */
    4001         162 :     result = ACLCHECK_NO_PRIV;
    4002             : 
    4003         414 :     for (curr_att = 1; curr_att <= nattrs; curr_att++)
    4004             :     {
    4005             :         HeapTuple   attTuple;
    4006             :         Datum       aclDatum;
    4007             :         bool        isNull;
    4008             :         Acl        *acl;
    4009             :         AclMode     attmask;
    4010             : 
    4011         330 :         attTuple = SearchSysCache2(ATTNUM,
    4012             :                                    ObjectIdGetDatum(table_oid),
    4013             :                                    Int16GetDatum(curr_att));
    4014             : 
    4015             :         /*
    4016             :          * Lookup failure probably indicates that the table was just dropped,
    4017             :          * but we'll treat it the same as a dropped column rather than
    4018             :          * throwing error.
    4019             :          */
    4020         330 :         if (!HeapTupleIsValid(attTuple))
    4021          18 :             continue;
    4022             : 
    4023             :         /* ignore dropped columns */
    4024         330 :         if (((Form_pg_attribute) GETSTRUCT(attTuple))->attisdropped)
    4025             :         {
    4026          18 :             ReleaseSysCache(attTuple);
    4027          18 :             continue;
    4028             :         }
    4029             : 
    4030         312 :         aclDatum = SysCacheGetAttr(ATTNUM, attTuple, Anum_pg_attribute_attacl,
    4031             :                                    &isNull);
    4032             : 
    4033             :         /*
    4034             :          * Here we hard-wire knowledge that the default ACL for a column
    4035             :          * grants no privileges, so that we can fall out quickly in the very
    4036             :          * common case where attacl is null.
    4037             :          */
    4038         312 :         if (isNull)
    4039         156 :             attmask = 0;
    4040             :         else
    4041             :         {
    4042             :             /* detoast column's ACL if necessary */
    4043         156 :             acl = DatumGetAclP(aclDatum);
    4044             : 
    4045         156 :             attmask = aclmask(acl, roleid, ownerId, mode, ACLMASK_ANY);
    4046             : 
    4047             :             /* if we have a detoasted copy, free it */
    4048         156 :             if ((Pointer) acl != DatumGetPointer(aclDatum))
    4049         156 :                 pfree(acl);
    4050             :         }
    4051             : 
    4052         312 :         ReleaseSysCache(attTuple);
    4053             : 
    4054         312 :         if (attmask != 0)
    4055             :         {
    4056         138 :             result = ACLCHECK_OK;
    4057         138 :             if (how == ACLMASK_ANY)
    4058          78 :                 break;          /* succeed on any success */
    4059             :         }
    4060             :         else
    4061             :         {
    4062         174 :             result = ACLCHECK_NO_PRIV;
    4063         174 :             if (how == ACLMASK_ALL)
    4064          36 :                 break;          /* fail on any failure */
    4065             :         }
    4066             :     }
    4067             : 
    4068         162 :     return result;
    4069             : }
    4070             : 
    4071             : /*
    4072             :  * Exported routine for checking a user's access privileges to a table
    4073             :  *
    4074             :  * Returns ACLCHECK_OK if the user has any of the privileges identified by
    4075             :  * 'mode'; otherwise returns a suitable error code (in practice, always
    4076             :  * ACLCHECK_NO_PRIV).
    4077             :  */
    4078             : AclResult
    4079     1915872 : pg_class_aclcheck(Oid table_oid, Oid roleid, AclMode mode)
    4080             : {
    4081     1915872 :     return pg_class_aclcheck_ext(table_oid, roleid, mode, NULL);
    4082             : }
    4083             : 
    4084             : /*
    4085             :  * Exported routine for checking a user's access privileges to a table,
    4086             :  * with is_missing
    4087             :  */
    4088             : AclResult
    4089     1917932 : pg_class_aclcheck_ext(Oid table_oid, Oid roleid,
    4090             :                       AclMode mode, bool *is_missing)
    4091             : {
    4092     1917932 :     if (pg_class_aclmask_ext(table_oid, roleid, mode,
    4093             :                              ACLMASK_ANY, is_missing) != 0)
    4094     1916782 :         return ACLCHECK_OK;
    4095             :     else
    4096        1150 :         return ACLCHECK_NO_PRIV;
    4097             : }
    4098             : 
    4099             : /*
    4100             :  * Exported routine for checking a user's access privileges to a configuration
    4101             :  * parameter (GUC), identified by GUC name.
    4102             :  */
    4103             : AclResult
    4104         160 : pg_parameter_aclcheck(const char *name, Oid roleid, AclMode mode)
    4105             : {
    4106         160 :     if (pg_parameter_aclmask(name, roleid, mode, ACLMASK_ANY) != 0)
    4107          68 :         return ACLCHECK_OK;
    4108             :     else
    4109          92 :         return ACLCHECK_NO_PRIV;
    4110             : }
    4111             : 
    4112             : /*
    4113             :  * Exported routine for checking a user's access privileges to a largeobject
    4114             :  */
    4115             : AclResult
    4116         560 : pg_largeobject_aclcheck_snapshot(Oid lobj_oid, Oid roleid, AclMode mode,
    4117             :                                  Snapshot snapshot)
    4118             : {
    4119         560 :     if (pg_largeobject_aclmask_snapshot(lobj_oid, roleid, mode,
    4120             :                                         ACLMASK_ANY, snapshot) != 0)
    4121         506 :         return ACLCHECK_OK;
    4122             :     else
    4123          54 :         return ACLCHECK_NO_PRIV;
    4124             : }
    4125             : 
    4126             : /*
    4127             :  * Generic ownership check for an object
    4128             :  */
    4129             : bool
    4130      232370 : object_ownercheck(Oid classid, Oid objectid, Oid roleid)
    4131             : {
    4132             :     int         cacheid;
    4133             :     Oid         ownerId;
    4134             : 
    4135             :     /* Superusers bypass all permission checking. */
    4136      232370 :     if (superuser_arg(roleid))
    4137      224544 :         return true;
    4138             : 
    4139             :     /* For large objects, the catalog to consult is pg_largeobject_metadata */
    4140        7826 :     if (classid == LargeObjectRelationId)
    4141          24 :         classid = LargeObjectMetadataRelationId;
    4142             : 
    4143        7826 :     cacheid = get_object_catcache_oid(classid);
    4144        7826 :     if (cacheid != -1)
    4145             :     {
    4146             :         /* we can get the object's tuple from the syscache */
    4147             :         HeapTuple   tuple;
    4148             : 
    4149        7798 :         tuple = SearchSysCache1(cacheid, ObjectIdGetDatum(objectid));
    4150        7798 :         if (!HeapTupleIsValid(tuple))
    4151           0 :             ereport(ERROR,
    4152             :                     (errcode(ERRCODE_UNDEFINED_OBJECT),
    4153             :                      errmsg("%s with OID %u does not exist", get_object_class_descr(classid), objectid)));
    4154             : 
    4155        7798 :         ownerId = DatumGetObjectId(SysCacheGetAttrNotNull(cacheid,
    4156             :                                                           tuple,
    4157        7798 :                                                           get_object_attnum_owner(classid)));
    4158        7798 :         ReleaseSysCache(tuple);
    4159             :     }
    4160             :     else
    4161             :     {
    4162             :         /* for catalogs without an appropriate syscache */
    4163             :         Relation    rel;
    4164             :         ScanKeyData entry[1];
    4165             :         SysScanDesc scan;
    4166             :         HeapTuple   tuple;
    4167             :         bool        isnull;
    4168             : 
    4169          28 :         rel = table_open(classid, AccessShareLock);
    4170             : 
    4171          56 :         ScanKeyInit(&entry[0],
    4172          28 :                     get_object_attnum_oid(classid),
    4173             :                     BTEqualStrategyNumber, F_OIDEQ,
    4174             :                     ObjectIdGetDatum(objectid));
    4175             : 
    4176          28 :         scan = systable_beginscan(rel,
    4177             :                                   get_object_oid_index(classid), true,
    4178             :                                   NULL, 1, entry);
    4179             : 
    4180          28 :         tuple = systable_getnext(scan);
    4181          28 :         if (!HeapTupleIsValid(tuple))
    4182           0 :             ereport(ERROR,
    4183             :                     (errcode(ERRCODE_UNDEFINED_OBJECT),
    4184             :                      errmsg("%s with OID %u does not exist", get_object_class_descr(classid), objectid)));
    4185             : 
    4186          28 :         ownerId = DatumGetObjectId(heap_getattr(tuple,
    4187          28 :                                                 get_object_attnum_owner(classid),
    4188             :                                                 RelationGetDescr(rel),
    4189             :                                                 &isnull));
    4190             :         Assert(!isnull);
    4191             : 
    4192          28 :         systable_endscan(scan);
    4193          28 :         table_close(rel, AccessShareLock);
    4194             :     }
    4195             : 
    4196        7826 :     return has_privs_of_role(roleid, ownerId);
    4197             : }
    4198             : 
    4199             : /*
    4200             :  * Check whether specified role has CREATEROLE privilege (or is a superuser)
    4201             :  *
    4202             :  * Note: roles do not have owners per se; instead we use this test in
    4203             :  * places where an ownership-like permissions test is needed for a role.
    4204             :  * Be sure to apply it to the role trying to do the operation, not the
    4205             :  * role being operated on!  Also note that this generally should not be
    4206             :  * considered enough privilege if the target role is a superuser.
    4207             :  * (We don't handle that consideration here because we want to give a
    4208             :  * separate error message for such cases, so the caller has to deal with it.)
    4209             :  */
    4210             : bool
    4211        2340 : has_createrole_privilege(Oid roleid)
    4212             : {
    4213        2340 :     bool        result = false;
    4214             :     HeapTuple   utup;
    4215             : 
    4216             :     /* Superusers bypass all permission checking. */
    4217        2340 :     if (superuser_arg(roleid))
    4218        1822 :         return true;
    4219             : 
    4220         518 :     utup = SearchSysCache1(AUTHOID, ObjectIdGetDatum(roleid));
    4221         518 :     if (HeapTupleIsValid(utup))
    4222             :     {
    4223         518 :         result = ((Form_pg_authid) GETSTRUCT(utup))->rolcreaterole;
    4224         518 :         ReleaseSysCache(utup);
    4225             :     }
    4226         518 :     return result;
    4227             : }
    4228             : 
    4229             : bool
    4230        4484 : has_bypassrls_privilege(Oid roleid)
    4231             : {
    4232        4484 :     bool        result = false;
    4233             :     HeapTuple   utup;
    4234             : 
    4235             :     /* Superusers bypass all permission checking. */
    4236        4484 :     if (superuser_arg(roleid))
    4237        1378 :         return true;
    4238             : 
    4239        3106 :     utup = SearchSysCache1(AUTHOID, ObjectIdGetDatum(roleid));
    4240        3106 :     if (HeapTupleIsValid(utup))
    4241             :     {
    4242        3106 :         result = ((Form_pg_authid) GETSTRUCT(utup))->rolbypassrls;
    4243        3106 :         ReleaseSysCache(utup);
    4244             :     }
    4245        3106 :     return result;
    4246             : }
    4247             : 
    4248             : /*
    4249             :  * Fetch pg_default_acl entry for given role, namespace and object type
    4250             :  * (object type must be given in pg_default_acl's encoding).
    4251             :  * Returns NULL if no such entry.
    4252             :  */
    4253             : static Acl *
    4254      140448 : get_default_acl_internal(Oid roleId, Oid nsp_oid, char objtype)
    4255             : {
    4256      140448 :     Acl        *result = NULL;
    4257             :     HeapTuple   tuple;
    4258             : 
    4259      140448 :     tuple = SearchSysCache3(DEFACLROLENSPOBJ,
    4260             :                             ObjectIdGetDatum(roleId),
    4261             :                             ObjectIdGetDatum(nsp_oid),
    4262             :                             CharGetDatum(objtype));
    4263             : 
    4264      140448 :     if (HeapTupleIsValid(tuple))
    4265             :     {
    4266             :         Datum       aclDatum;
    4267             :         bool        isNull;
    4268             : 
    4269         228 :         aclDatum = SysCacheGetAttr(DEFACLROLENSPOBJ, tuple,
    4270             :                                    Anum_pg_default_acl_defaclacl,
    4271             :                                    &isNull);
    4272         228 :         if (!isNull)
    4273         228 :             result = DatumGetAclPCopy(aclDatum);
    4274         228 :         ReleaseSysCache(tuple);
    4275             :     }
    4276             : 
    4277      140448 :     return result;
    4278             : }
    4279             : 
    4280             : /*
    4281             :  * Get default permissions for newly created object within given schema
    4282             :  *
    4283             :  * Returns NULL if built-in system defaults should be used.
    4284             :  *
    4285             :  * If the result is not NULL, caller must call recordDependencyOnNewAcl
    4286             :  * once the OID of the new object is known.
    4287             :  */
    4288             : Acl *
    4289       70224 : get_user_default_acl(ObjectType objtype, Oid ownerId, Oid nsp_oid)
    4290             : {
    4291             :     Acl        *result;
    4292             :     Acl        *glob_acl;
    4293             :     Acl        *schema_acl;
    4294             :     Acl        *def_acl;
    4295             :     char        defaclobjtype;
    4296             : 
    4297             :     /*
    4298             :      * Use NULL during bootstrap, since pg_default_acl probably isn't there
    4299             :      * yet.
    4300             :      */
    4301       70224 :     if (IsBootstrapProcessingMode())
    4302           0 :         return NULL;
    4303             : 
    4304             :     /* Check if object type is supported in pg_default_acl */
    4305       70224 :     switch (objtype)
    4306             :     {
    4307       50410 :         case OBJECT_TABLE:
    4308       50410 :             defaclobjtype = DEFACLOBJ_RELATION;
    4309       50410 :             break;
    4310             : 
    4311        1684 :         case OBJECT_SEQUENCE:
    4312        1684 :             defaclobjtype = DEFACLOBJ_SEQUENCE;
    4313        1684 :             break;
    4314             : 
    4315       14610 :         case OBJECT_FUNCTION:
    4316       14610 :             defaclobjtype = DEFACLOBJ_FUNCTION;
    4317       14610 :             break;
    4318             : 
    4319        2596 :         case OBJECT_TYPE:
    4320        2596 :             defaclobjtype = DEFACLOBJ_TYPE;
    4321        2596 :             break;
    4322             : 
    4323         924 :         case OBJECT_SCHEMA:
    4324         924 :             defaclobjtype = DEFACLOBJ_NAMESPACE;
    4325         924 :             break;
    4326             : 
    4327           0 :         default:
    4328           0 :             return NULL;
    4329             :     }
    4330             : 
    4331             :     /* Look up the relevant pg_default_acl entries */
    4332       70224 :     glob_acl = get_default_acl_internal(ownerId, InvalidOid, defaclobjtype);
    4333       70224 :     schema_acl = get_default_acl_internal(ownerId, nsp_oid, defaclobjtype);
    4334             : 
    4335             :     /* Quick out if neither entry exists */
    4336       70224 :     if (glob_acl == NULL && schema_acl == NULL)
    4337       70032 :         return NULL;
    4338             : 
    4339             :     /* We need to know the hard-wired default value, too */
    4340         192 :     def_acl = acldefault(objtype, ownerId);
    4341             : 
    4342             :     /* If there's no global entry, substitute the hard-wired default */
    4343         192 :     if (glob_acl == NULL)
    4344          18 :         glob_acl = def_acl;
    4345             : 
    4346             :     /* Merge in any per-schema privileges */
    4347         192 :     result = aclmerge(glob_acl, schema_acl, ownerId);
    4348             : 
    4349             :     /*
    4350             :      * For efficiency, we want to return NULL if the result equals default.
    4351             :      * This requires sorting both arrays to get an accurate comparison.
    4352             :      */
    4353         192 :     aclitemsort(result);
    4354         192 :     aclitemsort(def_acl);
    4355         192 :     if (aclequal(result, def_acl))
    4356          24 :         result = NULL;
    4357             : 
    4358         192 :     return result;
    4359             : }
    4360             : 
    4361             : /*
    4362             :  * Record dependencies on roles mentioned in a new object's ACL.
    4363             :  */
    4364             : void
    4365       72922 : recordDependencyOnNewAcl(Oid classId, Oid objectId, int32 objsubId,
    4366             :                          Oid ownerId, Acl *acl)
    4367             : {
    4368             :     int         nmembers;
    4369             :     Oid        *members;
    4370             : 
    4371             :     /* Nothing to do if ACL is defaulted */
    4372       72922 :     if (acl == NULL)
    4373       72754 :         return;
    4374             : 
    4375             :     /* Extract roles mentioned in ACL */
    4376         168 :     nmembers = aclmembers(acl, &members);
    4377             : 
    4378             :     /* Update the shared dependency ACL info */
    4379         168 :     updateAclDependencies(classId, objectId, objsubId,
    4380             :                           ownerId,
    4381             :                           0, NULL,
    4382             :                           nmembers, members);
    4383             : }
    4384             : 
    4385             : /*
    4386             :  * Record initial privileges for the top-level object passed in.
    4387             :  *
    4388             :  * For the object passed in, this will record its ACL (if any) and the ACLs of
    4389             :  * any sub-objects (eg: columns) into pg_init_privs.
    4390             :  */
    4391             : void
    4392          96 : recordExtObjInitPriv(Oid objoid, Oid classoid)
    4393             : {
    4394             :     /*
    4395             :      * pg_class / pg_attribute
    4396             :      *
    4397             :      * If this is a relation then we need to see if there are any sub-objects
    4398             :      * (eg: columns) for it and, if so, be sure to call
    4399             :      * recordExtensionInitPrivWorker() for each one.
    4400             :      */
    4401          96 :     if (classoid == RelationRelationId)
    4402             :     {
    4403             :         Form_pg_class pg_class_tuple;
    4404             :         Datum       aclDatum;
    4405             :         bool        isNull;
    4406             :         HeapTuple   tuple;
    4407             : 
    4408          16 :         tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(objoid));
    4409          16 :         if (!HeapTupleIsValid(tuple))
    4410           0 :             elog(ERROR, "cache lookup failed for relation %u", objoid);
    4411          16 :         pg_class_tuple = (Form_pg_class) GETSTRUCT(tuple);
    4412             : 
    4413             :         /*
    4414             :          * Indexes don't have permissions, neither do the pg_class rows for
    4415             :          * composite types.  (These cases are unreachable given the
    4416             :          * restrictions in ALTER EXTENSION ADD, but let's check anyway.)
    4417             :          */
    4418          16 :         if (pg_class_tuple->relkind == RELKIND_INDEX ||
    4419          16 :             pg_class_tuple->relkind == RELKIND_PARTITIONED_INDEX ||
    4420          16 :             pg_class_tuple->relkind == RELKIND_COMPOSITE_TYPE)
    4421             :         {
    4422           0 :             ReleaseSysCache(tuple);
    4423           0 :             return;
    4424             :         }
    4425             : 
    4426             :         /*
    4427             :          * If this isn't a sequence then it's possibly going to have
    4428             :          * column-level ACLs associated with it.
    4429             :          */
    4430          16 :         if (pg_class_tuple->relkind != RELKIND_SEQUENCE)
    4431             :         {
    4432             :             AttrNumber  curr_att;
    4433          14 :             AttrNumber  nattrs = pg_class_tuple->relnatts;
    4434             : 
    4435          38 :             for (curr_att = 1; curr_att <= nattrs; curr_att++)
    4436             :             {
    4437             :                 HeapTuple   attTuple;
    4438             :                 Datum       attaclDatum;
    4439             : 
    4440          24 :                 attTuple = SearchSysCache2(ATTNUM,
    4441             :                                            ObjectIdGetDatum(objoid),
    4442             :                                            Int16GetDatum(curr_att));
    4443             : 
    4444          24 :                 if (!HeapTupleIsValid(attTuple))
    4445           0 :                     continue;
    4446             : 
    4447             :                 /* ignore dropped columns */
    4448          24 :                 if (((Form_pg_attribute) GETSTRUCT(attTuple))->attisdropped)
    4449             :                 {
    4450           2 :                     ReleaseSysCache(attTuple);
    4451           2 :                     continue;
    4452             :                 }
    4453             : 
    4454          22 :                 attaclDatum = SysCacheGetAttr(ATTNUM, attTuple,
    4455             :                                               Anum_pg_attribute_attacl,
    4456             :                                               &isNull);
    4457             : 
    4458             :                 /* no need to do anything for a NULL ACL */
    4459          22 :                 if (isNull)
    4460             :                 {
    4461          18 :                     ReleaseSysCache(attTuple);
    4462          18 :                     continue;
    4463             :                 }
    4464             : 
    4465           4 :                 recordExtensionInitPrivWorker(objoid, classoid, curr_att,
    4466           4 :                                               DatumGetAclP(attaclDatum));
    4467             : 
    4468           4 :                 ReleaseSysCache(attTuple);
    4469             :             }
    4470             :         }
    4471             : 
    4472          16 :         aclDatum = SysCacheGetAttr(RELOID, tuple, Anum_pg_class_relacl,
    4473             :                                    &isNull);
    4474             : 
    4475             :         /* Add the record, if any, for the top-level object */
    4476          16 :         if (!isNull)
    4477           8 :             recordExtensionInitPrivWorker(objoid, classoid, 0,
    4478           8 :                                           DatumGetAclP(aclDatum));
    4479             : 
    4480          16 :         ReleaseSysCache(tuple);
    4481             :     }
    4482          80 :     else if (classoid == LargeObjectRelationId)
    4483             :     {
    4484             :         /* For large objects, we must consult pg_largeobject_metadata */
    4485             :         Datum       aclDatum;
    4486             :         bool        isNull;
    4487             :         HeapTuple   tuple;
    4488             :         ScanKeyData entry[1];
    4489             :         SysScanDesc scan;
    4490             :         Relation    relation;
    4491             : 
    4492             :         /*
    4493             :          * Note: this is dead code, given that we don't allow large objects to
    4494             :          * be made extension members.  But it seems worth carrying in case
    4495             :          * some future caller of this function has need for it.
    4496             :          */
    4497           0 :         relation = table_open(LargeObjectMetadataRelationId, RowExclusiveLock);
    4498             : 
    4499             :         /* There's no syscache for pg_largeobject_metadata */
    4500           0 :         ScanKeyInit(&entry[0],
    4501             :                     Anum_pg_largeobject_metadata_oid,
    4502             :                     BTEqualStrategyNumber, F_OIDEQ,
    4503             :                     ObjectIdGetDatum(objoid));
    4504             : 
    4505           0 :         scan = systable_beginscan(relation,
    4506             :                                   LargeObjectMetadataOidIndexId, true,
    4507             :                                   NULL, 1, entry);
    4508             : 
    4509           0 :         tuple = systable_getnext(scan);
    4510           0 :         if (!HeapTupleIsValid(tuple))
    4511           0 :             elog(ERROR, "could not find tuple for large object %u", objoid);
    4512             : 
    4513           0 :         aclDatum = heap_getattr(tuple,
    4514             :                                 Anum_pg_largeobject_metadata_lomacl,
    4515             :                                 RelationGetDescr(relation), &isNull);
    4516             : 
    4517             :         /* Add the record, if any, for the top-level object */
    4518           0 :         if (!isNull)
    4519           0 :             recordExtensionInitPrivWorker(objoid, classoid, 0,
    4520           0 :                                           DatumGetAclP(aclDatum));
    4521             : 
    4522           0 :         systable_endscan(scan);
    4523             :     }
    4524             :     /* This will error on unsupported classoid. */
    4525          80 :     else if (get_object_attnum_acl(classoid) != InvalidAttrNumber)
    4526             :     {
    4527             :         Datum       aclDatum;
    4528             :         bool        isNull;
    4529             :         HeapTuple   tuple;
    4530             : 
    4531          58 :         tuple = SearchSysCache1(get_object_catcache_oid(classoid),
    4532             :                                 ObjectIdGetDatum(objoid));
    4533          58 :         if (!HeapTupleIsValid(tuple))
    4534           0 :             elog(ERROR, "cache lookup failed for %s %u",
    4535             :                  get_object_class_descr(classoid), objoid);
    4536             : 
    4537          58 :         aclDatum = SysCacheGetAttr(get_object_catcache_oid(classoid), tuple,
    4538          58 :                                    get_object_attnum_acl(classoid),
    4539             :                                    &isNull);
    4540             : 
    4541             :         /* Add the record, if any, for the top-level object */
    4542          58 :         if (!isNull)
    4543          10 :             recordExtensionInitPrivWorker(objoid, classoid, 0,
    4544          10 :                                           DatumGetAclP(aclDatum));
    4545             : 
    4546          58 :         ReleaseSysCache(tuple);
    4547             :     }
    4548             : }
    4549             : 
    4550             : /*
    4551             :  * For the object passed in, remove its ACL and the ACLs of any object subIds
    4552             :  * from pg_init_privs (via recordExtensionInitPrivWorker()).
    4553             :  */
    4554             : void
    4555         226 : removeExtObjInitPriv(Oid objoid, Oid classoid)
    4556             : {
    4557             :     /*
    4558             :      * If this is a relation then we need to see if there are any sub-objects
    4559             :      * (eg: columns) for it and, if so, be sure to call
    4560             :      * recordExtensionInitPrivWorker() for each one.
    4561             :      */
    4562         226 :     if (classoid == RelationRelationId)
    4563             :     {
    4564             :         Form_pg_class pg_class_tuple;
    4565             :         HeapTuple   tuple;
    4566             : 
    4567          40 :         tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(objoid));
    4568          40 :         if (!HeapTupleIsValid(tuple))
    4569           0 :             elog(ERROR, "cache lookup failed for relation %u", objoid);
    4570          40 :         pg_class_tuple = (Form_pg_class) GETSTRUCT(tuple);
    4571             : 
    4572             :         /*
    4573             :          * Indexes don't have permissions, neither do the pg_class rows for
    4574             :          * composite types.  (These cases are unreachable given the
    4575             :          * restrictions in ALTER EXTENSION DROP, but let's check anyway.)
    4576             :          */
    4577          40 :         if (pg_class_tuple->relkind == RELKIND_INDEX ||
    4578          40 :             pg_class_tuple->relkind == RELKIND_PARTITIONED_INDEX ||
    4579          40 :             pg_class_tuple->relkind == RELKIND_COMPOSITE_TYPE)
    4580             :         {
    4581           0 :             ReleaseSysCache(tuple);
    4582           0 :             return;
    4583             :         }
    4584             : 
    4585             :         /*
    4586             :          * If this isn't a sequence then it's possibly going to have
    4587             :          * column-level ACLs associated with it.
    4588             :          */
    4589          40 :         if (pg_class_tuple->relkind != RELKIND_SEQUENCE)
    4590             :         {
    4591             :             AttrNumber  curr_att;
    4592          40 :             AttrNumber  nattrs = pg_class_tuple->relnatts;
    4593             : 
    4594         938 :             for (curr_att = 1; curr_att <= nattrs; curr_att++)
    4595             :             {
    4596             :                 HeapTuple   attTuple;
    4597             : 
    4598         898 :                 attTuple = SearchSysCache2(ATTNUM,
    4599             :                                            ObjectIdGetDatum(objoid),
    4600             :                                            Int16GetDatum(curr_att));
    4601             : 
    4602         898 :                 if (!HeapTupleIsValid(attTuple))
    4603           0 :                     continue;
    4604             : 
    4605             :                 /* when removing, remove all entries, even dropped columns */
    4606             : 
    4607         898 :                 recordExtensionInitPrivWorker(objoid, classoid, curr_att, NULL);
    4608             : 
    4609         898 :                 ReleaseSysCache(attTuple);
    4610             :             }
    4611             :         }
    4612             : 
    4613          40 :         ReleaseSysCache(tuple);
    4614             :     }
    4615             : 
    4616             :     /* Remove the record, if any, for the top-level object */
    4617         226 :     recordExtensionInitPrivWorker(objoid, classoid, 0, NULL);
    4618             : }
    4619             : 
    4620             : /*
    4621             :  * Record initial ACL for an extension object
    4622             :  *
    4623             :  * Can be called at any time, we check if 'creating_extension' is set and, if
    4624             :  * not, exit immediately.
    4625             :  *
    4626             :  * Pass in the object OID, the OID of the class (the OID of the table which
    4627             :  * the object is defined in) and the 'sub' id of the object (objsubid), if
    4628             :  * any.  If there is no 'sub' id (they are currently only used for columns of
    4629             :  * tables) then pass in '0'.  Finally, pass in the complete ACL to store.
    4630             :  *
    4631             :  * If an ACL already exists for this object/sub-object then we will replace
    4632             :  * it with what is passed in.
    4633             :  *
    4634             :  * Passing in NULL for 'new_acl' will result in the entry for the object being
    4635             :  * removed, if one is found.
    4636             :  */
    4637             : static void
    4638       17320 : recordExtensionInitPriv(Oid objoid, Oid classoid, int objsubid, Acl *new_acl)
    4639             : {
    4640             :     /*
    4641             :      * Generally, we only record the initial privileges when an extension is
    4642             :      * being created, but because we don't actually use CREATE EXTENSION
    4643             :      * during binary upgrades with pg_upgrade, there is a variable to let us
    4644             :      * know that the GRANT and REVOKE statements being issued, while this
    4645             :      * variable is true, are for the initial privileges of the extension
    4646             :      * object and therefore we need to record them.
    4647             :      */
    4648       17320 :     if (!creating_extension && !binary_upgrade_record_init_privs)
    4649       16694 :         return;
    4650             : 
    4651         626 :     recordExtensionInitPrivWorker(objoid, classoid, objsubid, new_acl);
    4652             : }
    4653             : 
    4654             : /*
    4655             :  * Record initial ACL for an extension object, worker.
    4656             :  *
    4657             :  * This will perform a wholesale replacement of the entire ACL for the object
    4658             :  * passed in, therefore be sure to pass in the complete new ACL to use.
    4659             :  *
    4660             :  * Generally speaking, do *not* use this function directly but instead use
    4661             :  * recordExtensionInitPriv(), which checks if 'creating_extension' is set.
    4662             :  * This function does *not* check if 'creating_extension' is set as it is also
    4663             :  * used when an object is added to or removed from an extension via ALTER
    4664             :  * EXTENSION ... ADD/DROP.
    4665             :  */
    4666             : static void
    4667        1772 : recordExtensionInitPrivWorker(Oid objoid, Oid classoid, int objsubid, Acl *new_acl)
    4668             : {
    4669             :     Relation    relation;
    4670             :     ScanKeyData key[3];
    4671             :     SysScanDesc scan;
    4672             :     HeapTuple   tuple;
    4673             :     HeapTuple   oldtuple;
    4674             : 
    4675        1772 :     relation = table_open(InitPrivsRelationId, RowExclusiveLock);
    4676             : 
    4677        1772 :     ScanKeyInit(&key[0],
    4678             :                 Anum_pg_init_privs_objoid,
    4679             :                 BTEqualStrategyNumber, F_OIDEQ,
    4680             :                 ObjectIdGetDatum(objoid));
    4681        1772 :     ScanKeyInit(&key[1],
    4682             :                 Anum_pg_init_privs_classoid,
    4683             :                 BTEqualStrategyNumber, F_OIDEQ,
    4684             :                 ObjectIdGetDatum(classoid));
    4685        1772 :     ScanKeyInit(&key[2],
    4686             :                 Anum_pg_init_privs_objsubid,
    4687             :                 BTEqualStrategyNumber, F_INT4EQ,
    4688             :                 Int32GetDatum(objsubid));
    4689             : 
    4690        1772 :     scan = systable_beginscan(relation, InitPrivsObjIndexId, true,
    4691             :                               NULL, 3, key);
    4692             : 
    4693             :     /* There should exist only one entry or none. */
    4694        1772 :     oldtuple = systable_getnext(scan);
    4695             : 
    4696             :     /* If we find an entry, update it with the latest ACL. */
    4697        1772 :     if (HeapTupleIsValid(oldtuple))
    4698             :     {
    4699         208 :         Datum       values[Natts_pg_init_privs] = {0};
    4700         208 :         bool        nulls[Natts_pg_init_privs] = {0};
    4701         208 :         bool        replace[Natts_pg_init_privs] = {0};
    4702             : 
    4703             :         /* If we have a new ACL to set, then update the row with it. */
    4704         208 :         if (new_acl)
    4705             :         {
    4706         144 :             values[Anum_pg_init_privs_initprivs - 1] = PointerGetDatum(new_acl);
    4707         144 :             replace[Anum_pg_init_privs_initprivs - 1] = true;
    4708             : 
    4709         144 :             oldtuple = heap_modify_tuple(oldtuple, RelationGetDescr(relation),
    4710             :                                          values, nulls, replace);
    4711             : 
    4712         144 :             CatalogTupleUpdate(relation, &oldtuple->t_self, oldtuple);
    4713             :         }
    4714             :         else
    4715             :         {
    4716             :             /* new_acl is NULL, so delete the entry we found. */
    4717          64 :             CatalogTupleDelete(relation, &oldtuple->t_self);
    4718             :         }
    4719             :     }
    4720             :     else
    4721             :     {
    4722        1564 :         Datum       values[Natts_pg_init_privs] = {0};
    4723        1564 :         bool        nulls[Natts_pg_init_privs] = {0};
    4724             : 
    4725             :         /*
    4726             :          * Only add a new entry if the new ACL is non-NULL.
    4727             :          *
    4728             :          * If we are passed in a NULL ACL and no entry exists, we can just
    4729             :          * fall through and do nothing.
    4730             :          */
    4731        1564 :         if (new_acl)
    4732             :         {
    4733             :             /* No entry found, so add it. */
    4734         500 :             values[Anum_pg_init_privs_objoid - 1] = ObjectIdGetDatum(objoid);
    4735         500 :             values[Anum_pg_init_privs_classoid - 1] = ObjectIdGetDatum(classoid);
    4736         500 :             values[Anum_pg_init_privs_objsubid - 1] = Int32GetDatum(objsubid);
    4737             : 
    4738             :             /* This function only handles initial privileges of extensions */
    4739         500 :             values[Anum_pg_init_privs_privtype - 1] =
    4740         500 :                 CharGetDatum(INITPRIVS_EXTENSION);
    4741             : 
    4742         500 :             values[Anum_pg_init_privs_initprivs - 1] = PointerGetDatum(new_acl);
    4743             : 
    4744         500 :             tuple = heap_form_tuple(RelationGetDescr(relation), values, nulls);
    4745             : 
    4746         500 :             CatalogTupleInsert(relation, tuple);
    4747             :         }
    4748             :     }
    4749             : 
    4750        1772 :     systable_endscan(scan);
    4751             : 
    4752             :     /* prevent error when processing objects multiple times */
    4753        1772 :     CommandCounterIncrement();
    4754             : 
    4755        1772 :     table_close(relation, RowExclusiveLock);
    4756        1772 : }

Generated by: LCOV version 1.14