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