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_class_aclmask (Oid table_oid, Oid roleid, AclMode mask, AclMaskHow how)
 
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_all (Oid table_oid, Oid roleid, AclMode mode, AclMaskHow how)
 
AclResult pg_class_aclcheck (Oid table_oid, Oid roleid, AclMode mode)
 
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 3297 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(), ExecCheckRTPerms(), ExecInitAgg(), ExecInitExprRec(), ExecInitFunc(), ExecInitWindowAgg(), 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(), 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().

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

◆ aclcheck_error_col()

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

Definition at line 3580 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().

3582 {
3583  switch (aclerr)
3584  {
3585  case ACLCHECK_OK:
3586  /* no error, so return to caller */
3587  break;
3588  case ACLCHECK_NO_PRIV:
3589  ereport(ERROR,
3590  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
3591  errmsg("permission denied for column \"%s\" of relation \"%s\"",
3592  colname, objectname)));
3593  break;
3594  case ACLCHECK_NOT_OWNER:
3595  /* relation msg is OK since columns don't have separate owners */
3596  aclcheck_error(aclerr, objtype, objectname);
3597  break;
3598  default:
3599  elog(ERROR, "unrecognized AclResult: %d", (int) aclerr);
3600  break;
3601  }
3602 }
int errcode(int sqlerrcode)
Definition: elog.c:704
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
Definition: aclchk.c:3297
#define ERROR
Definition: elog.h:45
#define ereport(elevel,...)
Definition: elog.h:155
int errmsg(const char *fmt,...)
Definition: elog.c:915
#define elog(elevel,...)
Definition: elog.h:228

◆ aclcheck_error_type()

void aclcheck_error_type ( AclResult  aclerr,
Oid  typeOid 
)

Definition at line 3610 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().

3611 {
3612  Oid element_type = get_element_type(typeOid);
3613 
3614  aclcheck_error(aclerr, OBJECT_TYPE, format_type_be(element_type ? element_type : typeOid));
3615 }
Oid get_element_type(Oid typid)
Definition: lsyscache.c:2679
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:3297

◆ ExecAlterDefaultPrivilegesStmt()

void ExecAlterDefaultPrivilegesStmt ( ParseState pstate,
AlterDefaultPrivilegesStmt stmt 
)

Definition at line 888 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().

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

◆ 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 1591 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().

1594 {
1595  HeapTuple attr_tuple;
1596  Form_pg_attribute pg_attribute_tuple;
1597  Acl *old_acl;
1598  Acl *new_acl;
1599  Acl *merged_acl;
1600  Datum aclDatum;
1601  bool isNull;
1602  Oid grantorId;
1603  AclMode avail_goptions;
1604  bool need_update;
1605  HeapTuple newtuple;
1606  Datum values[Natts_pg_attribute];
1607  bool nulls[Natts_pg_attribute];
1608  bool replaces[Natts_pg_attribute];
1609  int noldmembers;
1610  int nnewmembers;
1611  Oid *oldmembers;
1612  Oid *newmembers;
1613 
1614  attr_tuple = SearchSysCache2(ATTNUM,
1615  ObjectIdGetDatum(relOid),
1617  if (!HeapTupleIsValid(attr_tuple))
1618  elog(ERROR, "cache lookup failed for attribute %d of relation %u",
1619  attnum, relOid);
1620  pg_attribute_tuple = (Form_pg_attribute) GETSTRUCT(attr_tuple);
1621 
1622  /*
1623  * Get working copy of existing ACL. If there's no ACL, substitute the
1624  * proper default.
1625  */
1626  aclDatum = SysCacheGetAttr(ATTNUM, attr_tuple, Anum_pg_attribute_attacl,
1627  &isNull);
1628  if (isNull)
1629  {
1630  old_acl = acldefault(OBJECT_COLUMN, ownerId);
1631  /* There are no old member roles according to the catalogs */
1632  noldmembers = 0;
1633  oldmembers = NULL;
1634  }
1635  else
1636  {
1637  old_acl = DatumGetAclPCopy(aclDatum);
1638  /* Get the roles mentioned in the existing ACL */
1639  noldmembers = aclmembers(old_acl, &oldmembers);
1640  }
1641 
1642  /*
1643  * In select_best_grantor we should consider existing table-level ACL bits
1644  * as well as the per-column ACL. Build a new ACL that is their
1645  * concatenation. (This is a bit cheap and dirty compared to merging them
1646  * properly with no duplications, but it's all we need here.)
1647  */
1648  merged_acl = aclconcat(old_rel_acl, old_acl);
1649 
1650  /* Determine ID to do the grant as, and available grant options */
1651  select_best_grantor(GetUserId(), col_privileges,
1652  merged_acl, ownerId,
1653  &grantorId, &avail_goptions);
1654 
1655  pfree(merged_acl);
1656 
1657  /*
1658  * Restrict the privileges to what we can actually grant, and emit the
1659  * standards-mandated warning and error messages. Note: we don't track
1660  * whether the user actually used the ALL PRIVILEGES(columns) syntax for
1661  * each column; we just approximate it by whether all the possible
1662  * privileges are specified now. Since the all_privs flag only determines
1663  * whether a warning is issued, this seems close enough.
1664  */
1665  col_privileges =
1666  restrict_and_check_grant(istmt->is_grant, avail_goptions,
1667  (col_privileges == ACL_ALL_RIGHTS_COLUMN),
1668  col_privileges,
1669  relOid, grantorId, OBJECT_COLUMN,
1670  relname, attnum,
1671  NameStr(pg_attribute_tuple->attname));
1672 
1673  /*
1674  * Generate new ACL.
1675  */
1676  new_acl = merge_acl_with_grant(old_acl, istmt->is_grant,
1677  istmt->grant_option,
1678  istmt->behavior, istmt->grantees,
1679  col_privileges, grantorId,
1680  ownerId);
1681 
1682  /*
1683  * We need the members of both old and new ACLs so we can correct the
1684  * shared dependency information.
1685  */
1686  nnewmembers = aclmembers(new_acl, &newmembers);
1687 
1688  /* finished building new ACL value, now insert it */
1689  MemSet(values, 0, sizeof(values));
1690  MemSet(nulls, false, sizeof(nulls));
1691  MemSet(replaces, false, sizeof(replaces));
1692 
1693  /*
1694  * If the updated ACL is empty, we can set attacl to null, and maybe even
1695  * avoid an update of the pg_attribute row. This is worth testing because
1696  * we'll come through here multiple times for any relation-level REVOKE,
1697  * even if there were never any column GRANTs. Note we are assuming that
1698  * the "default" ACL state for columns is empty.
1699  */
1700  if (ACL_NUM(new_acl) > 0)
1701  {
1702  values[Anum_pg_attribute_attacl - 1] = PointerGetDatum(new_acl);
1703  need_update = true;
1704  }
1705  else
1706  {
1707  nulls[Anum_pg_attribute_attacl - 1] = true;
1708  need_update = !isNull;
1709  }
1710  replaces[Anum_pg_attribute_attacl - 1] = true;
1711 
1712  if (need_update)
1713  {
1714  newtuple = heap_modify_tuple(attr_tuple, RelationGetDescr(attRelation),
1715  values, nulls, replaces);
1716 
1717  CatalogTupleUpdate(attRelation, &newtuple->t_self, newtuple);
1718 
1719  /* Update initial privileges for extensions */
1720  recordExtensionInitPriv(relOid, RelationRelationId, attnum,
1721  ACL_NUM(new_acl) > 0 ? new_acl : NULL);
1722 
1723  /* Update the shared dependency ACL info */
1724  updateAclDependencies(RelationRelationId, relOid, attnum,
1725  ownerId,
1726  noldmembers, oldmembers,
1727  nnewmembers, newmembers);
1728  }
1729 
1730  pfree(new_acl);
1731 
1732  ReleaseSysCache(attr_tuple);
1733 }
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:655
#define ACL_ALL_RIGHTS_COLUMN
Definition: acl.h:156
#define RelationGetDescr(relation)
Definition: rel.h:483
Oid GetUserId(void)
Definition: miscinit.c:476
#define PointerGetDatum(X)
Definition: postgres.h:556
#define Int16GetDatum(X)
Definition: postgres.h:451
Acl * acldefault(ObjectType objtype, Oid ownerId)
Definition: acl.c:740
#define MemSet(start, val, len)
Definition: c.h:996
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:72
void pfree(void *pointer)
Definition: mcxt.c:1057
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:45
#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:193
static void recordExtensionInitPriv(Oid objoid, Oid classoid, int objsubid, Acl *new_acl)
Definition: aclchk.c:5880
Acl * aclconcat(const Acl *left_acl, const Acl *right_acl)
Definition: acl.c:419
uintptr_t Datum
Definition: postgres.h:367
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:79
DropBehavior behavior
int aclmembers(const Acl *acl, Oid **roleids)
Definition: acl.c:1464
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:302
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:5103
static Datum values[MAXATTR]
Definition: bootstrap.c:165
#define elog(elevel,...)
Definition: elog.h:228
#define NameStr(name)
Definition: c.h:669
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 2071 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().

2072 {
2073  Relation relation;
2074  ListCell *cell;
2075 
2076  if (istmt->all_privs && istmt->privileges == ACL_NO_RIGHTS)
2077  istmt->privileges = ACL_ALL_RIGHTS_DATABASE;
2078 
2079  relation = table_open(DatabaseRelationId, RowExclusiveLock);
2080 
2081  foreach(cell, istmt->objects)
2082  {
2083  Oid datId = lfirst_oid(cell);
2084  Form_pg_database pg_database_tuple;
2085  Datum aclDatum;
2086  bool isNull;
2087  AclMode avail_goptions;
2088  AclMode this_privileges;
2089  Acl *old_acl;
2090  Acl *new_acl;
2091  Oid grantorId;
2092  Oid ownerId;
2093  HeapTuple newtuple;
2094  Datum values[Natts_pg_database];
2095  bool nulls[Natts_pg_database];
2096  bool replaces[Natts_pg_database];
2097  int noldmembers;
2098  int nnewmembers;
2099  Oid *oldmembers;
2100  Oid *newmembers;
2101  HeapTuple tuple;
2102 
2103  tuple = SearchSysCache1(DATABASEOID, ObjectIdGetDatum(datId));
2104  if (!HeapTupleIsValid(tuple))
2105  elog(ERROR, "cache lookup failed for database %u", datId);
2106 
2107  pg_database_tuple = (Form_pg_database) GETSTRUCT(tuple);
2108 
2109  /*
2110  * Get owner ID and working copy of existing ACL. If there's no ACL,
2111  * substitute the proper default.
2112  */
2113  ownerId = pg_database_tuple->datdba;
2114  aclDatum = heap_getattr(tuple, Anum_pg_database_datacl,
2115  RelationGetDescr(relation), &isNull);
2116  if (isNull)
2117  {
2118  old_acl = acldefault(OBJECT_DATABASE, ownerId);
2119  /* There are no old member roles according to the catalogs */
2120  noldmembers = 0;
2121  oldmembers = NULL;
2122  }
2123  else
2124  {
2125  old_acl = DatumGetAclPCopy(aclDatum);
2126  /* Get the roles mentioned in the existing ACL */
2127  noldmembers = aclmembers(old_acl, &oldmembers);
2128  }
2129 
2130  /* Determine ID to do the grant as, and available grant options */
2131  select_best_grantor(GetUserId(), istmt->privileges,
2132  old_acl, ownerId,
2133  &grantorId, &avail_goptions);
2134 
2135  /*
2136  * Restrict the privileges to what we can actually grant, and emit the
2137  * standards-mandated warning and error messages.
2138  */
2139  this_privileges =
2140  restrict_and_check_grant(istmt->is_grant, avail_goptions,
2141  istmt->all_privs, istmt->privileges,
2142  datId, grantorId, OBJECT_DATABASE,
2143  NameStr(pg_database_tuple->datname),
2144  0, NULL);
2145 
2146  /*
2147  * Generate new ACL.
2148  */
2149  new_acl = merge_acl_with_grant(old_acl, istmt->is_grant,
2150  istmt->grant_option, istmt->behavior,
2151  istmt->grantees, this_privileges,
2152  grantorId, ownerId);
2153 
2154  /*
2155  * We need the members of both old and new ACLs so we can correct the
2156  * shared dependency information.
2157  */
2158  nnewmembers = aclmembers(new_acl, &newmembers);
2159 
2160  /* finished building new ACL value, now insert it */
2161  MemSet(values, 0, sizeof(values));
2162  MemSet(nulls, false, sizeof(nulls));
2163  MemSet(replaces, false, sizeof(replaces));
2164 
2165  replaces[Anum_pg_database_datacl - 1] = true;
2166  values[Anum_pg_database_datacl - 1] = PointerGetDatum(new_acl);
2167 
2168  newtuple = heap_modify_tuple(tuple, RelationGetDescr(relation), values,
2169  nulls, replaces);
2170 
2171  CatalogTupleUpdate(relation, &newtuple->t_self, newtuple);
2172 
2173  /* Update the shared dependency ACL info */
2174  updateAclDependencies(DatabaseRelationId, pg_database_tuple->oid, 0,
2175  ownerId,
2176  noldmembers, oldmembers,
2177  nnewmembers, newmembers);
2178 
2179  ReleaseSysCache(tuple);
2180 
2181  pfree(new_acl);
2182 
2183  /* prevent error when processing duplicate objects */
2185  }
2186 
2187  table_close(relation, RowExclusiveLock);
2188 }
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:655
#define RelationGetDescr(relation)
Definition: rel.h:483
Oid GetUserId(void)
Definition: miscinit.c:476
FormData_pg_database * Form_pg_database
Definition: pg_database.h:81
#define PointerGetDatum(X)
Definition: postgres.h:556
Acl * acldefault(ObjectType objtype, Oid ownerId)
Definition: acl.c:740
#define MemSet(start, val, len)
Definition: c.h:996
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:72
void pfree(void *pointer)
Definition: mcxt.c:1057
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:45
#define ACL_NO_RIGHTS
Definition: parsenodes.h:88
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
#define heap_getattr(tup, attnum, tupleDesc, isnull)
Definition: htup_details.h:762
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
uintptr_t Datum
Definition: postgres.h:367
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:1464
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:302
void select_best_grantor(Oid roleId, AclMode privileges, const Acl *acl, Oid ownerId, Oid *grantorId, AclMode *grantOptions)
Definition: acl.c:5103
#define ACL_ALL_RIGHTS_DATABASE
Definition: acl.h:159
static Datum values[MAXATTR]
Definition: bootstrap.c:165
#define elog(elevel,...)
Definition: elog.h:228
#define NameStr(name)
Definition: c.h:669
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 2191 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().

2192 {
2193  Relation relation;
2194  ListCell *cell;
2195 
2196  if (istmt->all_privs && istmt->privileges == ACL_NO_RIGHTS)
2197  istmt->privileges = ACL_ALL_RIGHTS_FDW;
2198 
2199  relation = table_open(ForeignDataWrapperRelationId, RowExclusiveLock);
2200 
2201  foreach(cell, istmt->objects)
2202  {
2203  Oid fdwid = lfirst_oid(cell);
2204  Form_pg_foreign_data_wrapper pg_fdw_tuple;
2205  Datum aclDatum;
2206  bool isNull;
2207  AclMode avail_goptions;
2208  AclMode this_privileges;
2209  Acl *old_acl;
2210  Acl *new_acl;
2211  Oid grantorId;
2212  Oid ownerId;
2213  HeapTuple tuple;
2214  HeapTuple newtuple;
2215  Datum values[Natts_pg_foreign_data_wrapper];
2216  bool nulls[Natts_pg_foreign_data_wrapper];
2217  bool replaces[Natts_pg_foreign_data_wrapper];
2218  int noldmembers;
2219  int nnewmembers;
2220  Oid *oldmembers;
2221  Oid *newmembers;
2222 
2224  ObjectIdGetDatum(fdwid));
2225  if (!HeapTupleIsValid(tuple))
2226  elog(ERROR, "cache lookup failed for foreign-data wrapper %u", fdwid);
2227 
2228  pg_fdw_tuple = (Form_pg_foreign_data_wrapper) GETSTRUCT(tuple);
2229 
2230  /*
2231  * Get owner ID and working copy of existing ACL. If there's no ACL,
2232  * substitute the proper default.
2233  */
2234  ownerId = pg_fdw_tuple->fdwowner;
2235  aclDatum = SysCacheGetAttr(FOREIGNDATAWRAPPEROID, tuple,
2236  Anum_pg_foreign_data_wrapper_fdwacl,
2237  &isNull);
2238  if (isNull)
2239  {
2240  old_acl = acldefault(OBJECT_FDW, ownerId);
2241  /* There are no old member roles according to the catalogs */
2242  noldmembers = 0;
2243  oldmembers = NULL;
2244  }
2245  else
2246  {
2247  old_acl = DatumGetAclPCopy(aclDatum);
2248  /* Get the roles mentioned in the existing ACL */
2249  noldmembers = aclmembers(old_acl, &oldmembers);
2250  }
2251 
2252  /* Determine ID to do the grant as, and available grant options */
2253  select_best_grantor(GetUserId(), istmt->privileges,
2254  old_acl, ownerId,
2255  &grantorId, &avail_goptions);
2256 
2257  /*
2258  * Restrict the privileges to what we can actually grant, and emit the
2259  * standards-mandated warning and error messages.
2260  */
2261  this_privileges =
2262  restrict_and_check_grant(istmt->is_grant, avail_goptions,
2263  istmt->all_privs, istmt->privileges,
2264  fdwid, grantorId, OBJECT_FDW,
2265  NameStr(pg_fdw_tuple->fdwname),
2266  0, NULL);
2267 
2268  /*
2269  * Generate new ACL.
2270  */
2271  new_acl = merge_acl_with_grant(old_acl, istmt->is_grant,
2272  istmt->grant_option, istmt->behavior,
2273  istmt->grantees, this_privileges,
2274  grantorId, ownerId);
2275 
2276  /*
2277  * We need the members of both old and new ACLs so we can correct the
2278  * shared dependency information.
2279  */
2280  nnewmembers = aclmembers(new_acl, &newmembers);
2281 
2282  /* finished building new ACL value, now insert it */
2283  MemSet(values, 0, sizeof(values));
2284  MemSet(nulls, false, sizeof(nulls));
2285  MemSet(replaces, false, sizeof(replaces));
2286 
2287  replaces[Anum_pg_foreign_data_wrapper_fdwacl - 1] = true;
2288  values[Anum_pg_foreign_data_wrapper_fdwacl - 1] = PointerGetDatum(new_acl);
2289 
2290  newtuple = heap_modify_tuple(tuple, RelationGetDescr(relation), values,
2291  nulls, replaces);
2292 
2293  CatalogTupleUpdate(relation, &newtuple->t_self, newtuple);
2294 
2295  /* Update initial privileges for extensions */
2296  recordExtensionInitPriv(fdwid, ForeignDataWrapperRelationId, 0,
2297  new_acl);
2298 
2299  /* Update the shared dependency ACL info */
2300  updateAclDependencies(ForeignDataWrapperRelationId,
2301  pg_fdw_tuple->oid, 0,
2302  ownerId,
2303  noldmembers, oldmembers,
2304  nnewmembers, newmembers);
2305 
2306  ReleaseSysCache(tuple);
2307 
2308  pfree(new_acl);
2309 
2310  /* prevent error when processing duplicate objects */
2312  }
2313 
2314  table_close(relation, RowExclusiveLock);
2315 }
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:655
#define RelationGetDescr(relation)
Definition: rel.h:483
Oid GetUserId(void)
Definition: miscinit.c:476
#define PointerGetDatum(X)
Definition: postgres.h:556
Acl * acldefault(ObjectType objtype, Oid ownerId)
Definition: acl.c:740
#define MemSet(start, val, len)
Definition: c.h:996
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:72
void pfree(void *pointer)
Definition: mcxt.c:1057
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:45
#define ACL_NO_RIGHTS
Definition: parsenodes.h:88
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:5880
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
uintptr_t Datum
Definition: postgres.h:367
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:1464
#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:302
void select_best_grantor(Oid roleId, AclMode privileges, const Acl *acl, Oid ownerId, Oid *grantorId, AclMode *grantOptions)
Definition: acl.c:5103
static Datum values[MAXATTR]
Definition: bootstrap.c:165
#define elog(elevel,...)
Definition: elog.h:228
#define NameStr(name)
Definition: c.h:669
#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 2318 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().

2319 {
2320  Relation relation;
2321  ListCell *cell;
2322 
2323  if (istmt->all_privs && istmt->privileges == ACL_NO_RIGHTS)
2324  istmt->privileges = ACL_ALL_RIGHTS_FOREIGN_SERVER;
2325 
2326  relation = table_open(ForeignServerRelationId, RowExclusiveLock);
2327 
2328  foreach(cell, istmt->objects)
2329  {
2330  Oid srvid = lfirst_oid(cell);
2331  Form_pg_foreign_server pg_server_tuple;
2332  Datum aclDatum;
2333  bool isNull;
2334  AclMode avail_goptions;
2335  AclMode this_privileges;
2336  Acl *old_acl;
2337  Acl *new_acl;
2338  Oid grantorId;
2339  Oid ownerId;
2340  HeapTuple tuple;
2341  HeapTuple newtuple;
2342  Datum values[Natts_pg_foreign_server];
2343  bool nulls[Natts_pg_foreign_server];
2344  bool replaces[Natts_pg_foreign_server];
2345  int noldmembers;
2346  int nnewmembers;
2347  Oid *oldmembers;
2348  Oid *newmembers;
2349 
2351  if (!HeapTupleIsValid(tuple))
2352  elog(ERROR, "cache lookup failed for foreign server %u", srvid);
2353 
2354  pg_server_tuple = (Form_pg_foreign_server) GETSTRUCT(tuple);
2355 
2356  /*
2357  * Get owner ID and working copy of existing ACL. If there's no ACL,
2358  * substitute the proper default.
2359  */
2360  ownerId = pg_server_tuple->srvowner;
2361  aclDatum = SysCacheGetAttr(FOREIGNSERVEROID, tuple,
2362  Anum_pg_foreign_server_srvacl,
2363  &isNull);
2364  if (isNull)
2365  {
2366  old_acl = acldefault(OBJECT_FOREIGN_SERVER, ownerId);
2367  /* There are no old member roles according to the catalogs */
2368  noldmembers = 0;
2369  oldmembers = NULL;
2370  }
2371  else
2372  {
2373  old_acl = DatumGetAclPCopy(aclDatum);
2374  /* Get the roles mentioned in the existing ACL */
2375  noldmembers = aclmembers(old_acl, &oldmembers);
2376  }
2377 
2378  /* Determine ID to do the grant as, and available grant options */
2379  select_best_grantor(GetUserId(), istmt->privileges,
2380  old_acl, ownerId,
2381  &grantorId, &avail_goptions);
2382 
2383  /*
2384  * Restrict the privileges to what we can actually grant, and emit the
2385  * standards-mandated warning and error messages.
2386  */
2387  this_privileges =
2388  restrict_and_check_grant(istmt->is_grant, avail_goptions,
2389  istmt->all_privs, istmt->privileges,
2390  srvid, grantorId, OBJECT_FOREIGN_SERVER,
2391  NameStr(pg_server_tuple->srvname),
2392  0, NULL);
2393 
2394  /*
2395  * Generate new ACL.
2396  */
2397  new_acl = merge_acl_with_grant(old_acl, istmt->is_grant,
2398  istmt->grant_option, istmt->behavior,
2399  istmt->grantees, this_privileges,
2400  grantorId, ownerId);
2401 
2402  /*
2403  * We need the members of both old and new ACLs so we can correct the
2404  * shared dependency information.
2405  */
2406  nnewmembers = aclmembers(new_acl, &newmembers);
2407 
2408  /* finished building new ACL value, now insert it */
2409  MemSet(values, 0, sizeof(values));
2410  MemSet(nulls, false, sizeof(nulls));
2411  MemSet(replaces, false, sizeof(replaces));
2412 
2413  replaces[Anum_pg_foreign_server_srvacl - 1] = true;
2414  values[Anum_pg_foreign_server_srvacl - 1] = PointerGetDatum(new_acl);
2415 
2416  newtuple = heap_modify_tuple(tuple, RelationGetDescr(relation), values,
2417  nulls, replaces);
2418 
2419  CatalogTupleUpdate(relation, &newtuple->t_self, newtuple);
2420 
2421  /* Update initial privileges for extensions */
2422  recordExtensionInitPriv(srvid, ForeignServerRelationId, 0, new_acl);
2423 
2424  /* Update the shared dependency ACL info */
2425  updateAclDependencies(ForeignServerRelationId,
2426  pg_server_tuple->oid, 0,
2427  ownerId,
2428  noldmembers, oldmembers,
2429  nnewmembers, newmembers);
2430 
2431  ReleaseSysCache(tuple);
2432 
2433  pfree(new_acl);
2434 
2435  /* prevent error when processing duplicate objects */
2437  }
2438 
2439  table_close(relation, RowExclusiveLock);
2440 }
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:655
#define RelationGetDescr(relation)
Definition: rel.h:483
Oid GetUserId(void)
Definition: miscinit.c:476
#define PointerGetDatum(X)
Definition: postgres.h:556
Acl * acldefault(ObjectType objtype, Oid ownerId)
Definition: acl.c:740
#define MemSet(start, val, len)
Definition: c.h:996
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:72
void pfree(void *pointer)
Definition: mcxt.c:1057
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:45
#define ACL_NO_RIGHTS
Definition: parsenodes.h:88
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:5880
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
uintptr_t Datum
Definition: postgres.h:367
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:1464
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:302
void select_best_grantor(Oid roleId, AclMode privileges, const Acl *acl, Oid ownerId, Oid *grantorId, AclMode *grantOptions)
Definition: acl.c:5103
#define ACL_ALL_RIGHTS_FOREIGN_SERVER
Definition: acl.h:161
static Datum values[MAXATTR]
Definition: bootstrap.c:165
#define elog(elevel,...)
Definition: elog.h:228
#define NameStr(name)
Definition: c.h:669
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 2443 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().

2444 {
2445  Relation relation;
2446  ListCell *cell;
2447 
2448  if (istmt->all_privs && istmt->privileges == ACL_NO_RIGHTS)
2449  istmt->privileges = ACL_ALL_RIGHTS_FUNCTION;
2450 
2451  relation = table_open(ProcedureRelationId, RowExclusiveLock);
2452 
2453  foreach(cell, istmt->objects)
2454  {
2455  Oid funcId = lfirst_oid(cell);
2456  Form_pg_proc pg_proc_tuple;
2457  Datum aclDatum;
2458  bool isNull;
2459  AclMode avail_goptions;
2460  AclMode this_privileges;
2461  Acl *old_acl;
2462  Acl *new_acl;
2463  Oid grantorId;
2464  Oid ownerId;
2465  HeapTuple tuple;
2466  HeapTuple newtuple;
2467  Datum values[Natts_pg_proc];
2468  bool nulls[Natts_pg_proc];
2469  bool replaces[Natts_pg_proc];
2470  int noldmembers;
2471  int nnewmembers;
2472  Oid *oldmembers;
2473  Oid *newmembers;
2474 
2475  tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcId));
2476  if (!HeapTupleIsValid(tuple))
2477  elog(ERROR, "cache lookup failed for function %u", funcId);
2478 
2479  pg_proc_tuple = (Form_pg_proc) GETSTRUCT(tuple);
2480 
2481  /*
2482  * Get owner ID and working copy of existing ACL. If there's no ACL,
2483  * substitute the proper default.
2484  */
2485  ownerId = pg_proc_tuple->proowner;
2486  aclDatum = SysCacheGetAttr(PROCOID, tuple, Anum_pg_proc_proacl,
2487  &isNull);
2488  if (isNull)
2489  {
2490  old_acl = acldefault(OBJECT_FUNCTION, ownerId);
2491  /* There are no old member roles according to the catalogs */
2492  noldmembers = 0;
2493  oldmembers = NULL;
2494  }
2495  else
2496  {
2497  old_acl = DatumGetAclPCopy(aclDatum);
2498  /* Get the roles mentioned in the existing ACL */
2499  noldmembers = aclmembers(old_acl, &oldmembers);
2500  }
2501 
2502  /* Determine ID to do the grant as, and available grant options */
2503  select_best_grantor(GetUserId(), istmt->privileges,
2504  old_acl, ownerId,
2505  &grantorId, &avail_goptions);
2506 
2507  /*
2508  * Restrict the privileges to what we can actually grant, and emit the
2509  * standards-mandated warning and error messages.
2510  */
2511  this_privileges =
2512  restrict_and_check_grant(istmt->is_grant, avail_goptions,
2513  istmt->all_privs, istmt->privileges,
2514  funcId, grantorId, OBJECT_FUNCTION,
2515  NameStr(pg_proc_tuple->proname),
2516  0, NULL);
2517 
2518  /*
2519  * Generate new ACL.
2520  */
2521  new_acl = merge_acl_with_grant(old_acl, istmt->is_grant,
2522  istmt->grant_option, istmt->behavior,
2523  istmt->grantees, this_privileges,
2524  grantorId, ownerId);
2525 
2526  /*
2527  * We need the members of both old and new ACLs so we can correct the
2528  * shared dependency information.
2529  */
2530  nnewmembers = aclmembers(new_acl, &newmembers);
2531 
2532  /* finished building new ACL value, now insert it */
2533  MemSet(values, 0, sizeof(values));
2534  MemSet(nulls, false, sizeof(nulls));
2535  MemSet(replaces, false, sizeof(replaces));
2536 
2537  replaces[Anum_pg_proc_proacl - 1] = true;
2538  values[Anum_pg_proc_proacl - 1] = PointerGetDatum(new_acl);
2539 
2540  newtuple = heap_modify_tuple(tuple, RelationGetDescr(relation), values,
2541  nulls, replaces);
2542 
2543  CatalogTupleUpdate(relation, &newtuple->t_self, newtuple);
2544 
2545  /* Update initial privileges for extensions */
2546  recordExtensionInitPriv(funcId, ProcedureRelationId, 0, new_acl);
2547 
2548  /* Update the shared dependency ACL info */
2549  updateAclDependencies(ProcedureRelationId, funcId, 0,
2550  ownerId,
2551  noldmembers, oldmembers,
2552  nnewmembers, newmembers);
2553 
2554  ReleaseSysCache(tuple);
2555 
2556  pfree(new_acl);
2557 
2558  /* prevent error when processing duplicate objects */
2560  }
2561 
2562  table_close(relation, RowExclusiveLock);
2563 }
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:655
#define RelationGetDescr(relation)
Definition: rel.h:483
Oid GetUserId(void)
Definition: miscinit.c:476
#define PointerGetDatum(X)
Definition: postgres.h:556
Acl * acldefault(ObjectType objtype, Oid ownerId)
Definition: acl.c:740
#define MemSet(start, val, len)
Definition: c.h:996
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:72
void pfree(void *pointer)
Definition: mcxt.c:1057
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:45
#define ACL_NO_RIGHTS
Definition: parsenodes.h:88
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:5880
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
uintptr_t Datum
Definition: postgres.h:367
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:133
int aclmembers(const Acl *acl, Oid **roleids)
Definition: acl.c:1464
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:302
void select_best_grantor(Oid roleId, AclMode privileges, const Acl *acl, Oid ownerId, Oid *grantorId, AclMode *grantOptions)
Definition: acl.c:5103
static Datum values[MAXATTR]
Definition: bootstrap.c:165
#define elog(elevel,...)
Definition: elog.h:228
#define NameStr(name)
Definition: c.h:669
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 2566 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().

2567 {
2568  Relation relation;
2569  ListCell *cell;
2570 
2571  if (istmt->all_privs && istmt->privileges == ACL_NO_RIGHTS)
2572  istmt->privileges = ACL_ALL_RIGHTS_LANGUAGE;
2573 
2574  relation = table_open(LanguageRelationId, RowExclusiveLock);
2575 
2576  foreach(cell, istmt->objects)
2577  {
2578  Oid langId = lfirst_oid(cell);
2579  Form_pg_language pg_language_tuple;
2580  Datum aclDatum;
2581  bool isNull;
2582  AclMode avail_goptions;
2583  AclMode this_privileges;
2584  Acl *old_acl;
2585  Acl *new_acl;
2586  Oid grantorId;
2587  Oid ownerId;
2588  HeapTuple tuple;
2589  HeapTuple newtuple;
2590  Datum values[Natts_pg_language];
2591  bool nulls[Natts_pg_language];
2592  bool replaces[Natts_pg_language];
2593  int noldmembers;
2594  int nnewmembers;
2595  Oid *oldmembers;
2596  Oid *newmembers;
2597 
2598  tuple = SearchSysCache1(LANGOID, ObjectIdGetDatum(langId));
2599  if (!HeapTupleIsValid(tuple))
2600  elog(ERROR, "cache lookup failed for language %u", langId);
2601 
2602  pg_language_tuple = (Form_pg_language) GETSTRUCT(tuple);
2603 
2604  if (!pg_language_tuple->lanpltrusted)
2605  ereport(ERROR,
2606  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
2607  errmsg("language \"%s\" is not trusted",
2608  NameStr(pg_language_tuple->lanname)),
2609  errdetail("GRANT and REVOKE are not allowed on untrusted languages, "
2610  "because only superusers can use untrusted languages.")));
2611 
2612  /*
2613  * Get owner ID and working copy of existing ACL. If there's no ACL,
2614  * substitute the proper default.
2615  */
2616  ownerId = pg_language_tuple->lanowner;
2617  aclDatum = SysCacheGetAttr(LANGNAME, tuple, Anum_pg_language_lanacl,
2618  &isNull);
2619  if (isNull)
2620  {
2621  old_acl = acldefault(OBJECT_LANGUAGE, ownerId);
2622  /* There are no old member roles according to the catalogs */
2623  noldmembers = 0;
2624  oldmembers = NULL;
2625  }
2626  else
2627  {
2628  old_acl = DatumGetAclPCopy(aclDatum);
2629  /* Get the roles mentioned in the existing ACL */
2630  noldmembers = aclmembers(old_acl, &oldmembers);
2631  }
2632 
2633  /* Determine ID to do the grant as, and available grant options */
2634  select_best_grantor(GetUserId(), istmt->privileges,
2635  old_acl, ownerId,
2636  &grantorId, &avail_goptions);
2637 
2638  /*
2639  * Restrict the privileges to what we can actually grant, and emit the
2640  * standards-mandated warning and error messages.
2641  */
2642  this_privileges =
2643  restrict_and_check_grant(istmt->is_grant, avail_goptions,
2644  istmt->all_privs, istmt->privileges,
2645  langId, grantorId, OBJECT_LANGUAGE,
2646  NameStr(pg_language_tuple->lanname),
2647  0, NULL);
2648 
2649  /*
2650  * Generate new ACL.
2651  */
2652  new_acl = merge_acl_with_grant(old_acl, istmt->is_grant,
2653  istmt->grant_option, istmt->behavior,
2654  istmt->grantees, this_privileges,
2655  grantorId, ownerId);
2656 
2657  /*
2658  * We need the members of both old and new ACLs so we can correct the
2659  * shared dependency information.
2660  */
2661  nnewmembers = aclmembers(new_acl, &newmembers);
2662 
2663  /* finished building new ACL value, now insert it */
2664  MemSet(values, 0, sizeof(values));
2665  MemSet(nulls, false, sizeof(nulls));
2666  MemSet(replaces, false, sizeof(replaces));
2667 
2668  replaces[Anum_pg_language_lanacl - 1] = true;
2669  values[Anum_pg_language_lanacl - 1] = PointerGetDatum(new_acl);
2670 
2671  newtuple = heap_modify_tuple(tuple, RelationGetDescr(relation), values,
2672  nulls, replaces);
2673 
2674  CatalogTupleUpdate(relation, &newtuple->t_self, newtuple);
2675 
2676  /* Update initial privileges for extensions */
2677  recordExtensionInitPriv(langId, LanguageRelationId, 0, new_acl);
2678 
2679  /* Update the shared dependency ACL info */
2680  updateAclDependencies(LanguageRelationId, pg_language_tuple->oid, 0,
2681  ownerId,
2682  noldmembers, oldmembers,
2683  nnewmembers, newmembers);
2684 
2685  ReleaseSysCache(tuple);
2686 
2687  pfree(new_acl);
2688 
2689  /* prevent error when processing duplicate objects */
2691  }
2692 
2693  table_close(relation, RowExclusiveLock);
2694 }
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:655
#define RelationGetDescr(relation)
Definition: rel.h:483
Oid GetUserId(void)
Definition: miscinit.c:476
#define PointerGetDatum(X)
Definition: postgres.h:556
int errcode(int sqlerrcode)
Definition: elog.c:704
Acl * acldefault(ObjectType objtype, Oid ownerId)
Definition: acl.c:740
#define MemSet(start, val, len)
Definition: c.h:996
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:72
void pfree(void *pointer)
Definition: mcxt.c:1057
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:45
#define ACL_NO_RIGHTS
Definition: parsenodes.h:88
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
int errdetail(const char *fmt,...)
Definition: elog.c:1048
static void recordExtensionInitPriv(Oid objoid, Oid classoid, int objsubid, Acl *new_acl)
Definition: aclchk.c:5880
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
uintptr_t Datum
Definition: postgres.h:367
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:155
int aclmembers(const Acl *acl, Oid **roleids)
Definition: acl.c:1464
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:302
void select_best_grantor(Oid roleId, AclMode privileges, const Acl *acl, Oid ownerId, Oid *grantorId, AclMode *grantOptions)
Definition: acl.c:5103
static Datum values[MAXATTR]
Definition: bootstrap.c:165
FormData_pg_language * Form_pg_language
Definition: pg_language.h:65
int errmsg(const char *fmt,...)
Definition: elog.c:915
#define elog(elevel,...)
Definition: elog.h:228
#define NameStr(name)
Definition: c.h:669
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 2697 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().

2698 {
2699  Relation relation;
2700  ListCell *cell;
2701 
2702  if (istmt->all_privs && istmt->privileges == ACL_NO_RIGHTS)
2703  istmt->privileges = ACL_ALL_RIGHTS_LARGEOBJECT;
2704 
2705  relation = table_open(LargeObjectMetadataRelationId,
2707 
2708  foreach(cell, istmt->objects)
2709  {
2710  Oid loid = lfirst_oid(cell);
2711  Form_pg_largeobject_metadata form_lo_meta;
2712  char loname[NAMEDATALEN];
2713  Datum aclDatum;
2714  bool isNull;
2715  AclMode avail_goptions;
2716  AclMode this_privileges;
2717  Acl *old_acl;
2718  Acl *new_acl;
2719  Oid grantorId;
2720  Oid ownerId;
2721  HeapTuple newtuple;
2722  Datum values[Natts_pg_largeobject_metadata];
2723  bool nulls[Natts_pg_largeobject_metadata];
2724  bool replaces[Natts_pg_largeobject_metadata];
2725  int noldmembers;
2726  int nnewmembers;
2727  Oid *oldmembers;
2728  Oid *newmembers;
2729  ScanKeyData entry[1];
2730  SysScanDesc scan;
2731  HeapTuple tuple;
2732 
2733  /* There's no syscache for pg_largeobject_metadata */
2734  ScanKeyInit(&entry[0],
2735  Anum_pg_largeobject_metadata_oid,
2736  BTEqualStrategyNumber, F_OIDEQ,
2737  ObjectIdGetDatum(loid));
2738 
2739  scan = systable_beginscan(relation,
2741  NULL, 1, entry);
2742 
2743  tuple = systable_getnext(scan);
2744  if (!HeapTupleIsValid(tuple))
2745  elog(ERROR, "could not find tuple for large object %u", loid);
2746 
2747  form_lo_meta = (Form_pg_largeobject_metadata) GETSTRUCT(tuple);
2748 
2749  /*
2750  * Get owner ID and working copy of existing ACL. If there's no ACL,
2751  * substitute the proper default.
2752  */
2753  ownerId = form_lo_meta->lomowner;
2754  aclDatum = heap_getattr(tuple,
2755  Anum_pg_largeobject_metadata_lomacl,
2756  RelationGetDescr(relation), &isNull);
2757  if (isNull)
2758  {
2759  old_acl = acldefault(OBJECT_LARGEOBJECT, ownerId);
2760  /* There are no old member roles according to the catalogs */
2761  noldmembers = 0;
2762  oldmembers = NULL;
2763  }
2764  else
2765  {
2766  old_acl = DatumGetAclPCopy(aclDatum);
2767  /* Get the roles mentioned in the existing ACL */
2768  noldmembers = aclmembers(old_acl, &oldmembers);
2769  }
2770 
2771  /* Determine ID to do the grant as, and available grant options */
2772  select_best_grantor(GetUserId(), istmt->privileges,
2773  old_acl, ownerId,
2774  &grantorId, &avail_goptions);
2775 
2776  /*
2777  * Restrict the privileges to what we can actually grant, and emit the
2778  * standards-mandated warning and error messages.
2779  */
2780  snprintf(loname, sizeof(loname), "large object %u", loid);
2781  this_privileges =
2782  restrict_and_check_grant(istmt->is_grant, avail_goptions,
2783  istmt->all_privs, istmt->privileges,
2784  loid, grantorId, OBJECT_LARGEOBJECT,
2785  loname, 0, NULL);
2786 
2787  /*
2788  * Generate new ACL.
2789  */
2790  new_acl = merge_acl_with_grant(old_acl, istmt->is_grant,
2791  istmt->grant_option, istmt->behavior,
2792  istmt->grantees, this_privileges,
2793  grantorId, ownerId);
2794 
2795  /*
2796  * We need the members of both old and new ACLs so we can correct the
2797  * shared dependency information.
2798  */
2799  nnewmembers = aclmembers(new_acl, &newmembers);
2800 
2801  /* finished building new ACL value, now insert it */
2802  MemSet(values, 0, sizeof(values));
2803  MemSet(nulls, false, sizeof(nulls));
2804  MemSet(replaces, false, sizeof(replaces));
2805 
2806  replaces[Anum_pg_largeobject_metadata_lomacl - 1] = true;
2807  values[Anum_pg_largeobject_metadata_lomacl - 1]
2808  = PointerGetDatum(new_acl);
2809 
2810  newtuple = heap_modify_tuple(tuple, RelationGetDescr(relation),
2811  values, nulls, replaces);
2812 
2813  CatalogTupleUpdate(relation, &newtuple->t_self, newtuple);
2814 
2815  /* Update initial privileges for extensions */
2816  recordExtensionInitPriv(loid, LargeObjectRelationId, 0, new_acl);
2817 
2818  /* Update the shared dependency ACL info */
2819  updateAclDependencies(LargeObjectRelationId,
2820  form_lo_meta->oid, 0,
2821  ownerId,
2822  noldmembers, oldmembers,
2823  nnewmembers, newmembers);
2824 
2825  systable_endscan(scan);
2826 
2827  pfree(new_acl);
2828 
2829  /* prevent error when processing duplicate objects */
2831  }
2832 
2833  table_close(relation, RowExclusiveLock);
2834 }
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:593
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
#define RelationGetDescr(relation)
Definition: rel.h:483
Oid GetUserId(void)
Definition: miscinit.c:476
#define PointerGetDatum(X)
Definition: postgres.h:556
Acl * acldefault(ObjectType objtype, Oid ownerId)
Definition: acl.c:740
#define MemSet(start, val, len)
Definition: c.h:996
#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:381
#define NAMEDATALEN
uint32 AclMode
Definition: parsenodes.h:72
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:500
void pfree(void *pointer)
Definition: mcxt.c:1057
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:45
#define ACL_NO_RIGHTS
Definition: parsenodes.h:88
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:5880
#define heap_getattr(tup, attnum, tupleDesc, isnull)
Definition: htup_details.h:762
FormData_pg_largeobject_metadata * Form_pg_largeobject_metadata
uintptr_t Datum
Definition: postgres.h:367
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:1464
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:302
void select_best_grantor(Oid roleId, AclMode privileges, const Acl *acl, Oid ownerId, Oid *grantorId, AclMode *grantOptions)
Definition: acl.c:5103
static Datum values[MAXATTR]
Definition: bootstrap.c:165
#define elog(elevel,...)
Definition: elog.h:228
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:215
#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 2837 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().

2838 {
2839  Relation relation;
2840  ListCell *cell;
2841 
2842  if (istmt->all_privs && istmt->privileges == ACL_NO_RIGHTS)
2843  istmt->privileges = ACL_ALL_RIGHTS_SCHEMA;
2844 
2845  relation = table_open(NamespaceRelationId, RowExclusiveLock);
2846 
2847  foreach(cell, istmt->objects)
2848  {
2849  Oid nspid = lfirst_oid(cell);
2850  Form_pg_namespace pg_namespace_tuple;
2851  Datum aclDatum;
2852  bool isNull;
2853  AclMode avail_goptions;
2854  AclMode this_privileges;
2855  Acl *old_acl;
2856  Acl *new_acl;
2857  Oid grantorId;
2858  Oid ownerId;
2859  HeapTuple tuple;
2860  HeapTuple newtuple;
2861  Datum values[Natts_pg_namespace];
2862  bool nulls[Natts_pg_namespace];
2863  bool replaces[Natts_pg_namespace];
2864  int noldmembers;
2865  int nnewmembers;
2866  Oid *oldmembers;
2867  Oid *newmembers;
2868 
2870  if (!HeapTupleIsValid(tuple))
2871  elog(ERROR, "cache lookup failed for namespace %u", nspid);
2872 
2873  pg_namespace_tuple = (Form_pg_namespace) GETSTRUCT(tuple);
2874 
2875  /*
2876  * Get owner ID and working copy of existing ACL. If there's no ACL,
2877  * substitute the proper default.
2878  */
2879  ownerId = pg_namespace_tuple->nspowner;
2880  aclDatum = SysCacheGetAttr(NAMESPACENAME, tuple,
2881  Anum_pg_namespace_nspacl,
2882  &isNull);
2883  if (isNull)
2884  {
2885  old_acl = acldefault(OBJECT_SCHEMA, ownerId);
2886  /* There are no old member roles according to the catalogs */
2887  noldmembers = 0;
2888  oldmembers = NULL;
2889  }
2890  else
2891  {
2892  old_acl = DatumGetAclPCopy(aclDatum);
2893  /* Get the roles mentioned in the existing ACL */
2894  noldmembers = aclmembers(old_acl, &oldmembers);
2895  }
2896 
2897  /* Determine ID to do the grant as, and available grant options */
2898  select_best_grantor(GetUserId(), istmt->privileges,
2899  old_acl, ownerId,
2900  &grantorId, &avail_goptions);
2901 
2902  /*
2903  * Restrict the privileges to what we can actually grant, and emit the
2904  * standards-mandated warning and error messages.
2905  */
2906  this_privileges =
2907  restrict_and_check_grant(istmt->is_grant, avail_goptions,
2908  istmt->all_privs, istmt->privileges,
2909  nspid, grantorId, OBJECT_SCHEMA,
2910  NameStr(pg_namespace_tuple->nspname),
2911  0, NULL);
2912 
2913  /*
2914  * Generate new ACL.
2915  */
2916  new_acl = merge_acl_with_grant(old_acl, istmt->is_grant,
2917  istmt->grant_option, istmt->behavior,
2918  istmt->grantees, this_privileges,
2919  grantorId, ownerId);
2920 
2921  /*
2922  * We need the members of both old and new ACLs so we can correct the
2923  * shared dependency information.
2924  */
2925  nnewmembers = aclmembers(new_acl, &newmembers);
2926 
2927  /* finished building new ACL value, now insert it */
2928  MemSet(values, 0, sizeof(values));
2929  MemSet(nulls, false, sizeof(nulls));
2930  MemSet(replaces, false, sizeof(replaces));
2931 
2932  replaces[Anum_pg_namespace_nspacl - 1] = true;
2933  values[Anum_pg_namespace_nspacl - 1] = PointerGetDatum(new_acl);
2934 
2935  newtuple = heap_modify_tuple(tuple, RelationGetDescr(relation), values,
2936  nulls, replaces);
2937 
2938  CatalogTupleUpdate(relation, &newtuple->t_self, newtuple);
2939 
2940  /* Update initial privileges for extensions */
2941  recordExtensionInitPriv(nspid, NamespaceRelationId, 0, new_acl);
2942 
2943  /* Update the shared dependency ACL info */
2944  updateAclDependencies(NamespaceRelationId, pg_namespace_tuple->oid, 0,
2945  ownerId,
2946  noldmembers, oldmembers,
2947  nnewmembers, newmembers);
2948 
2949  ReleaseSysCache(tuple);
2950 
2951  pfree(new_acl);
2952 
2953  /* prevent error when processing duplicate objects */
2955  }
2956 
2957  table_close(relation, RowExclusiveLock);
2958 }
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:655
FormData_pg_namespace * Form_pg_namespace
Definition: pg_namespace.h:52
#define RelationGetDescr(relation)
Definition: rel.h:483
Oid GetUserId(void)
Definition: miscinit.c:476
#define PointerGetDatum(X)
Definition: postgres.h:556
Acl * acldefault(ObjectType objtype, Oid ownerId)
Definition: acl.c:740
#define MemSet(start, val, len)
Definition: c.h:996
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:72
void pfree(void *pointer)
Definition: mcxt.c:1057
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:45
#define ACL_NO_RIGHTS
Definition: parsenodes.h:88
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:5880
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
uintptr_t Datum
Definition: postgres.h:367
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:1464
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:302
void select_best_grantor(Oid roleId, AclMode privileges, const Acl *acl, Oid ownerId, Oid *grantorId, AclMode *grantOptions)
Definition: acl.c:5103
static Datum values[MAXATTR]
Definition: bootstrap.c:165
#define elog(elevel,...)
Definition: elog.h:228
#define NameStr(name)
Definition: c.h:669
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 1739 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().

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

2962 {
2963  Relation relation;
2964  ListCell *cell;
2965 
2966  if (istmt->all_privs && istmt->privileges == ACL_NO_RIGHTS)
2967  istmt->privileges = ACL_ALL_RIGHTS_TABLESPACE;
2968 
2969  relation = table_open(TableSpaceRelationId, RowExclusiveLock);
2970 
2971  foreach(cell, istmt->objects)
2972  {
2973  Oid tblId = lfirst_oid(cell);
2974  Form_pg_tablespace pg_tablespace_tuple;
2975  Datum aclDatum;
2976  bool isNull;
2977  AclMode avail_goptions;
2978  AclMode this_privileges;
2979  Acl *old_acl;
2980  Acl *new_acl;
2981  Oid grantorId;
2982  Oid ownerId;
2983  HeapTuple newtuple;
2984  Datum values[Natts_pg_tablespace];
2985  bool nulls[Natts_pg_tablespace];
2986  bool replaces[Natts_pg_tablespace];
2987  int noldmembers;
2988  int nnewmembers;
2989  Oid *oldmembers;
2990  Oid *newmembers;
2991  HeapTuple tuple;
2992 
2993  /* Search syscache for pg_tablespace */
2995  if (!HeapTupleIsValid(tuple))
2996  elog(ERROR, "cache lookup failed for tablespace %u", tblId);
2997 
2998  pg_tablespace_tuple = (Form_pg_tablespace) GETSTRUCT(tuple);
2999 
3000  /*
3001  * Get owner ID and working copy of existing ACL. If there's no ACL,
3002  * substitute the proper default.
3003  */
3004  ownerId = pg_tablespace_tuple->spcowner;
3005  aclDatum = heap_getattr(tuple, Anum_pg_tablespace_spcacl,
3006  RelationGetDescr(relation), &isNull);
3007  if (isNull)
3008  {
3009  old_acl = acldefault(OBJECT_TABLESPACE, ownerId);
3010  /* There are no old member roles according to the catalogs */
3011  noldmembers = 0;
3012  oldmembers = NULL;
3013  }
3014  else
3015  {
3016  old_acl = DatumGetAclPCopy(aclDatum);
3017  /* Get the roles mentioned in the existing ACL */
3018  noldmembers = aclmembers(old_acl, &oldmembers);
3019  }
3020 
3021  /* Determine ID to do the grant as, and available grant options */
3022  select_best_grantor(GetUserId(), istmt->privileges,
3023  old_acl, ownerId,
3024  &grantorId, &avail_goptions);
3025 
3026  /*
3027  * Restrict the privileges to what we can actually grant, and emit the
3028  * standards-mandated warning and error messages.
3029  */
3030  this_privileges =
3031  restrict_and_check_grant(istmt->is_grant, avail_goptions,
3032  istmt->all_privs, istmt->privileges,
3033  tblId, grantorId, OBJECT_TABLESPACE,
3034  NameStr(pg_tablespace_tuple->spcname),
3035  0, NULL);
3036 
3037  /*
3038  * Generate new ACL.
3039  */
3040  new_acl = merge_acl_with_grant(old_acl, istmt->is_grant,
3041  istmt->grant_option, istmt->behavior,
3042  istmt->grantees, this_privileges,
3043  grantorId, ownerId);
3044 
3045  /*
3046  * We need the members of both old and new ACLs so we can correct the
3047  * shared dependency information.
3048  */
3049  nnewmembers = aclmembers(new_acl, &newmembers);
3050 
3051  /* finished building new ACL value, now insert it */
3052  MemSet(values, 0, sizeof(values));
3053  MemSet(nulls, false, sizeof(nulls));
3054  MemSet(replaces, false, sizeof(replaces));
3055 
3056  replaces[Anum_pg_tablespace_spcacl - 1] = true;
3057  values[Anum_pg_tablespace_spcacl - 1] = PointerGetDatum(new_acl);
3058 
3059  newtuple = heap_modify_tuple(tuple, RelationGetDescr(relation), values,
3060  nulls, replaces);
3061 
3062  CatalogTupleUpdate(relation, &newtuple->t_self, newtuple);
3063 
3064  /* Update the shared dependency ACL info */
3065  updateAclDependencies(TableSpaceRelationId, tblId, 0,
3066  ownerId,
3067  noldmembers, oldmembers,
3068  nnewmembers, newmembers);
3069 
3070  ReleaseSysCache(tuple);
3071  pfree(new_acl);
3072 
3073  /* prevent error when processing duplicate objects */
3075  }
3076 
3077  table_close(relation, RowExclusiveLock);
3078 }
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:655
#define RelationGetDescr(relation)
Definition: rel.h:483
Oid GetUserId(void)
Definition: miscinit.c:476
#define PointerGetDatum(X)
Definition: postgres.h:556
Acl * acldefault(ObjectType objtype, Oid ownerId)
Definition: acl.c:740
#define MemSet(start, val, len)
Definition: c.h:996
#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:72
void pfree(void *pointer)
Definition: mcxt.c:1057
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:45
#define ACL_NO_RIGHTS
Definition: parsenodes.h:88
ItemPointerData t_self
Definition: htup.h:65
#define RowExclusiveLock
Definition: lockdefs.h:38
FormData_pg_tablespace * Form_pg_tablespace
Definition: pg_tablespace.h:46
#define heap_getattr(tup, attnum, tupleDesc, isnull)
Definition: htup_details.h:762
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
uintptr_t Datum
Definition: postgres.h:367
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:1464
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:302
void select_best_grantor(Oid roleId, AclMode privileges, const Acl *acl, Oid ownerId, Oid *grantorId, AclMode *grantOptions)
Definition: acl.c:5103
static Datum values[MAXATTR]
Definition: bootstrap.c:165
#define elog(elevel,...)
Definition: elog.h:228
#define NameStr(name)
Definition: c.h:669
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 3081 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().

3082 {
3083  Relation relation;
3084  ListCell *cell;
3085 
3086  if (istmt->all_privs && istmt->privileges == ACL_NO_RIGHTS)
3087  istmt->privileges = ACL_ALL_RIGHTS_TYPE;
3088 
3089  relation = table_open(TypeRelationId, RowExclusiveLock);
3090 
3091  foreach(cell, istmt->objects)
3092  {
3093  Oid typId = lfirst_oid(cell);
3094  Form_pg_type pg_type_tuple;
3095  Datum aclDatum;
3096  bool isNull;
3097  AclMode avail_goptions;
3098  AclMode this_privileges;
3099  Acl *old_acl;
3100  Acl *new_acl;
3101  Oid grantorId;
3102  Oid ownerId;
3103  HeapTuple newtuple;
3104  Datum values[Natts_pg_type];
3105  bool nulls[Natts_pg_type];
3106  bool replaces[Natts_pg_type];
3107  int noldmembers;
3108  int nnewmembers;
3109  Oid *oldmembers;
3110  Oid *newmembers;
3111  HeapTuple tuple;
3112 
3113  /* Search syscache for pg_type */
3114  tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typId));
3115  if (!HeapTupleIsValid(tuple))
3116  elog(ERROR, "cache lookup failed for type %u", typId);
3117 
3118  pg_type_tuple = (Form_pg_type) GETSTRUCT(tuple);
3119 
3120  if (IsTrueArrayType(pg_type_tuple))
3121  ereport(ERROR,
3122  (errcode(ERRCODE_INVALID_GRANT_OPERATION),
3123  errmsg("cannot set privileges of array types"),
3124  errhint("Set the privileges of the element type instead.")));
3125 
3126  /* Used GRANT DOMAIN on a non-domain? */
3127  if (istmt->objtype == OBJECT_DOMAIN &&
3128  pg_type_tuple->typtype != TYPTYPE_DOMAIN)
3129  ereport(ERROR,
3130  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
3131  errmsg("\"%s\" is not a domain",
3132  NameStr(pg_type_tuple->typname))));
3133 
3134  /*
3135  * Get owner ID and working copy of existing ACL. If there's no ACL,
3136  * substitute the proper default.
3137  */
3138  ownerId = pg_type_tuple->typowner;
3139  aclDatum = heap_getattr(tuple, Anum_pg_type_typacl,
3140  RelationGetDescr(relation), &isNull);
3141  if (isNull)
3142  {
3143  old_acl = acldefault(istmt->objtype, ownerId);
3144  /* There are no old member roles according to the catalogs */
3145  noldmembers = 0;
3146  oldmembers = NULL;
3147  }
3148  else
3149  {
3150  old_acl = DatumGetAclPCopy(aclDatum);
3151  /* Get the roles mentioned in the existing ACL */
3152  noldmembers = aclmembers(old_acl, &oldmembers);
3153  }
3154 
3155  /* Determine ID to do the grant as, and available grant options */
3156  select_best_grantor(GetUserId(), istmt->privileges,
3157  old_acl, ownerId,
3158  &grantorId, &avail_goptions);
3159 
3160  /*
3161  * Restrict the privileges to what we can actually grant, and emit the
3162  * standards-mandated warning and error messages.
3163  */
3164  this_privileges =
3165  restrict_and_check_grant(istmt->is_grant, avail_goptions,
3166  istmt->all_privs, istmt->privileges,
3167  typId, grantorId, OBJECT_TYPE,
3168  NameStr(pg_type_tuple->typname),
3169  0, NULL);
3170 
3171  /*
3172  * Generate new ACL.
3173  */
3174  new_acl = merge_acl_with_grant(old_acl, istmt->is_grant,
3175  istmt->grant_option, istmt->behavior,
3176  istmt->grantees, this_privileges,
3177  grantorId, ownerId);
3178 
3179  /*
3180  * We need the members of both old and new ACLs so we can correct the
3181  * shared dependency information.
3182  */
3183  nnewmembers = aclmembers(new_acl, &newmembers);
3184 
3185  /* finished building new ACL value, now insert it */
3186  MemSet(values, 0, sizeof(values));
3187  MemSet(nulls, false, sizeof(nulls));
3188  MemSet(replaces, false, sizeof(replaces));
3189 
3190  replaces[Anum_pg_type_typacl - 1] = true;
3191  values[Anum_pg_type_typacl - 1] = PointerGetDatum(new_acl);
3192 
3193  newtuple = heap_modify_tuple(tuple, RelationGetDescr(relation), values,
3194  nulls, replaces);
3195 
3196  CatalogTupleUpdate(relation, &newtuple->t_self, newtuple);
3197 
3198  /* Update initial privileges for extensions */
3199  recordExtensionInitPriv(typId, TypeRelationId, 0, new_acl);
3200 
3201  /* Update the shared dependency ACL info */
3202  updateAclDependencies(TypeRelationId, typId, 0,
3203  ownerId,
3204  noldmembers, oldmembers,
3205  nnewmembers, newmembers);
3206 
3207  ReleaseSysCache(tuple);
3208  pfree(new_acl);
3209 
3210  /* prevent error when processing duplicate objects */
3212  }
3213 
3214  table_close(relation, RowExclusiveLock);
3215 }
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:1162
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
#define RelationGetDescr(relation)
Definition: rel.h:483
Oid GetUserId(void)
Definition: miscinit.c:476
#define PointerGetDatum(X)
Definition: postgres.h:556
int errcode(int sqlerrcode)
Definition: elog.c:704
Acl * acldefault(ObjectType objtype, Oid ownerId)
Definition: acl.c:740
#define MemSet(start, val, len)
Definition: c.h:996
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:72
void pfree(void *pointer)
Definition: mcxt.c:1057
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:45
#define ACL_NO_RIGHTS
Definition: parsenodes.h:88
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:5880
#define heap_getattr(tup, attnum, tupleDesc, isnull)
Definition: htup_details.h:762
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
uintptr_t Datum
Definition: postgres.h:367
void CommandCounterIncrement(void)
Definition: xact.c:1021
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
#define ereport(elevel,...)
Definition: elog.h:155
int aclmembers(const Acl *acl, Oid **roleids)
Definition: acl.c:1464
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
void CatalogTupleUpdate(Relation heapRel, ItemPointer otid, HeapTuple tup)
Definition: indexing.c:302
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:5103
static Datum values[MAXATTR]
Definition: bootstrap.c:165
int errmsg(const char *fmt,...)
Definition: elog.c:915
#define elog(elevel,...)
Definition: elog.h:228
#define NameStr(name)
Definition: c.h:669
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 548 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().

549 {
550  switch (istmt->objtype)
551  {
552  case OBJECT_TABLE:
553  case OBJECT_SEQUENCE:
554  ExecGrant_Relation(istmt);
555  break;
556  case OBJECT_DATABASE:
557  ExecGrant_Database(istmt);
558  break;
559  case OBJECT_DOMAIN:
560  case OBJECT_TYPE:
561  ExecGrant_Type(istmt);
562  break;
563  case OBJECT_FDW:
564  ExecGrant_Fdw(istmt);
565  break;
568  break;
569  case OBJECT_FUNCTION:
570  case OBJECT_PROCEDURE:
571  case OBJECT_ROUTINE:
572  ExecGrant_Function(istmt);
573  break;
574  case OBJECT_LANGUAGE:
575  ExecGrant_Language(istmt);
576  break;
577  case OBJECT_LARGEOBJECT:
578  ExecGrant_Largeobject(istmt);
579  break;
580  case OBJECT_SCHEMA:
581  ExecGrant_Namespace(istmt);
582  break;
583  case OBJECT_TABLESPACE:
584  ExecGrant_Tablespace(istmt);
585  break;
586  default:
587  elog(ERROR, "unrecognized GrantStmt.objtype: %d",
588  (int) istmt->objtype);
589  }
590 
591  /*
592  * Pass the info to event triggers about the just-executed GRANT. Note
593  * that we prefer to do it after actually executing it, because that gives
594  * the functions a chance to adjust the istmt with privileges actually
595  * granted.
596  */
599 }
static void ExecGrant_Language(InternalGrant *grantStmt)
Definition: aclchk.c:2566
static void ExecGrant_Function(InternalGrant *grantStmt)
Definition: aclchk.c:2443
static void ExecGrant_Tablespace(InternalGrant *grantStmt)
Definition: aclchk.c:2961
static void ExecGrant_Namespace(InternalGrant *grantStmt)
Definition: aclchk.c:2837
static void ExecGrant_Type(InternalGrant *grantStmt)
Definition: aclchk.c:3081
static void ExecGrant_Database(InternalGrant *grantStmt)
Definition: aclchk.c:2071
#define ERROR
Definition: elog.h:45
static void ExecGrant_Fdw(InternalGrant *grantStmt)
Definition: aclchk.c:2191
static void ExecGrant_Largeobject(InternalGrant *grantStmt)
Definition: aclchk.c:2697
static void ExecGrant_Relation(InternalGrant *grantStmt)
Definition: aclchk.c:1739
bool EventTriggerSupportsObjectType(ObjectType obtype)
ObjectType objtype
#define elog(elevel,...)
Definition: elog.h:228
void EventTriggerCollectGrant(InternalGrant *istmt)
static void ExecGrant_ForeignServer(InternalGrant *grantStmt)
Definition: aclchk.c:2318

◆ 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, InternalGrant::grant_option, GrantStmt::grant_option, InternalGrant::grantees, GrantStmt::grantees, 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  /*
367  * Turn the regular GrantStmt into the InternalGrant form.
368  */
369  istmt.is_grant = stmt->is_grant;
370  istmt.objtype = stmt->objtype;
371 
372  /* Collect the OIDs of the target objects */
373  switch (stmt->targtype)
374  {
375  case ACL_TARGET_OBJECT:
376  istmt.objects = objectNamesToOids(stmt->objtype, stmt->objects);
377  break;
379  istmt.objects = objectsInSchemaToOids(stmt->objtype, stmt->objects);
380  break;
381  /* ACL_TARGET_DEFAULTS should not be seen here */
382  default:
383  elog(ERROR, "unrecognized GrantStmt.targtype: %d",
384  (int) stmt->targtype);
385  }
386 
387  /* all_privs to be filled below */
388  /* privileges to be filled below */
389  istmt.col_privs = NIL; /* may get filled below */
390  istmt.grantees = NIL; /* filled below */
391  istmt.grant_option = stmt->grant_option;
392  istmt.behavior = stmt->behavior;
393 
394  /*
395  * Convert the RoleSpec list into an Oid list. Note that at this point we
396  * insert an ACL_ID_PUBLIC into the list if appropriate, so downstream
397  * there shouldn't be any additional work needed to support this case.
398  */
399  foreach(cell, stmt->grantees)
400  {
401  RoleSpec *grantee = (RoleSpec *) lfirst(cell);
402  Oid grantee_uid;
403 
404  switch (grantee->roletype)
405  {
406  case ROLESPEC_PUBLIC:
407  grantee_uid = ACL_ID_PUBLIC;
408  break;
409  default:
410  grantee_uid = get_rolespec_oid(grantee, false);
411  break;
412  }
413  istmt.grantees = lappend_oid(istmt.grantees, grantee_uid);
414  }
415 
416  /*
417  * Convert stmt->privileges, a list of AccessPriv nodes, into an AclMode
418  * bitmask. Note: objtype can't be OBJECT_COLUMN.
419  */
420  switch (stmt->objtype)
421  {
422  case OBJECT_TABLE:
423 
424  /*
425  * Because this might be a sequence, we test both relation and
426  * sequence bits, and later do a more limited test when we know
427  * the object type.
428  */
430  errormsg = gettext_noop("invalid privilege type %s for relation");
431  break;
432  case OBJECT_SEQUENCE:
433  all_privileges = ACL_ALL_RIGHTS_SEQUENCE;
434  errormsg = gettext_noop("invalid privilege type %s for sequence");
435  break;
436  case OBJECT_DATABASE:
437  all_privileges = ACL_ALL_RIGHTS_DATABASE;
438  errormsg = gettext_noop("invalid privilege type %s for database");
439  break;
440  case OBJECT_DOMAIN:
441  all_privileges = ACL_ALL_RIGHTS_TYPE;
442  errormsg = gettext_noop("invalid privilege type %s for domain");
443  break;
444  case OBJECT_FUNCTION:
445  all_privileges = ACL_ALL_RIGHTS_FUNCTION;
446  errormsg = gettext_noop("invalid privilege type %s for function");
447  break;
448  case OBJECT_LANGUAGE:
449  all_privileges = ACL_ALL_RIGHTS_LANGUAGE;
450  errormsg = gettext_noop("invalid privilege type %s for language");
451  break;
452  case OBJECT_LARGEOBJECT:
453  all_privileges = ACL_ALL_RIGHTS_LARGEOBJECT;
454  errormsg = gettext_noop("invalid privilege type %s for large object");
455  break;
456  case OBJECT_SCHEMA:
457  all_privileges = ACL_ALL_RIGHTS_SCHEMA;
458  errormsg = gettext_noop("invalid privilege type %s for schema");
459  break;
460  case OBJECT_PROCEDURE:
461  all_privileges = ACL_ALL_RIGHTS_FUNCTION;
462  errormsg = gettext_noop("invalid privilege type %s for procedure");
463  break;
464  case OBJECT_ROUTINE:
465  all_privileges = ACL_ALL_RIGHTS_FUNCTION;
466  errormsg = gettext_noop("invalid privilege type %s for routine");
467  break;
468  case OBJECT_TABLESPACE:
469  all_privileges = ACL_ALL_RIGHTS_TABLESPACE;
470  errormsg = gettext_noop("invalid privilege type %s for tablespace");
471  break;
472  case OBJECT_TYPE:
473  all_privileges = ACL_ALL_RIGHTS_TYPE;
474  errormsg = gettext_noop("invalid privilege type %s for type");
475  break;
476  case OBJECT_FDW:
477  all_privileges = ACL_ALL_RIGHTS_FDW;
478  errormsg = gettext_noop("invalid privilege type %s for foreign-data wrapper");
479  break;
481  all_privileges = ACL_ALL_RIGHTS_FOREIGN_SERVER;
482  errormsg = gettext_noop("invalid privilege type %s for foreign server");
483  break;
484  default:
485  elog(ERROR, "unrecognized GrantStmt.objtype: %d",
486  (int) stmt->objtype);
487  /* keep compiler quiet */
488  all_privileges = ACL_NO_RIGHTS;
489  errormsg = NULL;
490  }
491 
492  if (stmt->privileges == NIL)
493  {
494  istmt.all_privs = true;
495 
496  /*
497  * will be turned into ACL_ALL_RIGHTS_* by the internal routines
498  * depending on the object type
499  */
500  istmt.privileges = ACL_NO_RIGHTS;
501  }
502  else
503  {
504  istmt.all_privs = false;
505  istmt.privileges = ACL_NO_RIGHTS;
506 
507  foreach(cell, stmt->privileges)
508  {
509  AccessPriv *privnode = (AccessPriv *) lfirst(cell);
510  AclMode priv;
511 
512  /*
513  * If it's a column-level specification, we just set it aside in
514  * col_privs for the moment; but insist it's for a relation.
515  */
516  if (privnode->cols)
517  {
518  if (stmt->objtype != OBJECT_TABLE)
519  ereport(ERROR,
520  (errcode(ERRCODE_INVALID_GRANT_OPERATION),
521  errmsg("column privileges are only valid for relations")));
522  istmt.col_privs = lappend(istmt.col_privs, privnode);
523  continue;
524  }
525 
526  if (privnode->priv_name == NULL) /* parser mistake? */
527  elog(ERROR, "AccessPriv node must specify privilege or columns");
528  priv = string_to_privilege(privnode->priv_name);
529 
530  if (priv & ~((AclMode) all_privileges))
531  ereport(ERROR,
532  (errcode(ERRCODE_INVALID_GRANT_OPERATION),
533  errmsg(errormsg, privilege_to_string(priv))));
534 
535  istmt.privileges |= priv;
536  }
537  }
538 
539  ExecGrantStmt_oids(&istmt);
540 }
#define NIL
Definition: pg_list.h:65
#define ACL_ALL_RIGHTS_FUNCTION
Definition: acl.h:162
static AclMode string_to_privilege(const char *privname)
Definition: aclchk.c:3219
AclMode privileges
bool grant_option
Definition: parsenodes.h:1951
#define gettext_noop(x)
Definition: c.h:1185
int errcode(int sqlerrcode)
Definition: elog.c:704
#define ACL_ALL_RIGHTS_TABLESPACE
Definition: acl.h:166
List * cols
Definition: parsenodes.h:1981
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:72
#define ERROR
Definition: elog.h:45
#define ACL_NO_RIGHTS
Definition: parsenodes.h:88
#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:1943
static void ExecGrantStmt_oids(InternalGrant *istmt)
Definition: aclchk.c:548
static List * objectNamesToOids(ObjectType objtype, List *objnames)
Definition: aclchk.c:612
RoleSpecType roletype
Definition: parsenodes.h:328
Oid get_rolespec_oid(const RoleSpec *role, bool missing_ok)
Definition: acl.c:5212
#define ACL_ALL_RIGHTS_SEQUENCE
Definition: acl.h:158
List * privileges
Definition: parsenodes.h:1948
static List * objectsInSchemaToOids(ObjectType objtype, List *nspnames)
Definition: aclchk.c:761
#define ereport(elevel,...)
Definition: elog.h:155
#define ACL_ALL_RIGHTS_LARGEOBJECT
Definition: acl.h:164
DropBehavior behavior
DropBehavior behavior
Definition: parsenodes.h:1952
#define lfirst(lc)
Definition: pg_list.h:169
List * objects
Definition: parsenodes.h:1946
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:915
#define elog(elevel,...)
Definition: elog.h:228
#define ACL_ALL_RIGHTS_RELATION
Definition: acl.h:157
ObjectType objtype
Definition: parsenodes.h:1945
#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:3256
List * grantees
Definition: parsenodes.h:1950
GrantTargetType targtype
Definition: parsenodes.h:1944
char * priv_name
Definition: parsenodes.h:1980

◆ 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 1545 of file aclchk.c.

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

Referenced by ExecGrant_Relation().

1549 {
1550  AttrNumber curr_att;
1551 
1552  Assert(classForm->relnatts - FirstLowInvalidHeapAttributeNumber < num_col_privileges);
1553  for (curr_att = FirstLowInvalidHeapAttributeNumber + 1;
1554  curr_att <= classForm->relnatts;
1555  curr_att++)
1556  {
1557  HeapTuple attTuple;
1558  bool isdropped;
1559 
1560  if (curr_att == InvalidAttrNumber)
1561  continue;
1562 
1563  /* Views don't have any system columns at all */
1564  if (classForm->relkind == RELKIND_VIEW && curr_att < 0)
1565  continue;
1566 
1567  attTuple = SearchSysCache2(ATTNUM,
1568  ObjectIdGetDatum(table_oid),
1569  Int16GetDatum(curr_att));
1570  if (!HeapTupleIsValid(attTuple))
1571  elog(ERROR, "cache lookup failed for attribute %d of relation %u",
1572  curr_att, table_oid);
1573 
1574  isdropped = ((Form_pg_attribute) GETSTRUCT(attTuple))->attisdropped;
1575 
1576  ReleaseSysCache(attTuple);
1577 
1578  /* ignore dropped columns */
1579  if (isdropped)
1580  continue;
1581 
1582  col_privileges[curr_att - FirstLowInvalidHeapAttributeNumber] |= this_privileges;
1583  }
1584 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
#define Int16GetDatum(X)
Definition: postgres.h:451
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:45
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:193
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:792
HeapTuple SearchSysCache2(int cacheId, Datum key1, Datum key2)
Definition: syscache.c:1138
#define InvalidAttrNumber
Definition: attnum.h:23
#define elog(elevel,...)
Definition: elog.h:228
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 1512 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().

1516 {
1517  ListCell *cell;
1518 
1519  foreach(cell, colnames)
1520  {
1521  char *colname = strVal(lfirst(cell));
1523 
1524  attnum = get_attnum(table_oid, colname);
1525  if (attnum == InvalidAttrNumber)
1526  ereport(ERROR,
1527  (errcode(ERRCODE_UNDEFINED_COLUMN),
1528  errmsg("column \"%s\" of relation \"%s\" does not exist",
1529  colname, get_rel_name(table_oid))));
1531  if (attnum <= 0 || attnum >= num_col_privileges)
1532  elog(ERROR, "column number out of range"); /* safety check */
1533  col_privileges[attnum] |= this_privileges;
1534  }
1535 }
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:704
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
#define ERROR
Definition: elog.h:45
AttrNumber get_attnum(Oid relid, const char *attname)
Definition: lsyscache.c:856
int16 attnum
Definition: pg_attribute.h:79
#define ereport(elevel,...)
Definition: elog.h:155
#define lfirst(lc)
Definition: pg_list.h:169
#define InvalidAttrNumber
Definition: attnum.h:23
int errmsg(const char *fmt,...)
Definition: elog.c:915
#define elog(elevel,...)
Definition: elog.h:228
char * get_rel_name(Oid relid)
Definition: lsyscache.c:1872
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 5358 of file aclchk.c.

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

Referenced by get_user_default_acl().

5359 {
5360  Acl *result = NULL;
5361  HeapTuple tuple;
5362 
5364  ObjectIdGetDatum(roleId),
5365  ObjectIdGetDatum(nsp_oid),
5366  CharGetDatum(objtype));
5367 
5368  if (HeapTupleIsValid(tuple))
5369  {
5370  Datum aclDatum;
5371  bool isNull;
5372 
5373  aclDatum = SysCacheGetAttr(DEFACLROLENSPOBJ, tuple,
5374  Anum_pg_default_acl_defaclacl,
5375  &isNull);
5376  if (!isNull)
5377  result = DatumGetAclPCopy(aclDatum);
5378  ReleaseSysCache(tuple);
5379  }
5380 
5381  return result;
5382 }
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
HeapTuple SearchSysCache3(int cacheId, Datum key1, Datum key2, Datum key3)
Definition: syscache.c:1149
uintptr_t Datum
Definition: postgres.h:367
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:416
#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 5393 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().

5394 {
5395  Acl *result;
5396  Acl *glob_acl;
5397  Acl *schema_acl;
5398  Acl *def_acl;
5399  char defaclobjtype;
5400 
5401  /*
5402  * Use NULL during bootstrap, since pg_default_acl probably isn't there
5403  * yet.
5404  */
5406  return NULL;
5407 
5408  /* Check if object type is supported in pg_default_acl */
5409  switch (objtype)
5410  {
5411  case OBJECT_TABLE:
5412  defaclobjtype = DEFACLOBJ_RELATION;
5413  break;
5414 
5415  case OBJECT_SEQUENCE:
5416  defaclobjtype = DEFACLOBJ_SEQUENCE;
5417  break;
5418 
5419  case OBJECT_FUNCTION:
5420  defaclobjtype = DEFACLOBJ_FUNCTION;
5421  break;
5422 
5423  case OBJECT_TYPE:
5424  defaclobjtype = DEFACLOBJ_TYPE;
5425  break;
5426 
5427  case OBJECT_SCHEMA:
5428  defaclobjtype = DEFACLOBJ_NAMESPACE;
5429  break;
5430 
5431  default:
5432  return NULL;
5433  }
5434 
5435  /* Look up the relevant pg_default_acl entries */
5436  glob_acl = get_default_acl_internal(ownerId, InvalidOid, defaclobjtype);
5437  schema_acl = get_default_acl_internal(ownerId, nsp_oid, defaclobjtype);
5438 
5439  /* Quick out if neither entry exists */
5440  if (glob_acl == NULL && schema_acl == NULL)
5441  return NULL;
5442 
5443  /* We need to know the hard-wired default value, too */
5444  def_acl = acldefault(objtype, ownerId);
5445 
5446  /* If there's no global entry, substitute the hard-wired default */
5447  if (glob_acl == NULL)
5448  glob_acl = def_acl;
5449 
5450  /* Merge in any per-schema privileges */
5451  result = aclmerge(glob_acl, schema_acl, ownerId);
5452 
5453  /*
5454  * For efficiency, we want to return NULL if the result equals default.
5455  * This requires sorting both arrays to get an accurate comparison.
5456  */
5457  aclitemsort(result);
5458  aclitemsort(def_acl);
5459  if (aclequal(result, def_acl))
5460  result = NULL;
5461 
5462  return result;
5463 }
Acl * acldefault(ObjectType objtype, Oid ownerId)
Definition: acl.c:740
static Acl * get_default_acl_internal(Oid roleId, Oid nsp_oid, char objtype)
Definition: aclchk.c:5358
bool aclequal(const Acl *left_acl, const Acl *right_acl)
Definition: acl.c:501
#define InvalidOid
Definition: postgres_ext.h:36
Acl * aclmerge(const Acl *left_acl, const Acl *right_acl, Oid ownerId)
Definition: acl.c:443
void aclitemsort(Acl *acl)
Definition: acl.c:487
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:394

◆ getRelationsInNamespace()

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

Definition at line 850 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().

851 {
852  List *relations = NIL;
853  ScanKeyData key[2];
854  Relation rel;
855  TableScanDesc scan;
856  HeapTuple tuple;
857 
858  ScanKeyInit(&key[0],
859  Anum_pg_class_relnamespace,
860  BTEqualStrategyNumber, F_OIDEQ,
861  ObjectIdGetDatum(namespaceId));
862  ScanKeyInit(&key[1],
863  Anum_pg_class_relkind,
864  BTEqualStrategyNumber, F_CHAREQ,
865  CharGetDatum(relkind));
866 
867  rel = table_open(RelationRelationId, AccessShareLock);
868  scan = table_beginscan_catalog(rel, 2, key);
869 
870  while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
871  {
872  Oid oid = ((Form_pg_class) GETSTRUCT(tuple))->oid;
873 
874  relations = lappend_oid(relations, oid);
875  }
876 
877  table_endscan(scan);
879 
880  return relations;
881 }
#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:655
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:507
HeapTuple heap_getnext(TableScanDesc sscan, ScanDirection direction)
Definition: heapam.c:1304
#define CharGetDatum(X)
Definition: postgres.h:416
static void table_endscan(TableScanDesc scan)
Definition: tableam.h:962
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 5334 of file aclchk.c.

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

Referenced by check_enable_rls(), and RI_Initial_Check().

5335 {
5336  bool result = false;
5337  HeapTuple utup;
5338 
5339  /* Superusers bypass all permission checking. */
5340  if (superuser_arg(roleid))
5341  return true;
5342 
5343  utup = SearchSysCache1(AUTHOID, ObjectIdGetDatum(roleid));
5344  if (HeapTupleIsValid(utup))
5345  {
5346  result = ((Form_pg_authid) GETSTRUCT(utup))->rolbypassrls;
5347  ReleaseSysCache(utup);
5348  }
5349  return result;
5350 }
bool rolbypassrls
Definition: pg_authid.h:41
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
FormData_pg_authid * Form_pg_authid
Definition: pg_authid.h:56
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
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 5315 of file aclchk.c.

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

Referenced by check_object_ownership(), and have_createrole_privilege().

5316 {
5317  bool result = false;
5318  HeapTuple utup;
5319 
5320  /* Superusers bypass all permission checking. */
5321  if (superuser_arg(roleid))
5322  return true;
5323 
5324  utup = SearchSysCache1(AUTHOID, ObjectIdGetDatum(roleid));
5325  if (HeapTupleIsValid(utup))
5326  {
5327  result = ((Form_pg_authid) GETSTRUCT(utup))->rolcreaterole;
5328  ReleaseSysCache(utup);
5329  }
5330  return result;
5331 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
FormData_pg_authid * Form_pg_authid
Definition: pg_authid.h:56
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
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:704
Oid ai_grantor
Definition: acl.h:57
void pfree(void *pointer)
Definition: mcxt.c:1057
#define ERROR
Definition: elog.h:45
#define ACL_NO_RIGHTS
Definition: parsenodes.h:88
#define ACL_MODECHG_ADD
Definition: acl.h:129
#define ereport(elevel,...)
Definition: elog.h:155
Definition: acl.h:54
int errmsg(const char *fmt,...)
Definition: elog.c:915
#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:922
#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 612 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().

613 {
614  List *objects = NIL;
615  ListCell *cell;
616 
617  Assert(objnames != NIL);
618 
619  switch (objtype)
620  {
621  case OBJECT_TABLE:
622  case OBJECT_SEQUENCE:
623  foreach(cell, objnames)
624  {
625  RangeVar *relvar = (RangeVar *) lfirst(cell);
626  Oid relOid;
627 
628  relOid = RangeVarGetRelid(relvar, NoLock, false);
629  objects = lappend_oid(objects, relOid);
630  }
631  break;
632  case OBJECT_DATABASE:
633  foreach(cell, objnames)
634  {
635  char *dbname = strVal(lfirst(cell));
636  Oid dbid;
637 
638  dbid = get_database_oid(dbname, false);
639  objects = lappend_oid(objects, dbid);
640  }
641  break;
642  case OBJECT_DOMAIN:
643  case OBJECT_TYPE:
644  foreach(cell, objnames)
645  {
646  List *typname = (List *) lfirst(cell);
647  Oid oid;
648 
649  oid = typenameTypeId(NULL, makeTypeNameFromNameList(typname));
650  objects = lappend_oid(objects, oid);
651  }
652  break;
653  case OBJECT_FUNCTION:
654  foreach(cell, objnames)
655  {
656  ObjectWithArgs *func = (ObjectWithArgs *) lfirst(cell);
657  Oid funcid;
658 
659  funcid = LookupFuncWithArgs(OBJECT_FUNCTION, func, false);
660  objects = lappend_oid(objects, funcid);
661  }
662  break;
663  case OBJECT_LANGUAGE:
664  foreach(cell, objnames)
665  {
666  char *langname = strVal(lfirst(cell));
667  Oid oid;
668 
669  oid = get_language_oid(langname, false);
670  objects = lappend_oid(objects, oid);
671  }
672  break;
673  case OBJECT_LARGEOBJECT:
674  foreach(cell, objnames)
675  {
676  Oid lobjOid = oidparse(lfirst(cell));
677 
678  if (!LargeObjectExists(lobjOid))
679  ereport(ERROR,
680  (errcode(ERRCODE_UNDEFINED_OBJECT),
681  errmsg("large object %u does not exist",
682  lobjOid)));
683 
684  objects = lappend_oid(objects, lobjOid);
685  }
686  break;
687  case OBJECT_SCHEMA:
688  foreach(cell, objnames)
689  {
690  char *nspname = strVal(lfirst(cell));
691  Oid oid;
692 
693  oid = get_namespace_oid(nspname, false);
694  objects = lappend_oid(objects, oid);
695  }
696  break;
697  case OBJECT_PROCEDURE:
698  foreach(cell, objnames)
699  {
700  ObjectWithArgs *func = (ObjectWithArgs *) lfirst(cell);
701  Oid procid;
702 
703  procid = LookupFuncWithArgs(OBJECT_PROCEDURE, func, false);
704  objects = lappend_oid(objects, procid);
705  }
706  break;
707  case OBJECT_ROUTINE:
708  foreach(cell, objnames)
709  {
710  ObjectWithArgs *func = (ObjectWithArgs *) lfirst(cell);
711  Oid routid;
712 
713  routid = LookupFuncWithArgs(OBJECT_ROUTINE, func, false);
714  objects = lappend_oid(objects, routid);
715  }
716  break;
717  case OBJECT_TABLESPACE:
718  foreach(cell, objnames)
719  {
720  char *spcname = strVal(lfirst(cell));
721  Oid spcoid;
722 
723  spcoid = get_tablespace_oid(spcname, false);
724  objects = lappend_oid(objects, spcoid);
725  }
726  break;
727  case OBJECT_FDW:
728  foreach(cell, objnames)
729  {
730  char *fdwname = strVal(lfirst(cell));
731  Oid fdwid = get_foreign_data_wrapper_oid(fdwname, false);
732 
733  objects = lappend_oid(objects, fdwid);
734  }
735  break;
737  foreach(cell, objnames)
738  {
739  char *srvname = strVal(lfirst(cell));
740  Oid srvid = get_foreign_server_oid(srvname, false);
741 
742  objects = lappend_oid(objects, srvid);
743  }
744  break;
745  default:
746  elog(ERROR, "unrecognized GrantStmt.objtype: %d",
747  (int) objtype);
748  }
749 
750  return objects;
751 }
#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:704
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:45
#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:155
#define Assert(condition)
Definition: c.h:792
#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:915
#define elog(elevel,...)
Definition: elog.h:228
Oid LookupFuncWithArgs(ObjectType objtype, ObjectWithArgs *func, bool missing_ok)
Definition: parse_func.c:2171
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 761 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().

762 {
763  List *objects = NIL;
764  ListCell *cell;
765 
766  foreach(cell, nspnames)
767  {
768  char *nspname = strVal(lfirst(cell));
769  Oid namespaceId;
770  List *objs;
771 
772  namespaceId = LookupExplicitNamespace(nspname, false);
773 
774  switch (objtype)
775  {
776  case OBJECT_TABLE:
777  objs = getRelationsInNamespace(namespaceId, RELKIND_RELATION);
778  objects = list_concat(objects, objs);
779  objs = getRelationsInNamespace(namespaceId, RELKIND_VIEW);
780  objects = list_concat(objects, objs);
781  objs = getRelationsInNamespace(namespaceId, RELKIND_MATVIEW);
782  objects = list_concat(objects, objs);
783  objs = getRelationsInNamespace(namespaceId, RELKIND_FOREIGN_TABLE);
784  objects = list_concat(objects, objs);
785  objs = getRelationsInNamespace(namespaceId, RELKIND_PARTITIONED_TABLE);
786  objects = list_concat(objects, objs);
787  break;
788  case OBJECT_SEQUENCE:
789  objs = getRelationsInNamespace(namespaceId, RELKIND_SEQUENCE);
790  objects = list_concat(objects, objs);
791  break;
792  case OBJECT_FUNCTION:
793  case OBJECT_PROCEDURE:
794  case OBJECT_ROUTINE:
795  {
796  ScanKeyData key[2];
797  int keycount;
798  Relation rel;
799  TableScanDesc scan;
800  HeapTuple tuple;
801 
802  keycount = 0;
803  ScanKeyInit(&key[keycount++],
804  Anum_pg_proc_pronamespace,
805  BTEqualStrategyNumber, F_OIDEQ,
806  ObjectIdGetDatum(namespaceId));
807 
808  if (objtype == OBJECT_FUNCTION)
809  /* includes aggregates and window functions */
810  ScanKeyInit(&key[keycount++],
811  Anum_pg_proc_prokind,
812  BTEqualStrategyNumber, F_CHARNE,
813  CharGetDatum(PROKIND_PROCEDURE));
814  else if (objtype == OBJECT_PROCEDURE)
815  ScanKeyInit(&key[keycount++],
816  Anum_pg_proc_prokind,
817  BTEqualStrategyNumber, F_CHAREQ,
818  CharGetDatum(PROKIND_PROCEDURE));
819 
820  rel = table_open(ProcedureRelationId, AccessShareLock);
821  scan = table_beginscan_catalog(rel, keycount, key);
822 
823  while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
824  {
825  Oid oid = ((Form_pg_proc) GETSTRUCT(tuple))->oid;
826 
827  objects = lappend_oid(objects, oid);
828  }
829 
830  table_endscan(scan);
832  }
833  break;
834  default:
835  /* should not happen */
836  elog(ERROR, "unrecognized GrantStmt.objtype: %d",
837  (int) objtype);
838  }
839  }
840 
841  return objects;
842 }
#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:655
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:507
#define ERROR
Definition: elog.h:45
HeapTuple heap_getnext(TableScanDesc sscan, ScanDirection direction)
Definition: heapam.c:1304
static List * getRelationsInNamespace(Oid namespaceId, char relkind)
Definition: aclchk.c:850
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:133
#define lfirst(lc)
Definition: pg_list.h:169
#define CharGetDatum(X)
Definition: postgres.h:416
static void table_endscan(TableScanDesc scan)
Definition: tableam.h:962
#define elog(elevel,...)
Definition: elog.h:228
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 3622 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().

3624 {
3625  switch (objtype)
3626  {
3627  case OBJECT_COLUMN:
3628  return
3629  pg_class_aclmask(table_oid, roleid, mask, how) |
3630  pg_attribute_aclmask(table_oid, attnum, roleid, mask, how);
3631  case OBJECT_TABLE:
3632  case OBJECT_SEQUENCE:
3633  return pg_class_aclmask(table_oid, roleid, mask, how);
3634  case OBJECT_DATABASE:
3635  return pg_database_aclmask(table_oid, roleid, mask, how);
3636  case OBJECT_FUNCTION:
3637  return pg_proc_aclmask(table_oid, roleid, mask, how);
3638  case OBJECT_LANGUAGE:
3639  return pg_language_aclmask(table_oid, roleid, mask, how);
3640  case OBJECT_LARGEOBJECT:
3641  return pg_largeobject_aclmask_snapshot(table_oid, roleid,
3642  mask, how, NULL);
3643  case OBJECT_SCHEMA:
3644  return pg_namespace_aclmask(table_oid, roleid, mask, how);
3645  case OBJECT_STATISTIC_EXT:
3646  elog(ERROR, "grantable rights not supported for statistics objects");
3647  /* not reached, but keep compiler quiet */
3648  return ACL_NO_RIGHTS;
3649  case OBJECT_TABLESPACE:
3650  return pg_tablespace_aclmask(table_oid, roleid, mask, how);
3651  case OBJECT_FDW:
3652  return pg_foreign_data_wrapper_aclmask(table_oid, roleid, mask, how);
3653  case OBJECT_FOREIGN_SERVER:
3654  return pg_foreign_server_aclmask(table_oid, roleid, mask, how);
3655  case OBJECT_EVENT_TRIGGER:
3656  elog(ERROR, "grantable rights not supported for event triggers");
3657  /* not reached, but keep compiler quiet */
3658  return ACL_NO_RIGHTS;
3659  case OBJECT_TYPE:
3660  return pg_type_aclmask(table_oid, roleid, mask, how);
3661  default:
3662  elog(ERROR, "unrecognized objtype: %d",
3663  (int) objtype);
3664  /* not reached, but keep compiler quiet */
3665  return ACL_NO_RIGHTS;
3666  }
3667 }
AclMode pg_foreign_server_aclmask(Oid srv_oid, Oid roleid, AclMode mask, AclMaskHow how)
Definition: aclchk.c:4307
AclMode pg_attribute_aclmask(Oid table_oid, AttrNumber attnum, Oid roleid, AclMode mask, AclMaskHow how)
Definition: aclchk.c:3690
AclMode pg_tablespace_aclmask(Oid spc_oid, Oid roleid, AclMode mask, AclMaskHow how)
Definition: aclchk.c:4188
AclMode pg_class_aclmask(Oid table_oid, Oid roleid, AclMode mask, AclMaskHow how)
Definition: aclchk.c:3775
AclMode pg_database_aclmask(Oid db_oid, Oid roleid, AclMode mask, AclMaskHow how)
Definition: aclchk.c:3862
#define ERROR
Definition: elog.h:45
#define ACL_NO_RIGHTS
Definition: parsenodes.h:88
AclMode pg_namespace_aclmask(Oid nsp_oid, Oid roleid, AclMode mask, AclMaskHow how)
Definition: aclchk.c:4106
int16 attnum
Definition: pg_attribute.h:79
AclMode pg_type_aclmask(Oid type_oid, Oid roleid, AclMode mask, AclMaskHow how)
Definition: aclchk.c:4368
AclMode pg_language_aclmask(Oid lang_oid, Oid roleid, AclMode mask, AclMaskHow how)
Definition: aclchk.c:3970
AclMode pg_proc_aclmask(Oid proc_oid, Oid roleid, AclMode mask, AclMaskHow how)
Definition: aclchk.c:3916
#define elog(elevel,...)
Definition: elog.h:228
AclMode pg_foreign_data_wrapper_aclmask(Oid fdw_oid, Oid roleid, AclMode mask, AclMaskHow how)
Definition: aclchk.c:4245