sub aveclient_scanner {

    # Kaspersky aveclient socket-scanner
    &debug("aveclient: starting scan of directory \"$ENV{'TMPDIR'}\"...");
    my ($start_aveclient_time)=[gettimeofday];
    my ($DD,$aveclient_status,$stop_aveclient_time,$aveclient_time,$aveclient_silent);
    $aveclient_silent="-q";
    $aveclient_silent="" if ($DEBUG);
    

    # Need to scan each file since aveclient doesn't scan directories
    opendir(SCAN, $ENV{'TMPDIR'}) || warn "Could not open dir $ENV{'TMPDIR'}: $!\n";
    my @files_to_scan = grep { !/^\.+/ && -f } readdir(SCAN);
    closedir(SCAN);
    
    foreach my $file (@files_to_scan) {
	# Untaint $file
	&error_condition("Illegal file: $file") unless ($file =~ /^([\w+|\.|\-]+)$/);
        $file = $1;
	
        &debug("run $aveclient_binary $aveclient_silent -p $aveclient_socket -s $ENV{'TMPDIR'}/$file 2>&1");
        $DD=`$aveclient_binary $aveclient_silent -p $aveclient_socket -s $ENV{'TMPDIR'}/$file 2>&1`;
        $aveclient_status=$? >> 8;
	
        &debug("--output of aveclient was:\n$DD--");
	
        if( $aveclient_status != 0 ){
            if( $aveclient_status =~ /^(2|3|4)$/ ) {
                while ($DD =~ /(.*)INFECTED\nLINFECTED\s+(.*)/g) {
                    my $file_description=$1 || "unknown";
                    my $quarantine_description=$2 || "unknown";
                    &debug("Virus or suspicious objects found in $file_description ($quarantine_description)");
                }
                ($quarantine_event=$quarantine_description)=~s/\s/_/g;
                $quarantine_event="aveclient:".substr($quarantine_event,0,$QE_LEN);
            }
	    elsif( $aveclient_status == 1 ){
		&error_condition("Unable to connect to aveserver. Check if aveserver is running " .
				 "and for right path to aveserver-socket");
	    }
	    elsif( $aveclient_status =~ /^(6|8)$/ ){
		&error_condition("Scan results unavailable - exit status $aveclient_status");
	    }
	    else {
		&error_condition("Corrupt or unknown scanner error or memory/resource/perms " .
				 "problems - exit status $aveclient_status");
	    }
	}
	
	$stop_aveclient_time=[gettimeofday];
	$aveclient_time = tv_interval ($start_aveclient_time, $stop_aveclient_time);
	&debug("aveclient: finished scan of dir \"$ENV{'TMPDIR'}\" in $aveclient_time secs");
    }
}

