]>
git.proxmox.com Git - pmg-api.git/blob - src/PMG/Quarantine.pm
1 package PMG
::Quarantine
;
15 sub add_to_blackwhite
{
16 my ($dbh, $username, $listname, $addrs, $delete) = @_;
18 my $name = $listname eq 'BL' ?
'BL' : 'WL';
19 my $oname = $listname eq 'BL' ?
'WL' : 'BL';
20 my $qu = $dbh->quote (encode
('UTF-8', $username));
22 my $sth = $dbh->prepare(
23 "SELECT * FROM UserPrefs WHERE pmail = $qu AND (Name = 'BL' OR Name = 'WL')");
26 my $list = { 'WL' => {}, 'BL' => {} };
28 while (my $ref = $sth->fetchrow_hashref()) {
29 my $data = PMG
::Utils
::try_decode_utf8
($ref->{data
});
31 my @alist = split('\s+', $data);
34 foreach my $a (@alist) {
35 if ($a =~ m/^[^\s\\\@]+(?:\@[^\s\/\\\
@]+)?
$/) {
40 $list->{$ref->{name
}} = $tmp;
47 foreach my $v (@$addrs) {
48 die "email address '$v' is too long (> 512 characters)\n"
52 delete($list->{$name}->{$v});
54 if ($v =~ m/[\s\\]/) {
55 die "email address '$v' contains invalid characters\n";
57 $list->{$name}->{$v} = 1;
58 delete ($list->{$oname}->{$v});
62 my $wlist = $dbh->quote(encode
('UTF-8', join (',', sort keys %{$list->{WL
}})) || '');
63 my $blist = $dbh->quote(encode
('UTF-8', join (',', sort keys %{$list->{BL
}})) || '');
67 die "whitelist size exceeds limit (> $maxlen bytes)\n"
68 if length($wlist) > $maxlen;
69 die "blacklist size exceeds limit (> $maxlen bytes)\n"
70 if length($blist) > $maxlen;
73 my $queries = "DELETE FROM UserPrefs WHERE pmail = $qu AND (Name = 'WL' OR Name = 'BL');";
75 $queries .= "INSERT INTO UserPrefs (PMail, Name, Data, MTime) " .
76 "VALUES ($qu, 'WL', $wlist, EXTRACT (EPOCH FROM now())::INTEGER);";
78 $queries .= "INSERT INTO UserPrefs (PMail, Name, Data, MTime) " .
79 "VALUES ($qu, 'BL', $blist, EXTRACT (EPOCH FROM now())::INTEGER);";
84 my $values = [ sort keys %{$list->{$name}} ];
89 sub deliver_quarantined_mail
{
90 my ($dbh, $ref, $receiver) = @_;
92 my $filename = $ref->{file
};
93 my $spooldir = $PMG::MailQueue
::spooldir
;
94 my $path = "$spooldir/$filename";
96 my $id = 'C' . $ref->{cid
} . 'R' . $ref->{rid
} . 'T' . $ref->{ticketid
};;
98 my $parser = PMG
::MIMEUtils
::new_mime_parser
({
101 extract_uuencode
=> 0,
102 dumpdir
=> "/tmp/.quarantine-$id-$receiver-$$/",
105 my $entity = $parser->parse_open("$path");
106 PMG
::MIMEUtils
::fixup_multipart
($entity);
108 # delete Delivered-To and Return-Path (avoid problem with postfix
109 # forwarding loop detection (man local))
110 $entity->head->delete('Delivered-To');
111 $entity->head->delete('Return-Path');
113 my $sender = 'postmaster'; # notify postmaster if something fails
116 my ($qid, $code, $mess) = PMG
::Utils
::reinject_local_mail
(
117 $entity, $sender, [$receiver], undef, 'quarantine');
123 my $sth = $dbh->prepare(
124 "UPDATE CMSReceivers SET Status='D', MTime = ? " .
125 "WHERE CMailStore_CID = ? AND CMailStore_RID = ? AND TicketID = ?");
126 $sth->execute(time(), $ref->{cid
}, $ref->{rid
}, $ref->{ticketid
});
131 my $msg = "deliver quarantined mail '$id' ($path) failed: $err";
136 syslog
('info', "delivered quarantined mail '$id' ($path)");
141 sub delete_quarantined_mail
{
142 my ($dbh, $ref) = @_;
144 my $filename = $ref->{file
};
145 my $spooldir = $PMG::MailQueue
::spooldir
;
146 my $path = "$spooldir/$filename";
148 my $id = 'C' . $ref->{cid
} . 'R' . $ref->{rid
} . 'T' . $ref->{ticketid
};;
151 my $sth = $dbh->prepare(
152 "UPDATE CMSReceivers SET Status='D', MTime = ? WHERE " .
153 "CMailStore_CID = ? AND CMailStore_RID = ? AND TicketID = ?");
154 $sth->execute (time(), $ref->{cid
}, $ref->{rid
}, $ref->{ticketid
});
158 my $msg = "delete quarantined mail '$id' ($path) failed: $err";
159 syslog
('err', $msg);
163 syslog
('info', "marked quarantined mail '$id' as deleted ($path)");