aboutsummaryrefslogtreecommitdiff
path: root/contrib/sepgsql/schema.c
blob: 0de89971fbc15c879e7bbac765ebd26dea62460b (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
/* -------------------------------------------------------------------------
 *
 * contrib/sepgsql/schema.c
 *
 * Routines corresponding to schema objects
 *
 * Copyright (c) 2010-2011, PostgreSQL Global Development Group
 *
 * -------------------------------------------------------------------------
 */
#include "postgres.h"

#include "catalog/dependency.h"
#include "catalog/pg_namespace.h"
#include "commands/seclabel.h"
#include "utils/lsyscache.h"

#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)
{
	char	   *scontext = sepgsql_get_client_label();
	char	   *tcontext;
	char	   *ncontext;
	ObjectAddress object;

	/*
	 * FIXME: Right now, we assume pg_database object has a fixed security
	 * label, because pg_seclabel does not support to store label of shared
	 * database objects.
	 */
	tcontext = "system_u:object_r:sepgsql_db_t:s0";

	/*
	 * Compute a default security label when we create a new schema object
	 * under the working database.
	 */
	ncontext = sepgsql_compute_create(scontext, tcontext,
									  SEPG_CLASS_DB_SCHEMA);

	/*
	 * 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);
}

/*
 * sepgsql_schema_relabel
 *
 * It checks privileges to relabel the supplied schema
 * by the `seclabel'.
 */
void
sepgsql_schema_relabel(Oid namespaceId, const char *seclabel)
{
	char	   *scontext = sepgsql_get_client_label();
	char	   *tcontext;
	char	   *audit_name;

	audit_name = getObjectDescriptionOids(NamespaceRelationId, namespaceId);

	/*
	 * check db_schema:{setattr relabelfrom} permission
	 */
	tcontext = sepgsql_get_label(NamespaceRelationId, namespaceId, 0);

	sepgsql_check_perms(scontext,
						tcontext,
						SEPG_CLASS_DB_SCHEMA,
						SEPG_DB_SCHEMA__SETATTR |
						SEPG_DB_SCHEMA__RELABELFROM,
						audit_name,
						true);

	/*
	 * check db_schema:{relabelto} permission
	 */
	sepgsql_check_perms(scontext,
						seclabel,
						SEPG_CLASS_DB_SCHEMA,
						SEPG_DB_SCHEMA__RELABELTO,
						audit_name,
						true);

	pfree(tcontext);
	pfree(audit_name);
}