scripts/adduser
2021-10-06 19:06:15 -05:00

85 lines
2.4 KiB
Python
Executable file

#!/usr/bin/python3
import os
# Get user details
username = input('Enter username: ')
firstname = input('Enter first name: ')
lastname = input('Enter last name: ')
email = input('Enter email: ')
# Get UID
custom_uid = input('Would you like to enter a custom UID? (y/N) ')
if custom_uid == 'y' or custom_uid == 'Y':
uid = input('Enter UID: ')
else:
uid = 1001 + len([file for file in os.listdir('.') if file.endswith('.ldif')])
# Password
password = os.popen('tr -dc A-Za-z0-9 </dev/urandom | head -c 16; echo ""').read()[:-1]
hashedpassword = os.popen('slappasswd -s ' + password).read()[:-1]
print('Password:', password)
# Construct LDIF
ldif = '''dn: uid={username},ou=People,dc=exozy,dc=me
objectClass: top
objectClass: person
objectClass: organizationalPerson
objectClass: inetOrgPerson
objectClass: posixAccount
objectClass: shadowAccount
uid: {username}
cn: {firstname} {lastname}
sn: {lastname}
givenName: {firstname}
userPassword: {hashedpassword}
mail: {email}
loginShell: /bin/fish
uidNumber: {uid}
gidNumber: {uid}
homeDirectory: /home/{username}
dn: cn={username},ou=Group,dc=exozy,dc=me
objectClass: top
objectClass: posixGroup
cn: {username}
gidNumber: {uid}'''.format(username=username,
firstname=firstname,
lastname=lastname,
email=email,
uid=uid,
hashedpassword=hashedpassword)
# Write to file and get confirmation
filename = username + '.ldif'
with open(filename, 'w') as f:
f.write(ldif)
os.system('vim ' + filename)
confirm = input('OK? (y/N) ')
if confirm != 'y' and confirm != 'Y':
os.system('gio trash ' + filename)
print('Cancelled')
exit(0)
# Add user
os.system('ldapadd -D "cn=Manager,dc=exozy,dc=me" -W -f ' + filename)
# Configure user
os.system('sudo mkhomedir_helper ' + username + ' 077')
os.system('sudo -u ' + username + ' mkdir /home/' + username + '/.config')
os.system('sudo -u ' + username + ' flatpak remote-add --if-not-exists flathub https://dl.flathub.org/repo/flathub.flatpakrepo --user')
os.system('sudo -u ' + username + ' xdg-settings set default-web-browser firefox.desktop')
# Set up rootless Podman
# https://wiki.archlinux.org/title/Podman#Set_subuid_and_subgid
start = str((int(uid) - 999) * 100000)
end = str(int(start) + 65535) # Allocate 65536 UIDs
os.system('sudo usermod --add-subuids ' + start + '-' + end + ' --add-subgids ' + start + '-' + end + ' ' + username)