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