]>
Commit | Line | Data |
---|---|---|
b52f6573 DM |
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); | |
9d82c6bc | 9 | use PMG::RESTEnvironment; |
b52f6573 DM |
10 | use PVE::SafeSyslog; |
11 | use PVE::Tools qw(extract_param); | |
12 | ||
13 | use PMG::DBTools; | |
14 | use PMG::RuleDB; | |
3b9d7d09 | 15 | use PMG::RuleCache; |
b52f6573 | 16 | |
4bea3204 | 17 | use PMG::API2::ObjectGroupHelpers; |
1ad24a43 | 18 | use PMG::API2::Who; |
2499333b | 19 | use PMG::API2::When; |
5f78c353 | 20 | use PMG::API2::What; |
11a86e67 | 21 | use PMG::API2::Action; |
8d836d1d | 22 | use PMG::API2::Rules; |
1ad24a43 | 23 | |
b52f6573 DM |
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, | |
bdcc6f0f | 33 | properties => {}, |
b52f6573 | 34 | }, |
b07f4d68 | 35 | permissions => { check => [ 'admin', 'audit' ] }, |
b52f6573 DM |
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 = [ | |
3b9d7d09 | 48 | { name => 'digest' }, |
09992b83 | 49 | { name => 'action' }, |
b52f6573 | 50 | { name => 'rules' }, |
e6a59fe6 DM |
51 | { name => 'what' }, |
52 | { name => 'when' }, | |
53 | { name => 'who' }, | |
b52f6573 DM |
54 | ]; |
55 | ||
56 | return $result; | |
57 | }}); | |
58 | ||
1c0bc64c DM |
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 | ||
3b9d7d09 DM |
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 | }, | |
b07f4d68 | 90 | permissions => { check => [ 'admin', 'audit' ] }, |
3b9d7d09 DM |
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 | ||
b52f6573 DM |
101 | __PACKAGE__->register_method({ |
102 | name => 'list_rules', | |
103 | path => 'rules', | |
104 | method => 'GET', | |
105 | description => "Get list of rules.", | |
bdcc6f0f | 106 | proxyto => 'master', |
b07f4d68 | 107 | permissions => { check => [ 'admin', 'audit' ] }, |
b52f6573 DM |
108 | parameters => { |
109 | additionalProperties => 0, | |
bdcc6f0f | 110 | properties => {}, |
b52f6573 DM |
111 | }, |
112 | returns => { | |
113 | type => 'array', | |
114 | items => { | |
115 | type => "object", | |
116 | properties => { | |
09992b83 DM |
117 | id => { type => 'integer' }, |
118 | }, | |
119 | }, | |
120 | links => [ { rel => 'child', href => "{id}" } ], | |
b52f6573 DM |
121 | }, |
122 | code => sub { | |
123 | my ($param) = @_; | |
124 | ||
09992b83 | 125 | my $rdb = PMG::RuleDB->new(); |
b52f6573 | 126 | |
09992b83 | 127 | my $rules = $rdb->load_rules(); |
b52f6573 DM |
128 | |
129 | my $res = []; | |
130 | ||
131 | my $cond_create_group = sub { | |
132 | my ($res, $name, $groupdata) = @_; | |
133 | ||
134 | return if !$groupdata; | |
135 | ||
4bea3204 | 136 | $res->{$name} = PMG::API2::ObjectGroupHelpers::format_object_group($groupdata); |
b52f6573 DM |
137 | }; |
138 | ||
139 | foreach my $rule (@$rules) { | |
140 | my ($from, $to, $when, $what, $action) = | |
09992b83 | 141 | $rdb->load_groups($rule); |
b52f6573 | 142 | |
af748e8c DM |
143 | my $data = PMG::API2::ObjectGroupHelpers::format_rule( |
144 | $rule, $from, $to, $when, $what, $action); | |
b52f6573 DM |
145 | |
146 | push @$res, $data; | |
147 | } | |
148 | ||
09992b83 | 149 | $rdb->close(); |
b52f6573 DM |
150 | |
151 | return $res; | |
152 | }}); | |
153 | ||
4a34322a DM |
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, | |
b07f4d68 | 161 | permissions => { check => [ 'admin' ] }, |
4a34322a DM |
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 | ||
8d836d1d DM |
201 | __PACKAGE__->register_method ({ |
202 | subclass => 'PMG::API2::Rules', | |
203 | path => 'rules/{id}', | |
204 | }); | |
205 | ||
e6a59fe6 | 206 | |
11a86e67 DM |
207 | __PACKAGE__->register_method ({ |
208 | subclass => 'PMG::API2::Action', | |
59434740 | 209 | path => 'action', |
11a86e67 DM |
210 | }); |
211 | ||
59434740 DM |
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 | ||
1ad24a43 DM |
216 | __PACKAGE__->register_method ({ |
217 | subclass => 'PMG::API2::Who', | |
218 | path => 'who/{ogroup}', | |
219 | }); | |
11a86e67 | 220 | |
2499333b DM |
221 | __PACKAGE__->register_method ({ |
222 | subclass => 'PMG::API2::When', | |
223 | path => 'when/{ogroup}', | |
224 | }); | |
1ad24a43 | 225 | |
5f78c353 DM |
226 | __PACKAGE__->register_method ({ |
227 | subclass => 'PMG::API2::What', | |
228 | path => 'what/{ogroup}', | |
229 | }); | |
230 | ||
09992b83 | 231 | |
b52f6573 | 232 | 1; |