#!/usr/bin/perl
##########################################################################
# $Id: samba,v 1.1 1998/05/11 13:03:31 kirk Exp $
##########################################################################
# $Log: samba,v $
# Revision 1.1  1998/05/11 13:03:31  kirk
# Applied some wonderful patches sent in by
# Luuk de Boer <luuk_de_boer@pi.net>.
#
##########################################################################

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

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

# No sense in running if 'samba' doesn't even exist on this system...
unless (( -f "/usr/sbin/smbd" ) or 
	( -f "/usr/local/sbin/smbd") ) {
    exit (0);
}

if ( $Debug >= 5 ) {
    print STDERR "\n\nDEBUG: Inside Samba Filter \n\n";
}

while (defined($ThisLine = <STDIN>)) {
    if ( 
	 ($ThisLine =~ /^[^ ]+ \([^ ]+\) closed connection to/)
	) {
	# Don't care about these...
    }
    elsif ( ($Host,$Service,$User) = ( $ThisLine =~ /^([^ ]+ \([^ ]+\)) connect to service ([^ ]+) as user ([^ ]+ \([^ ]+\)) \(pid [^ ]+\)/ ) ) {
	$Connect{$Service}{$User}{$Host}++;
    }
    elsif ( ($NoService) = ( $ThisLine =~ /^couldn't find service ([^ ]+)$/ ) ) {
	chomp($NoService);
	$NoServ{$NoService}++;
    }
    else {
	# Report any unmatched entries...
	$OtherList{$ThisLine}++;
    }
}

if ( ( ($Detail >= 5) and (keys %Connect) ) or
     ( ($Detail >= 5) and (keys %NoServ) ) or
     ( keys %OtherList ) 
     ) {

    print "\n\n --------------------- Samba Begin ------------------------ \n";
    
    if ( ( $Detail >= 5 ) and (keys %Connect) ) {
	print "\nOpened Sessions:\n";
	foreach $Serv (sort {$a cmp $b} keys %Connect) {
	    print "   Service $Serv as user:\n";
	    foreach $Us (sort {$a cmp $b} keys %{$Connect{$Serv}}) {
		print "      $Us from host:\n";
		foreach $Ho (sort {$a cmp $b} keys %{$Connect{$Serv}{$Us}}) {
		    print "         $Ho : $Connect{$Serv}{$Us}{$Ho} Time(s)\n";
		}
	    }
	}
    }

    if ( ( $Detail >= 5 ) and (keys %NoServ) ) {
	print "\nCouldn't find services:\n";
	foreach $ThisOne (sort {$a cmp $b} keys %NoServ) {
	    print "   $ThisOne : $NoServ{$ThisOne} 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";
	}
    }
    
    print "\n\n ---------------------- Samba End ------------------------- \n\n";

}
    
exit(0);
    


