| 1 | =pod | ||
| 2 | |||
| 3 | =head1 LICENSE | ||
| 4 | |||
| 5 | Copyright (c) 2016-2022 G.W. Haywood. All rights reserved. | ||
| 6 | With thanks to all those who have trodden these paths before, | ||
| 7 | including | ||
| 8 | Copyright (c) 2002-2004 Todd Vierling. All rights reserved. | ||
| 9 | |||
| 10 | Redistribution and use in source and binary forms, with or without | ||
| 11 | modification, are permitted provided that the following conditions are met: | ||
| 12 | |||
| 13 | 1. Redistributions of source code must retain the above copyright notices, | ||
| 14 | this list of conditions and the following disclaimer. | ||
| 15 | |||
| 16 | 2. Redistributions in binary form must reproduce the above copyright | ||
| 17 | notices, this list of conditions and the following disclaimer in the | ||
| 18 | documentation and/or other materials provided with the distribution. | ||
| 19 | |||
| 20 | 3. Neither the name of the author nor the names of contributors may be used | ||
| 21 | to endorse or promote products derived from this software without specific | ||
| 22 | prior written permission. In the case of G.W. Haywood this permission is | ||
| 23 | hereby now granted. | ||
| 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 BE | ||
| 29 | 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 | =cut | ||
| 38 | |||
| 39 | package Sendmail::PMilter::Context; | ||
| 40 | |||
| 41 | use 5.014; # Don't use 5.016 yet. That would enable feature 'unicode_strings', and we | ||
| 42 | # probably aren't quite ready for that. We're counting *characters* passed | ||
| 43 | # between us and Sendmail, and Sendmail thinks that they're *bytes*. | ||
| 44 | use parent 'Exporter'; | ||
| 45 | |||
| 46 | use strict; | ||
| 47 | use warnings; | ||
| 48 | |||
| 49 | use Carp; | ||
| 50 | use Socket; | ||
| 51 | use UNIVERSAL; | ||
| 52 | |||
| 53 | use Sendmail::PMilter 1.24 qw(:all); | ||
| 54 | |||
| 55 | # use Data::Dumper; | ||
| 56 | |||
| 57 | our $VERSION = '1.24'; | ||
| 58 | $VERSION = eval $VERSION; | ||
| 59 | |||
| 60 | =pod | ||
| 61 | |||
| 62 | =head1 SYNOPSIS | ||
| 63 | |||
| 64 | Sendmail::PMilter::Context - per-connection milter context | ||
| 65 | |||
| 66 | =head1 DESCRIPTION | ||
| 67 | |||
| 68 | A Sendmail::PMilter::Context is the context object passed to milter callback | ||
| 69 | functions as the first argument, typically named "$ctx" for convenience. | ||
| 70 | This document details the publicly accessible operations on this object. | ||
| 71 | |||
| 72 | =head1 METHODS | ||
| 73 | |||
| 74 | =cut | ||
| 75 | |||
| 76 | ##### Symbols exported to the caller | ||
| 77 | |||
| 78 | use constant SMFIA_UNKNOWN => 'U'; | ||
| 79 | use constant SMFIA_UNIX => 'L'; | ||
| 80 | use constant SMFIA_INET => '4'; | ||
| 81 | use constant SMFIA_INET6 => '6'; | ||
| 82 | |||
| 83 | our @EXPORT_OK = qw( | ||
| 84 | SMFIA_UNKNOWN | ||
| 85 | SMFIA_UNIX | ||
| 86 | SMFIA_INET | ||
| 87 | SMFIA_INET6 | ||
| 88 | ); | ||
| 89 | our %EXPORT_TAGS = ( all => [ @EXPORT_OK ] ); | ||
| 90 | |||
| 91 | ##### Protocol constants | ||
| 92 | # Commands: | ||
| 93 | use constant SMFIC_ABORT => 'A'; | ||
| 94 | use constant SMFIC_BODY => 'B'; | ||
| 95 | use constant SMFIC_CONNECT => 'C'; | ||
| 96 | use constant SMFIC_MACRO => 'D'; | ||
| 97 | use constant SMFIC_BODYEOB => 'E'; | ||
| 98 | use constant SMFIC_HELO => 'H'; | ||
| 99 | use constant SMFIC_HEADER => 'L'; | ||
| 100 | use constant SMFIC_MAIL => 'M'; | ||
| 101 | use constant SMFIC_EOH => 'N'; | ||
| 102 | use constant SMFIC_OPTNEG => 'O'; | ||
| 103 | use constant SMFIC_RCPT => 'R'; | ||
| 104 | use constant SMFIC_QUIT => 'Q'; | ||
| 105 | use constant SMFIC_DATA => 'T'; # v4 | ||
| 106 | use constant SMFIC_UNKNOWN => 'U'; # v3 | ||
| 107 | # Responses: | ||
| 108 | use constant SMFIR_ADDRCPT => '+'; | ||
| 109 | use constant SMFIR_DELRCPT => '-'; | ||
| 110 | use constant SMFIR_ADDRCPT_PAR => '2'; | ||
| 111 | use constant SMFIR_ACCEPT => 'a'; | ||
| 112 | use constant SMFIR_REPLBODY => 'b'; | ||
| 113 | use constant SMFIR_CONTINUE => 'c'; | ||
| 114 | use constant SMFIR_DISCARD => 'd'; | ||
| 115 | use constant SMFIR_ADDHEADER => 'h'; | ||
| 116 | use constant SMFIR_INSHEADER => 'i'; # v3, or v2 and Sendmail 8.13+ | ||
| 117 | use constant SMFIR_SETSYMLIST => 'l'; | ||
| 118 | use constant SMFIR_CHGHEADER => 'm'; | ||
| 119 | use constant SMFIR_PROGRESS => 'p'; | ||
| 120 | use constant SMFIR_QUARANTINE => 'q'; | ||
| 121 | use constant SMFIR_REJECT => 'r'; | ||
| 122 | use constant SMFIR_CHGFROM => 'e'; # Sendmail 8.14+ | ||
| 123 | use constant SMFIR_TEMPFAIL => 't'; | ||
| 124 | use constant SMFIR_REPLYCODE => 'y'; | ||
| 125 | |||
| 126 | ##### Private data | ||
| 127 | |||
| 128 | no strict 'refs'; | ||
| 129 | my %replynames = map { &{$_} => $_ } qw( | ||
| 130 | SMFIR_ADDRCPT | ||
| 131 | SMFIR_DELRCPT | ||
| 132 | SMFIR_ADDRCPT_PAR | ||
| 133 | SMFIR_ACCEPT | ||
| 134 | SMFIR_REPLBODY | ||
| 135 | SMFIR_CONTINUE | ||
| 136 | SMFIR_DISCARD | ||
| 137 | SMFIR_ADDHEADER | ||
| 138 | SMFIR_INSHEADER | ||
| 139 | SMFIR_SETSYMLIST | ||
| 140 | SMFIR_CHGHEADER | ||
| 141 | SMFIR_PROGRESS | ||
| 142 | SMFIR_QUARANTINE | ||
| 143 | SMFIR_REJECT | ||
| 144 | SMFIR_CHGFROM | ||
| 145 | SMFIR_TEMPFAIL | ||
| 146 | SMFIR_REPLYCODE | ||
| 147 | ); | ||
| 148 | use strict 'refs'; | ||
| 149 | |||
| 150 | ##### Constructor, main loop, and internal calls | ||
| 151 | |||
| 152 | sub new ($$$$$) { | ||
| 153 | my $this = bless {}, shift; | ||
| 154 | # The means of communicating with the MTA. | ||
| 155 | $this->{socket} = shift; | ||
| 156 | # A hash containing $key,$value pairs where $value is a reference to a | ||
| 157 | # callback sub in the milter e.g. \&xm_connect_callback and $key is a | ||
| 158 | # name for the callback (as passed to sub call_hooks below), e.g. 'connect'. | ||
| 159 | my $callbacks = $this->{callbacks} = shift; | ||
| 160 | # The capabilities we're going to request from the MTA. | ||
| 161 | $this->{callback_flags} = shift; | ||
| 162 | # The protocol version we're going to be using. Probably 6, could be 2. | ||
| 163 | $this->{'milter protocol version'} = shift; | ||
| 164 | |||
| 165 | # Making the wild assumption that we're using a recent Sendmail version, offer to the milter all 'protocol' bits set. | ||
| 166 | $this->{protocol} = SMFI_CURR_PROT; # 0x001FFFFF | ||
| 167 | # Also all possible data chunk size bits. Two are available in milter protocol version 6. | ||
| 168 | $this->{protocol} |= SMFIP_MDS_256K; # 0x10000000 MILTER_MAX_DATA_SIZE=262143 bytes. | ||
| 169 | $this->{protocol} |= SMFIP_MDS_1M; # 0x20000000 MILTER_MAX_DATA_SIZE=1048575 bytes. | ||
| 170 | |||
| 171 | $this; | ||
| 172 | } | ||
| 173 | |||
| 174 | sub main ($) { | ||
| 175 | my $this = shift; | ||
| 176 | my $socket = $this->{socket} || return undef; | ||
| 177 | |||
| 178 | my $buf = ''; | ||
| 179 | my $gotquit = 0; | ||
| 180 | |||
| 181 | my $split_buf = sub { | ||
| 182 | $buf =~ s/\0$//; # remove trailing NUL | ||
| 183 | return [ split(/\0/, $buf) ]; | ||
| 184 | }; | ||
| 185 | |||
| 186 | $socket->autoflush(1); | ||
| 187 | |||
| 188 | $this->{lastsymbol} = ''; | ||
| 189 | |||
| 190 | #my $time_now = localtime; | ||
| 191 | |||
| 192 | eval { | ||
| 193 | |||
| 194 | #$time_now = localtime; | ||
| 195 | #printf( "%s PID=%d Context.pm(%3d): main(eval): entered eval, about to enter main loop.\n", $time_now, $$, __LINE__ ); | ||
| 196 | while (1) { | ||
| 197 | #$time_now = localtime; | ||
| 198 | #printf( "%s PID=%d Context.pm(%3d): main(eval): top of main loop\n", $time_now, $$, __LINE__ ); | ||
| 199 | # Loop, reading packets 'from the wire' into $buf and then extracting the commands and any data from them. | ||
| 200 | # Note that commands are known by the symbolic constants 'SMFIC_something'. There are 14 of these commands; | ||
| 201 | # all are listed in the 'Commands' section under 'Protocol constants' above. Correspondigly the responses | ||
| 202 | # are known by symbolic constants 'SMFIR_something'. The 17 possible responses are listed in the 'Responses' | ||
| 203 | # section under 'Protocol data' above. There is just one little wrinkle in all this; the (success) response | ||
| 204 | # to the 'SMFIC_OPTNEG' command is also 'SMFIC_OPTNEG'. There is no 'SMFIR_OPTNEG' response defined. | ||
| 205 | $this->read_block(\$buf, 4) || last; | ||
| 206 | my $len = unpack('N', $buf); | ||
| 207 | |||
| 208 | die "bad packet length $len\n" if ($len <= 0); | ||
| 209 | |||
| 210 | # save the overhead of stripping the first byte from $buf | ||
| 211 | $this->read_block(\$buf, 1) || last; | ||
| 212 | my $cmd = $buf; | ||
| 213 | |||
| 214 | #$time_now = localtime; | ||
| 215 | #printf( "%s PID=%d Context.pm(%3d): main(eval): got command=[%s]\n", $time_now, $$, __LINE__, $cmd ); | ||
| 216 | |||
| 217 | # get actual data | ||
| 218 | $this->read_block(\$buf, $len - 1) || die "EOF in stream\n"; | ||
| 219 | |||
| 220 | if ($cmd eq SMFIC_ABORT) { | ||
| 221 | # delete $this->{symbols}{&SMFIC_CONNECT}; | ||
| 222 | # delete $this->{symbols}{&SMFIC_HELO}; | ||
| 223 | delete $this->{symbols}{&SMFIC_MAIL}; | ||
| 224 | # delete $this->{symbols}{&SMFIC_RCPT}; | ||
| 225 | # delete $this->{symbols}{&SMFIC_DATA}; | ||
| 226 | # delete $this->{symbols}{&SMFIC_EOH}; | ||
| 227 | # delete $this->{symbols}{&SMFIC_BODYEOB}; | ||
| 228 | $this->call_hooks('abort'); | ||
| 229 | } elsif ($cmd eq SMFIC_BODY) { | ||
| 230 | $this->call_hooks('body', $buf, length($buf)); | ||
| 231 | } elsif ($cmd eq SMFIC_CONNECT) { | ||
| 232 | # Perl RE doesn't like matching multiple \0 instances. | ||
| 233 | # To avoid problems, we slice the string to the first null, | ||
| 234 | # then use unpack for the rest. | ||
| 235 | unless ($buf =~ s/^([^\0]*)\0(.)//) { | ||
| 236 | die "SMFIC_CONNECT: invalid connect info\n"; | ||
| 237 | # XXX should print a hexdump here? | ||
| 238 | } | ||
| 239 | |||
| 240 | my $host = $1; | ||
| 241 | my $af = $2; | ||
| 242 | my ($port, $addr) = unpack('nZ*', $buf); | ||
| 243 | my $pack; # default undef | ||
| 244 | |||
| 245 | if ($af eq SMFIA_INET) { | ||
| 246 | $pack = pack_sockaddr_in($port, inet_aton($addr)); | ||
| 247 | } elsif ($af eq SMFIA_INET6) { | ||
| 248 | $pack = eval { | ||
| 249 | require Socket6; | ||
| 250 | $addr =~ s/^IPv6://; | ||
| 251 | Socket6::pack_sockaddr_in6($port, | ||
| 252 | Socket6::inet_pton(&Socket6::AF_INET6, $addr)); | ||
| 253 | }; | ||
| 254 | } elsif ($af eq SMFIA_UNIX) { | ||
| 255 | $pack = eval { | ||
| 256 | sockaddr_un($addr); | ||
| 257 | }; | ||
| 258 | } | ||
| 259 | $this->call_hooks('connect', $host, $pack); | ||
| 260 | # delete $this->{symbols}{&SMFIC_CONNECT}; | ||
| 261 | } elsif ($cmd eq SMFIC_MACRO) { | ||
| 262 | die "SMFIC_MACRO: empty packet\n" unless ($buf =~ s/^(.)//); | ||
| 263 | my $code = $this->{lastsymbol} = $1; | ||
| 264 | my $marray = &$split_buf; | ||
| 265 | |||
| 266 | # odd number of entries: give last empty value | ||
| 267 | push(@$marray, '') if ((@$marray & 1) != 0); | ||
| 268 | |||
| 269 | my %macros = @$marray; | ||
| 270 | |||
| 271 | while (my ($name, $value) = each(%macros)) { | ||
| 272 | $this->{symbols}{$code}{$name} = $value; | ||
| 273 | } | ||
| 274 | } elsif ($cmd eq SMFIC_BODYEOB) { | ||
| 275 | $this->call_hooks('eom'); | ||
| 276 | # delete $this->{symbols}{&SMFIC_MAIL}; | ||
| 277 | # delete $this->{symbols}{&SMFIC_DATA}; | ||
| 278 | # delete $this->{symbols}{&SMFIC_EOH}; | ||
| 279 | # delete $this->{symbols}{&SMFIC_BODYEOB}; | ||
| 280 | } elsif ($cmd eq SMFIC_HELO) { | ||
| 281 | my $helo = &$split_buf; | ||
| 282 | die "SMFIC_HELO: bad packet\n" unless (@$helo == 1); | ||
| 283 | $this->call_hooks('helo', @$helo); | ||
| 284 | # delete $this->{symbols}{&SMFIC_HELO}; | ||
| 285 | } elsif ($cmd eq SMFIC_HEADER) { | ||
| 286 | my $header = &$split_buf; | ||
| 287 | |||
| 288 | # empty value: ensure an empty string | ||
| 289 | push(@$header, '') if (@$header == 1); | ||
| 290 | $this->call_hooks('header', @$header); | ||
| 291 | } elsif ($cmd eq SMFIC_MAIL) { | ||
| 292 | if ($this->{lastsymbol} ne SMFIC_MAIL) { delete $this->{symbols}{&SMFIC_MAIL}; } | ||
| 293 | my $envfrom = &$split_buf; | ||
| 294 | $this->call_hooks('envfrom', @$envfrom) | ||
| 295 | if scalar @$envfrom >= 1; | ||
| 296 | |||
| 297 | # delete $this->{symbols}{&SMFIC_MAIL}; | ||
| 298 | } elsif ($cmd eq SMFIC_EOH) { | ||
| 299 | $this->call_hooks('eoh'); | ||
| 300 | # delete $this->{symbols}{&SMFIC_EOH}; | ||
| 301 | |||
| 302 | } elsif ($cmd eq SMFIC_OPTNEG) { | ||
| 303 | |||
| 304 | # Here we've established that the 'NEGOTIATE' command (SMFIC_OPTNEG) has been received in the incoming packet from the MTA. | ||
| 305 | # If it happens at all, the negotiation step should happen once (and only once) at the beginning of a connection. | ||
| 306 | |||
| 307 | # Bleat about the packet size if it's not what's expected, perhaps somebody has, er, 'improved' the MTA. | ||
| 308 | # The expected size (including the command byte that we've already stripped off) is 13, so we | ||
| 309 | # expect 12 bytes to remain in the packet buffer $buf. Add 1 to the size and test for != 13. | ||
| 310 | my $packet_size = length( $buf ) + 1; | ||
| 311 | if( $packet_size != 13 ) {die "SMFIC_OPTNEG: unsupported packet size $packet_size\n"; } | ||
| 312 | # Extract the integers from the buffer into the @negotiate array. | ||
| 313 | my @negotiate = unpack( 'NNN', $buf ); # Three 32-bit numbers of four bytes each. | ||
| 314 | my $arraysize = scalar( @negotiate ); | ||
| 315 | if( $arraysize != 3 ) { die "SMFIC_OPTNEG: bad packet: expected 3 integer values, found $arraysize.\n"; } | ||
| 316 | # Named scalars for clarity, could as easily have used the array elements themselves. | ||
| 317 | my $milter_protocol_version = $negotiate[0]; | ||
| 318 | my $actions_available = $negotiate[1]; | ||
| 319 | my $protocol_steps_available = $negotiate[2]; | ||
| 320 | |||
| 321 | $this->{'milter_protocol_version_ref'} # 6. We hope. Later we may insist. | ||
| 322 | = \$milter_protocol_version; # | ||
| 323 | $this->{'actions_available_ref'} # 1FF. We hope. Later we may insist. | ||
| 324 | = \$actions_available; # $actions_available contains an integer consisting of bits which flag various protocol capabilities ('actions' in Sendmail parlance) available from the MTA. | ||
| 325 | # These are things which the milter can ask the MTA to do to the message, such as add a message header (milters very commonly add headers) or replace the message body. | ||
| 326 | # Symbolic constants for these bits are all SMFIF_something. Over the years,the list of available actions has been extended as Sendmail has developed, and a 'version' | ||
| 327 | # of the milter protocol - a single digit - is implied by the state of development of the milter protocol (i.e. the capabilities which are available) in any particular | ||
| 328 | # version of Sendmail. Milter protocol Version 6 was introduced by Sendmail 8.14.0 in January 2007, and as of 2019 only versions of Sendmail which support this milter | ||
| 329 | # protocol version are in widespread use. For security reasons alone you probably should not be using older versions of Sendmail which do not support milter protocol | ||
| 330 | # version 6, but other MTAs may not support all the V6 capabilities so it is probably best to check their availability in your milter before attempting to use them. | ||
| 331 | # The available protocol actions, symbolic constants, and the corresponding bit values for the flags are: | ||
| 332 | # Action Symbolic constant Value Available in milter protocol version. There's a bit of a muddle about V3, but nobody's using it any more. | ||
| 333 | # - SMFIF_NONE 0x0000 | ||
| 334 | # Add header SMFIF_ADDHDRS 0x0001 V1 A message header can be added at the end of the message headers, or inserted at a specified position. | ||
| 335 | # Replace body SMFIF_CHGBODY 0x0002 V2 The message body can be replaced. | ||
| 336 | # Add recipient SMFIF_ADDRCPT 0x0004 V1 A recipient can be added. ESMTP arguments cannot be included (see SMFIF_ADDRCPT_PAR below). | ||
| 337 | # Delete recipient SMFIF_DELRCPT 0x0008 V1 A recipient can be deleted. | ||
| 338 | # Change header SMFIF_CHGHDRS 0x0010 V2 A header can be modified. | ||
| 339 | # Replace body SMFIF_MODBODY == SMFIF_CHGBODY Historical - Milter Protocol V1 - exists for backward compatibility, do not use in new code. | ||
| 340 | # Quarantine message SMFIF_QUARANTINE 0x0020 V2 The MTA will not deliver the message, but instead place it in a holding area. | ||
| 341 | # Change envelope from SMFIF_CHGFROM 0x0040 V6 The sender (and ESMTP arguments) given by the client in the "MAIL FROM" command can be replaced. | ||
| 342 | # Add recipient + args SMFIF_ADDRCPT_PAR 0x0080 V6 A recipient (such as may have been received in a client "RCPT TO" command with ESMTP arguments) can be added. | ||
| 343 | # Request macro values SMFIF_SETSYMLIST 0x0100 V6 The MTA can provide a list of macros valid for a given protocol 'step'. See below for the protocol steps. | ||
| 344 | # | ||
| 345 | $this->{'protocol_steps_available_ref'} # 1FFFFF. We hope - later, we may even insist. | ||
| 346 | = \$protocol_steps_available; # $protocol_steps_available contains an integer consisting of bits which refer to four different types of configurable behaviour. | ||
| 347 | # Symbolic constants for these bits are all SMFIP_something. The first three of the four configuration types affect the protocol | ||
| 348 | # and are listed below in the same order as they are listed in the libmilter source (see st_optionneg() in libmilter/engine.c). | ||
| 349 | # A fourth type which is not related to protocol steps, and is not mentioned in that list, is included here for completness. | ||
| 350 | # Note that the positions of these bits in the integer are not significant - they merely reflect Sendmail development history. | ||
| 351 | # 1. The MTA offers to the milter the option for the milter to ask the MTA not to send a particular protocol step command to run one of the callbacks. | ||
| 352 | # The request may be made even if a callback for that step is defined in the milter, and has been 'hooked' by the MTA at startup. | ||
| 353 | # Symbolic constants for these bits are all SMFIP_NOsomething. There are NINE of these "Do not send protocol step command" features. | ||
| 354 | # The protocol step commands, symbolic constants, and the corresponding bits in this integer are: | ||
| 355 | # Step Symbolic constant Value Available in milter protocol version. There's a bit of a muddle about V3, but nobody's using it any more. | ||
| 356 | # CONNECT SMFIP_NOCONNECT 0x00000001 V1 It's difficult to imagine a milter not wanting to know that a connection has been made, but there you are. | ||
| 357 | # HELO SMFIP_NOHELO 0x00000002 | ||
| 358 | # MAIL FROM SMFIP_NOMAIL 0x00000004 | ||
| 359 | # RCPT TO SMFIP_NORCPT 0x00000008 | ||
| 360 | # DATA SMFIP_NODATA 0x00000200 | ||
| 361 | # HEADER SMFIP_NOHDRS 0x00000020 | ||
| 362 | # EOH SMFIP_NOEOH 0x00000040 | ||
| 363 | # BODY SMFIP_NOBODY 0x00000010 | ||
| 364 | # UNKNOWN COMMAND SMFIP_NOUNKNOWN 0x00000100 | ||
| 365 | # 2. The MTA understands if the milter requests certain additional features in the protocol. | ||
| 366 | # There are THREE of these features. | ||
| 367 | # Symbolic constants for these bits have, like the corresponding protocol features, nothing in common beyond the SMFIP_ prefix. | ||
| 368 | # The protocol step commands, the symbolic constants, and the corresponding bits are: | ||
| 369 | # SKIP SMFIP_SKIP 0x00000400 The MTA understands the 'SKIP' command, see Sendmail's libmilter documentation. | ||
| 370 | # SEND REJECTED RECIPIENT SMFIP_RCPT_REJ 0x00000800 The MTA should send the SMFIC_MAIL command even if the MTA has already decided to reject it as e.g. unknown. | ||
| 371 | # This will take effect only if Sendmail has been compiled with _FFR_MILTER_CHECK_REJECTIONS_TOO. | ||
| 372 | # HEADER LEADING SPACE SMFIP_HDR_LEADSPC 0x00100000 The MTA will not add leading spaces to header values, the milter must do that. See the Sendmail documentation. | ||
| 373 | # 3. The MTA will by prior negotiation permit the milter to make no reply to the MTA after a given individual milter callback has been run in response to an MTA command. | ||
| 374 | # Symbolic constants for these bits are all SMFIP_NR_something. There are NINE of these 'SEND NO REPLY' features. | ||
| 375 | # If the MTA and milter agree that the milter will not to send a reply to the MTA at a particular protocol step, it MUST NOT reply to that step. | ||
| 376 | # The protocol step 'NO REPLY' commands, the symbolic constants, and the corresponding bits are: | ||
| 377 | # No reply for CONNECT SMFIP_NR_CONN 0x00001000 | ||
| 378 | # No reply for HELO SMFIP_NR_HELO 0x00002000 | ||
| 379 | # No reply for MAIL SMFIP_NR_MAIL 0x00004000 | ||
| 380 | # No reply for RCPT SMFIP_NR_RCPT 0x00008000 | ||
| 381 | # No reply for DATA SMFIP_NR_DATA 0x00010000 | ||
| 382 | # No reply for HEADER SMFIP_NR_HDR 0x00000080 | ||
| 383 | # No reply for HEADER SMFIP_NOHREPL == SMFIP_NR_HDR Historical - exists for backward compatibility, do not use in new code. | ||
| 384 | # No reply for EOH SMFIP_NR_EOH 0x00040000 | ||
| 385 | # No reply for BODY SMFIP_NR_BODY 0x00080000 | ||
| 386 | # No reply for UNKNOWN SMFIP_NR_UNKN 0x00020000 An unknown command received from the client which is attempting to send mail. | ||
| 387 | # 4. The MTA offers to the milter the ability to use a data buffer larger than the default 65535 bytes. | ||
| 388 | # Here be dragons. The facility is not available in default Sendmail builds, it must be compiled with at least _FFR_MDS_NEGOTIATE to make use of this facility. | ||
| 389 | # Max data size 256K SMFIP_MDS_256K 0x10000000 262143 bytes. | ||
| 390 | # Max data size 1M SMFIP_MDS_1M 0x20000000 1048575 bytes. No sizes other than the three given are permitted. | ||
| 391 | |||
| 392 | if( ${$this->{'milter_protocol_version_ref'}} != 2 && ${$this->{'milter_protocol_version_ref'}} != 6) { die "SMFIC_OPTNEG: unsupported milter protocol version " . ${$this->{'milter_protocol_version_ref'}} . "\n"; } | ||
| 393 | |||
| 394 | # Next we call the milter's 'negotiate' callback, if there is one, via the 'call_hooks' sub. The 'call_hooks' sub is defined about 78 lines below in this file. | ||
| 395 | # The 'call_hooks' sub returns to the MTA a packet which contains (subject to translation of some symbolic constants) whatever the milter callback returned. | ||
| 396 | # The 'call_hooks' sub will unpack that packet into the three class variables $this->{'something_ref'} (where 'something' is one of 'milter_protocol_version', 'actions_available' and 'protocol_steps_available'). | ||
| 397 | my @negotiate_refs = (); | ||
| 398 | if( ! defined $this->{callbacks}{'negotiate'} ) | ||
| 399 | { # Default protocol steps if no negotiate callback registered. | ||
| 400 | ${$this->{'protocol_steps_available_ref'}} &= SMFIP_DEFAULTS; | ||
| 401 | } | ||
| 402 | push( @negotiate_refs, $this->{'milter_protocol_version_ref'}, $this->{'actions_available_ref'}, $this->{'protocol_steps_available_ref'} ); | ||
| 403 | $this->call_hooks('negotiate', @negotiate_refs); | ||
| 404 | |||
| 405 | } elsif ($cmd eq SMFIC_RCPT) { | ||
| 406 | my $envrcpt = &$split_buf; | ||
| 407 | $this->call_hooks('envrcpt', @$envrcpt) | ||
| 408 | if scalar @$envrcpt >= 1; | ||
| 409 | delete $this->{symbols}{&SMFIC_RCPT}; | ||
| 410 | |||
| 411 | } elsif ($cmd eq SMFIC_DATA) { | ||
| 412 | $this->call_hooks('data'); | ||
| 413 | # delete $this->{symbols}{&SMFIC_DATA}; | ||
| 414 | } elsif ($cmd eq SMFIC_QUIT) { | ||
| 415 | $this->call_hooks('quit'); # A long-felt want, but I'm not sure it will really do what I want. Is it called if the client does *not* send the 'QUIT' command? | ||
| 416 | last; | ||
| 417 | # that's all, folks! | ||
| 418 | } elsif ($cmd eq SMFIC_UNKNOWN) { | ||
| 419 | # This is not an unknown packet, but a packet to tell the milter that an unknown smtp command has been received. | ||
| 420 | # The argument passed to the milter is the unknown command plus any arguments there may have been, both of which can be null so we don't count arguments. | ||
| 421 | my $unknown = &$split_buf; | ||
| 422 | $this->call_hooks('unknown', @$unknown ); | ||
| 423 | } else { | ||
| 424 | die "unknown milter packet type $cmd\n"; | ||
| 425 | } | ||
| 426 | } | ||
| 427 | |||
| 428 | #$time_now = localtime; | ||
| 429 | #printf( "%s PID=%d Context.pm(%3d): main: exited main loop.\n", $time_now, $$, __LINE__ ); | ||
| 430 | |||
| 431 | }; | ||
| 432 | |||
| 433 | my $err = $@; | ||
| 434 | |||
| 435 | #$time_now = localtime; | ||
| 436 | #printf( "%s PID=%d Context.pm(%3d): main: exited eval, err=[%s], about to call 'close' callback.\n", $time_now, $$, __LINE__, $err ); | ||
| 437 | |||
| 438 | $this->call_hooks('close'); | ||
| 439 | |||
| 440 | # XXX better error handling? die here to let an eval further up get it? | ||
| 441 | if ($err) { | ||
| 442 | $this->write_packet(SMFIR_TEMPFAIL) if defined($socket); | ||
| 443 | #$time_now = localtime; | ||
| 444 | #printf( "%s PID=%d Context.pm(%3d): main: error found at loop exit: [%s]\n", $time_now, $$, __LINE__, $err ); | ||
| 445 | warn $err; | ||
| 446 | } else { | ||
| 447 | $this->write_packet(SMFIR_CONTINUE) if defined($socket); | ||
| 448 | } | ||
| 449 | |||
| 450 | #$time_now = localtime; | ||
| 451 | #printf( "%s PID=%d Context.pm(%3d): main: exit.\n", $time_now, $$, __LINE__ ); | ||
| 452 | undef; | ||
| 453 | } | ||
| 454 | |||
| 455 | sub read_block { | ||
| 456 | my $this = shift; | ||
| 457 | my $bufref = shift; | ||
| 458 | my $len = shift; | ||
| 459 | |||
| 460 | my $socket = $this->{socket}; | ||
| 461 | my $sofar = 0; | ||
| 462 | |||
| 463 | $$bufref = ''; | ||
| 464 | |||
| 465 | while ($len > $sofar) { | ||
| 466 | my $read = $socket->sysread($$bufref, $len - $sofar, $sofar); | ||
| 467 | return undef if (!defined($read) || $read <= 0); # if EOF | ||
| 468 | $sofar += $read; | ||
| 469 | } | ||
| 470 | 1; | ||
| 471 | } | ||
| 472 | |||
| 473 | sub write_packet { | ||
| 474 | my $this = shift; | ||
| 475 | my $code = shift; | ||
| 476 | my $out = shift; | ||
| 477 | $out = '' unless defined($out); | ||
| 478 | my $len = pack('N', length($out) + 1); | ||
| 479 | my $socket = $this->{socket}; | ||
| 480 | $socket->syswrite($len); | ||
| 481 | $socket->syswrite($code); | ||
| 482 | $socket->syswrite($out); | ||
| 483 | return length($code) + length($out); # XXXX | ||
| 484 | } | ||
| 485 | |||
| 486 | sub call_hooks ($$;@) { | ||
| 487 | my $this = shift; | ||
| 488 | my $what = $this->{cb} = shift; | ||
| 489 | |||
| 490 | #my $time_now = localtime; | ||
| 491 | #printf( "%s PID=%d Context.pm(%3d): call_hooks: callback=[%s]\n", $time_now, $$, __LINE__, $what ); | ||
| 492 | |||
| 493 | my $rc = SMFIS_CONTINUE; | ||
| 494 | my $sub = $this->{callbacks}{$what}; | ||
| 495 | #$time_now = localtime; | ||
| 496 | if( defined($sub) ) | ||
| 497 | { | ||
| 498 | #printf( "%s PID=%d Context.pm(%3d): call_hooks: about to call callback=[%s], rc=[%s]\n", $time_now, $$, __LINE__, $what, $rc ); | ||
| 499 | $rc = &$sub($this, @_); | ||
| 500 | #$time_now = localtime; | ||
| 501 | #printf( "%s PID=%d Context.pm(%3d): call_hooks: after calling callback=[%s], rc=[%s]\n", $time_now, $$, __LINE__, $what, $rc ); | ||
| 502 | } | ||
| 503 | else | ||
| 504 | { | ||
| 505 | #printf( "%s PID=%d Context.pm(%3d): call_hooks: (non-existent callback=[%s])\n", $time_now, $$, __LINE__, $what ); | ||
| 506 | } | ||
| 507 | |||
| 508 | # translate to response codes | ||
| 509 | if ($rc eq SMFIS_CONTINUE) { | ||
| 510 | $rc = SMFIR_CONTINUE; | ||
| 511 | } elsif ($rc eq SMFIS_ACCEPT) { | ||
| 512 | $rc = SMFIR_ACCEPT; | ||
| 513 | } elsif ($rc eq SMFIS_DISCARD) { | ||
| 514 | $rc = SMFIR_DISCARD; | ||
| 515 | } elsif ($rc eq SMFIS_REJECT) { | ||
| 516 | if (defined($this->{reply})) { | ||
| 517 | $rc = SMFIR_REPLYCODE; | ||
| 518 | } else { | ||
| 519 | $rc = SMFIR_REJECT; | ||
| 520 | } | ||
| 521 | } elsif ($rc eq SMFIS_TEMPFAIL) { | ||
| 522 | if (defined($this->{reply})) { | ||
| 523 | $rc = SMFIR_REPLYCODE; | ||
| 524 | } else { | ||
| 525 | $rc = SMFIR_TEMPFAIL; | ||
| 526 | } | ||
| 527 | } else { | ||
| 528 | die "invalid callback return $rc"; # XXXX Need to handle SMFIF_ALL_OPTS | ||
| 529 | } | ||
| 530 | |||
| 531 | my $len = 0; | ||
| 532 | #$time_now = localtime; | ||
| 533 | |||
| 534 | if( $what eq 'negotiate' ) | ||
| 535 | { | ||
| 536 | $this->{protocol} = ${$this->{'protocol_steps_available_ref'}}; | ||
| 537 | #printf( "%s PID=%d Context.pm(%3d): call_hooks: calling write_packet at [%s] callback, rc=[%s]\n", $time_now, $$, __LINE__, $what, $rc ); | ||
| 538 | #print Dumper($this->{symbols})."\n"; | ||
| 539 | $len = $this->write_packet | ||
| 540 | ( | ||
| 541 | SMFIC_OPTNEG, | ||
| 542 | pack( | ||
| 543 | 'NNN', | ||
| 544 | ${$this->{'milter_protocol_version_ref'}}, | ||
| 545 | ${$this->{'actions_available_ref'}}, | ||
| 546 | $this->{'protocol'} | ||
| 547 | ) | ||
| 548 | ); | ||
| 549 | } | ||
| 550 | elsif( $what eq 'abort' ) { ; } # According to the Sendmail docs the abort callback reply is ignored. | ||
| 551 | elsif( $rc ne SMFIR_REPLYCODE || $what eq 'close' ) | ||
| 552 | { | ||
| 553 | #printf( "%s PID=%d Context.pm(%3d): call_hooks: calling write_packet at [%s] callback, rc=[%s]\n", $time_now, $$, __LINE__, $what, $rc ); | ||
| 554 | ##printf( "%s Context.pm(%3d): call_hooks: calling write_packet at [%s] callback, rc=[%s] (symval{'_'}=[%s])\n", $time_now, __LINE__, $what, $rc, $this->{symbols}{SMFIC_CONNECT}{'_'}//'null' ); | ||
| 555 | #print Dumper($this->{symbols})."\n"; | ||
| 556 | $len = $this->write_packet($rc); | ||
| 557 | } | ||
| 558 | else | ||
| 559 | { | ||
| 560 | #printf( "%s PID=%d Context.pm(%3d): call_hooks: calling write_packet at [%s] callback, rc=[%s]\n", $time_now, $$, __LINE__, $what, $rc ); | ||
| 561 | #print Dumper($this->{symbols})."\n"; | ||
| 562 | $len = $this->write_packet($rc, $this->{reply}."\0"); | ||
| 563 | } | ||
| 564 | #$time_now = localtime; | ||
| 565 | #printf( "%s PID=%d Context.pm(%3d): call_hooks: packet length written=[%2d]\n", $time_now, $$, __LINE__, $len ); | ||
| 566 | undef $this->{reply}; | ||
| 567 | } | ||
| 568 | |||
| 569 | ##### General methods | ||
| 570 | |||
| 571 | =pod | ||
| 572 | |||
| 573 | =over 4 | ||
| 574 | |||
| 575 | =item $ctx->getpriv | ||
| 576 | |||
| 577 | Returns the private data object for this milter instance, set by | ||
| 578 | $ctx->setpriv() (see below). Returns undef if setpriv has never been called | ||
| 579 | by this milter instance. | ||
| 580 | |||
| 581 | =cut | ||
| 582 | |||
| 583 | sub getpriv ($) { | ||
| 584 | my $this = shift; | ||
| 585 | |||
| 586 | $this->{priv}; | ||
| 587 | } | ||
| 588 | |||
| 589 | =pod | ||
| 590 | |||
| 591 | =item $ctx->getsymval(NAME) | ||
| 592 | |||
| 593 | (The word 'macro' in Sendmail parlance refers to named variables which are | ||
| 594 | essentially text strings. They can be defined by the MTA, and populated as | ||
| 595 | messages are processed, or by milters, or by the MTA's configuration files.) | ||
| 596 | |||
| 597 | The getsymval method retrieves the macro symbol named NAME from the macros | ||
| 598 | available from the MTA for the current callback. NAME is either a one- | ||
| 599 | character macro name, or a multi-character name enclosed in {curly braces}. | ||
| 600 | If macro NAME is undefined when getsymval is called, it returns undef. | ||
| 601 | |||
| 602 | Some common macros are given below. The milter protocol was first | ||
| 603 | implemented in the Sendmail MTA, so these macro names are those used by | ||
| 604 | Sendmail itself; other MTAs e.g. Postfix may provide similar macros. | ||
| 605 | |||
| 606 | =over 2 | ||
| 607 | |||
| 608 | =item $ctx->getsymval('_') | ||
| 609 | |||
| 610 | The remote host name and address, in standard SMTP "name [address]" form. | ||
| 611 | |||
| 612 | =item $ctx->getsymval('i') | ||
| 613 | |||
| 614 | The MTA's queue ID for the current message. | ||
| 615 | |||
| 616 | =item $ctx->getsymval('j') | ||
| 617 | |||
| 618 | The MTA's idea of local host name. | ||
| 619 | |||
| 620 | =item $ctx->getsymval('{if_addr}') | ||
| 621 | |||
| 622 | The local address of the network interface upon which the connection was | ||
| 623 | received. | ||
| 624 | |||
| 625 | =item $ctx->getsymval('{if_name}') | ||
| 626 | |||
| 627 | The local hostname of the network interface upon which the connection was | ||
| 628 | received. | ||
| 629 | |||
| 630 | =item $ctx->getsymval('{mail_addr}') | ||
| 631 | |||
| 632 | The MAIL FROM: sender's address, canonicalized and angle bracket stripped. | ||
| 633 | (This is typically not the same value as the second argument to the | ||
| 634 | "envfrom" callback.) Will be defined to the empty string '' if the client | ||
| 635 | issued a MAIL FROM:<> null return path command. | ||
| 636 | |||
| 637 | =item $ctx->getsymval('{rcpt_addr}') | ||
| 638 | |||
| 639 | The RCPT TO: recipient's address, canonicalized and angle bracket stripped. | ||
| 640 | (This is typically not the same value as the second argument to the | ||
| 641 | "envrcpt" callback.) | ||
| 642 | |||
| 643 | =back | ||
| 644 | |||
| 645 | Not all macros may be available at all times. Some macros are only | ||
| 646 | available after a specific phase is reached, and some macros may only | ||
| 647 | be available from certain MTA implementations. Check returned values | ||
| 648 | for 'undef'. This version of the Sendmail::PMilter package collects | ||
| 649 | macro values only for the following callbacks: | ||
| 650 | |||
| 651 | CONNECT | ||
| 652 | HELO | ||
| 653 | ENVFROM | ||
| 654 | ENVRCPT | ||
| 655 | DATA | ||
| 656 | EOH | ||
| 657 | EOM | ||
| 658 | |||
| 659 | =cut | ||
| 660 | |||
| 661 | sub getsymval ($$) { | ||
| 662 | my $this = shift; | ||
| 663 | my $key = shift; | ||
| 664 | foreach my $code (SMFIC_CONNECT, SMFIC_HELO, SMFIC_MAIL, SMFIC_RCPT, SMFIC_DATA, SMFIC_EOH, SMFIC_BODYEOB) | ||
| 665 | { | ||
| 666 | my $val = $this->{symbols}{$code}{$key}; | ||
| 667 | if( defined $val ) { return $val; } | ||
| 668 | } | ||
| 669 | undef; | ||
| 670 | } | ||
| 671 | |||
| 672 | =pod | ||
| 673 | |||
| 674 | =item $ctx->setpriv(DATA) | ||
| 675 | |||
| 676 | This is the place to store milter-private data that is sensitive to the | ||
| 677 | current SMTP client connection. Only one value can be stored, so typically | ||
| 678 | an arrayref or hashref is initialized in the "connect" callback and set with | ||
| 679 | $ctx->setpriv. | ||
| 680 | |||
| 681 | This value can be retrieved on subsequent callback runs with $ctx->getpriv. | ||
| 682 | |||
| 683 | =cut | ||
| 684 | |||
| 685 | sub setpriv ($$) { | ||
| 686 | my $this = shift; | ||
| 687 | $this->{priv} = shift; | ||
| 688 | 1; | ||
| 689 | } | ||
| 690 | |||
| 691 | =pod | ||
| 692 | |||
| 693 | =item $ctx->setreply(RCODE, XCODE, MESSAGE) | ||
| 694 | |||
| 695 | Set an extended SMTP status reply (before returning SMFIS_REJECT or | ||
| 696 | SMFIS_TEMPFAIL). RCODE should be a short (4xx or 5xx) numeric reply | ||
| 697 | code, XCODE should be a long ('4.x.y' or '5.x.y') ESMTP reply code. | ||
| 698 | The first digit of RCODE must be the same as the first digit of XCODE. | ||
| 699 | There is no such restriction on the other digits. In RCODE and XCODE, | ||
| 700 | 'x' should be one decimal digit; in XCODE 'y' should be either one or | ||
| 701 | two decimal digits. MESSAGE is the full text of the message to send. | ||
| 702 | Refer to the appropriate RFCs for actual codes and suggested messages. | ||
| 703 | Examples: | ||
| 704 | |||
| 705 | $ctx->setreply(451, '4.7.0', 'Cannot authenticate you right now'); | ||
| 706 | return SMFIS_TEMPFAIL; | ||
| 707 | |||
| 708 | $ctx->setreply(550, '5.7.26', 'Multiple authentication failures'); | ||
| 709 | return SMFIS_REJECT; | ||
| 710 | |||
| 711 | Note that after setting a reply with this method, the SMTP result code | ||
| 712 | comes from RCODE, not from the symbolic constants SMFIS_REJECT and | ||
| 713 | SMFIS_TEMPFAIL. However for consistency, callbacks that set a 4xx | ||
| 714 | response code should use SMFIS_TEMPFAIL, and those that set a 5xx code | ||
| 715 | should return SMFIS_REJECT. | ||
| 716 | |||
| 717 | Returns 1 on success, undef on failure. In the case of failure, which | ||
| 718 | is typically only caused by bad parameters, a generic message will be | ||
| 719 | sent based on the SMFIS_* return code. | ||
| 720 | |||
| 721 | =cut | ||
| 722 | |||
| 723 | sub setreply ($$$$) { | ||
| 724 | my $this = shift; | ||
| 725 | my $rcode = shift || ''; | ||
| 726 | my $xcode = shift || ''; | ||
| 727 | my $message = shift || ''; | ||
| 728 | |||
| 729 | if ($rcode !~ /^[45]\d\d$/ || $xcode !~ /^[45]\.\d\.\d{1,2}$/ || substr($rcode, 0, 1) ne substr($xcode, 0, 1)) { | ||
| 730 | warn 'setreply: bad reply arguments'; | ||
| 731 | return undef; | ||
| 732 | } | ||
| 733 | |||
| 734 | $this->{reply} = "$rcode $xcode $message"; | ||
| 735 | 1; | ||
| 736 | } | ||
| 737 | |||
| 738 | =pod | ||
| 739 | |||
| 740 | =item $ctx->setmlreply(RCODE, XCODE, MESSAGES) | ||
| 741 | |||
| 742 | Set an extended SMTP status reply (before returning SMFIS_REJECT or | ||
| 743 | SMFIS_TEMPFAIL). See setreply() above for more information about the | ||
| 744 | reply codes RCODE and XCODE. MESSAGES is an array which contains a | ||
| 745 | multi-line reply. This array must contain no less than two string | ||
| 746 | elements. Sendmail dictates that it must contain no more than 32 | ||
| 747 | elements, and that each string element must contain no more than 980 | ||
| 748 | characters (although any of the strings may be NULL), and no string | ||
| 749 | may contain a newline ("\n") or a carriage return ("\r") character. | ||
| 750 | |||
| 751 | Example: | ||
| 752 | |||
| 753 | $ctx->setmlreply(451, '4.7.0', \('Cannot authenticate sender.', | ||
| 754 | 'Please refer to our published policies at', | ||
| 755 | 'http://www.example.com/policies') | ||
| 756 | ); | ||
| 757 | return SMFIS_TEMPFAIL; | ||
| 758 | |||
| 759 | Note that after setting a reply with this method, the SMTP result code | ||
| 760 | comes from RCODE, not from the symbolic constants SMFIS_REJECT and | ||
| 761 | SMFIS_TEMPFAIL. However for consistency, callbacks that set a 4xx | ||
| 762 | response code should use SMFIS_TEMPFAIL, and those that set a 5xx code | ||
| 763 | should return SMFIS_REJECT. | ||
| 764 | |||
| 765 | Returns 1 on success, undef on failure. In the case of failure, which | ||
| 766 | is typically caused by bad parameters, a generic message will be sent | ||
| 767 | based on the SMFIS_* return code. | ||
| 768 | |||
| 769 | =cut | ||
| 770 | |||
| 771 | # See Sendmail::PMilter and .../libmilter/smfi.c in the Sendmail | ||
| 772 | # source for MAXREPLIES and MAXREPLYLEN. | ||
| 773 | |||
| 774 | sub setmlreply ($$$$) { | ||
| 775 | my $this = shift; | ||
| 776 | my $rcode = shift || ''; | ||
| 777 | my $xcode = shift || ''; | ||
| 778 | my $messageref = shift || ''; | ||
| 779 | if | ||
| 780 | ( | ||
| 781 | ref( $messageref ) ne 'ARRAY' || | ||
| 782 | $rcode !~ /^[45]\d\d$/ || | ||
| 783 | $xcode !~ /^[45]\.\d\.\d{1,2}$/ || | ||
| 784 | substr($rcode, 0, 1) ne substr($xcode, 0, 1) || | ||
| 785 | @{$messageref} < 2 || | ||
| 786 | @{$messageref} > MAXREPLIES | ||
| 787 | ) | ||
| 788 | { | ||
| 789 | warn 'setmlreply: bad reply arguments'; | ||
| 790 | return undef; | ||
| 791 | } | ||
| 792 | my $message = $rcode . '-' . $xcode . ' '; # Admittedly this is a bit willful. | ||
| 793 | foreach( @{$messageref} ) | ||
| 794 | { | ||
| 795 | if( /[\r\n]/ ) # Sendmail does not allow these characters in the reply strings. | ||
| 796 | { | ||
| 797 | warn 'setmlreply: bad reply arguments'; | ||
| 798 | return undef; | ||
| 799 | } | ||
| 800 | $message .= "\r\n" . $rcode . '-' . $xcode . ' ' . $_; | ||
| 801 | } | ||
| 802 | $message .= "\r\n" . $rcode . ' ' . $xcode . ' '; | ||
| 803 | $this->{reply} = $message; | ||
| 804 | 1; | ||
| 805 | } | ||
| 806 | |||
| 807 | =item $ctx->shutdown() | ||
| 808 | |||
| 809 | A special case of C<< $ctx->setreply() >> which sets the short numeric reply | ||
| 810 | code to 421 and the ESMTP code to 4.7.0. Under Sendmail 8.13 and higher | ||
| 811 | (and you should not be using any version of Sendmail older than that), | ||
| 812 | this will close the MTA's communication channel quickly, which should | ||
| 813 | immediately result in a "close" callback and end of milter execution. | ||
| 814 | |||
| 815 | Returns 1. | ||
| 816 | |||
| 817 | =cut | ||
| 818 | |||
| 819 | sub shutdown ($) { | ||
| 820 | my $this = shift; | ||
| 821 | $this->setreply(421, '4.7.0', 'Closing communications channel'); | ||
| 822 | 1; | ||
| 823 | } | ||
| 824 | |||
| 825 | ##### Protocol action methods | ||
| 826 | |||
| 827 | =pod | ||
| 828 | |||
| 829 | =item $ctx->addheader(HEADER, VALUE) | ||
| 830 | |||
| 831 | Add header HEADER with value VALUE to this mail. Does not change any | ||
| 832 | existing headers with the same name. Only callable from the "eom" callback. | ||
| 833 | |||
| 834 | Returns 1 on success, undef on failure. | ||
| 835 | |||
| 836 | =cut | ||
| 837 | |||
| 838 | sub addheader ($$$) { | ||
| 839 | my $this = shift; | ||
| 840 | my $header = shift || die "addheader: no header name\n"; | ||
| 841 | my $value = shift; | ||
| 842 | die "addheader: called outside of EOM\n" if ($this->{cb} ne 'eom'); | ||
| 843 | die "addheader: SMFIF_ADDHDRS not in capability list\n" unless ($this->{callback_flags} & SMFIF_ADDHDRS); | ||
| 844 | die "addheader: no header value\n" unless defined $value; | ||
| 845 | |||
| 846 | $this->write_packet(SMFIR_ADDHEADER, "$header\0$value\0"); | ||
| 847 | 1; | ||
| 848 | } | ||
| 849 | |||
| 850 | =pod | ||
| 851 | |||
| 852 | =item $ctx->insheader(HEADER, VALUE, POSITION) | ||
| 853 | |||
| 854 | Insert header HEADER at position POSITION with value VALUE to this mail. | ||
| 855 | Does not change any existing headers with the same name. Only callable | ||
| 856 | from the "eom" callback. HEADER and VALUE are requred, but POSITION is | ||
| 857 | optional. A POSITION value of zero is acceptable and is the default if | ||
| 858 | not supplied - this inserts the HEADER before all existing headers. | ||
| 859 | |||
| 860 | Returns 1 on success, undef on failure. | ||
| 861 | |||
| 862 | =cut | ||
| 863 | |||
| 864 | sub insheader ($$$;$) { | ||
| 865 | my $this = shift; | ||
| 866 | my $header = shift || die "insheader: no header name\n"; | ||
| 867 | my $value = shift || die "insheader: no header value\n"; | ||
| 868 | my $position = shift; | ||
| 869 | if( not defined $position ) { $position = 0; } | ||
| 870 | |||
| 871 | die "insheader: called outside of EOM\n" if ($this->{cb} ne 'eom'); | ||
| 872 | die "addheader: SMFIF_ADDHDRS not in capability list\n" unless ($this->{callback_flags} & SMFIF_ADDHDRS); | ||
| 873 | |||
| 874 | $this->write_packet(SMFIR_INSHEADER, pack('N',$position)."$header\0$value\0"); | ||
| 875 | 1; | ||
| 876 | } | ||
| 877 | |||
| 878 | =pod | ||
| 879 | |||
| 880 | =item $ctx->chgheader(HEADER, INDEX, VALUE) | ||
| 881 | |||
| 882 | Change the INDEX'th header of name HEADER to the value VALUE. Only callable | ||
| 883 | from the "eom" callback. If INDEX exceeds the number of existing headers of | ||
| 884 | name HEADER, adds another header of that name. | ||
| 885 | |||
| 886 | Returns 1 on success, undef on failure. | ||
| 887 | |||
| 888 | =cut | ||
| 889 | |||
| 890 | sub chgheader ($$$$) { | ||
| 891 | my $this = shift; | ||
| 892 | my $header = shift || die "chgheader: no header name\n"; | ||
| 893 | my $num = shift || 0; | ||
| 894 | my $value = shift; | ||
| 895 | |||
| 896 | $value = '' unless defined($value); | ||
| 897 | |||
| 898 | die "chgheader: called outside of EOM\n" if ($this->{cb} ne 'eom'); | ||
| 899 | die "chgheader: SMFIF_CHGHDRS not in capability list\n" unless ($this->{callback_flags} & SMFIF_CHGHDRS); | ||
| 900 | |||
| 901 | $this->write_packet(SMFIR_CHGHEADER, pack('N', $num)."$header\0$value\0"); | ||
| 902 | 1; | ||
| 903 | } | ||
| 904 | |||
| 905 | =pod | ||
| 906 | |||
| 907 | =item $ctx->addrcpt(ADDRESS) | ||
| 908 | |||
| 909 | Add address ADDRESS to the list of recipients for this mail. Only callable | ||
| 910 | from the "eom" callback. | ||
| 911 | |||
| 912 | Returns 1 on success, undef on failure. | ||
| 913 | |||
| 914 | =cut | ||
| 915 | |||
| 916 | sub addrcpt ($$) { | ||
| 917 | my $this = shift; | ||
| 918 | my $rcpt = shift || die "addrcpt: no recipient specified\n"; | ||
| 919 | |||
| 920 | die "addrcpt: called outside of EOM\n" if ($this->{cb} ne 'eom'); | ||
| 921 | die "addrcpt: SMFIF_ADDRCPT not in capability list\n" unless ($this->{callback_flags} & SMFIF_ADDRCPT); | ||
| 922 | |||
| 923 | $this->write_packet(SMFIR_ADDRCPT, "$rcpt\0"); | ||
| 924 | 1; | ||
| 925 | } | ||
| 926 | |||
| 927 | =pod | ||
| 928 | |||
| 929 | =item $ctx->addrcpt_par(ADDRESS,PARAMS) | ||
| 930 | |||
| 931 | Add an address ADDRESS and its ESMTP arguments PARAMS to the list of | ||
| 932 | recipients for this mail. Only callable from the "eom" callback. | ||
| 933 | |||
| 934 | Returns 1 on success, undef on failure. | ||
| 935 | |||
| 936 | =cut | ||
| 937 | |||
| 938 | sub addrcpt_par ($$$) { | ||
| 939 | my $this = shift; | ||
| 940 | my $rcpt = shift || die "addrcpt: no recipient specified\n"; | ||
| 941 | my $params = shift; | ||
| 942 | |||
| 943 | die "addrcpt_par: called outside of EOM\n" if ($this->{cb} ne 'eom'); | ||
| 944 | die "addrcpt_par: SMFIF_ADDRCPT_PAR not in capability list\n" unless ($this->{callback_flags} & SMFIF_ADDRCPT_PAR); | ||
| 945 | |||
| 946 | $this->write_packet(SMFIR_ADDRCPT_PAR, "$rcpt\0"); | ||
| 947 | 1; | ||
| 948 | } | ||
| 949 | |||
| 950 | =pod | ||
| 951 | |||
| 952 | =item $ctx->delrcpt(ADDRESS) | ||
| 953 | |||
| 954 | Remove address ADDRESS from the list of recipients for this mail. The | ||
| 955 | ADDRESS argument must match a prior argument to the "envrcpt" callback | ||
| 956 | exactly (case sensitive, and including angle brackets if present). Only | ||
| 957 | callable from the "eom" callback. | ||
| 958 | |||
| 959 | Returns 1 on success, undef on failure. A success return means that | ||
| 960 | the command was queued for processing. It does not necessarily mean | ||
| 961 | that the recipient was successfully removed, that information is not | ||
| 962 | available from Sendmail. | ||
| 963 | |||
| 964 | =cut | ||
| 965 | |||
| 966 | sub delrcpt ($$) { | ||
| 967 | my $this = shift; | ||
| 968 | my $rcpt = shift || die "delrcpt: no recipient specified\n"; | ||
| 969 | |||
| 970 | die "delrcpt: called outside of EOM\n" if ($this->{cb} ne 'eom'); | ||
| 971 | die "delrcpt: SMFIF_DELRCPT not in capability list\n" unless ($this->{callback_flags} & SMFIF_DELRCPT); | ||
| 972 | |||
| 973 | $this->write_packet(SMFIR_DELRCPT, "$rcpt\0"); | ||
| 974 | 1; | ||
| 975 | } | ||
| 976 | |||
| 977 | =pod | ||
| 978 | |||
| 979 | =item $ctx->progress() | ||
| 980 | |||
| 981 | Sends an asynchronous "progress" message to the MTA, to allow longer | ||
| 982 | than normal operations such as extensive message body scanning or a | ||
| 983 | deliberate delay. This command should only be issued during the EOM | ||
| 984 | callback, it will fail (and return undef) if called at other times. | ||
| 985 | |||
| 986 | Returns 1 if the call is made during EOM and is permitted, else undef. | ||
| 987 | |||
| 988 | =cut | ||
| 989 | |||
| 990 | sub progress ($) { | ||
| 991 | my $this = shift; | ||
| 992 | die "progress: called outside of EOM\n" if ($this->{cb} ne 'eom'); | ||
| 993 | $this->write_packet(SMFIR_PROGRESS); | ||
| 994 | 1; | ||
| 995 | } | ||
| 996 | |||
| 997 | =pod | ||
| 998 | |||
| 999 | =item $ctx->quarantine(REASON) | ||
| 1000 | |||
| 1001 | Quarantine the current message in the MTA-defined quarantine area, using | ||
| 1002 | the given REASON as a text string describing the quarantine status. Only | ||
| 1003 | callable from the "eom" callback. | ||
| 1004 | |||
| 1005 | Returns 1 on success, undef on failure. | ||
| 1006 | |||
| 1007 | This method is an extension that is not available in the standard | ||
| 1008 | Sendmail::Milter package. | ||
| 1009 | |||
| 1010 | =cut | ||
| 1011 | |||
| 1012 | sub quarantine ($$) { | ||
| 1013 | my $this = shift; | ||
| 1014 | my $reason = shift; | ||
| 1015 | |||
| 1016 | die "quarantine: called outside of EOM\n" if ($this->{cb} ne 'eom'); | ||
| 1017 | die "quarantine: SMFIF_QUARANTINE not in capability list\n" unless ($this->{callback_flags} & SMFIF_QUARANTINE); | ||
| 1018 | |||
| 1019 | $this->write_packet(SMFIR_QUARANTINE, "$reason\0"); | ||
| 1020 | 1; | ||
| 1021 | } | ||
| 1022 | |||
| 1023 | =pod | ||
| 1024 | |||
| 1025 | =item $ctx->replacebody(BUFFER) | ||
| 1026 | |||
| 1027 | Replace the message body with the data in BUFFER (a scalar). This method | ||
| 1028 | may be called multiple times, each call appending to the replacement buffer. | ||
| 1029 | End-of-line should be represented by CR-LF ("\r\n"). Only callable from the | ||
| 1030 | "eom" callback. | ||
| 1031 | |||
| 1032 | Returns 1 on success, undef on failure. | ||
| 1033 | |||
| 1034 | =cut | ||
| 1035 | |||
| 1036 | sub replacebody ($$) { | ||
| 1037 | my $this = shift; | ||
| 1038 | my $chunk = shift; | ||
| 1039 | |||
| 1040 | die "replacebody: called outside of EOM\n" if ($this->{cb} ne 'eom'); | ||
| 1041 | die "replacebody: SMFIF_CHGBODY not in capability list\n" unless ($this->{callback_flags} & SMFIF_CHGBODY); | ||
| 1042 | |||
| 1043 | my $len = length($chunk); | ||
| 1044 | my $socket = $this->{socket}; | ||
| 1045 | |||
| 1046 | $len = pack('N', ($len + 1)); | ||
| 1047 | $socket->syswrite($len); | ||
| 1048 | $socket->syswrite(SMFIR_REPLBODY); | ||
| 1049 | $socket->syswrite($chunk); | ||
| 1050 | 1; | ||
| 1051 | } | ||
| 1052 | |||
| 1053 | =pod | ||
| 1054 | |||
| 1055 | =item $ctx->chgfrom(ADDRESS) | ||
| 1056 | =item $ctx->setsender(ADDRESS) (Deprecated) | ||
| 1057 | |||
| 1058 | Replace the envelope sender address for the given mail message. | ||
| 1059 | |||
| 1060 | Returns 1 on success, undef on failure. Successful return means that | ||
| 1061 | the command was queued for processing. It does not necessarily mean | ||
| 1062 | that the operation was successfully completed, that information is not | ||
| 1063 | available from Sendmail. | ||
| 1064 | |||
| 1065 | =cut | ||
| 1066 | |||
| 1067 | sub chgfrom ($$) { | ||
| 1068 | my $this = shift; | ||
| 1069 | my $sender = shift || die "chgfrom: no sender specified\n"; | ||
| 1070 | |||
| 1071 | die "chgfrom: called outside of EOM\n" if ($this->{cb} ne 'eom'); | ||
| 1072 | die "chgfrom: SMFIF_CHGFROM not in capability list\n" unless ($this->{callback_flags} & SMFIF_CHGFROM); | ||
| 1073 | |||
| 1074 | $this->write_packet(SMFIR_CHGFROM, "$sender\0"); | ||
| 1075 | 1; | ||
| 1076 | } | ||
| 1077 | |||
| 1078 | # Deprecated, may be removed from a future version with little or no warning. | ||
| 1079 | sub setsender ($$) { | ||
| 1080 | my $this = shift; | ||
| 1081 | my $sender = shift || die "setsender: no sender specified\n"; | ||
| 1082 | |||
| 1083 | die "setsender: called outside of EOM\n" if ($this->{cb} ne 'eom'); | ||
| 1084 | die "setsender: SMFIF_CHGFROM not in capability list\n" unless ($this->{callback_flags} & SMFIF_CHGFROM); | ||
| 1085 | |||
| 1086 | $this->write_packet(SMFIR_CHGFROM, "$sender\0"); | ||
| 1087 | 1; | ||
| 1088 | } | ||
| 1089 | |||
| 1090 | 1; | ||
| 1091 | |||
| 1092 | __END__ |