2424 HAS_LDAP = False
2525
2626# Defaults, override in master config
27- __defopts__ = {'auth.ldap.server' : 'localhost' ,
27+ __defopts__ = {'auth.ldap.uri' : '' ,
28+ 'auth.ldap.server' : 'localhost' ,
2829 'auth.ldap.port' : '389' ,
2930 'auth.ldap.tls' : False ,
3031 'auth.ldap.no_verify' : False ,
@@ -66,38 +67,43 @@ class _LDAPConnection(object):
6667 Setup an LDAP connection.
6768 '''
6869
69- def __init__ (self , server , port , tls , no_verify , binddn , bindpw ,
70+ def __init__ (self , uri , server , port , tls , no_verify , binddn , bindpw ,
7071 anonymous ):
7172 '''
7273 Bind to an LDAP directory using passed credentials.
7374 '''
75+ self .uri = uri
7476 self .server = server
7577 self .port = port
7678 self .tls = tls
7779 self .binddn = binddn
7880 self .bindpw = bindpw
79- schema = 'ldap'
8081 if not HAS_LDAP :
8182 raise CommandExecutionError ('Failed to connect to LDAP, module '
8283 'not loaded' )
84+ if self .uri == '' :
85+ self .uri = 'ldap://{0}:{1}' .format (self .server , self .port )
86+
8387 try :
8488 if no_verify :
8589 ldap .set_option (ldap .OPT_X_TLS_REQUIRE_CERT ,
8690 ldap .OPT_X_TLS_NEVER )
87- if self .tls :
88- schema = 'ldaps'
91+
8992 self .ldap = ldap .initialize (
90- '{0}://{1}:{2} ' .format (schema , self .server , self . port )
93+ '{0}' .format (self .uri )
9194 )
9295 self .ldap .protocol_version = 3 # ldap.VERSION3
9396 self .ldap .set_option (ldap .OPT_REFERRALS , 0 ) # Needed for AD
9497
98+ if self .tls :
99+ self .ldap .start_tls_s ()
100+
95101 if not anonymous :
96102 self .ldap .simple_bind_s (self .binddn , self .bindpw )
97103 except Exception as ldap_error :
98104 raise CommandExecutionError (
99- 'Failed to bind to LDAP server {0}:{1} as {2 }: {3 }' .format (
100- self .server , self . port , self .binddn , ldap_error
105+ 'Failed to bind to LDAP server {0} as {1 }: {2 }' .format (
106+ self .uri , self .binddn , ldap_error
101107 )
102108 )
103109
@@ -112,7 +118,7 @@ def _bind(username, password):
112118 connargs = {}
113119 # config params (auth.ldap.*)
114120 params = {
115- 'mandatory' : ['server' , 'port' , 'tls' , 'no_verify' , 'anonymous' ],
121+ 'mandatory' : ['uri' , ' server' , 'port' , 'tls' , 'no_verify' , 'anonymous' ],
116122 'additional' : ['binddn' , 'bindpw' , 'filter' ],
117123 }
118124
0 commit comments