示例#1
0
    async def search_params(self,
                            base_dn: str,
                            scope: rfc4511.Scope,
                            fil: str = None,
                            attrs: list = None,
                            deref_aliases: rfc4511.DerefAliases = None,
                            types_only: bool = False,
                            limit: int = 0,
                            time_limit: int = 0):
        req = rfc4511.SearchRequest()
        req.setComponentByName('baseObject', rfc4511.LDAPDN(base_dn))
        req.setComponentByName('scope', scope)
        if fil:
            req.setComponentByName('filter', parse_filter(fil))
        if attrs:
            attr_sel = rfc4511.AttributeSelection()
            for i, attr in enumerate(attrs):
                attr_sel.setComponentByPosition(i, attr)
            req.setComponentByName('attributes', attr_sel)
        if deref_aliases is None:
            deref_aliases = DerefAliases.NEVER
        req.setComponentByName('derefAliases', deref_aliases)
        req.setComponentByName('typesOnly', rfc4511.TypesOnly(types_only))
        req.setComponentByName('sizeLimit', rfc4511.Integer0ToMax(limit))
        req.setComponentByName('timeLimit', rfc4511.Integer0ToMax(time_limit))

        async for res in self.search(req):
            yield res
示例#2
0
    def test_unpack(self):
        message_id = 1
        proto_op = 'compareResponse'

        test_lm = rfc4511.LDAPMessage()
        test_lm.setComponentByName('messageID', rfc4511.MessageID(message_id))
        test_cr = rfc4511.CompareResponse()
        test_cr.setComponentByName('resultCode', protoutils.RESULT_compareTrue)
        test_cr.setComponentByName('matchedDN', rfc4511.LDAPDN('cn=testing,o=foo'))
        test_cr.setComponentByName('diagnosticMessage', rfc4511.LDAPString(''))
        test_po = rfc4511.ProtocolOp()
        test_po.setComponentByName(proto_op, test_cr)
        test_lm.setComponentByName('protocolOp', test_po)

        # simulate network transmission
        test_lm = encode_decode(test_lm)

        # ensure we successfully unpack the message ID and get back a compareResult
        actual_message_id, actual_cr, actual_controls = protoutils.unpack(proto_op, test_lm)

        self.assertEqual(actual_message_id, message_id)
        self.assertEqual(actual_cr.getComponentByName('resultCode'), protoutils.RESULT_compareTrue)

        # handling of optional controls varies by pyasn1 version
        # should either be None or length 0
        if actual_controls is not None:
            self.assertEqual(len(actual_controls), 0)

        # ensure unpacking another type raises an exception
        with self.assertRaises(exceptions.UnexpectedResponseType):
            protoutils.unpack('bindResponse', test_lm)
示例#3
0
def make_search_request(base_dn, scope, filter=None, limit=None):
    req = rfc4511.SearchRequest()
    req.setComponentByName('baseObject', rfc4511.LDAPDN(base_dn))
    req.setComponentByName('scope', scope)
    if filter:
        req.setComponentByName('filter', parse(filter))
    if limit is not None:
        req.setComponentByName('sizeLimit', rfc4511.Integer0ToMax(limit))
    return req
示例#4
0
    async def compare_params(self, dn, attr_type, attr_value):
        req = rfc4511.CompareRequest()
        req.setComponentByName('entry', rfc4511.LDAPDN(dn))
        ava = rfc4511.AttributeValueAssertion()
        ava.setComponentByName('attributeDesc',
                               rfc4511.AttributeDescription(attr_type))
        ava.setComponentByName('assertionValue',
                               rfc4511.AssertionValue(attr_value))
        req.setComponentByName('ava', ava)

        return await self.compare(req)
示例#5
0
 def add_sasl_bind_in_progress(self, challenge):
     br = rfc4511.BindResponse()
     br.setComponentByName('resultCode',
                           rfc4511.ResultCode('saslBindInProgress'))
     br.setComponentByName('serverSaslCreds',
                           rfc4511.ServerSaslCreds(challenge))
     br.setComponentByName('matchedDN', rfc4511.LDAPDN(''))
     br.setComponentByName('diagnosticMessage', rfc4511.LDAPString(''))
     mid = self._next_add_message_id
     self._next_add_message_id += 1
     self.add_message(protoutils.pack(mid, 'bindResponse', br))
示例#6
0
    async def mod_dn_params(self,
                            dn,
                            new_rdn,
                            del_old_rdn_attr,
                            new_parent=None):
        req = rfc4511.ModifyDNRequest()
        req.setComponentByName('entry', rfc4511.LDAPDN(dn))
        req.setComponentByName('newrdn', rfc4511.RelativeLDAPDN(new_rdn))
        req.setComponentByName('deleteoldrdn',
                               rfc4511.DeleteOldRDN(del_old_rdn_attr))
        if new_parent:
            req.setComponentByName('newSuperior',
                                   rfc4511.NewSuperior(new_parent))

        return await self.mod_dn(req)
示例#7
0
 async def modify_params(self, dn, mod_list):
     req = rfc4511.ModifyRequest()
     req.setComponentByName('object', rfc4511.LDAPDN(dn))
     changes = rfc4511.Changes()
     for i, mod_op in enumerate(mod_list):
         op, attr_type, attr_vals = mod_op
         change = rfc4511.Change()
         change.setComponentByName('operation', op)
         mod = rfc4511.PartialAttribute()
         mod.setComponentByName('type', rfc4511.Type(attr_type))
         vals = rfc4511.Vals()
         for j, val in enumerate(attr_vals):
             vals.setComponentByPosition(j, val)
         mod.setComponentByName('vals', vals)
         change.setComponentByName('modification', mod)
         changes.setComponentByPosition(i, change)
     req.setComponentByName('changes', changes)
     return await self.modify(req)
 def to_proto(self):
     op = rfc4511.ProtocolOp()
     res = rfc4511.SearchResultEntry()
     res.setComponentByName('objectName', rfc4511.LDAPDN(self.dn))
     attrs = rfc4511.PartialAttributeList()
     j = 0
     for attr, vals in self.attrs.items():
         if not vals:
             continue
         _attr = rfc4511.PartialAttribute()
         _attr.setComponentByName('type', rfc4511.AttributeDescription(attr))
         _vals = rfc4511.Vals()
         for i, val in enumerate(vals):
             _vals.setComponentByPosition(i, rfc4511.AttributeValue(val))
         _attr.setComponentByName('vals', _vals)
         attrs.setComponentByPosition(j, _attr)
         j += 1
     res.setComponentByName('attributes', attrs)
     op.setComponentByName('searchResEntry', res)
     return op
示例#9
0
def make_add_request(dn, attrs=None):
    req = rfc4511.AddRequest()
    req.setComponentByName('entry', rfc4511.LDAPDN(dn))
    al = rfc4511.AttributeList()
    if attrs:
        i = 0
        for attr_type, attr_vals in attrs.items():
            attr = rfc4511.Attribute()
            attr.setComponentByName('type',
                                    rfc4511.AttributeDescription(attr_type))
            vals = rfc4511.Vals()
            j = 0
            for val in attr_vals:
                vals.setComponentByPosition(j, rfc4511.AttributeValue(val))
                j += 1
            attr.setComponentByName('vals', vals)
            al.setComponentByPosition(i, attr)
            i += 1
    req.setComponentByName('attributes', al)
    return req
示例#10
0
    async def add_params(self, dn: str, attrs: dict):
        req = rfc4511.AddRequest()
        req.setComponentByName('entry', rfc4511.LDAPDN(dn))
        al = rfc4511.AttributeList()
        if attrs:
            i = 0
            for attr_type, attr_vals in attrs.items():
                attr = rfc4511.Attribute()
                attr.setComponentByName(
                    'type', rfc4511.AttributeDescription(attr_type))
                vals = rfc4511.Vals()
                j = 0
                for val in attr_vals:
                    vals.setComponentByPosition(j, rfc4511.AttributeValue(val))
                    j += 1
                attr.setComponentByName('vals', vals)
                al.setComponentByPosition(i, attr)
                i += 1
        req.setComponentByName('attributes', al)

        return await self.add(req)
示例#11
0
 def add_ldap_result(self,
                     cls,
                     op,
                     result_code=protoutils.RESULT_success,
                     dn='',
                     msg='',
                     controls=None,
                     referral=None):
     mid = self._next_add_message_id
     self._next_add_message_id += 1
     res = cls()
     if referral:
         result_code = protoutils.RESULT_referral
         _referral = rfc4511.Referral()
         for i, uri in enumerate(referral):
             _referral.setComponentByPosition(i, rfc4511.URI(uri))
         res.setComponentByName('referral', _referral)
     res.setComponentByName('resultCode', result_code)
     res.setComponentByName('matchedDN', rfc4511.LDAPDN(dn))
     res.setComponentByName('diagnosticMessage', rfc4511.LDAPString(msg))
     self.add_message(protoutils.pack(mid, op, res, controls))
示例#12
0
    def add_search_res_entry(self, dn, attrs_dict, controls=None):
        """Create a searchResEntry LDAPMessage"""
        sre = rfc4511.SearchResultEntry()
        sre.setComponentByName('objectName', rfc4511.LDAPDN(dn))

        attrs = rfc4511.PartialAttributeList()
        i = 0
        for attr, values in attrs_dict.items():
            _attr = rfc4511.PartialAttribute()
            _attr.setComponentByName('type',
                                     rfc4511.AttributeDescription(attr))
            _vals = rfc4511.Vals()
            for j, val in enumerate(values):
                _vals.setComponentByPosition(j, rfc4511.AttributeValue(val))
            _attr.setComponentByName('vals', _vals)
            attrs.setComponentByPosition(i, _attr)
            i += 1

        sre.setComponentByName('attributes', attrs, controls)

        self.add_message(
            protoutils.pack(self._next_add_message_id, 'searchResEntry', sre))