10 if sys.hexversion >= 0x30200f0:
11 print('To run "ldap-seeds" in python 3, it requires a python3 '
12 'compatible version of dev-python/python-ldap be installed')
13 print('Currently only dev-python/python-ldap-9999 has that capability')
18 from gkeys.config import GKEY
22 default_server = 'ldap://ldap1.gentoo.org'
23 # add uid to the results so you don't have to
24 # separate it out of the results tuple[0] value
25 default_fields = ['uid', 'cn', 'mail', 'gentooStatus', 'gpgkey', 'gpgfingerprint']
26 default_criteria = 'ou=devs,dc=gentoo,dc=org'
28 # establish a ldap fields to GKEY._fields map
33 'longkeyid': 'gpgkey',
34 # map the uid to keydir, since we want
35 # dev keydir to be separate from each other
37 'fingerprint': 'gpgfingerprint'
39 # Sanity check they are in sync
40 if not sorted(gkey2ldap_map) == sorted(GKEY._fields):
41 raise "Search.py out of sync with GKEY class"
44 # Now for some search field defaults
47 STATUS = '(gentooStatus=%s)'
48 GPGKEY = '(gpgkey=%s)'
50 GPGFINGERPRINT = '(gpgfingerprint=%s)'
58 'fingerprint': GPGFINGERPRINT,
62 class LdapSearch(object):
63 '''Class to perform searches on the configured ldap server
66 def __init__(self, server=None, fields=None, criteria=None):
67 self.server = server or default_server
68 self.fields = fields or default_fields
69 self.criteria = criteria or default_criteria
70 logger.debug('LdapSearch: __init__; server...: %s' % self.server)
71 logger.debug('LdapSearch: __init__; fields...: %s' % self.fields)
72 logger.debug('LdapSearch: __init__; criteria.: %s' % self.criteria)
73 self.ldap_connection = None
76 def connect(self, server=None,):
77 '''Creates our ldap server connection
82 logger.debug('LdapSearch: connect; new server: %s' % self.server)
84 self.ldap_connection = ldap.initialize(self.server)
85 self.ldap_connection.set_option(ldap.OPT_X_TLS_DEMAND, True)
86 self.ldap_connection.start_tls_s()
87 self.ldap_connection.simple_bind_s()
88 except Exception as e:
89 logger.error('LdapSearch: connect; failed to connect to server: %s' % self.server)
90 logger.error("Exception was: %s" % str(e))
92 logger.debug('LdapSearch: connect; connection: %s' % self.ldap_connection)
97 def search(self, target, search_field=UID, fields=None, criteria=None):
98 '''Perform the ldap search
101 logger.debug('LdapSearch: search; invalid target: "%s"' % target)
106 logger.debug('LdapSearch: search; new fields: %s' % str(fields))
108 criteria = self.criteria
110 logger.debug('LdapSearch: search; new criteria: %s' % criteria)
111 results = self.ldap_connection.search_s(criteria,
112 ldap.SCOPE_ONELEVEL, search_field % target, fields)
113 #logger.debug('LdapSearch: search; result = %s' % str(results))
117 def result2dict(self, results, key='uid'):
119 for entry in results:
121 key_value = info[key][0]
122 _dict[key_value] = info