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