summaryrefslogtreecommitdiff
path: root/localrmstaleaccounts
blob: 88efd28cc374420c5e84459356654772c51f5830 (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. # TODO: collect all indicators before making a verdict
  9. # TODO: ask before actual removal, listing reasons, unless --force
  10. set -e
  11. PRG=$(basename "$0")
  12. exit1() {
  13. echo >&2 "ERROR: $1"
  14. exit 1
  15. }
  16. TEMP=$(getopt -s sh -o vqnh --long verbose,quiet,dry-run,help -n "$PRG" -- "$@") || exit1 "Internal getopt error"
  17. eval set -- "$TEMP"
  18. usage() {
  19. cat <<EOF >&2
  20. Usage: $PRG [opts...] USER [USER...]
  21. -v, --verbose increase verbosity
  22. -q, --quiet suppress non-error messages
  23. -n, --dry-run perform a trial run with no changes made
  24. -h, --help show this help
  25. EOF
  26. exit 0
  27. }
  28. VERBOSE=
  29. QUIET=
  30. DRY_RUN=
  31. while true ; do
  32. case "$1" in
  33. -v|--verbose) VERBOSE=1; shift;;
  34. -q|--quiet) QUIET=1; shift;;
  35. -n|--dry-run) DRY_RUN=1; shift;;
  36. -h|--help) usage;;
  37. --) shift; break;;
  38. *) exit1 "Internal getopt parsing error";;
  39. esac
  40. done
  41. warn() {
  42. [ -n "$QUIET" ] || echo >&2 "WARNING: $1"
  43. }
  44. info() {
  45. [ -n "$QUIET" ] || [ -z "$VERBOSE" ] || echo >&2 "INFO: $1"
  46. }
  47. remove_account() {
  48. warn "Removing user $1: $2"
  49. [ -n "$DRY_RUN" ] || localrmaccount "$1" "$2"
  50. }
  51. for user in "$@"; do
  52. home=$(getent passwd "$user" | cut -d: -f6)
  53. if [ -z "$home" ]; then
  54. warn "Skipping user $user: failed resolving homedir"
  55. elif [ -e "$home/.forward" ]; then
  56. warn "Skipping user $user: Email gets forwarded"
  57. elif [ ! -d "$home/Maildir" ]; then
  58. warn "Skipping user $user: Missing Maildir"
  59. elif [ -d "/var/www/vhosts/$user" ]; then
  60. warn "Skipping user $user: Web hosting at /var/www/vhosts"
  61. elif grep -q "$home" /etc/apache2/sites-enabled/*.conf; then
  62. warn "Skipping user $user: Web hosting below /etc/apache2/sites-enabled"
  63. elif postalias -s /etc/aliases | grep -qP ':\s+'"$user"'$'; then
  64. warn "Skipping user $user: target in /etc/aliases"
  65. elif find -- /var/lib/radicale/collections/collection-root -maxdepth 1 -type d -name "$user" -not -empty | grep -q '^'; then
  66. warn "Skipping user $user: Uses Radicale"
  67. elif doveadm search -u "$user" MAILBOX INBOX SAVEDBEFORE 52weeks NEW | grep -q . && ! doveadm search -u "$user" MAILBOX INBOX SAVEDSINCE 52weeks \( NOT NEW \) | grep -q .; then
  68. remove_account "$user" "new mail in INBOX untouched for a year, and none touched more recently"
  69. else
  70. info "Skipping user $user"
  71. fi
  72. done