Example #1
0
    def uninstall(self):
        print("Deinstalliere eingeschränkte SFTP Umgebung")

        ssh_service_try()

        users = user_list(self.sftphomedir)

        if self.check_installation():
            rmres = ssh_remove_config(self.sftpgroup)
            if rmres is False:
                exit(3)

        if self.sftphomedir.exists():
            if not is_empty_sequence(users):
                for username in users:
                    user_delete(self.sftphomedir, username,
                                self.on_rmtree_error)

            print("Entferne SSH Home-Verzeichnis: '" + str(self.sftphomedir) +
                  "'")
            shutil.rmtree(str(self.sftphomedir),
                          ignore_errors=False,
                          onerror=self.on_rmtree_error)

        if group_pexists(self.sftpgroup):
            print("Entferne Gruppe: '" + self.sftpgroup + "'")
            retobj = CGROUPDEL[self.sftpgroup].run(retcode=None)
            assert_retcode(
                retobj,
                "Konnte Gruppe nicht entfernen: '" + self.sftpgroup + "'")

        print("Fertig")
Example #2
0
    def install(self):

        print("Installiere eingeschränkte SFTP Umgebung")
        print("Lege Gruppe an: '" + self.sftpgroup + "'")

        if group_pexists(self.sftpgroup):
            print("Gruppe existiert bereits: '" + self.sftpgroup + "'")
        else:
            retobj = CGROUPADD[self.sftpgroup].run(retcode=None)
            groupadderror = "Konnte Gruppe nicht hinzufügen: '" + self.sftpgroup + "'"
            assert_retcode(retobj, groupadderror)
            if not group_pexists(self.sftpgroup):
                raise Exception(groupadderror)

        print("Lege SFTP Home-Verzeichnis an: '" + str(self.sftphomedir) + "'")
        user_createfolder(self.sftphomedir, "root", "root")

        sshconfiguserdir = ssh_config_get_userdir(self.sftpgroup)

        if sshconfiguserdir is None or sshconfiguserdir != str(
                self.sftphomedir):
            print("Lege SSH Konfiguration an")
            ssh_add_config(self.sftpgroup, self.sftphomedir)

        if ssh_service_try() is False:
            print("Die Installation schlug fehl\nStarte Rollback!")
            self.uninstall()

        print("Fertig")
Example #3
0
def user_create(username: str, password: str, sftpgroup: str) -> None:
    retobj = CADDUSER["-g", sftpgroup, "-d", "/in", "-s", "/sbin/nologin",
                      username].run(retcode=None)
    usrerr = "Konnte Benutzer nicht anlegen: '" + username + "'"
    assert_retcode(retobj, usrerr)
    if not user_exists(username):
        raise Exception(usrerr)
    user_change_password(username, password)
Example #4
0
def user_delete(sftphome: Path, username: str,
                rmtree_error_handler: Callable) -> bool:
    print("Entferne Benutzer: '" + username + "'")
    b = False
    if user_exists(username):
        retobj = CUSERDEL[username].run(retcode=None)
        assert_retcode(retobj,
                       "Konnte Benutzer nicht entfernen: '" + username + "'")

    if user_folderexists(sftphome, username):
        user_folderdelete(sftphome, username, rmtree_error_handler)
Example #5
0
def group_pexists(groupname: str) -> bool:
    retobj = CGROUPLIST.run(retcode=None)
    res = assert_retcode(retobj, "Konnte System-Gruppen nicht ermitteln")
    found = [
        row for row in res.split("\n") if row.strip().lower() == groupname
    ]
    return found and len(found) > 0
Example #6
0
def ssh_service(svcstatus: str) -> bool:
    retobj = CSYSTEMCTL[svcstatus, "ssh.service"].run(retcode=None)

    if svcstatus != "status":
        assert_retcode(retobj,
                       "Konnte SSH Status nicht wechseln (" + svcstatus + ")")
    else:
        if len(retobj) > 1 or string_is_empty(retobj[1]):
            sout = retobj[1]
            started = False
            loaded = False
            for r in sout.split("\n"):
                if loaded is False and r.lower().find("loaded: loaded") > -1:
                    loaded = True
                if started is False and r.lower().find(
                        "active: active (running)") > -1:
                    started = True
            return loaded and started
        else:
            raise Exception("SSH Status konnte nicht ermittelt werden")
Example #7
0
def user_createfolder(sftphome: Path,
                      username: str,
                      groupname: str,
                      permissions=0) -> None:
    pstr = str(sftphome)
    if sftphome.exists():
        print("Verzeichnis '" + pstr + "' existiert bereits")
    else:
        retobj = CMAKEDIR[pstr].run(retcode=None)
        assert_retcode(
            retobj, "Verzeichnis konnte nicht erstellt werden: '" + pstr + '"')
        retobj = CCHOWN[username + ":" + groupname, pstr].run(retcode=None)
        assert_retcode(
            retobj,
            "Benutzer/Gruppe für Verzeichnis konnten nicht vergeben werden: '"
            + pstr + " (" + retobj[2] if len(retobj) > 2 else retobj[1] + ")")
        if permissions != 0:
            retobj = CCHMOD[str(permissions), pstr].run(retcode=None)
            assert_retcode(
                retobj,
                "Rechte für Verzeichnis konnten nicht vergeben werden: '" +
                pstr + "'")
Example #8
0
def user_exists(username: str) -> bool:
    retobj = CUSERLIST.run(retcode=None)
    res = assert_retcode(retobj, "Konnte Benutzerliste nicht ermitteln")
    found = [row for row in res.split("\n") if row.strip().lower() == username]
    return found and len(found) > 0