# HG changeset patch # User ft # Date 1392734370 -3600 # Node ID 9f2855a83dae076fec049b2cae5492edde43a234 initial commit working clamav scan engine diff -r 000000000000 -r 9f2855a83dae config/ClamAVScanner.cfg --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/config/ClamAVScanner.cfg Tue Feb 18 15:39:30 2014 +0100 @@ -0,0 +1,2 @@ +[Main] +Nothing: placeholder \ No newline at end of file diff -r 000000000000 -r 9f2855a83dae src/ClamAVScanner.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/ClamAVScanner.py Tue Feb 18 15:39:30 2014 +0100 @@ -0,0 +1,77 @@ +#!/usr/bin/python + +import ConfigParser + +import sys + +import logging +import os +import errno +import time + +import pyclamav + + +class ClamAVScanner: + + # User the existing logger instance + __LOG = logging.getLogger("IkarusScanner") + + __MINOPTS = { "Main" : ["Nothing"]} + __CONFIG_NOT_READABLE = "Configfile is not readable" + __CONFIG_WRONG = "Something is wrong with the config" + __CONFIG_MISSING = "Section: \"%s\" Option: \"%s\" in configfile is missing" + + + + def __init__ (self, scanner_config_path): + config = self.loadConfig (scanner_config_path) + + + + def checkMinimumOptions (self, config): + for section, options in self.__MINOPTS.iteritems (): + for option in options: + if (config.has_option(section, option) == False): + self.__LOG.error (self.__CONFIG_MISSING % (section, option)) + exit (129) + + def loadConfig (self, scanner_config_path): + + configfile = scanner_config_path + config = ConfigParser.SafeConfigParser () + + 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)): + self.__LOG.error(self.__CONFIG_NOT_READABLE); + raise SystemError(self.__CONFIG_NOT_READABLE) + + try: + config.read (scanner_config_path) + except Exception, e: + self.__LOG.error("Error: %s" % (e)); + raise SystemError("Error: %s" % (e)) + + self.checkMinimumOptions (config) + + return config + + + def scanFile (self, path, fileobject): + return self.scanFileClamAV (path) + + def scanFileClamAV (self, path): + retval = { "infected" : False, "virusname" : "Unknown" } + + self.__LOG.debug ("Scan File: %s" % (path)) + + result = pyclamav.scanfile (path) + self.__LOG.debug ("Result of file \"%s\": %s" % (path, result)) + if (result[0] != 0): + retval["infected"] = True + retval["virusname"] = result[1] + + if (retval["infected"] == True): + self.__LOG.error ("Virus found, deny Access %s" % (result,)) + + return retval +