user912877 user912877 - 4 months ago 12
Python Question

python ldap attribute query

I am trying to modify a python based-authenticator for murmur (voip software) to work with my ldap tree.

The LDAP authenticator is available at:

http://www.winex.org/linux/zealot/src/mumble-scripts/Authenticators/LDAP/LDAPauth.py

It works, but not quite with my ldap layout, so I have to modify it a bit. I know an approach that could work, but unfortunately I have no more knowledge about python than what I learned from google (I have some other programming expertise though).

My ldap layout looks like this:

charName=xxx, ou=people, dc=xxx, dc=com


Under this there are attributes stored such as
userPassword
and
login
among others.

The python script above is tailored to use a ldap bind to authenticate. In this case I would have to bind as
"charName=logindatafromapp, ou=people, dc=xxx, dc=com"
. Unfortunately people don't log in with
"charName"
but with
"login"
which is an attribute, but isn't identical with
"charName"
.

I do not know a way to bind to an attribute, so here is my idea:


  • I first bind as ldap admin and perform a search over all entries for
    "logindatafromapp"
    and match that value against
    "login"
    . If a match is found I grab the matching
    "charName"
    and re-bind with that
    charName
    as originally intended.



I am currently stuck on querying the
"charName"
value and at assigning that value to a variable, so i could use it in a second ldap bind (google didn't really help me).

Here is my code:

ldap_conn = ldap.initialize(ldap_uri, 0)
ldap_conn.bind_s("cn=admin,dc=xxxxxxxx,dc=com","pass")
res = ldap_conn.search_s('ou=people,dc=xxxxxx,dc=com', ldap.SCOPE_ONELEVEL,'login=trony',['charName'])
print(res)


It then prints
"[('charName=Trony,ou=people,dc=xxxxxxx,dc=com', {'charName': ['Trony']})]"
.

(the
"login=trony"
) is a temporary filter that I would have to replace with the
applogin
var. My problem is now how can I assign
"Trony"
(in this case) to a variable? The output seems to be a special struct?

Answer

'Trony' is in

res[0][1]['charName'][0]

You take the first element of the list — it's a tuple; then the first element of the tuple; it's a dictionary; then value of the dictionary for the key 'charName'; it's a list once again; and then the first element of the list.