2020-10-10 12:54:28 +02:00
|
|
|
import re
|
2020-10-05 14:51:16 +02:00
|
|
|
from phi.logging import get_logger
|
|
|
|
|
|
|
|
log = get_logger(__name__)
|
|
|
|
|
|
|
|
|
2020-10-10 12:54:28 +02:00
|
|
|
def make_user_dict(client, entry):
|
|
|
|
attributes = entry['attributes']
|
2020-10-05 14:51:16 +02:00
|
|
|
|
2020-10-10 12:54:28 +02:00
|
|
|
user = {}
|
|
|
|
user['uid'] = attributes['uid'][0]
|
|
|
|
user['dn'] = 'uid={},ou=Hackers,{}'.format(user['uid'], client.base_dn)
|
|
|
|
user['cn'] = attributes['cn'][0]
|
|
|
|
user['sn'] = attributes['sn'][0]
|
|
|
|
user['mail'] = attributes['mail'][0]
|
|
|
|
user['password'] = attributes['userPassword'][0]
|
2020-10-05 14:51:16 +02:00
|
|
|
|
2020-10-10 12:54:28 +02:00
|
|
|
return user
|
2020-10-05 14:51:16 +02:00
|
|
|
|
|
|
|
|
2020-10-10 12:54:28 +02:00
|
|
|
def get_uid_from_dn(client, dn):
|
|
|
|
uid = re.search('uid=(.+?),ou=Hackers,{}'.format(client.base_dn),
|
|
|
|
dn).group(1)
|
|
|
|
return uid
|
2020-10-05 14:51:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
def make_group_dict(client, entry):
|
2020-10-10 12:54:28 +02:00
|
|
|
attributes = entry['attributes']
|
2020-10-05 14:51:16 +02:00
|
|
|
|
2020-10-10 12:54:28 +02:00
|
|
|
cn = attributes['cn'][0]
|
|
|
|
dn = 'cn={},ou=Groups,{}'.format(cn, client.base_dn)
|
|
|
|
members = [get_uid_from_dn(client, u_dn)
|
|
|
|
for u_dn in attributes['member']]
|
|
|
|
|
|
|
|
group = {}
|
|
|
|
group['dn'] = dn
|
|
|
|
group['cn'] = cn
|
|
|
|
group['members'] = members
|
2020-10-05 14:51:16 +02:00
|
|
|
|
|
|
|
return group
|
|
|
|
|
|
|
|
|
|
|
|
def get_response(client, response_id):
|
|
|
|
response, result, request = client.connection.get_response(
|
|
|
|
response_id, get_request=True
|
|
|
|
)
|
|
|
|
|
|
|
|
log.debug("Request: {}".format(request))
|
|
|
|
log.debug("Response: {}".format(response))
|
|
|
|
log.debug("Result: {}".format(result))
|
|
|
|
|
|
|
|
if result['description'] is not 'success':
|
|
|
|
raise Exception(result['description'])
|
|
|
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
def add_entry(client, dn, attributes):
|
|
|
|
log.info('Adding entry with distinguiscet name: {}'
|
|
|
|
'and attributes {}'.format(dn, attributes))
|
|
|
|
response_id = client.connection.add(dn, attributes=attributes)
|
|
|
|
response = get_response(client, response_id)
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
def delete_entry(client, dn):
|
|
|
|
log.info('Deleting entry with distinguiscet name: {}')
|
|
|
|
response_id = client.connection.delete(dn)
|
|
|
|
response = get_response(client, response_id)
|
|
|
|
return response
|