-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.py
More file actions
34 lines (30 loc) · 1.04 KB
/
search.py
File metadata and controls
34 lines (30 loc) · 1.04 KB
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
#!/usr/bin/env python
import ldap
import ldif
import sys
import base64
CONSULTA = sys.argv[1]
password = base64.b64decode("your_already_encoded_password")
def authenticate(address, username, password):
conn = ldap.initialize('ldap://' + address)
conn.protocol_version = 3
conn.set_option(ldap.OPT_REFERRALS,0)
try:
conn.simple_bind_s(username, password)
except ldap.INVALID_CREDENTIALS:
return "Usuario ou senha incorreta"
except ldap.SERVER_DOWN:
return "O Servidor parece ter caido"
except ldap.LDAPError, e:
if type(e.message) == dict and e.message.has_key('desc'):
return e.message['desc']
else:
return e
finally:
ldif_writer = ldif.LDIFWriter(sys.stdout)
basedn = "OU=Some_OU,DC=my_domain,DC=local"
results = conn.search_s(basedn,ldap.SCOPE_SUBTREE, "(cn=*%s*)" % CONSULTA)
for dn, entry in results:
ldif_writer.unparse(dn,entry)
# calls the authenticate method
authenticate('dc_hostname', 'user@domain.local', password)