File: //sbin/sendmail
#!/usr/bin/env perl
#
# sendmail wrapper to support multiple sendmail solutions in customer
# context (chroots) on infongs.
#
use strict;
use warnings;
use 5.010;
use Config::Tiny;
my $DEBUG = $ENV{UI_DEBUG} // 0;
use constant {
CONFIGFILE => '/etc/ui-sendmail-wrapper.conf',
EXITCODE => 127,
};
sub error {
my $errmsg = shift;
print STDERR "Error: $errmsg\n";
exit EXITCODE;
}
sub debug {
my $msg = shift;
print STDERR "[DEBUG] $msg\n" if $DEBUG;
}
sub call {
my @args = @_;
my $cmd = shift @args;
debug("Executing command: $cmd " . join(' ', @args));
no warnings;
exec { $cmd } "/usr/sbin/sendmail", @args or error("could not execute $cmd: $!");
}
##
## MAIN
##
#
# Read config file
my $config;
if (-r CONFIGFILE) {
$config = Config::Tiny->read(CONFIGFILE)
or error("could not read config from " . CONFIGFILE);
}
$config //= {};
#
# Initialize sendmail clients from config with defaults
my $msmtp_exe = $config->{msmtp}->{executable} // '/usr/bin/msmtp';
debug("Sendmail Clients: msmtp=$msmtp_exe");
#
# Get space owner (reverse uid lookup)
my $spaceowner;
{
delete local @ENV{qw/USER LOGNAME/}; # this ensures we do not get back an account name
$spaceowner = getpwuid($<) or error "could not resolve uid $<";
}
debug("Space Owner: $spaceowner");
#
# Get customer msmtp config
my $msmtp_configfile_pattern = ( $config->{msmtp}->{configfile_pattern}
// '/homepages/config/smtp/msmtprc.%s' );
my $msmtp_customer_config = sprintf($msmtp_configfile_pattern, $spaceowner);
debug("Checking msmtp config file at $msmtp_customer_config.");
#
# Execute msmtp if customer msmtp config exists
if (-r $msmtp_customer_config) {
debug("Config found. Will execute msmtp with custom config");
call($msmtp_exe, "-C", $msmtp_customer_config, @ARGV);
}
else {
error("This webspace is not enabled to use sendmail due to missing configuration.");
}
1;