mainly 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 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
97 fi
98 ;;
99 esac
100
101
102 # avoid errors like this:
103 # 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.
104 #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.
105 # i dont know why, i just found the solution online
106 cd /m/md
107
108
109 getspamdpid
110 # first time we write, overwrite anything existing
111 if [[ -e /var/lib/prometheus/node-exporter ]]; then
112 cat >/var/lib/prometheus/node-exporter/mailtest-check.prom.$$ <<EOF
113 mailtest_check_found_spamd_pid_bool $(( ${spamdpid:-0} > 0 ))
114 EOF
115 fi
116 e spamdpid: $spamdpid
117 if [[ ! $spamdpid ]]; then
118 echo mailtest spamd pid not found. systemctl status spamassassin:
119 systemctl status spamassassin
120 fi
121 tmpfile=$(mktemp)
122 declare -i unexpected=0
123 for folder in ${folders[@]}; do
124 for from in ${froms[@]}; do
125 latest=
126 last_sec=0
127
128 if ! grep -rlFx "From: $from" $folder/{new,cur} >$tmpfile; then
129 echo "no message found from: $from"
130 continue
131 fi
132 # webmail sends them to cur it seems
133 while read -r file; do
134 if [[ $file -nt $latest ]]; then
135 latest=$file
136 fi
137 done <$tmpfile
138
139 to=$(awk '/^Envelope-to: / {print $2}' $latest)
140 last_sec=$(awk '/^Subject: / {print $4}' $latest)
141
142 if $slow; then
143 if ! $int; then
144 find $folder/new $folder/cur -type f -mmin +1080 -delete
145 fi
146 getspamdpid
147 if [[ $spamdpid ]]; then
148 if [[ $(readlink /proc/$$/ns/net) != "$(readlink /proc/$spamdpid/ns/net)" ]]; then
149 spamcpre="nsenter -t $spamdpid -n -m"
150 fi
151
152 declare -A results
153 # pyzor fails for our test message, so dont put useless load on their
154 # servers.
155 # example line that sed is parsing:
156 # (-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
157 resultfile=$(mktemp)
158 $spamcpre sudo -u Debian-exim spamassassin -t --cf='score PYZOR_CHECK 0' <"$latest" &>$resultfile
159
160 raw_results="$(tail $resultfile | grep -A1 -Fx /usr/local/bin/send-test-forward | tail -n1 | sed -r 's/^\([^)]*\) *//;s/=[^, ]*([, ]|$)/ /g')"
161 for r in $raw_results; do
162 case $r in
163 # got this in an update 2022-01. dun care
164 T_SCC_BODY_TEXT_LINE|SCC_BODY_SINGLE_WORD) : ;;
165 # we have a new domain, ignore this.
166 # it seems like some versions of spamassassin do BODY_SINGLE_WORD, others dont, we dun care.
167 # bayes_00 is a new one indicating ham, we dont care if its missing.
168 BAYES_00|BODY_SINGLE_WORD|FROM_FMBLA_NEWDOM*|autolearn) : ;;
169 SPF_HELO_NEUTRAL)
170 # some of my domains use neutral spf, treat them the same.
171 results[SPF_HELO_PASS]=t
172 ;;
173 *)
174 results[$r]=t
175 ;;
176 esac
177 done
178 # debugging
179 # e results = ${!results[@]}
180 missing=()
181
182 keys=(DKIM_SIGNED DKIM_VALID{,_AU,_EF} SPF_HELO_PASS SPF_PASS TVD_SPACE_RATIO)
183 if [[ $to == *@gnu.org && $from == *@gnu.org ]]; then
184 keys=(ALL_TRUSTED TVD_SPACE_RATIO)
185 elif [[ $to == *@gnu.org ]]; then
186 # eggs has RCVD_IN_DNSWL_MED
187 keys+=(RCVD_IN_DNSWL_MED)
188 elif [[ $from == *@gnu.org ]]; then
189 # eggs has these
190 keys+=(RCVD_IN_DNSWL_MED DKIMWL_WL_HIGH)
191 fi
192
193 for t in ${keys[@]}; do
194 if [[ ${results[$t]} ]]; then
195 unset "results[$t]"
196 elif [[ $t == DKIM_VALID_EF && $from == *@[^.]*.[^.]*.[^.]* ]]; then
197 :
198 # third level domains dont hit this. its because
199 # /usr/share/perl5/Mail/SpamAssassin/Plugin/DKIM.pm checks
200 # if its signed with the registryboundaries domain. afaik:
201 # we need the actual domain to sign it, this would result in
202 # a second signature. I only use second level domains for
203 # testing atm, fsf doesnt use them for anything but the
204 # forum and I dont expect that to have any deliverability
205 # problems. So, not bothering atm.
206 else
207 missing+=($t)
208 fi
209 done
210 if (( ${#results[@]} || ${#missing[@]} )); then
211 printf "$HOSTNAME spamtest %s\n" "$latest"
212 if (( ${#results[@]} )); then
213 printf "unexpected %s" "${!results[*]} "
214 fi
215 if (( ${#missing[@]} )); then
216 printf "missing %s" "${missing[*]}"
217 fi
218 echo # ends our printf string buildup
219 cat $resultfile
220 echo mailtest-check: end of spam debug results
221
222 # less verbose debug output, commented since I might want it another time.
223 # if $int; then
224 # echo mailtest-check: cat $latest:
225 # cat $latest
226 # echo mailtest-check: end of cat
227 # echo "$(tput setaf 5 2>/dev/null ||:)█$(tput sgr0 2>/dev/null||:)%.0s" $(eval echo "{1..${COLUMNS:-60}}")
228 #fi
229 fi
230 rm -f $resultfile
231 fi # if spamdpid
232 fi # if $slow
233
234 now=$EPOCHSECONDS
235 age_sec=$(( now - last_sec ))
236 e $((age_sec / 60)):$(( age_sec % 60 )) ago. to:$to from:$from $latest
237
238 # usec = unix seconds
239 pr <<EOF
240 mailtest_check_last_usec{folder="$folder",from="$from"} $last_sec
241 EOF
242 done
243 unexpected=$(( unexpected + ${#results[@]} + ${#missing[@]} ))
244 done
245 if $slow; then
246 pr <<EOF
247 mailtest_check_unexpected_spamd_results $unexpected
248 EOF
249 fi
250
251 dir=/var/lib/prometheus/node-exporter
252 if [[ -e $dir ]]; then
253 mv $dir/mailtest-check.prom.$$ $dir/mailtest-check.prom
254 # note: node_textfile_mtime_seconds will tell us when this last happened. useful for debugging.
255 fi
256 }
257
258 loop-main() {
259 while true; do
260 premain_sec=$EPOCHSECONDS
261 main
262 sleep $(( 300 - ( $EPOCHSECONDS - premain_sec ) ))
263 done
264 }
265
266
267 if [[ $INVOCATION_ID ]]; then
268 loop-main
269 else
270 main
271 fi
272
273 exit 0