Exemplo n.º 1
0
 def test3(self):
     "LDAP URL to hostname and IP - ldap_url2hostname_ip('ldap://127.0.0.1:2170')."
     exp1 = '[ldap://localhost:2170 [127.0.0.1]]'
     exp2 = '[ldap://%s:2170 [127.0.0.1]]' % LOCALHOST
     res = samutils.ldap_url2hostname_ip('ldap://127.0.0.1:2170')
     if res != exp1 and res != exp2:
         self.fail("Expected '%s' or '%s', got %s" % (exp1, exp2, res))
Exemplo n.º 2
0
 def test3(self):
     "LDAP URL to hostname and IP - ldap_url2hostname_ip('ldap://127.0.0.1:2170')."
     exp1 = '[ldap://localhost:2170 [127.0.0.1]]'
     exp2 = '[ldap://%s:2170 [127.0.0.1]]' % LOCALHOST
     res = samutils.ldap_url2hostname_ip('ldap://127.0.0.1:2170')
     if res != exp1 and res != exp2:
         self.fail("Expected '%s' or '%s', got %s" % (exp1, exp2, res))
Exemplo n.º 3
0
def __return_query_failed_emtpy_set(ldap_url, ldap_attrlist, ldap_filter, ldap_base):
    """Formatted output on empty set returned by a query."""
    ldap_url = samutils.ldap_url2hostname_ip(ldap_url)
    stsmsg = 'No information for [attribute(s): %s] in %s.' % \
                (ldap_attrlist, ldap_url)
    detmsg = 'No information for [base: %s; filter: %s; attribute(s): %s] in %s.' % \
        (ldap_base, ldap_filter, ldap_attrlist, ldap_url)
    return (0, (LDAP_QE_EMPTYSET, stsmsg, detmsg))
Exemplo n.º 4
0
def get_working_ldap(ldaps, net_timeout=LDAP_TIMEOUT_NETWORK):
    """Test given list of LDAP servers and return a first working one as IP
    address.

    Depending on availability uses either LDAP API or CLI.

    @param  ldaps: list of LDAP endpoints (ldap://<hostname>:[<port>]).
    @type ldaps: L{list}
    @param net_timeout: connection timeout (default: L{LDAP_TIMEOUT_NETWORK}).
    @type net_timeout: L{int}

    @return:
      - on success:
          - C{endpoint} - first working LDAP endpoint as IP address
    @rtype: L{str}

    @raises LookupError,TypeError,ValueError:
      - LookupError - if no working endpoints found.
      - TypeError - L{ldaps} must be a list object.
      - ValueError - list of empty endpoints or empty list is given.
    """

    if not isinstance(ldaps, list):
        raise TypeError('ldaps should be a list object.')
    l = len(ldaps)
    if l == 0:
        raise ValueError('Empty LDAP endpoints list given (%s()).' % \
                         sys._getframe(0).f_code.co_name)
    else:
        i = 0
        for v in ldaps:
            if not v:
                i += 1
        if i == l:
            raise ValueError('List of empty LDAP endpoints given (%s()).' % \
                             sys._getframe(0).f_code.co_name)
    failed_ldaps = {}
    for ldap_url in ldaps:
        proto, hostname, port = samutils.parse_uri3(ldap_url)
        try:
            ips = samutils.dns_lookup_forward(hostname)
        except IOError, e:
            # Forward DNS resolution failed. Continue with the next host.
            failed_ldaps[ldap_url] = str(e)
            continue
        else:
            for ip in ips:
                ldap_url_ip = '%s%s:%s' %(proto or '', ip, port)
                if LDAP_LIB:
                    rc, error = __ldap_bind_API(ldap_url_ip, net_timeout)
                    if rc:
                        return ldap_url_ip
                else:
                    rc, error = __ldap_bind_CLI(ldap_url_ip, net_timeout)
                    if rc:
                        return ldap_url_ip
                host_ip = samutils.ldap_url2hostname_ip(ldap_url_ip)
                failed_ldaps[host_ip] = error
Exemplo n.º 5
0
def get_working_ldap(ldaps, net_timeout=LDAP_TIMEOUT_NETWORK):
    """Test given list of LDAP servers and return a first working one as IP
    address.

    Depending on availability uses either LDAP API or CLI.

    @param  ldaps: list of LDAP endpoints (ldap://<hostname>:[<port>]).
    @type ldaps: L{list}
    @param net_timeout: connection timeout (default: L{LDAP_TIMEOUT_NETWORK}).
    @type net_timeout: L{int}

    @return:
      - on success:
          - C{endpoint} - first working LDAP endpoint as IP address
    @rtype: L{str}

    @raises LookupError,TypeError,ValueError:
      - LookupError - if no working endpoints found.
      - TypeError - L{ldaps} must be a list object.
      - ValueError - list of empty endpoints or empty list is given.
    """

    if not isinstance(ldaps, list):
        raise TypeError('ldaps should be a list object.')
    l = len(ldaps)
    if l == 0:
        raise ValueError('Empty LDAP endpoints list given (%s()).' % \
                         sys._getframe(0).f_code.co_name)
    else:
        i = 0
        for v in ldaps:
            if not v:
                i += 1
        if i == l:
            raise ValueError('List of empty LDAP endpoints given (%s()).' % \
                             sys._getframe(0).f_code.co_name)
    failed_ldaps = {}
    for ldap_url in ldaps:
        proto, hostname, port = samutils.parse_uri3(ldap_url)
        try:
            ips = samutils.dns_lookup_forward(hostname)
        except IOError, e:
            # Forward DNS resolution failed. Continue with the next host.
            failed_ldaps[ldap_url] = str(e)
            continue
        else:
            for ip in ips:
                ldap_url_ip = '%s%s:%s' % (proto or '', ip, port)
                if LDAP_LIB:
                    rc, error = __ldap_bind_API(ldap_url_ip, net_timeout)
                    if rc:
                        return ldap_url_ip
                else:
                    rc, error = __ldap_bind_CLI(ldap_url_ip, net_timeout)
                    if rc:
                        return ldap_url_ip
                host_ip = samutils.ldap_url2hostname_ip(ldap_url_ip)
                failed_ldaps[host_ip] = error
Exemplo n.º 6
0
def __return_query_failed_emtpy_set(ldap_url, ldap_attrlist, ldap_filter,
                                    ldap_base):
    """Formatted output on empty set returned by a query."""
    ldap_url = samutils.ldap_url2hostname_ip(ldap_url)
    stsmsg = 'No information for [attribute(s): %s] in %s.' % \
                (ldap_attrlist, ldap_url)
    detmsg = 'No information for [base: %s; filter: %s; attribute(s): %s] in %s.' % \
        (ldap_base, ldap_filter, ldap_attrlist, ldap_url)
    return (0, (LDAP_QE_EMPTYSET, stsmsg, detmsg))
Exemplo n.º 7
0
 def test4(self):
     "LDAP URL to hostname and IP - ldap_url2hostname_ip('localhost:2170')."
     exp = '[ldap://localhost:2170]'
     res = samutils.ldap_url2hostname_ip('localhost:2170')
     self.failUnlessEqual(res, exp, "Expected '%s', got %s" % (exp, res))
Exemplo n.º 8
0
 def test1(self):
     "LDAP URL to hostname and IP - ldap_url2hostname_ip('')."
     res = samutils.ldap_url2hostname_ip('')
     self.failUnlessEqual(res, '[ldap://]',
                          "Expected '[ldap://]', got %s" % res)
Exemplo n.º 9
0
 def test4(self):
     "LDAP URL to hostname and IP - ldap_url2hostname_ip('localhost:2170')."
     exp = '[ldap://localhost:2170]'
     res = samutils.ldap_url2hostname_ip('localhost:2170')
     self.failUnlessEqual(res, exp, "Expected '%s', got %s" % (exp, res))
Exemplo n.º 10
0
 def test1(self):
     "LDAP URL to hostname and IP - ldap_url2hostname_ip('')."
     res = samutils.ldap_url2hostname_ip('')
     self.failUnlessEqual(res, '[ldap://]',
                          "Expected '[ldap://]', got %s" % res)
Exemplo n.º 11
0
    if not isinstance(ldap_attrlist, list):
        raise TypeError('attributes list must be a list object.')

    try:
        l = ldap.initialize(to_full_ldap_url(ldap_url))
        l.protocol_version = ldap.VERSION3
        l.network_timeout = net_timetout
        entries = l.search_st(ldap_base, ldap.SCOPE_SUBTREE,
                              ldap_filter, ldap_attrlist,
                              0, ldap_timelimit)
    except TypeError, e:
        stsmsg = detmsg = 'Error invoking LDAP search API: %s' % str(e)
        return (0, (LDAP_QE_OTHER, stsmsg, detmsg))
    except ErrLDAPTimeout:
        stsmsg = detmsg = 'LDAP search timed out after %i sec. %s' % \
                (ldap_timelimit, samutils.ldap_url2hostname_ip(ldap_url))
        return (0, (LDAP_QE_TIMEOUT, stsmsg, detmsg))
    except ldap.LDAPError, e:
        stsmsg = detmsg = 'LDAPError: %s %s' % (
                                samutils.ldap_url2hostname_ip(ldap_url),
                                e[0]['desc'])
        return (0, (LDAP_QE_LDAP, stsmsg, detmsg))

    if len(entries) == 0:
        return __return_query_failed_emtpy_set(ldap_url, ldap_attrlist,
                                               ldap_filter, ldap_base)
    return (1, entries)

def __ldap_CLI(ldap_filter, ldap_attrlist, ldap_url, ldap_base, ldap_timelimit,
                                                                 net_timetout):
    """Query LDAP using CLI.
Exemplo n.º 12
0
    if not isinstance(ldap_attrlist, list):
        raise TypeError('attributes list must be a list object.')

    try:
        l = ldap.initialize(to_full_ldap_url(ldap_url))
        l.protocol_version = ldap.VERSION3
        l.network_timeout = net_timetout
        entries = l.search_st(ldap_base, ldap.SCOPE_SUBTREE, ldap_filter,
                              ldap_attrlist, 0, ldap_timelimit)
    except TypeError, e:
        stsmsg = detmsg = 'Error invoking LDAP search API: %s' % str(e)
        return (0, (LDAP_QE_OTHER, stsmsg, detmsg))
    except ErrLDAPTimeout:
        stsmsg = detmsg = 'LDAP search timed out after %i sec. %s' % \
                (ldap_timelimit, samutils.ldap_url2hostname_ip(ldap_url))
        return (0, (LDAP_QE_TIMEOUT, stsmsg, detmsg))
    except ldap.LDAPError, e:
        stsmsg = detmsg = 'LDAPError: %s %s' % (
            samutils.ldap_url2hostname_ip(ldap_url), e[0]['desc'])
        return (0, (LDAP_QE_LDAP, stsmsg, detmsg))

    if len(entries) == 0:
        return __return_query_failed_emtpy_set(ldap_url, ldap_attrlist,
                                               ldap_filter, ldap_base)
    return (1, entries)


def __ldap_CLI(ldap_filter, ldap_attrlist, ldap_url, ldap_base, ldap_timelimit,
               net_timetout):
    """Query LDAP using CLI.