PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
test_rls_hooks.c File Reference
#include "postgres.h"
#include "fmgr.h"
#include "miscadmin.h"
#include "test_rls_hooks.h"
#include <catalog/pg_type.h>
#include <nodes/makefuncs.h>
#include <parser/parse_clause.h>
#include <parser/parse_node.h>
#include <parser/parse_relation.h>
#include <rewrite/rowsecurity.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)
 
void _PG_fini (void)
 
Listtest_rls_hooks_permissive (CmdType cmdtype, Relation relation)
 
Listtest_rls_hooks_restrictive (CmdType cmdtype, Relation relation)
 

Variables

 PG_MODULE_MAGIC
 
static
row_security_policy_hook_type 
prev_row_security_policy_hook_permissive = NULL
 
static
row_security_policy_hook_type 
prev_row_security_policy_hook_restrictive = NULL
 

Function Documentation

void _PG_fini ( void  )

Definition at line 56 of file test_rls_hooks.c.

References prev_row_security_policy_hook_permissive, prev_row_security_policy_hook_restrictive, row_security_policy_hook_permissive, and row_security_policy_hook_restrictive.

57 {
60 }
static row_security_policy_hook_type prev_row_security_policy_hook_permissive
row_security_policy_hook_type row_security_policy_hook_permissive
Definition: rowsecurity.c:95
static row_security_policy_hook_type prev_row_security_policy_hook_restrictive
row_security_policy_hook_type row_security_policy_hook_restrictive
Definition: rowsecurity.c:96
void _PG_init ( void  )

Definition at line 43 of file test_rls_hooks.c.

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

44 {
45  /* Save values for unload */
48 
49  /* Set our hooks */
52 }
static row_security_policy_hook_type prev_row_security_policy_hook_permissive
row_security_policy_hook_type row_security_policy_hook_permissive
Definition: rowsecurity.c:95
List * test_rls_hooks_permissive(CmdType cmdtype, Relation relation)
static row_security_policy_hook_type prev_row_security_policy_hook_restrictive
row_security_policy_hook_type row_security_policy_hook_restrictive
Definition: rowsecurity.c:96
List * test_rls_hooks_restrictive(CmdType cmdtype, Relation relation)
List* test_rls_hooks_permissive ( CmdType  cmdtype,
Relation  relation 
)

Definition at line 66 of file test_rls_hooks.c.

References ACL_ID_PUBLIC, addRangeTableEntryForRelation(), addRTEtoQuery(), AEXPR_OP, construct_array(), copyObject, EXPR_KIND_POLICY, ColumnRef::fields, RowSecurityPolicy::hassublinks, list_make1, list_make2, ColumnRef::location, make_parsestate(), makeFuncCall(), makeNode, makeSimpleA_Expr(), makeString(), NIL, NULL, ObjectIdGetDatum, OIDOID, palloc0(), RowSecurityPolicy::polcmd, RowSecurityPolicy::policy_name, pstrdup(), RowSecurityPolicy::qual, RelationGetRelationName, RowSecurityPolicy::roles, transformWhereClause(), and RowSecurityPolicy::with_check_qual.

Referenced by _PG_init().

67 {
68  List *policies = NIL;
69  RowSecurityPolicy *policy = palloc0(sizeof(RowSecurityPolicy));
70  Datum role;
71  FuncCall *n;
72  Node *e;
73  ColumnRef *c;
74  ParseState *qual_pstate;
75  RangeTblEntry *rte;
76 
77  if (strcmp(RelationGetRelationName(relation), "rls_test_permissive")
78  && strcmp(RelationGetRelationName(relation), "rls_test_both"))
79  return NIL;
80 
81  qual_pstate = make_parsestate(NULL);
82 
83  rte = addRangeTableEntryForRelation(qual_pstate, relation, NULL, false,
84  false);
85  addRTEtoQuery(qual_pstate, rte, false, true, true);
86 
88 
89  policy->policy_name = pstrdup("extension policy");
90  policy->polcmd = '*';
91  policy->roles = construct_array(&role, 1, OIDOID, sizeof(Oid), true, 'i');
92 
93  /*
94  * policy->qual = (Expr *) makeConst(BOOLOID, -1, InvalidOid,
95  * sizeof(bool), BoolGetDatum(true), false, true);
96  */
97 
98  n = makeFuncCall(list_make2(makeString("pg_catalog"),
99  makeString("current_user")), NIL, 0);
100 
101  c = makeNode(ColumnRef);
102  c->fields = list_make1(makeString("username"));
103  c->location = 0;
104 
105  e = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", (Node *) n, (Node *) c, 0);
106 
107  policy->qual = (Expr *) transformWhereClause(qual_pstate, copyObject(e),
109  "POLICY");
110 
111  policy->with_check_qual = copyObject(policy->qual);
112  policy->hassublinks = false;
113 
114  policies = list_make1(policy);
115 
116  return policies;
117 }
#define list_make2(x1, x2)
Definition: pg_list.h:140
Value * makeString(char *str)
Definition: value.c:53
#define NIL
Definition: pg_list.h:69
#define OIDOID
Definition: pg_type.h:328
char * pstrdup(const char *in)
Definition: mcxt.c:1077
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3306
Definition: nodes.h:509
unsigned int Oid
Definition: postgres_ext.h:31
A_Expr * makeSimpleA_Expr(A_Expr_Kind kind, char *name, Node *lexpr, Node *rexpr, int location)
Definition: makefuncs.c:49
ParseState * make_parsestate(ParseState *parentParseState)
Definition: parse_node.c:44
int location
Definition: parsenodes.h:235
#define list_make1(x1)
Definition: pg_list.h:139
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
char * c
Expr * with_check_qual
Definition: rowsecurity.h:27
#define RelationGetRelationName(relation)
Definition: rel.h:436
ArrayType * roles
Definition: rowsecurity.h:24
void addRTEtoQuery(ParseState *pstate, RangeTblEntry *rte, bool addToJoinList, bool addToRelNameSpace, bool addToVarNameSpace)
FuncCall * makeFuncCall(List *name, List *args, int location)
Definition: makefuncs.c:583
void * palloc0(Size size)
Definition: mcxt.c:878
uintptr_t Datum
Definition: postgres.h:372
#define makeNode(_type_)
Definition: nodes.h:557
#define NULL
Definition: c.h:229
Node * transformWhereClause(ParseState *pstate, Node *clause, ParseExprKind exprKind, const char *constructName)
RangeTblEntry * addRangeTableEntryForRelation(ParseState *pstate, Relation rel, Alias *alias, bool inh, bool inFromCl)
e
Definition: preproc-init.c:82
#define ACL_ID_PUBLIC
Definition: acl.h:39
#define copyObject(obj)
Definition: nodes.h:622
Definition: pg_list.h:45
List * fields
Definition: parsenodes.h:234
List* test_rls_hooks_restrictive ( CmdType  cmdtype,
Relation  relation 
)

Definition at line 128 of file test_rls_hooks.c.

References ACL_ID_PUBLIC, addRangeTableEntryForRelation(), addRTEtoQuery(), AEXPR_OP, construct_array(), copyObject, EXPR_KIND_POLICY, ColumnRef::fields, RowSecurityPolicy::hassublinks, list_make1, list_make2, ColumnRef::location, make_parsestate(), makeFuncCall(), makeNode, makeSimpleA_Expr(), makeString(), NIL, NULL, ObjectIdGetDatum, OIDOID, palloc0(), RowSecurityPolicy::polcmd, RowSecurityPolicy::policy_name, pstrdup(), RowSecurityPolicy::qual, RelationGetRelationName, RowSecurityPolicy::roles, transformWhereClause(), and RowSecurityPolicy::with_check_qual.

Referenced by _PG_init().

129 {
130  List *policies = NIL;
131  RowSecurityPolicy *policy = palloc0(sizeof(RowSecurityPolicy));
132  Datum role;
133  FuncCall *n;
134  Node *e;
135  ColumnRef *c;
136  ParseState *qual_pstate;
137  RangeTblEntry *rte;
138 
139 
140  if (strcmp(RelationGetRelationName(relation), "rls_test_restrictive")
141  && strcmp(RelationGetRelationName(relation), "rls_test_both"))
142  return NIL;
143 
144  qual_pstate = make_parsestate(NULL);
145 
146  rte = addRangeTableEntryForRelation(qual_pstate, relation, NULL, false,
147  false);
148  addRTEtoQuery(qual_pstate, rte, false, true, true);
149 
151 
152  policy->policy_name = pstrdup("extension policy");
153  policy->polcmd = '*';
154  policy->roles = construct_array(&role, 1, OIDOID, sizeof(Oid), true, 'i');
155 
156  n = makeFuncCall(list_make2(makeString("pg_catalog"),
157  makeString("current_user")), NIL, 0);
158 
159  c = makeNode(ColumnRef);
160  c->fields = list_make1(makeString("supervisor"));
161  c->location = 0;
162 
163  e = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", (Node *) n, (Node *) c, 0);
164 
165  policy->qual = (Expr *) transformWhereClause(qual_pstate, copyObject(e),
167  "POLICY");
168 
169  policy->with_check_qual = copyObject(policy->qual);
170  policy->hassublinks = false;
171 
172  policies = list_make1(policy);
173 
174  return policies;
175 }
#define list_make2(x1, x2)
Definition: pg_list.h:140
Value * makeString(char *str)
Definition: value.c:53
#define NIL
Definition: pg_list.h:69
#define OIDOID
Definition: pg_type.h:328
char * pstrdup(const char *in)
Definition: mcxt.c:1077
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3306
Definition: nodes.h:509
unsigned int Oid
Definition: postgres_ext.h:31
A_Expr * makeSimpleA_Expr(A_Expr_Kind kind, char *name, Node *lexpr, Node *rexpr, int location)
Definition: makefuncs.c:49
ParseState * make_parsestate(ParseState *parentParseState)
Definition: parse_node.c:44
int location
Definition: parsenodes.h:235
#define list_make1(x1)
Definition: pg_list.h:139
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
char * c
Expr * with_check_qual
Definition: rowsecurity.h:27
#define RelationGetRelationName(relation)
Definition: rel.h:436
ArrayType * roles
Definition: rowsecurity.h:24
void addRTEtoQuery(ParseState *pstate, RangeTblEntry *rte, bool addToJoinList, bool addToRelNameSpace, bool addToVarNameSpace)
FuncCall * makeFuncCall(List *name, List *args, int location)
Definition: makefuncs.c:583
void * palloc0(Size size)
Definition: mcxt.c:878
uintptr_t Datum
Definition: postgres.h:372
#define makeNode(_type_)
Definition: nodes.h:557
#define NULL
Definition: c.h:229
Node * transformWhereClause(ParseState *pstate, Node *clause, ParseExprKind exprKind, const char *constructName)
RangeTblEntry * addRangeTableEntryForRelation(ParseState *pstate, Relation rel, Alias *alias, bool inh, bool inFromCl)
e
Definition: preproc-init.c:82
#define ACL_ID_PUBLIC
Definition: acl.h:39
#define copyObject(obj)
Definition: nodes.h:622
Definition: pg_list.h:45
List * fields
Definition: parsenodes.h:234

Variable Documentation

PG_MODULE_MAGIC

Definition at line 32 of file test_rls_hooks.c.

row_security_policy_hook_type prev_row_security_policy_hook_permissive = NULL
static

Definition at line 35 of file test_rls_hooks.c.

Referenced by _PG_fini(), and _PG_init().

row_security_policy_hook_type prev_row_security_policy_hook_restrictive = NULL
static

Definition at line 36 of file test_rls_hooks.c.

Referenced by _PG_fini(), and _PG_init().