summaryrefslogtreecommitdiff
path: root/localresetpasswd
blob: d49916434c27e9210a0436011fb345a4a77c7967 (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 passphrase 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. PHRASE_LENGTH=${PHRASE_LENGTH:-3}
  17. WORD_LENGTH=${WORD_LENGTH:-11}
  18. VERBOSE=${VERBOSE:-yes}
  19. SIMULATE=${SIMULATE:-}
  20. simulate=${SIMULATE:+true}
  21. # echo something, but only if in verbose mode
  22. vecho() {
  23. test -n "$VERBOSE" && echo "$@" >&2
  24. }
  25. randompass() {
  26. xkcdpass -n "$PHRASE_LENGTH" 2>&- ||
  27. gpw 1 "$WORD_LENGTH" 2>&- ||
  28. pwgen "$WORD_LENGTH" 1 2>&- ||
  29. tr -d '\000-\057\072-\100\133-\140\173-\377' < /dev/urandom |
  30. dd bs="$WORD_LENGTH" count=1 status=none
  31. }
  32. u=$1
  33. shift
  34. vecho -n "Generating random passphrase..."
  35. pass=$(randompass)
  36. vecho " Done!"
  37. sudo=sudo
  38. [ "$(id -u)" -ne 0 ] || sudo=
  39. vecho -n "Applying new passphrase to account $u..."
  40. echo "$u:$pass" | $simulate $sudo "/usr/sbin/chpasswd"
  41. vecho " Done!"
  42. $simulate localresetpasswdexpiry "$u"
  43. vecho "New passphrase is $pass"