HI!
I've committed a method LDAPObject.supported_ldap_version() to
Lib/ldap/ldapobject.py in CVS.
See the code snippet below. Please comment whether you find it
useful or annoying. Personally I'm using this code in web2ldap for
quite some while now and it seems to work with LDAP servers from
several vendors.
Hmm, I also thought about placing something like this in
LDAPObject.bind() to avoid an extra BindRequest. But this might be
too much hidden magic.
Ciao, Michael.
--------------------------------------------------------------------------
def supported_ldap_version(self):
"""
supported_ldap_version() -> int
Tries to negotiate the highest supported protocol version.
First this method binds anonymously with LDAPv3. If
that fails with info field 'version not supported' the
connection is completely dropped and re-openend like described
in RFC2251.
Mainly this is useful when connecting to a LDAP server without
prior knowledge. If you know the highest protocol version
supported by your server you won't need this method.
Caveat is that it sends an extra BindRequest to the server and
it does not work if the server does not allow anonymous bind
or mandates SASL bind.
The result of this method is an integer containing
the negotiated protocol version.
"""
# Try to set protocol version
self.set_option(ldap.OPT_PROTOCOL_VERSION,ldap.VERSION3)
# first try LDAPv3 bind
try:
# Try to bind to provoke error reponse at this very time
# if protocol version is not supported
self.bind_s('','',ldap.AUTH_SIMPLE)
except ldap.PROTOCOL_ERROR,e:
# Make sure that error just happened because of wrong
# protocol version
if hasattr(e,'args') and \
type(e.args)==type(()) and \
type(e.args[0])==type({}) and \
e.args[0].get('info','').lower()=='version not supported':
# Drop connection completely
self.unbind_s() ; del self._l
# Reconnect to host
self._l = self._ldap_call(_ldap.initialize,self._uri)
# Switch to new connection to LDAPv2
self.set_option(ldap.OPT_PROTOCOL_VERSION,ldap.VERSION2)
else:
# Raise any other error exception
raise e
# Set currently determined protocol version
protocol_version = ldap.VERSION2
else:
protocol_version = ldap.VERSION3
return protocol_version
|