ft@22: #!/usr/bin/python ft@22: ft@22: # ------------------------------------------------------------ ft@22: # opensecurity package file ft@22: # ft@22: # Autor: X-Net Services GmbH ft@22: # ft@22: # Copyright 2013-2014 X-Net and AIT Austrian Institute of Technology ft@22: # ft@22: # ft@22: # X-Net Technologies GmbH ft@22: # Elisabethstrasse 1 ft@22: # 4020 Linz ft@22: # AUSTRIA ft@22: # https://www.x-net.at ft@22: # ft@22: # AIT Austrian Institute of Technology ft@22: # Donau City Strasse 1 ft@22: # 1220 Wien ft@22: # AUSTRIA ft@22: # http://www.ait.ac.at ft@22: # ft@22: # ft@22: # Licensed under the Apache License, Version 2.0 (the "License"); ft@22: # you may not use this file except in compliance with the License. ft@22: # You may obtain a copy of the License at ft@22: # ft@22: # http://www.apache.org/licenses/LICENSE-2.0 ft@22: # ft@22: # Unless required by applicable law or agreed to in writing, software ft@22: # distributed under the License is distributed on an "AS IS" BASIS, ft@22: # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ft@22: # See the License for the specific language governing permissions and ft@22: # limitations under the License. ft@22: # ------------------------------------------------------------ ft@22: ft@22: import ConfigParser ft@22: ft@22: import sys ft@22: ft@22: import logging ft@22: import os ft@22: import errno ft@22: import time ft@22: ft@22: import pyclamav ft@22: ft@22: ft@22: class ClamAVScanner: ft@22: ft@22: # User the existing logger instance ft@22: __LOG = logging.getLogger("IkarusScanner") ft@22: ft@22: __MINOPTS = { "Main" : ["Nothing"]} ft@22: __CONFIG_NOT_READABLE = "Configfile is not readable" ft@22: __CONFIG_WRONG = "Something is wrong with the config" ft@22: __CONFIG_MISSING = "Section: \"%s\" Option: \"%s\" in configfile is missing" ft@22: ft@22: ft@22: ft@22: def __init__ (self, scanner_config_path): ft@22: config = self.loadConfig (scanner_config_path) ft@22: ft@22: ft@22: ft@22: def checkMinimumOptions (self, config): ft@22: for section, options in self.__MINOPTS.iteritems (): ft@22: for option in options: ft@22: if (config.has_option(section, option) == False): ft@22: self.__LOG.error (self.__CONFIG_MISSING % (section, option)) ft@22: exit (129) ft@22: ft@22: def loadConfig (self, scanner_config_path): ft@22: ft@22: configfile = scanner_config_path ft@22: config = ConfigParser.SafeConfigParser () ft@22: ft@22: 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@22: self.__LOG.error(self.__CONFIG_NOT_READABLE); ft@22: raise SystemError(self.__CONFIG_NOT_READABLE) ft@22: ft@22: try: ft@22: config.read (scanner_config_path) ft@22: except Exception, e: ft@22: self.__LOG.error("Error: %s" % (e)); ft@22: raise SystemError("Error: %s" % (e)) ft@22: ft@22: self.checkMinimumOptions (config) ft@22: ft@22: return config ft@22: ft@22: ft@22: def scanFile (self, path, fileobject): ft@22: return self.scanFileClamAV (path) ft@22: ft@22: def scanFileClamAV (self, path): ft@22: retval = { "infected" : False, "virusname" : "Unknown" } ft@22: ft@22: self.__LOG.debug ("Scan File: %s" % (path)) ft@22: ft@22: result = pyclamav.scanfile (path) ft@22: self.__LOG.debug ("Result of file \"%s\": %s" % (path, result)) ft@22: if (result[0] != 0): ft@22: retval["infected"] = True ft@22: retval["virusname"] = result[1] ft@22: ft@22: if (retval["infected"] == True): ft@22: self.__LOG.error ("Virus found, deny Access %s" % (result,)) ft@22: ft@22: return retval ft@22: