Merge branch 'feature/changeRights' into dev

This commit is contained in:
Tatyik
2017-04-10 12:51:07 +02:00
5 changed files with 173 additions and 5 deletions

47
src/Common/cryptModule.py Normal file

@ -0,0 +1,47 @@
#!/usr/bin/env python3
import sys
from simplecrypt import encrypt, decrypt, DecryptionException
def cryptFile(file_source, key, file_destination):
try:
source = open(file_source, 'rb')
except:
print("No such file or directory : " + file_source)
else:
destination = open(file_destination, 'wb')
line_file = source.read()
crypt_line = encrypt(key, line_file)
destination.write(crypt_line)
source.close()
destination.close()
def decryptFile(file_source, key, file_destination):
try:
source = open(file_source, 'rb')
except:
print("No such file or directory : " + file_source)
else:
destination = open(file_destination, 'wb')
line_file = source.read()
try:
decrypt_line = decrypt(key, line_file)
except DecryptionException:
print("Bad password or corrupt / modified data.")
else:
destination.write(decrypt_line)
source.close()
destination.close()
if sys.argv[1] == "chiffrer":
cryptFile(sys.argv[3], sys.argv[2], sys.argv[4])
elif sys.argv[1] == "dechiffrer":
decryptFile(sys.argv[3], sys.argv[2], sys.argv[4])
else:
print("Unknown command")

@ -169,9 +169,9 @@ class AccessControlList():
except:
raise CommandException("An error occurred...")
def changeRightsOnFile(self, user, file, read=[], write=[], execute=[]):
def setRightsOnFile(self, user, file, read, write, execute):
try:
if user == self.__data['files'][file]['owner']:
if user == self.__data['files'][file]['owner'] or self.isAdministrator(user):
self.__data['files'][file] = {'owner': user, 'read': read, 'write': write, 'execute': execute}
self.saveToFile()
@ -181,6 +181,12 @@ class AccessControlList():
except:
raise CommandException("This file does not exist...")
def getRightsOnFile(self, file):
if list(self.__data['files'].keys()).count(file):
return (self.__data['files'][file]['write'], self.__data['files'][file]['read'], self.__data['files'][file]['execute'])
else:
raise CommandException("This file does not exist...")
def changeOwnOnFile(self, user, file, newUser):
try:
if user == self.__data['files'][file]['owner'] or self.isAdministrator(user):

@ -358,6 +358,8 @@ def listFiles(command, client, acl):
command[1], command[2] = command[2], command[1]
path = interpretPath(command[1], client)
if os.path.isdir(path):
path = path + "/"
if not os.path.exists(path) or not os.path.isdir(path):
raise CommandException("Unknown path (or is it really a directory ?).")
@ -390,7 +392,7 @@ def listFiles(command, client, acl):
return message
else:
raise CommandException("You\re not allowed to perform this operation.")
raise CommandException("You\'re not allowed to perform this operation.")
else:
raise CommandException("Wrong number of (or invalid) arguments.")

@ -5,6 +5,7 @@
List of command.
"""
import os
from Common.socketCommands import sendData, recvData
from .CommandException import CommandException
@ -93,7 +94,9 @@ def removeUserGroup(command, client, acl):
def ownerFile(command, client, acl):
if len(command) == 3:
command[1] = interpretPath(command[1], client) + '/'
command[1] = interpretPath(command[1], client)
if os.path.isdir(command[1]):
command[1] = command[1] + "/"
try:
acl.changeOwnOnFile(client.username, command[1], command[2])
return "The owner of " + command[1] + " is now " + command[2]
@ -138,3 +141,110 @@ def passwd(command, client, userList, acl):
else:
raise CommandException("Wrong number of arguments.")
def changeRights(command, client, acl):
def moreRights(path, acl, user, right, client, recursif):
(write, read, execute) = acl.getRightsOnFile(path)
if("r" in right) and not (read.count(user)):
read.append(user)
if("w" in right) and not (write.count(user)):
write.append(user)
if("x" in right) and not (execute.count(user)):
execute.append(user)
acl.setRightsOnFile(client, path, read, write, execute)
if(recursif is True):
for file in os.listdir(path):
if os.path.isdir(path + file):
file = file + '/'
moreRights(path + file, acl, user, right, client, True)
def lessRights(path, acl, user, right, client, recursif):
(write, read, execute) = acl.getRightsOnFile(path)
if("r" in right) and (read.count(user)):
read.remove(user)
if("w" in right) and (write.count(user)):
write.remove(user)
if("x" in right) and (execute.count(user)):
execute.remove(user)
acl.setRightsOnFile(client, path, read, write, execute)
if(recursif is True):
for file in os.listdir(path):
if os.path.isdir(path + file):
file = file + '/'
lessRights(path + file, acl, user, right, client, True)
def makeRights(path, acl, user, right, client, recursif):
(write, read, execute) = acl.getRightsOnFile(path)
if("r" in right):
read[:] = []
read.append(user)
if("w" in right):
write[:] = []
write.append(user)
if("x" in right):
execute[:] = []
execute.append(user)
acl.setRightsOnFile(client, path, read, write, execute)
if(recursif is True):
for file in os.listdir(path):
if os.path.isdir(path + file):
file = file + '/'
makeRights(path + file, acl, user, right, client, True)
nbArgs = len(command)
if not ((nbArgs == 3) or (nbArgs == 4)):
raise CommandException("Wrong number of arguments.")
elif nbArgs == 3:
command[1] = interpretPath(command[1], client)
if os.path.isdir(command[1]):
command[1] = command[1] + "/"
if("-" in command[2]):
right = command[2].partition("-")[2]
user = command[2].partition("-")[0]
lessRights(command[1], acl, user, right, client.username, False)
return("done")
elif("+" in command[2]):
right = command[2].partition("+")[2]
user = command[2].partition("+")[0]
moreRights(command[1], acl, user, right, client.username, False)
return "Done"
elif("=" in command[2]):
right = command[2].partition("=")[2]
user = command[2].partition("=")[0]
makeRights(command[1], acl, user, right, client.username, False)
return "Done"
else:
raise CommandException("Unknown command")
# Recursif
elif nbArgs == 4:
command[2] = interpretPath(command[2], client)
if not command[1] == '-r':
raise CommandException("Unknown command")
elif not os.path.isdir(command[2]):
raise CommandException("Directory is needed in argument, not a file.")
else:
command[2] = command[2] + "/"
if("-" in command[3]):
right = command[3].partition("-")[2]
user = command[3].partition("-")[0]
lessRights(command[2], acl, user, right, client.username, True)
return("done")
elif("+" in command[3]):
right = command[3].partition("+")[2]
user = command[3].partition("+")[0]
moreRights(command[2], acl, user, right, client.username, True)
return "Done"
elif("=" in command[3]):
right = command[3].partition("=")[2]
user = command[3].partition("=")[0]
makeRights(command[2], acl, user, right, client.username, True)
return "Done"
else:
raise CommandException("Unknown command")

@ -19,7 +19,7 @@ from Common.socketCommands import sendData, recvData
from Server.fileCommands import DATA_PATH, listFiles, changeDirectory, makeDirectory, removeFile, copyFile, textEditor, moveFile
from Server.CommandException import CommandException
from Server.errorHandler import serverSendErrHandler, serverRecvErrHandler
from Server.ulCommands import add, remove, addUserGroup, removeUserGroup, ownerFile, passwd
from Server.ulCommands import add, remove, addUserGroup, removeUserGroup, ownerFile, passwd, changeRights
from Server.miscCommands import helpMan
@ -250,6 +250,9 @@ def computeCommand(command, client):
elif command[0] == 'passwd':
sendData(client.sock, passwd(command, client, userList, acl), serverSendErrHandler, client.sock)
elif command[0] == 'chmod':
sendData(client.sock, changeRights(command, client, acl), serverSendErrHandler, client.sock)
# Unknown command
else:
sendData(client.sock, "An unknown command has been received by server.", serverSendErrHandler, client.sock)