#!/usr/bin/perl use strict; use warnings; use Config::IniFiles; use IO::Socket; use IO::Socket qw(:crlf); use IO::Select; use POSIX qw(:sys_wait_h); my ($Ini, $socket, $session, $child, $ident, @param, $peername, @allow, $tmp, @anrufe, @reverse, $sel, @read, @write, $end, $tt, ); my $port = 2345; my $quit = 0; $SIG{INT} = sub { $quit++; }; sub REAPER { while (waitpid(-1, WNOHANG) > 0) { print "closed\n"; } } $SIG{CHLD} = \&REAPER; $Ini = new Config::IniFiles( -file => "/etc/anrufer.conf", -nocase => 1 ); $socket = IO::Socket::INET->new( Listen => 20, LocalPort => $port, Timeout => 60*60, Reuse => 1) || die "Konnte Socket nicht oeffnen: $!\n"; $sel = new IO::Select($socket); print "Verbindung auf Port $port wird erwartet...\n"; while(!$quit) { next unless $session = $socket->accept(); $sel->add($session); print $session->peerhost."\n"; $child = fork(); die "Forking nicht mgoelich: $!\n" unless defined $child; unless($child) { $socket->close; $ident = $session->peerhost; @param = $Ini->Parameters('Number'); foreach $tmp ( @param ) { my @array = split /,/, $Ini->val('Number', $tmp); foreach ( @array ) { if ( m/^$ident$/ ) { push @allow, $tmp; } } } my $test = join "|", @allow; print $session "abonierte Nummern: @allow$CRLF" if $test; $peername = gethostbyaddr($session->peeraddr,AF_INET) || $session->peerhost; print "Verbindung von $peername auf Port $port akzeptiert ($$)\n"; if ( $test ) { open TAIL, "tail -n200 /var/log/anrufe.log |"; while () { if ( m/$test$/ ) { chomp; push @anrufe, "$_$CRLF"; } } close TAIL; for ( 1..$Ini->val('Common', 'Anzahl') ) { push @reverse, pop @anrufe; } shift @reverse; print $session reverse @reverse; open TL, "tail -fn1 /var/log/anrufe.log |"; $sel->add(\*TL); $end = 1; while ( @write = $sel->can_write(1) ) { foreach $tt (@write) { if ( $tt == $session ) { print $session "\b"; $end = 1; } } while ( (@read = $sel->can_read() ) and $end ) { foreach $tmp ( @read ) { if ( $tmp == $session ) { print "".<$tmp>; $end = 0; } else { $_ = <$tmp>; if ( m/$test$/ ) { chomp; print $session "$_ $CRLF"; } } } } } close TAIL; exit 0; } else { print $session "\nSie haben keine Nummern aboniert,\nbitte wenden Sie sich an ihren Administrator!\n\nAuf Wiedersehen.\n\n"; exit 0; } } $session->close; }