aboutsummaryrefslogtreecommitdiff
path: root/mail/sa-utils/files/sa-utils.in
blob: 0e041b87ad952837d0bdcf9482df2f45d12a4589 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/bin/sh
#
# $FreeBSD$
#
# periodic(8) script that runs sa-update on a nightly basis, updating
# SpamAssassin rules if needed. Relies on sa-update command.
#
# Originally contributed by: Jeremy Chadwick <koitsu@FreeBSD.org>
# Extended and enhanced by: Matthew Seaman <m.seaman@infracaninophile.co.uk>
#
# Define these variables in either /etc/periodic.conf or
# /etc/periodic.conf.local to override the defaults.
#
# Configuration Settings (with default values):
#
# daily_sa_enable="YES"
#          run sa-update(1) daily.  We assume you want this since
#          you've installed the port.
#
# daily_sa_quiet="NO"
#           discard output from sa-update, sa-compile, sa-spamd if set
#           to YES.  sa-compile in particular is quite verbose, so
#           setting this to YES can avoid some occasional excess
#           verbiage in your daily e-mails.
#
# daily_sa_update_flags=""
#           flags to pass to sa-update. eg. to download additional
#           updates from saupdates.example.com, signed with a GPG key
#           with fingerprint 0xCAFEBABE which you have previously
#           downloaded and installed into the sa-update keyring:
#               daily_sa_update_flags="--gpgkey CAFEBABE \
#                    --channel saupdates.example.com     \
#                    --channel updates.spamassassin.org"  
#
# daily_sa_compile="%%SACOMPILE%%"
#           run sa-compile to create a compiled form of the filter
#           rules when new rules are found.  Note: this can be time
#           consuming and CPU intensive.
#
# daily_sa_compile_flags=""
#           flags to pass to sa-compile. eg to enable debug output
#               daily_sa_compile_flags="-D"
#
# daily_sa_compile_nice="YES"
#           run sa-compile via nice(1) to minimize its impact on the
#           the system.
#
# daily_sa_compile_nice_flags=""
#           flags to pass to nice(1).  eg to use a priority increment
#           different than the default.
#               daily_sa_compile_nice_flags="-n 16"
#
# daily_sa_restart_spamd="YES"
#           automatically restart sa-spamd when new rules are found.
#           If daily_sa_compile is enabled, only restart if new rules
#           are found and compilation succeeds  

# If there is a global system configuration file, suck it in.
#
if [ -r /etc/defaults/periodic.conf ]
then
    . /etc/defaults/periodic.conf
    source_periodic_confs
fi

: ${daily_sa_enable="YES"}
: ${daily_sa_quiet="NO"}
: ${daily_sa_compile="%%SACOMPILE%%"}
: ${daily_sa_compile_nice="YES"}
: ${daily_sa_restart_spamd="YES"}

PATH=/bin:/sbin:/usr/bin:/usr/sbin:%%LOCALBASE%%/bin:%%LOCALBASE%%/sbin
export PATH

update_rules() {
    echo
    echo "Checking for new SpamAssassin rules:"

    eval sa-update ${daily_sa_update_flags} ${_output} || rc=$?

    return $rc
}

# If enabled, run sa-compile to compile the updated rulesets.  This
# can require significant time and CPU.
compile_rules() {
    case "$daily_sa_compile_nice" in
        [Yy][Ee][Ss])
            _nice="nice $daily_sa_compile_nice_flags"
            ;;
	*)
            _nice=
            ;;
    esac

    case "$daily_sa_compile" in
	[Yy][Ee][Ss])
	    echo "  Compiling new rules"
	    eval ${_nice} sa-compile ${daily_sa_compile_flags} ${_output} || rc=$?

	    if [ $rc -ne 0 ] ; then
		echo "Error: sa-compile exited with code $rc"
		rc=3
	    fi
	    ;;

	*)  ;;			# Do nothing
    esac

    return $rc
}

# If update_rules() downloads new rules, and if compile_rules succeeds
# or is not enabled, then restart the spamd daemon.
restart_spamd() {
    case "$daily_sa_restart_spamd" in
	[Yy][Ee][Ss])
	    echo "  Restarting sa-spamd"
	    eval %%LOCALBASE%%/etc/rc.d/sa-spamd restart ${_output} || rc=$?

	    if [ $rc -ne 0 ] ; then
		echo "Error: failed to restart sa-spamd"
		rc=3
	    fi
	    ;;

	*)  ;;			# Do nothing
    esac

    return $rc
}

rc=0
case "$daily_sa_enable" in
    [Yy][Ee][Ss])
	case ${daily_sa_quiet} in
	    [Yy][Ee][Ss])
		_output='>/dev/null 2>&1'
		;;
	    *)
		_output=
		;;
	esac

	# In FreeBSD 12.0 the anticongestion function should be used
	# instead of a hard-coded sleep
	if [ -n "$anticongestion_sleeptime" ]; then
	    anticongestion
	else
	    sleep $( jot -r 1 0 300 )
	fi

	update_rules || rc=$?
	case ${rc} in
	    0)
		echo "Successfully downloaded updated rules"
		compile_rules && \
		    restart_spamd
		;;
	    1)
		echo "No new rules available."
		rc=0
		;;
	    *)
		echo "Error: sa-update exited with code ${rc}"
		rc=3
		;;
	esac
	;;

    *)	;;			# Nothing to do
esac

exit $rc