Пример #1
0
    def read(self):
        try:
            users_fd = open(self.users_file, "r")
            policies_fd = open(self.policies_file, "r")
        except IOError as e:
            self.log(e, "E")
            return False
        for line in users_fd.readlines():
            l = line.strip().encode().split(b":")
            # TODO: move this check somewhere else
            if len(l) > 2:
                self.log("More than one ':' while parsing users file?", "E")
                return False
            if b"group" in l[0][0:5]:
                self.key_manager.add_group(l[0].split(b" ")[1])
                for u in l[1].strip().split(b" "):
                    self.key_manager.add_group_member(u, l[0].split(b" ")[1])
            else:
                self.key_manager.add_user(l[0].strip(), l[1].strip())
        users_fd.close()

        cnt = 0
        for line in policies_fd.readlines():
            cnt += 1
            tokens = line.strip().encode().split(b" ")
            policy = Policy()
            policy.parameters = []
            prev_token = None
            for token in tokens:
                if prev_token == b'-u':
                    policy.user = token
                elif prev_token == b'-g':
                    policy.group = token
                elif prev_token == b'-p':
                    policy.parameters.append(token)
                prev_token = token
            policy.script = tokens[-1]
            if not self.policy_manager.add_policy(policy):
                self.log("    File %s line %d" % (config["path_to_policies"], cnt), "E")
        policies_fd.close()