#!/usr/bin/env bash # shellcheck shell=bash # # Destructive-operation confirmation infrastructure. # # There are two independent gates: # # 1. PHOEBUS_DESTRUCTIVE_AUTHORIZED must be "yes" before run_destructive will # execute anything. Only install.sh sets it, and only after the user has # approved the generated plan. # 2. Whole-disk erase additionally requires the user to type the word ERASE. # # This means a bug in the TUI cannot silently wipe a disk: every destructive # command goes through run_destructive. declare -g PHOEBUS_DESTRUCTIVE_AUTHORIZED="${PHOEBUS_DESTRUCTIVE_AUTHORIZED:-no}" destructive_authorized() { [[ "$PHOEBUS_DESTRUCTIVE_AUTHORIZED" == "yes" ]]; } # destructive_grant / destructive_revoke destructive_grant() { PHOEBUS_DESTRUCTIVE_AUTHORIZED="yes"; log_warn "destructive operations AUTHORIZED for this session"; } destructive_revoke() { PHOEBUS_DESTRUCTIVE_AUTHORIZED="no"; log_info "destructive operations revoked"; } # _prompt_yesno MESSAGE - returns 0 for yes _prompt_yesno() { local message="$1" if declare -F ui_yesno >/dev/null 2>&1; then ui_yesno "Confirm" "$message" return $? fi local answer printf '%s [y/N] ' "$message" >&2 read -r answer || return 1 [[ "$answer" =~ ^[Yy] ]] } # _prompt_word PROMPT EXPECTED - returns 0 when the typed word matches _prompt_word() { local prompt="$1" expected="$2" answer if declare -F ui_inputbox >/dev/null 2>&1; then ui_inputbox "Destructive operation" "$prompt" "" || return 1 answer="$UI_RESULT" else printf '%s ' "$prompt" >&2 read -r answer || return 1 fi [[ "$answer" == "$expected" ]] } # confirm_action MESSAGE - plain yes/no confirmation confirm_action() { if config_bool ASSUME_YES; then log_warn "auto-approving (ASSUME_YES): $1" return 0 fi _prompt_yesno "$1" } # confirm_erase PREVIEW - strong confirmation for whole-disk destruction. # Displays PREVIEW and requires the user to type ERASE. confirm_erase() { local preview="$1" if config_bool ASSUME_YES; then log_warn "auto-approving ERASE confirmation (ASSUME_YES)" return 0 fi if declare -F ui_msgbox >/dev/null 2>&1; then ui_msgbox "DESTRUCTIVE OPERATION" "$preview Type ERASE to continue." else printf '%s\n\n' "$preview" >&2 fi _prompt_word "Type ERASE to confirm:" "ERASE" } # require_destructive_authorization CALLER - hard gate used by run_destructive require_destructive_authorization() { if ! destructive_authorized; then log_error "blocked destructive operation from ${1:-unknown}: not authorized" return 1 fi return 0 } # run_destructive DESCRIPTION ARGS... # Execute a previously approved destructive command. Honours DRY_RUN via # run_cmd. Refuses to run without authorization. run_destructive() { local desc="${1:-destructive operation}"; shift require_destructive_authorization "$desc" || return 1 run_cmd "$desc" "$@" }