#!/usr/bin/env bash # NexgenStreamer platform bootstrap. Usage, as root on Ubuntu 20.04 or later: # apt-get update && apt-get install -y ca-certificates curl jq && \ # curl --proto '=https' --tlsv1.2 -fsSL https://installer.nexgenstreamer.com/platform/installer.sh | bash # Without a terminal, put the activation key in a root-owned 0600 file and run: # ... | bash -s -- --activation-key-file /root/nexgen-activation.key # # This tracked file is a template: scripts/installer-release.sh pins the # release origin and signing key. No product binary is public: the trial or # licence activation key obtains a short-lived download of its assigned shared # variant installer, whose size and SHA-256 come from the fixed HTTPS licensing # endpoint. The key is read from /dev/tty (echo off) or a protected file and # never appears on a command line, stdout or a log. Every action lives in # functions and runs only from the final complete `nexgen_main` call, inside one # brace group bash must parse completely, so a truncated download executes # nothing. { set -euo pipefail umask 077 NEXGEN_ORIGIN='https://installer.nexgenstreamer.com/platform' NEXGEN_RELEASE_KEY='release-2026a:l1Cv0YhmsPy2WPoWoN2d3jIHzLiRjxlxj9DyXhtMiu8=' NEXGEN_SBIN=/usr/local/sbin NEXGEN_STATE=/var/lib/nexgen-installer NEXGEN_RUN=/run/nexgen-installer NEXGEN_CONFIG=/etc/nexgen-installer/config.json NEXGEN_ACTIVATED=/opt/nexgen/.secrets-local/installer/licensing/installation.id NEXGEN_STAGED=/opt/nexgen/.secrets-local/installer/staged-installer NEXGEN_META_MAX=65536 NEXGEN_BINARY_MAX=268435456 nexgen_fail() { printf 'nexgen-installer: %s\n' "$*" >&2; exit 1; } nexgen_sha256() { if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1" | cut -d' ' -f1; else shasum -a 256 "$1" | cut -d' ' -f1; fi } nexgen_usage_no_key() { cat >&2 <<'EOF' nexgen-installer: no terminal is available to enter the activation key. Write the key to a root-owned file readable only by root, then rerun: install -m 0600 /dev/null /root/nexgen-activation.key # then put the key in this file curl --proto '=https' --tlsv1.2 -fsSL https://installer.nexgenstreamer.com/platform/installer.sh | bash -s -- --activation-key-file /root/nexgen-activation.key EOF exit 1 } # nexgen_owner_mode prints "UID MODE" without following a symlink. nexgen_owner_mode() { stat -c '%u %a' -- "$1" 2>/dev/null || stat -f '%u %Lp' -- "$1" } # nexgen_protected_key_file accepts only an absolute root-owned 0600 regular file # whose every ancestor is a real directory not writable by others. nexgen_protected_key_file() { local f=$1 d case "$f" in /*) ;; *) return 1;; esac case "$f/" in */../*|*/./*|*//*) return 1;; esac [ -f "$f" ] && [ ! -L "$f" ] || return 1 [ "$(nexgen_owner_mode "$f")" = '0 600' ] || return 1 d=$(dirname -- "$f") while :; do [ -d "$d" ] && [ ! -L "$d" ] || return 1 case "$(nexgen_owner_mode "$d")" in 0\ *[2367]) return 1;; 0\ *) ;; *) return 1;; esac [ "$d" = / ] && break d=$(dirname -- "$d") done } # nexgen_read_tty_key reads the key from the controlling terminal with echo off. nexgen_read_tty_key() { local out=$1 key='' saved saved=$(stty -g /dev/null || true' INT TERM HUP stty -echo >/dev/tty IFS= read -r key >/dev/tty printf '%s\n' "$key" >"$out" key='' } # nexgen_check_metadata validates the bounded bootstrap response strictly. nexgen_check_metadata() { local meta=$1 arch=$2 [ "$(wc -c <"$meta" | tr -d ' ')" -le "$NEXGEN_META_MAX" ] || return 1 jq -e --arg arch "$arch" --argjson max "$NEXGEN_BINARY_MAX" ' type == "object" and (keys == ["arch","distribution_id","download_token","installer","token_expires_at","version"]) and .arch == $arch and (.distribution_id | type == "string" and test("^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$")) and (.version | type == "string" and test("^v[0-9]+\\.[0-9]+\\.[0-9]+$")) and (.download_token | type == "string" and test("^[A-Za-z0-9_-]{16,128}$")) and (.token_expires_at | type == "string") and (.installer | type == "object" and (keys == ["asset_id","name","sha256","size"])) and (.installer.asset_id | type == "number" and . == floor and . > 0 and . < 9007199254740992) and .installer.name == ("nexgen-installer-linux-" + $arch) and (.installer.size | type == "number" and . == floor and . > 0 and . <= $max) and (.installer.sha256 | type == "string" and test("^[0-9a-f]{64}$")) ' "$meta" >/dev/null } nexgen_main() { case "$NEXGEN_ORIGIN$NEXGEN_RELEASE_KEY" in *__*) nexgen_fail 'this is an unpublished template; download the published installer.sh';; esac local keyfile='' while (($#)); do case "$1" in --activation-key-file) [ $# -ge 2 ] || nexgen_fail '--activation-key-file needs a path'; keyfile=$2; shift 2;; *) nexgen_fail "unknown argument: $1";; esac done [ "$(id -u)" = 0 ] || nexgen_fail 'run as root (for example: sudo -i, then rerun the command)' [ "$(uname -s)" = Linux ] || nexgen_fail 'Linux is required' local arch case "$(uname -m)" in x86_64|amd64) arch=amd64;; aarch64|arm64) arch=arm64;; *) nexgen_fail "unsupported architecture $(uname -m); amd64 or arm64 is required";; esac local target="$NEXGEN_SBIN/nexgen-installer" # The activated installer keeps its exact verified bytes in the root-only # secrets tree; a lost executable is restored from there (digest checked), # never by a new key, trial or identity. if [ ! -x "$target" ] && [ -f "$NEXGEN_STAGED/nexgen-installer" ] && [ ! -L "$NEXGEN_STAGED/nexgen-installer" ] && [ ! -L "$NEXGEN_STAGED" ]; then [ "$(nexgen_sha256 "$NEXGEN_STAGED/nexgen-installer")" = "$(tr -d '\n' <"$NEXGEN_STAGED/nexgen-installer.sha256" 2>/dev/null)" ] || nexgen_fail "the staged installer in $NEXGEN_STAGED failed SHA-256 verification; refusing to restore it" install -d -o root -g root -m 0755 "$NEXGEN_SBIN" install -o root -g root -m 0755 "$NEXGEN_STAGED/nexgen-installer" "$target.new" mv -f "$target.new" "$target" printf 'Restored %s from the verified staged copy.\n' "$target" >&2 fi # An existing installation (journal or bootstrap configuration) is never # replaced or re-bootstrapped from here: report its status (URLs, recovery # commands) with the managed binary. A host that activated its licence but # stopped before configuration (for example a failed download) resumes with # the installed binary and its stored binding: the spent one-use key is not # needed again. Anything else (e.g. only a lock file after a refused key) # starts afresh; nothing was installed yet. if [ -e "$NEXGEN_STATE/journal.json" ] || [ -e "$NEXGEN_CONFIG" ]; then [ -x "$target" ] || nexgen_fail "installer state exists in $NEXGEN_STATE but $target is missing; refusing to replace it" printf 'An installation is already managed on this host; showing its status.\n' >&2 exec "$target" status /dev/null || true) [[ "$installed" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]] || nexgen_fail "$target does not report a release version; refusing to resume" printf 'Resuming the activated installation with %s.\n' "$installed" >&2 exec "$target" bootstrap --release-origin "$NEXGEN_ORIGIN" --release-version "$installed" --release-key "$NEXGEN_RELEASE_KEY" --activation-key-file "${keyfile:-$NEXGEN_RUN/activation-key}" /dev/null 2>&1 || nexgen_fail 'curl is required: apt-get install -y ca-certificates curl jq' command -v jq >/dev/null 2>&1 || nexgen_fail 'jq is required: apt-get install -y ca-certificates curl jq' local work work=$(mktemp -d) trap 'rm -rf "$work"' EXIT if [ -n "$keyfile" ]; then nexgen_protected_key_file "$keyfile" || nexgen_fail 'the activation key file must be an absolute root-owned 0600 regular file in root-owned directories, without symlinks' cat -- "$keyfile" >"$work/key" elif { : /dev/null; then nexgen_read_tty_key "$work/key" else nexgen_usage_no_key fi # One line of 43 base64url characters (32 random bytes); never echoed. [ "$(wc -l <"$work/key" | tr -d ' ')" -le 1 ] && grep -Exq '[A-Za-z0-9_-]{43}' "$work/key" || nexgen_fail 'the activation key is not valid; check it and try again' jq -n --rawfile key "$work/key" --arg arch "$arch" '{activation_key: ($key | rtrimstr("\n")), arch: $arch}' >"$work/request.json" local licensing="$NEXGEN_ORIGIN/licensing/v1" status code status=$(curl --proto '=https' --tlsv1.2 -sS --max-redirs 0 --max-time 30 --max-filesize "$NEXGEN_META_MAX" \ -H 'Content-Type: application/json' --data-binary "@$work/request.json" -o "$work/meta.json" -w '%{http_code}' \ "$licensing/bootstrap" "$work/meta.json" [ "$(wc -c <"$work/meta.json" | tr -d ' ')" -le "$NEXGEN_META_MAX" ] || nexgen_fail 'the licensing service returned an oversized response' code=$(jq -r 'if type == "object" and (.code | type == "string") and (.code | test("^[A-Z_]{1,64}$")) then .code else "" end' "$work/meta.json" 2>/dev/null || true) case "$status" in 200) ;; 503) if [ "$code" = RELEASE_UNAVAILABLE ]; then nexgen_fail 'your approved release is not yet available; nothing was installed and your key is unchanged, retry later' fi nexgen_fail 'the licensing service is temporarily unavailable; nothing was installed, retry later';; 429|5??) nexgen_fail 'the licensing service is temporarily unavailable; nothing was installed, retry later';; 202) nexgen_fail 'unexpected licensing response; nothing was installed';; *) nexgen_fail "the licensing service refused this activation key${code:+ ($code)}; nothing was installed";; esac nexgen_check_metadata "$work/meta.json" "$arch" || nexgen_fail 'the licensing service returned invalid installer metadata; nothing was installed' local version asset size want version=$(jq -r .version "$work/meta.json") asset=$(jq -r .installer.asset_id "$work/meta.json") size=$(jq -r .installer.size "$work/meta.json") want=$(jq -r .installer.sha256 "$work/meta.json") # The short-lived bearer goes only into a private curl config file. jq -r '"header = \"Authorization: Bearer " + .download_token + "\""' "$work/meta.json" >"$work/auth.conf" curl --proto '=https' --tlsv1.2 -fsS --max-redirs 0 --max-time 600 --max-filesize "$size" -K "$work/auth.conf" \ -o "$work/nexgen-installer" "$licensing/delivery/assets/$asset"