| 1 |
#!/usr/bin/perl -T |
| 2 |
# SPDX-License-Identifier: BSD-2-Clause |
| 3 |
|
| 4 |
#------------------------------------------------------------------------------ |
| 5 |
# This is amavisd-release, an EXAMPLE quarantine release utility program. |
| 6 |
# It uses AM.PDP protocol to send a request to amavisd daemon to release |
| 7 |
# a quarantined mail message. |
| 8 |
# |
| 9 |
# Author: Mark Martinec <[email protected]> |
| 10 |
# |
| 11 |
# Copyright (c) 2005-2014, Mark Martinec |
| 12 |
# All rights reserved. |
| 13 |
# |
| 14 |
# Redistribution and use in source and binary forms, with or without |
| 15 |
# modification, are permitted provided that the following conditions |
| 16 |
# are met: |
| 17 |
# 1. Redistributions of source code must retain the above copyright notice, |
| 18 |
# this list of conditions and the following disclaimer. |
| 19 |
# 2. Redistributions in binary form must reproduce the above copyright notice, |
| 20 |
# this list of conditions and the following disclaimer in the documentation |
| 21 |
# and/or other materials provided with the distribution. |
| 22 |
# |
| 23 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 24 |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 25 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 26 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 27 |
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 28 |
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 29 |
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 30 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 31 |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 32 |
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 33 |
# POSSIBILITY OF SUCH DAMAGE. |
| 34 |
# |
| 35 |
# The views and conclusions contained in the software and documentation are |
| 36 |
# those of the authors and should not be interpreted as representing official |
| 37 |
# policies, either expressed or implied, of the Jozef Stefan Institute. |
| 38 |
|
| 39 |
# (the above license is the 2-clause BSD license, also known as |
| 40 |
# a "Simplified BSD License", and pertains to this program only) |
| 41 |
# |
| 42 |
# Patches and problem reports are welcome. |
| 43 |
# The latest version of this program is available at: |
| 44 |
# http://www.ijs.si/software/amavisd/ |
| 45 |
#------------------------------------------------------------------------------ |
| 46 |
|
| 47 |
# Usage: |
| 48 |
# amavisd-release mail_file secret_id [alt_recip1 alt_recip2 ..] |
| 49 |
# |
| 50 |
# To be placed in amavisd.conf: |
| 51 |
# $interface_policy{'SOCK'} = 'AM.PDP'; |
| 52 |
# $policy_bank{'AM.PDP'} = { protocol=>'AM.PDP' }; |
| 53 |
# $unix_socketname = '/var/amavis/amavisd.sock'; |
| 54 |
# or: |
| 55 |
# $interface_policy{'9998'} = 'AM.PDP'; |
| 56 |
# $policy_bank{'AM.PDP'} = { protocol=>'AM.PDP' }; |
| 57 |
# $inet_socket_port = [10024,9998]; |
| 58 |
# |
| 59 |
# To obtain secret_id and quar_type belonging to some mail_id: |
| 60 |
# $ mysql mail_log -e \ |
| 61 |
# 'select secret_id,quar_type,content from msgs where mail_id="W+7uJyXUjw32"' |
| 62 |
# |
| 63 |
# If secret_id is not available, administrator may choose to skip checking |
| 64 |
# of secret_id in the amavisd daemon by setting a configuration variable |
| 65 |
# $auth_required_release to false (it defaults to true). If the release |
| 66 |
# client program specifies a nonempty secret_id in the request, the secret_id |
| 67 |
# will be validated and a request will fail if not valid, regardless of the |
| 68 |
# setting of $auth_required_release. Turning off a requirement for a valid |
| 69 |
# secret_id widens the right to release to anyone who can connect to amavisd |
| 70 |
# socket (Unix or inet). Access to the socket therefore needs to be restricted |
| 71 |
# using socket protection (unix socket) or @inet_acl (for inet socket). |
| 72 |
|
| 73 |
|
| 74 |
use warnings; |
| 75 |
use warnings FATAL => 'utf8'; |
| 76 |
no warnings 'uninitialized'; |
| 77 |
use strict; |
| 78 |
use re 'taint'; |
| 79 |
use IO::Socket; |
| 80 |
use Time::HiRes (); |
| 81 |
|
| 82 |
use vars qw($VERSION); $VERSION = 2.002; |
| 83 |
use vars qw($log_level $socketname $have_inet4 $have_inet6 $have_socket_ip); |
| 84 |
|
| 85 |
BEGIN { |
| 86 |
### USER CONFIGURABLE: |
| 87 |
|
| 88 |
$log_level = 1; |
| 89 |
# $socketname = '127.0.0.1:9998'; |
| 90 |
# $socketname = '[::1]:9998'; |
| 91 |
$socketname = '/var/amavis/amavisd.sock'; |
| 92 |
|
| 93 |
### END OF USER CONFIGURABLE |
| 94 |
} |
| 95 |
|
| 96 |
BEGIN { |
| 97 |
# no need to load INET/INET6 modules when a socket is a Unix socket |
| 98 |
return if $socketname =~ m{^/}; |
| 99 |
|
| 100 |
# prefer using IO::Socket::IP if it exists, otherwise |
| 101 |
# fall back to IO::Socket::INET6 or IO::Socket::INET as appropriate |
| 102 |
# |
| 103 |
$have_socket_ip = eval { |
| 104 |
require IO::Socket::IP; |
| 105 |
}; |
| 106 |
|
| 107 |
$have_inet4 = # can we make a PF_INET socket? |
| 108 |
$have_socket_ip ? eval { |
| 109 |
my $sock = IO::Socket::IP->new(LocalAddr => '0.0.0.0', Proto => 'udp'); |
| 110 |
$sock->close or die "error closing inet6 socket: $!" if $sock; |
| 111 |
$sock ? 1 : undef; |
| 112 |
} : eval { |
| 113 |
require IO::Socket::INET; |
| 114 |
my $sock = IO::Socket::INET->new(LocalAddr => '0.0.0.0', Proto => 'udp'); |
| 115 |
$sock->close or die "error closing inet socket: $!" if $sock; |
| 116 |
$sock ? 1 : undef; |
| 117 |
}; |
| 118 |
|
| 119 |
$have_inet6 = # can we make a PF_INET6 socket? |
| 120 |
$have_socket_ip ? eval { |
| 121 |
my $sock = IO::Socket::IP->new(LocalAddr => '::', Proto => 'udp'); |
| 122 |
$sock->close or die "error closing inet6 socket: $!" if $sock; |
| 123 |
$sock ? 1 : undef; |
| 124 |
} : eval { |
| 125 |
require IO::Socket::INET6; |
| 126 |
my $sock = IO::Socket::INET6->new(LocalAddr => '::', Proto => 'udp'); |
| 127 |
$sock->close or die "error closing inet6 socket: $!" if $sock; |
| 128 |
$sock ? 1 : undef; |
| 129 |
}; |
| 130 |
|
| 131 |
1; |
| 132 |
} # end BEGIN |
| 133 |
|
| 134 |
sub sanitize_str { |
| 135 |
my($str, $keep_eol) = @_; |
| 136 |
my(%map) = ("\r" => '\\r', "\n" => '\\n', "\f" => '\\f', "\t" => '\\t', |
| 137 |
"\b" => '\\b', "\e" => '\\e', "\\" => '\\\\'); |
| 138 |
if ($keep_eol) { |
| 139 |
$str =~ s/([^\012\040-\133\135-\176])/ # and \240-\376 ? |
| 140 |
exists($map{$1}) ? $map{$1} : |
| 141 |
sprintf(ord($1)>255 ? '\\x{%04x}' : '\\%03o', ord($1))/eg; |
| 142 |
} else { |
| 143 |
$str =~ s/([^\040-\133\135-\176])/ # and \240-\376 ? |
| 144 |
exists($map{$1}) ? $map{$1} : |
| 145 |
sprintf(ord($1)>255 ? '\\x{%04x}' : '\\%03o', ord($1))/eg; |
| 146 |
} |
| 147 |
$str; |
| 148 |
} |
| 149 |
|
| 150 |
sub ll($) { |
| 151 |
my($level) = @_; |
| 152 |
$level <= $log_level; |
| 153 |
} |
| 154 |
|
| 155 |
sub do_log($$;@) { |
| 156 |
my($level, $errmsg, @args) = @_; |
| 157 |
if ($level <= $log_level) { |
| 158 |
$errmsg = sprintf($errmsg,@args) if @args; |
| 159 |
print STDERR sanitize_str($errmsg)."\n"; # ignoring I/O status |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
sub proto_decode($) { |
| 164 |
my($str) = @_; |
| 165 |
$str =~ s/%([0-9a-fA-F]{2})/pack("C",hex($1))/eg; |
| 166 |
$str; |
| 167 |
} |
| 168 |
|
| 169 |
sub proto_encode($@) { |
| 170 |
my($attribute_name,@strings) = @_; local($1); |
| 171 |
$attribute_name =~ # encode all but alfanumerics, '_' and '-' |
| 172 |
s/([^0-9a-zA-Z_-])/sprintf("%%%02x",ord($1))/eg; |
| 173 |
for (@strings) { # encode % and nonprintables |
| 174 |
s/([^\041-\044\046-\176])/sprintf("%%%02x",ord($1))/eg; |
| 175 |
} |
| 176 |
$attribute_name . '=' . join(' ',@strings); |
| 177 |
} |
| 178 |
|
| 179 |
sub ask_amavisd($$) { |
| 180 |
my($sock,$query_ref) = @_; |
| 181 |
my(@encoded_query) = |
| 182 |
map { /^([^=]+)=(.*)\z/s; proto_encode($1,$2) } @$query_ref; |
| 183 |
do_log(2,'> '.$_) for @encoded_query; |
| 184 |
$sock->print( map { $_."\015\012" } (@encoded_query,'') ) |
| 185 |
or die "Can't write response to socket: $!"; |
| 186 |
$sock->flush or die "Can't flush on socket: $!"; |
| 187 |
my(%attr); local($1,$2,$3); |
| 188 |
local($/) = "\015\012"; # set line terminator to CRLF |
| 189 |
# must not use \r and \n, which may not be \015 and \012 on certain platforms |
| 190 |
do_log(2,"waiting for response"); |
| 191 |
while(<$sock>) { |
| 192 |
last if /^\015\012\z/; # end of response |
| 193 |
if (/^ ([^=\000\012]*?) (=|:[ \t]*) ([^\012]*?) \015\012 \z/xsi) { |
| 194 |
my $attr_name = proto_decode($1); |
| 195 |
my $attr_val = proto_decode($3); |
| 196 |
if (!exists $attr{$attr_name}) { $attr{$attr_name} = [] } |
| 197 |
push(@{$attr{$attr_name}}, $attr_val); |
| 198 |
} |
| 199 |
} |
| 200 |
if (!defined($_) && $! != 0) { die "read from socket failed: $!" } |
| 201 |
\%attr; |
| 202 |
} |
| 203 |
|
| 204 |
sub release_file($$$@) { |
| 205 |
my($sock,$mail_file,$secret_id,@alt_recips) = @_; |
| 206 |
my($fn_path,$fn_prefix,$mail_id,$fn_suffix,$part_tag); local($1,$2,$3,$4); |
| 207 |
$part_tag = $1 if $mail_file =~ s/ \[ ( [^\]]* ) \] \z//xs; |
| 208 |
if ($mail_file =~ m{^ ([^/].*/)? ([A-Z0-9][A-Z0-9._-]*[_-])? |
| 209 |
([A-Z0-9][A-Z0-9_+-]{10,14}[A-Z0-9]) (\.gz)? \z}xsi) { |
| 210 |
($fn_path,$fn_prefix,$mail_id,$fn_suffix) = ($1,$2,$3,$4); |
| 211 |
} elsif ($mail_file =~ m{^ ([^/].*/)? () ([A-Za-z0-9$._=+-]+?) (\.gz)?\z}xs){ |
| 212 |
($fn_path,$fn_prefix,$mail_id,$fn_suffix) = ($1,$2,$3,$4); # old style |
| 213 |
} else { |
| 214 |
usage("Invalid quarantine ID: $mail_file"); |
| 215 |
} |
| 216 |
my $quar_type = $fn_suffix eq '.gz' ? 'Z' : $fn_path ne '' ? 'F' : ''; |
| 217 |
my $request_type = $0 =~ /\breport\z/i ? 'report' |
| 218 |
: $0 =~ /\brequeue\z/i ? 'requeue' : 'release'; |
| 219 |
my(@query); |
| 220 |
push(@query, "request=$request_type"); |
| 221 |
push(@query, "mail_id=$mail_id"); |
| 222 |
push(@query, "quar_type=$quar_type") if $quar_type ne ''; |
| 223 |
push(@query, "secret_id=$secret_id") if $secret_id ne ''; |
| 224 |
push(@query, "mail_file=$mail_file"); # ignored with an SQL quarantine |
| 225 |
push(@query, "partition_tag=$part_tag") if $part_tag ne ''; |
| 226 |
if (@alt_recips) { # override original recipients if list is nonempty |
| 227 |
push(@query, map {"recipient=$_"} @alt_recips); |
| 228 |
} |
| 229 |
my $attr_ref = ask_amavisd($sock,\@query); |
| 230 |
$attr_ref && %$attr_ref or die "Invalid response received"; |
| 231 |
if (ll(2)) { |
| 232 |
for my $attr_name (keys %$attr_ref) { |
| 233 |
for my $attr_val (@{$attr_ref->{$attr_name}}) { |
| 234 |
do_log(2,"< %s=%s", $attr_name,$attr_val); |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
do_log(0,$_) for (@{$attr_ref->{'setreply'}}); |
| 239 |
# may do another release request on the same session if needed ... |
| 240 |
} |
| 241 |
|
| 242 |
sub usage(;$) { |
| 243 |
my($msg) = @_; |
| 244 |
print STDERR $msg,"\n\n" if $msg ne ''; |
| 245 |
my $prog = $0; $prog =~ s{^.*/(?=[^/]+\z)}{}; |
| 246 |
print STDERR "$prog version $VERSION\n"; |
| 247 |
die "Usage: \$ $prog mail_file [secret_id [alt_recip1 alt_recip2 ...]]\n". |
| 248 |
" or to read request lines from stdin: \$ $prog -\n"; |
| 249 |
} |
| 250 |
|
| 251 |
# Main program starts here |
| 252 |
|
| 253 |
my($sock,$mail_file,$secret_id); |
| 254 |
@ARGV >= 1 or usage("Not enough arguments"); |
| 255 |
$mail_file = shift(@ARGV); # quarantine file id or '-' |
| 256 |
if ($mail_file eq '-') { @ARGV==0 or usage("Extra arguments after '-'") } |
| 257 |
|
| 258 |
my($peeraddress, $peerport, $is_inet); local($1,$2,$3); |
| 259 |
if ($socketname =~ m{^/}) { # simpleminded: unix vs. inet |
| 260 |
$is_inet = 0; |
| 261 |
} elsif ($socketname =~ /^(?: \[ ([^\]]*) \] | ([^:]*) ) : ([^:]*)/sx) { |
| 262 |
$peeraddress = defined $1 ? $1 : $2; $peerport = $3; $is_inet = 1; |
| 263 |
} elsif ($socketname =~ /^(?: \[ ([^\]]*) \] | ([0-9a-fA-F.:]+) ) \z/sx) { |
| 264 |
$peeraddress = defined $1 ? $1 : $2; $is_inet = 1; |
| 265 |
} else { # probably a syntax error, but let's assume it is a Unix socket |
| 266 |
$is_inet = 0; |
| 267 |
} |
| 268 |
if (!$is_inet) { # unix socket |
| 269 |
do_log(3,"connecting to a UNIX socket %s", $socketname); |
| 270 |
$sock = IO::Socket::UNIX->new(Type => SOCK_STREAM); |
| 271 |
$sock or die "Can't create UNIX socket: $!"; |
| 272 |
$sock->connect( pack_sockaddr_un($socketname) ) |
| 273 |
or die "Can't connect to UNIX socket $socketname: $!"; |
| 274 |
} else { # inet socket |
| 275 |
$peerport or die "port number not specified in \$socketname: $socketname\n"; |
| 276 |
my $module = $have_socket_ip ? 'IO::Socket::IP' |
| 277 |
: $have_inet4 && (!$have_inet6 || |
| 278 |
$peeraddress=~/^\d+\.\d+\.\d+\.\d+\z/) ? 'IO::Socket::INET' |
| 279 |
: 'IO::Socket::INET6'; |
| 280 |
my(%args) = (Type => SOCK_STREAM, Proto => 'tcp', |
| 281 |
PeerAddr => $peeraddress, PeerPort => $peerport); |
| 282 |
do_log(3,"connecting using %s to [%s]:%s", |
| 283 |
$module, $peeraddress, $peerport); |
| 284 |
if ($have_socket_ip) { # $module eq 'IO::Socket::IP' |
| 285 |
# inet or inet6 socket, let IO::Socket::IP handle dirty details |
| 286 |
$sock = IO::Socket::IP->new(%args); |
| 287 |
# note: the IO::Socket::IP constructor provides error message in $@ |
| 288 |
$sock or die "Can't connect to socket $socketname using $module: $@\n"; |
| 289 |
} elsif ($module eq 'IO::Socket::INET') { # inet socket (IPv4) |
| 290 |
$sock = IO::Socket::INET->new(%args); |
| 291 |
$sock or die "Can't connect to socket $socketname using $module: $!\n"; |
| 292 |
} else { # inet6 socket: no inet or IPv6 or unknown addr family |
| 293 |
$sock = IO::Socket::INET6->new(%args); |
| 294 |
$sock or die "Can't connect to socket $socketname using $module: $!\n"; |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
if ($mail_file eq '-') { |
| 299 |
undef $!; |
| 300 |
while (<>) { # read from STDIN: mail_file [secret_id [alt_recips...]] |
| 301 |
chomp; |
| 302 |
next if /^[ \t]*(#.*)?$/; # skip empty lines or comments |
| 303 |
my($mail_file, $secret_id, @alt_recips) = split(' '); |
| 304 |
release_file($sock,$mail_file,$secret_id,@alt_recips); |
| 305 |
} |
| 306 |
$!==0 or die "Error reading from STDIN: $!"; |
| 307 |
} else { |
| 308 |
# assume empty secret_id if the second arg looks like an e-mail addr |
| 309 |
$secret_id = $ARGV[0]=~/\@/ ? '' : shift(@ARGV); |
| 310 |
release_file($sock,$mail_file,$secret_id,@ARGV); |
| 311 |
} |
| 312 |
$sock->close or die "Error closing socket: $!"; |
| 313 |
close(STDIN) or die "Error closing STDIN: $!"; |
| 314 |
close(STDOUT) or die "Error closing STDOUT: $!"; |
| 315 |
close(STDERR) or die "Error closing STDERR: $!"; |