various 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
20 ## Minutes before we give error.
21 # We run this cronjob along with sending the test email every 5 minutes,
22 # so give it 1 minute to arrive, then if the latest email is older than
23 # 7 minutes, the last 2 haven't arrived in a reasonable amount of time.
24 # However, when machines reboot things can get delayed, so add 10 mins,
25 # not sure if that is a good number or not.
26 min_limit=17
27
28
29 # spamassassin checking takes about 8 seconds. only do that every
30 # once in a while.
31 slow=false
32 if [[ $1 == slow ]]; then
33 slow=true
34 shift
35 fi
36
37 int=false
38 if [[ $SUDO_USER || $SSH_CONNECTION ]]; then
39 int=true
40 fi
41
42 if [[ $1 == int ]]; then
43 int=true
44 fi
45
46 if [[ $1 == nonint ]]; then
47 int=false
48 fi
49
50
51 if ! $int; then
52 sleep 60
53 fi
54
55 # avoid errors like this:
56 # 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.
57 #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.
58 # i dont know why, i just found the solution online
59 cd /m/md
60
61 case $HOSTNAME in
62 bk)
63 folders=(/m/md/{expertpathologyreview.com,amnimal.ninja}/testignore)
64 froms=(ian@iankelling.org z@zroe.org testignore@je.b8.nz iank@gnu.org)
65 ;;
66 je)
67 froms=(ian@iankelling.org z@zroe.org testignore@expertpathologyreview.com testignore@amnimal.ninja)
68 folders=(/m/md/je.b8.nz/testignore)
69 ;;
70 *)
71 folders=(/m/md/l/testignore)
72 froms=(testignore@je.b8.nz testignore@expertpathologyreview.com testignore@amnimal.ninja ian@iankelling.org z@zroe.org iank@gnu.org)
73 if ! $int; then
74 timeout 120 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
75 fi
76 ;;
77 esac
78
79 getspamdpid() {
80 if [[ ! $spamdpid || ! -d /proc/$spamdpid ]]; then
81 # try twice in case we are restarting, it happens.
82 for i in 1 2; do
83 spamdpid=$(systemctl show --property MainPID --value spamassassin | sed 's/^[10]$//' ||:)
84 if [[ $spamdpid ]]; then
85 break
86 fi
87 sleep 30
88 done
89 fi
90 }
91 getspamdpid
92 pr() {
93 if [[ -e /var/lib/prometheus/node-exporter ]]; then
94 cat >>/var/lib/prometheus/node-exporter/mailtest-check.prom.$$
95 fi
96 }
97 pr <<EOF
98 mailtest_check_found_spamd_pid_bool $(( ${spamdpid:-0} > 0 ))
99 EOF
100 e spamdpid: $spamdpid
101 if [[ ! $spamdpid ]]; then
102 echo $HOSTNAME mailtest spamd pid not found. systemctl status spamassassin:
103 systemctl status spamassassin
104 fi
105 tmpfile=$(mktemp)
106 declare -i unexpected=0
107 for folder in ${folders[@]}; do
108 for from in ${froms[@]}; do
109 latest=
110 last_sec=0
111
112 if ! grep -rlFx "From: $from" $folder/{new,cur} >$tmpfile; then
113 e "no message found from: $from"
114 continue
115 fi
116 # webmail sends them to cur it seems
117 while read -r file; do
118 if [[ $file -nt $latest ]]; then
119 latest=$file
120 fi
121 done <$tmpfile
122
123 if [[ ! $latest ]]; then
124 # 10 is an arbitrary bad value
125 unexpected+=10
126 else
127 to=$(awk '/^Envelope-to: / {print $2}' $latest)
128 last_sec=$(awk '/^Subject: / {print $4}' $latest)
129
130 if $slow; then
131 if ! $int; then
132 find $folder/new $folder/cur -type f -mmin +1080 -delete
133 fi
134 getspamdpid
135 if [[ $spamdpid ]]; then
136 if [[ $(readlink /proc/$$/ns/net) != "$(readlink /proc/$spamdpid/ns/net)" ]]; then
137 spamcpre="nsenter -t $spamdpid -n -m"
138 fi
139
140 declare -A results
141 # pyzor fails for our test message, so dont put useless load on their
142 # servers.
143 # example line that sed is parsing:
144 # (-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
145 raw_results="$($spamcpre sudo -u Debian-exim spamassassin -t --cf='score PYZOR_CHECK 0' <"$latest" | tail -n2 | head -n1 | sed -r 's/^\([^)]*\) *//;s/=[^, ]*([, ]|$)/ /g')"
146 for r in $raw_results; do
147 case $r in
148 # got this in an update 2022-01. dun care
149 T_SCC_BODY_TEXT_LINE|SCC_BODY_SINGLE_WORD) : ;;
150 # we have a new domain, ignore this.
151 # it seems like some versions of spamassassin do BODY_SINGLE_WORD, others dont, we dun care.
152 # bayes_00 is a new one indicating ham, we dont care if its missing.
153 BAYES_00|BODY_SINGLE_WORD|FROM_FMBLA_NEWDOM*|autolearn) : ;;
154 SPF_HELO_NEUTRAL)
155 # some of my domains use neutral spf, treat them the same.
156 results[SPF_HELO_PASS]=t
157 ;;
158 *)
159 results[$r]=t
160 ;;
161 esac
162 done
163 # debugging
164 # e results = ${!results[@]}
165 missing=()
166
167 keys=(DKIM_SIGNED DKIM_VALID{,_AU,_EF} SPF_HELO_PASS SPF_PASS TVD_SPACE_RATIO)
168 if [[ $to == *@gnu.org && $from == *@gnu.org ]]; then
169 keys=(ALL_TRUSTED TVD_SPACE_RATIO)
170 elif [[ $to == *@gnu.org ]]; then
171 # eggs has RCVD_IN_DNSWL_MED
172 keys+=(RCVD_IN_DNSWL_MED)
173 elif [[ $from == *@gnu.org ]]; then
174 # eggs has these
175 keys+=(RCVD_IN_DNSWL_MED DKIMWL_WL_HIGH)
176 fi
177
178 for t in ${keys[@]}; do
179 if [[ ${results[$t]} ]]; then
180 unset "results[$t]"
181 elif [[ $t == DKIM_VALID_EF && $from == *@[^.]*.[^.]*.[^.]* ]]; then
182 :
183 # third level domains dont hit this. its because
184 # /usr/share/perl5/Mail/SpamAssassin/Plugin/DKIM.pm checks
185 # if its signed with the registryboundaries domain. afaik:
186 # we need the actual domain to sign it, this would result in
187 # a second signature. I only use second level domains for
188 # testing atm, fsf doesnt use them for anything but the
189 # forum and I dont expect that to have any deliverability
190 # problems. So, not bothering atm.
191 else
192 missing+=($t)
193 fi
194 done
195 if (( ${#results[@]} || ${#missing[@]} )); then
196 printf "$HOSTNAME spamtest %s/%s\n" "$latest"
197 if (( ${#results[@]} )); then
198 printf "unexpected %s" "${!results[*]} "
199 fi
200 if (( ${#missing[@]} )); then
201 printf "missing %s" "${missing[*]}"
202 fi
203 echo
204 echo mailtest-check: cat $latest:
205 cat $latest
206 echo mailtest-check: end of cat
207 printf "$(tput setaf 5 2>/dev/null ||:)█$(tput sgr0 2>/dev/null||:)%.0s" $(eval echo "{1..${COLUMNS:-60}}")
208 fi
209 fi # if spamdpid
210 fi # if $slow
211 fi # if [[ $latest ]]
212
213 now=$(date +%s)
214 limit=$(( now - 60 * min_limit ))
215 age_sec=$(( now - last_sec ))
216 e $((age_sec / 60)):$(( age_sec % 60 )) ago. to:$to from:$from $latest
217
218 if (( last_sec <= limit )); then
219 echo $HOSTNAME mailtest $folder $from $(date -d @$last_sec +'%a %m-%d %H:%M')
220 fi
221 # usec = unix seconds
222 pr <<EOF
223 mailtest_check_last_usec{folder="$folder",from="$from"} $last_sec
224 EOF
225 done
226 done
227 if $slow; then
228 pr <<EOF
229 mailtest_check_unexpected_spamd_results $unexpected
230 EOF
231 fi
232
233 dir=/var/lib/prometheus/node-exporter
234 if [[ -e $dir ]]; then
235 mv $dir/mailtest-check.prom.$$ $dir/mailtest-check.prom
236 fi