@ -4,17 +4,23 @@ package Anvil::Tools::Email;
# mailx.
#
### TODO: By default, a recipient receives all alerts at their default level. Later, we'll add an
### override table to allow a user to ignore a given striker or Anvil! node / dr host set. So
### creating this list is no longer needed.
use strict ;
use warnings ;
use Scalar::Util qw( weaken isweak ) ;
use Data::Dumper ;
use Scalar::Util qw( weaken isweak ) ;
use Text::Diff ;
our $ VERSION = "3.0.0" ;
my $ THIS_FILE = "Email.pm" ;
### Methods;
# check_alert_recipients
# check_postfix
# check_queue
# check_config
#
= pod
@ -76,103 +82,103 @@ sub parent
#############################################################################################################
= head2 check_alert_recipients
= head2 check_config
This loops through all known hosts and all known C << recipients >> and any C << hosts >> that don 't have a corresponding entry in C<< notifications >>. When found, an entry is created using the recipient' s new level .
This method checks the current postfix server configuration to see if it needs to be updated , then checks to see if the local C << postfix >> daemin is enabled and started .
If any problem is encountered , C << 1 >> is returned . Otherwise , if all is well , C << 0 >> is returned .
This method takes no parameters .
= cut
sub check_alert_recipients
sub check_config
{
my $ self = shift ;
my $ parameter = shift ;
my $ anvil = $ self - > parent ;
my $ debug = defined $ parameter - > { debug } ? $ parameter - > { debug } : 3 ;
$ anvil - > Log - > entry ( { source = > $ THIS_FILE , line = > __LINE__ , level = > $ debug , key = > "log_0125" , variables = > { method = > "Email->check_config()" } } ) ;
# Get a list of all recipients.
$ anvil - > Database - > get_recipients ( { debug = > 2 } ) ;
my $ problem = 0 ;
# Get a list of hosts.
$ anvil - > Database - > get_hosts ( { debug = > 2 } ) ;
# We check to see if there are any emails in the queue. If we see queued emails for more than five
# minutes, and a second mail server is configured, we'll automatically reconfigure for the next
# known server.
my ( $ oldest_message ) = $ anvil - > Email - > check_queue ( { debug = > $ debug } ) ;
if ( $ oldest_message > 600 )
{
$ anvil - > Email - > swap_server ( { debug = > $ debug } ) ;
}
# Get the notification list
$ anvil - > Database - > get_notifications ( { debug = > 2 } ) ;
# If not configured look in variables for 'mail_server::last_used::<mail_server_uuid>'. The first one
# that doesn't have an existing variable will be used. If all known mail servers have variables, the
# oldest is used.
#
# In any case where the mail server is configured, the server that is used has their
# 'mail_server::last_used::<mail_server_uuid>' variable set to the current time stamp.
# Now loop!
foreach my $ host_uuid ( keys % { $ anvil - > data - > { hosts } { host_uuid } } )
# Is the postfix daemon running?
my $ reconfigure = 1 ;
if ( $ reconfigure )
{
my $ host_name = $ anvil - > data - > { hosts } { host_uuid } { $ host_uuid } { host_name } ;
# Get the list of mail servers.
$ anvil - > Database - > get_mail_servers ( { debug = > $ debug } ) ;
# Loop through recipients.
foreach my $ recipient_uuid ( keys % { $ anvil - > data - > { recipients } { recipient_uuid } } )
### TODO: This just bootstraps the initial config
my $ used_mail_server_count = exists $ anvil - > data - > { mail_servers } { use_order } ? keys % { $ anvil - > data - > { mail_servers } { use_order } } : 0 ;
if ( not $ used_mail_server_count )
{
my $ recipient_new_level = $ anvil - > data - > { recipients } { recipient_uuid } { $ recipient_uuid } { recipient_new_level } ;
# Now see if there's already an entry in notifications.
my $ exists = 0 ;
foreach my $ notification_uuid ( keys % { $ anvil - > data - > { notifications } { notification_uuid } } )
# Just pick the first one.
foreach my $ mail_server_uuid ( keys % { $ anvil - > data - > { mail_servers } { mail_server } } )
{
my $ notification_recipient_uuid = $ anvil - > data - > { notifications } { notification_uuid } { $ notification_uuid } { notification_recipient_uuid } ;
my $ notification_host_uuid = $ anvil - > data - > { notifications } { notification_uuid } { $ notification_uuid } { notification_host_uuid } ;
if ( ( $ host_uuid eq $ notification_host_uuid ) && ( $ recipient_uuid eq $ notification_recipient_uuid ) )
{
$ exists = 1 ;
last ;
}
}
# Did we find an entry?
if ( not $ exists )
{
# Nope, save it.
my ( $ notification_uuid ) = $ anvil - > Database - > insert_or_update_notifications ( {
debug = > 2 ,
notification_recipient_uuid = > $ recipient_uuid ,
notification_host_uuid = > $ host_uuid ,
notification_alert_level = > $ recipient_new_level ,
$ anvil - > Email - > _configure_for_server ( {
debug = > $ debug ,
mail_server_uuid = > $ mail_server_uuid ,
} ) ;
last ;
}
}
}
return ( 0 ) ;
return ( $ problem ) ;
}
= head2 check_postfix
This method checks the current postfix server configuration to see if it needs to be updated , then checks to see if the local C << postfix >> daemin is enabled and started .
If any problem is encountered , C << 1 >> is returned . Otherwise , if all is well , C << 0 >> is returned .
Parameters ;
= head3 config ( optional , default '1' )
If set to C << 0 >> , the configuration is not checked or updated .
= head2 check_queue
= head3 daemon ( optional , default '1' )
This method looks to see how many email messages are in the send queue and how long they ' ve been there . The age of the older queued message is returned ( in seconds ) .
If set to C << 0 >> , the C << postfix >> daemon is not checked or started .
This method takes no parameters .
= cut
sub check_postfix
sub check_queue
{
my $ self = shift ;
my $ parameter = shift ;
my $ anvil = $ self - > parent ;
my $ debug = defined $ parameter - > { debug } ? $ parameter - > { debug } : 3 ;
$ anvil - > Log - > entry ( { source = > $ THIS_FILE , line = > __LINE__ , level = > $ debug , key = > "log_0125" , variables = > { method = > "Email->check_queue()" } } ) ;
my $ problem = 0 ;
my $ config = defined $ parameter - > { config } ? $ parameter - > { config } : 1 ;
my $ daemon = defined $ parameter - > { daemon } ? $ parameter - > { daemon } : 1 ;
$ anvil - > Log - > variables ( { source = > $ THIS_FILE , line = > __LINE__ , level = > $ debug , list = > {
config = > $ config ,
daemon = > $ daemon ,
} } ) ;
my $ oldest_message = 0 ;
### TODO
# If configured/running, the number of messages in queue is checked. If '0',
# 'mail_server::queue_empty' is updated with the current time. If 1 or more, the time since the queue
# was last 0 is checked. If > 300, the mail server is reconfigured to use the mail server with the
# oldest 'mail_server::last_used::<mail_server_uuid>' time.
return ( $ problem ) ;
return ( $ oldest_message ) ;
}
= head2 swap_server
= cut
sub swap_server
{
}
# =head3
@ -184,3 +190,557 @@ sub check_postfix
#############################################################################################################
# Private functions #
#############################################################################################################
# This does the actual work of configuring postfix for a give mail server. Returns '1' if reconfigured,
# returns '0' if not.
sub _configure_for_server
{
my $ self = shift ;
my $ parameter = shift ;
my $ anvil = $ self - > parent ;
my $ debug = defined $ parameter - > { debug } ? $ parameter - > { debug } : 3 ;
$ anvil - > Log - > entry ( { source = > $ THIS_FILE , line = > __LINE__ , level = > $ debug , key = > "log_0125" , variables = > { method = > "Email->_configure_for_server()" } } ) ;
my $ reload = 0 ;
my $ mail_server_uuid = defined $ parameter - > { mail_server_uuid } ? $ parameter - > { mail_server_uuid } : "" ;
$ anvil - > Log - > variables ( { source = > $ THIS_FILE , line = > __LINE__ , level = > $ debug , list = > {
mail_server_uuid = > $ mail_server_uuid ,
} } ) ;
if ( not $ mail_server_uuid )
{
$ anvil - > Log - > entry ( { source = > $ THIS_FILE , line = > __LINE__ , level = > 0 , priority = > "err" , key = > "log_0020" , variables = > { method = > "Email->register()" , parameter = > "_configure_for_server" } } ) ;
return ( $ reload ) ;
}
if ( not exists $ anvil - > data - > { mail_servers } { mail_server } { $ mail_server_uuid } )
{
# Try loading the mail server data.
$ anvil - > Database - > get_mail_servers ( { debug = > $ debug } ) ;
if ( not exists $ anvil - > data - > { mail_servers } { mail_server } { $ mail_server_uuid } )
{
# Invalid UUID / mail server
$ anvil - > Log - > entry ( { source = > $ THIS_FILE , line = > __LINE__ , level = > 0 , priority = > "err" , key = > "error_0141" , variables = > { uuid = > $ mail_server_uuid } } ) ;
return ( $ reload ) ;
}
}
### Check / update / create relay_password
my $ mail_server_address = $ anvil - > data - > { mail_servers } { mail_server } { $ mail_server_uuid } { mail_server_address } ;
my $ mail_server_port = $ anvil - > data - > { mail_servers } { mail_server } { $ mail_server_uuid } { mail_server_port } ;
my $ mail_server_username = $ anvil - > data - > { mail_servers } { mail_server } { $ mail_server_uuid } { mail_server_username } ;
my $ mail_server_password = $ anvil - > data - > { mail_servers } { mail_server } { $ mail_server_uuid } { mail_server_password } ;
my $ mail_server_security = $ anvil - > data - > { mail_servers } { mail_server } { $ mail_server_uuid } { mail_server_security } ;
my $ mail_server_authentication = $ anvil - > data - > { mail_servers } { mail_server } { $ mail_server_uuid } { mail_server_authentication } ;
my $ mail_server_helo_domain = $ anvil - > data - > { mail_servers } { mail_server } { $ mail_server_uuid } { mail_server_helo_domain } ;
$ anvil - > Log - > variables ( { source = > $ THIS_FILE , line = > __LINE__ , level = > $ debug , list = > {
mail_server_address = > $ mail_server_address ,
mail_server_port = > $ mail_server_port ,
mail_server_username = > $ mail_server_username ,
mail_server_password = > $ mail_server_password ,
mail_server_security = > $ mail_server_security ,
mail_server_authentication = > $ mail_server_authentication ,
mail_server_helo_domain = > $ mail_server_helo_domain ,
} } ) ;
my $ old_postfix_relay_file = "" ;
if ( - e $ anvil - > data - > { path } { configs } { postfix_relay_password } )
{
$ old_postfix_relay_file = $ anvil - > Storage - > read_file ( {
debug = > $ debug ,
file = > $ anvil - > data - > { path } { configs } { postfix_relay_password } ,
} ) ;
$ anvil - > Log - > variables ( { source = > $ THIS_FILE , line = > __LINE__ , level = > $ debug , secure = > 1 , list = > {
old_postfix_relay_file = > $ old_postfix_relay_file ,
} } ) ;
}
my $ new_postfix_relay_file = "[" . $ mail_server_address . "]:" . $ mail_server_port . " " . $ mail_server_username . ":" . $ mail_server_password . "\n" ;
$ anvil - > Log - > variables ( { source = > $ THIS_FILE , line = > __LINE__ , level = > $ debug , secure = > 1 , list = > {
new_postfix_relay_file = > $ new_postfix_relay_file ,
} } ) ;
if ( $ new_postfix_relay_file ne $ old_postfix_relay_file )
{
# Create the new relay file.
$ anvil - > Log - > entry ( { source = > $ THIS_FILE , line = > __LINE__ , level = > 1 , key = > "log_0530" } ) ;
$ reload = 1 ;
my $ error = $ anvil - > Storage - > write_file ( {
backup = > 0 ,
debug = > $ debug ,
body = > $ new_postfix_relay_file ,
file = > $ anvil - > data - > { path } { configs } { postfix_relay_password } ,
mode = > "0644" ,
user = > "root" ,
group = > "root" ,
overwrite = > 1 ,
secure = > 1 ,
} ) ;
$ anvil - > Log - > variables ( { source = > $ THIS_FILE , line = > __LINE__ , level = > $ debug , list = > {
reload = > $ reload ,
error = > $ error ,
} } ) ;
# Generate the binary version.
$ anvil - > Log - > entry ( { source = > $ THIS_FILE , line = > __LINE__ , level = > 1 , key = > "log_0531" } ) ;
my ( $ output , $ return_code ) = $ anvil - > System - > call ( { debug = > $ debug , shell_call = > $ anvil - > data - > { path } { exe } { postmap } . " " . $ anvil - > data - > { path } { configs } { postfix_relay_password } } ) ;
$ anvil - > Log - > variables ( { source = > $ THIS_FILE , line = > __LINE__ , level = > $ debug , list = > {
output = > $ output ,
return_code = > $ return_code ,
} } ) ;
}
### Check / update main.cf
my $ new_postfix_main = "" ;
my $ old_postfix_main = $ anvil - > Storage - > read_file ( {
debug = > $ debug ,
file = > $ anvil - > data - > { path } { configs } { postfix_main } ,
} ) ;
$ anvil - > Log - > variables ( { source = > $ THIS_FILE , line = > __LINE__ , level = > $ debug , list = > {
old_postfix_relay_file = > $ old_postfix_relay_file ,
} } ) ;
my $ last_line = "" ;
my $ relayhost_seen = 0 ;
my $ relayhost_line = "relayhost = [" . $ mail_server_address . "]:" . $ mail_server_port ;
my $ smtp_helo_name_seen = 0 ;
my $ smtp_helo_name_line = "smtp_helo_name = " . $ anvil - > _domain_name ( ) ;
my $ smtp_use_tls_seen = 0 ;
my $ smtp_use_tls_line = "smtp_use_tls = yes" ;
my $ smtp_sasl_auth_enable_seen = 0 ;
my $ smtp_sasl_auth_enable_line = "smtp_sasl_auth_enable = yes" ;
my $ smtp_sasl_password_maps_seen = 0 ;
my $ smtp_sasl_password_maps_line = "smtp_sasl_password_maps = hash:" . $ anvil - > data - > { path } { configs } { postfix_relay_password } ;
my $ smtp_sasl_security_options_seen = 0 ;
my $ smtp_sasl_security_options_line = "smtp_sasl_security_options =" ;
my $ smtp_tls_CAfile_seen = 0 ;
my $ smtp_tls_CAfile_line = "smtp_tls_CAfile = /etc/pki/tls/certs/ca-bundle.crt" ;
$ anvil - > Log - > variables ( { source = > $ THIS_FILE , line = > __LINE__ , level = > $ debug , list = > {
relayhost_line = > $ relayhost_line ,
smtp_helo_name_line = > $ smtp_helo_name_line ,
smtp_use_tls_line = > $ smtp_use_tls_line ,
smtp_sasl_auth_enable_line = > $ smtp_sasl_auth_enable_line ,
smtp_sasl_password_maps_line = > $ smtp_sasl_password_maps_line ,
smtp_sasl_security_options_line = > $ smtp_sasl_security_options_line ,
smtp_tls_CAfile_line = > $ smtp_tls_CAfile_line ,
} } ) ;
# Before we start, we'll see if our variables have been seen. If not, we'll inject the below the
# 'relay_host' section.
foreach my $ line ( split /\n/ , $ old_postfix_main )
{
$ anvil - > Log - > variables ( { source = > $ THIS_FILE , line = > __LINE__ , level = > $ debug , list = > { line = > $ line } } ) ;
if ( $ line =~ /^relayhost = / )
{
$ relayhost_seen = 1 ;
$ anvil - > Log - > variables ( { source = > $ THIS_FILE , line = > __LINE__ , level = > $ debug , list = > { relayhost_seen = > $ relayhost_seen } } ) ;
}
if ( $ line =~ /^smtp_helo_name =/ )
{
$ smtp_helo_name_seen = 1 ;
$ anvil - > Log - > variables ( { source = > $ THIS_FILE , line = > __LINE__ , level = > $ debug , list = > { smtp_helo_name_seen = > $ smtp_helo_name_seen } } ) ;
}
if ( $ line =~ /^smtp_use_tls =/ )
{
$ smtp_use_tls_seen = 1 ;
$ anvil - > Log - > variables ( { source = > $ THIS_FILE , line = > __LINE__ , level = > $ debug , list = > { smtp_use_tls_seen = > $ smtp_use_tls_seen } } ) ;
}
if ( $ line =~ /^smtp_sasl_auth_enable =/ )
{
$ smtp_sasl_auth_enable_seen = 1 ;
$ anvil - > Log - > variables ( { source = > $ THIS_FILE , line = > __LINE__ , level = > $ debug , list = > { smtp_sasl_auth_enable_seen = > $ smtp_sasl_auth_enable_seen } } ) ;
}
if ( $ line =~ /^smtp_sasl_password_maps =/ )
{
$ smtp_sasl_password_maps_seen = 1 ;
$ anvil - > Log - > variables ( { source = > $ THIS_FILE , line = > __LINE__ , level = > $ debug , list = > { smtp_sasl_password_maps_seen = > $ smtp_sasl_password_maps_seen } } ) ;
}
if ( $ line =~ /^smtp_sasl_security_options =/ )
{
$ smtp_sasl_security_options_seen = 1 ;
$ anvil - > Log - > variables ( { source = > $ THIS_FILE , line = > __LINE__ , level = > $ debug , list = > { smtp_sasl_security_options_seen = > $ smtp_sasl_security_options_seen } } ) ;
}
if ( $ line =~ /^smtp_tls_CAfile =/ )
{
$ smtp_tls_CAfile_seen = 1 ;
$ anvil - > Log - > variables ( { source = > $ THIS_FILE , line = > __LINE__ , level = > $ debug , list = > { smtp_tls_CAfile_seen = > $ smtp_tls_CAfile_seen } } ) ;
}
}
foreach my $ line ( split /\n/ , $ old_postfix_main )
{
$ anvil - > Log - > variables ( { source = > $ THIS_FILE , line = > __LINE__ , level = > $ debug , list = > { line = > $ line } } ) ;
if ( $ line =~ /^relayhost = / )
{
$ relayhost_seen = 1 ;
$ anvil - > Log - > variables ( { source = > $ THIS_FILE , line = > __LINE__ , level = > $ debug , list = > { relayhost_seen = > $ relayhost_seen } } ) ;
if ( $ line ne $ relayhost_line )
{
# Rewrite the line.
$ line = $ relayhost_line ;
$ reload = 1 ;
$ anvil - > Log - > variables ( { source = > $ THIS_FILE , line = > __LINE__ , level = > $ debug , list = > {
's1:line' = > $ line ,
's2:reload' = > $ reload ,
} } ) ;
}
}
if ( ( $ last_line eq "#relayhost = [an.ip.add.ress]" ) && ( $ line eq "" ) && ( not $ relayhost_seen ) )
{
# Never configured before, inject our line.
$ anvil - > Log - > entry ( { source = > $ THIS_FILE , line = > __LINE__ , level = > 1 , key = > "log_0532" } ) ;
$ new_postfix_main . = $ relayhost_line . "\n" ;
$ relayhost_seen = 1 ;
$ reload = 1 ;
$ anvil - > Log - > variables ( { source = > $ THIS_FILE , line = > __LINE__ , level = > $ debug , list = > {
's1:relayhost_line' = > $ relayhost_line ,
's2:relayhost_seen' = > $ relayhost_seen ,
's3:reload' = > $ reload ,
} } ) ;
# Inject any other variables we've not seen yet.
if ( not $ smtp_helo_name_seen )
{
$ anvil - > Log - > entry ( { source = > $ THIS_FILE , line = > __LINE__ , level = > 1 , key = > "log_0533" , variables = > { line = > $ smtp_helo_name_line } } ) ;
$ reload = 1 ;
$ smtp_helo_name_seen = 1 ;
$ new_postfix_main . = $ smtp_helo_name_line . "\n" ;
$ anvil - > Log - > variables ( { source = > $ THIS_FILE , line = > __LINE__ , level = > $ debug , list = > {
's1:smtp_helo_name_line' = > $ smtp_helo_name_line ,
's2:smtp_helo_name_seen' = > $ smtp_helo_name_seen ,
's3:reload' = > $ reload ,
} } ) ;
}
if ( not $ smtp_use_tls_seen )
{
$ anvil - > Log - > entry ( { source = > $ THIS_FILE , line = > __LINE__ , level = > 1 , key = > "log_0533" , variables = > { line = > $ smtp_use_tls_line } } ) ;
$ reload = 1 ;
$ smtp_use_tls_seen = 1 ;
$ new_postfix_main . = $ smtp_use_tls_line . "\n" ;
$ anvil - > Log - > variables ( { source = > $ THIS_FILE , line = > __LINE__ , level = > $ debug , list = > {
's1:smtp_use_tls_line' = > $ smtp_use_tls_line ,
's2:smtp_use_tls_seen' = > $ smtp_use_tls_seen ,
's3:reload' = > $ reload ,
} } ) ;
}
if ( not $ smtp_sasl_auth_enable_seen )
{
$ anvil - > Log - > entry ( { source = > $ THIS_FILE , line = > __LINE__ , level = > 1 , key = > "log_0533" , variables = > { line = > $ smtp_sasl_auth_enable_line } } ) ;
$ reload = 1 ;
$ smtp_sasl_auth_enable_seen = 1 ;
$ new_postfix_main . = $ smtp_sasl_auth_enable_line . "\n" ;
$ anvil - > Log - > variables ( { source = > $ THIS_FILE , line = > __LINE__ , level = > $ debug , list = > {
's1:smtp_sasl_auth_enable_line' = > $ smtp_sasl_auth_enable_line ,
's2:smtp_sasl_auth_enable_seen' = > $ smtp_sasl_auth_enable_seen ,
's3:reload' = > $ reload ,
} } ) ;
}
if ( not $ smtp_sasl_password_maps_seen )
{
$ anvil - > Log - > entry ( { source = > $ THIS_FILE , line = > __LINE__ , level = > 1 , key = > "log_0533" , variables = > { line = > $ smtp_sasl_password_maps_line } } ) ;
$ reload = 1 ;
$ smtp_sasl_password_maps_seen = 1 ;
$ new_postfix_main . = $ smtp_sasl_password_maps_line . "\n" ;
$ anvil - > Log - > variables ( { source = > $ THIS_FILE , line = > __LINE__ , level = > $ debug , list = > {
's1:smtp_sasl_password_maps_line' = > $ smtp_sasl_password_maps_line ,
's2:smtp_sasl_password_maps_seen' = > $ smtp_sasl_password_maps_seen ,
's3:reload' = > $ reload ,
} } ) ;
}
if ( not $ smtp_sasl_security_options_seen )
{
$ anvil - > Log - > entry ( { source = > $ THIS_FILE , line = > __LINE__ , level = > 1 , key = > "log_0533" , variables = > { line = > $ smtp_sasl_security_options_line } } ) ;
$ reload = 1 ;
$ smtp_sasl_security_options_seen = 1 ;
$ new_postfix_main . = $ smtp_sasl_security_options_line . "\n" ;
$ anvil - > Log - > variables ( { source = > $ THIS_FILE , line = > __LINE__ , level = > $ debug , list = > {
's1:smtp_sasl_security_options_line' = > $ smtp_sasl_security_options_line ,
's2:smtp_sasl_security_options_seen' = > $ smtp_sasl_security_options_seen ,
's3:reload' = > $ reload ,
} } ) ;
}
if ( not $ smtp_tls_CAfile_seen )
{
$ anvil - > Log - > entry ( { source = > $ THIS_FILE , line = > __LINE__ , level = > 1 , key = > "log_0533" , variables = > { line = > $ smtp_tls_CAfile_line } } ) ;
$ reload = 1 ;
$ smtp_tls_CAfile_seen = 1 ;
$ new_postfix_main . = $ smtp_tls_CAfile_line . "\n" ;
$ anvil - > Log - > variables ( { source = > $ THIS_FILE , line = > __LINE__ , level = > $ debug , list = > {
's1:smtp_tls_CAfile_line' = > $ smtp_tls_CAfile_line ,
's2:smtp_tls_CAfile_seen' = > $ smtp_tls_CAfile_seen ,
's3:reload' = > $ reload ,
} } ) ;
}
}
# Any other existing config lines
if ( $ line =~ /^smtp_helo_name =/ )
{
if ( $ line ne $ smtp_helo_name_line )
{
$ anvil - > Log - > entry ( { source = > $ THIS_FILE , line = > __LINE__ , level = > 1 , key = > "log_0534" , variables = > {
old_line = > $ line ,
new_line = > $ smtp_tls_CAfile_line ,
} } ) ;
$ line = $ smtp_helo_name_line ;
$ smtp_helo_name_seen = 1 ;
$ reload = 1 ;
$ anvil - > Log - > variables ( { source = > $ THIS_FILE , line = > __LINE__ , level = > $ debug , list = > {
's1:line' = > $ line ,
's2:smtp_helo_name_seen' = > $ smtp_helo_name_seen ,
's2:reload' = > $ reload ,
} } ) ;
}
}
if ( $ line =~ /^smtp_use_tls =/ )
{
if ( $ line ne $ smtp_use_tls_line )
{
$ anvil - > Log - > entry ( { source = > $ THIS_FILE , line = > __LINE__ , level = > 1 , key = > "log_0534" , variables = > {
old_line = > $ line ,
new_line = > $ smtp_use_tls_line ,
} } ) ;
$ line = $ smtp_use_tls_line ;
$ smtp_use_tls_seen = 1 ;
$ reload = 1 ;
$ anvil - > Log - > variables ( { source = > $ THIS_FILE , line = > __LINE__ , level = > $ debug , list = > {
's1:line' = > $ line ,
's2:smtp_use_tls_seen' = > $ smtp_use_tls_seen ,
's2:reload' = > $ reload ,
} } ) ;
}
}
if ( $ line =~ /^smtp_sasl_auth_enable =/ )
{
if ( $ line ne $ smtp_sasl_auth_enable_line )
{
$ anvil - > Log - > entry ( { source = > $ THIS_FILE , line = > __LINE__ , level = > 1 , key = > "log_0534" , variables = > {
old_line = > $ line ,
new_line = > $ smtp_sasl_auth_enable_line ,
} } ) ;
$ line = $ smtp_sasl_auth_enable_line ;
$ smtp_sasl_auth_enable_seen = 1 ;
$ reload = 1 ;
$ anvil - > Log - > variables ( { source = > $ THIS_FILE , line = > __LINE__ , level = > $ debug , list = > {
's1:line' = > $ line ,
's2:smtp_sasl_auth_enable_seen' = > $ smtp_sasl_auth_enable_seen ,
's2:reload' = > $ reload ,
} } ) ;
}
}
if ( $ line =~ /^smtp_sasl_password_maps =/ )
{
if ( $ line ne $ smtp_sasl_password_maps_line )
{
$ anvil - > Log - > entry ( { source = > $ THIS_FILE , line = > __LINE__ , level = > 1 , key = > "log_0534" , variables = > {
old_line = > $ line ,
new_line = > $ smtp_sasl_password_maps_line ,
} } ) ;
$ line = $ smtp_sasl_password_maps_line ;
$ smtp_sasl_password_maps_seen = 1 ;
$ reload = 1 ;
$ anvil - > Log - > variables ( { source = > $ THIS_FILE , line = > __LINE__ , level = > $ debug , list = > {
's1:line' = > $ line ,
's2:smtp_sasl_password_maps_seen' = > $ smtp_sasl_password_maps_seen ,
's2:reload' = > $ reload ,
} } ) ;
}
}
if ( $ line =~ /^smtp_sasl_security_options =/ )
{
if ( $ line ne $ smtp_sasl_security_options_line )
{
$ anvil - > Log - > entry ( { source = > $ THIS_FILE , line = > __LINE__ , level = > 1 , key = > "log_0534" , variables = > {
old_line = > $ line ,
new_line = > $ smtp_sasl_security_options_line ,
} } ) ;
$ line = $ smtp_sasl_security_options_line ;
$ smtp_sasl_security_options_seen = 1 ;
$ reload = 1 ;
$ anvil - > Log - > variables ( { source = > $ THIS_FILE , line = > __LINE__ , level = > $ debug , list = > {
's1:line' = > $ line ,
's2:smtp_sasl_security_options_seen' = > $ smtp_sasl_security_options_seen ,
's2:reload' = > $ reload ,
} } ) ;
}
}
if ( $ line =~ /^smtp_tls_CAfile =/ )
{
if ( $ line ne $ smtp_tls_CAfile_line )
{
$ anvil - > Log - > entry ( { source = > $ THIS_FILE , line = > __LINE__ , level = > 1 , key = > "log_0534" , variables = > {
old_line = > $ line ,
new_line = > $ smtp_tls_CAfile_line ,
} } ) ;
$ line = $ smtp_tls_CAfile_line ;
$ smtp_tls_CAfile_seen = 1 ;
$ reload = 1 ;
$ anvil - > Log - > variables ( { source = > $ THIS_FILE , line = > __LINE__ , level = > $ debug , list = > {
's1:line' = > $ line ,
's2:smtp_tls_CAfile_seen' = > $ smtp_tls_CAfile_seen ,
's2:reload' = > $ reload ,
} } ) ;
}
}
$ new_postfix_main . = $ line . "\n" ;
$ last_line = $ line ;
}
if ( not $ relayhost_seen )
{
# We apparently missed our injection point, append it to the end of the file.
$ anvil - > Log - > entry ( { source = > $ THIS_FILE , line = > __LINE__ , level = > 1 , key = > "log_0533" , variables = > { line = > $ relayhost_line } } ) ;
$ new_postfix_main . = $ relayhost_line . "\n" ;
$ reload = 1 ;
$ anvil - > Log - > variables ( { source = > $ THIS_FILE , line = > __LINE__ , level = > $ debug , list = > {
's1:relayhost_line' = > $ relayhost_line ,
's2:reload' = > $ reload ,
} } ) ;
}
if ( not $ smtp_helo_name_seen )
{
$ anvil - > Log - > entry ( { source = > $ THIS_FILE , line = > __LINE__ , level = > 1 , key = > "log_0533" , variables = > { line = > $ smtp_helo_name_line } } ) ;
$ reload = 1 ;
$ new_postfix_main . = $ smtp_helo_name_line . "\n" ;
$ anvil - > Log - > variables ( { source = > $ THIS_FILE , line = > __LINE__ , level = > $ debug , list = > {
's1:smtp_helo_name_line' = > $ smtp_helo_name_line ,
's2:reload' = > $ reload ,
} } ) ;
}
if ( not $ smtp_use_tls_seen )
{
$ anvil - > Log - > entry ( { source = > $ THIS_FILE , line = > __LINE__ , level = > 1 , key = > "log_0533" , variables = > { line = > $ smtp_use_tls_line } } ) ;
$ reload = 1 ;
$ new_postfix_main . = $ smtp_use_tls_line . "\n" ;
$ anvil - > Log - > variables ( { source = > $ THIS_FILE , line = > __LINE__ , level = > $ debug , list = > {
's1:smtp_use_tls_line' = > $ smtp_use_tls_line ,
's2:reload' = > $ reload ,
} } ) ;
}
if ( not $ smtp_sasl_auth_enable_seen )
{
$ anvil - > Log - > entry ( { source = > $ THIS_FILE , line = > __LINE__ , level = > 1 , key = > "log_0533" , variables = > { line = > $ smtp_sasl_auth_enable_line } } ) ;
$ reload = 1 ;
$ new_postfix_main . = $ smtp_sasl_auth_enable_line . "\n" ;
$ anvil - > Log - > variables ( { source = > $ THIS_FILE , line = > __LINE__ , level = > $ debug , list = > {
's1:smtp_sasl_auth_enable_line' = > $ smtp_sasl_auth_enable_line ,
's2:reload' = > $ reload ,
} } ) ;
}
if ( not $ smtp_sasl_password_maps_seen )
{
$ anvil - > Log - > entry ( { source = > $ THIS_FILE , line = > __LINE__ , level = > 1 , key = > "log_0533" , variables = > { line = > $ smtp_sasl_password_maps_line } } ) ;
$ reload = 1 ;
$ new_postfix_main . = $ smtp_sasl_password_maps_line . "\n" ;
$ anvil - > Log - > variables ( { source = > $ THIS_FILE , line = > __LINE__ , level = > $ debug , list = > {
's1:smtp_sasl_password_maps_line' = > $ smtp_sasl_password_maps_line ,
's2:reload' = > $ reload ,
} } ) ;
}
if ( not $ smtp_sasl_security_options_seen )
{
$ anvil - > Log - > entry ( { source = > $ THIS_FILE , line = > __LINE__ , level = > 1 , key = > "log_0533" , variables = > { line = > $ smtp_sasl_security_options_line } } ) ;
$ reload = 1 ;
$ new_postfix_main . = $ smtp_sasl_security_options_line . "\n" ;
$ anvil - > Log - > variables ( { source = > $ THIS_FILE , line = > __LINE__ , level = > $ debug , list = > {
's1:smtp_sasl_security_options_line' = > $ smtp_sasl_security_options_line ,
's2:reload' = > $ reload ,
} } ) ;
}
if ( not $ smtp_tls_CAfile_seen )
{
$ anvil - > Log - > entry ( { source = > $ THIS_FILE , line = > __LINE__ , level = > 1 , key = > "log_0533" , variables = > { line = > $ smtp_tls_CAfile_line } } ) ;
$ reload = 1 ;
$ new_postfix_main . = $ smtp_tls_CAfile_line . "\n" ;
$ anvil - > Log - > variables ( { source = > $ THIS_FILE , line = > __LINE__ , level = > $ debug , list = > {
's1:smtp_tls_CAfile_line' = > $ smtp_tls_CAfile_line ,
's2:reload' = > $ reload ,
} } ) ;
}
# Write out the file, if needed.
if ( $ old_postfix_main ne $ new_postfix_main )
{
$ anvil - > Log - > entry ( { source = > $ THIS_FILE , line = > __LINE__ , level = > 1 , key = > "log_0517" , variables = > {
file = > $ anvil - > data - > { path } { configs } { postfix_main } ,
diff = > diff \ $ old_postfix_main , \ $ new_postfix_main , { STYLE = > 'Unified' } ,
} } ) ;
# Create the new relay file.
$ reload = 1 ;
my $ error = $ anvil - > Storage - > write_file ( {
debug = > $ debug ,
backup = > 1 ,
body = > $ new_postfix_main ,
file = > $ anvil - > data - > { path } { configs } { postfix_main } ,
mode = > "0644" ,
user = > "root" ,
group = > "root" ,
overwrite = > 1 ,
secure = > 1 ,
} ) ;
$ anvil - > Log - > variables ( { source = > $ THIS_FILE , line = > __LINE__ , level = > $ debug , list = > {
reload = > $ reload ,
error = > $ error ,
} } ) ;
}
# Make sure the postfix daemon is running and enabled.
my $ postfix_started = 0 ;
# Is the postfix daemon running?
my $ postfix_running = $ anvil - > System - > check_daemon ( { daemon = > "postfix.service" } ) ;
if ( not $ postfix_running )
{
# Start it.
$ anvil - > Log - > entry ( { source = > $ THIS_FILE , line = > __LINE__ , level = > 1 , key = > "log_0535" , variables = > { daemon = > "postfix.service" } } ) ;
my $ start_return_code = $ anvil - > System - > start_daemon ( { daemon = > "postfix.service" } ) ;
my $ enable_return_code = $ anvil - > System - > enable_daemon ( { daemon = > "postfix.service" } ) ;
$ postfix_started = 1 ;
$ anvil - > Log - > variables ( { source = > $ THIS_FILE , line = > __LINE__ , level = > $ debug , list = > {
start_return_code = > $ start_return_code ,
enable_return_code = > $ enable_return_code ,
postfix_started = > $ postfix_started ,
} } ) ;
}
if ( $ reload )
{
# Record that we've switched to this mail server.
my $ variable_uuid = $ anvil - > Database - > insert_or_update_variables ( {
variable_name = > "mail_server::last_used::${mail_server_uuid}" ,
variable_value = > time ,
variable_source_table = > "hosts" ,
variable_source_uuid = > $ anvil - > Get - > host_uuid ,
variable_section = > "email::servers" ,
variable_description = > "striker_0276" ,
} ) ;
$ anvil - > Log - > variables ( { source = > $ THIS_FILE , line = > __LINE__ , level = > $ debug , list = > { variable_uuid = > $ variable_uuid } } ) ;
# Start the daemon
if ( not $ postfix_started )
{
$ anvil - > Log - > entry ( { source = > $ THIS_FILE , line = > __LINE__ , level = > 1 , key = > "job_0091" , variables = > { daemon = > "postfix.service" } } ) ;
my $ restart_return_code = $ anvil - > System - > restart_daemon ( { daemon = > "postfix.service" } ) ;
$ anvil - > Log - > variables ( { source = > $ THIS_FILE , line = > __LINE__ , level = > $ debug , list = > { restart_return_code = > $ restart_return_code } } ) ;
}
}
# Lastly, make sure the alert email directory exists.
if ( not - d $ anvil - > data - > { path } { directories } { alert_emails } )
{
$ anvil - > Log - > entry ( { source = > $ THIS_FILE , line = > __LINE__ , level = > 1 , key = > "log_0536" } ) ;
my $ failed = $ anvil - > Storage - > make_directory ( {
debug = > $ debug ,
directory = > $ anvil - > data - > { path } { directories } { alert_emails } ,
mode = > "0775" ,
user = > "root" ,
group = > "root" ,
} ) ;
$ anvil - > Log - > variables ( { source = > $ THIS_FILE , line = > __LINE__ , level = > $ debug , list = > { failed = > $ failed } } ) ;
}
$ anvil - > Log - > variables ( { source = > $ THIS_FILE , line = > __LINE__ , level = > $ debug , list = > { reload = > $ reload } } ) ;
return ( $ reload ) ;
}