]> git.proxmox.com Git - pmg-api.git/commitdiff
start quarantine API
authorDietmar Maurer <dietmar@proxmox.com>
Fri, 28 Apr 2017 09:16:26 +0000 (11:16 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Fri, 28 Apr 2017 09:27:19 +0000 (11:27 +0200)
Makefile
PMG/API2.pm
PMG/API2/Quarantine.pm [new file with mode: 0644]
PMG/CLI/pmgqm.pm

index a813b688c8a8a9808122dac3f045d2b36f66ad04..88faff83ba7c2bbadabcf66b05144b2fe9813426 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -124,6 +124,7 @@ LIBSOURCES =                                \
        PMG/API2/ClamAV.pm              \
        PMG/API2/Nodes.pm               \
        PMG/API2/Postfix.pm             \
+       PMG/API2/Quarantine.pm          \
        PMG/API2/AccessControl.pm       \
        PMG/API2/ObjectGroupHelpers.pm  \
        PMG/API2/Rules.pm               \
index d8672bd3970485cd797780bf471cacb9324a63ce..736f1f947f3bf98332aed5872e36ad7eab72a7b5 100644 (file)
@@ -9,6 +9,7 @@ use PVE::JSONSchema;
 use PMG::API2::AccessControl;
 use PMG::API2::Nodes;
 use PMG::API2::Config;
+use PMG::API2::Quarantine;
 use PMG::pmgcfg;
 
 use base qw(PVE::RESTHandler);
@@ -26,6 +27,11 @@ __PACKAGE__->register_method ({
 __PACKAGE__->register_method ({
     subclass => "PMG::API2::AccessControl",
     path => 'access',
+                             });
+
+__PACKAGE__->register_method ({
+    subclass => "PMG::API2::Quarantine",
+    path => 'quarantine',
 });
 
 __PACKAGE__->register_method ({
@@ -52,9 +58,11 @@ __PACKAGE__->register_method ({
        my ($resp, $param) = @_;
 
        my $res = [
+           { subdir => 'access' },
            { subdir => 'config' },
            { subdir => 'nodes' },
            { subdir => 'version' },
+           { subdir => 'quarantine' },
            ];
 
        return $res;
diff --git a/PMG/API2/Quarantine.pm b/PMG/API2/Quarantine.pm
new file mode 100644 (file)
index 0000000..7f01963
--- /dev/null
@@ -0,0 +1,150 @@
+package PMG::API2::Quarantine;
+
+use strict;
+use warnings;
+use Time::Local;
+use Time::Zone;
+use Data::Dumper;
+
+use PVE::SafeSyslog;
+use PVE::Exception qw(raise_param_exc);
+use PVE::Tools qw(extract_param);
+use PVE::JSONSchema qw(get_standard_option);
+use PVE::RESTHandler;
+use PVE::INotify;
+
+use PMG::AccessControl;
+use PMG::DBTools;
+
+use base qw(PVE::RESTHandler);
+
+
+__PACKAGE__->register_method ({
+    name => 'index',
+    path => '',
+    method => 'GET',
+    permissions => { user => 'all' },
+    description => "Directory index.",
+    parameters => {
+       additionalProperties => 0,
+       properties => {},
+    },
+    returns => {
+       type => 'array',
+       items => {
+           type => "object",
+           properties => {},
+       },
+       links => [ { rel => 'child', href => "{name}" } ],
+    },
+    code => sub {
+       my ($param) = @_;
+
+       my $result = [
+           { name => 'deliver' },
+           { name => 'spam' },
+           { name => 'virus' },
+       ];
+
+       return $result;
+    }});
+
+__PACKAGE__->register_method ({
+    name => 'spam',
+    path => 'spam',
+    method => 'GET',
+    permissions => { check => [ 'admin', 'qmanager', 'audit', 'quser'] },
+    description => "Show spam mails distribution (per day).",
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           starttime => {
+               description => "Only consider entries newer than 'startime' (unix epoch).",
+               type => 'integer',
+               minimum => 0,
+               optional => 1,
+           },
+           endtime => {
+               description => "Only consider entries older than 'endtime' (unix epoch).",
+               type => 'integer',
+               minimum => 1,
+               optional => 1,
+           },
+           pmail => {
+               description => "List entries for the user with this primary email address.",
+               type => 'string', format => 'email',
+               optional => 1,
+           },
+       },
+    },
+    returns => {
+       type => 'array',
+       items => {
+           type => "object",
+           properties => {
+               day => {
+                   description => "Day (as unix epoch).",
+                   type => 'integer',
+               },
+               count => { 
+                   description => "Number of quarantine entries.",
+                   type => 'integer',
+               },
+               spamavg => {
+                   description => "Average spam level.",
+                   type => 'number',
+               },                  
+           },
+       },
+    },
+    code => sub {
+       my ($param) = @_;
+
+       my $rpcenv = PMG::RESTEnvironment->get();
+       my $authuser = $rpcenv->get_user();
+       my $role = $rpcenv->get_role();
+
+       my $pmail = $param->{pmail};
+
+       if ($role eq 'quser') {
+           raise_param_exc({ pmail => "paramater not allwed with role '$role'"})
+               if defined($pmail);
+           $pmail = $authuser;
+       }
+
+       my $res = [];
+       
+       my $dbh = PMG::DBTools::open_ruledb();
+
+       my $start = $param->{starttime} // 0;
+       my $end = $param->{endtime};
+
+       my $timezone = tz_local_offset();
+
+       my $sth = $dbh->prepare(
+           "SELECT " .
+           "((time + $timezone) / 86400) * 86400 - $timezone as day, " .
+           "count (ID) as count, avg (Spamlevel) as spamavg " .
+           "FROM CMailStore, CMSReceivers WHERE " .
+           ($start ? "time >= $start AND " : '') .
+           ($end ? "time < $end AND " : '') .
+           (defined($pmail) ? "pmail = ? AND " : '') .
+           "QType = 'S' AND CID = CMailStore_CID AND RID = CMailStore_RID " .
+           "AND Status = 'N' " .
+           "GROUP BY day " .
+           "ORDER BY day DESC");
+
+       if (defined($pmail)) {
+           $sth->execute($pmail);
+       } else {
+           $sth->execute();
+       }
+
+       while (my $ref = $sth->fetchrow_hashref()) {
+           push @$res, $ref;
+       }
+
+       return $res;
+    }});
+
+1;
index 01a1b908c18fdd29ca1a8ad02c33644c58ff0613..cd4981a36046609317836a8d596fe4c467fbfb0a 100755 (executable)
@@ -24,6 +24,7 @@ use PMG::DBTools;
 use PMG::RuleDB;
 use PMG::Config;
 use PMG::ClusterConfig;
+use PMG::API2::Quarantine;
 
 use base qw(PVE::CLIHandler);
 
@@ -396,7 +397,7 @@ __PACKAGE__->register_method ({
        additionalProperties => 0,
        properties => {
            check => {
-               description => "Only search for quarantine files older than configured quarantine lifetime. Just print found files, but does not remove them.",
+               description => "Only search for quarantine files older than configured quarantine lifetime. Just print found files, but do not remove them.",
                type => 'boolean',
                optional => 1,
                default => 0,
@@ -437,6 +438,15 @@ __PACKAGE__->register_method ({
 our $cmddef = {
     'purge' => [ __PACKAGE__, 'purge', []],
     'send' => [ __PACKAGE__, 'send', []],
+    'spam' => [ 'PMG::API2::Quarantine', 'spam', [], undef, sub {
+       my $res = shift;
+       print "Day          Count  AVG(Spam)\n";
+       foreach my $ref (@$res) {
+           print sprintf("%-12s %5d %10.2f\n",
+                         strftime("%F", localtime($ref->{day})),
+                         $ref->{count}, $ref->{spamavg});
+       }
+     }],
 };
 
 1;