SPDX: Mass-update to GPL-2.0-or-later
[exim.git] / src / util / renew-opendmarc-tlds.sh
1 #!/bin/sh -eu
2 # Copyright (c) The Exim Maintainers 2022
3 # SPDX-License-Identifier: GPL-2.0-or-later
4 #
5 # Short version of this script:
6 #   curl -f -o /var/cache/exim/opendmarc.tlds https://publicsuffix.org/list/public_suffix_list.dat
7 # but run as Exim runtime user, writing to a place it can write to, and with
8 # sanity checks and atomic replacement.
9 #
10 # For now, we deliberately leave the invalid file around for analysis
11 # with .<pid> suffix.
12 #
13 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~8< cut here >8~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
14 #
15 # Create a cron-job as the Exim run-time user to invoke this daily, with a
16 # single parameter, 'cron'.  Eg:
17 #
18 #    3 4 * * *    /usr/local/sbin/renew-opendmarc-tlds.sh cron
19 #
20 # That will, at 3 minutes past the 4th hour (in whatever timezone cron is
21 # running it) invoke this script with 'cron'; we will then sleep between 10 and
22 # 50 seconds, before continuing.
23 #
24 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~8< cut here >8~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25 #
26 # This should be "pretty portable"; the only things it depends upon are:
27 #  * a POSIX shell which additionally implements 'local' (dash works)
28 #  * the 'curl' command; change the fetch_candidate() function to replace that
29 #  * the 'stat' command, to get the size of a file; else Perl
30 #    + change size_of() if need be; it's defined per-OS
31 #  * the 'hexdump' command and /dev/urandom existing
32 #    + used when invoked with 'cron', to avoid retrieving on a minute boundary
33 #      and contending with many other automated systems.
34 #    + with bash/zsh, can replace with: $(( 10 + ( RANDOM % 40 ) ))
35 #    + on Debian/Ubuntu systems, hexdump is in the 'bsdmainutils' package.
36
37 # Consider putting an email address inside the parentheses, something like
38 # noc@example.org or other reachable address, so that if something goes wrong
39 # and the server operators need to step in, they can see from logs who to
40 # contact instead of just blocking your IP:
41 readonly CurlUserAgent='renew-opendmarc-tlds/0.1 (distributed with Exim)'
42
43 # change this to your Exim run-time user (exim -n -bP exim_user) :
44 readonly RuntimeUser='_exim'
45
46 # Do not make this a directory which untrusted users can write to:
47 readonly StateDir='/var/cache/exim'
48
49 readonly URL='https://publicsuffix.org/list/public_suffix_list.dat'
50
51 readonly TargetShortFile='opendmarc.tlds'
52
53 # When replacing, new file must be at least this percentage the size of
54 # the old one or it's an error:
55 readonly MinNewSizeRation=90
56
57 # Each of these regexps must be matched by the file, or it's an error:
58 readonly MustExistRegexps='
59   ^ac\.uk$
60   ^org$
61   ^tech$
62   '
63
64 # =======================8< end of configuration >8=======================
65
66 set -eu
67
68 readonly FullTargetPath="${StateDir}/${TargetShortFile}"
69 readonly WorkingFile="${FullTargetPath}.$$"
70
71 progname="$(basename "$0")"
72 note() { printf >&2 '%s: %s\n' "$progname" "$*"; }
73 die() { note "$@"; exit 1; }
74
75 # guard against stomping on file-permissions
76 [ ".$(id -un)" = ".${RuntimeUser:?}" ] || \
77   die "must be invoked as ${RuntimeUser}"
78
79 fetch_candidate() {
80         curl --user-agent "$CurlUserAgent" -fSs -o "${WorkingFile}" "${URL}"
81 }
82
83 case $(uname -s) in
84 *BSD|Darwin)
85         size_of() { stat -f %z "$1"; }
86         ;;
87 Linux)
88         size_of() { stat -c %s "$1"; }
89         ;;
90 *)
91         # why do we live in a world where Perl is the safe portable solution
92         # to getting the size of a file?
93         size_of() { perl -le 'print((stat($ARGV[0]))[7])' -- "$1"; }
94         ;;
95 esac
96
97 sanity_check_candidate() {
98         local new_size prev_size re
99         new_size="$(size_of "$WorkingFile")"
100
101         for re in $MustExistRegexps; do
102                 grep -qs "$re" -- "$WorkingFile" || \
103                   die "regexp $re not found in $WorkingFile"
104         done
105
106         if ! prev_size="$(size_of "$FullTargetPath")"; then
107                 note "missing previous file, can't size-compare: $FullTargetPath"
108                 # We're sane by definition, probably initial fetch, and the
109                 # stat failure and this note will be printed.  That's fine; if
110                 # a cron invocation is missing the file then something has gone
111                 # badly wrong.
112                 return 0
113         fi
114         local ratio
115         ratio=$(expr $new_size \* 100 / $prev_size)
116         if [ $ratio -lt $MinNewSizeRation ]; then
117                 die "New $TargetShortFile candidate only ${ratio}% size of old; $new_size vs $prev_size"
118         fi
119 }
120
121 if [ "${1:-.}" = "cron" ]; then
122         shift
123         # Don't pull on-the-minute, wait for off-cycle-peak
124         sleep $(( ($(dd if=/dev/urandom bs=1 count=1 2>/dev/null | hexdump -e '1/1 "%u"') % 40) + 10))
125 fi
126
127 umask 022
128 fetch_candidate
129 sanity_check_candidate
130 mv -- "$WorkingFile" "$FullTargetPath"