src/OsecFS.py
changeset 7 e75bc05c279d
parent 6 b4b18827d89d
child 8 0a5ba0ef1058
     1.1 --- a/src/OsecFS.py	Wed Dec 04 15:36:45 2013 +0100
     1.2 +++ b/src/OsecFS.py	Fri Dec 06 13:15:45 2013 +0100
     1.3 @@ -10,6 +10,7 @@
     1.4  import logging
     1.5  import os
     1.6  import errno
     1.7 +import time
     1.8  
     1.9  # ToDo replace with ikarus
    1.10  #import pyclamav
    1.11 @@ -30,12 +31,15 @@
    1.12  STATUS_CODE_INFECTED = 210
    1.13  STATUS_CODE_NOT_FOUND = 404
    1.14  
    1.15 -MAX_SCAN_FILE_SIZE = 50 * 0x100000
    1.16 -
    1.17  SYSTEM_FILE_COMMAND = "file"
    1.18  
    1.19 +MAX_SCAN_FILE_SIZE = 50 * 0x100000
    1.20 +SCANSERVER_RETRY_TIMEOUT = 60
    1.21 +
    1.22  # Global http pool manager used to connect to the scan server
    1.23 -httpPool = urllib3.PoolManager()
    1.24 +remoteScanserverReachable = True
    1.25 +scanserverTimestamp = 0
    1.26 +httpPool = urllib3.PoolManager(num_pools = 1, timeout = 3)
    1.27  
    1.28  def checkMinimumOptions (config):
    1.29      for section, options in MINOPTS.iteritems ():
    1.30 @@ -110,8 +114,15 @@
    1.31          m = m.replace('w', 'a', 1)
    1.32  
    1.33      return m
    1.34 +    
    1.35 +def contactScanserver(url, fields):
    1.36 +    return httpPool.request_encode_body('POST', url, fields = fields, retries = 0)
    1.37 +    
    1.38  
    1.39  def scanFileIkarus (path, fileobject):
    1.40 +    global remoteScanserverReachable
    1.41 +    global scanserverTimestamp
    1.42 +
    1.43      infected = False
    1.44      LOG.debug ("Scan File: %s" % (path))
    1.45  
    1.46 @@ -121,18 +132,34 @@
    1.47  
    1.48      fields = { 'up_file' : fileobject.read() }
    1.49  
    1.50 -    try:
    1.51 -        response = httpPool.request_encode_body('POST', REMOTE_SCANSERVER_URL, fields = fields)
    1.52 -        # We should catch socket.error here, but this does not work. Needs checking.
    1.53 -    except:
    1.54 -        LOG.info("Remote scan server unreachable, using local scan server.")
    1.55 +    if (remoteScanserverReachable == False) and ((scanserverTimestamp + SCANSERVER_RETRY_TIMEOUT) < time.time()):
    1.56 +        remoteScanserverReachable = True
    1.57  
    1.58 +    if remoteScanserverReachable:
    1.59          try:
    1.60 -            response = httpPool.request_encode_body('POST', LOCAL_SCANSERVER_URL, fields = fields)
    1.61 +            response = contactScanserver(REMOTE_SCANSERVER_URL, fields)
    1.62 +            # We should catch socket.error here, but this does not work. Needs checking.
    1.63 +        except:
    1.64 +            LOG.info("Remote scan server unreachable, using local scan server.")
    1.65 +            LOG.info("Next check for remote server in %s seconds." % (SCANSERVER_RETRY_TIMEOUT))
    1.66 +            
    1.67 +            remoteScanserverReachable = False
    1.68 +            scanserverTimestamp = time.time()
    1.69 +
    1.70 +            try:
    1.71 +                response = contactScanserver(LOCAL_SCANSERVER_URL, fields)
    1.72 +            except:
    1.73 +                LOG.error ("Connection to local scan server could not be established.")
    1.74 +                LOG.error ("Exception: %s" %(sys.exc_info()[0]))
    1.75 +                return False
    1.76 +    else:
    1.77 +        try:
    1.78 +            response = contactScanserver(LOCAL_SCANSERVER_URL, fields)
    1.79          except:
    1.80              LOG.error ("Connection to local scan server could not be established.")
    1.81              LOG.error ("Exception: %s" %(sys.exc_info()[0]))
    1.82              return False
    1.83 +    
    1.84  
    1.85      if response.status == STATUS_CODE_OK:
    1.86          infected = False
    1.87 @@ -157,7 +184,6 @@
    1.88  
    1.89      LOG.debug ("Scan File: %s" % (path))
    1.90  
    1.91 -    # ToDo implement ikarus
    1.92      result = pyclamav.scanfile (path)
    1.93      LOG.debug ("Result of file \"%s\": %s" % (path, result))
    1.94      if (result[0] != 0):
    1.95 @@ -343,12 +369,15 @@
    1.96      config = loadConfig ()
    1.97      initLog (config)
    1.98  
    1.99 +    scanserverTimestamp = time.time()
   1.100 +
   1.101      LOCAL_SCANSERVER_URL = config.get("Main", "LocalScanserverURL")
   1.102      REMOTE_SCANSERVER_URL = config.get("Main", "RemoteScanserverURL")
   1.103 +    SCANSERVER_RETRY_TIMEOUT = int(config.get("Main", "RetryTimeout"))
   1.104  
   1.105      # Convert file size from MB to byte
   1.106      MAX_SCAN_FILE_SIZE = int(config.get("Main", "MaxFileSize")) * 0x100000
   1.107 -
   1.108 +    
   1.109      osecfs = OsecFS (config.get ("Main", "Rootpath"))
   1.110      osecfs.flags = 0
   1.111      osecfs.multithreaded = 0