src/ClamAVScanner.py
changeset 0 9f2855a83dae
child 1 0b9c9cee655d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/ClamAVScanner.py	Tue Feb 18 15:39:30 2014 +0100
     1.3 @@ -0,0 +1,77 @@
     1.4 +#!/usr/bin/python
     1.5 +
     1.6 +import ConfigParser
     1.7 +
     1.8 +import sys
     1.9 +
    1.10 +import logging
    1.11 +import os
    1.12 +import errno
    1.13 +import time
    1.14 +
    1.15 +import pyclamav
    1.16 +
    1.17 +
    1.18 +class ClamAVScanner:
    1.19 +    
    1.20 +    # User the existing logger  instance
    1.21 +    __LOG = logging.getLogger("IkarusScanner")
    1.22 +    
    1.23 +    __MINOPTS = { "Main" : ["Nothing"]}
    1.24 +    __CONFIG_NOT_READABLE = "Configfile is not readable"
    1.25 +    __CONFIG_WRONG = "Something is wrong with the config"
    1.26 +    __CONFIG_MISSING = "Section: \"%s\" Option: \"%s\" in configfile is missing"
    1.27 +    
    1.28 +
    1.29 +    
    1.30 +    def __init__ (self, scanner_config_path):
    1.31 +        config = self.loadConfig (scanner_config_path)
    1.32 +
    1.33 +    
    1.34 +
    1.35 +    def checkMinimumOptions (self, config):
    1.36 +        for section, options in self.__MINOPTS.iteritems ():
    1.37 +            for option in options:
    1.38 +                if (config.has_option(section, option) == False):
    1.39 +                    self.__LOG.error (self.__CONFIG_MISSING % (section, option))
    1.40 +                    exit (129)
    1.41 +
    1.42 +    def loadConfig (self, scanner_config_path):
    1.43 +
    1.44 +        configfile = scanner_config_path
    1.45 +        config = ConfigParser.SafeConfigParser ()
    1.46 +    
    1.47 +        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)):
    1.48 +            self.__LOG.error(self.__CONFIG_NOT_READABLE);
    1.49 +            raise SystemError(self.__CONFIG_NOT_READABLE)
    1.50 +    
    1.51 +        try:
    1.52 +            config.read (scanner_config_path)
    1.53 +        except Exception, e:
    1.54 +            self.__LOG.error("Error: %s" % (e));
    1.55 +            raise SystemError("Error: %s" % (e))
    1.56 +
    1.57 +        self.checkMinimumOptions (config)
    1.58 +    
    1.59 +        return config
    1.60 +
    1.61 +    
    1.62 +    def scanFile (self, path, fileobject):
    1.63 +        return self.scanFileClamAV (path)
    1.64 +    
    1.65 +    def scanFileClamAV (self, path):        
    1.66 +        retval = { "infected" : False, "virusname" : "Unknown" }
    1.67 +    
    1.68 +        self.__LOG.debug ("Scan File: %s" % (path))
    1.69 +    
    1.70 +        result = pyclamav.scanfile (path)
    1.71 +        self.__LOG.debug ("Result of file \"%s\": %s" % (path, result))
    1.72 +        if (result[0] != 0):
    1.73 +            retval["infected"] = True
    1.74 +            retval["virusname"] = result[1]
    1.75 +    
    1.76 +        if (retval["infected"] == True):
    1.77 +            self.__LOG.error ("Virus found, deny Access %s" % (result,))
    1.78 +    
    1.79 +        return retval
    1.80 +