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