3 # ------------------------------------------------------------
4 # opensecurity package file
6 # Autor: X-Net Services GmbH <office@x-net.at>
8 # Copyright 2013-2014 X-Net and AIT Austrian Institute of Technology
11 # X-Net Technologies GmbH
15 # https://www.x-net.at
17 # AIT Austrian Institute of Technology
18 # Donau City Strasse 1
21 # http://www.ait.ac.at
24 # Licensed under the Apache License, Version 2.0 (the "License");
25 # you may not use this file except in compliance with the License.
26 # You may obtain a copy of the License at
28 # http://www.apache.org/licenses/LICENSE-2.0
30 # Unless required by applicable law or agreed to in writing, software
31 # distributed under the License is distributed on an "AS IS" BASIS,
32 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33 # See the License for the specific language governing permissions and
34 # limitations under the License.
35 # ------------------------------------------------------------
51 # User the existing logger instance
52 __LOG = logging.getLogger("IkarusScanner")
54 __MINOPTS = { "Main" : ["Nothing"]}
55 __CONFIG_NOT_READABLE = "Configfile is not readable"
56 __CONFIG_WRONG = "Something is wrong with the config"
57 __CONFIG_MISSING = "Section: \"%s\" Option: \"%s\" in configfile is missing"
61 def __init__ (self, scanner_config_path):
62 config = self.loadConfig (scanner_config_path)
66 def checkMinimumOptions (self, config):
67 for section, options in self.__MINOPTS.iteritems ():
68 for option in options:
69 if (config.has_option(section, option) == False):
70 self.__LOG.error (self.__CONFIG_MISSING % (section, option))
73 def loadConfig (self, scanner_config_path):
75 configfile = scanner_config_path
76 config = ConfigParser.SafeConfigParser ()
78 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)):
79 self.__LOG.error(self.__CONFIG_NOT_READABLE);
80 raise SystemError(self.__CONFIG_NOT_READABLE)
83 config.read (scanner_config_path)
85 self.__LOG.error("Error: %s" % (e));
86 raise SystemError("Error: %s" % (e))
88 self.checkMinimumOptions (config)
93 def scanFile (self, path, fileobject):
94 return self.scanFileClamAV (path)
96 def scanFileClamAV (self, path):
97 retval = { "infected" : False, "virusname" : "Unknown" }
99 self.__LOG.debug ("Scan File: %s" % (path))
101 result = pyclamav.scanfile (path)
102 self.__LOG.debug ("Result of file \"%s\": %s" % (path, result))
104 retval["infected"] = True
105 retval["virusname"] = result[1]
107 if (retval["infected"] == True):
108 self.__LOG.error ("Virus found, deny Access %s" % (result,))