summaryrefslogtreecommitdiff
path: root/src/backend/catalog/objectaccess.c
blob: 2181978d2d046ac17ead7a8dbf27835c4c659216 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/* -------------------------------------------------------------------------
 *
 * objectaccess.c
 *		functions for object_access_hook on various events
 *
 * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 * -------------------------------------------------------------------------
 */
#include "postgres.h"

#include "catalog/objectaccess.h"
#include "catalog/pg_class.h"
#include "catalog/pg_namespace.h"
#include "catalog/pg_proc.h"

/*
 * Hook on object accesses.  This is intended as infrastructure for security
 * and logging plugins.
 */
object_access_hook_type object_access_hook = NULL;
object_access_hook_type_str object_access_hook_str = NULL;


/*
 * RunObjectPostCreateHook
 *
 * OAT_POST_CREATE object ID based event hook entrypoint
 */
void
RunObjectPostCreateHook(Oid classId, Oid objectId, int subId,
						bool is_internal)
{
	ObjectAccessPostCreate pc_arg;

	/* caller should check, but just in case... */
	Assert(object_access_hook != NULL);

	memset(&pc_arg, 0, sizeof(ObjectAccessPostCreate));
	pc_arg.is_internal = is_internal;

	(*object_access_hook) (OAT_POST_CREATE,
						   classId, objectId, subId,
						   (void *) &pc_arg);
}

/*
 * RunObjectDropHook
 *
 * OAT_DROP object ID based event hook entrypoint
 */
void
RunObjectDropHook(Oid classId, Oid objectId, int subId,
				  int dropflags)
{
	ObjectAccessDrop drop_arg;

	/* caller should check, but just in case... */
	Assert(object_access_hook != NULL);

	memset(&drop_arg, 0, sizeof(ObjectAccessDrop));
	drop_arg.dropflags = dropflags;

	(*object_access_hook) (OAT_DROP,
						   classId, objectId, subId,
						   (void *) &drop_arg);
}

/*
 * RunObjectTruncateHook
 *
 * OAT_TRUNCATE object ID based event hook entrypoint
 */
void
RunObjectTruncateHook(Oid objectId)
{
	/* caller should check, but just in case... */
	Assert(object_access_hook != NULL);

	(*object_access_hook) (OAT_TRUNCATE,
						   RelationRelationId, objectId, 0,
						   NULL);
}

/*
 * RunObjectPostAlterHook
 *
 * OAT_POST_ALTER object ID based event hook entrypoint
 */
void
RunObjectPostAlterHook(Oid classId, Oid objectId, int subId,
					   Oid auxiliaryId, bool is_internal)
{
	ObjectAccessPostAlter pa_arg;

	/* caller should check, but just in case... */
	Assert(object_access_hook != NULL);

	memset(&pa_arg, 0, sizeof(ObjectAccessPostAlter));
	pa_arg.auxiliary_id = auxiliaryId;
	pa_arg.is_internal = is_internal;

	(*object_access_hook) (OAT_POST_ALTER,
						   classId, objectId, subId,
						   (void *) &pa_arg);
}

/*
 * RunNamespaceSearchHook
 *
 * OAT_NAMESPACE_SEARCH object ID based event hook entrypoint
 */
bool
RunNamespaceSearchHook(Oid objectId, bool ereport_on_violation)
{
	ObjectAccessNamespaceSearch ns_arg;

	/* caller should check, but just in case... */
	Assert(object_access_hook != NULL);

	memset(&ns_arg, 0, sizeof(ObjectAccessNamespaceSearch));
	ns_arg.ereport_on_violation = ereport_on_violation;
	ns_arg.result = true;

	(*object_access_hook) (OAT_NAMESPACE_SEARCH,
						   NamespaceRelationId, objectId, 0,
						   (void *) &ns_arg);

	return ns_arg.result;
}

/*
 * RunFunctionExecuteHook
 *
 * OAT_FUNCTION_EXECUTE object ID based event hook entrypoint
 */
void
RunFunctionExecuteHook(Oid objectId)
{
	/* caller should check, but just in case... */
	Assert(object_access_hook != NULL);

	(*object_access_hook) (OAT_FUNCTION_EXECUTE,
						   ProcedureRelationId, objectId, 0,
						   NULL);
}

/* String versions */


/*
 * RunObjectPostCreateHookStr
 *
 * OAT_POST_CREATE object name based event hook entrypoint
 */
void
RunObjectPostCreateHookStr(Oid classId, const char *objectName, int subId,
						   bool is_internal)
{
	ObjectAccessPostCreate pc_arg;

	/* caller should check, but just in case... */
	Assert(object_access_hook_str != NULL);

	memset(&pc_arg, 0, sizeof(ObjectAccessPostCreate));
	pc_arg.is_internal = is_internal;

	(*object_access_hook_str) (OAT_POST_CREATE,
							   classId, objectName, subId,
							   (void *) &pc_arg);
}

/*
 * RunObjectDropHookStr
 *
 * OAT_DROP object name based event hook entrypoint
 */
void
RunObjectDropHookStr(Oid classId, const char *objectName, int subId,
					 int dropflags)
{
	ObjectAccessDrop drop_arg;

	/* caller should check, but just in case... */
	Assert(object_access_hook_str != NULL);

	memset(&drop_arg, 0, sizeof(ObjectAccessDrop));
	drop_arg.dropflags = dropflags;

	(*object_access_hook_str) (OAT_DROP,
							   classId, objectName, subId,
							   (void *) &drop_arg);
}

/*
 * RunObjectTruncateHookStr
 *
 * OAT_TRUNCATE object name based event hook entrypoint
 */
void
RunObjectTruncateHookStr(const char *objectName)
{
	/* caller should check, but just in case... */
	Assert(object_access_hook_str != NULL);

	(*object_access_hook_str) (OAT_TRUNCATE,
							   RelationRelationId, objectName, 0,
							   NULL);
}

/*
 * RunObjectPostAlterHookStr
 *
 * OAT_POST_ALTER object name based event hook entrypoint
 */
void
RunObjectPostAlterHookStr(Oid classId, const char *objectName, int subId,
						  Oid auxiliaryId, bool is_internal)
{
	ObjectAccessPostAlter pa_arg;

	/* caller should check, but just in case... */
	Assert(object_access_hook_str != NULL);

	memset(&pa_arg, 0, sizeof(ObjectAccessPostAlter));
	pa_arg.auxiliary_id = auxiliaryId;
	pa_arg.is_internal = is_internal;

	(*object_access_hook_str) (OAT_POST_ALTER,
							   classId, objectName, subId,
							   (void *) &pa_arg);
}

/*
 * RunNamespaceSearchHookStr
 *
 * OAT_NAMESPACE_SEARCH object name based event hook entrypoint
 */
bool
RunNamespaceSearchHookStr(const char *objectName, bool ereport_on_violation)
{
	ObjectAccessNamespaceSearch ns_arg;

	/* caller should check, but just in case... */
	Assert(object_access_hook_str != NULL);

	memset(&ns_arg, 0, sizeof(ObjectAccessNamespaceSearch));
	ns_arg.ereport_on_violation = ereport_on_violation;
	ns_arg.result = true;

	(*object_access_hook_str) (OAT_NAMESPACE_SEARCH,
							   NamespaceRelationId, objectName, 0,
							   (void *) &ns_arg);

	return ns_arg.result;
}

/*
 * RunFunctionExecuteHookStr
 *
 * OAT_FUNCTION_EXECUTE object name based event hook entrypoint
 */
void
RunFunctionExecuteHookStr(const char *objectName)
{
	/* caller should check, but just in case... */
	Assert(object_access_hook_str != NULL);

	(*object_access_hook_str) (OAT_FUNCTION_EXECUTE,
							   ProcedureRelationId, objectName, 0,
							   NULL);
}