ldap_check.py needs python-ldap module! Code taken from http://homepage.mac.com/mengelhart/python-ldap-samples.html

import ldap

def ldap_check(user, passw):
        try:
                l = ldap.open("127.0.0.1", 389)

                # you should  set this to ldap.VERSION2 if you're using a v2 directory
                l.protocol_version = ldap.VERSION3
                # Pass in a valid username and password to get
                # privileged directory access.
                # If you leave them as empty strings or pass an invalid value
                # you will still bind to the server but with limited privileges.
                cut = 0
                for c in range(1,len(user)):
                        if user[c].isupper():
                                cut = c

                user = user[:cut] + " " + user[cut:]

                username = "cn=" + user + ", ou=people, dc=..., dc=..."
                password  = passw

                # Any errors will throw an ldap.LDAPError exception
                # or related exception so you can ignore the result
                result = l.simple_bind_s(username, password)
                if (result == None):
                        return True
                else:
                        return False
        except ldap.LDAPError, e:
                # print e
                # handle error however you like
                return False

This function will take two arguments: username and password. It then cuts the username at the second uppercase letter and pastes it into a new string with a space: "SebastianBreier" (wikiname) becomes "Sebastian Breier" (ldap commonname). Then it tries to do a simple bind to the ldap server using the username and password (fill in the bind fields in username!). It should return False on all errors, and if the username/password was wrong. It will return true, if everything worked out.

MoinMoin: SebastianBreier/LdapCheck (last edited 2007-10-29 19:09:27 by localhost)