]> git.proxmox.com Git - pmg-api.git/blame - PMG/CLI/pmgqm.pm
PMG/CLI/pmgqm.pm - implement new status command
[pmg-api.git] / PMG / CLI / pmgqm.pm
CommitLineData
fe6075cd 1package PMG::CLI::pmgqm;
9a56f771
DM
2
3use strict;
4use Data::Dumper;
5use Template;
6use MIME::Entity;
7use HTML::Entities;
8use Time::Local;
9use Clone 'clone';
10use Mail::Header;
11use POSIX qw(strftime);
7205df3e 12use File::Find;
04c66027 13use File::stat;
d9750bee 14use URI::Escape;
9a56f771
DM
15
16use PVE::SafeSyslog;
17use PVE::Tools;
18use PVE::INotify;
19use PVE::CLIHandler;
20
21use PMG::RESTEnvironment;
22use PMG::Utils;
9a728eba 23use PMG::Ticket;
9a56f771
DM
24use PMG::DBTools;
25use PMG::RuleDB;
26use PMG::Config;
27use PMG::ClusterConfig;
b66faa68 28use PMG::API2::Quarantine;
9a56f771
DM
29
30use base qw(PVE::CLIHandler);
31
32sub setup_environment {
33 PMG::RESTEnvironment->setup_default_cli_env();
34}
35
36sub domain_regex {
37 my ($domains) = @_;
38
39 my @ra;
40 foreach my $d (@$domains) {
41 # skip domains with non-DNS name characters
42 next if $d =~ m/[^A-Za-z0-9\-\.]/;
43 if ($d =~ m/^\.(.*)$/) {
44 my $dom = $1;
45 $dom =~ s/\./\\\./g;
46 push @ra, $dom;
47 push @ra, "\.\*\\.$dom";
48 } else {
49 $d =~ s/\./\\\./g;
50 push @ra, $d;
51 }
52 }
53
54 my $re = join ('|', @ra);
55
56 my $regex = qr/\@($re)$/i;
57
58 return $regex;
59}
60
61sub get_item_data {
1d20daff 62 my ($data, $ref) = @_;
9a56f771
DM
63
64 my @lines = split ('\n', $ref->{header});
65 my $head = new Mail::Header(\@lines);
66
1d20daff 67 my $item = {};
f46af1cf
DM
68
69 $item->{id} = sprintf("C%dR%d", $ref->{cid}, $ref->{rid});
252b770d 70
1d20daff 71 $item->{subject} = PMG::Utils::rfc1522_to_html(
9a56f771
DM
72 PVE::Tools::trim($head->get('subject')) || 'No Subject');
73
74 my @fromarray = split('\s*,\s*', $head->get('from') || $ref->{sender});
75 my $from = PMG::Utils::rfc1522_to_html(PVE::Tools::trim($fromarray[0]));
76 my $sender = PMG::Utils::rfc1522_to_html(PVE::Tools::trim($head->get('sender')));
77
78 if ($sender) {
1d20daff
DM
79 $item->{sender} = $sender;
80 $item->{from} = sprintf ("%s on behalf of %s", $sender, $from);
9a56f771 81 } else {
1d20daff 82 $item->{from} = $from;
9a56f771
DM
83 }
84
1d20daff
DM
85 $item->{pmail} = $ref->{pmail};
86 $item->{receiver} = $ref->{receiver} || $ref->{pmail};
9a56f771 87
1d20daff
DM
88 $item->{date} = strftime("%F", localtime($ref->{time}));
89 $item->{time} = strftime("%H:%M:%S", localtime($ref->{time}));
252b770d 90
1d20daff
DM
91 $item->{bytes} = $ref->{bytes};
92 $item->{spamlevel} = $ref->{spamlevel};
93 $item->{spaminfo} = $ref->{info};
252b770d 94
1d20daff 95 my $title = "Received: $item->{date} $item->{time}\n";
9a56f771
DM
96 $title .= "From: $ref->{sender}\n";
97 $title .= "To: $ref->{receiver}\n" if $ref->{receiver};
98 $title .= sprintf("Size: %d KB\n", int (($ref->{bytes} + 1023) / 1024 ));
99 $title .= sprintf("Spam level: %d\n", $ref->{spamlevel}) if $ref->{qtype} eq 'S';
100 $title .= sprintf("Virus info: %s\n", encode_entities ($ref->{info})) if $ref->{qtype} eq 'V';
101 $title .= sprintf("File: %s", encode_entities($ref->{file}));
102
1d20daff 103 $item->{title} = $title;
9b0428e2 104
7eebbc6b 105 my $basehref = "https://$data->{fqdn}:$data->{port}/quarantine";
d9750bee 106 my $ticket = uri_escape($data->{ticket});
e13942a1
DM
107 $item->{wlhref} = "$basehref?ticket=$ticket&cselect=$item->{id}&action=whitelist";
108 $item->{blhref} = "$basehref?ticket=$ticket&cselect=$item->{id}&action=blacklist";
109 $item->{deliverhref} = "$basehref?ticket=$ticket&cselect=$item->{id}&action=deliver";
110 $item->{deletehref} = "$basehref?ticket=$ticket&cselect=$item->{id}&action=delete";
1d20daff
DM
111
112 return $item;
9a56f771
DM
113}
114
252b770d
DM
115__PACKAGE__->register_method ({
116 name => 'status',
117 path => 'status',
118 method => 'POST',
119 description => "Print quarantine status (mails per user) for specified time span.",
120 parameters => {
121 additionalProperties => 0,
122 properties => {
123 timespan => {
124 description => "Select time span.",
125 type => 'string',
126 enum => ['today', 'yesterday', 'week'],
127 default => 'today',
128 optional => 1,
129 },
130 },
131 },
132 returns => { type => 'null'},
133 code => sub {
134 my ($param) = @_;
135
136 my $cinfo = PMG::ClusterConfig->new();
137 my $role = $cinfo->{local}->{type} // '-';
138
139 if (!(($role eq '-') || ($role eq 'master'))) {
140 die "local node is not mastern";
141 }
142
143 my $cfg = PMG::Config->new();
144
145 my $timespan = $param->{timespan} // 'today';
146
147 my ($start, $end) = PMG::Utils::lookup_timespan($timespan);
148
149 my $hostname = PVE::INotify::nodename();
150
151 my $fqdn = $cfg->get('spamquar', 'hostname') //
152 PVE::Tools::get_fqdn($hostname);
153
154
155 my $dbh = PMG::DBTools::open_ruledb();
156
157 my $domains = PVE::INotify::read_file('domains');
158 my $domainregex = domain_regex([keys %$domains]);
159
160 my $sth = $dbh->prepare(
161 "SELECT pmail, AVG(spamlevel) as spamlevel, count(*) FROM CMailStore, CMSReceivers " .
162 "WHERE time >= $start AND time < $end AND " .
163 "QType = 'S' AND CID = CMailStore_CID AND RID = CMailStore_RID " .
164 "AND Status = 'N' " .
165 "GROUP BY pmail " .
166 "ORDER BY pmail");
167
168 $sth->execute();
169
170 print "Count Spamlevel Mail\n";
171 my $res = [];
172 while (my $ref = $sth->fetchrow_hashref()) {
173 push @$res, $ref;
174 my $extern = ($domainregex && $ref->{pmail} !~ $domainregex);
175 my $hint = $extern ? " (external address)" : "";
176 printf ("%-5d %10.2f %s$hint\n", $ref->{count}, $ref->{spamlevel}, $ref->{pmail});
177 }
178
179 $sth->finish();
180
181 return undef;
182 }});
183
9a56f771
DM
184__PACKAGE__->register_method ({
185 name => 'send',
186 path => 'send',
187 method => 'POST',
188 description => "Generate and send spam report emails.",
189 parameters => {
190 additionalProperties => 0,
191 properties => {
192 receiver => {
193 description => "Generate report for a single email address. If not specified, generate reports for all users.",
194 type => 'string', format => 'email',
252b770d 195 optional => 1,
9a56f771
DM
196 },
197 timespan => {
198 description => "Select time span.",
199 type => 'string',
200 enum => ['today', 'yesterday', 'week'],
201 default => 'today',
202 optional => 1,
203 },
204 style => {
252b770d 205 description => "Spam report style. Default value is read from spam quarantine configuration.",
9a56f771 206 type => 'string',
252b770d 207 enum => ['short', 'verbose', 'custom'],
9a56f771 208 optional => 1,
9a56f771
DM
209 },
210 redirect => {
211 description => "Redirect spam report email to this address.",
212 type => 'string', format => 'email',
213 optional => 1,
214 },
f73a1872
DM
215 debug => {
216 description => "Debug mode. Print raw email to stdout instead of sending them.",
217 type => 'boolean',
218 optional => 1,
219 default => 0,
220 }
9a56f771
DM
221 },
222 },
223 returns => { type => 'null'},
224 code => sub {
225 my ($param) = @_;
226
227 my $cinfo = PMG::ClusterConfig->new();
228 my $role = $cinfo->{local}->{type} // '-';
229
230 if (!(($role eq '-') || ($role eq 'master'))) {
231 die "local node is not master - not sending spam report\n";
252b770d 232 }
9a56f771
DM
233
234 my $cfg = PMG::Config->new();
235
f73a1872 236 my $reportstyle = $param->{style} // $cfg->get('spamquar', 'reportstyle');
9a56f771 237
252b770d
DM
238 return if $reportstyle eq 'none'; # do nothing
239
9a56f771
DM
240 my $timespan = $param->{timespan} // 'today';
241
2f7031b7 242 my ($start, $end) = PMG::Utils::lookup_timespan($timespan);
9a56f771
DM
243
244 my $hostname = PVE::INotify::nodename();
252b770d
DM
245
246 my $fqdn = $cfg->get('spamquar', 'hostname') //
9a56f771 247 PVE::Tools::get_fqdn($hostname);
252b770d 248
9a56f771 249 my $port = 8006;
252b770d 250
9a56f771
DM
251 my $global_data = {
252 protocol => 'https',
253 port => $port,
254 fqdn => $fqdn,
255 hostname => $hostname,
9a56f771
DM
256 date => strftime("%F", localtime($end - 1)),
257 timespan => $timespan,
258 items => [],
259 };
260
252b770d 261 my $mailfrom = $cfg->get ('spamquar', 'mailfrom') //
9a56f771 262 "Proxmox Mail Gateway <postmaster>";
252b770d 263
9a56f771
DM
264 my $dbh = PMG::DBTools::open_ruledb();
265
266 my $target = $param->{receiver};
267 my $redirect = $param->{redirect};
252b770d 268
1388c118 269 if (defined($redirect) && !defined($target)) {
9a56f771
DM
270 die "can't redirect mails for all users\n";
271 }
252b770d 272
9a56f771
DM
273 my $domains = PVE::INotify::read_file('domains');
274 my $domainregex = domain_regex([keys %$domains]);
275
252b770d
DM
276 my $template = "spamreport-${reportstyle}.tt";
277 my $found = 0;
278 foreach my $path (@$PMG::Config::tt_include_path) {
279 if (-f "$path/$template") { $found = 1; last; }
280 }
281 if (!$found) {
282 warn "unable to find template '$template' - using default\n";
283 $template = "spamreport-verbose.tt";
9a56f771 284 }
78ee390d 285
9a56f771 286 my $sth = $dbh->prepare(
252b770d
DM
287 "SELECT * FROM CMailStore, CMSReceivers " .
288 "WHERE time >= $start AND time < $end AND " .
9a56f771
DM
289 ($target ? "pmail = ? AND " : '') .
290 "QType = 'S' AND CID = CMailStore_CID AND RID = CMailStore_RID " .
291 "AND Status = 'N' " .
292 "ORDER BY pmail, time, receiver");
252b770d 293
9a56f771
DM
294 if ($target) {
295 $sth->execute($target);
296 } else {
297 $sth->execute();
298 }
299
300 my $lastref;
301 my $mailcount = 0;
302 my $creceiver = '';
303 my $data;
78ee390d 304
310daf18
DM
305 my $tt = PMG::Config::get_template_toolkit();
306
9a56f771 307 my $finalize = sub {
252b770d 308
9a56f771 309 my $extern = ($domainregex && $creceiver !~ $domainregex);
252b770d
DM
310 if (!$extern) {
311 $data->{mailcount} = $mailcount;
312 my $sendto = $redirect ? $redirect : $creceiver;
313 PMG::Utils::finalize_report($tt, $template, $data, $mailfrom, $sendto, $param->{debug});
9a56f771
DM
314 }
315 };
252b770d 316
9a56f771
DM
317 while (my $ref = $sth->fetchrow_hashref()) {
318 if ($creceiver ne $ref->{pmail}) {
319
320 $finalize->() if $data;
321
322 $data = clone($global_data);
252b770d 323
9a56f771
DM
324 $creceiver = $ref->{pmail};
325 $mailcount = 0;
326
327 $data->{pmail} = $creceiver;
f46af1cf 328 $data->{ticket} = PMG::Ticket::assemble_quarantine_ticket($data->{pmail});
d9750bee
DM
329 my $esc_ticket = uri_escape($data->{ticket});
330 $data->{managehref} = "https://$fqdn:$port/quarantine?ticket=${esc_ticket}";
9a56f771
DM
331 }
332
252b770d
DM
333 push @{$data->{items}}, get_item_data($data, $ref);
334
9a56f771
DM
335 $mailcount++;
336 $lastref = $ref;
337 }
338
339 $sth->finish();
340
341 $finalize->() if $data;
342
343 if (defined($target) && !$mailcount) {
344 print STDERR "no mails for '$target'\n";
345 }
346
347 return undef;
348 }});
349
350sub find_stale_files {
351 my ($path, $lifetime, $purge) = @_;
352
7205df3e
DM
353 return if ! -d $path;
354
27fbc388
DM
355 my (undef, undef, undef, $mday, $mon, $year) = localtime(time());
356 my $daystart = timelocal(0, 0, 0, $mday, $mon, $year);
357 my $expire = $daystart - $lifetime*86400;
358
359 my $wanted = sub {
360 my $name = $File::Find::name;
361 return if $name !~ m|^($path/.*)$|;
362 $name = $1; # untaint
363 my $stat = stat($name);
364 return if ! -f _;
cd938884 365 return if $stat->mtime >= $expire;
27fbc388
DM
366 if ($purge) {
367 if (unlink($name)) {
368 print "removed: $name\n";
369 }
370 } else {
371 print "$name\n";
372 }
373 };
9a56f771 374
27fbc388 375 find({ wanted => $wanted, no_chdir => 1 }, $path);
9a56f771
DM
376}
377
378sub test_quarantine_files {
379 my ($spamlifetime, $viruslifetime, $purge) = @_;
252b770d
DM
380
381 print STDERR "searching for stale files\n" if !$purge;
9a56f771 382
7205df3e
DM
383 my $spooldir = $PMG::MailQueue::spooldir;
384
385 find_stale_files ("$spooldir/spam", $spamlifetime, $purge);
386 foreach my $dir (<"/var/spool/pmg/cluster/*/spam">) {
387 next if $dir !~ m|^(/var/spool/pmg/cluster/\d+/spam)$|;
388 $dir = $1; # untaint
389 find_stale_files ($dir, $spamlifetime, $purge);
390 }
9a56f771 391
7205df3e
DM
392 find_stale_files ("$spooldir/virus", $viruslifetime, $purge);
393 foreach my $dir (<"/var/spool/pmg/cluster/*/virus">) {
394 next if $dir !~ m|^(/var/spool/pmg/cluster/\d+/virus)$|;
395 $dir = $1; # untaint
396 find_stale_files ($dir, $viruslifetime, $purge);
397 }
9a56f771
DM
398}
399
400__PACKAGE__->register_method ({
401 name => 'purge',
402 path => 'purge',
403 method => 'POST',
404 description => "Cleanup Quarantine database. Remove entries older than configured quarantine lifetime.",
405 parameters => {
406 additionalProperties => 0,
27fbc388
DM
407 properties => {
408 check => {
b66faa68 409 description => "Only search for quarantine files older than configured quarantine lifetime. Just print found files, but do not remove them.",
27fbc388
DM
410 type => 'boolean',
411 optional => 1,
412 default => 0,
413 }
414 }
9a56f771
DM
415 },
416 returns => { type => 'null'},
417 code => sub {
418 my ($param) = @_;
419
420 my $cfg = PMG::Config->new();
421
422 my $spamlifetime = $cfg->get('spamquar', 'lifetime');
423 my $viruslifetime = $cfg->get ('virusquar', 'lifetime');
424
27fbc388 425 my $purge = !$param->{check};
9a56f771 426
27fbc388 427 if ($purge) {
252b770d 428 print STDERR "purging database\n";
9a56f771 429
27fbc388
DM
430 my $dbh = PMG::DBTools::open_ruledb();
431
432 if (my $count = PMG::DBTools::purge_quarantine_database($dbh, 'S', $spamlifetime)) {
252b770d 433 print STDERR "removed $count spam quarantine files\n";
27fbc388 434 }
9a56f771 435
27fbc388 436 if (my $count = PMG::DBTools::purge_quarantine_database($dbh, 'V', $viruslifetime)) {
252b770d 437 print STDERR "removed $count virus quarantine files\n";
27fbc388 438 }
9a56f771
DM
439 }
440
27fbc388 441 test_quarantine_files($spamlifetime, $viruslifetime, $purge);
9a56f771
DM
442
443 return undef;
444 }});
445
9a56f771
DM
446
447our $cmddef = {
9a56f771
DM
448 'purge' => [ __PACKAGE__, 'purge', []],
449 'send' => [ __PACKAGE__, 'send', []],
252b770d 450 'status' => [ __PACKAGE__, 'status', []],
9a56f771
DM
451};
452
4531;