2020-09-15 00:18:38 +00:00
|
|
|
from log import logger
|
2020-02-05 04:31:22 +00:00
|
|
|
import time
|
|
|
|
import ldap
|
|
|
|
import ldap.modlist as modlist
|
2020-02-05 04:32:25 +00:00
|
|
|
import secrets
|
2020-02-05 04:31:22 +00:00
|
|
|
import base64
|
|
|
|
|
2020-02-05 05:42:42 +00:00
|
|
|
from flask import abort
|
2020-02-09 02:51:16 +00:00
|
|
|
|
2020-02-05 05:42:42 +00:00
|
|
|
HTTP_NOTFOUND = 404
|
|
|
|
|
2020-02-05 04:31:22 +00:00
|
|
|
ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)
|
2020-09-15 00:18:38 +00:00
|
|
|
ldap.set_option(ldap.OPT_X_TLS_CACERTFILE, secrets.LDAP_CERTFILE)
|
2020-02-05 04:31:22 +00:00
|
|
|
|
2020-02-09 02:51:16 +00:00
|
|
|
def init_ldap():
|
2020-09-15 00:18:38 +00:00
|
|
|
ldap_conn = ldap.initialize(secrets.LDAP_URL)
|
2020-02-09 02:51:16 +00:00
|
|
|
ldap_conn.set_option(ldap.OPT_REFERRALS, 0)
|
|
|
|
ldap_conn.set_option(ldap.OPT_PROTOCOL_VERSION, 3)
|
|
|
|
ldap_conn.set_option(ldap.OPT_X_TLS,ldap.OPT_X_TLS_DEMAND)
|
|
|
|
ldap_conn.set_option(ldap.OPT_X_TLS_DEMAND, True)
|
|
|
|
ldap_conn.set_option(ldap.OPT_DEBUG_LEVEL, 255)
|
|
|
|
|
|
|
|
return ldap_conn
|
2020-02-05 04:31:22 +00:00
|
|
|
|
2020-09-11 06:04:38 +00:00
|
|
|
def convert(data):
|
|
|
|
if isinstance(data, dict):
|
|
|
|
return {convert(key): convert(value) for key, value in data.items()}
|
|
|
|
elif isinstance(data, (list, tuple)):
|
|
|
|
if len(data) == 1:
|
|
|
|
return convert(data[0])
|
|
|
|
else:
|
|
|
|
return [convert(element) for element in data]
|
|
|
|
elif isinstance(data, (bytes, bytearray)):
|
2020-09-14 00:13:00 +00:00
|
|
|
try:
|
|
|
|
return data.decode()
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
return data.hex()
|
2020-09-11 06:04:38 +00:00
|
|
|
else:
|
|
|
|
return data
|
|
|
|
|
2020-09-11 07:36:00 +00:00
|
|
|
def find_user(query):
|
2020-02-05 04:31:22 +00:00
|
|
|
'''
|
2020-09-11 07:36:00 +00:00
|
|
|
Search for a user by sAMAccountname or email
|
2020-02-05 04:31:22 +00:00
|
|
|
'''
|
2020-02-09 02:51:16 +00:00
|
|
|
ldap_conn = init_ldap()
|
|
|
|
try:
|
2020-09-15 00:18:38 +00:00
|
|
|
logger.info('Looking up user ' + query)
|
2020-02-09 02:51:16 +00:00
|
|
|
ldap_conn.simple_bind_s(secrets.LDAP_USERNAME, secrets.LDAP_PASSWORD)
|
2021-09-18 06:26:38 +00:00
|
|
|
criteria = '(&(objectClass=user)(|(mail={})(sAMAccountName={})(userPrincipalName={}*))(!(objectClass=computer)))'.format(query, query, query)
|
2020-09-15 00:18:38 +00:00
|
|
|
results = ldap_conn.search_s(secrets.BASE_MEMBERS, ldap.SCOPE_SUBTREE, criteria, ['displayName','sAMAccountName','email'])
|
2020-02-05 05:42:42 +00:00
|
|
|
|
2020-11-04 00:39:07 +00:00
|
|
|
logger.info(' Results: ' + str(results))
|
2020-09-14 00:13:00 +00:00
|
|
|
|
2020-02-09 02:51:16 +00:00
|
|
|
if len(results) != 1:
|
|
|
|
abort(HTTP_NOTFOUND)
|
2020-02-05 05:42:42 +00:00
|
|
|
|
2020-02-09 02:51:16 +00:00
|
|
|
return results[0][0]
|
|
|
|
finally:
|
|
|
|
ldap_conn.unbind()
|
2020-02-05 04:31:22 +00:00
|
|
|
|
2020-09-11 06:04:38 +00:00
|
|
|
def find_dn(dn):
|
|
|
|
'''
|
|
|
|
Search for a user by dn
|
|
|
|
'''
|
|
|
|
ldap_conn = init_ldap()
|
|
|
|
try:
|
2020-11-04 00:39:07 +00:00
|
|
|
logger.info('Finding user for dn: ' + dn)
|
2020-09-11 06:04:38 +00:00
|
|
|
ldap_conn.simple_bind_s(secrets.LDAP_USERNAME, secrets.LDAP_PASSWORD)
|
|
|
|
criteria = '(&(objectClass=user)(!(objectClass=computer)))'
|
|
|
|
results = ldap_conn.search_s(dn, ldap.SCOPE_SUBTREE, criteria, ['sAMAccountName'])
|
|
|
|
|
2020-11-04 00:39:07 +00:00
|
|
|
logger.info(' Results: ' + str(results))
|
|
|
|
|
2020-09-11 06:04:38 +00:00
|
|
|
return results[0][1]['sAMAccountName'][0].decode()
|
|
|
|
finally:
|
|
|
|
ldap_conn.unbind()
|
|
|
|
|
2020-02-04 06:18:37 +00:00
|
|
|
def create_user(first, last, username, email, password):
|
2020-02-05 04:31:22 +00:00
|
|
|
'''
|
|
|
|
Create a User; required data is first, last, email, username, password
|
|
|
|
Note: this creates a disabled user, then sets a password, then enables the user
|
|
|
|
'''
|
2020-02-09 02:51:16 +00:00
|
|
|
ldap_conn = init_ldap()
|
|
|
|
try:
|
2020-11-04 00:39:07 +00:00
|
|
|
logger.info('Creating user: ' + username)
|
2020-02-09 02:51:16 +00:00
|
|
|
ldap_conn.simple_bind_s(secrets.LDAP_USERNAME, secrets.LDAP_PASSWORD)
|
2020-09-15 00:18:38 +00:00
|
|
|
dn = 'CN={} {},{}'.format(first, last, secrets.BASE_MEMBERS)
|
2020-02-09 02:51:16 +00:00
|
|
|
full_name = '{} {}'.format(first, last)
|
|
|
|
|
|
|
|
ldif = [
|
|
|
|
('objectClass', [b'top', b'person', b'organizationalPerson', b'user']),
|
|
|
|
('cn', [full_name.encode()]),
|
|
|
|
('userPrincipalName', [username.encode()]),
|
2021-09-18 06:26:38 +00:00
|
|
|
('sAMAccountName', [username.encode()[:20]]),
|
2020-02-09 02:51:16 +00:00
|
|
|
('givenName', [first.encode()]),
|
|
|
|
('sn', [last.encode()]),
|
|
|
|
('DisplayName', [full_name.encode()]),
|
|
|
|
('userAccountControl', [b'514']),
|
|
|
|
('mail', [email.encode()]),
|
2020-02-09 02:51:51 +00:00
|
|
|
('company', [b'Spaceport']),
|
2020-02-09 02:51:16 +00:00
|
|
|
]
|
|
|
|
|
2020-09-14 00:13:00 +00:00
|
|
|
result = ldap_conn.add_s(dn, ldif)
|
|
|
|
|
2020-11-04 04:03:55 +00:00
|
|
|
logger.info(' Result: ' + str(result))
|
2020-02-09 02:51:16 +00:00
|
|
|
|
|
|
|
# set password
|
|
|
|
pass_quotes = '"{}"'.format(password)
|
|
|
|
pass_uni = pass_quotes.encode('utf-16-le')
|
|
|
|
change_des = [(ldap.MOD_REPLACE, 'unicodePwd', [pass_uni])]
|
|
|
|
result = ldap_conn.modify_s(dn, change_des)
|
|
|
|
|
2020-11-04 04:03:55 +00:00
|
|
|
logger.info(' Result: ' + str(result))
|
2020-09-14 00:13:00 +00:00
|
|
|
|
2020-02-09 02:51:16 +00:00
|
|
|
# 512 will set user account to enabled
|
|
|
|
mod_acct = [(ldap.MOD_REPLACE, 'userAccountControl', b'512')]
|
|
|
|
result = ldap_conn.modify_s(dn, mod_acct)
|
2020-09-14 00:13:00 +00:00
|
|
|
|
2020-11-04 04:03:55 +00:00
|
|
|
logger.info(' Result: ' + str(result))
|
2020-02-09 02:51:16 +00:00
|
|
|
finally:
|
|
|
|
ldap_conn.unbind()
|
2020-02-05 04:31:22 +00:00
|
|
|
|
2020-02-05 05:42:42 +00:00
|
|
|
def set_password(username, password):
|
2020-02-09 02:51:16 +00:00
|
|
|
ldap_conn = init_ldap()
|
|
|
|
try:
|
2020-11-04 00:39:07 +00:00
|
|
|
logger.info('Setting password for: ' + username)
|
2020-02-09 02:51:16 +00:00
|
|
|
ldap_conn.simple_bind_s(secrets.LDAP_USERNAME, secrets.LDAP_PASSWORD)
|
|
|
|
criteria = '(&(objectClass=user)(sAMAccountName={})(!(objectClass=computer)))'.format(username)
|
2020-09-15 00:18:38 +00:00
|
|
|
results = ldap_conn.search_s(secrets.BASE_MEMBERS, ldap.SCOPE_SUBTREE, criteria, ['displayName','sAMAccountName','email'] )
|
2020-02-09 02:51:16 +00:00
|
|
|
|
|
|
|
if len(results) != 1:
|
|
|
|
abort(HTTP_NOTFOUND)
|
|
|
|
|
|
|
|
dn = results[0][0]
|
|
|
|
|
2020-11-04 00:39:07 +00:00
|
|
|
logger.info(' Dn found: ' + dn)
|
|
|
|
|
2020-02-09 02:51:16 +00:00
|
|
|
# set password
|
|
|
|
pass_quotes = '"{}"'.format(password)
|
|
|
|
pass_uni = pass_quotes.encode('utf-16-le')
|
|
|
|
change_des = [(ldap.MOD_REPLACE, 'unicodePwd', [pass_uni])]
|
|
|
|
result = ldap_conn.modify_s(dn, change_des)
|
2020-11-04 00:39:07 +00:00
|
|
|
|
|
|
|
logger.info(' Set password result: ' + str(result))
|
2020-02-09 02:51:16 +00:00
|
|
|
finally:
|
|
|
|
ldap_conn.unbind()
|
2020-02-05 05:42:42 +00:00
|
|
|
|
2020-03-17 16:53:08 +00:00
|
|
|
def find_group(groupname):
|
|
|
|
'''
|
2020-09-08 06:46:34 +00:00
|
|
|
Search for a group by name or sAMAccountname
|
2020-03-17 16:53:08 +00:00
|
|
|
'''
|
|
|
|
ldap_conn = init_ldap()
|
|
|
|
try:
|
2020-09-15 00:18:38 +00:00
|
|
|
logger.info('Looking up group ' + groupname)
|
2020-03-17 16:53:08 +00:00
|
|
|
ldap_conn.simple_bind_s(secrets.LDAP_USERNAME, secrets.LDAP_PASSWORD)
|
|
|
|
criteria = '(&(objectClass=group)(sAMAccountName={}))'.format(groupname)
|
2020-09-15 00:18:38 +00:00
|
|
|
results = ldap_conn.search_s(secrets.BASE_GROUPS, ldap.SCOPE_SUBTREE, criteria, ['name','groupType'] )
|
2020-03-17 16:53:08 +00:00
|
|
|
|
2020-11-04 00:39:07 +00:00
|
|
|
logger.info(' Results: ' + str(results))
|
2020-09-14 00:13:00 +00:00
|
|
|
|
2020-03-17 16:53:08 +00:00
|
|
|
if len(results) != 1:
|
|
|
|
abort(HTTP_NOTFOUND)
|
|
|
|
|
|
|
|
return results[0][0]
|
|
|
|
finally:
|
|
|
|
ldap_conn.unbind()
|
|
|
|
|
2020-09-11 02:29:29 +00:00
|
|
|
def create_group(groupname, description):
|
2020-03-17 16:53:08 +00:00
|
|
|
'''
|
|
|
|
Create a Group; required data is sAMAccountName, Description
|
|
|
|
'''
|
|
|
|
ldap_conn = init_ldap()
|
|
|
|
try:
|
|
|
|
ldap_conn.simple_bind_s(secrets.LDAP_USERNAME, secrets.LDAP_PASSWORD)
|
2020-09-15 00:18:38 +00:00
|
|
|
dn = 'CN={},{}'.format(groupname, secrets.BASE_GROUPS)
|
2020-03-17 16:53:08 +00:00
|
|
|
|
|
|
|
ldif = [
|
|
|
|
('objectClass', [b'top', b'group']),
|
|
|
|
('cn', [groupname.encode()]),
|
|
|
|
('DisplayName', [groupname.encode()]),
|
|
|
|
('description', [description.encode()]),
|
|
|
|
('sAMAccountName', [groupname.encode()])
|
|
|
|
]
|
|
|
|
|
|
|
|
rcode = ldap_conn.add_s(dn, ldif)
|
2020-09-08 06:46:34 +00:00
|
|
|
return rcode
|
|
|
|
|
|
|
|
finally:
|
|
|
|
ldap_conn.unbind()
|
|
|
|
|
2020-09-11 02:29:29 +00:00
|
|
|
def add_to_group(groupname, username):
|
2020-09-08 06:46:34 +00:00
|
|
|
'''
|
|
|
|
Add a user to a Group; required data is GroupName, Username
|
|
|
|
'''
|
|
|
|
ldap_conn = init_ldap()
|
|
|
|
try:
|
2020-11-04 00:39:07 +00:00
|
|
|
logger.info('Adding ' + username + ' to group: ' + groupname)
|
2020-09-08 06:46:34 +00:00
|
|
|
ldap_conn.simple_bind_s(secrets.LDAP_USERNAME, secrets.LDAP_PASSWORD)
|
|
|
|
user_dn = find_user(username)
|
2020-09-14 00:13:00 +00:00
|
|
|
group_dn = find_group(groupname)
|
2020-09-11 01:15:42 +00:00
|
|
|
|
2020-09-11 02:29:29 +00:00
|
|
|
if not is_member(groupname, username):
|
2020-09-08 06:46:34 +00:00
|
|
|
mod_acct = [(ldap.MOD_ADD, 'member', user_dn.encode())]
|
|
|
|
ldap_conn.modify_s(group_dn, mod_acct)
|
2020-11-04 00:39:07 +00:00
|
|
|
logger.info(' Added.')
|
2020-09-11 02:29:29 +00:00
|
|
|
return True
|
|
|
|
else:
|
2020-11-04 00:39:07 +00:00
|
|
|
logger.info(' Already a member, skipping.')
|
2020-09-11 02:29:29 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
finally:
|
|
|
|
ldap_conn.unbind()
|
|
|
|
|
|
|
|
def remove_from_group(groupname, username):
|
|
|
|
'''
|
|
|
|
Remove a user from a Group; required data is GroupName, Username
|
|
|
|
'''
|
|
|
|
ldap_conn = init_ldap()
|
|
|
|
try:
|
|
|
|
ldap_conn.simple_bind_s(secrets.LDAP_USERNAME, secrets.LDAP_PASSWORD)
|
|
|
|
user_dn = find_user(username)
|
2020-09-14 00:13:00 +00:00
|
|
|
group_dn = find_group(groupname)
|
2020-09-11 02:29:29 +00:00
|
|
|
|
|
|
|
if is_member(groupname, username):
|
|
|
|
mod_acct = [(ldap.MOD_DELETE, 'member', user_dn.encode())]
|
|
|
|
ldap_conn.modify_s(group_dn, mod_acct)
|
|
|
|
return True
|
|
|
|
else:
|
2020-09-15 21:01:07 +00:00
|
|
|
logger.info('Not a member, skipping')
|
2020-09-11 02:29:29 +00:00
|
|
|
return False
|
2020-03-17 16:53:08 +00:00
|
|
|
|
|
|
|
finally:
|
|
|
|
ldap_conn.unbind()
|
|
|
|
|
|
|
|
def list_group(groupname):
|
|
|
|
'''
|
|
|
|
List users in a Group; required data is GroupName
|
|
|
|
'''
|
|
|
|
ldap_conn = init_ldap()
|
|
|
|
try:
|
|
|
|
ldap_conn.simple_bind_s(secrets.LDAP_USERNAME, secrets.LDAP_PASSWORD)
|
|
|
|
group_dn = find_group(groupname)
|
2021-09-18 06:26:38 +00:00
|
|
|
|
2020-03-17 16:53:08 +00:00
|
|
|
criteria = '(&(objectClass=group)(sAMAccountName={}))'.format(groupname)
|
2020-09-15 00:18:38 +00:00
|
|
|
results = ldap_conn.search_s(secrets.BASE_GROUPS, ldap.SCOPE_SUBTREE, criteria, ['member'])
|
2020-09-11 02:29:29 +00:00
|
|
|
members_tmp = results[0][1]
|
2020-09-11 06:04:38 +00:00
|
|
|
members = members_tmp.get('member', [])
|
|
|
|
return [find_dn(dn.decode()) for dn in members]
|
|
|
|
|
2020-03-17 16:53:08 +00:00
|
|
|
finally:
|
|
|
|
ldap_conn.unbind()
|
|
|
|
|
2021-09-18 06:26:38 +00:00
|
|
|
def delete_user(username):
|
|
|
|
'''
|
|
|
|
Delete user; required data is sAMAccountName or userPrincipleName
|
|
|
|
'''
|
|
|
|
ldap_conn = init_ldap()
|
|
|
|
try:
|
|
|
|
logger.info('Deleting user: ' + username)
|
|
|
|
|
|
|
|
ldap_conn.simple_bind_s(secrets.LDAP_USERNAME, secrets.LDAP_PASSWORD)
|
|
|
|
user_dn = find_user(username)
|
|
|
|
result = ldap_conn.delete_s(user_dn)
|
|
|
|
|
|
|
|
logger.info(' Result: ' + str(result))
|
|
|
|
return result
|
|
|
|
|
|
|
|
finally:
|
|
|
|
ldap_conn.unbind()
|
|
|
|
|
2020-09-11 02:29:29 +00:00
|
|
|
def is_member(groupname, username):
|
2020-09-11 01:15:42 +00:00
|
|
|
'''
|
2020-09-08 06:46:34 +00:00
|
|
|
Checks to see if a user is a member of a group
|
2020-03-17 16:53:08 +00:00
|
|
|
'''
|
|
|
|
ldap_conn = init_ldap()
|
|
|
|
try:
|
2020-11-04 00:39:07 +00:00
|
|
|
logger.info('Checking if ' + username + ' is in group: ' + groupname)
|
2020-03-17 16:53:08 +00:00
|
|
|
ldap_conn.simple_bind_s(secrets.LDAP_USERNAME, secrets.LDAP_PASSWORD)
|
2020-09-08 06:46:34 +00:00
|
|
|
group_dn = find_group(groupname)
|
2020-09-11 02:29:29 +00:00
|
|
|
user_dn = find_user(username).encode()
|
2020-09-08 06:46:34 +00:00
|
|
|
memflag = False
|
|
|
|
criteria = '(&(objectClass=group)(sAMAccountName={}))'.format(groupname)
|
2020-09-15 00:18:38 +00:00
|
|
|
results = ldap_conn.search_s(secrets.BASE_GROUPS, ldap.SCOPE_SUBTREE, criteria, ['member'] )
|
2020-09-11 02:29:29 +00:00
|
|
|
members_tmp = results[0][1]
|
|
|
|
members = members_tmp.get('member', [])
|
2020-11-04 00:39:07 +00:00
|
|
|
result = user_dn in members
|
|
|
|
|
|
|
|
logger.info(' Result: ' + str(result))
|
|
|
|
return result
|
2020-03-17 16:53:08 +00:00
|
|
|
finally:
|
|
|
|
ldap_conn.unbind()
|
|
|
|
|
2020-09-11 06:04:38 +00:00
|
|
|
def dump_users():
|
|
|
|
'''
|
|
|
|
Dump all AD users
|
|
|
|
'''
|
|
|
|
ldap_conn = init_ldap()
|
|
|
|
try:
|
|
|
|
ldap_conn.simple_bind_s(secrets.LDAP_USERNAME, secrets.LDAP_PASSWORD)
|
2020-09-14 00:13:00 +00:00
|
|
|
criteria = '(&(objectClass=user)(objectGUID=*))'
|
|
|
|
attributes = ['cn', 'sAMAccountName', 'mail', 'displayName', 'givenName', 'name', 'sn', 'logonCount', 'objectGUID']
|
2020-09-15 00:18:38 +00:00
|
|
|
results = ldap_conn.search_s(secrets.BASE_MEMBERS, ldap.SCOPE_SUBTREE, criteria, attributes)
|
2020-09-11 06:04:38 +00:00
|
|
|
results = convert(results)
|
|
|
|
|
|
|
|
output = {}
|
|
|
|
for r in results:
|
|
|
|
tmp = r[1]
|
|
|
|
tmp['dn'] = r[0]
|
|
|
|
output[r[1]['sAMAccountName']] = tmp
|
|
|
|
|
|
|
|
import json
|
|
|
|
return json.dumps(output, indent=4)
|
|
|
|
finally:
|
|
|
|
ldap_conn.unbind()
|
|
|
|
|
2020-03-17 16:53:08 +00:00
|
|
|
|
2020-09-08 06:46:34 +00:00
|
|
|
# ===========================================================================
|
2020-09-11 07:36:00 +00:00
|
|
|
|
2020-09-11 06:04:38 +00:00
|
|
|
#guid = '\\b4\\51\\1adce6709c449bd21a812c423e82'
|
|
|
|
#guid = ''.join(['\\%s' % guid[i:i+2] for i in range(0, len(guid), 2)])
|
|
|
|
#print(guid)
|
|
|
|
#criteria = '(&(objectClass=user)(objectGUID={}))'.format(guid)
|
|
|
|
|
2020-02-05 04:31:22 +00:00
|
|
|
if __name__ == '__main__':
|
2020-09-11 02:29:29 +00:00
|
|
|
pass
|
2021-09-18 06:26:38 +00:00
|
|
|
print("=-=-=-=-=-=-=-=-=-=")
|
|
|
|
#print(create_user('Elon', 'Tusk', 'elon.tusk', 'elon.tusk@lab39.lab', 'protospace*&^g87g6'))
|
|
|
|
#print(find_user('noorullah.hussain.zada'))
|
|
|
|
#print("----------")
|
|
|
|
#print(find_user('pat.spencer'))
|
|
|
|
print("----------")
|
|
|
|
print(find_user('elon.tusk'))
|
|
|
|
print("----------")
|
|
|
|
print(delete_user('elon.tusk'))
|
|
|
|
print("----------")
|
|
|
|
print(find_user('elon.tusk'))
|
2020-09-11 02:29:29 +00:00
|
|
|
#print("============================================================")
|
|
|
|
#print(create_group("newgroup", "new group"))
|
|
|
|
#print(" ============== ")
|
2020-09-11 06:04:38 +00:00
|
|
|
#print(list_group("Laser Users"))
|
2020-09-11 02:29:29 +00:00
|
|
|
#print(" ============== ")
|
|
|
|
#print(is_member('newgroup','tanner.collin'))
|
|
|
|
#print(" ============== ")
|
|
|
|
#print(add_to_group('newgroup','tanner.collin'))
|
|
|
|
#print(" ============== ")
|
|
|
|
#print(list_group("newgroup"))
|
|
|
|
#print(" ============== ")
|
|
|
|
#print(remove_from_group('newgroup','tanner.collin'))
|
|
|
|
#print(" ============== ")
|
2021-09-18 06:26:38 +00:00
|
|
|
#print(list_group('Trotec Users'))
|
2020-09-11 06:04:38 +00:00
|
|
|
#print(dump_users())
|
2020-09-11 07:36:00 +00:00
|
|
|
|
2020-09-15 21:01:07 +00:00
|
|
|
#users = list_group('Laser Users')
|
|
|
|
#import json
|
|
|
|
#print(json.dumps(users, indent=4))
|