-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathldap_access.py
58 lines (42 loc) · 1.53 KB
/
ldap_access.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/python
import ldap
import pprint
import ConfigParser
def get_user_infos():
user_infos = []
config = ConfigParser.RawConfigParser()
config.read('avatar.cfg')
LDAP_SERVER = config.get('LDAP', 'SERVER')
LDAP_USERNAME = config.get('LDAP', 'USERNAME')
LDAP_PASSWORD = config.get('LDAP', 'PASSWORD')
LDAP_BASE_DN = config.get('LDAP', 'BASE_DN')
LDAP_FILTER = config.get('LDAP', 'FILTER')
print(LDAP_BASE_DN)
print(LDAP_FILTER)
attrs = ['proxyAddresses','thumbnailPhoto']
try:
# build a client
ldap_client = ldap.initialize(LDAP_SERVER)
# perform a synchronous bind
ldap_client.set_option(ldap.OPT_REFERRALS,0)
ldap_client.simple_bind_s(LDAP_USERNAME, LDAP_PASSWORD)
except ldap.INVALID_CREDENTIALS:
ldap_client.unbind()
print 'Wrong username or password'
except ldap.SERVER_DOWN:
print 'AD server not available'
result = ldap_client.search_s(LDAP_BASE_DN, ldap.SCOPE_SUBTREE, LDAP_FILTER, attrs)
ldap_client.unbind()
for dn,entry in result:
if not 'proxyAddresses' in entry:
continue
proxyAddresses = entry['proxyAddresses']
smtp_addresses = [ a[5:].lower() for a in proxyAddresses if a.lower().startswith('smtp:')]
photo = entry['thumbnailPhoto'][0]
user = (smtp_addresses, photo)
user_infos.append(user)
return user_infos
if __name__ == "__main__":
user_infos = get_user_infos()
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(user_infos)