-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplainmails-out
executable file
·82 lines (81 loc) · 2.5 KB
/
plainmails-out
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/bin/php
<?php
$max_mx = 10;
$conns = [];
$stdin = fopen( 'php://stdin', 'r' );
while ( !feof( $stdin ) ) {
$line = fgets( $stdin );
# get both of the following lines and group them by pid
# Jun 23 15:45:16 smtp-out postfix/smtp[996628]: Trusted TLS connection established to example.com[123.123.124.136]:25: ...
# Jun 23 15:44:29 smtp-out postfix/smtp[994487]: AE3097C000B5: to=<[email protected]>, relayfoo.example.com[123.123.135.90]:25 ... status=sent ...
preg_match( '#^.*?postfix/smtp\[(.*?)\]: (.*?(TLS connection| to=<.*?status=sent).*?)$#', $line, $matches );
if ( $matches ) {
$pid = $matches[1];
$txt = $matches[2];
$conns[$pid][] = $txt;
}
}
fclose( $stdin );
ksort( $conns );
$mx_plain = [];
$mx_tls = [];
$mails_tls = 0;
$mails_plain = 0;
foreach ( $conns as $conn ) {
$tls_ips = [];
foreach ( $conn as $line ) {
# add TLS connection IP to array
preg_match( '#^.*?TLS connection established to (.*?)\[(.*?)\].*?$#', $line, $matches );
if ( $matches ) {
list( $full, $host, $ip ) = $matches;
$tls_ips[] = $ip;
}
# check if "to"-IP is in array
# ... to=<[email protected]>, relay=mailproxy.example.com[123.123.162.145]...
preg_match( '#^.*? to=<(.*?)>.*?relay=(.*?)\[(.*?)\].*?$#', $line, $matches );
if ( $matches ) {
list( $to_line, $to_addr, $to_relay, $to_ip ) = $matches;
if ( ! in_array( $to_ip, $tls_ips ) ) {
#echo "no tls to: $to_addr via $to_relay\n";
$mails_plain++;
$mx_plain[] = "$to_relay ($to_ip)";
}
else {
$mails_tls++;
$mx_tls[] = "$to_relay ($to_ip)";
}
}
}
}
$mails_total = $mails_tls + $mails_plain;
$mails_plain_perc = ( $mails_total == 0 ) ? 0 : round( $mails_plain / $mails_total * 100);
underline( "Outgoing mail count (total $mails_total):" );
echo "$mails_tls\tusing TLS\n";
echo "$mails_plain\tunencrypted ($mails_plain_perc %)\n";
$mx_plain_count = array_count_values( $mx_plain );
arsort( $mx_plain_count );
echo "\n";
$mx_tls_count = array_count_values( $mx_tls );
arsort( $mx_tls_count );
underline( "Top $max_mx encrypted MX hosts:" );
$i = 0;
foreach ( $mx_tls_count as $mx_tls_host=>$mx_tls_val ) {
echo "$mx_tls_val\t$mx_tls_host\n";
$i++;
if ( $i == $max_mx ) break;
}
echo "\n";
underline( "Top $max_mx unencrypted MX hosts:" );
$i = 0;
foreach ( $mx_plain_count as $mx_plain_host=>$mx_plain_val ) {
echo "$mx_plain_val\t$mx_plain_host\n";
$i++;
if ( $i == $max_mx ) break;
}
echo "\n";
function underline( $string ) {
$len = strlen( $string );
echo "$string\n";
echo str_repeat( '-', $len ) . "\n";
return;
}