]> git.proxmox.com Git - pmg-api.git/blob - PMG/API2/RuleDB.pm
delete/deliver_quarantined_mail: use receiver instead of pmail
[pmg-api.git] / PMG / API2 / RuleDB.pm
1 package PMG::API2::RuleDB;
2
3 use strict;
4 use warnings;
5
6 use PVE::INotify;
7 use PVE::RESTHandler;
8 use PVE::JSONSchema qw(get_standard_option);
9 use PMG::RESTEnvironment;
10 use PVE::SafeSyslog;
11 use PVE::Tools qw(extract_param);
12
13 use PMG::DBTools;
14 use PMG::RuleDB;
15 use PMG::RuleCache;
16
17 use PMG::API2::ObjectGroupHelpers;
18 use PMG::API2::Who;
19 use PMG::API2::When;
20 use PMG::API2::What;
21 use PMG::API2::Action;
22 use PMG::API2::Rules;
23
24 use base qw(PVE::RESTHandler);
25
26 __PACKAGE__->register_method ({
27 name => 'index',
28 path => '',
29 method => 'GET',
30 description => "Directory index.",
31 parameters => {
32 additionalProperties => 0,
33 properties => {},
34 },
35 permissions => { check => [ 'admin', 'audit' ] },
36 returns => {
37 type => 'array',
38 items => {
39 type => "object",
40 properties => {},
41 },
42 links => [ { rel => 'child', href => "{name}" } ],
43 },
44 code => sub {
45 my ($param) = @_;
46
47 my $result = [
48 { name => 'digest' },
49 { name => 'action' },
50 { name => 'rules' },
51 { name => 'what' },
52 { name => 'when' },
53 { name => 'who' },
54 ];
55
56 return $result;
57 }});
58
59 __PACKAGE__->register_method ({
60 name => 'reset_ruledb',
61 path => '',
62 method => 'POST',
63 description => "Reset PMG rule database back to factory defaults.",
64 protected => 1,
65 parameters => {
66 additionalProperties => 0,
67 properties => {}
68 },
69 returns => { type => 'null'},
70 code => sub {
71 my ($param) = @_;
72
73 my $dbh = PMG::DBTools::open_ruledb("Proxmox_ruledb");
74 my $ruledb = PMG::RuleDB->new($dbh);
75 PMG::DBTools::init_ruledb($ruledb, 1);
76
77 return undef;
78 }});
79
80 __PACKAGE__->register_method({
81 name => 'ruledb_digest',
82 path => 'digest',
83 method => 'GET',
84 description => "Returns the rule database digest. This is used internally for cluster synchronization.",
85 # always run on local node, root@pam only
86 parameters => {
87 additionalProperties => 0,
88 properties => {},
89 },
90 permissions => { check => [ 'admin', 'audit' ] },
91 returns => { type => 'string' },
92 code => sub {
93 my ($param) = @_;
94
95 my $rdb = PMG::RuleDB->new();
96 my $rulecache = PMG::RuleCache->new($rdb);
97
98 return $rulecache->{digest};
99 }});
100
101 __PACKAGE__->register_method({
102 name => 'list_rules',
103 path => 'rules',
104 method => 'GET',
105 description => "Get list of rules.",
106 proxyto => 'master',
107 permissions => { check => [ 'admin', 'audit' ] },
108 parameters => {
109 additionalProperties => 0,
110 properties => {},
111 },
112 returns => {
113 type => 'array',
114 items => {
115 type => "object",
116 properties => {
117 id => { type => 'integer' },
118 },
119 },
120 links => [ { rel => 'child', href => "{id}" } ],
121 },
122 code => sub {
123 my ($param) = @_;
124
125 my $rdb = PMG::RuleDB->new();
126
127 my $rules = $rdb->load_rules();
128
129 my $res = [];
130
131 my $cond_create_group = sub {
132 my ($res, $name, $groupdata) = @_;
133
134 return if !$groupdata;
135
136 $res->{$name} = PMG::API2::ObjectGroupHelpers::format_object_group($groupdata);
137 };
138
139 foreach my $rule (@$rules) {
140 my ($from, $to, $when, $what, $action) =
141 $rdb->load_groups($rule);
142
143 my $data = PMG::API2::ObjectGroupHelpers::format_rule(
144 $rule, $from, $to, $when, $what, $action);
145
146 push @$res, $data;
147 }
148
149 $rdb->close();
150
151 return $res;
152 }});
153
154 __PACKAGE__->register_method({
155 name => 'create_rule',
156 path => 'rules',
157 method => 'POST',
158 description => "Create new rule.",
159 proxyto => 'master',
160 protected => 1,
161 permissions => { check => [ 'admin' ] },
162 parameters => {
163 additionalProperties => 0,
164 properties => {
165 name => {
166 description => "Rule name",
167 type => 'string',
168 },
169 priority => {
170 description => "Rule priotity.",
171 type => 'integer',
172 minimum => 0,
173 maximum => 100,
174 },
175 direction => {
176 description => "Rule direction. Value `0` matches incomming mails, value `1` matches outgoing mails, and value `2` matches both directions.",
177 type => 'integer',
178 minimum => 0,
179 maximum => 2,
180 optional => 1,
181 },
182 active => {
183 description => "Flag to activate rule.",
184 type => 'boolean',
185 optional => 1,
186 },
187 },
188 },
189 returns => { type => 'integer' },
190 code => sub {
191 my ($param) = @_;
192
193 my $rdb = PMG::RuleDB->new();
194
195 my $rule = PMG::RuleDB::Rule->new (
196 $param->{name}, $param->{priority}, $param->{active}, $param->{direction});
197
198 return $rdb->save_rule($rule);
199 }});
200
201 __PACKAGE__->register_method ({
202 subclass => 'PMG::API2::Rules',
203 path => 'rules/{id}',
204 });
205
206
207 __PACKAGE__->register_method ({
208 subclass => 'PMG::API2::Action',
209 path => 'action',
210 });
211
212 PMG::API2::ObjectGroupHelpers::register_group_list_api(__PACKAGE__, 'what');
213 PMG::API2::ObjectGroupHelpers::register_group_list_api(__PACKAGE__, 'when');
214 PMG::API2::ObjectGroupHelpers::register_group_list_api(__PACKAGE__, 'who');
215
216 __PACKAGE__->register_method ({
217 subclass => 'PMG::API2::Who',
218 path => 'who/{ogroup}',
219 });
220
221 __PACKAGE__->register_method ({
222 subclass => 'PMG::API2::When',
223 path => 'when/{ogroup}',
224 });
225
226 __PACKAGE__->register_method ({
227 subclass => 'PMG::API2::What',
228 path => 'what/{ogroup}',
229 });
230
231
232 1;