def __init__(self, uri, server, port,
                 starttls, tls, no_verify,
                 binddn, bindpw,
                 anonymous, accountattributename, activedirectory=False):
        '''
        Bind to an LDAP directory using passed credentials.
        '''
        self.uri = uri
        self.server = server
        self.port = port
        self.starttls = starttls
        self.tls = tls
        self.binddn = binddn
        self.bindpw = bindpw
        if not HAS_LDAP:
            raise CommandExecutionError(
                'LDAP connection could not be made, the python-ldap module is '
                'not installed. Install python-ldap to use LDAP external auth.'
            )
        if self.starttls and self.tls:
            raise CommandExecutionError(
                'Cannot bind with both starttls and tls enabled.'
                'Please enable only one of the protocols'
            )

        schema = 'ldaps' if tls else 'ldap'
        if self.uri == '':
            self.uri = '{0}://{1}:{2}'.format(schema, self.server, self.port)

        try:
            if no_verify:
                ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT,
                                ldap.OPT_X_TLS_NEVER)

            self.ldap = ldap.initialize('{0}'.format(self.uri))
            self.ldap.protocol_version = 3  # ldap.VERSION3
            self.ldap.set_option(ldap.OPT_REFERRALS, 0)  # Needed for AD

            if not anonymous:
                if not self.bindpw:
                    raise CommandExecutionError(
                        'LDAP bind password is not set: password cannot be empty if auth.ldap.anonymous is False'
                    )
                if self.starttls:
                    self.ldap.start_tls_s()
                self.ldap.simple_bind_s(self.binddn, self.bindpw)
        except Exception as ldap_error:
            raise CommandExecutionError(
                'Failed to bind to LDAP server {0} as {1}: {2}'.format(
                    self.uri, self.binddn, ldap_error
                )
            )
def __init__ ( self , uri , server , port , starttls , tls , no_verify , binddn , bindpw , anonymous , accountattributename , activedirectory = False ) : self . uri = uri self . server = server self . port = port self . starttls = starttls self . tls = tls self . binddn = binddn self . bindpw = bindpw if not HAS_LDAP : raise CommandExecutionError ( 'LDAP connection could not be made, the python-ldap module is ' 'not installed. Install python-ldap to use LDAP external auth.' ) if self . starttls and self . tls : raise CommandExecutionError ( 'Cannot bind with both starttls and tls enabled.' 'Please enable only one of the protocols' ) schema = 'ldaps' if tls else 'ldap' if self . uri == '' : self . uri = '{0}://{1}:{2}' . format ( schema , self . server , self . port ) try : if no_verify : ldap . set_option ( ldap . OPT_X_TLS_REQUIRE_CERT , ldap . OPT_X_TLS_NEVER ) self . ldap = ldap . initialize ( '{0}' . format ( self . uri ) ) self . ldap . protocol_version = 3 self . ldap . set_option ( ldap . OPT_REFERRALS , 0 ) if not anonymous : if not self . bindpw : raise CommandExecutionError ( 'LDAP bind password is not set: password cannot be empty if auth.ldap.anonymous is False' ) if self . starttls : self . ldap . start_tls_s ( ) self . ldap . simple_bind_s ( self . binddn , self . bindpw ) except Exception as ldap_error : raise CommandExecutionError ( 'Failed to bind to LDAP server {0} as {1}: {2}' . format ( self . uri , self . binddn , ldap_error ) )
Bind to an LDAP directory using passed credentials.