#!/bin/sh # # /usr/local/sbin/localrmstaleaccounts # Copyright 2012, 2014, 2019 Jonas Smedegaard # # Remove unused user accounts # set -e PRG=$(basename "$0") exit1() { echo >&2 "ERROR: $1" exit 1 } TEMP=$(getopt -s sh -o vqnh --long verbose,quiet,dry-run,help -n "$PRG" -- "$@") || exit1 "Internal getopt error" eval set -- "$TEMP" usage() { cat <&2 Usage: $PRG [opts...] USER [USER...] -v, --verbose increase verbosity -q, --quiet suppress non-error messages -n, --dry-run perform a trial run with no changes made -h, --help show this help EOF exit 0 } VERBOSE= QUIET= DRY_RUN= while true ; do case "$1" in -v|--verbose) VERBOSE=1; shift;; -q|--quiet) QUIET=1; shift;; -n|--dry-run) DRY_RUN=1; shift;; -h|--help) usage;; --) shift; break;; *) exit1 "Internal getopt parsing error";; esac done warn() { [ -n "$QUIET" ] || echo >&2 "WARNING: $1" } info() { [ -n "$QUIET" ] || [ -z "$VERBOSE" ] || echo >&2 "INFO: $1" } remove_account() { [ -n "$DRY_RUN" ] || localrmaccount "$1" } for user in "$@"; do home=$(getent passwd "$user" | cut -d: -f6) if [ -z "$home" ]; then warn "Skipping user $user: failed resolving homedir" elif [ -e "$home/.forward" ]; then warn "Skipping user $user: Email gets forwarded" elif [ ! -d "$home/Maildir" ]; then warn "Skipping user $user: Missing Maildir" elif find "$home/Maildir/new" -maxdepth 0 -type d -mtime +365 | grep -q .; then info "Removing user $user: Maildir INBOX/new untouched for a year" remove_account "$user" elif find "$home/Maildir/cur" -maxdepth 0 -type d -mtime +365 | grep -q .; then info "Removing user $user: Maildir INBOX/cur untouched for a year" remove_account "$user" elif find "$home/Maildir/new" -type f -mtime +365 | grep -q .; then info "Removing user $user: new mail in INBOX untouched for a year" remove_account "$user" else info "Skipping user $user" fi done