179 lines
5.7 KiB
Python
179 lines
5.7 KiB
Python
print("newcmd setup start.")
|
|
# init
|
|
print("importing modules")
|
|
try:
|
|
import ast
|
|
import hashlib
|
|
from simplecrypt import encrypt, decrypt
|
|
from copy import deepcopy
|
|
import sys
|
|
except ModuleNotFoundError:
|
|
print("error: some modules are missing or not being found")
|
|
print("done")
|
|
print("loading users")
|
|
users = open("ressources/users", 'r')
|
|
users = users.read()
|
|
users = ast.literal_eval(users)
|
|
userslen = len(users)
|
|
usersdata = {}
|
|
if userslen != 0:
|
|
errorcount = 0
|
|
sucesscount = 0
|
|
for i in range(len(users)):
|
|
try:
|
|
tempuser = users[i]
|
|
usersdata[tempuser["name"]] = tempuser
|
|
sucesscount += 1
|
|
except:
|
|
errorcount += 1
|
|
print("loading users done. {} users failed to load {} users loaded correctly".format(errorcount, sucesscount))
|
|
else:
|
|
print("no users found")
|
|
# define's
|
|
def login(user, password):
|
|
backuser = user
|
|
h = hashlib.new('sha256')
|
|
h.update(password.encode())
|
|
h = h.hexdigest()
|
|
if h == user["pass"]:
|
|
decrypted = decrypt(h, user["data"])
|
|
backuser["data"] = ast.literal_eval(decrypted.decode('utf-8'))
|
|
backuser["pass"] = h
|
|
return backuser
|
|
else:
|
|
return False
|
|
|
|
def updateusersfile(user):
|
|
global users
|
|
user1 = deepcopy(user)
|
|
user1["data"] = encrypt(user1["pass"], str(user1["data"]).encode())
|
|
users[user1["name"]] = user1
|
|
usersfile = {}
|
|
for i in range(len(users)):
|
|
usersfile[i] = list(users.values())[i]
|
|
file = open("ressources/users", 'w')
|
|
file.write(str(usersfile))
|
|
file.close()
|
|
|
|
def createnewaccount(right):
|
|
name = input("enter name for your new user: ")
|
|
password = input("enter password for your new user: ")
|
|
back = {}
|
|
back["name"] = name
|
|
h = hashlib.new('sha256')
|
|
h.update(password.encode())
|
|
h = h.hexdigest()
|
|
back["pass"] = h
|
|
back["data"] = {"rights": right, "files": {}}
|
|
updateusersfile(back)
|
|
return back
|
|
|
|
def cd(command):
|
|
global currentdir
|
|
if currentdir == currentdir:
|
|
if len(command) != 1:
|
|
if command[1] != "..":
|
|
if command[1] in user["data"]["files"]:
|
|
currentdir = command[1]
|
|
else:
|
|
print("error in cd: directory '{}' not existing".format(command[1]))
|
|
else:
|
|
currentdir = ""
|
|
else:
|
|
print("error in cd: cd requires 1 argument")
|
|
|
|
def ls():
|
|
if currentdir != "":
|
|
back = str(user["data"]["files"][currentdir].keys())
|
|
back = back.replace("dict_keys([", "")
|
|
back = back.replace("])", "")
|
|
print(back)
|
|
else:
|
|
back = str(user["data"]["files"].keys())
|
|
back = back.replace("dict_keys([", "")
|
|
back = back.replace("])", "")
|
|
print(back)
|
|
|
|
def createdir(command):
|
|
if len(command) != 1:
|
|
user["data"]["files"][command[1]] = {}
|
|
updateusersfile(user)
|
|
else:
|
|
print("error in createdir: createdir requires 1 argument")
|
|
|
|
def remove(command):
|
|
if len(command) != 1:
|
|
if command[1] in user["data"]["files"]:
|
|
del user["data"]["files"][command[1]]
|
|
updateusersfile(user)
|
|
else:
|
|
print("error in removedir: directory {} is not existing".format(command[1]))
|
|
else:
|
|
print("error in removedir: removedir requires 1 argument")
|
|
|
|
def createfile(command):
|
|
if len(command) != 1:
|
|
user["data"]["files"][command[1]] = ""
|
|
updateusersfile(user)
|
|
else:
|
|
print("error in createfile: createfile requires 1 argument")
|
|
|
|
def editfile(command):
|
|
if len(command) != 1:
|
|
if currentdir != "":
|
|
if command[1] in user["data"]["files"][currentdir]:
|
|
towrite = sys.stdin.read()
|
|
while not "exit:editor" in towrite:
|
|
print(towrite)
|
|
towrite = towrite.replace("exit:editor", "")
|
|
user["data"]["files"][currentdir][command[1]] = towrite
|
|
else:
|
|
print("error in editfile: file {} not found".format(command[1]))
|
|
else:
|
|
if command[1] in user["data"]["files"]:
|
|
towrite = sys.stdin.readlines()
|
|
while not "exit:editor" in towrite:
|
|
print(towrite)
|
|
towrite = towrite.replace("exit:editor", "")
|
|
user["data"]["files"][command[1]] = towrite
|
|
else:
|
|
print("error in editfile: file {} not found".format(command[1]))
|
|
else:
|
|
print("error in editfile: editfile requires 1 argument")
|
|
# login
|
|
if userslen != 0:
|
|
count = 0
|
|
for i in usersdata:
|
|
count += 1
|
|
print("{}: {}".format(count, i))
|
|
userin = int(input("please enter target user to login by number: "))
|
|
if 1 <= userin <= count:
|
|
back = False
|
|
while back == False:
|
|
password = input("please enter password for {}: ".format(list(usersdata.keys())[userin-1]))
|
|
back = login(list(usersdata.values())[userin-1], password)
|
|
user = back
|
|
else:
|
|
user = createnewaccount("Admin")
|
|
#main
|
|
currentdir = ""
|
|
while True:
|
|
command = input("{}: ".format(currentdir))
|
|
command = command.split()
|
|
if len(command) != 0:
|
|
if command[0] == "cd":
|
|
cd(command)
|
|
elif command[0] == "ls":
|
|
ls()
|
|
elif command[0] == "createdir":
|
|
createdir(command)
|
|
elif command[0] == "remove":
|
|
remove(command)
|
|
elif command[0] == "createfile":
|
|
createfile(command)
|
|
elif command[0] == "editfile":
|
|
editfile(command)
|
|
else:
|
|
print("unknown command: {}".format(command[0]))
|
|
|