#!/usr/bin/perl
##########################################################################
# $Id: pop3,v 1.1 2004/06/21 14:59:05 kirk Exp $
##########################################################################

########################################################
# Logwatch was written and is maintained by:
#    Kirk Bauer <kirk@kaybee.org>
#
# The pop-3 script was written by:
#    Pawe Goaszewski <blues@gda.pl>
#
########################################################

my $Debug = $ENV{'LOGWATCH_DEBUG'};
my $Detail = $ENV{'LOGWATCH_DETAIL_LEVEL'};

#Make pseudo IPv6 to IPv4
sub LookupIPv46 {
   my $IPv4Addr;
   my $Addr = $_[0];
   if ( ($IPv4Addr) = ($Addr =~ /::ffff:([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})/ ) ) {
#      print "$IPv4Addr\n";
      return $IPv4Addr;
      
      }
   else {
#      print $Addr;
      return $Addr;
      
      }
}
   
if ( $Debug >= 5 ) {
    print STDERR "\n\nDEBUG \n\n";
}

while (defined($ThisLine = <STDIN>)) {
   if (
      ($ThisLine =~ /^auth: PAM error: Authentication failure$/) or
      ($ThisLine =~ /^getpeername: Socket operation on non-socket$/) or
      ($ThisLine =~ /^Initializing */) or
      ($ThisLine =~ /^Installing */) or
      ($ThisLine =~ /^(mailbox: )?open: No such file or directory$/) or
      ($ThisLine =~ /^(sktbuff|maildrop): write: Broken pipe$/) or
      ($ThisLine =~ /^maildrop: can't read message$/) or
      ($ThisLine =~ /^maildrop: can't write to socket$/) or
      ($ThisLine =~ /^mailbox: mailbox content has been changed$/) or
      ($ThisLine =~ /^maildrop: write: Connection reset by peer$/) or
      ($ThisLine =~ /^(sktbuff|maildrop): write: Connection timed out$/) or
      ($ThisLine =~ /^open: Permission denied$/) or
      ($ThisLine =~ /^read: Broken pipe$/) or
      ($ThisLine =~ /^read: Connection reset by peer$/) or
      ($ThisLine =~ /^spgetpwnam: can't find user: */) or
      ($ThisLine =~ /^sptls: SSL_accept error: (-|)\d+$/) or
      ($ThisLine =~ /^sptls: do need at least RSA or DSA cert\/key data$/)
   ) {
      # Don't care about these...
   } elsif ( ($User, $Host) = ( $ThisLine =~ /^user (.*?) authenticated - (.*)$/ ) ) {
      $Login{$User}{$Host}++;
   } elsif ( ($User,$Downloaded,$DownloadSize,$Left,$LeftSize) = ( $ThisLine =~ /^Stats: (.*?) (.*?) (.*?) (.*?) (.*?)$/) ) {
      $DownloadedMessages{$User} += $Downloaded;
      $DownloadedMessagesSize{$User} += $DownloadSize;
      $MessagesLeft{$User} = $Left;
      $MboxSize{$User} = $LeftSize;
   } elsif ( ($User, $Host) = ( $ThisLine =~ /^session ended for user (.*?) - (.*)/) ) {
      $Logout{$User}{$Host}++;
      $Logout2{$User}++;
      $Connection{$Host}++;
   } elsif  ( ($Host) = ( $ThisLine =~ /^session ended - (.*)$/) ) {
      $Logout{"UNKNOWN"}{$Host}++;
      $Connection{$Host}++;
   } elsif ( ($User,$Host) = ( $ThisLine =~ /^authentication failed for user (.*?) - (.*)/ ) ) {
      $LoginFailed{"$Host ($User)"}++;
   } elsif ( ($User,$Host) = ( $ThisLine =~ /^authentication failed: no such user: (.*?) - (.*)/ ) ) {
      $LoginFailed{"$Host (UNKNOWN: $User)"}++;
   } elsif ( ($Mechanism) = ( $ThisLine =~ /^sptls: TLS connection established: (.*)$/ ) ) {
      $sslMechanism{$Mechanism}++;
   } elsif ($ThisLine =~ /^sptls: created \d+bit temporary [^ ].* key$/ ) {
      $sslTempkey++;
   } elsif ( ($Host) = ( $ThisLine =~ /^autologout time elapsed - (.*)$/ ) ) {
      $AutoLogout{$Host}++;
   } elsif (
      (($File) = ( $ThisLine =~ /^can't open or create file: (.*)$/ )) or
      (($File) = ( $ThisLine =~ /^mailbox: can't open mailbox file: (.*)$/ ))
   ) {
      $PermissionDenied{$File}++;
   } elsif ( ($User, $Host) = ( $ThisLine =~ /^can't find APOP secret for user (.*?) - (.*)$/ ) ) {
      $NoApopSecret{$User}++;
      $Logout{$User}{$Host}++;
      $Connection{$Host}++;
      $Logout2{$User}++;
   } elsif ($ThisLine =~ /^mailbox: no memory available$/ ) {
      $OutOfMemory++;
   } else {
      # Report any unmatched entries...
      # remove PID from named messages

      $ThisLine =~ s/^(client [.0-9]+)\S+/$1/;
      chomp($ThisLine);
      $OtherList{$ThisLine}++;
   }
   $LastLine = $ThisLine;
}

################################################

if ( ( $Detail >= 0 ) and (keys %PermissionDenied)) {
   print "WARNING:\n";
   print "Can't open or create files:\n";
   foreach $File (sort {$a cmp $b} keys %PermissionDenied) {
      print "   $File: $PermissionDenied{$File} Time(s)\n";
   }
}

if ( ( $Detail >= 0 ) and ($OutOfMemory > 0) ) {
   print "\nPOP3 processes were running out of memory $OutOfMemory Time(s)\n";
}

if ( ( $Detail >= 0 ) and (keys %LoginFailed)) {
   print     "\n\n[POP3] Login failures:".
             "\n=========================".
             "\n                                                  Host (user) |          # ".
             "\n------------------------------------------------------------- | -----------";
  
   $ConnCount = 0;
   foreach $Host (sort keys %LoginFailed) {
      $Conns = $LoginFailed{$Host};
      printf "\n%61s | %11.0f", $Host, $Conns;
      $ConnCount += $Conns;
   }
   print "\n" . "-" x 75;
   printf "\n%75s\n\n\n", $ConnCount;
}

if ( ( $Detail >= 5 ) and (keys %Connection)) {
   print     "\n[POP3] Connections:".
             "\n=========================".
             "\n                                                         Host | Connections".
             "\n------------------------------------------------------------- | -----------";
  
   $ConnCount = 0;
   foreach $Host (sort keys %Connection) {
      $Conns = $Connection{$Host};
      printf "\n%61s | %11.0f", $Host, $Conns;
      $ConnCount += $Conns;
   }
   print "\n" . "-" x 75;
   printf "\n%75s\n\n\n", $ConnCount;
}



if (keys %Logout2) {
   print     "\n[POP3] Logout stats (in MB):".
             "\n============================".
             "\n                                   User | Logouts | Downloaded |  Mbox Size".
             "\n--------------------------------------- | ------- | ---------- | ----------";
  
   $ConnCount = 0;
   $SizeAll = 0;
   $DownAll = 0;
   foreach $User (sort keys %Logout2) {
      $Conns = $Logout2{$User};
      $Down = $DownloadedMessagesSize{$User}/(1024*1024);
      $Size = $MboxSize{$User}/(1024*1024);
      printf "\n%39s | %7d | ", $User, $Conns;
      if ($Down > 0) {
         printf "%10.2f | ",$Down;
      } else {
         printf "%10.0f | ",$Down;
      }
      if ($Size > 0) {
         printf "%10.2f",$Size;
      } else {
         printf "%10.0f",$Size;
      }
      $ConnCount += $Conns;
      $SizeAll += $Size;
      $DownAll += $Down;
   }
   print "\n" . "-" x 75;
   printf "\n%49d | %10.2f | %10.2f\n\n\n",$ConnCount,$DownAll,$SizeAll;
}


if ( ( $Detail >= 10 ) and (keys %Login)) {
   print "\n[POP3] Successful Logins:\n";
   $LoginCount = 0;
   foreach my $User (keys %Login) {
      print "  User $User: \n";
      $UserCount = 0;
      foreach $Host (keys %{$Login{$User}}) {
         $HostCount = $Login{$User}{$Host};
         print "    From $Host: $HostCount Time(s)\n";
         $UserCount += $HostCount;
      }
      $LoginCount += $UserCount;
      print "  Total $UserCount Time(s)\n";
      print "\n";
   }
   print "Total $LoginCount successful logins\n\n\n";
}

if ($sslTempkey > 0) {
   print "\nTemporary SSL key created and used $sslTempkey Time(s)\n";
}

if ( ( $Detail >= 5 ) and (keys %sslMechanism)) {
   print "\nTLS Connection types:\n";
   $TotalConnections = 0;
   foreach $Mechanism (keys %sslMechanism) {
      print "   $Mechanism $sslMechanism{$Mechanism} Time(s)\n";
      $TotalConnections += $sslMechanism{$Mechanism};
   }
   print "Total TLS connections: $TotalConnections Time(s)\n";
}

if ( ( $Detail >= 5 ) and (keys %AutoLogout)) {
   print "\nAutologout:\n";
   foreach $Host (sort {$a cmp $b} keys %AutoLogout) {
      print "   $Host: $AutoLogout{$Host} Time(s)\n";
   }
}

if ( ( $Detail >= 5 ) and (keys %NoApopSecret)) {
   print "\nCan't find APOP secret:\n";
   $TotalAPOP = 0;
   foreach $User (keys %NoApopSecret) {
      print "   $User: $NoApopSecret{$User} Time(s)\n";
      $TotalAPOP += $NoApopSecret{$User};
   }
   print "Total APOP errors: $TotalAPOP Time(s)\n";
}

if (keys %OtherList) {
   print "\n**Unmatched Entries**\n";
   foreach $line (sort {$a cmp $b} keys %OtherList) {
      print "   $line: $OtherList{$line} Time(s)\n";
   }
}

exit(0);


# vi: shiftwidth=3 tabstop=3 et

