Files
CISS.debian.installer/lib/cdi_0100_arg/0104_arg_passphrase_modules.sh
Marc S. Weidner d0b363d7d4
All checks were successful
🛡️ Shell Script Linting / 🛡️ Shell Script Linting (push) Successful in 53s
V8.00.000.2025.06.17
Signed-off-by: Marc S. Weidner <msw@coresecret.dev>
2025-09-05 21:35:38 +02:00

110 lines
2.8 KiB
Bash

#!/bin/bash
# SPDX-Version: 3.0
# SPDX-CreationInfo: 2025-06-17; WEIDNER, Marc S.; <msw@coresecret.dev>
# SPDX-ExternalRef: GIT https://git.coresecret.dev/msw/CISS.debian.installer.git
# SPDX-FileContributor: WEIDNER, Marc S.; Centurion Intelligence Consulting Agency
# SPDX-FileCopyrightText: 2024-2025; WEIDNER, Marc S.; <msw@coresecret.dev>
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: EUPL-1.2 OR LicenseRef-CCLA-1.0
# SPDX-LicenseComment: This file is part of the CISS.debian.installer.secure framework.
# SPDX-PackageName: CISS.debian.installer
# SPDX-Security-Contact: security@coresecret.eu
guard_sourcing
#######################################
# Generates salt.
# Globals:
# ERR_GENERATE_SALT
# NL
# Arguments:
# None
# Returns:
# 0: on success
# ERR_GENERATE_SALT
#######################################
generate_salt() {
declare var_salt=""
while :; do
# shellcheck disable=SC2312
var_salt=$(head -c 12 /dev/random | base64 | tr -dc 'A-Za-z0-9' | head -c 16) || return "${ERR_GENERATE_SALT}"
[[ ${#var_salt} -eq 16 ]] && break
done
printf '%s%b' "${var_salt}" "${NL}"
return 0
}
#######################################
# Reads and validates a secure one-line password file.
# Globals:
# ERR_READ_PASS_FILE
# VAR_DEBUG_TRACE
# Arguments:
# 1: Path to password file
# 2: Name of target variable (via nameref)
# Returns:
# 0: on success
# ERR_READ_PASS_FILE
#######################################
read_password_file() {
declare -r var_input_file="${1}"
declare -n var_output_file="${2}"
declare -a ary_lines=()
guard_trace on
if [[ ! -f "${var_input_file}" ]]; then
do_log "fatal" "file_only" "0104() Password file '${var_input_file}' not found."
return "${ERR_READ_PASS_FILE}"
fi
mapfile -t ary_lines < "${var_input_file}"
if (( ${#ary_lines[@]} != 1 )); then
do_log "fatal" "file_only" "0104() Password file '${var_input_file}' MUST contain exactly one line."
return "${ERR_READ_PASS_FILE}"
fi
var_output_file="${ary_lines[0]}"
### Trim leading and trailing whitespace.
var_output_file="${var_output_file#"${var_output_file%%[![:space:]]*}"}" ### leading
var_output_file="${var_output_file%"${var_output_file##*[![:space:]]}"}" ### trailing
if [[ -z "${var_output_file}" ]]; then
do_log "fatal" "file_only" "0104() Password file '${var_input_file}' contains only whitespace."
return "${ERR_READ_PASS_FILE}"
fi
guard_trace off
sync
if shred -vfzu -n 5 "${var_input_file}" > /dev/null 2>&1; then
do_log "info" "file_only" "0104() Password file '${var_input_file}': shred -vfzu -n 5 >> done."
else
do_log "warn" "file_only" "0104() Password file '${var_input_file}': shred -vfzu -n 5 >> NOT successful."
fi
sync
unset ary_lines
return 0
}
# vim: number et ts=2 sw=2 sts=2 ai tw=128 ft=sh