Example #1
0
    def testconnection(params):
        """
        Test if the given filename exists.

        :param params:
        :return:
        """
        success = False
        ldap_uri = params.get("LDAPURI")
        if is_true(params.get("TLS_VERIFY")) \
                and (ldap_uri.lower().startswith("ldaps") or
                                    params.get("START_TLS")):
            tls_ca_file = params.get("TLS_CA_FILE") or DEFAULT_CA_FILE
            tls_context = Tls(validate=ssl.CERT_REQUIRED,
                              version=ssl.PROTOCOL_TLSv1,
                              ca_certs_file=tls_ca_file)
        else:
            tls_context = None
        try:
            server_pool = IdResolver.create_serverpool(
                ldap_uri,
                float(params.get("TIMEOUT", 5)),
                tls_context=tls_context)
            l = IdResolver.create_connection(authtype=\
                                                 params.get("AUTHTYPE",
                                                            AUTHTYPE.SIMPLE),
                                             server=server_pool,
                                             user=params.get("BINDDN"),
                                             password=params.get("BINDPW"),
                                             auto_referrals=not params.get(
                                                 "NOREFERRALS"),
                                             start_tls=params.get("START_TLS", False))
            if not l.bind():
                raise Exception("Wrong credentials")
            # search for users...
            l.search(search_base=params["LDAPBASE"],
                     search_scope=ldap3.SUBTREE,
                     search_filter="(&" + params["SEARCHFILTER"] + ")",
                     attributes=[params["HOSTNAMEATTRIBUTE"]])

            count = len(
                [x for x in l.response if x.get("type") == "searchResEntry"])
            desc = _("Your LDAP config seems to be OK, %i machine objects "
                     "found.")\
                % count

            l.unbind()
            success = True

        except Exception as e:
            desc = "{0!r}".format(e)

        return success, desc
Example #2
0
 def _bind(self):
     if not self.i_am_bound:
         server_pool = IdResolver.create_serverpool(
             self.uri, self.timeout, tls_context=self.tls_context)
         self.l = IdResolver.create_connection(
             authtype=self.authtype,
             server=server_pool,
             user=self.binddn,
             password=self.bindpw,
             auto_referrals=not self.noreferrals,
             start_tls=self.start_tls)
         if not self.l.bind():
             raise Exception("Wrong credentials")
         self.i_am_bound = True