src/ClamAVScanner.py
author ft
Tue, 18 Feb 2014 15:39:30 +0100
changeset 0 9f2855a83dae
child 1 0b9c9cee655d
permissions -rwxr-xr-x
initial commit
working clamav scan engine
ft@0
     1
#!/usr/bin/python
ft@0
     2
ft@0
     3
import ConfigParser
ft@0
     4
ft@0
     5
import sys
ft@0
     6
ft@0
     7
import logging
ft@0
     8
import os
ft@0
     9
import errno
ft@0
    10
import time
ft@0
    11
ft@0
    12
import pyclamav
ft@0
    13
ft@0
    14
ft@0
    15
class ClamAVScanner:
ft@0
    16
    
ft@0
    17
    # User the existing logger  instance
ft@0
    18
    __LOG = logging.getLogger("IkarusScanner")
ft@0
    19
    
ft@0
    20
    __MINOPTS = { "Main" : ["Nothing"]}
ft@0
    21
    __CONFIG_NOT_READABLE = "Configfile is not readable"
ft@0
    22
    __CONFIG_WRONG = "Something is wrong with the config"
ft@0
    23
    __CONFIG_MISSING = "Section: \"%s\" Option: \"%s\" in configfile is missing"
ft@0
    24
    
ft@0
    25
ft@0
    26
    
ft@0
    27
    def __init__ (self, scanner_config_path):
ft@0
    28
        config = self.loadConfig (scanner_config_path)
ft@0
    29
ft@0
    30
    
ft@0
    31
ft@0
    32
    def checkMinimumOptions (self, config):
ft@0
    33
        for section, options in self.__MINOPTS.iteritems ():
ft@0
    34
            for option in options:
ft@0
    35
                if (config.has_option(section, option) == False):
ft@0
    36
                    self.__LOG.error (self.__CONFIG_MISSING % (section, option))
ft@0
    37
                    exit (129)
ft@0
    38
ft@0
    39
    def loadConfig (self, scanner_config_path):
ft@0
    40
ft@0
    41
        configfile = scanner_config_path
ft@0
    42
        config = ConfigParser.SafeConfigParser ()
ft@0
    43
    
ft@0
    44
        if ((os.path.exists (scanner_config_path) == False) or (os.path.isfile (scanner_config_path) == False) or (os.access (scanner_config_path, os.R_OK) == False)):
ft@0
    45
            self.__LOG.error(self.__CONFIG_NOT_READABLE);
ft@0
    46
            raise SystemError(self.__CONFIG_NOT_READABLE)
ft@0
    47
    
ft@0
    48
        try:
ft@0
    49
            config.read (scanner_config_path)
ft@0
    50
        except Exception, e:
ft@0
    51
            self.__LOG.error("Error: %s" % (e));
ft@0
    52
            raise SystemError("Error: %s" % (e))
ft@0
    53
ft@0
    54
        self.checkMinimumOptions (config)
ft@0
    55
    
ft@0
    56
        return config
ft@0
    57
ft@0
    58
    
ft@0
    59
    def scanFile (self, path, fileobject):
ft@0
    60
        return self.scanFileClamAV (path)
ft@0
    61
    
ft@0
    62
    def scanFileClamAV (self, path):        
ft@0
    63
        retval = { "infected" : False, "virusname" : "Unknown" }
ft@0
    64
    
ft@0
    65
        self.__LOG.debug ("Scan File: %s" % (path))
ft@0
    66
    
ft@0
    67
        result = pyclamav.scanfile (path)
ft@0
    68
        self.__LOG.debug ("Result of file \"%s\": %s" % (path, result))
ft@0
    69
        if (result[0] != 0):
ft@0
    70
            retval["infected"] = True
ft@0
    71
            retval["virusname"] = result[1]
ft@0
    72
    
ft@0
    73
        if (retval["infected"] == True):
ft@0
    74
            self.__LOG.error ("Virus found, deny Access %s" % (result,))
ft@0
    75
    
ft@0
    76
        return retval
ft@0
    77