PostgreSQL Source Code  git master
aclchk.c File Reference
#include "postgres.h"
#include "access/genam.h"
#include "access/heapam.h"
#include "access/htup_details.h"
#include "access/sysattr.h"
#include "access/tableam.h"
#include "access/xact.h"
#include "catalog/binary_upgrade.h"
#include "catalog/catalog.h"
#include "catalog/dependency.h"
#include "catalog/indexing.h"
#include "catalog/objectaccess.h"
#include "catalog/pg_aggregate.h"
#include "catalog/pg_am.h"
#include "catalog/pg_authid.h"
#include "catalog/pg_cast.h"
#include "catalog/pg_collation.h"
#include "catalog/pg_conversion.h"
#include "catalog/pg_database.h"
#include "catalog/pg_default_acl.h"
#include "catalog/pg_event_trigger.h"
#include "catalog/pg_extension.h"
#include "catalog/pg_foreign_data_wrapper.h"
#include "catalog/pg_foreign_server.h"
#include "catalog/pg_init_privs.h"
#include "catalog/pg_language.h"
#include "catalog/pg_largeobject.h"
#include "catalog/pg_largeobject_metadata.h"
#include "catalog/pg_namespace.h"
#include "catalog/pg_opclass.h"
#include "catalog/pg_operator.h"
#include "catalog/pg_opfamily.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_statistic_ext.h"
#include "catalog/pg_subscription.h"
#include "catalog/pg_tablespace.h"
#include "catalog/pg_transform.h"
#include "catalog/pg_ts_config.h"
#include "catalog/pg_ts_dict.h"
#include "catalog/pg_ts_parser.h"
#include "catalog/pg_ts_template.h"
#include "catalog/pg_type.h"
#include "commands/dbcommands.h"
#include "commands/event_trigger.h"
#include "commands/extension.h"
#include "commands/proclang.h"
#include "commands/tablespace.h"
#include "foreign/foreign.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "parser/parse_func.h"
#include "parser/parse_type.h"
#include "utils/acl.h"
#include "utils/aclchk_internal.h"
#include "utils/builtins.h"
#include "utils/fmgroids.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
#include "utils/syscache.h"
Include dependency graph for aclchk.c:

Go to the source code of this file.

Data Structures

struct  InternalDefaultACL
 

Functions

static void ExecGrantStmt_oids (InternalGrant *istmt)
 
static void ExecGrant_Relation (InternalGrant *grantStmt)
 
static void ExecGrant_Database (InternalGrant *grantStmt)
 
static void ExecGrant_Fdw (InternalGrant *grantStmt)
 
static void ExecGrant_ForeignServer (InternalGrant *grantStmt)
 
static void ExecGrant_Function (InternalGrant *grantStmt)
 
static void ExecGrant_Language (InternalGrant *grantStmt)
 
static void ExecGrant_Largeobject (InternalGrant *grantStmt)
 
static void ExecGrant_Namespace (InternalGrant *grantStmt)
 
static void ExecGrant_Tablespace (InternalGrant *grantStmt)
 
static void ExecGrant_Type (InternalGrant *grantStmt)
 
static void SetDefaultACLsInSchemas (InternalDefaultACL *iacls, List *nspnames)
 
static void SetDefaultACL (InternalDefaultACL *iacls)
 
static ListobjectNamesToOids (ObjectType objtype, List *objnames)
 
static ListobjectsInSchemaToOids (ObjectType objtype, List *nspnames)
 
static ListgetRelationsInNamespace (Oid namespaceId, char relkind)
 
static void expand_col_privileges (List *colnames, Oid table_oid, AclMode this_privileges, AclMode *col_privileges, int num_col_privileges)
 
static void expand_all_col_privileges (Oid table_oid, Form_pg_class classForm, AclMode this_privileges, AclMode *col_privileges, int num_col_privileges)
 
static AclMode string_to_privilege (const char *privname)
 
static const char * privilege_to_string (AclMode privilege)
 
static AclMode restrict_and_check_grant (bool is_grant, AclMode avail_goptions, bool all_privs, AclMode privileges, Oid objectId, Oid grantorId, ObjectType objtype, const char *objname, AttrNumber att_number, const char *colname)
 
static AclMode pg_aclmask (ObjectType objtype, Oid table_oid, AttrNumber attnum, Oid roleid, AclMode mask, AclMaskHow how)
 
static void recordExtensionInitPriv (Oid objoid, Oid classoid, int objsubid, Acl *new_acl)
 
static void recordExtensionInitPrivWorker (Oid objoid, Oid classoid, int objsubid, Acl *new_acl)
 
static Aclmerge_acl_with_grant (Acl *old_acl, bool is_grant, bool grant_option, DropBehavior behavior, List *grantees, AclMode privileges, Oid grantorId, Oid ownerId)
 
void ExecuteGrantStmt (GrantStmt *stmt)
 
void ExecAlterDefaultPrivilegesStmt (ParseState *pstate, AlterDefaultPrivilegesStmt *stmt)
 
void RemoveRoleFromObjectACL (Oid roleid, Oid classid, Oid objid)
 
static void ExecGrant_Attribute (InternalGrant *istmt, Oid relOid, const char *relname, AttrNumber attnum, Oid ownerId, AclMode col_privileges, Relation attRelation, const Acl *old_rel_acl)
 
void aclcheck_error (AclResult aclerr, ObjectType objtype, const char *objectname)
 
void aclcheck_error_col (AclResult aclerr, ObjectType objtype, const char *objectname, const char *colname)
 
void aclcheck_error_type (AclResult aclerr, Oid typeOid)
 
AclMode pg_attribute_aclmask (Oid table_oid, AttrNumber attnum, Oid roleid, AclMode mask, AclMaskHow how)
 
AclMode pg_attribute_aclmask_ext (Oid table_oid, AttrNumber attnum, Oid roleid, AclMode mask, AclMaskHow how, bool *is_missing)
 
AclMode pg_class_aclmask (Oid table_oid, Oid roleid, AclMode mask, AclMaskHow how)
 
AclMode pg_class_aclmask_ext (Oid table_oid, Oid roleid, AclMode mask, AclMaskHow how, bool *is_missing)
 
AclMode pg_database_aclmask (Oid db_oid, Oid roleid, AclMode mask, AclMaskHow how)
 
AclMode pg_proc_aclmask (Oid proc_oid, Oid roleid, AclMode mask, AclMaskHow how)
 
AclMode pg_language_aclmask (Oid lang_oid, Oid roleid, AclMode mask, AclMaskHow how)
 
AclMode pg_largeobject_aclmask_snapshot (Oid lobj_oid, Oid roleid, AclMode mask, AclMaskHow how, Snapshot snapshot)
 
AclMode pg_namespace_aclmask (Oid nsp_oid, Oid roleid, AclMode mask, AclMaskHow how)
 
AclMode pg_tablespace_aclmask (Oid spc_oid, Oid roleid, AclMode mask, AclMaskHow how)
 
AclMode pg_foreign_data_wrapper_aclmask (Oid fdw_oid, Oid roleid, AclMode mask, AclMaskHow how)
 
AclMode pg_foreign_server_aclmask (Oid srv_oid, Oid roleid, AclMode mask, AclMaskHow how)
 
AclMode pg_type_aclmask (Oid type_oid, Oid roleid, AclMode mask, AclMaskHow how)
 
AclResult pg_attribute_aclcheck (Oid table_oid, AttrNumber attnum, Oid roleid, AclMode mode)
 
AclResult pg_attribute_aclcheck_ext (Oid table_oid, AttrNumber attnum, Oid roleid, AclMode mode, bool *is_missing)
 
AclResult pg_attribute_aclcheck_all (Oid table_oid, Oid roleid, AclMode mode, AclMaskHow how)
 
AclResult pg_class_aclcheck (Oid table_oid, Oid roleid, AclMode mode)
 
AclResult pg_class_aclcheck_ext (Oid table_oid, Oid roleid, AclMode mode, bool *is_missing)
 
AclResult pg_database_aclcheck (Oid db_oid, Oid roleid, AclMode mode)
 
AclResult pg_proc_aclcheck (Oid proc_oid, Oid roleid, AclMode mode)
 
AclResult pg_language_aclcheck (Oid lang_oid, Oid roleid, AclMode mode)
 
AclResult pg_largeobject_aclcheck_snapshot (Oid lobj_oid, Oid roleid, AclMode mode, Snapshot snapshot)
 
AclResult pg_namespace_aclcheck (Oid nsp_oid, Oid roleid, AclMode mode)
 
AclResult pg_tablespace_aclcheck (Oid spc_oid, Oid roleid, AclMode mode)
 
AclResult pg_foreign_data_wrapper_aclcheck (Oid fdw_oid, Oid roleid, AclMode mode)
 
AclResult pg_foreign_server_aclcheck (Oid srv_oid, Oid roleid, AclMode mode)
 
AclResult pg_type_aclcheck (Oid type_oid, Oid roleid, AclMode mode)
 
bool pg_class_ownercheck (Oid class_oid, Oid roleid)
 
bool pg_type_ownercheck (Oid type_oid, Oid roleid)
 
bool pg_oper_ownercheck (Oid oper_oid, Oid roleid)
 
bool pg_proc_ownercheck (Oid proc_oid, Oid roleid)
 
bool pg_language_ownercheck (Oid lan_oid, Oid roleid)
 
bool pg_largeobject_ownercheck (Oid lobj_oid, Oid roleid)
 
bool pg_namespace_ownercheck (Oid nsp_oid, Oid roleid)
 
bool pg_tablespace_ownercheck (Oid spc_oid, Oid roleid)
 
bool pg_opclass_ownercheck (Oid opc_oid, Oid roleid)
 
bool pg_opfamily_ownercheck (Oid opf_oid, Oid roleid)
 
bool pg_ts_dict_ownercheck (Oid dict_oid, Oid roleid)
 
bool pg_ts_config_ownercheck (Oid cfg_oid, Oid roleid)
 
bool pg_foreign_data_wrapper_ownercheck (Oid srv_oid, Oid roleid)
 
bool pg_foreign_server_ownercheck (Oid srv_oid, Oid roleid)
 
bool pg_event_trigger_ownercheck (Oid et_oid, Oid roleid)
 
bool pg_database_ownercheck (Oid db_oid, Oid roleid)
 
bool pg_collation_ownercheck (Oid coll_oid, Oid roleid)
 
bool pg_conversion_ownercheck (Oid conv_oid, Oid roleid)
 
bool pg_extension_ownercheck (Oid ext_oid, Oid roleid)
 
bool pg_publication_ownercheck (Oid pub_oid, Oid roleid)
 
bool pg_subscription_ownercheck (Oid sub_oid, Oid roleid)
 
bool pg_statistics_object_ownercheck (Oid stat_oid, Oid roleid)
 
bool has_createrole_privilege (Oid roleid)
 
bool has_bypassrls_privilege (Oid roleid)
 
static Aclget_default_acl_internal (Oid roleId, Oid nsp_oid, char objtype)
 
Aclget_user_default_acl (ObjectType objtype, Oid ownerId, Oid nsp_oid)
 
void recordDependencyOnNewAcl (Oid classId, Oid objectId, int32 objsubId, Oid ownerId, Acl *acl)
 
void recordExtObjInitPriv (Oid objoid, Oid classoid)
 
void removeExtObjInitPriv (Oid objoid, Oid classoid)
 

Variables

bool binary_upgrade_record_init_privs = false
 

Function Documentation

◆ aclcheck_error()

void aclcheck_error ( AclResult  aclerr,
ObjectType  objtype,
const char *  objectname 
)

Definition at line 3313 of file aclchk.c.

References ACLCHECK_NO_PRIV, ACLCHECK_NOT_OWNER, ACLCHECK_OK, elog, ereport, errcode(), errmsg(), ERROR, gettext_noop, OBJECT_ACCESS_METHOD, OBJECT_AGGREGATE, OBJECT_AMOP, OBJECT_AMPROC, OBJECT_ATTRIBUTE, OBJECT_CAST, OBJECT_COLLATION, OBJECT_COLUMN, OBJECT_CONVERSION, OBJECT_DATABASE, OBJECT_DEFACL, OBJECT_DEFAULT, OBJECT_DOMAIN, OBJECT_DOMCONSTRAINT, OBJECT_EVENT_TRIGGER, OBJECT_EXTENSION, OBJECT_FDW, OBJECT_FOREIGN_SERVER, OBJECT_FOREIGN_TABLE, OBJECT_FUNCTION, OBJECT_INDEX, OBJECT_LANGUAGE, OBJECT_LARGEOBJECT, OBJECT_MATVIEW, OBJECT_OPCLASS, OBJECT_OPERATOR, OBJECT_OPFAMILY, OBJECT_POLICY, OBJECT_PROCEDURE, OBJECT_PUBLICATION, OBJECT_PUBLICATION_REL, OBJECT_ROLE, OBJECT_ROUTINE, OBJECT_RULE, OBJECT_SCHEMA, OBJECT_SEQUENCE, OBJECT_STATISTIC_EXT, OBJECT_SUBSCRIPTION, OBJECT_TABCONSTRAINT, OBJECT_TABLE, OBJECT_TABLESPACE, OBJECT_TRANSFORM, OBJECT_TRIGGER, OBJECT_TSCONFIGURATION, OBJECT_TSDICTIONARY, OBJECT_TSPARSER, OBJECT_TSTEMPLATE, OBJECT_TYPE, OBJECT_USER_MAPPING, and OBJECT_VIEW.

Referenced by aclcheck_error_col(), aclcheck_error_type(), AlterDatabase(), AlterDatabaseOwner(), AlterDatabaseSet(), AlterEventTrigger(), AlterEventTriggerOwner_internal(), AlterExtensionNamespace(), AlterForeignServer(), AlterForeignServerOwner_internal(), AlterFunction(), AlterObjectNamespace_internal(), AlterObjectOwner_internal(), AlterObjectRename_internal(), AlterOperator(), AlterOpFamilyAdd(), AlterPublication(), AlterPublicationOwner_internal(), AlterRoleSet(), AlterSchemaOwner_internal(), AlterStatistics(), AlterSubscription(), AlterSubscriptionOwner_internal(), AlterTableMoveAll(), AlterTableSpaceOptions(), AlterTSConfiguration(), AlterTSDictionary(), AlterTypeOwner(), ATExecChangeOwner(), ATPrepSetTableSpace(), ATSimplePermissions(), brin_desummarize_range(), brin_summarize_range(), calculate_database_size(), calculate_tablespace_size(), call_pltcl_start_proc(), check_object_ownership(), check_temp_tablespaces(), checkFkeyPermissions(), CheckFunctionValidatorAccess(), compute_return_type(), CreateConversionCommand(), createdb(), CreateForeignServer(), CreateForeignTable(), CreateFunction(), CreateProceduralLanguage(), CreatePublication(), CreateSchemaCommand(), CreateStatistics(), CreateTransform(), CreateTrigger(), currtid_internal(), DefineAggregate(), DefineCollation(), DefineDomain(), DefineEnum(), DefineIndex(), DefineOpClass(), DefineOperator(), DefineOpFamily(), DefineQueryRewrite(), DefineRange(), DefineRelation(), DefineTSConfiguration(), DefineTSDictionary(), DefineType(), dropdb(), DropSubscription(), DropTableSpace(), EnableDisableRule(), ExecAlterExtensionContentsStmt(), ExecAlterExtensionStmt(), ExecBuildGroupingEqual(), ExecBuildParamSetEqual(), ExecCheckRTPerms(), ExecInitAgg(), ExecInitExprRec(), ExecInitFunc(), ExecInitWindowAgg(), ExecReindex(), ExecuteCallStmt(), ExecuteDoStmt(), ExecuteTruncateGuts(), findRangeCanonicalFunction(), findRangeSubtypeDiffFunction(), get_connect_string(), get_other_operator(), get_rel_from_relname(), gin_clean_pending_list(), HandleFunctionRequest(), ImportForeignSchema(), init_sexpr(), initialize_peragg(), LockViewRecurse_walker(), lookup_agg_function(), LookupCreationNamespace(), LookupExplicitNamespace(), MergeAttributes(), movedb(), OperatorCreate(), pg_prewarm(), pgrowlocks(), ProcedureCreate(), PublicationAddTables(), RangeVarCallbackForAlterRelation(), RangeVarCallbackForDropRelation(), RangeVarCallbackForLockTable(), RangeVarCallbackForPolicy(), RangeVarCallbackForReindexIndex(), RangeVarCallbackForRenameRule(), RangeVarCallbackForRenameTrigger(), RangeVarCallbackOwnsRelation(), RangeVarCallbackOwnsTable(), RangeVarGetAndCheckCreationNamespace(), ReindexMultipleInternal(), ReindexMultipleTables(), renameatt_check(), RenameDatabase(), RenameSchema(), RenameTableSpace(), restrict_and_check_grant(), sanity_check_relation(), transformTableLikeClause(), truncate_check_perms(), TypeCreate(), user_mapping_ddl_aclcheck(), ValidateJoinEstimator(), and ValidateRestrictionEstimator().

3315 {
3316  switch (aclerr)
3317  {
3318  case ACLCHECK_OK:
3319  /* no error, so return to caller */
3320  break;
3321  case ACLCHECK_NO_PRIV:
3322  {
3323  const char *msg = "???";
3324 
3325  switch (objtype)
3326  {
3327  case OBJECT_AGGREGATE:
3328  msg = gettext_noop("permission denied for aggregate %s");
3329  break;
3330  case OBJECT_COLLATION:
3331  msg = gettext_noop("permission denied for collation %s");
3332  break;
3333  case OBJECT_COLUMN:
3334  msg = gettext_noop("permission denied for column %s");
3335  break;
3336  case OBJECT_CONVERSION:
3337  msg = gettext_noop("permission denied for conversion %s");
3338  break;
3339  case OBJECT_DATABASE:
3340  msg = gettext_noop("permission denied for database %s");
3341  break;
3342  case OBJECT_DOMAIN:
3343  msg = gettext_noop("permission denied for domain %s");
3344  break;
3345  case OBJECT_EVENT_TRIGGER:
3346  msg = gettext_noop("permission denied for event trigger %s");
3347  break;
3348  case OBJECT_EXTENSION:
3349  msg = gettext_noop("permission denied for extension %s");
3350  break;
3351  case OBJECT_FDW:
3352  msg = gettext_noop("permission denied for foreign-data wrapper %s");
3353  break;
3354  case OBJECT_FOREIGN_SERVER:
3355  msg = gettext_noop("permission denied for foreign server %s");
3356  break;
3357  case OBJECT_FOREIGN_TABLE:
3358  msg = gettext_noop("permission denied for foreign table %s");
3359  break;
3360  case OBJECT_FUNCTION:
3361  msg = gettext_noop("permission denied for function %s");
3362  break;
3363  case OBJECT_INDEX:
3364  msg = gettext_noop("permission denied for index %s");
3365  break;
3366  case OBJECT_LANGUAGE:
3367  msg = gettext_noop("permission denied for language %s");
3368  break;
3369  case OBJECT_LARGEOBJECT:
3370  msg = gettext_noop("permission denied for large object %s");
3371  break;
3372  case OBJECT_MATVIEW:
3373  msg = gettext_noop("permission denied for materialized view %s");
3374  break;
3375  case OBJECT_OPCLASS:
3376  msg = gettext_noop("permission denied for operator class %s");
3377  break;
3378  case OBJECT_OPERATOR:
3379  msg = gettext_noop("permission denied for operator %s");
3380  break;
3381  case OBJECT_OPFAMILY:
3382  msg = gettext_noop("permission denied for operator family %s");
3383  break;
3384  case OBJECT_POLICY:
3385  msg = gettext_noop("permission denied for policy %s");
3386  break;
3387  case OBJECT_PROCEDURE:
3388  msg = gettext_noop("permission denied for procedure %s");
3389  break;
3390  case OBJECT_PUBLICATION:
3391  msg = gettext_noop("permission denied for publication %s");
3392  break;
3393  case OBJECT_ROUTINE:
3394  msg = gettext_noop("permission denied for routine %s");
3395  break;
3396  case OBJECT_SCHEMA:
3397  msg = gettext_noop("permission denied for schema %s");
3398  break;
3399  case OBJECT_SEQUENCE:
3400  msg = gettext_noop("permission denied for sequence %s");
3401  break;
3402  case OBJECT_STATISTIC_EXT:
3403  msg = gettext_noop("permission denied for statistics object %s");
3404  break;
3405  case OBJECT_SUBSCRIPTION:
3406  msg = gettext_noop("permission denied for subscription %s");
3407  break;
3408  case OBJECT_TABLE:
3409  msg = gettext_noop("permission denied for table %s");
3410  break;
3411  case OBJECT_TABLESPACE:
3412  msg = gettext_noop("permission denied for tablespace %s");
3413  break;
3415  msg = gettext_noop("permission denied for text search configuration %s");
3416  break;
3417  case OBJECT_TSDICTIONARY:
3418  msg = gettext_noop("permission denied for text search dictionary %s");
3419  break;
3420  case OBJECT_TYPE:
3421  msg = gettext_noop("permission denied for type %s");
3422  break;
3423  case OBJECT_VIEW:
3424  msg = gettext_noop("permission denied for view %s");
3425  break;
3426  /* these currently aren't used */
3427  case OBJECT_ACCESS_METHOD:
3428  case OBJECT_AMOP:
3429  case OBJECT_AMPROC:
3430  case OBJECT_ATTRIBUTE:
3431  case OBJECT_CAST:
3432  case OBJECT_DEFAULT:
3433  case OBJECT_DEFACL:
3434  case OBJECT_DOMCONSTRAINT:
3436  case OBJECT_ROLE:
3437  case OBJECT_RULE:
3438  case OBJECT_TABCONSTRAINT:
3439  case OBJECT_TRANSFORM:
3440  case OBJECT_TRIGGER:
3441  case OBJECT_TSPARSER:
3442  case OBJECT_TSTEMPLATE:
3443  case OBJECT_USER_MAPPING:
3444  elog(ERROR, "unsupported object type %d", objtype);
3445  }
3446 
3447  ereport(ERROR,
3448  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
3449  errmsg(msg, objectname)));
3450  break;
3451  }
3452  case ACLCHECK_NOT_OWNER:
3453  {
3454  const char *msg = "???";
3455 
3456  switch (objtype)
3457  {
3458  case OBJECT_AGGREGATE:
3459  msg = gettext_noop("must be owner of aggregate %s");
3460  break;
3461  case OBJECT_COLLATION:
3462  msg = gettext_noop("must be owner of collation %s");
3463  break;
3464  case OBJECT_CONVERSION:
3465  msg = gettext_noop("must be owner of conversion %s");
3466  break;
3467  case OBJECT_DATABASE:
3468  msg = gettext_noop("must be owner of database %s");
3469  break;
3470  case OBJECT_DOMAIN:
3471  msg = gettext_noop("must be owner of domain %s");
3472  break;
3473  case OBJECT_EVENT_TRIGGER:
3474  msg = gettext_noop("must be owner of event trigger %s");
3475  break;
3476  case OBJECT_EXTENSION:
3477  msg = gettext_noop("must be owner of extension %s");
3478  break;
3479  case OBJECT_FDW:
3480  msg = gettext_noop("must be owner of foreign-data wrapper %s");
3481  break;
3482  case OBJECT_FOREIGN_SERVER:
3483  msg = gettext_noop("must be owner of foreign server %s");
3484  break;
3485  case OBJECT_FOREIGN_TABLE:
3486  msg = gettext_noop("must be owner of foreign table %s");
3487  break;
3488  case OBJECT_FUNCTION:
3489  msg = gettext_noop("must be owner of function %s");
3490  break;
3491  case OBJECT_INDEX:
3492  msg = gettext_noop("must be owner of index %s");
3493  break;
3494  case OBJECT_LANGUAGE:
3495  msg = gettext_noop("must be owner of language %s");
3496  break;
3497  case OBJECT_LARGEOBJECT:
3498  msg = gettext_noop("must be owner of large object %s");
3499  break;
3500  case OBJECT_MATVIEW:
3501  msg = gettext_noop("must be owner of materialized view %s");
3502  break;
3503  case OBJECT_OPCLASS:
3504  msg = gettext_noop("must be owner of operator class %s");
3505  break;
3506  case OBJECT_OPERATOR:
3507  msg = gettext_noop("must be owner of operator %s");
3508  break;
3509  case OBJECT_OPFAMILY:
3510  msg = gettext_noop("must be owner of operator family %s");
3511  break;
3512  case OBJECT_PROCEDURE:
3513  msg = gettext_noop("must be owner of procedure %s");
3514  break;
3515  case OBJECT_PUBLICATION:
3516  msg = gettext_noop("must be owner of publication %s");
3517  break;
3518  case OBJECT_ROUTINE:
3519  msg = gettext_noop("must be owner of routine %s");
3520  break;
3521  case OBJECT_SEQUENCE:
3522  msg = gettext_noop("must be owner of sequence %s");
3523  break;
3524  case OBJECT_SUBSCRIPTION:
3525  msg = gettext_noop("must be owner of subscription %s");
3526  break;
3527  case OBJECT_TABLE:
3528  msg = gettext_noop("must be owner of table %s");
3529  break;
3530  case OBJECT_TYPE:
3531  msg = gettext_noop("must be owner of type %s");
3532  break;
3533  case OBJECT_VIEW:
3534  msg = gettext_noop("must be owner of view %s");
3535  break;
3536  case OBJECT_SCHEMA:
3537  msg = gettext_noop("must be owner of schema %s");
3538  break;
3539  case OBJECT_STATISTIC_EXT:
3540  msg = gettext_noop("must be owner of statistics object %s");
3541  break;
3542  case OBJECT_TABLESPACE:
3543  msg = gettext_noop("must be owner of tablespace %s");
3544  break;
3546  msg = gettext_noop("must be owner of text search configuration %s");
3547  break;
3548  case OBJECT_TSDICTIONARY:
3549  msg = gettext_noop("must be owner of text search dictionary %s");
3550  break;
3551 
3552  /*
3553  * Special cases: For these, the error message talks
3554  * about "relation", because that's where the
3555  * ownership is attached. See also
3556  * check_object_ownership().
3557  */
3558  case OBJECT_COLUMN:
3559  case OBJECT_POLICY:
3560  case OBJECT_RULE:
3561  case OBJECT_TABCONSTRAINT:
3562  case OBJECT_TRIGGER:
3563  msg = gettext_noop("must be owner of relation %s");
3564  break;
3565  /* these currently aren't used */
3566  case OBJECT_ACCESS_METHOD:
3567  case OBJECT_AMOP:
3568  case OBJECT_AMPROC:
3569  case OBJECT_ATTRIBUTE:
3570  case OBJECT_CAST:
3571  case OBJECT_DEFAULT:
3572  case OBJECT_DEFACL:
3573  case OBJECT_DOMCONSTRAINT:
3575  case OBJECT_ROLE:
3576  case OBJECT_TRANSFORM:
3577  case OBJECT_TSPARSER:
3578  case OBJECT_TSTEMPLATE:
3579  case OBJECT_USER_MAPPING:
3580  elog(ERROR, "unsupported object type %d", objtype);
3581  }
3582 
3583  ereport(ERROR,
3584  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
3585  errmsg(msg, objectname)));
3586  break;
3587  }
3588  default:
3589  elog(ERROR, "unrecognized AclResult: %d", (int) aclerr);
3590  break;
3591  }
3592 }
#define gettext_noop(x)
Definition: c.h:1197
int errcode(int sqlerrcode)
Definition: elog.c:698
#define ERROR
Definition: elog.h:46
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232

◆ aclcheck_error_col()

void aclcheck_error_col ( AclResult  aclerr,
ObjectType  objtype,
const char *  objectname,
const char *  colname 
)

Definition at line 3596 of file aclchk.c.

References aclcheck_error(), ACLCHECK_NO_PRIV, ACLCHECK_NOT_OWNER, ACLCHECK_OK, elog, ereport, errcode(), errmsg(), and ERROR.

Referenced by restrict_and_check_grant().

3598 {
3599  switch (aclerr)
3600  {
3601  case ACLCHECK_OK:
3602  /* no error, so return to caller */
3603  break;
3604  case ACLCHECK_NO_PRIV:
3605  ereport(ERROR,
3606  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
3607  errmsg("permission denied for column \"%s\" of relation \"%s\"",
3608  colname, objectname)));
3609  break;
3610  case ACLCHECK_NOT_OWNER:
3611  /* relation msg is OK since columns don't have separate owners */
3612  aclcheck_error(aclerr, objtype, objectname);
3613  break;
3614  default:
3615  elog(ERROR, "unrecognized AclResult: %d", (int) aclerr);
3616  break;
3617  }
3618 }
int errcode(int sqlerrcode)
Definition: elog.c:698
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3313
#define ERROR
Definition: elog.h:46
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232

◆ aclcheck_error_type()

void aclcheck_error_type ( AclResult  aclerr,
Oid  typeOid 
)

Definition at line 3626 of file aclchk.c.

References aclcheck_error(), format_type_be(), get_element_type(), and OBJECT_TYPE.

Referenced by AggregateCreate(), AlterType(), AlterTypeNamespace_oid(), AlterTypeOwner(), ATExecAddColumn(), ATPrepAlterColumnType(), BuildDescForRelation(), check_object_ownership(), checkDomainOwner(), checkEnumOwner(), compute_return_type(), CreateCast(), CreateTransform(), DefineDomain(), DefineOpClass(), DefineOperator(), DefineRelation(), interpret_function_parameter_list(), and RenameType().

3627 {
3628  Oid element_type = get_element_type(typeOid);
3629 
3630  aclcheck_error(aclerr, OBJECT_TYPE, format_type_be(element_type ? element_type : typeOid));
3631 }
Oid get_element_type(Oid typid)
Definition: lsyscache.c:2706
char * format_type_be(Oid type_oid)
Definition: format_type.c:339
unsigned int Oid
Definition: postgres_ext.h:31
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3313

◆ ExecAlterDefaultPrivilegesStmt()

void ExecAlterDefaultPrivilegesStmt ( ParseState pstate,
AlterDefaultPrivilegesStmt stmt 
)

Definition at line 904 of file aclchk.c.

References ACL_ALL_RIGHTS_FUNCTION, ACL_ALL_RIGHTS_RELATION, ACL_ALL_RIGHTS_SCHEMA, ACL_ALL_RIGHTS_SEQUENCE, ACL_ALL_RIGHTS_TYPE, ACL_ID_PUBLIC, ACL_NO_RIGHTS, generate_unaccent_rules::action, AlterDefaultPrivilegesStmt::action, InternalDefaultACL::all_privs, DefElem::arg, InternalDefaultACL::behavior, GrantStmt::behavior, check_is_member_of_role(), AccessPriv::cols, DefElem::defname, elog, ereport, errcode(), errmsg(), ERROR, get_rolespec_oid(), gettext_noop, GetUserId(), InternalDefaultACL::grant_option, GrantStmt::grant_option, InternalDefaultACL::grantees, GrantStmt::grantees, InternalDefaultACL::is_grant, GrantStmt::is_grant, lappend_oid(), lfirst, DefElem::location, NIL, OBJECT_FUNCTION, OBJECT_PROCEDURE, OBJECT_ROUTINE, OBJECT_SCHEMA, OBJECT_SEQUENCE, OBJECT_TABLE, OBJECT_TYPE, InternalDefaultACL::objtype, GrantStmt::objtype, AlterDefaultPrivilegesStmt::options, parser_errposition(), AccessPriv::priv_name, privilege_to_string(), InternalDefaultACL::privileges, GrantStmt::privileges, InternalDefaultACL::roleid, ROLESPEC_PUBLIC, RoleSpec::roletype, SetDefaultACLsInSchemas(), and string_to_privilege().

Referenced by ProcessUtilitySlow().

905 {
906  GrantStmt *action = stmt->action;
907  InternalDefaultACL iacls;
908  ListCell *cell;
909  List *rolespecs = NIL;
910  List *nspnames = NIL;
911  DefElem *drolespecs = NULL;
912  DefElem *dnspnames = NULL;
913  AclMode all_privileges;
914  const char *errormsg;
915 
916  /* Deconstruct the "options" part of the statement */
917  foreach(cell, stmt->options)
918  {
919  DefElem *defel = (DefElem *) lfirst(cell);
920 
921  if (strcmp(defel->defname, "schemas") == 0)
922  {
923  if (dnspnames)
924  ereport(ERROR,
925  (errcode(ERRCODE_SYNTAX_ERROR),
926  errmsg("conflicting or redundant options"),
927  parser_errposition(pstate, defel->location)));
928  dnspnames = defel;
929  }
930  else if (strcmp(defel->defname, "roles") == 0)
931  {
932  if (drolespecs)
933  ereport(ERROR,
934  (errcode(ERRCODE_SYNTAX_ERROR),
935  errmsg("conflicting or redundant options"),
936  parser_errposition(pstate, defel->location)));
937  drolespecs = defel;
938  }
939  else
940  elog(ERROR, "option \"%s\" not recognized", defel->defname);
941  }
942 
943  if (dnspnames)
944  nspnames = (List *) dnspnames->arg;
945  if (drolespecs)
946  rolespecs = (List *) drolespecs->arg;
947 
948  /* Prepare the InternalDefaultACL representation of the statement */
949  /* roleid to be filled below */
950  /* nspid to be filled in SetDefaultACLsInSchemas */
951  iacls.is_grant = action->is_grant;
952  iacls.objtype = action->objtype;
953  /* all_privs to be filled below */
954  /* privileges to be filled below */
955  iacls.grantees = NIL; /* filled below */
956  iacls.grant_option = action->grant_option;
957  iacls.behavior = action->behavior;
958 
959  /*
960  * Convert the RoleSpec list into an Oid list. Note that at this point we
961  * insert an ACL_ID_PUBLIC into the list if appropriate, so downstream
962  * there shouldn't be any additional work needed to support this case.
963  */
964  foreach(cell, action->grantees)
965  {
966  RoleSpec *grantee = (RoleSpec *) lfirst(cell);
967  Oid grantee_uid;
968 
969  switch (grantee->roletype)
970  {
971  case ROLESPEC_PUBLIC:
972  grantee_uid = ACL_ID_PUBLIC;
973  break;
974  default:
975  grantee_uid = get_rolespec_oid(grantee, false);
976  break;
977  }
978  iacls.grantees = lappend_oid(iacls.grantees, grantee_uid);
979  }
980 
981  /*
982  * Convert action->privileges, a list of privilege strings, into an
983  * AclMode bitmask.
984  */
985  switch (action->objtype)
986  {
987  case OBJECT_TABLE:
988  all_privileges = ACL_ALL_RIGHTS_RELATION;
989  errormsg = gettext_noop("invalid privilege type %s for relation");
990  break;
991  case OBJECT_SEQUENCE:
992  all_privileges = ACL_ALL_RIGHTS_SEQUENCE;
993  errormsg = gettext_noop("invalid privilege type %s for sequence");
994  break;
995  case OBJECT_FUNCTION:
996  all_privileges = ACL_ALL_RIGHTS_FUNCTION;
997  errormsg = gettext_noop("invalid privilege type %s for function");
998  break;
999  case OBJECT_PROCEDURE:
1000  all_privileges = ACL_ALL_RIGHTS_FUNCTION;
1001  errormsg = gettext_noop("invalid privilege type %s for procedure");
1002  break;
1003  case OBJECT_ROUTINE:
1004  all_privileges = ACL_ALL_RIGHTS_FUNCTION;
1005  errormsg = gettext_noop("invalid privilege type %s for routine");
1006  break;
1007  case OBJECT_TYPE:
1008  all_privileges = ACL_ALL_RIGHTS_TYPE;
1009  errormsg = gettext_noop("invalid privilege type %s for type");
1010  break;
1011  case OBJECT_SCHEMA:
1012  all_privileges = ACL_ALL_RIGHTS_SCHEMA;
1013  errormsg = gettext_noop("invalid privilege type %s for schema");
1014  break;
1015  default:
1016  elog(ERROR, "unrecognized GrantStmt.objtype: %d",
1017  (int) action->objtype);
1018  /* keep compiler quiet */
1019  all_privileges = ACL_NO_RIGHTS;
1020  errormsg = NULL;
1021  }
1022 
1023  if (action->privileges == NIL)
1024  {
1025  iacls.all_privs = true;
1026 
1027  /*
1028  * will be turned into ACL_ALL_RIGHTS_* by the internal routines
1029  * depending on the object type
1030  */
1031  iacls.privileges = ACL_NO_RIGHTS;
1032  }
1033  else
1034  {
1035  iacls.all_privs = false;
1036  iacls.privileges = ACL_NO_RIGHTS;
1037 
1038  foreach(cell, action->privileges)
1039  {
1040  AccessPriv *privnode = (AccessPriv *) lfirst(cell);
1041  AclMode priv;
1042 
1043  if (privnode->cols)
1044  ereport(ERROR,
1045  (errcode(ERRCODE_INVALID_GRANT_OPERATION),
1046  errmsg("default privileges cannot be set for columns")));
1047 
1048  if (privnode->priv_name == NULL) /* parser mistake? */
1049  elog(ERROR, "AccessPriv node must specify privilege");
1050  priv = string_to_privilege(privnode->priv_name);
1051 
1052  if (priv & ~((AclMode) all_privileges))
1053  ereport(ERROR,
1054  (errcode(ERRCODE_INVALID_GRANT_OPERATION),
1055  errmsg(errormsg, privilege_to_string(priv))));
1056 
1057  iacls.privileges |= priv;
1058  }
1059  }
1060 
1061  if (rolespecs == NIL)
1062  {
1063  /* Set permissions for myself */
1064  iacls.roleid = GetUserId();
1065 
1066  SetDefaultACLsInSchemas(&iacls, nspnames);
1067  }
1068  else
1069  {
1070  /* Look up the role OIDs and do permissions checks */
1071  ListCell *rolecell;
1072 
1073  foreach(rolecell, rolespecs)
1074  {
1075  RoleSpec *rolespec = lfirst(rolecell);
1076 
1077  iacls.roleid = get_rolespec_oid(rolespec, false);
1078 
1079  /*
1080  * We insist that calling user be a member of each target role. If
1081  * he has that, he could become that role anyway via SET ROLE, so
1082  * FOR ROLE is just a syntactic convenience and doesn't give any
1083  * special privileges.
1084  */
1086 
1087  SetDefaultACLsInSchemas(&iacls, nspnames);
1088  }
1089  }
1090 }
#define NIL
Definition: pg_list.h:65
#define ACL_ALL_RIGHTS_FUNCTION
Definition: acl.h:162
bool grant_option
Definition: aclchk.c:92
Oid GetUserId(void)
Definition: miscinit.c:478
static AclMode string_to_privilege(const char *privname)
Definition: aclchk.c:3235
bool grant_option
Definition: parsenodes.h:2012
#define gettext_noop(x)
Definition: c.h:1197
int errcode(int sqlerrcode)
Definition: elog.c:698
List * cols
Definition: parsenodes.h:2043
unsigned int Oid
Definition: postgres_ext.h:31
List * grantees
Definition: aclchk.c:91
List * lappend_oid(List *list, Oid datum)
Definition: list.c:372
uint32 AclMode
Definition: parsenodes.h:80
#define ERROR
Definition: elog.h:46
#define ACL_NO_RIGHTS
Definition: parsenodes.h:96
#define ACL_ALL_RIGHTS_SCHEMA
Definition: acl.h:165
int location
Definition: parsenodes.h:749
#define ACL_ALL_RIGHTS_TYPE
Definition: acl.h:167
void check_is_member_of_role(Oid member, Oid role)
Definition: acl.c:4893
Node * arg
Definition: parsenodes.h:747
bool is_grant
Definition: parsenodes.h:2004
RoleSpecType roletype
Definition: parsenodes.h:339
Oid get_rolespec_oid(const RoleSpec *role, bool missing_ok)
Definition: acl.c:5128
#define ACL_ALL_RIGHTS_SEQUENCE
Definition: acl.h:158
DropBehavior behavior
Definition: aclchk.c:93
List * privileges
Definition: parsenodes.h:2009
#define ereport(elevel,...)
Definition: elog.h:157
DropBehavior behavior
Definition: parsenodes.h:2014
#define lfirst(lc)
Definition: pg_list.h:169
int parser_errposition(ParseState *pstate, int location)
Definition: parse_node.c:111
static void SetDefaultACLsInSchemas(InternalDefaultACL *iacls, List *nspnames)
Definition: aclchk.c:1098
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
#define ACL_ALL_RIGHTS_RELATION
Definition: acl.h:157
ObjectType objtype
Definition: parsenodes.h:2006
#define ACL_ID_PUBLIC
Definition: acl.h:46
char * defname
Definition: parsenodes.h:746
static const char * privilege_to_string(AclMode privilege)
Definition: aclchk.c:3272
List * grantees
Definition: parsenodes.h:2011
AclMode privileges
Definition: aclchk.c:90
Definition: pg_list.h:50
ObjectType objtype
Definition: aclchk.c:88
char * priv_name
Definition: parsenodes.h:2042

◆ ExecGrant_Attribute()

static void ExecGrant_Attribute ( InternalGrant istmt,
Oid  relOid,
const char *  relname,
AttrNumber  attnum,
Oid  ownerId,
AclMode  col_privileges,
Relation  attRelation,
const Acl old_rel_acl 
)
static

Definition at line 1607 of file aclchk.c.

References ACL_ALL_RIGHTS_COLUMN, ACL_NUM, aclconcat(), acldefault(), aclmembers(), ATTNUM, InternalGrant::behavior, CatalogTupleUpdate(), DatumGetAclPCopy, elog, ERROR, GETSTRUCT, GetUserId(), InternalGrant::grant_option, InternalGrant::grantees, heap_modify_tuple(), HeapTupleIsValid, Int16GetDatum, InternalGrant::is_grant, MemSet, merge_acl_with_grant(), NameStr, OBJECT_COLUMN, ObjectIdGetDatum, pfree(), PointerGetDatum, recordExtensionInitPriv(), RelationGetDescr, ReleaseSysCache(), restrict_and_check_grant(), SearchSysCache2(), select_best_grantor(), SysCacheGetAttr(), HeapTupleData::t_self, updateAclDependencies(), and values.

Referenced by ExecGrant_Relation().

1610 {
1611  HeapTuple attr_tuple;
1612  Form_pg_attribute pg_attribute_tuple;
1613  Acl *old_acl;
1614  Acl *new_acl;
1615  Acl *merged_acl;
1616  Datum aclDatum;
1617  bool isNull;
1618  Oid grantorId;
1619  AclMode avail_goptions;
1620  bool need_update;
1621  HeapTuple newtuple;
1622  Datum values[Natts_pg_attribute];
1623  bool nulls[Natts_pg_attribute];
1624  bool replaces[Natts_pg_attribute];
1625  int noldmembers;
1626  int nnewmembers;
1627  Oid *oldmembers;
1628  Oid *newmembers;
1629 
1630  attr_tuple = SearchSysCache2(ATTNUM,
1631  ObjectIdGetDatum(relOid),
1633  if (!HeapTupleIsValid(attr_tuple))
1634  elog(ERROR, "cache lookup failed for attribute %d of relation %u",
1635  attnum, relOid);
1636  pg_attribute_tuple = (Form_pg_attribute) GETSTRUCT(attr_tuple);
1637 
1638  /*
1639  * Get working copy of existing ACL. If there's no ACL, substitute the
1640  * proper default.
1641  */
1642  aclDatum = SysCacheGetAttr(ATTNUM, attr_tuple, Anum_pg_attribute_attacl,
1643  &isNull);
1644  if (isNull)
1645  {
1646  old_acl = acldefault(OBJECT_COLUMN, ownerId);
1647  /* There are no old member roles according to the catalogs */
1648  noldmembers = 0;
1649  oldmembers = NULL;
1650  }
1651  else
1652  {
1653  old_acl = DatumGetAclPCopy(aclDatum);
1654  /* Get the roles mentioned in the existing ACL */
1655  noldmembers = aclmembers(old_acl, &oldmembers);
1656  }
1657 
1658  /*
1659  * In select_best_grantor we should consider existing table-level ACL bits
1660  * as well as the per-column ACL. Build a new ACL that is their
1661  * concatenation. (This is a bit cheap and dirty compared to merging them
1662  * properly with no duplications, but it's all we need here.)
1663  */
1664  merged_acl = aclconcat(old_rel_acl, old_acl);
1665 
1666  /* Determine ID to do the grant as, and available grant options */
1667  select_best_grantor(GetUserId(), col_privileges,
1668  merged_acl, ownerId,
1669  &grantorId, &avail_goptions);
1670 
1671  pfree(merged_acl);
1672 
1673  /*
1674  * Restrict the privileges to what we can actually grant, and emit the
1675  * standards-mandated warning and error messages. Note: we don't track
1676  * whether the user actually used the ALL PRIVILEGES(columns) syntax for
1677  * each column; we just approximate it by whether all the possible
1678  * privileges are specified now. Since the all_privs flag only determines
1679  * whether a warning is issued, this seems close enough.
1680  */
1681  col_privileges =
1682  restrict_and_check_grant(istmt->is_grant, avail_goptions,
1683  (col_privileges == ACL_ALL_RIGHTS_COLUMN),
1684  col_privileges,
1685  relOid, grantorId, OBJECT_COLUMN,
1686  relname, attnum,
1687  NameStr(pg_attribute_tuple->attname));
1688 
1689  /*
1690  * Generate new ACL.
1691  */
1692  new_acl = merge_acl_with_grant(old_acl, istmt->is_grant,
1693  istmt->grant_option,
1694  istmt->behavior, istmt->grantees,
1695  col_privileges, grantorId,
1696  ownerId);
1697 
1698  /*
1699  * We need the members of both old and new ACLs so we can correct the
1700  * shared dependency information.
1701  */
1702  nnewmembers = aclmembers(new_acl, &newmembers);
1703 
1704  /* finished building new ACL value, now insert it */
1705  MemSet(values, 0, sizeof(values));
1706  MemSet(nulls, false, sizeof(nulls));
1707  MemSet(replaces, false, sizeof(replaces));
1708 
1709  /*
1710  * If the updated ACL is empty, we can set attacl to null, and maybe even
1711  * avoid an update of the pg_attribute row. This is worth testing because
1712  * we'll come through here multiple times for any relation-level REVOKE,
1713  * even if there were never any column GRANTs. Note we are assuming that
1714  * the "default" ACL state for columns is empty.
1715  */
1716  if (ACL_NUM(new_acl) > 0)
1717  {
1718  values[Anum_pg_attribute_attacl - 1] = PointerGetDatum(new_acl);
1719  need_update = true;
1720  }
1721  else
1722  {
1723  nulls[Anum_pg_attribute_attacl - 1] = true;
1724  need_update = !isNull;
1725  }
1726  replaces[Anum_pg_attribute_attacl - 1] = true;
1727 
1728  if (need_update)
1729  {
1730  newtuple = heap_modify_tuple(attr_tuple, RelationGetDescr(attRelation),
1731  values, nulls, replaces);
1732 
1733  CatalogTupleUpdate(attRelation, &newtuple->t_self, newtuple);
1734 
1735  /* Update initial privileges for extensions */
1736  recordExtensionInitPriv(relOid, RelationRelationId, attnum,
1737  ACL_NUM(new_acl) > 0 ? new_acl : NULL);
1738 
1739  /* Update the shared dependency ACL info */
1740  updateAclDependencies(RelationRelationId, relOid, attnum,
1741  ownerId,
1742  noldmembers, oldmembers,
1743  nnewmembers, newmembers);
1744  }
1745 
1746  pfree(new_acl);
1747 
1748  ReleaseSysCache(attr_tuple);
1749 }
void updateAclDependencies(Oid classId, Oid objectId, int32 objsubId, Oid ownerId, int noldmembers, Oid *oldmembers, int nnewmembers, Oid *newmembers)
Definition: pg_shdepend.c:482
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
#define ACL_ALL_RIGHTS_COLUMN
Definition: acl.h:156
#define RelationGetDescr(relation)
Definition: rel.h:483
Oid GetUserId(void)
Definition: miscinit.c:478
#define PointerGetDatum(X)
Definition: postgres.h:600
#define Int16GetDatum(X)
Definition: postgres.h:495
Acl * acldefault(ObjectType objtype, Oid ownerId)
Definition: acl.c:734
#define MemSet(start, val, len)
Definition: c.h:1008
NameData relname
Definition: pg_class.h:38
unsigned int Oid
Definition: postgres_ext.h:31
static Acl * merge_acl_with_grant(Acl *old_acl, bool is_grant, bool grant_option, DropBehavior behavior, List *grantees, AclMode privileges, Oid grantorId, Oid ownerId)
Definition: aclchk.c:152
uint32 AclMode
Definition: parsenodes.h:80
void pfree(void *pointer)
Definition: mcxt.c:1169
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
#define ACL_NUM(ACL)
Definition: acl.h:108
ItemPointerData t_self
Definition: htup.h:65
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:203
static void recordExtensionInitPriv(Oid objoid, Oid classoid, int objsubid, Acl *new_acl)
Definition: aclchk.c:6013
Acl * aclconcat(const Acl *left_acl, const Acl *right_acl)
Definition: acl.c:413
uintptr_t Datum
Definition: postgres.h:411
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1388
int16 attnum
Definition: pg_attribute.h:83
DropBehavior behavior
int aclmembers(const Acl *acl, Oid **roleids)
Definition: acl.c:1458
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:301
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:1138
void select_best_grantor(Oid roleId, AclMode privileges, const Acl *acl, Oid ownerId, Oid *grantorId, AclMode *grantOptions)
Definition: acl.c:5018
static Datum values[MAXATTR]
Definition: bootstrap.c:166
#define elog(elevel,...)
Definition: elog.h:232
#define NameStr(name)
Definition: c.h:681
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *replValues, bool *replIsnull, bool *doReplace)
Definition: heaptuple.c:1113
static AclMode restrict_and_check_grant(bool is_grant, AclMode avail_goptions, bool all_privs, AclMode privileges, Oid objectId, Oid grantorId, ObjectType objtype, const char *objname, AttrNumber att_number, const char *colname)
Definition: aclchk.c:211
#define DatumGetAclPCopy(X)
Definition: acl.h:121

◆ ExecGrant_Database()

static void ExecGrant_Database ( InternalGrant grantStmt)
static

Definition at line 2087 of file aclchk.c.

References ACL_ALL_RIGHTS_DATABASE, ACL_NO_RIGHTS, acldefault(), aclmembers(), InternalGrant::all_privs, InternalGrant::behavior, CatalogTupleUpdate(), CommandCounterIncrement(), DATABASEOID, DatumGetAclPCopy, elog, ERROR, GETSTRUCT, GetUserId(), InternalGrant::grant_option, InternalGrant::grantees, heap_getattr, heap_modify_tuple(), HeapTupleIsValid, InternalGrant::is_grant, lfirst_oid, MemSet, merge_acl_with_grant(), NameStr, OBJECT_DATABASE, ObjectIdGetDatum, InternalGrant::objects, pfree(), PointerGetDatum, InternalGrant::privileges, RelationGetDescr, ReleaseSysCache(), restrict_and_check_grant(), RowExclusiveLock, SearchSysCache1(), select_best_grantor(), HeapTupleData::t_self, table_close(), table_open(), updateAclDependencies(), and values.

Referenced by ExecGrantStmt_oids().

2088 {
2089  Relation relation;
2090  ListCell *cell;
2091 
2092  if (istmt->all_privs && istmt->privileges == ACL_NO_RIGHTS)
2093  istmt->privileges = ACL_ALL_RIGHTS_DATABASE;
2094 
2095  relation = table_open(DatabaseRelationId, RowExclusiveLock);
2096 
2097  foreach(cell, istmt->objects)
2098  {
2099  Oid datId = lfirst_oid(cell);
2100  Form_pg_database pg_database_tuple;
2101  Datum aclDatum;
2102  bool isNull;
2103  AclMode avail_goptions;
2104  AclMode this_privileges;
2105  Acl *old_acl;
2106  Acl *new_acl;
2107  Oid grantorId;
2108  Oid ownerId;
2109  HeapTuple newtuple;
2110  Datum values[Natts_pg_database];
2111  bool nulls[Natts_pg_database];
2112  bool replaces[Natts_pg_database];
2113  int noldmembers;
2114  int nnewmembers;
2115  Oid *oldmembers;
2116  Oid *newmembers;
2117  HeapTuple tuple;
2118 
2119  tuple = SearchSysCache1(DATABASEOID, ObjectIdGetDatum(datId));
2120  if (!HeapTupleIsValid(tuple))
2121  elog(ERROR, "cache lookup failed for database %u", datId);
2122 
2123  pg_database_tuple = (Form_pg_database) GETSTRUCT(tuple);
2124 
2125  /*
2126  * Get owner ID and working copy of existing ACL. If there's no ACL,
2127  * substitute the proper default.
2128  */
2129  ownerId = pg_database_tuple->datdba;
2130  aclDatum = heap_getattr(tuple, Anum_pg_database_datacl,
2131  RelationGetDescr(relation), &isNull);
2132  if (isNull)
2133  {
2134  old_acl = acldefault(OBJECT_DATABASE, ownerId);
2135  /* There are no old member roles according to the catalogs */
2136  noldmembers = 0;
2137  oldmembers = NULL;
2138  }
2139  else
2140  {
2141  old_acl = DatumGetAclPCopy(aclDatum);
2142  /* Get the roles mentioned in the existing ACL */
2143  noldmembers = aclmembers(old_acl, &oldmembers);
2144  }
2145 
2146  /* Determine ID to do the grant as, and available grant options */
2147  select_best_grantor(GetUserId(), istmt->privileges,
2148  old_acl, ownerId,
2149  &grantorId, &avail_goptions);
2150 
2151  /*
2152  * Restrict the privileges to what we can actually grant, and emit the
2153  * standards-mandated warning and error messages.
2154  */
2155  this_privileges =
2156  restrict_and_check_grant(istmt->is_grant, avail_goptions,
2157  istmt->all_privs, istmt->privileges,
2158  datId, grantorId, OBJECT_DATABASE,
2159  NameStr(pg_database_tuple->datname),
2160  0, NULL);
2161 
2162  /*
2163  * Generate new ACL.
2164  */
2165  new_acl = merge_acl_with_grant(old_acl, istmt->is_grant,
2166  istmt->grant_option, istmt->behavior,
2167  istmt->grantees, this_privileges,
2168  grantorId, ownerId);
2169 
2170  /*
2171  * We need the members of both old and new ACLs so we can correct the
2172  * shared dependency information.
2173  */
2174  nnewmembers = aclmembers(new_acl, &newmembers);
2175 
2176  /* finished building new ACL value, now insert it */
2177  MemSet(values, 0, sizeof(values));
2178  MemSet(nulls, false, sizeof(nulls));
2179  MemSet(replaces, false, sizeof(replaces));
2180 
2181  replaces[Anum_pg_database_datacl - 1] = true;
2182  values[Anum_pg_database_datacl - 1] = PointerGetDatum(new_acl);
2183 
2184  newtuple = heap_modify_tuple(tuple, RelationGetDescr(relation), values,
2185  nulls, replaces);
2186 
2187  CatalogTupleUpdate(relation, &newtuple->t_self, newtuple);
2188 
2189  /* Update the shared dependency ACL info */
2190  updateAclDependencies(DatabaseRelationId, pg_database_tuple->oid, 0,
2191  ownerId,
2192  noldmembers, oldmembers,
2193  nnewmembers, newmembers);
2194 
2195  ReleaseSysCache(tuple);
2196 
2197  pfree(new_acl);
2198 
2199  /* prevent error when processing duplicate objects */
2201  }
2202 
2203  table_close(relation, RowExclusiveLock);
2204 }
void updateAclDependencies(Oid classId, Oid objectId, int32 objsubId, Oid ownerId, int noldmembers, Oid *oldmembers, int nnewmembers, Oid *newmembers)
Definition: pg_shdepend.c:482
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
#define RelationGetDescr(relation)
Definition: rel.h:483
Oid GetUserId(void)
Definition: miscinit.c:478
FormData_pg_database * Form_pg_database
Definition: pg_database.h:81
#define PointerGetDatum(X)
Definition: postgres.h:600
Acl * acldefault(ObjectType objtype, Oid ownerId)
Definition: acl.c:734
#define MemSet(start, val, len)
Definition: c.h:1008
unsigned int Oid
Definition: postgres_ext.h:31
static Acl * merge_acl_with_grant(Acl *old_acl, bool is_grant, bool grant_option, DropBehavior behavior, List *grantees, AclMode privileges, Oid grantorId, Oid ownerId)
Definition: aclchk.c:152
uint32 AclMode
Definition: parsenodes.h:80
void pfree(void *pointer)
Definition: mcxt.c:1169
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
#define ACL_NO_RIGHTS
Definition: parsenodes.h:96
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
#define heap_getattr(tup, attnum, tupleDesc, isnull)
Definition: htup_details.h:761
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
uintptr_t Datum
Definition: postgres.h:411
void CommandCounterIncrement(void)
Definition: xact.c:1021
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
int aclmembers(const Acl *acl, Oid **roleids)
Definition: acl.c:1458
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:301
void select_best_grantor(Oid roleId, AclMode privileges, const Acl *acl, Oid ownerId, Oid *grantorId, AclMode *grantOptions)
Definition: acl.c:5018
#define ACL_ALL_RIGHTS_DATABASE
Definition: acl.h:159
static Datum values[MAXATTR]
Definition: bootstrap.c:166
#define elog(elevel,...)
Definition: elog.h:232
#define NameStr(name)
Definition: c.h:681
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *replValues, bool *replIsnull, bool *doReplace)
Definition: heaptuple.c:1113
static AclMode restrict_and_check_grant(bool is_grant, AclMode avail_goptions, bool all_privs, AclMode privileges, Oid objectId, Oid grantorId, ObjectType objtype, const char *objname, AttrNumber att_number, const char *colname)
Definition: aclchk.c:211
#define lfirst_oid(lc)
Definition: pg_list.h:171
#define DatumGetAclPCopy(X)
Definition: acl.h:121

◆ ExecGrant_Fdw()

static void ExecGrant_Fdw ( InternalGrant grantStmt)
static

Definition at line 2207 of file aclchk.c.

References ACL_ALL_RIGHTS_FDW, ACL_NO_RIGHTS, acldefault(), aclmembers(), InternalGrant::all_privs, InternalGrant::behavior, CatalogTupleUpdate(), CommandCounterIncrement(), DatumGetAclPCopy, elog, ERROR, FOREIGNDATAWRAPPEROID, GETSTRUCT, GetUserId(), InternalGrant::grant_option, InternalGrant::grantees, heap_modify_tuple(), HeapTupleIsValid, InternalGrant::is_grant, lfirst_oid, MemSet, merge_acl_with_grant(), NameStr, OBJECT_FDW, ObjectIdGetDatum, InternalGrant::objects, pfree(), PointerGetDatum, InternalGrant::privileges, recordExtensionInitPriv(), RelationGetDescr, ReleaseSysCache(), restrict_and_check_grant(), RowExclusiveLock, SearchSysCache1(), select_best_grantor(), SysCacheGetAttr(), HeapTupleData::t_self, table_close(), table_open(), updateAclDependencies(), and values.

Referenced by ExecGrantStmt_oids().

2208 {
2209  Relation relation;
2210  ListCell *cell;
2211 
2212  if (istmt->all_privs && istmt->privileges == ACL_NO_RIGHTS)
2213  istmt->privileges = ACL_ALL_RIGHTS_FDW;
2214 
2215  relation = table_open(ForeignDataWrapperRelationId, RowExclusiveLock);
2216 
2217  foreach(cell, istmt->objects)
2218  {
2219  Oid fdwid = lfirst_oid(cell);
2220  Form_pg_foreign_data_wrapper pg_fdw_tuple;
2221  Datum aclDatum;
2222  bool isNull;
2223  AclMode avail_goptions;
2224  AclMode this_privileges;
2225  Acl *old_acl;
2226  Acl *new_acl;
2227  Oid grantorId;
2228  Oid ownerId;
2229  HeapTuple tuple;
2230  HeapTuple newtuple;
2231  Datum values[Natts_pg_foreign_data_wrapper];
2232  bool nulls[Natts_pg_foreign_data_wrapper];
2233  bool replaces[Natts_pg_foreign_data_wrapper];
2234  int noldmembers;
2235  int nnewmembers;
2236  Oid *oldmembers;
2237  Oid *newmembers;
2238 
2240  ObjectIdGetDatum(fdwid));
2241  if (!HeapTupleIsValid(tuple))
2242  elog(ERROR, "cache lookup failed for foreign-data wrapper %u", fdwid);
2243 
2244  pg_fdw_tuple = (Form_pg_foreign_data_wrapper) GETSTRUCT(tuple);
2245 
2246  /*
2247  * Get owner ID and working copy of existing ACL. If there's no ACL,
2248  * substitute the proper default.
2249  */
2250  ownerId = pg_fdw_tuple->fdwowner;
2251  aclDatum = SysCacheGetAttr(FOREIGNDATAWRAPPEROID, tuple,
2252  Anum_pg_foreign_data_wrapper_fdwacl,
2253  &isNull);
2254  if (isNull)
2255  {
2256  old_acl = acldefault(OBJECT_FDW, ownerId);
2257  /* There are no old member roles according to the catalogs */
2258  noldmembers = 0;
2259  oldmembers = NULL;
2260  }
2261  else
2262  {
2263  old_acl = DatumGetAclPCopy(aclDatum);
2264  /* Get the roles mentioned in the existing ACL */
2265  noldmembers = aclmembers(old_acl, &oldmembers);
2266  }
2267 
2268  /* Determine ID to do the grant as, and available grant options */
2269  select_best_grantor(GetUserId(), istmt->privileges,
2270  old_acl, ownerId,
2271  &grantorId, &avail_goptions);
2272 
2273  /*
2274  * Restrict the privileges to what we can actually grant, and emit the
2275  * standards-mandated warning and error messages.
2276  */
2277  this_privileges =
2278  restrict_and_check_grant(istmt->is_grant, avail_goptions,
2279  istmt->all_privs, istmt->privileges,
2280  fdwid, grantorId, OBJECT_FDW,
2281  NameStr(pg_fdw_tuple->fdwname),
2282  0, NULL);
2283 
2284  /*
2285  * Generate new ACL.
2286  */
2287  new_acl = merge_acl_with_grant(old_acl, istmt->is_grant,
2288  istmt->grant_option, istmt->behavior,
2289  istmt->grantees, this_privileges,
2290  grantorId, ownerId);
2291 
2292  /*
2293  * We need the members of both old and new ACLs so we can correct the
2294  * shared dependency information.
2295  */
2296  nnewmembers = aclmembers(new_acl, &newmembers);
2297 
2298  /* finished building new ACL value, now insert it */
2299  MemSet(values, 0, sizeof(values));
2300  MemSet(nulls, false, sizeof(nulls));
2301  MemSet(replaces, false, sizeof(replaces));
2302 
2303  replaces[Anum_pg_foreign_data_wrapper_fdwacl - 1] = true;
2304  values[Anum_pg_foreign_data_wrapper_fdwacl - 1] = PointerGetDatum(new_acl);
2305 
2306  newtuple = heap_modify_tuple(tuple, RelationGetDescr(relation), values,
2307  nulls, replaces);
2308 
2309  CatalogTupleUpdate(relation, &newtuple->t_self, newtuple);
2310 
2311  /* Update initial privileges for extensions */
2312  recordExtensionInitPriv(fdwid, ForeignDataWrapperRelationId, 0,
2313  new_acl);
2314 
2315  /* Update the shared dependency ACL info */
2316  updateAclDependencies(ForeignDataWrapperRelationId,
2317  pg_fdw_tuple->oid, 0,
2318  ownerId,
2319  noldmembers, oldmembers,
2320  nnewmembers, newmembers);
2321 
2322  ReleaseSysCache(tuple);
2323 
2324  pfree(new_acl);
2325 
2326  /* prevent error when processing duplicate objects */
2328  }
2329 
2330  table_close(relation, RowExclusiveLock);
2331 }
void updateAclDependencies(Oid classId, Oid objectId, int32 objsubId, Oid ownerId, int noldmembers, Oid *oldmembers, int nnewmembers, Oid *newmembers)
Definition: pg_shdepend.c:482
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
#define RelationGetDescr(relation)
Definition: rel.h:483
Oid GetUserId(void)
Definition: miscinit.c:478
#define PointerGetDatum(X)
Definition: postgres.h:600
Acl * acldefault(ObjectType objtype, Oid ownerId)
Definition: acl.c:734
#define MemSet(start, val, len)
Definition: c.h:1008
unsigned int Oid
Definition: postgres_ext.h:31
static Acl * merge_acl_with_grant(Acl *old_acl, bool is_grant, bool grant_option, DropBehavior behavior, List *grantees, AclMode privileges, Oid grantorId, Oid ownerId)
Definition: aclchk.c:152
uint32 AclMode
Definition: parsenodes.h:80
void pfree(void *pointer)
Definition: mcxt.c:1169
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
#define ACL_NO_RIGHTS
Definition: parsenodes.h:96
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
static void recordExtensionInitPriv(Oid objoid, Oid classoid, int objsubid, Acl *new_acl)
Definition: aclchk.c:6013
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
uintptr_t Datum
Definition: postgres.h:411
void CommandCounterIncrement(void)
Definition: xact.c:1021
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1388
int aclmembers(const Acl *acl, Oid **roleids)
Definition: acl.c:1458
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
FormData_pg_foreign_data_wrapper * Form_pg_foreign_data_wrapper
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:301
void select_best_grantor(Oid roleId, AclMode privileges, const Acl *acl, Oid ownerId, Oid *grantorId, AclMode *grantOptions)
Definition: acl.c:5018
static Datum values[MAXATTR]
Definition: bootstrap.c:166
#define elog(elevel,...)
Definition: elog.h:232
#define NameStr(name)
Definition: c.h:681
#define ACL_ALL_RIGHTS_FDW
Definition: acl.h:160
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *replValues, bool *replIsnull, bool *doReplace)
Definition: heaptuple.c:1113
static AclMode restrict_and_check_grant(bool is_grant, AclMode avail_goptions, bool all_privs, AclMode privileges, Oid objectId, Oid grantorId, ObjectType objtype, const char *objname, AttrNumber att_number, const char *colname)
Definition: aclchk.c:211
#define lfirst_oid(lc)
Definition: pg_list.h:171
#define DatumGetAclPCopy(X)
Definition: acl.h:121

◆ ExecGrant_ForeignServer()

static void ExecGrant_ForeignServer ( InternalGrant grantStmt)
static

Definition at line 2334 of file aclchk.c.

References ACL_ALL_RIGHTS_FOREIGN_SERVER, ACL_NO_RIGHTS, acldefault(), aclmembers(), InternalGrant::all_privs, InternalGrant::behavior, CatalogTupleUpdate(), CommandCounterIncrement(), DatumGetAclPCopy, elog, ERROR, FOREIGNSERVEROID, GETSTRUCT, GetUserId(), InternalGrant::grant_option, InternalGrant::grantees, heap_modify_tuple(), HeapTupleIsValid, InternalGrant::is_grant, lfirst_oid, MemSet, merge_acl_with_grant(), NameStr, OBJECT_FOREIGN_SERVER, ObjectIdGetDatum, InternalGrant::objects, pfree(), PointerGetDatum, InternalGrant::privileges, recordExtensionInitPriv(), RelationGetDescr, ReleaseSysCache(), restrict_and_check_grant(), RowExclusiveLock, SearchSysCache1(), select_best_grantor(), SysCacheGetAttr(), HeapTupleData::t_self, table_close(), table_open(), updateAclDependencies(), and values.

Referenced by ExecGrantStmt_oids().

2335 {
2336  Relation relation;
2337  ListCell *cell;
2338 
2339  if (istmt->all_privs && istmt->privileges == ACL_NO_RIGHTS)
2340  istmt->privileges = ACL_ALL_RIGHTS_FOREIGN_SERVER;
2341 
2342  relation = table_open(ForeignServerRelationId, RowExclusiveLock);
2343 
2344  foreach(cell, istmt->objects)
2345  {
2346  Oid srvid = lfirst_oid(cell);
2347  Form_pg_foreign_server pg_server_tuple;
2348  Datum aclDatum;
2349  bool isNull;
2350  AclMode avail_goptions;
2351  AclMode this_privileges;
2352  Acl *old_acl;
2353  Acl *new_acl;
2354  Oid grantorId;
2355  Oid ownerId;
2356  HeapTuple tuple;
2357  HeapTuple newtuple;
2358  Datum values[Natts_pg_foreign_server];
2359  bool nulls[Natts_pg_foreign_server];
2360  bool replaces[Natts_pg_foreign_server];
2361  int noldmembers;
2362  int nnewmembers;
2363  Oid *oldmembers;
2364  Oid *newmembers;
2365 
2367  if (!HeapTupleIsValid(tuple))
2368  elog(ERROR, "cache lookup failed for foreign server %u", srvid);
2369 
2370  pg_server_tuple = (Form_pg_foreign_server) GETSTRUCT(tuple);
2371 
2372  /*
2373  * Get owner ID and working copy of existing ACL. If there's no ACL,
2374  * substitute the proper default.
2375  */
2376  ownerId = pg_server_tuple->srvowner;
2377  aclDatum = SysCacheGetAttr(FOREIGNSERVEROID, tuple,
2378  Anum_pg_foreign_server_srvacl,
2379  &isNull);
2380  if (isNull)
2381  {
2382  old_acl = acldefault(OBJECT_FOREIGN_SERVER, ownerId);
2383  /* There are no old member roles according to the catalogs */
2384  noldmembers = 0;
2385  oldmembers = NULL;
2386  }
2387  else
2388  {
2389  old_acl = DatumGetAclPCopy(aclDatum);
2390  /* Get the roles mentioned in the existing ACL */
2391  noldmembers = aclmembers(old_acl, &oldmembers);
2392  }
2393 
2394  /* Determine ID to do the grant as, and available grant options */
2395  select_best_grantor(GetUserId(), istmt->privileges,
2396  old_acl, ownerId,
2397  &grantorId, &avail_goptions);
2398 
2399  /*
2400  * Restrict the privileges to what we can actually grant, and emit the
2401  * standards-mandated warning and error messages.
2402  */
2403  this_privileges =
2404  restrict_and_check_grant(istmt->is_grant, avail_goptions,
2405  istmt->all_privs, istmt->privileges,
2406  srvid, grantorId, OBJECT_FOREIGN_SERVER,
2407  NameStr(pg_server_tuple->srvname),
2408  0, NULL);
2409 
2410  /*
2411  * Generate new ACL.
2412  */
2413  new_acl = merge_acl_with_grant(old_acl, istmt->is_grant,
2414  istmt->grant_option, istmt->behavior,
2415  istmt->grantees, this_privileges,
2416  grantorId, ownerId);
2417 
2418  /*
2419  * We need the members of both old and new ACLs so we can correct the
2420  * shared dependency information.
2421  */
2422  nnewmembers = aclmembers(new_acl, &newmembers);
2423 
2424  /* finished building new ACL value, now insert it */
2425  MemSet(values, 0, sizeof(values));
2426  MemSet(nulls, false, sizeof(nulls));
2427  MemSet(replaces, false, sizeof(replaces));
2428 
2429  replaces[Anum_pg_foreign_server_srvacl - 1] = true;
2430  values[Anum_pg_foreign_server_srvacl - 1] = PointerGetDatum(new_acl);
2431 
2432  newtuple = heap_modify_tuple(tuple, RelationGetDescr(relation), values,
2433  nulls, replaces);
2434 
2435  CatalogTupleUpdate(relation, &newtuple->t_self, newtuple);
2436 
2437  /* Update initial privileges for extensions */
2438  recordExtensionInitPriv(srvid, ForeignServerRelationId, 0, new_acl);
2439 
2440  /* Update the shared dependency ACL info */
2441  updateAclDependencies(ForeignServerRelationId,
2442  pg_server_tuple->oid, 0,
2443  ownerId,
2444  noldmembers, oldmembers,
2445  nnewmembers, newmembers);
2446 
2447  ReleaseSysCache(tuple);
2448 
2449  pfree(new_acl);
2450 
2451  /* prevent error when processing duplicate objects */
2453  }
2454 
2455  table_close(relation, RowExclusiveLock);
2456 }
void updateAclDependencies(Oid classId, Oid objectId, int32 objsubId, Oid ownerId, int noldmembers, Oid *oldmembers, int nnewmembers, Oid *newmembers)
Definition: pg_shdepend.c:482
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
#define RelationGetDescr(relation)
Definition: rel.h:483
Oid GetUserId(void)
Definition: miscinit.c:478
#define PointerGetDatum(X)
Definition: postgres.h:600
Acl * acldefault(ObjectType objtype, Oid ownerId)
Definition: acl.c:734
#define MemSet(start, val, len)
Definition: c.h:1008
unsigned int Oid
Definition: postgres_ext.h:31
static Acl * merge_acl_with_grant(Acl *old_acl, bool is_grant, bool grant_option, DropBehavior behavior, List *grantees, AclMode privileges, Oid grantorId, Oid ownerId)
Definition: aclchk.c:152
uint32 AclMode
Definition: parsenodes.h:80
void pfree(void *pointer)
Definition: mcxt.c:1169
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
#define ACL_NO_RIGHTS
Definition: parsenodes.h:96
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
static void recordExtensionInitPriv(Oid objoid, Oid classoid, int objsubid, Acl *new_acl)
Definition: aclchk.c:6013
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
uintptr_t Datum
Definition: postgres.h:411
void CommandCounterIncrement(void)
Definition: xact.c:1021
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1388
FormData_pg_foreign_server * Form_pg_foreign_server
int aclmembers(const Acl *acl, Oid **roleids)
Definition: acl.c:1458
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:301
void select_best_grantor(Oid roleId, AclMode privileges, const Acl *acl, Oid ownerId, Oid *grantorId, AclMode *grantOptions)
Definition: acl.c:5018
#define ACL_ALL_RIGHTS_FOREIGN_SERVER
Definition: acl.h:161
static Datum values[MAXATTR]
Definition: bootstrap.c:166
#define elog(elevel,...)
Definition: elog.h:232
#define NameStr(name)
Definition: c.h:681
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *replValues, bool *replIsnull, bool *doReplace)
Definition: heaptuple.c:1113
static AclMode restrict_and_check_grant(bool is_grant, AclMode avail_goptions, bool all_privs, AclMode privileges, Oid objectId, Oid grantorId, ObjectType objtype, const char *objname, AttrNumber att_number, const char *colname)
Definition: aclchk.c:211
#define lfirst_oid(lc)
Definition: pg_list.h:171
#define DatumGetAclPCopy(X)
Definition: acl.h:121

◆ ExecGrant_Function()

static void ExecGrant_Function ( InternalGrant grantStmt)
static

Definition at line 2459 of file aclchk.c.

References ACL_ALL_RIGHTS_FUNCTION, ACL_NO_RIGHTS, acldefault(), aclmembers(), InternalGrant::all_privs, InternalGrant::behavior, CatalogTupleUpdate(), CommandCounterIncrement(), DatumGetAclPCopy, elog, ERROR, GETSTRUCT, GetUserId(), InternalGrant::grant_option, InternalGrant::grantees, heap_modify_tuple(), HeapTupleIsValid, InternalGrant::is_grant, lfirst_oid, MemSet, merge_acl_with_grant(), NameStr, OBJECT_FUNCTION, ObjectIdGetDatum, InternalGrant::objects, pfree(), PointerGetDatum, InternalGrant::privileges, PROCOID, recordExtensionInitPriv(), RelationGetDescr, ReleaseSysCache(), restrict_and_check_grant(), RowExclusiveLock, SearchSysCache1(), select_best_grantor(), SysCacheGetAttr(), HeapTupleData::t_self, table_close(), table_open(), updateAclDependencies(), and values.

Referenced by ExecGrantStmt_oids().

2460 {
2461  Relation relation;
2462  ListCell *cell;
2463 
2464  if (istmt->all_privs && istmt->privileges == ACL_NO_RIGHTS)
2465  istmt->privileges = ACL_ALL_RIGHTS_FUNCTION;
2466 
2467  relation = table_open(ProcedureRelationId, RowExclusiveLock);
2468 
2469  foreach(cell, istmt->objects)
2470  {
2471  Oid funcId = lfirst_oid(cell);
2472  Form_pg_proc pg_proc_tuple;
2473  Datum aclDatum;
2474  bool isNull;
2475  AclMode avail_goptions;
2476  AclMode this_privileges;
2477  Acl *old_acl;
2478  Acl *new_acl;
2479  Oid grantorId;
2480  Oid ownerId;
2481  HeapTuple tuple;
2482  HeapTuple newtuple;
2483  Datum values[Natts_pg_proc];
2484  bool nulls[Natts_pg_proc];
2485  bool replaces[Natts_pg_proc];
2486  int noldmembers;
2487  int nnewmembers;
2488  Oid *oldmembers;
2489  Oid *newmembers;
2490 
2491  tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcId));
2492  if (!HeapTupleIsValid(tuple))
2493  elog(ERROR, "cache lookup failed for function %u", funcId);
2494 
2495  pg_proc_tuple = (Form_pg_proc) GETSTRUCT(tuple);
2496 
2497  /*
2498  * Get owner ID and working copy of existing ACL. If there's no ACL,
2499  * substitute the proper default.
2500  */
2501  ownerId = pg_proc_tuple->proowner;
2502  aclDatum = SysCacheGetAttr(PROCOID, tuple, Anum_pg_proc_proacl,
2503  &isNull);
2504  if (isNull)
2505  {
2506  old_acl = acldefault(OBJECT_FUNCTION, ownerId);
2507  /* There are no old member roles according to the catalogs */
2508  noldmembers = 0;
2509  oldmembers = NULL;
2510  }
2511  else
2512  {
2513  old_acl = DatumGetAclPCopy(aclDatum);
2514  /* Get the roles mentioned in the existing ACL */
2515  noldmembers = aclmembers(old_acl, &oldmembers);
2516  }
2517 
2518  /* Determine ID to do the grant as, and available grant options */
2519  select_best_grantor(GetUserId(), istmt->privileges,
2520  old_acl, ownerId,
2521  &grantorId, &avail_goptions);
2522 
2523  /*
2524  * Restrict the privileges to what we can actually grant, and emit the
2525  * standards-mandated warning and error messages.
2526  */
2527  this_privileges =
2528  restrict_and_check_grant(istmt->is_grant, avail_goptions,
2529  istmt->all_privs, istmt->privileges,
2530  funcId, grantorId, OBJECT_FUNCTION,
2531  NameStr(pg_proc_tuple->proname),
2532  0, NULL);
2533 
2534  /*
2535  * Generate new ACL.
2536  */
2537  new_acl = merge_acl_with_grant(old_acl, istmt->is_grant,
2538  istmt->grant_option, istmt->behavior,
2539  istmt->grantees, this_privileges,
2540  grantorId, ownerId);
2541 
2542  /*
2543  * We need the members of both old and new ACLs so we can correct the
2544  * shared dependency information.
2545  */
2546  nnewmembers = aclmembers(new_acl, &newmembers);
2547 
2548  /* finished building new ACL value, now insert it */
2549  MemSet(values, 0, sizeof(values));
2550  MemSet(nulls, false, sizeof(nulls));
2551  MemSet(replaces, false, sizeof(replaces));
2552 
2553  replaces[Anum_pg_proc_proacl - 1] = true;
2554  values[Anum_pg_proc_proacl - 1] = PointerGetDatum(new_acl);
2555 
2556  newtuple = heap_modify_tuple(tuple, RelationGetDescr(relation), values,
2557  nulls, replaces);
2558 
2559  CatalogTupleUpdate(relation, &newtuple->t_self, newtuple);
2560 
2561  /* Update initial privileges for extensions */
2562  recordExtensionInitPriv(funcId, ProcedureRelationId, 0, new_acl);
2563 
2564  /* Update the shared dependency ACL info */
2565  updateAclDependencies(ProcedureRelationId, funcId, 0,
2566  ownerId,
2567  noldmembers, oldmembers,
2568  nnewmembers, newmembers);
2569 
2570  ReleaseSysCache(tuple);
2571 
2572  pfree(new_acl);
2573 
2574  /* prevent error when processing duplicate objects */
2576  }
2577 
2578  table_close(relation, RowExclusiveLock);
2579 }
void updateAclDependencies(Oid classId, Oid objectId, int32 objsubId, Oid ownerId, int noldmembers, Oid *oldmembers, int nnewmembers, Oid *newmembers)
Definition: pg_shdepend.c:482
#define ACL_ALL_RIGHTS_FUNCTION
Definition: acl.h:162
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
#define RelationGetDescr(relation)
Definition: rel.h:483
Oid GetUserId(void)
Definition: miscinit.c:478
#define PointerGetDatum(X)
Definition: postgres.h:600
Acl * acldefault(ObjectType objtype, Oid ownerId)
Definition: acl.c:734
#define MemSet(start, val, len)
Definition: c.h:1008
unsigned int Oid
Definition: postgres_ext.h:31
static Acl * merge_acl_with_grant(Acl *old_acl, bool is_grant, bool grant_option, DropBehavior behavior, List *grantees, AclMode privileges, Oid grantorId, Oid ownerId)
Definition: aclchk.c:152
uint32 AclMode
Definition: parsenodes.h:80
void pfree(void *pointer)
Definition: mcxt.c:1169
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
#define ACL_NO_RIGHTS
Definition: parsenodes.h:96
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
static void recordExtensionInitPriv(Oid objoid, Oid classoid, int objsubid, Acl *new_acl)
Definition: aclchk.c:6013
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
uintptr_t Datum
Definition: postgres.h:411
void CommandCounterIncrement(void)
Definition: xact.c:1021
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1388
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:136
int aclmembers(const Acl *acl, Oid **roleids)
Definition: acl.c:1458
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:301
void select_best_grantor(Oid roleId, AclMode privileges, const Acl *acl, Oid ownerId, Oid *grantorId, AclMode *grantOptions)
Definition: acl.c:5018
static Datum values[MAXATTR]
Definition: bootstrap.c:166
#define elog(elevel,...)
Definition: elog.h:232
#define NameStr(name)
Definition: c.h:681
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *replValues, bool *replIsnull, bool *doReplace)
Definition: heaptuple.c:1113
static AclMode restrict_and_check_grant(bool is_grant, AclMode avail_goptions, bool all_privs, AclMode privileges, Oid objectId, Oid grantorId, ObjectType objtype, const char *objname, AttrNumber att_number, const char *colname)
Definition: aclchk.c:211
#define lfirst_oid(lc)
Definition: pg_list.h:171
#define DatumGetAclPCopy(X)
Definition: acl.h:121

◆ ExecGrant_Language()

static void ExecGrant_Language ( InternalGrant grantStmt)
static

Definition at line 2582 of file aclchk.c.

References ACL_ALL_RIGHTS_LANGUAGE, ACL_NO_RIGHTS, acldefault(), aclmembers(), InternalGrant::all_privs, InternalGrant::behavior, CatalogTupleUpdate(), CommandCounterIncrement(), DatumGetAclPCopy, elog, ereport, errcode(), errdetail(), errmsg(), ERROR, GETSTRUCT, GetUserId(), InternalGrant::grant_option, InternalGrant::grantees, heap_modify_tuple(), HeapTupleIsValid, InternalGrant::is_grant, LANGNAME, LANGOID, lfirst_oid, MemSet, merge_acl_with_grant(), NameStr, OBJECT_LANGUAGE, ObjectIdGetDatum, InternalGrant::objects, pfree(), PointerGetDatum, InternalGrant::privileges, recordExtensionInitPriv(), RelationGetDescr, ReleaseSysCache(), restrict_and_check_grant(), RowExclusiveLock, SearchSysCache1(), select_best_grantor(), SysCacheGetAttr(), HeapTupleData::t_self, table_close(), table_open(), updateAclDependencies(), and values.

Referenced by ExecGrantStmt_oids().

2583 {
2584  Relation relation;
2585  ListCell *cell;
2586 
2587  if (istmt->all_privs && istmt->privileges == ACL_NO_RIGHTS)
2588  istmt->privileges = ACL_ALL_RIGHTS_LANGUAGE;
2589 
2590  relation = table_open(LanguageRelationId, RowExclusiveLock);
2591 
2592  foreach(cell, istmt->objects)
2593  {
2594  Oid langId = lfirst_oid(cell);
2595  Form_pg_language pg_language_tuple;
2596  Datum aclDatum;
2597  bool isNull;
2598  AclMode avail_goptions;
2599  AclMode this_privileges;
2600  Acl *old_acl;
2601  Acl *new_acl;
2602  Oid grantorId;
2603  Oid ownerId;
2604  HeapTuple tuple;
2605  HeapTuple newtuple;
2606  Datum values[Natts_pg_language];
2607  bool nulls[Natts_pg_language];
2608  bool replaces[Natts_pg_language];
2609  int noldmembers;
2610  int nnewmembers;
2611  Oid *oldmembers;
2612  Oid *newmembers;
2613 
2614  tuple = SearchSysCache1(LANGOID, ObjectIdGetDatum(langId));
2615  if (!HeapTupleIsValid(tuple))
2616  elog(ERROR, "cache lookup failed for language %u", langId);
2617 
2618  pg_language_tuple = (Form_pg_language) GETSTRUCT(tuple);
2619 
2620  if (!pg_language_tuple->lanpltrusted)
2621  ereport(ERROR,
2622  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2623  errmsg("language \"%s\" is not trusted",
2624  NameStr(pg_language_tuple->lanname)),
2625  errdetail("GRANT and REVOKE are not allowed on untrusted languages, "
2626  "because only superusers can use untrusted languages.")));
2627 
2628  /*
2629  * Get owner ID and working copy of existing ACL. If there's no ACL,
2630  * substitute the proper default.
2631  */
2632  ownerId = pg_language_tuple->lanowner;
2633  aclDatum = SysCacheGetAttr(LANGNAME, tuple, Anum_pg_language_lanacl,
2634  &isNull);
2635  if (isNull)
2636  {
2637  old_acl = acldefault(OBJECT_LANGUAGE, ownerId);
2638  /* There are no old member roles according to the catalogs */
2639  noldmembers = 0;
2640  oldmembers = NULL;
2641  }
2642  else
2643  {
2644  old_acl = DatumGetAclPCopy(aclDatum);
2645  /* Get the roles mentioned in the existing ACL */
2646  noldmembers = aclmembers(old_acl, &oldmembers);
2647  }
2648 
2649  /* Determine ID to do the grant as, and available grant options */
2650  select_best_grantor(GetUserId(), istmt->privileges,
2651  old_acl, ownerId,
2652  &grantorId, &avail_goptions);
2653 
2654  /*
2655  * Restrict the privileges to what we can actually grant, and emit the
2656  * standards-mandated warning and error messages.
2657  */
2658  this_privileges =
2659  restrict_and_check_grant(istmt->is_grant, avail_goptions,
2660  istmt->all_privs, istmt->privileges,
2661  langId, grantorId, OBJECT_LANGUAGE,
2662  NameStr(pg_language_tuple->lanname),
2663  0, NULL);
2664 
2665  /*
2666  * Generate new ACL.
2667  */
2668  new_acl = merge_acl_with_grant(old_acl, istmt->is_grant,
2669  istmt->grant_option, istmt->behavior,
2670  istmt->grantees, this_privileges,
2671  grantorId, ownerId);
2672 
2673  /*
2674  * We need the members of both old and new ACLs so we can correct the
2675  * shared dependency information.
2676  */
2677  nnewmembers = aclmembers(new_acl, &newmembers);
2678 
2679  /* finished building new ACL value, now insert it */
2680  MemSet(values, 0, sizeof(values));
2681  MemSet(nulls, false, sizeof(nulls));
2682  MemSet(replaces, false, sizeof(replaces));
2683 
2684  replaces[Anum_pg_language_lanacl - 1] = true;
2685  values[Anum_pg_language_lanacl - 1] = PointerGetDatum(new_acl);
2686 
2687  newtuple = heap_modify_tuple(tuple, RelationGetDescr(relation), values,
2688  nulls, replaces);
2689 
2690  CatalogTupleUpdate(relation, &newtuple->t_self, newtuple);
2691 
2692  /* Update initial privileges for extensions */
2693  recordExtensionInitPriv(langId, LanguageRelationId, 0, new_acl);
2694 
2695  /* Update the shared dependency ACL info */
2696  updateAclDependencies(LanguageRelationId, pg_language_tuple->oid, 0,
2697  ownerId,
2698  noldmembers, oldmembers,
2699  nnewmembers, newmembers);
2700 
2701  ReleaseSysCache(tuple);
2702 
2703  pfree(new_acl);
2704 
2705  /* prevent error when processing duplicate objects */
2707  }
2708 
2709  table_close(relation, RowExclusiveLock);
2710 }
void updateAclDependencies(Oid classId, Oid objectId, int32 objsubId, Oid ownerId, int noldmembers, Oid *oldmembers, int nnewmembers, Oid *newmembers)
Definition: pg_shdepend.c:482
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
#define RelationGetDescr(relation)
Definition: rel.h:483
Oid GetUserId(void)
Definition: miscinit.c:478
#define PointerGetDatum(X)
Definition: postgres.h:600
int errcode(int sqlerrcode)
Definition: elog.c:698
Acl * acldefault(ObjectType objtype, Oid ownerId)
Definition: acl.c:734
#define MemSet(start, val, len)
Definition: c.h:1008
unsigned int Oid
Definition: postgres_ext.h:31
static Acl * merge_acl_with_grant(Acl *old_acl, bool is_grant, bool grant_option, DropBehavior behavior, List *grantees, AclMode privileges, Oid grantorId, Oid ownerId)
Definition: aclchk.c:152
#define ACL_ALL_RIGHTS_LANGUAGE
Definition: acl.h:163
uint32 AclMode
Definition: parsenodes.h:80
void pfree(void *pointer)
Definition: mcxt.c:1169
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
#define ACL_NO_RIGHTS
Definition: parsenodes.h:96
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
int errdetail(const char *fmt,...)
Definition: elog.c:1042
static void recordExtensionInitPriv(Oid objoid, Oid classoid, int objsubid, Acl *new_acl)
Definition: aclchk.c:6013
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
uintptr_t Datum
Definition: postgres.h:411
void CommandCounterIncrement(void)
Definition: xact.c:1021
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1388
#define ereport(elevel,...)
Definition: elog.h:157
int aclmembers(const Acl *acl, Oid **roleids)
Definition: acl.c:1458
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:301
void select_best_grantor(Oid roleId, AclMode privileges, const Acl *acl, Oid ownerId, Oid *grantorId, AclMode *grantOptions)
Definition: acl.c:5018
static Datum values[MAXATTR]
Definition: bootstrap.c:166
FormData_pg_language * Form_pg_language
Definition: pg_language.h:65
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
#define NameStr(name)
Definition: c.h:681
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *replValues, bool *replIsnull, bool *doReplace)
Definition: heaptuple.c:1113
static AclMode restrict_and_check_grant(bool is_grant, AclMode avail_goptions, bool all_privs, AclMode privileges, Oid objectId, Oid grantorId, ObjectType objtype, const char *objname, AttrNumber att_number, const char *colname)
Definition: aclchk.c:211
#define lfirst_oid(lc)
Definition: pg_list.h:171
#define DatumGetAclPCopy(X)
Definition: acl.h:121

◆ ExecGrant_Largeobject()

static void ExecGrant_Largeobject ( InternalGrant grantStmt)
static

Definition at line 2713 of file aclchk.c.

References ACL_ALL_RIGHTS_LARGEOBJECT, ACL_NO_RIGHTS, acldefault(), aclmembers(), InternalGrant::all_privs, InternalGrant::behavior, BTEqualStrategyNumber, CatalogTupleUpdate(), CommandCounterIncrement(), DatumGetAclPCopy, elog, ERROR, GETSTRUCT, GetUserId(), InternalGrant::grant_option, InternalGrant::grantees, heap_getattr, heap_modify_tuple(), HeapTupleIsValid, InternalGrant::is_grant, LargeObjectMetadataOidIndexId, lfirst_oid, MemSet, merge_acl_with_grant(), NAMEDATALEN, OBJECT_LARGEOBJECT, ObjectIdGetDatum, InternalGrant::objects, pfree(), PointerGetDatum, InternalGrant::privileges, recordExtensionInitPriv(), RelationGetDescr, restrict_and_check_grant(), RowExclusiveLock, ScanKeyInit(), select_best_grantor(), snprintf, systable_beginscan(), systable_endscan(), systable_getnext(), HeapTupleData::t_self, table_close(), table_open(), updateAclDependencies(), and values.

Referenced by ExecGrantStmt_oids().

2714 {
2715  Relation relation;
2716  ListCell *cell;
2717 
2718  if (istmt->all_privs && istmt->privileges == ACL_NO_RIGHTS)
2719  istmt->privileges = ACL_ALL_RIGHTS_LARGEOBJECT;
2720 
2721  relation = table_open(LargeObjectMetadataRelationId,
2723 
2724  foreach(cell, istmt->objects)
2725  {
2726  Oid loid = lfirst_oid(cell);
2727  Form_pg_largeobject_metadata form_lo_meta;
2728  char loname[NAMEDATALEN];
2729  Datum aclDatum;
2730  bool isNull;
2731  AclMode avail_goptions;
2732  AclMode this_privileges;
2733  Acl *old_acl;
2734  Acl *new_acl;
2735  Oid grantorId;
2736  Oid ownerId;
2737  HeapTuple newtuple;
2738  Datum values[Natts_pg_largeobject_metadata];
2739  bool nulls[Natts_pg_largeobject_metadata];
2740  bool replaces[Natts_pg_largeobject_metadata];
2741  int noldmembers;
2742  int nnewmembers;
2743  Oid *oldmembers;
2744  Oid *newmembers;
2745  ScanKeyData entry[1];
2746  SysScanDesc scan;
2747  HeapTuple tuple;
2748 
2749  /* There's no syscache for pg_largeobject_metadata */
2750  ScanKeyInit(&entry[0],
2751  Anum_pg_largeobject_metadata_oid,
2752  BTEqualStrategyNumber, F_OIDEQ,
2753  ObjectIdGetDatum(loid));
2754 
2755  scan = systable_beginscan(relation,
2757  NULL, 1, entry);
2758 
2759  tuple = systable_getnext(scan);
2760  if (!HeapTupleIsValid(tuple))
2761  elog(ERROR, "could not find tuple for large object %u", loid);
2762 
2763  form_lo_meta = (Form_pg_largeobject_metadata) GETSTRUCT(tuple);
2764 
2765  /*
2766  * Get owner ID and working copy of existing ACL. If there's no ACL,
2767  * substitute the proper default.
2768  */
2769  ownerId = form_lo_meta->lomowner;
2770  aclDatum = heap_getattr(tuple,
2771  Anum_pg_largeobject_metadata_lomacl,
2772  RelationGetDescr(relation), &isNull);
2773  if (isNull)
2774  {
2775  old_acl = acldefault(OBJECT_LARGEOBJECT, ownerId);
2776  /* There are no old member roles according to the catalogs */
2777  noldmembers = 0;
2778  oldmembers = NULL;
2779  }
2780  else
2781  {
2782  old_acl = DatumGetAclPCopy(aclDatum);
2783  /* Get the roles mentioned in the existing ACL */
2784  noldmembers = aclmembers(old_acl, &oldmembers);
2785  }
2786 
2787  /* Determine ID to do the grant as, and available grant options */
2788  select_best_grantor(GetUserId(), istmt->privileges,
2789  old_acl, ownerId,
2790  &grantorId, &avail_goptions);
2791 
2792  /*
2793  * Restrict the privileges to what we can actually grant, and emit the
2794  * standards-mandated warning and error messages.
2795  */
2796  snprintf(loname, sizeof(loname), "large object %u", loid);
2797  this_privileges =
2798  restrict_and_check_grant(istmt->is_grant, avail_goptions,
2799  istmt->all_privs, istmt->privileges,
2800  loid, grantorId, OBJECT_LARGEOBJECT,
2801  loname, 0, NULL);
2802 
2803  /*
2804  * Generate new ACL.
2805  */
2806  new_acl = merge_acl_with_grant(old_acl, istmt->is_grant,
2807  istmt->grant_option, istmt->behavior,
2808  istmt->grantees, this_privileges,
2809  grantorId, ownerId);
2810 
2811  /*
2812  * We need the members of both old and new ACLs so we can correct the
2813  * shared dependency information.
2814  */
2815  nnewmembers = aclmembers(new_acl, &newmembers);
2816 
2817  /* finished building new ACL value, now insert it */
2818  MemSet(values, 0, sizeof(values));
2819  MemSet(nulls, false, sizeof(nulls));
2820  MemSet(replaces, false, sizeof(replaces));
2821 
2822  replaces[Anum_pg_largeobject_metadata_lomacl - 1] = true;
2823  values[Anum_pg_largeobject_metadata_lomacl - 1]
2824  = PointerGetDatum(new_acl);
2825 
2826  newtuple = heap_modify_tuple(tuple, RelationGetDescr(relation),
2827  values, nulls, replaces);
2828 
2829  CatalogTupleUpdate(relation, &newtuple->t_self, newtuple);
2830 
2831  /* Update initial privileges for extensions */
2832  recordExtensionInitPriv(loid, LargeObjectRelationId, 0, new_acl);
2833 
2834  /* Update the shared dependency ACL info */
2835  updateAclDependencies(LargeObjectRelationId,
2836  form_lo_meta->oid, 0,
2837  ownerId,
2838  noldmembers, oldmembers,
2839  nnewmembers, newmembers);
2840 
2841  systable_endscan(scan);
2842 
2843  pfree(new_acl);
2844 
2845  /* prevent error when processing duplicate objects */
2847  }
2848 
2849  table_close(relation, RowExclusiveLock);
2850 }
void updateAclDependencies(Oid classId, Oid objectId, int32 objsubId, Oid ownerId, int noldmembers, Oid *oldmembers, int nnewmembers, Oid *newmembers)
Definition: pg_shdepend.c:482
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:595
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
#define RelationGetDescr(relation)
Definition: rel.h:483
Oid GetUserId(void)
Definition: miscinit.c:478
#define PointerGetDatum(X)
Definition: postgres.h:600
Acl * acldefault(ObjectType objtype, Oid ownerId)
Definition: acl.c:734
#define MemSet(start, val, len)
Definition: c.h:1008
#define LargeObjectMetadataOidIndexId
unsigned int Oid
Definition: postgres_ext.h:31
static Acl * merge_acl_with_grant(Acl *old_acl, bool is_grant, bool grant_option, DropBehavior behavior, List *grantees, AclMode privileges, Oid grantorId, Oid ownerId)
Definition: aclchk.c:152
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:383
#define NAMEDATALEN
uint32 AclMode
Definition: parsenodes.h:80
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:502
void pfree(void *pointer)
Definition: mcxt.c:1169
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
#define ACL_NO_RIGHTS
Definition: parsenodes.h:96
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
static void recordExtensionInitPriv(Oid objoid, Oid classoid, int objsubid, Acl *new_acl)
Definition: aclchk.c:6013
#define heap_getattr(tup, attnum, tupleDesc, isnull)
Definition: htup_details.h:761
FormData_pg_largeobject_metadata * Form_pg_largeobject_metadata
uintptr_t Datum
Definition: postgres.h:411
void CommandCounterIncrement(void)
Definition: xact.c:1021
#define ACL_ALL_RIGHTS_LARGEOBJECT
Definition: acl.h:164
int aclmembers(const Acl *acl, Oid **roleids)
Definition: acl.c:1458
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:301
void select_best_grantor(Oid roleId, AclMode privileges, const Acl *acl, Oid ownerId, Oid *grantorId, AclMode *grantOptions)
Definition: acl.c:5018
static Datum values[MAXATTR]
Definition: bootstrap.c:166
#define elog(elevel,...)
Definition: elog.h:232
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *replValues, bool *replIsnull, bool *doReplace)
Definition: heaptuple.c:1113
#define snprintf
Definition: port.h:216
#define BTEqualStrategyNumber
Definition: stratnum.h:31
static AclMode restrict_and_check_grant(bool is_grant, AclMode avail_goptions, bool all_privs, AclMode privileges, Oid objectId, Oid grantorId, ObjectType objtype, const char *objname, AttrNumber att_number, const char *colname)
Definition: aclchk.c:211
#define lfirst_oid(lc)
Definition: pg_list.h:171
#define DatumGetAclPCopy(X)
Definition: acl.h:121

◆ ExecGrant_Namespace()

static void ExecGrant_Namespace ( InternalGrant grantStmt)
static

Definition at line 2853 of file aclchk.c.

References ACL_ALL_RIGHTS_SCHEMA, ACL_NO_RIGHTS, acldefault(), aclmembers(), InternalGrant::all_privs, InternalGrant::behavior, CatalogTupleUpdate(), CommandCounterIncrement(), DatumGetAclPCopy, elog, ERROR, GETSTRUCT, GetUserId(), InternalGrant::grant_option, InternalGrant::grantees, heap_modify_tuple(), HeapTupleIsValid, InternalGrant::is_grant, lfirst_oid, MemSet, merge_acl_with_grant(), NAMESPACENAME, NAMESPACEOID, NameStr, OBJECT_SCHEMA, ObjectIdGetDatum, InternalGrant::objects, pfree(), PointerGetDatum, InternalGrant::privileges, recordExtensionInitPriv(), RelationGetDescr, ReleaseSysCache(), restrict_and_check_grant(), RowExclusiveLock, SearchSysCache1(), select_best_grantor(), SysCacheGetAttr(), HeapTupleData::t_self, table_close(), table_open(), updateAclDependencies(), and values.

Referenced by ExecGrantStmt_oids().

2854 {
2855  Relation relation;
2856  ListCell *cell;
2857 
2858  if (istmt->all_privs && istmt->privileges == ACL_NO_RIGHTS)
2859  istmt->privileges = ACL_ALL_RIGHTS_SCHEMA;
2860 
2861  relation = table_open(NamespaceRelationId, RowExclusiveLock);
2862 
2863  foreach(cell, istmt->objects)
2864  {
2865  Oid nspid = lfirst_oid(cell);
2866  Form_pg_namespace pg_namespace_tuple;
2867  Datum aclDatum;
2868  bool isNull;
2869  AclMode avail_goptions;
2870  AclMode this_privileges;
2871  Acl *old_acl;
2872  Acl *new_acl;
2873  Oid grantorId;
2874  Oid ownerId;
2875  HeapTuple tuple;
2876  HeapTuple newtuple;
2877  Datum values[Natts_pg_namespace];
2878  bool nulls[Natts_pg_namespace];
2879  bool replaces[Natts_pg_namespace];
2880  int noldmembers;
2881  int nnewmembers;
2882  Oid *oldmembers;
2883  Oid *newmembers;
2884 
2886  if (!HeapTupleIsValid(tuple))
2887  elog(ERROR, "cache lookup failed for namespace %u", nspid);
2888 
2889  pg_namespace_tuple = (Form_pg_namespace) GETSTRUCT(tuple);
2890 
2891  /*
2892  * Get owner ID and working copy of existing ACL. If there's no ACL,
2893  * substitute the proper default.
2894  */
2895  ownerId = pg_namespace_tuple->nspowner;
2896  aclDatum = SysCacheGetAttr(NAMESPACENAME, tuple,
2897  Anum_pg_namespace_nspacl,
2898  &isNull);
2899  if (isNull)
2900  {
2901  old_acl = acldefault(OBJECT_SCHEMA, ownerId);
2902  /* There are no old member roles according to the catalogs */
2903  noldmembers = 0;
2904  oldmembers = NULL;
2905  }
2906  else
2907  {
2908  old_acl = DatumGetAclPCopy(aclDatum);
2909  /* Get the roles mentioned in the existing ACL */
2910  noldmembers = aclmembers(old_acl, &oldmembers);
2911  }
2912 
2913  /* Determine ID to do the grant as, and available grant options */
2914  select_best_grantor(GetUserId(), istmt->privileges,
2915  old_acl, ownerId,
2916  &grantorId, &avail_goptions);
2917 
2918  /*
2919  * Restrict the privileges to what we can actually grant, and emit the
2920  * standards-mandated warning and error messages.
2921  */
2922  this_privileges =
2923  restrict_and_check_grant(istmt->is_grant, avail_goptions,
2924  istmt->all_privs, istmt->privileges,
2925  nspid, grantorId, OBJECT_SCHEMA,
2926  NameStr(pg_namespace_tuple->nspname),
2927  0, NULL);
2928 
2929  /*
2930  * Generate new ACL.
2931  */
2932  new_acl = merge_acl_with_grant(old_acl, istmt->is_grant,
2933  istmt->grant_option, istmt->behavior,
2934  istmt->grantees, this_privileges,
2935  grantorId, ownerId);
2936 
2937  /*
2938  * We need the members of both old and new ACLs so we can correct the
2939  * shared dependency information.
2940  */
2941  nnewmembers = aclmembers(new_acl, &newmembers);
2942 
2943  /* finished building new ACL value, now insert it */
2944  MemSet(values, 0, sizeof(values));
2945  MemSet(nulls, false, sizeof(nulls));
2946  MemSet(replaces, false, sizeof(replaces));
2947 
2948  replaces[Anum_pg_namespace_nspacl - 1] = true;
2949  values[Anum_pg_namespace_nspacl - 1] = PointerGetDatum(new_acl);
2950 
2951  newtuple = heap_modify_tuple(tuple, RelationGetDescr(relation), values,
2952  nulls, replaces);
2953 
2954  CatalogTupleUpdate(relation, &newtuple->t_self, newtuple);
2955 
2956  /* Update initial privileges for extensions */
2957  recordExtensionInitPriv(nspid, NamespaceRelationId, 0, new_acl);
2958 
2959  /* Update the shared dependency ACL info */
2960  updateAclDependencies(NamespaceRelationId, pg_namespace_tuple->oid, 0,
2961  ownerId,
2962  noldmembers, oldmembers,
2963  nnewmembers, newmembers);
2964 
2965  ReleaseSysCache(tuple);
2966 
2967  pfree(new_acl);
2968 
2969  /* prevent error when processing duplicate objects */
2971  }
2972 
2973  table_close(relation, RowExclusiveLock);
2974 }
void updateAclDependencies(Oid classId, Oid objectId, int32 objsubId, Oid ownerId, int noldmembers, Oid *oldmembers, int nnewmembers, Oid *newmembers)
Definition: pg_shdepend.c:482
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
FormData_pg_namespace * Form_pg_namespace
Definition: pg_namespace.h:52
#define RelationGetDescr(relation)
Definition: rel.h:483
Oid GetUserId(void)
Definition: miscinit.c:478
#define PointerGetDatum(X)
Definition: postgres.h:600
Acl * acldefault(ObjectType objtype, Oid ownerId)
Definition: acl.c:734
#define MemSet(start, val, len)
Definition: c.h:1008
unsigned int Oid
Definition: postgres_ext.h:31
static Acl * merge_acl_with_grant(Acl *old_acl, bool is_grant, bool grant_option, DropBehavior behavior, List *grantees, AclMode privileges, Oid grantorId, Oid ownerId)
Definition: aclchk.c:152
uint32 AclMode
Definition: parsenodes.h:80
void pfree(void *pointer)
Definition: mcxt.c:1169
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
#define ACL_NO_RIGHTS
Definition: parsenodes.h:96
ItemPointerData t_self
Definition: htup.h:65
#define ACL_ALL_RIGHTS_SCHEMA
Definition: acl.h:165
#define RowExclusiveLock
Definition: lockdefs.h:38
static void recordExtensionInitPriv(Oid objoid, Oid classoid, int objsubid, Acl *new_acl)
Definition: aclchk.c:6013
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
uintptr_t Datum
Definition: postgres.h:411
void CommandCounterIncrement(void)
Definition: xact.c:1021
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1388
int aclmembers(const Acl *acl, Oid **roleids)
Definition: acl.c:1458
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:301
void select_best_grantor(Oid roleId, AclMode privileges, const Acl *acl, Oid ownerId, Oid *grantorId, AclMode *grantOptions)
Definition: acl.c:5018
static Datum values[MAXATTR]
Definition: bootstrap.c:166
#define elog(elevel,...)
Definition: elog.h:232
#define NameStr(name)
Definition: c.h:681
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *replValues, bool *replIsnull, bool *doReplace)
Definition: heaptuple.c:1113
static AclMode restrict_and_check_grant(bool is_grant, AclMode avail_goptions, bool all_privs, AclMode privileges, Oid objectId, Oid grantorId, ObjectType objtype, const char *objname, AttrNumber att_number, const char *colname)
Definition: aclchk.c:211
#define lfirst_oid(lc)
Definition: pg_list.h:171
#define DatumGetAclPCopy(X)
Definition: acl.h:121

◆ ExecGrant_Relation()

static void ExecGrant_Relation ( InternalGrant grantStmt)
static

Definition at line 1755 of file aclchk.c.

References ACL_ALL_RIGHTS_COLUMN, ACL_ALL_RIGHTS_RELATION, ACL_ALL_RIGHTS_SEQUENCE, ACL_NO_RIGHTS, ACL_SELECT, aclcopy(), acldefault(), aclmembers(), InternalGrant::all_privs, InternalGrant::behavior, CatalogTupleUpdate(), InternalGrant::col_privs, AccessPriv::cols, CommandCounterIncrement(), DatumGetAclPCopy, elog, ereport, errcode(), errmsg(), ERROR, ExecGrant_Attribute(), expand_all_col_privileges(), expand_col_privileges(), FirstLowInvalidHeapAttributeNumber, GETSTRUCT, GetUserId(), InternalGrant::grant_option, InternalGrant::grantees, heap_modify_tuple(), HeapTupleIsValid, i, InternalGrant::is_grant, lfirst, lfirst_oid, MemSet, merge_acl_with_grant(), NameStr, OBJECT_SEQUENCE, OBJECT_TABLE, ObjectIdGetDatum, InternalGrant::objects, InternalGrant::objtype, palloc0(), pfree(), PointerGetDatum, AccessPriv::priv_name, privilege_to_string(), InternalGrant::privileges, recordExtensionInitPriv(), RelationGetDescr, ReleaseSysCache(), RELOID, restrict_and_check_grant(), RowExclusiveLock, SearchSysCache1(), select_best_grantor(), string_to_privilege(), SysCacheGetAttr(), HeapTupleData::t_self, table_close(), table_open(), updateAclDependencies(), values, and WARNING.

Referenced by ExecGrantStmt_oids().

1756 {
1757  Relation relation;
1758  Relation attRelation;
1759  ListCell *cell;
1760 
1761  relation = table_open(RelationRelationId, RowExclusiveLock);
1762  attRelation = table_open(AttributeRelationId, RowExclusiveLock);
1763 
1764  foreach(cell, istmt->objects)
1765  {
1766  Oid relOid = lfirst_oid(cell);
1767  Datum aclDatum;
1768  Form_pg_class pg_class_tuple;
1769  bool isNull;
1770  AclMode this_privileges;
1771  AclMode *col_privileges;
1772  int num_col_privileges;
1773  bool have_col_privileges;
1774  Acl *old_acl;
1775  Acl *old_rel_acl;
1776  int noldmembers;
1777  Oid *oldmembers;
1778  Oid ownerId;
1779  HeapTuple tuple;
1780  ListCell *cell_colprivs;
1781 
1782  tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relOid));
1783  if (!HeapTupleIsValid(tuple))
1784  elog(ERROR, "cache lookup failed for relation %u", relOid);
1785  pg_class_tuple = (Form_pg_class) GETSTRUCT(tuple);
1786 
1787  /* Not sensible to grant on an index */
1788  if (pg_class_tuple->relkind == RELKIND_INDEX ||
1789  pg_class_tuple->relkind == RELKIND_PARTITIONED_INDEX)
1790  ereport(ERROR,
1791  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1792  errmsg("\"%s\" is an index",
1793  NameStr(pg_class_tuple->relname))));
1794 
1795  /* Composite types aren't tables either */
1796  if (pg_class_tuple->relkind == RELKIND_COMPOSITE_TYPE)
1797  ereport(ERROR,
1798  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1799  errmsg("\"%s\" is a composite type",
1800  NameStr(pg_class_tuple->relname))));
1801 
1802  /* Used GRANT SEQUENCE on a non-sequence? */
1803  if (istmt->objtype == OBJECT_SEQUENCE &&
1804  pg_class_tuple->relkind != RELKIND_SEQUENCE)
1805  ereport(ERROR,
1806  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1807  errmsg("\"%s\" is not a sequence",
1808  NameStr(pg_class_tuple->relname))));
1809 
1810  /* Adjust the default permissions based on object type */
1811  if (istmt->all_privs && istmt->privileges == ACL_NO_RIGHTS)
1812  {
1813  if (pg_class_tuple->relkind == RELKIND_SEQUENCE)
1814  this_privileges = ACL_ALL_RIGHTS_SEQUENCE;
1815  else
1816  this_privileges = ACL_ALL_RIGHTS_RELATION;
1817  }
1818  else
1819  this_privileges = istmt->privileges;
1820 
1821  /*
1822  * The GRANT TABLE syntax can be used for sequences and non-sequences,
1823  * so we have to look at the relkind to determine the supported
1824  * permissions. The OR of table and sequence permissions were already
1825  * checked.
1826  */
1827  if (istmt->objtype == OBJECT_TABLE)
1828  {
1829  if (pg_class_tuple->relkind == RELKIND_SEQUENCE)
1830  {
1831  /*
1832  * For backward compatibility, just throw a warning for
1833  * invalid sequence permissions when using the non-sequence
1834  * GRANT syntax.
1835  */
1836  if (this_privileges & ~((AclMode) ACL_ALL_RIGHTS_SEQUENCE))
1837  {
1838  /*
1839  * Mention the object name because the user needs to know
1840  * which operations succeeded. This is required because
1841  * WARNING allows the command to continue.
1842  */
1843  ereport(WARNING,
1844  (errcode(ERRCODE_INVALID_GRANT_OPERATION),
1845  errmsg("sequence \"%s\" only supports USAGE, SELECT, and UPDATE privileges",
1846  NameStr(pg_class_tuple->relname))));
1847  this_privileges &= (AclMode) ACL_ALL_RIGHTS_SEQUENCE;
1848  }
1849  }
1850  else
1851  {
1852  if (this_privileges & ~((AclMode) ACL_ALL_RIGHTS_RELATION))
1853  {
1854  /*
1855  * USAGE is the only permission supported by sequences but
1856  * not by non-sequences. Don't mention the object name
1857  * because we didn't in the combined TABLE | SEQUENCE
1858  * check.
1859  */
1860  ereport(ERROR,
1861  (errcode(ERRCODE_INVALID_GRANT_OPERATION),
1862  errmsg("invalid privilege type %s for table",
1863  "USAGE")));
1864  }
1865  }
1866  }
1867 
1868  /*
1869  * Set up array in which we'll accumulate any column privilege bits
1870  * that need modification. The array is indexed such that entry [0]
1871  * corresponds to FirstLowInvalidHeapAttributeNumber.
1872  */
1873  num_col_privileges = pg_class_tuple->relnatts - FirstLowInvalidHeapAttributeNumber + 1;
1874  col_privileges = (AclMode *) palloc0(num_col_privileges * sizeof(AclMode));
1875  have_col_privileges = false;
1876 
1877  /*
1878  * If we are revoking relation privileges that are also column
1879  * privileges, we must implicitly revoke them from each column too,
1880  * per SQL spec. (We don't need to implicitly add column privileges
1881  * during GRANT because the permissions-checking code always checks
1882  * both relation and per-column privileges.)
1883  */
1884  if (!istmt->is_grant &&
1885  (this_privileges & ACL_ALL_RIGHTS_COLUMN) != 0)
1886  {
1887  expand_all_col_privileges(relOid, pg_class_tuple,
1888  this_privileges & ACL_ALL_RIGHTS_COLUMN,
1889  col_privileges,
1890  num_col_privileges);
1891  have_col_privileges = true;
1892  }
1893 
1894  /*
1895  * Get owner ID and working copy of existing ACL. If there's no ACL,
1896  * substitute the proper default.
1897  */
1898  ownerId = pg_class_tuple->relowner;
1899  aclDatum = SysCacheGetAttr(RELOID, tuple, Anum_pg_class_relacl,
1900  &isNull);
1901  if (isNull)
1902  {
1903  switch (pg_class_tuple->relkind)
1904  {
1905  case RELKIND_SEQUENCE:
1906  old_acl = acldefault(OBJECT_SEQUENCE, ownerId);
1907  break;
1908  default:
1909  old_acl = acldefault(OBJECT_TABLE, ownerId);
1910  break;
1911  }
1912  /* There are no old member roles according to the catalogs */
1913  noldmembers = 0;
1914  oldmembers = NULL;
1915  }
1916  else
1917  {
1918  old_acl = DatumGetAclPCopy(aclDatum);
1919  /* Get the roles mentioned in the existing ACL */
1920  noldmembers = aclmembers(old_acl, &oldmembers);
1921  }
1922 
1923  /* Need an extra copy of original rel ACL for column handling */
1924  old_rel_acl = aclcopy(old_acl);
1925 
1926  /*
1927  * Handle relation-level privileges, if any were specified
1928  */
1929  if (this_privileges != ACL_NO_RIGHTS)
1930  {
1931  AclMode avail_goptions;
1932  Acl *new_acl;
1933  Oid grantorId;
1934  HeapTuple newtuple;
1935  Datum values[Natts_pg_class];
1936  bool nulls[Natts_pg_class];
1937  bool replaces[Natts_pg_class];
1938  int nnewmembers;
1939  Oid *newmembers;
1940  ObjectType objtype;
1941 
1942  /* Determine ID to do the grant as, and available grant options */
1943  select_best_grantor(GetUserId(), this_privileges,
1944  old_acl, ownerId,
1945  &grantorId, &avail_goptions);
1946 
1947  switch (pg_class_tuple->relkind)
1948  {
1949  case RELKIND_SEQUENCE:
1950  objtype = OBJECT_SEQUENCE;
1951  break;
1952  default:
1953  objtype = OBJECT_TABLE;
1954  break;
1955  }
1956 
1957  /*
1958  * Restrict the privileges to what we can actually grant, and emit
1959  * the standards-mandated warning and error messages.
1960  */
1961  this_privileges =
1962  restrict_and_check_grant(istmt->is_grant, avail_goptions,
1963  istmt->all_privs, this_privileges,
1964  relOid, grantorId, objtype,
1965  NameStr(pg_class_tuple->relname),
1966  0, NULL);
1967 
1968  /*
1969  * Generate new ACL.
1970  */
1971  new_acl = merge_acl_with_grant(old_acl,
1972  istmt->is_grant,
1973  istmt->grant_option,
1974  istmt->behavior,
1975  istmt->grantees,
1976  this_privileges,
1977  grantorId,
1978  ownerId);
1979 
1980  /*
1981  * We need the members of both old and new ACLs so we can correct
1982  * the shared dependency information.
1983  */
1984  nnewmembers = aclmembers(new_acl, &newmembers);
1985 
1986  /* finished building new ACL value, now insert it */
1987  MemSet(values, 0, sizeof(values));
1988  MemSet(nulls, false, sizeof(nulls));
1989  MemSet(replaces, false, sizeof(replaces));
1990 
1991  replaces[Anum_pg_class_relacl - 1] = true;
1992  values[Anum_pg_class_relacl - 1] = PointerGetDatum(new_acl);
1993 
1994  newtuple = heap_modify_tuple(tuple, RelationGetDescr(relation),
1995  values, nulls, replaces);
1996 
1997  CatalogTupleUpdate(relation, &newtuple->t_self, newtuple);
1998 
1999  /* Update initial privileges for extensions */
2000  recordExtensionInitPriv(relOid, RelationRelationId, 0, new_acl);
2001 
2002  /* Update the shared dependency ACL info */
2003  updateAclDependencies(RelationRelationId, relOid, 0,
2004  ownerId,
2005  noldmembers, oldmembers,
2006  nnewmembers, newmembers);
2007 
2008  pfree(new_acl);
2009  }
2010 
2011  /*
2012  * Handle column-level privileges, if any were specified or implied.
2013  * We first expand the user-specified column privileges into the
2014  * array, and then iterate over all nonempty array entries.
2015  */
2016  foreach(cell_colprivs, istmt->col_privs)
2017  {
2018  AccessPriv *col_privs = (AccessPriv *) lfirst(cell_colprivs);
2019 
2020  if (col_privs->priv_name == NULL)
2021  this_privileges = ACL_ALL_RIGHTS_COLUMN;
2022  else
2023  this_privileges = string_to_privilege(col_privs->priv_name);
2024 
2025  if (this_privileges & ~((AclMode) ACL_ALL_RIGHTS_COLUMN))
2026  ereport(ERROR,
2027  (errcode(ERRCODE_INVALID_GRANT_OPERATION),
2028  errmsg("invalid privilege type %s for column",
2029  privilege_to_string(this_privileges))));
2030 
2031  if (pg_class_tuple->relkind == RELKIND_SEQUENCE &&
2032  this_privileges & ~((AclMode) ACL_SELECT))
2033  {
2034  /*
2035  * The only column privilege allowed on sequences is SELECT.
2036  * This is a warning not error because we do it that way for
2037  * relation-level privileges.
2038  */
2039  ereport(WARNING,
2040  (errcode(ERRCODE_INVALID_GRANT_OPERATION),
2041  errmsg("sequence \"%s\" only supports SELECT column privileges",
2042  NameStr(pg_class_tuple->relname))));
2043 
2044  this_privileges &= (AclMode) ACL_SELECT;
2045  }
2046 
2047  expand_col_privileges(col_privs->cols, relOid,
2048  this_privileges,
2049  col_privileges,
2050  num_col_privileges);
2051  have_col_privileges = true;
2052  }
2053 
2054  if (have_col_privileges)
2055  {
2056  AttrNumber i;
2057 
2058  for (i = 0; i < num_col_privileges; i++)
2059  {
2060  if (col_privileges[i] == ACL_NO_RIGHTS)
2061  continue;
2062  ExecGrant_Attribute(istmt,
2063  relOid,
2064  NameStr(pg_class_tuple->relname),
2066  ownerId,
2067  col_privileges[i],
2068  attRelation,
2069  old_rel_acl);
2070  }
2071  }
2072 
2073  pfree(old_rel_acl);
2074  pfree(col_privileges);
2075 
2076  ReleaseSysCache(tuple);
2077 
2078  /* prevent error when processing duplicate objects */
2080  }
2081 
2082  table_close(attRelation, RowExclusiveLock);
2083  table_close(relation, RowExclusiveLock);
2084 }
void updateAclDependencies(Oid classId, Oid objectId, int32 objsubId, Oid ownerId, int noldmembers, Oid *oldmembers, int nnewmembers, Oid *newmembers)
Definition: pg_shdepend.c:482
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
#define ACL_ALL_RIGHTS_COLUMN
Definition: acl.h:156
#define RelationGetDescr(relation)
Definition: rel.h:483
Oid GetUserId(void)
Definition: miscinit.c:478
#define PointerGetDatum(X)
Definition: postgres.h:600
static AclMode string_to_privilege(const char *privname)
Definition: aclchk.c:3235
int errcode(int sqlerrcode)
Definition: elog.c:698
Acl * acldefault(ObjectType objtype, Oid ownerId)
Definition: acl.c:734
#define MemSet(start, val, len)
Definition: c.h:1008
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
List * cols
Definition: parsenodes.h:2043
unsigned int Oid
Definition: postgres_ext.h:31
static Acl * merge_acl_with_grant(Acl *old_acl, bool is_grant, bool grant_option, DropBehavior behavior, List *grantees, AclMode privileges, Oid grantorId, Oid ownerId)
Definition: aclchk.c:152
uint32 AclMode
Definition: parsenodes.h:80
static void expand_col_privileges(List *colnames, Oid table_oid, AclMode this_privileges, AclMode *col_privileges, int num_col_privileges)
Definition: aclchk.c:1528
void pfree(void *pointer)
Definition: mcxt.c:1169
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
#define ACL_NO_RIGHTS
Definition: parsenodes.h:96
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
static void recordExtensionInitPriv(Oid objoid, Oid classoid, int objsubid, Acl *new_acl)
Definition: aclchk.c:6013
static void expand_all_col_privileges(Oid table_oid, Form_pg_class classForm, AclMode this_privileges, AclMode *col_privileges, int num_col_privileges)
Definition: aclchk.c:1561
static void ExecGrant_Attribute(InternalGrant *istmt, Oid relOid, const char *relname, AttrNumber attnum, Oid ownerId, AclMode col_privileges, Relation attRelation, const Acl *old_rel_acl)
Definition: aclchk.c:1607
ObjectType
Definition: parsenodes.h:1775
#define WARNING
Definition: elog.h:40
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
void * palloc0(Size size)
Definition: mcxt.c:1093
uintptr_t Datum
Definition: postgres.h:411
void CommandCounterIncrement(void)
Definition: xact.c:1021
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
#define ACL_SELECT
Definition: parsenodes.h:83
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1388
#define ACL_ALL_RIGHTS_SEQUENCE
Definition: acl.h:158
#define ereport(elevel,...)
Definition: elog.h:157
Acl * aclcopy(const Acl *orig_acl)
Definition: acl.c:393
int aclmembers(const Acl *acl, Oid **roleids)
Definition: acl.c:1458
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define lfirst(lc)
Definition: pg_list.h:169
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:301
void select_best_grantor(Oid roleId, AclMode privileges, const Acl *acl, Oid ownerId, Oid *grantorId, AclMode *grantOptions)
Definition: acl.c:5018
static Datum values[MAXATTR]
Definition: bootstrap.c:166
FormData_pg_class * Form_pg_class
Definition: pg_class.h:153
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
int i
#define ACL_ALL_RIGHTS_RELATION
Definition: acl.h:157
#define NameStr(name)
Definition: c.h:681
static const char * privilege_to_string(AclMode privilege)
Definition: aclchk.c:3272
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *replValues, bool *replIsnull, bool *doReplace)
Definition: heaptuple.c:1113
int16 AttrNumber
Definition: attnum.h:21
static AclMode restrict_and_check_grant(bool is_grant, AclMode avail_goptions, bool all_privs, AclMode privileges, Oid objectId, Oid grantorId, ObjectType objtype, const char *objname, AttrNumber att_number, const char *colname)
Definition: aclchk.c:211
char * priv_name
Definition: parsenodes.h:2042
#define lfirst_oid(lc)
Definition: pg_list.h:171
#define DatumGetAclPCopy(X)
Definition: acl.h:121

◆ ExecGrant_Tablespace()

static void ExecGrant_Tablespace ( InternalGrant grantStmt)
static

Definition at line 2977 of file aclchk.c.

References ACL_ALL_RIGHTS_TABLESPACE, ACL_NO_RIGHTS, acldefault(), aclmembers(), InternalGrant::all_privs, InternalGrant::behavior, CatalogTupleUpdate(), CommandCounterIncrement(), DatumGetAclPCopy, elog, ERROR, GETSTRUCT, GetUserId(), InternalGrant::grant_option, InternalGrant::grantees, heap_getattr, heap_modify_tuple(), HeapTupleIsValid, InternalGrant::is_grant, lfirst_oid, MemSet, merge_acl_with_grant(), NameStr, OBJECT_TABLESPACE, ObjectIdGetDatum, InternalGrant::objects, pfree(), PointerGetDatum, InternalGrant::privileges, RelationGetDescr, ReleaseSysCache(), restrict_and_check_grant(), RowExclusiveLock, SearchSysCache1(), select_best_grantor(), HeapTupleData::t_self, table_close(), table_open(), TABLESPACEOID, updateAclDependencies(), and values.

Referenced by ExecGrantStmt_oids().

2978 {
2979  Relation relation;
2980  ListCell *cell;
2981 
2982  if (istmt->all_privs && istmt->privileges == ACL_NO_RIGHTS)
2983  istmt->privileges = ACL_ALL_RIGHTS_TABLESPACE;
2984 
2985  relation = table_open(TableSpaceRelationId, RowExclusiveLock);
2986 
2987  foreach(cell, istmt->objects)
2988  {
2989  Oid tblId = lfirst_oid(cell);
2990  Form_pg_tablespace pg_tablespace_tuple;
2991  Datum aclDatum;
2992  bool isNull;
2993  AclMode avail_goptions;
2994  AclMode this_privileges;
2995  Acl *old_acl;
2996  Acl *new_acl;
2997  Oid grantorId;
2998  Oid ownerId;
2999  HeapTuple newtuple;
3000  Datum values[Natts_pg_tablespace];
3001  bool nulls[Natts_pg_tablespace];
3002  bool replaces[Natts_pg_tablespace];
3003  int noldmembers;
3004  int nnewmembers;
3005  Oid *oldmembers;
3006  Oid *newmembers;
3007  HeapTuple tuple;
3008 
3009  /* Search syscache for pg_tablespace */
3011  if (!HeapTupleIsValid(tuple))
3012  elog(ERROR, "cache lookup failed for tablespace %u", tblId);
3013 
3014  pg_tablespace_tuple = (Form_pg_tablespace) GETSTRUCT(tuple);
3015 
3016  /*
3017  * Get owner ID and working copy of existing ACL. If there's no ACL,
3018  * substitute the proper default.
3019  */
3020  ownerId = pg_tablespace_tuple->spcowner;
3021  aclDatum = heap_getattr(tuple, Anum_pg_tablespace_spcacl,
3022  RelationGetDescr(relation), &isNull);
3023  if (isNull)
3024  {
3025  old_acl = acldefault(OBJECT_TABLESPACE, ownerId);
3026  /* There are no old member roles according to the catalogs */
3027  noldmembers = 0;
3028  oldmembers = NULL;
3029  }
3030  else
3031  {
3032  old_acl = DatumGetAclPCopy(aclDatum);
3033  /* Get the roles mentioned in the existing ACL */
3034  noldmembers = aclmembers(old_acl, &oldmembers);
3035  }
3036 
3037  /* Determine ID to do the grant as, and available grant options */
3038  select_best_grantor(GetUserId(), istmt->privileges,
3039  old_acl, ownerId,
3040  &grantorId, &avail_goptions);
3041 
3042  /*
3043  * Restrict the privileges to what we can actually grant, and emit the
3044  * standards-mandated warning and error messages.
3045  */
3046  this_privileges =
3047  restrict_and_check_grant(istmt->is_grant, avail_goptions,
3048  istmt->all_privs, istmt->privileges,
3049  tblId, grantorId, OBJECT_TABLESPACE,
3050  NameStr(pg_tablespace_tuple->spcname),
3051  0, NULL);
3052 
3053  /*
3054  * Generate new ACL.
3055  */
3056  new_acl = merge_acl_with_grant(old_acl, istmt->is_grant,
3057  istmt->grant_option, istmt->behavior,
3058  istmt->grantees, this_privileges,
3059  grantorId, ownerId);
3060 
3061  /*
3062  * We need the members of both old and new ACLs so we can correct the
3063  * shared dependency information.
3064  */
3065  nnewmembers = aclmembers(new_acl, &newmembers);
3066 
3067  /* finished building new ACL value, now insert it */
3068  MemSet(values, 0, sizeof(values));
3069  MemSet(nulls, false, sizeof(nulls));
3070  MemSet(replaces, false, sizeof(replaces));
3071 
3072  replaces[Anum_pg_tablespace_spcacl - 1] = true;
3073  values[Anum_pg_tablespace_spcacl - 1] = PointerGetDatum(new_acl);
3074 
3075  newtuple = heap_modify_tuple(tuple, RelationGetDescr(relation), values,
3076  nulls, replaces);
3077 
3078  CatalogTupleUpdate(relation, &newtuple->t_self, newtuple);
3079 
3080  /* Update the shared dependency ACL info */
3081  updateAclDependencies(TableSpaceRelationId, tblId, 0,
3082  ownerId,
3083  noldmembers, oldmembers,
3084  nnewmembers, newmembers);
3085 
3086  ReleaseSysCache(tuple);
3087  pfree(new_acl);
3088 
3089  /* prevent error when processing duplicate objects */
3091  }
3092 
3093  table_close(relation, RowExclusiveLock);
3094 }
void updateAclDependencies(Oid classId, Oid objectId, int32 objsubId, Oid ownerId, int noldmembers, Oid *oldmembers, int nnewmembers, Oid *newmembers)
Definition: pg_shdepend.c:482
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
#define RelationGetDescr(relation)
Definition: rel.h:483
Oid GetUserId(void)
Definition: miscinit.c:478
#define PointerGetDatum(X)
Definition: postgres.h:600
Acl * acldefault(ObjectType objtype, Oid ownerId)
Definition: acl.c:734
#define MemSet(start, val, len)
Definition: c.h:1008
#define ACL_ALL_RIGHTS_TABLESPACE
Definition: acl.h:166
unsigned int Oid
Definition: postgres_ext.h:31
static Acl * merge_acl_with_grant(Acl *old_acl, bool is_grant, bool grant_option, DropBehavior behavior, List *grantees, AclMode privileges, Oid grantorId, Oid ownerId)
Definition: aclchk.c:152
uint32 AclMode
Definition: parsenodes.h:80
void pfree(void *pointer)
Definition: mcxt.c:1169
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
#define ACL_NO_RIGHTS
Definition: parsenodes.h:96
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
FormData_pg_tablespace * Form_pg_tablespace
Definition: pg_tablespace.h:48
#define heap_getattr(tup, attnum, tupleDesc, isnull)
Definition: htup_details.h:761
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
uintptr_t Datum
Definition: postgres.h:411
void CommandCounterIncrement(void)
Definition: xact.c:1021
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
int aclmembers(const Acl *acl, Oid **roleids)
Definition: acl.c:1458
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:301
void select_best_grantor(Oid roleId, AclMode privileges, const Acl *acl, Oid ownerId, Oid *grantorId, AclMode *grantOptions)
Definition: acl.c:5018
static Datum values[MAXATTR]
Definition: bootstrap.c:166
#define elog(elevel,...)
Definition: elog.h:232
#define NameStr(name)
Definition: c.h:681
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *replValues, bool *replIsnull, bool *doReplace)
Definition: heaptuple.c:1113
static AclMode restrict_and_check_grant(bool is_grant, AclMode avail_goptions, bool all_privs, AclMode privileges, Oid objectId, Oid grantorId, ObjectType objtype, const char *objname, AttrNumber att_number, const char *colname)
Definition: aclchk.c:211
#define lfirst_oid(lc)
Definition: pg_list.h:171
#define DatumGetAclPCopy(X)
Definition: acl.h:121

◆ ExecGrant_Type()

static void ExecGrant_Type ( InternalGrant grantStmt)
static

Definition at line 3097 of file aclchk.c.

References ACL_ALL_RIGHTS_TYPE, ACL_NO_RIGHTS, acldefault(), aclmembers(), InternalGrant::all_privs, InternalGrant::behavior, CatalogTupleUpdate(), CommandCounterIncrement(), DatumGetAclPCopy, elog, ereport, errcode(), errhint(), errmsg(), ERROR, GETSTRUCT, GetUserId(), InternalGrant::grant_option, InternalGrant::grantees, heap_getattr, heap_modify_tuple(), HeapTupleIsValid, InternalGrant::is_grant, lfirst_oid, MemSet, merge_acl_with_grant(), NameStr, OBJECT_DOMAIN, OBJECT_TYPE, ObjectIdGetDatum, InternalGrant::objects, InternalGrant::objtype, pfree(), PointerGetDatum, InternalGrant::privileges, recordExtensionInitPriv(), RelationGetDescr, ReleaseSysCache(), restrict_and_check_grant(), RowExclusiveLock, SearchSysCache1(), select_best_grantor(), HeapTupleData::t_self, table_close(), table_open(), TYPEOID, updateAclDependencies(), and values.

Referenced by ExecGrantStmt_oids().

3098 {
3099  Relation relation;
3100  ListCell *cell;
3101 
3102  if (istmt->all_privs && istmt->privileges == ACL_NO_RIGHTS)
3103  istmt->privileges = ACL_ALL_RIGHTS_TYPE;
3104 
3105  relation = table_open(TypeRelationId, RowExclusiveLock);
3106 
3107  foreach(cell, istmt->objects)
3108  {
3109  Oid typId = lfirst_oid(cell);
3110  Form_pg_type pg_type_tuple;
3111  Datum aclDatum;
3112  bool isNull;
3113  AclMode avail_goptions;
3114  AclMode this_privileges;
3115  Acl *old_acl;
3116  Acl *new_acl;
3117  Oid grantorId;
3118  Oid ownerId;
3119  HeapTuple newtuple;
3120  Datum values[Natts_pg_type];
3121  bool nulls[Natts_pg_type];
3122  bool replaces[Natts_pg_type];
3123  int noldmembers;
3124  int nnewmembers;
3125  Oid *oldmembers;
3126  Oid *newmembers;
3127  HeapTuple tuple;
3128 
3129  /* Search syscache for pg_type */
3130  tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typId));
3131  if (!HeapTupleIsValid(tuple))
3132  elog(ERROR, "cache lookup failed for type %u", typId);
3133 
3134  pg_type_tuple = (Form_pg_type) GETSTRUCT(tuple);
3135 
3136  if (IsTrueArrayType(pg_type_tuple))
3137  ereport(ERROR,
3138  (errcode(ERRCODE_INVALID_GRANT_OPERATION),
3139  errmsg("cannot set privileges of array types"),
3140  errhint("Set the privileges of the element type instead.")));
3141 
3142  /* Used GRANT DOMAIN on a non-domain? */
3143  if (istmt->objtype == OBJECT_DOMAIN &&
3144  pg_type_tuple->typtype != TYPTYPE_DOMAIN)
3145  ereport(ERROR,
3146  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
3147  errmsg("\"%s\" is not a domain",
3148  NameStr(pg_type_tuple->typname))));
3149 
3150  /*
3151  * Get owner ID and working copy of existing ACL. If there's no ACL,
3152  * substitute the proper default.
3153  */
3154  ownerId = pg_type_tuple->typowner;
3155  aclDatum = heap_getattr(tuple, Anum_pg_type_typacl,
3156  RelationGetDescr(relation), &isNull);
3157  if (isNull)
3158  {
3159  old_acl = acldefault(istmt->objtype, ownerId);
3160  /* There are no old member roles according to the catalogs */
3161  noldmembers = 0;
3162  oldmembers = NULL;
3163  }
3164  else
3165  {
3166  old_acl = DatumGetAclPCopy(aclDatum);
3167  /* Get the roles mentioned in the existing ACL */
3168  noldmembers = aclmembers(old_acl, &oldmembers);
3169  }
3170 
3171  /* Determine ID to do the grant as, and available grant options */
3172  select_best_grantor(GetUserId(), istmt->privileges,
3173  old_acl, ownerId,
3174  &grantorId, &avail_goptions);
3175 
3176  /*
3177  * Restrict the privileges to what we can actually grant, and emit the
3178  * standards-mandated warning and error messages.
3179  */
3180  this_privileges =
3181  restrict_and_check_grant(istmt->is_grant, avail_goptions,
3182  istmt->all_privs, istmt->privileges,
3183  typId, grantorId, OBJECT_TYPE,
3184  NameStr(pg_type_tuple->typname),
3185  0, NULL);
3186 
3187  /*
3188  * Generate new ACL.
3189  */
3190  new_acl = merge_acl_with_grant(old_acl, istmt->is_grant,
3191  istmt->grant_option, istmt->behavior,
3192  istmt->grantees, this_privileges,
3193  grantorId, ownerId);
3194 
3195  /*
3196  * We need the members of both old and new ACLs so we can correct the
3197  * shared dependency information.
3198  */
3199  nnewmembers = aclmembers(new_acl, &newmembers);
3200 
3201  /* finished building new ACL value, now insert it */
3202  MemSet(values, 0, sizeof(values));
3203  MemSet(nulls, false, sizeof(nulls));
3204  MemSet(replaces, false, sizeof(replaces));
3205 
3206  replaces[Anum_pg_type_typacl - 1] = true;
3207  values[Anum_pg_type_typacl - 1] = PointerGetDatum(new_acl);
3208 
3209  newtuple = heap_modify_tuple(tuple, RelationGetDescr(relation), values,
3210  nulls, replaces);
3211 
3212  CatalogTupleUpdate(relation, &newtuple->t_self, newtuple);
3213 
3214  /* Update initial privileges for extensions */
3215  recordExtensionInitPriv(typId, TypeRelationId, 0, new_acl);
3216 
3217  /* Update the shared dependency ACL info */
3218  updateAclDependencies(TypeRelationId, typId, 0,
3219  ownerId,
3220  noldmembers, oldmembers,
3221  nnewmembers, newmembers);
3222 
3223  ReleaseSysCache(tuple);
3224  pfree(new_acl);
3225 
3226  /* prevent error when processing duplicate objects */
3228  }
3229 
3230  table_close(relation, RowExclusiveLock);
3231 }
void updateAclDependencies(Oid classId, Oid objectId, int32 objsubId, Oid ownerId, int noldmembers, Oid *oldmembers, int nnewmembers, Oid *newmembers)
Definition: pg_shdepend.c:482
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
int errhint(const char *fmt,...)
Definition: elog.c:1156
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
#define RelationGetDescr(relation)
Definition: rel.h:483
Oid GetUserId(void)
Definition: miscinit.c:478
#define PointerGetDatum(X)
Definition: postgres.h:600
int errcode(int sqlerrcode)
Definition: elog.c:698
Acl * acldefault(ObjectType objtype, Oid ownerId)
Definition: acl.c:734
#define MemSet(start, val, len)
Definition: c.h:1008
unsigned int Oid
Definition: postgres_ext.h:31
static Acl * merge_acl_with_grant(Acl *old_acl, bool is_grant, bool grant_option, DropBehavior behavior, List *grantees, AclMode privileges, Oid grantorId, Oid ownerId)
Definition: aclchk.c:152
uint32 AclMode
Definition: parsenodes.h:80
void pfree(void *pointer)
Definition: mcxt.c:1169
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
#define ACL_NO_RIGHTS
Definition: parsenodes.h:96
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
#define ACL_ALL_RIGHTS_TYPE
Definition: acl.h:167
static void recordExtensionInitPriv(Oid objoid, Oid classoid, int objsubid, Acl *new_acl)
Definition: aclchk.c:6013
#define heap_getattr(tup, attnum, tupleDesc, isnull)
Definition: htup_details.h:761
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
uintptr_t Datum
Definition: postgres.h:411
void CommandCounterIncrement(void)
Definition: xact.c:1021
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
#define ereport(elevel,...)
Definition: elog.h:157
int aclmembers(const Acl *acl, Oid **roleids)
Definition: acl.c:1458
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:301
FormData_pg_type * Form_pg_type
Definition: pg_type.h:261
void select_best_grantor(Oid roleId, AclMode privileges, const Acl *acl, Oid ownerId, Oid *grantorId, AclMode *grantOptions)
Definition: acl.c:5018
static Datum values[MAXATTR]
Definition: bootstrap.c:166
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
#define NameStr(name)
Definition: c.h:681
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *replValues, bool *replIsnull, bool *doReplace)
Definition: heaptuple.c:1113
static AclMode restrict_and_check_grant(bool is_grant, AclMode avail_goptions, bool all_privs, AclMode privileges, Oid objectId, Oid grantorId, ObjectType objtype, const char *objname, AttrNumber att_number, const char *colname)
Definition: aclchk.c:211
#define lfirst_oid(lc)
Definition: pg_list.h:171
#define DatumGetAclPCopy(X)
Definition: acl.h:121

◆ ExecGrantStmt_oids()

static void ExecGrantStmt_oids ( InternalGrant istmt)
static

Definition at line 564 of file aclchk.c.

References elog, ERROR, EventTriggerCollectGrant(), EventTriggerSupportsObjectType(), ExecGrant_Database(), ExecGrant_Fdw(), ExecGrant_ForeignServer(), ExecGrant_Function(), ExecGrant_Language(), ExecGrant_Largeobject(), ExecGrant_Namespace(), ExecGrant_Relation(), ExecGrant_Tablespace(), ExecGrant_Type(), OBJECT_DATABASE, OBJECT_DOMAIN, OBJECT_FDW, OBJECT_FOREIGN_SERVER, OBJECT_FUNCTION, OBJECT_LANGUAGE, OBJECT_LARGEOBJECT, OBJECT_PROCEDURE, OBJECT_ROUTINE, OBJECT_SCHEMA, OBJECT_SEQUENCE, OBJECT_TABLE, OBJECT_TABLESPACE, OBJECT_TYPE, and InternalGrant::objtype.

Referenced by ExecuteGrantStmt(), and RemoveRoleFromObjectACL().

565 {
566  switch (istmt->objtype)
567  {
568  case OBJECT_TABLE:
569  case OBJECT_SEQUENCE:
570  ExecGrant_Relation(istmt);
571  break;
572  case OBJECT_DATABASE:
573  ExecGrant_Database(istmt);
574  break;
575  case OBJECT_DOMAIN:
576  case OBJECT_TYPE:
577  ExecGrant_Type(istmt);
578  break;
579  case OBJECT_FDW:
580  ExecGrant_Fdw(istmt);
581  break;
584  break;
585  case OBJECT_FUNCTION:
586  case OBJECT_PROCEDURE:
587  case OBJECT_ROUTINE:
588  ExecGrant_Function(istmt);
589  break;
590  case OBJECT_LANGUAGE:
591  ExecGrant_Language(istmt);
592  break;
593  case OBJECT_LARGEOBJECT:
594  ExecGrant_Largeobject(istmt);
595  break;
596  case OBJECT_SCHEMA:
597  ExecGrant_Namespace(istmt);
598  break;
599  case OBJECT_TABLESPACE:
600  ExecGrant_Tablespace(istmt);
601  break;
602  default:
603  elog(ERROR, "unrecognized GrantStmt.objtype: %d",
604  (int) istmt->objtype);
605  }
606 
607  /*
608  * Pass the info to event triggers about the just-executed GRANT. Note
609  * that we prefer to do it after actually executing it, because that gives
610  * the functions a chance to adjust the istmt with privileges actually
611  * granted.
612  */
615 }
static void ExecGrant_Language(InternalGrant *grantStmt)
Definition: aclchk.c:2582
static void ExecGrant_Function(InternalGrant *grantStmt)
Definition: aclchk.c:2459
static void ExecGrant_Tablespace(InternalGrant *grantStmt)
Definition: aclchk.c:2977
static void ExecGrant_Namespace(InternalGrant *grantStmt)
Definition: aclchk.c:2853
static void ExecGrant_Type(InternalGrant *grantStmt)
Definition: aclchk.c:3097
static void ExecGrant_Database(InternalGrant *grantStmt)
Definition: aclchk.c:2087
#define ERROR
Definition: elog.h:46
static void ExecGrant_Fdw(InternalGrant *grantStmt)
Definition: aclchk.c:2207
static void ExecGrant_Largeobject(InternalGrant *grantStmt)
Definition: aclchk.c:2713
static void ExecGrant_Relation(InternalGrant *grantStmt)
Definition: aclchk.c:1755
bool EventTriggerSupportsObjectType(ObjectType obtype)
ObjectType objtype
#define elog(elevel,...)
Definition: elog.h:232
void EventTriggerCollectGrant(InternalGrant *istmt)
static void ExecGrant_ForeignServer(InternalGrant *grantStmt)
Definition: aclchk.c:2334

◆ ExecuteGrantStmt()

void ExecuteGrantStmt ( GrantStmt stmt)

Definition at line 359 of file aclchk.c.

References ACL_ALL_RIGHTS_DATABASE, ACL_ALL_RIGHTS_FDW, ACL_ALL_RIGHTS_FOREIGN_SERVER, ACL_ALL_RIGHTS_FUNCTION, ACL_ALL_RIGHTS_LANGUAGE, ACL_ALL_RIGHTS_LARGEOBJECT, ACL_ALL_RIGHTS_RELATION, ACL_ALL_RIGHTS_SCHEMA, ACL_ALL_RIGHTS_SEQUENCE, ACL_ALL_RIGHTS_TABLESPACE, ACL_ALL_RIGHTS_TYPE, ACL_ID_PUBLIC, ACL_NO_RIGHTS, ACL_TARGET_ALL_IN_SCHEMA, ACL_TARGET_OBJECT, InternalGrant::all_privs, InternalGrant::behavior, GrantStmt::behavior, InternalGrant::col_privs, AccessPriv::cols, elog, ereport, errcode(), errmsg(), ERROR, ExecGrantStmt_oids(), get_rolespec_oid(), gettext_noop, GetUserId(), InternalGrant::grant_option, GrantStmt::grant_option, InternalGrant::grantees, GrantStmt::grantees, GrantStmt::grantor, InternalGrant::is_grant, GrantStmt::is_grant, lappend(), lappend_oid(), lfirst, NIL, OBJECT_DATABASE, OBJECT_DOMAIN, OBJECT_FDW, OBJECT_FOREIGN_SERVER, OBJECT_FUNCTION, OBJECT_LANGUAGE, OBJECT_LARGEOBJECT, OBJECT_PROCEDURE, OBJECT_ROUTINE, OBJECT_SCHEMA, OBJECT_SEQUENCE, OBJECT_TABLE, OBJECT_TABLESPACE, OBJECT_TYPE, objectNamesToOids(), InternalGrant::objects, GrantStmt::objects, objectsInSchemaToOids(), InternalGrant::objtype, GrantStmt::objtype, AccessPriv::priv_name, privilege_to_string(), InternalGrant::privileges, GrantStmt::privileges, ROLESPEC_PUBLIC, RoleSpec::roletype, string_to_privilege(), and GrantStmt::targtype.

Referenced by ProcessUtilitySlow(), and standard_ProcessUtility().

360 {
361  InternalGrant istmt;
362  ListCell *cell;
363  const char *errormsg;
364  AclMode all_privileges;
365 
366  if (stmt->grantor)
367  {
368  Oid grantor;
369 
370  grantor = get_rolespec_oid(stmt->grantor, false);
371 
372  /*
373  * Currently, this clause is only for SQL compatibility, not very
374  * interesting otherwise.
375  */
376  if (grantor != GetUserId())
377  ereport(ERROR,
378  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
379  errmsg("grantor must be current user")));
380  }
381 
382  /*
383  * Turn the regular GrantStmt into the InternalGrant form.
384  */
385  istmt.is_grant = stmt->is_grant;
386  istmt.objtype = stmt->objtype;
387 
388  /* Collect the OIDs of the target objects */
389  switch (stmt->targtype)
390  {
391  case ACL_TARGET_OBJECT:
392  istmt.objects = objectNamesToOids(stmt->objtype, stmt->objects);
393  break;
395  istmt.objects = objectsInSchemaToOids(stmt->objtype, stmt->objects);
396  break;
397  /* ACL_TARGET_DEFAULTS should not be seen here */
398  default:
399  elog(ERROR, "unrecognized GrantStmt.targtype: %d",
400  (int) stmt->targtype);
401  }
402 
403  /* all_privs to be filled below */
404  /* privileges to be filled below */
405  istmt.col_privs = NIL; /* may get filled below */
406  istmt.grantees = NIL; /* filled below */
407  istmt.grant_option = stmt->grant_option;
408  istmt.behavior = stmt->behavior;
409 
410  /*
411  * Convert the RoleSpec list into an Oid list. Note that at this point we
412  * insert an ACL_ID_PUBLIC into the list if appropriate, so downstream
413  * there shouldn't be any additional work needed to support this case.
414  */
415  foreach(cell, stmt->grantees)
416  {
417  RoleSpec *grantee = (RoleSpec *) lfirst(cell);
418  Oid grantee_uid;
419 
420  switch (grantee->roletype)
421  {
422  case ROLESPEC_PUBLIC:
423  grantee_uid = ACL_ID_PUBLIC;
424  break;
425  default:
426  grantee_uid = get_rolespec_oid(grantee, false);
427  break;
428  }
429  istmt.grantees = lappend_oid(istmt.grantees, grantee_uid);
430  }
431 
432  /*
433  * Convert stmt->privileges, a list of AccessPriv nodes, into an AclMode
434  * bitmask. Note: objtype can't be OBJECT_COLUMN.
435  */
436  switch (stmt->objtype)
437  {
438  case OBJECT_TABLE:
439 
440  /*
441  * Because this might be a sequence, we test both relation and
442  * sequence bits, and later do a more limited test when we know
443  * the object type.
444  */
446  errormsg = gettext_noop("invalid privilege type %s for relation");
447  break;
448  case OBJECT_SEQUENCE:
449  all_privileges = ACL_ALL_RIGHTS_SEQUENCE;
450  errormsg = gettext_noop("invalid privilege type %s for sequence");
451  break;
452  case OBJECT_DATABASE:
453  all_privileges = ACL_ALL_RIGHTS_DATABASE;
454  errormsg = gettext_noop("invalid privilege type %s for database");
455  break;
456  case OBJECT_DOMAIN:
457  all_privileges = ACL_ALL_RIGHTS_TYPE;
458  errormsg = gettext_noop("invalid privilege type %s for domain");
459  break;
460  case OBJECT_FUNCTION:
461  all_privileges = ACL_ALL_RIGHTS_FUNCTION;
462  errormsg = gettext_noop("invalid privilege type %s for function");
463  break;
464  case OBJECT_LANGUAGE:
465  all_privileges = ACL_ALL_RIGHTS_LANGUAGE;
466  errormsg = gettext_noop("invalid privilege type %s for language");
467  break;
468  case OBJECT_LARGEOBJECT:
469  all_privileges = ACL_ALL_RIGHTS_LARGEOBJECT;
470  errormsg = gettext_noop("invalid privilege type %s for large object");
471  break;
472  case OBJECT_SCHEMA:
473  all_privileges = ACL_ALL_RIGHTS_SCHEMA;
474  errormsg = gettext_noop("invalid privilege type %s for schema");
475  break;
476  case OBJECT_PROCEDURE:
477  all_privileges = ACL_ALL_RIGHTS_FUNCTION;
478  errormsg = gettext_noop("invalid privilege type %s for procedure");
479  break;
480  case OBJECT_ROUTINE:
481  all_privileges = ACL_ALL_RIGHTS_FUNCTION;
482  errormsg = gettext_noop("invalid privilege type %s for routine");
483  break;
484  case OBJECT_TABLESPACE:
485  all_privileges = ACL_ALL_RIGHTS_TABLESPACE;
486  errormsg = gettext_noop("invalid privilege type %s for tablespace");
487  break;
488  case OBJECT_TYPE:
489  all_privileges = ACL_ALL_RIGHTS_TYPE;
490  errormsg = gettext_noop("invalid privilege type %s for type");
491  break;
492  case OBJECT_FDW:
493  all_privileges = ACL_ALL_RIGHTS_FDW;
494  errormsg = gettext_noop("invalid privilege type %s for foreign-data wrapper");
495  break;
497  all_privileges = ACL_ALL_RIGHTS_FOREIGN_SERVER;
498  errormsg = gettext_noop("invalid privilege type %s for foreign server");
499  break;
500  default:
501  elog(ERROR, "unrecognized GrantStmt.objtype: %d",
502  (int) stmt->objtype);
503  /* keep compiler quiet */
504  all_privileges = ACL_NO_RIGHTS;
505  errormsg = NULL;
506  }
507 
508  if (stmt->privileges == NIL)
509  {
510  istmt.all_privs = true;
511 
512  /*
513  * will be turned into ACL_ALL_RIGHTS_* by the internal routines
514  * depending on the object type
515  */
516  istmt.privileges = ACL_NO_RIGHTS;
517  }
518  else
519  {
520  istmt.all_privs = false;
521  istmt.privileges = ACL_NO_RIGHTS;
522 
523  foreach(cell, stmt->privileges)
524  {
525  AccessPriv *privnode = (AccessPriv *) lfirst(cell);
526  AclMode priv;
527 
528  /*
529  * If it's a column-level specification, we just set it aside in
530  * col_privs for the moment; but insist it's for a relation.
531  */
532  if (privnode->cols)
533  {
534  if (stmt->objtype != OBJECT_TABLE)
535  ereport(ERROR,
536  (errcode(ERRCODE_INVALID_GRANT_OPERATION),
537  errmsg("column privileges are only valid for relations")));
538  istmt.col_privs = lappend(istmt.col_privs, privnode);
539  continue;
540  }
541 
542  if (privnode->priv_name == NULL) /* parser mistake? */
543  elog(ERROR, "AccessPriv node must specify privilege or columns");
544  priv = string_to_privilege(privnode->priv_name);
545 
546  if (priv & ~((AclMode) all_privileges))
547  ereport(ERROR,
548  (errcode(ERRCODE_INVALID_GRANT_OPERATION),
549  errmsg(errormsg, privilege_to_string(priv))));
550 
551  istmt.privileges |= priv;
552  }
553  }
554 
555  ExecGrantStmt_oids(&istmt);
556 }
#define NIL
Definition: pg_list.h:65
#define ACL_ALL_RIGHTS_FUNCTION
Definition: acl.h:162
Oid GetUserId(void)
Definition: miscinit.c:478
static AclMode string_to_privilege(const char *privname)
Definition: aclchk.c:3235
AclMode privileges
bool grant_option
Definition: parsenodes.h:2012
#define gettext_noop(x)
Definition: c.h:1197
int errcode(int sqlerrcode)
Definition: elog.c:698
#define ACL_ALL_RIGHTS_TABLESPACE
Definition: acl.h:166
List * cols
Definition: parsenodes.h:2043
unsigned int Oid
Definition: postgres_ext.h:31
List * lappend_oid(List *list, Oid datum)
Definition: list.c:372
#define ACL_ALL_RIGHTS_LANGUAGE
Definition: acl.h:163
uint32 AclMode
Definition: parsenodes.h:80
#define ERROR
Definition: elog.h:46
#define ACL_NO_RIGHTS
Definition: parsenodes.h:96
#define ACL_ALL_RIGHTS_SCHEMA
Definition: acl.h:165
#define ACL_ALL_RIGHTS_TYPE
Definition: acl.h:167
List * lappend(List *list, void *datum)
Definition: list.c:336
bool is_grant
Definition: parsenodes.h:2004
static void ExecGrantStmt_oids(InternalGrant *istmt)
Definition: aclchk.c:564
static List * objectNamesToOids(ObjectType objtype, List *objnames)
Definition: aclchk.c:628
RoleSpecType roletype
Definition: parsenodes.h:339
Oid get_rolespec_oid(const RoleSpec *role, bool missing_ok)
Definition: acl.c:5128
#define ACL_ALL_RIGHTS_SEQUENCE
Definition: acl.h:158
List * privileges
Definition: parsenodes.h:2009
static List * objectsInSchemaToOids(ObjectType objtype, List *nspnames)
Definition: aclchk.c:777
#define ereport(elevel,...)
Definition: elog.h:157
RoleSpec * grantor
Definition: parsenodes.h:2013
#define ACL_ALL_RIGHTS_LARGEOBJECT
Definition: acl.h:164
DropBehavior behavior
DropBehavior behavior
Definition: parsenodes.h:2014
#define lfirst(lc)
Definition: pg_list.h:169
List * objects
Definition: parsenodes.h:2007
ObjectType objtype
#define ACL_ALL_RIGHTS_DATABASE
Definition: acl.h:159
#define ACL_ALL_RIGHTS_FOREIGN_SERVER
Definition: acl.h:161
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
#define ACL_ALL_RIGHTS_RELATION
Definition: acl.h:157
ObjectType objtype
Definition: parsenodes.h:2006
#define ACL_ID_PUBLIC
Definition: acl.h:46
#define ACL_ALL_RIGHTS_FDW
Definition: acl.h:160
static const char * privilege_to_string(AclMode privilege)
Definition: aclchk.c:3272
List * grantees
Definition: parsenodes.h:2011
GrantTargetType targtype
Definition: parsenodes.h:2005
char * priv_name
Definition: parsenodes.h:2042

◆ expand_all_col_privileges()

static void expand_all_col_privileges ( Oid  table_oid,
Form_pg_class  classForm,
AclMode  this_privileges,
AclMode col_privileges,
int  num_col_privileges 
)
static

Definition at line 1561 of file aclchk.c.

References Assert, ATTNUM, elog, ERROR, FirstLowInvalidHeapAttributeNumber, GETSTRUCT, HeapTupleIsValid, Int16GetDatum, InvalidAttrNumber, ObjectIdGetDatum, ReleaseSysCache(), and SearchSysCache2().

Referenced by ExecGrant_Relation().

1565 {
1566  AttrNumber curr_att;
1567 
1568  Assert(classForm->relnatts - FirstLowInvalidHeapAttributeNumber < num_col_privileges);
1569  for (curr_att = FirstLowInvalidHeapAttributeNumber + 1;
1570  curr_att <= classForm->relnatts;
1571  curr_att++)
1572  {
1573  HeapTuple attTuple;
1574  bool isdropped;
1575 
1576  if (curr_att == InvalidAttrNumber)
1577  continue;
1578 
1579  /* Views don't have any system columns at all */
1580  if (classForm->relkind == RELKIND_VIEW && curr_att < 0)
1581  continue;
1582 
1583  attTuple = SearchSysCache2(ATTNUM,
1584  ObjectIdGetDatum(table_oid),
1585  Int16GetDatum(curr_att));
1586  if (!HeapTupleIsValid(attTuple))
1587  elog(ERROR, "cache lookup failed for attribute %d of relation %u",
1588  curr_att, table_oid);
1589 
1590  isdropped = ((Form_pg_attribute) GETSTRUCT(attTuple))->attisdropped;
1591 
1592  ReleaseSysCache(attTuple);
1593 
1594  /* ignore dropped columns */
1595  if (isdropped)
1596  continue;
1597 
1598  col_privileges[curr_att - FirstLowInvalidHeapAttributeNumber] |= this_privileges;
1599  }
1600 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
#define Int16GetDatum(X)
Definition: postgres.h:495
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:203
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:804
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:1138
#define InvalidAttrNumber
Definition: attnum.h:23
#define elog(elevel,...)
Definition: elog.h:232
int16 AttrNumber
Definition: attnum.h:21

◆ expand_col_privileges()

static void expand_col_privileges ( List colnames,
Oid  table_oid,
AclMode  this_privileges,
AclMode col_privileges,
int  num_col_privileges 
)
static

Definition at line 1528 of file aclchk.c.

References attnum, elog, ereport, errcode(), errmsg(), ERROR, FirstLowInvalidHeapAttributeNumber, get_attnum(), get_rel_name(), InvalidAttrNumber, lfirst, and strVal.

Referenced by ExecGrant_Relation().

1532 {
1533  ListCell *cell;
1534 
1535  foreach(cell, colnames)
1536  {
1537  char *colname = strVal(lfirst(cell));
1539 
1540  attnum = get_attnum(table_oid, colname);
1541  if (attnum == InvalidAttrNumber)
1542  ereport(ERROR,
1543  (errcode(ERRCODE_UNDEFINED_COLUMN),
1544  errmsg("column \"%s\" of relation \"%s\" does not exist",
1545  colname, get_rel_name(table_oid))));
1547  if (attnum <= 0 || attnum >= num_col_privileges)
1548  elog(ERROR, "column number out of range"); /* safety check */
1549  col_privileges[attnum] |= this_privileges;
1550  }
1551 }
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:698
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
#define ERROR
Definition: elog.h:46
AttrNumber get_attnum(Oid relid, const char *attname)
Definition: lsyscache.c:856
int16 attnum
Definition: pg_attribute.h:83
#define ereport(elevel,...)
Definition: elog.h:157
#define lfirst(lc)
Definition: pg_list.h:169
#define InvalidAttrNumber
Definition: attnum.h:23
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1899
int16 AttrNumber
Definition: attnum.h:21

◆ get_default_acl_internal()

static Acl* get_default_acl_internal ( Oid  roleId,
Oid  nsp_oid,
char  objtype 
)
static

Definition at line 5491 of file aclchk.c.

References CharGetDatum, DatumGetAclPCopy, DEFACLROLENSPOBJ, HeapTupleIsValid, ObjectIdGetDatum, ReleaseSysCache(), SearchSysCache3(), and SysCacheGetAttr().

Referenced by get_user_default_acl().

5492 {
5493  Acl *result = NULL;
5494  HeapTuple tuple;
5495 
5497  ObjectIdGetDatum(roleId),
5498  ObjectIdGetDatum(nsp_oid),
5499  CharGetDatum(objtype));
5500 
5501  if (HeapTupleIsValid(tuple))
5502  {
5503  Datum aclDatum;
5504  bool isNull;
5505 
5506  aclDatum = SysCacheGetAttr(DEFACLROLENSPOBJ, tuple,
5507  Anum_pg_default_acl_defaclacl,
5508  &isNull);
5509  if (!isNull)
5510  result = DatumGetAclPCopy(aclDatum);
5511  ReleaseSysCache(tuple);
5512  }
5513 
5514  return result;
5515 }
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
HeapTuple SearchSysCache3(int cacheId, Datum key1, Datum key2, Datum key3)
Definition: syscache.c:1149
uintptr_t Datum
Definition: postgres.h:411
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1388
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define CharGetDatum(X)
Definition: postgres.h:460
#define DatumGetAclPCopy(X)
Definition: acl.h:121

◆ get_user_default_acl()

Acl* get_user_default_acl ( ObjectType  objtype,
Oid  ownerId,
Oid  nsp_oid 
)

Definition at line 5526 of file aclchk.c.

References acldefault(), aclequal(), aclitemsort(), aclmerge(), get_default_acl_internal(), InvalidOid, IsBootstrapProcessingMode, OBJECT_FUNCTION, OBJECT_SCHEMA, OBJECT_SEQUENCE, OBJECT_TABLE, and OBJECT_TYPE.

Referenced by heap_create_with_catalog(), NamespaceCreate(), ProcedureCreate(), and TypeCreate().

5527 {
5528  Acl *result;
5529  Acl *glob_acl;
5530  Acl *schema_acl;
5531  Acl *def_acl;
5532  char defaclobjtype;
5533 
5534  /*
5535  * Use NULL during bootstrap, since pg_default_acl probably isn't there
5536  * yet.
5537  */
5539  return NULL;
5540 
5541  /* Check if object type is supported in pg_default_acl */
5542  switch (objtype)
5543  {
5544  case OBJECT_TABLE:
5545  defaclobjtype = DEFACLOBJ_RELATION;
5546  break;
5547 
5548  case OBJECT_SEQUENCE:
5549  defaclobjtype = DEFACLOBJ_SEQUENCE;
5550  break;
5551 
5552  case OBJECT_FUNCTION:
5553  defaclobjtype = DEFACLOBJ_FUNCTION;
5554  break;
5555 
5556  case OBJECT_TYPE:
5557  defaclobjtype = DEFACLOBJ_TYPE;
5558  break;
5559 
5560  case OBJECT_SCHEMA:
5561  defaclobjtype = DEFACLOBJ_NAMESPACE;
5562  break;
5563 
5564  default:
5565  return NULL;
5566  }
5567 
5568  /* Look up the relevant pg_default_acl entries */
5569  glob_acl = get_default_acl_internal(ownerId, InvalidOid, defaclobjtype);
5570  schema_acl = get_default_acl_internal(ownerId, nsp_oid, defaclobjtype);
5571 
5572  /* Quick out if neither entry exists */
5573  if (glob_acl == NULL && schema_acl == NULL)
5574  return NULL;
5575 
5576  /* We need to know the hard-wired default value, too */
5577  def_acl = acldefault(objtype, ownerId);
5578 
5579  /* If there's no global entry, substitute the hard-wired default */
5580  if (glob_acl == NULL)
5581  glob_acl = def_acl;
5582 
5583  /* Merge in any per-schema privileges */
5584  result = aclmerge(glob_acl, schema_acl, ownerId);
5585 
5586  /*
5587  * For efficiency, we want to return NULL if the result equals default.
5588  * This requires sorting both arrays to get an accurate comparison.
5589  */
5590  aclitemsort(result);
5591  aclitemsort(def_acl);
5592  if (aclequal(result, def_acl))
5593  result = NULL;
5594 
5595  return result;
5596 }
Acl * acldefault(ObjectType objtype, Oid ownerId)
Definition: acl.c:734
static Acl * get_default_acl_internal(Oid roleId, Oid nsp_oid, char objtype)
Definition: aclchk.c:5491
bool aclequal(const Acl *left_acl, const Acl *right_acl)
Definition: acl.c:495
#define InvalidOid
Definition: postgres_ext.h:36
Acl * aclmerge(const Acl *left_acl, const Acl *right_acl, Oid ownerId)
Definition: acl.c:437
void aclitemsort(Acl *acl)
Definition: acl.c:481
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:394

◆ getRelationsInNamespace()

static List * getRelationsInNamespace ( Oid  namespaceId,
char  relkind 
)
static

Definition at line 866 of file aclchk.c.

References AccessShareLock, BTEqualStrategyNumber, CharGetDatum, ForwardScanDirection, GETSTRUCT, heap_getnext(), sort-test::key, lappend_oid(), NIL, ObjectIdGetDatum, ScanKeyInit(), table_beginscan_catalog(), table_close(), table_endscan(), and table_open().

Referenced by objectsInSchemaToOids().

867 {
868  List *relations = NIL;
869  ScanKeyData key[2];
870  Relation rel;
871  TableScanDesc scan;
872  HeapTuple tuple;
873 
874  ScanKeyInit(&key[0],
875  Anum_pg_class_relnamespace,
876  BTEqualStrategyNumber, F_OIDEQ,
877  ObjectIdGetDatum(namespaceId));
878  ScanKeyInit(&key[1],
879  Anum_pg_class_relkind,
880  BTEqualStrategyNumber, F_CHAREQ,
881  CharGetDatum(relkind));
882 
883  rel = table_open(RelationRelationId, AccessShareLock);
884  scan = table_beginscan_catalog(rel, 2, key);
885 
886  while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
887  {
888  Oid oid = ((Form_pg_class) GETSTRUCT(tuple))->oid;
889 
890  relations = lappend_oid(relations, oid);
891  }
892 
893  table_endscan(scan);
895 
896  return relations;
897 }
#define NIL
Definition: pg_list.h:65
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
TableScanDesc table_beginscan_catalog(Relation relation, int nkeys, struct ScanKeyData *key)
Definition: tableam.c:112
#define AccessShareLock
Definition: lockdefs.h:36
unsigned int Oid
Definition: postgres_ext.h:31
List * lappend_oid(List *list, Oid datum)
Definition: list.c:372
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
HeapTuple heap_getnext(TableScanDesc sscan, ScanDirection direction)
Definition: heapam.c:1340
#define CharGetDatum(X)
Definition: postgres.h:460
static void table_endscan(TableScanDesc scan)
Definition: tableam.h:991
FormData_pg_class * Form_pg_class
Definition: pg_class.h:153
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
Definition: pg_list.h:50
#define BTEqualStrategyNumber
Definition: stratnum.h:31

◆ has_bypassrls_privilege()

bool has_bypassrls_privilege ( Oid  roleid)

Definition at line 5467 of file aclchk.c.

References AUTHOID, GETSTRUCT, HeapTupleIsValid, ObjectIdGetDatum, ReleaseSysCache(), rolbypassrls, SearchSysCache1(), and superuser_arg().

Referenced by check_enable_rls(), and RI_Initial_Check().

5468 {
5469  bool result = false;
5470  HeapTuple utup;
5471 
5472  /* Superusers bypass all permission checking. */
5473  if (superuser_arg(roleid))
5474  return true;
5475 
5476  utup = SearchSysCache1(AUTHOID, ObjectIdGetDatum(roleid));
5477  if (HeapTupleIsValid(utup))
5478  {
5479  result = ((Form_pg_authid) GETSTRUCT(utup))->rolbypassrls;
5480  ReleaseSysCache(utup);
5481  }
5482  return result;
5483 }
bool rolbypassrls
Definition: pg_authid.h:41
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
FormData_pg_authid * Form_pg_authid
Definition: pg_authid.h:56
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
bool superuser_arg(Oid roleid)
Definition: superuser.c:56
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
#define HeapTupleIsValid(tuple)
Definition: htup.h:78

◆ has_createrole_privilege()

bool has_createrole_privilege ( Oid  roleid)

Definition at line 5448 of file aclchk.c.

References AUTHOID, GETSTRUCT, HeapTupleIsValid, ObjectIdGetDatum, ReleaseSysCache(), rolcreaterole, SearchSysCache1(), and superuser_arg().

Referenced by check_object_ownership(), and have_createrole_privilege().

5449 {
5450  bool result = false;
5451  HeapTuple utup;
5452 
5453  /* Superusers bypass all permission checking. */
5454  if (superuser_arg(roleid))
5455  return true;
5456 
5457  utup = SearchSysCache1(AUTHOID, ObjectIdGetDatum(roleid));
5458  if (HeapTupleIsValid(utup))
5459  {
5460  result = ((Form_pg_authid) GETSTRUCT(utup))->rolcreaterole;
5461  ReleaseSysCache(utup);
5462  }
5463  return result;
5464 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
FormData_pg_authid * Form_pg_authid
Definition: pg_authid.h:56
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
bool rolcreaterole
Definition: pg_authid.h:37
bool superuser_arg(Oid roleid)
Definition: superuser.c:56
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
#define HeapTupleIsValid(tuple)
Definition: htup.h:78

◆ merge_acl_with_grant()

static Acl* merge_acl_with_grant ( Acl old_acl,
bool  is_grant,
bool  grant_option,
DropBehavior  behavior,
List grantees,
AclMode  privileges,
Oid  grantorId,
Oid  ownerId 
)
static

Definition at line 152 of file aclchk.c.

References ACL_ID_PUBLIC, ACL_MODECHG_ADD, ACL_MODECHG_DEL, ACL_NO_RIGHTS, ACLITEM_SET_PRIVS_GOPTIONS, aclupdate(), AclItem::ai_grantee, AclItem::ai_grantor, ereport, errcode(), errmsg(), ERROR, lfirst_oid, and pfree().

Referenced by ExecGrant_Attribute(), ExecGrant_Database(), ExecGrant_Fdw(), ExecGrant_ForeignServer(), ExecGrant_Function(), ExecGrant_Language(), ExecGrant_Largeobject(), ExecGrant_Namespace(), ExecGrant_Relation(), ExecGrant_Tablespace(), ExecGrant_Type(), and SetDefaultACL().

156 {
157  unsigned modechg;
158  ListCell *j;
159  Acl *new_acl;
160 
161  modechg = is_grant ? ACL_MODECHG_ADD : ACL_MODECHG_DEL;
162 
163  new_acl = old_acl;
164 
165  foreach(j, grantees)
166  {
167  AclItem aclitem;
168  Acl *newer_acl;
169 
170  aclitem.ai_grantee = lfirst_oid(j);
171 
172  /*
173  * Grant options can only be granted to individual roles, not PUBLIC.
174  * The reason is that if a user would re-grant a privilege that he
175  * held through PUBLIC, and later the user is removed, the situation
176  * is impossible to clean up.
177  */
178  if (is_grant && grant_option && aclitem.ai_grantee == ACL_ID_PUBLIC)
179  ereport(ERROR,
180  (errcode(ERRCODE_INVALID_GRANT_OPERATION),
181  errmsg("grant options can only be granted to roles")));
182 
183  aclitem.ai_grantor = grantorId;
184 
185  /*
186  * The asymmetry in the conditions here comes from the spec. In
187  * GRANT, the grant_option flag signals WITH GRANT OPTION, which means
188  * to grant both the basic privilege and its grant option. But in
189  * REVOKE, plain revoke revokes both the basic privilege and its grant
190  * option, while REVOKE GRANT OPTION revokes only the option.
191  */
193  (is_grant || !grant_option) ? privileges : ACL_NO_RIGHTS,
194  (!is_grant || grant_option) ? privileges : ACL_NO_RIGHTS);
195 
196  newer_acl = aclupdate(new_acl, &aclitem, modechg, ownerId, behavior);
197 
198  /* avoid memory leak when there are many grantees */
199  pfree(new_acl);
200  new_acl = newer_acl;
201  }
202 
203  return new_acl;
204 }
Oid ai_grantee
Definition: acl.h:56
#define ACL_MODECHG_DEL
Definition: acl.h:130
int errcode(int sqlerrcode)
Definition: elog.c:698
Oid ai_grantor
Definition: acl.h:57
void pfree(void *pointer)
Definition: mcxt.c:1169
#define ERROR
Definition: elog.h:46
#define ACL_NO_RIGHTS
Definition: parsenodes.h:96
#define ACL_MODECHG_ADD
Definition: acl.h:129
#define ereport(elevel,...)
Definition: elog.h:157
Definition: acl.h:54
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define ACL_ID_PUBLIC
Definition: acl.h:46
Acl * aclupdate(const Acl *old_acl, const AclItem *mod_aip, int modechg, Oid ownerId, DropBehavior behavior)
Definition: acl.c:916
#define ACLITEM_SET_PRIVS_GOPTIONS(item, privs, goptions)
Definition: acl.h:82
#define lfirst_oid(lc)
Definition: pg_list.h:171

◆ objectNamesToOids()

static List * objectNamesToOids ( ObjectType  objtype,
List objnames 
)
static

Definition at line 628 of file aclchk.c.

References Assert, dbname, elog, ereport, errcode(), errmsg(), ERROR, get_database_oid(), get_foreign_data_wrapper_oid(), get_foreign_server_oid(), get_language_oid(), get_namespace_oid(), get_tablespace_oid(), lappend_oid(), LargeObjectExists(), lfirst, LookupFuncWithArgs(), makeTypeNameFromNameList(), NIL, NoLock, OBJECT_DATABASE, OBJECT_DOMAIN, OBJECT_FDW, OBJECT_FOREIGN_SERVER, OBJECT_FUNCTION, OBJECT_LANGUAGE, OBJECT_LARGEOBJECT, OBJECT_PROCEDURE, OBJECT_ROUTINE, OBJECT_SCHEMA, OBJECT_SEQUENCE, OBJECT_TABLE, OBJECT_TABLESPACE, OBJECT_TYPE, oidparse(), RangeVarGetRelid, strVal, typenameTypeId(), and typname.

Referenced by ExecuteGrantStmt().

629 {
630  List *objects = NIL;
631  ListCell *cell;
632 
633  Assert(objnames != NIL);
634 
635  switch (objtype)
636  {
637  case OBJECT_TABLE:
638  case OBJECT_SEQUENCE:
639  foreach(cell, objnames)
640  {
641  RangeVar *relvar = (RangeVar *) lfirst(cell);
642  Oid relOid;
643 
644  relOid = RangeVarGetRelid(relvar, NoLock, false);
645  objects = lappend_oid(objects, relOid);
646  }
647  break;
648  case OBJECT_DATABASE:
649  foreach(cell, objnames)
650  {
651  char *dbname = strVal(lfirst(cell));
652  Oid dbid;
653 
654  dbid = get_database_oid(dbname, false);
655  objects = lappend_oid(objects, dbid);
656  }
657  break;
658  case OBJECT_DOMAIN:
659  case OBJECT_TYPE:
660  foreach(cell, objnames)
661  {
662  List *typname = (List *) lfirst(cell);
663  Oid oid;
664 
665  oid = typenameTypeId(NULL, makeTypeNameFromNameList(typname));
666  objects = lappend_oid(objects, oid);
667  }
668  break;
669  case OBJECT_FUNCTION:
670  foreach(cell, objnames)
671  {
672  ObjectWithArgs *func = (ObjectWithArgs *) lfirst(cell);
673  Oid funcid;
674 
675  funcid = LookupFuncWithArgs(OBJECT_FUNCTION, func, false);
676  objects = lappend_oid(objects, funcid);
677  }
678  break;
679  case OBJECT_LANGUAGE:
680  foreach(cell, objnames)
681  {
682  char *langname = strVal(lfirst(cell));
683  Oid oid;
684 
685  oid = get_language_oid(langname, false);
686  objects = lappend_oid(objects, oid);
687  }
688  break;
689  case OBJECT_LARGEOBJECT:
690  foreach(cell, objnames)
691  {
692  Oid lobjOid = oidparse(lfirst(cell));
693 
694  if (!LargeObjectExists(lobjOid))
695  ereport(ERROR,
696  (errcode(ERRCODE_UNDEFINED_OBJECT),
697  errmsg("large object %u does not exist",
698  lobjOid)));
699 
700  objects = lappend_oid(objects, lobjOid);
701  }
702  break;
703  case OBJECT_SCHEMA:
704  foreach(cell, objnames)
705  {
706  char *nspname = strVal(lfirst(cell));
707  Oid oid;
708 
709  oid = get_namespace_oid(nspname, false);
710  objects = lappend_oid(objects, oid);
711  }
712  break;
713  case OBJECT_PROCEDURE:
714  foreach(cell, objnames)
715  {
716  ObjectWithArgs *func = (ObjectWithArgs *) lfirst(cell);
717  Oid procid;
718 
719  procid = LookupFuncWithArgs(OBJECT_PROCEDURE, func, false);
720  objects = lappend_oid(objects, procid);
721  }
722  break;
723  case OBJECT_ROUTINE:
724  foreach(cell, objnames)
725  {
726  ObjectWithArgs *func = (ObjectWithArgs *) lfirst(cell);
727  Oid routid;
728 
729  routid = LookupFuncWithArgs(OBJECT_ROUTINE, func, false);
730  objects = lappend_oid(objects, routid);
731  }
732  break;
733  case OBJECT_TABLESPACE:
734  foreach(cell, objnames)
735  {
736  char *spcname = strVal(lfirst(cell));
737  Oid spcoid;
738 
739  spcoid = get_tablespace_oid(spcname, false);
740  objects = lappend_oid(objects, spcoid);
741  }
742  break;
743  case OBJECT_FDW:
744  foreach(cell, objnames)
745  {
746  char *fdwname = strVal(lfirst(cell));
747  Oid fdwid = get_foreign_data_wrapper_oid(fdwname, false);
748 
749  objects = lappend_oid(objects, fdwid);
750  }
751  break;
753  foreach(cell, objnames)
754  {
755  char *srvname = strVal(lfirst(cell));
756  Oid srvid = get_foreign_server_oid(srvname, false);
757 
758  objects = lappend_oid(objects, srvid);
759  }
760  break;
761  default:
762  elog(ERROR, "unrecognized GrantStmt.objtype: %d",
763  (int) objtype);
764  }
765 
766  return objects;
767 }
#define NIL
Definition: pg_list.h:65
Oid get_tablespace_oid(const char *tablespacename, bool missing_ok)
Definition: tablespace.c:1433
Oid get_namespace_oid(const char *nspname, bool missing_ok)
Definition: namespace.c:3042
Oid oidparse(Node *node)
Definition: oid.c:314
#define RangeVarGetRelid(relation, lockmode, missing_ok)
Definition: namespace.h:78
Oid get_language_oid(const char *langname, bool missing_ok)
Definition: proclang.c:228
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:698
unsigned int Oid
Definition: postgres_ext.h:31
List * lappend_oid(List *list, Oid datum)
Definition: list.c:372
Oid get_foreign_data_wrapper_oid(const char *fdwname, bool missing_ok)
Definition: foreign.c:691
#define ERROR
Definition: elog.h:46
#define NoLock
Definition: lockdefs.h:34
NameData typname
Definition: pg_type.h:41
Oid get_database_oid(const char *dbname, bool missing_ok)
Definition: dbcommands.c:2108
#define ereport(elevel,...)
Definition: elog.h:157
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
char * dbname
Definition: streamutil.c:51
bool LargeObjectExists(Oid loid)
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
Oid LookupFuncWithArgs(ObjectType objtype, ObjectWithArgs *func, bool missing_ok)
Definition: parse_func.c:2177
TypeName * makeTypeNameFromNameList(List *names)
Definition: makefuncs.c:456
Definition: pg_list.h:50
Oid get_foreign_server_oid(const char *servername, bool missing_ok)
Definition: foreign.c:714
Oid typenameTypeId(ParseState *pstate, const TypeName *typeName)
Definition: parse_type.c:291

◆ objectsInSchemaToOids()

static List * objectsInSchemaToOids ( ObjectType  objtype,
List nspnames 
)
static

Definition at line 777 of file aclchk.c.

References AccessShareLock, BTEqualStrategyNumber, CharGetDatum, elog, ERROR, ForwardScanDirection, getRelationsInNamespace(), GETSTRUCT, heap_getnext(), sort-test::key, lappend_oid(), lfirst, list_concat(), LookupExplicitNamespace(), NIL, OBJECT_FUNCTION, OBJECT_PROCEDURE, OBJECT_ROUTINE, OBJECT_SEQUENCE, OBJECT_TABLE, ObjectIdGetDatum, ScanKeyInit(), strVal, table_beginscan_catalog(), table_close(), table_endscan(), and table_open().

Referenced by ExecuteGrantStmt().

778 {
779  List *objects = NIL;
780  ListCell *cell;
781 
782  foreach(cell, nspnames)
783  {
784  char *nspname = strVal(lfirst(cell));
785  Oid namespaceId;
786  List *objs;
787 
788  namespaceId = LookupExplicitNamespace(nspname, false);
789 
790  switch (objtype)
791  {
792  case OBJECT_TABLE:
793  objs = getRelationsInNamespace(namespaceId, RELKIND_RELATION);
794  objects = list_concat(objects, objs);
795  objs = getRelationsInNamespace(namespaceId, RELKIND_VIEW);
796  objects = list_concat(objects, objs);
797  objs = getRelationsInNamespace(namespaceId, RELKIND_MATVIEW);
798  objects = list_concat(objects, objs);
799  objs = getRelationsInNamespace(namespaceId, RELKIND_FOREIGN_TABLE);
800  objects = list_concat(objects, objs);
801  objs = getRelationsInNamespace(namespaceId, RELKIND_PARTITIONED_TABLE);
802  objects = list_concat(objects, objs);
803  break;
804  case OBJECT_SEQUENCE:
805  objs = getRelationsInNamespace(namespaceId, RELKIND_SEQUENCE);
806  objects = list_concat(objects, objs);
807  break;
808  case OBJECT_FUNCTION:
809  case OBJECT_PROCEDURE:
810  case OBJECT_ROUTINE:
811  {
812  ScanKeyData key[2];
813  int keycount;
814  Relation rel;
815  TableScanDesc scan;
816  HeapTuple tuple;
817 
818  keycount = 0;
819  ScanKeyInit(&key[keycount++],
820  Anum_pg_proc_pronamespace,
821  BTEqualStrategyNumber, F_OIDEQ,
822  ObjectIdGetDatum(namespaceId));
823 
824  if (objtype == OBJECT_FUNCTION)
825  /* includes aggregates and window functions */
826  ScanKeyInit(&key[keycount++],
827  Anum_pg_proc_prokind,
828  BTEqualStrategyNumber, F_CHARNE,
829  CharGetDatum(PROKIND_PROCEDURE));
830  else if (objtype == OBJECT_PROCEDURE)
831  ScanKeyInit(&key[keycount++],
832  Anum_pg_proc_prokind,
833  BTEqualStrategyNumber, F_CHAREQ,
834  CharGetDatum(PROKIND_PROCEDURE));
835 
836  rel = table_open(ProcedureRelationId, AccessShareLock);
837  scan = table_beginscan_catalog(rel, keycount, key);
838 
839  while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
840  {
841  Oid oid = ((Form_pg_proc) GETSTRUCT(tuple))->oid;
842 
843  objects = lappend_oid(objects, oid);
844  }
845 
846  table_endscan(scan);
848  }
849  break;
850  default:
851  /* should not happen */
852  elog(ERROR, "unrecognized GrantStmt.objtype: %d",
853  (int) objtype);
854  }
855  }
856 
857  return objects;
858 }
#define NIL
Definition: pg_list.h:65
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
Oid LookupExplicitNamespace(const char *nspname, bool missing_ok)
Definition: namespace.c:2892
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
TableScanDesc table_beginscan_catalog(Relation relation, int nkeys, struct ScanKeyData *key)
Definition: tableam.c:112
#define AccessShareLock
Definition: lockdefs.h:36
#define strVal(v)
Definition: value.h:54
List * list_concat(List *list1, const List *list2)
Definition: list.c:530
unsigned int Oid
Definition: postgres_ext.h:31
List * lappend_oid(List *list, Oid datum)
Definition: list.c:372
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
HeapTuple heap_getnext(TableScanDesc sscan, ScanDirection direction)
Definition: heapam.c:1340
static List * getRelationsInNamespace(Oid namespaceId, char relkind)
Definition: aclchk.c:866
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:136
#define lfirst(lc)
Definition: pg_list.h:169
#define CharGetDatum(X)
Definition: postgres.h:460
static void table_endscan(TableScanDesc scan)
Definition: tableam.h:991
#define elog(elevel,...)
Definition: elog.h:232
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
Definition: pg_list.h:50
#define BTEqualStrategyNumber
Definition: stratnum.h:31

◆ pg_aclmask()

static AclMode pg_aclmask ( ObjectType  objtype,
Oid  table_oid,
AttrNumber  attnum,
Oid  roleid,
AclMode  mask,
AclMaskHow  how 
)
static

Definition at line 3638 of file aclchk.c.

References ACL_NO_RIGHTS, elog, ERROR, OBJECT_COLUMN, OBJECT_DATABASE, OBJECT_EVENT_TRIGGER, OBJECT_FDW, OBJECT_FOREIGN_SERVER, OBJECT_FUNCTION, OBJECT_LANGUAGE, OBJECT_LARGEOBJECT, OBJECT_SCHEMA, OBJECT_SEQUENCE, OBJECT_STATISTIC_EXT, OBJECT_TABLE, OBJECT_TABLESPACE, OBJECT_TYPE, pg_attribute_aclmask(), pg_class_aclmask(), pg_database_aclmask(), pg_foreign_data_wrapper_aclmask(), pg_foreign_server_aclmask(), pg_language_aclmask(), pg_largeobject_aclmask_snapshot(), pg_namespace_aclmask(), pg_proc_aclmask(), pg_tablespace_aclmask(), and pg_type_aclmask().

Referenced by restrict_and_check_grant().

3640 {
3641  switch (objtype)
3642  {
3643  case OBJECT_COLUMN:
3644  return
3645  pg_class_aclmask(table_oid, roleid, mask, how) |
3646  pg_attribute_aclmask(table_oid, attnum, roleid, mask, how);
3647  case OBJECT_TABLE:
3648  case OBJECT_SEQUENCE:
3649  return pg_class_aclmask(table_oid, roleid, mask, how);
3650  case OBJECT_DATABASE:
3651  return pg_database_aclmask(table_oid, roleid, mask, how);
3652  case OBJECT_FUNCTION:
3653  return pg_proc_aclmask(table_oid, roleid, mask, how);
3654  case OBJECT_LANGUAGE:
3655  return pg_language_aclmask(table_oid, roleid, mask, how);
3656  case OBJECT_LARGEOBJECT:
3657  return pg_largeobject_aclmask_snapshot(table_oid, roleid,
3658  mask, how, NULL);
3659  case OBJECT_SCHEMA:
3660  return pg_namespace_aclmask(table_oid, roleid, mask, how);
3661  case OBJECT_STATISTIC_EXT:
3662  elog(ERROR, "grantable rights not supported for statistics objects");
3663  /* not reached, but keep compiler quiet */
3664  return ACL_NO_RIGHTS;
3665  case OBJECT_TABLESPACE:
3666  return pg_tablespace_aclmask(table_oid, roleid, mask, how);
3667  case OBJECT_FDW:
3668  return pg_foreign_data_wrapper_aclmask(table_oid, roleid, mask, how);
3669  case OBJECT_FOREIGN_SERVER:
3670  return pg_foreign_server_aclmask(table_oid, roleid, mask, how);
3671  case OBJECT_EVENT_TRIGGER:
3672  elog(ERROR, "grantable rights not supported for event triggers");
3673  /* not reached, but keep compiler quiet */
3674  return ACL_NO_RIGHTS;
3675  case OBJECT_TYPE:
3676  return pg_type_aclmask(table_oid, roleid, mask, how);
3677  default:
3678  elog(ERROR, "unrecognized objtype: %d",
3679  (int) objtype);
3680  /* not reached, but keep compiler quiet */
3681  return ACL_NO_RIGHTS;
3682  }
3683 }
AclMode pg_foreign_server_aclmask(Oid srv_oid, Oid roleid, AclMode mask, AclMaskHow how)
Definition: aclchk.c:4411
AclMode pg_attribute_aclmask(Oid table_oid, AttrNumber attnum, Oid roleid, AclMode