]>
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.", | |
7d0b9337 | 64 | permissions => { check => [ 'admin' ] }, |
1c0bc64c DM |
65 | protected => 1, |
66 | parameters => { | |
67 | additionalProperties => 0, | |
68 | properties => {} | |
69 | }, | |
70 | returns => { type => 'null'}, | |
71 | code => sub { | |
72 | my ($param) = @_; | |
73 | ||
74 | my $dbh = PMG::DBTools::open_ruledb("Proxmox_ruledb"); | |
75 | my $ruledb = PMG::RuleDB->new($dbh); | |
76 | PMG::DBTools::init_ruledb($ruledb, 1); | |
77 | ||
78 | return undef; | |
79 | }}); | |
80 | ||
3b9d7d09 DM |
81 | __PACKAGE__->register_method({ |
82 | name => 'ruledb_digest', | |
83 | path => 'digest', | |
84 | method => 'GET', | |
85 | description => "Returns the rule database digest. This is used internally for cluster synchronization.", | |
86 | # always run on local node, root@pam only | |
87 | parameters => { | |
88 | additionalProperties => 0, | |
89 | properties => {}, | |
90 | }, | |
b07f4d68 | 91 | permissions => { check => [ 'admin', 'audit' ] }, |
3b9d7d09 DM |
92 | returns => { type => 'string' }, |
93 | code => sub { | |
94 | my ($param) = @_; | |
95 | ||
96 | my $rdb = PMG::RuleDB->new(); | |
97 | my $rulecache = PMG::RuleCache->new($rdb); | |
98 | ||
99 | return $rulecache->{digest}; | |
100 | }}); | |
101 | ||
b52f6573 DM |
102 | __PACKAGE__->register_method({ |
103 | name => 'list_rules', | |
104 | path => 'rules', | |
105 | method => 'GET', | |
106 | description => "Get list of rules.", | |
bdcc6f0f | 107 | proxyto => 'master', |
b07f4d68 | 108 | permissions => { check => [ 'admin', 'audit' ] }, |
b52f6573 DM |
109 | parameters => { |
110 | additionalProperties => 0, | |
bdcc6f0f | 111 | properties => {}, |
b52f6573 DM |
112 | }, |
113 | returns => { | |
114 | type => 'array', | |
115 | items => { | |
116 | type => "object", | |
117 | properties => { | |
09992b83 DM |
118 | id => { type => 'integer' }, |
119 | }, | |
120 | }, | |
121 | links => [ { rel => 'child', href => "{id}" } ], | |
b52f6573 DM |
122 | }, |
123 | code => sub { | |
124 | my ($param) = @_; | |
125 | ||
09992b83 | 126 | my $rdb = PMG::RuleDB->new(); |
b52f6573 | 127 | |
09992b83 | 128 | my $rules = $rdb->load_rules(); |
b52f6573 DM |
129 | |
130 | my $res = []; | |
131 | ||
132 | my $cond_create_group = sub { | |
133 | my ($res, $name, $groupdata) = @_; | |
134 | ||
135 | return if !$groupdata; | |
136 | ||
4bea3204 | 137 | $res->{$name} = PMG::API2::ObjectGroupHelpers::format_object_group($groupdata); |
b52f6573 DM |
138 | }; |
139 | ||
140 | foreach my $rule (@$rules) { | |
141 | my ($from, $to, $when, $what, $action) = | |
09992b83 | 142 | $rdb->load_groups($rule); |
b52f6573 | 143 | |
af748e8c DM |
144 | my $data = PMG::API2::ObjectGroupHelpers::format_rule( |
145 | $rule, $from, $to, $when, $what, $action); | |
b52f6573 DM |
146 | |
147 | push @$res, $data; | |
148 | } | |
149 | ||
09992b83 | 150 | $rdb->close(); |
b52f6573 DM |
151 | |
152 | return $res; | |
153 | }}); | |
154 | ||
4a34322a DM |
155 | __PACKAGE__->register_method({ |
156 | name => 'create_rule', | |
157 | path => 'rules', | |
158 | method => 'POST', | |
159 | description => "Create new rule.", | |
160 | proxyto => 'master', | |
161 | protected => 1, | |
b07f4d68 | 162 | permissions => { check => [ 'admin' ] }, |
4a34322a DM |
163 | parameters => { |
164 | additionalProperties => 0, | |
165 | properties => { | |
166 | name => { | |
167 | description => "Rule name", | |
168 | type => 'string', | |
169 | }, | |
170 | priority => { | |
171 | description => "Rule priotity.", | |
172 | type => 'integer', | |
173 | minimum => 0, | |
174 | maximum => 100, | |
175 | }, | |
176 | direction => { | |
1359baef | 177 | description => "Rule direction. Value `0` matches incoming mails, value `1` matches outgoing mails, and value `2` matches both directions.", |
4a34322a DM |
178 | type => 'integer', |
179 | minimum => 0, | |
180 | maximum => 2, | |
181 | optional => 1, | |
182 | }, | |
183 | active => { | |
184 | description => "Flag to activate rule.", | |
185 | type => 'boolean', | |
186 | optional => 1, | |
187 | }, | |
188 | }, | |
189 | }, | |
190 | returns => { type => 'integer' }, | |
191 | code => sub { | |
192 | my ($param) = @_; | |
193 | ||
194 | my $rdb = PMG::RuleDB->new(); | |
195 | ||
196 | my $rule = PMG::RuleDB::Rule->new ( | |
197 | $param->{name}, $param->{priority}, $param->{active}, $param->{direction}); | |
198 | ||
199 | return $rdb->save_rule($rule); | |
200 | }}); | |
201 | ||
8d836d1d DM |
202 | __PACKAGE__->register_method ({ |
203 | subclass => 'PMG::API2::Rules', | |
204 | path => 'rules/{id}', | |
205 | }); | |
206 | ||
e6a59fe6 | 207 | |
11a86e67 DM |
208 | __PACKAGE__->register_method ({ |
209 | subclass => 'PMG::API2::Action', | |
59434740 | 210 | path => 'action', |
11a86e67 DM |
211 | }); |
212 | ||
59434740 DM |
213 | PMG::API2::ObjectGroupHelpers::register_group_list_api(__PACKAGE__, 'what'); |
214 | PMG::API2::ObjectGroupHelpers::register_group_list_api(__PACKAGE__, 'when'); | |
215 | PMG::API2::ObjectGroupHelpers::register_group_list_api(__PACKAGE__, 'who'); | |
216 | ||
1ad24a43 DM |
217 | __PACKAGE__->register_method ({ |
218 | subclass => 'PMG::API2::Who', | |
219 | path => 'who/{ogroup}', | |
220 | }); | |
11a86e67 | 221 | |
2499333b DM |
222 | __PACKAGE__->register_method ({ |
223 | subclass => 'PMG::API2::When', | |
224 | path => 'when/{ogroup}', | |
225 | }); | |
1ad24a43 | 226 | |
5f78c353 DM |
227 | __PACKAGE__->register_method ({ |
228 | subclass => 'PMG::API2::What', | |
229 | path => 'what/{ogroup}', | |
230 | }); | |
231 | ||
09992b83 | 232 | |
b52f6573 | 233 | 1; |