2018-11-03 04:24:11 +00:00
|
|
|
from django.conf import settings
|
2019-02-03 12:05:44 +00:00
|
|
|
from ldap3 import Server, Connection, ObjectDef, Writer, SUBTREE
|
|
|
|
import logging
|
2018-11-03 04:24:11 +00:00
|
|
|
|
2019-02-03 12:05:44 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2018-11-03 04:24:11 +00:00
|
|
|
server = Server(settings.AUTH_LDAP_SERVER_URI)
|
|
|
|
|
|
|
|
|
|
|
|
def create_user(user, password, firstname, lastname, email):
|
2019-02-03 12:31:36 +00:00
|
|
|
logger.debug("In create_user")
|
2018-11-03 04:24:11 +00:00
|
|
|
conn = Connection(server, settings.AUTH_LDAP_BIND_DN,
|
|
|
|
settings.AUTH_LDAP_BIND_PASSWORD)
|
|
|
|
if not conn.bind():
|
2019-02-03 12:05:44 +00:00
|
|
|
logger.error("conn.bind() returned False. Could not connect.")
|
2018-11-03 04:24:11 +00:00
|
|
|
raise Exception('Could not connect to LDAP Server')
|
2019-02-03 12:31:36 +00:00
|
|
|
|
2019-02-03 12:05:44 +00:00
|
|
|
obj_new_user = ObjectDef(['inetOrgPerson', 'posixAccount'], conn)
|
2019-02-03 12:34:29 +00:00
|
|
|
uidNumber = get_max_uid() + 1
|
2019-02-03 12:31:36 +00:00
|
|
|
logger.debug("uidNumber={uidNumber}".format(uidNumber=uidNumber))
|
2019-02-03 12:05:44 +00:00
|
|
|
results = True
|
|
|
|
while results:
|
|
|
|
results = conn.search(
|
|
|
|
search_base=settings.LDAP_SEARCH_BASE,
|
|
|
|
search_filter=(
|
|
|
|
'(&(objectClass=inetOrgPerson)(objectClass=posixAccount)'
|
|
|
|
'(objectClass=top)(uidNumber={uidNumber}))'.format(
|
2019-02-03 12:31:36 +00:00
|
|
|
uidNumber=uidNumber
|
2019-02-03 12:05:44 +00:00
|
|
|
)
|
|
|
|
),
|
|
|
|
search_scope=SUBTREE,
|
|
|
|
attributes=['uidNumber'],
|
|
|
|
)
|
|
|
|
if results:
|
2019-02-03 12:31:36 +00:00
|
|
|
logger.debug("{uid} exists. Trying next.".format(uid=uidNumber))
|
|
|
|
uidNumber += 1
|
2019-02-03 12:05:44 +00:00
|
|
|
|
2019-02-03 12:50:47 +00:00
|
|
|
logger.debug("{uid} does not exist. Using it".format(uid=uidNumber))
|
|
|
|
set_max_uid(uidNumber)
|
2018-11-03 04:24:11 +00:00
|
|
|
w = Writer(conn, obj_new_user)
|
2018-11-16 14:53:16 +00:00
|
|
|
dn = 'uid=%s,ou=users,dc=ungleich,dc=ch' % user
|
2018-11-03 04:24:11 +00:00
|
|
|
w.new(dn)
|
|
|
|
w[0].givenName = firstname
|
|
|
|
w[0].sn = lastname
|
|
|
|
w[0].cn = firstname + " " + lastname
|
|
|
|
w[0].mail = email
|
|
|
|
w[0].userPassword = password
|
2019-02-03 12:43:08 +00:00
|
|
|
w[0].gidNumber = settings.LDAP_IPV6_WORK_USER_GROUP
|
2019-02-03 12:31:36 +00:00
|
|
|
w[0].uidNumber = uidNumber
|
2019-02-03 12:05:44 +00:00
|
|
|
w[0].homeDirectory = "/home/" + user
|
2018-11-03 04:24:11 +00:00
|
|
|
|
|
|
|
if not w.commit():
|
|
|
|
conn.unbind()
|
2019-02-03 12:05:44 +00:00
|
|
|
logger.error("w.commit() returned False. Could not write user.")
|
2018-11-03 04:24:11 +00:00
|
|
|
raise Exception("Couldn't write user")
|
2019-02-03 12:31:36 +00:00
|
|
|
logger.debug("Created user {user} successfully.".format(user=user))
|
2018-11-03 04:24:11 +00:00
|
|
|
conn.unbind()
|
2018-11-16 14:53:16 +00:00
|
|
|
return True
|
2019-02-03 12:31:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
def set_max_uid(max_uid):
|
|
|
|
"""
|
|
|
|
a utility function to save max_uid value to a file
|
|
|
|
|
|
|
|
:param max_uid: an integer representing the max uid
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
with open(settings.LDAP_MAX_UID_PATH, 'w+') as handler:
|
Bugfix: TypeError: write() argument must be str, not int
Internal Server Error: /ipv6/work/signup/
Traceback (most recent call last):
File "/home/app/pyvenv/lib/python3.5/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/app/pyvenv/lib/python3.5/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/app/pyvenv/lib/python3.5/site-packages/django/core/handlers/base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "./users/views.py", line 19, in signup
create_user(username, raw_password, first_name, last_name, email)
File "./users/ldap_funcs.py", line 38, in create_user
set_max_uid(uidNumber)
File "./users/ldap_funcs.py", line 68, in set_max_uid
handler.write(max_uid)
TypeError: write() argument must be str, not int
2019-02-03 12:55:34 +00:00
|
|
|
handler.write(str(max_uid))
|
2019-02-03 12:31:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_max_uid():
|
|
|
|
"""
|
|
|
|
A utility function to read the max uid value that was previously set
|
|
|
|
|
|
|
|
:return: An integer representing the max uid value that was previously set
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
with open(settings.LDAP_MAX_UID_PATH, 'r+') as handler:
|
Handle more errors
Internal Server Error: /ipv6/work/signup/
Traceback (most recent call last):
File "/home/app/pyvenv/lib/python3.5/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/app/pyvenv/lib/python3.5/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/app/pyvenv/lib/python3.5/site-packages/django/core/handlers/base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "./users/views.py", line 19, in signup
create_user(username, raw_password, first_name, last_name, email)
File "./users/ldap_funcs.py", line 18, in create_user
uidNumber = get_max_uid() + 1
File "./users/ldap_funcs.py", line 79, in get_max_uid
return int(handler.read())
ValueError: invalid literal for int() with base 10: ''
2019-02-03 13:04:04 +00:00
|
|
|
try:
|
|
|
|
return_value = int(handler.read())
|
|
|
|
except ValueError as ve:
|
|
|
|
logger.error(
|
|
|
|
"Error reading int value from {}. {}"
|
|
|
|
"Returning default value {} instead".format(
|
|
|
|
settings.LDAP_MAX_UID_PATH,
|
|
|
|
str(ve),
|
|
|
|
settings.LDAP_DEFAULT_START_UID
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return_value = settings.LDAP_DEFAULT_START_UID
|
|
|
|
return return_value
|
2019-02-03 12:31:36 +00:00
|
|
|
except FileNotFoundError as fnfe:
|
|
|
|
logger.error("File not found : " + str(fnfe))
|
Handle more errors
Internal Server Error: /ipv6/work/signup/
Traceback (most recent call last):
File "/home/app/pyvenv/lib/python3.5/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/app/pyvenv/lib/python3.5/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/app/pyvenv/lib/python3.5/site-packages/django/core/handlers/base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "./users/views.py", line 19, in signup
create_user(username, raw_password, first_name, last_name, email)
File "./users/ldap_funcs.py", line 18, in create_user
uidNumber = get_max_uid() + 1
File "./users/ldap_funcs.py", line 79, in get_max_uid
return int(handler.read())
ValueError: invalid literal for int() with base 10: ''
2019-02-03 13:04:04 +00:00
|
|
|
retrun_value = settings.LDAP_DEFAULT_START_UID
|
|
|
|
logger.error("So, returning UID={}".format(retrun_value))
|
|
|
|
return retrun_value
|