1
0
Fork 0

added syslog

This commit is contained in:
ston1th 2020-01-19 10:22:31 +01:00
commit 7c0d769fa9

View file

@ -5,6 +5,7 @@ use warnings;
use Config; use Config;
use IO::Socket; use IO::Socket;
use IO::Select; use IO::Select;
use Sys::Syslog qw(:standard :macros);
use IPC::Run qw(start finish); use IPC::Run qw(start finish);
# Configs # Configs
@ -26,8 +27,8 @@ sub read_timeout {
} }
my ($r_uid,$r_gid) = (getpwnam("root"))[2,3]; my ($r_uid,$r_gid) = (getpwnam("root"))[2,3];
my $s_gid = (getpwnam($smtp_group))[3] or die "group '$smtp_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 $w_gid = (getpwnam($www_group))[3] or die "group '${www_group}' not found: $!";
my $authfiletmp = "${authfile}.tmp"; my $authfiletmp = "${authfile}.tmp";
if ($Config{osname} eq "openbsd") { 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: $!"; 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) { 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( my $sock = IO::Socket::UNIX->new(
Type => SOCK_STREAM(), Type => SOCK_STREAM(),
@ -62,8 +64,8 @@ sub cleanup {
$SIG{INT} = \&cleanup; $SIG{INT} = \&cleanup;
$SIG{TERM} = \&cleanup; $SIG{TERM} = \&cleanup;
my $enc = "$smtpctl encrypt"; my @enc = (${smtpctl}, "encrypt");
my $update = "$smtpctl update table secrets"; my @update = (${smtpctl}, "update", "table", "secrets");
while (my $conn = $sock->accept()) { while (my $conn = $sock->accept()) {
my $user; my $user;
my $pass; my $pass;
@ -80,6 +82,7 @@ while (my $conn = $sock->accept()) {
my $h = start(\@enc, \<<IN, \$hash); my $h = start(\@enc, \<<IN, \$hash);
$pass $pass
IN IN
finish $h or next;
chomp($hash); chomp($hash);
if ($hash eq "") { if ($hash eq "") {
next; next;
@ -88,31 +91,34 @@ IN
open(my $auth, ">", $authfile) or next; open(my $auth, ">", $authfile) or next;
chown($r_uid, $s_gid, $authfile); chown($r_uid, $s_gid, $authfile);
chmod(0640, $authfile); chmod(0640, $authfile);
print $auth "$user $hash\n"; print $auth "${user} ${hash}\n";
close($auth); close($auth);
next; next;
} }
my $found = 0; 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); chown($r_uid, $s_gid, $authfile);
chmod(0640, $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); chown($r_uid, $s_gid, $authfiletmp);
chmod(0640, $authfiletmp); chmod(0640, $authfiletmp);
while (<$auth>) { while (<$auth>) {
my $line = $_; my $line = $_;
if ($line =~ /$user/) { if ($line =~ /$user/) {
print $authtmp "$user $hash\n"; print $authtmp "${user} ${hash}\n";
$found = 1; $found = 1;
} else { } else {
print $authtmp "$line"; print $authtmp "${line}";
} }
} }
if ($found == 0) { if ($found == 0) {
print $authtmp "$user $hash\n"; print $authtmp "${user} ${hash}\n";
} }
close($auth); close($auth);
close($authtmp); close($authtmp);
rename($authfiletmp, $authfile); 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}");
} }