Created
November 9, 2018 21:27
-
-
Save Evidlo/07856cc1bfe255bbea6715754d36ec9c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Evan Widloski - 2018-11-05 | |
import ldap # this is python-ldap | |
import sys | |
import ldif | |
from ldap import modlist as modlist | |
import pprint | |
# set debugging level | |
ldap.set_option(ldap.OPT_DEBUG_LEVEL, 1) | |
# get pass from password manager | |
import subprocess | |
process = subprocess.Popen( | |
['ph', 'show', 'edu/illinois', '-f', 'password'], | |
stdout=subprocess.PIPE | |
) | |
password, err = process.communicate() | |
# set login credential | |
LDAP_SERVER = "ldap://ad.uillinois.edu:389" | |
BIND_DN = "cn=evanw3,ou=People,dc=ad,dc=uillinois,dc=edu" | |
BIND_PASS = password.decode('utf8') | |
ldap_connection = ldap.initialize( | |
LDAP_SERVER, | |
trace_level=1, | |
trace_file=sys.stderr | |
) | |
ldap_connection.start_tls_s() | |
ldap_connection.simple_bind_s(BIND_DN, BIND_PASS) | |
# look myself up | |
BASE_DN = "ou=People,dc=ad,dc=uillinois,dc=edu" | |
dn, entry = ldap_connection.search_s(BASE_DN, ldap.SCOPE_SUBTREE, "(cn=evanw3)")[0] | |
# dump LDAP data to file | |
pp = pprint.PrettyPrinter() | |
entry_pretty = pp.pformat(entry) | |
with open('/tmp/dump', 'w') as f: | |
f.write(entry_pretty) | |
# try to add myself to engr-acm-users | |
# https://mail.python.org/pipermail/python-ldap/2011q3/003001.html | |
GROUP_DN = b'CN=engr-acm-users,OU=Organizational,OU=UsersAndGroups,OU=ACMInfrastructure,OU=ACM,OU=CS,OU=Delegated,OU=Engineering,OU=Urbana,DC=ad,DC=uillinois,DC=edu' | |
modlist = [ | |
( | |
ldap.MOD_ADD, | |
'memberOf', | |
[GROUP_DN] | |
) | |
] | |
ldif.LDIFWriter(sys.stderr).unparse(dn, modlist) | |
ldap_connection.modify_s(dn, modlist) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment