Fix UID mismatch bug by directly querying ldapsearch

This commit is contained in:
Anthony Wang 2023-01-11 16:21:01 +00:00
parent 660e75bff2
commit c666bbe689
Signed by: a
GPG key ID: 42A5B952E6DD8D38

10
adduser
View file

@ -13,8 +13,12 @@ def adduser(username, firstname, lastname, email, password):
"""Add a new user"""
# Get UID
output = str(check_output(['getent', 'passwd']))
uid = [u for u in range(1000, 10000) if str(u) not in output][0]
output = check_output(['ldapsearch', '-x', 'uidNumber']).decode('utf-8')
used = set()
for line in output.split('\n'):
if line.startswith('uidNumber'):
used.add(int(line.split()[1]))
uid = [u for u in range(1001, 10000) if u not in used][0]
# Construct LDIF
ldif = f'''dn: uid={username},ou=People,dc=exozy,dc=me
@ -49,9 +53,9 @@ gidNumber: {uid}'''
# Add user
ret = call(['ldapadd', '-D', 'cn=Manager,dc=exozy,dc=me', '-w',
open('/etc/ldappass', 'r').read(), '-f', filename])
remove(filename)
if ret != 0:
return
remove(filename)
# Configure and set up user
# Make home directory