# HG changeset patch # User ft # Date 1400149294 -7200 # Node ID 0b4d2bf9d306f955c0f6fd6d3b292fc276436bb3 # Parent 74a3519ac9b340c3b3be35e7cdf89178497c2b77 Added md5 calculation written files will now be logged diff -r 74a3519ac9b3 -r 0b4d2bf9d306 src/OsecFS.py --- a/src/OsecFS.py Wed Apr 09 10:27:19 2014 +0200 +++ b/src/OsecFS.py Thu May 15 12:21:34 2014 +0200 @@ -18,9 +18,9 @@ import subprocess import urllib3 -import urllib import netifaces import netaddr +import hashlib sys.stderr = open('/var/log/osecfs_error.log', 'a+') @@ -161,34 +161,56 @@ return whitelisted -def sendNotification (type, message): +def sendDataToRest (urlpath, data): netifaces.ifaddresses("eth0")[2][0]["addr"] # Get first address in network (0 = network ip -> 192.168.0.0) remote_ip = netaddr.IPNetwork("%s/%s" %(netifaces.ifaddresses("eth0")[2][0]["addr"], netifaces.ifaddresses("eth0")[2][0]["netmask"]))[1] - url_options = {"type" : type, "message" : message } - - # BUG in urllib3. Starting / is missing -> workarround use 2 of them -.- - url = ("http://%s:8090//notification?%s" %(remote_ip, urllib.urlencode(url_options))) - - LOG.debug ("Send notification to \"%s\"" %(url, )) + url = ("http://%s:8090//%s" %(remote_ip, urlpath)) + + LOG.debug ("Send data to \"%s\"" %(url, )) + LOG.debug ("Data: %s" %(data, )) try: - #response = httpPool.request_encode_body('GET', url, retries = 0) - response = httpPool.request("GET", url, retries = 0) + response = httpPool.request_encode_body("POST", url, fields=data, retries=0) except: LOG.error("Remote host not reachable") LOG.error ("Exception: %s" %(sys.exc_info()[0])) return if response.status == STATUS_CODE_OK: - LOG.info("Notification sent successfully") + LOG.info("Data sent successfully to rest server") + return True else: LOG.error("Server returned errorcode: %s" %(response.status,)) + return False + + +def sendNotification (type, message): + data = {"type" : type, "message" : message} + sendDataToRest ("notification", data) def sendReadOnlyNotification(): sendNotification("critical", "Filesystem is in read only mode. If you want to export files please initialize an encrypted filesystem.") + +def sendLogNotPossibleNotification(): + sendNotification ("critical", "Send log entry to opensecurity rest server failed.") + +def sendFileLog(filename, filesize, filehash, hashtype): + data = {"filename" : filename, "filesize" : filesize, "filehash" : filehash, "hashtype" : hashtype} + retval = sendDataToRest ("log", data) + if (retval == False): + sendLogNotPossibleNotification() + +def calcMD5 (path, block_size=256*128, hr=True): + md5 = hashlib.md5() + with open(path,'rb') as f: + for chunk in iter(lambda: f.read(block_size), b''): + md5.update(chunk) + if hr: + return md5.hexdigest() + return md5.digest() class OsecFS (Fuse): @@ -257,6 +279,7 @@ def open (self, path, flags): LOG.debug ("*** open %s %s" % (path, oct (flags))) self.file = os.fdopen (os.open (fixPath (path), flags), flag2mode (flags)) + self.written = False self.fd = self.file.fileno () LOG.debug(self.__rootpath) @@ -301,6 +324,12 @@ def release (self, path, flags): LOG.debug ("*** release %s %s" % (path, oct (flags))) self.file.close () + + if (self.written == True): + hashsum = calcMD5(fixPath(path)) + filesize = os.path.getsize(fixPath(path)) + sendFileLog(path, filesize, hashsum, "md5") + def rename (self, oldPath, newPath): LOG.debug ("*** rename %s %s %s" % (oldPath, newPath, config.get("Main", "ReadOnly"))) @@ -348,13 +377,15 @@ os.utime (fixPath (path), times) def write (self, path, buf, offset): - LOG.debug ("*** write %s %s %s %s" % (path, buf, offset, config.get("Main", "ReadOnly"))) + #LOG.debug ("*** write %s %s %s %s" % (path, buf, offset, config.get("Main", "ReadOnly"))) + LOG.debug ("*** write %s %s %s %s" % (path, "filecontent", offset, config.get("Main", "ReadOnly"))) if (config.get("Main", "ReadOnly") == "true"): self.file.close() sendReadOnlyNotification() return -errno.EACCES self.file.seek (offset) self.file.write (buf) + self.written = True return len (buf) def access (self, path, mode): @@ -367,9 +398,9 @@ if (config.get("Main", "ReadOnly") == "true"): sendReadOnlyNotification() return -errno.EACCES - #self.file = os.fdopen (os.open (fixPath (path), flags, mode), flag2mode (flags)) - # fix strange Windows behaviour - self.file = os.fdopen (os.open (fixPath (path), flags, mode), "w+") + + self.file = os.fdopen (os.open (fixPath (path), flags), flag2mode(flags)) + self.written = True self.fd = self.file.fileno ()