2011-01-24 09:44:48 +08:00
|
|
|
/* -------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* contrib/sepgsql/schema.c
|
|
|
|
*
|
|
|
|
* Routines corresponding to schema objects
|
|
|
|
*
|
2012-01-02 07:01:58 +08:00
|
|
|
* Copyright (c) 2010-2012, PostgreSQL Global Development Group
|
2011-01-24 09:44:48 +08:00
|
|
|
*
|
|
|
|
* -------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
2011-12-21 22:12:43 +08:00
|
|
|
#include "access/genam.h"
|
|
|
|
#include "access/heapam.h"
|
2012-09-06 02:01:15 +08:00
|
|
|
#include "access/htup_details.h"
|
2011-12-21 22:12:43 +08:00
|
|
|
#include "access/sysattr.h"
|
2011-02-03 12:39:43 +08:00
|
|
|
#include "catalog/dependency.h"
|
2011-12-21 22:12:43 +08:00
|
|
|
#include "catalog/indexing.h"
|
2011-09-24 05:09:34 +08:00
|
|
|
#include "catalog/pg_database.h"
|
2011-01-24 09:44:48 +08:00
|
|
|
#include "catalog/pg_namespace.h"
|
|
|
|
#include "commands/seclabel.h"
|
2011-09-24 05:09:34 +08:00
|
|
|
#include "miscadmin.h"
|
2011-12-21 22:12:43 +08:00
|
|
|
#include "utils/fmgroids.h"
|
2011-01-24 09:44:48 +08:00
|
|
|
#include "utils/lsyscache.h"
|
2011-12-21 22:12:43 +08:00
|
|
|
#include "utils/tqual.h"
|
2011-01-24 09:44:48 +08:00
|
|
|
|
|
|
|
#include "sepgsql.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sepgsql_schema_post_create
|
|
|
|
*
|
|
|
|
* This routine assigns a default security label on a newly defined
|
|
|
|
* schema.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
sepgsql_schema_post_create(Oid namespaceId)
|
|
|
|
{
|
2011-12-21 22:12:43 +08:00
|
|
|
Relation rel;
|
2012-06-11 03:20:04 +08:00
|
|
|
ScanKeyData skey;
|
|
|
|
SysScanDesc sscan;
|
2011-12-21 22:12:43 +08:00
|
|
|
HeapTuple tuple;
|
2011-04-10 23:42:00 +08:00
|
|
|
char *tcontext;
|
|
|
|
char *ncontext;
|
2011-12-21 22:12:43 +08:00
|
|
|
char audit_name[NAMEDATALEN + 20];
|
2012-06-11 03:20:04 +08:00
|
|
|
ObjectAddress object;
|
|
|
|
Form_pg_namespace nspForm;
|
2011-01-24 09:44:48 +08:00
|
|
|
|
|
|
|
/*
|
2011-04-10 23:42:00 +08:00
|
|
|
* Compute a default security label when we create a new schema object
|
|
|
|
* under the working database.
|
2011-12-21 22:12:43 +08:00
|
|
|
*
|
2012-06-11 03:20:04 +08:00
|
|
|
* XXX - uncoming version of libselinux supports to take object name to
|
|
|
|
* handle special treatment on default security label; such as special
|
|
|
|
* label on "pg_temp" schema.
|
2011-01-24 09:44:48 +08:00
|
|
|
*/
|
2011-12-21 22:12:43 +08:00
|
|
|
rel = heap_open(NamespaceRelationId, AccessShareLock);
|
|
|
|
|
|
|
|
ScanKeyInit(&skey,
|
|
|
|
ObjectIdAttributeNumber,
|
|
|
|
BTEqualStrategyNumber, F_OIDEQ,
|
|
|
|
ObjectIdGetDatum(namespaceId));
|
|
|
|
|
|
|
|
sscan = systable_beginscan(rel, NamespaceOidIndexId, true,
|
|
|
|
SnapshotSelf, 1, &skey);
|
|
|
|
tuple = systable_getnext(sscan);
|
|
|
|
if (!HeapTupleIsValid(tuple))
|
|
|
|
elog(ERROR, "catalog lookup failed for namespace %u", namespaceId);
|
|
|
|
|
|
|
|
nspForm = (Form_pg_namespace) GETSTRUCT(tuple);
|
|
|
|
|
2011-09-24 05:09:34 +08:00
|
|
|
tcontext = sepgsql_get_label(DatabaseRelationId, MyDatabaseId, 0);
|
2011-12-21 22:12:43 +08:00
|
|
|
ncontext = sepgsql_compute_create(sepgsql_get_client_label(),
|
|
|
|
tcontext,
|
2011-01-24 09:44:48 +08:00
|
|
|
SEPG_CLASS_DB_SCHEMA);
|
2012-06-11 03:20:04 +08:00
|
|
|
|
2011-12-21 22:12:43 +08:00
|
|
|
/*
|
|
|
|
* check db_schema:{create}
|
|
|
|
*/
|
|
|
|
snprintf(audit_name, sizeof(audit_name),
|
|
|
|
"schema %s", NameStr(nspForm->nspname));
|
|
|
|
sepgsql_avc_check_perms_label(ncontext,
|
|
|
|
SEPG_CLASS_DB_SCHEMA,
|
|
|
|
SEPG_DB_SCHEMA__CREATE,
|
|
|
|
audit_name,
|
|
|
|
true);
|
|
|
|
systable_endscan(sscan);
|
|
|
|
heap_close(rel, AccessShareLock);
|
2011-01-24 09:44:48 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Assign the default security label on a new procedure
|
|
|
|
*/
|
|
|
|
object.classId = NamespaceRelationId;
|
|
|
|
object.objectId = namespaceId;
|
|
|
|
object.objectSubId = 0;
|
|
|
|
SetSecurityLabel(&object, SEPGSQL_LABEL_TAG, ncontext);
|
|
|
|
|
|
|
|
pfree(ncontext);
|
2011-09-24 05:09:34 +08:00
|
|
|
pfree(tcontext);
|
2011-01-24 09:44:48 +08:00
|
|
|
}
|
|
|
|
|
2012-03-10 04:18:45 +08:00
|
|
|
/*
|
|
|
|
* sepgsql_schema_drop
|
|
|
|
*
|
|
|
|
* It checks privileges to drop the supplied schema object.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
sepgsql_schema_drop(Oid namespaceId)
|
|
|
|
{
|
2012-06-11 03:20:04 +08:00
|
|
|
ObjectAddress object;
|
|
|
|
char *audit_name;
|
2012-03-10 04:18:45 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* check db_schema:{drop} permission
|
|
|
|
*/
|
|
|
|
object.classId = NamespaceRelationId;
|
|
|
|
object.objectId = namespaceId;
|
|
|
|
object.objectSubId = 0;
|
|
|
|
audit_name = getObjectDescription(&object);
|
|
|
|
|
|
|
|
sepgsql_avc_check_perms(&object,
|
2012-06-11 03:20:04 +08:00
|
|
|
SEPG_CLASS_DB_SCHEMA,
|
2012-03-10 04:18:45 +08:00
|
|
|
SEPG_DB_SCHEMA__DROP,
|
|
|
|
audit_name,
|
|
|
|
true);
|
|
|
|
pfree(audit_name);
|
|
|
|
}
|
|
|
|
|
2011-01-24 09:44:48 +08:00
|
|
|
/*
|
|
|
|
* sepgsql_schema_relabel
|
|
|
|
*
|
|
|
|
* It checks privileges to relabel the supplied schema
|
|
|
|
* by the `seclabel'.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
sepgsql_schema_relabel(Oid namespaceId, const char *seclabel)
|
|
|
|
{
|
2012-06-11 03:20:04 +08:00
|
|
|
ObjectAddress object;
|
|
|
|
char *audit_name;
|
2011-01-24 09:44:48 +08:00
|
|
|
|
2011-09-01 20:37:33 +08:00
|
|
|
object.classId = NamespaceRelationId;
|
|
|
|
object.objectId = namespaceId;
|
|
|
|
object.objectSubId = 0;
|
|
|
|
audit_name = getObjectDescription(&object);
|
2011-01-24 09:44:48 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* check db_schema:{setattr relabelfrom} permission
|
|
|
|
*/
|
2011-09-01 20:37:33 +08:00
|
|
|
sepgsql_avc_check_perms(&object,
|
|
|
|
SEPG_CLASS_DB_SCHEMA,
|
|
|
|
SEPG_DB_SCHEMA__SETATTR |
|
|
|
|
SEPG_DB_SCHEMA__RELABELFROM,
|
|
|
|
audit_name,
|
|
|
|
true);
|
2012-06-11 03:20:04 +08:00
|
|
|
|
2011-01-24 09:44:48 +08:00
|
|
|
/*
|
|
|
|
* check db_schema:{relabelto} permission
|
|
|
|
*/
|
2011-09-01 20:37:33 +08:00
|
|
|
sepgsql_avc_check_perms_label(seclabel,
|
|
|
|
SEPG_CLASS_DB_SCHEMA,
|
|
|
|
SEPG_DB_SCHEMA__RELABELTO,
|
|
|
|
audit_name,
|
|
|
|
true);
|
2011-01-24 09:44:48 +08:00
|
|
|
pfree(audit_name);
|
|
|
|
}
|