#!/usr/bin/env bash # shellcheck shell=bash # # Configuration validation. # # Pure(ish) decision logic, separated from the TUI so it can be unit tested and # reused for unattended installs. validate_config prints one problem per line # and returns the number of problems found. validate_hostname() { local h="$1" [[ ${#h} -ge 1 && ${#h} -le 63 ]] || { printf 'hostname must be 1-63 characters\n'; return 1; } [[ "$h" =~ ^[A-Za-z0-9]([A-Za-z0-9-]*[A-Za-z0-9])?$ ]] || { printf 'hostname may only contain letters, digits and internal hyphens\n'; return 1; } return 0 } validate_username() { local u="$1" [[ -n "$u" ]] || { printf 'username is required\n'; return 1; } [[ "$u" != "root" ]] || { printf 'refusing to create an account named root\n'; return 1; } [[ ${#u} -le 32 ]] || { printf 'username must be at most 32 characters\n'; return 1; } [[ "$u" =~ ^[a-z_][a-z0-9_-]*$ ]] || { printf 'username must start with a lowercase letter and contain only [a-z0-9_-]\n'; return 1; } return 0 } validate_locale() { local l="$1" [[ -n "$l" ]] || { printf 'locale is required\n'; return 1; } local base="${l%%.*}" if [[ -r /usr/share/i18n/locales/$base ]] || \ [[ -d /usr/lib/locale && -e /usr/lib/locale/locale-archive ]]; then return 0 fi [[ "$l" =~ ^[A-Za-z_]+\.?[A-Za-z0-9-]*$ ]] && return 0 printf 'locale looks malformed: %s\n' "$l" return 1 } validate_timezone() { local tz="$1" [[ -n "$tz" ]] || { printf 'timezone is required\n'; return 1; } if [[ -d /usr/share/zoneinfo ]]; then [[ -e "/usr/share/zoneinfo/$tz" && ! -d "/usr/share/zoneinfo/$tz" ]] && return 0 printf 'unknown timezone: %s\n' "$tz" return 1 fi return 0 } validate_keymap() { local km="$1" [[ -n "$km" ]] || { printf 'keymap is required\n'; return 1; } return 0 } validate_target_disk() { local disk="$1" [[ -n "$disk" ]] || { printf 'no target disk selected\n'; return 1; } [[ -b "$disk" ]] || { printf 'target is not a block device: %s\n' "$disk"; return 1; } local reason reason="$(disk_protected_reason "$disk")" if [[ -n "$reason" ]]; then printf 'refusing target %s: %s\n' "$disk" "$reason" return 1 fi return 0 } # validate_config [KEYS...] - print problems; return problem count validate_config() { local -a keys=("$@") local problems=0 msg ((${#keys[@]})) || keys=(HOSTNAME USERNAME LOCALE TIMEZONE KEYMAP TARGET_DISK) local key for key in "${keys[@]}"; do case "$key" in HOSTNAME) validate_hostname "$(config_get HOSTNAME)" && true || { problems=$((problems+1)); } ;; USERNAME) validate_username "$(config_get USERNAME)" && true || { problems=$((problems+1)); } ;; LOCALE) validate_locale "$(config_get LOCALE)" && true || { problems=$((problems+1)); } ;; TIMEZONE) validate_timezone "$(config_get TIMEZONE)" && true || { problems=$((problems+1)); } ;; KEYMAP) validate_keymap "$(config_get KEYMAP)" && true || { problems=$((problems+1)); } ;; TARGET_DISK) validate_target_disk "$(config_get TARGET_DISK)" && true || { problems=$((problems+1)); } ;; esac done return "$problems" } # validate_password_strength PASSWORD - print a short assessment validate_password_strength() { local pw="$1" score=0 ((${#pw} >= 8)) && score=$((score+1)) ((${#pw} >= 12)) && score=$((score+1)) [[ "$pw" =~ [A-Z] ]] && score=$((score+1)) [[ "$pw" =~ [a-z] ]] && score=$((score+1)) [[ "$pw" =~ [0-9] ]] && score=$((score+1)) [[ "$pw" =~ [^A-Za-z0-9] ]] && score=$((score+1)) if ((score <= 2)); then printf 'weak' elif ((score <= 4)); then printf 'fair' else printf 'strong'; fi }