#!/bin/sh # Download and verify a versioned aid release before installing without root. # Also offers aic, selected by default; AID_INSTALL_AIC=0 or answering n leaves it out. # Dependencies: curl, tar, mktemp, and sha256sum or shasum (cargo on unsupported targets). set -eu repo=agent-tools-org/ai-dispatch install_dir=${AID_INSTALL_DIR:-$HOME/.cargo/bin} # Never install aic by crate name: the crates.io package called aic is an unrelated project. aic_repo=https://github.com/agent-tools-org/aic aic_version=0.3.2 aic_command="cargo install --git $aic_repo --tag v$aic_version --locked" fail() { echo "aid install: $*" >&2 exit 1 } warn_aic() { echo "Warning: aic was not installed: $*" >&2 echo "aid works without it, but --audit and --rigor critical need it. To add it: $aic_command" >&2 } # Ask on the terminal because stdin is the script under curl | sh; no terminal keeps the default. choose_aic() { want_aic=1 case "${AID_INSTALL_AIC:-}" in 1) return ;; 0) want_aic=0; return ;; '') ;; *) fail 'AID_INSTALL_AIC must be 1 or 0' ;; esac (: /dev/null || return 0 { echo 'Optional component:' echo ' [x] aic cross-audit CLI used by aid run --audit and --rigor critical' printf 'Press Enter to keep it selected, or type n to untick: ' } >/dev/tty # read still stores a final unterminated answer when it reports EOF. answer= read -r answer /dev/tty ;; esac } sha256_of() { if command -v sha256sum >/dev/null 2>&1; then sum=$(sha256sum "$1") || return 1 elif command -v shasum >/dev/null 2>&1; then sum=$(shasum -a 256 "$1") || return 1 else return 1 fi echo "${sum%% *}" } # Download a release asset beside its checksum list. On failure, fetch_error holds the reason and # fetch_refused is 1 when the download arrived but must not be trusted. fetch_verified() { fetch_error= fetch_refused=0 curl --proto '=https' --tlsv1.2 -fsSL "$1/$2" -o "$scratch/$2" || { fetch_error="cannot download $2; check the release and platform" return 1 } curl --proto '=https' --tlsv1.2 -fsSL "$1/checksums-sha256.txt" -o "$scratch/checksums" || { fetch_error='cannot download release checksums' return 1 } fetch_refused=1 expected=$(awk -v name="$2" '$2 == name || $2 == "*" name { print $1 }' "$scratch/checksums") [ "${#expected}" = 64 ] || { fetch_error='missing or ambiguous release checksum'; return 1; } case "$expected" in *[!0-9a-fA-F]*) fetch_error='invalid release checksum'; return 1 ;; esac actual=$(sha256_of "$scratch/$2") || { fetch_error='sha256sum or shasum is required'; return 1; } [ "$actual" = "$expected" ] || { fetch_error='SHA-256 mismatch; refusing installation'; return 1; } fetch_refused=0 } # On failure, place_error holds the reason and the exit trap removes install_tmp. place_binary() { place_error= install_tmp=$(mktemp "$install_dir/.$2.XXXXXX") || { place_error='cannot create temporary binary'; return 1; } cp "$1" "$install_tmp" || { place_error="cannot write $install_dir/$2"; return 1; } chmod 755 "$install_tmp" || { place_error="cannot make $install_dir/$2 executable"; return 1; } if [ "$(uname -s)" = Darwin ] && command -v codesign >/dev/null 2>&1; then codesign --force --sign - "$install_tmp" || { place_error='macOS ad hoc signing failed'; return 1; } fi mv -f "$install_tmp" "$install_dir/$2" || { place_error="cannot replace $install_dir/$2"; return 1; } install_tmp= } install_aic() { aic_path="$install_dir/aic" if [ -e "$aic_path" ]; then if [ "$("$aic_path" --version 2>/dev/null)" = "aic $aic_version" ]; then echo "aic v$aic_version is already installed" return 0 fi # An unrelated tool can also be called aic; replacing it would destroy the user's install. if ! "$aic_path" --help 2>/dev/null | grep -Fxq 'Cross-audit aid agent outputs'; then warn_aic "$aic_path is a different program; it was left in place" return 0 fi fi aic_binary= if [ -n "$target_os" ] && [ -n "$target_arch" ]; then fetch_aic_release || return 0 fi if [ -z "$aic_binary" ]; then build_aic || return 0 fi if [ "$("$aic_binary" --version 2>/dev/null)" != "aic $aic_version" ]; then warn_aic "the binary did not report version $aic_version" return 0 fi if ! place_binary "$aic_binary" aic; then warn_aic "$place_error" return 0 fi echo "Installed aic v$aic_version to $aic_path" } # Sets aic_binary on success. Returns 1 only when a download must not be trusted: a tampered # release is a reason to stop, while a missing one still leaves the source build. fetch_aic_release() { aic_stem="aic-v$aic_version-$target_arch-$target_os" echo "Installing aic v$aic_version for $target_arch-$target_os..." if ! fetch_verified "$aic_repo/releases/download/v$aic_version" "$aic_stem.tar.gz"; then if [ "$fetch_refused" = 1 ]; then warn_aic "$fetch_error" return 1 fi echo "No aic release binary was available: $fetch_error" >&2 return 0 fi if ! tar -xzf "$scratch/$aic_stem.tar.gz" -C "$scratch" "$aic_stem/aic"; then warn_aic 'the release archive has no aic binary' return 1 fi if [ ! -f "$scratch/$aic_stem/aic" ] || [ -L "$scratch/$aic_stem/aic" ]; then warn_aic 'the release archive holds an invalid aic binary' return 1 fi aic_binary="$scratch/$aic_stem/aic" } build_aic() { if ! command -v cargo >/dev/null 2>&1; then warn_aic 'there is no release binary for it and Cargo is not available' return 1 fi echo "Building aic v$aic_version with Cargo..." if ! cargo install --git "$aic_repo" --tag "v$aic_version" --locked --root "$scratch/aic"; then warn_aic 'the Cargo build failed' return 1 fi aic_binary="$scratch/aic/bin/aic" } choose_aic command -v curl >/dev/null 2>&1 || fail 'curl is required' version=${AID_VERSION:-} if [ -z "$version" ]; then latest=$(curl --proto '=https' --tlsv1.2 -fsSL -o /dev/null -w '%{url_effective}' \ "https://github.com/$repo/releases/latest") || fail 'cannot resolve the latest release' case "$latest" in "https://github.com/$repo/releases/tag/v"*) version=${latest##*/v} ;; *) fail 'latest release redirect did not resolve to a version tag' ;; esac fi version=${version#v} case "$version" in ''|*[!0-9A-Za-z.+-]*) fail 'AID_VERSION must be a release version, such as 11.0.0' ;; esac case "$(uname -s)" in Linux) target_os=unknown-linux-gnu ;; Darwin) target_os=apple-darwin ;; *) target_os= ;; esac case "$(uname -m)" in x86_64|amd64) target_arch=x86_64 ;; aarch64|arm64) target_arch=aarch64 ;; *) target_arch= ;; esac scratch=$(mktemp -d) || fail 'cannot create temporary directory' install_tmp= trap 'rm -rf "$scratch"; if [ -n "$install_tmp" ]; then rm -f "$install_tmp"; fi' EXIT trap 'exit 1' HUP INT TERM if [ -z "$target_os" ] || [ -z "$target_arch" ]; then echo 'No release binary for this platform; installing with Cargo.' command -v cargo >/dev/null 2>&1 || fail 'unsupported target; install Rust and Cargo first' cargo install ai-dispatch --version "$version" --locked --root "$scratch/cargo" \ || fail 'Cargo installation failed' binary="$scratch/cargo/bin/aid" else command -v tar >/dev/null 2>&1 || fail 'tar is required' asset="aid-v$version-$target_arch-$target_os.tar.gz" base="https://github.com/$repo/releases/download/v$version" echo "Installing aid v$version for $target_arch-$target_os..." fetch_verified "$base" "$asset" || fail "$fetch_error" member="${asset%.tar.gz}/aid" tar -xzf "$scratch/$asset" -C "$scratch" "$member" || fail 'release archive has no aid binary' binary="$scratch/$member" fi [ -f "$binary" ] && [ ! -L "$binary" ] || fail 'invalid aid binary in archive' mkdir -p "$install_dir" || fail "cannot create $install_dir" place_binary "$binary" aid || fail "$place_error" echo "Installed aid v$version to $install_dir/aid" if [ "$want_aic" = 1 ]; then install_aic; fi case ":$PATH:" in *":$install_dir:"*) ;; *) echo "Warning: $install_dir is not on PATH. Add it to your shell configuration." >&2 ;; esac echo 'Next: aid setup'