summaryrefslogtreecommitdiff
path: root/localrmstaleaccounts
blob: c7ceaaeb31f388867deacd00c57446e446e19466 (plain)
  1. #!/bin/sh
  2. #
  3. # /usr/local/sbin/localrmstaleaccounts
  4. # Copyright 2012, 2014, 2019 Jonas Smedegaard <dr@jones.dk>
  5. #
  6. # Remove unused user accounts
  7. #
  8. set -e
  9. PRG=$(basename "$0")
  10. exit1() {
  11. echo >&2 "ERROR: $1"
  12. exit 1
  13. }
  14. TEMP=$(getopt -s sh -o vqnh --long verbose,quiet,dry-run,help -n "$PRG" -- "$@") || exit1 "Internal getopt error"
  15. eval set -- "$TEMP"
  16. usage() {
  17. cat <<EOF >&2
  18. Usage: $PRG [opts...] USER [USER...]
  19. -v, --verbose increase verbosity
  20. -q, --quiet suppress non-error messages
  21. -n, --dry-run perform a trial run with no changes made
  22. -h, --help show this help
  23. EOF
  24. exit 0
  25. }
  26. VERBOSE=
  27. QUIET=
  28. DRY_RUN=
  29. while true ; do
  30. case "$1" in
  31. -v|--verbose) VERBOSE=1; shift;;
  32. -q|--quiet) QUIET=1; shift;;
  33. -n|--dry-run) DRY_RUN=1; shift;;
  34. -h|--help) usage;;
  35. --) shift; break;;
  36. *) exit1 "Internal getopt parsing error";;
  37. esac
  38. done
  39. warn() {
  40. [ -n "$QUIET" ] || echo >&2 "WARNING: $1"
  41. }
  42. info() {
  43. [ -n "$QUIET" ] || [ -z "$VERBOSE" ] || echo >&2 "INFO: $1"
  44. }
  45. remove_account() {
  46. [ -n "$DRY_RUN" ] || localrmaccount "$1"
  47. }
  48. for user in "$@"; do
  49. home=$(getent passwd "$user" | cut -d: -f6)
  50. if [ -z "$home" ]; then
  51. warn "Skipping user $user: failed resolving homedir"
  52. elif [ -e "$home/.forward" ]; then
  53. warn "Skipping user $user: Email gets forwarded"
  54. elif [ ! -d "$home/Maildir" ]; then
  55. warn "Skipping user $user: Missing Maildir"
  56. elif find "$home/Maildir/new" -maxdepth 0 -type d -mtime +365 | grep -q .; then
  57. info "Removing user $user: Maildir INBOX/new untouched for a year"
  58. remove_account "$user"
  59. elif find "$home/Maildir/cur" -maxdepth 0 -type d -mtime +365 | grep -q .; then
  60. info "Removing user $user: Maildir INBOX/cur untouched for a year"
  61. remove_account "$user"
  62. elif find "$home/Maildir/new" -type f -mtime +365 | grep -q .; then
  63. info "Removing user $user: new mail in INBOX untouched for a year"
  64. remove_account "$user"
  65. else
  66. info "Skipping user $user"
  67. fi
  68. done