#!/bin/sh # SPDX-License-Identifier: Apache-2.0 # # Installs the Zentri CLI, zentri, and its short name zt, on Linux and macOS. # # curl -fsSL https://get.zentri.dev | sh # # It downloads a release from GitHub Releases, checks the SSH signature on # checksums.txt against the release key below, checks the archive against # checksums.txt, and only then installs. Any failed check stops it before it # writes to the install directory. ADR-0014 explains the choices. # # Environment: # ZENTRI_VERSION release tag to install, like v0.1.0 (default: the latest release) # ZENTRI_INSTALL_DIR where zentri and zt go (default: $HOME/.local/bin) # ZENTRI_DOWNLOAD_URL releases base URL (default: https://github.com/zentri-repo/zentri/releases) # # Needs curl or wget, tar, ssh-keygen from OpenSSH 8.1 or later, and one of # sha256sum, shasum or openssl. # The public half of the release signing key. The owner replaces this line # after the key ceremony in docs/plan/DEPLOY.md; until then every install # stops at the signature check. install.ps1 holds the same line. release_key='ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPqjvo4q3YUSqupbJgV8P/fLsppak6Nab7ToUE6L+6D7 release@zentri.dev' set -eu signer=release@zentri.dev namespace=zentri-release default_url=https://github.com/zentri-repo/zentri/releases tmp= say() { printf 'zentri-install: %s\n' "$*"; } die() { printf 'zentri-install: error: %s\n' "$*" >&2 exit 1 } have() { command -v "$1" >/dev/null 2>&1; } cleanup() { if [ -n "$tmp" ]; then rm -rf "$tmp"; fi } detect_os() { case $(uname -s) in Linux) echo linux ;; Darwin) echo darwin ;; MINGW* | MSYS* | CYGWIN* | Windows_NT) die "on Windows, run in PowerShell: irm https://get.zentri.dev/install.ps1 | iex" ;; *) die "no Zentri build for $(uname -s); build from source: https://github.com/zentri-repo/zentri" ;; esac } detect_arch() { case $(uname -m) in x86_64 | amd64) # A shell under Rosetta 2 reports x86_64 on Apple silicon. Install # the native build. if [ "$1" = darwin ] && [ "$(sysctl -n sysctl.proc_translated 2>/dev/null || true)" = 1 ]; then echo arm64 else echo amd64 fi ;; arm64 | aarch64) echo arm64 ;; *) die "no Zentri build for $(uname -m); build from source: https://github.com/zentri-repo/zentri" ;; esac } # check_url refuses anything but HTTPS, except a server on this machine, # which the installer tests use. check_url() { case $1 in https://*) ;; http://127.0.0.1:* | http://localhost:*) ;; *) die "ZENTRI_DOWNLOAD_URL must start with https://, got $1" ;; esac } fetch() { if have curl; then case $1 in https://*) curl --fail --silent --show-error --location --proto '=https' --tlsv1.2 --retry 3 --output "$2" "$1" ;; *) curl --fail --silent --show-error --location --retry 3 --output "$2" "$1" ;; esac elif have wget; then wget --quiet --output-document "$2" "$1" else die "needs curl or wget" fi || die "download failed: $1" } sha256() { if have sha256sum; then sha256sum "$1" | cut -d ' ' -f 1 elif have shasum; then shasum -a 256 "$1" | cut -d ' ' -f 1 elif have openssl; then openssl dgst -sha256 -r "$1" | cut -d ' ' -f 1 else die "needs sha256sum, shasum or openssl to check the download" fi } verify_signature() { case $release_key in *REPLACE_WITH_THE_RELEASE_PUBLIC_KEY*) die "this installer has no release signing key yet, so it cannot check a download; Zentri has not published a release" ;; esac have ssh-keygen || die "needs ssh-keygen (OpenSSH 8.1 or later) to check the release signature; install openssh-client (Debian, Ubuntu) or openssh (Fedora, Alpine)" # allowed_signers wants "type key", without the comment. key_type=${release_key%% *} key_data=${release_key#* } key_data=${key_data%% *} printf '%s namespaces="%s" %s %s\n' "$signer" "$namespace" "$key_type" "$key_data" >"$tmp/allowed_signers" if ! out=$(ssh-keygen -Y verify -f "$tmp/allowed_signers" -I "$signer" -n "$namespace" \ -s "$tmp/checksums.txt.sig" <"$tmp/checksums.txt" 2>&1); then printf '%s\n' "$out" >&2 die "checksums.txt is not signed by the Zentri release key; nothing was installed (ssh-keygen -Y needs OpenSSH 8.1 or later)" fi } verify_checksum() { want=$(awk -v f="$1" '$2 == f || $2 == "*" f { print $1; exit }' "$tmp/checksums.txt") [ -n "$want" ] || die "checksums.txt has no entry for $1; nothing was installed" got=$(sha256 "$tmp/$1") [ "$got" = "$want" ] || die "SHA-256 of $1 is $got, checksums.txt says $want; nothing was installed" } install_files() { dir=$1 mkdir -p "$dir" || die "cannot create $dir; set ZENTRI_INSTALL_DIR to a directory you can write" [ -w "$dir" ] || die "cannot write to $dir; set ZENTRI_INSTALL_DIR to a directory you can write" # Copy next to the target, then rename, so a running zentri is never # half overwritten. cp "$tmp/x/zentri" "$dir/.zentri.$$" chmod 755 "$dir/.zentri.$$" mv -f "$dir/.zentri.$$" "$dir/zentri" ln -sf zentri "$dir/zt" } main() { trap cleanup EXIT trap 'exit 130' INT trap 'exit 143' TERM os=$(detect_os) arch=$(detect_arch "$os") base=${ZENTRI_DOWNLOAD_URL:-$default_url} base=${base%/} check_url "$base" version=${ZENTRI_VERSION:-} case $version in '') from=$base/latest/download ;; v[0-9]*) case $version in *[!A-Za-z0-9._-]*) die "ZENTRI_VERSION must look like v0.1.0, got $version" ;; esac from=$base/download/$version ;; *) die "ZENTRI_VERSION must look like v0.1.0, got $version" ;; esac dir=${ZENTRI_INSTALL_DIR:-${HOME:?HOME is not set}/.local/bin} have tar || die "needs tar" tmp=$(mktemp -d "${TMPDIR:-/tmp}/zentri-install.XXXXXX") archive=zentri_${os}_${arch}.tar.gz say "downloading ${version:-the latest release} for $os/$arch" fetch "$from/checksums.txt" "$tmp/checksums.txt" fetch "$from/checksums.txt.sig" "$tmp/checksums.txt.sig" verify_signature fetch "$from/$archive" "$tmp/$archive" verify_checksum "$archive" say "signature and SHA-256 checked" mkdir "$tmp/x" tar -xzf "$tmp/$archive" -C "$tmp/x" zentri || die "cannot unpack $archive" install_files "$dir" installed=$("$dir/zentri" --version) || die "installed $dir/zentri, but it does not run" say "installed $installed" say " $dir/zentri" say " $dir/zt -> zentri" case :${PATH:-}: in *:"$dir":*) ;; *) say "" say "$dir is not on your PATH. Add it, then open a new shell:" say " echo 'export PATH=\"$dir:\$PATH\"' >> ~/.profile" ;; esac say "" say "Next steps:" say " zt doctor check git, tmux and your coding agent" say " zt login --server --project --invite - join your team (ask your admin for an invite)" say " Docs: https://docs.zentri.dev" } main "$@"