#!/bin/sh # up99 connected install, served at https://get.up99.ai. Downloads the server # bundle for this host with the install token up99 sent, checks it against # up99's release signature and its checksum, and runs its installer with the # same token, which applies the licence the token holds. # # curl -fsSL https://get.up99.ai | sudo sh -s -- --token FILE [--version X] [installer options...] # # Without --token it uses the token an earlier install kept # (/etc/up99/install-token), so an upgrade is: curl -fsSL https://get.up99.ai | sudo sh # Options it does not know go to install.sh (sudo ./install.sh --help lists # them). Needs curl, openssl, tar and sha256sum. # # Canonical copy: internal/download/get.sh, embedded into the download server. set -eu server=https://get.up99.ai releases_dir=/opt/up99/releases kept_token=/etc/up99/install-token say() { printf 'up99: %s\n' "$*"; } fail() { printf 'up99: %s\n' "$*" >&2 exit 1 } usage() { cat <<'EOF' usage: curl -fsSL https://get.up99.ai | sudo sh -s -- --token FILE [--version X] [installer options...] --token FILE the install token file up99 sent (default: /etc/up99/install-token, kept by an earlier install) --version X install this release instead of the newest your licence covers --server URL download from another https address (default https://get.up99.ai) Every other option goes to install.sh, for example: --domain monitor.example.com --tls admin@example.com EOF } # json_field NAME FILE prints the string NAME holds in the server's JSON # answer. The answers are flat and their strings hold no quotes. json_field() { sed -n "s/.*\"$1\":\"\([^\"]*\)\".*/\1/p" "$2" | head -n 1 } # get PATH OUT [progress] downloads PATH with the token into OUT, or stops # with the reason the server gave. get() { show=-sS if [ "${3:-}" = progress ] && [ -t 2 ]; then show=--progress-bar; fi status="$(curl "$show" --proto =https --tlsv1.2 --connect-timeout 20 --retry 2 \ -H "@$work/auth" -o "$2" -w '%{http_code}' "$server$1")" || fail "could not download $server$1: check this host's internet access, DNS and proxy" [ "$status" = 200 ] && return 0 code="" message="" if [ -f "$2" ]; then code="$(json_field code "$2")" message="$(json_field message "$2")" rm -f -- "$2" fi case "$status:$code" in 401:*) fail "the install token was refused. ${message:-}${message:+ }Check that --token names the file up99 sent." ;; 403:revoked) fail "up99 has revoked this install token. Ask up99 for a new one." ;; 403:not_covered) fail "${message:-Your licence does not cover this release. Renew to install it.}" ;; 404:*) fail "${message:-$server has no $1}" ;; 429:*) fail "too many requests from this address. Wait a minute and run this again." ;; *) fail "$server answered $status for $1${message:+: $message}" ;; esac } # The whole script runs from here, called on its last line: piped into sh, # nothing runs before all of it has arrived. main() { token_file="" version="" # Options this script reads are taken out; the rest are put back, in # order, for install.sh. n=$# while [ "$n" -gt 0 ]; do arg="$1" shift n=$((n - 1)) case "$arg" in --token | --version | --server) [ "$n" -gt 0 ] || fail "$arg needs a value" value="$1" shift n=$((n - 1)) ;; --token=* | --version=* | --server=*) value="${arg#*=}" arg="${arg%%=*}" ;; -h | --help) usage exit 0 ;; *) set -- "$@" "$arg" continue ;; esac case "$arg" in --token) token_file="$value" ;; --version) version="$value" ;; --server) server="${value%/}" ;; esac done [ "$(id -u)" -eq 0 ] || fail "run as root: curl -fsSL https://get.up99.ai | sudo sh -s -- --token FILE" [ "$(uname -s)" = Linux ] || fail "the up99 server runs on Linux" case "$(uname -m)" in x86_64 | amd64) arch=amd64 ;; aarch64 | arm64) arch=arm64 ;; *) fail "no up99 server for this processor: $(uname -m)" ;; esac for tool in curl openssl tar sha256sum; do command -v "$tool" > /dev/null 2>&1 || fail "$tool is required: install it and run this again" done case "$server" in https://*) ;; *) fail "--server must be an https:// address" ;; esac if [ -n "$version" ]; then printf '%s\n' "$version" | grep -Eqx '[0-9]+\.[0-9]+\.[0-9]+' || fail "--version must look like 1.2.3: $version" fi if [ -z "$token_file" ]; then [ -f "$kept_token" ] || fail "--token FILE is required: the install token file up99 sent" token_file="$kept_token" fi [ -f "$token_file" ] && [ -r "$token_file" ] || fail "--token: no such file: $token_file" # install.sh runs from the bundle's directory: give it an absolute path. case "$token_file" in /*) ;; *) token_file="$(pwd)/$token_file" ;; esac token="$(tr -d ' \t\r\n' < "$token_file")" case "$token" in '' | *[!A-Za-z0-9._-]* | *.*.*.* | *..*) fail "$token_file does not hold an up99 install token" ;; *.*.*) ;; *) fail "$token_file does not hold an up99 install token" ;; esac umask 077 work="$(mktemp -d)" trap 'rm -rf -- "$work"' EXIT trap 'exit 1' HUP INT TERM # The token goes to curl in a file, never on its command line, where # every user of this host could read it. printf 'Authorization: Bearer %s\n' "$token" > "$work/auth" if [ -z "$version" ]; then get /v1/releases/latest "$work/latest" version="$(json_field version "$work/latest")" printf '%s\n' "$version" | grep -Eqx '[0-9]+\.[0-9]+\.[0-9]+' || fail "$server named no release to install" fi bundle="up99-server-$version-linux-$arch" dir="$releases_dir/$version" say "up99 $version for $arch, into $dir" get "/v1/releases/$version/SHA256SUMS" "$work/SHA256SUMS" get "/v1/releases/$version/SHA256SUMS.up99.sig" "$work/SHA256SUMS.up99.sig" # up99's release signing key: deploy/release/up99-release.pub. cat > "$work/up99-release.pub" <<'EOF' -----BEGIN PUBLIC KEY----- MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEDWZzbmmv+en/eIS97isB5C5A8/Kh m6OJCz7rwrpm/vadVkQxQISdPRbz4eSMpbz0r6rmMAvLKYg2lTTztsS4tA== -----END PUBLIC KEY----- EOF tr -d '\r\n' < "$work/SHA256SUMS.up99.sig" | openssl base64 -d -A > "$work/SHA256SUMS.sig" 2> /dev/null || fail "the release signature of up99 $version is unreadable; nothing was installed" openssl dgst -sha256 -verify "$work/up99-release.pub" -signature "$work/SHA256SUMS.sig" "$work/SHA256SUMS" > /dev/null 2>&1 || fail "SHA256SUMS of up99 $version does not carry up99's release signature; nothing was installed" expected="$(awk -v n="$bundle.tar.gz" '$2 == n || $2 == "*" n { print $1 }' "$work/SHA256SUMS")" printf '%s\n' "$expected" | grep -Eqx '[0-9a-f]{64}' || fail "up99 $version has no server bundle for $arch" umask 022 mkdir -p "$dir" if [ -f "$dir/$bundle.tar.gz" ] && [ "$(sha256sum < "$dir/$bundle.tar.gz" | cut -d ' ' -f 1)" = "$expected" ]; then say "already downloaded: $dir/$bundle.tar.gz" else get "/v1/releases/$version/$bundle.tar.gz" "$dir/$bundle.tar.gz.part" progress if [ "$(sha256sum < "$dir/$bundle.tar.gz.part" | cut -d ' ' -f 1)" != "$expected" ]; then rm -f -- "$dir/$bundle.tar.gz.part" fail "the downloaded bundle does not match the signed SHA256SUMS; nothing was installed" fi mv -f -- "$dir/$bundle.tar.gz.part" "$dir/$bundle.tar.gz" fi mv -f -- "$work/SHA256SUMS" "$work/SHA256SUMS.up99.sig" "$dir/" say "verified against up99's release signature" rm -rf -- "${dir:?}/$bundle" tar -xzf "$dir/$bundle.tar.gz" -C "$dir" || fail "could not unpack $dir/$bundle.tar.gz" [ -x "$dir/$bundle/install.sh" ] || fail "the bundle holds no install.sh" # exec replaces this shell, so the EXIT trap would not run: clean up now. rm -rf -- "$work" trap - EXIT HUP INT TERM cd "$dir/$bundle" say "running $dir/$bundle/install.sh" # Piped into sh, standard input is this script: the installer's questions # are asked on the terminal when there is one. if (exec < /dev/tty) 2> /dev/null; then exec ./install.sh --token "$token_file" "$@" < /dev/tty fi exec ./install.sh --token "$token_file" "$@" < /dev/null } main "$@"