示例#1
0
    def run(self, params={}):
        formatter = ADUtils()
        conn = self.connection.conn
        query = params.get('search_filter')

        query = query.replace("\\>=", ">=")
        query = query.replace("\\<=", "<=")

        # find pars of `(` `)`
        pairs = formatter.find_parentheses_pairs(query)

        # replace ( and ) when they are part of a name rather than a search parameter
        escaped_query = formatter.escape_brackets_for_query(query, pairs)
        self.logger.info(f"Escaped query: {escaped_query}")

        attributes = params.get(Input.ATTRIBUTES)
        if not attributes:
            attributes = [
                ldap3.ALL_ATTRIBUTES, ldap3.ALL_OPERATIONAL_ATTRIBUTES
            ]

        conn.search(search_base=params.get('search_base'),
                    search_filter=escaped_query,
                    attributes=attributes)

        result_list_json = conn.response_to_json()
        result_list_object = json.loads(result_list_json)
        entries = result_list_object["entries"]

        return {Output.RESULTS: entries, Output.COUNT: len(entries)}
示例#2
0
    def run(self, params={}):
        conn = self.connection.conn
        dn = params.get('distinguished_name')
        dn = ADUtils.dn_normalize(dn)
        temp_list = ADUtils.dn_escape_and_split(dn)
        dc_list = [s for s in temp_list if 'DC' in s]
        dc = ','.join(dc_list)
        escaped_dn = ','.join(temp_list)

        pairs = ADUtils.find_parentheses_pairs(escaped_dn)
        self.logger.info(pairs)
        # replace ( and ) when they are part of a name rather than a search parameter
        if pairs:
            for key, value in pairs.items():
                tempstring = escaped_dn
                if tempstring.find('=', key, value) == -1:
                    escaped_dn = escaped_dn[:value] + '\\29' + escaped_dn[
                        value + 1:]
                    escaped_dn = escaped_dn[:key] + '\\28' + escaped_dn[key +
                                                                        1:]

        self.logger.info(escaped_dn)

        conn.search(search_base=dc,
                    search_filter=f'(distinguishedName={escaped_dn})',
                    attributes=['userAccountControl'])
        results = conn.response
        dn_test = [d['dn'] for d in results if 'dn' in d]
        try:
            dn_test[0]
        except Exception as ex:
            self.logger.error('The DN ' + escaped_dn + ' was not found')
            raise PluginException(cause="The DN was not found",
                                  assistance="The DN " + escaped_dn +
                                  " was not found") from ex
        user_list = [d['attributes'] for d in results if 'attributes' in d]
        user_control = user_list[0]
        try:
            account_status = user_control['userAccountControl']
        except Exception as ex:
            self.logger.error('The DN ' + escaped_dn + ' is not a user')
            raise PluginException(cause="The DN is not a user",
                                  assistance="The DN " + escaped_dn +
                                  " is not a user") from ex
        user_account_flag = 2
        account_status = account_status | user_account_flag

        conn.modify(
            escaped_dn,
            {'userAccountControl': [(MODIFY_REPLACE, [account_status])]})
        result = conn.result
        output = result['description']

        if result['result'] == 0:
            return {'success': True}

        self.logger.error('failed: error message %s' % output)
        return {'success': False}
    def run(self, params={}):
        formatter = ADUtils()
        conn = self.connection.conn
        dn = params.get('distinguished_name')
        dn, search_base = formatter.format_dn(dn)
        self.logger.info(f'Escaped DN {dn}')

        pairs = formatter.find_parentheses_pairs(dn)
        self.logger.info(pairs)
        # replace ( and ) when they are part of a name rather than a search parameter
        if pairs:
            dn = formatter.escape_brackets_for_query(dn, pairs)

        self.logger.info(f'Search DN {dn}')

        conn.search(search_base=search_base,
                    search_filter=f'(distinguishedName={dn})',
                    attributes=['userAccountControl'])
        results = conn.response
        dn_test = [d['dn'] for d in results if 'dn' in d]
        try:
            dn_test[0]
        except Exception as ex:
            self.logger.error('The DN ' + dn + ' was not found')
            raise PluginException(cause="The DN was not found",
                                  assistance="The DN " + dn +
                                  " was not found") from ex
        user_list = [d['attributes'] for d in results if 'attributes' in d]
        user_control = user_list[0]
        try:
            account_status = user_control['userAccountControl']
        except Exception as ex:
            self.logger.error('The DN ' + dn + ' is not a user')
            raise PluginException(cause="The DN is not a user",
                                  assistance="The DN " + dn +
                                  " is not a user") from ex
        user_account_flag = 2
        account_status = account_status | user_account_flag

        conn.modify(
            dn, {'userAccountControl': [(MODIFY_REPLACE, [account_status])]})
        result = conn.result
        output = result['description']

        if result['result'] == 0:
            return {'success': True}

        self.logger.error('failed: error message %s' % output)
        return {'success': False}
示例#4
0
    def run(self, params={}):
        conn = self.connection.conn
        query = params.get('search_filter')

        query = ADUtils.dn_normalize(query)
        temp_list = ADUtils.dn_escape_and_split(query)
        query_list = [s for s in temp_list if 'DC' in s]
        query = ','.join(query_list)
        escaped_query = ','.join(temp_list)
        escaped_query = escaped_query.replace("\\>=", ">=")
        escaped_query = escaped_query.replace("\\<=", "<=")

        # find pars of `(` `)`
        pairs = ADUtils.find_parentheses_pairs(escaped_query)

        # replace ( and ) when they are part of a name rather than a search parameter
        for key, value in pairs.items():
            tempstring = escaped_query
            if tempstring.find('=', key, value) == -1:
                escaped_query = escaped_query[:value] + '\\29' + escaped_query[
                    value + 1:]
                escaped_query = escaped_query[:key] + '\\28' + escaped_query[
                    key + 1:]
        self.logger.info(f"Escaped query: {escaped_query}")

        conn.search(search_base=params.get('search_base'),
                    search_filter=escaped_query,
                    attributes=[
                        ldap3.ALL_ATTRIBUTES, ldap3.ALL_OPERATIONAL_ATTRIBUTES
                    ])

        result_list_json = conn.response_to_json()
        result_list_object = json.loads(result_list_json)
        entries = result_list_object["entries"]

        for entry in entries:
            if entry.get("dn"):
                entry["dn"] = entry["dn"].replace("\\", "")

            if entry.get("attributes") and entry.get("attributes").get(
                    "distinguishedName"):
                entry.get("attributes")["distinguishedName"] = \
                    entry.get("attributes").get("distinguishedName").replace("\\", "")

        return {'results': entries}
    def run(self, params={}):
        formatter = ADUtils()
        conn = self.connection.conn
        dn = params.get(Input.DISTINGUISHED_NAME)
        attribute = params.get(Input.ATTRIBUTE_TO_MODIFY)
        attribute_value = params.get(Input.ATTRIBUTE_VALUE)
        dn, search_base = formatter.format_dn(dn)
        self.logger.info(f"Escaped DN {dn}")

        pairs = formatter.find_parentheses_pairs(dn)
        # replace ( and ) when they are part of a name rather than a search parameter
        if pairs:
            dn = formatter.escape_brackets_for_query(dn)

        self.logger.info(dn)

        # Check that the distinguishedName is valid
        conn.search(
            search_base=search_base,
            search_filter=f"(distinguishedName={dn})",
            attributes=[ALL_ATTRIBUTES, ALL_OPERATIONAL_ATTRIBUTES],
        )
        result = conn.response_to_json()
        result_list_object = loads(result)
        entries = result_list_object["entries"]

        dn_test = [d["dn"] for d in entries if "dn" in d]
        if len(dn_test) == 0:
            self.logger.error("The DN " + dn + " was not found")
            raise PluginException(cause="The DN was not found.",
                                  assistance="The DN " + dn + " was not found")

        # Update attribute
        dn = formatter.unescape_asterisk(dn)
        conn.modify(dn, {attribute: [(MODIFY_REPLACE, [attribute_value])]})
        result = conn.result
        output = result["description"]

        if result["result"] == 0:
            return {Output.SUCCESS: True}

        self.logger.error("failed: error message %s" % output)
        return {Output.SUCCESS: False}