xref: /freebsd/usr.sbin/periodic/etc/daily/460.status-mail-rejects (revision 405c3050f102b8c74782f0366c8ead927bd07b68)
1#!/bin/sh
2#
3# $FreeBSD$
4#
5
6# If there is a global system configuration file, suck it in.
7#
8if [ -r /etc/defaults/periodic.conf ]
9then
10    . /etc/defaults/periodic.conf
11    source_periodic_confs
12fi
13
14case "$daily_status_mail_rejects_shorten" in
15[Yy][Ee][Ss])	shorten='cut -d" " -f2,3';;
16*)		shorten=cat;;
17esac
18
19case "$daily_status_mail_rejects_enable" in
20    [Yy][Ee][Ss])
21	if [ ! -d /etc/mail ]
22	then
23	    echo '$daily_status_mail_rejects_enable is set but /etc/mail' \
24		"doesn't exist"
25	    rc=2
26	elif [ ! -f /var/log/maillog ]
27	then
28	    echo '$daily_status_mail_rejects_enable is set but ' \
29		"/var/log/maillog doesn't exist"
30	    rc=2
31	elif [ "$daily_status_mail_rejects_logs" -le 0 ]
32	then
33	    echo '$daily_status_mail_rejects_enable is set but ' \
34		'$daily_status_mail_rejects_logs is not greater than zero'
35	    rc=2
36	else
37	    echo
38	    echo Checking for rejected mail hosts:
39
40	    yesterday=$(date -v-1d '+%b %e')
41	    today=$(date '+%b %e')
42	    n=$(($daily_status_mail_rejects_logs - 2))
43	    rc=$({
44		while [ $n -ge 0 ]
45		do
46		    if [ -f /var/log/maillog.$n ]
47		    then
48			cat /var/log/maillog.$n
49		    elif [ -f /var/log/maillog.$n.gz ]
50		    then
51			zcat -fc /var/log/maillog.$n.gz
52		    elif [ -f /var/log/maillog.$n.bz2 ]
53		    then
54			bzcat -fc /var/log/maillog.$n.bz2
55		    fi
56		    n=$(($n - 1))
57		done
58		cat /var/log/maillog
59	    } | sed -Ene "/^$today/q" -e "/^$yesterday/{"'
60		    s/.*ruleset=check_relay,.* relay=([^,]+), reject=([^ ]*).*/\2 check_relay \1/p
61		    t end
62                    s/.*ruleset=check_rcpt,.* arg1=<?([^>,]+).* reject=([^ ]+) .* ([^ ]+)/\2 check_rcpt \1 \3/p
63		    t end
64                    s/.*ruleset=check_([^,]+),.* arg1=<?([^@]+@)?([^>,]+).* reject=([^ ]+) .* ([^ ]+)/\4 check_\1 \3 \5/p
65		    :end
66		}' | eval $shorten | sort -f | uniq -ic | sort -fnr | tee /dev/stderr | wc -l)
67	    [ $rc -gt 0 ] && rc=1
68	fi;;
69
70    *)  rc=0;;
71esac
72
73exit $rc
74