Debian Codegraph @

debian/crowdsec-firewall-bouncer.postinst

raw
1 #!/bin/sh
2 set -e
3
4 CONFIG=/etc/crowdsec/bouncers/crowdsec-firewall-bouncer.yaml
5 PENDING=/var/lib/crowdsec/pending-registration
6
7 # Create a configuration override file only once, see README.Debian:
8 if [ "$1" = configure ] && [ ! -f "$CONFIG.local" ]; then
9 # Bail early if the Recommends on crowdsec isn't met:
10 if ! which cscli >/dev/null 2>&1; then
11 echo "W: cscli not found, no automatic registration" >&2
12 else
13 # Generate a unique identifier, since a given API can deal with several
14 # machines; reuse the logic from crowdsec's postinst:
15 unique=$(tr -dc 'a-zA-Z0-9' < /dev/urandom | fold -w 32 | head -n 1)
16 id="FirewallBouncer-$unique"
17
18 # crowdsec might be unpacked but not configured (#1036985):
19 if [ -f /etc/crowdsec/config.yaml ]; then
20 api_key=$(cscli --error -oraw bouncers add "$id")
21 if [ -z "$api_key" ]; then
22 echo "E: Local registration failed to yield an API key" >&2
23 exit 1
24 fi
25 else
26 api_key=$(tr -dc 'a-f0-9' < /dev/urandom | fold -w 32 | head -n 1)
27 if [ ! -f $PENDING ]; then
28 touch $PENDING
29 chmod 600 $PENDING
30 fi
31 echo "crowdsec-firewall-bouncer $id $api_key" >> $PENDING
32 fi
33
34 # Store it so that it can be unregistered when purging the package:
35 echo "$id" > "$CONFIG.id"
36 fi
37
38 # Logic and rationale detailed in README.Debian:
39 alternative=$(update-alternatives --query iptables 2>/dev/null|awk '/^Value: / {print $2}')
40 if [ "$alternative" = /usr/sbin/iptables-legacy ]; then
41 if ! which ipset >/dev/null 2>&1; then
42 echo 'W: Configuring iptables (iptables-legacy detected) but ipset is missing [see README.Debian]' >&2
43 else
44 echo 'I: Configuring iptables (iptables-legacy detected) [see README.Debian]' >&2
45 fi
46 firewall=iptables
47 else
48 echo 'I: Configuring nftables [see README.Debian]' >&2
49 firewall=nftables
50 fi
51
52 touch "$CONFIG.local"
53 chmod 600 "$CONFIG.local"
54
55 # Generate the override:
56 if [ -n "$api_key" ]; then
57 cat > "$CONFIG.local" <<EOF
58 mode: $firewall
59 api_key: $api_key
60 EOF
61 else
62 cat > "$CONFIG.local" <<EOF
63 mode: $firewall
64 # non-local crowdsec setup: please specify both api_url and api_key manually
65 EOF
66 fi
67 echo "To adjust the config: editor $CONFIG.local && systemctl restart crowdsec-firewall-bouncer" >&2
68 fi
69
70 ##DEBHELPER##