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