our
$VERSION
=
'2.20191106'
;
sub
default_config {
return
{};
}
sub
grafana_rows {
my
(
$self
) =
@_
;
my
@rows
;
push
@rows
,
$self
->get_json(
'TLS_metrics'
);
return
\
@rows
;
}
sub
register_metrics {
return
{
'tls_connect_total'
=>
'The number of connections which were enctypted'
,
};
}
sub
pre_loop_setup {
my
(
$self
) =
@_
;
my
$protocol
= Mail::Milter::Authentication::Config::get_config()->{
'protocol'
};
if
(
$protocol
eq
'smtp'
) {
warn
'When in smtp mode, the TLS handler requires the MTA to write TLS data into the first Received header.'
;
}
return
;
}
sub
connect_callback {
my
(
$self
) =
@_
;
delete
$self
->{
'is_encrypted'
};
return
;
}
sub
envfrom_callback {
my
(
$self
,
$env_from
) =
@_
;
delete
$self
->{
'first_header_read'
};
my
$protocol
= Mail::Milter::Authentication::Config::get_config()->{
'protocol'
};
return
if
$protocol
ne
'milter'
;
my
$version
=
$self
->get_symbol(
'{tls_version}'
);
my
$cipher
=
$self
->get_symbol(
'{cipher}'
);
my
$bits
=
$self
->get_symbol(
'{cipher_bits}'
);
my
$trusted
=
$self
->get_symbol(
'{cert_issuer}'
) ?
', trusted'
:
''
;
if
(
$version
) {
$self
->dbgout(
'EncryptedAs'
,
"$version, $cipher, $bits bits$trusted"
, LOG_INFO );
$self
->{
'is_encrypted'
} = 1;
my
$metric_data
= {};
my
$header
= Mail::AuthenticationResults::Header::Entry->new()->set_key(
'x-tls'
)->safe_set_value(
'pass'
);
$header
->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key(
'smtp.version'
)->safe_set_value(
$version
) );
$metric_data
->{
'version'
} =
$version
;
if
(
$cipher
) {
$header
->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key(
'smtp.cipher'
)->safe_set_value(
$cipher
) );
$metric_data
->{
'cipher'
} =
$cipher
;
}
if
(
$bits
) {
$header
->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key(
'smtp.bits'
)->safe_set_value(
$bits
) );
$metric_data
->{
'bits'
} =
$bits
;
}
$metric_data
->{
'trusted'
} =
$trusted
? 1 : 0;
$self
->metric_count(
'tls_connect_total'
,
$metric_data
);
$self
->add_auth_header(
$header
);
}
else
{
$self
->{
'is_encrypted'
} = 0;
}
return
;
}
sub
header_callback {
my
(
$self
,
$header
,
$value
) =
@_
;
return
if
lc
$header
ne
'received'
;
return
if
(
exists
(
$self
->{
'first_header_read'
} ) );
$self
->{
'first_header_read'
} = 1;
my
$protocol
= Mail::Milter::Authentication::Config::get_config()->{
'protocol'
};
return
if
$protocol
ne
'smtp'
;
$value
=~ m/using ([^ ]*)
with
cipher ([^ ]+) \(([^ ]+) bits\)/;
my
$version
= $1;
my
$cipher
= $2;
my
$bits
= $3;
if
(
$version
) {
$self
->dbgout(
'EncryptedAs'
,
"$version, $cipher, $bits bits"
, LOG_INFO );
$self
->{
'is_encrypted'
} = 1;
my
$metric_data
= {};
my
$header
= Mail::AuthenticationResults::Header::Entry->new()->set_key(
'x-tls'
)->safe_set_value(
'pass'
);
$header
->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key(
'smtp.version'
)->safe_set_value(
$version
) );
$metric_data
->{
'version'
} =
$version
;
if
(
$cipher
) {
$header
->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key(
'smtp.cipher'
)->safe_set_value(
$cipher
) );
$metric_data
->{
'cipher'
} =
$cipher
;
}
if
(
$bits
) {
$header
->add_child( Mail::AuthenticationResults::Header::SubEntry->new()->set_key(
'smtp.bits'
)->safe_set_value(
$bits
) );
$metric_data
->{
'bits'
} =
$bits
;
}
$self
->metric_count(
'tls_connect_total'
,
$metric_data
);
$self
->add_auth_header(
$header
);
}
return
;
}
sub
eoh_callback {
my
(
$self
) =
@_
;
my
$protocol
= Mail::Milter::Authentication::Config::get_config()->{
'protocol'
};
return
if
$protocol
ne
'smtp'
;
return
if
defined
$self
->{
'is_encrypted'
};
$self
->{
'is_encrypted'
} = 0;
return
;
}
sub
close_callback {
my
(
$self
) =
@_
;
delete
$self
->{
'first_header_read'
};
delete
$self
->{
'is_encrypted'
};
return
;
}
1;