fix: shellcheck issues

This commit is contained in:
Krzysztof Rudnicki 2025-11-01 15:36:22 +01:00
parent dcd4251992
commit fef44f0015
6 changed files with 178 additions and 117 deletions

View File

@ -12,7 +12,7 @@ log() {
printf '%s - %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$*" | tee -a "$LOG_FILE" >&2 printf '%s - %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$*" | tee -a "$LOG_FILE" >&2
} }
if [[ ! -f "$CANONICAL_SOURCE" ]]; then if [[ ! -f $CANONICAL_SOURCE ]]; then
log "Canonical hosts not found at $CANONICAL_SOURCE; aborting enforcement" log "Canonical hosts not found at $CANONICAL_SOURCE; aborting enforcement"
exit 0 exit 0
fi fi

View File

@ -1,7 +1,7 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -euo pipefail set -euo pipefail
require_root() { if [[ $EUID -ne 0 ]]; then exec sudo -E bash "$0" "$@"; fi } require_root() { if [[ $EUID -ne 0 ]]; then exec sudo -E bash "$0" "$@"; fi; }
require_root "$@" require_root "$@"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

View File

@ -31,7 +31,7 @@ logger -t "$LOGTAG" "post: relocking /etc/hosts (starting)"
echo "$(date -Is) post-relock(start)" >> /run/hosts-guard-hook.log 2> /dev/null || true echo "$(date -Is) post-relock(start)" >> /run/hosts-guard-hook.log 2> /dev/null || true
collapse_mounts collapse_mounts
if [[ -x "$ENFORCE" ]]; then if [[ -x $ENFORCE ]]; then
"$ENFORCE" > /dev/null 2>&1 || true "$ENFORCE" > /dev/null 2>&1 || true
fi fi

View File

@ -16,7 +16,6 @@ stop_units_if_present() {
} }
is_ro_mount() { findmnt -no OPTIONS -T "$TARGET" 2> /dev/null | grep -qw ro; } is_ro_mount() { findmnt -no OPTIONS -T "$TARGET" 2> /dev/null | grep -qw ro; }
is_bind_mount() { findmnt -no OPTIONS -T "$TARGET" 2>/dev/null | grep -qw bind; }
mount_layers_count() { awk '$5=="/etc/hosts"{c++} END{print c+0}' /proc/self/mountinfo 2> /dev/null || echo 0; } mount_layers_count() { awk '$5=="/etc/hosts"{c++} END{print c+0}' /proc/self/mountinfo 2> /dev/null || echo 0; }
cleanup_mount_stacks() { cleanup_mount_stacks() {

View File

@ -11,12 +11,12 @@ DELAY_SECONDS=45
log() { printf '%s - %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$*" | tee -a "$LOG" >&2; } log() { printf '%s - %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$*" | tee -a "$LOG" >&2; }
require_root() { if [[ $EUID -ne 0 ]]; then exec sudo -E bash "$0" "$@"; fi } require_root() { if [[ $EUID -ne 0 ]]; then exec sudo -E bash "$0" "$@"; fi; }
require_root "$@" require_root "$@"
echo "Reason for editing /etc/hosts (will be logged):" >&2 echo "Reason for editing /etc/hosts (will be logged):" >&2
read -r -p "Enter reason: " REASON read -r -p "Enter reason: " REASON
if [[ -z ${REASON// } ]]; then if [[ -z ${REASON// /} ]]; then
echo "Empty reason not allowed. Aborting." >&2 echo "Empty reason not allowed. Aborting." >&2
exit 1 exit 1
fi fi
@ -49,7 +49,7 @@ sha_before=$(sha256sum "$TARGET" | awk '{print $1}')
"$EDITOR_CMD" "$TARGET" "$EDITOR_CMD" "$TARGET"
sha_after=$(sha256sum "$TARGET" | awk '{print $1}') sha_after=$(sha256sum "$TARGET" | awk '{print $1}')
if [[ "$sha_before" == "$sha_after" ]]; then if [[ $sha_before == "$sha_after" ]]; then
log "No changes made to $TARGET. Reason: $REASON" log "No changes made to $TARGET. Reason: $REASON"
logger -t "$SYSLOG_TAG" "no_change user=${SUDO_USER:-$USER} reason='$REASON'" logger -t "$SYSLOG_TAG" "no_change user=${SUDO_USER:-$USER} reason='$REASON'"
else else

View File

@ -47,9 +47,19 @@ msg() { printf '\e[1;32m[+]\e[0m %s\n' "$*"; }
note() { printf '\e[1;34m[i]\e[0m %s\n' "$*"; } note() { printf '\e[1;34m[i]\e[0m %s\n' "$*"; }
warn() { printf '\e[1;33m[!]\e[0m %s\n' "$*"; } warn() { printf '\e[1;33m[!]\e[0m %s\n' "$*"; }
err() { printf '\e[1;31m[x]\e[0m %s\n' "$*" >&2; } err() { printf '\e[1;31m[x]\e[0m %s\n' "$*" >&2; }
run() { if [[ $DRY_RUN -eq 1 ]]; then printf 'DRY-RUN: %s\n' "$*"; else eval "$@"; fi } run() {
if [[ $DRY_RUN -eq 1 ]]; then
printf 'DRY-RUN:'
if [ "$#" -gt 0 ]; then
printf ' %q' "$@"
fi
printf '\n'
else
"$@"
fi
}
require_root() { if [[ $EUID -ne 0 ]]; then exec sudo -E bash "$0" "$@"; fi } require_root() { if [[ $EUID -ne 0 ]]; then exec sudo -E bash "$0" "$@"; fi; }
usage() { sed -n '1,/^set -euo pipefail/p' "$0" | sed 's/^# \{0,1\}//'; } usage() { sed -n '1,/^set -euo pipefail/p' "$0" | sed 's/^# \{0,1\}//'; }
@ -58,21 +68,71 @@ usage() { sed -n '1,/^set -euo pipefail/p' "$0" | sed 's/^# \{0,1\}//'; }
###################################################################### ######################################################################
while [[ $# -gt 0 ]]; do while [[ $# -gt 0 ]]; do
case "$1" in case "$1" in
--force-snapshot) FORCE_SNAPSHOT=1 ; shift ;; --force-snapshot)
--no-snapshot) DO_SNAPSHOT=0 ; shift ;; FORCE_SNAPSHOT=1
--skip-bind) ENABLE_BIND=0 ; shift ;; shift
--skip-path-watch) ENABLE_PATH=0 ; shift ;; ;;
--delay) DELAY=${2:-} ; [[ -z ${DELAY} ]] && { err '--delay requires value'; exit 2; } ; shift 2 ;; --no-snapshot)
--dry-run) DRY_RUN=1 ; shift ;; DO_SNAPSHOT=0
--no-shell-hooks) INSTALL_SHELL_HOOKS=0 ; shift ;; shift
--shell-hooks) INSTALL_SHELL_HOOKS=1 ; shift ;; ;;
--no-audit) INSTALL_AUDIT_RULE=0 ; shift ;; --skip-bind)
--audit) INSTALL_AUDIT_RULE=1 ; shift ;; ENABLE_BIND=0
--no-alias-stub) ADD_ALIAS_STUB=0 ; shift ;; shift
--alias-stub) ADD_ALIAS_STUB=1 ; shift ;; ;;
--uninstall) UNINSTALL=1 ; shift ;; --skip-path-watch)
-h|--help) usage; exit 0 ;; ENABLE_PATH=0
*) err "Unknown argument: $1"; usage; exit 2 ;; shift
;;
--delay)
DELAY=${2:-}
[[ -z ${DELAY} ]] && {
err '--delay requires value'
exit 2
}
shift 2
;;
--dry-run)
DRY_RUN=1
shift
;;
--no-shell-hooks)
INSTALL_SHELL_HOOKS=0
shift
;;
--shell-hooks)
INSTALL_SHELL_HOOKS=1
shift
;;
--no-audit)
INSTALL_AUDIT_RULE=0
shift
;;
--audit)
INSTALL_AUDIT_RULE=1
shift
;;
--no-alias-stub)
ADD_ALIAS_STUB=0
shift
;;
--alias-stub)
ADD_ALIAS_STUB=1
shift
;;
--uninstall)
UNINSTALL=1
shift
;;
-h | --help)
usage
exit 0
;;
*)
err "Unknown argument: $1"
usage
exit 2
;;
esac esac
done done
@ -134,10 +194,13 @@ note "Script directory: $SCRIPT_DIR"
note "Repository root: $REPO_ROOT" note "Repository root: $REPO_ROOT"
for req in "$TEMPLATE_ENFORCE" "$TEMPLATE_UNLOCK" "$UNIT_GUARD_SERVICE"; do for req in "$TEMPLATE_ENFORCE" "$TEMPLATE_UNLOCK" "$UNIT_GUARD_SERVICE"; do
[[ -f $req ]] || { err "Missing template: $req"; exit 1; } [[ -f $req ]] || {
err "Missing template: $req"
exit 1
}
done done
if [[ ! -f "$HOSTS" ]]; then if [[ ! -f $HOSTS ]]; then
err "$HOSTS does not exist. Run your hosts/install.sh first." err "$HOSTS does not exist. Run your hosts/install.sh first."
exit 1 exit 1
fi fi
@ -146,7 +209,7 @@ fi
# Snapshot # Snapshot
###################################################################### ######################################################################
if [[ $DO_SNAPSHOT -eq 1 ]]; then if [[ $DO_SNAPSHOT -eq 1 ]]; then
if [[ -f "$CANON" && $FORCE_SNAPSHOT -eq 0 ]]; then if [[ -f $CANON && $FORCE_SNAPSHOT -eq 0 ]]; then
note "Canonical snapshot exists (use --force-snapshot to overwrite)" note "Canonical snapshot exists (use --force-snapshot to overwrite)"
else else
msg "Creating canonical snapshot at $CANON" msg "Creating canonical snapshot at $CANON"
@ -182,8 +245,6 @@ fi
if [[ $INSTALL_SHELL_HOOKS -eq 1 ]]; then if [[ $INSTALL_SHELL_HOOKS -eq 1 ]]; then
msg "Installing shell history suppression hooks for unlock command" msg "Installing shell history suppression hooks for unlock command"
# Pattern matches commands invoking unlock-hosts (with or without sudo) & setup script force snapshot # Pattern matches commands invoking unlock-hosts (with or without sudo) & setup script force snapshot
FILTER_PATTERN='(^|;|&&|\|\|)\s*(sudo\s+)?(/usr/local/sbin/)?unlock-hosts(\s|;|$)'
# Zsh: use zshaddhistory function # Zsh: use zshaddhistory function
if command -v zsh > /dev/null 2>&1; then if command -v zsh > /dev/null 2>&1; then
if [[ $DRY_RUN -eq 1 ]]; then if [[ $DRY_RUN -eq 1 ]]; then
@ -268,15 +329,16 @@ fi
###################################################################### ######################################################################
if [[ $INSTALL_AUDIT_RULE -eq 1 ]]; then if [[ $INSTALL_AUDIT_RULE -eq 1 ]]; then
if command -v auditctl > /dev/null 2>&1; then if command -v auditctl > /dev/null 2>&1; then
AUDIT_RULE="-w /usr/local/sbin/unlock-hosts -p x -k hosts_unlock" audit_rule_str="-w /usr/local/sbin/unlock-hosts -p x -k hosts_unlock"
audit_rule_args=(-w /usr/local/sbin/unlock-hosts -p x -k hosts_unlock)
if auditctl -l 2> /dev/null | grep -Fq "/usr/local/sbin/unlock-hosts"; then if auditctl -l 2> /dev/null | grep -Fq "/usr/local/sbin/unlock-hosts"; then
note "Audit rule already present" note "Audit rule already present"
else else
run auditctl $AUDIT_RULE || warn "Failed to add audit rule (runtime)" run auditctl "${audit_rule_args[@]}" || warn "Failed to add audit rule (runtime)"
if [[ $DRY_RUN -eq 1 ]]; then if [[ $DRY_RUN -eq 1 ]]; then
echo "DRY-RUN: would create /etc/audit/rules.d/hosts_unlock.rules" echo "DRY-RUN: would create /etc/audit/rules.d/hosts_unlock.rules"
else else
echo "$AUDIT_RULE" > /etc/audit/rules.d/hosts_unlock.rules echo "$audit_rule_str" > /etc/audit/rules.d/hosts_unlock.rules
fi fi
fi fi
else else