Add interactive mode for all *user scripts

This commit is contained in:
Anthony Wang 2021-12-11 14:45:12 -06:00
parent 6db81fda52
commit 4cfd8d01ad
Signed by: a
GPG key ID: BC96B00AEC5F2D76
3 changed files with 35 additions and 48 deletions

38
adduser
View file

@ -3,7 +3,6 @@
import sys
import os
# Determine if running in interactive mode
interactive = len(sys.argv) == 1
@ -20,15 +19,8 @@ else:
email = sys.argv[4]
# Get UID
if interactive:
custom_uid = input('Would you like to enter a custom UID? (y/N) ')
else:
custom_uid = 'n'
if custom_uid == 'y' or custom_uid == 'Y':
uid = input('Enter UID: ')
else:
output = os.popen('getent passwd').read()
uid = [u for u in range(1000, 10000) if str(u) not in output][0]
output = os.popen('getent passwd').read()
uid = [u for u in range(1000, 10000) if str(u) not in output][0]
# Password
if interactive:
@ -88,29 +80,19 @@ if interactive:
# Add user
if interactive:
ret = os.system('ldapadd -D "cn=Manager,dc=exozy,dc=me" -W -f ' + filename)
if ret != 0:
os.remove(filename)
exit(0)
else:
ret = os.system('ldapadd -D "cn=Manager,dc=exozy,dc=me" -w "' +
sys.argv[6] + '" -f ' + filename)
if ret != 0:
os.remove(filename)
exit(0)
os.system('chown ta180m:ta180m ' + filename)
os.system('mv ' + filename + ' /home/ta180m/git/LDAP/users')
if ret != 0:
os.remove(filename)
exit(0)
os.system('chown ta180m:ta180m ' + filename)
os.system('mv ' + filename + ' /home/ta180m/git/LDAP/users')
# 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') # Set up Flatpak
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)
' xdg-settings set default-web-browser firefox.desktop') # Set default browser

36
deluser
View file

@ -1,25 +1,25 @@
#!/usr/bin/python3
import sys
import os
# Determine if running in interactive mode
interactive = len(sys.argv) == 1
# Get user details
username = input('Enter username: ')
if interactive:
usernames = [input('Enter username: ')]
# Confirmation
confirm = input('OK? (y/n) ')
if confirm != 'y':
print('Cancelled')
exit(0)
else:
usernames = sys.argv[1:]
# Confirmation
confirm = input('OK? (y/n) ')
if confirm != 'y':
print('Cancelled')
exit(0)
# Delete user
os.system('ldapdelete -W -D "cn=Manager,dc=exozy,dc=me" "uid=' + username +
',ou=People,dc=exozy,dc=me" "cn=' + username + ',ou=Group,dc=exozy,dc=me"')
# Cleanup
os.system('sudo rm -rf /home/' + username)
os.system('sudo sed -i \'/' + username + '/d\' /etc/subuid')
os.system('sudo sed -i \'/' + username + '/d\' /etc/subgid')
for username in usernames:
# Delete user
os.system('ldapdelete -W -D "cn=Manager,dc=exozy,dc=me" "uid=' + username +
',ou=People,dc=exozy,dc=me" "cn=' + username + ',ou=Group,dc=exozy,dc=me"')
# Cleanup
os.system('sudo rm -rf /home/' + username)

View file

@ -1,16 +1,21 @@
#!/usr/bin/python3
import sys
import os
# Determine if running in interactive mode
interactive = len(sys.argv) == 1
# Get user details
username = input('Enter username: ')
if interactive:
username = input('Enter username: ')
else:
username = sys.argv[1]
if username == 'Manager':
bind_user = 'cn=Manager,dc=exozy,dc=me'
else:
bind_user = 'uid=' + username + ',ou=People,dc=exozy,dc=me'
# Modify user
os.system('ldapvi --user ' + bind_user)