#!/bin/sh # Installs this machine as your first CawCo hub, dashboard and agent. set -eu umask 077 main() { REPO='https://github.com/Bewinxed/cawco' PUBLIC_KEY='-----BEGIN PUBLIC KEY----- MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEj963TG8syvVsD4ARqN6vLL8IBcAt d2g5Ei0j7wosaXpEOaupx0uGLD4IlJHuP1ERoqVQKXPxxiMjJaYEXzWrVA== -----END PUBLIC KEY----- ' RELEASE_HOST='' RELEASE_HOST="${CAWCO_RELEASE_HOST:-$RELEASE_HOST}" CHANNEL="${CAWCO_CHANNEL:-stable}" ROOT="${XDG_DATA_HOME:-$HOME/.local/share}/cawco/binary" BIN_LINK="$HOME/.local/bin/cawco" WORK="" CREATED_ROOT=0 CREATED_LINK=0 CREATED_VERSION="" SETUP_BEGAN=0 LINGER_ENABLED=0 say() { printf 'cawco-install: %s\n' "$*"; } fail() { printf 'cawco: %s\n' "$*" >&2; exit 1; } # Whatever this run created is removed if it does not finish; before the # first file is placed there is nothing to remove. undo() { [ "$?" != 0 ] || return 0 set +e if [ "$SETUP_BEGAN" = 1 ] && [ -n "$CREATED_VERSION" ]; then "$ROOT/versions/$CREATED_VERSION/$EXE" service uninstall >/dev/null 2>&1 fi if [ "$CREATED_ROOT" = 1 ]; then rm -rf "$ROOT" rmdir "$(dirname "$ROOT")" 2>/dev/null elif [ -n "$CREATED_VERSION" ]; then rm -rf "$ROOT/versions/$CREATED_VERSION" "$ROOT/current" "$ROOT/keeper" "$ROOT/run" "$ROOT/installation.json" "$ROOT/update-state.json" fi [ "$CREATED_LINK" = 0 ] || rm -f "$BIN_LINK" [ "$LINGER_ENABLED" = 0 ] || loginctl disable-linger "$(id -un)" >/dev/null 2>&1 [ -z "$WORK" ] || rm -rf "$WORK" } trap undo EXIT trap '' HUP case "$(uname -s)" in Linux) OS=linux ;; Darwin) OS=darwin ;; *) fail "CawCo runs on Linux and macOS." ;; esac # What a release archive holds and a build's folder versions/ keeps (TOP), and the build's executable in it # (EXE): on macOS the signed bundle, exactly as the release signed it, the only place macOS lets it run. if [ "$OS" = darwin ]; then TOP='CawCo Agent.app' EXE='CawCo Agent.app/Contents/MacOS/cawco' else TOP=cawco EXE=cawco fi ask_yes() { [ -t 1 ] && (: /dev/null || return 1 printf '%s [y/N] ' "$1" >/dev/tty read -r answer /dev/null || :)" printf 'CawCo %s is already installed on this machine (channel: %s).\n' "${installed:-unknown}" "${channel:-not reported yet}" printf 'Updates are installed from the CawCo app, automatically when this machine is idle or with Install now. Nothing was changed.\n' return 0 fi case "$(uname -m)" in x86_64|amd64) ARCH=x64 ;; arm64|aarch64) ARCH=arm64 ;; *) fail "CawCo has builds for x86_64 and arm64 machines; this one is $(uname -m)." ;; esac TARGET="$OS-$ARCH" case "$CHANNEL" in stable|nightly) ;; *) fail "CAWCO_CHANNEL must be stable or nightly." ;; esac for tool in curl tar; do command -v "$tool" >/dev/null 2>&1 || fail "$tool is needed to download CawCo and this machine has none. Install it with the system's package manager, then run this again." done if command -v sha256sum >/dev/null 2>&1; then sha256_of() { sha256sum "$1" | cut -d ' ' -f 1; } elif command -v shasum >/dev/null 2>&1; then sha256_of() { shasum -a 256 "$1" | cut -d ' ' -f 1; } else fail "A SHA-256 tool (sha256sum or shasum) is needed to check the download and this machine has none." fi # The service manager and lingering are checked before anything is placed. if [ "$OS" = linux ]; then [ -d /run/systemd/system ] && command -v systemctl >/dev/null 2>&1 || fail "this machine is not running systemd; CawCo needs systemd user services." USER_NAME="$(id -un)" if [ "$(loginctl show-user "$USER_NAME" --property=Linger --value 2>/dev/null)" != yes ]; then if loginctl enable-linger "$USER_NAME" 2>/dev/null; then LINGER_ENABLED=1 say "turned on persistent user services for $USER_NAME" else say "persistent user services are off for $USER_NAME; the command to turn them on is: sudo loginctl enable-linger $USER_NAME" if ask_yes "Run that command now?" && sudo loginctl enable-linger "$USER_NAME"; then LINGER_ENABLED=1 else fail "persistent user services are off, so systemd would stop CawCo when your last session closes. Run: sudo loginctl enable-linger $USER_NAME, then run this again. Nothing was installed." fi fi fi else command -v launchctl >/dev/null 2>&1 || fail "launchctl is missing; CawCo needs launchd on a Mac." fi # An existing command by this name that this installer did not make is left alone. if [ -e "$BIN_LINK" ] || [ -L "$BIN_LINK" ]; then case "$(readlink "$BIN_LINK" 2>/dev/null || :)" in "$ROOT/current/$EXE") ;; *) fail "$BIN_LINK already exists and is not CawCo's. Move it aside, then run this again. Nothing was installed." ;; esac fi # The signature is checked with the system's own openssl. It is never skipped. if ! command -v openssl >/dev/null 2>&1; then [ "$OS" = linux ] || fail "openssl is missing and this Mac should have one at /usr/bin/openssl. Install it, then run this again." if command -v apt-get >/dev/null 2>&1; then INSTALL="apt-get install -y openssl" elif command -v dnf >/dev/null 2>&1; then INSTALL="dnf install -y openssl" elif command -v yum >/dev/null 2>&1; then INSTALL="yum install -y openssl" elif command -v zypper >/dev/null 2>&1; then INSTALL="zypper --non-interactive install openssl" elif command -v pacman >/dev/null 2>&1; then INSTALL="pacman --noconfirm -S openssl" elif command -v apk >/dev/null 2>&1; then INSTALL="apk add openssl" else fail "openssl is missing and no supported package manager was found. Install openssl, then run this again." fi SUDO="" if [ "$(id -u)" != 0 ]; then command -v sudo >/dev/null 2>&1 && sudo -n true 2>/dev/null || fail "openssl is missing and installing it needs administrator access. Ask an administrator to run: sudo $INSTALL, then run this again." SUDO="sudo " fi say "installing openssl, to check the release signature; command: $SUDO$INSTALL" if command -v apt-get >/dev/null 2>&1; then $SUDO apt-get update >/dev/null || fail "refreshing package indexes failed; run: ${SUDO}apt-get update, then run this again." fi $SUDO $INSTALL >/dev/null || fail "installing openssl failed; run: $SUDO$INSTALL, then run this again." fi WORK="$(mktemp -d)" download() { curl -fsSL --retry 3 --connect-timeout 15 --max-filesize "$3" "$1" -o "$2" || fail "could not download $1"; } say "finding the newest $CHANNEL release" if [ -n "$RELEASE_HOST" ]; then BASE="${RELEASE_HOST%/}/$CHANNEL" else SLUG="${REPO#https://github.com/}" if [ "$CHANNEL" = stable ]; then TAG="$(curl -fsSLI -o /dev/null -w '%{url_effective}' "$REPO/releases/latest" | sed 's|.*/tag/||')" || fail "could not reach $REPO" else # newestNightly's rule: the nightly-- tags, highest count first, and the first whose # release is published and a pre-release. Release-by-tag serves published releases only, so a draft # answers 404 and the next tag is tried. In a release, draft and prerelease follow its author object # with no brace between them. download "https://api.github.com/repos/$SLUG/git/matching-refs/tags/nightly-" "$WORK/tags.json" 1048576 TAG="" for CANDIDATE in $(grep -o '"ref": *"refs/tags/nightly-[0-9][0-9]*-[0-9a-f][0-9a-f]*"' "$WORK/tags.json" \ | sed 's|.*"refs/tags/\(nightly-\([0-9]*\)-[0-9a-f]*\)"|\2 \1|' | sort -rn | cut -d ' ' -f 2); do STATUS="$(curl -sSL --retry 3 --connect-timeout 15 --max-filesize 4194304 -o "$WORK/release-tag.json" -w '%{http_code}' "https://api.github.com/repos/$SLUG/releases/tags/$CANDIDATE")" \ || fail "could not download the $CANDIDATE release" case "$STATUS" in 200) ;; 404) continue ;; *) fail "could not download the $CANDIDATE release (HTTP $STATUS)" ;; esac if tr -d '\n' < "$WORK/release-tag.json" | grep -q '"draft": *false,[^{}]*"prerelease": *true'; then TAG="$CANDIDATE" break fi done fi [ -n "${TAG:-}" ] || fail "no published $CHANNEL release was found at $REPO" BASE="$REPO/releases/download/$TAG" fi download "$BASE/release.json" "$WORK/release.json" 1048576 download "$BASE/release.json.sig" "$WORK/release.json.sig" 4096 say "verifying the release signature" printf '%s\n' "$PUBLIC_KEY" > "$WORK/release.pub" openssl base64 -d -A -in "$WORK/release.json.sig" -out "$WORK/release.sig" 2>/dev/null \ || fail "the release signature is not readable. Nothing was installed." openssl dgst -sha256 -verify "$WORK/release.pub" -signature "$WORK/release.sig" "$WORK/release.json" >/dev/null 2>&1 \ || fail "the release signature does not match CawCo's release key. Nothing was installed." MANIFEST="$(cat "$WORK/release.json")" VERSION="$(printf '%s' "$MANIFEST" | sed -n 's/^{"version":"\([^"]*\)".*/\1/p')" [ -n "$VERSION" ] || fail "the signed release names no version. Nothing was installed." printf '%s' "$MANIFEST" | grep -q "\"channel\":\"$CHANNEL\"" || fail "the signed release is not on the $CHANNEL channel. Nothing was installed." ENTRY="$(printf '%s' "$MANIFEST" | grep -o "{\"target\":\"$TARGET\",\"archive\":\"[^\"]*\",\"sha256\":\"[0-9a-f]*\",\"size\":[0-9]*,\"binarySha256\":\"[0-9a-f]*\"")" \ || fail "this release has no build for $TARGET. Nothing was installed." ARCHIVE="$(printf '%s' "$ENTRY" | sed 's/.*"archive":"\([^"]*\)".*/\1/')" ARCHIVE_SHA="$(printf '%s' "$ENTRY" | sed 's/.*"sha256":"\([0-9a-f]*\)".*/\1/')" ARCHIVE_SIZE="$(printf '%s' "$ENTRY" | sed 's/.*"size":\([0-9]*\).*/\1/')" BINARY_SHA="$(printf '%s' "$ENTRY" | sed 's/.*"binarySha256":"\([0-9a-f]*\)".*/\1/')" say "downloading CawCo $VERSION for $TARGET" download "$BASE/$ARCHIVE" "$WORK/$ARCHIVE" "$ARCHIVE_SIZE" [ "$(sha256_of "$WORK/$ARCHIVE")" = "$ARCHIVE_SHA" ] || fail "the download does not match the signed checksum. Nothing was installed." mkdir "$WORK/extract" tar -xzf "$WORK/$ARCHIVE" -C "$WORK/extract" "$TOP" || fail "the download could not be unpacked. Nothing was installed." [ "$(sha256_of "$WORK/extract/$EXE")" = "$BINARY_SHA" ] || fail "the unpacked program does not match the signed checksum. Nothing was installed." # Everything is checked; from here on a failure removes what this run created. say "installing CawCo $VERSION into $ROOT" [ -d "$ROOT" ] || CREATED_ROOT=1 mkdir -p "$ROOT/versions/$VERSION" "$HOME/.local/bin" CREATED_VERSION="$VERSION" mv "$WORK/extract/$TOP" "$ROOT/versions/$VERSION/$TOP" chmod 700 "$ROOT/versions/$VERSION/$EXE" cp "$WORK/release.json" "$ROOT/versions/$VERSION/release.json" printf '{"signature":"%s"}\n' "$(cat "$WORK/release.json.sig")" > "$ROOT/versions/$VERSION/release.signature.json" ln -sfn "versions/$VERSION" "$ROOT/current" ln -sfn "versions/$VERSION" "$ROOT/keeper" [ -L "$BIN_LINK" ] || CREATED_LINK=1 ln -sf "$ROOT/current/$EXE" "$BIN_LINK" AUTO="" ASK="" if [ -t 1 ] && (: /dev/null; then ASK=--ask if [ -z "${CAWCO_AUTO_UPDATE:-}" ] && ask_yes "Install CawCo updates automatically when this machine is idle?"; then CAWCO_AUTO_UPDATE=1 fi fi [ "${CAWCO_AUTO_UPDATE:-0}" = 1 ] && AUTO=--auto-update # Fix the socket address explicitly so the printed URL names this install. PORT="${PORT:-3000}" HOST="${HOST:-0.0.0.0}" CAWCO_HUB_PORT="${CAWCO_HUB_PORT:-3456}" export PORT HOST CAWCO_HUB_PORT # A hub already on this network or tailnet: this machine joins it unless a person says to start another. JOINED_EXISTING=0 FOUND="$("$ROOT/current/$EXE" binary-find-hub /dev/null || :)" if [ -n "$FOUND" ]; then say "a CawCo hub is already running at $FOUND" if [ -n "$ASK" ]; then ask_yes "Start a second, separate hub anyway? (No joins that one.)" && FOUND="" else say "this is not a terminal, so this machine starts its own hub. To join that one instead, run: curl -fsSL $FOUND/install.sh | sh" FOUND="" fi fi if [ -n "$FOUND" ]; then say "joining $FOUND" set -- binary-install agent --hub "$FOUND" --channel "$CHANNEL" JOINED_EXISTING=1 else say "starting the hub, dashboard and agent" set -- binary-install hub --hub "http://127.0.0.1:$CAWCO_HUB_PORT" --channel "$CHANNEL" fi [ -z "$RELEASE_HOST" ] || set -- "$@" --release-host "$RELEASE_HOST" [ -z "$AUTO" ] || set -- "$@" "$AUTO" SETUP_BEGAN=1 if [ -n "$ASK" ]; then "$ROOT/current/$EXE" "$@" --ask