def load_private_key(self): # load keys. if not self.memory: self.private_key_data = Files.load(self.private_key) else: self.private_key_data = self.private_key # initialize keys. if self.passphrase == None: try: self.private_key_object = _RSA_.importKey(str(self.private_key_data)) except ValueError as e: self.private_key_object = None if "Padding is incorrect" in str(e): return _response_.error("Provided an incorrect passphrase.") else: return _response_.error(f"ValueError: {e}") return response else: try: self.private_key_object = _RSA_.importKey(str(self.private_key_data), passphrase=self.passphrase) except ValueError as e: self.private_key_object = None if "Padding is incorrect" in str(e): return _response_.error("Provided an incorrect passphrase.") else: return _response_.error(f"ValueError: {e}") return response # response. return _response_.success("Successfully loaded the key pair.")
def set_password(self, password=None): # check params. response = _response_.parameters.check( parameters={ "password":password, }) if not response.success: return response # handle linux. if OS in ["linux"]: # get output. output = code.execute(f"export HISTIGNORE='*sudo -S*' && echo '{password}\n{password}\n' | sudo -S -k sudo passwd {self.username}") # success. if "password updated successfully" in output: return _response_.success(f"Successfully edited the password of user [{self.username}].") # fail. else: print(output) return _response_.error(f"Failed to edit the password of user [{self.username}].") # handle macos. elif OS in ["macos"]: return _response_.error(f"Unsupported operating system [{OS}].")
def create(self): # check duplicates. l_response = self.check() if l_response["error"] != None: return l_response if l_response["exists"]: return _response_.error(f"User [{self.username}] already exists.") # check home dir. if Files.exists(self.home_directory): return _response_.error(f"Home directory [{self.home_directory}] already exists.") # handle linux. if OS in ["linux"]: # ubuntu. output = code.execute(f"sudo useradd -s /bin/bash -m {self.username}") # success. if output == "": return _response_.success(f"Successfully created user [{self.username}].") else: return _response_.error(String(output.output.replace("useradd: ", "").replace("\n", ". ")).capitalized_word()) # handle macos. elif OS in ["macos"]: return _response_.error(f"Unsupported operating system [{OS}].")
def list_users(self): # check existance. l_response = self.check() if l_response["error"] != None: return l_response elif not l_response["exists"]: return _response_.error(f"Group [{self.name}] does not exists.") # handle linux. users = [] if OS in ["linux"]: try: output = subprocess.check_output("members "+self.name, shell=True).decode().replace("\n", "").split(" ") except: output = [] for i in output: if i not in [""]: users.append(i.replace("\n", "")) # handle macos. elif OS in ["macos"]: return _response_.error(f"Unsupported operating system [{OS}].") # success. self.users = users return _response_.success(f"Successfully listed all users {len(users)} of group [{self.name}].", { "users":users, })
def delete(self): # check existance. l_response = self.check() if l_response["error"] != None: return l_response if not l_response["exists"]: return _response_.error(f"User [{self.username}] does not exist.") # handle linux. if OS in ["linux"]: # delete. os.system(f"sudo userdel -r {self.username}") os.system(f"sudo rm -fr {self.home_directory}") # handle macos. elif OS in ["macos"]: return _response_.error(f"Unsupported operating system [{OS}].") # check. l_response = self.check() if l_response["error"] != None: return l_response # success. if not l_response["exists"]: return _response_.success(f"Successfully deleted user [{self.username}].") else: return _response_.error(f"Failed to delete user [{self.username}].")
def process_dict_input(dictionary): # vars. expect = list(dictionary.keys()) send = list(dictionary.values()) # expect . if log_level >= 8: print(f"Expecting one of the following inputs: {expect}.") response = spawn.expect(expect=expect, send=send, timeout=timeout) if not response.success: if "None of the specified inputs were expected." in response.error: if optional: return _response_.error( f"Unable to find the expected input but still success since it is optional." ) else: return _response_.error( f"Specified input [{Array(expect).string(joiner=', ')}] was not expected." ) else: return response if log_level >= 8: print("Send response message:", response.message) # success. return _response_.success("Success.")
def safe_stop(self, timeout=120, sleeptime=1): # check already crashed. if self.crashed: return _response_.error( f"Thread {self.id} has crashed, error: {self.response.error}") # send stop. self.log(f"Stopping thread {self.id}.") self.send_stop() for i in range(int(timeout / sleeptime)): if self.stopped: break time.sleep(sleeptime) if not self.stopped: return _response_.error(f"Unable to stop thread {self}.") # handle stop functions. found = False try: response = self.stop() found = True except AttributeError: response = None if not found: try: response = self.__stop__() found = True except AttributeError: response = None if isinstance(response, ResponseObject) and not response.success: return response # handler. return _response_.success(f"Successfully stopped thread {self.id}.", required_log_level=0)
def create(self): # check params. response = _response_.parameters.check( traceback=self.__traceback__(function="create"), parameters={ "id": self.id, "user": self.user, "start": self.start_, }) if not response.success: return response # checks. if self.service.fp.exists(): return _response_.error( f"Service [{self.service.fp.path}] already exists (call service.check() instead)." ) # save. _response_.log( f"&ORANGE&Root permission&END& required to save changes to [{self.service.fp}].", log_level=self.log_level) self.service.save(data=self.__create__(), sudo=True) self.service.fp.ownership.set("root", sudo=True) self.service.fp.permission.set(700, sudo=True) if dev0s.defaults.vars.os in ["macos"]: os.system("sudo systemctl daemon-reload") # handler. if self.service.fp.exists(): return _response_.success( f"Successfully created service {self.id}.") else: return _response_.error(f"Failed to create service {self.id}.")
def decrypt_directory(self, path, recursive=False, layers=1): # checks. if not self.private_key_activated: return _response_.error("Can not decrypt data since the private key is not activated yet.") # defaults. # recursively decrypt all files. if recursive: # recursive. for i in os.listdir(path): i = f'{path}/{i}'.replace("//",'/') if os.path.isdir(i): response = self.decrypt_directory(i, recursive=True) if response["error"] != None: return response else: response = self.decrypt_file(i) if response["error"] != None: return response # success. if layers-1 > 0: return self.decrypt_directory(path, recursive=True, layers=layers-1) else: return _response_.success(f"Successfully decrypted directory [{path}] (recursively).") # decrypt the encrypted.zip file. else: path = path.replace('.encrypted.zip', '/') # checks. file_path = FilePath(path) # set zip path. if path[len(path)-1] == "/": zip_path = path[:-1] else: zip_path = str(path) zip_path = f'{zip_path}.encrypted.zip' if not Files.exists(zip_path): return _response_.error(f"System encrypted zip [{path}] does not exist.") # decrypt zip. response = self.decrypt_file(zip_path, layers=layers) if response["error"] != None: return _response_.error(f"Failed to decrypted directory [{path}].") # extract zip. zip = Zip(path=zip_path) zip.extract(base=None) if Files.exists(path): zip.file_path.delete(forced=True) return _response_.success(f"Successfully decrypted directory [{path}].") else: return _response_.error(f"Failed to decrypted directory [{path}].")
def save(self): if not self.loaded: return _response_.error("The data object is not loaded yet, you must call content.load() first.") content = Formats.denitialize(self.content) response = self.aes.encrypt(content) if not response.success: return response try: content = Files.save(str(self.path), f"{self.format} ".encode()+response.encrypted, format="bytes") except Exception as e: return _response_.error(f"Failed to save content {self.path}, {e}.") return _response_.success(f"Successfully saved content {self.path}.")
def save( self, # the sub path (str, FilePath) (#1). path=None, # the data to save (bool, str, int, float, dict, list) (#2) data=None, # the data format [bool, str, int, float, dict, list] (str) (#3). format="dict", # with overwrite disabled the dict format data is inserted into the existing data (bool). overwrite=False, ): if path == None: return _response_.error( self.__traceback__(function="save") + " Define parameter: [path].") if data == None: return _response_.error( self.__traceback__(function="save") + " Define parameter: [data].") path = str(path) path = gfp.clean(f"{self.path}/{path}") format = self.__serialize_format__(format) try: if not Files.exists(path=gfp.base(path)): Files.create(path=gfp.base(path), directory=True) except ValueError: a = 1 try: if format in ["bytes"]: obj = Bytes(data, path=path) elif format in ["bool"]: obj = Boolean(data, path=path) elif format in ["str"]: obj = String(data, path=path) elif format in ["int", "float"]: obj = Integer(data, path=path) elif format in ["list"]: obj = Array(data, path=path) elif format in ["dict"]: obj = Dictionary({}, path=path) if format in ["dict"]: if overwrite: obj.save(dictionary=data) else: if obj.fp.exists(): obj.load() else: obj.dictionary = {} obj.insert(data) obj.save() else: obj.save() except Exception as e: return _response_.error(str(e)) return _response_.success(f"Successfully saved [{path}].")
def encrypt_directory(self, path, recursive=False, layers=1): # checks. if not self.public_key_activated: return _response_.error("Can not encrypt data since the public key is not activated yet.") # recursively encrypt all files. if recursive: # recursive. for i in os.listdir(path): i = f'{path}/{i}'.replace("//",'/') if os.path.isdir(i): response = self.encrypt_directory(i, recursive=True) if response["error"] != None: return response else: response = self.encrypt_file(i) if response["error"] != None: return response # success. if layers-1 > 0: return self.encrypt_directory(path, recursive=True, layers=layers-1) else: return _response_.success(f"Successfully encrypted directory [{path}] (recursively).") # encrypt the directory and save to x.encrypted.zip file. else: # checks. file_path = FilePath(path) if not file_path.exists(): return _response_.error(f"Directory [{path}] does not exist.") if not file_path.directory(): return _response_.error(f"Directory path [{path}] is not a directory.") # create zip. if path[len(path)-1] == "/": zip_path = path[:-1] else: zip_path = str(path) zip_path = f'{zip_path}.encrypted.zip' zip = Zip(path=zip_path) zip.create(source=path) # encrypt zip. response = self.encrypt_file(zip.file_path.path, layers=layers) if response["success"]: file_path.delete(forced=True) return response else: zip.file_path.delete(forced=True) return _response_.error(f"Failed to encrypted directory [{path}].")
def load(self, # the subpath of the content (! param number 1). path=None, # the default data, specify to call self.check() automatically on the data object. default=None, ): if not self.activated: return _response_.error(f"Encrypted database [{self.path}] is not activated yet, call database.activate() first.") response = _response_.parameters.check({ "path:str":path,}) if not response.success: return response path = gfp.clean(f"{self.path}/{path}", remove_last_slash=True, remove_double_slash=True) if not os.path.exists(str(path)): if default == None: return _response_.error(f"Specified path [{path}] does not exist.") elif isinstance(default, (str, String, File)): if isinstance(default, File): default = default.data return _response_.success(f"Successfully parsed {path}.", { "format":"File", "content":self.File(path=path, default=str(default), aes=self.aes), }) elif isinstance(default, (list, Array)): return _response_.success(f"Successfully parsed {path}.", { "format":"Array", "content":self.Array(path=path, default=default, aes=self.aes), }) elif isinstance(default, (dict, Dictionary)): return _response_.success(f"Successfully parsed {path}.", { "format":"Dictionary", "content":self.Dictionary(path=path, default=default, aes=self.aes), }) else: try: format, _ = unpack(Files.load(path)) except Exception as e: return _response_.error(f"Failed to load content {path}, {e}.") if format in ["list", "Array"]: return _response_.success(f"Successfully parsed {path}.", { "format":"Array", "content":self.Array(path=path, default=default, aes=self.aes), }) elif format in ["dict", "Dictionary"]: return _response_.success(f"Successfully parsed {path}.", { "format":"Dictionary", "content":self.Dictionary(path=path, default=default, aes=self.aes), }) elif format in ["file", "File"]: return _response_.success(f"Successfully parsed {path}.", { "format":"File", "content":self.File(path=path, default=default, aes=self.aes), }) else: return _response_.error(f"Unknown format [{format}] for path [{path}].")
def load(self): try: format, content = unpack(Files.load(self.path)) except Exception as e: return _response_.error(f"Failed to load content {self.path}, {e}.") response = self.aes.decrypt(content.encode()) if not response.success: return response decrypted = response.decrypted.decode() try: content = json.loads(decrypted) except Exception as e: return _response_.error(f"Failed to serialize content {self.path}, {e}.") self.dictionary = content self.format = format self.loaded = True return _response_.success(f"Successfully loaded content {self.path}.")
def decrypt_directory(self, input=None, output=None, remove=False): input = Formats.denitialize(input) output = Formats.denitialize(output) # checks. file_path = FilePath(input) if not file_path.exists(): return _response_.error(f"Input [{input}] does not exist.") if file_path.directory(): return _response_.error(f"Input path [{input}] is a directory.") # dir path. if output == None: if ".enc.zip" not in input: return _response_.error(f"Invalid input format [{input}], the format must end with [***.enc.zip]") dir_path = output.replace(".enc.zip", "/").replace("//","/").replace("//","/").replace("//","/") else: if ".enc.zip" in output: return _response_.error(f"Invalid output format [{output}], the format can not end with [***.enc.zip]") dir_path = output.replace(".enc.zip", "/") # check output. l_dir_path = dir_path if l_dir_path[len(l_dir_path)-1] == "/": l_dir_path = l_dir_path[:-1] if Files.exists(l_dir_path): return _response_.error(f"Output path [{l_dir_path}] already exists.") # decrypt zip. tmp_zip = f"/tmp/{FilePath(input).name()}" if Files.exists(tmp_zip): os.system(f"rm -fr {tmp_zip}") response = self.decrypt_file(input=input, output=tmp_zip, remove=False, base64_encoding=True) if not response.success: return _response_.error(f"Failed to decrypted directory [{input}], error: {response.error}") # extract zip. tmp_extract = f"/tmp/{String('').generate(length=32,capitalize=True,digits=True)}/" if Files.exists(tmp_extract): os.system(f"rm -fr {tmp_extract}") os.mkdir(tmp_extract) zip = Zip(path=tmp_zip) zip.extract(base=tmp_extract) paths = Files.Directory(path=tmp_extract).paths() if len(paths) == 1: os.system(f"mv {paths[0]} {output}") if Files.exists(output): os.system(f"rm -fr {tmp_extract}") os.system(f"rm -fr {tmp_zip}") if remove and input != output: try: os.remove(input) except PermissionError: os.system(f"sudo rm -fr {input}") return _response_.success(f"Successfully decrypted directory [{input}].") else: os.system(f"rm -fr {tmp_extract}") os.system(f"rm -fr {tmp_zip}") return _response_.error(f"Failed to decrypt directory [{input}].") else: os.system(f"rm -fr {tmp_extract}") os.system(f"rm -fr {tmp_zip}") return _response_.error(f"Failed to decrypt directory [{input}].")
def decrypt_file(self, input=None, output=None, remove=False, base64_encoding=False): input = Formats.denitialize(input) output = Formats.denitialize(output) # check params. response = _response_.parameters.check({ "input":input, "output":output,}) if not response.success: return response # encrypt. response = self.decrypt(Files.load(input, format="bytes"), decode=False) if not response.success: return response # write out. decrypted = response.decrypted if base64_encoding: decrypted = base64.b64decode(decrypted) try: Files.save(output, decrypted, format="bytes") except: return _response_.error(f"Failed to write out decrypted file {output}.") # remove. if remove and input != output: try: os.remove(input) except PermissionError: os.system(f"sudo rm -fr {input}") # handler. return _response_.success(f"Successfully decrypted file {input} to {output}.")
def decrypt(self, string, decode=False): # split encrypted aes key. string = Formats.denitialize(string) if isinstance(string, bytes): string = string.decode() try: key,encrypted = unpack(string) #except: except KeyboardInterrupt: return _response_.error("Unable to unpack the encrypted data.") # decypt key with rsa. response = self.rsa.decrypt_string(key, decode=False) if not response.success: return response passphrase = response["decrypted"].decode() # decrypt with aes. aes = AES(passphrase=passphrase) response = aes.decrypt(encrypted) if not response.success: return response decrypted = response["decrypted"] # success. if decode: decrypted = decrypted.decode() return _response_.success("Successfully decrypted the specified data.", { "decrypted":decrypted })
def encrypt(self, string, decode=False): string = Formats.denitialize(string) if isinstance(string, bytes): string = string.decode() # encrypt data with aes. passphrase = String().generate(length=64, digits=True, capitalize=True) aes = AES(passphrase=passphrase) response = aes.encrypt(string) if not response.success: return response aes_encrypted = response["encrypted"] if b" " in aes_encrypted: return _response_.error("AES encrypt data contains invalid ' ' character(s).") # encrypt aes key with rsa. response = self.rsa.encrypt_string(passphrase, decode=False) if not response.success: return response rsa_encrypted = response["encrypted"] # pack encrypted. encrypted = rsa_encrypted+b" "+aes_encrypted # success. if decode: encrypted = encrypted.decode() return _response_.success("Successfully encrypted the specified data.", { "encrypted":encrypted })
def kill(self): # handle output. """ killed = None try: #if self.async_: # g = yield from self.child.terminate(force=True, async_=True) # killed = next(x) #else: killed = self.child.terminate(force=True) except Exception as e: return _response_.error(f"Failed to kill process [{self.command}], error: {e}.") if killed == None: return _response_.error(f"Failed to kill process [{self.command}] (#452983).") if not killed: """ # handle output. response = kill(pid=self.pid, log_level=self.log_level) if not response.success: return _response_.error( f"Unable to kill process [{self.command}].") else: return _response_.success( f"Successfully killed process [{self.command}].")
def connect(self, # the network's ssid. ssid=None, # the network's password. password=None, ): # linux. if defaults.vars.os in ["linux"]: path = f"/etc/netplan/{ssid}.yaml" Files.Save(path, f"""network: wifis: wlan0: dhcp4: true optional: true access-points: "{ssid}": password: "******" """, sudo=True) response = code.execute("sudo netplan generate") if not response.success: return response response = code.execute("sudo netplan apply") if not response.success: return response return _response_.success(f"Successfully connected with {ssid}.") # invalid os. else: return _response_.error(f"Invalid operating system {defaults.vars.os}.")
def convert_dns(self, dns, timeout=1): response = self.ping(dns, timeout=timeout) if response["error"] != None: return response if response["ip"] == None: return _response_.error(f"Failed to convert dns [{dns}].") return _response_.success(f"Successfully converted dns [{dns}].", {"ip": response["ip"]})
def format( self, # the device with partition number (/dev/sdb1). device=None, # the assigned label (name). label=None, ): coming_soon() # check parameters. response = _response_.parameters.check({ "device:str": device, "label:str": label, }) if not response.success: return response # check os. response = check_os() if not response.success: return response # handler. if "SUCCESS!" in output: return _response_.success( f"Successfully formatted device {device}.") else: return _response_.error( f"Failed to format device {device}, output: {output}")
def mount( self, # the device with partition number (/dev/sdb1). device=None, # the mountpoint path. path=None, ): coming_soon() # check parameters. response = _response_.parameters.check({ "device:str": device, "path:str": path, }) if not response.success: return response # check os. response = check_os() if not response.success: return response # handler. if "SUCCESS!" in output: return _response_.success( f"Successfully mounted device {device} to {path}.") else: return _response_.error( f"Failed to mount device {device} to {path}, output: {output}")
def active(): token = flask.request.args.get('token') if token != Database(path=flask.request.args.get('cache')).load( Files.join(flask.request.args.get('cache_id'), "token")): return _response_.error( f"Provided an invalid token {token}.").json() return _response_.success(f"Active.").json()
def delete(self): # checks. if not self.service.fp.exists(): return _response_.error( f"Service [{self.service.fp.path}] does not already exist.") # save. self.service.fp.delete(sudo=True, forced=True) # handler. if not self.service.fp.exists(): return _response_.success( f"Successfully deleted service {self.id}.") else: return _response_.error(f"Failed to delete service {self.id}.")
def partition( self, # the device without partition number (/dev/sdb). device=None, ): coming_soon() # check parameters. response = _response_.parameters.check({ "device:str": device, }) if not response.success: return response # check os. response = check_os() if not response.success: return response # execute. output = utils.__execute_script__(f""" sudo parted {device} mklabel gpt sudo parted -a opt {device} mkpart primary ext4 0% 100% """) # handler. if "SUCCESS!" in output: return _response_.success( f"Successfully partitioned device {device}.") else: return _response_.error( f"Failed to partition device {device}, output: {output}")
def stop(self): if not self.running: return _response_.success( f"{self.__traceback__(function='stop')}: The {self.id} is not running." ) processes = code.processes( includes=f"--dev0s-webserver-tag {self.tag}") if not processes.success: return response if len(processes.processes) <= 1: return _response_.error( f"Unable to find the pid of the {self.id}.") for pid, info in processes.processes.items(): if info["process"] not in ["grep"]: response = code.kill(pid=pid) if not response.success: return response return _response_.error(f"Successfully stopped the {self.id}.")
def fork(self, timeout=15, sleeptime=1): if self.running: return _response_.success(f"The {self.id} is already running.") if self.log_level <= 0: print(f"Starting the {self.id}.") serialized = self.dict( keys={ "id": "webserver", "host": "127.0.0.1", "port": 52379, "sleeptime": 3, "log_level": 0, }) for i in [ "__cache__", "cache", "system_cache", "_stder", "_traceback_", "_name", "_daemonic", "_ident", "_native_id", "_tstate_lock", "_started", "_stderr", "_initialized", "_invoke_excepthook", "__status__", "__running__", "__response__", "_is_stopped", "_args", "_kwargs", "_target", "_raw_traceback_", ]: try: del serialized[i] except: a = 1 serialized = f"{serialized}" command = [ str(defaults.vars.executable), f"{SOURCE_PATH}classes/database/fork.py", "--serialized", f"'{serialized}'", "--dev0s-webserver-tag", self.tag ] if self.log_level < 0: command += ["2>", "/dev/null"] p = subprocess.Popen(command) success = False for i in range(int(timeout / sleeptime)): if self.running: success = True break time.sleep(sleeptime) if success: return _response_.success(f"Successfully started the {self.id}.") else: return _response_.error(f"Failed to start the {self.id}.")
def check_os(supported=["linux"], error=False): if OS not in supported: if error: raise ValueError(f"Unsupported operating system: {OS}.") else: return _response_.error(f"Unsupported operating system: {OS}.") else: if error: return None else: return _response_.success(f"Supported operating system: {OS}.")
def crashed(self): response = self.read(wait=False) if not response.success: return response if self.exit_status not in [0, None]: return _response_.error( f"{self.response_str} returned exit status: [{self.exit_status}] (output: {self.read(wait=False, __safe__=True).output})." ) return _response_.success(f"{self.response_str} is not crashed.")