]> git.proxmox.com Git - pmg-api.git/blob - src/PMG/Quarantine.pm
api: acme: add eab parameters
[pmg-api.git] / src / PMG / Quarantine.pm
1 package PMG::Quarantine;
2
3 use strict;
4 use warnings;
5 use Encode qw(encode);
6
7 use PVE::SafeSyslog;
8 use PVE::Tools;
9
10 use PMG::Utils;
11 use PMG::RuleDB;
12 use PMG::MailQueue;
13 use PMG::MIMEUtils;
14
15 sub add_to_blackwhite {
16 my ($dbh, $username, $listname, $addrs, $delete) = @_;
17
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));
21
22 my $sth = $dbh->prepare(
23 "SELECT * FROM UserPrefs WHERE pmail = $qu AND (Name = 'BL' OR Name = 'WL')");
24 $sth->execute();
25
26 my $list = { 'WL' => {}, 'BL' => {} };
27
28 while (my $ref = $sth->fetchrow_hashref()) {
29 my $data = PMG::Utils::try_decode_utf8($ref->{data});
30 $data =~ s/[,;]/ /g;
31 my @alist = split('\s+', $data);
32
33 my $tmp = {};
34 foreach my $a (@alist) {
35 if ($a =~ m/^[^\s\\\@]+(?:\@[^\s\/\\\@]+)?$/) {
36 $tmp->{$a} = 1;
37 }
38 }
39
40 $list->{$ref->{name}} = $tmp;
41 }
42
43 $sth->finish;
44
45 if ($addrs) {
46
47 foreach my $v (@$addrs) {
48 die "email address '$v' is too long (> 512 characters)\n"
49 if length($v) > 512;
50
51 if ($delete) {
52 delete($list->{$name}->{$v});
53 } else {
54 if ($v =~ m/[\s\\]/) {
55 die "email address '$v' contains invalid characters\n";
56 }
57 $list->{$name}->{$v} = 1;
58 delete ($list->{$oname}->{$v});
59 }
60 }
61
62 my $wlist = $dbh->quote(encode('UTF-8', join (',', keys %{$list->{WL}})) || '');
63 my $blist = $dbh->quote(encode('UTF-8', join (',', keys %{$list->{BL}})) || '');
64
65 if (!$delete) {
66 my $maxlen = 200000;
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;
71 }
72
73 my $queries = "DELETE FROM UserPrefs WHERE pmail = $qu AND (Name = 'WL' OR Name = 'BL');";
74 if (scalar(keys %{$list->{WL}})) {
75 $queries .=
76 "INSERT INTO UserPrefs (PMail, Name, Data, MTime) " .
77 "VALUES ($qu, 'WL', $wlist, EXTRACT (EPOCH FROM now())::INTEGER);";
78 }
79 if (scalar(keys %{$list->{BL}})) {
80 $queries .=
81 "INSERT INTO UserPrefs (PMail, Name, Data, MTime) " .
82 "VALUES ($qu, 'BL', $blist, EXTRACT (EPOCH FROM now())::INTEGER);";
83 }
84 $dbh->do($queries);
85 }
86
87 my $values = [ keys %{$list->{$name}} ];
88
89 return $values;
90 }
91
92 sub deliver_quarantined_mail {
93 my ($dbh, $ref, $receiver) = @_;
94
95 my $filename = $ref->{file};
96 my $spooldir = $PMG::MailQueue::spooldir;
97 my $path = "$spooldir/$filename";
98
99 my $id = 'C' . $ref->{cid} . 'R' . $ref->{rid} . 'T' . $ref->{ticketid};;
100
101 my $parser = PMG::MIMEUtils::new_mime_parser({
102 nested => 1,
103 decode_bodies => 0,
104 extract_uuencode => 0,
105 dumpdir => "/tmp/.quarantine-$id-$receiver-$$/",
106 });
107
108 my $entity = $parser->parse_open("$path");
109 PMG::MIMEUtils::fixup_multipart($entity);
110
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');
115
116 my $sender = 'postmaster'; # notify postmaster if something fails
117
118 eval {
119 my ($qid, $code, $mess) = PMG::Utils::reinject_local_mail(
120 $entity, $sender, [$receiver], undef, 'quarantine');
121
122 if (!$qid) {
123 die "$mess\n";
124 }
125
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});
130 $sth->finish;
131 };
132 my $err = $@;
133 if ($err) {
134 my $msg = "deliver quarantined mail '$id' ($path) failed: $err";
135 syslog('err', $msg);
136 die "$msg\n";
137 }
138
139 syslog('info', "delivered quarantined mail '$id' ($path)");
140
141 return 1;
142 }
143
144 sub delete_quarantined_mail {
145 my ($dbh, $ref) = @_;
146
147 my $filename = $ref->{file};
148 my $spooldir = $PMG::MailQueue::spooldir;
149 my $path = "$spooldir/$filename";
150
151 my $id = 'C' . $ref->{cid} . 'R' . $ref->{rid} . 'T' . $ref->{ticketid};;
152
153 eval {
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});
158 $sth->finish;
159 };
160 if (my $err = $@) {
161 my $msg = "delete quarantined mail '$id' ($path) failed: $err";
162 syslog ('err', $msg);
163 die "$msg\n";
164 }
165
166 syslog ('info', "marked quarantined mail '$id' as deleted ($path)");
167
168 return 1;
169 }
170
171
172 1;