]>
Commit | Line | Data |
---|---|---|
b66faa68 DM |
1 | package PMG::API2::Quarantine; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | use Time::Local; | |
6 | use Time::Zone; | |
7 | use Data::Dumper; | |
34db0c3f | 8 | use Encode; |
b66faa68 | 9 | |
34db0c3f | 10 | use Mail::Header; |
e325aa6f | 11 | use Mail::SpamAssassin; |
dae021a8 | 12 | |
b66faa68 | 13 | use PVE::SafeSyslog; |
6e8886d4 | 14 | use PVE::Exception qw(raise_param_exc raise_perm_exc); |
b66faa68 DM |
15 | use PVE::Tools qw(extract_param); |
16 | use PVE::JSONSchema qw(get_standard_option); | |
17 | use PVE::RESTHandler; | |
18 | use PVE::INotify; | |
34db0c3f | 19 | use PVE::APIServer::Formatter; |
b66faa68 | 20 | |
dae021a8 | 21 | use PMG::Utils; |
b66faa68 | 22 | use PMG::AccessControl; |
34db0c3f | 23 | use PMG::Config; |
b66faa68 | 24 | use PMG::DBTools; |
34db0c3f | 25 | use PMG::HTMLMail; |
e84bf942 | 26 | use PMG::Quarantine; |
b66faa68 DM |
27 | |
28 | use base qw(PVE::RESTHandler); | |
29 | ||
1284c016 DM |
30 | my $spamdesc; |
31 | ||
9efdabf0 DM |
32 | my $extract_pmail = sub { |
33 | my ($authuser, $role) = @_; | |
34 | ||
35 | if ($authuser =~ m/^(.+)\@quarantine$/) { | |
36 | return $1; | |
37 | } | |
38 | raise_param_exc({ pmail => "got unexpected authuser '$authuser' with role '$role'"}); | |
39 | }; | |
40 | ||
157a946b | 41 | my $verify_optional_pmail = sub { |
666b5e8f | 42 | my ($authuser, $role, $pmail_param) = @_; |
157a946b | 43 | |
666b5e8f | 44 | my $pmail; |
157a946b | 45 | if ($role eq 'quser') { |
9efdabf0 | 46 | $pmail = $extract_pmail->($authuser, $role); |
666b5e8f DM |
47 | raise_param_exc({ pmail => "parameter not allwed with role '$role'"}) |
48 | if defined($pmail_param) && ($pmail ne $pmail_param); | |
157a946b | 49 | } else { |
5182cea0 | 50 | raise_param_exc({ pmail => "parameter required with role '$role'"}) |
666b5e8f DM |
51 | if !defined($pmail_param); |
52 | $pmail = $pmail_param; | |
157a946b DM |
53 | } |
54 | return $pmail; | |
55 | }; | |
56 | ||
1284c016 DM |
57 | sub decode_spaminfo { |
58 | my ($info) = @_; | |
59 | ||
e325aa6f DC |
60 | my $saversion = Mail::SpamAssassin->VERSION; |
61 | ||
62 | my $salocaldir = "/var/lib/spamassassin/$saversion/updates_spamassassin_org"; | |
63 | ||
64 | $spamdesc = PMG::Utils::load_sa_descriptions([$salocaldir]) if !$spamdesc; | |
1284c016 DM |
65 | |
66 | my $res = []; | |
67 | ||
68 | foreach my $test (split (',', $info)) { | |
69 | my ($name, $score) = split (':', $test); | |
70 | ||
273c538f | 71 | my $info = { name => $name, score => $score + 0, desc => '-' }; |
1284c016 DM |
72 | if (my $si = $spamdesc->{$name}) { |
73 | $info->{desc} = $si->{desc}; | |
74 | $info->{url} = $si->{url} if defined($si->{url}); | |
75 | } | |
76 | push @$res, $info; | |
77 | } | |
78 | ||
79 | return $res; | |
80 | } | |
b66faa68 | 81 | |
157a946b DM |
82 | my $extract_email = sub { |
83 | my $data = shift; | |
84 | ||
85 | return $data if !$data; | |
86 | ||
87 | if ($data =~ m/^.*\s(\S+)\s*$/) { | |
88 | $data = $1; | |
89 | } | |
90 | ||
91 | if ($data =~ m/^<([^<>\s]+)>$/) { | |
92 | $data = $1; | |
93 | } | |
94 | ||
95 | if ($data !~ m/[\s><]/ && $data =~ m/^(.+\@[^\.]+\..*[^\.]+)$/) { | |
96 | $data = $1; | |
97 | } else { | |
98 | $data = undef; | |
99 | } | |
100 | ||
101 | return $data; | |
102 | }; | |
103 | ||
104 | my $get_real_sender = sub { | |
105 | my ($ref) = @_; | |
106 | ||
107 | my @lines = split('\n', $ref->{header}); | |
108 | my $head = Mail::Header->new(\@lines); | |
109 | ||
110 | my @fromarray = split ('\s*,\s*', $head->get ('from') || $ref->{sender}); | |
111 | my $from = $extract_email->($fromarray[0]) || $ref->{sender};; | |
112 | my $sender = $extract_email->($head->get ('sender')); | |
113 | ||
114 | return $sender if $sender; | |
115 | ||
116 | return $from; | |
117 | }; | |
118 | ||
dae021a8 DM |
119 | my $parse_header_info = sub { |
120 | my ($ref) = @_; | |
121 | ||
122 | my $res = { subject => '', from => '' }; | |
123 | ||
124 | my @lines = split('\n', $ref->{header}); | |
125 | my $head = Mail::Header->new(\@lines); | |
126 | ||
127 | $res->{subject} = PMG::Utils::decode_rfc1522(PVE::Tools::trim($head->get('subject'))) // ''; | |
128 | ||
129 | my @fromarray = split('\s*,\s*', $head->get('from') || $ref->{sender}); | |
130 | ||
131 | $res->{from} = PMG::Utils::decode_rfc1522(PVE::Tools::trim ($fromarray[0])) // ''; | |
132 | ||
133 | my $sender = PMG::Utils::decode_rfc1522(PVE::Tools::trim($head->get('sender'))); | |
1284c016 | 134 | $res->{sender} = $sender if $sender && ($sender ne $res->{from}); |
dae021a8 DM |
135 | |
136 | $res->{envelope_sender} = $ref->{sender}; | |
e84bf942 | 137 | $res->{receiver} = $ref->{receiver} // $ref->{pmail}; |
666b5e8f | 138 | $res->{id} = 'C' . $ref->{cid} . 'R' . $ref->{rid} . 'T' . $ref->{ticketid}; |
dae021a8 DM |
139 | $res->{time} = $ref->{time}; |
140 | $res->{bytes} = $ref->{bytes}; | |
141 | ||
1284c016 DM |
142 | my $qtype = $ref->{qtype}; |
143 | ||
144 | if ($qtype eq 'V') { | |
145 | $res->{virusname} = $ref->{info}; | |
2ad4def7 | 146 | $res->{spamlevel} = 0; |
1284c016 DM |
147 | } elsif ($qtype eq 'S') { |
148 | $res->{spamlevel} = $ref->{spamlevel} // 0; | |
1284c016 DM |
149 | } |
150 | ||
dae021a8 DM |
151 | return $res; |
152 | }; | |
153 | ||
157a946b DM |
154 | my $pmail_param_type = { |
155 | description => "List entries for the user with this primary email address. Quarantine users cannot speficy this parameter, but it is required for all other roles.", | |
156 | type => 'string', format => 'email', | |
157 | optional => 1, | |
158 | }; | |
6e8886d4 | 159 | |
b66faa68 DM |
160 | __PACKAGE__->register_method ({ |
161 | name => 'index', | |
162 | path => '', | |
163 | method => 'GET', | |
164 | permissions => { user => 'all' }, | |
165 | description => "Directory index.", | |
166 | parameters => { | |
167 | additionalProperties => 0, | |
168 | properties => {}, | |
169 | }, | |
170 | returns => { | |
171 | type => 'array', | |
172 | items => { | |
173 | type => "object", | |
174 | properties => {}, | |
175 | }, | |
176 | links => [ { rel => 'child', href => "{name}" } ], | |
177 | }, | |
178 | code => sub { | |
179 | my ($param) = @_; | |
180 | ||
181 | my $result = [ | |
157a946b DM |
182 | { name => 'whitelist' }, |
183 | { name => 'blacklist' }, | |
6e8886d4 | 184 | { name => 'content' }, |
b66faa68 | 185 | { name => 'spam' }, |
4f41cebc | 186 | { name => 'spamusers' }, |
ab02ba10 | 187 | { name => 'spamstatus' }, |
b66faa68 | 188 | { name => 'virus' }, |
c3246f47 | 189 | { name => 'virusstatus' }, |
091d8086 | 190 | { name => 'quarusers' }, |
b66faa68 DM |
191 | ]; |
192 | ||
193 | return $result; | |
194 | }}); | |
195 | ||
157a946b | 196 | |
767657cb DM |
197 | my $read_or_modify_user_bw_list = sub { |
198 | my ($listname, $param, $addrs, $delete) = @_; | |
157a946b DM |
199 | |
200 | my $rpcenv = PMG::RESTEnvironment->get(); | |
201 | my $authuser = $rpcenv->get_user(); | |
202 | my $role = $rpcenv->get_role(); | |
203 | ||
204 | my $pmail = $verify_optional_pmail->($authuser, $role, $param->{pmail}); | |
205 | ||
206 | my $dbh = PMG::DBTools::open_ruledb(); | |
207 | ||
767657cb DM |
208 | my $list = PMG::Quarantine::add_to_blackwhite( |
209 | $dbh, $pmail, $listname, $addrs, $delete); | |
157a946b DM |
210 | |
211 | my $res = []; | |
212 | foreach my $a (@$list) { push @$res, { address => $a }; } | |
213 | return $res; | |
214 | }; | |
215 | ||
216 | __PACKAGE__->register_method ({ | |
217 | name => 'whitelist', | |
218 | path => 'whitelist', | |
219 | method => 'GET', | |
220 | permissions => { check => [ 'admin', 'qmanager', 'audit', 'quser'] }, | |
221 | description => "Show user whitelist.", | |
222 | parameters => { | |
223 | additionalProperties => 0, | |
224 | properties => { | |
225 | pmail => $pmail_param_type, | |
226 | }, | |
227 | }, | |
228 | returns => { | |
229 | type => 'array', | |
230 | items => { | |
231 | type => "object", | |
232 | properties => { | |
233 | address => { | |
234 | type => "string", | |
235 | }, | |
236 | }, | |
237 | }, | |
238 | }, | |
239 | code => sub { | |
240 | my ($param) = @_; | |
241 | ||
767657cb DM |
242 | return $read_or_modify_user_bw_list->('WL', $param); |
243 | }}); | |
244 | ||
245 | __PACKAGE__->register_method ({ | |
246 | name => 'whitelist_add', | |
247 | path => 'whitelist', | |
248 | method => 'POST', | |
249 | description => "Add user whitelist entries.", | |
250 | permissions => { check => [ 'admin', 'qmanager', 'audit', 'quser'] }, | |
251 | protected => 1, | |
252 | parameters => { | |
253 | additionalProperties => 0, | |
254 | properties => { | |
255 | pmail => $pmail_param_type, | |
1a1bde13 | 256 | address => get_standard_option('pmg-whiteblacklist-entry-list', { |
767657cb | 257 | description => "The address you want to add.", |
01929101 | 258 | }), |
767657cb DM |
259 | }, |
260 | }, | |
261 | returns => { type => 'null' }, | |
262 | code => sub { | |
263 | my ($param) = @_; | |
264 | ||
1a1bde13 DC |
265 | my $addresses = [split(',', $param->{address})]; |
266 | $read_or_modify_user_bw_list->('WL', $param, $addresses); | |
767657cb DM |
267 | |
268 | return undef; | |
269 | }}); | |
270 | ||
271 | __PACKAGE__->register_method ({ | |
272 | name => 'whitelist_delete', | |
273 | path => 'whitelist/{address}', | |
274 | method => 'DELETE', | |
275 | description => "Delete user whitelist entries.", | |
276 | permissions => { check => [ 'admin', 'qmanager', 'audit', 'quser'] }, | |
277 | protected => 1, | |
278 | parameters => { | |
279 | additionalProperties => 0, | |
280 | properties => { | |
281 | pmail => $pmail_param_type, | |
1a1bde13 | 282 | address => get_standard_option('pmg-whiteblacklist-entry-list', { |
767657cb | 283 | description => "The address you want to remove.", |
01929101 | 284 | }), |
767657cb DM |
285 | }, |
286 | }, | |
287 | returns => { type => 'null' }, | |
288 | code => sub { | |
289 | my ($param) = @_; | |
290 | ||
1a1bde13 DC |
291 | my $addresses = [split(',', $param->{address})]; |
292 | $read_or_modify_user_bw_list->('WL', $param, $addresses, 1); | |
767657cb DM |
293 | |
294 | return undef; | |
157a946b DM |
295 | }}); |
296 | ||
297 | __PACKAGE__->register_method ({ | |
298 | name => 'blacklist', | |
299 | path => 'blacklist', | |
300 | method => 'GET', | |
301 | permissions => { check => [ 'admin', 'qmanager', 'audit', 'quser'] }, | |
302 | description => "Show user blacklist.", | |
303 | parameters => { | |
304 | additionalProperties => 0, | |
305 | properties => { | |
306 | pmail => $pmail_param_type, | |
307 | }, | |
308 | }, | |
309 | returns => { | |
310 | type => 'array', | |
311 | items => { | |
312 | type => "object", | |
313 | properties => { | |
314 | address => { | |
315 | type => "string", | |
316 | }, | |
317 | }, | |
318 | }, | |
319 | }, | |
320 | code => sub { | |
321 | my ($param) = @_; | |
322 | ||
767657cb DM |
323 | return $read_or_modify_user_bw_list->('BL', $param); |
324 | }}); | |
325 | ||
326 | __PACKAGE__->register_method ({ | |
327 | name => 'blacklist_add', | |
328 | path => 'blacklist', | |
329 | method => 'POST', | |
330 | description => "Add user blacklist entries.", | |
331 | permissions => { check => [ 'admin', 'qmanager', 'audit', 'quser'] }, | |
332 | protected => 1, | |
333 | parameters => { | |
334 | additionalProperties => 0, | |
335 | properties => { | |
336 | pmail => $pmail_param_type, | |
1a1bde13 | 337 | address => get_standard_option('pmg-whiteblacklist-entry-list', { |
767657cb | 338 | description => "The address you want to add.", |
01929101 | 339 | }), |
767657cb DM |
340 | }, |
341 | }, | |
342 | returns => { type => 'null' }, | |
343 | code => sub { | |
344 | my ($param) = @_; | |
345 | ||
1a1bde13 DC |
346 | my $addresses = [split(',', $param->{address})]; |
347 | $read_or_modify_user_bw_list->('BL', $param, $addresses); | |
767657cb DM |
348 | |
349 | return undef; | |
350 | }}); | |
351 | ||
352 | __PACKAGE__->register_method ({ | |
353 | name => 'blacklist_delete', | |
354 | path => 'blacklist/{address}', | |
355 | method => 'DELETE', | |
356 | description => "Delete user blacklist entries.", | |
357 | permissions => { check => [ 'admin', 'qmanager', 'audit', 'quser'] }, | |
358 | protected => 1, | |
359 | parameters => { | |
360 | additionalProperties => 0, | |
361 | properties => { | |
362 | pmail => $pmail_param_type, | |
1a1bde13 | 363 | address => get_standard_option('pmg-whiteblacklist-entry-list', { |
767657cb | 364 | description => "The address you want to remove.", |
01929101 | 365 | }), |
767657cb DM |
366 | }, |
367 | }, | |
368 | returns => { type => 'null' }, | |
369 | code => sub { | |
370 | my ($param) = @_; | |
371 | ||
1a1bde13 DC |
372 | my $addresses = [split(',', $param->{address})]; |
373 | $read_or_modify_user_bw_list->('BL', $param, $addresses, 1); | |
767657cb DM |
374 | |
375 | return undef; | |
157a946b DM |
376 | }}); |
377 | ||
4f41cebc DC |
378 | __PACKAGE__->register_method ({ |
379 | name => 'spamusers', | |
380 | path => 'spamusers', | |
381 | method => 'GET', | |
24a0be04 | 382 | permissions => { check => [ 'admin', 'qmanager', 'audit'] }, |
4f41cebc DC |
383 | description => "Get a list of receivers of spam in the given timespan (Default the last 24 hours).", |
384 | parameters => { | |
385 | additionalProperties => 0, | |
386 | properties => { | |
6214b5cf DM |
387 | starttime => get_standard_option('pmg-starttime'), |
388 | endtime => get_standard_option('pmg-endtime'), | |
4f41cebc DC |
389 | }, |
390 | }, | |
391 | returns => { | |
392 | type => 'array', | |
393 | items => { | |
394 | type => "object", | |
395 | properties => { | |
396 | mail => { | |
397 | description => 'the receiving email', | |
398 | type => 'string', | |
399 | }, | |
400 | }, | |
401 | }, | |
402 | }, | |
403 | code => sub { | |
404 | my ($param) = @_; | |
405 | ||
406 | my $rpcenv = PMG::RESTEnvironment->get(); | |
407 | my $authuser = $rpcenv->get_user(); | |
4f41cebc DC |
408 | |
409 | my $res = []; | |
410 | ||
411 | my $dbh = PMG::DBTools::open_ruledb(); | |
412 | ||
413 | my $start = $param->{starttime} // (time - 86400); | |
414 | my $end = $param->{endtime} // ($start + 86400); | |
415 | ||
416 | my $sth = $dbh->prepare( | |
417 | "SELECT DISTINCT pmail " . | |
418 | "FROM CMailStore, CMSReceivers WHERE " . | |
419 | "time >= $start AND time < $end AND " . | |
420 | "QType = 'S' AND CID = CMailStore_CID AND RID = CMailStore_RID " . | |
421 | "AND Status = 'N' ORDER BY pmail"); | |
422 | ||
423 | $sth->execute(); | |
424 | ||
425 | while (my $ref = $sth->fetchrow_hashref()) { | |
426 | push @$res, { mail => $ref->{pmail} }; | |
427 | } | |
428 | ||
429 | return $res; | |
430 | }}); | |
431 | ||
ab02ba10 DM |
432 | __PACKAGE__->register_method ({ |
433 | name => 'spamstatus', | |
434 | path => 'spamstatus', | |
435 | method => 'GET', | |
436 | permissions => { check => [ 'admin', 'qmanager', 'audit'] }, | |
437 | description => "Get Spam Quarantine Status", | |
438 | parameters => { | |
439 | additionalProperties => 0, | |
440 | properties => {}, | |
441 | }, | |
442 | returns => { | |
443 | type => "object", | |
444 | properties => { | |
445 | count => { | |
446 | description => 'Number of stored mails.', | |
447 | type => 'integer', | |
448 | }, | |
449 | mbytes => { | |
450 | description => "Estimated disk space usage in MByte.", | |
451 | type => 'number', | |
452 | }, | |
453 | avgbytes => { | |
454 | description => "Average size of stored mails in bytes.", | |
455 | type => 'number', | |
456 | }, | |
457 | avgspam => { | |
458 | description => "Average spam level.", | |
459 | type => 'number', | |
460 | }, | |
461 | }, | |
462 | }, | |
463 | code => sub { | |
464 | my ($param) = @_; | |
465 | ||
ab02ba10 DM |
466 | my $dbh = PMG::DBTools::open_ruledb(); |
467 | my $ref = PMG::DBTools::get_quarantine_count($dbh, 'S'); | |
468 | ||
469 | return $ref; | |
470 | }}); | |
471 | ||
9867e2da DM |
472 | __PACKAGE__->register_method ({ |
473 | name => 'quarusers', | |
474 | path => 'quarusers', | |
475 | method => 'GET', | |
476 | permissions => { check => [ 'admin', 'qmanager', 'audit'] }, | |
477 | description => "Get a list of users with whitelist/blacklist setttings.", | |
478 | parameters => { | |
479 | additionalProperties => 0, | |
480 | properties => {}, | |
481 | }, | |
482 | returns => { | |
483 | type => 'array', | |
484 | items => { | |
485 | type => "object", | |
486 | properties => { | |
487 | mail => { | |
488 | description => 'the receiving email', | |
489 | type => 'string', | |
490 | }, | |
491 | }, | |
492 | }, | |
493 | }, | |
494 | code => sub { | |
495 | my ($param) = @_; | |
496 | ||
497 | my $rpcenv = PMG::RESTEnvironment->get(); | |
498 | my $authuser = $rpcenv->get_user(); | |
9867e2da DM |
499 | |
500 | my $res = []; | |
501 | ||
502 | my $dbh = PMG::DBTools::open_ruledb(); | |
503 | ||
504 | my $sth = $dbh->prepare( | |
505 | "SELECT DISTINCT pmail FROM UserPrefs ORDER BY pmail"); | |
506 | ||
507 | $sth->execute(); | |
508 | ||
509 | while (my $ref = $sth->fetchrow_hashref()) { | |
510 | push @$res, { mail => $ref->{pmail} }; | |
511 | } | |
512 | ||
513 | return $res; | |
514 | }}); | |
515 | ||
ded33c7c | 516 | __PACKAGE__->register_method ({ |
83ce499f DC |
517 | name => 'spam', |
518 | path => 'spam', | |
ded33c7c DM |
519 | method => 'GET', |
520 | permissions => { check => [ 'admin', 'qmanager', 'audit', 'quser'] }, | |
83ce499f | 521 | description => "Get a list of quarantined spam mails in the given timeframe (default the last 24 hours) for the given user.", |
ded33c7c DM |
522 | parameters => { |
523 | additionalProperties => 0, | |
524 | properties => { | |
6214b5cf DM |
525 | starttime => get_standard_option('pmg-starttime'), |
526 | endtime => get_standard_option('pmg-endtime'), | |
157a946b | 527 | pmail => $pmail_param_type, |
ded33c7c DM |
528 | }, |
529 | }, | |
530 | returns => { | |
531 | type => 'array', | |
532 | items => { | |
533 | type => "object", | |
dae021a8 DM |
534 | properties => { |
535 | id => { | |
536 | description => 'Unique ID', | |
537 | type => 'string', | |
538 | }, | |
539 | bytes => { | |
540 | description => "Size of raw email.", | |
541 | type => 'integer' , | |
542 | }, | |
543 | envelope_sender => { | |
544 | description => "SMTP envelope sender.", | |
545 | type => 'string', | |
546 | }, | |
547 | from => { | |
548 | description => "Header 'From' field.", | |
549 | type => 'string', | |
550 | }, | |
551 | sender => { | |
552 | description => "Header 'Sender' field.", | |
553 | type => 'string', | |
554 | optional => 1, | |
555 | }, | |
556 | receiver => { | |
557 | description => "Receiver email address", | |
558 | type => 'string', | |
559 | }, | |
560 | subject => { | |
561 | description => "Header 'Subject' field.", | |
562 | type => 'string', | |
563 | }, | |
564 | time => { | |
565 | description => "Receive time stamp", | |
566 | type => 'integer', | |
567 | }, | |
1284c016 DM |
568 | spamlevel => { |
569 | description => "Spam score.", | |
570 | type => 'number', | |
571 | }, | |
dae021a8 | 572 | }, |
ded33c7c DM |
573 | }, |
574 | }, | |
575 | code => sub { | |
576 | my ($param) = @_; | |
577 | ||
578 | my $rpcenv = PMG::RESTEnvironment->get(); | |
579 | my $authuser = $rpcenv->get_user(); | |
580 | my $role = $rpcenv->get_role(); | |
581 | ||
157a946b | 582 | my $pmail = $verify_optional_pmail->($authuser, $role, $param->{pmail}); |
b66faa68 | 583 | |
ded33c7c DM |
584 | my $res = []; |
585 | ||
586 | my $dbh = PMG::DBTools::open_ruledb(); | |
587 | ||
83ce499f | 588 | my $start = $param->{starttime} // (time - 86400); |
ded33c7c DM |
589 | my $end = $param->{endtime} // ($start + 86400); |
590 | ||
591 | my $sth = $dbh->prepare( | |
592 | "SELECT * " . | |
593 | "FROM CMailStore, CMSReceivers WHERE " . | |
594 | "pmail = ? AND time >= $start AND time < $end AND " . | |
595 | "QType = 'S' AND CID = CMailStore_CID AND RID = CMailStore_RID " . | |
596 | "AND Status = 'N' ORDER BY pmail, time, receiver"); | |
597 | ||
598 | $sth->execute($pmail); | |
599 | ||
b66faa68 | 600 | while (my $ref = $sth->fetchrow_hashref()) { |
dae021a8 DM |
601 | my $data = $parse_header_info->($ref); |
602 | push @$res, $data; | |
b66faa68 DM |
603 | } |
604 | ||
605 | return $res; | |
606 | }}); | |
607 | ||
bb01dcf8 DM |
608 | __PACKAGE__->register_method ({ |
609 | name => 'virus', | |
610 | path => 'virus', | |
611 | method => 'GET', | |
612 | permissions => { check => [ 'admin', 'qmanager', 'audit' ] }, | |
613 | description => "Get a list of quarantined virus mails in the given timeframe (default the last 24 hours).", | |
614 | parameters => { | |
615 | additionalProperties => 0, | |
616 | properties => { | |
6214b5cf DM |
617 | starttime => get_standard_option('pmg-starttime'), |
618 | endtime => get_standard_option('pmg-endtime'), | |
bb01dcf8 DM |
619 | }, |
620 | }, | |
621 | returns => { | |
622 | type => 'array', | |
623 | items => { | |
624 | type => "object", | |
625 | properties => { | |
626 | id => { | |
627 | description => 'Unique ID', | |
628 | type => 'string', | |
629 | }, | |
630 | bytes => { | |
631 | description => "Size of raw email.", | |
632 | type => 'integer' , | |
633 | }, | |
634 | envelope_sender => { | |
635 | description => "SMTP envelope sender.", | |
636 | type => 'string', | |
637 | }, | |
638 | from => { | |
639 | description => "Header 'From' field.", | |
640 | type => 'string', | |
641 | }, | |
642 | sender => { | |
643 | description => "Header 'Sender' field.", | |
644 | type => 'string', | |
645 | optional => 1, | |
646 | }, | |
647 | receiver => { | |
648 | description => "Receiver email address", | |
649 | type => 'string', | |
650 | }, | |
651 | subject => { | |
652 | description => "Header 'Subject' field.", | |
653 | type => 'string', | |
654 | }, | |
655 | time => { | |
656 | description => "Receive time stamp", | |
657 | type => 'integer', | |
658 | }, | |
659 | virusname => { | |
660 | description => "Virus name.", | |
661 | type => 'string', | |
662 | }, | |
663 | }, | |
664 | }, | |
665 | }, | |
666 | code => sub { | |
667 | my ($param) = @_; | |
668 | ||
669 | my $rpcenv = PMG::RESTEnvironment->get(); | |
670 | my $authuser = $rpcenv->get_user(); | |
bb01dcf8 DM |
671 | |
672 | my $res = []; | |
673 | ||
674 | my $dbh = PMG::DBTools::open_ruledb(); | |
675 | ||
676 | my $start = $param->{starttime} // (time - 86400); | |
677 | my $end = $param->{endtime} // ($start + 86400); | |
678 | ||
679 | my $sth = $dbh->prepare( | |
680 | "SELECT * " . | |
681 | "FROM CMailStore, CMSReceivers WHERE " . | |
682 | "time >= $start AND time < $end AND " . | |
683 | "QType = 'V' AND CID = CMailStore_CID AND RID = CMailStore_RID " . | |
684 | "AND Status = 'N' ORDER BY time, receiver"); | |
685 | ||
686 | $sth->execute(); | |
687 | ||
688 | while (my $ref = $sth->fetchrow_hashref()) { | |
689 | my $data = $parse_header_info->($ref); | |
690 | push @$res, $data; | |
691 | } | |
692 | ||
693 | return $res; | |
694 | }}); | |
695 | ||
c3246f47 DM |
696 | __PACKAGE__->register_method ({ |
697 | name => 'virusstatus', | |
698 | path => 'virusstatus', | |
699 | method => 'GET', | |
700 | permissions => { check => [ 'admin', 'qmanager', 'audit'] }, | |
701 | description => "Get Virus Quarantine Status", | |
702 | parameters => { | |
703 | additionalProperties => 0, | |
704 | properties => {}, | |
705 | }, | |
706 | returns => { | |
707 | type => "object", | |
708 | properties => { | |
709 | count => { | |
710 | description => 'Number of stored mails.', | |
711 | type => 'integer', | |
712 | }, | |
713 | mbytes => { | |
714 | description => "Estimated disk space usage in MByte.", | |
715 | type => 'number', | |
716 | }, | |
717 | avgbytes => { | |
718 | description => "Average size of stored mails in bytes.", | |
719 | type => 'number', | |
720 | }, | |
721 | }, | |
722 | }, | |
723 | code => sub { | |
724 | my ($param) = @_; | |
725 | ||
726 | my $dbh = PMG::DBTools::open_ruledb(); | |
727 | my $ref = PMG::DBTools::get_quarantine_count($dbh, 'V'); | |
728 | ||
f7fa880f DM |
729 | delete $ref->{avgspam}; |
730 | ||
c3246f47 DM |
731 | return $ref; |
732 | }}); | |
733 | ||
6e8886d4 DM |
734 | __PACKAGE__->register_method ({ |
735 | name => 'content', | |
736 | path => 'content', | |
737 | method => 'GET', | |
738 | permissions => { check => [ 'admin', 'qmanager', 'audit', 'quser'] }, | |
34db0c3f | 739 | description => "Get email data. There is a special formatter called 'htmlmail' to get sanitized html view of the mail content (use the '/api2/htmlmail/quarantine/content' url).", |
6e8886d4 DM |
740 | parameters => { |
741 | additionalProperties => 0, | |
742 | properties => { | |
743 | id => { | |
744 | description => 'Unique ID', | |
745 | type => 'string', | |
666b5e8f DM |
746 | pattern => 'C\d+R\d+T\d+', |
747 | maxLength => 60, | |
6e8886d4 | 748 | }, |
34db0c3f | 749 | raw => { |
c81cf188 | 750 | description => "Display 'raw' eml data. Deactivates size limit.", |
34db0c3f DM |
751 | type => 'boolean', |
752 | optional => 1, | |
753 | default => 0, | |
754 | }, | |
6e8886d4 DM |
755 | }, |
756 | }, | |
757 | returns => { | |
758 | type => "object", | |
cd31bb45 DM |
759 | properties => { |
760 | id => { | |
761 | description => 'Unique ID', | |
762 | type => 'string', | |
763 | }, | |
764 | bytes => { | |
765 | description => "Size of raw email.", | |
766 | type => 'integer' , | |
767 | }, | |
768 | envelope_sender => { | |
769 | description => "SMTP envelope sender.", | |
770 | type => 'string', | |
771 | }, | |
772 | from => { | |
773 | description => "Header 'From' field.", | |
774 | type => 'string', | |
775 | }, | |
776 | sender => { | |
777 | description => "Header 'Sender' field.", | |
778 | type => 'string', | |
779 | optional => 1, | |
780 | }, | |
781 | receiver => { | |
782 | description => "Receiver email address", | |
783 | type => 'string', | |
784 | }, | |
785 | subject => { | |
786 | description => "Header 'Subject' field.", | |
787 | type => 'string', | |
788 | }, | |
789 | time => { | |
790 | description => "Receive time stamp", | |
791 | type => 'integer', | |
792 | }, | |
793 | spamlevel => { | |
794 | description => "Spam score.", | |
795 | type => 'number', | |
796 | }, | |
797 | spaminfo => { | |
798 | description => "Information about matched spam tests (name, score, desc, url).", | |
799 | type => 'array', | |
800 | }, | |
801 | header => { | |
802 | description => "Raw email header data.", | |
803 | type => 'string', | |
804 | }, | |
805 | content => { | |
806 | description => "Raw email data (first 4096 bytes). Useful for preview. NOTE: The 'htmlmail' formatter displays the whole email.", | |
807 | type => 'string', | |
6eac8473 | 808 | }, |
cd31bb45 | 809 | }, |
6e8886d4 DM |
810 | }, |
811 | code => sub { | |
812 | my ($param) = @_; | |
813 | ||
814 | my $rpcenv = PMG::RESTEnvironment->get(); | |
815 | my $authuser = $rpcenv->get_user(); | |
816 | my $role = $rpcenv->get_role(); | |
34db0c3f | 817 | my $format = $rpcenv->get_format(); |
6e8886d4 | 818 | |
c81cf188 DC |
819 | my $raw = $param->{raw} // 0; |
820 | ||
666b5e8f | 821 | my ($cid, $rid, $tid) = $param->{id} =~ m/^C(\d+)R(\d+)T(\d+)$/; |
6e8886d4 DM |
822 | $cid = int($cid); |
823 | $rid = int($rid); | |
666b5e8f | 824 | $tid = int($tid); |
9efdabf0 | 825 | |
6e8886d4 DM |
826 | my $dbh = PMG::DBTools::open_ruledb(); |
827 | ||
666b5e8f | 828 | my $ref = PMG::DBTools::load_mail_data($dbh, $cid, $rid, $tid); |
6e8886d4 DM |
829 | |
830 | if ($role eq 'quser') { | |
6060e345 | 831 | my $quar_username = $ref->{pmail} . '@quarantine'; |
666b5e8f | 832 | raise_perm_exc("mail does not belong to user '$authuser' ($ref->{pmail})") |
6060e345 | 833 | if $authuser ne $quar_username; |
6e8886d4 DM |
834 | } |
835 | ||
836 | my $res = $parse_header_info->($ref); | |
837 | ||
6e8886d4 DM |
838 | my $filename = $ref->{file}; |
839 | my $spooldir = $PMG::MailQueue::spooldir; | |
840 | ||
841 | my $path = "$spooldir/$filename"; | |
842 | ||
34db0c3f DM |
843 | if ($format eq 'htmlmail') { |
844 | ||
845 | my $cfg = PMG::Config->new(); | |
846 | my $viewimages = $cfg->get('spamquar', 'viewimages'); | |
847 | my $allowhref = $cfg->get('spamquar', 'allowhrefs'); | |
848 | ||
c81cf188 | 849 | $res->{content} = PMG::HTMLMail::email_to_html($path, $raw, $viewimages, $allowhref); |
34db0c3f | 850 | |
157a946b DM |
851 | # to make result verification happy |
852 | $res->{file} = ''; | |
853 | $res->{header} = ''; | |
2ad4def7 | 854 | $res->{spamlevel} = 0; |
157a946b | 855 | $res->{spaminfo} = []; |
34db0c3f | 856 | } else { |
cd31bb45 | 857 | # include additional details |
34db0c3f | 858 | |
c81cf188 DC |
859 | # we want to get the whole email in raw mode |
860 | my $maxbytes = (!$raw)? 4096 : undef; | |
861 | ||
862 | my ($header, $content) = PMG::HTMLMail::read_raw_email($path, $maxbytes); | |
157a946b | 863 | |
cd31bb45 DM |
864 | $res->{file} = $ref->{file}; |
865 | $res->{spaminfo} = decode_spaminfo($ref->{info}); | |
34db0c3f DM |
866 | $res->{header} = $header; |
867 | $res->{content} = $content; | |
868 | } | |
6e8886d4 | 869 | |
6e8886d4 DM |
870 | |
871 | return $res; | |
872 | ||
873 | }}); | |
874 | ||
34db0c3f DM |
875 | PVE::APIServer::Formatter::register_page_formatter( |
876 | 'format' => 'htmlmail', | |
877 | method => 'GET', | |
878 | path => '/quarantine/content', | |
879 | code => sub { | |
880 | my ($res, $data, $param, $path, $auth, $config) = @_; | |
881 | ||
882 | if(!HTTP::Status::is_success($res->{status})) { | |
883 | return ("Error $res->{status}: $res->{message}", "text/plain"); | |
884 | } | |
885 | ||
886 | my $ct = "text/html;charset=UTF-8"; | |
887 | ||
888 | my $raw = $data->{content}; | |
889 | ||
890 | return (encode('UTF-8', $raw), $ct, 1); | |
891 | }); | |
892 | ||
157a946b DM |
893 | __PACKAGE__->register_method ({ |
894 | name =>'action', | |
895 | path => 'content', | |
896 | method => 'POST', | |
897 | description => "Execute quarantine actions.", | |
898 | permissions => { check => [ 'admin', 'qmanager', 'quser'] }, | |
899 | protected => 1, | |
900 | parameters => { | |
901 | additionalProperties => 0, | |
902 | properties => { | |
903 | id => { | |
b7894d7c | 904 | description => 'Unique IDs, seperate with ;', |
157a946b | 905 | type => 'string', |
b7894d7c | 906 | pattern => 'C\d+R\d+T\d+(;C\d+R\d+T\d+)*', |
157a946b DM |
907 | }, |
908 | action => { | |
909 | description => 'Action - specify what you want to do with the mail.', | |
910 | type => 'string', | |
911 | enum => ['whitelist', 'blacklist', 'deliver', 'delete'], | |
912 | }, | |
913 | }, | |
914 | }, | |
915 | returns => { type => "null" }, | |
916 | code => sub { | |
917 | my ($param) = @_; | |
918 | ||
919 | my $rpcenv = PMG::RESTEnvironment->get(); | |
920 | my $authuser = $rpcenv->get_user(); | |
921 | my $role = $rpcenv->get_role(); | |
922 | my $action = $param->{action}; | |
b7894d7c | 923 | my @idlist = split(';', $param->{id}); |
157a946b | 924 | |
e18901ef DM |
925 | my $dbh = PMG::DBTools::open_ruledb(); |
926 | ||
b7894d7c DC |
927 | for my $id (@idlist) { |
928 | my ($cid, $rid, $tid) = $id =~ m/^C(\d+)R(\d+)T(\d+)$/; | |
929 | $cid = int($cid); | |
930 | $rid = int($rid); | |
931 | $tid = int($tid); | |
8f34e0b1 | 932 | |
b7894d7c | 933 | my $ref = PMG::DBTools::load_mail_data($dbh, $cid, $rid, $tid); |
157a946b | 934 | |
b7894d7c DC |
935 | if ($role eq 'quser') { |
936 | my $quar_username = $ref->{pmail} . '@quarantine'; | |
937 | raise_perm_exc("mail does not belong to user '$authuser' ($ref->{pmail})") | |
6060e345 | 938 | if $authuser ne $quar_username; |
b7894d7c DC |
939 | } |
940 | ||
941 | my $sender = $get_real_sender->($ref); | |
942 | ||
943 | if ($action eq 'whitelist') { | |
944 | PMG::Quarantine::add_to_blackwhite($dbh, $ref->{pmail}, 'WL', [ $sender ]); | |
945 | } elsif ($action eq 'blacklist') { | |
946 | PMG::Quarantine::add_to_blackwhite($dbh, $ref->{pmail}, 'BL', [ $sender ]); | |
947 | } elsif ($action eq 'deliver') { | |
948 | PMG::Quarantine::deliver_quarantined_mail($dbh, $ref, $ref->{receiver} // $ref->{pmail}); | |
949 | } elsif ($action eq 'delete') { | |
950 | PMG::Quarantine::delete_quarantined_mail($dbh, $ref); | |
951 | } else { | |
952 | die "internal error"; # should not be reached | |
953 | } | |
157a946b DM |
954 | } |
955 | ||
956 | return undef; | |
957 | }}); | |
e84bf942 | 958 | |
b66faa68 | 959 | 1; |