225 lines
6.6 KiB
Bash
225 lines
6.6 KiB
Bash
# shellcheck shell=bash
|
|
# after.rules file: finding blocks, composing the new content, atomic
|
|
# write, backups and rotation.
|
|
|
|
readonly FENCE_LEGACY_BEGIN='# BEGIN UFW AND DOCKER'
|
|
readonly FENCE_LEGACY_END='# END UFW AND DOCKER'
|
|
|
|
# Original ufw-docker block, known version (README of the ufw-docker project).
|
|
# Compared without empty lines nor trailing blanks.
|
|
fence_legacy_known() {
|
|
cat <<'EOF'
|
|
# BEGIN UFW AND DOCKER
|
|
*filter
|
|
:ufw-user-forward - [0:0]
|
|
:ufw-docker-logging-deny - [0:0]
|
|
:DOCKER-USER - [0:0]
|
|
-A DOCKER-USER -j ufw-user-forward
|
|
-A DOCKER-USER -j RETURN -s 10.0.0.0/8
|
|
-A DOCKER-USER -j RETURN -s 172.16.0.0/12
|
|
-A DOCKER-USER -j RETURN -s 192.168.0.0/16
|
|
-A DOCKER-USER -p udp -m udp --sport 53 --dport 1024:65535 -j RETURN
|
|
-A DOCKER-USER -j ufw-docker-logging-deny -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -d 192.168.0.0/16
|
|
-A DOCKER-USER -j ufw-docker-logging-deny -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -d 10.0.0.0/8
|
|
-A DOCKER-USER -j ufw-docker-logging-deny -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -d 172.16.0.0/12
|
|
-A DOCKER-USER -j ufw-docker-logging-deny -p udp -m udp --dport 0:32767 -d 192.168.0.0/16
|
|
-A DOCKER-USER -j ufw-docker-logging-deny -p udp -m udp --dport 0:32767 -d 10.0.0.0/8
|
|
-A DOCKER-USER -j ufw-docker-logging-deny -p udp -m udp --dport 0:32767 -d 172.16.0.0/12
|
|
-A DOCKER-USER -j RETURN
|
|
-A ufw-docker-logging-deny -m limit --limit 3/min --limit-burst 10 -j LOG --log-prefix "[UFW DOCKER BLOCK] "
|
|
-A ufw-docker-logging-deny -j DROP
|
|
COMMIT
|
|
# END UFW AND DOCKER
|
|
EOF
|
|
}
|
|
|
|
# Reads a file into the FENCE_LINES array.
|
|
fence_read_lines() {
|
|
FENCE_LINES=()
|
|
mapfile -t FENCE_LINES <"$1"
|
|
}
|
|
|
|
# Looks for a block in FENCE_LINES.
|
|
# fence_find_block BEGIN_REGEX END_REGEX
|
|
# Returns 0 and sets FENCE_B / FENCE_E (0-based indexes) if found; 1 if
|
|
# missing; 2 if the file is inconsistent (FENCE_BLOCK_ERR = key).
|
|
fence_find_block() {
|
|
local re_b="$1" re_e="$2" i
|
|
FENCE_B=-1 FENCE_E=-1
|
|
for i in "${!FENCE_LINES[@]}"; do
|
|
if [[ "${FENCE_LINES[i]}" =~ $re_b ]]; then
|
|
if (( FENCE_B >= 0 )); then
|
|
FENCE_BLOCK_ERR=e_block_twice
|
|
return 2
|
|
fi
|
|
FENCE_B=$i
|
|
elif [[ "${FENCE_LINES[i]}" =~ $re_e ]]; then
|
|
if (( FENCE_B < 0 || FENCE_E >= 0 )); then
|
|
FENCE_BLOCK_ERR=e_block_end
|
|
return 2
|
|
fi
|
|
FENCE_E=$i
|
|
fi
|
|
done
|
|
if (( FENCE_B < 0 )); then
|
|
return 1
|
|
fi
|
|
if (( FENCE_E < 0 )); then
|
|
FENCE_BLOCK_ERR=e_block_unclosed
|
|
return 2
|
|
fi
|
|
return 0
|
|
}
|
|
|
|
fence_regex_quote() {
|
|
printf '%s' "$1" | sed 's/[][\.*^$+?(){}|/]/\\&/g'
|
|
}
|
|
|
|
fence_find_managed() {
|
|
local m
|
|
m="$(fence_regex_quote "${CFG[block_marker]}")"
|
|
fence_find_block "^# BEGIN ${m}( |\$)" "^# END ${m}[[:space:]]*\$"
|
|
}
|
|
|
|
fence_find_legacy() {
|
|
fence_find_block "^${FENCE_LEGACY_BEGIN}[[:space:]]*\$" "^${FENCE_LEGACY_END}[[:space:]]*\$"
|
|
}
|
|
|
|
# Removes lines FENCE_B..FENCE_E from FENCE_LINES.
|
|
fence_cut_block() {
|
|
FENCE_LINES=("${FENCE_LINES[@]:0:FENCE_B}" "${FENCE_LINES[@]:FENCE_E+1}")
|
|
}
|
|
|
|
# Prints lines FENCE_B..FENCE_E.
|
|
fence_print_block() {
|
|
printf '%s\n' "${FENCE_LINES[@]:FENCE_B:FENCE_E-FENCE_B+1}"
|
|
}
|
|
|
|
# Removes the trailing empty lines of FENCE_LINES.
|
|
fence_trim_trailing_blank() {
|
|
local n=${#FENCE_LINES[@]}
|
|
while (( n > 0 )) && [[ "${FENCE_LINES[n-1]}" =~ ^[[:space:]]*$ ]]; do
|
|
n=$((n - 1))
|
|
done
|
|
FENCE_LINES=("${FENCE_LINES[@]:0:n}")
|
|
}
|
|
|
|
# Is the original block found (FENCE_B..FENCE_E) the known version?
|
|
fence_legacy_is_known() {
|
|
diff -q <(fence_print_block | sed 's/[[:space:]]*$//' | grep -v '^$') \
|
|
<(fence_legacy_known) >/dev/null
|
|
}
|
|
|
|
fence_legacy_diff() {
|
|
diff -u <(fence_legacy_known) <(fence_print_block | sed 's/[[:space:]]*$//' | grep -v '^$') \
|
|
| sed '1,2d' || true
|
|
}
|
|
|
|
# Prints FENCE_LINES followed, if asked, by a block (file).
|
|
# fence_compose [BLOCK_FILE]
|
|
fence_compose() {
|
|
if (( ${#FENCE_LINES[@]} > 0 )); then
|
|
printf '%s\n' "${FENCE_LINES[@]}"
|
|
[[ -n "${1:-}" ]] && printf '\n'
|
|
fi
|
|
if [[ -n "${1:-}" ]]; then
|
|
cat -- "$1"
|
|
fi
|
|
}
|
|
|
|
# Writes standard input to TARGET atomically: temporary file in the same
|
|
# directory, same mode and owner as the target, then mv.
|
|
fence_atomic_write() {
|
|
local target="$1" dir base tmp st
|
|
dir="$(dirname -- "$target")"
|
|
base="$(basename -- "$target")"
|
|
tmp="$(mktemp -- "$dir/.$base.XXXXXX")" || return 1
|
|
if ! cat >"$tmp"; then
|
|
rm -f -- "$tmp"
|
|
return 1
|
|
fi
|
|
if [[ -e "$target" ]]; then
|
|
st="$(stat -c '%a %u %g' -- "$target")"
|
|
local mode="${st%% *}" rest="${st#* }"
|
|
local uid="${rest% *}" gid="${rest#* }"
|
|
if ! chmod "$mode" -- "$tmp" || ! chown "$uid:$gid" -- "$tmp" 2>/dev/null; then
|
|
rm -f -- "$tmp"
|
|
return 1
|
|
fi
|
|
else
|
|
chmod 0640 -- "$tmp"
|
|
fi
|
|
if ! mv -f -- "$tmp" "$target"; then
|
|
rm -f -- "$tmp"
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
fence_backup_dir() {
|
|
fence_path "${CFG[backup_dir]}"
|
|
}
|
|
|
|
fence_rules_path() {
|
|
fence_path "${CFG[ufw_after_rules]}"
|
|
}
|
|
|
|
fence_pending_file() {
|
|
printf '%s/pending-rollback' "$(fence_backup_dir)"
|
|
}
|
|
|
|
fence_ensure_backup_dir() {
|
|
local dir
|
|
dir="$(fence_backup_dir)"
|
|
if [[ ! -d "$dir" ]]; then
|
|
(umask 077 && mkdir -p -- "$dir")
|
|
fi
|
|
}
|
|
|
|
# Backs up the rules file. fence_backup PREFIX; prints the path.
|
|
fence_backup() {
|
|
local prefix="$1" src dir dest
|
|
src="$(fence_rules_path)"
|
|
dir="$(fence_backup_dir)"
|
|
fence_ensure_backup_dir || return 1
|
|
dest="$dir/$prefix.$(date +%Y%m%d-%H%M%S-%N)"
|
|
(umask 077 && cat -- "$src" >"$dest") || return 1
|
|
chmod 0600 -- "$dest"
|
|
fence_rotate "$prefix"
|
|
printf '%s\n' "$dest"
|
|
}
|
|
|
|
# Lists the backups of a prefix, oldest first.
|
|
fence_list_backups() {
|
|
local prefix="$1" dir f
|
|
dir="$(fence_backup_dir)"
|
|
[[ -d "$dir" ]] || return 0
|
|
for f in "$dir/$prefix".[0-9]*; do
|
|
[[ -f "$f" ]] && printf '%s\n' "$f"
|
|
done | sort
|
|
}
|
|
|
|
# Keeps the backup_keep most recent backups, never removing the one a
|
|
# pending rollback waits for.
|
|
fence_rotate() {
|
|
local prefix="$1" keep="${CFG[backup_keep]}" pending="" f
|
|
local -a all
|
|
[[ -f "$(fence_pending_file)" ]] && pending="$(<"$(fence_pending_file)")"
|
|
mapfile -t all < <(fence_list_backups "$prefix")
|
|
local excess=$(( ${#all[@]} - keep ))
|
|
for f in "${all[@]}"; do
|
|
(( excess > 0 )) || break
|
|
if [[ "$f" != "$pending" ]]; then
|
|
rm -f -- "$f"
|
|
fi
|
|
excess=$((excess - 1))
|
|
done
|
|
}
|
|
|
|
fence_rules_basename() {
|
|
basename -- "${CFG[ufw_after_rules]}"
|
|
}
|
|
|
|
fence_latest_backup() {
|
|
fence_list_backups "$(fence_rules_basename)" | tail -n 1
|
|
}
|