What's new? | Help | Directory | Sign in
Google
sepgsql
Security Enhanced PostgreSQL
  
  
  
  
    
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
/*
* include/security/pgace.h
* headers for PostgreSQL Access Control Extensions (PGACE)
* Copyright 2007 KaiGai Kohei <kaigai@kaigai.gr.jp>
*/
#ifndef PGACE_H
#define PGACE_H

#include "access/htup.h"
#include "commands/trigger.h"
#include "lib/stringinfo.h"
#include "nodes/execnodes.h"
#include "nodes/parsenodes.h"
#include "storage/itemptr.h"
#include "storage/large_object.h"
#include "tcop/dest.h"
#include "utils/rel.h"

/*
* SECURITY_SYSATTR_NAME is the name of system column name
* for security attribute, defined in pg_config.h
* If it is not defined, security attribute support is disabled
*
* see, src/include/pg_config.h
*/

#ifdef HAVE_SELINUX
#include "security/sepgsql.h"
// the following line will be fixed by Sun's people
// #elifdef HAVE_SOLARISTX
// #include "security/solaristx.h"
#else

/******************************************************************
* Initialize / Finalize related hooks
******************************************************************/

/*
* pgaceShmemSize() have to return the size of shared memory segment
* required by PGACE implementation. If no shared memory segment needed,
* it should return 0.
*/
static inline Size pgaceShmemSize(void) {
return 0;
}

/*
* pgaceInitialize() is called when a new PostgreSQL instance is generated.
* A PGACE implementation can initialize itself.
*/
static inline void pgaceInitialize(void) {
/* do nothing */
}

/*
* pgaceInitializePostmaster() is called when a postmaster server process
* is started up. If it returns false, the server starting up process
* will be aborted.
*/
static inline bool pgaceInitializePostmaster(void) {
return true;
}

/*
* pgaceFinalizePostmaster() is called when a postmaster server process
* is just ending up.
*/
static inline void pgaceFinalizePostmaster(void) {
/* do nothing */
}

/******************************************************************
* SQL proxy hooks
******************************************************************/

/*
* pgaceProxyQuery() is called just after query rewrite phase.
* PGACE implementation can modify the query trees in this hook,
* if necessary.
*
* @queryList : a list of Query typed objects.
*/
static inline List *pgaceProxyQuery(List *queryList) {
return queryList;
}

/*
* pgacePortalStart() is called on the top of PortalStart().
*
* @portal : a Portal object currently executed.
*/
static inline void pgacePortalStart(Portal portal) {
/* do nothing */
}

/*
* pgaceExecutorStart() is called on the top of ExecutorStart().
*
* @queryDesc : a QueryDesc object given to ExecutorStart().
* @eflags : eflags valus given to ExecutorStart().
* if EXEC_FLAG_EXPLAIN_ONLY is set, no real access will run.
*/
static inline void pgaceExecutorStart(QueryDesc *queryDesc, int eflags) {
/* do nothing */
}

/******************************************************************
* HeapTuple modification hooks
******************************************************************/

/*
* pgaceExecInsert() is called when a client tries to insert a new tuple
* via explicit INSERT statement from ExecInsert() at execMain.c
* If it returns false, insertion of the tuple will be cancelled.
*
* @rel : the target relation of INSERT
* @tuple : the contains of the inserted tuple
* @with_returning : true, if the query has RETURNING clause
*/
static inline bool pgaceExecInsert(Relation rel, HeapTuple tuple, bool with_returning) {
return true;
}

/*
* pgaceExecUpdate() is called when clients tries to update a tuple
* via explicit UPDATE statement from ExecUpdate() at execMain.c
* If it returns false, updating the tuple will be cancelled.
*
* @rel : the target relation of UPDATE
* @newtup : the new contains of the updated tuple
* @tid : ItemPointer of the tuple updated
* @with_returning : true, if the query has RETURNING clause
*/
static inline bool pgaceExecUpdate(Relation rel, HeapTuple newtup, ItemPointer tid, bool with_returning) {
return true;
}

/*
* pgaceExecUpdate() is called when clients tries to delete a tuple
* via explicit DELETE statement from ExecDelete() at execMain.c
* If it returns false, deletion of the tuple will be cancelled.
*
* @rel : the target relation of DELETE
* @tid : ItemPointer of the tuple deleted
* @with_returning : true, if the query has RETURNING clause
*/
static inline bool pgaceExecDelete(Relation rel, ItemPointer tid, bool with_returning) {
return true;
}

/*
* pgaceSimpleHeapInsert() is called just before simple_heap_insert() is processed
*
* @rel : the target relation of simple_heap_insert()
* @tuple : the contains of the inserted tuple
*/
static inline void pgaceSimpleHeapInsert(Relation rel, HeapTuple tuple) {
/* do nothing */
}

/*
* pgaceSimpleHeapUpdate() is called just before simple_heap_update() is processed
*
* @rel : the target relation of simple_heap_update()
* @tid : ItemPointer of the tuple updated
* @tuple : the new contains of the updated tuple
*/
static inline void pgaceSimpleHeapUpdate(Relation rel, ItemPointer tid, HeapTuple tuple) {
/* do nothing */
}

/*
* pgaceSimpleHeapDelete() is called just before simple_heap_delete() is processed
*
* @rel : the target relation of simple_heap_delete()
* @tid : ItemPointer of the tuple deleted
*/
static inline void pgaceSimpleHeapDelete(Relation rel, ItemPointer tid) {
/* do nothing */
}

/*
* pgaceHeapInsert() is called from heap_insert()
*
* @rel : the target relation of heap_insert()
* @tuple : the contains of the inserted tuples. It also contains system attribute like Oid
*/
static inline void pgaceHeapInsert(Relation rel, HeapTuple tuple) {
/* do nothing */
}

/*
* pgaceHeapUpdate() is called from heap_update()
*
* @rel : the target relation of heap_update()
* @newtup : the contains of the updated tuples. It also contains system attribute like Oid
* @oldtup : the tuple which will be updated
*/
static inline void pgaceHeapUpdate(Relation rel, HeapTuple newtup, HeapTuple oldtup) {
/* do nothing */
}

/*
* pgaceHeapDelete() is called from heap_delete()
*
* @rel : the target relation of heap_delete()
* @oldtup : the tuple which will be deleted
*/
static inline void pgaceHeapDelete(Relation rel, HeapTuple oldtup) {
/* do nothing */
}

/******************************************************************
* Extended SQL statement hooks
******************************************************************/
/*
* PGACE implementation can use pgaceGramSecurityLabel() hook to extend
* SQL statement for explicit labeling. This hook is deployed on parser/gram.y
* as a part of the SQL grammer. If no SQL extension is necessary, it has to
* return NULL to cause yyerror().
*
* @defname : given <parameter> string
* @value : given <value> string
*/
static inline DefElem *pgaceGramSecurityLabel(char *defname, char *value) {
return NULL;
}

/*
* PGACE implementation has to return true, if the given DefElem holds
* security label generated in pgaceGramSecurityLabel(). false, if any other.
*
* @defel : given DefElem object
*/
static inline bool pgaceNodeIsSecurityLabel(DefElem *defel) {
return false;
}

/*
* PGACE implementation has to translate DefElem object generated by
* pgaceGramSecurityLabel(), into t_security of HeapTupleHeader.
*
* @defel : given DefElem object
*/
static inline Oid pgaceParseSecurityLabel(DefElem *defel) {
return InvalidOid;
}

/******************************************************************
* DATABASE related hooks
******************************************************************/

/*
* pgaceSetDatabaseParam() is called when clients tries to set GUC variables
*
* @name : The name of GUC variable
* @argstr : The new valus of GUC variable. If argstr is NULL, it means
* clients tries to reset the variable.
*/
static inline void pgaceSetDatabaseParam(const char *name, char *argstring) {
/* do nothing */
}

/*
* pgaceGetDatabaseParam() is called when clients tries to refer GUC variables
*
* @name : The name of GUC variable
*/
static inline void pgaceGetDatabaseParam(const char *name) {
/* do nothing */
}

/******************************************************************
* FUNCTION related hooks
******************************************************************/

/*
* pgaceCallFunction() is called just before executing SQL function
* as a part of query.
*
* @finfo : FmgrInfo object for the target function
*/
static inline void pgaceCallFunction(FmgrInfo *finfo) {
/* do nothing */
}

/*
* pgaceCallFunctionTrigger() is called just before executing
* trigger function.
* If it returns false, the trigger function will not be called and caller
* receives NULL tuple as a result. In the case when Before-Row triggers,
* it means the current operations on the tuple should be skipped.
*
* @finfo : FmgrInfo object for the target function
* @tgdata : TriggerData object for the current trigger invokation
*/
static inline bool pgaceCallFunctionTrigger(FmgrInfo *finfo, TriggerData *tgdata) {
return true;
}

/*
* pgaceCallFunctionFastPath() is called just before executing
* SQL function in the fast path.
*
* @finfo : FmgrInfo object for the target function
*/
static inline void pgaceCallFunctionFastPath(FmgrInfo *finfo) {
/* do nothing */
}

/*
* pgacePreparePlanCheck() is called before foreign key/primary key constraint checks,
* at ri_PlanCheck(). PGACE implementation can return its opaque data for any purpose.
*
* @rel : the target relation in which a constraint is configured
*/
static inline Datum pgacePreparePlanCheck(Relation rel) {
return (Datum) 0;
}

/*
* pgaceRestorePlanCheck() is called after foreign key/primary key constraint checks,
* at ri_PlanCheck(). PGACE implementation can use an opaque data generated in the above
* pgacePreparePlanCheck().
*
* @rel : the target relation in which a constraint is configured
* @pgace_saved : an opaque data returned from pgacePreparePlanCheck()
*/
static inline void pgaceRestorePlanCheck(Relation rel, Datum pgace_saved) {
/* do nothing */
}

/******************************************************************
* TABLE related hooks
******************************************************************/

/*
* pgaceLockTable() is called when explicit LOCK statement used.
*
* @relid : the target relation id
*/
static inline void pgaceLockTable(Oid relid) {
/* do nothing */
}

/*
* pgaceAlterTable() is called to modify table/column. The PGACE implementation
* have to update the target tuples within pg_class or pg_attribute.
* If AlterTableCmd tag is unexpected one,
*
* @rel : the target relation
* @cmd : AlterTableCmd object
*/
static inline bool pgaceAlterTable(Relation rel, AlterTableCmd *cmd) {
return false;
}

/******************************************************************
* COPY TO/COPY FROM statement hooks
******************************************************************/

/*
* pgaceCopyTable() is called when COPY TO/COPY FROM statement is processed
*
* @rel : the target relation
* @attNumList : the list of attribute numbers
* @isFrom : true, if the given statement is 'COPY FROM'
*/
static inline void pgaceCopyTable(Relation rel, List *attNumList, bool isFrom) {
/* do nothing */
}

/*
* pgaceCopyToTuple() is called to check whether the given tuple should be
* filtered, or not in the process of COPY TO statement.
* If it returns false, the given tuple will be filtered from the result set
*
* @rel : the target relation
* @tuple : the target tuple
*/
static inline bool pgaceCopyToTuple(Relation rel, HeapTuple tuple) {
return true;
}

/*
* pgaceCopyFromTuple() is called to check whether the given tuple should be
* filtered, or not in the process of COPY FROM statement.
* If it returns false, the given tuple will be filtered from the result set
*
* @rel : the target relation
* @tuple : the target tuple
*/
static inline bool pgaceCopyFromTuple(Relation rel, HeapTuple tuple) {
return true;
}

/******************************************************************
* Loadable shared library module hooks
******************************************************************/

/*
* pgaceLoadSharedModule() is called just before load a shared library
* module.
*
* @filename : full path name of the shared library module
*/
static inline void pgaceLoadSharedModule(const char *filename) {
/* do nothing */
}

/******************************************************************
* Binary Large Object (BLOB) hooks
******************************************************************/

/*
* pgaceLargeObjectGetSecurity() is called when lo_get_security() is executed
* It returns it's security attribute.
*
* @tuple : a tuple which is a part of the target largeobject.
*/
static inline Oid pgaceLargeObjectGetSecurity(HeapTuple tuple) {
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("There is no security attribute support.")));
return InvalidOid;
}

/*
* pgaceLargeObjectSetSecurity() is called when lo_set_security() is executed
*
* @tuple : a tuple which is a part of the target largeobject.
* @lo_security : new security attribute specified
* @is_first : true, if it's the first call in the largeobject.
* Because a largeobject may contain some tuples, this hook
* may be called several times for a single largeobject.
*/
static inline void pgaceLargeObjectSetSecurity(HeapTuple tuple, Oid lo_security, bool is_first) {
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("There is no security attribute support.")));
}

/*
* pgaceLargeObjectCreate() is called when a new large object is created
*
* @rel : pg_largeobject relation opened with RowExclusiveLock
* @tuple : a new tuple for the new large object
*/
static inline void pgaceLargeObjectCreate(Relation rel, HeapTuple tuple) {
/* do nothing */
}

/*
* pgaceLargeObjectDrop() is called when a large object is dropped once for
* a large object
*
* @rel : pg_largeobject relation opened with RowExclusiveLock
* @tuple : one of the tuples within the target large object
*/
static inline void pgaceLargeObjectDrop(Relation rel, HeapTuple tuple) {
/* do nothing */
}

/*
* pgaceLargeObjectOpen() is called when a large object is opened
*
* @rel : pg_largeobject relation opened with RowExclusiveLock
* @tuple : head of the tuples within the target large object
* @read_only : true, if large object is opened as read only mode
*/
static inline void pgaceLargeObjectOpen(Relation rel, HeapTuple tuple, bool read_only) {
/* do nothing */
}

/*
* pgaceLargeObjectRead is called when they read from a large object
*
* @rel : pg_largeobject relation opened with AccessShareLock
* @tuple : a tuple within the target large object
*/
static inline void pgaceLargeObjectRead(Relation rel, HeapTuple tuple) {
/* do nothing */
}

/*
* pgaceLargeObjectWrite() is called when they write to a large object
*
* @rel : pg_largeobject relation opened with RowExclusiveLock
* @newtup : a new tuple within the target large object
* @oldtup : a original tuple within the target large object, if exist
*/
static inline void pgaceLargeObjectWrite(Relation rel, HeapTuple newtup, HeapTuple oldtup) {
/* do nothing */
}

/*
* pgaceLargeObjectImport() is called when lo_import() is processed
*/
static inline void pgaceLargeObjectImport(void) {
/* do nothing */
}

/*
* pgaceLargeObjectExport() is called when lo_import() is processed
*/
static inline void pgaceLargeObjectExport(void) {
/* do nothing */
}

/******************************************************************
* Security Label hooks
******************************************************************/

/*
* PGACE implementation can use pgaceSecurityLabelIn() hook to translate
* a input security label from external representation into internal one.
* If no translation is necessary, it has to return @seclabel as is.
*
* @seclabel : security label being input
*/
static inline char *pgaceSecurityLabelIn(char *seclabel) {
return seclabel;
}

/*
* PGACE implementation can use pgaceSecurityLabelOut() hook to translate
* a security label in internal representation into external one.
* If no translation is necessary, it has to return @seclabel as is.
*
* @seclabel : security label being output
*/
static inline char *pgaceSecurityLabelOut(char *seclabel) {
return seclabel;
}

/*
* pgaceSecurityLabelIsValid() checks whether the @seclabel is valid or not.
* return false, if @seclabel is not valid security attribute in text representation.
*
* @seclabel : security attribute in text representation
*/
static inline bool pgaceSecurityLabelIsValid(char *seclabel) {
return true;
}

/*
* pgaceSecurityLabelOfLabel() returns the security attribute of a newly
* generated tuple within pg_security
*
* @new_label : a text representation of security context which will be newly
* inserted into pg_security.
*/
static inline char *pgaceSecurityLabelOfLabel(char *new_label) {
return pstrdup("unlabeled");
}

/*
* pgaceSecurityLabelNotFound() has to return a string representation of security
* attribute, when no tuple with oid equals to @sid is within pg_security system
* catalog.
*
* @sid : required sid, but not found on pg_security
*/
static inline char *pgaceSecurityLabelNotFound(Oid sid) {
return pstrdup("unlabeled");
}

/******************************************************************
* Extended node type hooks
******************************************************************/

/*
* If PGACE implementation requires new node type, a method to copy object.
* pgaceCopyObject() provides a hook to copy new node typed object.
* If a given object (@orig) has a tag extended by PGACE implementation,
* it have to copy and return it.
* If it returns NULL, @orig is not available for the PGACE implementation.
*
* @orig : a object which to copy
*/
static inline Node *pgaceCopyObject(Node *orig) {
return NULL;
}

/*
* pgaceOutObject() provides a hook to translate a object to text representation.
* If a given object (@node) has a tag extended by PGACE implementation, it have
* to put a text representation into StringInfo.
* If it returns false, @node is not available for the PGACE implementation.
*
* @str : StringInfo which to put the text representation
* @node : a object that text representation is required
*/
static inline bool pgaceOutObject(StringInfo str, Node *node) {
return false;
}

#endif

/* writable system column support */
#ifdef SECURITY_SYSATTR_NAME
static inline bool pgaceWritableSystemColumn(int attrno) {
return ((attrno == SecurityAttributeNumber) ? true : false);
}
extern void pgaceTransformSelectStmt(List *targetList);
extern void pgaceTransformInsertStmt(List **p_icolumns, List **p_attrnos, List *targetList);
extern void pgaceFetchSecurityLabel(JunkFilter *junkfilter, TupleTableSlot *slot, Oid *tts_security);
#else
static inline bool pgaceWritableSystemColumn(int attrno) {
return false;
}
static inline void pgaceTransformSelectStmt(List *targetList) { /* do nothing */ }
static inline void pgaceTransformInsertStmt(List **p_icolumns,
List **p_attrnos,
List *targetList) { /* do nothing */ }
static inline void pgaceFetchSecurityLabel(JunkFilter *junkfilter,
TupleTableSlot *slot,
Oid *tts_security) { /* do nothing */ }
#endif

/* Extended SQL statements related */
extern List *pgaceBuildAttrListForRelation(CreateStmt *stmt);
extern void pgaceCreateRelationCommon(Relation rel, HeapTuple tuple, List *pgace_attr_list);
extern void pgaceCreateAttributeCommon(Relation rel, HeapTuple tuple, List *pgace_attr_list);
extern void pgaceAlterRelationCommon(Relation rel, AlterTableCmd *cmd);
extern void pgaceCreateDatabaseCommon(HeapTuple tuple, DefElem *defel);
extern void pgaceAlterDatabaseCommon(HeapTuple tuple, DefElem *defel);
extern void pgaceCreateFunctionCommon(HeapTuple tuple, DefElem *defel);
extern void pgaceAlterFunctionCommon(HeapTuple tuple, DefElem *defel);

/* SQL functions related to security label */
extern Datum security_label_in(PG_FUNCTION_ARGS);
extern Datum security_label_out(PG_FUNCTION_ARGS);
extern Datum security_label_raw_in(PG_FUNCTION_ARGS);
extern Datum security_label_raw_out(PG_FUNCTION_ARGS);
extern Datum text_to_security_label(PG_FUNCTION_ARGS);
extern Datum security_label_to_text(PG_FUNCTION_ARGS);

/* SQL functions related to large object */
extern Datum lo_get_security(PG_FUNCTION_ARGS);
extern Datum lo_set_security(PG_FUNCTION_ARGS);

#endif // PGACE_H
Show details Hide details

Change log

r483 by KaiGai.Kohei on Oct 24, 2007   Diff
import 8.2.x based sepostgresql
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 20702 bytes, 643 lines