diff --git a/smtp_auth_sync.pl b/smtp_auth_sync.pl index d601029..b9eb145 100755 --- a/smtp_auth_sync.pl +++ b/smtp_auth_sync.pl @@ -5,7 +5,8 @@ use warnings; use Config; use IO::Socket; use IO::Select; -use IPC::Run qw (start finish ); +use Sys::Syslog qw(:standard :macros); +use IPC::Run qw(start finish); # Configs # File to store the user:password hash mappings @@ -26,8 +27,8 @@ sub read_timeout { } my ($r_uid,$r_gid) = (getpwnam("root"))[2,3]; -my $s_gid = (getpwnam($smtp_group))[3] or die "group '$smtp_group' not found: $!"; -my $w_gid = (getpwnam($www_group))[3] or die "group '$www_group' not found: $!"; +my $s_gid = (getpwnam($smtp_group))[3] or die "group '${smtp_group}' not found: $!"; +my $w_gid = (getpwnam($www_group))[3] or die "group '${www_group}' not found: $!"; my $authfiletmp = "${authfile}.tmp"; if ($Config{osname} eq "openbsd") { @@ -42,8 +43,9 @@ if ($Config{osname} eq "openbsd") { pledge("rpath wpath cpath fattr chown unix proc exec prot_exec") or die "Unable to pledge: $!"; } +openlog("smtp_auth_sync", "ndelay,pid", LOG_AUDIT) or die "Could not open syslog: $!"; if (-e $sfile) { - unlink $sfile or die "Could not delete socket file '$sfile': $!"; + unlink $sfile or die "Could not delete socket file '${sfile}': $!"; } my $sock = IO::Socket::UNIX->new( Type => SOCK_STREAM(), @@ -62,8 +64,8 @@ sub cleanup { $SIG{INT} = \&cleanup; $SIG{TERM} = \&cleanup; -my $enc = "$smtpctl encrypt"; -my $update = "$smtpctl update table secrets"; +my @enc = (${smtpctl}, "encrypt"); +my @update = (${smtpctl}, "update", "table", "secrets"); while (my $conn = $sock->accept()) { my $user; my $pass; @@ -80,6 +82,7 @@ while (my $conn = $sock->accept()) { my $h = start(\@enc, \<", $authfile) or next; chown($r_uid, $s_gid, $authfile); chmod(0640, $authfile); - print $auth "$user $hash\n"; + print $auth "${user} ${hash}\n"; close($auth); next; } my $found = 0; - open(my $auth, "<", $authfile) or do { print "Could not open file '$authfile': $!"; next; }; + open(my $auth, "<", $authfile) or do { print "Could not open file '${authfile}': $!"; next; }; chown($r_uid, $s_gid, $authfile); chmod(0640, $authfile); - open(my $authtmp, ">", $authfiletmp) or do { print "Could not open file '$authfiletmp': $!"; next; }; + open(my $authtmp, ">", $authfiletmp) or do { print "Could not open file '${authfiletmp}': $!"; next; }; chown($r_uid, $s_gid, $authfiletmp); chmod(0640, $authfiletmp); while (<$auth>) { my $line = $_; if ($line =~ /$user/) { - print $authtmp "$user $hash\n"; + print $authtmp "${user} ${hash}\n"; $found = 1; } else { - print $authtmp "$line"; + print $authtmp "${line}"; } } if ($found == 0) { - print $authtmp "$user $hash\n"; + print $authtmp "${user} ${hash}\n"; } close($auth); close($authtmp); rename($authfiletmp, $authfile); - system("$update >/dev/null"); + my $out; + my $u = start(\@update, \undef, \$out); + finish $u or next; + syslog(LOG_INFO, "updated password of user ${user}"); }