]>
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 (',', keys %{$list->{WL
}})) || '');
63 my $blist = $dbh->quote(encode
('UTF-8', join (',', 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');";
74 if (scalar(keys %{$list->{WL
}})) {
76 "INSERT INTO UserPrefs (PMail, Name, Data, MTime) " .
77 "VALUES ($qu, 'WL', $wlist, EXTRACT (EPOCH FROM now())::INTEGER);";
79 if (scalar(keys %{$list->{BL
}})) {
81 "INSERT INTO UserPrefs (PMail, Name, Data, MTime) " .
82 "VALUES ($qu, 'BL', $blist, EXTRACT (EPOCH FROM now())::INTEGER);";
87 my $values = [ keys %{$list->{$name}} ];
92 sub deliver_quarantined_mail
{
93 my ($dbh, $ref, $receiver) = @_;
95 my $filename = $ref->{file
};
96 my $spooldir = $PMG::MailQueue
::spooldir
;
97 my $path = "$spooldir/$filename";
99 my $id = 'C' . $ref->{cid
} . 'R' . $ref->{rid
} . 'T' . $ref->{ticketid
};;
101 my $parser = PMG
::MIMEUtils
::new_mime_parser
({
104 extract_uuencode
=> 0,
105 dumpdir
=> "/tmp/.quarantine-$id-$receiver-$$/",
108 my $entity = $parser->parse_open("$path");
109 PMG
::MIMEUtils
::fixup_multipart
($entity);
111 # delete Delivered-To and Return-Path (avoid problem with postfix
112 # forwarding loop detection (man local))
113 $entity->head->delete('Delivered-To');
114 $entity->head->delete('Return-Path');
116 my $sender = 'postmaster'; # notify postmaster if something fails
119 my ($qid, $code, $mess) = PMG
::Utils
::reinject_local_mail
(
120 $entity, $sender, [$receiver], undef, 'quarantine');
126 my $sth = $dbh->prepare(
127 "UPDATE CMSReceivers SET Status='D', MTime = ? " .
128 "WHERE CMailStore_CID = ? AND CMailStore_RID = ? AND TicketID = ?");
129 $sth->execute(time(), $ref->{cid
}, $ref->{rid
}, $ref->{ticketid
});
134 my $msg = "deliver quarantined mail '$id' ($path) failed: $err";
139 syslog
('info', "delivered quarantined mail '$id' ($path)");
144 sub delete_quarantined_mail
{
145 my ($dbh, $ref) = @_;
147 my $filename = $ref->{file
};
148 my $spooldir = $PMG::MailQueue
::spooldir
;
149 my $path = "$spooldir/$filename";
151 my $id = 'C' . $ref->{cid
} . 'R' . $ref->{rid
} . 'T' . $ref->{ticketid
};;
154 my $sth = $dbh->prepare(
155 "UPDATE CMSReceivers SET Status='D', MTime = ? WHERE " .
156 "CMailStore_CID = ? AND CMailStore_RID = ? AND TicketID = ?");
157 $sth->execute (time(), $ref->{cid
}, $ref->{rid
}, $ref->{ticketid
});
161 my $msg = "delete quarantined mail '$id' ($path) failed: $err";
162 syslog
('err', $msg);
166 syslog
('info', "marked quarantined mail '$id' as deleted ($path)");