PostgreSQL Source Code git master
test_rls_hooks.c File Reference
#include "postgres.h"
#include "catalog/pg_type.h"
#include "fmgr.h"
#include "nodes/makefuncs.h"
#include "parser/parse_clause.h"
#include "parser/parse_collate.h"
#include "parser/parse_node.h"
#include "parser/parse_relation.h"
#include "rewrite/rowsecurity.h"
#include "test_rls_hooks.h"
#include "utils/acl.h"
#include "utils/rel.h"
#include "utils/relcache.h"
Include dependency graph for test_rls_hooks.c:

Go to the source code of this file.

Functions

void _PG_init (void)
 
Listtest_rls_hooks_permissive (CmdType cmdtype, Relation relation)
 
Listtest_rls_hooks_restrictive (CmdType cmdtype, Relation relation)
 

Variables

 PG_MODULE_MAGIC
 

Function Documentation

◆ _PG_init()

void _PG_init ( void  )

Definition at line 33 of file test_rls_hooks.c.

34{
35 /* Set our hooks */
38}
row_security_policy_hook_type row_security_policy_hook_permissive
Definition: rowsecurity.c:86
row_security_policy_hook_type row_security_policy_hook_restrictive
Definition: rowsecurity.c:87
List * test_rls_hooks_restrictive(CmdType cmdtype, Relation relation)
List * test_rls_hooks_permissive(CmdType cmdtype, Relation relation)

References row_security_policy_hook_permissive, row_security_policy_hook_restrictive, test_rls_hooks_permissive(), and test_rls_hooks_restrictive().

◆ test_rls_hooks_permissive()

List * test_rls_hooks_permissive ( CmdType  cmdtype,
Relation  relation 
)

Definition at line 44 of file test_rls_hooks.c.

45{
46 List *policies = NIL;
48 Datum role;
49 FuncCall *n;
50 Node *e;
51 ColumnRef *c;
52 ParseState *qual_pstate;
53 ParseNamespaceItem *nsitem;
54
55 if (strcmp(RelationGetRelationName(relation), "rls_test_permissive") != 0 &&
56 strcmp(RelationGetRelationName(relation), "rls_test_both") != 0)
57 return NIL;
58
59 qual_pstate = make_parsestate(NULL);
60
61 nsitem = addRangeTableEntryForRelation(qual_pstate,
62 relation, AccessShareLock,
63 NULL, false, false);
64 addNSItemToQuery(qual_pstate, nsitem, false, true, true);
65
67
68 policy->policy_name = pstrdup("extension policy");
69 policy->polcmd = '*';
70 policy->roles = construct_array_builtin(&role, 1, OIDOID);
71
72 /*
73 * policy->qual = (Expr *) makeConst(BOOLOID, -1, InvalidOid,
74 * sizeof(bool), BoolGetDatum(true), false, true);
75 */
76
77 n = makeFuncCall(list_make2(makeString("pg_catalog"),
78 makeString("current_user")),
79 NIL,
81 -1);
82
84 c->fields = list_make1(makeString("username"));
85 c->location = 0;
86
87 e = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", (Node *) n, (Node *) c, 0);
88
89 policy->qual = (Expr *) transformWhereClause(qual_pstate, copyObject(e),
91 "POLICY");
92 /* Fix up collation information */
93 assign_expr_collations(qual_pstate, (Node *) policy->qual);
94
95 policy->with_check_qual = copyObject(policy->qual);
96 policy->hassublinks = false;
97
98 policies = list_make1(policy);
99
100 return policies;
101}
#define ACL_ID_PUBLIC
Definition: acl.h:46
ArrayType * construct_array_builtin(Datum *elems, int nelems, Oid elmtype)
Definition: arrayfuncs.c:3381
#define AccessShareLock
Definition: lockdefs.h:36
A_Expr * makeSimpleA_Expr(A_Expr_Kind kind, char *name, Node *lexpr, Node *rexpr, int location)
Definition: makefuncs.c:48
FuncCall * makeFuncCall(List *name, List *args, CoercionForm funcformat, int location)
Definition: makefuncs.c:629
char * pstrdup(const char *in)
Definition: mcxt.c:1696
void * palloc0(Size size)
Definition: mcxt.c:1347
#define copyObject(obj)
Definition: nodes.h:224
#define makeNode(_type_)
Definition: nodes.h:155
Node * transformWhereClause(ParseState *pstate, Node *clause, ParseExprKind exprKind, const char *constructName)
void assign_expr_collations(ParseState *pstate, Node *expr)
ParseState * make_parsestate(ParseState *parentParseState)
Definition: parse_node.c:39
@ EXPR_KIND_POLICY
Definition: parse_node.h:78
ParseNamespaceItem * addRangeTableEntryForRelation(ParseState *pstate, Relation rel, int lockmode, Alias *alias, bool inh, bool inFromCl)
void addNSItemToQuery(ParseState *pstate, ParseNamespaceItem *nsitem, bool addToJoinList, bool addToRelNameSpace, bool addToVarNameSpace)
@ AEXPR_OP
Definition: parsenodes.h:324
#define NIL
Definition: pg_list.h:68
#define list_make1(x1)
Definition: pg_list.h:212
#define list_make2(x1, x2)
Definition: pg_list.h:214
uintptr_t Datum
Definition: postgres.h:69
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:257
char * c
e
Definition: preproc-init.c:82
@ COERCE_EXPLICIT_CALL
Definition: primnodes.h:750
#define RelationGetRelationName(relation)
Definition: rel.h:539
Definition: pg_list.h:54
Definition: nodes.h:129
ArrayType * roles
Definition: rowsecurity.h:24
Expr * with_check_qual
Definition: rowsecurity.h:27
String * makeString(char *str)
Definition: value.c:63

References AccessShareLock, ACL_ID_PUBLIC, addNSItemToQuery(), addRangeTableEntryForRelation(), AEXPR_OP, assign_expr_collations(), COERCE_EXPLICIT_CALL, construct_array_builtin(), copyObject, EXPR_KIND_POLICY, RowSecurityPolicy::hassublinks, list_make1, list_make2, make_parsestate(), makeFuncCall(), makeNode, makeSimpleA_Expr(), makeString(), NIL, ObjectIdGetDatum(), palloc0(), RowSecurityPolicy::polcmd, RowSecurityPolicy::policy_name, pstrdup(), RowSecurityPolicy::qual, RelationGetRelationName, RowSecurityPolicy::roles, transformWhereClause(), and RowSecurityPolicy::with_check_qual.

Referenced by _PG_init().

◆ test_rls_hooks_restrictive()

List * test_rls_hooks_restrictive ( CmdType  cmdtype,
Relation  relation 
)

Definition at line 112 of file test_rls_hooks.c.

113{
114 List *policies = NIL;
116 Datum role;
117 FuncCall *n;
118 Node *e;
119 ColumnRef *c;
120 ParseState *qual_pstate;
121 ParseNamespaceItem *nsitem;
122
123 if (strcmp(RelationGetRelationName(relation), "rls_test_restrictive") != 0 &&
124 strcmp(RelationGetRelationName(relation), "rls_test_both") != 0)
125 return NIL;
126
127 qual_pstate = make_parsestate(NULL);
128
129 nsitem = addRangeTableEntryForRelation(qual_pstate,
130 relation, AccessShareLock,
131 NULL, false, false);
132 addNSItemToQuery(qual_pstate, nsitem, false, true, true);
133
135
136 policy->policy_name = pstrdup("extension policy");
137 policy->polcmd = '*';
138 policy->roles = construct_array_builtin(&role, 1, OIDOID);
139
140 n = makeFuncCall(list_make2(makeString("pg_catalog"),
141 makeString("current_user")),
142 NIL,
144 -1);
145
147 c->fields = list_make1(makeString("supervisor"));
148 c->location = 0;
149
150 e = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", (Node *) n, (Node *) c, 0);
151
152 policy->qual = (Expr *) transformWhereClause(qual_pstate, copyObject(e),
154 "POLICY");
155 /* Fix up collation information */
156 assign_expr_collations(qual_pstate, (Node *) policy->qual);
157
158 policy->with_check_qual = copyObject(policy->qual);
159 policy->hassublinks = false;
160
161 policies = list_make1(policy);
162
163 return policies;
164}

References AccessShareLock, ACL_ID_PUBLIC, addNSItemToQuery(), addRangeTableEntryForRelation(), AEXPR_OP, assign_expr_collations(), COERCE_EXPLICIT_CALL, construct_array_builtin(), copyObject, EXPR_KIND_POLICY, RowSecurityPolicy::hassublinks, list_make1, list_make2, make_parsestate(), makeFuncCall(), makeNode, makeSimpleA_Expr(), makeString(), NIL, ObjectIdGetDatum(), palloc0(), RowSecurityPolicy::polcmd, RowSecurityPolicy::policy_name, pstrdup(), RowSecurityPolicy::qual, RelationGetRelationName, RowSecurityPolicy::roles, transformWhereClause(), and RowSecurityPolicy::with_check_qual.

Referenced by _PG_init().

Variable Documentation

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 29 of file test_rls_hooks.c.