aboutsummaryrefslogtreecommitdiff
path: root/parcimini.bash
blob: 016f166487170e908648430c71e534c8d8b39d07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
self=parcimini

# Base interval between key retrievals from first arg; default 20 mins
interval=${1:-120}

# Check we have gpg and shuf, neither of which are POSIX
hash gpg || exit
hash shuf || exit

# Make a temporary file for the key listings, delete on exit
trap 'rm -f "$list"' EXIT
list=$(mktemp) || exit

# Define a function to retrieve all keychain fingerprints
key_ids() {
    gpg --batch --no-tty --list-keys --with-colons |
    awk 'BEGIN { FS = ":" }
$1 == "pub" { pub = 1 ; next }
$1 == "fpr" && pub { pub = 0 ; key_ids[$(NF-1)]++ }
END { for (key_id in key_ids) print key_id }'
}

# Log process start
printf >&2 '%s: Started; base interval %u seconds.\n' \
    "$self" "$interval"

# While we're able to write a shuffled key list to the file, refresh all of them
while key_ids | shuf > "$list" ; do
    printf >&2 '%s: Beginning new round; %u key IDs found.\n' \
        "$self" "$(sed '$=;d' "$list")"

    # Shuffle list and read each ID
    while read -r key_id ; do

        # Sleep for a random interval
        spell=$((RANDOM % interval + 1))
        printf >&2 '%s: Sleeping for %u seconds...\n' \
            "$self" "$spell"
        sleep "$spell"

        # Retrieve key
        printf >&2 '%s: Retrieving key %s...\n' \
            "$self" "$key_id"
        gpg --batch --no-tty --recv-key "$key_id"

    done < $list
done