From e6d36fa1fdf1ccc872f02842e084d3dd19a1cebf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torma=20Krist=C3=B3f?= Date: Sun, 18 Apr 2021 15:24:31 +0200 Subject: [PATCH 1/5] executor almost done --- server/executor.py | 62 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/server/executor.py b/server/executor.py index 63f77b6..3050f87 100644 --- a/server/executor.py +++ b/server/executor.py @@ -1,2 +1,64 @@ #!/usr/bin/env python3 +import os +import re + + +class Executor: + """This class executes commands recieved by the server""" + + def __init__(self, currentDiectory: str, baseDir: str = ""): + self.currentDirectory = currentDiectory + if baseDir == "": + self.baseDir = self.currentDirectory + else: + self.baseDir = baseDir + + def sanitizeDirectory(self, inDirectory: str) -> str: + return re.sub('[^a-zA-Z0-9]', '', inDirectory) + + def sanitizeFile(self, inFile: str) -> str: + return re.sub('[^a-zA-Z0-9].[^a-zA-Z0-9]', '', inFile) + + def createDirectory(self, dirName: str) -> str: + dirName = self.sanitizeDirectory(dirName) + actualDirName = self.currentDirectory + "/" + dirName + os.mkdir(actualDirName) + return actualDirName + + def removeDirectory(self, dirName: str) -> str: + dirName = self.sanitizeDirectory(dirName) + actualDirName = self.currentDirectory + "/" + dirName + os.rmdir(actualDirName) + return actualDirName + + def getCurrentDirectory(self) -> str: + return self.currentDirectory + + def setCurrentDirectory(self, dirName: str) -> str: + # TODO : Navigate up a directory structure + dirName = self.sanitizeDirectory(dirName) + self.currentDirectory = self.currentDirectory + "/" + dirName + return self.currentDirectory + + def listCurrentDirectoryContent(self) -> str: + contents = os.listdir(self.currentDirectory) + strdirectory = "" + for content in contents: + strdirectory += content + ", " + strdirectory = strdirectory[:-1] + return strdirectory + + def putFileInCurrentDirectory(self, filename: str, content) -> str: + pass + + def getFileInCurrentDirectory(self, file: str): + file = self.sanitizeFile(file) + currentfile = self.currentDirectory + file + return open(currentfile, "r") + + def removeFileInCurrentDirectory(self, file: str) -> str: + file = self.sanitizeFile(file) + currentfile = self.currentDirectory + file + os.remove(currentfile) + return currentfile From b8828d52b4c7eabb2c0240334d8efb4135fb6655 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torma=20Krist=C3=B3f?= Date: Sun, 18 Apr 2021 15:34:00 +0200 Subject: [PATCH 2/5] executor almost finito --- server/executor.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/server/executor.py b/server/executor.py index 3050f87..2da0185 100644 --- a/server/executor.py +++ b/server/executor.py @@ -50,15 +50,20 @@ class Executor: return strdirectory def putFileInCurrentDirectory(self, filename: str, content) -> str: - pass + filename = self.sanitizeFile(filename) + currenctfile = self.currentDirectory + "/" + filename + f = open(currenctfile,"wb") + f.write(content) + f.close() + return currenctfile def getFileInCurrentDirectory(self, file: str): file = self.sanitizeFile(file) - currentfile = self.currentDirectory + file + currentfile = self.currentDirectory + "/" + file return open(currentfile, "r") def removeFileInCurrentDirectory(self, file: str) -> str: file = self.sanitizeFile(file) - currentfile = self.currentDirectory + file + currentfile = self.currentDirectory + "/" + file os.remove(currentfile) return currentfile From 89b7b68dfe871c3e6be6d984c57e0135b52c9772 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torma=20Krist=C3=B3f?= Date: Sun, 18 Apr 2021 15:54:13 +0200 Subject: [PATCH 3/5] finito execturo for real --- server/executor.py | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/server/executor.py b/server/executor.py index 2da0185..e0340e5 100644 --- a/server/executor.py +++ b/server/executor.py @@ -36,10 +36,21 @@ class Executor: return self.currentDirectory def setCurrentDirectory(self, dirName: str) -> str: - # TODO : Navigate up a directory structure - dirName = self.sanitizeDirectory(dirName) - self.currentDirectory = self.currentDirectory + "/" + dirName - return self.currentDirectory + if dirName == "..": + if self.currentDirectory == self.baseDir: + return self.currentDirectory + else: + directories = self.currentDirectory.split("/") + strdirectory = "" + for dir in directories: + strdirectory += dir + "/" + strdirectory = strdirectory[:-3] + self.currentDirectory = strdirectory + return self.currentDirectory + else: + dirName = self.sanitizeDirectory(dirName) + self.currentDirectory = self.currentDirectory + "/" + dirName + return self.currentDirectory def listCurrentDirectoryContent(self) -> str: contents = os.listdir(self.currentDirectory) @@ -52,10 +63,10 @@ class Executor: def putFileInCurrentDirectory(self, filename: str, content) -> str: filename = self.sanitizeFile(filename) currenctfile = self.currentDirectory + "/" + filename - f = open(currenctfile,"wb") + f = open(currenctfile, "wb") f.write(content) f.close() - return currenctfile + return currenctfile def getFileInCurrentDirectory(self, file: str): file = self.sanitizeFile(file) From d3e830c59a44754dcf908d02eea08c76bdc4eb78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torma=20Krist=C3=B3f?= Date: Sun, 18 Apr 2021 16:07:41 +0200 Subject: [PATCH 4/5] minor changes to authenticator --- server/authentication.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/server/authentication.py b/server/authentication.py index 9ddc19b..a6de72e 100644 --- a/server/authentication.py +++ b/server/authentication.py @@ -1,10 +1,12 @@ -import os, sys +import json +import logging +import os +import shutil +import sys from base64 import b64encode + from Crypto.Hash import SHA256 from Crypto.Protocol.KDF import bcrypt, bcrypt_check -import json -import shutil -import logging auth_logger = logging.getLogger('AUTH APPLICATION ') auth_logger.root.setLevel(logging.INFO) @@ -30,7 +32,7 @@ class Authetication: json.dump(data, outfile) def login(self, username: str, password: str) -> str: - with open(Authetication.CONFIG_DIRECTORY_LOCATION + '\\config.txt') as json_file: + with open(self.CONFIG_DIRECTORY_LOCATION + '\\config.txt') as json_file: data = json.load(json_file) for user in data['user']: @@ -46,7 +48,7 @@ class Authetication: return user['homeDir'] def checkUserExists(self, username: str) -> bool: - with open(Authetication.CONFIG_DIRECTORY_LOCATION + '\\config.txt') as json_file: + with open(self.CONFIG_DIRECTORY_LOCATION + '\\config.txt') as json_file: data = json.load(json_file) for user in data['user']: @@ -67,7 +69,7 @@ class Authetication: b64pwd = b64encode(SHA256.new(bytePass).digest()) bcrypt_hash = bcrypt(b64pwd, 12) - with open(Authetication.CONFIG_DIRECTORY_LOCATION + '\\config.txt') as json_file: + with open(self.CONFIG_DIRECTORY_LOCATION + '\\config.txt') as json_file: data = json.load(json_file) user = { From 6dc735c18041a70a42214bda23f5f876599a92a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torma=20Krist=C3=B3f?= Date: Sun, 18 Apr 2021 16:18:44 +0200 Subject: [PATCH 5/5] use os independent path --- server/executor.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/server/executor.py b/server/executor.py index e0340e5..4620657 100644 --- a/server/executor.py +++ b/server/executor.py @@ -22,13 +22,13 @@ class Executor: def createDirectory(self, dirName: str) -> str: dirName = self.sanitizeDirectory(dirName) - actualDirName = self.currentDirectory + "/" + dirName + actualDirName = os.path.join(self.currentDirectory, dirName) os.mkdir(actualDirName) return actualDirName def removeDirectory(self, dirName: str) -> str: dirName = self.sanitizeDirectory(dirName) - actualDirName = self.currentDirectory + "/" + dirName + actualDirName = os.path.join(self.currentDirectory, dirName) os.rmdir(actualDirName) return actualDirName @@ -49,7 +49,7 @@ class Executor: return self.currentDirectory else: dirName = self.sanitizeDirectory(dirName) - self.currentDirectory = self.currentDirectory + "/" + dirName + self.currentDirectory = os.path.join(self.currentDirectory, dirName) return self.currentDirectory def listCurrentDirectoryContent(self) -> str: @@ -62,7 +62,7 @@ class Executor: def putFileInCurrentDirectory(self, filename: str, content) -> str: filename = self.sanitizeFile(filename) - currenctfile = self.currentDirectory + "/" + filename + currenctfile = os.path.join(self.currentDirectory, filename) f = open(currenctfile, "wb") f.write(content) f.close() @@ -70,11 +70,11 @@ class Executor: def getFileInCurrentDirectory(self, file: str): file = self.sanitizeFile(file) - currentfile = self.currentDirectory + "/" + file + currentfile = os.path.join(self.currentDirectory, file) return open(currentfile, "r") def removeFileInCurrentDirectory(self, file: str) -> str: file = self.sanitizeFile(file) - currentfile = self.currentDirectory + "/" + file + currentfile = os.path.join(self.currentDirectory, file) os.remove(currentfile) return currentfile