#!/bin/sh # RDX installer: installs the `rdx` CLI. # curl -fsSL https://getrdx.io/install.sh | sh # curl -fsSL https://getrdx.io/install.sh | sh -s -- --version 0.1.0 # # Every step lives in a function and `main "$@"` is the last line, so a # truncated download defines functions at most and installs nothing. # # Environment: # RDX_DL_BASE download base URL (default https://dl.getrdx.io) # RDX_SKIP_CODESIGN set to 1 to skip the program checks (codesign and spctl # on a Mac, the program's own header on Linux). For test # fixtures only: never set it for a real install. # # On Linux the download is checked against the SHA-256 the release manifest # names, which comes over HTTPS from dl.getrdx.io, and the program must be a # 64-bit Linux program for this computer; there is no code signature to check. # Updates after that come from `rdx update`, which also checks the manifest's # signature. The default prefix is the account's own, so `rdx update` can # replace the program without root. DEFAULT_DL_BASE="https://dl.getrdx.io" setup_colors() { if [ -t 1 ]; then C_RED=$(printf '\033[31m') C_GREEN=$(printf '\033[32m') C_BOLD=$(printf '\033[1m') C_RESET=$(printf '\033[0m') else C_RED="" C_GREEN="" C_BOLD="" C_RESET="" fi } say() { printf '%s\n' "$*"; } ok() { printf '%s%s%s\n' "$C_GREEN" "$*" "$C_RESET"; } die() { printf '%serror:%s %s\n' "$C_RED" "$C_RESET" "$*" >&2 exit 1 } usage() { cat <<'USAGE' Install the RDX command-line tool. Usage: install.sh [options] Options: --version X install release X instead of the latest --channel C install from channel C (default: release) --prefix DIR install into DIR (default: $HOME/.local/bin) --uninstall remove DIR/rdx --dry-run show what would happen, change nothing --help show this help Environment: RDX_DL_BASE download base URL (default https://dl.getrdx.io) USAGE } have() { command -v "$1" >/dev/null 2>&1; } detect_platform() { os=$(uname -s 2>/dev/null || echo unknown) arch=$(uname -m 2>/dev/null || echo unknown) case "$os" in Darwin) case "$arch" in arm64 | aarch64) PLATFORM="macos-arm64" ;; x86_64) die "Intel Macs are not supported. RDX needs an Apple silicon Mac (arm64)." ;; *) die "Unsupported Mac architecture: $arch" ;; esac ;; Linux) case "$arch" in x86_64 | amd64) PLATFORM="linux-x64" ;; aarch64 | arm64) PLATFORM="linux-arm64" ;; *) die "Unsupported Linux architecture: $arch. RDX runs on x64 and arm64 (64-bit)." ;; esac ;; *) die "Unsupported operating system: $os. On Windows use: irm https://getrdx.io/install.ps1 | iex" ;; esac } # http_get URL OUTFILE: prints the HTTP status (404 for any wget server error). http_get() { if have curl; then curl -sS -L -o "$2" -w '%{http_code}' "$1" || true elif have wget; then if wget -q -O "$2" "$1" 2>/dev/null; then echo 200 else rc=$? if [ "$rc" -eq 8 ]; then echo 404; else echo 000; fi fi else die "curl or wget is required." fi } # json_string JSON KEY: first string value for KEY in the flattened JSON. json_string() { printf '%s' "$1" | grep -o "\"$2\"[[:space:]]*:[[:space:]]*\"[^\"]*\"" | head -1 | sed 's/^[^:]*:[[:space:]]*"\(.*\)"$/\1/' } json_number() { printf '%s' "$1" | grep -o "\"$2\"[[:space:]]*:[[:space:]]*[0-9][0-9]*" | head -1 | sed 's/^[^:]*:[[:space:]]*//' } sha256_of() { if have sha256sum; then sha256sum "$1" | awk '{print $1}' elif have shasum; then shasum -a 256 "$1" | awk '{print $1}' else die "sha256sum or shasum is required to verify the download." fi } manifest_url() { if [ -n "$VERSION" ]; then echo "$DL_BASE/v${VERSION#v}/manifest.json" elif [ "$CHANNEL" = "release" ]; then echo "$DL_BASE/latest.json" else echo "$DL_BASE/$CHANNEL/latest.json" fi } fetch_manifest() { url=$(manifest_url) MANIFEST_FILE="$WORK/manifest.json" status=$(http_get "$url" "$MANIFEST_FILE") case "$status" in 200) ;; 404) if [ -n "$VERSION" ]; then die "RDX version $VERSION was not found." fi say "No RDX release is published yet." exit 1 ;; *) die "Could not download $url (HTTP $status)." ;; esac FLAT=$(tr -d '\n\r' <"$MANIFEST_FILE") } parse_artifact() { REL_VERSION=$(json_string "$FLAT" version) obj=$(printf '%s' "$FLAT" | grep -o "\"$PLATFORM\"[[:space:]]*:[[:space:]]*{[^}]*}" | head -1) [ -n "$obj" ] || die "This release has no build for $PLATFORM." ART_URL=$(json_string "$obj" url) ART_SHA=$(json_string "$obj" sha256 | tr 'A-F' 'a-f') ART_SIZE=$(json_number "$obj" size) [ -n "$ART_URL" ] && [ -n "$ART_SHA" ] && [ -n "$ART_SIZE" ] || die "The release manifest is incomplete for $PLATFORM." case "$ART_SHA" in *[!0-9a-f]*) die "The release manifest has an invalid checksum." ;; esac [ "${#ART_SHA}" -eq 64 ] || die "The release manifest has an invalid checksum." case "$ART_URL" in http://* | https://*) ART_FULL_URL="$ART_URL" ;; *) ART_FULL_URL="$DL_BASE/$ART_URL" ;; esac } download_artifact() { ARCHIVE="$WORK/rdx-archive.tar.gz" status=$(http_get "$ART_FULL_URL" "$ARCHIVE") [ "$status" = "200" ] || die "Could not download $ART_FULL_URL (HTTP $status)." size=$(wc -c <"$ARCHIVE" | tr -d ' ') [ "$size" = "$ART_SIZE" ] || die "Download size mismatch (expected $ART_SIZE bytes, got $size). Nothing was installed." sum=$(sha256_of "$ARCHIVE") [ "$sum" = "$ART_SHA" ] || die "SHA-256 mismatch (expected $ART_SHA, got $sum). Nothing was installed." } # Reject absolute paths and any ".." component before extracting. check_archive_members() { members=$(tar -tzf "$ARCHIVE") || die "The download is not a valid archive." bad=$(printf '%s\n' "$members" | awk ' /^\// { print; next } { n = split($0, p, "/"); for (i = 1; i <= n; i++) if (p[i] == "..") { print; break } }') [ -z "$bad" ] || die "Unsafe path in archive: $bad" } extract_artifact() { check_archive_members EXTRACT="$WORK/extract" mkdir -p "$EXTRACT" tar -xzf "$ARCHIVE" -C "$EXTRACT" || die "Could not extract the archive." NEW_BIN="$EXTRACT/rdx" [ -f "$NEW_BIN" ] && [ ! -L "$NEW_BIN" ] || die "The archive does not contain an rdx binary." chmod 755 "$NEW_BIN" } # The first 20 bytes of a 64-bit little-endian Linux executable (or position-independent one) for this # architecture: e_machine 62 (x86_64) or 183 (aarch64). verify_linux_program() { case "$PLATFORM" in linux-x64) machine=3e00 ;; *) machine=b700 ;; esac header=$(head -c 20 "$NEW_BIN" | od -An -tx1 | tr -d ' \n') case "$header" in 7f454c46020101??????????????????0200"$machine" | 7f454c46020101??????????????????0300"$machine") ;; *) die "The download is not a Linux program for $PLATFORM. Nothing was installed." ;; esac } verify_signature() { if [ "${RDX_SKIP_CODESIGN:-}" = "1" ]; then say "Skipping the program checks (RDX_SKIP_CODESIGN=1)." return fi case "$PLATFORM" in linux-*) verify_linux_program return ;; esac have codesign && have spctl || die "codesign and spctl are required to verify the download." codesign --verify --strict "$NEW_BIN" || die "Code signature check failed. Nothing was installed." # `--type execute` assesses only apps and turns every bare command-line binary down; `open` # with the primary-signature context assesses the binary itself and accepts it as a # notarized Developer ID program (as scripts/release/rc_verify.py checks the release CLI). spctl --assess --type open --context context:primary-signature "$NEW_BIN" || die "Gatekeeper rejected the download. Nothing was installed." } install_binary() { mkdir -p "$PREFIX" || die "Could not create $PREFIX." tmp_name="$PREFIX/.rdx.install.$$" cp "$NEW_BIN" "$tmp_name" || die "Could not write to $PREFIX." chmod 755 "$tmp_name" mv -f "$tmp_name" "$PREFIX/rdx" || { rm -f "$tmp_name" die "Could not install $PREFIX/rdx." } } path_hint() { case ":$PATH:" in *":$PREFIX:"*) ;; *) say "" say "$PREFIX is not on your PATH. Add it with:" say " export PATH=\"$PREFIX:\$PATH\"" ;; esac } do_uninstall() { if [ -e "$PREFIX/rdx" ] || [ -L "$PREFIX/rdx" ]; then if [ "$DRY_RUN" = "1" ]; then say "[dry-run] would remove $PREFIX/rdx" return fi rm -f "$PREFIX/rdx" ok "Removed $PREFIX/rdx" else say "Nothing to remove: $PREFIX/rdx does not exist." fi } cleanup() { [ -n "${WORK:-}" ] && rm -rf "$WORK" } parse_args() { VERSION="" CHANNEL="release" PREFIX="$HOME/.local/bin" UNINSTALL=0 DRY_RUN=0 while [ $# -gt 0 ]; do case "$1" in --version) [ $# -ge 2 ] || die "--version needs a value." VERSION="$2" shift 2 ;; --channel) [ $# -ge 2 ] || die "--channel needs a value." CHANNEL="$2" shift 2 ;; --prefix) [ $# -ge 2 ] || die "--prefix needs a value." PREFIX="$2" shift 2 ;; --uninstall) UNINSTALL=1 shift ;; --dry-run) DRY_RUN=1 shift ;; -h | --help) usage exit 0 ;; *) die "Unknown option: $1 (try --help)" ;; esac done case "$CHANNEL" in *[!A-Za-z0-9._-]* | "") die "Invalid channel: $CHANNEL" ;; esac case "$VERSION" in *[!A-Za-z0-9._+-]*) die "Invalid version: $VERSION" ;; esac } install_flow() { detect_platform WORK=$(mktemp -d "${TMPDIR:-/tmp}/rdx-install.XXXXXX") || die "Could not create a temporary directory." fetch_manifest parse_artifact say "RDX ${REL_VERSION:-unknown} for $PLATFORM" if [ "$DRY_RUN" = "1" ]; then say "[dry-run] would download $ART_FULL_URL ($ART_SIZE bytes)" case "$PLATFORM" in linux-*) say "[dry-run] would verify SHA-256 $ART_SHA and that it is a Linux program" ;; *) say "[dry-run] would verify SHA-256 $ART_SHA and the code signature" ;; esac say "[dry-run] would install to $PREFIX/rdx" return fi download_artifact extract_artifact verify_signature install_binary ok "Installed $PREFIX/rdx" "$PREFIX/rdx" --version || die "The installed rdx did not run." path_hint say "" say "Next: ${C_BOLD}rdx host${C_RESET} on the machine to control, ${C_BOLD}rdx join ${C_RESET} on the viewing machine." } main() { setup_colors DL_BASE="${RDX_DL_BASE:-$DEFAULT_DL_BASE}" DL_BASE="${DL_BASE%/}" WORK="" parse_args "$@" trap cleanup EXIT trap 'exit 130' INT TERM if [ "$UNINSTALL" = "1" ]; then do_uninstall return fi install_flow } main "$@"