| 1 | #!/usr/bin/perl | ||||||||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use 5.020; use strict; use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use FindBin; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use lib -d "$FindBin::Bin/../lib/Mail/DKIM2" ? "$FindBin::Bin/../lib" : (); # running from a checkout | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.1
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use IO::Socket::INET; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Net::SMTP; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use POSIX ':sys_wait_h'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Sys::Syslog qw(:standard :macros); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Mail::DKIM2::Split qw(plan_copies); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||
| 11 | # dkim2-split-lmtp: a Bcc-safe DKIM2 origination content filter. | ||||||||||||||||||||||||||||||||||
| 12 | # | ||||||||||||||||||||||||||||||||||
| 13 | # An LMTP server that Postfix delivers submitted mail to (after-queue content | ||||||||||||||||||||||||||||||||||
| 14 | # filter). It fans one message out into copies -- disclosed recipients share a | ||||||||||||||||||||||||||||||||||
| 15 | # copy, each Bcc recipient gets its own (Mail::DKIM2::Split) -- and re-injects | ||||||||||||||||||||||||||||||||||
| 16 | # each copy to the DKIM2 signing listener, so the signer records only that | ||||||||||||||||||||||||||||||||||
| 17 | # copy's recipients in rt= and Bcc never leaks. | ||||||||||||||||||||||||||||||||||
| 18 | # | ||||||||||||||||||||||||||||||||||
| 19 | # LMTP is used deliberately: it returns a status PER RECIPIENT after the final | ||||||||||||||||||||||||||||||||||
| 20 | # ".", so each recipient is answered 250 (its copy re-injected) or 5xx (bounce) | ||||||||||||||||||||||||||||||||||
| 21 | # individually -- which Postfix turns into a per-recipient delivery/bounce. | ||||||||||||||||||||||||||||||||||
| 22 | # | ||||||||||||||||||||||||||||||||||
| 23 | # Wiring (see deploy/SERVER.md "Bcc-safe origination"): | ||||||||||||||||||||||||||||||||||
| 24 | # submission smtpd: -o content_filter=lmtp:[127.0.0.1]:10590 (no signing milter) | ||||||||||||||||||||||||||||||||||
| 25 | # this daemon: listens on 10590, re-injects to 10589 | ||||||||||||||||||||||||||||||||||
| 26 | # 127.0.0.1:10589: smtpd with the DKIM2 signing milter and content_filter= empty | ||||||||||||||||||||||||||||||||||
| 27 | # | ||||||||||||||||||||||||||||||||||
| 28 | # Config via env: DKIM2_SPLIT_PORT (10590), DKIM2_INJECT_HOST (127.0.0.1), | ||||||||||||||||||||||||||||||||||
| 29 | # DKIM2_INJECT_PORT (10589). | ||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||
| 31 | 1 | ▶ | my $LISTEN_HOST = $ENV{DKIM2_SPLIT_HOST} // '127.0.0.1'; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$ENV{"DKIM2_SPLIT_HOST"} // "127.0.0.1"
MC/DC0%0/1
$ENV{"DKIM2_SPLIT_HOST"} // "127.0.0.1"
$ENV{"DKIM2_SPLIT_HOST"}
Truth table50%1/2
A
A= $ENV{"DKIM2_SPLIT_HOST"}
| |||||||||||||||||||||||||||||||||||
| 32 | 1 | ▶ | my $LISTEN_PORT = $ENV{DKIM2_SPLIT_PORT} // 10590; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$ENV{"DKIM2_SPLIT_PORT"} // 10590
MC/DC0%0/1
$ENV{"DKIM2_SPLIT_PORT"} // 10590
$ENV{"DKIM2_SPLIT_PORT"}
Truth table50%1/2
A
A= $ENV{"DKIM2_SPLIT_PORT"}
| |||||||||||||||||||||||||||||||||||
| 33 | 1 | ▶ | my $INJECT_HOST = $ENV{DKIM2_INJECT_HOST} // '127.0.0.1'; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$ENV{"DKIM2_INJECT_HOST"} // "127.0.0.1"
MC/DC0%0/1
$ENV{"DKIM2_INJECT_HOST"} // "127.0.0.1"
$ENV{"DKIM2_INJECT_HOST"}
Truth table50%1/2
A
A= $ENV{"DKIM2_INJECT_HOST"}
| |||||||||||||||||||||||||||||||||||
| 34 | 1 | ▶ | my $INJECT_PORT = $ENV{DKIM2_INJECT_PORT} // 10589; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$ENV{"DKIM2_INJECT_PORT"} // 10589
MC/DC0%0/1
$ENV{"DKIM2_INJECT_PORT"} // 10589
$ENV{"DKIM2_INJECT_PORT"}
Truth table50%1/2
A
A= $ENV{"DKIM2_INJECT_PORT"}
| |||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||
| 36 | 1 | openlog('dkim2-split', 'pid', LOG_MAIL); | |||||||||||||||||||||||||||||||||
| 37 | 3 | ▶ | sub logmsg { syslog(LOG_INFO, '%s', $_[0]); } | ||||||||||||||||||||||||||||||||
|
Subroutine
logmsg: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||
| 39 | # Re-inject one copy (a single SMTP transaction) to the signing listener. | ||||||||||||||||||||||||||||||||||
| 40 | # Returns 1 on success, 0 on any failure. | ||||||||||||||||||||||||||||||||||
| 41 | 2 | ▶ | sub inject_copy { | ||||||||||||||||||||||||||||||||
|
Subroutine
inject_copy: called CC=6 SCAR=19.1
| |||||||||||||||||||||||||||||||||||
| 42 | 2 | my ($sender, $rcpts, $msg) = @_; | |||||||||||||||||||||||||||||||||
| 43 | 2 | my $smtp = Net::SMTP->new($INJECT_HOST, Port => $INJECT_PORT, Timeout => 60); | |||||||||||||||||||||||||||||||||
| 44 | 2 | ▶ | return 0 unless $smtp; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 45 | 2 | ▶ | my $ok = $smtp->mail(length $sender ? $sender : '<>'); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 46 | 2 | ▶ | $ok &&= $smtp->recipient(@$rcpts); | ||||||||||||||||||||||||||||||||
|
Condition33%1/3
$ok &&= $smtp->recipient(@$rcpts)
MC/DC0%0/2
$ok &&= $smtp->recipient(@$rcpts)
$ok
$smtp->recipient(@$rcpts)
Truth table33%1/3
A &&= B
A= $okB= $smtp->recipient(@$rcpts)
| |||||||||||||||||||||||||||||||||||
| 47 | 2 | ▶ | if ($ok) { $smtp->data; $smtp->datasend($msg); $ok = $smtp->dataend; } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 48 | 2 | $smtp->quit; | |||||||||||||||||||||||||||||||||
| 49 | 2 | ▶ | return $ok ? 1 : 0; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||
| 52 | # Handle one LMTP transaction on an accepted connection. | ||||||||||||||||||||||||||||||||||
| 53 | 1 | ▶ | sub handle_conn { | ||||||||||||||||||||||||||||||||
|
Subroutine
handle_conn: called CC=18 SCAR=31.6
| |||||||||||||||||||||||||||||||||||
| 54 | 1 | my ($c) = @_; | |||||||||||||||||||||||||||||||||
| 55 | 1 | $c->autoflush(1); | |||||||||||||||||||||||||||||||||
| 56 | 1 | print $c "220 dkim2-split LMTP ready\r\n"; | |||||||||||||||||||||||||||||||||
| 57 | 1 | my ($sender, @rcpts); | |||||||||||||||||||||||||||||||||
| 58 | 1 | ▶ | while (my $line = <$c>) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 59 | 7 | $line =~ s/\r?\n$//; | |||||||||||||||||||||||||||||||||
| 60 | 7 | ▶ | if ($line =~ /^LHLO\b/i) { | ||||||||||||||||||||||||||||||||
|
Branch: 7 branches
| |||||||||||||||||||||||||||||||||||
| 61 | 1 | print $c "250 dkim2-split\r\n"; | |||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||
| 63 | elsif ($line =~ /^MAIL\s+FROM:\s*(<[^>]*>|\S+)/i) { | ||||||||||||||||||||||||||||||||||
| 64 | 1 | $sender = $1; $sender =~ s/^<//; $sender =~ s/>$//; | |||||||||||||||||||||||||||||||||
| 65 | 1 | @rcpts = (); | |||||||||||||||||||||||||||||||||
| 66 | 1 | print $c "250 2.1.0 Ok\r\n"; | |||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||
| 68 | elsif ($line =~ /^RCPT\s+TO:\s*(<[^>]*>|\S+)/i) { | ||||||||||||||||||||||||||||||||||
| 69 | 3 | push @rcpts, $1; | |||||||||||||||||||||||||||||||||
| 70 | 3 | print $c "250 2.1.5 Ok\r\n"; | |||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||
| 72 | elsif ($line =~ /^DATA\b/i) { | ||||||||||||||||||||||||||||||||||
| 73 | 1 | print $c "354 End data with <CR><LF>.<CR><LF>\r\n"; | |||||||||||||||||||||||||||||||||
| 74 | 1 | my $msg = ''; | |||||||||||||||||||||||||||||||||
| 75 | 1 | ▶ | while (my $l = <$c>) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 76 | 6 | ▶ | last if $l eq ".\r\n" || $l eq ".\n"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$l eq ".\r\n" or $l eq ".\n"
MC/DC50%1/2
$l eq ".\r\n" or $l eq ".\n"
$l eq ".\r\n"
$l eq ".\n"
Truth table66%2/3
A or B
A= $l eq ".\r\n"B= $l eq ".\n"
| |||||||||||||||||||||||||||||||||||
| 77 | 5 | $l =~ s/^\.//; # dot-unstuffing | |||||||||||||||||||||||||||||||||
| 78 | 5 | $msg .= $l; | |||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||
| 80 | # Plan copies and re-inject each; record success per copy. | ||||||||||||||||||||||||||||||||||
| 81 | 1 | my $copies = eval { plan_copies($msg, \@rcpts) }; | |||||||||||||||||||||||||||||||||
| 82 | 1 | ▶ | if (!$copies) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 83 | # couldn't parse/plan -> temp-fail every recipient (LMTP: one per rcpt) | ||||||||||||||||||||||||||||||||||
| 84 | 0 | print $c "451 4.3.0 split failed, try again later\r\n" for @rcpts; | |||||||||||||||||||||||||||||||||
| 85 | 0 | logmsg("plan failed for sender=$sender rcpts=@rcpts: $@"); | |||||||||||||||||||||||||||||||||
| 86 | 0 | @rcpts = (); $sender = undef; next; | |||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||
| 88 | 1 | my %result; # envelope-rcpt string -> 1 ok / 0 fail | |||||||||||||||||||||||||||||||||
| 89 | 1 | for my $copy (@$copies) { | |||||||||||||||||||||||||||||||||
| 90 | 2 | my $ok = inject_copy($sender, $copy->{rcpts}, $msg); | |||||||||||||||||||||||||||||||||
| 91 | 2 | $result{$_} = $ok for @{$copy->{rcpts}}; | |||||||||||||||||||||||||||||||||
| 92 | 2 | logmsg(sprintf("copy rcpts=[%s] injected=%d", join(',', @{$copy->{rcpts}}), $ok)); | |||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||
| 94 | # LMTP: one reply per recipient, in the order they were given. | ||||||||||||||||||||||||||||||||||
| 95 | 1 | for my $r (@rcpts) { | |||||||||||||||||||||||||||||||||
| 96 | 3 | ▶ | if ($result{$r}) { print $c "250 2.1.5 Ok (split+re-injected)\r\n"; } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 97 | 0 | else { print $c "451 4.3.0 re-injection failed\r\n"; } | |||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||
| 99 | 1 | @rcpts = (); $sender = undef; | |||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||
| 101 | elsif ($line =~ /^RSET\b/i) { | ||||||||||||||||||||||||||||||||||
| 102 | 0 | @rcpts = (); $sender = undef; | |||||||||||||||||||||||||||||||||
| 103 | 0 | print $c "250 2.0.0 Ok\r\n"; | |||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||
| 105 | elsif ($line =~ /^(QUIT)\b/i) { | ||||||||||||||||||||||||||||||||||
| 106 | 1 | print $c "221 2.0.0 Bye\r\n"; | |||||||||||||||||||||||||||||||||
| 107 | 1 | last; | |||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||
| 109 | elsif ($line =~ /^(NOOP|VRFY|HELP)\b/i) { | ||||||||||||||||||||||||||||||||||
| 110 | 0 | print $c "250 2.0.0 Ok\r\n"; | |||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||
| 112 | else { | ||||||||||||||||||||||||||||||||||
| 113 | 0 | print $c "500 5.5.2 Unrecognized command\r\n"; | |||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||
| 116 | 1 | close $c; | |||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||
| 119 | # --- fork-per-connection server --------------------------------------------- | ||||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | $SIG{CHLD} = sub { while (waitpid(-1, WNOHANG) > 0) {} }; | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=13.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 121 | 1 | ▶ | my $srv = IO::Socket::INET->new( | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 122 | LocalHost => $LISTEN_HOST, LocalPort => $LISTEN_PORT, | ||||||||||||||||||||||||||||||||||
| 123 | Listen => 20, ReuseAddr => 1, Proto => 'tcp', | ||||||||||||||||||||||||||||||||||
| 124 | ) or die "dkim2-split: cannot listen on $LISTEN_HOST:$LISTEN_PORT: $!\n"; | ||||||||||||||||||||||||||||||||||
| 125 | 1 | logmsg("listening on $LISTEN_HOST:$LISTEN_PORT, re-injecting to $INJECT_HOST:$INJECT_PORT"); | |||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||
| 127 | 1 | while (1) { | |||||||||||||||||||||||||||||||||
| 128 | 1 | ▶ | my $c = $srv->accept or next; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 129 | 1 | my $pid = fork; | |||||||||||||||||||||||||||||||||
| 130 | 0 | ▶ | if (!defined $pid) { print $c "421 4.3.0 fork failed\r\n"; close $c; next; } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 131 | 1 | ▶ | if ($pid == 0) { $srv->close; handle_conn($c); exit 0; } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 132 | 0 | $c->close; | |||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||