Created
May 26, 2016 20:43
-
-
Save tyler274/5ee6e75e85c2e33667d8ab832806b774 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
#!/usr/bin/python2 | |
from __future__ import print_function | |
from ipalib import api | |
import ldap, sys, getpass, collections | |
# 1. Initialize ipalib | |
# | |
# Run ./python-api.py --help to see the global options. Some useful options: | |
# | |
# -v Produce more verbose output | |
# -d Produce full debugging output | |
# -e in_server=True Force running in server mode | |
# -e xmlrpc_uri=https://foo.com/ipa/xml # Connect to a specific server | |
api.bootstrap_with_global_options(context='example') | |
api.finalize() | |
# You will need to create a connection. If you're in_server, call | |
# Backend.ldap.connect(), otherwise Backend.rpcclient.connect(). | |
if api.env.in_server: | |
api.Backend.ldap2.connect() | |
else: | |
api.Backend.rpcclient.connect() | |
# Now that you're connected, you can make calls to api.Command.whatever(): | |
con = ldap.initialize('ldap://lenin.dabney.moe') | |
# give three tries to enter password | |
tries = 3 | |
success = False | |
while (tries > 0) and (not success): | |
try: | |
bindpass = getpass.getpass('Input your(comptroller) Lenin uid: ') | |
bindpass = getpass.getpass('Input your(comptroller) Lenin password: ') | |
con.simple_bind_s('uid={},ou=people,dc=lenin,dc=dabney,dc=moe'.format(binduser), bindpass) | |
success = True | |
except ldap.INVALID_CREDENTIALS: | |
print( "Invalid password: " + str(tries-1) + " attempts left") | |
tries -= 1 | |
except ldap.LDAPError: | |
print( "Could not connect to the LDAP server") | |
sys.exit(1) | |
if not success: | |
print( "Invalid password") | |
sys.exit(1) | |
def lower_keys(x): | |
if isinstance(x, list): | |
return [lower_keys(v) for v in x] | |
elif isinstance(x, dict): | |
return dict((k.lower(), lower_keys(v)) for k, v in x.iteritems()) | |
else: | |
return x | |
def convert_single_list(user): | |
converted_user = user.copy() | |
for key, value in user.iteritems(): | |
if isinstance(value, list) and len(value) is 1: | |
converted_user[key] = value[0] | |
return converted_user | |
def convert_to_unicode(user): | |
if isinstance(user, basestring): | |
return unicode(user) | |
elif isinstance(user, collections.Mapping): | |
return dict(map(convert_to_unicode, user.iteritems())) | |
elif isinstance(user, collections.Iterable): | |
return type(user)(map(convert_to_unicode, user)) | |
else: | |
return user | |
def add_givenname(user): | |
user['givenname'] = user.get('givenname', user['cn']) | |
return user | |
def clean_displayname(user): | |
user['displayname'] = user['displayname'].strip() | |
return user | |
lenin_users = con.search_s( | |
'ou=people,dc=lenin,dc=dabney,dc=moe', | |
ldap.SCOPE_SUBTREE, | |
'(objectClass=inetOrgPerson)', | |
[ | |
'uid', | |
'givenname', | |
'sn', | |
'cn', | |
'displayname', | |
'homedirectory', | |
'loginshell', | |
'mail', | |
'userpassword', | |
'telephonenumber', | |
'house', | |
'matriculationyear', | |
'graduationyear', | |
'imicq', | |
'imskype', | |
'imgtalk', | |
'imaim', | |
'major', | |
'uidugcs', | |
'uidimss', | |
'nickname', | |
'uiddonut', | |
'userpicurl', | |
'caltechuid', | |
]) | |
for lenin_user in lenin_users: | |
lenin_user = convert_to_unicode(clean_displayname(convert_single_list(add_givenname(lower_keys(lenin_user[1]))))) | |
ipa_search = api.Command.user_find(uid=unicode(lenin_user['uid'])) | |
if ipa_search[u'count'] > 0: | |
print( '''Error: uid already exists''') | |
else: | |
print('user does not exist yet\n \n') | |
print(lenin_user) | |
print('\n') | |
api.Command.user_add(**lenin_user) | |
print('\n') | |
# print(api.Command.user_add(uid=u'testuser', givenname=u'Test', sn=u'User')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment