Aktionen

Proxmox Mail Gateway PMG Spam Quarantäne Virus Mails löschen per Skript als Admin root

Aus znilwiki

Changelog:

  • 04.05.2025 erste Version

Vorwort

Das Skript stammt nicht von mir sondern aus dem Proxmox-Forum und ist dort von cocconi veröffentlicht worden:

https://forum.proxmox.com/threads/how-to-delete-all-the-emails-in-quarantine.65670/#post-296135



Skript installieren

Important.png
Hinweis: Das Skipt ist nicht von mir sondern von cocconi. Ich habe es an 2 Punkten angepasst: Die Ausgabe der Hilfe zeigt nun auch pmgdq als Programmnamen an und der Parameter allemails hört nun auch auf allmails (also ohne das e)
nano /usr/bin/pmgdq

Inhalt:

#!/usr/bin/perl

# 2020-02-01 Original Version by cocconi - https://forum.proxmox.com/threads/how-to-delete-all-the-emails-in-quarantine.65670/#post-296135
# 2025-05-04 modify Help Message to pmgdq, change parameter allemails to allmails by Bernhard Linz / bernhard@znil.de

use strict;
use warnings;

use Getopt::Std;
use Net::SMTP;
use JSON;

use PMG::RuleDB;
use PMG::Quarantine;

binmode(STDOUT, "encoding(UTF-8)");

my %options;
my $dbh;

sub myhelp{
    print "delete_quarantine.pl -f <from> -r <rcpt> -u <subject> -a <action>\n";
    print "\taction is mandatory : count/list/remove/deliver\n";
    print "\tit needs at least one : from, rcpt or subject ; you can mix several\n";
    print "\tIf '-r allmails' or '-f allmails' is used,  the action will be done to all the quarantine queue !\n";
    exit(2);
}

sub deliver_quarantined_mail {
    my ($id, $receiver) = @_;

    my ($cid, $rid, $tid) = $id =~ m/^C(\d+)R(\d+)T(\d+)$/;
    $cid = int($cid);
    $rid = int($rid);
    $tid = int($tid);

    if (!$dbh) {
        $dbh = PMG::DBTools::open_ruledb();
    }

    my $ref = PMG::DBTools::load_mail_data($dbh, $cid, $rid, $tid);

    PMG::Quarantine::deliver_quarantined_mail($dbh, $ref, $receiver);
}

sub delete_quarantined_mail {
    my ($id) = @_;

    my ($cid, $rid, $tid) = $id =~ m/^C(\d+)R(\d+)T(\d+)$/;
    $cid = int($cid);
    $rid = int($rid);
    $tid = int($tid);

    if (!$dbh) {
        $dbh = PMG::DBTools::open_ruledb();
    }

    my $ref = PMG::DBTools::load_mail_data($dbh, $cid, $rid, $tid);

    PMG::Quarantine::delete_quarantined_mail($dbh, $ref);
}

$options{f} = '';
$options{r} = '';
$options{s} = '';
$options{a} = '';


getopts('f:r:s:a:',\%options);

my $from = $options{f};
my $rcpt = $options{r};
my $subject = $options{s};
my $action = lc($options{a});

# List the spam rcpt since when ?
my $endtime = time(); # now
my $starttime = $endtime - (7 * 24 * 60 * 60) ; # 7 seven days before now !

if($action ne 'count' && $action ne 'list' && $action ne 'remove' && $action ne 'deliver'){
        myhelp();
}

if($from eq '' && $rcpt eq '' && $subject eq ''){
        myhelp();
}

#print("action = ".$action.", from = ".$from.", rcpt = ".$rcpt.", subject = ".$subject."\n");

my $users = qx(/usr/bin/pmgsh get /quarantine/spamusers --starttime $starttime --endtime $endtime 2>/dev/null);
my $data = decode_json($users);

#Global counter
my $ecount = 0;

foreach my $u (@$data){
    my $result = 1;
    if ($rcpt ne '') {
        if($rcpt eq 'allmails'){
            $result = 1;
        } elsif($rcpt eq 'allemails'){
            $result = 1;
        } elsif($u->{'mail'} =~ /^$rcpt$/){
            $result = 1;
        } else {
            $result = 0;
        }
    }

    if($result == 1){
#       print("Mail: ".$u->{'mail'}."\n");

        my $jspam= qx(/usr/bin/pmgsh get /quarantine/spam  --starttime $starttime --endtime $endtime -pmail $u->{'mail'} 2>/dev/null);
        my $emails = decode_json($jspam);
        foreach my $em (@$emails){
            $result = 1;
            if ($from ne '') {
                if($from eq 'allmails'){
                    $result = 1;
                } elsif($from eq 'allemails'){
                    $result = 1;
                } elsif($em->{'from'} =~ /.*$from.*/){
                    $result = 1;
                } else {
                    $result = 0;
                }
            }
            if ($result == 1 && $subject ne '') {
                if($em->{'subject'} =~ /$subject/){
                    $result = 1;
                } else {
                    $result = 0;
                }
            }
            if ($result == 1){
                $ecount++;
                if($action eq 'list'){
                        print("ID: ".$em->{'id'}." - Rcpt: ".$u->{'mail'}." - From: ".$em->{'from'}." - Subject: ".$em->{'subject'}."\n");
                } elsif($action eq 'remove'){
                    print("REMOVE => ID: ".$em->{'id'}." - Rcpt: ".$u->{'mail'}." - From: ".$em->{'from'}." - Subject: ".$em->{'subject'}."\n");
                    delete_quarantined_mail($em->{'id'});
                } elsif($action eq 'deliver'){
                    print("DELIVER => ID: ".$em->{'id'}." - Rcpt: ".$u->{'mail'}." - From: ".$em->{'from'}." - Subject: ".$em->{'subject'}."\n");
                    deliver_quarantined_mail($em->{'id'},$u->{'mail'});
                }
            }
        }
    }
}
print("Total of $ecount quarantined mails\n");

Danach Ausführbar machen:

chmod +x /usr/bin/pmgdq



Aufruf

pmgdq

ohne Parameter gibt die Hilfe aus:

pmgdq -f <from> -r <rcpt> -u <subject> -a <action>
       action is mandatory : count/list/remove/deliver
       it needs at least one : from, rcpt or subject ; you can mix several
       If '-r allemails' or '-f allemails' is used,  the action will be done to all the quarantine queue !

Um sich alle Mails in Quarantäne für alle Benutzer anzeigen zu lassen, verwendet zum Beispiel

pmgdq -r allemails -a list

Beispielausgabe:

ID: C0R350T40313079 - Rcpt: xxxxx.xxxxxxx@znil.de - From: "MiniPower Tools Co." <info@vitalhealthy.my> - Subject: SPAM: No More Dust. No More Cans. Just Pure Blower Power.
ID: C0R357T12901008 - Rcpt: xxxxxxxxxxx.xxxxxxx@znil.net - From: Omega <katie@ifq76.com> - Subject: SPAM: Omega Seamaster Starting at $250 - Shop Today!
ID: C0R374T10403149 - Rcpt: xxxxxx@linz.email - From: "SmartEnergy-Gerät"   <Wachstumsberater@8659.de> - Subject: SPAM: Gehen Sie intelligenter mit Ihrem Stromverbrauch um - SmartEnergy erwartet Sie!

mit

pmgdq -r allemails -a remove

löscht diese dann.


Quellen



Kommentare

Loading comments...