scripts/adduser

85 lines
2.4 KiB
Text
Raw Normal View History

2021-08-03 03:22:53 +00:00
#!/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: ')
2021-10-07 00:05:05 +00:00
# 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')])
2021-08-03 03:22:53 +00:00
2021-08-20 22:01:29 +00:00
# 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]
2021-08-20 22:01:29 +00:00
print('Password:', password)
2021-08-03 03:22:53 +00:00
2021-08-20 22:03:44 +00:00
2021-08-03 03:22:53 +00:00
# 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}
2021-08-20 22:01:29 +00:00
userPassword: {hashedpassword}
2021-08-03 03:22:53 +00:00
mail: {email}
2021-08-20 21:29:46 +00:00
loginShell: /bin/fish
2021-08-03 03:22:53 +00:00
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,
2021-08-20 22:01:29 +00:00
uid=uid,
hashedpassword=hashedpassword)
2021-08-03 03:22:53 +00:00
# Write to file and get confirmation
filename = username + '.ldif'
with open(filename, 'w') as f:
f.write(ldif)
os.system('vim ' + filename)
2021-10-07 00:06:15 +00:00
confirm = input('OK? (y/N) ')
2021-08-03 03:22:53 +00:00
2021-10-07 00:06:15 +00:00
if confirm != 'y' and confirm != 'Y':
2021-08-03 14:08:35 +00:00
os.system('gio trash ' + filename)
2021-08-20 22:02:53 +00:00
print('Cancelled')
2021-08-03 03:22:53 +00:00
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')
2021-08-03 14:08:35 +00:00
os.system('sudo -u ' + username + ' mkdir /home/' + username + '/.config')
2021-08-03 03:22:53 +00:00
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
2021-08-03 14:08:35 +00:00
start = str((int(uid) - 999) * 100000)
2021-08-03 13:50:39 +00:00
end = str(int(start) + 65535) # Allocate 65536 UIDs
2021-08-03 14:08:35 +00:00
os.system('sudo usermod --add-subuids ' + start + '-' + end + ' --add-subgids ' + start + '-' + end + ' ' + username)
2021-08-03 03:22:53 +00:00