| 1 | #!/bin/bash |
| 2 | # |
| 3 | # Redeliver any messages that may have accidentally been sent to |
| 4 | # /var/mail/$USER, due to temporary AFS glitches. |
| 5 | # |
| 6 | # This should be called as root on deleuze. |
| 7 | # |
| 8 | # USAGE: redeliver-mail [USER] |
| 9 | # |
| 10 | # If USER is specified, redeliver their mail, regardless of whether |
| 11 | # mail redelivery is stopped for all users. |
| 12 | |
| 13 | BLOCKFILE=/tmp/exim4/inhibit-redelivery |
| 14 | |
| 15 | # Function to deliver mail for $USER. If the message still can't be |
| 16 | # written to $USER/Maildir, Exim will place it in /var/mail/$USER |
| 17 | # again. |
| 18 | function deliver_mail() { |
| 19 | local USER=$1 |
| 20 | local TMPMBOX=$2 |
| 21 | |
| 22 | mv /var/mail/$USER $TMPMBOX |
| 23 | formail -s /etc/exim4/deliver-once $USER < $TMPMBOX |
| 24 | rm -f $TMPMBOX |
| 25 | } |
| 26 | |
| 27 | # Check arguments. |
| 28 | if [ -n "$2" ]; then |
| 29 | echo "Incorrect number of arguments" |
| 30 | exit 1 |
| 31 | elif [ -n "$1" ]; then |
| 32 | # Do redelivery for just one person |
| 33 | USER=$1 |
| 34 | MBOX=/var/mail/$USER |
| 35 | TMPMBOX=/tmp/exim4/$USER.mbox.tmp |
| 36 | |
| 37 | if test -f $TMPMBOX; then |
| 38 | echo "The temporary file $TMPMBOX still exists." |
| 39 | echo "Please wait an hour for it to go away before running this" \ |
| 40 | "script again." |
| 41 | exit 1 |
| 42 | fi |
| 43 | |
| 44 | if ! test -s $MBOX || ! test -f $MBOX; then |
| 45 | echo "$MBOX is empty" |
| 46 | exit 1 |
| 47 | fi |
| 48 | |
| 49 | deliver_mail $USER $TMPMBOX |
| 50 | echo Done delivering mail for $USER |
| 51 | exit 0 |
| 52 | fi |
| 53 | |
| 54 | # Make sure we didn't get into trouble with an earlier run of this |
| 55 | # script. A separate daily cron job should run to check for the |
| 56 | # existence of this file. |
| 57 | if test -f $BLOCKFILE; then |
| 58 | exit 0 |
| 59 | fi |
| 60 | |
| 61 | # Run redelivery for everyone |
| 62 | for MBOX in /var/mail/*; do |
| 63 | |
| 64 | USER=$(basename $MBOX) |
| 65 | TMPMBOX=/tmp/exim4/$USER.mbox.tmp |
| 66 | |
| 67 | # Sanity check: file exists and has nonzero size |
| 68 | if ! test -s $MBOX; then |
| 69 | continue |
| 70 | fi |
| 71 | |
| 72 | # Sanity check: If TMPMBOX exists and is 20 minutes old or less, |
| 73 | # there is some serious trouble, and we should (1) email |
| 74 | # postmaster about it and (2) block future processing of /var/mail |
| 75 | # until someone removes BLOCKFILE. |
| 76 | # |
| 77 | # If TMPMBOX exists and is older than 20 minutes, it's probably |
| 78 | # stale, so blow it away. Note that we will never get to this |
| 79 | # point if BLOCKFILE exists, so there is no need to refine the |
| 80 | # metric further. |
| 81 | if test -f $TMPMBOX; then |
| 82 | if test $(stat --format=%Y $TMPMBOX) -le \ |
| 83 | $(date --date='-20 min' +%s); then |
| 84 | # Set block file |
| 85 | touch $BLOCKFILE |
| 86 | echo -e "/var/mail/$USER contains too much email." \ |
| 87 | "\nBlocking email redelivery script until this is resolved." \ |
| 88 | "\n\nTo indicate that this is resolved, do the following" \ |
| 89 | "\non deleuze." \ |
| 90 | "\n (1) Wait for $TMPMBOX to go away (should take an hour)" \ |
| 91 | "\n (2) Run '$0 $USER'" \ |
| 92 | "\n (3) Remove $BLOCKFILE" | \ |
| 93 | mail -s "[redeliver-mail] Mail backlog too large on deleuze" \ |
| 94 | -e -a "From: root@deleuze.hcoop.net" \ |
| 95 | postmaster@deleuze.hcoop.net |
| 96 | exit 0 |
| 97 | else |
| 98 | rm -fr $TMPMBOX |
| 99 | fi |
| 100 | fi |
| 101 | |
| 102 | # Deliver mail. If the message still can't be written to |
| 103 | # $USER/Maildir, Exim will place it in /var/mail/$USER again. |
| 104 | deliver_mail $USER $TMPMBOX |
| 105 | done |