abolotnov
abolotnov

Reputation: 4322

python ldap "Bad search filter" error

This filter works just fine in my LDAP browser by python ldap won't pick it up:

(&(!objectClass=computer)(sn=*%s*))

resulting in:

Request Method: GET Request
URL: http://localhost:8000/ldap_find/%D0%B1%D0%BE%D0%BB%D0%BE%D1%82/
Django Version: 1.4
Exception Type: FILTER_ERROR
Exception Value: {'desc': 'Bad search filter'}

here's the code that does it:

try:
    LDAPClient.connect()
    base = AUTH_LDAP_SEARCH_BASE
    scope = ldap.SCOPE_SUBTREE
    filter = '(&(!objectClass=computer)(sn=*%s*))' % search_string
    result_set = list()
    result = LDAPClient.client.search(base.encode(encoding='utf-8'), scope, filter.encode(encoding='utf-8'),['cn','mail'])
    res_type, res_data = LDAPClient.client.result(result)
    for data in res_data:
        if data[0]:
            result_set.append(data)
    return json.dumps(result_set)
except Exception, e:
    raise e
finally:
    LDAPClient.unconnect()

it works fine with simple filters, like

filter = 'sn=*%s*' % search_string

so I'm guessing this is some kind of escaping of & or something inside ldap lib but can't find the root yet.

Upvotes: 2

Views: 8261

Answers (1)

Terry Gardner
Terry Gardner

Reputation: 11134

The search filter syntax is incorrect. Use (&(sn=*%s*)(!(objectClass=computer))). Search filters are well-documented in RFC4511 and RFC4515.

Upvotes: 7

Related Questions