| 1 |
/*------------------------------------------------------------------------- |
| 2 |
* |
| 3 |
* policy.c |
| 4 |
* Commands for manipulating policies. |
| 5 |
* |
| 6 |
* Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group |
| 7 |
* Portions Copyright (c) 1994, Regents of the University of California |
| 8 |
* |
| 9 |
* src/backend/commands/policy.c |
| 10 |
* |
| 11 |
*------------------------------------------------------------------------- |
| 12 |
*/ |
| 13 |
#include "postgres.h" |
| 14 |
|
| 15 |
#include "access/genam.h" |
| 16 |
#include "access/htup.h" |
| 17 |
#include "access/htup_details.h" |
| 18 |
#include "access/relation.h" |
| 19 |
#include "access/table.h" |
| 20 |
#include "access/xact.h" |
| 21 |
#include "catalog/catalog.h" |
| 22 |
#include "catalog/dependency.h" |
| 23 |
#include "catalog/indexing.h" |
| 24 |
#include "catalog/namespace.h" |
| 25 |
#include "catalog/objectaccess.h" |
| 26 |
#include "catalog/pg_authid.h" |
| 27 |
#include "catalog/pg_policy.h" |
| 28 |
#include "catalog/pg_type.h" |
| 29 |
#include "commands/policy.h" |
| 30 |
#include "miscadmin.h" |
| 31 |
#include "nodes/pg_list.h" |
| 32 |
#include "parser/parse_clause.h" |
| 33 |
#include "parser/parse_collate.h" |
| 34 |
#include "parser/parse_node.h" |
| 35 |
#include "parser/parse_relation.h" |
| 36 |
#include "rewrite/rewriteManip.h" |
| 37 |
#include "rewrite/rowsecurity.h" |
| 38 |
#include "utils/acl.h" |
| 39 |
#include "utils/array.h" |
| 40 |
#include "utils/builtins.h" |
| 41 |
#include "utils/fmgroids.h" |
| 42 |
#include "utils/inval.h" |
| 43 |
#include "utils/lsyscache.h" |
| 44 |
#include "utils/memutils.h" |
| 45 |
#include "utils/rel.h" |
| 46 |
#include "utils/syscache.h" |
| 47 |
|
| 48 |
static void RangeVarCallbackForPolicy(const RangeVar *rv, |
| 49 |
Oid relid, Oid oldrelid, void *arg); |
| 50 |
static char parse_policy_command(const char *cmd_name); |
| 51 |
static Datum *policy_role_list_to_array(List *roles, int *num_roles); |
| 52 |
|
| 53 |
/* |
| 54 |
* Callback to RangeVarGetRelidExtended(). |
| 55 |
* |
| 56 |
* Checks the following: |
| 57 |
* - the relation specified is a table. |
| 58 |
* - current user owns the table. |
| 59 |
* - the table is not a system table. |
| 60 |
* |
| 61 |
* If any of these checks fails then an error is raised. |
| 62 |
*/ |
| 63 |
static void |
| 64 |
RangeVarCallbackForPolicy(const RangeVar *rv, Oid relid, Oid oldrelid, |
| 65 |
void *arg) |
| 66 |
{ |
| 67 |
HeapTuple tuple; |
| 68 |
Form_pg_class classform; |
| 69 |
char relkind; |
| 70 |
|
| 71 |
tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid)); |
| 72 |
if (!HeapTupleIsValid(tuple)) |
| 73 |
return; |
| 74 |
|
| 75 |
classform = (Form_pg_class) GETSTRUCT(tuple); |
| 76 |
relkind = classform->relkind; |
| 77 |
|
| 78 |
/* Must own relation. */ |
| 79 |
if (!object_ownercheck(RelationRelationId, relid, GetUserId())) |
| 80 |
aclcheck_error(ACLCHECK_NOT_OWNER, get_relkind_objtype(get_rel_relkind(relid)), rv->relname); |
| 81 |
|
| 82 |
/* No system table modifications unless explicitly allowed. */ |
| 83 |
if (!allowSystemTableMods && IsSystemClass(relid, classform)) |
| 84 |
ereport(ERROR, |
| 85 |
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), |
| 86 |
errmsg("permission denied: \"%s\" is a system catalog", |
| 87 |
rv->relname))); |
| 88 |
|
| 89 |
/* Relation type MUST be a table. */ |
| 90 |
if (relkind != RELKIND_RELATION && relkind != RELKIND_PARTITIONED_TABLE) |
| 91 |
ereport(ERROR, |
| 92 |
(errcode(ERRCODE_WRONG_OBJECT_TYPE), |
| 93 |
errmsg("\"%s\" is not a table", rv->relname))); |
| 94 |
|
| 95 |
ReleaseSysCache(tuple); |
| 96 |
} |
| 97 |
|
| 98 |
/* |
| 99 |
* parse_policy_command - |
| 100 |
* helper function to convert full command strings to their char |
| 101 |
* representation. |
| 102 |
* |
| 103 |
* cmd_name - full string command name. Valid values are 'all', 'select', |
| 104 |
* 'insert', 'update' and 'delete'. |
| 105 |
* |
| 106 |
*/ |
| 107 |
static char |
| 108 |
parse_policy_command(const char *cmd_name) |
| 109 |
{ |
| 110 |
char polcmd; |
| 111 |
|
| 112 |
if (!cmd_name) |
| 113 |
elog(ERROR, "unrecognized policy command"); |
| 114 |
|
| 115 |
if (strcmp(cmd_name, "all") == 0) |
| 116 |
polcmd = '*'; |
| 117 |
else if (strcmp(cmd_name, "select") == 0) |
| 118 |
polcmd = ACL_SELECT_CHR; |
| 119 |
else if (strcmp(cmd_name, "insert") == 0) |
| 120 |
polcmd = ACL_INSERT_CHR; |
| 121 |
else if (strcmp(cmd_name, "update") == 0) |
| 122 |
polcmd = ACL_UPDATE_CHR; |
| 123 |
else if (strcmp(cmd_name, "delete") == 0) |
| 124 |
polcmd = ACL_DELETE_CHR; |
| 125 |
else |
| 126 |
elog(ERROR, "unrecognized policy command"); |
| 127 |
|
| 128 |
return polcmd; |
| 129 |
} |
| 130 |
|
| 131 |
/* |
| 132 |
* policy_role_list_to_array |
| 133 |
* helper function to convert a list of RoleSpecs to an array of |
| 134 |
* role id Datums. |
| 135 |
*/ |
| 136 |
static Datum * |
| 137 |
policy_role_list_to_array(List *roles, int *num_roles) |
| 138 |
{ |
| 139 |
Datum *role_oids; |
| 140 |
ListCell *cell; |
| 141 |
int i = 0; |
| 142 |
|
| 143 |
/* Handle no roles being passed in as being for public */ |
| 144 |
if (roles == NIL) |
| 145 |
{ |
| 146 |
*num_roles = 1; |
| 147 |
role_oids = (Datum *) palloc(*num_roles * sizeof(Datum)); |
| 148 |
role_oids[0] = ObjectIdGetDatum(ACL_ID_PUBLIC); |
| 149 |
|
| 150 |
return role_oids; |
| 151 |
} |
| 152 |
|
| 153 |
*num_roles = list_length(roles); |
| 154 |
role_oids = (Datum *) palloc(*num_roles * sizeof(Datum)); |
| 155 |
|
| 156 |
foreach(cell, roles) |
| 157 |
{ |
| 158 |
RoleSpec *spec = lfirst(cell); |
| 159 |
|
| 160 |
/* |
| 161 |
* PUBLIC covers all roles, so it only makes sense alone. |
| 162 |
*/ |
| 163 |
if (spec->roletype == ROLESPEC_PUBLIC) |
| 164 |
{ |
| 165 |
if (*num_roles != 1) |
| 166 |
{ |
| 167 |
ereport(WARNING, |
| 168 |
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 169 |
errmsg("ignoring specified roles other than PUBLIC"), |
| 170 |
errhint("All roles are members of the PUBLIC role."))); |
| 171 |
*num_roles = 1; |
| 172 |
} |
| 173 |
role_oids[0] = ObjectIdGetDatum(ACL_ID_PUBLIC); |
| 174 |
|
| 175 |
return role_oids; |
| 176 |
} |
| 177 |
else |
| 178 |
role_oids[i++] = |
| 179 |
ObjectIdGetDatum(get_rolespec_oid(spec, false)); |
| 180 |
} |
| 181 |
|
| 182 |
return role_oids; |
| 183 |
} |
| 184 |
|
| 185 |
/* |
| 186 |
* Load row security policy from the catalog, and store it in |
| 187 |
* the relation's relcache entry. |
| 188 |
* |
| 189 |
* Note that caller should have verified that pg_class.relrowsecurity |
| 190 |
* is true for this relation. |
| 191 |
*/ |
| 192 |
void |
| 193 |
RelationBuildRowSecurity(Relation relation) |
| 194 |
{ |
| 195 |
MemoryContext rscxt; |
| 196 |
MemoryContext oldcxt = CurrentMemoryContext; |
| 197 |
RowSecurityDesc *rsdesc; |
| 198 |
Relation catalog; |
| 199 |
ScanKeyData skey; |
| 200 |
SysScanDesc sscan; |
| 201 |
HeapTuple tuple; |
| 202 |
|
| 203 |
/* |
| 204 |
* Create a memory context to hold everything associated with this |
| 205 |
* relation's row security policy. This makes it easy to clean up during |
| 206 |
* a relcache flush. However, to cover the possibility of an error |
| 207 |
* partway through, we don't make the context long-lived till we're done. |
| 208 |
*/ |
| 209 |
rscxt = AllocSetContextCreate(CurrentMemoryContext, |
| 210 |
"row security descriptor", |
| 211 |
ALLOCSET_SMALL_SIZES); |
| 212 |
MemoryContextCopyAndSetIdentifier(rscxt, |
| 213 |
RelationGetRelationName(relation)); |
| 214 |
|
| 215 |
rsdesc = MemoryContextAllocZero(rscxt, sizeof(RowSecurityDesc)); |
| 216 |
rsdesc->rscxt = rscxt; |
| 217 |
|
| 218 |
/* |
| 219 |
* Now scan pg_policy for RLS policies associated with this relation. |
| 220 |
* Because we use the index on (polrelid, polname), we should consistently |
| 221 |
* visit the rel's policies in name order, at least when system indexes |
| 222 |
* aren't disabled. This simplifies equalRSDesc(). |
| 223 |
*/ |
| 224 |
catalog = table_open(PolicyRelationId, AccessShareLock); |
| 225 |
|
| 226 |
ScanKeyInit(&skey, |
| 227 |
Anum_pg_policy_polrelid, |
| 228 |
BTEqualStrategyNumber, F_OIDEQ, |
| 229 |
ObjectIdGetDatum(RelationGetRelid(relation))); |
| 230 |
|
| 231 |
sscan = systable_beginscan(catalog, PolicyPolrelidPolnameIndexId, true, |
| 232 |
NULL, 1, &skey); |
| 233 |
|
| 234 |
while (HeapTupleIsValid(tuple = systable_getnext(sscan))) |
| 235 |
{ |
| 236 |
Form_pg_policy policy_form = (Form_pg_policy) GETSTRUCT(tuple); |
| 237 |
RowSecurityPolicy *policy; |
| 238 |
Datum datum; |
| 239 |
bool isnull; |
| 240 |
char *str_value; |
| 241 |
|
| 242 |
policy = MemoryContextAllocZero(rscxt, sizeof(RowSecurityPolicy)); |
| 243 |
|
| 244 |
/* |
| 245 |
* Note: we must be sure that pass-by-reference data gets copied into |
| 246 |
* rscxt. We avoid making that context current over wider spans than |
| 247 |
* we have to, though. |
| 248 |
*/ |
| 249 |
|
| 250 |
/* Get policy command */ |
| 251 |
policy->polcmd = policy_form->polcmd; |
| 252 |
|
| 253 |
/* Get policy, permissive or restrictive */ |
| 254 |
policy->permissive = policy_form->polpermissive; |
| 255 |
|
| 256 |
/* Get policy name */ |
| 257 |
policy->policy_name = |
| 258 |
MemoryContextStrdup(rscxt, NameStr(policy_form->polname)); |
| 259 |
|
| 260 |
/* Get policy roles */ |
| 261 |
datum = heap_getattr(tuple, Anum_pg_policy_polroles, |
| 262 |
RelationGetDescr(catalog), &isnull); |
| 263 |
/* shouldn't be null, but let's check for luck */ |
| 264 |
if (isnull) |
| 265 |
elog(ERROR, "unexpected null value in pg_policy.polroles"); |
| 266 |
MemoryContextSwitchTo(rscxt); |
| 267 |
policy->roles = DatumGetArrayTypePCopy(datum); |
| 268 |
MemoryContextSwitchTo(oldcxt); |
| 269 |
|
| 270 |
/* Get policy qual */ |
| 271 |
datum = heap_getattr(tuple, Anum_pg_policy_polqual, |
| 272 |
RelationGetDescr(catalog), &isnull); |
| 273 |
if (!isnull) |
| 274 |
{ |
| 275 |
str_value = TextDatumGetCString(datum); |
| 276 |
MemoryContextSwitchTo(rscxt); |
| 277 |
policy->qual = (Expr *) stringToNode(str_value); |
| 278 |
MemoryContextSwitchTo(oldcxt); |
| 279 |
pfree(str_value); |
| 280 |
} |
| 281 |
else |
| 282 |
policy->qual = NULL; |
| 283 |
|
| 284 |
/* Get WITH CHECK qual */ |
| 285 |
datum = heap_getattr(tuple, Anum_pg_policy_polwithcheck, |
| 286 |
RelationGetDescr(catalog), &isnull); |
| 287 |
if (!isnull) |
| 288 |
{ |
| 289 |
str_value = TextDatumGetCString(datum); |
| 290 |
MemoryContextSwitchTo(rscxt); |
| 291 |
policy->with_check_qual = (Expr *) stringToNode(str_value); |
| 292 |
MemoryContextSwitchTo(oldcxt); |
| 293 |
pfree(str_value); |
| 294 |
} |
| 295 |
else |
| 296 |
policy->with_check_qual = NULL; |
| 297 |
|
| 298 |
/* We want to cache whether there are SubLinks in these expressions */ |
| 299 |
policy->hassublinks = checkExprHasSubLink((Node *) policy->qual) || |
| 300 |
checkExprHasSubLink((Node *) policy->with_check_qual); |
| 301 |
|
| 302 |
/* |
| 303 |
* Add this object to list. For historical reasons, the list is built |
| 304 |
* in reverse order. |
| 305 |
*/ |
| 306 |
MemoryContextSwitchTo(rscxt); |
| 307 |
rsdesc->policies = lcons(policy, rsdesc->policies); |
| 308 |
MemoryContextSwitchTo(oldcxt); |
| 309 |
} |
| 310 |
|
| 311 |
systable_endscan(sscan); |
| 312 |
table_close(catalog, AccessShareLock); |
| 313 |
|
| 314 |
/* |
| 315 |
* Success. Reparent the descriptor's memory context under |
| 316 |
* CacheMemoryContext so that it will live indefinitely, then attach the |
| 317 |
* policy descriptor to the relcache entry. |
| 318 |
*/ |
| 319 |
MemoryContextSetParent(rscxt, CacheMemoryContext); |
| 320 |
|
| 321 |
relation->rd_rsdesc = rsdesc; |
| 322 |
} |
| 323 |
|
| 324 |
/* |
| 325 |
* RemovePolicyById - |
| 326 |
* remove a policy by its OID. If a policy does not exist with the provided |
| 327 |
* oid, then an error is raised. |
| 328 |
* |
| 329 |
* policy_id - the oid of the policy. |
| 330 |
*/ |
| 331 |
void |
| 332 |
RemovePolicyById(Oid policy_id) |
| 333 |
{ |
| 334 |
Relation pg_policy_rel; |
| 335 |
SysScanDesc sscan; |
| 336 |
ScanKeyData skey[1]; |
| 337 |
HeapTuple tuple; |
| 338 |
Oid relid; |
| 339 |
Relation rel; |
| 340 |
|
| 341 |
pg_policy_rel = table_open(PolicyRelationId, RowExclusiveLock); |
| 342 |
|
| 343 |
/* |
| 344 |
* Find the policy to delete. |
| 345 |
*/ |
| 346 |
ScanKeyInit(&skey[0], |
| 347 |
Anum_pg_policy_oid, |
| 348 |
BTEqualStrategyNumber, F_OIDEQ, |
| 349 |
ObjectIdGetDatum(policy_id)); |
| 350 |
|
| 351 |
sscan = systable_beginscan(pg_policy_rel, PolicyOidIndexId, true, |
| 352 |
NULL, 1, skey); |
| 353 |
|
| 354 |
tuple = systable_getnext(sscan); |
| 355 |
|
| 356 |
/* If the policy exists, then remove it, otherwise raise an error. */ |
| 357 |
if (!HeapTupleIsValid(tuple)) |
| 358 |
elog(ERROR, "could not find tuple for policy %u", policy_id); |
| 359 |
|
| 360 |
/* |
| 361 |
* Open and exclusive-lock the relation the policy belongs to. (We need |
| 362 |
* exclusive lock to lock out queries that might otherwise depend on the |
| 363 |
* set of policies the rel has; furthermore we've got to hold the lock |
| 364 |
* till commit.) |
| 365 |
*/ |
| 366 |
relid = ((Form_pg_policy) GETSTRUCT(tuple))->polrelid; |
| 367 |
|
| 368 |
rel = table_open(relid, AccessExclusiveLock); |
| 369 |
if (rel->rd_rel->relkind != RELKIND_RELATION && |
| 370 |
rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE) |
| 371 |
ereport(ERROR, |
| 372 |
(errcode(ERRCODE_WRONG_OBJECT_TYPE), |
| 373 |
errmsg("\"%s\" is not a table", |
| 374 |
RelationGetRelationName(rel)))); |
| 375 |
|
| 376 |
if (!allowSystemTableMods && IsSystemRelation(rel)) |
| 377 |
ereport(ERROR, |
| 378 |
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), |
| 379 |
errmsg("permission denied: \"%s\" is a system catalog", |
| 380 |
RelationGetRelationName(rel)))); |
| 381 |
|
| 382 |
CatalogTupleDelete(pg_policy_rel, &tuple->t_self); |
| 383 |
|
| 384 |
systable_endscan(sscan); |
| 385 |
|
| 386 |
/* |
| 387 |
* Note that, unlike some of the other flags in pg_class, relrowsecurity |
| 388 |
* is not just an indication of if policies exist. When relrowsecurity is |
| 389 |
* set by a user, then all access to the relation must be through a |
| 390 |
* policy. If no policy is defined for the relation then a default-deny |
| 391 |
* policy is created and all records are filtered (except for queries from |
| 392 |
* the owner). |
| 393 |
*/ |
| 394 |
CacheInvalidateRelcache(rel); |
| 395 |
|
| 396 |
table_close(rel, NoLock); |
| 397 |
|
| 398 |
/* Clean up */ |
| 399 |
table_close(pg_policy_rel, RowExclusiveLock); |
| 400 |
} |
| 401 |
|
| 402 |
/* |
| 403 |
* RemoveRoleFromObjectPolicy - |
| 404 |
* remove a role from a policy's applicable-roles list. |
| 405 |
* |
| 406 |
* Returns true if the role was successfully removed from the policy. |
| 407 |
* Returns false if the role was not removed because it would have left |
| 408 |
* polroles empty (which is disallowed, though perhaps it should not be). |
| 409 |
* On false return, the caller should instead drop the policy altogether. |
| 410 |
* |
| 411 |
* roleid - the oid of the role to remove |
| 412 |
* classid - should always be PolicyRelationId |
| 413 |
* policy_id - the oid of the policy. |
| 414 |
*/ |
| 415 |
bool |
| 416 |
RemoveRoleFromObjectPolicy(Oid roleid, Oid classid, Oid policy_id) |
| 417 |
{ |
| 418 |
Relation pg_policy_rel; |
| 419 |
SysScanDesc sscan; |
| 420 |
ScanKeyData skey[1]; |
| 421 |
HeapTuple tuple; |
| 422 |
Oid relid; |
| 423 |
ArrayType *policy_roles; |
| 424 |
Datum roles_datum; |
| 425 |
Oid *roles; |
| 426 |
int num_roles; |
| 427 |
Datum *role_oids; |
| 428 |
bool attr_isnull; |
| 429 |
bool keep_policy = true; |
| 430 |
int i, |
| 431 |
j; |
| 432 |
|
| 433 |
Assert(classid == PolicyRelationId); |
| 434 |
|
| 435 |
pg_policy_rel = table_open(PolicyRelationId, RowExclusiveLock); |
| 436 |
|
| 437 |
/* |
| 438 |
* Find the policy to update. |
| 439 |
*/ |
| 440 |
ScanKeyInit(&skey[0], |
| 441 |
Anum_pg_policy_oid, |
| 442 |
BTEqualStrategyNumber, F_OIDEQ, |
| 443 |
ObjectIdGetDatum(policy_id)); |
| 444 |
|
| 445 |
sscan = systable_beginscan(pg_policy_rel, PolicyOidIndexId, true, |
| 446 |
NULL, 1, skey); |
| 447 |
|
| 448 |
tuple = systable_getnext(sscan); |
| 449 |
|
| 450 |
/* Raise an error if we don't find the policy. */ |
| 451 |
if (!HeapTupleIsValid(tuple)) |
| 452 |
elog(ERROR, "could not find tuple for policy %u", policy_id); |
| 453 |
|
| 454 |
/* Identify rel the policy belongs to */ |
| 455 |
relid = ((Form_pg_policy) GETSTRUCT(tuple))->polrelid; |
| 456 |
|
| 457 |
/* Get the current set of roles */ |
| 458 |
roles_datum = heap_getattr(tuple, |
| 459 |
Anum_pg_policy_polroles, |
| 460 |
RelationGetDescr(pg_policy_rel), |
| 461 |
&attr_isnull); |
| 462 |
|
| 463 |
Assert(!attr_isnull); |
| 464 |
|
| 465 |
policy_roles = DatumGetArrayTypePCopy(roles_datum); |
| 466 |
roles = (Oid *) ARR_DATA_PTR(policy_roles); |
| 467 |
num_roles = ARR_DIMS(policy_roles)[0]; |
| 468 |
|
| 469 |
/* |
| 470 |
* Rebuild the polroles array, without any mentions of the target role. |
| 471 |
* Ordinarily there'd be exactly one, but we must cope with duplicate |
| 472 |
* mentions, since CREATE/ALTER POLICY historically have allowed that. |
| 473 |
*/ |
| 474 |
role_oids = (Datum *) palloc(num_roles * sizeof(Datum)); |
| 475 |
for (i = 0, j = 0; i < num_roles; i++) |
| 476 |
{ |
| 477 |
if (roles[i] != roleid) |
| 478 |
role_oids[j++] = ObjectIdGetDatum(roles[i]); |
| 479 |
} |
| 480 |
num_roles = j; |
| 481 |
|
| 482 |
/* If any roles remain, update the policy entry. */ |
| 483 |
if (num_roles > 0) |
| 484 |
{ |
| 485 |
ArrayType *role_ids; |
| 486 |
Datum values[Natts_pg_policy]; |
| 487 |
bool isnull[Natts_pg_policy]; |
| 488 |
bool replaces[Natts_pg_policy]; |
| 489 |
HeapTuple new_tuple; |
| 490 |
HeapTuple reltup; |
| 491 |
ObjectAddress target; |
| 492 |
ObjectAddress myself; |
| 493 |
|
| 494 |
/* zero-clear */ |
| 495 |
memset(values, 0, sizeof(values)); |
| 496 |
memset(replaces, 0, sizeof(replaces)); |
| 497 |
memset(isnull, 0, sizeof(isnull)); |
| 498 |
|
| 499 |
/* This is the array for the new tuple */ |
| 500 |
role_ids = construct_array_builtin(role_oids, num_roles, OIDOID); |
| 501 |
|
| 502 |
replaces[Anum_pg_policy_polroles - 1] = true; |
| 503 |
values[Anum_pg_policy_polroles - 1] = PointerGetDatum(role_ids); |
| 504 |
|
| 505 |
new_tuple = heap_modify_tuple(tuple, |
| 506 |
RelationGetDescr(pg_policy_rel), |
| 507 |
values, isnull, replaces); |
| 508 |
CatalogTupleUpdate(pg_policy_rel, &new_tuple->t_self, new_tuple); |
| 509 |
|
| 510 |
/* Remove all the old shared dependencies (roles) */ |
| 511 |
deleteSharedDependencyRecordsFor(PolicyRelationId, policy_id, 0); |
| 512 |
|
| 513 |
/* Record the new shared dependencies (roles) */ |
| 514 |
myself.classId = PolicyRelationId; |
| 515 |
myself.objectId = policy_id; |
| 516 |
myself.objectSubId = 0; |
| 517 |
|
| 518 |
target.classId = AuthIdRelationId; |
| 519 |
target.objectSubId = 0; |
| 520 |
for (i = 0; i < num_roles; i++) |
| 521 |
{ |
| 522 |
target.objectId = DatumGetObjectId(role_oids[i]); |
| 523 |
/* no need for dependency on the public role */ |
| 524 |
if (target.objectId != ACL_ID_PUBLIC) |
| 525 |
recordSharedDependencyOn(&myself, &target, |
| 526 |
SHARED_DEPENDENCY_POLICY); |
| 527 |
} |
| 528 |
|
| 529 |
InvokeObjectPostAlterHook(PolicyRelationId, policy_id, 0); |
| 530 |
|
| 531 |
heap_freetuple(new_tuple); |
| 532 |
|
| 533 |
/* Make updates visible */ |
| 534 |
CommandCounterIncrement(); |
| 535 |
|
| 536 |
/* |
| 537 |
* Invalidate relcache entry for rel the policy belongs to, to force |
| 538 |
* redoing any dependent plans. In case of a race condition where the |
| 539 |
* rel was just dropped, we need do nothing. |
| 540 |
*/ |
| 541 |
reltup = SearchSysCache1(RELOID, ObjectIdGetDatum(relid)); |
| 542 |
if (HeapTupleIsValid(reltup)) |
| 543 |
{ |
| 544 |
CacheInvalidateRelcacheByTuple(reltup); |
| 545 |
ReleaseSysCache(reltup); |
| 546 |
} |
| 547 |
} |
| 548 |
else |
| 549 |
{ |
| 550 |
/* No roles would remain, so drop the policy instead. */ |
| 551 |
keep_policy = false; |
| 552 |
} |
| 553 |
|
| 554 |
/* Clean up. */ |
| 555 |
systable_endscan(sscan); |
| 556 |
|
| 557 |
table_close(pg_policy_rel, RowExclusiveLock); |
| 558 |
|
| 559 |
return keep_policy; |
| 560 |
} |
| 561 |
|
| 562 |
/* |
| 563 |
* CreatePolicy - |
| 564 |
* handles the execution of the CREATE POLICY command. |
| 565 |
* |
| 566 |
* stmt - the CreatePolicyStmt that describes the policy to create. |
| 567 |
*/ |
| 568 |
ObjectAddress |
| 569 |
CreatePolicy(CreatePolicyStmt *stmt) |
| 570 |
{ |
| 571 |
Relation pg_policy_rel; |
| 572 |
Oid policy_id; |
| 573 |
Relation target_table; |
| 574 |
Oid table_id; |
| 575 |
char polcmd; |
| 576 |
Datum *role_oids; |
| 577 |
int nitems = 0; |
| 578 |
ArrayType *role_ids; |
| 579 |
ParseState *qual_pstate; |
| 580 |
ParseState *with_check_pstate; |
| 581 |
ParseNamespaceItem *nsitem; |
| 582 |
Node *qual; |
| 583 |
Node *with_check_qual; |
| 584 |
ScanKeyData skey[2]; |
| 585 |
SysScanDesc sscan; |
| 586 |
HeapTuple policy_tuple; |
| 587 |
Datum values[Natts_pg_policy]; |
| 588 |
bool isnull[Natts_pg_policy]; |
| 589 |
ObjectAddress target; |
| 590 |
ObjectAddress myself; |
| 591 |
int i; |
| 592 |
|
| 593 |
/* Parse command */ |
| 594 |
polcmd = parse_policy_command(stmt->cmd_name); |
| 595 |
|
| 596 |
/* |
| 597 |
* If the command is SELECT or DELETE then WITH CHECK should be NULL. |
| 598 |
*/ |
| 599 |
if ((polcmd == ACL_SELECT_CHR || polcmd == ACL_DELETE_CHR) |
| 600 |
&& stmt->with_check != NULL) |
| 601 |
ereport(ERROR, |
| 602 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 603 |
errmsg("WITH CHECK cannot be applied to SELECT or DELETE"))); |
| 604 |
|
| 605 |
/* |
| 606 |
* If the command is INSERT then WITH CHECK should be the only expression |
| 607 |
* provided. |
| 608 |
*/ |
| 609 |
if (polcmd == ACL_INSERT_CHR && stmt->qual != NULL) |
| 610 |
ereport(ERROR, |
| 611 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 612 |
errmsg("only WITH CHECK expression allowed for INSERT"))); |
| 613 |
|
| 614 |
/* Collect role ids */ |
| 615 |
role_oids = policy_role_list_to_array(stmt->roles, &nitems); |
| 616 |
role_ids = construct_array_builtin(role_oids, nitems, OIDOID); |
| 617 |
|
| 618 |
/* Parse the supplied clause */ |
| 619 |
qual_pstate = make_parsestate(NULL); |
| 620 |
with_check_pstate = make_parsestate(NULL); |
| 621 |
|
| 622 |
/* zero-clear */ |
| 623 |
memset(values, 0, sizeof(values)); |
| 624 |
memset(isnull, 0, sizeof(isnull)); |
| 625 |
|
| 626 |
/* Get id of table. Also handles permissions checks. */ |
| 627 |
table_id = RangeVarGetRelidExtended(stmt->table, AccessExclusiveLock, |
| 628 |
0, |
| 629 |
RangeVarCallbackForPolicy, |
| 630 |
stmt); |
| 631 |
|
| 632 |
/* Open target_table to build quals. No additional lock is necessary. */ |
| 633 |
target_table = relation_open(table_id, NoLock); |
| 634 |
|
| 635 |
/* Add for the regular security quals */ |
| 636 |
nsitem = addRangeTableEntryForRelation(qual_pstate, target_table, |
| 637 |
AccessShareLock, |
| 638 |
NULL, false, false); |
| 639 |
addNSItemToQuery(qual_pstate, nsitem, false, true, true); |
| 640 |
|
| 641 |
/* Add for the with-check quals */ |
| 642 |
nsitem = addRangeTableEntryForRelation(with_check_pstate, target_table, |
| 643 |
AccessShareLock, |
| 644 |
NULL, false, false); |
| 645 |
addNSItemToQuery(with_check_pstate, nsitem, false, true, true); |
| 646 |
|
| 647 |
qual = transformWhereClause(qual_pstate, |
| 648 |
stmt->qual, |
| 649 |
EXPR_KIND_POLICY, |
| 650 |
"POLICY"); |
| 651 |
|
| 652 |
with_check_qual = transformWhereClause(with_check_pstate, |
| 653 |
stmt->with_check, |
| 654 |
EXPR_KIND_POLICY, |
| 655 |
"POLICY"); |
| 656 |
|
| 657 |
/* Fix up collation information */ |
| 658 |
assign_expr_collations(qual_pstate, qual); |
| 659 |
assign_expr_collations(with_check_pstate, with_check_qual); |
| 660 |
|
| 661 |
/* Open pg_policy catalog */ |
| 662 |
pg_policy_rel = table_open(PolicyRelationId, RowExclusiveLock); |
| 663 |
|
| 664 |
/* Set key - policy's relation id. */ |
| 665 |
ScanKeyInit(&skey[0], |
| 666 |
Anum_pg_policy_polrelid, |
| 667 |
BTEqualStrategyNumber, F_OIDEQ, |
| 668 |
ObjectIdGetDatum(table_id)); |
| 669 |
|
| 670 |
/* Set key - policy's name. */ |
| 671 |
ScanKeyInit(&skey[1], |
| 672 |
Anum_pg_policy_polname, |
| 673 |
BTEqualStrategyNumber, F_NAMEEQ, |
| 674 |
CStringGetDatum(stmt->policy_name)); |
| 675 |
|
| 676 |
sscan = systable_beginscan(pg_policy_rel, |
| 677 |
PolicyPolrelidPolnameIndexId, true, NULL, 2, |
| 678 |
skey); |
| 679 |
|
| 680 |
policy_tuple = systable_getnext(sscan); |
| 681 |
|
| 682 |
/* Complain if the policy name already exists for the table */ |
| 683 |
if (HeapTupleIsValid(policy_tuple)) |
| 684 |
ereport(ERROR, |
| 685 |
(errcode(ERRCODE_DUPLICATE_OBJECT), |
| 686 |
errmsg("policy \"%s\" for table \"%s\" already exists", |
| 687 |
stmt->policy_name, RelationGetRelationName(target_table)))); |
| 688 |
|
| 689 |
policy_id = GetNewOidWithIndex(pg_policy_rel, PolicyOidIndexId, |
| 690 |
Anum_pg_policy_oid); |
| 691 |
values[Anum_pg_policy_oid - 1] = ObjectIdGetDatum(policy_id); |
| 692 |
values[Anum_pg_policy_polrelid - 1] = ObjectIdGetDatum(table_id); |
| 693 |
values[Anum_pg_policy_polname - 1] = DirectFunctionCall1(namein, |
| 694 |
CStringGetDatum(stmt->policy_name)); |
| 695 |
values[Anum_pg_policy_polcmd - 1] = CharGetDatum(polcmd); |
| 696 |
values[Anum_pg_policy_polpermissive - 1] = BoolGetDatum(stmt->permissive); |
| 697 |
values[Anum_pg_policy_polroles - 1] = PointerGetDatum(role_ids); |
| 698 |
|
| 699 |
/* Add qual if present. */ |
| 700 |
if (qual) |
| 701 |
values[Anum_pg_policy_polqual - 1] = CStringGetTextDatum(nodeToString(qual)); |
| 702 |
else |
| 703 |
isnull[Anum_pg_policy_polqual - 1] = true; |
| 704 |
|
| 705 |
/* Add WITH CHECK qual if present */ |
| 706 |
if (with_check_qual) |
| 707 |
values[Anum_pg_policy_polwithcheck - 1] = CStringGetTextDatum(nodeToString(with_check_qual)); |
| 708 |
else |
| 709 |
isnull[Anum_pg_policy_polwithcheck - 1] = true; |
| 710 |
|
| 711 |
policy_tuple = heap_form_tuple(RelationGetDescr(pg_policy_rel), values, |
| 712 |
isnull); |
| 713 |
|
| 714 |
CatalogTupleInsert(pg_policy_rel, policy_tuple); |
| 715 |
|
| 716 |
/* Record Dependencies */ |
| 717 |
target.classId = RelationRelationId; |
| 718 |
target.objectId = table_id; |
| 719 |
target.objectSubId = 0; |
| 720 |
|
| 721 |
myself.classId = PolicyRelationId; |
| 722 |
myself.objectId = policy_id; |
| 723 |
myself.objectSubId = 0; |
| 724 |
|
| 725 |
recordDependencyOn(&myself, &target, DEPENDENCY_AUTO); |
| 726 |
|
| 727 |
CheckUsageOnTypesInExpr(qual, qual_pstate->p_rtable, GetUserId()); |
| 728 |
recordDependencyOnExpr(&myself, qual, qual_pstate->p_rtable, |
| 729 |
DEPENDENCY_NORMAL); |
| 730 |
|
| 731 |
CheckUsageOnTypesInExpr(with_check_qual, with_check_pstate->p_rtable, |
| 732 |
GetUserId()); |
| 733 |
recordDependencyOnExpr(&myself, with_check_qual, |
| 734 |
with_check_pstate->p_rtable, DEPENDENCY_NORMAL); |
| 735 |
|
| 736 |
/* Register role dependencies */ |
| 737 |
target.classId = AuthIdRelationId; |
| 738 |
target.objectSubId = 0; |
| 739 |
for (i = 0; i < nitems; i++) |
| 740 |
{ |
| 741 |
target.objectId = DatumGetObjectId(role_oids[i]); |
| 742 |
/* no dependency if public */ |
| 743 |
if (target.objectId != ACL_ID_PUBLIC) |
| 744 |
recordSharedDependencyOn(&myself, &target, |
| 745 |
SHARED_DEPENDENCY_POLICY); |
| 746 |
} |
| 747 |
|
| 748 |
InvokeObjectPostCreateHook(PolicyRelationId, policy_id, 0); |
| 749 |
|
| 750 |
/* Invalidate Relation Cache */ |
| 751 |
CacheInvalidateRelcache(target_table); |
| 752 |
|
| 753 |
/* Clean up. */ |
| 754 |
heap_freetuple(policy_tuple); |
| 755 |
free_parsestate(qual_pstate); |
| 756 |
free_parsestate(with_check_pstate); |
| 757 |
systable_endscan(sscan); |
| 758 |
relation_close(target_table, NoLock); |
| 759 |
table_close(pg_policy_rel, RowExclusiveLock); |
| 760 |
|
| 761 |
return myself; |
| 762 |
} |
| 763 |
|
| 764 |
/* |
| 765 |
* AlterPolicy - |
| 766 |
* handles the execution of the ALTER POLICY command. |
| 767 |
* |
| 768 |
* stmt - the AlterPolicyStmt that describes the policy and how to alter it. |
| 769 |
*/ |
| 770 |
ObjectAddress |
| 771 |
AlterPolicy(AlterPolicyStmt *stmt) |
| 772 |
{ |
| 773 |
Relation pg_policy_rel; |
| 774 |
Oid policy_id; |
| 775 |
Relation target_table; |
| 776 |
Oid table_id; |
| 777 |
Datum *role_oids = NULL; |
| 778 |
int nitems = 0; |
| 779 |
ArrayType *role_ids = NULL; |
| 780 |
List *qual_parse_rtable = NIL; |
| 781 |
List *with_check_parse_rtable = NIL; |
| 782 |
Node *qual = NULL; |
| 783 |
Node *with_check_qual = NULL; |
| 784 |
ScanKeyData skey[2]; |
| 785 |
SysScanDesc sscan; |
| 786 |
HeapTuple policy_tuple; |
| 787 |
HeapTuple new_tuple; |
| 788 |
Datum values[Natts_pg_policy]; |
| 789 |
bool isnull[Natts_pg_policy]; |
| 790 |
bool replaces[Natts_pg_policy]; |
| 791 |
ObjectAddress target; |
| 792 |
ObjectAddress myself; |
| 793 |
Datum polcmd_datum; |
| 794 |
char polcmd; |
| 795 |
bool polcmd_isnull; |
| 796 |
int i; |
| 797 |
|
| 798 |
/* Parse role_ids */ |
| 799 |
if (stmt->roles != NULL) |
| 800 |
{ |
| 801 |
role_oids = policy_role_list_to_array(stmt->roles, &nitems); |
| 802 |
role_ids = construct_array_builtin(role_oids, nitems, OIDOID); |
| 803 |
} |
| 804 |
|
| 805 |
/* Get id of table. Also handles permissions checks. */ |
| 806 |
table_id = RangeVarGetRelidExtended(stmt->table, AccessExclusiveLock, |
| 807 |
0, |
| 808 |
RangeVarCallbackForPolicy, |
| 809 |
stmt); |
| 810 |
|
| 811 |
target_table = relation_open(table_id, NoLock); |
| 812 |
|
| 813 |
/* Parse the using policy clause */ |
| 814 |
if (stmt->qual) |
| 815 |
{ |
| 816 |
ParseNamespaceItem *nsitem; |
| 817 |
ParseState *qual_pstate = make_parsestate(NULL); |
| 818 |
|
| 819 |
nsitem = addRangeTableEntryForRelation(qual_pstate, target_table, |
| 820 |
AccessShareLock, |
| 821 |
NULL, false, false); |
| 822 |
|
| 823 |
addNSItemToQuery(qual_pstate, nsitem, false, true, true); |
| 824 |
|
| 825 |
qual = transformWhereClause(qual_pstate, stmt->qual, |
| 826 |
EXPR_KIND_POLICY, |
| 827 |
"POLICY"); |
| 828 |
|
| 829 |
/* Fix up collation information */ |
| 830 |
assign_expr_collations(qual_pstate, qual); |
| 831 |
|
| 832 |
qual_parse_rtable = qual_pstate->p_rtable; |
| 833 |
free_parsestate(qual_pstate); |
| 834 |
} |
| 835 |
|
| 836 |
/* Parse the with-check policy clause */ |
| 837 |
if (stmt->with_check) |
| 838 |
{ |
| 839 |
ParseNamespaceItem *nsitem; |
| 840 |
ParseState *with_check_pstate = make_parsestate(NULL); |
| 841 |
|
| 842 |
nsitem = addRangeTableEntryForRelation(with_check_pstate, target_table, |
| 843 |
AccessShareLock, |
| 844 |
NULL, false, false); |
| 845 |
|
| 846 |
addNSItemToQuery(with_check_pstate, nsitem, false, true, true); |
| 847 |
|
| 848 |
with_check_qual = transformWhereClause(with_check_pstate, |
| 849 |
stmt->with_check, |
| 850 |
EXPR_KIND_POLICY, |
| 851 |
"POLICY"); |
| 852 |
|
| 853 |
/* Fix up collation information */ |
| 854 |
assign_expr_collations(with_check_pstate, with_check_qual); |
| 855 |
|
| 856 |
with_check_parse_rtable = with_check_pstate->p_rtable; |
| 857 |
free_parsestate(with_check_pstate); |
| 858 |
} |
| 859 |
|
| 860 |
/* zero-clear */ |
| 861 |
memset(values, 0, sizeof(values)); |
| 862 |
memset(replaces, 0, sizeof(replaces)); |
| 863 |
memset(isnull, 0, sizeof(isnull)); |
| 864 |
|
| 865 |
/* Find policy to update. */ |
| 866 |
pg_policy_rel = table_open(PolicyRelationId, RowExclusiveLock); |
| 867 |
|
| 868 |
/* Set key - policy's relation id. */ |
| 869 |
ScanKeyInit(&skey[0], |
| 870 |
Anum_pg_policy_polrelid, |
| 871 |
BTEqualStrategyNumber, F_OIDEQ, |
| 872 |
ObjectIdGetDatum(table_id)); |
| 873 |
|
| 874 |
/* Set key - policy's name. */ |
| 875 |
ScanKeyInit(&skey[1], |
| 876 |
Anum_pg_policy_polname, |
| 877 |
BTEqualStrategyNumber, F_NAMEEQ, |
| 878 |
CStringGetDatum(stmt->policy_name)); |
| 879 |
|
| 880 |
sscan = systable_beginscan(pg_policy_rel, |
| 881 |
PolicyPolrelidPolnameIndexId, true, NULL, 2, |
| 882 |
skey); |
| 883 |
|
| 884 |
policy_tuple = systable_getnext(sscan); |
| 885 |
|
| 886 |
/* Check that the policy is found, raise an error if not. */ |
| 887 |
if (!HeapTupleIsValid(policy_tuple)) |
| 888 |
ereport(ERROR, |
| 889 |
(errcode(ERRCODE_UNDEFINED_OBJECT), |
| 890 |
errmsg("policy \"%s\" for table \"%s\" does not exist", |
| 891 |
stmt->policy_name, |
| 892 |
RelationGetRelationName(target_table)))); |
| 893 |
|
| 894 |
/* Get policy command */ |
| 895 |
polcmd_datum = heap_getattr(policy_tuple, Anum_pg_policy_polcmd, |
| 896 |
RelationGetDescr(pg_policy_rel), |
| 897 |
&polcmd_isnull); |
| 898 |
Assert(!polcmd_isnull); |
| 899 |
polcmd = DatumGetChar(polcmd_datum); |
| 900 |
|
| 901 |
/* |
| 902 |
* If the command is SELECT or DELETE then WITH CHECK should be NULL. |
| 903 |
*/ |
| 904 |
if ((polcmd == ACL_SELECT_CHR || polcmd == ACL_DELETE_CHR) |
| 905 |
&& stmt->with_check != NULL) |
| 906 |
ereport(ERROR, |
| 907 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 908 |
errmsg("only USING expression allowed for SELECT, DELETE"))); |
| 909 |
|
| 910 |
/* |
| 911 |
* If the command is INSERT then WITH CHECK should be the only expression |
| 912 |
* provided. |
| 913 |
*/ |
| 914 |
if ((polcmd == ACL_INSERT_CHR) |
| 915 |
&& stmt->qual != NULL) |
| 916 |
ereport(ERROR, |
| 917 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 918 |
errmsg("only WITH CHECK expression allowed for INSERT"))); |
| 919 |
|
| 920 |
policy_id = ((Form_pg_policy) GETSTRUCT(policy_tuple))->oid; |
| 921 |
|
| 922 |
if (role_ids != NULL) |
| 923 |
{ |
| 924 |
replaces[Anum_pg_policy_polroles - 1] = true; |
| 925 |
values[Anum_pg_policy_polroles - 1] = PointerGetDatum(role_ids); |
| 926 |
} |
| 927 |
else |
| 928 |
{ |
| 929 |
Oid *roles; |
| 930 |
Datum roles_datum; |
| 931 |
bool attr_isnull; |
| 932 |
ArrayType *policy_roles; |
| 933 |
|
| 934 |
/* |
| 935 |
* We need to pull the set of roles this policy applies to from what's |
| 936 |
* in the catalog, so that we can recreate the dependencies correctly |
| 937 |
* for the policy. |
| 938 |
*/ |
| 939 |
|
| 940 |
roles_datum = heap_getattr(policy_tuple, Anum_pg_policy_polroles, |
| 941 |
RelationGetDescr(pg_policy_rel), |
| 942 |
&attr_isnull); |
| 943 |
Assert(!attr_isnull); |
| 944 |
|
| 945 |
policy_roles = DatumGetArrayTypePCopy(roles_datum); |
| 946 |
|
| 947 |
roles = (Oid *) ARR_DATA_PTR(policy_roles); |
| 948 |
|
| 949 |
nitems = ARR_DIMS(policy_roles)[0]; |
| 950 |
|
| 951 |
role_oids = (Datum *) palloc(nitems * sizeof(Datum)); |
| 952 |
|
| 953 |
for (i = 0; i < nitems; i++) |
| 954 |
role_oids[i] = ObjectIdGetDatum(roles[i]); |
| 955 |
} |
| 956 |
|
| 957 |
if (qual != NULL) |
| 958 |
{ |
| 959 |
replaces[Anum_pg_policy_polqual - 1] = true; |
| 960 |
values[Anum_pg_policy_polqual - 1] |
| 961 |
= CStringGetTextDatum(nodeToString(qual)); |
| 962 |
} |
| 963 |
else |
| 964 |
{ |
| 965 |
Datum value_datum; |
| 966 |
bool attr_isnull; |
| 967 |
|
| 968 |
/* |
| 969 |
* We need to pull the USING expression and build the range table for |
| 970 |
* the policy from what's in the catalog, so that we can recreate the |
| 971 |
* dependencies correctly for the policy. |
| 972 |
*/ |
| 973 |
|
| 974 |
/* Check if the policy has a USING expr */ |
| 975 |
value_datum = heap_getattr(policy_tuple, Anum_pg_policy_polqual, |
| 976 |
RelationGetDescr(pg_policy_rel), |
| 977 |
&attr_isnull); |
| 978 |
if (!attr_isnull) |
| 979 |
{ |
| 980 |
char *qual_value; |
| 981 |
ParseState *qual_pstate; |
| 982 |
|
| 983 |
/* parsestate is built just to build the range table */ |
| 984 |
qual_pstate = make_parsestate(NULL); |
| 985 |
|
| 986 |
qual_value = TextDatumGetCString(value_datum); |
| 987 |
qual = stringToNode(qual_value); |
| 988 |
|
| 989 |
/* Add this rel to the parsestate's rangetable, for dependencies */ |
| 990 |
(void) addRangeTableEntryForRelation(qual_pstate, target_table, |
| 991 |
AccessShareLock, |
| 992 |
NULL, false, false); |
| 993 |
|
| 994 |
qual_parse_rtable = qual_pstate->p_rtable; |
| 995 |
free_parsestate(qual_pstate); |
| 996 |
} |
| 997 |
} |
| 998 |
|
| 999 |
if (with_check_qual != NULL) |
| 1000 |
{ |
| 1001 |
replaces[Anum_pg_policy_polwithcheck - 1] = true; |
| 1002 |
values[Anum_pg_policy_polwithcheck - 1] |
| 1003 |
= CStringGetTextDatum(nodeToString(with_check_qual)); |
| 1004 |
} |
| 1005 |
else |
| 1006 |
{ |
| 1007 |
Datum value_datum; |
| 1008 |
bool attr_isnull; |
| 1009 |
|
| 1010 |
/* |
| 1011 |
* We need to pull the WITH CHECK expression and build the range table |
| 1012 |
* for the policy from what's in the catalog, so that we can recreate |
| 1013 |
* the dependencies correctly for the policy. |
| 1014 |
*/ |
| 1015 |
|
| 1016 |
/* Check if the policy has a WITH CHECK expr */ |
| 1017 |
value_datum = heap_getattr(policy_tuple, Anum_pg_policy_polwithcheck, |
| 1018 |
RelationGetDescr(pg_policy_rel), |
| 1019 |
&attr_isnull); |
| 1020 |
if (!attr_isnull) |
| 1021 |
{ |
| 1022 |
char *with_check_value; |
| 1023 |
ParseState *with_check_pstate; |
| 1024 |
|
| 1025 |
/* parsestate is built just to build the range table */ |
| 1026 |
with_check_pstate = make_parsestate(NULL); |
| 1027 |
|
| 1028 |
with_check_value = TextDatumGetCString(value_datum); |
| 1029 |
with_check_qual = stringToNode(with_check_value); |
| 1030 |
|
| 1031 |
/* Add this rel to the parsestate's rangetable, for dependencies */ |
| 1032 |
(void) addRangeTableEntryForRelation(with_check_pstate, |
| 1033 |
target_table, |
| 1034 |
AccessShareLock, |
| 1035 |
NULL, false, false); |
| 1036 |
|
| 1037 |
with_check_parse_rtable = with_check_pstate->p_rtable; |
| 1038 |
free_parsestate(with_check_pstate); |
| 1039 |
} |
| 1040 |
} |
| 1041 |
|
| 1042 |
new_tuple = heap_modify_tuple(policy_tuple, |
| 1043 |
RelationGetDescr(pg_policy_rel), |
| 1044 |
values, isnull, replaces); |
| 1045 |
CatalogTupleUpdate(pg_policy_rel, &new_tuple->t_self, new_tuple); |
| 1046 |
|
| 1047 |
/* Update Dependencies. */ |
| 1048 |
deleteDependencyRecordsFor(PolicyRelationId, policy_id, false); |
| 1049 |
|
| 1050 |
/* Record Dependencies */ |
| 1051 |
target.classId = RelationRelationId; |
| 1052 |
target.objectId = table_id; |
| 1053 |
target.objectSubId = 0; |
| 1054 |
|
| 1055 |
myself.classId = PolicyRelationId; |
| 1056 |
myself.objectId = policy_id; |
| 1057 |
myself.objectSubId = 0; |
| 1058 |
|
| 1059 |
recordDependencyOn(&myself, &target, DEPENDENCY_AUTO); |
| 1060 |
|
| 1061 |
if (stmt->qual) |
| 1062 |
CheckUsageOnTypesInExpr(qual, qual_parse_rtable, GetUserId()); |
| 1063 |
recordDependencyOnExpr(&myself, qual, qual_parse_rtable, DEPENDENCY_NORMAL); |
| 1064 |
|
| 1065 |
if (stmt->with_check) |
| 1066 |
CheckUsageOnTypesInExpr(with_check_qual, with_check_parse_rtable, |
| 1067 |
GetUserId()); |
| 1068 |
recordDependencyOnExpr(&myself, with_check_qual, with_check_parse_rtable, |
| 1069 |
DEPENDENCY_NORMAL); |
| 1070 |
|
| 1071 |
/* Register role dependencies */ |
| 1072 |
deleteSharedDependencyRecordsFor(PolicyRelationId, policy_id, 0); |
| 1073 |
target.classId = AuthIdRelationId; |
| 1074 |
target.objectSubId = 0; |
| 1075 |
for (i = 0; i < nitems; i++) |
| 1076 |
{ |
| 1077 |
target.objectId = DatumGetObjectId(role_oids[i]); |
| 1078 |
/* no dependency if public */ |
| 1079 |
if (target.objectId != ACL_ID_PUBLIC) |
| 1080 |
recordSharedDependencyOn(&myself, &target, |
| 1081 |
SHARED_DEPENDENCY_POLICY); |
| 1082 |
} |
| 1083 |
|
| 1084 |
InvokeObjectPostAlterHook(PolicyRelationId, policy_id, 0); |
| 1085 |
|
| 1086 |
heap_freetuple(new_tuple); |
| 1087 |
|
| 1088 |
/* Invalidate Relation Cache */ |
| 1089 |
CacheInvalidateRelcache(target_table); |
| 1090 |
|
| 1091 |
/* Clean up. */ |
| 1092 |
systable_endscan(sscan); |
| 1093 |
relation_close(target_table, NoLock); |
| 1094 |
table_close(pg_policy_rel, RowExclusiveLock); |
| 1095 |
|
| 1096 |
return myself; |
| 1097 |
} |
| 1098 |
|
| 1099 |
/* |
| 1100 |
* rename_policy - |
| 1101 |
* change the name of a policy on a relation |
| 1102 |
*/ |
| 1103 |
ObjectAddress |
| 1104 |
rename_policy(RenameStmt *stmt) |
| 1105 |
{ |
| 1106 |
Relation pg_policy_rel; |
| 1107 |
Relation target_table; |
| 1108 |
Oid table_id; |
| 1109 |
Oid opoloid; |
| 1110 |
ScanKeyData skey[2]; |
| 1111 |
SysScanDesc sscan; |
| 1112 |
HeapTuple policy_tuple; |
| 1113 |
ObjectAddress address; |
| 1114 |
|
| 1115 |
/* Get id of table. Also handles permissions checks. */ |
| 1116 |
table_id = RangeVarGetRelidExtended(stmt->relation, AccessExclusiveLock, |
| 1117 |
0, |
| 1118 |
RangeVarCallbackForPolicy, |
| 1119 |
stmt); |
| 1120 |
|
| 1121 |
target_table = relation_open(table_id, NoLock); |
| 1122 |
|
| 1123 |
pg_policy_rel = table_open(PolicyRelationId, RowExclusiveLock); |
| 1124 |
|
| 1125 |
/* First pass -- check for conflict */ |
| 1126 |
|
| 1127 |
/* Add key - policy's relation id. */ |
| 1128 |
ScanKeyInit(&skey[0], |
| 1129 |
Anum_pg_policy_polrelid, |
| 1130 |
BTEqualStrategyNumber, F_OIDEQ, |
| 1131 |
ObjectIdGetDatum(table_id)); |
| 1132 |
|
| 1133 |
/* Add key - policy's name. */ |
| 1134 |
ScanKeyInit(&skey[1], |
| 1135 |
Anum_pg_policy_polname, |
| 1136 |
BTEqualStrategyNumber, F_NAMEEQ, |
| 1137 |
CStringGetDatum(stmt->newname)); |
| 1138 |
|
| 1139 |
sscan = systable_beginscan(pg_policy_rel, |
| 1140 |
PolicyPolrelidPolnameIndexId, true, NULL, 2, |
| 1141 |
skey); |
| 1142 |
|
| 1143 |
if (HeapTupleIsValid(systable_getnext(sscan))) |
| 1144 |
ereport(ERROR, |
| 1145 |
(errcode(ERRCODE_DUPLICATE_OBJECT), |
| 1146 |
errmsg("policy \"%s\" for table \"%s\" already exists", |
| 1147 |
stmt->newname, RelationGetRelationName(target_table)))); |
| 1148 |
|
| 1149 |
systable_endscan(sscan); |
| 1150 |
|
| 1151 |
/* Second pass -- find existing policy and update */ |
| 1152 |
/* Add key - policy's relation id. */ |
| 1153 |
ScanKeyInit(&skey[0], |
| 1154 |
Anum_pg_policy_polrelid, |
| 1155 |
BTEqualStrategyNumber, F_OIDEQ, |
| 1156 |
ObjectIdGetDatum(table_id)); |
| 1157 |
|
| 1158 |
/* Add key - policy's name. */ |
| 1159 |
ScanKeyInit(&skey[1], |
| 1160 |
Anum_pg_policy_polname, |
| 1161 |
BTEqualStrategyNumber, F_NAMEEQ, |
| 1162 |
CStringGetDatum(stmt->subname)); |
| 1163 |
|
| 1164 |
sscan = systable_beginscan(pg_policy_rel, |
| 1165 |
PolicyPolrelidPolnameIndexId, true, NULL, 2, |
| 1166 |
skey); |
| 1167 |
|
| 1168 |
policy_tuple = systable_getnext(sscan); |
| 1169 |
|
| 1170 |
/* Complain if we did not find the policy */ |
| 1171 |
if (!HeapTupleIsValid(policy_tuple)) |
| 1172 |
ereport(ERROR, |
| 1173 |
(errcode(ERRCODE_UNDEFINED_OBJECT), |
| 1174 |
errmsg("policy \"%s\" for table \"%s\" does not exist", |
| 1175 |
stmt->subname, RelationGetRelationName(target_table)))); |
| 1176 |
|
| 1177 |
opoloid = ((Form_pg_policy) GETSTRUCT(policy_tuple))->oid; |
| 1178 |
|
| 1179 |
policy_tuple = heap_copytuple(policy_tuple); |
| 1180 |
|
| 1181 |
namestrcpy(&((Form_pg_policy) GETSTRUCT(policy_tuple))->polname, |
| 1182 |
stmt->newname); |
| 1183 |
|
| 1184 |
CatalogTupleUpdate(pg_policy_rel, &policy_tuple->t_self, policy_tuple); |
| 1185 |
|
| 1186 |
InvokeObjectPostAlterHook(PolicyRelationId, opoloid, 0); |
| 1187 |
|
| 1188 |
ObjectAddressSet(address, PolicyRelationId, opoloid); |
| 1189 |
|
| 1190 |
/* |
| 1191 |
* Invalidate relation's relcache entry so that other backends (and this |
| 1192 |
* one too!) are sent SI message to make them rebuild relcache entries. |
| 1193 |
* (Ideally this should happen automatically...) |
| 1194 |
*/ |
| 1195 |
CacheInvalidateRelcache(target_table); |
| 1196 |
|
| 1197 |
/* Clean up. */ |
| 1198 |
systable_endscan(sscan); |
| 1199 |
table_close(pg_policy_rel, RowExclusiveLock); |
| 1200 |
relation_close(target_table, NoLock); |
| 1201 |
|
| 1202 |
return address; |
| 1203 |
} |
| 1204 |
|
| 1205 |
/* |
| 1206 |
* get_relation_policy_oid - Look up a policy by name to find its OID |
| 1207 |
* |
| 1208 |
* If missing_ok is false, throw an error if policy not found. If |
| 1209 |
* true, just return InvalidOid. |
| 1210 |
*/ |
| 1211 |
Oid |
| 1212 |
get_relation_policy_oid(Oid relid, const char *policy_name, bool missing_ok) |
| 1213 |
{ |
| 1214 |
Relation pg_policy_rel; |
| 1215 |
ScanKeyData skey[2]; |
| 1216 |
SysScanDesc sscan; |
| 1217 |
HeapTuple policy_tuple; |
| 1218 |
Oid policy_oid; |
| 1219 |
|
| 1220 |
pg_policy_rel = table_open(PolicyRelationId, AccessShareLock); |
| 1221 |
|
| 1222 |
/* Add key - policy's relation id. */ |
| 1223 |
ScanKeyInit(&skey[0], |
| 1224 |
Anum_pg_policy_polrelid, |
| 1225 |
BTEqualStrategyNumber, F_OIDEQ, |
| 1226 |
ObjectIdGetDatum(relid)); |
| 1227 |
|
| 1228 |
/* Add key - policy's name. */ |
| 1229 |
ScanKeyInit(&skey[1], |
| 1230 |
Anum_pg_policy_polname, |
| 1231 |
BTEqualStrategyNumber, F_NAMEEQ, |
| 1232 |
CStringGetDatum(policy_name)); |
| 1233 |
|
| 1234 |
sscan = systable_beginscan(pg_policy_rel, |
| 1235 |
PolicyPolrelidPolnameIndexId, true, NULL, 2, |
| 1236 |
skey); |
| 1237 |
|
| 1238 |
policy_tuple = systable_getnext(sscan); |
| 1239 |
|
| 1240 |
if (!HeapTupleIsValid(policy_tuple)) |
| 1241 |
{ |
| 1242 |
if (!missing_ok) |
| 1243 |
ereport(ERROR, |
| 1244 |
(errcode(ERRCODE_UNDEFINED_OBJECT), |
| 1245 |
errmsg("policy \"%s\" for table \"%s\" does not exist", |
| 1246 |
policy_name, get_rel_name(relid)))); |
| 1247 |
|
| 1248 |
policy_oid = InvalidOid; |
| 1249 |
} |
| 1250 |
else |
| 1251 |
policy_oid = ((Form_pg_policy) GETSTRUCT(policy_tuple))->oid; |
| 1252 |
|
| 1253 |
/* Clean up. */ |
| 1254 |
systable_endscan(sscan); |
| 1255 |
table_close(pg_policy_rel, AccessShareLock); |
| 1256 |
|
| 1257 |
return policy_oid; |
| 1258 |
} |
| 1259 |
|
| 1260 |
/* |
| 1261 |
* relation_has_policies - Determine if relation has any policies |
| 1262 |
*/ |
| 1263 |
bool |
| 1264 |
relation_has_policies(Relation rel) |
| 1265 |
{ |
| 1266 |
Relation catalog; |
| 1267 |
ScanKeyData skey; |
| 1268 |
SysScanDesc sscan; |
| 1269 |
HeapTuple policy_tuple; |
| 1270 |
bool ret = false; |
| 1271 |
|
| 1272 |
catalog = table_open(PolicyRelationId, AccessShareLock); |
| 1273 |
ScanKeyInit(&skey, |
| 1274 |
Anum_pg_policy_polrelid, |
| 1275 |
BTEqualStrategyNumber, F_OIDEQ, |
| 1276 |
ObjectIdGetDatum(RelationGetRelid(rel))); |
| 1277 |
sscan = systable_beginscan(catalog, PolicyPolrelidPolnameIndexId, true, |
| 1278 |
NULL, 1, &skey); |
| 1279 |
policy_tuple = systable_getnext(sscan); |
| 1280 |
if (HeapTupleIsValid(policy_tuple)) |
| 1281 |
ret = true; |
| 1282 |
|
| 1283 |
systable_endscan(sscan); |
| 1284 |
table_close(catalog, AccessShareLock); |
| 1285 |
|
| 1286 |
return ret; |
| 1287 |
} |