import logging
import os
import re
+import socket
import time
try:
from pathlib import Path
except ImportError:
from pathlib2 import Path
from functools import cmp_to_key
+from socket import gaierror
# Third party modules
from fb_tools.argparse_actions import TimeoutOptionAction
from ..errors import DpxLdapParseError
from ..errors import DpxLdapSearchError
from ..errors import DpxLdapSessionError
+from ..errors import DpxNoLdapServerAddressError
from ..errors import DpxWriteLdapItemError
from ..xlate import XLATOR, format_list
-__version__ = '1.3.8'
+__version__ = '1.4.0'
LOG = logging.getLogger(__name__)
_ = XLATOR.gettext
max_retries_on_conn_error = 100
max_wait_on_conn_error = 600
+ default_address_family = socket.AF_INET
+
# pattern_re_ldap_dn = (
# '^([a-z][a-z0-9-]*)=(?![ #])(((?![\\="+,;<>]).)|(\\[ \\#="+,;<>])|(\\[a-f0-9][a-f0-9]))*'
# '(,([a-z][a-z0-9-]*)=(?![ #])(((?![\\="+,;<>]).)|(\\[ \\#="+,;<>])|(\\[a-f0-9][a-f0-9]))*)*$'
msg = _('Trying to get LDAP server object for {} ...').format(connect_info.url)
LOG.debug(msg)
+ server_ip = None
+ if connect_info.ip:
+ server_ip = connect_info.ip
+ if self.verbose >= min_verb_level:
+ LOG.debug(_('Already resolved hostname {h!r} to {a!r}.').format(
+ h=connect_info.host, a=str(server_ip)))
+ else:
+ LOG.debug(_('Resolving hostname {!r} to an IP address ...').format(connect_info.host))
+ try:
+ addresses = self.get_address(connect_info.host)
+ except gaierror as e:
+ msg = _('Could not resolve hostname {!r}:').format(connect_info.host)
+ msg += ' ' + str(e)
+ raise DpxNoLdapServerAddressError(msg)
+ if not addresses:
+ msg = _('Could not resolve hostname {!r}.').format(connect_info.host)
+ raise DpxNoLdapServerAddressError(msg)
+ server_ip = addresses[0]
+ connect_info.ip = server_ip
+
server_opts = {}
if connect_info.use_ldaps:
server_opts['use_ssl'] = True
msg += ' ' + pp(server_opts)
LOG.debug(msg)
- ldap_server = Server(connect_info.host, **server_opts)
+ ldap_server = Server(str(server_ip), **server_opts)
if self.verbose > min_verb_level:
LOG.debug(_('LDAP server {s}: {re}').format(s=ldap_server, re=repr(ldap_server)))
cur_try = 0
if self.verbose > min_verb_level:
- msg = _('Connecting to LDAP server {url} as {dn!r} ...').format(
- url=connect_info.url, dn=bind_dn)
+ msg = _('Connecting to LDAP server {url} ({addr}) as {dn!r} ...').format(
+ url=connect_info.url, addr=connect_info.ip, dn=bind_dn)
LOG.debug(msg)
while True:
if self.verbose > 2:
LOG.debug(_(
'Try number {nr} for connecting to LDAP server instance {i!r} ...').format(
- cur_try))
+ nr=cur_try, i=inst))
try:
ldap_connection = Connection(
if self.verbose > 2:
LOG.debug(_(
'Try number {nr} for disconnecting from LDAP server '
- 'instance {i!r} ...').format(cur_try))
+ 'instance {i!r} ...').format(nr=cur_try, i=inst))
try:
ldap_connection.unbind()
ldap_connection = None