Referral support (was: port of Net::LDAP::Entry)

Robert Sander ml-it-python-ldap-dev at epigenomics.com
Sun Jul 28 19:48:44 CEST 2002


On Fri, 26 Jul 2002 10:43:28 +0000 (UTC),
 Michael Ströder <michael at stroeder.com> wrote:
>> We have a "wrapper" search function that will deal with this.
> 
> This might be a nice inspiration for python-ldap.

Good, I haven't tested it very well, but it seems to work.

Here it is:

    import ldap, ldapurl

    def ldapsearch(self,
                   server,
                   base,
                   scope = ldap.SCOPE_SUBTREE,
                   filter,
                   attrs = None,
                   attrsonly = 0,
                   binddn = None,
                   bindpw = None,
                   debug = None):
        if debug:
            print "%s [%s]: %s; %s" % (server, base, filter, attrs)
	l = ldap.open(server)
	l.set_option(ldap.OPT_PROTOCOL_VERSION, ldap.VERSION3)
        l.set_option(ldap.OPT_REFERRALS, 0)
        l.simple_bind_s(binddn, bindpw)
        hash = {}
        if self.debug:
            print " searching in %s for %s" % ( base, filter )
        try:
            for entry, values in l.search_s(base, scope, filter, attrs, attrsonly):
                if entry:
                    hash[entry] = values
                    if debug:
                        print "  found %s" % entry
                else:
                    for value in values:
                        if debug:
                            print " got reference to %s" % value
                        url = ldapurl.LDAPUrl(value)
                        hash.update(ldapsearch(server=url.hostport, base=url.dn, scope=scope, filter=filter, attrs=attrs, attrsonly=attrsonly, binddn=binddn, bindpw=bindpw, debug=debug))
        except ldap.REFERRAL, er:
            if er.has_key('info'):
                url = ldapurl.LDAPUrl(er['info'])
                hash.update(ldapsearch(server=url.hostport, base=url.dn, scope=scope, filter=filter, attrs=attrs, attrsonly=attrsonly, binddn=binddn, bindpw=bindpw, debug=debug))
        l.unbind()
        return hash

It fills a hash indexed by the dn with the value of the dn.

Currently it needs the same binddn on every server accessed. But you can
easily integrate a method that returns a new binddn and bindpw depending
on the server and/or the search base.

Greetings
-- 
Robert Sander
Manager
Information Systems        www.epigenomics.com        Kastanienallee 24
+493024345330                                              10435 Berlin





More information about the python-ldap mailing list