-
Notifications
You must be signed in to change notification settings - Fork 0
/
pgaudit.c
1488 lines (1256 loc) · 35.1 KB
/
pgaudit.c
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* pgaudit/pgaudit.c
*
* Copyright © 2014, PostgreSQL Global Development Group
*
* Permission to use, copy, modify, and distribute this software and
* its documentation for any purpose, without fee, and without a
* written agreement is hereby granted, provided that the above
* copyright notice and this paragraph and the following two
* paragraphs appear in all copies.
*
* IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT,
* INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
* LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
* DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS
* IS" BASIS, AND THE AUTHOR HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE,
* SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*/
#include "postgres.h"
#include "access/htup_details.h"
#include "access/sysattr.h"
#include "access/xact.h"
#include "catalog/catalog.h"
#include "catalog/objectaccess.h"
#include "catalog/pg_class.h"
#include "commands/dbcommands.h"
#include "catalog/pg_proc.h"
#include "commands/event_trigger.h"
#include "executor/executor.h"
#include "executor/spi.h"
#include "miscadmin.h"
#include "libpq/auth.h"
#include "nodes/nodes.h"
#include "tcop/utility.h"
#include "utils/acl.h"
#include "utils/builtins.h"
#include "utils/guc.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/rel.h"
#include "utils/syscache.h"
#include "utils/timestamp.h"
PG_MODULE_MAGIC;
void _PG_init(void);
Datum pgaudit_func_ddl_command_end(PG_FUNCTION_ARGS);
Datum pgaudit_func_sql_drop(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(pgaudit_func_ddl_command_end);
PG_FUNCTION_INFO_V1(pgaudit_func_sql_drop);
/*
* pgaudit_roles_str is the string value of the pgaudit.roles
* configuration variable, which is a list of role names.
*/
char *pgaudit_roles_str = NULL;
/*
* pgaudit_log_str is the string value of the pgaudit.log configuration
* variable, e.g. "read, write, user". Each token corresponds to a flag
* in enum LogClass below. We convert the list of tokens into a bitmap
* in pgaudit_log for internal use.
*/
char *pgaudit_log_str = NULL;
static uint64 pgaudit_log = 0;
enum LogClass {
LOG_NONE = 0,
/* SELECT */
LOG_READ = (1 << 0),
/* INSERT, UPDATE, DELETE, TRUNCATE */
LOG_WRITE = (1 << 1),
/* GRANT, REVOKE, ALTER … */
LOG_PRIVILEGE = (1 << 2),
/* CREATE/DROP/ALTER ROLE */
LOG_USER = (1 << 3),
/* DDL: CREATE/DROP/ALTER */
LOG_DEFINITION = (1 << 4),
/* DDL: CREATE OPERATOR etc. */
LOG_CONFIG = (1 << 5),
/* VACUUM, REINDEX, ANALYZE */
LOG_ADMIN = (1 << 6),
/* Function execution */
LOG_FUNCTION = (1 << 7),
/* Absolutely everything; not available via pgaudit.log */
LOG_ALL = ~(uint64)0
};
/*
* This module collects AuditEvents from various sources (event
* triggers, and executor/utility hooks) and passes them to the
* log_audit_event() function.
*
* An AuditEvent represents an operation that potentially affects a
* single object. If an underlying command affects multiple objects,
* multiple AuditEvents must be created to represent it.
*/
typedef struct {
NodeTag type;
const char *object_id;
const char *object_type;
const char *command_tag;
const char *command_text;
bool granted;
} AuditEvent;
/*
* Returns the oid of the hardcoded "audit" role.
*/
static Oid
audit_role_oid()
{
HeapTuple roleTup;
Oid oid = InvalidOid;
roleTup = SearchSysCache1(AUTHNAME, PointerGetDatum("audit"));
if (HeapTupleIsValid(roleTup)) {
oid = HeapTupleGetOid(roleTup);
ReleaseSysCache(roleTup);
}
return oid;
}
/* Returns true if either pgaudit.roles or pgaudit.log is set. */
static inline bool
pgaudit_configured()
{
return (pgaudit_roles_str && *pgaudit_roles_str) || pgaudit_log != 0;
}
/*
* Takes a role OID and returns true if the role is mentioned in
* pgaudit.roles or if it inherits from a role mentioned therein;
* returns false otherwise.
*/
static bool
role_is_audited(Oid roleid)
{
List *roles;
ListCell *lt;
if (!pgaudit_roles_str || !*pgaudit_roles_str)
return false;
if (!SplitIdentifierString(pgaudit_roles_str, ',', &roles))
return false;
foreach(lt, roles)
{
char *name = (char *)lfirst(lt);
HeapTuple roleTup;
roleTup = SearchSysCache1(AUTHNAME, PointerGetDatum(name));
if (HeapTupleIsValid(roleTup))
{
Oid parentrole = HeapTupleGetOid(roleTup);
ReleaseSysCache(roleTup);
if (is_member_of_role_nosuper(roleid, parentrole))
return true;
}
}
return false;
}
/*
* Takes a role OID and an AuditEvent and returns true or false
* depending on whether the event should be logged according to the
* pgaudit.roles/log settings. If it returns true, it also fills in the
* name of the LogClass which it is to be logged under.
*/
static bool
should_be_logged(Oid userid, AuditEvent *e, const char **classname)
{
enum LogClass class = LOG_NONE;
char *name;
/*
* Look at the type of the command and decide what LogClass needs to
* be enabled for the command to be logged.
*/
switch (e->type)
{
case T_SelectStmt:
name = "READ";
class = LOG_READ;
break;
case T_InsertStmt:
case T_UpdateStmt:
case T_DeleteStmt:
case T_TruncateStmt:
name = "WRITE";
class = LOG_WRITE;
break;
case T_GrantStmt:
case T_GrantRoleStmt:
case T_AlterDefaultPrivilegesStmt:
case T_AlterOwnerStmt:
name = "PRIVILEGE";
class = LOG_PRIVILEGE;
break;
case T_CreateRoleStmt:
case T_AlterRoleStmt:
case T_DropRoleStmt:
name = "USER";
class = LOG_USER;
break;
case T_AlterTableStmt:
case T_AlterTableCmd:
case T_AlterDomainStmt:
case T_CreateStmt:
case T_DefineStmt:
case T_DropStmt:
case T_CommentStmt:
case T_IndexStmt:
case T_LockStmt:
case T_CreateFunctionStmt:
case T_AlterFunctionStmt:
case T_DoStmt:
case T_RenameStmt:
case T_RuleStmt:
case T_ViewStmt:
case T_CreateDomainStmt:
case T_CreateTableAsStmt:
case T_CreateSeqStmt:
case T_AlterSeqStmt:
case T_CreateTrigStmt:
case T_CreateSchemaStmt:
case T_AlterObjectSchemaStmt:
case T_CreateEnumStmt:
case T_CreateRangeStmt:
case T_AlterEnumStmt:
case T_RefreshMatViewStmt:
case T_CreateForeignTableStmt:
case T_CompositeTypeStmt:
name = "DEFINITION";
class = LOG_DEFINITION;
break;
case T_CreatePLangStmt:
case T_CreateConversionStmt:
case T_CreateCastStmt:
case T_CreateOpClassStmt:
case T_CreateOpFamilyStmt:
case T_AlterOpFamilyStmt:
case T_AlterTSDictionaryStmt:
case T_AlterTSConfigurationStmt:
name = "CONFIG";
class = LOG_CONFIG;
break;
case T_ClusterStmt:
case T_CreatedbStmt:
case T_DropdbStmt:
case T_LoadStmt:
case T_VacuumStmt:
case T_ExplainStmt:
case T_VariableSetStmt:
case T_DiscardStmt:
case T_ReindexStmt:
case T_CheckPointStmt:
case T_AlterDatabaseStmt:
case T_AlterDatabaseSetStmt:
case T_AlterRoleSetStmt:
case T_CreateTableSpaceStmt:
case T_DropTableSpaceStmt:
case T_DropOwnedStmt:
case T_ReassignOwnedStmt:
case T_CreateFdwStmt:
case T_AlterFdwStmt:
case T_CreateForeignServerStmt:
case T_AlterForeignServerStmt:
case T_CreateUserMappingStmt:
case T_AlterUserMappingStmt:
case T_DropUserMappingStmt:
case T_AlterTableSpaceOptionsStmt:
case T_SecLabelStmt:
case T_CreateExtensionStmt:
case T_AlterExtensionStmt:
case T_AlterExtensionContentsStmt:
case T_CreateEventTrigStmt:
case T_AlterEventTrigStmt:
#if PG_VERSION_NUM >= 90400
case T_AlterTableMoveAllStmt:
case T_AlterSystemStmt:
#endif
name = "ADMIN";
class = LOG_ADMIN;
break;
case T_ExecuteStmt:
name = "FUNCTION";
class = LOG_FUNCTION;
break;
/*
* Anything that's left out of the list above is just noise,
* and not very interesting from an auditing perspective. So
* there's intentionally no way to enable LOG_ALL.
*/
default:
name = "UNKNOWN";
class = LOG_ALL;
break;
}
*classname = name;
/*
* We log audit events under the following conditions:
*
* 1. If the audit role has been explicitly granted permission for
* an operation.
*/
if (e->granted)
return true;
/* 2. If the current user is covered by pgaudit.roles. */
if (role_is_audited(userid))
return true;
/* 3. If the event belongs to a class covered by pgaudit.log. */
if ((pgaudit_log & class) != class)
return false;
return true;
}
/*
* Takes an AuditEvent and, if it should_be_logged(), writes it to the
* audit log. The AuditEvent is assumed to be completely filled in by
* the caller (unknown values must be set to "" so that they can be
* logged without error checking).
*/
static void
log_audit_event(AuditEvent *e)
{
Oid userid;
const char *timestamp;
const char *database;
const char *username;
const char *eusername;
const char *classname;
userid = GetSessionUserId();
if (!should_be_logged(userid, e, &classname))
return;
timestamp = timestamptz_to_str(GetCurrentTimestamp());
database = get_database_name(MyDatabaseId);
username = GetUserNameFromId(userid);
eusername = GetUserNameFromId(GetUserId());
/*
* XXX We only support logging via ereport(). In future, we may log
* to a separate file or a table.
*/
ereport(LOG,
(errmsg("AUDIT,%s,%s,%s,%s,%s,%s,%s,%s,%s",
timestamp, database,
username, eusername, classname,
e->command_tag, e->object_type, e->object_id,
e->command_text),
errhidestmt(true)));
}
/*
* Create AuditEvents for DML operations via executor permissions
* checks. We create an AuditEvent for each table in the list of
* RangeTableEntries from the query.
*/
static void
log_executor_check_perms(Oid auditOid, List *rangeTabls, bool abort_on_violation)
{
ListCell *lr;
foreach(lr, rangeTabls)
{
Oid relOid;
Relation rel;
AuditEvent e;
RangeTblEntry *rte = lfirst(lr);
char *relname;
const char *tag;
const char *reltype;
NodeTag type;
/* We only care about tables, and can ignore subqueries etc. */
if (rte->rtekind != RTE_RELATION)
continue;
/*
* Get the fully-qualified name of the relation.
*
* User queries against catalog tables (e.g. "\dt") are logged
* here. Should we filter them out, as we do for functions in
* pg_catalog?
*/
relOid = rte->relid;
rel = relation_open(relOid, NoLock);
relname = quote_qualified_identifier(get_namespace_name(RelationGetNamespace(rel)),
RelationGetRelationName(rel));
relation_close(rel, NoLock);
/*
* We don't have access to the parsetree here, so we have to
* generate the node type, object type, and command tag by
* decoding rte->requiredPerms and rte->relkind.
*/
if (rte->requiredPerms & ACL_INSERT)
{
tag = "INSERT";
type = T_InsertStmt;
}
else if (rte->requiredPerms & ACL_UPDATE)
{
tag = "UPDATE";
type = T_UpdateStmt;
}
else if (rte->requiredPerms & ACL_DELETE)
{
tag = "DELETE";
type = T_DeleteStmt;
}
else if (rte->requiredPerms & ACL_SELECT)
{
tag = "SELECT";
type = T_SelectStmt;
}
else
{
tag = "UNKNOWN";
type = T_Invalid;
}
switch (rte->relkind)
{
case RELKIND_RELATION:
reltype = "TABLE";
break;
case RELKIND_INDEX:
reltype = "INDEX";
break;
case RELKIND_SEQUENCE:
reltype = "SEQUENCE";
break;
case RELKIND_TOASTVALUE:
reltype = "TOASTVALUE";
break;
case RELKIND_VIEW:
reltype = "VIEW";
break;
case RELKIND_COMPOSITE_TYPE:
reltype = "COMPOSITE_TYPE";
break;
case RELKIND_FOREIGN_TABLE:
reltype = "FOREIGN_TABLE";
break;
case RELKIND_MATVIEW:
reltype = "MATVIEW";
break;
default:
reltype = "UNKNOWN";
break;
}
e.type = type;
e.object_id = relname;
e.object_type = reltype;
e.command_tag = tag;
if (debug_query_string)
e.command_text = debug_query_string;
else
e.command_text = "";
e.granted = false;
/*
* If a role named "audit" exists, we check if it has been
* granted permission to perform the operation identified above.
* If so, we must log the event regardless of the static pgaudit
* settings.
*/
if (auditOid != InvalidOid)
{
AclMode relPerms;
AclMode remainingPerms;
relPerms = pg_class_aclmask(relOid, auditOid,
rte->requiredPerms, ACLMASK_ALL);
remainingPerms = rte->requiredPerms & ~relPerms;
if (remainingPerms == 0)
e.granted = true;
/*
* If the audit role doesn't have the necessary permissions
* on the relation, but could have the required permissions
* through column-level grants, we check rte->selectedCols
* and rte->modifiedCols to make sure.
*/
else if ((remainingPerms & ~(ACL_SELECT | ACL_INSERT | ACL_UPDATE)) == 0)
{
AttrNumber col;
Bitmapset *tmpset;
/* This code is adapted from ExecCheckRTEPerms */
if (remainingPerms & ACL_SELECT)
{
if (bms_is_empty(rte->selectedCols))
{
if (pg_attribute_aclcheck_all(relOid, auditOid, ACL_SELECT,
ACLMASK_ANY) == ACLCHECK_OK)
e.granted = true;
}
tmpset = bms_copy(rte->selectedCols);
while ((col = bms_first_member(tmpset)) >= 0)
{
col += FirstLowInvalidHeapAttributeNumber;
if (col == InvalidAttrNumber)
{
if (pg_attribute_aclcheck_all(relOid, auditOid, ACL_SELECT,
ACLMASK_ALL) == ACLCHECK_OK)
e.granted = true;
}
else
{
if (pg_attribute_aclcheck(relOid, col, auditOid,
ACL_SELECT) == ACLCHECK_OK)
e.granted = true;
}
}
bms_free(tmpset);
}
remainingPerms &= ~ACL_SELECT;
if (remainingPerms != 0)
{
if (bms_is_empty(rte->modifiedCols))
{
if (pg_attribute_aclcheck_all(relOid, auditOid,
remainingPerms,
ACLMASK_ANY) != ACLCHECK_OK)
e.granted = true;
}
tmpset = bms_copy(rte->modifiedCols);
while ((col = bms_first_member(tmpset)) >= 0)
{
col += FirstLowInvalidHeapAttributeNumber;
if (col != InvalidAttrNumber)
{
if (pg_attribute_aclcheck(relOid, col, auditOid,
remainingPerms) == ACLCHECK_OK)
e.granted = true;
}
}
bms_free(tmpset);
}
}
}
log_audit_event(&e);
pfree(relname);
}
}
/*
* Create AuditEvents for utility commands that are not supported by
* event triggers, particularly those which affect global objects.
*
* Exactly what commands are supported by event triggers depends on the
* version of Postgres in use. In versions 9.3 and 9.4, we can use only
* the sql_drop event trigger, because our ddl_command_end trigger needs
* pg_event_trigger_{get_creation_commands,expand_command}. Therefore we
* must handle all DDL commands other than DROP here.
*
* In 9.5 (as represented by the latest deparse branch), we can use the
* ddl_command_end trigger, which handles CREATE/ALTER for a variety of
* objects. Therefore we can skip those cases.
*/
static void
log_utility_command(Node *parsetree,
const char *queryString,
ProcessUtilityContext context,
ParamListInfo params,
DestReceiver *dest,
char *completionTag)
{
AuditEvent e;
bool supported_stmt = true;
/*
* If the statement (and, for some statements, the object type) is
* supported by event triggers, then we don't need to log anything.
* Otherwise, we log the query string.
*
* The following logic is copied from standard_ProcessUtility in
* tcop/utility.c, and will need to be changed if event trigger
* support is expanded to other commands (if not, the command
* will be logged twice).
*/
switch (nodeTag(parsetree))
{
/*
* The following statements are never supported by event
* triggers.
*/
case T_DoStmt:
case T_CreateTableSpaceStmt:
case T_DropTableSpaceStmt:
case T_AlterTableSpaceOptionsStmt:
case T_TruncateStmt:
case T_CommentStmt:
case T_SecLabelStmt:
case T_GrantStmt:
case T_GrantRoleStmt:
case T_CreatedbStmt:
case T_AlterDatabaseStmt:
case T_AlterDatabaseSetStmt:
case T_DropdbStmt:
case T_LoadStmt:
case T_ClusterStmt:
case T_VacuumStmt:
case T_ExplainStmt:
case T_VariableSetStmt:
case T_DiscardStmt:
case T_CreateEventTrigStmt:
case T_AlterEventTrigStmt:
case T_CreateRoleStmt:
case T_AlterRoleStmt:
case T_AlterRoleSetStmt:
case T_DropRoleStmt:
case T_ReassignOwnedStmt:
case T_LockStmt:
case T_CheckPointStmt:
case T_ReindexStmt:
#if PG_VERSION_NUM >= 90400
case T_AlterTableMoveAllStmt:
case T_AlterSystemStmt:
#endif
/*
* The following statements are supported only by the
* ddl_command_end event trigger. (This list is from
* ProcessUtilitySlow.)
*/
#ifndef USE_DEPARSE_FUNCTIONS
case T_CreateSchemaStmt:
case T_AlterDomainStmt:
case T_DefineStmt:
case T_CreateExtensionStmt:
case T_AlterExtensionStmt:
case T_AlterExtensionContentsStmt:
case T_CreateFdwStmt:
case T_AlterFdwStmt:
case T_CreateForeignServerStmt:
case T_AlterForeignServerStmt:
case T_CreateUserMappingStmt:
case T_AlterUserMappingStmt:
case T_DropUserMappingStmt:
case T_CreateEnumStmt:
case T_CreateRangeStmt:
case T_AlterEnumStmt:
case T_CreateFunctionStmt:
case T_AlterFunctionStmt:
case T_RuleStmt:
case T_CreateTrigStmt:
case T_CreatePLangStmt:
case T_CreateDomainStmt:
case T_CreateConversionStmt:
case T_CreateCastStmt:
case T_CreateOpClassStmt:
case T_CreateOpFamilyStmt:
case T_AlterOpFamilyStmt:
case T_AlterTSDictionaryStmt:
case T_AlterTSConfigurationStmt:
case T_RenameStmt:
case T_AlterOwnerStmt:
case T_DropOwnedStmt:
case T_AlterDefaultPrivilegesStmt:
#endif
supported_stmt = false;
break;
#ifndef USE_DEPARSE_FUNCTIONS
/*
* Exclude any ALTER <object> SET SCHEMA statements which
* will be handled by log_object_access() in 9.3 and 9.4
*/
case T_AlterObjectSchemaStmt:
{
AlterObjectSchemaStmt *stmt = (AlterObjectSchemaStmt *) parsetree;
switch(stmt->objectType)
{
case OBJECT_FOREIGN_TABLE:
case OBJECT_INDEX:
case OBJECT_MATVIEW:
case OBJECT_SEQUENCE:
case OBJECT_TABLE:
case OBJECT_TYPE:
case OBJECT_VIEW:
break;
default:
supported_stmt = false;
}
}
break;
#endif
/*
* The following statements are supported by event triggers for
* certain object types. We can always use DROP support, but the
* others are dependent on the ddl_command_end trigger.
*/
case T_DropStmt:
{
DropStmt *stmt = (DropStmt *) parsetree;
if (!EventTriggerSupportsObjectType(stmt->removeType))
supported_stmt = false;
}
break;
#ifdef USE_DEPARSE_FUNCTIONS
case T_RenameStmt:
{
RenameStmt *stmt = (RenameStmt *) parsetree;
if (!EventTriggerSupportsObjectType(stmt->renameType))
supported_stmt = false;
}
break;
case T_AlterObjectSchemaStmt:
{
AlterObjectSchemaStmt *stmt = (AlterObjectSchemaStmt *) parsetree;
if (!EventTriggerSupportsObjectType(stmt->objectType))
supported_stmt = false;
}
break;
case T_AlterOwnerStmt:
{
AlterOwnerStmt *stmt = (AlterOwnerStmt *) parsetree;
if (!EventTriggerSupportsObjectType(stmt->objectType))
supported_stmt = false;
}
break;
#endif
/*
* All other statement types have event trigger support, or we
* don't care about them at all.
*/
default:
break;
}
if (supported_stmt)
return;
e.type = nodeTag(parsetree);
e.object_id = "";
e.object_type = "";
e.command_tag = CreateCommandTag(parsetree);
e.command_text = queryString;
e.granted = false;
log_audit_event(&e);
}
/*
* Create AuditEvents for certain kinds of CREATE and ALTER statements,
* as detected by log_object_access() in lieu of event trigger support
* for them.
*/
#ifndef USE_DEPARSE_FUNCTIONS
static void
log_create_or_alter(bool create,
Oid classId,
Oid objectId,
int subId)
{
AuditEvent e;
NodeTag type;
const char *tag;
const char *name;
const char *objtype;
switch (classId)
{
case RelationRelationId:
{
Relation rel;
Form_pg_class class;
char *relnsp;
char *relname;
rel = relation_open(objectId, NoLock);
class = RelationGetForm(rel);
relnsp = get_namespace_name(RelationGetNamespace(rel));
relname = RelationGetRelationName(rel);
name = quote_qualified_identifier(relnsp, relname);
switch (class->relkind)
{
case RELKIND_RELATION:
objtype = "TABLE";
type = create ? T_CreateStmt : T_AlterTableStmt;
tag = create ? "CREATE TABLE" : "ALTER TABLE";
break;
case RELKIND_INDEX:
objtype = "INDEX";
type = T_IndexStmt;
tag = create ? "CREATE INDEX" : "ALTER INDEX";
break;
case RELKIND_SEQUENCE:
objtype = "SEQUENCE";
type = create ? T_CreateStmt : T_AlterSeqStmt;
tag = create ? "CREATE SEQUENCE" : "ALTER SEQUENCE";
break;
case RELKIND_VIEW:
objtype = "VIEW";
/* T_ViewStmt covers both CREATE and ALTER */
type = T_ViewStmt;
tag = create ? "CREATE VIEW" : "ALTER VIEW";
break;
case RELKIND_COMPOSITE_TYPE:
objtype = "TYPE";
/* T_CompositeTypeStmt covers both CREATE and ALTER */
type = T_CompositeTypeStmt;
tag = create ? "CREATE TYPE" : "ALTER TYPE";
break;
case RELKIND_FOREIGN_TABLE:
objtype = "FOREIGN TABLE";
/* There is no T_AlterForeignTableStmt */
type = T_CreateForeignTableStmt;
tag = create ? "CREATE FOREIGN TABLE" : "ALTER FOREIGN TABLE";
break;
case RELKIND_MATVIEW:
objtype = "MATERIALIZED VIEW";
/* Pretend that materialized views are a kind of table */
type = create ? T_CreateStmt : T_AlterTableStmt;
tag = create ? "CREATE MATERIALIZED VIEW" : "ALTER MATERIALIZED VIEW";
break;
/*
* XXX Are there any other RELKIND_xxx cases that we
* need to handle here?
*/
default:
objtype = "UNKNOWN";
type = T_Invalid;
tag = "";
break;
}
relation_close(rel, NoLock);
}
break;
/*
* We leave it to the ProcessUtility_hook to handle all other
* commands. There's not much we can do to improve on "create
* database foo", for example.
*/
default:
return;
break;
}
e.type = type;
e.object_id = name;
e.object_type = objtype;
e.command_tag = tag;
if (debug_query_string)
e.command_text = debug_query_string;
else
e.command_text = "";
e.granted = false;
log_audit_event(&e);
}
#endif
/*
* Create AuditEvents for non-catalog function execution, as detected by
* log_object_access() below.
*/
static void
log_function_execution(Oid objectId)
{
HeapTuple proctup;
Form_pg_proc proc;
const char *name;
AuditEvent e;
proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(objectId));
if (!proctup)
elog(ERROR, "cache lookup failed for function %u", objectId);
proc = (Form_pg_proc) GETSTRUCT(proctup);
/*
* Logging execution of all pg_catalog functions would
* make the log unusably noisy.
*/
if (IsSystemNamespace(proc->pronamespace))
{
ReleaseSysCache(proctup);
return;
}
name = quote_qualified_identifier(get_namespace_name(proc->pronamespace),
NameStr(proc->proname));
ReleaseSysCache(proctup);
e.type = T_ExecuteStmt;
e.object_id = name;
e.object_type = "FUNCTION";
e.command_tag = "EXECUTE";
if (debug_query_string)
e.command_text = debug_query_string;
else
e.command_text = "";
e.granted = false;