improvements and fixes
[distro-setup] / mailtest-check
1 #!/bin/bash
2
3 # Usage: mail-test-check [slow] [anything]
4 #
5 # slow: do slow checks, like spamassassin
6 #
7 # anything: consider non-interactive, dont print unless something went
8 # wrong
9
10
11 source /b/errhandle/err
12
13 [[ $EUID == 0 ]] || exec sudo -E "${BASH_SOURCE[0]}" "$@"
14
15 shopt -s nullglob
16
17 e() { $int || return 0; printf "mailtest-check: %s\n" "$*"; }
18
19 getspamdpid() {
20 if [[ ! $spamdpid || ! -d /proc/$spamdpid ]]; then
21 # try twice in case we are restarting, it happens.
22 for i in 1 2; do
23 spamdpid=$(systemctl show --property MainPID --value spamassassin | sed 's/^[10]$//' ||:)
24 if [[ $spamdpid ]]; then
25 break
26 fi
27 sleep 30
28 done
29 fi
30 }
31 pr() {
32 if $doprom && [[ -e /var/lib/prometheus/node-exporter ]]; then
33 cat >>/var/lib/prometheus/node-exporter/mailtest-check.prom.$$
34 fi
35 }
36
37
38 #### begin arg processing ####
39 # spamassassin checking takes about 8 seconds.
40 slow=false
41 if [[ $1 == slow ]]; then
42 slow=true
43 shift
44 fi
45
46 int=false
47 if [[ $SUDO_USER || $SSH_CONNECTION ]]; then
48 int=true
49 fi
50
51 if [[ $1 == int ]]; then
52 int=true
53 fi
54
55 if [[ $1 == nonint ]]; then
56 int=false
57 fi
58 #### end arg processing ####
59
60
61 if ! $int; then
62 sleep 60
63 fi
64
65
66 # TODO, get je to deliver the local mailbox: /m/md/INBOX
67 # dovecot appears to setup, i can t be sure.
68
69 source /a/bin/bash_unpublished/source-state
70
71 doprom=false
72 case $HOSTNAME in
73 $MAIL_HOST|bk|je)
74 doprom=true
75 ;;
76 *)
77 rm -f /var/lib/prometheus/node-exporter/mailtest-check.prom*
78 ;;
79 esac
80
81 main() {
82
83 case $HOSTNAME in
84 bk)
85 folders=(/m/md/{expertpathologyreview.com,amnimal.ninja}/testignore)
86 froms=(ian@iankelling.org z@zroe.org testignore@je.b8.nz iank@gnu.org)
87 ;;
88 je)
89 froms=(ian@iankelling.org z@zroe.org iank@gnu.org testignore@amnimal.ninja)
90 folders=(/m/md/je.b8.nz/testignore)
91 ;;
92 *)
93 folders=(/m/md/l/testignore)
94 froms=(testignore@je.b8.nz testignore@expertpathologyreview.com testignore@amnimal.ninja ian@iankelling.org z@zroe.org iank@gnu.org)
95 if ! $int; then
96 ### begin rsyncing fencepost email ###
97 # We dont want to exit if rsync fails, that will get caught by
98 # our later test by virtue of not having the latest email.
99 did_rsync=false
100 try_start_time=$EPOCHSECONDS
101 try_limit=140 # somewhat arbitrary value
102 while ! $did_rsync; do
103 try_left=$(( try_limit - ( EPOCHSECONDS - try_start_time) ))
104 timeout=120 # somewhat arbitrary value
105 if (( try_left < 0 )); then
106 break
107 fi
108 if (( try_left < timeout )); then
109 timeout=$try_left
110 fi
111 if timeout $timeout rsync --chown iank:iank -e "ssh -oIdentitiesOnly=yes -F /dev/null -i /root/.ssh/jtuttle" -t --inplace -r 'jtuttle@fencepost.gnu.org:/home/j/jtuttle/Maildir/new/' /m/md/l/testignore/new; then
112 did_rsync=true
113 else
114 sleep 4
115 fi
116 done
117 if ! $did_rsync; then
118 echo mailtest-check: warning: fencepost rsync failed
119 fi
120 ### end rsyncing fencepost email ###
121 fi
122 ;;
123 esac
124
125
126 # avoid errors like this:
127 # Nov 8 08:16:05.439 [6080] warn: plugin: failed to parse plugin (from @INC): Can't locate Mail/SpamAssassin/Plugin/WLBLEval.pm: lib/Mail/SpamAssassin/Plugin/WLBLEval.pm: Permission denied at (eval 59) line 1.
128 #Nov 8 08:16:05.439 [6080] warn: plugin: failed to parse plugin (from @INC): Can't locate Mail/SpamAssassin/Plugin/VBounce.pm: lib/Mail/SpamAssassin/Plugin/VBounce.pm: Permission denied at (eval 60) line 1.
129 # i dont know why, i just found the solution online
130 cd /m/md
131
132
133 getspamdpid
134 # first time we write, overwrite anything existing
135 if [[ -e /var/lib/prometheus/node-exporter ]]; then
136 cat >/var/lib/prometheus/node-exporter/mailtest-check.prom.$$ <<EOF
137 mailtest_check_found_spamd_pid_bool $(( ${spamdpid:-0} > 0 ))
138 EOF
139 fi
140 e spamdpid: $spamdpid
141 if [[ ! $spamdpid ]]; then
142 echo mailtest spamd pid not found. systemctl status spamassassin:
143 systemctl status spamassassin
144 fi
145 tmpfile=$(mktemp)
146 declare -i unexpected=0
147 declare -i missing_dnswl=0
148 for folder in ${folders[@]}; do
149 for from in ${froms[@]}; do
150 latest=
151 last_sec=0
152
153 if ! grep -rlFx "From: $from" $folder/{new,cur} >$tmpfile; then
154 echo "no message found from: $from"
155 continue
156 fi
157 # webmail sends them to cur it seems
158 while read -r file; do
159 file_sec=$(awk '/^Subject: / {print $4}' $file)
160 if [[ $file_sec ]] && (( file_sec > last_sec )); then
161 latest=$file
162 last_sec="$file_sec"
163 fi
164 done <$tmpfile
165 rm -f $tmpfile
166
167 to=$(awk '/^Envelope-to: / {print $2}' $latest)
168
169 if $slow; then
170 if ! $int; then
171 find $folder/new $folder/cur -type f -mmin +1080 -delete
172 fi
173 getspamdpid
174 if [[ $spamdpid ]]; then
175 if [[ $(readlink /proc/$$/ns/net) != "$(readlink /proc/$spamdpid/ns/net)" ]]; then
176 spamcpre="nsenter -t $spamdpid -n -m"
177 fi
178 unset results
179 declare -A results
180 # pyzor fails for our test message, so dont put useless load on their
181 # servers.
182 # example line that sed is parsing:
183 # (-0.1 / 5.0 requ) DKIM_SIGNED=0.1,DKIM_VALID=-0.1,DKIM_VALID_AU=-0.1,SPF_HELO_PASS=-0.001,SPF_PASS=-0.001,TVD_SPACE_RATIO=0.001 autolearn=_AUTOLEARN
184 resultfile=$(mktemp)
185 $spamcpre sudo -u Debian-exim spamassassin -D -t --cf='score PYZOR_CHECK 0' <"$latest" &>$resultfile
186
187 # note: on some mail, its 1 line after the send-test-forward, on others its 2 with a blank inbetween.
188 # I use the sed -n to filter this.
189 raw_results="$(tail $resultfile | grep -A2 -Fx /usr/local/bin/send-test-forward | tail -n+2 | sed -nr 's/^\([^)]*\) *//;s/=[^, ]*([, ]|$)/ /gp')"
190 for r in $raw_results; do
191 case $r in
192 # got this in an update 2022-01. dun care
193 T_SCC_BODY_TEXT_LINE|SCC_BODY_SINGLE_WORD) : ;;
194 # we have a new domain, ignore this.
195 # it seems like some versions of spamassassin do BODY_SINGLE_WORD, others dont, we dun care.
196 # bayes_00 is a new one indicating ham, we dont care if its missing.
197 BAYES_00|BODY_SINGLE_WORD|FROM_FMBLA_NEWDOM*|autolearn) : ;;
198 SPF_HELO_NEUTRAL)
199 # some of my domains use neutral spf, treat them the same.
200 results[SPF_HELO_PASS]=t
201 ;;
202 *)
203 results[$r]=t
204 ;;
205 esac
206 done
207 # debugging
208 # e results = ${!results[@]}
209 missing=()
210
211 keys=(DKIM_SIGNED DKIM_VALID{,_AU,_EF} SPF_HELO_PASS SPF_PASS TVD_SPACE_RATIO)
212 if [[ $to == *@gnu.org && $from == *@gnu.org ]]; then
213 keys=(ALL_TRUSTED TVD_SPACE_RATIO)
214 elif [[ $to == *@gnu.org ]]; then
215 # eggs has RCVD_IN_DNSWL_MED
216 keys+=(RCVD_IN_DNSWL_MED)
217 elif [[ $from == *@gnu.org ]]; then
218 # eggs has these
219 keys+=(RCVD_IN_DNSWL_MED DKIMWL_WL_HIGH)
220 fi
221
222 for t in ${keys[@]}; do
223 if [[ ${results[$t]} ]]; then
224 unset "results[$t]"
225 elif [[ $t == DKIM_VALID_EF && $from == *@[^.]*.[^.]*.[^.]* ]]; then
226 :
227 # third level domains dont hit this. its because
228 # /usr/share/perl5/Mail/SpamAssassin/Plugin/DKIM.pm checks
229 # if its signed with the registryboundaries domain. afaik:
230 # we need the actual domain to sign it, this would result in
231 # a second signature. I only use second level domains for
232 # testing atm, fsf doesnt use them for anything but the
233 # forum and I dont expect that to have any deliverability
234 # problems. So, not bothering atm.
235 else
236 missing+=($t)
237 fi
238 done
239 if (( ${#results[@]} || ${#missing[@]} )); then
240 printf "$HOSTNAME spamtest %s\n" "$latest"
241 if (( ${#results[@]} )); then
242 printf "unexpected %s" "${!results[*]} "
243 fi
244 if (( ${#missing[@]} )); then
245 printf "missing %s" "${missing[*]}"
246 fi
247 echo # ends our printf string buildup
248 cat $resultfile
249 echo mailtest-check: end of spam debug results
250 # lets just handle 1 failure at a time in interactive mode.
251 if $int; then
252 echo mailtest-check: from: $from, to: $to
253 exit 0
254 fi
255
256 # less verbose debug output, commented since I might want it another time.
257 # if $int; then
258 # echo mailtest-check: cat $latest:
259 # cat $latest
260 # echo mailtest-check: end of cat
261 # echo "$(tput setaf 5 2>/dev/null ||:)█$(tput sgr0 2>/dev/null||:)%.0s" $(eval echo "{1..${COLUMNS:-60}}")
262 #fi
263 fi
264 rm -f $resultfile
265 for r in ${results[@]}; do
266 case $r in
267 DKIM_INVALID|T_SPF_TEMPERROR|T_SPF_HELO_TEMPERROR)
268 missing_dnswl+=1
269 ;;
270 *)
271 unexpected=$(( unexpected + 1 ))
272 ;;
273 esac
274 done
275 for miss in ${missing[@]}; do
276 # We expect dns failures from time to time, so
277 # we count them separately and alert differently.
278 case $miss in
279 DKIM_VALID|DKIM_VALID_AU|DKIM_VALID_EF|SPF_HELO_PASS|SPF_PASS|RCVD_IN_DNSWL_MED|DKIMWL_WL_HIGH)
280 missing_dnswl+=1
281 ;;
282 *)
283 unexpected+=1
284 ;;
285 esac
286 done
287 fi # if spamdpid
288 fi # if $slow
289
290 now=$EPOCHSECONDS
291 age_sec=$(( now - last_sec ))
292 e $((age_sec / 60)):$(( age_sec % 60 )) ago. to:$to from:$from $latest
293
294 # usec = unix seconds
295 pr <<EOF
296 mailtest_check_last_usec{folder="$folder",from="$from"} $last_sec
297 EOF
298 done # end for from in ${froms[@]}
299 done # end for folder in ${folders[@]}
300 if $slow; then
301 pr <<EOF
302 mailtest_check_missing_dnswl $missing_dnswl
303 mailtest_check_unexpected_spamd_results $unexpected
304 EOF
305 fi
306
307 dir=/var/lib/prometheus/node-exporter
308 if [[ -e $dir ]]; then
309 mv $dir/mailtest-check.prom.$$ $dir/mailtest-check.prom
310 # note: node_textfile_mtime_seconds will tell us when this last happened. useful for debugging.
311 fi
312 }
313
314 loop-main() {
315 # When running under systemd, the system just started. Ve nice and
316 # give programs some time to finish their startup.
317 sleep 10
318 while true; do
319 premain_sec=$EPOCHSECONDS
320 main
321 sleep $(( 300 - ( EPOCHSECONDS - premain_sec ) ))
322 done
323 }
324
325
326 if [[ $INVOCATION_ID ]]; then
327 loop-main
328 else
329 main
330 fi
331
332 exit 0