Esempio n. 1
0
    def get_status(self):
        # verify content of /etc/group
        grep_result = self.node.run(
            "grep -e '^{}:' /etc/group".format(self.name),
            may_fail=True,
        )
        if grep_result.return_code != 0:
            return ItemStatus(correct=self.attributes['delete'], info={'exists': False})

        status = ItemStatus(correct=not self.attributes['delete'], info={'exists': True})
        status.info.update(_parse_group_line(grep_result.stdout_text))

        if self.attributes['gid'] is not None and \
                status.info['gid'] != self.attributes['gid']:
            status.correct = False

        return status
Esempio n. 2
0
    def get_status(self):
        # verify content of /etc/passwd
        passwd_grep_result = self.node.run(
            "grep -e '^{}:' /etc/passwd".format(self.name),
            may_fail=True,
        )
        if passwd_grep_result.return_code != 0:
            return ItemStatus(
                correct=self.attributes['delete'],
                info={'exists': False, 'needs_fixing': sorted(_ATTRIBUTE_OPTIONS.keys())},
            )
        elif self.attributes['delete']:
            return ItemStatus(correct=False, info={
                'exists': True,
                'needs_fixing': sorted(_ATTRIBUTE_OPTIONS.keys()),
            })

        status = ItemStatus(correct=True, info={'exists': True})
        status.info['needs_fixing'] = []

        status.info.update(_parse_passwd_line(passwd_grep_result.stdout_text))

        if self.attributes['gid'] is not None:
            if self.attributes['gid'].isdigit():
                if int(self.attributes['gid']) != status.info['gid']:
                    status.info['needs_fixing'].append('gid')
            elif _group_name_for_gid(self.node, status.info['gid']) != self.attributes['gid']:
                status.info['needs_fixing'].append('gid')

        for fieldname in ('uid', 'full_name', 'home', 'shell'):
            if self.attributes[fieldname] is None:
                continue
            if status.info[fieldname] != self.attributes[fieldname]:
                status.info['needs_fixing'].append(fieldname)

        if self.attributes['password_hash'] is not None:
            if self.attributes['use_shadow']:
                # verify content of /etc/shadow
                shadow_grep_result = self.node.run(
                    "grep -e '^{}:' /etc/shadow".format(self.name),
                    may_fail=True,
                )
                if shadow_grep_result.return_code != 0:
                    status.info['shadow_hash'] = None
                    status.info['needs_fixing'].append('password')
                else:
                    status.info['shadow_hash'] = shadow_grep_result.stdout_text.split(":")[1]
                    if status.info['shadow_hash'] != self.attributes['password_hash']:
                        status.info['needs_fixing'].append('password_hash')
            else:
                if status.info['passwd_hash'] != self.attributes['password_hash']:
                    status.info['needs_fixing'].append('password_hash')

        # verify content of /etc/group
        status.info['groups'] = _groups_for_user(self.node, self.name)

        if self.attributes['groups'] is not None and \
                set(self.attributes['groups']) != set(status.info['groups']):
            status.info['needs_fixing'].append('groups')

        if status.info['needs_fixing']:
            status.correct = False

        return status