summaryrefslogtreecommitdiff
path: root/localrmstaleaccounts
blob: f3c9b12104e1b794b845d82c8e97875f8d14b7d9 (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. warn "$2"
  47. [ -n "$DRY_RUN" ] || localrmaccount "$1" "$2"
  48. }
  49. for user in "$@"; do
  50. home=$(getent passwd "$user" | cut -d: -f6)
  51. if [ -z "$home" ]; then
  52. warn "Skipping user $user: failed resolving homedir"
  53. elif [ -e "$home/.forward" ]; then
  54. warn "Skipping user $user: Email gets forwarded"
  55. elif [ ! -d "$home/Maildir" ]; then
  56. warn "Skipping user $user: Missing Maildir"
  57. elif postalias -s /etc/aliases | grep -qP ':\s+'"$user"'$'; then
  58. warn "Skipping user $user: target in /etc/aliases"
  59. elif find -- /var/lib/radicale/collections/collection-root -maxdepth 1 -type d -name "$user" -not -empty | grep -q '^'; then
  60. warn "Skipping user $user: Uses Radicale"
  61. elif find "$home/Maildir/new" -maxdepth 0 -type d -mtime +365 | grep -q .; then
  62. remove_account "$user" "Removing user $user: Maildir INBOX/new untouched for a year"
  63. elif find "$home/Maildir/cur" -maxdepth 0 -type d -mtime +365 | grep -q .; then
  64. remove_account "$user" "Removing user $user: Maildir INBOX/cur untouched for a year"
  65. elif find "$home/Maildir/new" -type f -mtime +365 | grep -q .; then
  66. remove_account "$user" "Removing user $user: new mail in INBOX untouched for a year"
  67. else
  68. info "Skipping user $user"
  69. fi
  70. done