コード例 #1
0
ファイル: __init__.py プロジェクト: vandenberghinc/ssht00ls
    def index(self,
              path=None,
              alias=None,
              log_level=0,
              checks=True,
              accept_new_host_keys=True):

        # check specific.
        if self.specific:
            if alias == None: alias = self.alias

        # checks.
        if path == None:
            return r3sponse.error(f"Define parameter: path.")
        path = gfp.clean(path)

        # check encryption activated.
        if not encryption.activated:
            return r3sponse.error("The encryption requires to be activated.")

        # remote.
        if alias != None:

            # checks.
            if checks:

                # check alias.
                response = aliases.check(alias)
                if not response["success"]: return response

                # check passphrase.
                if CONFIG["aliases"][alias]["smartcard"] in [
                        True, "true", "True"
                ]:
                    response = encryption.encryption.decrypt(
                        CONFIG["aliases"][alias]["passphrase"])
                else:
                    response = encryption.encryption.decrypt(
                        CONFIG["aliases"][alias]["passphrase"])
                if not response["success"]: return response
                passphrase = response.decrypted.decode()

                # tests.
                response = agent.add(
                    private_key=CONFIG["aliases"][alias]["private_key"],
                    passphrase=passphrase)
                if not response["success"]: return response
                response = ssh.utils.test_ssht00ls(
                    alias=alias, accept_new_host_keys=accept_new_host_keys)
                if not response["success"]: return response
                response = ssh.utils.test_path(
                    alias=alias,
                    path=path,
                    accept_new_host_keys=accept_new_host_keys)
                if not response["success"]: return response

            # index.
            return self.utils.execute(
                command=
                f"""printf 'yes' | ssh {DEFAULT_SSH_OPTIONS} {alias} ' export IPINFO_API_KEY="{IPINFO_API_KEY}" && python3 /usr/local/lib/ssht00ls/classes/ssync/index.py --path {path} --json --non-interactive --no-checks ' """,
                serialize=True,
                log_level=log_level,
            )

        # local.
        else:
            if checks:
                if not Files.exists(path):
                    return r3sponse.error(f"Path [{path}] does not exist.")
                elif not os.path.isdir(path):
                    return r3sponse.error(f"Path [{path}] is not a directory.")

            # handler.
            dict = self.utils.index(path)
            return r3sponse.success(
                f"Successfully indexed {len(dict)} files from directory [{path}].",
                {
                    "index": dict,
                })
コード例 #2
0
ファイル: __init__.py プロジェクト: vandenberghinc/ssht00ls
    def push(
        self,
        # the local path.
        path=None,
        # the ssht00ls alias.
        alias=None,
        # the remote path.
        remote=None,
        # exlude subpaths (list) (leave None to use default).
        exclude=None,
        # path is directory boolean (leave None to parse automatically).
        directory=True,
        empty_directory=False,
        # update deleted files.
        delete=False,
        # forced mode.
        forced=False,
        # version control.
        safe=False,
        # accept new hosts keys.
        accept_new_host_keys=True,
        # checks.
        checks=True,
        check_base=True,
        # log level.
        log_level=0,
        # get the command in str.
        command=False,
    ):

        # check specific.
        if self.specific:
            if alias == None: alias = self.alias

        # check encryption activated.
        if not encryption.activated:
            return r3sponse.error("The encryption requires to be activated.")
        if checks:
            if self.specific:
                try:
                    self.activated
                except:
                    self.activated = False
            if not self.specific or not self.activated:
                if CONFIG["aliases"][alias]["smartcard"] in [
                        True, "true", "True"
                ]:
                    response = encryption.decrypt(
                        CONFIG["aliases"][alias]["passphrase"])
                else:
                    response = encryption.decrypt(
                        CONFIG["aliases"][alias]["passphrase"])
                if not response["success"]: return response
                passphrase = response.decrypted.decode()
                response = aliases.check(alias)
                if not response["success"]: return response
                response = agent.add(
                    private_key=CONFIG["aliases"][alias]["private_key"],
                    passphrase=passphrase)
                if not response["success"]: return response
                if self.specific: self.activated = True
        return self.utils.push(
            # the local path.
            path=path,
            # the ssht00ls alias.
            alias=alias,
            # the remote path.
            remote=remote,
            # exlude subpaths (list) (leave None to use default).
            exclude=exclude,
            # path is directory boolean (leave None to parse automatically).
            directory=directory,
            empty_directory=empty_directory,
            # update deleted files.
            delete=delete,
            # forced mode.
            forced=forced,
            # version control.
            safe=safe,
            # accept new hosts keys.
            accept_new_host_keys=accept_new_host_keys,
            # checks.
            checks=checks,
            check_base=check_base,
            # log level.
            log_level=log_level,
            # get the command in str.
            command=command,
        )
コード例 #3
0
ファイル: __init__.py プロジェクト: vandenberghinc/ssht00ls
    def sync(self, aliases=["*"], interactive=None):
        if interactive == None: interactive = INTERACTIVE

        # all aliases.
        if "*" in aliases or "all" in aliases:
            if self.specific:
                aliases = [self.alias]
            else:
                aliases = list(CONFIG["aliases"].keys())
        _aliases_ = list(aliases)

        # check ssh dir.
        if not Files.exists(f"{syst3m.defaults.vars.home}/.ssh"):
            os.system(
                f"mkdir {syst3m.defaults.vars.home}/.ssh && chown -R {syst3m.defaults.vars.user}:{syst3m.defaults.vars.group} {syst3m.defaults.vars.home}/.ssh && chmod 700 {syst3m.defaults.vars.home}/.ssh"
            )

        # check include.
        include = f"include ~/.ssht00ls/lib/aliases"
        if not Files.exists(f"{syst3m.defaults.vars.home}/.ssh/config"):
            Files.save(f"{syst3m.defaults.vars.home}/.ssh/config", include)
            os.system(
                f"chown {syst3m.defaults.vars.user}:{syst3m.defaults.vars.group} {syst3m.defaults.vars.home}/.ssh/config && chmod 770 {syst3m.defaults.vars.home}/.ssh/config"
            )
        if include not in Files.load(
                f"{syst3m.defaults.vars.home}/.ssh/config"):
            data = Files.load(f"{syst3m.defaults.vars.home}/.ssh/config")
            new, included = "", False
            for line in data.split("\n"):
                if len(line) > 0 and line[0] == "#":
                    a = 1
                elif not included:
                    new += include + "\n"
                    included = True
                new += line + "\n"
            Files.save(f"{syst3m.defaults.vars.home}/.ssh/config", new)

        # iterate.
        aliases, c = "", 0
        for alias in _aliases_:
            info = CONFIG["aliases"][alias]
            if "example.com " not in alias:

                # deprications.
                if "user" in info:
                    user = info["user"]
                    del info["user"]
                    info["username"] = user
                    CONFIG["aliases"][alias]["user"] = user
                    utils.save_config_safely()

                # check existance.
                response = self.check(alias=alias)
                if not response.success: return response

                # proceed.
                checked = Dictionary(path=False, dictionary=info).check(
                    default={
                        "username": None,
                        "public_ip": None,
                        "private_ip": None,
                        "public_port": None,
                        "private_port": None,
                        "private_key": None,
                        "public_key": None,
                        "passphrase": None,
                        "smartcard": None,
                        "pin": None,
                    })
                if Dictionary(checked) != Dictionary(CONFIG["aliases"][alias]):
                    CONFIG["aliases"][alias] = checked
                    utils.save_config_safely()
                if isinstance(checked["private_key"], str):
                    checked["private_key"] = syst3m.env.fill(
                        checked["private_key"])
                    Files.chmod(checked["private_key"], permission=700)
                if isinstance(checked["public_key"], str):
                    checked["public_key"] = syst3m.env.fill(
                        checked["public_key"])
                    Files.chmod(checked["public_key"], permission=700)
                if interactive:
                    passphrase, has_passphrase, new_passphrase = None, True, True
                    if checked["smartcard"] == True:
                        if checked["pin"] in [False, None, "", "none", "None"]:
                            if checked["pin"] in [False, "", "none", "None"]:
                                has_passphrase = False
                            else:
                                passphrase = getpass.getpass(
                                    f"Enter the passphrase of key {checked['private_key']}:"
                                )
                        else:
                            # check encryption activated.
                            if not encryption.activated:
                                return r3sponse.error(
                                    "The encryption requires to be activated.")
                            new_passphrase = False
                            response = encryption.encryption.decrypt(
                                checked["pin"])
                            if not response.success: return response
                            passphrase = response.decrypted.decode()
                    else:
                        if checked["passphrase"] in [
                                False, None, "", "none", "None"
                        ]:
                            if checked["passphrase"] in [
                                    False, "", "none", "None"
                            ]:
                                has_passphrase = False
                            else:
                                passphrase = getpass.getpass(
                                    f"Enter the passphrase of key {checked['private_key']}:"
                                )
                        else:
                            # check encryption activated.
                            if not encryption.activated:
                                return r3sponse.error(
                                    "The encryption requires to be activated.")
                            new_passphrase = False
                            response = encryption.encryption.decrypt(
                                checked["passphrase"])
                            if not response.success: return response
                            passphrase = response.decrypted.decode()
                    if has_passphrase:
                        if checked["smartcard"] == True:
                            response = agent.check(
                                public_key=checked["public_key"], raw=True)
                        else:
                            response = agent.check(
                                public_key=checked["public_key"], raw=False)
                        if not response["success"]:
                            if "is not added" not in response["error"]:
                                return response
                            elif "is not added" in response["error"]:
                                if checked["smartcard"]:
                                    response = agent.add(
                                        private_key=checked["private_key"],
                                        smartcard=True,
                                        pin=passphrase)
                                    if not response["success"]: return response
                                else:
                                    response = agent.add(
                                        private_key=checked["private_key"],
                                        passphrase=passphrase)
                                    if not response["success"]: return response
                        if new_passphrase:
                            # check encryption activated.
                            if not encryption.activated:
                                return r3sponse.error(
                                    "The encryption requires to be activated.")
                            response = encryption.encryption.encrypt(
                                passphrase)
                            if not response.success: return response
                            if checked["smartcard"] == True:
                                CONFIG["aliases"][alias][
                                    "pin"] = response.encrypted.decode()
                            else:
                                CONFIG["aliases"][alias][
                                    "passphrase"] = response.encrypted.decode(
                                    )
                            utils.save_config_safely()
                response = self.create(
                    save=False,
                    checks=False,
                    serialized=Dictionary(dictionary=checked).append(
                        {"alias": alias}))
                if not response["success"]: return response
                aliases += response["str"]
                c += 1

        # save lib.
        Files.save(f"{syst3m.defaults.vars.home}/.ssht00ls/lib/aliases",
                   aliases)

        # handler.
        return r3sponse.success(f"Successfully synchronized {c} alias(es).")
コード例 #4
0
ファイル: __init__.py プロジェクト: vandenberghinc/ssht00ls
    def sync(self, aliases=["*"], interactive=None, log_level=None):
        if interactive == None:
            interactive = dev0s.defaults.options.interactive
        if log_level == None: log_level = self.log_level

        # all aliases.
        if "*" in aliases or "all" in aliases:
            if self.specific:
                aliases = [self.alias]
            else:
                aliases = self.list()["array"]
        _aliases_ = list(aliases)

        # loader.
        if log_level >= 0:
            loader = dev0s.console.Loader(
                f"Synchronizing {len(_aliases_)} aliases.")

        # check ssh dir.
        if not Files.exists(f"{dev0s.defaults.vars.home}/.ssh"):
            os.system(
                f"mkdir {dev0s.defaults.vars.home}/.ssh && chown -R {dev0s.defaults.vars.user}:{dev0s.defaults.vars.group} {dev0s.defaults.vars.home}/.ssh && chmod 700 {dev0s.defaults.vars.home}/.ssh"
            )

        # check include.
        include = f"include ~/.ssht00ls/lib/aliases"
        if not Files.exists(f"{dev0s.defaults.vars.home}/.ssh/config"):
            Files.save(f"{dev0s.defaults.vars.home}/.ssh/config", include)
            os.system(
                f"chown {dev0s.defaults.vars.user}:{dev0s.defaults.vars.group} {dev0s.defaults.vars.home}/.ssh/config && chmod 770 {dev0s.defaults.vars.home}/.ssh/config"
            )
        if include not in Files.load(
                f"{dev0s.defaults.vars.home}/.ssh/config"):
            data = Files.load(f"{dev0s.defaults.vars.home}/.ssh/config")
            new, included = "", False
            for line in data.split("\n"):
                if len(line) > 0 and line[0] == "#":
                    a = 1
                elif not included:
                    new += include + "\n"
                    included = True
                new += line + "\n"
            Files.save(f"{dev0s.defaults.vars.home}/.ssh/config", new)

        # iterate.
        aliases, c = "", 0
        for alias in _aliases_:
            info = CONFIG["aliases"][alias]
            if "example.com " not in alias:

                # check existance.
                response = self.check(alias=alias)
                if not response.success:
                    if log_level >= 0: loader.stop(success=False)
                    return response

                # proceed.
                checked = Dictionary(path=False, dictionary=info).check(
                    default={
                        "username": None,
                        "public_ip": None,
                        "private_ip": None,
                        "public_port": None,
                        "private_port": None,
                        "private_key": None,
                        "public_key": None,
                        "passphrase": None,
                        "smartcard": None,
                        "serial_numbers": [],
                        "pin": None,
                    })
                if checked["smartcard"] not in [True, False]:
                    checked["smartcard"] = False
                if Dictionary(checked) != Dictionary(CONFIG["aliases"][alias]):
                    CONFIG["aliases"][alias] = checked
                    utils.save_config_safely()
                if isinstance(checked["private_key"], str):
                    checked["private_key"] = dev0s.env.fill(
                        checked["private_key"])
                    Files.chmod(checked["private_key"], permission=700)
                if isinstance(checked["public_key"], str):
                    checked["public_key"] = dev0s.env.fill(
                        checked["public_key"])
                    Files.chmod(checked["public_key"], permission=700)
                if interactive:
                    passphrase, has_passphrase, new_passphrase = None, True, True

                    # smart card.
                    if checked["smartcard"] == True:

                        # pin disabled.
                        if checked["pin"] in [False, None, "", "none", "None"]:

                            # skip when passphrase is False.
                            if checked["pin"] in [False]:
                                has_passphrase = False

                            # prompt when passphrase is invalid.
                            else:
                                if log_level >= 0: loader.hold()
                                passphrase = getpass.getpass(
                                    f"Enter the pin of smartcard [{gfp.clean(checked['private_key'])}]:"
                                )
                                if log_level >= 0: loader.release()

                        # prompt for pin.
                        elif str(checked["pin"]).lower() in ["prompt"]:
                            loader.hold()
                            passphrase = dev0s.console.input(
                                f"Enter the pin code for smartcard {alias}:",
                                password=True)
                            loader.release()

                        # has pincode cached.
                        else:

                            # check encryption activated.
                            if not ssht00ls_agent.activated:

                                if log_level >= 0: loader.stop(success=False)
                                return dev0s.response.error(
                                    f"The {ssht00ls_agent.id} encryption requires to be activated."
                                )

                            # retrieve pass.
                            new_passphrase = False
                            response = ssht00ls_agent.encryption.decrypt(
                                checked["pin"])
                            if not response.success:
                                if log_level >= 0: loader.stop(success=False)
                                return response
                            passphrase = response.decrypted.decode()

                    # no smart card.
                    else:
                        if checked["passphrase"] in [
                                False, None, "", "none", "None"
                        ]:
                            if checked["passphrase"] in [
                                    False, "", "none", "None"
                            ]:
                                has_passphrase = False
                            else:
                                if log_level >= 0: loader.hold()
                                passphrase = getpass.getpass(
                                    f"Enter the passphrase of key [{gfp.clean(checked['private_key'])}] (leave '' for no passphrase):"
                                )
                                if log_level >= 0: loader.release()
                                if checked["passphrase"] in [
                                        False, "", "none", "None"
                                ]:
                                    has_passphrase = False
                                    CONFIG["aliases"][alias]["passphrase"] = ""
                                    utils.save_config_safely()
                        else:
                            # check encryption activated.
                            if not ssht00ls_agent.activated:

                                if log_level >= 0: loader.stop(success=False)
                                return dev0s.response.error(
                                    f"The {ssht00ls_agent.id} encryption requires to be activated."
                                )
                            new_passphrase = False
                            response = ssht00ls_agent.encryption.decrypt(
                                checked["passphrase"])
                            if not response.success:
                                if log_level >= 0: loader.stop(success=False)
                                return response
                            passphrase = response.decrypted.decode()

                    # add to agent.
                    if has_passphrase:
                        if checked["smartcard"] == True:
                            response = agent.check(
                                public_key=checked["public_key"], raw=True)
                        else:
                            response = agent.check(
                                public_key=checked["public_key"], raw=False)
                        if not response["success"]:
                            if "is not added" not in response["error"]:
                                if log_level >= 0: loader.stop(success=False)
                                return response
                            elif "is not added" in response["error"]:
                                if checked["smartcard"]:
                                    response = agent.add(
                                        private_key=checked["private_key"],
                                        smartcard=True,
                                        pin=passphrase)
                                    if not response["success"]:
                                        if log_level >= 0:
                                            loader.stop(success=False)
                                        return response
                                else:
                                    response = agent.add(
                                        private_key=checked["private_key"],
                                        passphrase=passphrase)
                                    if not response["success"]:
                                        if log_level >= 0:
                                            loader.stop(success=False)
                                        return response
                        if new_passphrase:
                            # check encryption activated.
                            if not ssht00ls_agent.activated:

                                if log_level >= 0: loader.stop(success=False)
                                return dev0s.response.error(
                                    f"The {ssht00ls_agent.id} encryption requires to be activated."
                                )
                            response = ssht00ls_agent.encryption.encrypt(
                                passphrase)
                            if not response.success:
                                if log_level >= 0: loader.stop(success=False)
                                return response
                            if checked["smartcard"] == True:
                                CONFIG["aliases"][alias][
                                    "pin"] = response.encrypted.decode()
                            else:
                                CONFIG["aliases"][alias][
                                    "passphrase"] = response.encrypted.decode(
                                    )
                            utils.save_config_safely()
                response = self.create(
                    save=False,
                    checks=False,
                    serialized=Dictionary(dictionary=checked).append(
                        {"alias": alias}))
                if not response["success"]:
                    if log_level >= 0: loader.stop(success=False)
                    return response
                self.__edit_alias_lib__(alias, response["str"])
                aliases += response["str"]
                c += 1

        # handler.
        if log_level >= 0: loader.stop()
        return dev0s.response.success(
            f"Successfully synchronized {c} alias(es).")
コード例 #5
0
    def index(self,
              path=None,
              alias=None,
              log_level=dev0s.defaults.options.log_level,
              checks=True,
              accept_new_host_keys=True):

        # check specific.
        if self.specific:
            if alias == None: alias = self.alias

        # checks.
        if path == None:
            return dev0s.response.error(f"Define parameter: path.")
        path = gfp.clean(path)

        # check encryption activated.
        if not ssht00ls_agent.activated:
            return dev0s.response.error(
                f"The {ssht00ls_agent.id} encryption requires to be activated."
            )

        # remote.
        if alias != None:

            # checks.
            if checks:

                # check alias.
                response = aliases.check(alias)
                if not response["success"]: return response

                # check passphrase.
                if CONFIG["aliases"][alias]["smartcard"] in [
                        True, "true", "True"
                ]:
                    passphrase = CONFIG["aliases"][alias]["pin"]
                else:
                    passphrase = CONFIG["aliases"][alias]["passphrase"]
                if passphrase not in ["", "none", "None", None]:
                    response = ssht00ls_agent.encryption.decrypt()
                    if not response["success"]: return response
                    passphrase = response.decrypted.decode()
                else:
                    passphrase = None

                # tests.
                response = agent.add(
                    private_key=CONFIG["aliases"][alias]["private_key"],
                    passphrase=passphrase)
                if not response["success"]: return response
                response = ssh.utils.test_ssht00ls(
                    alias=alias, accept_new_host_keys=accept_new_host_keys)
                if not response["success"]: return response
                response = ssh.utils.test_path(
                    alias=alias,
                    path=path,
                    accept_new_host_keys=accept_new_host_keys)
                if not response["success"]: return response

            # index.
            response = self.utils.execute(
                command=
                f"""printf 'yes' | ssh {DEFAULT_SSH_OPTIONS} {alias} ' export IPINFO_API_KEY="{IPINFO_API_KEY}" && python3 /usr/local/lib/ssht00ls/classes/ssync/index.py --path {path} --json --non-interactive --no-checks ' """,
                serialize=True,
                log_level=log_level,
                input={
                    "Are you sure you want to continue connecting":
                    Boolean(accept_new_host_keys).string(true="yes",
                                                         false="no"),
                },
                optional=True,
            )
            if not response.success:
                return dev0s.response.error(
                    f"Failed to connect with {alias}, error: {response.error}")
            else:
                return response

        # local.
        else:
            if checks:
                if not Files.exists(path):
                    return dev0s.response.error(
                        f"Path [{path}] does not exist.")
                elif not os.path.isdir(path):
                    return dev0s.response.error(
                        f"Path [{path}] is not a directory.")

            # handler.
            dict = self.utils.index(path)
            return dev0s.response.success(
                f"Successfully indexed {len(dict)} files from directory [{path}].",
                {
                    "index": dict,
                })
コード例 #6
0
    def pull(
        self,
        # the local path.
        path=None,
        # the ssht00ls alias.
        alias=None,
        # the remote path.
        remote=None,
        # exlude subpaths (list) (leave None to exclude none).
        exclude=[],
        # path is directory boolean (leave None to parse automatically).
        directory=True,
        empty_directory=False,
        # update deleted files.
        delete=False,
        # forced mode.
        forced=False,
        # version control.
        safe=False,
        # accept new hosts keys.
        accept_new_host_keys=True,
        # checks.
        checks=True,
        # log level.
        log_level=dev0s.defaults.options.log_level,
        # get the command in str.
        command=False,
    ):

        # check specific.
        if self.specific:
            if alias == None: alias = self.alias

        # check encryption activated.
        if not ssht00ls_agent.activated:
            return dev0s.response.error(
                f"The {ssht00ls_agent.id} encryption requires to be activated."
            )
        if checks:
            if self.specific:
                try:
                    self.activated
                except:
                    self.activated = False
            if not self.specific or not self.activated:
                response = aliases.check(alias)
                if not response["success"]: return response
                response = agent.check(
                    public_key=CONFIG["aliases"][alias]["public_key"])
                if not response.success:
                    if "is not added to the" not in response.error:
                        return response
                    else:
                        if CONFIG["aliases"][alias]["smartcard"] in [
                                True, "true", "True"
                        ]:
                            passphrase = CONFIG["aliases"][alias]["pin"]
                        else:
                            passphrase = CONFIG["aliases"][alias]["passphrase"]
                        if passphrase not in [None, "None", "none", "", False]:
                            response = ssht00ls_agent.encryption.decrypt(
                                passphrase)
                            if not response["success"]: return response
                            passphrase = response.decrypted.decode()
                        else:
                            passphrase = None
                        response = agent.add(private_key=CONFIG["aliases"]
                                             [alias]["private_key"],
                                             passphrase=passphrase)
                        if not response["success"]: return response
                if self.specific: self.activated = True
        return self.utils.pull(
            # the local path.
            path=path,
            # the ssht00ls alias.
            alias=alias,
            # the remote path.
            remote=remote,
            # exlude subpaths (list) (leave None to use default).
            exclude=exclude,
            # path is directory boolean (leave None to parse automatically).
            directory=directory,
            empty_directory=empty_directory,
            # update deleted files.
            delete=delete,
            # forced mode.
            forced=forced,
            # version control.
            safe=safe,
            # accept new hosts keys.
            accept_new_host_keys=accept_new_host_keys,
            # checks.
            checks=checks,
            # log level.
            log_level=log_level,
            # get the command in str.
            command=command,
        )