From 7786df3761ac9b59de3dd02349bb334aa57bbee1 Mon Sep 17 00:00:00 2001 From: Darksider3 Date: Sun, 20 Oct 2019 15:24:11 +0200 Subject: [PATCH] System: Introduce function to write just write the auth file --- private/lib/System.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/private/lib/System.py b/private/lib/System.py index 92e081c..39ec9f7 100644 --- a/private/lib/System.py +++ b/private/lib/System.py @@ -80,11 +80,7 @@ class System: raise lib.UserExceptions.SSHDirUncreatable(f"Could not create {ssh_dir}: Exception: {e}") try: - with open(ssh_dir + "authorized_keys", "w") as f: - print(pubkey, file=f) - f.close() - os.chmod(ssh_dir + "authorized_keys", 0o700) # directory is already 777? - os.chmod(ssh_dir, 0o700) # directory is already 777? + self.write_ssh(pubkey, ssh_dir) except OSError as e: raise lib.UserExceptions.ModifyFilesystem( f"Could not write and/or chmod 0700 {ssh_dir} or {ssh_dir}/authorized_keys, Exception: {e}") @@ -99,6 +95,12 @@ class System: raise lib.UserExceptions.General(f"PWD can't find {username}: {e}") return True + def write_ssh(self, key: str, ssh_dir: str): + with open(ssh_dir + "authorized_keys", "w") as f: + print(key, file=f) + f.close() + os.chmod(ssh_dir + "authorized_keys", 0o700) # we dont care about the directory here + def lock_user_pw(self, username: str, cc: tuple = tuple(["usermod", "--lock"])) -> bool: """Lock a users password so it stays empty