script-user-add 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/usr/bin/env bash
  2. # Add a user to the Hartman Lab server
  3. # Copyright Bryan C. Roessler
  4. parent="${BASH_SOURCE[0]}"
  5. parent=${parent%/*}
  6. [[ -f $parent/script-functions ]] && . "$parent"/script-functions || exit 1
  7. is_root
  8. echo "This script supports two optional arguments, a username and password"
  9. if [[ $# -eq 0 ]]; then
  10. prompt user
  11. prompt password
  12. elif [[ $# -eq 1 ]]; then
  13. user="$1"
  14. prompt password
  15. elif [[ $# -eq 2 ]]; then
  16. user="$1"
  17. password="$2"
  18. elif [[ $# -gt 2 ]]; then
  19. echo "Too many arguments provided"
  20. exit 1
  21. fi
  22. useradd_cmd=(useradd -m -U)
  23. if id -u "$user" &>/dev/null; then
  24. ask_ok "User $user exists. Run script-user-remove first?" || exit $?
  25. "$parent"/script-user-remove "$user" || exit $?
  26. fi
  27. ask_ok "Create user $user with password $password?" || exit $?
  28. restore=0
  29. if [[ -d /mnt/array/home-retired/$user ]]; then
  30. ask_ok "Restore user $user's files from /mnt/array/home-retired/$user?" && restore=1
  31. fi
  32. samba=0
  33. ask_ok "Enable shared file access for user $user?" && group_str="smbgrp" && samba=1
  34. ask_ok "Make $user an admin?" && \
  35. group_str+=",wheel"
  36. useradd_cmd+=("-G" "$group_str")
  37. useradd_cmd+=("$user")
  38. if (( restore )); then
  39. if rsync -av --progress=info2 /mnt/array/home-retired/"$user" /home/"$user"; then
  40. ask_ok "User $user's files successfully restored, remove backup at /mnt/array/home-retired/$user?" && \
  41. rm -rf /mnt/array/home-retired/"$user"
  42. fi
  43. fi
  44. # echo "Running: ${useradd_cmd[*]}"
  45. "${useradd_cmd[@]}"
  46. echo "$user":"$password" | chpasswd
  47. if (( samba )); then
  48. (echo "$password"; echo "$password") | smbpasswd -a -s "$user"
  49. fi
  50. ask_ok "Prompt user to reset password on next login?" &&
  51. passwd --expire "$user" &&
  52. echo "NOTE: The file sharing (smbpasswd) will not be changed"
  53. # TODO check if centos 9 does by default
  54. # Add subuids & subgids for container namespace
  55. # id_offset=100000
  56. # id_num=65536
  57. # last_uid=$(tail -1 /etc/subuid | cut -d':' -f2)
  58. # last_gid=$(tail -1 /etc/subgid | cut -d':' -f2)
  59. # start_uid=$(( last_uid + id_offset ))
  60. # start_gid=$(( last_gid + id_offset ))
  61. # echo "$user:$start_uid:$id_num" >> /etc/subuid
  62. # echo "$user:$start_gid:$id_num" >> /etc/subgid
  63. # Copy manual to user desktop
  64. desktop="/home/$user/Desktop"
  65. [[ -d $desktop ]] || sudo -u "$user" mkdir -p "$desktop"
  66. copy_manual "$desktop"
  67. exit 0