99 lines
3.8 KiB
Bash
99 lines
3.8 KiB
Bash
# shellcheck shell=bash
|
|
# Generation of the managed block for after.rules (iptables-restore format).
|
|
# Only values validated by lib/config.sh go into it.
|
|
|
|
fence_block_begin() {
|
|
printf '# BEGIN %s (%s)' "${CFG[block_marker]}" "$(fence_fmt block_header)"
|
|
}
|
|
|
|
fence_block_end() {
|
|
printf '# END %s' "${CFG[block_marker]}"
|
|
}
|
|
|
|
fence_deny_chain() {
|
|
printf '%s-deny' "${CFG[chain]}"
|
|
}
|
|
|
|
# Every rule of the block, in order, one per line:
|
|
# ROLE<US>INDEX<US>CHAIN<US>ARGUMENTS
|
|
# ROLE says what the rule is for (used by the rules listing), INDEX points
|
|
# into the rule arrays of lib/config.sh (or is empty). The block (generate)
|
|
# and the kernel check (rules) both come from this single list.
|
|
fence_rule_list() {
|
|
local c="${CFG[chain]}" d i iface a
|
|
local us=$'\x1f'
|
|
d="$(fence_deny_chain)"
|
|
printf '%s\n' "jump${us}${us}DOCKER-USER${us}-j $c" "return${us}${us}DOCKER-USER${us}-j RETURN"
|
|
printf '%s\n' "established${us}${us}$c${us}-m conntrack --ctstate RELATED,ESTABLISHED -j RETURN"
|
|
for i in "${!TRUSTED_IFACE[@]}"; do
|
|
iface="${TRUSTED_IFACE[i]}"
|
|
printf '%s\n' "trusted${us}$i${us}$c${us}-i $iface -j RETURN"
|
|
done
|
|
for i in "${!ADMIN_SRC[@]}"; do
|
|
a=""
|
|
[[ -n "${ADMIN_IFACE[i]}" ]] && a+="-i ${ADMIN_IFACE[i]} "
|
|
a+="-s ${ADMIN_SRC[i]} -j RETURN"
|
|
printf '%s\n' "admin${us}$i${us}$c${us}$a"
|
|
done
|
|
for i in "${!ALLOW_PROTO[@]}"; do
|
|
a=""
|
|
[[ -n "${ALLOW_IFACE[i]}" ]] && a+="-i ${ALLOW_IFACE[i]} "
|
|
a+="-p ${ALLOW_PROTO[i]} -m conntrack --ctorigdstport ${ALLOW_PORT[i]}"
|
|
[[ "${ALLOW_SRC[i]}" != any ]] && a+=" -s ${ALLOW_SRC[i]}"
|
|
a+=" -j RETURN"
|
|
printf '%s\n' "allow${us}$i${us}$c${us}$a"
|
|
done
|
|
if [[ "${CFG[mode]}" == enforce ]]; then
|
|
a="-m conntrack --ctstate DNAT -j $d"
|
|
else
|
|
a="-m conntrack --ctstate DNAT -m limit --limit ${CFG[log_rate_observe]} --limit-burst ${CFG[log_burst_observe]}"
|
|
a+=" -j LOG --log-prefix \"${CFG[log_prefix_observe]} \" --log-level ${CFG[log_level]}"
|
|
fi
|
|
printf '%s\n' "final${us}${us}$c${us}$a" "return${us}${us}$c${us}-j RETURN"
|
|
a="-m limit --limit ${CFG[log_rate_enforce]} --limit-burst ${CFG[log_burst_enforce]}"
|
|
a+=" -j LOG --log-prefix \"${CFG[log_prefix_drop]} \" --log-level ${CFG[log_level]}"
|
|
printf '%s\n' "deny_log${us}${us}$d${us}$a" "deny_drop${us}${us}$d${us}-j DROP"
|
|
# Container -> host rules: ufw-after-input is declared earlier in
|
|
# after.rules, rules are only appended to it.
|
|
for i in "${!C2H_PROTO[@]}"; do
|
|
a="-i ${C2H_IFACE[i]} -p ${C2H_PROTO[i]} --dport ${C2H_PORT[i]} -j ACCEPT"
|
|
printf '%s\n' "c2h${us}$i${us}ufw-after-input${us}$a"
|
|
done
|
|
}
|
|
|
|
# Splits the arguments of a rule into FENCE_ARGS; "..." is one argument
|
|
# (only the log prefixes are quoted, and they hold no quote themselves).
|
|
fence_split_args() {
|
|
local s="$1" re='^[[:space:]]*"([^"]*)"(.*)$' re2='^[[:space:]]*([^[:space:]"]+)(.*)$'
|
|
FENCE_ARGS=()
|
|
while [[ -n "${s//[[:space:]]/}" ]]; do
|
|
if [[ "$s" =~ $re || "$s" =~ $re2 ]]; then
|
|
FENCE_ARGS+=("${BASH_REMATCH[1]}")
|
|
s="${BASH_REMATCH[2]}"
|
|
else
|
|
return 1
|
|
fi
|
|
done
|
|
}
|
|
|
|
# Prints the whole block to standard output.
|
|
fence_generate_block() {
|
|
local c d role _idx chain args c2h_open=0
|
|
c="${CFG[chain]}"
|
|
d="$(fence_deny_chain)"
|
|
fence_block_begin; printf '\n'
|
|
printf '*filter\n'
|
|
printf ':DOCKER-USER - [0:0]\n'
|
|
printf ':%s - [0:0]\n' "$c"
|
|
printf ':%s - [0:0]\n' "$d"
|
|
while IFS=$'\x1f' read -r role _idx chain args; do
|
|
if [[ "$role" == c2h && "$c2h_open" -eq 0 ]]; then
|
|
printf 'COMMIT\n*filter\n'
|
|
c2h_open=1
|
|
fi
|
|
printf -- '-A %s %s\n' "$chain" "$args"
|
|
done < <(fence_rule_list)
|
|
printf 'COMMIT\n'
|
|
fence_block_end; printf '\n'
|
|
}
|