| 1 | package Mail::Milter::Authentication::Handler::TLS; | ||||||||||
| 2 | 16 | ▶ | use 5.20.0; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 16 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 16 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 16 | ▶ | use Mail::Milter::Authentication::Pragmas; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | # ABSTRACT: Handler class for TLS | ||||||||||
| 7 | 16 | our $VERSION = '4.20261116'; # VERSION | |||||||||
| 8 | 16 | ▶ | use base 'Mail::Milter::Authentication::Handler'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 0 | ▶ | sub default_config { | ||||||||
|
Subroutine
default_config: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 11 | 0 | return {}; | |||||||||
| 12 | } | ||||||||||
| 13 | |||||||||||
| 14 | 0 | ▶ | sub grafana_rows { | ||||||||
|
Subroutine
grafana_rows: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 15 | 0 | my ( $self ) = @_; | |||||||||
| 16 | 0 | my @rows; | |||||||||
| 17 | 0 | push @rows, $self->get_json( 'TLS_metrics' ); | |||||||||
| 18 | 0 | return \@rows; | |||||||||
| 19 | } | ||||||||||
| 20 | |||||||||||
| 21 | 15 | ▶ | sub register_metrics { | ||||||||
|
Subroutine
register_metrics: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 22 | return { | ||||||||||
| 23 | 15 | 'tls_connect_total' => 'The number of connections which were enctypted', | |||||||||
| 24 | }; | ||||||||||
| 25 | } | ||||||||||
| 26 | |||||||||||
| 27 | 15 | ▶ | sub pre_loop_setup { | ||||||||
|
Subroutine
pre_loop_setup: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||
| 28 | 15 | my ( $self ) = @_; | |||||||||
| 29 | 15 | my $protocol = Mail::Milter::Authentication::Config::get_config()->{'protocol'}; | |||||||||
| 30 | 15 | ▶ | if ( $protocol eq 'smtp' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | 15 | warn 'When in smtp mode, the TLS handler requires the MTA to write TLS data into the first Received header.'; | |||||||||
| 32 | } | ||||||||||
| 33 | } | ||||||||||
| 34 | |||||||||||
| 35 | 40 | ▶ | sub connect_callback { | ||||||||
|
Subroutine
connect_callback: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 36 | 40 | my ( $self ) = @_; | |||||||||
| 37 | # Reset state on a connection | ||||||||||
| 38 | 40 | delete $self->{'is_encrypted'}; | |||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | 40 | ▶ | sub envfrom_callback { | ||||||||
|
Subroutine
envfrom_callback: called CC=7 SCAR=36.6
Pod
undocumented
| |||||||||||
| 42 | 40 | my ( $self, $env_from ) = @_; | |||||||||
| 43 | |||||||||||
| 44 | 40 | delete $self->{'first_header_read'}; | |||||||||
| 45 | |||||||||||
| 46 | 40 | my $protocol = Mail::Milter::Authentication::Config::get_config()->{'protocol'}; | |||||||||
| 47 | 40 | ▶ | return if $protocol ne 'milter'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | |||||||||||
| 49 | 0 | my $version = $self->get_symbol('{tls_version}'); | |||||||||
| 50 | 0 | my $cipher = $self->get_symbol('{cipher}'); | |||||||||
| 51 | 0 | my $bits = $self->get_symbol('{cipher_bits}'); | |||||||||
| 52 | # on postfix the macro is empty on untrusted connections | ||||||||||
| 53 | 0 | ▶ | my $trusted = $self->get_symbol('{cert_issuer}') ? ', trusted' : ''; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | |||||||||||
| 55 | 0 | ▶ | if ($version) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | 0 | $self->dbgout( 'EncryptedAs', "$version, $cipher, $bits bits$trusted", LOG_DEBUG ); | |||||||||
| 57 | 0 | $self->{'is_encrypted'} = 1; | |||||||||
| 58 | |||||||||||
| 59 | 0 | my $metric_data = {}; | |||||||||
| 60 | 0 | my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'x-tls' )->safe_set_value( 'pass' ); | |||||||||
| 61 | 0 | $header->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'smtp.version' )->safe_set_value( $version ) ); | |||||||||
| 62 | 0 | $metric_data->{ 'version' } = $version; | |||||||||
| 63 | |||||||||||
| 64 | 0 | ▶ | if ( $cipher ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | 0 | $header->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'smtp.cipher' )->safe_set_value( $cipher ) ); | |||||||||
| 66 | 0 | $metric_data->{ 'cipher' } = $cipher; | |||||||||
| 67 | } | ||||||||||
| 68 | 0 | ▶ | if ( $bits ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | 0 | $header->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'smtp.bits' )->safe_set_value( $bits ) ); | |||||||||
| 70 | 0 | $metric_data->{ 'bits' } = $bits; | |||||||||
| 71 | } | ||||||||||
| 72 | 0 | ▶ | $metric_data->{ 'trusted' } = $trusted ? 1 : 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | |||||||||||
| 74 | 0 | $self->metric_count( 'tls_connect_total', $metric_data ); | |||||||||
| 75 | |||||||||||
| 76 | 0 | $self->add_auth_header( $header ); | |||||||||
| 77 | } | ||||||||||
| 78 | else { | ||||||||||
| 79 | 0 | $self->{'is_encrypted'} = 0; | |||||||||
| 80 | } | ||||||||||
| 81 | } | ||||||||||
| 82 | |||||||||||
| 83 | 409 | ▶ | sub header_callback { | ||||||||
|
Subroutine
header_callback: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||
| 84 | 409 | my ( $self, $header, $value ) = @_; | |||||||||
| 85 | |||||||||||
| 86 | 409 | ▶ | return if lc $header ne 'received'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | 64 | ▶ | return if ( exists( $self->{'first_header_read'} ) ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | 28 | $self->{'first_header_read'} = 1; | |||||||||
| 89 | |||||||||||
| 90 | |||||||||||
| 91 | 28 | my $protocol = Mail::Milter::Authentication::Config::get_config()->{'protocol'}; | |||||||||
| 92 | 28 | ▶ | return if $protocol ne 'smtp'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 93 | |||||||||||
| 94 | # Try and parse the first received header, this should be something like... | ||||||||||
| 95 | # Received: from mail-ua0-f173.google.com (mail-ua0-f173.google.com [209.85.217.173]) | ||||||||||
| 96 | # (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) | ||||||||||
| 97 | # (No client certificate requested) | ||||||||||
| 98 | # by mx5.messagingengine.com (Postfix) with ESMTPS | ||||||||||
| 99 | # for <marcmctest@fastmail.com>; Thu, 1 Dec 2016 22:35:06 -0500 (EST) | ||||||||||
| 100 | |||||||||||
| 101 | # Future, extend to check for client certificates | ||||||||||
| 102 | |||||||||||
| 103 | 28 | $value =~ m/using ([^ ]*) with cipher ([^ ]+) \(([^ ]+) bits\)/; | |||||||||
| 104 | 28 | my $version = $1; | |||||||||
| 105 | 28 | my $cipher = $2; | |||||||||
| 106 | 28 | my $bits = $3; | |||||||||
| 107 | |||||||||||
| 108 | 28 | ▶ | if ($version) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 109 | 14 | $self->dbgout( 'EncryptedAs', "$version, $cipher, $bits bits", LOG_DEBUG ); | |||||||||
| 110 | 14 | $self->{'is_encrypted'} = 1; | |||||||||
| 111 | |||||||||||
| 112 | 14 | my $metric_data = {}; | |||||||||
| 113 | 14 | my $header = Mail::AuthenticationResults::Header::Entry->new()->set_key( 'x-tls' )->safe_set_value( 'pass' ); | |||||||||
| 114 | 14 | $header->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'smtp.version' )->safe_set_value( $version ) ); | |||||||||
| 115 | 14 | $metric_data->{ 'version' } = $version; | |||||||||
| 116 | |||||||||||
| 117 | 14 | ▶ | if ( $cipher ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 118 | 14 | $header->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'smtp.cipher' )->safe_set_value( $cipher ) ); | |||||||||
| 119 | 14 | $metric_data->{ 'cipher' } = $cipher; | |||||||||
| 120 | } | ||||||||||
| 121 | 14 | ▶ | if ( $bits ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 122 | 14 | $header->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key( 'smtp.bits' )->safe_set_value( $bits ) ); | |||||||||
| 123 | 14 | $metric_data->{ 'bits' } = $bits; | |||||||||
| 124 | } | ||||||||||
| 125 | |||||||||||
| 126 | 14 | $self->metric_count( 'tls_connect_total', $metric_data ); | |||||||||
| 127 | |||||||||||
| 128 | 14 | $self->add_auth_header( $header ); | |||||||||
| 129 | } | ||||||||||
| 130 | } | ||||||||||
| 131 | |||||||||||
| 132 | 40 | ▶ | sub eoh_callback { | ||||||||
|
Subroutine
eoh_callback: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 133 | 40 | my ( $self ) = @_; | |||||||||
| 134 | 40 | my $protocol = Mail::Milter::Authentication::Config::get_config()->{'protocol'}; | |||||||||
| 135 | 40 | ▶ | return if $protocol ne 'smtp'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 136 | 40 | ▶ | return if defined $self->{'is_encrypted'}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 137 | 26 | $self->{'is_encrypted'} = 0; | |||||||||
| 138 | } | ||||||||||
| 139 | |||||||||||
| 140 | 72 | ▶ | sub close_callback { | ||||||||
|
Subroutine
close_callback: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 141 | 72 | my ( $self ) = @_; | |||||||||
| 142 | 72 | delete $self->{'first_header_read'}; | |||||||||
| 143 | 72 | delete $self->{'is_encrypted'}; | |||||||||
| 144 | } | ||||||||||
| 145 | |||||||||||
| 146 | 16 | 1; | |||||||||
| 147 | |||||||||||
| 148 | __END__ | ||||||||||