2011-01-24 04:44:48 +03:00
|
|
|
/* -------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* contrib/sepgsql/schema.c
|
|
|
|
*
|
|
|
|
* Routines corresponding to schema objects
|
|
|
|
*
|
2024-01-04 04:49:05 +03:00
|
|
|
* Copyright (c) 2010-2024, PostgreSQL Global Development Group
|
2011-01-24 04:44:48 +03:00
|
|
|
*
|
|
|
|
* -------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
2019-12-27 02:09:00 +03:00
|
|
|
#include "access/genam.h"
|
2012-09-05 22:01:15 +04:00
|
|
|
#include "access/htup_details.h"
|
2011-12-21 18:12:43 +04:00
|
|
|
#include "access/sysattr.h"
|
2019-01-21 21:18:20 +03:00
|
|
|
#include "access/table.h"
|
2011-02-03 07:39:43 +03:00
|
|
|
#include "catalog/dependency.h"
|
2011-09-24 01:09:34 +04:00
|
|
|
#include "catalog/pg_database.h"
|
2011-01-24 04:44:48 +03:00
|
|
|
#include "catalog/pg_namespace.h"
|
|
|
|
#include "commands/seclabel.h"
|
2013-04-12 16:35:55 +04:00
|
|
|
#include "lib/stringinfo.h"
|
2011-09-24 01:09:34 +04:00
|
|
|
#include "miscadmin.h"
|
2019-10-23 06:56:22 +03:00
|
|
|
#include "sepgsql.h"
|
2013-04-12 16:35:55 +04:00
|
|
|
#include "utils/builtins.h"
|
2011-12-21 18:12:43 +04:00
|
|
|
#include "utils/fmgroids.h"
|
2011-01-24 04:44:48 +03:00
|
|
|
#include "utils/lsyscache.h"
|
2019-01-22 04:03:15 +03:00
|
|
|
#include "utils/snapmgr.h"
|
2011-01-24 04:44:48 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 18:12:43 +04:00
|
|
|
Relation rel;
|
|
|
|
ScanKeyData skey;
|
|
|
|
SysScanDesc sscan;
|
|
|
|
HeapTuple tuple;
|
2011-01-24 04:44:48 +03:00
|
|
|
char *tcontext;
|
|
|
|
char *ncontext;
|
2013-03-28 23:38:35 +04:00
|
|
|
const char *nsp_name;
|
2011-12-21 18:12:43 +04:00
|
|
|
ObjectAddress object;
|
|
|
|
Form_pg_namespace nspForm;
|
2013-04-12 16:35:55 +04:00
|
|
|
StringInfoData audit_name;
|
2011-01-24 04:44:48 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Compute a default security label when we create a new schema object
|
|
|
|
* under the working database.
|
2011-12-21 18:12:43 +04:00
|
|
|
*
|
2019-10-30 04:03:00 +03:00
|
|
|
* XXX - upcoming version of libselinux supports to take object name to
|
2011-12-21 18:12:43 +04:00
|
|
|
* handle special treatment on default security label; such as special
|
|
|
|
* label on "pg_temp" schema.
|
2011-01-24 04:44:48 +03:00
|
|
|
*/
|
2019-01-21 21:32:19 +03:00
|
|
|
rel = table_open(NamespaceRelationId, AccessShareLock);
|
2011-12-21 18:12:43 +04:00
|
|
|
|
|
|
|
ScanKeyInit(&skey,
|
2018-11-21 07:03:46 +03:00
|
|
|
Anum_pg_namespace_oid,
|
2011-12-21 18:12:43 +04:00
|
|
|
BTEqualStrategyNumber, F_OIDEQ,
|
|
|
|
ObjectIdGetDatum(namespaceId));
|
|
|
|
|
|
|
|
sscan = systable_beginscan(rel, NamespaceOidIndexId, true,
|
|
|
|
SnapshotSelf, 1, &skey);
|
|
|
|
tuple = systable_getnext(sscan);
|
|
|
|
if (!HeapTupleIsValid(tuple))
|
2017-06-04 23:20:03 +03:00
|
|
|
elog(ERROR, "could not find tuple for namespace %u", namespaceId);
|
2011-12-21 18:12:43 +04:00
|
|
|
|
|
|
|
nspForm = (Form_pg_namespace) GETSTRUCT(tuple);
|
2013-03-28 23:38:35 +04:00
|
|
|
nsp_name = NameStr(nspForm->nspname);
|
|
|
|
if (strncmp(nsp_name, "pg_temp_", 8) == 0)
|
|
|
|
nsp_name = "pg_temp";
|
|
|
|
else if (strncmp(nsp_name, "pg_toast_temp_", 14) == 0)
|
|
|
|
nsp_name = "pg_toast_temp";
|
2011-12-21 18:12:43 +04:00
|
|
|
|
2011-09-24 01:09:34 +04:00
|
|
|
tcontext = sepgsql_get_label(DatabaseRelationId, MyDatabaseId, 0);
|
2011-12-21 18:12:43 +04:00
|
|
|
ncontext = sepgsql_compute_create(sepgsql_get_client_label(),
|
|
|
|
tcontext,
|
2013-03-28 23:38:35 +04:00
|
|
|
SEPG_CLASS_DB_SCHEMA,
|
|
|
|
nsp_name);
|
2013-05-30 00:58:43 +04:00
|
|
|
|
2011-12-21 18:12:43 +04:00
|
|
|
/*
|
|
|
|
* check db_schema:{create}
|
|
|
|
*/
|
2013-04-12 16:35:55 +04:00
|
|
|
initStringInfo(&audit_name);
|
2022-09-06 04:19:44 +03:00
|
|
|
appendStringInfoString(&audit_name, quote_identifier(nsp_name));
|
2011-12-21 18:12:43 +04:00
|
|
|
sepgsql_avc_check_perms_label(ncontext,
|
|
|
|
SEPG_CLASS_DB_SCHEMA,
|
|
|
|
SEPG_DB_SCHEMA__CREATE,
|
2013-04-12 16:35:55 +04:00
|
|
|
audit_name.data,
|
2011-12-21 18:12:43 +04:00
|
|
|
true);
|
|
|
|
systable_endscan(sscan);
|
2019-01-21 21:32:19 +03:00
|
|
|
table_close(rel, AccessShareLock);
|
2011-01-24 04:44:48 +03: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 01:09:34 +04:00
|
|
|
pfree(tcontext);
|
2011-01-24 04:44:48 +03:00
|
|
|
}
|
|
|
|
|
2012-03-10 00:18:45 +04:00
|
|
|
/*
|
|
|
|
* sepgsql_schema_drop
|
|
|
|
*
|
|
|
|
* It checks privileges to drop the supplied schema object.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
sepgsql_schema_drop(Oid namespaceId)
|
|
|
|
{
|
|
|
|
ObjectAddress object;
|
|
|
|
char *audit_name;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check db_schema:{drop} permission
|
|
|
|
*/
|
|
|
|
object.classId = NamespaceRelationId;
|
|
|
|
object.objectId = namespaceId;
|
|
|
|
object.objectSubId = 0;
|
2020-07-15 03:03:10 +03:00
|
|
|
audit_name = getObjectIdentity(&object, false);
|
2012-03-10 00:18:45 +04:00
|
|
|
|
|
|
|
sepgsql_avc_check_perms(&object,
|
|
|
|
SEPG_CLASS_DB_SCHEMA,
|
|
|
|
SEPG_DB_SCHEMA__DROP,
|
|
|
|
audit_name,
|
|
|
|
true);
|
|
|
|
pfree(audit_name);
|
|
|
|
}
|
|
|
|
|
2011-01-24 04:44:48 +03:00
|
|
|
/*
|
|
|
|
* sepgsql_schema_relabel
|
|
|
|
*
|
|
|
|
* It checks privileges to relabel the supplied schema
|
|
|
|
* by the `seclabel'.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
sepgsql_schema_relabel(Oid namespaceId, const char *seclabel)
|
|
|
|
{
|
2011-09-01 16:37:33 +04:00
|
|
|
ObjectAddress object;
|
|
|
|
char *audit_name;
|
2011-01-24 04:44:48 +03:00
|
|
|
|
2011-09-01 16:37:33 +04:00
|
|
|
object.classId = NamespaceRelationId;
|
|
|
|
object.objectId = namespaceId;
|
|
|
|
object.objectSubId = 0;
|
2020-07-15 03:03:10 +03:00
|
|
|
audit_name = getObjectIdentity(&object, false);
|
2011-01-24 04:44:48 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* check db_schema:{setattr relabelfrom} permission
|
|
|
|
*/
|
2011-09-01 16:37:33 +04:00
|
|
|
sepgsql_avc_check_perms(&object,
|
|
|
|
SEPG_CLASS_DB_SCHEMA,
|
|
|
|
SEPG_DB_SCHEMA__SETATTR |
|
|
|
|
SEPG_DB_SCHEMA__RELABELFROM,
|
|
|
|
audit_name,
|
|
|
|
true);
|
2012-06-10 23:20:04 +04:00
|
|
|
|
2011-01-24 04:44:48 +03:00
|
|
|
/*
|
|
|
|
* check db_schema:{relabelto} permission
|
|
|
|
*/
|
2011-09-01 16:37:33 +04:00
|
|
|
sepgsql_avc_check_perms_label(seclabel,
|
|
|
|
SEPG_CLASS_DB_SCHEMA,
|
|
|
|
SEPG_DB_SCHEMA__RELABELTO,
|
|
|
|
audit_name,
|
|
|
|
true);
|
2011-01-24 04:44:48 +03:00
|
|
|
pfree(audit_name);
|
|
|
|
}
|
2013-03-27 16:10:14 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* sepgsql_schema_check_perms
|
|
|
|
*
|
|
|
|
* utility routine to check db_schema:{xxx} permissions
|
|
|
|
*/
|
2013-04-05 16:51:31 +04:00
|
|
|
static bool
|
|
|
|
check_schema_perms(Oid namespaceId, uint32 required, bool abort_on_violation)
|
2013-03-27 16:10:14 +04:00
|
|
|
{
|
|
|
|
ObjectAddress object;
|
|
|
|
char *audit_name;
|
2013-04-05 16:51:31 +04:00
|
|
|
bool result;
|
2013-03-27 16:10:14 +04:00
|
|
|
|
|
|
|
object.classId = NamespaceRelationId;
|
|
|
|
object.objectId = namespaceId;
|
|
|
|
object.objectSubId = 0;
|
2020-07-15 03:03:10 +03:00
|
|
|
audit_name = getObjectIdentity(&object, false);
|
2013-03-27 16:10:14 +04:00
|
|
|
|
2013-04-05 16:51:31 +04:00
|
|
|
result = sepgsql_avc_check_perms(&object,
|
|
|
|
SEPG_CLASS_DB_SCHEMA,
|
|
|
|
required,
|
|
|
|
audit_name,
|
|
|
|
abort_on_violation);
|
2013-03-27 16:10:14 +04:00
|
|
|
pfree(audit_name);
|
2013-04-05 16:51:31 +04:00
|
|
|
|
|
|
|
return result;
|
2013-03-27 16:10:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* db_schema:{setattr} permission */
|
|
|
|
void
|
|
|
|
sepgsql_schema_setattr(Oid namespaceId)
|
|
|
|
{
|
2013-04-05 16:51:31 +04:00
|
|
|
check_schema_perms(namespaceId, SEPG_DB_SCHEMA__SETATTR, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* db_schema:{search} permission */
|
|
|
|
bool
|
|
|
|
sepgsql_schema_search(Oid namespaceId, bool abort_on_violation)
|
|
|
|
{
|
|
|
|
return check_schema_perms(namespaceId,
|
|
|
|
SEPG_DB_SCHEMA__SEARCH,
|
|
|
|
abort_on_violation);
|
2013-03-27 16:10:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sepgsql_schema_add_name(Oid namespaceId)
|
|
|
|
{
|
2013-04-05 16:51:31 +04:00
|
|
|
check_schema_perms(namespaceId, SEPG_DB_SCHEMA__ADD_NAME, true);
|
2013-03-27 16:10:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sepgsql_schema_remove_name(Oid namespaceId)
|
|
|
|
{
|
2013-04-05 16:51:31 +04:00
|
|
|
check_schema_perms(namespaceId, SEPG_DB_SCHEMA__REMOVE_NAME, true);
|
2013-03-27 16:10:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sepgsql_schema_rename(Oid namespaceId)
|
|
|
|
{
|
|
|
|
check_schema_perms(namespaceId,
|
|
|
|
SEPG_DB_SCHEMA__ADD_NAME |
|
2013-04-05 16:51:31 +04:00
|
|
|
SEPG_DB_SCHEMA__REMOVE_NAME,
|
|
|
|
true);
|
2013-03-27 16:10:14 +04:00
|
|
|
}
|