| 1 | package Mail::Sendmail; | ||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||
| 3 | # Mail::Sendmail by Milivoj Ivkovic <mi\x40alma.ch> | ||||||||||||||||||||||||||||||||||
| 4 | # see embedded POD documentation after __END__ | ||||||||||||||||||||||||||||||||||
| 5 | # or http://alma.ch/perl/mail.html | ||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||
| 7 | =head1 NAME | ||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||
| 9 | Mail::Sendmail - Simple platform independent mailer | ||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||
| 11 | =cut | ||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||
| 13 | 2 | our $VERSION = "0.83"; | |||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||
| 15 | 2 | require 5.006; | |||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||
| 17 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 18 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||
| 20 | 2 | ▶ | use parent 'Exporter'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||
| 22 | # *************** Configuration you may want to change ******************* | ||||||||||||||||||||||||||||||||||
| 23 | # You probably want to set your SMTP server here (unless you specify it in | ||||||||||||||||||||||||||||||||||
| 24 | # every script), and leave the rest as is. See pod documentation for details | ||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||
| 26 | 2 | our %mailcfg = ( | |||||||||||||||||||||||||||||||||
| 27 | # List of SMTP servers: | ||||||||||||||||||||||||||||||||||
| 28 | 'smtp' => [ qw( localhost ) ], | ||||||||||||||||||||||||||||||||||
| 29 | #'smtp' => [ qw( mail.mydomain.com ) ], # example | ||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||
| 31 | 'from' => '', # default sender e-mail, used when no From header in mail | ||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||
| 33 | 'mime' => 1, # use MIME encoding by default | ||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||
| 35 | 'retries' => 1, # number of retries on smtp connect failure | ||||||||||||||||||||||||||||||||||
| 36 | 'delay' => 1, # delay in seconds between retries | ||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||
| 38 | 'tz' => '', # only to override automatic detection | ||||||||||||||||||||||||||||||||||
| 39 | 'port' => 25, # change it if you always use a non-standard port | ||||||||||||||||||||||||||||||||||
| 40 | 'debug' => 0 # prints stuff to STDERR | ||||||||||||||||||||||||||||||||||
| 41 | ); | ||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||
| 43 | # ******************************************************************* | ||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||
| 45 | 2 | our $address_rx; | |||||||||||||||||||||||||||||||||
| 46 | 2 | our $debug; | |||||||||||||||||||||||||||||||||
| 47 | 2 | our $log; | |||||||||||||||||||||||||||||||||
| 48 | 2 | our $error; | |||||||||||||||||||||||||||||||||
| 49 | 2 | our $retry_delay; | |||||||||||||||||||||||||||||||||
| 50 | 2 | our $connect_retries; | |||||||||||||||||||||||||||||||||
| 51 | 2 | our $auth_support; | |||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||
| 53 | 2 | ▶ | use Socket; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 54 | 2 | ▶ | use Time::Local; # for automatic time zone detection | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 55 | 2 | ▶ | use Sys::Hostname; # for use of hostname in HELO | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 56 | 2 | ▶ | use Sys::Hostname::Long; # for use of hostname in HELO | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||
| 58 | #use Digest::HMAC_MD5 qw(hmac_md5 hmac_md5_hex); | ||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||
| 60 | 2 | $auth_support = 'DIGEST-MD5 CRAM-MD5 PLAIN LOGIN'; | |||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||
| 62 | # use MIME::QuotedPrint if available and configured in %mailcfg | ||||||||||||||||||||||||||||||||||
| 63 | 2 | ▶ | eval("use MIME::QuotedPrint"); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 64 | 2 | ▶ | $mailcfg{'mime'} &&= (!$@); | ||||||||||||||||||||||||||||||||
|
Condition33%1/3
$Mail::Sendmail::mailcfg{"mime"} &&= !$@
MC/DC0%0/2
$Mail::Sendmail::mailcfg{"mime"} &&= !$@
$Mail::Sendmail::mailcfg{"mime"}
!$@
Truth table33%1/3
A &&= not(B)
A= $Mail::Sendmail::mailcfg{"mime"}B= !$@
| |||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||
| 66 | 2 | our @EXPORT = qw(&sendmail); | |||||||||||||||||||||||||||||||||
| 67 | 2 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||||||||
| 68 | %mailcfg | ||||||||||||||||||||||||||||||||||
| 69 | time_to_date | ||||||||||||||||||||||||||||||||||
| 70 | $address_rx | ||||||||||||||||||||||||||||||||||
| 71 | $debug | ||||||||||||||||||||||||||||||||||
| 72 | $log | ||||||||||||||||||||||||||||||||||
| 73 | $error | ||||||||||||||||||||||||||||||||||
| 74 | ); | ||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||
| 76 | # regex for e-mail addresses where full=$1, user=$2, domain=$3 | ||||||||||||||||||||||||||||||||||
| 77 | # see pod documentation about this regex | ||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||
| 79 | 2 | my $word_rx = '[\x21\x23-\x27\x2A-\x2B\x2D\x2F\w\x3D\x3F]+'; | |||||||||||||||||||||||||||||||||
| 80 | 2 | my $user_rx = $word_rx # valid chars | |||||||||||||||||||||||||||||||||
| 81 | .'(?:\.' . $word_rx . ')*' # possibly more words preceded by a dot | ||||||||||||||||||||||||||||||||||
| 82 | ; | ||||||||||||||||||||||||||||||||||
| 83 | 2 | my $dom_rx = '\w[-\w]*(?:\.\w[-\w]*)*'; # less valid chars in domain names | |||||||||||||||||||||||||||||||||
| 84 | 2 | my $ip_rx = '\[\d{1,3}(?:\.\d{1,3}){3}\]'; | |||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||
| 86 | 2 | $address_rx = '((' . $user_rx . ')\@(' . $dom_rx . '|' . $ip_rx . '))'; | |||||||||||||||||||||||||||||||||
| 87 | ; # v. 0.61 | ||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||
| 89 | 0 | ▶ | sub _require_md5 { | ||||||||||||||||||||||||||||||||
|
Subroutine
_require_md5: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||
| 90 | 0 | eval { require Digest::MD5; Digest::MD5->import(qw(md5 md5_hex)); }; | |||||||||||||||||||||||||||||||||
| 91 | 0 | ▶ | $error .= $@ if $@; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 92 | 0 | ▶ | return ($@ ? undef : 1); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||
| 95 | 0 | ▶ | sub _require_base64 { | ||||||||||||||||||||||||||||||||
|
Subroutine
_require_base64: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||
| 96 | 0 | eval { | |||||||||||||||||||||||||||||||||
| 97 | 0 | require MIME::Base64; MIME::Base64->import(qw(encode_base64 decode_base64)); | |||||||||||||||||||||||||||||||||
| 98 | }; | ||||||||||||||||||||||||||||||||||
| 99 | 0 | ▶ | $error .= $@ if $@; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 100 | 0 | ▶ | return ($@ ? undef : 1); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | sub _hmac_md5 { | ||||||||||||||||||||||||||||||||
|
Subroutine
_hmac_md5: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||
| 104 | 0 | my ($pass, $ckey) = @_; | |||||||||||||||||||||||||||||||||
| 105 | 0 | my $size = 64; | |||||||||||||||||||||||||||||||||
| 106 | 0 | ▶ | $pass = md5($pass) if length($pass) > $size; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 107 | 0 | my $ipad = $pass ^ (chr(0x36) x $size); | |||||||||||||||||||||||||||||||||
| 108 | 0 | my $opad = $pass ^ (chr(0x5c) x $size); | |||||||||||||||||||||||||||||||||
| 109 | 0 | return md5_hex($opad, md5($ipad, $ckey)); | |||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | sub _digest_md5 { | ||||||||||||||||||||||||||||||||
|
Subroutine
_digest_md5: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||
| 113 | 0 | my ($user, $pass, $challenge, $realm) = @_; | |||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||
| 115 | 0 | my %ckey = map { /^([^=]+)="?(.+?)"?$/ } split(/,/, $challenge); | |||||||||||||||||||||||||||||||||
| 116 | 0 | ▶ | $realm ||= $ckey{realm}; #($user =~ s/\@(.+)$//o) ? $1 : $server; | ||||||||||||||||||||||||||||||||
|
Condition0%0/3
$realm ||= $ckey{"realm"}
MC/DC0%0/2
$realm ||= $ckey{"realm"}
$realm
$ckey{"realm"}
Truth table0%0/3
A ||= B
A= $realmB= $ckey{"realm"}
| |||||||||||||||||||||||||||||||||||
| 117 | 0 | my $nonce = $ckey{nonce}; | |||||||||||||||||||||||||||||||||
| 118 | 0 | my $cnonce = &make_cnonce; | |||||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | my $uri = join('/', 'smtp', hostname()||'localhost', $ckey{realm}); | ||||||||||||||||||||||||||||||||
|
Condition0%0/2
hostname() || 'localhost'
MC/DC0%0/1
hostname() || 'localhost'
hostname()
Truth table0%0/2
A
A= hostname()
| |||||||||||||||||||||||||||||||||||
| 120 | 0 | my $qop = 'auth'; | |||||||||||||||||||||||||||||||||
| 121 | 0 | my $nc = '00000001'; | |||||||||||||||||||||||||||||||||
| 122 | 0 | my($hv, $a1, $a2); | |||||||||||||||||||||||||||||||||
| 123 | 0 | $hv = md5("$user:$realm:$pass"); | |||||||||||||||||||||||||||||||||
| 124 | 0 | $a1 = md5_hex("$hv:$nonce:$cnonce"); | |||||||||||||||||||||||||||||||||
| 125 | 0 | $a2 = md5_hex("AUTHENTICATE:$uri"); | |||||||||||||||||||||||||||||||||
| 126 | 0 | $hv = md5_hex("$a1:$nonce:$nc:$cnonce:$qop:$a2"); | |||||||||||||||||||||||||||||||||
| 127 | 0 | return qq(username="$user",realm="$ckey{realm}",nonce="$nonce",nc=$nc,cnonce="$cnonce",digest-uri="$uri",response=$hv,qop=$qop); | |||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||
| 130 | 0 | ▶ | sub make_cnonce { | ||||||||||||||||||||||||||||||||
|
Subroutine
make_cnonce: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 131 | 0 | my $s = '' ; | |||||||||||||||||||||||||||||||||
| 132 | 0 | for(1..16) { $s .= chr(rand 256) } | |||||||||||||||||||||||||||||||||
| 133 | 0 | $s = encode_base64($s, ""); | |||||||||||||||||||||||||||||||||
| 134 | 0 | $s =~ s/\W/X/go; | |||||||||||||||||||||||||||||||||
| 135 | 0 | return substr($s, 0, 16); | |||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||
| 138 | 4 | ▶ | sub time_to_date { | ||||||||||||||||||||||||||||||||
|
Subroutine
time_to_date: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 139 | # convert a time() value to a date-time string according to RFC 822 | ||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||
| 141 | 4 | ▶ | my $time = $_[0] || time(); # default to now if no argument | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$_[0] || time
MC/DC0%0/1
$_[0] || time
$_[0]
Truth table50%1/2
A
A= $_[0]
| |||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||
| 143 | 4 | my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); | |||||||||||||||||||||||||||||||||
| 144 | 4 | my @wdays = qw(Sun Mon Tue Wed Thu Fri Sat); | |||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||
| 146 | 4 | my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) | |||||||||||||||||||||||||||||||||
| 147 | = localtime($time); | ||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||
| 149 | 4 | my $TZ = $mailcfg{'tz'}; | |||||||||||||||||||||||||||||||||
| 150 | 4 | ▶ | if ( $TZ eq "" ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 151 | # offset in hours | ||||||||||||||||||||||||||||||||||
| 152 | 4 | my $offset = sprintf "%.1f", (timegm(localtime) - time) / 3600; | |||||||||||||||||||||||||||||||||
| 153 | 4 | my $minutes = sprintf "%02d", abs( $offset - int($offset) ) * 60; | |||||||||||||||||||||||||||||||||
| 154 | 4 | $TZ = sprintf("%+03d", int($offset)) . $minutes; | |||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||
| 156 | 4 | return join(" ", | |||||||||||||||||||||||||||||||||
| 157 | ($wdays[$wday] . ','), | ||||||||||||||||||||||||||||||||||
| 158 | $mday, | ||||||||||||||||||||||||||||||||||
| 159 | $months[$mon], | ||||||||||||||||||||||||||||||||||
| 160 | $year+1900, | ||||||||||||||||||||||||||||||||||
| 161 | sprintf("%02d:%02d:%02d", $hour, $min, $sec), | ||||||||||||||||||||||||||||||||||
| 162 | $TZ | ||||||||||||||||||||||||||||||||||
| 163 | ); | ||||||||||||||||||||||||||||||||||
| 164 | } # end sub time_to_date | ||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||
| 166 | 3 | ▶ | sub sendmail { | ||||||||||||||||||||||||||||||||
|
Subroutine
sendmail: called CC=104 SCAR=72.1
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||
| 168 | 3 | $error = ''; | |||||||||||||||||||||||||||||||||
| 169 | 3 | $log = "Mail::Sendmail v. $VERSION - " . scalar(localtime()) . "\n"; | |||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||
| 171 | 3 | my $CRLF = "\015\012"; | |||||||||||||||||||||||||||||||||
| 172 | 3 | local $/ = $CRLF; | |||||||||||||||||||||||||||||||||
| 173 | 3 | local $\ = ''; # to protect us from outside settings | |||||||||||||||||||||||||||||||||
| 174 | 3 | local $_; | |||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||
| 176 | 3 | my (%mail, $k, | |||||||||||||||||||||||||||||||||
| 177 | $smtp, $server, $port, $connected, $localhost, | ||||||||||||||||||||||||||||||||||
| 178 | $fromaddr, $recip, @recipients, $to, $header, | ||||||||||||||||||||||||||||||||||
| 179 | %esmtp, @wanted_methods, | ||||||||||||||||||||||||||||||||||
| 180 | ); | ||||||||||||||||||||||||||||||||||
| 181 | 2 | ▶ | use vars qw($server_reply); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 182 | # -------- a few internal subs ---------- | ||||||||||||||||||||||||||||||||||
| 183 | 1 | ▶ | sub fail { | ||||||||||||||||||||||||||||||||
|
Subroutine
fail: called CC=3 SCAR=14.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 184 | # things to do before returning a sendmail failure | ||||||||||||||||||||||||||||||||||
| 185 | 1 | $error .= join(" ", @_) . "\n"; | |||||||||||||||||||||||||||||||||
| 186 | 1 | ▶ | if ($server_reply) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 187 | 0 | $error .= "Server said: $server_reply\n"; | |||||||||||||||||||||||||||||||||
| 188 | 0 | ▶ | print STDERR "Server said: $server_reply\n" if $^W; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||||
| 190 | 1 | close S; | |||||||||||||||||||||||||||||||||
| 191 | 1 | return 0; | |||||||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||
| 194 | 26 | ▶ | sub socket_write { | ||||||||||||||||||||||||||||||||
|
Subroutine
socket_write: called CC=6 SCAR=21.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 195 | 26 | my $i; | |||||||||||||||||||||||||||||||||
| 196 | 26 | for $i (0..$#_) { | |||||||||||||||||||||||||||||||||
| 197 | # accept references, so we don't copy potentially big data | ||||||||||||||||||||||||||||||||||
| 198 | 30 | ▶ | my $data = ref($_[$i]) ? $_[$i] : \$_[$i]; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 199 | 30 | ▶ | if ($mailcfg{'debug'} > 5) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 200 | 0 | ▶ | if (length($$data) < 500) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 201 | 0 | print ">", $$data; | |||||||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||||||||
| 203 | else { | ||||||||||||||||||||||||||||||||||
| 204 | 0 | print "> [...", length($$data), " bytes sent ...]\n"; | |||||||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||||||||
| 207 | 30 | ▶ | print(S $$data) || return 0; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||||||||
| 209 | 26 | 1; | |||||||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||
| 212 | 14 | ▶ | sub socket_read { | ||||||||||||||||||||||||||||||||
|
Subroutine
socket_read: called CC=5 SCAR=18.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 213 | 14 | $server_reply = ""; | |||||||||||||||||||||||||||||||||
| 214 | 14 | ▶ | do { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 215 | 14 | $_ = <S>; | |||||||||||||||||||||||||||||||||
| 216 | 14 | $server_reply .= $_; | |||||||||||||||||||||||||||||||||
| 217 | #chomp $_; | ||||||||||||||||||||||||||||||||||
| 218 | 14 | ▶ | print "<$_" if $mailcfg{'debug'} > 5; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 219 | 14 | ▶ | if (/^[45]/ or !$_) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
/^[45]/ or not $_
MC/DC0%0/2
/^[45]/ or not $_
/^[45]/
not $_
Truth table33%1/3
A or not(B)
A= /^[45]/B= not $_
| |||||||||||||||||||||||||||||||||||
| 220 | 0 | chomp $server_reply; | |||||||||||||||||||||||||||||||||
| 221 | 0 | return; # return false | |||||||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||||||||
| 223 | } while (/^[\d]+-/); | ||||||||||||||||||||||||||||||||||
| 224 | 14 | chomp $server_reply; | |||||||||||||||||||||||||||||||||
| 225 | 14 | return $server_reply; | |||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||
| 227 | # -------- end of internal subs ---------- | ||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||
| 229 | # all config keys to lowercase, to prevent typo errors | ||||||||||||||||||||||||||||||||||
| 230 | 3 | foreach $k (keys %mailcfg) { | |||||||||||||||||||||||||||||||||
| 231 | 24 | ▶ | if ($k =~ /[A-Z]/) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 232 | 0 | $mailcfg{lc($k)} = $mailcfg{$k}; | |||||||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||
| 236 | # redo mail hash, arranging keys case etc... | ||||||||||||||||||||||||||||||||||
| 237 | 3 | ▶ | while (@_) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 238 | 15 | $k = shift @_; | |||||||||||||||||||||||||||||||||
| 239 | 15 | ▶ | if (!$k and $^W) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $k and $^W
MC/DC0%0/2
not $k and $^W
not $k
$^W
Truth table33%1/3
not(A) and B
A= not $kB= $^W
| |||||||||||||||||||||||||||||||||||
| 240 | 0 | warn "Received false mail hash key: \'$k\'. Did you forget to put it in quotes?\n"; | |||||||||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||
| 243 | # arrange keys case | ||||||||||||||||||||||||||||||||||
| 244 | 15 | $k = ucfirst lc($k); | |||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||
| 246 | 15 | $k =~ s/\s*:\s*$//o; # kill colon (and possible spaces) at end, we add it later. | |||||||||||||||||||||||||||||||||
| 247 | # uppercase also after "-", so people don't complain that headers case is different | ||||||||||||||||||||||||||||||||||
| 248 | # than in Outlook. | ||||||||||||||||||||||||||||||||||
| 249 | 0 | $k =~ s/-(.)/"-" . uc($1)/ge; | |||||||||||||||||||||||||||||||||
| 250 | 15 | $mail{$k} = shift @_; | |||||||||||||||||||||||||||||||||
| 251 | 15 | ▶ | if ($k !~ /^(Message|Body|Text)$/i) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 252 | # Strip any CR/LF characters from header values to prevent | ||||||||||||||||||||||||||||||||||
| 253 | # CRLF injection (SMTP header injection). Attacker-controlled | ||||||||||||||||||||||||||||||||||
| 254 | # header values containing "\nBcc: attacker@evil.com" would | ||||||||||||||||||||||||||||||||||
| 255 | # otherwise inject new headers or body content. | ||||||||||||||||||||||||||||||||||
| 256 | # See CWE-93 (Improper Neutralization of CRLF Sequences) and | ||||||||||||||||||||||||||||||||||
| 257 | # the equivalent fixes in Email::MIME (CVE-2024-4140) and | ||||||||||||||||||||||||||||||||||
| 258 | # many other mail libraries. | ||||||||||||||||||||||||||||||||||
| 259 | 12 | ▶ | if ($mail{$k} =~ /[\015\012]/) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 260 | 1 | warn "Mail::Sendmail: stripping CR/LF from $k header to " | |||||||||||||||||||||||||||||||||
| 261 | . "prevent CRLF injection\n"; | ||||||||||||||||||||||||||||||||||
| 262 | 1 | $mail{$k} =~ s/[\015\012]+//g; | |||||||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||
| 267 | 3 | ▶ | $smtp = $mail{'Smtp'} || $mail{'Server'}; | ||||||||||||||||||||||||||||||||
|
Condition33%1/3
$mail{'Smtp'} || $mail{'Server'}
MC/DC0%0/2
$mail{'Smtp'} || $mail{'Server'}
$mail{'Smtp'}
$mail{'Server'}
Truth table33%1/3
A || B
A= $mail{'Smtp'}B= $mail{'Server'}
| |||||||||||||||||||||||||||||||||||
| 268 | 3 | ▶ | unshift @{$mailcfg{'smtp'}}, $smtp if ($smtp and $mailcfg{'smtp'}->[0] ne $smtp); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$smtp and $Mail::Sendmail::mailcfg{"smtp"}[0] ne $smtp
MC/DC0%0/2
$smtp and $Mail::Sendmail::mailcfg{"smtp"}[0] ne $smtp
$smtp
$Mail::Sendmail::mailcfg{"smtp"}[0] ne $smtp
Truth table33%1/3
A and B
A= $smtpB= $Mail::Sendmail::mailcfg{"smtp"}[0] ne $smtp
| |||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||
| 270 | # delete non-header keys, so we don't send them later as mail headers | ||||||||||||||||||||||||||||||||||
| 271 | # I like this syntax, but it doesn't seem to work with AS port 5.003_07: | ||||||||||||||||||||||||||||||||||
| 272 | # delete @mail{'Smtp', 'Server'}; | ||||||||||||||||||||||||||||||||||
| 273 | # so instead: | ||||||||||||||||||||||||||||||||||
| 274 | 3 | delete $mail{'Smtp'}; delete $mail{'Server'}; | |||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||
| 276 | 3 | ▶ | $mailcfg{'port'} = $mail{'Port'} || $mailcfg{'port'} || 25; | ||||||||||||||||||||||||||||||||
|
Condition40%2/5
$mail{'Port'} || $Mail::Sendmail::mailcfg{'port'} || 25
$mail{"Port"} or $Mail::Sendmail::mailcfg{"port"}
MC/DC0%0/2
$mail{'Port'} || $Mail::Sendmail::mailcfg{'port'} || 25
$mail{"Port"}
$Mail::Sendmail::mailcfg{"port"}
Truth table33%1/3
A or B
A= $mail{"Port"}B= $Mail::Sendmail::mailcfg{"port"}
| |||||||||||||||||||||||||||||||||||
| 277 | 3 | delete $mail{'Port'}; | |||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||
| 279 | 3 | my $auth = $mail{'Auth'}; | |||||||||||||||||||||||||||||||||
| 280 | 3 | delete $mail{'Auth'}; | |||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||
| 282 | 3 | my @parts; | |||||||||||||||||||||||||||||||||
| 283 | 3 | ▶ | push(@parts, $mail{'Message'}) if defined($mail{'Message'}); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 284 | 3 | ▶ | push(@parts, $mail{'Body'}) if defined($mail{'Body'}); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 285 | 3 | ▶ | push(@parts, $mail{'Text'}) if defined($mail{'Text'}); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 286 | 3 | $mail{'Message'} = join("", @parts); | |||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||
| 288 | # delete @mail{'Body', 'Text'}; | ||||||||||||||||||||||||||||||||||
| 289 | 3 | delete $mail{'Body'}; | |||||||||||||||||||||||||||||||||
| 290 | 3 | delete $mail{'Text'}; | |||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||
| 292 | # Extract 'From:' e-mail address to use as envelope sender | ||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||
| 294 | 3 | ▶ | $fromaddr = $mail{'Sender'} || $mail{'From'} || $mailcfg{'from'}; | ||||||||||||||||||||||||||||||||
|
Condition33%2/6
$mail{'Sender'} || $mail{'From'} || $Mail::Sendmail::mailcfg{'from'}
$mail{"Sender"} or $mail{"From"}
MC/DC0%0/3
$mail{'Sender'} || $mail{'From'} || $Mail::Sendmail::mailcfg{'from'}
$mail{"Sender"}
$mail{"From"}
$Mail::Sendmail::mailcfg{'from'}
Truth table25%1/4
A or B || C
A= $mail{"Sender"}B= $mail{"From"}C= $Mail::Sendmail::mailcfg{'from'}
| |||||||||||||||||||||||||||||||||||
| 295 | #delete $mail{'Sender'}; | ||||||||||||||||||||||||||||||||||
| 296 | 3 | ▶ | unless ($fromaddr =~ /$address_rx/) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 297 | 0 | return fail("Bad or missing From address: \'$fromaddr\'"); | |||||||||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||||||||
| 299 | 3 | $fromaddr = $1; | |||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||
| 301 | # add Date header if needed | ||||||||||||||||||||||||||||||||||
| 302 | 3 | ▶ | $mail{Date} ||= time_to_date() ; | ||||||||||||||||||||||||||||||||
|
Condition33%1/3
$mail{"Date"} ||= time_to_date()
MC/DC0%0/2
$mail{"Date"} ||= time_to_date()
$mail{"Date"}
time_to_date()
Truth table33%1/3
A ||= B
A= $mail{"Date"}B= time_to_date()
| |||||||||||||||||||||||||||||||||||
| 303 | 3 | $log .= "Date: $mail{Date}\n"; | |||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||
| 305 | # cleanup message, and encode if needed | ||||||||||||||||||||||||||||||||||
| 306 | 3 | $mail{'Message'} =~ s/\r\n/\n/go; # normalize line endings, step 1 of 2 (next step after MIME encoding) | |||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||
| 308 | 3 | ▶ | $mail{'Mime-Version'} ||= '1.0'; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$mail{"Mime-Version"} ||= "1.0"
MC/DC0%0/1
$mail{"Mime-Version"} ||= "1.0"
$mail{"Mime-Version"}
Truth table50%1/2
A
A= $mail{"Mime-Version"}
| |||||||||||||||||||||||||||||||||||
| 309 | 3 | ▶ | $mail{'Content-Type'} ||= 'text/plain; charset="iso-8859-1"'; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$mail{"Content-Type"} ||= "text/plain; charset=\"iso-8859-1\""
MC/DC0%0/1
$mail{"Content-Type"} ||= "text/plain; charset=\"iso-8859-1\""
$mail{"Content-Type"}
Truth table50%1/2
A
A= $mail{"Content-Type"}
| |||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||
| 311 | 3 | ▶ | unless ( $mail{'Content-Transfer-Encoding'} | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$mail{"Content-Transfer-Encoding"} or $mail{"Content-Type"} =~ /multipart/io
MC/DC0%0/2
$mail{"Content-Transfer-Encoding"} or $mail{"Content-Type"} =~ /multipart/io
$mail{"Content-Transfer-Encoding"}
$mail{"Content-Type"} =~ /multipart/io
Truth table33%1/3
A or B
A= $mail{"Content-Transfer-Encoding"}B= $mail{"Content-Type"} =~ /multipart/io
| |||||||||||||||||||||||||||||||||||
| 312 | || $mail{'Content-Type'} =~ /multipart/io ) | ||||||||||||||||||||||||||||||||||
| 313 | { | ||||||||||||||||||||||||||||||||||
| 314 | 3 | ▶ | if ($mailcfg{'mime'}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 315 | 1 | $mail{'Content-Transfer-Encoding'} = 'quoted-printable'; | |||||||||||||||||||||||||||||||||
| 316 | 1 | $mail{'Message'} = encode_qp($mail{'Message'}); | |||||||||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||||||||
| 318 | else { | ||||||||||||||||||||||||||||||||||
| 319 | 2 | $mail{'Content-Transfer-Encoding'} = '8bit'; | |||||||||||||||||||||||||||||||||
| 320 | 2 | ▶ | if ($mail{'Message'} =~ /[\x80-\xFF]/o) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 321 | 0 | $error .= "MIME::QuotedPrint not present!\nSending 8bit characters, hoping it will come across OK.\n"; | |||||||||||||||||||||||||||||||||
| 322 | 0 | ▶ | warn "MIME::QuotedPrint not present!\n", | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 323 | "Sending 8bit characters without encoding, hoping it will come across OK.\n" | ||||||||||||||||||||||||||||||||||
| 324 | if $^W; | ||||||||||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||
| 329 | 3 | $mail{'Message'} =~ s/^\./\.\./gom; # handle . as first character | |||||||||||||||||||||||||||||||||
| 330 | 3 | $mail{'Message'} =~ s/\n/$CRLF/go; # normalize line endings, step 2. | |||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||
| 332 | # Get recipients | ||||||||||||||||||||||||||||||||||
| 333 | { # don't warn for undefined values below | ||||||||||||||||||||||||||||||||||
| 334 | 3 | my @recipients; | |||||||||||||||||||||||||||||||||
| 335 | 3 | ▶ | push(@recipients, $mail{To}) if defined($mail{To}); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 336 | 3 | ▶ | push(@recipients, $mail{Cc}) if defined($mail{Cc}); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 337 | 3 | ▶ | push(@recipients, $mail{Bcc}) if defined($mail{Bcc}); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 338 | 3 | $recip = join(", ", @recipients); | |||||||||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||
| 341 | 3 | delete $mail{'Bcc'}; | |||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||
| 343 | 3 | @recipients = (); | |||||||||||||||||||||||||||||||||
| 344 | 3 | ▶ | while ($recip =~ /$address_rx/go) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 345 | 3 | push @recipients, $1; | |||||||||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||||||||
| 347 | 3 | ▶ | unless (@recipients) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 348 | 0 | return fail("No recipient!") | |||||||||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||
| 351 | # get local hostname for polite HELO | ||||||||||||||||||||||||||||||||||
| 352 | 3 | ▶ | $localhost = hostname_long() || hostname() || 'localhost'; | ||||||||||||||||||||||||||||||||
|
Condition40%2/5
hostname_long() || hostname() || 'localhost'
hostname_long() or hostname()
MC/DC0%0/2
hostname_long() || hostname() || 'localhost'
hostname_long()
hostname()
Truth table33%1/3
A or B
A= hostname_long()B= hostname()
| |||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||
| 354 | 3 | foreach $server ( @{$mailcfg{'smtp'}} ) { | |||||||||||||||||||||||||||||||||
| 355 | # open socket needs to be inside this foreach loop on Linux, | ||||||||||||||||||||||||||||||||||
| 356 | # otherwise all servers fail if 1st one fails !??! why? | ||||||||||||||||||||||||||||||||||
| 357 | 4 | ▶ | unless ( socket S, AF_INET, SOCK_STREAM, scalar(getprotobyname 'tcp') ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 358 | 0 | return fail("socket failed ($!)") | |||||||||||||||||||||||||||||||||
| 359 | } | ||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||
| 361 | 4 | ▶ | print "- trying $server\n" if $mailcfg{'debug'} > 1; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||
| 363 | 4 | $server =~ s/\s+//go; # remove spaces just in case of a typo | |||||||||||||||||||||||||||||||||
| 364 | # extract port if server name like "mail.domain.com:2525" | ||||||||||||||||||||||||||||||||||
| 365 | 4 | ▶ | $port = ($server =~ s/:(\d+)$//o) ? $1 : $mailcfg{'port'}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 366 | 4 | $smtp = $server; # save $server for use outside foreach loop | |||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||
| 368 | 4 | my $smtpaddr = inet_aton $server; | |||||||||||||||||||||||||||||||||
| 369 | 4 | ▶ | unless ($smtpaddr) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 370 | 0 | $error .= "$server not found\n"; | |||||||||||||||||||||||||||||||||
| 371 | 0 | next; # next server | |||||||||||||||||||||||||||||||||
| 372 | } | ||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||
| 374 | 4 | my $retried = 0; # reset retries for each server | |||||||||||||||||||||||||||||||||
| 375 | 4 | ▶ | while ( ( not $connected = connect S, pack_sockaddr_in($port, $smtpaddr) ) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $connected = connect(S, pack_sockaddr_in($port, $smtpaddr)) and $retried < $Mail::Sendmail::mailcfg{"retries"}
MC/DC100%2/2
not $connected = connect(S, pack_sockaddr_in($port, $smtpaddr)) and $retried < $Mail::Sendmail::mailcfg{"retries"}
not $connected = connect(S, pack_sockaddr_in($port, $smtpaddr))
$retried < $Mail::Sendmail::mailcfg{"retries"}
Truth table100%3/3
not(A) and B
A= not $connected = connect(S, pack_sockaddr_in($port, $smtpaddr))B= $retried < $Mail::Sendmail::mailcfg{"retries"}
| |||||||||||||||||||||||||||||||||||
| 376 | and ( $retried < $mailcfg{'retries'} ) | ||||||||||||||||||||||||||||||||||
| 377 | ) { | ||||||||||||||||||||||||||||||||||
| 378 | 2 | $retried++; | |||||||||||||||||||||||||||||||||
| 379 | 2 | $error .= "connect to $server failed ($!)\n"; | |||||||||||||||||||||||||||||||||
| 380 | 2 | ▶ | print "- connect to $server failed ($!)\n" if $mailcfg{'debug'} > 1; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 381 | 2 | ▶ | print "retrying in $mailcfg{'delay'} seconds...\n" if $mailcfg{'debug'} > 1; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 382 | 2 | sleep $mailcfg{'delay'}; | |||||||||||||||||||||||||||||||||
| 383 | } | ||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||
| 385 | 4 | ▶ | if ( $connected ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 386 | 2 | ▶ | print "- connected to $server\n" if $mailcfg{'debug'} > 3; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 387 | 2 | last; | |||||||||||||||||||||||||||||||||
| 388 | } | ||||||||||||||||||||||||||||||||||
| 389 | else { | ||||||||||||||||||||||||||||||||||
| 390 | 2 | $error .= "connect to $server failed\n"; | |||||||||||||||||||||||||||||||||
| 391 | 2 | ▶ | print "- connect to $server failed, next server...\n" if $mailcfg{'debug'} > 1; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 392 | 2 | next; # next server | |||||||||||||||||||||||||||||||||
| 393 | } | ||||||||||||||||||||||||||||||||||
| 394 | } | ||||||||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||||||||
| 396 | 3 | ▶ | unless ( $connected ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 397 | 1 | return fail("connect to $smtp failed ($!) no (more) retries!") | |||||||||||||||||||||||||||||||||
| 398 | }; | ||||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||||
| 400 | { | ||||||||||||||||||||||||||||||||||
| 401 | 2 | local $^W = 0; # don't warn on undefined variables | |||||||||||||||||||||||||||||||||
| 402 | # Add info to log variable | ||||||||||||||||||||||||||||||||||
| 403 | 2 | $log .= "Server: $smtp Port: $port\n" | |||||||||||||||||||||||||||||||||
| 404 | . "From: $fromaddr\n" | ||||||||||||||||||||||||||||||||||
| 405 | . "Subject: $mail{Subject}\n" | ||||||||||||||||||||||||||||||||||
| 406 | ; | ||||||||||||||||||||||||||||||||||
| 407 | } | ||||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||||
| 409 | 2 | my($oldfh) = select(S); $| = 1; select($oldfh); | |||||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||||||
| 411 | 2 | ▶ | socket_read() | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 412 | || return fail("Connection error from $smtp on port $port ($_)"); | ||||||||||||||||||||||||||||||||||
| 413 | 2 | ▶ | socket_write("EHLO $localhost$CRLF") | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 414 | || return fail("send EHLO error (lost connection?)"); | ||||||||||||||||||||||||||||||||||
| 415 | 2 | my $ehlo = socket_read(); | |||||||||||||||||||||||||||||||||
| 416 | 2 | ▶ | if ($ehlo) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 417 | # parse EHLO response | ||||||||||||||||||||||||||||||||||
| 418 | map { | ||||||||||||||||||||||||||||||||||
| 419 | 2 | s/^\d+[- ]//; | |||||||||||||||||||||||||||||||||
| 420 | 2 | my ($k, $v) = split /\s+/, $_, 2; | |||||||||||||||||||||||||||||||||
| 421 | 2 | ▶ | $esmtp{$k} = $v || 1 if $k; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$v || 1
MC/DC0%0/1
$v || 1
$v
Truth table50%1/2
A
A= $v
| |||||||||||||||||||||||||||||||||||
| 422 | } split(/\n/, $ehlo); | ||||||||||||||||||||||||||||||||||
| 423 | } | ||||||||||||||||||||||||||||||||||
| 424 | else { | ||||||||||||||||||||||||||||||||||
| 425 | # try plain HELO instead | ||||||||||||||||||||||||||||||||||
| 426 | 0 | ▶ | socket_write("HELO $localhost$CRLF") | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 427 | || return fail("send HELO error (lost connection?)"); | ||||||||||||||||||||||||||||||||||
| 428 | } | ||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||
| 430 | 2 | ▶ | if ($auth) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 431 | 0 | ▶ | warn "AUTH requested\n" if ($mailcfg{debug} > 4); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 432 | # reduce wanted methods to those supported | ||||||||||||||||||||||||||||||||||
| 433 | 0 | my @methods = grep {$esmtp{'AUTH'}=~/(^|\s)$_(\s|$)/i} | |||||||||||||||||||||||||||||||||
| 434 | 0 | grep {$auth_support =~ /(^|\s)$_(\s|$)/i} | |||||||||||||||||||||||||||||||||
| 435 | 0 | grep /\S/, split(/\s+/, $auth->{method}); | |||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||
| 437 | 0 | ▶ | if (@methods) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 438 | # try to authenticate | ||||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||||
| 440 | 0 | ▶ | if (exists $auth->{pass}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 441 | 0 | $auth->{password} = $auth->{pass}; | |||||||||||||||||||||||||||||||||
| 442 | } | ||||||||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||||||||
| 444 | 0 | my $method = uc $methods[0]; | |||||||||||||||||||||||||||||||||
| 445 | 0 | ▶ | _require_base64() || fail("Could not use MIME::Base64 module required for authentication"); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 446 | 0 | ▶ | if ($method eq "LOGIN") { | ||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||
| 447 | 0 | ▶ | print STDERR "Trying AUTH LOGIN\n" if ($mailcfg{debug} > 9); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 448 | 0 | ▶ | socket_write("AUTH LOGIN$CRLF") | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 449 | || return fail("send AUTH LOGIN failed (lost connection?)"); | ||||||||||||||||||||||||||||||||||
| 450 | 0 | ▶ | socket_read() | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 451 | || return fail("AUTH LOGIN failed: $server_reply"); | ||||||||||||||||||||||||||||||||||
| 452 | 0 | ▶ | socket_write(encode_base64($auth->{user},$CRLF)) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 453 | || return fail("send LOGIN username failed (lost connection?)"); | ||||||||||||||||||||||||||||||||||
| 454 | 0 | ▶ | socket_read() | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 455 | || return fail("LOGIN username failed: $server_reply"); | ||||||||||||||||||||||||||||||||||
| 456 | 0 | ▶ | socket_write(encode_base64($auth->{password},$CRLF)) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 457 | || return fail("send LOGIN password failed (lost connection?)"); | ||||||||||||||||||||||||||||||||||
| 458 | 0 | ▶ | socket_read() | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 459 | || return fail("LOGIN password failed: $server_reply"); | ||||||||||||||||||||||||||||||||||
| 460 | } | ||||||||||||||||||||||||||||||||||
| 461 | elsif ($method eq "PLAIN") { | ||||||||||||||||||||||||||||||||||
| 462 | 0 | ▶ | warn "Trying AUTH PLAIN\n" if ($mailcfg{debug} > 9); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 463 | socket_write( | ||||||||||||||||||||||||||||||||||
| 464 | "AUTH PLAIN " | ||||||||||||||||||||||||||||||||||
| 465 | 0 | ▶ | . encode_base64(join("\0", $auth->{user}, $auth->{user}, $auth->{password}), $CRLF) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 466 | ) || return fail("send AUTH PLAIN failed (lost connection?)"); | ||||||||||||||||||||||||||||||||||
| 467 | 0 | ▶ | socket_read() | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 468 | || return fail("AUTH PLAIN failed: $server_reply"); | ||||||||||||||||||||||||||||||||||
| 469 | } | ||||||||||||||||||||||||||||||||||
| 470 | elsif ($method eq "CRAM-MD5") { | ||||||||||||||||||||||||||||||||||
| 471 | 0 | ▶ | _require_md5() || fail("Could not use Digest::MD5 module required for authentication"); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 472 | 0 | ▶ | warn "Trying AUTH CRAM-MD5\n" if ($mailcfg{debug} > 9); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 473 | 0 | ▶ | socket_write("AUTH CRAM-MD5$CRLF") | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 474 | || return fail("send CRAM-MD5 failed (lost connection?)"); | ||||||||||||||||||||||||||||||||||
| 475 | 0 | ▶ | my $challenge = socket_read() | ||||||||||||||||||||||||||||||||
|
Condition0%0/2
socket_read() || (return fail("AUTH CRAM-MD5 failed: $Mail::Sendmail::server_reply"))
MC/DC0%0/1
socket_read() || (return fail("AUTH CRAM-MD5 failed: $Mail::Sendmail::server_reply"))
socket_read()
Truth table0%0/2
A
A= socket_read()
| |||||||||||||||||||||||||||||||||||
| 476 | || return fail("AUTH CRAM-MD5 failed: $server_reply"); | ||||||||||||||||||||||||||||||||||
| 477 | 0 | $challenge =~ s/^\d+\s+//; | |||||||||||||||||||||||||||||||||
| 478 | 0 | my $response = _hmac_md5($auth->{password}, decode_base64($challenge)); | |||||||||||||||||||||||||||||||||
| 479 | 0 | ▶ | socket_write(encode_base64("$auth->{user} $response", $CRLF)) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 480 | || return fail("AUTH CRAM-MD5 failed: $server_reply"); | ||||||||||||||||||||||||||||||||||
| 481 | 0 | ▶ | socket_read() | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 482 | || return fail("AUTH CRAM-MD5 failed: $server_reply"); | ||||||||||||||||||||||||||||||||||
| 483 | } | ||||||||||||||||||||||||||||||||||
| 484 | elsif ($method eq "DIGEST-MD5") { | ||||||||||||||||||||||||||||||||||
| 485 | 0 | ▶ | _require_md5() || fail("Could not use Digest::MD5 module required for authentication"); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 486 | 0 | ▶ | warn "Trying AUTH DIGEST-MD5\n" if ($mailcfg{debug} > 9); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 487 | 0 | ▶ | socket_write("AUTH DIGEST-MD5$CRLF") | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 488 | || return fail("send CRAM-MD5 failed (lost connection?)"); | ||||||||||||||||||||||||||||||||||
| 489 | 0 | ▶ | my $challenge = socket_read() | ||||||||||||||||||||||||||||||||
|
Condition0%0/2
socket_read() || (return fail("AUTH DIGEST-MD5 failed: $Mail::Sendmail::server_reply"))
MC/DC0%0/1
socket_read() || (return fail("AUTH DIGEST-MD5 failed: $Mail::Sendmail::server_reply"))
socket_read()
Truth table0%0/2
A
A= socket_read()
| |||||||||||||||||||||||||||||||||||
| 490 | || return fail("AUTH DIGEST-MD5 failed: $server_reply"); | ||||||||||||||||||||||||||||||||||
| 491 | 0 | $challenge =~ s/^\d+\s+//; $challenge =~ s/[\r\n]+$//; | |||||||||||||||||||||||||||||||||
| 492 | 0 | ▶ | warn "\nCHALLENGE=", decode_base64($challenge), "\n" if ($mailcfg{debug} > 10); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 493 | 0 | my $response = _digest_md5($auth->{user}, $auth->{password}, decode_base64($challenge), $auth->{realm}); | |||||||||||||||||||||||||||||||||
| 494 | 0 | ▶ | warn "\nRESPONSE=$response\n" if ($mailcfg{debug} > 10); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 495 | 0 | ▶ | socket_write(encode_base64($response, ""), $CRLF) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 496 | || return fail("AUTH DIGEST-MD5 failed: $server_reply"); | ||||||||||||||||||||||||||||||||||
| 497 | 0 | ▶ | my $status = socket_read() | ||||||||||||||||||||||||||||||||
|
Condition0%0/2
socket_read() || (return fail("AUTH DIGEST-MD5 failed: $Mail::Sendmail::server_reply"))
MC/DC0%0/1
socket_read() || (return fail("AUTH DIGEST-MD5 failed: $Mail::Sendmail::server_reply"))
socket_read()
Truth table0%0/2
A
A= socket_read()
| |||||||||||||||||||||||||||||||||||
| 498 | || return fail("AUTH DIGEST-MD5 failed: $server_reply"); | ||||||||||||||||||||||||||||||||||
| 499 | 0 | ▶ | if ($status =~ /^3/) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 500 | 0 | ▶ | socket_write($CRLF) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 501 | || return fail("AUTH DIGEST-MD5 failed: $server_reply"); | ||||||||||||||||||||||||||||||||||
| 502 | 0 | ▶ | socket_read() | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 503 | || return fail("AUTH DIGEST-MD5 failed: $server_reply"); | ||||||||||||||||||||||||||||||||||
| 504 | } | ||||||||||||||||||||||||||||||||||
| 505 | } | ||||||||||||||||||||||||||||||||||
| 506 | else { | ||||||||||||||||||||||||||||||||||
| 507 | 0 | return fail("$method not supported (and wrongly advertised as supported by this silly module)\n"); | |||||||||||||||||||||||||||||||||
| 508 | } | ||||||||||||||||||||||||||||||||||
| 509 | 0 | $log .= "AUTH $method succeeded as user $auth->{user}\n"; | |||||||||||||||||||||||||||||||||
| 510 | } | ||||||||||||||||||||||||||||||||||
| 511 | else { | ||||||||||||||||||||||||||||||||||
| 512 | 0 | $esmtp{'AUTH'} =~ s/(^\s+|\s+$)//g; # cleanup for printig it below | |||||||||||||||||||||||||||||||||
| 513 | 0 | ▶ | if ($auth->{required}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 514 | 0 | return fail("Required AUTH method '$auth->{method}' not supported. " | |||||||||||||||||||||||||||||||||
| 515 | ."(Server supports '$esmtp{'AUTH'}'. Module supports: '$auth_support')"); | ||||||||||||||||||||||||||||||||||
| 516 | } | ||||||||||||||||||||||||||||||||||
| 517 | else { | ||||||||||||||||||||||||||||||||||
| 518 | 0 | warn "No common authentication method! Requested: '$auth->{method}'. Server supports '$esmtp{'AUTH'}'. Module supports: '$auth_support'. Skipping authentication\n"; | |||||||||||||||||||||||||||||||||
| 519 | } | ||||||||||||||||||||||||||||||||||
| 520 | } | ||||||||||||||||||||||||||||||||||
| 521 | } | ||||||||||||||||||||||||||||||||||
| 522 | 2 | ▶ | socket_write("MAIL FROM:<$fromaddr>$CRLF") | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 523 | || return fail("send MAIL FROM: error"); | ||||||||||||||||||||||||||||||||||
| 524 | 2 | ▶ | socket_read() | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 525 | || return fail("MAIL FROM: error ($_)"); | ||||||||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||||||||
| 527 | 2 | my $to_ok = 0; | |||||||||||||||||||||||||||||||||
| 528 | 2 | foreach $to (@recipients) { | |||||||||||||||||||||||||||||||||
| 529 | 2 | ▶ | socket_write("RCPT TO:<$to>$CRLF") | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 530 | || return fail("send RCPT TO: error"); | ||||||||||||||||||||||||||||||||||
| 531 | 2 | ▶ | if (socket_read()) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 532 | 2 | $log .= "To: $to\n"; | |||||||||||||||||||||||||||||||||
| 533 | 2 | $to_ok++; | |||||||||||||||||||||||||||||||||
| 534 | } else { | ||||||||||||||||||||||||||||||||||
| 535 | 0 | $log .= "FAILED To: $to ($server_reply)"; | |||||||||||||||||||||||||||||||||
| 536 | 0 | $error .= "Bad recipient <$to>: $server_reply\n"; | |||||||||||||||||||||||||||||||||
| 537 | } | ||||||||||||||||||||||||||||||||||
| 538 | } | ||||||||||||||||||||||||||||||||||
| 539 | 2 | ▶ | unless ($to_ok) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 540 | 0 | return fail("No valid recipient"); | |||||||||||||||||||||||||||||||||
| 541 | } | ||||||||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||||||||
| 543 | # start data part | ||||||||||||||||||||||||||||||||||
| 544 | |||||||||||||||||||||||||||||||||||
| 545 | 2 | ▶ | socket_write("DATA$CRLF") | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 546 | || return fail("send DATA error"); | ||||||||||||||||||||||||||||||||||
| 547 | 2 | ▶ | socket_read() | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 548 | || return fail("DATA error ($_)"); | ||||||||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||||||||
| 550 | # print headers | ||||||||||||||||||||||||||||||||||
| 551 | 2 | foreach $header (keys %mail) { | |||||||||||||||||||||||||||||||||
| 552 | 16 | ▶ | next if $header eq "Message"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 553 | 14 | $mail{$header} =~ s/\s+$//o; # kill possible trailing garbage | |||||||||||||||||||||||||||||||||
| 554 | 14 | ▶ | socket_write("$header: $mail{$header}$CRLF") | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 555 | || return fail("send $header: error"); | ||||||||||||||||||||||||||||||||||
| 556 | }; | ||||||||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||||||||
| 558 | #- test disconnecting from network here, to see what happens | ||||||||||||||||||||||||||||||||||
| 559 | #- print STDERR "DISCONNECT NOW!\n"; | ||||||||||||||||||||||||||||||||||
| 560 | #- sleep 4; | ||||||||||||||||||||||||||||||||||
| 561 | #- print STDERR "trying to continue, expecting an error... \n"; | ||||||||||||||||||||||||||||||||||
| 562 | |||||||||||||||||||||||||||||||||||
| 563 | # send message body (passed as a reference, in case it's big) | ||||||||||||||||||||||||||||||||||
| 564 | 2 | ▶ | socket_write($CRLF, \$mail{'Message'}, "$CRLF.$CRLF") | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 565 | || return fail("send message error"); | ||||||||||||||||||||||||||||||||||
| 566 | 2 | ▶ | socket_read() | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 567 | || return fail("message transmission error ($_)"); | ||||||||||||||||||||||||||||||||||
| 568 | 2 | $log .= "\nResult: $_"; | |||||||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||||||||
| 570 | # finish | ||||||||||||||||||||||||||||||||||
| 571 | 2 | ▶ | socket_write("QUIT$CRLF") | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 572 | || return fail("send QUIT error"); | ||||||||||||||||||||||||||||||||||
| 573 | 2 | socket_read(); | |||||||||||||||||||||||||||||||||
| 574 | 2 | close S; | |||||||||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||||||||
| 576 | 2 | return 1; | |||||||||||||||||||||||||||||||||
| 577 | } # end sub sendmail | ||||||||||||||||||||||||||||||||||
| 578 | |||||||||||||||||||||||||||||||||||
| 579 | 2 | 1; | |||||||||||||||||||||||||||||||||
| 580 | __END__ | ||||||||||||||||||||||||||||||||||