]>
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 | ||
21752ddf ML |
60 | my $res = []; |
61 | return $res if !defined($info); | |
62 | ||
e325aa6f DC |
63 | my $saversion = Mail::SpamAssassin->VERSION; |
64 | ||
65 | my $salocaldir = "/var/lib/spamassassin/$saversion/updates_spamassassin_org"; | |
66 | ||
67 | $spamdesc = PMG::Utils::load_sa_descriptions([$salocaldir]) if !$spamdesc; | |
1284c016 | 68 | |
1284c016 DM |
69 | foreach my $test (split (',', $info)) { |
70 | my ($name, $score) = split (':', $test); | |
71 | ||
273c538f | 72 | my $info = { name => $name, score => $score + 0, desc => '-' }; |
1284c016 DM |
73 | if (my $si = $spamdesc->{$name}) { |
74 | $info->{desc} = $si->{desc}; | |
75 | $info->{url} = $si->{url} if defined($si->{url}); | |
76 | } | |
77 | push @$res, $info; | |
78 | } | |
79 | ||
80 | return $res; | |
81 | } | |
b66faa68 | 82 | |
157a946b DM |
83 | my $extract_email = sub { |
84 | my $data = shift; | |
85 | ||
86 | return $data if !$data; | |
87 | ||
88 | if ($data =~ m/^.*\s(\S+)\s*$/) { | |
89 | $data = $1; | |
90 | } | |
91 | ||
92 | if ($data =~ m/^<([^<>\s]+)>$/) { | |
93 | $data = $1; | |
94 | } | |
95 | ||
96 | if ($data !~ m/[\s><]/ && $data =~ m/^(.+\@[^\.]+\..*[^\.]+)$/) { | |
97 | $data = $1; | |
98 | } else { | |
99 | $data = undef; | |
100 | } | |
101 | ||
102 | return $data; | |
103 | }; | |
104 | ||
105 | my $get_real_sender = sub { | |
106 | my ($ref) = @_; | |
107 | ||
108 | my @lines = split('\n', $ref->{header}); | |
109 | my $head = Mail::Header->new(\@lines); | |
110 | ||
111 | my @fromarray = split ('\s*,\s*', $head->get ('from') || $ref->{sender}); | |
112 | my $from = $extract_email->($fromarray[0]) || $ref->{sender};; | |
113 | my $sender = $extract_email->($head->get ('sender')); | |
114 | ||
115 | return $sender if $sender; | |
116 | ||
117 | return $from; | |
118 | }; | |
119 | ||
dae021a8 DM |
120 | my $parse_header_info = sub { |
121 | my ($ref) = @_; | |
122 | ||
123 | my $res = { subject => '', from => '' }; | |
124 | ||
125 | my @lines = split('\n', $ref->{header}); | |
126 | my $head = Mail::Header->new(\@lines); | |
127 | ||
128 | $res->{subject} = PMG::Utils::decode_rfc1522(PVE::Tools::trim($head->get('subject'))) // ''; | |
129 | ||
130 | my @fromarray = split('\s*,\s*', $head->get('from') || $ref->{sender}); | |
131 | ||
132 | $res->{from} = PMG::Utils::decode_rfc1522(PVE::Tools::trim ($fromarray[0])) // ''; | |
133 | ||
134 | my $sender = PMG::Utils::decode_rfc1522(PVE::Tools::trim($head->get('sender'))); | |
1284c016 | 135 | $res->{sender} = $sender if $sender && ($sender ne $res->{from}); |
dae021a8 DM |
136 | |
137 | $res->{envelope_sender} = $ref->{sender}; | |
e84bf942 | 138 | $res->{receiver} = $ref->{receiver} // $ref->{pmail}; |
666b5e8f | 139 | $res->{id} = 'C' . $ref->{cid} . 'R' . $ref->{rid} . 'T' . $ref->{ticketid}; |
dae021a8 DM |
140 | $res->{time} = $ref->{time}; |
141 | $res->{bytes} = $ref->{bytes}; | |
142 | ||
1284c016 DM |
143 | my $qtype = $ref->{qtype}; |
144 | ||
145 | if ($qtype eq 'V') { | |
146 | $res->{virusname} = $ref->{info}; | |
2ad4def7 | 147 | $res->{spamlevel} = 0; |
1284c016 DM |
148 | } elsif ($qtype eq 'S') { |
149 | $res->{spamlevel} = $ref->{spamlevel} // 0; | |
1284c016 DM |
150 | } |
151 | ||
dae021a8 DM |
152 | return $res; |
153 | }; | |
154 | ||
7b6ff2ee | 155 | my $pmail_param_type = get_standard_option('pmg-email-address', { |
157a946b | 156 | 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.", |
157a946b | 157 | optional => 1, |
7b6ff2ee | 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, | |
648954c6 DC |
480 | properties => { |
481 | list => { | |
482 | type => 'string', | |
483 | description => 'If set, limits the result to the given list.', | |
484 | enum => ['BL', 'WL'], | |
485 | optional => 1, | |
486 | }, | |
487 | }, | |
9867e2da DM |
488 | }, |
489 | returns => { | |
490 | type => 'array', | |
491 | items => { | |
492 | type => "object", | |
493 | properties => { | |
494 | mail => { | |
495 | description => 'the receiving email', | |
496 | type => 'string', | |
497 | }, | |
498 | }, | |
499 | }, | |
500 | }, | |
501 | code => sub { | |
502 | my ($param) = @_; | |
503 | ||
504 | my $rpcenv = PMG::RESTEnvironment->get(); | |
505 | my $authuser = $rpcenv->get_user(); | |
9867e2da DM |
506 | |
507 | my $res = []; | |
508 | ||
509 | my $dbh = PMG::DBTools::open_ruledb(); | |
510 | ||
648954c6 DC |
511 | my $sth; |
512 | if ($param->{list}) { | |
513 | $sth = $dbh->prepare("SELECT DISTINCT pmail FROM UserPrefs WHERE name = ? ORDER BY pmail"); | |
514 | $sth->execute($param->{list}); | |
515 | } else { | |
516 | $sth = $dbh->prepare("SELECT DISTINCT pmail FROM UserPrefs ORDER BY pmail"); | |
517 | $sth->execute(); | |
518 | } | |
9867e2da DM |
519 | |
520 | while (my $ref = $sth->fetchrow_hashref()) { | |
521 | push @$res, { mail => $ref->{pmail} }; | |
522 | } | |
523 | ||
524 | return $res; | |
525 | }}); | |
526 | ||
ded33c7c | 527 | __PACKAGE__->register_method ({ |
83ce499f DC |
528 | name => 'spam', |
529 | path => 'spam', | |
ded33c7c DM |
530 | method => 'GET', |
531 | permissions => { check => [ 'admin', 'qmanager', 'audit', 'quser'] }, | |
83ce499f | 532 | description => "Get a list of quarantined spam mails in the given timeframe (default the last 24 hours) for the given user.", |
ded33c7c DM |
533 | parameters => { |
534 | additionalProperties => 0, | |
535 | properties => { | |
6214b5cf DM |
536 | starttime => get_standard_option('pmg-starttime'), |
537 | endtime => get_standard_option('pmg-endtime'), | |
157a946b | 538 | pmail => $pmail_param_type, |
ded33c7c DM |
539 | }, |
540 | }, | |
541 | returns => { | |
542 | type => 'array', | |
543 | items => { | |
544 | type => "object", | |
dae021a8 DM |
545 | properties => { |
546 | id => { | |
547 | description => 'Unique ID', | |
548 | type => 'string', | |
549 | }, | |
550 | bytes => { | |
551 | description => "Size of raw email.", | |
552 | type => 'integer' , | |
553 | }, | |
554 | envelope_sender => { | |
555 | description => "SMTP envelope sender.", | |
556 | type => 'string', | |
557 | }, | |
558 | from => { | |
559 | description => "Header 'From' field.", | |
560 | type => 'string', | |
561 | }, | |
562 | sender => { | |
563 | description => "Header 'Sender' field.", | |
564 | type => 'string', | |
565 | optional => 1, | |
566 | }, | |
567 | receiver => { | |
568 | description => "Receiver email address", | |
569 | type => 'string', | |
570 | }, | |
571 | subject => { | |
572 | description => "Header 'Subject' field.", | |
573 | type => 'string', | |
574 | }, | |
575 | time => { | |
576 | description => "Receive time stamp", | |
577 | type => 'integer', | |
578 | }, | |
1284c016 DM |
579 | spamlevel => { |
580 | description => "Spam score.", | |
581 | type => 'number', | |
582 | }, | |
dae021a8 | 583 | }, |
ded33c7c DM |
584 | }, |
585 | }, | |
586 | code => sub { | |
587 | my ($param) = @_; | |
588 | ||
589 | my $rpcenv = PMG::RESTEnvironment->get(); | |
590 | my $authuser = $rpcenv->get_user(); | |
591 | my $role = $rpcenv->get_role(); | |
592 | ||
157a946b | 593 | my $pmail = $verify_optional_pmail->($authuser, $role, $param->{pmail}); |
b66faa68 | 594 | |
ded33c7c DM |
595 | my $res = []; |
596 | ||
597 | my $dbh = PMG::DBTools::open_ruledb(); | |
598 | ||
83ce499f | 599 | my $start = $param->{starttime} // (time - 86400); |
ded33c7c DM |
600 | my $end = $param->{endtime} // ($start + 86400); |
601 | ||
602 | my $sth = $dbh->prepare( | |
603 | "SELECT * " . | |
604 | "FROM CMailStore, CMSReceivers WHERE " . | |
605 | "pmail = ? AND time >= $start AND time < $end AND " . | |
606 | "QType = 'S' AND CID = CMailStore_CID AND RID = CMailStore_RID " . | |
607 | "AND Status = 'N' ORDER BY pmail, time, receiver"); | |
608 | ||
609 | $sth->execute($pmail); | |
610 | ||
b66faa68 | 611 | while (my $ref = $sth->fetchrow_hashref()) { |
dae021a8 DM |
612 | my $data = $parse_header_info->($ref); |
613 | push @$res, $data; | |
b66faa68 DM |
614 | } |
615 | ||
616 | return $res; | |
617 | }}); | |
618 | ||
bb01dcf8 DM |
619 | __PACKAGE__->register_method ({ |
620 | name => 'virus', | |
621 | path => 'virus', | |
622 | method => 'GET', | |
623 | permissions => { check => [ 'admin', 'qmanager', 'audit' ] }, | |
624 | description => "Get a list of quarantined virus mails in the given timeframe (default the last 24 hours).", | |
625 | parameters => { | |
626 | additionalProperties => 0, | |
627 | properties => { | |
6214b5cf DM |
628 | starttime => get_standard_option('pmg-starttime'), |
629 | endtime => get_standard_option('pmg-endtime'), | |
bb01dcf8 DM |
630 | }, |
631 | }, | |
632 | returns => { | |
633 | type => 'array', | |
634 | items => { | |
635 | type => "object", | |
636 | properties => { | |
637 | id => { | |
638 | description => 'Unique ID', | |
639 | type => 'string', | |
640 | }, | |
641 | bytes => { | |
642 | description => "Size of raw email.", | |
643 | type => 'integer' , | |
644 | }, | |
645 | envelope_sender => { | |
646 | description => "SMTP envelope sender.", | |
647 | type => 'string', | |
648 | }, | |
649 | from => { | |
650 | description => "Header 'From' field.", | |
651 | type => 'string', | |
652 | }, | |
653 | sender => { | |
654 | description => "Header 'Sender' field.", | |
655 | type => 'string', | |
656 | optional => 1, | |
657 | }, | |
658 | receiver => { | |
659 | description => "Receiver email address", | |
660 | type => 'string', | |
661 | }, | |
662 | subject => { | |
663 | description => "Header 'Subject' field.", | |
664 | type => 'string', | |
665 | }, | |
666 | time => { | |
667 | description => "Receive time stamp", | |
668 | type => 'integer', | |
669 | }, | |
670 | virusname => { | |
671 | description => "Virus name.", | |
672 | type => 'string', | |
673 | }, | |
674 | }, | |
675 | }, | |
676 | }, | |
677 | code => sub { | |
678 | my ($param) = @_; | |
679 | ||
680 | my $rpcenv = PMG::RESTEnvironment->get(); | |
681 | my $authuser = $rpcenv->get_user(); | |
bb01dcf8 DM |
682 | |
683 | my $res = []; | |
684 | ||
685 | my $dbh = PMG::DBTools::open_ruledb(); | |
686 | ||
687 | my $start = $param->{starttime} // (time - 86400); | |
688 | my $end = $param->{endtime} // ($start + 86400); | |
689 | ||
690 | my $sth = $dbh->prepare( | |
691 | "SELECT * " . | |
692 | "FROM CMailStore, CMSReceivers WHERE " . | |
693 | "time >= $start AND time < $end AND " . | |
694 | "QType = 'V' AND CID = CMailStore_CID AND RID = CMailStore_RID " . | |
695 | "AND Status = 'N' ORDER BY time, receiver"); | |
696 | ||
697 | $sth->execute(); | |
698 | ||
699 | while (my $ref = $sth->fetchrow_hashref()) { | |
700 | my $data = $parse_header_info->($ref); | |
701 | push @$res, $data; | |
702 | } | |
703 | ||
704 | return $res; | |
705 | }}); | |
706 | ||
c3246f47 DM |
707 | __PACKAGE__->register_method ({ |
708 | name => 'virusstatus', | |
709 | path => 'virusstatus', | |
710 | method => 'GET', | |
711 | permissions => { check => [ 'admin', 'qmanager', 'audit'] }, | |
712 | description => "Get Virus Quarantine Status", | |
713 | parameters => { | |
714 | additionalProperties => 0, | |
715 | properties => {}, | |
716 | }, | |
717 | returns => { | |
718 | type => "object", | |
719 | properties => { | |
720 | count => { | |
721 | description => 'Number of stored mails.', | |
722 | type => 'integer', | |
723 | }, | |
724 | mbytes => { | |
725 | description => "Estimated disk space usage in MByte.", | |
726 | type => 'number', | |
727 | }, | |
728 | avgbytes => { | |
729 | description => "Average size of stored mails in bytes.", | |
730 | type => 'number', | |
731 | }, | |
732 | }, | |
733 | }, | |
734 | code => sub { | |
735 | my ($param) = @_; | |
736 | ||
737 | my $dbh = PMG::DBTools::open_ruledb(); | |
738 | my $ref = PMG::DBTools::get_quarantine_count($dbh, 'V'); | |
739 | ||
f7fa880f DM |
740 | delete $ref->{avgspam}; |
741 | ||
c3246f47 DM |
742 | return $ref; |
743 | }}); | |
744 | ||
6e8886d4 DM |
745 | __PACKAGE__->register_method ({ |
746 | name => 'content', | |
747 | path => 'content', | |
748 | method => 'GET', | |
749 | permissions => { check => [ 'admin', 'qmanager', 'audit', 'quser'] }, | |
34db0c3f | 750 | 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 |
751 | parameters => { |
752 | additionalProperties => 0, | |
753 | properties => { | |
754 | id => { | |
755 | description => 'Unique ID', | |
756 | type => 'string', | |
666b5e8f DM |
757 | pattern => 'C\d+R\d+T\d+', |
758 | maxLength => 60, | |
6e8886d4 | 759 | }, |
34db0c3f | 760 | raw => { |
c81cf188 | 761 | description => "Display 'raw' eml data. Deactivates size limit.", |
34db0c3f DM |
762 | type => 'boolean', |
763 | optional => 1, | |
764 | default => 0, | |
765 | }, | |
6e8886d4 DM |
766 | }, |
767 | }, | |
768 | returns => { | |
769 | type => "object", | |
cd31bb45 DM |
770 | properties => { |
771 | id => { | |
772 | description => 'Unique ID', | |
773 | type => 'string', | |
774 | }, | |
775 | bytes => { | |
776 | description => "Size of raw email.", | |
777 | type => 'integer' , | |
778 | }, | |
779 | envelope_sender => { | |
780 | description => "SMTP envelope sender.", | |
781 | type => 'string', | |
782 | }, | |
783 | from => { | |
784 | description => "Header 'From' field.", | |
785 | type => 'string', | |
786 | }, | |
787 | sender => { | |
788 | description => "Header 'Sender' field.", | |
789 | type => 'string', | |
790 | optional => 1, | |
791 | }, | |
792 | receiver => { | |
793 | description => "Receiver email address", | |
794 | type => 'string', | |
795 | }, | |
796 | subject => { | |
797 | description => "Header 'Subject' field.", | |
798 | type => 'string', | |
799 | }, | |
800 | time => { | |
801 | description => "Receive time stamp", | |
802 | type => 'integer', | |
803 | }, | |
804 | spamlevel => { | |
805 | description => "Spam score.", | |
806 | type => 'number', | |
807 | }, | |
808 | spaminfo => { | |
809 | description => "Information about matched spam tests (name, score, desc, url).", | |
810 | type => 'array', | |
811 | }, | |
812 | header => { | |
813 | description => "Raw email header data.", | |
814 | type => 'string', | |
815 | }, | |
816 | content => { | |
817 | description => "Raw email data (first 4096 bytes). Useful for preview. NOTE: The 'htmlmail' formatter displays the whole email.", | |
818 | type => 'string', | |
6eac8473 | 819 | }, |
cd31bb45 | 820 | }, |
6e8886d4 DM |
821 | }, |
822 | code => sub { | |
823 | my ($param) = @_; | |
824 | ||
825 | my $rpcenv = PMG::RESTEnvironment->get(); | |
826 | my $authuser = $rpcenv->get_user(); | |
827 | my $role = $rpcenv->get_role(); | |
34db0c3f | 828 | my $format = $rpcenv->get_format(); |
6e8886d4 | 829 | |
c81cf188 DC |
830 | my $raw = $param->{raw} // 0; |
831 | ||
666b5e8f | 832 | my ($cid, $rid, $tid) = $param->{id} =~ m/^C(\d+)R(\d+)T(\d+)$/; |
6e8886d4 DM |
833 | $cid = int($cid); |
834 | $rid = int($rid); | |
666b5e8f | 835 | $tid = int($tid); |
9efdabf0 | 836 | |
6e8886d4 DM |
837 | my $dbh = PMG::DBTools::open_ruledb(); |
838 | ||
666b5e8f | 839 | my $ref = PMG::DBTools::load_mail_data($dbh, $cid, $rid, $tid); |
6e8886d4 DM |
840 | |
841 | if ($role eq 'quser') { | |
6060e345 | 842 | my $quar_username = $ref->{pmail} . '@quarantine'; |
666b5e8f | 843 | raise_perm_exc("mail does not belong to user '$authuser' ($ref->{pmail})") |
6060e345 | 844 | if $authuser ne $quar_username; |
6e8886d4 DM |
845 | } |
846 | ||
847 | my $res = $parse_header_info->($ref); | |
848 | ||
6e8886d4 DM |
849 | my $filename = $ref->{file}; |
850 | my $spooldir = $PMG::MailQueue::spooldir; | |
851 | ||
852 | my $path = "$spooldir/$filename"; | |
853 | ||
34db0c3f DM |
854 | if ($format eq 'htmlmail') { |
855 | ||
856 | my $cfg = PMG::Config->new(); | |
857 | my $viewimages = $cfg->get('spamquar', 'viewimages'); | |
858 | my $allowhref = $cfg->get('spamquar', 'allowhrefs'); | |
859 | ||
69da4eb6 | 860 | $res->{content} = PMG::HTMLMail::email_to_html($path, $raw, $viewimages, $allowhref) // 'unable to parse mail'; |
34db0c3f | 861 | |
157a946b DM |
862 | # to make result verification happy |
863 | $res->{file} = ''; | |
864 | $res->{header} = ''; | |
2ad4def7 | 865 | $res->{spamlevel} = 0; |
157a946b | 866 | $res->{spaminfo} = []; |
34db0c3f | 867 | } else { |
cd31bb45 | 868 | # include additional details |
34db0c3f | 869 | |
c81cf188 DC |
870 | # we want to get the whole email in raw mode |
871 | my $maxbytes = (!$raw)? 4096 : undef; | |
872 | ||
873 | my ($header, $content) = PMG::HTMLMail::read_raw_email($path, $maxbytes); | |
157a946b | 874 | |
cd31bb45 DM |
875 | $res->{file} = $ref->{file}; |
876 | $res->{spaminfo} = decode_spaminfo($ref->{info}); | |
34db0c3f DM |
877 | $res->{header} = $header; |
878 | $res->{content} = $content; | |
879 | } | |
6e8886d4 | 880 | |
6e8886d4 DM |
881 | |
882 | return $res; | |
883 | ||
884 | }}); | |
885 | ||
34db0c3f DM |
886 | PVE::APIServer::Formatter::register_page_formatter( |
887 | 'format' => 'htmlmail', | |
888 | method => 'GET', | |
889 | path => '/quarantine/content', | |
890 | code => sub { | |
891 | my ($res, $data, $param, $path, $auth, $config) = @_; | |
892 | ||
893 | if(!HTTP::Status::is_success($res->{status})) { | |
894 | return ("Error $res->{status}: $res->{message}", "text/plain"); | |
895 | } | |
896 | ||
897 | my $ct = "text/html;charset=UTF-8"; | |
898 | ||
899 | my $raw = $data->{content}; | |
900 | ||
901 | return (encode('UTF-8', $raw), $ct, 1); | |
902 | }); | |
903 | ||
157a946b DM |
904 | __PACKAGE__->register_method ({ |
905 | name =>'action', | |
906 | path => 'content', | |
907 | method => 'POST', | |
908 | description => "Execute quarantine actions.", | |
909 | permissions => { check => [ 'admin', 'qmanager', 'quser'] }, | |
910 | protected => 1, | |
911 | parameters => { | |
912 | additionalProperties => 0, | |
913 | properties => { | |
914 | id => { | |
b7894d7c | 915 | description => 'Unique IDs, seperate with ;', |
157a946b | 916 | type => 'string', |
b7894d7c | 917 | pattern => 'C\d+R\d+T\d+(;C\d+R\d+T\d+)*', |
157a946b DM |
918 | }, |
919 | action => { | |
920 | description => 'Action - specify what you want to do with the mail.', | |
921 | type => 'string', | |
922 | enum => ['whitelist', 'blacklist', 'deliver', 'delete'], | |
923 | }, | |
924 | }, | |
925 | }, | |
926 | returns => { type => "null" }, | |
927 | code => sub { | |
928 | my ($param) = @_; | |
929 | ||
930 | my $rpcenv = PMG::RESTEnvironment->get(); | |
931 | my $authuser = $rpcenv->get_user(); | |
932 | my $role = $rpcenv->get_role(); | |
933 | my $action = $param->{action}; | |
b7894d7c | 934 | my @idlist = split(';', $param->{id}); |
157a946b | 935 | |
e18901ef DM |
936 | my $dbh = PMG::DBTools::open_ruledb(); |
937 | ||
b7894d7c DC |
938 | for my $id (@idlist) { |
939 | my ($cid, $rid, $tid) = $id =~ m/^C(\d+)R(\d+)T(\d+)$/; | |
940 | $cid = int($cid); | |
941 | $rid = int($rid); | |
942 | $tid = int($tid); | |
8f34e0b1 | 943 | |
b7894d7c | 944 | my $ref = PMG::DBTools::load_mail_data($dbh, $cid, $rid, $tid); |
157a946b | 945 | |
b7894d7c DC |
946 | if ($role eq 'quser') { |
947 | my $quar_username = $ref->{pmail} . '@quarantine'; | |
948 | raise_perm_exc("mail does not belong to user '$authuser' ($ref->{pmail})") | |
6060e345 | 949 | if $authuser ne $quar_username; |
b7894d7c DC |
950 | } |
951 | ||
952 | my $sender = $get_real_sender->($ref); | |
953 | ||
954 | if ($action eq 'whitelist') { | |
955 | PMG::Quarantine::add_to_blackwhite($dbh, $ref->{pmail}, 'WL', [ $sender ]); | |
956 | } elsif ($action eq 'blacklist') { | |
957 | PMG::Quarantine::add_to_blackwhite($dbh, $ref->{pmail}, 'BL', [ $sender ]); | |
958 | } elsif ($action eq 'deliver') { | |
959 | PMG::Quarantine::deliver_quarantined_mail($dbh, $ref, $ref->{receiver} // $ref->{pmail}); | |
960 | } elsif ($action eq 'delete') { | |
961 | PMG::Quarantine::delete_quarantined_mail($dbh, $ref); | |
962 | } else { | |
963 | die "internal error"; # should not be reached | |
964 | } | |
157a946b DM |
965 | } |
966 | ||
967 | return undef; | |
968 | }}); | |
e84bf942 | 969 | |
b66faa68 | 970 | 1; |