Sophie

Sophie

distrib > Mandriva > 2008.1 > i586 > by-pkgid > e28667f4e1cf50e0b002c8a83e0e0d6f > files > 210

logwatch-7.3.6-2mdv2008.1.noarch.rpm

use strict;
##########################################################################
# $Id: pam_unix,v 1.29 2006/12/20 15:46:45 bjorn Exp $
##########################################################################
# $Log: pam_unix,v $
# Revision 1.29  2006/12/20 15:46:45  bjorn
# Additional filtering by Ivana Varekova.
#
# Revision 1.28  2006/10/20 16:41:38  bjorn
# Resolve uids, and better capture of authentication failure, by Willi Mann.
#
# Revision 1.27  2006/09/15 15:40:58  bjorn
# Additional filtering by Ivana Varekova.
#
# Revision 1.26  2006/01/20 22:31:04  bjorn
# Handle new pam_unix format, by Ivana Varekova.
#
# Revision 1.25  2005/12/01 04:15:04  bjorn
# Added dovecot.
#
# Revision 1.24  2005/11/30 05:03:00  bjorn
# Add support for kcheckpass, by Willi Mann.
#
# Revision 1.23  2005/11/28 01:16:33  bjorn
# Fixed typo introduced at previous update.
#
# Revision 1.22  2005/11/24 16:45:44  bjorn
# Cleaned up some unknowns, made regexps consistent, by David Baldwin.
#
# Revision 1.21  2005/09/26 18:19:03  mike
# Added rsh support per David Baldwin -mgt
#
# Revision 1.20  2005/08/23 22:10:26  mike
# Auth failure patch for RHEL 3 from Sergey Svishchev -mgt
#
# Revision 1.19  2005/04/20 17:12:26  bjorn
# Changes for Debian by Willi Mann
#
# Revision 1.18  2005/04/17 23:45:16  bjorn
# Bug fixes on Authentication Failure patch from Markus Lude and
# empty lognames and samba service names from Willi Mann
#
# Revision 1.17  2005/02/24 17:08:05  kirk
# Applying consolidated patches from Mike Tremaine
#
# Revision 1.9  2005/02/16 00:43:28  mgt
# Added #vi tag to everything, updated ignore.conf with comments, added emerge and netopia to the tree from Laurent -mgt
#
# Revision 1.8  2005/02/13 23:50:42  mgt
# Tons of patches from Pawel and PLD Linux folks...Thanks! -mgt
#
# Revision 1.7  2005/02/13 21:26:13  mgt
# patches from Michael Weiser -mgt
#
# Revision 1.6  2005/02/13 20:28:42  mgt
# More init corrections -mgt
#
# Revision 1.5  2004/10/11 18:37:15  mgt
# patches from Pawel -mgt
#
# Revision 1.4  2004/07/29 19:33:29  mgt
# Chmod and removed perl call -mgt
#
# Revision 1.3  2004/07/10 01:54:35  mgt
# sync with kirk -mgt
#
# Revision 1.14  2004/06/21 14:59:05  kirk
# Added tons of patches from Pawe? Go?aszewski" <blues@ds.pg.gda.pl>
#
# Thanks, as always!
#
# Revision 1.13  2004/02/03 02:45:26  kirk
# Tons of patches, and new 'oidentd' and 'shaperd' filters from
# Pawe? Go?aszewski" <blues@ds.pg.gda.pl>
#
##########################################################################

########################################################
# This was written and is maintained by:
#    Kirk Bauer <kirk@kaybee.org>
#
# Please send all comments, suggestions, bug reports,
#    etc, to kirk@kaybee.org.
########################################################


##########################################################################
# TO-DO
# We really should search for specific strings (authentication failure,
# bad username, check pass, password changed, session opened/closed,
# account expired, etc., using the service name as a variable in the hash,
# instead of having to add a test for every new service.
###########################################################################

use Logwatch ':sort';

my $Detail = $ENV{'LOGWATCH_DETAIL_LEVEL'} || 0;

my ($service, $line, %data);

while ($line = <STDIN>) {
	chomp $line;
	$service = $line;
   # for fedora and others
	if ($line =~ s/^... .. ..:..:.. .+ .+\(pam_unix\)\[\d+\]: //) {
		$service =~ s/^... .. ..:..:.. .+ (.+)\(pam_unix\)\[\d+\]: .*$/$1/;
   # new fedora (fc5) version 
   } elsif ( $line =~ s/^... .. ..:..:.. .+ pam_unix(.+:.+): //  ) {
      $service =~ s/^... .. ..:..:.. .+ pam_unix\((.+):.+\): .*$/$1/;
   # for debian sarge - "normal" lines
   } elsif ($line =~ s/^... .. ..:..:.. .+ [^ :]+: \(pam_unix\) //) {
      $service =~ s/^... .. ..:..:.. .+ ([^\s:\[\]]+)(?:\[[0-9]+\]|): \(pam_unix\) .*$/$1/;
   #for debian sarge - kdm - why can't they log in standard-compliant way?
   } elsif ( $line =~ s/^... .. ..:..:.. .+ [^\s:\[\]]+: [0-9:\[\]\.]+ \(pam_unix\) //) {
      $service =~ s/^... .. ..:..:.. .+ ([^\s:\[\]]+): [0-9:\[\]\.]+ \(pam_unix\) .*$/$1/;
   #for debian woody
   } elsif ( $line =~ s/^... .. ..:..:.. .+ PAM_unix\[\d+\]: \((.*?)\) // ) {
      $service =~ s/^... .. ..:..:.. .+ PAM_unix\[\d+\]: \(([^ ]*)\) .*/$1/;
	} else {
		next;
	}
   #lowercase the service 
   $service = lc($service); 
	if (($service eq 'sshd') or ($service eq 'login') or ($service eq 'ftp') or ($service eq 'rsh')) {
		if ($line =~ s/^session opened for user (.+) by \(uid=\d+\)/$1/) {
			($Detail >= 5) && $data{$service}{'Sessions Opened'}{$line}++;
      } elsif ($line =~ s/^session opened for user ([^ ]*) by ([^ ]*)\(uid=\d+\)/$1 by $2/) {
         ($Detail >= 5) && $data{$service}{'Sessions Opened'}{$line}++;
		} elsif ($line =~ s/^session opened for user (.+) by LOGIN\(uid=\d+\)/$1/) {
			$data{$service}{'Sessions Opened'}{$line}++;
		} elsif ($line =~ /session closed for user/) {
			# ignore this line
		} elsif ($line =~ s/^authentication failure; .*rhost=(\S*)\s+user=(\S*)$/$2 ($1)/) {
			$data{$service}{'Authentication Failures'}{$line}++;
      } elsif ($line =~ s/^authentication failure; .*rhost=(\S*).$/unknown ($1)/) {
         $data{$service}{'Authentication Failures'}{$line}++;
      } elsif ($line =~ s/^authentication failure; logname=(\S*) uid=(\d+) .*user=(\S*)$/$1($2) -> $3/) {
         $data{$service}{'Authentication Failures'}{$line}++;
      } elsif ($line =~ s/^(\d+) more authentication failure; .*rhost=(\S*)\s+user=(\S*)$/$3 ($2)/) {
         $data{$service}{'Authentication Failures'}{$line} += $1;
      } elsif ($line =~ s/^(\d+) more authentication failure; .*rhost=(\S*)$/unknown ($2)/) {
         $data{$service}{'Authentication Failures'}{$line} += $1;
      } elsif ($line =~ /check pass; user unknown/) {
         $data{$service}{'Invalid Users'}{'Unknown Account'}++;
      } elsif ($line =~ s/^password changed for (.+)/$1(by sshd)/) {
         ($Detail >= 5) && $data{passwd}{'Password changed'}{$line}++;
      } elsif ($line =~ s/^account (.+) has expired (failed to change password)$/$1/) {
         $data{$service}{'Expired Accounts'}{$line}++;
      } elsif ($line =~ s/bad username \[(.*)\]/$1/) {
         $data{$service}{'Invalid Users'}{"Bad User: $line"}++;
      } else {
         $data{$service}{'Unknown Entries'}{$line}++;
      }
   } elsif (($service eq 'su') or ($service eq 'sudo') or ($service eq 'su-l')) {
      if ( my ($logname, $uid, $ruser, $user) = ($line =~ /^authentication failure; logname=(\S*)\s+uid=(\d+) (?:.*ruser=(\S*)\s+)?.*user=(\S*)$/)) {
         $line = ($logname or $ruser)."($uid) -> $user";
         $data{$service}{'Authentication Failures'}{$line}++;
      } elsif ($line =~ /session closed for user/) {
         # ignore this line
      } elsif (my ($nam, $byid) = ($line =~ /session opened for user (.+) by (.+)$/)) {
         #resolve uid to name if possible
         $byid =~ s/\(uid=(\d+)\)/$1/;
         my $onam = getpwuid($byid) or $byid;
         $data{$service}{'Sessions Opened'}{"$onam -> $nam"}++;
      } elsif ($line =~ /^account root has password changed in future/) {
         #I'm not sure whether this info could not be reported
      } else {
         $data{$service}{'Unknown Entries'}{$line}++;
      }
   } elsif ($service eq 'passwd') {
      if ($line =~ s/^password changed for (.+)/$1/) {
         ($Detail >= 5) && $data{$service}{'Password changed'}{$line}++;
      }
   } elsif ($service eq 'poppassd') {
      if ($line =~ s/^password changed for (.+)/$1/) {
         ($Detail >= 5) && $data{$service}{'Password changed'}{$line}++;
      }
   } elsif ($service eq 'gdm') {
      if ($line =~ s/^session opened for user (.+) by \(uid=\d+\)/$1/) {
         ($Detail >= 5) && $data{$service}{'Sessions Opened'}{$line}++;
      } elsif ($line =~ s/^authentication failure; logname=.*user=(.+)$/$1/) {
         $data{$service}{'Authentication Failures'}{$line}++;
      } elsif ($line =~ /session closed for user/) {
         # ignore this line
      } elsif ($line =~ /bad username \[\]/) {
         # ignore this line
      } else {
         $data{$service}{'Unknown Entries'}{$line}++;
      }
   } elsif (($service eq 'kdm') or ($service eq 'kcheckpass')) {
     if ($line =~ s/^session opened for user (.+) by \(uid=\d+\)/$1/) {
        ($Detail >= 5) && $data{$service}{'Sessions Opened'}{$line}++;
     } elsif ($line =~ /session closed for user/) {
        # ignore this line
     } elsif ($line =~ s/^authentication failure; logname=\S* uid=\d* euid=\d* tty=[:\d]* ruser= rhost=  user=// ) {
       $data{$service}{'Authentication Failures'}{$line}++;
     }
     
 } elsif ($service eq 'xdm') {
	 if ($line =~ s/^session opened for user (.+) by \(uid=\d+\)/$1/) {
		 ($Detail >= 5) && $data{$service}{'Sessions Opened'}{$line}++;
	 } elsif ($line =~ /session closed for user/) {
		 # ignore this line
	 }
   } elsif ($service eq 'imap') {
      if ($line =~ s/^authentication failure; .*user=(.+)$/$1/) {
		   $data{$service}{'Authentication Failures'}{$line}++;
	   } else {
		   $data{$service}{'Unknown Entries'}{$line}++;
	   }
   } elsif ($service eq 'dovecot') {
      if ($line =~ s/^authentication failure; .*user=(.+)$/$1/) {
		   $data{$service}{'Authentication Failures'}{$line}++;
	   } else {
		   $data{$service}{'Unknown Entries'}{$line}++;
	   }
   } elsif ($service eq 'spop3d' || $service eq 'pop') {
	   if ($line =~ s/^session opened for user (.+)/$1/) {
		   $data{$service}{'Sessions Opened'}{$line}++;
	   } elsif ($line =~ /session closed for user/) {
		   # ignore this line
      } elsif ($line =~ s/^authentication failure; .*user=(.+)$/$1/) {
		   $data{$service}{'Authentication Failures'}{$line}++;
      } elsif ($line =~ s/^account (.+) has expired (failed to change password)$/$1/) {
         $data{$service}{'Expired Accounts'}{$line}++;
	   } else {
		   $data{$service}{'Unknown Entries'}{$line}++;
	   }
   } elsif ($service eq 'tpop3d') {
      if ($line =~ s/^authentication failure; .*rhost=(.+)  user=(.+)$/$2 ($1)/) {
         $data{$service}{'Authentication Failures'}{$line}++;
      } else {
         $data{$service}{'Unknown Entries'}{$line}++;
      }
   } elsif ($service eq 'pure-ftpd') {
      if ($line =~ s/^session opened for user (.+)/$1/) {
         $data{$service}{'Sessions Opened'}{$line}++;
      } elsif ($line =~ s/^check pass; (.+)/$1/) {
         $data{$service}{'Password Failures'}{$line}++;
      } else {
         $data{$service}{'Unknown Entries'}{$line}++;
      }
   } elsif (($service eq 'xscreensaver') || ($service eq 'gnome-screensaver') || ($service eq 'kscreensaver')) {
      if ($line =~ s/^authentication failure; .*uid=(\d+) euid=(\d+) tty=(.+) ruser= rhost=  user=(.+)$/$4($1,$2) on display $3/) {
         $data{$service}{'Authentication Failures'}{$line}++;
      }
   } elsif ($service =~ /^(f)?crond?$/ ) {
	   if ($line =~ s/^session opened for user (.+) by \(uid=\d+\)/$1/) {
		   ($Detail >= 5) && $data{$service}{'Sessions Opened'}{$line}++;
	   } elsif ($line =~ /session closed for user/) {
		   # ignore this line
	   } elsif ($line =~ /^account root has password changed in future/) {
	          #I'm not sure whether this info could not be reported
	   } else {
		   $data{$service}{'Unknown Entries'}{$line}++;
	   }
   } elsif ($service eq 'cyrus') {
      if ($line =~ /check pass; user unknown/) {
         $data{$service}{'Invalid Users'}{'Unknown Account'}++;
      } elsif ($line =~ /authentication failure; logname= uid=0 euid=0 tty= ruser= rhost=/) {
         # ignore this line
      } else {
         $data{$service}{'Unknown Entries'}{$line}++;
      }
   } elsif (($service eq 'samba') or ($service eq 'smbd')) {
      if ($line =~ s/^session opened for user ([a-zA-Z\d]+) by (.+)/$1/) {
         ($Detail >= 5) && $data{$service}{'Sessions Opened'}{$line}++;
      } elsif ($line =~ s/^session closed for user (.+)/$1/) {
         ($Detail >= 8) && $data{$service}{'Sessions Closed'}{$line}++;
      } else {
         $data{$service}{'Unknown Entries'}{$line}++;
      }
   } elsif ($service eq 'runuser') {
     if ($line =~/^session (opened)?(\/)?(closed)? for user [a-zA-Z\d]+/) {
     } else {
         $data{$service}{'Unknown Entries'}{$line}++;
     }
   } elsif ($service eq 'atd') {
     if ($line =~/^session (opened)?(\/)?(closed)? for user [a-zA-Z\d]+/) {
     } elsif ($line =~ /^account root has password changed in future/) { 
       #I'm not sure whether this info could not be reported
     } else {
         $data{$service}{'Unknown Entries'}{$line}++;
     }
   } elsif ($service eq 'system-config-date') {
     if ($line =~ s/auth could not identify password for \[(.*)\]/$1/) {
         $data{$service}{'Not Identify Password For'}{$line}++;
     } else {
         $data{$service}{'Unknown Entries'}{$line}++;
     }
   } else {      
      $data{$service}{'Unknown Entries'}{$line}++;
   }
}

foreach my $service (sort {$a cmp $b} keys %data) {
   print "$service:\n";
   foreach my $type (sort {$a cmp $b} keys %{$data{$service}}) {
      print "   $type:\n";
      my $sort = CountOrder(%{$data{$service}{$type}});
      foreach my $entry (sort $sort keys %{$data{$service}{$type}}) {
         print "      $entry: $data{$service}{$type}{$entry} Time(s)\n";
      }
   }
   print "\n";
}

exit(0);

# vi: shiftwidth=3 tabstop=3 syntax=perl et