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