clamavscanner/ClamAVScanner.py
changeset 0 2342e6cefd65
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/clamavscanner/ClamAVScanner.py	Tue Nov 04 17:44:32 2014 +0100
     1.3 @@ -0,0 +1,111 @@
     1.4 +#!/usr/bin/python
     1.5 +
     1.6 +# ------------------------------------------------------------
     1.7 +# opensecurity package file
     1.8 +#
     1.9 +# Autor: X-Net Services GmbH <office@x-net.at>
    1.10 +#
    1.11 +# Copyright 2013-2014 X-Net and AIT Austrian Institute of Technology
    1.12 +#
    1.13 +#
    1.14 +#     X-Net Technologies GmbH
    1.15 +#     Elisabethstrasse 1
    1.16 +#     4020 Linz
    1.17 +#     AUSTRIA
    1.18 +#     https://www.x-net.at
    1.19 +#
    1.20 +#     AIT Austrian Institute of Technology
    1.21 +#     Donau City Strasse 1
    1.22 +#     1220 Wien
    1.23 +#     AUSTRIA
    1.24 +#     http://www.ait.ac.at
    1.25 +#
    1.26 +#
    1.27 +# Licensed under the Apache License, Version 2.0 (the "License");
    1.28 +# you may not use this file except in compliance with the License.
    1.29 +# You may obtain a copy of the License at
    1.30 +#
    1.31 +#    http://www.apache.org/licenses/LICENSE-2.0
    1.32 +#
    1.33 +# Unless required by applicable law or agreed to in writing, software
    1.34 +# distributed under the License is distributed on an "AS IS" BASIS,
    1.35 +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    1.36 +# See the License for the specific language governing permissions and
    1.37 +# limitations under the License.
    1.38 +# ------------------------------------------------------------
    1.39 +
    1.40 +import ConfigParser
    1.41 +
    1.42 +import sys
    1.43 +
    1.44 +import logging
    1.45 +import os
    1.46 +import errno
    1.47 +import time
    1.48 +
    1.49 +import pyclamav
    1.50 +
    1.51 +
    1.52 +class ClamAVScanner:
    1.53 +    
    1.54 +    # User the existing logger  instance
    1.55 +    __LOG = logging.getLogger("IkarusScanner")
    1.56 +    
    1.57 +    __MINOPTS = { "Main" : ["Nothing"]}
    1.58 +    __CONFIG_NOT_READABLE = "Configfile is not readable"
    1.59 +    __CONFIG_WRONG = "Something is wrong with the config"
    1.60 +    __CONFIG_MISSING = "Section: \"%s\" Option: \"%s\" in configfile is missing"
    1.61 +    
    1.62 +
    1.63 +    
    1.64 +    def __init__ (self, scanner_config_path):
    1.65 +        config = self.loadConfig (scanner_config_path)
    1.66 +
    1.67 +    
    1.68 +
    1.69 +    def checkMinimumOptions (self, config):
    1.70 +        for section, options in self.__MINOPTS.iteritems ():
    1.71 +            for option in options:
    1.72 +                if (config.has_option(section, option) == False):
    1.73 +                    self.__LOG.error (self.__CONFIG_MISSING % (section, option))
    1.74 +                    exit (129)
    1.75 +
    1.76 +    def loadConfig (self, scanner_config_path):
    1.77 +
    1.78 +        configfile = scanner_config_path
    1.79 +        config = ConfigParser.SafeConfigParser ()
    1.80 +    
    1.81 +        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.82 +            self.__LOG.error(self.__CONFIG_NOT_READABLE);
    1.83 +            raise SystemError(self.__CONFIG_NOT_READABLE)
    1.84 +    
    1.85 +        try:
    1.86 +            config.read (scanner_config_path)
    1.87 +        except Exception, e:
    1.88 +            self.__LOG.error("Error: %s" % (e));
    1.89 +            raise SystemError("Error: %s" % (e))
    1.90 +
    1.91 +        self.checkMinimumOptions (config)
    1.92 +    
    1.93 +        return config
    1.94 +
    1.95 +    
    1.96 +    def scanFile (self, path, fileobject):
    1.97 +        return self.scanFileClamAV (path)
    1.98 +    
    1.99 +    def scanFileClamAV (self, path):        
   1.100 +        retval = { "infected" : False, "virusname" : "Unknown" }
   1.101 +    
   1.102 +        self.__LOG.debug ("Scan File: %s" % (path))
   1.103 +    
   1.104 +        result = pyclamav.scanfile (path)
   1.105 +        self.__LOG.debug ("Result of file \"%s\": %s" % (path, result))
   1.106 +        if (result[0] != 0):
   1.107 +            retval["infected"] = True
   1.108 +            retval["virusname"] = result[1]
   1.109 +    
   1.110 +        if (retval["infected"] == True):
   1.111 +            self.__LOG.error ("Virus found, deny Access %s" % (result,))
   1.112 +    
   1.113 +        return retval
   1.114 +