#!/bin/sh # Install lvcs on Linux or macOS from a release on releases.lvcs.sh # (docs/production-readiness.md, R3; decisions 252 and 421): # # curl -fsSL https://sh.lvcs.sh | sh # # It downloads lvcs--.tar.gz, SHA256SUMS, and the release's # Ed25519-signed manifest.json (decisions 293, 315, and 380), and installs # only when the archive passes a check independent of the download. # SHA256SUMS comes from the same place as the archive, so it only catches # corruption. The independent check is, in order: the manifest's SHA-256 # pinned in this script for the version (release_pins below, written when # the release is published; this script is served from sh.lvcs.sh, not from # the release), then the archive's hash that manifest lists; else the # manifest's signature by a release key it trusts (RELEASE_KEYS below), # checked by the lvcs already installed, else by OpenSSL when it verifies # Ed25519. With none of these it refuses and says how to go on; it never # asks the downloaded lvcs to vouch for itself. When cosign is on PATH and # the release has a Sigstore bundle (one published by release.yml), # SHA256SUMS's bundle is verified too; a release published by hand has # none, which it says in one line. It installs without sudo. # # Environment: # LVCS_VERSION the release to install, as 0.1.0 or v0.1.0 (default: # the newest release pinned below, else the latest) # LVCS_INSTALL_DIR where lvcs goes (default: ~/.local/bin) # LVCS_BASE_URL a URL serving one release's files in place of the # releases host (a # mirror, or an install test); needs a version # (LVCS_VERSION or a pin) # LVCS_NO_SERVICE when 1, skip the last step, `lvcs install`, which runs # the per-user daemon as a service (decision 272) # # Everything runs from main, called on the last line, so a truncated download # runs nothing. set -eu # The releases host (lvcs_vocab::ids::RELEASES_URL, decision 421): each # release's archives, SHA256SUMS, and signed manifest under v/, and # the newest release's manifest under latest/. host=https://releases.lvcs.sh # lvcs_vocab::ids::RELEASE_KEYS, the release keys every lvcs trusts # (ID_ENCODING); keep the two equal. release_keys="qm2z5dqrvdy4ojzhib2yzb4fpyfqvnn2e5uynzs4gr46gj65yubq" # lvcs_vocab::ids::UPDATE_MANIFEST_DOMAIN, which a manifest's signature # covers before its bytes. manifest_domain=lvcs-release-manifest-v1 # One " " per published release, # newest last, written by scripts/release-pin.sh when the release is # published (decision 380). # release-pins:begin release_pins=' 0.1.0 cbe4b24ef91c21cbe4b8b09ef4ae6b16cf6897895fdabf6adef0256ed27c315a 0.1.2 3905c8c6c09d38625bf376a98a732c30ded8c1e8c717a4609e080e06b5ba5706 0.1.3 d100472a0cb795f7f1e880f0c3473e7494da487a102b8d5210e7aea5941b267c 0.2.0 5891d7c5c4f3123dc96b7eef437901f6b33c05feeee2850cf391b93b2fafd94f 0.2.1 4742f31001927c2aa660c6d4265078e505fae481c0a3273ff6b004adef552b0b 0.2.2 26931d5f1c9232d1f655bb2483138a09d514e1c807f6818ea903f56ecd8fc104 0.2.3 70bbd01f0a6a63ee35e00cc1d63fd356f4d2a81460d56918b321df5a6eede242 0.2.4 1b0fbd11bc6e767666a727da12c3a58df7b67cc7c8022960b441ad174172585c 0.2.5 a520befa408630ef422e48ffe8ca6eda98accf45290a571780c2e960851bca0f 0.2.6 e376d55e9ea9872f65e94957737e8c8b2cabc79a7ec1f274c57da40ec5413a81 0.2.7 cdf60029187d40b0385b4ba4044b108e7a1e56810b7a63469f711120db1825e6 0.2.8 3ec55e0a76802c9cd0f5dacb70ab8812e0492f0d86dd2e17e29bac72e41b9757 0.2.9 503e3e096bd6b12ef153813878eed43e7a75be056ba741f8d65f8f5d8e54ebb8 0.2.10 ff761b5d7e577a80b0a9a1f0a6a2152b3e25b93a4b05fdf5814519e0941d7358 0.2.11 941b8d3c03f0a16dfa9bff28c40b3ca4fac787131e2f350fb494b6d32c1099bc 0.2.12 772036cfd20085b07e6952fc11154ba913b64b465b0ffdf08c7b2a0e633e289c 0.2.13 d430a8b858de1041d1f5f4e1e1a6856247eb3a11a496c18a456dcc9f71dbeb60 0.2.14 3d4af9c217128166aa8c46323a5282a6f80434fe553448e6a94de8beaf4e0b00 0.3.0 9e35d80a057db617c0aca6788735562e2a04a2cf7db26d8c0c030f238eb9d70a 0.3.1 bb075cde64dbbb1cbbce14def5276dbadc7f71ce3381bf56f05ccc968ed5f30e ' # release-pins:end # A Sigstore bundle is signed by the source repository's release workflow. signer="^https://github\\.com/gabewillen/lvcs/\\.github/workflows/release\\.yml@refs/tags/v" issuer=https://token.actions.githubusercontent.com say() { printf 'agentflare-install: %s\n' "$*" >&2; } die() { say "$*"; exit 1; } fetch() { if [ -n "${LVCS_BASE_URL:-}" ]; then curl -fsSL -o "$2" "$1" else curl -fsSL --proto '=https' --tlsv1.2 -o "$2" "$1" fi } detect_target() { arch=$(uname -m) case $arch in x86_64 | amd64) arch=x86_64 ;; aarch64 | arm64) arch=aarch64 ;; *) die "no release for architecture $arch: releases cover x86_64 and aarch64" ;; esac case $(uname -s) in Linux) echo "$arch-unknown-linux-musl" ;; Darwin) # Under Rosetta uname reports x86_64; install the native build. if [ "$arch" = x86_64 ] && [ "$(sysctl -n sysctl.proc_translated 2>/dev/null || true)" = 1 ]; then arch=aarch64 fi echo "$arch-apple-darwin" ;; MINGW* | MSYS* | CYGWIN*) die "on Windows, run in PowerShell: irm https://ps.lvcs.sh | iex" ;; *) die "no release for $(uname -s): releases cover Linux, macOS, and Windows" ;; esac } latest_version() { # Only the version is read here; the release itself is checked below. latest=$(curl -fsSL --proto '=https' --tlsv1.2 "$host/latest/manifest.json") \ || die "could not reach $host/latest/manifest.json; set LVCS_VERSION to pick a release" version=$(printf '%s\n' "$latest" | sed -n 's/^ *"version": *"\([0-9A-Za-z.+-]*\)".*/\1/p' | head -n 1) [ -n "$version" ] || die "found no version in $host/latest/manifest.json; set LVCS_VERSION to pick one" echo "$version" } sha256() { if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1" | cut -d ' ' -f 1 elif command -v shasum >/dev/null 2>&1; then shasum -a 256 "$1" | cut -d ' ' -f 1 else die "found neither sha256sum nor shasum to check the download with" fi } # b32hex : ID_ENCODING (lowercase base32, no padding) as hex. b32hex() { printf '%s' "$1" | awk '{ a = "abcdefghijklmnopqrstuvwxyz234567"; h = "0123456789abcdef" bits = 0; n = 0; out = "" for (i = 1; i <= length($0); i++) { v = index(a, substr($0, i, 1)) - 1 if (v < 0) exit 1 bits = bits * 32 + v; n += 5 if (n >= 8) { n -= 8; b = int(bits / 2 ^ n); bits -= b * 2 ^ n out = out substr(h, int(b / 16) + 1, 1) substr(h, b % 16 + 1, 1) } } print out }' } # hex2bin : the bytes, on stdout. hex2bin() { fmt=$(printf '%s' "$1" | awk '{ h = "0123456789abcdef" for (i = 1; i <= length($0); i += 2) printf "\\%03o", (index(h, substr($0, i, 1)) - 1) * 16 + index(h, substr($0, i + 1, 1)) - 1 }') # shellcheck disable=SC2059 # the format is octal escapes only printf "$fmt" } # openssl_listed : the manifest's SHA-256 for the # archive, when OpenSSL verifies its Ed25519 signature by a trusted key; # status 2 when this OpenSSL can't verify Ed25519, 1 when it fails. openssl_listed() { command -v openssl >/dev/null 2>&1 || return 2 openssl_ed25519 || return 2 key=$(cut -d ' ' -f 1 "$2"); sig=$(cut -d ' ' -f 2 "$2" | tr -d '\n') case " $release_keys " in *" $key "*) ;; *) return 1 ;; esac keyhex=$(b32hex "$key") && sighex=$(b32hex "$sig") || return 1 [ ${#keyhex} -eq 64 ] && [ ${#sighex} -eq 128 ] || return 1 hex2bin "302a300506032b6570032100$keyhex" >"$tmp/key.der" { printf '%s\000' "$manifest_domain"; cat "$1"; } >"$tmp/signed" hex2bin "$sighex" >"$tmp/sig.bin" openssl pkey -pubin -inform DER -in "$tmp/key.der" -out "$tmp/key.pem" 2>/dev/null || return 1 openssl pkeyutl -verify -pubin -inkey "$tmp/key.pem" -rawin -in "$tmp/signed" -sigfile "$tmp/sig.bin" \ >/dev/null 2>&1 || return 1 manifest_hash "$1" "$3" } # openssl_ed25519: whether this OpenSSL verifies Ed25519 at all, probed with # RFC 8032's second test vector (the one-byte message 0x72; OpenSSL 3.6 # refuses an empty one), so an OpenSSL that can't is told apart from a # signature that doesn't verify. openssl_ed25519() { hex2bin "302a300506032b65700321003d4017c3e843895a92b70aa74d1b7ebc9c982ccf2ec4968cc0cd55f12af4660c" >"$tmp/probe.der" hex2bin "92a009a9f0d4cab8720e820b5f642540a2b27b5416503f8fb3762223ebdb69da085ac1e43e15996e458f3613d0f11d8c387b2eaeb4302aeeb00d291612bb0c00" >"$tmp/probe.sig" printf 'r' >"$tmp/probe.msg" openssl pkey -pubin -inform DER -in "$tmp/probe.der" -out "$tmp/probe.pem" 2>/dev/null \ && openssl pkeyutl -verify -pubin -inkey "$tmp/probe.pem" -rawin -in "$tmp/probe.msg" \ -sigfile "$tmp/probe.sig" >/dev/null 2>&1 } # manifest_file : the archive filename the manifest lists # for the target (agentflare--.tar.gz for 0.3.1+, or # lvcs--.tar.gz for pinned <= 0.3.0 releases). manifest_file() { awk -v t="$2" ' index($0, "\"" t "\"") { found = 1 } found && /"file"/ { gsub(/.*"file": *"|".*/, ""); print; exit } ' "$1" } # manifest_hash : the SHA-256 the manifest lists for the # archive (release-sign's pretty JSON: each target's file, then its sha256). manifest_hash() { awk -v f="$2" ' index($0, "\"file\": \"" f "\"") { found = 1 } found && /"sha256"/ { gsub(/.*"sha256": *"|".*/, ""); print; exit } ' "$1" } # pinned : the manifest SHA-256 this script pins for it, if any. pinned() { printf '%s\n' "$release_pins" | awk -v v="$1" 'NF == 2 && $1 == v { print $2; exit }' } # newest_pinned: the newest pinned version (the last pin), if any. newest_pinned() { printf '%s\n' "$release_pins" | awk 'NF == 2 { v = $1 } END { if (v != "") print v }' } # manifest_listed : the SHA-256 the release lists # for the archive, checked independently of the download (see the header), # saying how; refuses when nothing here can check it. manifest_listed() { pin=$(pinned "$version") if [ -n "$pin" ]; then have=$(sha256 "$1") [ "$have" = "$pin" ] \ || die "refusing to install: manifest.json has SHA-256 $have, but this installer pins $pin for agentflare $version, so the release is not the one published" say "verified the release manifest against the hash this installer pins for agentflare $version" manifest_hash "$1" "$3" return 0 fi if [ -x "$dir/agentflare" ] && listed=$("$dir/agentflare" release-verify "$1" "$2" "$3" 2>/dev/null); then say "verified the release manifest's signature with the installed agentflare" printf '%s\n' "${listed%% *}" return 0 fi if [ -x "$dir/lvcs" ] && listed=$("$dir/lvcs" release-verify "$1" "$2" "$3" 2>/dev/null); then say "verified the release manifest's signature with the installed lvcs" printf '%s\n' "${listed%% *}" return 0 fi status=0 listed=$(openssl_listed "$1" "$2" "$3") || status=$? case $status in 0) say "verified the release manifest's signature with OpenSSL" printf '%s\n' "$listed" return 0 ;; 1) die "refusing to install: the release manifest's signature did not verify" ;; esac pins=$(printf '%s\n' "$release_pins" | awk 'NF == 2 { printf "%s%s", sep, $1; sep = ", " }') say "refusing to install: nothing here can check agentflare $version independently of the download:" say " this installer pins no manifest hash for $version (it pins ${pins:-none}), there is no agentflare" say " or lvcs in $dir, and no OpenSSL here verifies Ed25519 signatures." say "next: install a pinned version (unset LVCS_VERSION), then run agentflare update, which checks the" say " signature itself; or verify manifest.json.sig on a machine with OpenSSL 3 and install by hand." exit 1 } main() { command -v curl >/dev/null 2>&1 || die "curl is required" command -v tar >/dev/null 2>&1 || die "tar is required" target=$(detect_target) version=${LVCS_VERSION:-$(newest_pinned)} version=${version#v} if [ -n "${LVCS_BASE_URL:-}" ]; then [ -n "$version" ] || die "LVCS_BASE_URL needs LVCS_VERSION (this installer pins no release)" base=${LVCS_BASE_URL%/} else [ -n "$version" ] || version=$(latest_version) base="$host/v$version" fi dir=${LVCS_INSTALL_DIR:-${HOME:?HOME is unset; set LVCS_INSTALL_DIR}/.local/bin} tmp=$(mktemp -d 2>/dev/null || mktemp -d -t agentflare-install) trap 'rm -rf "$tmp"' EXIT trap 'exit 1' HUP INT TERM say "downloading agentflare $version for $target" fetch "$base/manifest.json" "$tmp/manifest.json" || die "could not download $base/manifest.json" fetch "$base/manifest.json.sig" "$tmp/manifest.json.sig" \ || die "could not download $base/manifest.json.sig" archive=$(manifest_file "$tmp/manifest.json" "$target") case $archive in '' | */* | *..*) die "manifest.json lists no valid archive for $target" ;; esac # Nothing from the download is unpacked or run before this check passes. listed=$(manifest_listed "$tmp/manifest.json" "$tmp/manifest.json.sig" "$archive") fetch "$base/$archive" "$tmp/$archive" || die "could not download $base/$archive" fetch "$base/SHA256SUMS" "$tmp/SHA256SUMS" || die "could not download $base/SHA256SUMS" if ! command -v cosign >/dev/null 2>&1; then say "cosign is not on PATH, so no Sigstore signature is checked (the release manifest still is)" elif fetch "$base/SHA256SUMS.sigstore.json" "$tmp/SHA256SUMS.sigstore.json" 2>/dev/null; then cosign verify-blob --bundle "$tmp/SHA256SUMS.sigstore.json" \ --certificate-identity-regexp "$signer" --certificate-oidc-issuer "$issuer" \ "$tmp/SHA256SUMS" >/dev/null \ || die "refusing to install: the Sigstore signature on SHA256SUMS did not verify" say "verified the Sigstore signature on SHA256SUMS" else say "this release has no Sigstore bundle (it was published outside GitHub Actions), so cosign is skipped; the signed manifest is checked" fi want=$(awk -v f="$archive" '$2 == f || $2 == "*" f { print $1; exit }' "$tmp/SHA256SUMS") [ -n "$want" ] || die "SHA256SUMS lists no $archive" got=$(sha256 "$tmp/$archive") [ "$got" = "$want" ] || die "refusing to install: $archive has SHA-256 $got, but SHA256SUMS says $want" [ "$listed" = "$got" ] || die "refusing to install: $archive has SHA-256 $got, but the release manifest says ${listed:-nothing}" mkdir -p "$tmp/x" tar -xzf "$tmp/$archive" -C "$tmp/x" # Install agentflare on 0.3.1+ (ignoring any lvcs bridge hardlink in the # 0.3.1 tarball); fall back to lvcs only for pinned <= 0.3.0 releases. if [ -f "$tmp/x/agentflare" ]; then bin_name=agentflare marker=agentflare.install elif [ -f "$tmp/x/lvcs" ]; then bin_name=lvcs marker=lvcs.install else die "$archive holds neither agentflare nor lvcs" fi chmod 0755 "$tmp/x/$bin_name" mkdir -p "$dir" || die "could not create $dir; set LVCS_INSTALL_DIR to a writable directory" # Copy beside the target, then rename over it, so a running binary keeps # its old image and a failed copy leaves the old one in place. cp "$tmp/x/$bin_name" "$dir/.$bin_name.install.$$" chmod 0755 "$dir/.$bin_name.install.$$" mv -f "$dir/.$bin_name.install.$$" "$dir/$bin_name" # The Claude Code plugin's hook helper ships in the same signed archive # (decision 513); its hooks find it here. if [ -f "$tmp/x/lvcs-claude-code" ]; then cp "$tmp/x/lvcs-claude-code" "$dir/.lvcs-claude-code.install.$$" chmod 0755 "$dir/.lvcs-claude-code.install.$$" mv -f "$dir/.lvcs-claude-code.install.$$" "$dir/lvcs-claude-code" fi # The plugins the release ships (decision 514), each a directory under # plugins/ beside the binary, where `init` finds the bundled ones and # `plugin install ` any of them. The new set is copied beside # the old and swapped in, so a project's installed plugin keeps its path. if [ -d "$tmp/x/plugins" ]; then rm -rf "$dir/.plugins.install.$$" "$dir/.plugins.old.$$" cp -R "$tmp/x/plugins" "$dir/.plugins.install.$$" [ ! -e "$dir/plugins" ] || mv "$dir/plugins" "$dir/.plugins.old.$$" mv "$dir/.plugins.install.$$" "$dir/plugins" rm -rf "$dir/.plugins.old.$$" fi # The marker update needs before it replaces a binary: this one is # the install script's (decision 293). : > "$dir/$marker" say "installed $("$dir/$bin_name" --version) to $dir/$bin_name" case ":${PATH:-}:" in *":$dir:"*) ;; *) say "$dir is not on PATH; add it, for example: export PATH=\"$dir:\$PATH\"" ;; esac # The per-user daemon, as a service of this user's (decision 272). # Linux without a systemd user manager (a container, WSL without # systemd) has no service manager to run it: say how to run the daemon # instead, and the install still succeeds. if [ "${LVCS_NO_SERVICE:-}" = 1 ]; then say "LVCS_NO_SERVICE=1: not installing the daemon's service; run $bin_name install, or $bin_name daemon in the foreground" elif [ "$(uname -s)" = Linux ] && ! systemctl --user show-environment >/dev/null 2>&1; then say "no systemd user manager here (a container, or WSL without systemd), so no service; run the daemon yourself:" say " nohup $dir/$bin_name daemon > ~/$bin_name-daemon.log 2>&1 &" else "$dir/$bin_name" install || die "$bin_name install failed; run it again, or set LVCS_NO_SERVICE=1 and run $bin_name daemon yourself" fi } main "$@"