ft@0: #!/usr/bin/python ft@0: ft@0: import ConfigParser ft@0: ft@0: import sys ft@0: ft@0: import logging ft@0: import os ft@0: import errno ft@0: import time ft@0: ft@0: import pyclamav ft@0: ft@0: ft@0: class ClamAVScanner: ft@0: ft@0: # User the existing logger instance ft@0: __LOG = logging.getLogger("IkarusScanner") ft@0: ft@0: __MINOPTS = { "Main" : ["Nothing"]} ft@0: __CONFIG_NOT_READABLE = "Configfile is not readable" ft@0: __CONFIG_WRONG = "Something is wrong with the config" ft@0: __CONFIG_MISSING = "Section: \"%s\" Option: \"%s\" in configfile is missing" ft@0: ft@0: ft@0: ft@0: def __init__ (self, scanner_config_path): ft@0: config = self.loadConfig (scanner_config_path) ft@0: ft@0: ft@0: ft@0: def checkMinimumOptions (self, config): ft@0: for section, options in self.__MINOPTS.iteritems (): ft@0: for option in options: ft@0: if (config.has_option(section, option) == False): ft@0: self.__LOG.error (self.__CONFIG_MISSING % (section, option)) ft@0: exit (129) ft@0: ft@0: def loadConfig (self, scanner_config_path): ft@0: ft@0: configfile = scanner_config_path ft@0: config = ConfigParser.SafeConfigParser () ft@0: ft@0: 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: self.__LOG.error(self.__CONFIG_NOT_READABLE); ft@0: raise SystemError(self.__CONFIG_NOT_READABLE) ft@0: ft@0: try: ft@0: config.read (scanner_config_path) ft@0: except Exception, e: ft@0: self.__LOG.error("Error: %s" % (e)); ft@0: raise SystemError("Error: %s" % (e)) ft@0: ft@0: self.checkMinimumOptions (config) ft@0: ft@0: return config ft@0: ft@0: ft@0: def scanFile (self, path, fileobject): ft@0: return self.scanFileClamAV (path) ft@0: ft@0: def scanFileClamAV (self, path): ft@0: retval = { "infected" : False, "virusname" : "Unknown" } ft@0: ft@0: self.__LOG.debug ("Scan File: %s" % (path)) ft@0: ft@0: result = pyclamav.scanfile (path) ft@0: self.__LOG.debug ("Result of file \"%s\": %s" % (path, result)) ft@0: if (result[0] != 0): ft@0: retval["infected"] = True ft@0: retval["virusname"] = result[1] ft@0: ft@0: if (retval["infected"] == True): ft@0: self.__LOG.error ("Virus found, deny Access %s" % (result,)) ft@0: ft@0: return retval ft@0: