script-user-reset-password 1022 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/usr/bin/env bash
  2. # This script will reset a user password on the server
  3. # Copyright 2021-24 Bryan C. Roessler
  4. unset user password
  5. parent="${BASH_SOURCE[0]}"
  6. parent=${parent%/*}
  7. [[ -f $parent/script-functions ]] && . "$parent"/script-functions || exit 1
  8. is_root
  9. echo "This script supports two optional arguments, a username and password"
  10. if [[ $# -eq 0 ]]; then
  11. prompt user
  12. prompt password
  13. elif [[ $# -eq 1 ]]; then
  14. user="$1"
  15. prompt password
  16. elif [[ $# -eq 2 ]]; then
  17. user="$1"
  18. password="$2"
  19. elif [[ $# -gt 2 ]]; then
  20. echo "Too many arguments provided"
  21. exit 1
  22. fi
  23. if ! id -u "$user" &>/dev/null; then
  24. echo "User $user does not exist"
  25. exit 1
  26. fi
  27. if ask_ok "Change user $user's password to $password?"; then
  28. echo "$user":"$password" | chpasswd
  29. (echo "$password"; echo "$password") | smbpasswd -a -s "$user"
  30. fi
  31. ask_ok "Prompt user to reset password on next login?" &&
  32. passwd --expire "$user" &&
  33. echo "NOTE: The file sharing (smbpasswd) will not be changed"
  34. exit 0