#!/usr/bin/perl
##########################################################################
# $Id: pureftpd,v 1.1 2003/11/03 14:25:22 kirk Exp $
##########################################################################
# Written & Maintained by Chris Smith (csmith@squiz.net)
##########################################################################

$Detail = $ENV{'LOGWATCH_DETAIL_LEVEL'};
$ShowLogins  = $ENV{'show_logins'};
$ShowLogouts = $ENV{'show_logouts'};
$ShowDataTransfers = $ENV{'show_data_transfers'};
$ShowNewConnections = $ENV{'show_new_connections'};

while (defined($ThisLine = <STDIN>)) {
   if (
         ( $ThisLine =~ /last message repeated/ ) or
		( $ThisLine =~ /Timeout/) or
		( $ThisLine =~ /Can't change directory/)
      # We don't care about these
      ) {
   } elsif (($IP,$j) = ($ThisLine =~ /\@(.*?)\)(.*?)new connection/i )) {
		$NewConnections{$IP}++;
   } elsif (($IP,$j) = ($ThisLine =~ /\@(.*?)\)(.*?)logout/i )) {
		$Logouts{$IP}++;
   } elsif (($IP,$j) = ($ThisLine =~ /\@(.*?)\)(.*?)unable to set up secure anonymous ftp/i )) {
		$SecureAnon{$IP}++;
   } elsif (($IP,$User) = ($ThisLine =~ /\@(.*?)\)\s*\[info\]\s*(.*?) is now logged in/i )) {
		$Logins->{$IP}->{$User}++;
   } elsif (($j,$ConnectionCount,$IP) = ($ThisLine =~ /(.*?)too many connections \((.*?)\) from this ip\: \[(.*?)\]/i )) {
		$TooManyConnections->{$ConnectionCount}->{$IP}++;
	} elsif (($User,$Location,$File,$Direction) = ($ThisLine =~ /\((.*?)\@(.*?)\)\s+\[\w+\]\s+(.*?)\s+(\w+)\s+/)) {
		$Direction->{$User}->{$Location}->{$File}++;
   } else {
      # Report any unmatched entries...
      push @OtherList,$ThisLine;
   }
}

if ($ShowNewConnections) {
	if ( (keys %NewConnections) ) {
	   print "\nNew Connections:\n";
	   foreach $Line (sort {$a cmp $b} keys %NewConnections) {
		  print "\t" . $Line . " - ". $NewConnections{$Line} . " Time(s)\n";
	   }
	}
}

if ($ShowLogins) {
	if ( (keys %{$Logins}) ) {
	   print "\nSuccessful Logins:\n";
	   foreach $Line (sort {$a cmp $b} keys %{$Logins}) {
		   foreach $Detail (sort {$a cmp $b} keys %{$Logins->{$Line}}) {
			print "\t" . $Detail. " (" . $Line . ") - ". $Logins->{$Line}->{$Detail} . " Time(s)\n";
		   }
		}
	}
}

if ( (keys %{$TooManyConnections}) ) {
   print "\nToo Many Connections:\n";
   foreach $Line (sort {$a cmp $b} keys %{$TooManyConnections}) {
	   foreach $Detail (sort {$a cmp $b} keys %{$TooManyConnections->{$Line}}) {
		print "\t" . $Detail. " (" . $Line . " connections) - ". $TooManyConnections->{$Line}->{$Detail} . " Time(s)\n";
	   }
	}
}

if ($ShowDataTransfers) {
	if ( (keys %{$Direction}) ) {
	   print "\nData Transferred:\n";
	   foreach $User (sort {$a cmp $b} keys %{$Direction}) {
		   foreach $Location (sort {$a cmp $b} keys %{$Direction->{$User}}) {
			   foreach $Filename (sort {$a cmp $b} keys %{$Direction->{$User}->{$Location}}) {
					print "\tUser " . $User . " " . $Direction . " " . $Filename . " from " . $Location . " - ". $Direction->{$User}->{$Location}->{$Filename} . " Time(s)\n";
			   }
		   }
		}
	}
}

if ( (keys %SecureAnon) ) {
   print "\nUnsuccessful Secure Anonymous Connections:\n";
   foreach $Line (sort {$a cmp $b} keys %SecureAnon) {
	  print "\t" . $Line . " - ". $SecureAnon{$Line} . " Time(s)\n";
   }
}

if ($ShowLogouts) {
	if ( (keys %Logouts) ) {
	   print "\nLogouts:\n";
	   foreach $Line (sort {$a cmp $b} keys %Logouts) {
		  print "\t" . $Line . " - ". $Logouts{$Line} . " Time(s)\n";
	   }
	}
}

if (($#OtherList >= 0) and (not $IngoreUnmatched)){
   print "\n**Unmatched Entries**\n";
   print @OtherList;
}

exit(0);

