#!/usr/bin/perl -w
##########################################################################
# $Id: ftpd-xferlog,v 1.4 1999/02/23 00:41:40 kirk Exp $
##########################################################################
# $Log: ftpd-xferlog,v $
# Revision 1.4  1999/02/23 00:41:40  kirk
# Added patch submitted by Jason McMullan <jmcc@ontv.com>.
#
# Revision 1.3  1998/02/23 01:16:55  kirk
# Getting ready for a first distribution
#
# Revision 1.2  1998/02/22 20:55:03  kirk
# Finalized FTP filters...
#
# Revision 1.1  1998/02/22 20:02:35  kirk
# Finished PAM_pwdb, split ftpd into 2 filters...
#
##########################################################################

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

sub remove_dups {
	my(@info)=sort @_;

	my(%count,@out,$i);
	foreach $i (@info) {
		$count{$i}++;
	}

	foreach $i (keys %count) {
		my($j)=$i;
		$j =~ s/\n//;
		push @out,$j." (".$count{$i}.")\n";
	}
	return @out;
}
	

$Detail = $ENV{'LOGWATCH_DETAIL_LEVEL'};

$TotalBytesOut = 0;
$TotalBytesIn = 0;

while (defined($ThisLine = <STDIN>)) {
    if ( ($RemoteHost,$Size,$FileName,$Direction,$AccessMode,$UserName) = 
	 ( $ThisLine =~ /^([^ ]+) ([0123456789]+) (.*) . . (.) (.) (.*) ftp . .*$/ ) ) {
	if ( ( $AccessMode eq "a") or ( $AccessMode eq "g" ) ) {
	    # Anonymous transfers
	    if ( $Direction eq "o" ) {
		# File was outgoing
		$TotalBytesOut+= $Size;
		if ($Detail >= 15) {
		    $Temp = "   " . $FileName . " -> " . $RemoteHost . " (Email: " . $UserName . ")\n";
		}		
		else {
		    $Temp = "   " . $FileName . " -> " . $RemoteHost . "\n";
		}
		push @AnonOut,$Temp;
	    }
	    else {
		# File was incoming
		$TotalBytesIn+= $Size;
		if ($Detail >= 15) {
		    $Temp = "   " . $RemoteHost . " -> " . $FileName . " (Email: " . $UserName . ")\n";
		}		
		else {
		    $Temp = "   " . $RemoteHost . " -> " . $FileName . "\n";
		}
		push @AnonIn,$Temp;
	    }
	}
	else {
	    # User transfers
	    if ( $Direction eq "o" ) {
		# File was outgoing
		$TotalBytesOut+= $Size;
		$Temp = "   " . $FileName . " -> " . $RemoteHost . " (User: " . $UserName . ")\n";
		push @UserOut,$Temp;
	    }
	    else {
		# File was incoming
		$TotalBytesIn+= $Size;
		$Temp = "   " . $RemoteHost . " -> " . $FileName . " (User: " . $UserName . ")\n";
		push @UserIn,$Temp;
	    }
	}
    }			 
    else {
	# Report any unmatched entries...
	push @OtherList,$ThisLine;
    }
}

@AnonOut=&remove_dups(@AnonOut);
@AnonIn=&remove_dups(@AnonIn);
@UserOut=&remove_dups(@UserOut);
@UserIn=&remove_dups(@UserIn);
@OtherList=&remove_dups(@OtherList);

if ( 
     ( (@AnonOut) and ($Detail >= 5 ) ) or
     ( @AnonIn ) or
     ( (@UserOut) and ($Detail >= 10 ) ) or
     ( (@UserIn) and ($Detail >= 10 ) ) or
     ( @OtherList ) 
     ) {			 

    print "\n\n --------------------- ftpd-xferlog Begin ------------------------ \n";

    $TotalKBytesOut = int $TotalBytesOut/1000;
    $TotalKBytesIn = int $TotalBytesIn/1000;
    $TotalMBytesOut = int $TotalKBytesOut/1000;
    $TotalMBytesIn = int $TotalKBytesIn/1000;
    print "TOTAL KB OUT: " . $TotalKBytesOut . "KB (" . $TotalMBytesOut . "MB)\n";
    print "TOTAL KB IN: " . $TotalKBytesIn . "KB (" . $TotalMBytesIn . "MB)\n";
    
    if (@AnonIn) {
	print "\nIncoming Anonymous FTP Transfers:\n";
	print @AnonIn;
    }

    if ( (@AnonOut) and ($Detail >= 5) ) {
	print "\nOutgoing Anonymous FTP Transfers:\n";
	print @AnonOut;
    }

    if ( (@UserIn) and ($Detail >= 10) ) {
	print "\nIncoming User FTP Transfers:\n";
	print @UserIn;
    }

    if ( (@UserOut) and ($Detail >= 10) ) {
	print "\nOutgoing User FTP Transfers:\n";
	print @UserOut;
    }

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

    print "\n\n ---------------------- ftpd-xferlog End ------------------------- \n\n";

}

exit(0);



