| 1 |
#!perl -w |
| 2 |
|
| 3 |
=head1 NAME |
| 4 |
|
| 5 |
milter |
| 6 |
|
| 7 |
=head1 DESCRIPTION |
| 8 |
|
| 9 |
This plugin allows you to attach to milter filters (yes, those written for |
| 10 |
sendmail) as though they were qpsmtpd plugins. |
| 11 |
|
| 12 |
In order to do this you need the C<Net::Milter> module from CPAN. |
| 13 |
|
| 14 |
=head1 CONFIG |
| 15 |
|
| 16 |
It takes two required parameters - a milter name (for logging) and the port |
| 17 |
to connect to on the localhost. This can also contain a hostname if |
| 18 |
the filter is on another machine: |
| 19 |
|
| 20 |
milter Brightmail 5513 |
| 21 |
|
| 22 |
or |
| 23 |
|
| 24 |
milter Brightmail bmcluster:5513 |
| 25 |
|
| 26 |
This plugin has so far only been tested with Brightmail's milter module. |
| 27 |
|
| 28 |
=cut |
| 29 |
|
| 30 |
use Net::Milter; |
| 31 |
use Qpsmtpd::Constants; |
| 32 |
no warnings; |
| 33 |
|
| 34 |
sub register { |
| 35 |
my ($self, $qp, @args) = @_; |
| 36 |
|
| 37 |
die "Invalid milter setup args: '@args'" unless @args > 1; |
| 38 |
my ($name, $port) = @args; |
| 39 |
my $host = '127.0.0.1'; |
| 40 |
if ($port =~ s/^(.*)://) { |
| 41 |
$host = $1; |
| 42 |
} |
| 43 |
|
| 44 |
$self->{name} = $name; |
| 45 |
$self->{host} = $host; |
| 46 |
$self->{port} = $port; |
| 47 |
|
| 48 |
} |
| 49 |
|
| 50 |
sub hook_disconnect { |
| 51 |
my ($self) = @_; |
| 52 |
|
| 53 |
my $milter = $self->connection->notes('milter') || return DECLINED; |
| 54 |
$milter->send_quit(); |
| 55 |
|
| 56 |
$self->connection->notes('spam', undef); |
| 57 |
$self->connection->notes('milter', undef); |
| 58 |
|
| 59 |
return DECLINED; |
| 60 |
} |
| 61 |
|
| 62 |
sub check_results { |
| 63 |
my ($self, $transaction, $where, @results) = @_; |
| 64 |
foreach my $result (@results) { |
| 65 |
next if $result->{action} eq 'continue'; |
| 66 |
$self->log(LOGINFO, |
| 67 |
"milter $self->{name} result action: $result->{action}"); |
| 68 |
if ($result->{action} eq 'reject') { |
| 69 |
die( |
| 70 |
"Rejected at $where by $self->{name} milter ($result->{explanation})"); |
| 71 |
} |
| 72 |
elsif ($result->{action} eq 'add') { |
| 73 |
if ($result->{header} eq 'body') { |
| 74 |
$transaction->body_write($result->{value}); |
| 75 |
} |
| 76 |
else { |
| 77 |
push @{$transaction->notes('milter_header_changes')->{add}}, |
| 78 |
[$result->{header}, $result->{value}]; |
| 79 |
} |
| 80 |
} |
| 81 |
elsif ($result->{action} eq 'delete') { |
| 82 |
push @{$transaction->notes('milter_header_changes')->{delete}}, |
| 83 |
$result->{header}; |
| 84 |
} |
| 85 |
elsif ($result->{action} eq 'accept') { |
| 86 |
|
| 87 |
# TODO - figure out what this is used for |
| 88 |
} |
| 89 |
elsif ($result->{action} eq 'replace') { |
| 90 |
push @{$transaction->notes('milter_header_changes')->{replace}}, |
| 91 |
[$result->{header}, $result->{value}]; |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
sub hook_connect { |
| 97 |
my ($self, $transaction) = @_; |
| 98 |
|
| 99 |
$self->log(LOGDEBUG, |
| 100 |
"milter $self->{name} opening connection to milter backend"); |
| 101 |
my $milter = Net::Milter->new(); |
| 102 |
$milter->open($self->{host}, $self->{port}, 'tcp'); |
| 103 |
$milter->protocol_negotiation(); |
| 104 |
|
| 105 |
$self->connection->notes(milter => $milter); |
| 106 |
|
| 107 |
$self->connection->notes( |
| 108 |
milter_header_changes => {add => [], delete => [], replace => [],}); |
| 109 |
my $remote_ip = $self->qp->connection->remote_ip; |
| 110 |
my $remote_host = $self->qp->connection->remote_host; |
| 111 |
$self->log(LOGDEBUG, |
| 112 |
"milter $self->{name} checking connect from $remote_host\[$remote_ip\]" |
| 113 |
); |
| 114 |
|
| 115 |
eval { |
| 116 |
$self->check_results( |
| 117 |
$transaction, |
| 118 |
"connection", |
| 119 |
$milter->send_connect( |
| 120 |
$remote_host, 'tcp4', 0, $remote_ip |
| 121 |
) |
| 122 |
); |
| 123 |
}; |
| 124 |
$self->connection->notes('spam', $@) if $@; |
| 125 |
|
| 126 |
return DECLINED; |
| 127 |
} |
| 128 |
|
| 129 |
sub hook_helo { |
| 130 |
my ($self, $transaction) = @_; |
| 131 |
|
| 132 |
if (my $txt = $self->connection->notes('spam')) { |
| 133 |
return DENY, $txt; |
| 134 |
} |
| 135 |
|
| 136 |
my $milter = $self->connection->notes('milter'); |
| 137 |
|
| 138 |
my $helo = $self->qp->connection->hello; |
| 139 |
my $host = $self->qp->connection->hello_host; |
| 140 |
|
| 141 |
$self->log(LOGDEBUG, "milter $self->{name} checking HELO $host"); |
| 142 |
|
| 143 |
eval { |
| 144 |
$self->check_results($transaction, "HELO", $milter->send_helo($host)); |
| 145 |
}; |
| 146 |
return (DENY, $@) if $@; |
| 147 |
|
| 148 |
return DECLINED; |
| 149 |
} |
| 150 |
|
| 151 |
sub hook_mail { |
| 152 |
my ($self, $transaction, $address, %param) = @_; |
| 153 |
|
| 154 |
my $milter = $self->connection->notes('milter'); |
| 155 |
|
| 156 |
$self->log(LOGDEBUG, |
| 157 |
"milter $self->{name} checking MAIL FROM " . $address->format); |
| 158 |
eval { |
| 159 |
$self->check_results($transaction, "MAIL FROM", |
| 160 |
$milter->send_mail_from($address->format)); |
| 161 |
}; |
| 162 |
return (DENY, $@) if $@; |
| 163 |
|
| 164 |
return DECLINED; |
| 165 |
} |
| 166 |
|
| 167 |
sub hook_rcpt { |
| 168 |
my ($self, $transaction, $address, %param) = @_; |
| 169 |
|
| 170 |
my $milter = $self->connection->notes('milter'); |
| 171 |
|
| 172 |
$self->log(LOGDEBUG, |
| 173 |
"milter $self->{name} checking RCPT TO " . $address->format); |
| 174 |
|
| 175 |
eval { |
| 176 |
$self->check_results($transaction, "RCPT TO", |
| 177 |
$milter->send_rcpt_to($address->format)); |
| 178 |
}; |
| 179 |
return (DENY, $@) if $@; |
| 180 |
|
| 181 |
return DECLINED; |
| 182 |
} |
| 183 |
|
| 184 |
sub hook_data_post { |
| 185 |
my ($self, $transaction) = @_; |
| 186 |
|
| 187 |
my $milter = $self->connection->notes('milter'); |
| 188 |
|
| 189 |
$self->log(LOGDEBUG, "milter $self->{name} checking headers"); |
| 190 |
|
| 191 |
my $headers = $transaction->header(); # Mail::Header object |
| 192 |
foreach my $h ($headers->tags) { |
| 193 |
|
| 194 |
# munge these headers because milters prefer them this way |
| 195 |
$h =~ s/\b(\w)/\U$1/g; |
| 196 |
$h =~ s/\bid\b/ID/g; |
| 197 |
foreach my $val ($headers->get($h)) { |
| 198 |
|
| 199 |
# $self->log(LOGDEBUG, "milter $self->{name} checking header: $h: $val"); |
| 200 |
eval { |
| 201 |
$self->check_results($transaction, "header $h", |
| 202 |
$milter->send_header($h, $val)); |
| 203 |
}; |
| 204 |
return (DENY, $@) if $@; |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
eval { |
| 209 |
$self->check_results($transaction, "end headers", |
| 210 |
$milter->send_end_headers()); |
| 211 |
}; |
| 212 |
return (DENY, $@) if $@; |
| 213 |
|
| 214 |
$transaction->body_resetpos; |
| 215 |
|
| 216 |
# skip past headers |
| 217 |
while (my $line = $transaction->body_getline) { |
| 218 |
$line =~ s/\r?\n//; |
| 219 |
$line =~ s/\s*$//; |
| 220 |
last unless length($line); |
| 221 |
} |
| 222 |
|
| 223 |
$self->log(LOGDEBUG, "milter $self->{name} checking body"); |
| 224 |
|
| 225 |
my $data = ''; |
| 226 |
while (my $line = $transaction->body_getline) { |
| 227 |
$data .= $line; |
| 228 |
if (length($data) > 60000) { |
| 229 |
eval { |
| 230 |
$self->check_results($transaction, "body", |
| 231 |
$milter->send_body($data)); |
| 232 |
}; |
| 233 |
return (DENY, $@) if $@; |
| 234 |
$data = ''; |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
if (length($data)) { |
| 239 |
eval { |
| 240 |
$self->check_results($transaction, "body", |
| 241 |
$milter->send_body($data)); |
| 242 |
}; |
| 243 |
return (DENY, $@) if $@; |
| 244 |
$data = ''; |
| 245 |
} |
| 246 |
|
| 247 |
eval { |
| 248 |
$self->check_results($transaction, "end of DATA", |
| 249 |
$milter->send_end_body()); |
| 250 |
}; |
| 251 |
return (DENY, $@) if $@; |
| 252 |
|
| 253 |
my $milter_header_changes = $transaction->notes('milter_header_changes'); |
| 254 |
|
| 255 |
foreach my $add (@{$milter_header_changes->{add}}) { |
| 256 |
$headers->add($add->[0], $add->[1]); |
| 257 |
} |
| 258 |
foreach my $del (@{$milter_header_changes->{'delete'}}) { |
| 259 |
$headers->delete($del); |
| 260 |
} |
| 261 |
foreach my $repl (@{$milter_header_changes->{replace}}) { |
| 262 |
$headers->replace($repl->[0], $repl->[1]); |
| 263 |
} |
| 264 |
|
| 265 |
return DECLINED; |
| 266 |
} |