summaryrefslogtreecommitdiff
path: root/localresetpasswd
blob: a7733f1d85888dfb9d96af117dc67bac1b0d4a29 (plain)
  1. #!/bin/sh
  2. #
  3. # /usr/local/bin/localresetpasswd
  4. # Copyright 2006 Jonas Smedegaard <dr@jones.dk>
  5. #
  6. # $Id: localresetpasswd,v 1.1 2006-08-31 22:51:03 jonas Exp $
  7. #
  8. # Generate random password and apply to account through sudo
  9. #
  10. # TODO: Check for bad arguments
  11. # TODO: Use getopts to offer help
  12. # TODO: Support overriding options in /etc/local file
  13. #
  14. set -eu
  15. # these are overridable as environment variables
  16. WORD_LENGTH=${WORD_LENGTH:-11}
  17. VERBOSE=${VERBOSE:-yes}
  18. SIMULATE=${SIMULATE:-}
  19. simulate=${SIMULATE:+true}
  20. # echo something, but only if in verbose mode
  21. vecho() {
  22. test -n "$VERBOSE" && echo "$@" >&2
  23. }
  24. randompass() {
  25. gpw 1 "$WORD_LENGTH" 2>&- ||
  26. pwgen "$WORD_LENGTH" 1 2>&- ||
  27. tr -d '\000-\057\072-\100\133-\140\173-\377' < /dev/urandom |
  28. dd bs="$WORD_LENGTH" count=1 status=none
  29. }
  30. u=$1
  31. shift
  32. vecho -n "Generating random password..."
  33. pass=$(randompass)
  34. vecho " Done!"
  35. sudo=sudo
  36. [ "$(id -u)" -ne 0 ] || sudo=
  37. vecho -n "Applying new password to account $u..."
  38. echo "$u:$pass" | $simulate $sudo "/usr/sbin/chpasswd"
  39. vecho " Done!"
  40. $simulate localresetpasswdexpiry "$u"
  41. vecho "New password is $pass"