]>
git.proxmox.com Git - pmg-api.git/blob - src/PMG/Quarantine.pm
1 package PMG
::Quarantine
;
14 sub add_to_blackwhite
{
15 my ($dbh, $username, $listname, $addrs, $delete) = @_;
17 my $name = $listname eq 'BL' ?
'BL' : 'WL';
18 my $oname = $listname eq 'BL' ?
'WL' : 'BL';
19 my $qu = $dbh->quote ($username);
21 my $sth = $dbh->prepare(
22 "SELECT * FROM UserPrefs WHERE pmail = $qu AND (Name = 'BL' OR Name = 'WL')");
25 my $list = { 'WL' => {}, 'BL' => {} };
27 while (my $ref = $sth->fetchrow_hashref()) {
28 my $data = $ref->{data
};
30 my @alist = split('\s+', $data);
33 foreach my $a (@alist) {
34 if ($a =~ m/^[[:ascii:]]+$/) {
39 $list->{$ref->{name
}} = $tmp;
46 foreach my $v (@$addrs) {
47 die "email address '$v' is too long (> 512 characters)\n"
51 delete($list->{$name}->{$v});
53 if ($v =~ m/[[:^ascii:]]/) {
54 die "email address '$v' contains invalid characters\n";
56 $list->{$name}->{$v} = 1;
57 delete ($list->{$oname}->{$v});
61 my $wlist = $dbh->quote(join (',', keys %{$list->{WL
}}) || '');
62 my $blist = $dbh->quote(join (',', keys %{$list->{BL
}}) || '');
66 die "whitelist size exceeds limit (> $maxlen bytes)\n"
67 if length($wlist) > $maxlen;
68 die "blacklist size exceeds limit (> $maxlen bytes)\n"
69 if length($blist) > $maxlen;
72 my $queries = "DELETE FROM UserPrefs WHERE pmail = $qu AND (Name = 'WL' OR Name = 'BL');";
73 if (scalar(keys %{$list->{WL
}})) {
75 "INSERT INTO UserPrefs (PMail, Name, Data, MTime) " .
76 "VALUES ($qu, 'WL', $wlist, EXTRACT (EPOCH FROM now()));";
78 if (scalar(keys %{$list->{BL
}})) {
80 "INSERT INTO UserPrefs (PMail, Name, Data, MTime) " .
81 "VALUES ($qu, 'BL', $blist, EXTRACT (EPOCH FROM now()));";
86 my $values = [ keys %{$list->{$name}} ];
91 sub deliver_quarantined_mail
{
92 my ($dbh, $ref, $receiver) = @_;
94 my $filename = $ref->{file
};
95 my $spooldir = $PMG::MailQueue
::spooldir
;
96 my $path = "$spooldir/$filename";
98 my $id = 'C' . $ref->{cid
} . 'R' . $ref->{rid
} . 'T' . $ref->{ticketid
};;
100 my $sender = 'postmaster'; # notify postmaster if something fails
105 my $smtp = Net
::SMTP-
>new ('127.0.0.1', Port
=> 10025, Hello
=> 'quarantine') ||
106 die "unable to connect to localhost at port 10025\n";
110 if (!$smtp->mail($sender)) {
111 die sprintf("smtp from error - got: %s %s\n", $smtp->code, $smtp->message);
114 if (!$smtp->to($receiver)) {
115 die sprintf("smtp to error - got: %s %s\n", $smtp->code, $smtp->message);
122 open(my $fh, '<', $path) || die "unable to open file '$path' - $!\n";
124 while (defined(my $line = <$fh>)) {
126 if ($header && ($line =~ m/^\s*$/)) {
130 # skip Delivered-To and Return-Path (avoid problem with postfix
131 # forwarding loop detection (man local))
132 next if ($header && (($line =~ m/^Delivered-To:/i) || ($line =~ m/^Return-Path:/i)));
134 # rfc821 requires this
135 $line =~ s/^\./\.\./mg;
136 $smtp->datasend("$line\n");
140 if ($smtp->dataend()) {
141 my (@msgs) = $smtp->message;
142 my ($last_msg) = $msgs[$#msgs];
143 ($resid) = $last_msg =~ m/Ok: queued as ([0-9A-Z]+)/;
145 die sprintf("smtp error - got: %s %s\n", $smtp->code, $smtp->message);
148 die sprintf("sending data failed - got: %s %s\n", $smtp->code, $smtp->message);
151 my $sth = $dbh->prepare(
152 "UPDATE CMSReceivers SET Status='D', MTime = ? " .
153 "WHERE CMailStore_CID = ? AND CMailStore_RID = ? AND TicketID = ?");
154 $sth->execute(time(), $ref->{cid
}, $ref->{rid
}, $ref->{ticketid
});
159 $smtp->quit if $smtp;
162 my $msg = "deliver quarantined mail '$id' ($path) failed: $err";
167 syslog
('info', "delivered quarantined mail '$id' ($path)");
172 sub delete_quarantined_mail
{
173 my ($dbh, $ref) = @_;
175 my $filename = $ref->{file
};
176 my $spooldir = $PMG::MailQueue
::spooldir
;
177 my $path = "$spooldir/$filename";
179 my $id = 'C' . $ref->{cid
} . 'R' . $ref->{rid
} . 'T' . $ref->{ticketid
};;
182 my $sth = $dbh->prepare(
183 "UPDATE CMSReceivers SET Status='D', MTime = ? WHERE " .
184 "CMailStore_CID = ? AND CMailStore_RID = ? AND TicketID = ?");
185 $sth->execute (time(), $ref->{cid
}, $ref->{rid
}, $ref->{ticketid
});
189 my $msg = "delete quarantined mail '$id' ($path) failed: $err";
190 syslog
('err', $msg);
194 syslog
('info', "marked quarantined mail '$id' as deleted ($path)");