]> git.proxmox.com Git - pmg-api.git/blob - PMG/RuleCache.pm
actually calculate day when it is given
[pmg-api.git] / PMG / RuleCache.pm
1 package PMG::RuleCache;
2
3 use strict;
4 use warnings;
5 use DBI;
6
7 use PVE::SafeSyslog;
8
9 use PMG::Utils;
10 use PMG::RuleDB;
11 use Digest::SHA;
12
13 my $ocache_size = 1023;
14
15 sub new {
16 my ($type, $ruledb) = @_;
17
18 my $self;
19
20 $self->{ruledb} = $ruledb;
21 $self->{ocache} = ();
22
23 bless $self, $type;
24
25 my $rules = ();
26
27 my $dbh = $ruledb->{dbh};
28
29 my $sha1 = Digest::SHA->new;
30
31 eval {
32 $dbh->begin_work;
33
34 # read a consistent snapshot
35 $dbh->do("SET TRANSACTION ISOLATION LEVEL SERIALIZABLE");
36
37 my $sth = $dbh->prepare(
38 "SELECT ID, Name, Priority, Active, Direction FROM Rule " .
39 "where Active > 0 " .
40 "ORDER BY Priority DESC");
41
42 $sth->execute();
43
44 while (my $ref = $sth->fetchrow_hashref()) {
45 my $ruleid = $ref->{id};
46 my $rule = PMG::RuleDB::Rule->new(
47 $ref->{name}, $ref->{priority}, $ref->{active},
48 $ref->{direction});
49
50 $rule->{id} = $ruleid;
51 push @$rules, $rule;
52
53 $sha1->add(join(',', $ref->{id}, $ref->{name}, $ref->{priority}, $ref->{active},
54 $ref->{direction}) . "|");
55
56 my ($from, $to, $when, $what, $action);
57
58 my $sth1 = $dbh->prepare(
59 "SELECT Objectgroup_ID, Grouptype FROM RuleGroup " .
60 "where RuleGroup.Rule_ID = '$ruleid' " .
61 "ORDER BY Grouptype, Objectgroup_ID");
62
63 $sth1->execute();
64 while (my $ref1 = $sth1->fetchrow_hashref()) {
65 my $gtype = $ref1->{grouptype};
66 my $groupid = $ref1->{objectgroup_id};
67
68 # emtyp groups differ from non-existent groups!
69
70 if ($gtype == 0) { #from
71 $from = [] if !defined ($from);
72 } elsif ($gtype == 1) { # to
73 $to = [] if !defined ($to);
74 } elsif ($gtype == 2) { # when
75 $when = [] if !defined ($when);
76 } elsif ($gtype == 3) { # what
77 $what = [] if !defined ($what);
78 } elsif ($gtype == 4) { # action
79 $action = [] if !defined ($action);
80 }
81
82 my $sth2 = $dbh->prepare(
83 "SELECT ID FROM Object where Objectgroup_ID = '$groupid' " .
84 "ORDER BY ID");
85 $sth2->execute();
86 while (my $ref2 = $sth2->fetchrow_hashref()) {
87 my $objid = $ref2->{'id'};
88 my $obj = $self->_get_object($objid);
89
90 $sha1->add (join (',', $objid, $gtype, $groupid) . "|");
91 $sha1->add ($obj->{digest}, "|");
92
93 if ($gtype == 0) { #from
94 push @$from, $obj;
95 } elsif ($gtype == 1) { # to
96 push @$to, $obj;
97 } elsif ($gtype == 2) { # when
98 push @$when, $obj;
99 } elsif ($gtype == 3) { # what
100 push @$what, $obj;
101 if ($obj->otype == PMG::RuleDB::ArchiveFilter->otype) {
102 if ($rule->{direction} == 0) {
103 $self->{archivefilter_in} = 1;
104 } elsif ($rule->{direction} == 1) {
105 $self->{archivefilter_out} = 1;
106 } else {
107 $self->{archivefilter_in} = 1;
108 $self->{archivefilter_out} = 1;
109 }
110 }
111 } elsif ($gtype == 4) { # action
112 push @$action, $obj;
113 $self->{"$ruleid:final"} = 1 if $obj->final();
114 }
115 }
116 $sth2->finish();
117 }
118
119 $sth1->finish();
120
121 $self->{"$ruleid:from"} = $from;
122 $self->{"$ruleid:to"} = $to;
123 $self->{"$ruleid:when"} = $when;
124 $self->{"$ruleid:what"} = $what;
125 $self->{"$ruleid:action"} = $action;
126 }
127
128 # Cache Greylist Exclusion
129 $sth = $dbh->prepare(
130 "SELECT object.id FROM object, objectgroup " .
131 "WHERE class = 'greylist' AND " .
132 "objectgroup.id = object.objectgroup_id " .
133 "ORDER BY object.id");
134
135 $sth->execute();
136 my $grey_excl_sender = ();
137 my $grey_excl_receiver = ();
138 while (my $ref2 = $sth->fetchrow_hashref()) {
139 my $obj = $self->_get_object ($ref2->{'id'});
140
141 if ($obj->receivertest()) {
142 push @$grey_excl_receiver, $obj;
143 } else {
144 push @$grey_excl_sender, $obj;
145 }
146 $sha1->add ($ref2->{'id'}, "|");
147 $sha1->add ($obj->{digest}, "|");
148 }
149
150 $self->{"greylist:sender"} = $grey_excl_sender;
151 $self->{"greylist:receiver"} = $grey_excl_receiver;
152
153 $sth->finish();
154 };
155 my $err = $@;
156
157 $dbh->rollback; # end transaction
158
159 syslog ('err', "unable to load rulecache : $err") if $err;
160
161 $self->{rules} = $rules;
162
163 $self->{digest} = $sha1->hexdigest;
164
165 return $self;
166 }
167
168 sub final {
169 my ($self, $ruleid) = @_;
170
171 defined($ruleid) || die "undefined rule id: ERROR";
172
173 return $self->{"$ruleid:final"};
174 }
175
176 sub rules {
177 my ($self) = @_;
178
179 $self->{rules};
180 }
181
182 sub _get_object {
183 my ($self, $objid) = @_;
184
185 my $cid = $objid % $ocache_size;
186
187 my $obj = $self->{ocache}[$cid];
188
189 if (!defined ($obj) || $obj->{id} != $objid) {
190 $obj = $self->{ruledb}->load_object($objid);
191 $self->{ocache}[$cid] = $obj;
192 }
193
194 $obj || die "unable to get object $objid: ERROR";
195
196 return $obj;
197 }
198
199 sub get_actions {
200 my ($self, $ruleid) = @_;
201
202 defined($ruleid) || die "undefined rule id: ERROR";
203
204 return $self->{"$ruleid:action"};
205 }
206
207 sub greylist_match {
208 my ($self, $addr, $ip) = @_;
209
210 my $grey = $self->{"greylist:sender"};
211
212 foreach my $obj (@$grey) {
213 if ($obj->who_match ($addr, $ip)) {
214 return 1;
215 }
216 }
217
218 return 0;
219 }
220
221 sub greylist_match_receiver {
222 my ($self, $addr) = @_;
223
224 my $grey = $self->{"greylist:receiver"};
225
226 foreach my $obj (@$grey) {
227 if ($obj->who_match($addr)) {
228 return 1;
229 }
230 }
231
232 return 0;
233 }
234
235 sub from_match {
236 my ($self, $ruleid, $addr, $ip, $ldap) = @_;
237
238 my $from = $self->{"$ruleid:from"};
239
240 return 1 if !defined ($from);
241
242 foreach my $obj (@$from) {
243 return 1 if $obj->who_match($addr, $ip, $ldap);
244 }
245
246 return 0;
247 }
248
249 sub to_match {
250 my ($self, $ruleid, $addr, $ldap) = @_;
251
252 my $to = $self->{"$ruleid:to"};
253
254 return 1 if !defined ($to);
255
256 foreach my $obj (@$to) {
257 return 1 if $obj->who_match($addr, undef, $ldap);
258 }
259
260 return 0;
261 }
262
263 sub when_match {
264 my ($self, $ruleid, $time) = @_;
265
266 my $when = $self->{"$ruleid:when"};
267
268 return 1 if !defined ($when);
269
270 foreach my $obj (@$when) {
271 return 1 if $obj->when_match($time);
272 }
273
274 return 0;
275 }
276
277 sub what_match {
278 my ($self, $ruleid, $queue, $element, $msginfo, $dbh) = @_;
279
280 my $what = $self->{"$ruleid:what"};
281
282 my $res;
283
284 # $res->{marks} is used by mark specific actions like remove-attachments
285 # $res->{$target}->{marks} is only used in apply_rules() to exclude some
286 # targets (spam blacklist and whitelist)
287
288 if (!defined ($what)) {
289 # match all targets
290 foreach my $target (@{$msginfo->{targets}}) {
291 $res->{$target}->{marks} = [];
292 }
293
294 $res->{marks} = [];
295 return $res;
296 }
297
298 my $marks;
299
300 foreach my $obj (@$what) {
301 if (!$obj->can('what_match_targets')) {
302 if (my $match = $obj->what_match($queue, $element, $msginfo, $dbh)) {
303 push @$marks, @$match;
304 }
305 }
306 }
307
308 foreach my $target (@{$msginfo->{targets}}) {
309 $res->{$target}->{marks} = $marks;
310 $res->{marks} = $marks;
311 }
312
313 foreach my $obj (@$what) {
314 if ($obj->can ("what_match_targets")) {
315 my $target_info;
316 if ($target_info = $obj->what_match_targets($queue, $element, $msginfo, $dbh)) {
317 foreach my $k (keys %$target_info) {
318 my $cmarks = $target_info->{$k}->{marks}; # make a copy
319 $res->{$k} = $target_info->{$k};
320 push @{$res->{$k}->{marks}}, @$cmarks if $cmarks;
321 }
322 }
323 }
324 }
325
326 return $res;
327 }
328
329 1;