def __init__(self, app=None, handler=None):
        """:func:`burpui.misc.auth.ldap.LdapLoader.__init__` establishes a
        connection to the LDAP server.

        :param app: Instance of the app we are running in
        :type app: :class:`burpui.server.BUIServer`
        """
        self.app = app
        conf = self.app.config['CFG']
        defaults = {
            'host': 'localhost',
            'port': None,
            'encryption': None,
            'binddn': None,
            'bindpw': None,
            'filter': None,
            'base': None,
            'searchattr': 'uid',
            'validate': 'none',
            'version': None,
            'cafile': None
        }
        mapping = {
            'host': 'host',
            'port': 'port',
            'encryption': 'encryption',
            'filt': 'filter',
            'base': 'base',
            'attr': 'searchattr',
            'binddn': 'binddn',
            'bindpw': 'bindpw',
            'validate': 'validate',
            'version': 'version',
            'cafile': 'cafile'
        }
        c = ConfigParser.ConfigParser(defaults)
        with open(conf) as fp:
            c.readfp(fp)
            # Maybe the handler argument is None, maybe the 'priority'
            # option is missing. We don't care.
            try:
                handler.priority = c.getint('LDAP', 'priority')
            except:
                pass
            for (opt, key) in viewitems(mapping):
                try:
                    setattr(self, opt, c.get('LDAP', key))
                except ConfigParser.NoOptionError as e:
                    self.builogger.info(str(e))
                except ConfigParser.NoSectionError as e:
                    self.builogger.error(str(e))

        if self.validate and self.validate.lower() in ['none', 'optional', 'required']:
            self.validate = getattr(ssl, 'CERT_{}'.format(self.validate.upper()))
        else:
            self.validate = None
        if self.version and self.version in ['SSLv2', 'SSLv3', 'SSLv23', 'TLSv1', 'TLSv1_1']:
            self.version = getattr(ssl, 'PROTOCOL_{}'.format(self.version))
        else:
            self.version = None
        self.tls = None
        self.ssl = False
        self.auto_bind = AUTO_BIND_NONE
        if self.encryption == 'ssl':
            self.ssl = True
        elif self.encryption == 'tls':
            self.tls = Tls(local_certificate_file=self.cafile, validate=self.validate, version=self.version)
            self.auto_bind = AUTO_BIND_TLS_BEFORE_BIND
        if self.port:
            try:
                self.port = int(self.port)
            except ValueError:
                self.builogger.error('LDAP port must be a valid integer')
                self.port = None
        self.builogger.info('LDAP host: {0}'.format(self.host))
        self.builogger.info('LDAP port: {0}'.format(self.port))
        self.builogger.info('LDAP encryption: {0}'.format(self.encryption))
        self.builogger.info('LDAP filter: {0}'.format(self.filt))
        self.builogger.info('LDAP base: {0}'.format(self.base))
        self.builogger.info('LDAP search attr: {0}'.format(self.attr))
        self.builogger.info('LDAP binddn: {0}'.format(self.binddn))
        self.builogger.info('LDAP bindpw: {0}'.format('*****' if self.bindpw else 'None'))
        self.builogger.info('TLS object: {0}'.format(self.tls))

        try:
            self.server = Server(host=self.host, port=self.port, use_ssl=self.ssl, get_info=ALL, tls=self.tls)
            self.builogger.debug('LDAP Server = {0}'.format(str(self.server)))
            if self.binddn:
                self.ldap = Connection(self.server, user=self.binddn, password=self.bindpw, raise_exceptions=True, client_strategy=RESTARTABLE, auto_bind=self.auto_bind, authentication=SIMPLE)
            else:
                self.ldap = Connection(self.server, raise_exceptions=True, client_strategy=RESTARTABLE, auto_bind=self.auto_bind)
            with self.ldap:
                self.builogger.debug('LDAP Connection = {0}'.format(str(self.ldap)))
                self.builogger.info('OK, connected to LDAP')
                return

            raise Exception('Not connected')
        except Exception as e:
            self.builogger.error('Could not connect to LDAP: {0}'.format(str(e)))
            self.server = None
            self.ldap = None
dataset/ETHPy150Open ziirish/burp-ui/burpui/misc/auth/ldap.py/LdapLoader.__init__/original