clamavscanner/ClamAVScanner.py
author ft
Tue, 04 Nov 2014 17:44:32 +0100
changeset 0 2342e6cefd65
permissions -rwxr-xr-x
initial commit of osecfs-deb
ft@0
     1
#!/usr/bin/python
ft@0
     2
ft@0
     3
# ------------------------------------------------------------
ft@0
     4
# opensecurity package file
ft@0
     5
#
ft@0
     6
# Autor: X-Net Services GmbH <office@x-net.at>
ft@0
     7
#
ft@0
     8
# Copyright 2013-2014 X-Net and AIT Austrian Institute of Technology
ft@0
     9
#
ft@0
    10
#
ft@0
    11
#     X-Net Technologies GmbH
ft@0
    12
#     Elisabethstrasse 1
ft@0
    13
#     4020 Linz
ft@0
    14
#     AUSTRIA
ft@0
    15
#     https://www.x-net.at
ft@0
    16
#
ft@0
    17
#     AIT Austrian Institute of Technology
ft@0
    18
#     Donau City Strasse 1
ft@0
    19
#     1220 Wien
ft@0
    20
#     AUSTRIA
ft@0
    21
#     http://www.ait.ac.at
ft@0
    22
#
ft@0
    23
#
ft@0
    24
# Licensed under the Apache License, Version 2.0 (the "License");
ft@0
    25
# you may not use this file except in compliance with the License.
ft@0
    26
# You may obtain a copy of the License at
ft@0
    27
#
ft@0
    28
#    http://www.apache.org/licenses/LICENSE-2.0
ft@0
    29
#
ft@0
    30
# Unless required by applicable law or agreed to in writing, software
ft@0
    31
# distributed under the License is distributed on an "AS IS" BASIS,
ft@0
    32
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ft@0
    33
# See the License for the specific language governing permissions and
ft@0
    34
# limitations under the License.
ft@0
    35
# ------------------------------------------------------------
ft@0
    36
ft@0
    37
import ConfigParser
ft@0
    38
ft@0
    39
import sys
ft@0
    40
ft@0
    41
import logging
ft@0
    42
import os
ft@0
    43
import errno
ft@0
    44
import time
ft@0
    45
ft@0
    46
import pyclamav
ft@0
    47
ft@0
    48
ft@0
    49
class ClamAVScanner:
ft@0
    50
    
ft@0
    51
    # User the existing logger  instance
ft@0
    52
    __LOG = logging.getLogger("IkarusScanner")
ft@0
    53
    
ft@0
    54
    __MINOPTS = { "Main" : ["Nothing"]}
ft@0
    55
    __CONFIG_NOT_READABLE = "Configfile is not readable"
ft@0
    56
    __CONFIG_WRONG = "Something is wrong with the config"
ft@0
    57
    __CONFIG_MISSING = "Section: \"%s\" Option: \"%s\" in configfile is missing"
ft@0
    58
    
ft@0
    59
ft@0
    60
    
ft@0
    61
    def __init__ (self, scanner_config_path):
ft@0
    62
        config = self.loadConfig (scanner_config_path)
ft@0
    63
ft@0
    64
    
ft@0
    65
ft@0
    66
    def checkMinimumOptions (self, config):
ft@0
    67
        for section, options in self.__MINOPTS.iteritems ():
ft@0
    68
            for option in options:
ft@0
    69
                if (config.has_option(section, option) == False):
ft@0
    70
                    self.__LOG.error (self.__CONFIG_MISSING % (section, option))
ft@0
    71
                    exit (129)
ft@0
    72
ft@0
    73
    def loadConfig (self, scanner_config_path):
ft@0
    74
ft@0
    75
        configfile = scanner_config_path
ft@0
    76
        config = ConfigParser.SafeConfigParser ()
ft@0
    77
    
ft@0
    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)):
ft@0
    79
            self.__LOG.error(self.__CONFIG_NOT_READABLE);
ft@0
    80
            raise SystemError(self.__CONFIG_NOT_READABLE)
ft@0
    81
    
ft@0
    82
        try:
ft@0
    83
            config.read (scanner_config_path)
ft@0
    84
        except Exception, e:
ft@0
    85
            self.__LOG.error("Error: %s" % (e));
ft@0
    86
            raise SystemError("Error: %s" % (e))
ft@0
    87
ft@0
    88
        self.checkMinimumOptions (config)
ft@0
    89
    
ft@0
    90
        return config
ft@0
    91
ft@0
    92
    
ft@0
    93
    def scanFile (self, path, fileobject):
ft@0
    94
        return self.scanFileClamAV (path)
ft@0
    95
    
ft@0
    96
    def scanFileClamAV (self, path):        
ft@0
    97
        retval = { "infected" : False, "virusname" : "Unknown" }
ft@0
    98
    
ft@0
    99
        self.__LOG.debug ("Scan File: %s" % (path))
ft@0
   100
    
ft@0
   101
        result = pyclamav.scanfile (path)
ft@0
   102
        self.__LOG.debug ("Result of file \"%s\": %s" % (path, result))
ft@0
   103
        if (result[0] != 0):
ft@0
   104
            retval["infected"] = True
ft@0
   105
            retval["virusname"] = result[1]
ft@0
   106
    
ft@0
   107
        if (retval["infected"] == True):
ft@0
   108
            self.__LOG.error ("Virus found, deny Access %s" % (result,))
ft@0
   109
    
ft@0
   110
        return retval
ft@0
   111