xref: /freebsd/usr.sbin/periodic/etc/daily/460.status-mail-rejects (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
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		    elif [ -f /var/log/maillog.$n.xz ]
56		    then
57			xzcat -f /var/log/maillog.$n.xz
58		    elif [ -f /var/log/maillog.$n.zst ]
59		    then
60			zstdcat -fc /var/log/maillog.$n.zst
61		    fi
62		    n=$(($n - 1))
63		done
64		cat /var/log/maillog
65	    } | sed -Ene "/^$today/q" -e "/^$yesterday/{"'
66		    s/.*ruleset=check_relay,.* relay=([^,]+), reject=([^ ]*).*/\2 check_relay \1/p
67		    t end
68                    s/.*ruleset=check_rcpt,.* arg1=<?([^>,]+).* reject=([^ ]+) .* ([^ ]+)/\2 check_rcpt \1 \3/p
69		    t end
70                    s/.*ruleset=check_([^,]+),.* arg1=<?([^@]+@)?([^>,]+).* reject=([^ ]+) .* ([^ ]+)/\4 check_\1 \3 \5/p
71		    :end
72		}' | eval $shorten | sort -f | uniq -ic | sort -fnr | tee /dev/stderr | wc -l)
73	    [ $rc -gt 0 ] && rc=1
74	fi;;
75
76    *)  rc=0;;
77esac
78
79exit $rc
80