Debian Codegraph @

bin/amavisd-submit

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 #!/usr/bin/perl -T
2 # SPDX-License-Identifier: BSD-2-Clause
3
4 #------------------------------------------------------------------------------
5 # This is amavisd-submit, a simple demonstrational program, taking an email
6 # message on stdin and submiting it to amavisd daemon. It is functionally
7 # much like the old amavis.c helper program, except that it talks a new
8 # AM.PDP protocol with the amavisd daemon. See README.protocol for the
9 # description of AM.PDP protocol.
10 #
11 # Author: Mark Martinec <[email protected]>
12 #
13 # Copyright (c) 2004,2010-2014, Mark Martinec
14 # All rights reserved.
15 #
16 # Redistribution and use in source and binary forms, with or without
17 # modification, are permitted provided that the following conditions
18 # are met:
19 # 1. Redistributions of source code must retain the above copyright notice,
20 # this list of conditions and the following disclaimer.
21 # 2. Redistributions in binary form must reproduce the above copyright notice,
22 # this list of conditions and the following disclaimer in the documentation
23 # and/or other materials provided with the distribution.
24 #
25 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
29 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 # POSSIBILITY OF SUCH DAMAGE.
36 #
37 # The views and conclusions contained in the software and documentation are
38 # those of the authors and should not be interpreted as representing official
39 # policies, either expressed or implied, of the Jozef Stefan Institute.
40
41 # (the above license is the 2-clause BSD license, also known as
42 # a "Simplified BSD License", and pertains to this program only)
43 #
44 # Patches and problem reports are welcome.
45 # The latest version of this program is available at:
46 # http://www.ijs.si/software/amavisd/
47 #------------------------------------------------------------------------------
48
49 # Usage:
50 # amavisd-submit sender recip1 recip2 recip3 ... <email.msg
51 # (should run under the same GID as amavisd, to make files accessible to it)
52 #
53 # To be placed in amavisd.conf:
54 # $interface_policy{'SOCK'} = 'AM.PDP';
55 # $policy_bank{'AM.PDP'} = { protocol=>'AM.PDP' };
56 # $unix_socketname = '/var/amavis/amavisd.sock';
57
58 use warnings;
59 use warnings FATAL => 'utf8';
60 no warnings 'uninitialized';
61 use strict;
62 use re 'taint';
63 use IO::Socket;
64 use IO::File;
65 use File::Temp ();
66 use Time::HiRes ();
67
68 BEGIN {
69 use vars qw($VERSION); $VERSION = 2.101;
70 use vars qw($log_level $socketname $tempbase $io_socket_module_name);
71
72
73 ### USER CONFIGURABLE:
74
75 $log_level = 0;
76 $tempbase = '/var/amavis/tmp'; # where to create a temp directory with a msg
77
78 $socketname = '/var/amavis/amavisd.sock';
79 # $socketname = '127.0.0.1:9998';
80 # $socketname = '[::1]:9998';
81
82 ### END OF USER CONFIGURABLE
83
84
85 # load a suitable sockets module
86 if ($socketname =~ m{^/}) {
87 require IO::Socket::UNIX;
88 $io_socket_module_name = 'IO::Socket::UNIX';
89 } elsif (eval { require IO::Socket::IP }) {
90 # prefer using module IO::Socket::IP if available,
91 $io_socket_module_name = 'IO::Socket::IP';
92 } elsif (eval { require IO::Socket::INET6 }) {
93 # otherwise fall back to IO::Socket::INET6
94 $io_socket_module_name = 'IO::Socket::INET6';
95 } elsif (eval { require IO::Socket::INET }) {
96 $io_socket_module_name = 'IO::Socket::INET';
97 }
98 $io_socket_module_name or die "No suitable socket module available";
99 }
100
101 sub sanitize_str {
102 my($str, $keep_eol) = @_;
103 my(%map) = ("\r" => '\\r', "\n" => '\\n', "\f" => '\\f', "\t" => '\\t',
104 "\b" => '\\b', "\e" => '\\e', "\\" => '\\\\');
105 if ($keep_eol) {
106 $str =~ s/([^\012\040-\133\135-\176])/ # and \240-\376 ?
107 exists($map{$1}) ? $map{$1} :
108 sprintf(ord($1)>255 ? '\\x{%04x}' : '\\%03o', ord($1))/eg;
109 } else {
110 $str =~ s/([^\040-\133\135-\176])/ # and \240-\376 ?
111 exists($map{$1}) ? $map{$1} :
112 sprintf(ord($1)>255 ? '\\x{%04x}' : '\\%03o', ord($1))/eg;
113 }
114 $str;
115 }
116
117 sub ll($) {
118 my($level) = @_;
119 $level <= $log_level;
120 }
121
122 sub do_log($$;@) {
123 my($level, $errmsg, @args) = @_;
124 $errmsg = sprintf($errmsg,@args) if @args;
125 print STDERR sanitize_str($errmsg),"\n" if $level <= $log_level;
126 }
127
128 sub proto_decode($) {
129 my($str) = @_;
130 $str =~ s/%([0-9a-fA-F]{2})/pack("C",hex($1))/eg;
131 $str;
132 }
133
134 sub proto_encode($@) {
135 my($attribute_name,@strings) = @_; local($1);
136 $attribute_name =~ # encode all but alfanumerics, '_' and '-'
137 s/([^0-9a-zA-Z_-])/sprintf("%%%02x",ord($1))/eg;
138 for (@strings) { # encode % and nonprintables
139 s/([^\041-\044\046-\176])/sprintf("%%%02x",ord($1))/eg;
140 }
141 $attribute_name . '=' . join(' ',@strings);
142 }
143
144 sub ask_amavisd($$) {
145 my($sock,$query_ref) = @_;
146 my(@encoded_query) =
147 map { /^([^=]+)=(.*)\z/s; proto_encode($1,$2) } @$query_ref;
148 do_log(2, "> %s", $_) for @encoded_query;
149 $sock->print( map($_."\015\012", (@encoded_query,'')) )
150 or die "Can't write response to socket: $!";
151 $sock->flush or die "Can't flush on socket: $!";
152 my(%attr);
153 local($/) = "\015\012"; # set line terminator to CRLF
154 # must not use \r and \n, which may not be \015 and \012 on certain platforms
155 do_log(2, "waiting for response");
156 while(<$sock>) {
157 last if /^\015\012\z/; # end of response
158 if (/^ ([^=\000\012]*?) (=|:[ \t]*) ([^\012]*?) \015\012 \z/xsi) {
159 my $attr_name = proto_decode($1);
160 my $attr_val = proto_decode($3);
161 if (!exists $attr{$attr_name}) { $attr{$attr_name} = [] }
162 push(@{$attr{$attr_name}}, $attr_val);
163 }
164 }
165 if (!defined($_) && $! != 0) { die "read from socket failed: $!" }
166 \%attr;
167 }
168
169 sub usage(;$) {
170 my($msg) = @_;
171 print STDERR $msg,"\n\n" if $msg ne '';
172 my $prog = $0; $prog =~ s{^.*/(?=[^/]+\z)}{};
173 print STDERR "$prog version $VERSION\n";
174 die "Usage: \$ $prog sender recip1 recip2 ... < email.msg\n";
175 }
176
177 # Main program starts here
178
179 $SIG{INT} = sub { die "\n" }; # do the END code block when interrupted
180 $SIG{TERM} = sub { die "\n" }; # do the END code block when killed
181 umask(0027); # set our preferred umask
182
183 @ARGV >= 1 or usage("Not enough arguments");
184
185 my($sock, %sock_args);
186 if ($io_socket_module_name eq 'IO::Socket::UNIX') {
187 %sock_args = (Type => &SOCK_STREAM, Peer => $socketname);
188 } else {
189 %sock_args = (Type => &SOCK_STREAM, PeerAddr => $socketname);
190 }
191 do_log(2, "Connecting to %s using a module %s",
192 $socketname, $io_socket_module_name);
193 $sock = $io_socket_module_name->new(%sock_args)
194 or die "Can't connect to a $io_socket_module_name socket $socketname: $!\n";
195
196 my $tempdir = File::Temp::tempdir('amavis-XXXXXXXXXX', DIR => $tempbase);
197 defined $tempdir && $tempdir ne ''
198 or die "Can't create a temporary directory: $!";
199 chmod(0750, $tempdir)
200 or die "Can't change protection on directory $tempdir: $!";
201 my $fname = "$tempdir/email.txt";
202
203 # copy message from stdin to a file email.txt in the temporary directory
204
205 my $fh = IO::File->new;
206 $fh->open($fname, O_CREAT|O_EXCL|O_RDWR, 0640)
207 or die "Can't create file $fname: $!";
208 my($nbytes,$buff);
209 while (($nbytes=read(STDIN,$buff,32768)) > 0) {
210 $fh->print($buff) or die "Error writing to $fname: $!";
211 }
212 defined $nbytes or die "Error reading mail file: $!";
213 $fh->close or die "Error closing $fname: $!";
214 close(STDIN) or die "Error closing STDIN: $!";
215
216 my(@query) = (
217 'request=AM.PDP',
218 "mail_file=$fname",
219 "tempdir=$tempdir",
220 'tempdir_removed_by=server',
221 map("sender=<$_>", shift(@ARGV)),
222 map("recipient=<$_>", @ARGV),
223 # 'delivery_care_of=server',
224 # 'protocol_name=ESMTP',
225 # 'helo_name=b.example.com',
226 # 'client_address=::1',
227 );
228 my $attr_ref = ask_amavisd($sock,\@query);
229 if (ll(2)) {
230 for my $attr_name (keys %$attr_ref) {
231 for my $attr_val (@{$attr_ref->{$attr_name}}) {
232 do_log(2, "< %s=%s", $attr_name,$attr_val);
233 }
234 }
235 }
236 my($setreply,$exit_code);
237 $setreply = $attr_ref->{'setreply'}->[0] if $attr_ref->{'setreply'};
238 $exit_code = $attr_ref->{'exit_code'}->[0] if $attr_ref->{'exit_code'};
239 if (defined $setreply && $setreply =~ /^2\d\d/) { # all ok
240 do_log(1, "%s", $setreply);
241 } elsif (!defined($setreply)) {
242 do_log(0, "Error, missing 'setreply' attribute");
243 } else {
244 do_log(0, "%s", $setreply);
245 }
246 # may do another request here if needed ...
247 $sock->close or die "Error closing socket: $!";
248 $exit_code = 0 if $exit_code==99; # same thing in this case, both is ok
249 exit 0+$exit_code;
250
251 END {
252 # remove a temporary file and directory if necessary
253 if (defined $fname && -f $fname) {
254 unlink $fname or warn "Error deleting file $fname: $!";
255 }
256 if (defined $tempdir && -d $tempdir) {
257 rmdir $tempdir or warn "Error deleting temporary directory $tempdir: $!";
258 }
259 }