Can remove the sender and/or the recipient from the key of the greylister.
[apps/pfixtools.git] / tools / postlicyd-rsyncrbl
1 #!/bin/bash
2
3 # Setup and Create temporary path
4 init() {
5     export LC_ALL=C
6     export LANC=C
7     export LANGUAGE=C
8
9     tmp=$(mktemp -td postlicyd-rsyncrbl.XXXXXX) || exit 1
10     successfile="$tmp/.success"
11 }
12
13 # Cleanup and force reload of the postlicyd daemon
14 fini() {
15     if [ -f "$successfile" ] && [ -f "$pidfile" ] ; then
16         pid=$(cat "$pidfile")
17         [ -z "$pid" ] || kill -HUP $(cat "$pidfile")
18     fi
19     rm -rf $tmp
20 }
21
22 # Bye bye beautifyl
23 die() {
24     echo "$1"
25     fini
26     exit 1
27 }
28
29 # Usage
30 usage() {
31     echo "usage: $(basename "$0") confile spoolroot pidfile"
32     [ -z "$1" ] || echo "$1"
33     exit 1
34 }
35
36 file_stats() {
37   if [ -f "$1" ] ; then
38       stat -c="%s-%Y" "$1"
39   else
40       echo "0-0"
41   fi
42 }
43
44 # Rsync a rbl file
45 getfile() {
46     url="$1"
47     out="$2"
48
49     mtime_before=$(file_stats "$out")
50     rsync --no-motd -q -t -T "$tmp" "$url" "$out" || return 1
51     mtime_after=$(file_stats "$out")
52
53     # The file has changed
54     if [ "$mtime_after" != "$mtime_before" ] ; then
55         # Update ownership and permissions
56         chown nobody:nogroup "$out"
57         chmod 600 "$out"
58
59         # Touch the successful file that is used to check if a reload
60         # is needed
61         touch "$successfile"
62     fi
63     return 0
64 }
65
66 # Check command line arguments
67 conf="$1"
68 spool="$2"
69 pidfile="$3"
70
71 [ -z "$pidfile" ] && usage
72 [ -f "$conf" ] || usage "configuration file ($conf) does not exists"
73 [ -d "$spool" ] || usage "spool directory ($spool) does not exists"
74
75 init
76
77 cd "$spool" || die "Can't cd to $spool"
78
79 for line in $(grep -v "^#" "$conf" | grep '=') ; do
80     url=${line/*=/}
81     out=${line/=*/}
82     getfile $url $out || echo "error downloading file $out from $url"
83 done
84
85 fini
86 exit 0