# shellcheck shell=bash # rules: one table with the native UFW rules and the docker-fence rules. # Read only. UFW rules come from "ufw status verbose"; docker-fence rules # come from the configuration, and their state from "iptables -C" on the # exact rules of the block (lib/generate.sh). # Table rows: fields separated by US, in column order. FENCE_ROWS=() fence_row() { local IFS=$'\x1f' FENCE_ROWS+=("$*") } # Length in characters (not bytes) of a string. fence_strlen() { local LC_ALL=C.UTF-8 printf '%s' "${#1}" } # Colors: --color always, never, or auto (standard output is a terminal, # NO_COLOR unset, TERM not dumb). fence_color_enabled() { case "${OPT_COLOR:-auto}" in always) return 0 ;; never) return 1 ;; esac [[ -t 1 && -z "${NO_COLOR:-}" && "${TERM:-dumb}" != dumb ]] } # SGR code of a cell, from its column and value; empty for no color. # fence_cell_style KIND COLUMN TEXT (KIND: head, sep or row) fence_cell_style() { local kind="$1" col="$2" text="$3" case "$kind" in head) printf '1'; return ;; sep) printf '2'; return ;; esac case "$col" in 0) case "$text" in ufw) printf '36' ;; fence) printf '35' ;; esac ;; 2) case "$text" in ALLOW) printf '1;32' ;; DENY|REJECT|DROP) printf '1;31' ;; LIMIT|LOG) printf '1;33' ;; esac ;; 5) if [[ "$text" == "$(fence_fmt l_loaded)" || "$text" == "$(fence_fmt l_active)" ]]; then printf '32' elif [[ "$text" == "$(fence_fmt l_not_loaded)" ]]; then printf '31' elif [[ "$text" == "$(fence_fmt l_unknown)" ]]; then printf '33' fi ;; 6) printf '2' ;; esac } # Prints FENCE_ROWS as an aligned table, with a header. fence_print_table() { local -a head=("$@") widths=() cells local row i n w color=0 fence_color_enabled && color=1 for i in "${!head[@]}"; do widths[i]="$(fence_strlen "${head[i]}")" done for row in "${FENCE_ROWS[@]}"; do IFS=$'\x1f' read -r -a cells <<<"$row" for i in "${!head[@]}"; do n="$(fence_strlen "${cells[i]:-}")" (( n > widths[i] )) && widths[i]=$n done done fence_table_line head "${head[@]}" local -a dashes=() for i in "${!head[@]}"; do w="${widths[i]}" dashes+=("$(printf '%*s' "$w" '' | tr ' ' '-')") done fence_table_line sep "${dashes[@]}" for row in "${FENCE_ROWS[@]}"; do IFS=$'\x1f' read -r -a cells <<<"$row" local -a full=() for i in "${!head[@]}"; do full+=("${cells[i]:-}") done fence_table_line row "${full[@]}" done } # One table line, padded to the widths computed by fence_print_table. The # padding is computed on the plain text, colors only wrap the text. # fence_table_line KIND CELL... fence_table_line() { local kind="$1" shift local i last=$(( $# - 1 )) cell pad out="" sgr local -a c=("$@") # Last non-empty cell: no trailing blanks after it. while (( last > 0 )) && [[ -z "${c[last]}" ]]; do last=$(( last - 1 )) done for i in "${!c[@]}"; do (( i <= last )) || break cell="${c[i]}" pad=$(( widths[i] - $(fence_strlen "$cell") )) if (( color )) && [[ -n "$cell" ]]; then sgr="$(fence_cell_style "$kind" "$i" "$cell")" [[ -n "$sgr" ]] && cell=$'\e['"${sgr}m$cell"$'\e[0m' fi out+="$cell" (( i < last )) && out+="$(printf '%*s' "$(( pad + 2 ))" '')" done printf '%s\n' "$out" } # ---------------------------------------------------------------- UFW rules # "Anywhere" -> any, "Anywhere on wg0" -> any on wg0 (translated); IPv6 # marker removed. fence_ufw_addr() { local s="$1" iface="" s="${s/ (v6)/}" if [[ "$s" =~ ^(.*)\ on\ ([^ ]+)$ ]]; then s="${BASH_REMATCH[1]}" iface="${BASH_REMATCH[2]}" fi s="${s/Anywhere/$(fence_fmt l_any)}" [[ -n "$iface" ]] && s+=" $(fence_fmt l_on "$iface")" printf '%s' "$s" } fence_ufw_rows() { local out line state to action from comment dir traffic note if ! out="$("${CFG[bin_ufw]}" status verbose 2>/dev/null)"; then fence_warn w_ufw_unreadable return 0 fi if ! grep -q '^Status: active' <<<"$out"; then fence_warn w_ufw_inactive_rules return 0 fi state="$(fence_fmt l_active)" local in_table=0 local -a f while IFS= read -r line; do if [[ "$line" =~ ^--[[:space:]] ]]; then in_table=1 continue fi (( in_table )) || continue [[ -n "${line//[[:space:]]/}" ]] || continue # Columns are separated by two blanks or more. mapfile -t f < <(sed -E 's/[[:space:]]{2,}/\n/g' <<<"$line") to="${f[0]:-}" action="${f[1]:-}" from="${f[2]:-}" comment="${f[3]:-}" comment="${comment#\# }" dir="${action##* }" action="${action%% *}" case "$dir" in OUT) traffic="$(fence_fmt l_host_out)" ;; FWD) traffic="$(fence_fmt l_routed)" ;; *) traffic="$(fence_fmt l_host_in)" ;; esac note="$comment" if [[ "$to" == *"(v6)"* || "$from" == *"(v6)"* ]]; then note="IPv6${note:+, $note}" fi fence_row ufw "$traffic" "$action" "$(fence_ufw_addr "$to")" "$(fence_ufw_addr "$from")" "$state" "$note" done <<<"$out" # Default policies, applied after every rule. local def re='Default: ([a-z]+) \(incoming\), ([a-z]+) \(outgoing\), ([a-z]+|disabled) \(routed\)' def="$(grep -m1 '^Default:' <<<"$out" || true)" if [[ "$def" =~ $re ]]; then local any any="$(fence_fmt l_any)" fence_row ufw "$(fence_fmt l_host_in)" "${BASH_REMATCH[1]^^}" "$any" "$any" "$state" "$(fence_fmt l_default_policy)" fence_row ufw "$(fence_fmt l_host_out)" "${BASH_REMATCH[2]^^}" "$any" "$any" "$state" "$(fence_fmt l_default_policy)" fence_row ufw "$(fence_fmt l_routed)" "${BASH_REMATCH[3]^^}" "$any" "$any" "$state" "$(fence_fmt l_default_policy)" fi } # ------------------------------------------------------- docker-fence rules # State of one rule of the block, once the firewall is known to be readable: # loaded or not loaded. iptables -C exits 1 for a missing rule, but 2 when a # chain it names does not exist yet: both mean not loaded. fence_rule_state() { local chain="$1" args="$2" if fence_split_args "$args" \ && "${CFG[bin_iptables]}" -C "$chain" "${FENCE_ARGS[@]}" >/dev/null 2>&1; then fence_fmt l_loaded else fence_fmt l_not_loaded fi } fence_port_text() { printf '%s/%s' "$(fence_port_conf "$1")" "$2" } fence_fence_rows() { local role idx chain args state jump=ok any to from note any="$(fence_fmt l_any)" local pub c2h pub="$(fence_fmt l_published)" c2h="$(fence_fmt l_c2h)" # Firewall readable (root)? Otherwise every state is unknown. if fence_have bin_iptables && "${CFG[bin_iptables]}" -S INPUT >/dev/null 2>&1; then # Without the DOCKER-USER jump, no rule of the chain applies. if "${CFG[bin_iptables]}" -C DOCKER-USER -j "${CFG[chain]}" >/dev/null 2>&1; then jump=ok else jump=missing fi else jump=unknown fi while IFS=$'\x1f' read -r role idx chain args; do case "$role" in jump|return|deny_log|deny_drop) continue ;; esac if [[ "$jump" == unknown ]]; then state="$(fence_fmt l_unknown)" elif [[ "$role" != c2h && "$jump" == missing ]]; then state="$(fence_fmt l_not_loaded)" else state="$(fence_rule_state "$chain" "$args")" fi case "$role" in established) fence_row fence "$pub" ALLOW "$any" "$any" "$state" "$(fence_fmt l_replies)" ;; trusted) fence_row fence "$pub" ALLOW "$any" "$(fence_fmt l_iface "${TRUSTED_IFACE[idx]}")" "$state" "$(fence_fmt l_containers)" ;; admin) from="${ADMIN_SRC[idx]}" [[ -n "${ADMIN_IFACE[idx]}" ]] && from+=" $(fence_fmt l_on "${ADMIN_IFACE[idx]}")" fence_row fence "$pub" ALLOW "$(fence_fmt l_all_published)" "$from" "$state" "admin" ;; allow) to="$(fence_port_text "${ALLOW_PORT[idx]}" "${ALLOW_PROTO[idx]}")" from="${ALLOW_SRC[idx]}" [[ "$from" == any ]] && from="$any" [[ -n "${ALLOW_IFACE[idx]}" ]] && from+=" $(fence_fmt l_on "${ALLOW_IFACE[idx]}")" fence_row fence "$pub" ALLOW "$to" "$from" "$state" "allow" ;; final) if [[ "${CFG[mode]}" == enforce ]]; then note="$(fence_fmt l_final_enforce)" fence_row fence "$pub" DROP "$(fence_fmt l_other_published)" "$any" "$state" "$note" else note="$(fence_fmt l_final_observe)" fence_row fence "$pub" LOG "$(fence_fmt l_other_published)" "$any" "$state" "$note" fi ;; c2h) to="$(fence_port_text "${C2H_PORT[idx]}" "${C2H_PROTO[idx]}")" fence_row fence "$c2h" ALLOW "$to" "$(fence_fmt l_iface "${C2H_IFACE[idx]}")" "$state" "container_to_host" ;; esac done < <(fence_rule_list) } fence_cmd_rules() { FENCE_ROWS=() if fence_have bin_ufw; then fence_ufw_rows else fence_warn w_missing_bin "${CFG[bin_ufw]}" fi fence_fence_rows fence_print_table "$(fence_fmt l_origin)" "$(fence_fmt l_traffic)" "$(fence_fmt l_action)" \ "$(fence_fmt l_to)" "$(fence_fmt l_from)" "$(fence_fmt l_state)" "$(fence_fmt l_note)" }