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
     1 #!/usr/bin/python
     2 
     3 # ------------------------------------------------------------
     4 # opensecurity package file
     5 #
     6 # Autor: X-Net Services GmbH <office@x-net.at>
     7 #
     8 # Copyright 2013-2014 X-Net and AIT Austrian Institute of Technology
     9 #
    10 #
    11 #     X-Net Technologies GmbH
    12 #     Elisabethstrasse 1
    13 #     4020 Linz
    14 #     AUSTRIA
    15 #     https://www.x-net.at
    16 #
    17 #     AIT Austrian Institute of Technology
    18 #     Donau City Strasse 1
    19 #     1220 Wien
    20 #     AUSTRIA
    21 #     http://www.ait.ac.at
    22 #
    23 #
    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
    27 #
    28 #    http://www.apache.org/licenses/LICENSE-2.0
    29 #
    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 # ------------------------------------------------------------
    36 
    37 import ConfigParser
    38 
    39 import sys
    40 
    41 import logging
    42 import os
    43 import errno
    44 import time
    45 
    46 import pyclamav
    47 
    48 
    49 class ClamAVScanner:
    50     
    51     # User the existing logger  instance
    52     __LOG = logging.getLogger("IkarusScanner")
    53     
    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"
    58     
    59 
    60     
    61     def __init__ (self, scanner_config_path):
    62         config = self.loadConfig (scanner_config_path)
    63 
    64     
    65 
    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))
    71                     exit (129)
    72 
    73     def loadConfig (self, scanner_config_path):
    74 
    75         configfile = scanner_config_path
    76         config = ConfigParser.SafeConfigParser ()
    77     
    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)
    81     
    82         try:
    83             config.read (scanner_config_path)
    84         except Exception, e:
    85             self.__LOG.error("Error: %s" % (e));
    86             raise SystemError("Error: %s" % (e))
    87 
    88         self.checkMinimumOptions (config)
    89     
    90         return config
    91 
    92     
    93     def scanFile (self, path, fileobject):
    94         return self.scanFileClamAV (path)
    95     
    96     def scanFileClamAV (self, path):        
    97         retval = { "infected" : False, "virusname" : "Unknown" }
    98     
    99         self.__LOG.debug ("Scan File: %s" % (path))
   100     
   101         result = pyclamav.scanfile (path)
   102         self.__LOG.debug ("Result of file \"%s\": %s" % (path, result))
   103         if (result[0] != 0):
   104             retval["infected"] = True
   105             retval["virusname"] = result[1]
   106     
   107         if (retval["infected"] == True):
   108             self.__LOG.error ("Virus found, deny Access %s" % (result,))
   109     
   110         return retval
   111