src/OsecFS.py
author ft
Wed, 09 Apr 2014 10:27:19 +0200
changeset 14 74a3519ac9b3
parent 13 df3d231adebb
child 15 0b4d2bf9d306
permissions -rwxr-xr-x
Fixed windows append isse
removed useless modes
     1 #!/usr/bin/python
     2 
     3 from fuse import Fuse
     4 import fuse
     5 
     6 import ConfigParser
     7 
     8 import sys
     9 
    10 import logging
    11 import os
    12 import errno
    13 import time
    14 
    15 from importlib import import_module
    16 
    17 
    18 import subprocess
    19 
    20 import urllib3
    21 import urllib
    22 import netifaces
    23 import netaddr
    24 
    25 
    26 sys.stderr = open('/var/log/osecfs_error.log', 'a+')
    27 
    28 
    29 MINOPTS = { "Main" : ["Logfile", "LogLevel", "Mountpoint", "Rootpath", "ScannerPath", "ScannerModuleName", "ScannerClassName", "ScannerConfig", "ReadOnly"]}
    30 
    31 CONFIG_NOT_READABLE = "Configfile is not readable"
    32 CONFIG_WRONG = "Something is wrong with the config"
    33 CONFIG_MISSING = "Section: \"%s\" Option: \"%s\" in configfile is missing"
    34 SCAN_WRONG_RETURN_VALUE = "The return Value of the malware scanner is wrong. Has to be an dictionary"
    35 SCAN_RETURN_VALUE_KEY_MISSING = "The dictionary has to include key \"infected\" (True, False) and \"virusname\" (String)"
    36 VIRUS_FOUND = "Virus found. Access denied"
    37 NOTIFICATION_CRITICAL = "critical"
    38 NOTIFICATION_INFO = "info"
    39 LOG = None
    40 MalwareScanner = None
    41 STATUS_CODE_OK = 200
    42 
    43 SYSTEM_FILE_COMMAND = "file"
    44 httpPool = urllib3.PoolManager(num_pools = 1, timeout = 3)
    45 
    46 def checkMinimumOptions (config):
    47     for section, options in MINOPTS.iteritems ():
    48         for option in options:
    49             if (config.has_option(section, option) == False):
    50                 print (CONFIG_MISSING % (section, option))
    51                 exit (129)
    52 
    53 def printUsage ():
    54     print ("Usage:")
    55     print ("%s configfile mountpath ro/rw" % (sys.argv[0]))
    56     exit (128)
    57 
    58 def loadConfig ():
    59     print ("load config")
    60 
    61     if (len (sys.argv) < 4):
    62         printUsage ()
    63 
    64     configfile = sys.argv[1]
    65     config = ConfigParser.SafeConfigParser ()
    66 
    67     if ((os.path.exists (configfile) == False) or (os.path.isfile (configfile) == False) or (os.access (configfile, os.R_OK) == False)):
    68         print (CONFIG_NOT_READABLE)
    69         printUsage ()
    70 
    71     try:
    72         config.read (sys.argv[1])
    73     except Exception, e:
    74         print (CONFIG_WRONG)
    75         print ("Error: %s" % (e))
    76 
    77 
    78     config.set("Main", "Mountpoint", sys.argv[2])
    79     if (sys.argv[3] == "rw"):
    80         config.set("Main", "ReadOnly", "false")
    81     else:
    82         config.set("Main", "ReadOnly", "true")
    83 
    84     checkMinimumOptions (config)
    85 
    86     return config
    87 
    88 def initLog (config):
    89     print ("init log")
    90 
    91     global LOG
    92     logfile = config.get("Main", "Logfile")
    93     
    94     numeric_level = getattr(logging, config.get("Main", "LogLevel").upper(), None)
    95     if not isinstance(numeric_level, int):
    96         raise ValueError('Invalid log level: %s' % loglevel)
    97 
    98     # ToDo move log level and maybe other things to config file
    99     logging.basicConfig(
   100                         level = numeric_level,
   101                         format = "%(asctime)s %(name)-12s %(funcName)-15s %(levelname)-8s %(message)s",
   102                         datefmt = "%Y-%m-%d %H:%M:%S",
   103                         filename = logfile,
   104                         filemode = "a+",
   105     )
   106     LOG = logging.getLogger("fuse_main")
   107 
   108 
   109 def fixPath (path):
   110     return ".%s" % (path)
   111 
   112 def rootPath (rootpath, path):
   113     return "%s%s" % (rootpath, path)
   114 
   115 def flag2mode (flags):
   116     md = {os.O_RDONLY: 'r', os.O_WRONLY: 'w', os.O_RDWR: 'w+'}
   117     m = md[flags & (os.O_RDONLY | os.O_WRONLY | os.O_RDWR)]
   118 
   119     # windows sets append even if it would overwrite the whole file (seek 0)
   120     # so ignore append option
   121     #if flags | os.O_APPEND:
   122     #    m = m.replace('w', 'a', 1)
   123 
   124     return m
   125 
   126 def scanFile (path, fileobject):
   127     LOG.debug ("Scan File \"%s\" with malware Scanner" %(path,) )
   128     return MalwareScanner.scanFile (path, fileobject)
   129 
   130 
   131 def scanFileClamAV (path):
   132     infected = False
   133 
   134     LOG.debug ("Scan File: %s" % (path))
   135 
   136     result = pyclamav.scanfile (path)
   137     LOG.debug ("Result of file \"%s\": %s" % (path, result))
   138     if (result[0] != 0):
   139         infected = True
   140 
   141     if (infected == True):
   142         LOG.error ("Virus found, deny Access %s" % (result,))
   143 
   144     return infected
   145 
   146 def whitelistFile (path):
   147     whitelisted = False;
   148 
   149     LOG.debug ("Execute \"%s\" command on \"%s\"" %(SYSTEM_FILE_COMMAND, path))
   150     
   151     result = None
   152     try:
   153         result = subprocess.check_output ([SYSTEM_FILE_COMMAND, path]);
   154         # ToDo replace with real whitelist
   155         whitelisted = True
   156     except Exception as e:
   157         LOG.error ("Call returns with an error!")
   158         LOG.error (e)
   159 
   160     LOG.debug ("Type: %s" %(result))
   161 
   162     return whitelisted
   163 
   164 def sendNotification (type, message):
   165     netifaces.ifaddresses("eth0")[2][0]["addr"]
   166     
   167     # Get first address in network (0 = network ip -> 192.168.0.0)
   168     remote_ip = netaddr.IPNetwork("%s/%s" %(netifaces.ifaddresses("eth0")[2][0]["addr"], netifaces.ifaddresses("eth0")[2][0]["netmask"]))[1]
   169     
   170     url_options = {"type" : type, "message" : message }
   171     
   172     # BUG in urllib3. Starting / is missing -> workarround use 2 of them -.- 
   173     url = ("http://%s:8090//notification?%s" %(remote_ip, urllib.urlencode(url_options)))
   174     
   175     LOG.debug ("Send notification to \"%s\"" %(url, ))
   176     
   177     try:
   178         #response = httpPool.request_encode_body('GET', url, retries = 0)
   179         response = httpPool.request("GET", url, retries = 0)
   180     except:
   181         LOG.error("Remote host not reachable")
   182         LOG.error ("Exception: %s" %(sys.exc_info()[0]))
   183         return
   184     
   185     if response.status == STATUS_CODE_OK:
   186         LOG.info("Notification sent successfully")
   187     else:
   188         LOG.error("Server returned errorcode: %s" %(response.status,))
   189 
   190 def sendReadOnlyNotification():
   191     sendNotification("critical", "Filesystem is in read only mode. If you want to export files please initialize an encrypted filesystem.")
   192 
   193 class OsecFS (Fuse):
   194 
   195     __rootpath = None
   196 
   197     # default fuse init
   198     def __init__(self, rootpath, *args, **kw):
   199         self.__rootpath = rootpath
   200         Fuse.__init__ (self, *args, **kw)
   201         LOG.debug ("Init complete.")
   202         sendNotification("information", "Filesystem successfully mounted.")
   203 
   204     # defines that our working directory will be the __rootpath
   205     def fsinit(self):
   206         os.chdir (self.__rootpath)
   207 
   208     def getattr(self, path):
   209         LOG.debug ("*** getattr (%s)" % (fixPath (path)))
   210         return os.lstat (fixPath (path));
   211 
   212     def getdir(self, path):
   213         LOG.debug ("*** getdir (%s)" % (path));
   214         return os.listdir (fixPath (path))
   215 
   216     def readdir(self, path, offset):
   217         LOG.debug ("*** readdir (%s %s)" % (path, offset));
   218         for e in os.listdir (fixPath (path)):
   219             yield fuse.Direntry(e)
   220 
   221     def chmod (self, path, mode):
   222         LOG.debug ("*** chmod %s %s" % (path, oct(mode)))
   223         if (config.get("Main", "ReadOnly") == "true"):
   224             sendReadOnlyNotification()
   225             return -errno.EACCES
   226         os.chmod (fixPath (path), mode)
   227 
   228     def chown (self, path, uid, gid):
   229         LOG.debug ("*** chown %s %s %s" % (path, uid, gid))
   230         if (config.get("Main", "ReadOnly") == "true"):
   231             sendReadOnlyNotification()
   232             return -errno.EACCES
   233         os.chown (fixPath (path), uid, gid)
   234 
   235     def link (self, targetPath, linkPath):
   236         LOG.debug ("*** link %s %s" % (targetPath, linkPath))
   237         if (config.get("Main", "ReadOnly") == "true"):
   238             sendReadOnlyNotification()
   239             return -errno.EACCES
   240         os.link (fixPath (targetPath), fixPath (linkPath))
   241 
   242     def mkdir (self, path, mode):
   243         LOG.debug ("*** mkdir %s %s" % (path, oct(mode)))
   244         if (config.get("Main", "ReadOnly") == "true"):
   245             sendReadOnlyNotification()
   246             return -errno.EACCES
   247         os.mkdir (fixPath (path), mode)
   248 
   249     def mknod (self, path, mode, dev):
   250         LOG.debug ("*** mknod %s %s %s" % (path, oct (mode), dev))
   251         if (config.get("Main", "ReadOnly") == "true"):
   252             sendReadOnlyNotification()
   253             return -errno.EACCES
   254         os.mknod (fixPath (path), mode, dev)
   255 
   256     # to implement virus scan
   257     def open (self, path, flags):
   258         LOG.debug ("*** open %s %s" % (path, oct (flags)))
   259         self.file = os.fdopen (os.open (fixPath (path), flags), flag2mode (flags))
   260         self.fd = self.file.fileno ()
   261         
   262         LOG.debug(self.__rootpath)
   263         LOG.debug(path)
   264         
   265         retval = scanFile (rootPath(self.__rootpath, path), self.file)
   266         
   267         #if type(retval) is not dict:
   268         if (isinstance(retval, dict) == False):
   269             LOG.error(SCAN_WRONG_RETURN_VALUE)
   270             self.file.close ()
   271             return -errno.EACCES
   272         
   273         if ((retval.has_key("infected") == False) or (retval.has_key("virusname") == False)):
   274             LOG.error(SCAN_RETURN_VALUE_KEY_MISSING)
   275             self.file.close ()
   276             return -errno.EACCES
   277             
   278         
   279         if (retval.get("infected") == True):
   280             self.file.close ()
   281             sendNotification(NOTIFICATION_CRITICAL, "%s\nFile: %s\nVirus: %s" %(VIRUS_FOUND, path, retval.get("virusname")))
   282             LOG.error("%s" %(VIRUS_FOUND,))
   283             LOG.error("Virus: %s" %(retval.get("virusname"),))
   284             return -errno.EACCES
   285         
   286         whitelisted = whitelistFile (rootPath(self.__rootpath, path))
   287         if (whitelisted == False):
   288             self.file.close ()
   289             sendNotification(NOTIFICATION_CRITICAL, "File not in whitelist. Access denied.")
   290             return -errno.EACCES
   291 
   292     def read (self, path, length, offset):
   293         LOG.debug ("*** read %s %s %s" % (path, length, offset))
   294         self.file.seek (offset)
   295         return self.file.read (length)
   296 
   297     def readlink (self, path):
   298         LOG.debug ("*** readlink %s" % (path))
   299         return os.readlink (fixPath (path))
   300 
   301     def release (self, path, flags):
   302         LOG.debug ("*** release %s %s" % (path, oct (flags)))
   303         self.file.close ()
   304 
   305     def rename (self, oldPath, newPath):
   306         LOG.debug ("*** rename %s %s %s" % (oldPath, newPath, config.get("Main", "ReadOnly")))
   307         if (config.get("Main", "ReadOnly") == "true"):
   308             sendReadOnlyNotification()
   309             return -errno.EACCES
   310         os.rename (fixPath (oldPath), fixPath (newPath))
   311 
   312     def rmdir (self, path):
   313         LOG.debug ("*** rmdir %s %s" % (path, config.get("Main", "ReadOnly")))
   314         if (config.get("Main", "ReadOnly") == "true"):
   315             sendReadOnlyNotification()
   316             return -errno.EACCES
   317         os.rmdir (fixPath (path))
   318 
   319     def statfs (self):
   320         LOG.debug ("*** statfs")
   321         return os.statvfs(".")
   322 
   323     def symlink (self, targetPath, linkPath):
   324         LOG.debug ("*** symlink %s %s %s" % (targetPath, linkPath, config.get("Main", "ReadOnly")))
   325         if (config.get("Main", "ReadOnly") == "true"):
   326             sendReadOnlyNotification()
   327             return -errno.EACCES
   328         os.symlink (fixPath (targetPath), fixPath (linkPath))
   329 
   330     def truncate (self, path, length):
   331         LOG.debug ("*** truncate %s %s %s" % (path, length, config.get("Main", "ReadOnly")))
   332         if (config.get("Main", "ReadOnly") == "true"):
   333             sendReadOnlyNotification()
   334             return -errno.EACCES
   335         f = open (fixPath (path), "w+")
   336         f.truncate (length)
   337         f.close ()
   338 
   339     def unlink (self, path):
   340         LOG.debug ("*** unlink %s %s" % (path, config.get("Main", "ReadOnly")))
   341         if (config.get("Main", "ReadOnly") == "true"):
   342             sendReadOnlyNotification()
   343             return -errno.EACCES
   344         os.unlink (fixPath (path))
   345 
   346     def utime (self, path, times):
   347         LOG.debug ("*** utime %s %s" % (path, times))
   348         os.utime (fixPath (path), times)
   349 
   350     def write (self, path, buf, offset):
   351         LOG.debug ("*** write %s %s %s %s" % (path, buf, offset, config.get("Main", "ReadOnly")))
   352         if (config.get("Main", "ReadOnly") == "true"):
   353             self.file.close()
   354             sendReadOnlyNotification()
   355             return -errno.EACCES
   356         self.file.seek (offset)
   357         self.file.write (buf)
   358         return len (buf)
   359 
   360     def access (self, path, mode):
   361         LOG.debug ("*** access %s %s" % (path, oct (mode)))
   362         if not os.access (fixPath (path), mode):
   363             return -errno.EACCES
   364 
   365     def create (self, path, flags, mode):
   366         LOG.debug ("*** create %s %s %s %s %s" % (fixPath (path), oct (flags), oct (mode), flag2mode (flags), config.get("Main", "ReadOnly")))
   367         if (config.get("Main", "ReadOnly") == "true"):
   368             sendReadOnlyNotification()
   369             return -errno.EACCES
   370         #self.file = os.fdopen (os.open (fixPath (path), flags, mode), flag2mode (flags))
   371         # fix strange Windows behaviour
   372         self.file = os.fdopen (os.open (fixPath (path), flags, mode), "w+")
   373         self.fd = self.file.fileno ()
   374 
   375 
   376 if __name__ == "__main__":
   377     # Set api version
   378     fuse.fuse_python_api = (0, 2)
   379     fuse.feature_assert ('stateful_files', 'has_init')
   380 
   381     config = loadConfig ()
   382     initLog (config)
   383     
   384     #sendNotification("Info", "OsecFS started")
   385     
   386     # Import the Malware Scanner
   387     sys.path.append(config.get("Main", "ScannerPath"))
   388     
   389     MalwareModule = import_module(config.get("Main", "ScannerModuleName"))
   390     MalwareClass = getattr(MalwareModule, config.get("Main", "ScannerClassName"))
   391     
   392     MalwareScanner = MalwareClass (config.get("Main", "ScannerConfig"));
   393     
   394     osecfs = OsecFS (config.get ("Main", "Rootpath"))
   395     osecfs.flags = 0
   396     osecfs.multithreaded = 0
   397 
   398     fuse_args = [sys.argv[0], config.get ("Main", "Mountpoint")];
   399     osecfs.main (fuse_args)