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'
41 # Now for some search field defaults
44 STATUS = '(gentooStatus=%s)'
45 GPGKEY = '(gpgkey=%s)'
47 GPGFINGERPRINT = '(gpgfingerprint=%s)'
55 'fingerprint': GPGFINGERPRINT,
59 class LdapSearch(object):
60 '''Class to perform searches on the configured LDAP server
63 def __init__(self, server=None, fields=None, criteria=None):
64 self.server = server or default_server
65 self.fields = fields or default_fields
66 self.criteria = criteria or default_criteria
67 logger.debug('LdapSearch: __init__; server...: %s' % self.server)
68 logger.debug('LdapSearch: __init__; fields...: %s' % self.fields)
69 logger.debug('LdapSearch: __init__; criteria.: %s' % self.criteria)
70 self.ldap_connection = None
73 def connect(self, server=None,):
74 '''Creates our LDAP server connection
79 logger.debug('LdapSearch: connect; new server: %s' % self.server)
81 self.ldap_connection = ldap.initialize(self.server)
82 self.ldap_connection.set_option(ldap.OPT_X_TLS_DEMAND, True)
83 self.ldap_connection.start_tls_s()
84 self.ldap_connection.simple_bind_s()
85 except Exception as e:
86 logger.error('LdapSearch: connect; failed to connect to server: %s' % self.server)
87 logger.error("Exception was: %s" % str(e))
89 logger.debug('LdapSearch: connect; connection: %s' % self.ldap_connection)
93 def search(self, target, search_field=UID, fields=None, criteria=None):
94 '''Perform the LDAP search
97 logger.debug('LdapSearch: search; invalid target: "%s"' % target)
102 logger.debug('LdapSearch: search; new fields: %s' % str(fields))
104 criteria = self.criteria
106 logger.debug('LdapSearch: search; new criteria: %s' % criteria)
107 results = self.ldap_connection.search_s(criteria,
108 ldap.SCOPE_ONELEVEL, search_field % target, fields)
109 #logger.debug('LdapSearch: search; result = %s' % str(results))
113 def result2dict(self, results, key='uid'):
115 for entry in results:
117 key_value = info[key][0]
118 _dict[key_value] = info