Blueforce-Linux/scripts/install/installer/identity.sh
2026-09-17 13:22:26 +03:00

36 lines
1.1 KiB
Bash

#!/usr/bin/env bash
# Shared shell identity contract.
BF_IDENTITY_FILE="${BF_IDENTITY_FILE:-/etc/blueforce/dealer-id}"
bf_valid_dealer_id() { [[ "${1:-}" =~ ^[0-9]{8}$ ]]; }
bf_username() { printf 'bf-%s' "$1"; }
bf_hostname() { printf 'bf-%s' "$1"; }
bf_device_id() { printf 'BF-%s' "$1"; }
bf_write_identity() {
local dealer="$1" dir tmp existing
bf_valid_dealer_id "$dealer" || return 1
dir="$(dirname "$BF_IDENTITY_FILE")"
install -d -m 755 "$dir"
if [[ -f "$BF_IDENTITY_FILE" ]]; then
existing="$(tr -d '[:space:]' < "$BF_IDENTITY_FILE")"
[[ "$existing" == "$dealer" ]] || {
printf 'FAIL: dealer identity is already locked to %s (requested %s)\n' "$existing" "$dealer" >&2
return 1
}
return 0
fi
tmp="${BF_IDENTITY_FILE}.tmp.$$"
printf '%s\n' "$dealer" > "$tmp"
chmod 600 "$tmp"
mv -f "$tmp" "$BF_IDENTITY_FILE"
}
bf_load_identity() {
local dealer
[[ -r "$BF_IDENTITY_FILE" ]] || return 1
dealer="$(tr -d '[:space:]' < "$BF_IDENTITY_FILE")"
bf_valid_dealer_id "$dealer" || return 1
printf '%s' "$dealer"
}