LDAP search with username as variable

Question:

I am using the Python-LDAP module and trying to make a query on the logged in user. The username will be passed into the query. When I simply type the username in as a string my results come out correctly.

But if I try to pass the (username) variable it returns
LDAPError - FILTER_ERROR: {'desc': u'Bad search filter'}
I’ve tried a number of different combinations but continue to get the same error returned. Any insight here would be great!

Edited for Minimal, Complete, and Verifiable example:

import ldap

LDAP_SERVER = "ldap://myldapserver.domain.ad:389"
username = r"domainserviceAccount"
password = "Password"

l = ldap.initialize(LDAP_SERVER)
def login(username, password):
    try:
        l.simple_bind_s(username, password)
        base = "OU=Users,OU=Group,DC=domain,DC=ad"

        criteria = "(&(objectClass=user)(sAMAccountName=anActualUsername))" #WORKS    
        criteria = '(&(objectClass=user)(sAMAccountName=%s))' % username #DOESNT WORK
        criteria = "(&(objectClass=user)" + "(sAMAccountName=" + username + "))" #DOESNT WORK

        attributes = ['displayName']
        result = l.search_s(base, ldap.SCOPE_SUBTREE, criteria, attributes)
        print result
    except ldap.INVALID_CREDENTIALS:
        return False
    return True

login(username,password)
Asked By: Infinity8

||

Answers:

Did you try to encode your string ?

criteria = ('(&(objectClass=user)(sAMAccountName=%s))' % username).encode('utf8')
Answered By: julien2313

In the “WORKS” case, your filter string contains a simple name with no domain:

    (&(objectClass=user)(sAMAccountName=bobsmith))

In the “DOESN’T WORK” case, you use a name with a domain:

    (&(objectClass=user)(sAMAccountName=domainserviceAccount)

The character is not allowed in a filter string unless it is escaped.

How to fix this depends upon the data present in your ldap server. Perhaps this:

criteria = '(&(objectClass=user)(sAMAccountName=%s))' % (
    username if '\' not in username else username.split('\')[1])

Or perhaps this:

criteria = '(&(objectClass=user)(sAMAccountName=%s))' % (
    ldap.filter.escape_filter_chars(username))
Answered By: Robᵩ

I needed to use ldap.filter.filter_format for proper character escaping.

import ldap.filter

criteria= ldap.filter.filter_format('(&(objectClass=user)(sAMAccountName=%s))', [username])
Answered By: Infinity8

Try switching single quotes with double quotes.

criteria = "(&(objectClass=user)(sAMAccountName=anActualUsername))" #WORKS    
criteria = '(&(objectClass=user)(sAMAccountName=%s))' % username #DOESNT WORK

the second criteria change it to this one (I didn’t try with %s but only string):

criteria = "(&(objectClass=user)(sAMAccountName=%s))" % username #SHOULD WORK
Answered By: Rafael Gramoschi