forked from hlinnaka/pg_quota
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenforcement.c
112 lines (96 loc) · 2.56 KB
/
enforcement.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
/* -------------------------------------------------------------------------
*
* enforcement.c
* Hooks to enforce the disk space quotas.
*
* This file contains functions for enforcing quotas. Currently, they are
* only enforced for INSERTS and COPY, by using the ExecCheckRTPerms hook.
*
* Copyright (c) 2013-2018, PostgreSQL Global Development Group
* -------------------------------------------------------------------------
*/
#include "postgres.h"
#include "access/htup_details.h"
#include "catalog/pg_class.h"
#include "executor/executor.h"
#include "utils/syscache.h"
#include "pg_quota.h"
static bool quota_check_ExecCheckRTPerms(List *rangeTable, bool ereport_on_violation);
static ExecutorCheckPerms_hook_type prev_ExecutorCheckPerms_hook;
static bool ExecutorCheckPerms_hook_installed = false;
/*
* Initialize enforcement, by installing the executor permission hook.
*/
void
init_quota_enforcement(void)
{
if (!ExecutorCheckPerms_hook_installed)
{
prev_ExecutorCheckPerms_hook = ExecutorCheckPerms_hook;
ExecutorCheckPerms_hook = quota_check_ExecCheckRTPerms;
elog(DEBUG1, "disk quota permissions hook installed");
}
}
static Oid
get_rel_owner(Oid relid)
{
HeapTuple tp;
tp = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
if (HeapTupleIsValid(tp))
{
Form_pg_class reltup = (Form_pg_class) GETSTRUCT(tp);
Oid result;
result = reltup->relowner;
ReleaseSysCache(tp);
return result;
}
else
{
elog(DEBUG1, "could not find owner for relation %u", relid);
return InvalidOid;
}
}
/*
* Permission check hook function. Throws an error if you try to INSERT
* (or COPY) into a table, and the quota has been exceeded.
*/
static bool
quota_check_ExecCheckRTPerms(List *rangeTable, bool ereport_on_violation)
{
ListCell *l;
foreach(l, rangeTable)
{
RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);
Oid owner;
/* see ExecCheckRTEPerms() */
if (rte->rtekind != RTE_RELATION)
continue;
/*
* Only check quota on inserts. UPDATEs may well increase
* space usage too, but we ignore that for now.
*/
if ((rte->requiredPerms & ACL_INSERT) == 0)
continue;
/*
* Perform the check as the relation's owner, rather than the current
* user.
*/
owner = get_rel_owner(rte->relid);
if (owner == InvalidOid)
return true; /* no owner, huh? */
if (!CheckQuota(owner))
{
/*
* The owner is out of quota. Report error.
*
* We
*/
if (ereport_on_violation)
ereport(ERROR,
(errcode(ERRCODE_DISK_FULL),
errmsg("user's disk space quota exceeded")));
return false;
}
}
return true;
}