src/OsecFS.py
changeset 5 5b7c05fc9a5e
parent 4 114537186d9e
child 6 b4b18827d89d
     1.1 --- a/src/OsecFS.py	Tue Dec 03 14:53:22 2013 +0100
     1.2 +++ b/src/OsecFS.py	Wed Dec 04 15:19:15 2013 +0100
     1.3 @@ -15,7 +15,7 @@
     1.4  #import pyclamav
     1.5  import subprocess
     1.6  
     1.7 -import requests
     1.8 +import urllib3
     1.9  
    1.10  
    1.11  MINOPTS = { "Main" : ["Logfile", "Mountpoint", "Rootpath", "LocalScanserverURL", "RemoteScanserverURL", "ReadOnly"]}
    1.12 @@ -30,8 +30,12 @@
    1.13  STATUS_CODE_INFECTED = 210
    1.14  STATUS_CODE_NOT_FOUND = 404
    1.15  
    1.16 +MAX_SCAN_FILE_SIZE = 50 * 0x100000
    1.17 +
    1.18  SYSTEM_FILE_COMMAND = "file"
    1.19  
    1.20 +# Global http pool manager used to connect to the scan server
    1.21 +httpPool = urllib3.PoolManager()
    1.22  
    1.23  def checkMinimumOptions (config):
    1.24      for section, options in MINOPTS.iteritems ():
    1.25 @@ -110,37 +114,29 @@
    1.26  def scanFileIkarus (path, fileobject):
    1.27      infected = False
    1.28      LOG.debug ("Scan File: %s" % (path))
    1.29 -    
    1.30 -    files = {'up_file': (path, fileobject)}
    1.31 -    
    1.32 -    try:
    1.33 -        #TODO: change to remote server
    1.34 -        r = requests.post(LOCAL_SCANSERVER_URL, files=files)
    1.35 -    except requests.exceptions.ConnectionError:
    1.36 -        #LOG.info("Remote scan server unreachable, using local scan server.")
    1.37  
    1.38 -        # TODO:
    1.39 -        # Here the local scan server should be contacted.
    1.40 -        # The requests package does not upload content in the second post request,
    1.41 -        # so no fallback server can be used right now (bug?)
    1.42 -        # I did not a find a solution yet, maybe another http package has to be used.
    1.43 -        # Disabled for now.
    1.44 -
    1.45 -        #try:
    1.46 -        #    r = requests.post(LOCAL_SCANSERVER_URL, files=files)
    1.47 -        #except requests.exceptions.ConnectionError:
    1.48 -        #    return 2
    1.49 -        LOG.error ("Connection to scan server could not be established.")
    1.50 -        return False
    1.51 -    except:
    1.52 -        LOG.error ("Something went wrong at scanning.")
    1.53 -        LOG.error ("Exception: %s" %(sys.exc_info()[0],))
    1.54 +    if (os.fstat(fileobject.fileno()).st_size > MAX_SCAN_FILE_SIZE):
    1.55 +        LOG.info("File max size exceeded. The file is not scanned.")
    1.56          return False
    1.57  
    1.58 +    fields = { 'up_file' : (path, fileobject.read()) }
    1.59  
    1.60 -    if r.status_code == STATUS_CODE_OK:
    1.61 +    try:
    1.62 +        response = httpPool.request_encode_body('POST', REMOTE_SCANSERVER_URL, fields = fields)
    1.63 +        # We should catch socket.error here, but this does not work. Needs checking.
    1.64 +    except:
    1.65 +        LOG.info("Remote scan server unreachable, using local scan server.")
    1.66 +
    1.67 +        try:
    1.68 +            response = httpPool.request_encode_body('POST', LOCAL_SCANSERVER_URL, fields = fields)
    1.69 +        except:
    1.70 +            LOG.error ("Connection to local scan server could not be established.")
    1.71 +            LOG.error ("Exception: %s" %(sys.exc_info()[0]))
    1.72 +            return False
    1.73 +
    1.74 +    if response.status == STATUS_CODE_OK:
    1.75          infected = False
    1.76 -    elif r.status_code == STATUS_CODE_INFECTED:
    1.77 +    elif response.status == STATUS_CODE_INFECTED:
    1.78          # Parse xml for info if desired
    1.79          #contentXML = r.content
    1.80          #root = ET.fromstring(contentXML)
    1.81 @@ -350,6 +346,9 @@
    1.82      LOCAL_SCANSERVER_URL = config.get("Main", "LocalScanserverURL")
    1.83      REMOTE_SCANSERVER_URL = config.get("Main", "RemoteScanserverURL")
    1.84  
    1.85 +    # Convert file size from MB to byte
    1.86 +    MAX_SCAN_FILE_SIZE = int(config.get("Main", "MaxFileSize")) * 0x100000
    1.87 +
    1.88      osecfs = OsecFS (config.get ("Main", "Rootpath"))
    1.89      osecfs.flags = 0
    1.90      osecfs.multithreaded = 0