#!/bin/sh # YadoriLink installer for macOS (Apple silicon) and Linux (x86_64). # # curl -fsSL https://yadori.link/install.sh | sh # curl -fsSL https://yadori.link/install.sh | sh -s -- --channel beta --yes # # Prefer to read it first? See https://yadori.link/#install ("verify first"). # # What it does, in order, and nothing else: # 1. Fetches the signed update manifest for the channel over HTTPS and # verifies its Ed25519 signature against the pinned release keys, using a # small verifier program whose SHA-256 is pinned in this script. # 2. Downloads the engine bundle the signed manifest names and checks its # size and SHA-256 against the manifest. # 3. Unpacks it in a private staging directory and checks every file against # the SHA256SUMS inside the bundle, SHA256SUMS against engine-identity.json, # and the identity's channel, platform and source. # 4. Only then switches the two program links (yadorilink, # yadorilink-daemon) in /bin to the new version. The previous # version stays on disk until the next upgrade. Any failure before this # step leaves an existing install untouched. # It never uses sudo, never runs anything from the bundle before the checks # pass (then only `yadorilink --version`), and never starts the daemon. It does stop a running # daemon of the install it replaces (that install's own `yadorilink daemon stop`) before switching, # so the old daemon never outlives its programs. # # Options: # --prefix DIR install under DIR (default: $HOME/.local; programs in DIR/bin) # --channel NAME beta (default) or nightly # --version X.Y.Z install exactly this version (env: YADORILINK_VERSION). # The channel's current version, or an older one from its # signed per-version manifest; both are also checked against # the live channel manifest, which can withdraw a version # --from FILE offline install: FILE is a signed manifest envelope (a # channel or per-version manifest); the engine zip and the # pinned verifier program sit next to it. If the live channel # manifest is reachable it must not withdraw the version; # if it is not, a loud warning says withdrawal was not checked # --no-modify-path do not edit a shell profile # --no-start do not start the daemon at the end (the default starts it) # --yes, -y do not ask for confirmation # --force reinstall even if this version is already installed # --uninstall remove what this script installed # --rollback switch back to the previous version this script installed, if its # files are still intact (no network; nothing is downloaded) # --help, -h show this text # # Exit status: 0 ok or already current, 1 other failure, 2 usage, 3 network, # 4 signature, hash, replay or withdrawal refusal, 5 unsupported platform, # 6 downgrade refused, 7 prefix not writable, 19 managed elsewhere (the app or a # package manager owns this machine's YadoriLink data; nothing is installed), # 20 the data on this machine was written by a newer YadoriLink than this bundle # reads, 21 whether the data is usable by this bundle could not be established (20 # and 21 come from the bundle's own `yadorilink-daemon --check-data-compat`, which # reads the data without changing it; nothing is installed), 22 the live manifest is signed # with a key this installer does not pin (this installation is too far behind to update # itself; run the one-liner again to get an installer with the current keys). # # Needs: curl, unzip, awk, and sha256sum or shasum. The Ed25519 signature is # checked by a small pinned verifier program the script downloads and hash-checks # first (see "Ed25519 verification" below); OpenSSL is not needed. set -eu # ---- pins --------------------------------------------------------------- # Everything between the two PINS markers is generated by installer-sync from the # release trust set (never edited by hand; packaging/install/pins.py renders and # checks it, and landing CI compares it with the attested release). # TRUST_SET space-separated "key_id:public_key_hex", the current key first and # the next key after it during a rotation. A manifest is accepted # only if its key_id is in this set and its signature verifies # under that entry's key. # VERIFIER_SHA_* SHA-256 of the signature verifier program per platform # (empty until the verifier is released; the installer then # refuses with a clear message). # MIN_MANIFEST_GENERATED_AT the oldest channel manifest this installer # accepts (a replay floor for a first install; empty = none). # >>> PINS >>> TRUST_SET="yadorilink-release-2026-11:ee25c354773766289f46e0dc2837b017538e9b08c710655450f938dc297581df" VERIFIER_SHA_macos_aarch64="6f116f81a2735c49baba41e89b93b5573bce8fc19d2c9c7a8d0278812df4ee56" VERIFIER_SHA_linux_x86_64="75d60676afc6797e0f60f93b16ac663218c14f6e3763a17e7fa03168fb0def75" VERIFIER_BASE="https://github.com/yadorilink/yadorilink/releases/download/verifier-1" MIN_MANIFEST_GENERATED_AT="2026-10-25T03:57:34Z" # <<< PINS <<< MANIFEST_BASE="https://yadori.link/dl" CURL_PROTO="=https" # Test hooks. Honoured ONLY when YADORILINK_INSTALL_TEST=1, which a real # install never sets. They let the test suite run against a local fixture # release signed by a throw-away key. TEST_MODE=0 TEST_OS="" TEST_ARCH="" TEST_LOADER="" TEST_VERIFIER_SHA="" TEST_FAIL_RECEIPT=0 TEST_NOW="" if [ "${YADORILINK_INSTALL_TEST:-}" = "1" ]; then TEST_MODE=1 if [ -n "${YADORILINK_INSTALL_PUBKEY_HEX:-}" ]; then TRUST_SET="${YADORILINK_INSTALL_KEY_ID:-test-key}:$YADORILINK_INSTALL_PUBKEY_HEX" fi TRUST_SET=${YADORILINK_INSTALL_TRUST_SET:-$TRUST_SET} VERIFIER_BASE=${YADORILINK_INSTALL_VERIFIER_BASE:-$VERIFIER_BASE} TEST_VERIFIER_SHA=${YADORILINK_INSTALL_VERIFIER_SHA256:-} MANIFEST_BASE=${YADORILINK_INSTALL_BASE_URL:-$MANIFEST_BASE} MIN_MANIFEST_GENERATED_AT=${YADORILINK_INSTALL_MIN_GENERATED_AT:-$MIN_MANIFEST_GENERATED_AT} TEST_NOW=${YADORILINK_INSTALL_NOW:-} TEST_OS=${YADORILINK_INSTALL_OS:-} TEST_ARCH=${YADORILINK_INSTALL_ARCH:-} TEST_LOADER=${YADORILINK_INSTALL_LOADER:-} TEST_FAIL_RECEIPT=${YADORILINK_INSTALL_FAIL_RECEIPT:-0} CURL_PROTO="=http,https" printf 'yadorilink-install: TEST MODE (trust root and endpoints overridden)\n' >&2 fi PREFIX=${YADORILINK_PREFIX:-} CHANNEL=${YADORILINK_CHANNEL:-beta} WANT_VERSION=${YADORILINK_VERSION:-} FROM_FILE="" FROM_DIR="" MODIFY_PATH=1 START_DAEMON=1 ASSUME_YES=0 FORCE=0 ACTION=install say() { printf '%s\n' "$*"; } err() { printf 'yadorilink-install: error: %s\n' "$*" >&2; } die() { err "$*"; exit 1; } # refuse CODE MESSAGE: print the reason and exit with CODE (see "Exit status" above). refuse() { code=$1; shift; err "$*"; exit "$code"; } # Human text goes through t/tf so the language of the environment (LC_ALL, LC_MESSAGES, LANG: ja # selects Japanese, anything else English) applies; English is the source text and the fallback. # Output a script reads (exit codes, the "yadorilink-install:" prefix, commands) stays English. tr_language() { tl=${LC_ALL:-}; [ -n "$tl" ] || tl=${LC_MESSAGES:-}; [ -n "$tl" ] || tl=${LANG:-} case $tl in ja|ja_*|ja.*|ja@*) LANGUAGE_JA=1 ;; *) LANGUAGE_JA=0 ;; esac } # t ENGLISH: the template in the language in use. t() { if [ "$LANGUAGE_JA" = 1 ] && tt=$(ja_text "$1") && [ -n "$tt" ]; then printf '%s' "$tt"; else printf '%s' "$1"; fi } # tf ENGLISH ARGS...: the template filled in (printf conversions are %s only, in English order). # shellcheck disable=SC2059 # the template is the message, chosen by language tf() { tf_template=$(t "$1"); shift; printf -- "$tf_template" "$@"; } tsay() { tf "$@"; printf '\n'; } terr() { { printf 'yadorilink-install: error: '; tf "$@"; printf '\n'; } >&2; } tdie() { terr "$@"; exit 1; } trefuse() { code=$1; shift; terr "$@"; exit "$code"; } tr_language # ja_text ENGLISH: its Japanese template (same %s conversions in the same order), or nothing. ja_text() { case $1 in 'HOME is not set') printf '%s' 'HOME が設定されていません' ;; 'need sha256sum, shasum or openssl to check downloads') printf '%s' 'ダウンロードの検証には sha256sum、shasum、openssl のいずれかが必要です' ;; 'this is the macOS/Linux installer. On Windows, install YadoriLink from the Microsoft Store.') printf '%s' 'これは macOS/Linux 用のインストーラーです。Windows では Microsoft Store から YadoriLink をインストールしてください。' ;; 'no YadoriLink engine bundle for %s yet (available: macos-aarch64, linux-x86_64). Nothing was installed.') printf '%s' '%s 用の YadoriLink エンジンバンドルはまだありません(提供中: macos-aarch64、linux-x86_64)。何もインストールされていません。' ;; 'note: the running YadoriLink daemon was stopped before this failure. Start it again with: yadorilink daemon start') printf '%s' '注意: この失敗の前に、実行中の YadoriLink デーモンを停止しました。次のコマンドでもう一度起動してください: yadorilink daemon start' ;; 'another yadorilink install is starting, or left %s/.lock behind; remove that directory if no install is running') printf '%s' '別の yadorilink のインストールが開始中か、%s/.lock が残っています。インストールが実行中でなければ、そのディレクトリを削除してください' ;; 'another yadorilink install is running (pid %s)') printf '%s' '別の yadorilink のインストールが実行中です(pid %s)' ;; 'cannot take the install lock in %s') printf '%s' '%s でインストールのロックを取得できません' ;; 'warning: could not tell whether a YadoriLink daemon is running (yadorilink status exited %s); continuing.') printf '%s' '警告: YadoriLink デーモンが動作中かどうかを判断できませんでした(yadorilink status の終了コード %s)。続行します。' ;; 'Stopping the running YadoriLink daemon, so it does not keep the old program after the switch...') printf '%s' '切り替え後に古いプログラムが残らないよう、実行中の YadoriLink デーモンを停止しています...' ;; 'The running YadoriLink daemon could not be stopped. Run: yadorilink daemon stop (then run this installer again). Nothing was changed.') printf '%s' '実行中の YadoriLink デーモンを停止できませんでした。yadorilink daemon stop を実行してから、このインストーラーをもう一度実行してください。何も変更されていません。' ;; 'The YadoriLink daemon did not stop within 30 seconds. Run: yadorilink daemon stop (then run this installer again). Nothing was changed.') printf '%s' 'YadoriLink デーモンが 30 秒以内に停止しませんでした。yadorilink daemon stop を実行してから、このインストーラーをもう一度実行してください。何も変更されていません。' ;; 'Nothing to uninstall: no YadoriLink install found under %s.') printf '%s' 'アンインストールするものがありません。%s に YadoriLink のインストールが見つかりません。' ;; 'cancelled') printf '%s' 'キャンセルしました' ;; 'Removed. Your sync data and sign-in are untouched (a running daemon was stopped);') printf '%s' '削除しました。同期データとサインインはそのまま残っています(実行中のデーモンは停止しました)。' ;; 'remove its data directory yourself if you want it gone.') printf '%s' 'データディレクトリが不要な場合は、ご自身で削除してください。' ;; 'curl is required') printf '%s' 'curl が必要です' ;; 'unzip is required') printf '%s' 'unzip が必要です' ;; 'cannot write to %s (read-only or no permission). Use --prefix with a directory you own. Nothing was installed.') printf '%s' '%s に書き込めません(読み取り専用か権限がありません)。--prefix で自分が所有するディレクトリを指定してください。何もインストールされていません。' ;; 'cannot create %s') printf '%s' '%s を作成できません' ;; 'The live %s manifest does not withdraw %s.') printf '%s' '公開中の %s のマニフェストでは、%s は取り下げられていません。' ;; 'WARNING: %s could not be reached (offline?), so it was NOT checked whether version %s has been withdrawn. Withdrawal not checked.') printf '%s' '警告: %s に接続できなかった(オフライン?)ため、バージョン %s が取り下げられていないかは確認していません。取り下げの確認なし。' ;; 'Checking %s channel (%s)...') printf '%s' '%s チャンネル(%s)を確認しています...' ;; 'No release is published yet on the %s channel (no update manifest at %s).') printf '%s' '%s チャンネルにはまだリリースが公開されていません(%s にアップデートのマニフェストがありません)。' ;; 'Nothing was installed. Check https://yadori.link/ for the first release.') printf '%s' '何もインストールされていません。最初のリリースについては https://yadori.link/ をご確認ください。' ;; 'could not reach %s. Check your network connection. Nothing was installed.') printf '%s' '%s に接続できませんでした。ネットワーク接続を確認してください。何もインストールされていません。' ;; 'unexpected HTTP status %s from %s. Nothing was installed.') printf '%s' 'HTTP ステータス %s が %s から返されました。何もインストールされていません。' ;; 'version %s has no published manifest (%s). Nothing was installed.') printf '%s' 'バージョン %s には公開されたマニフェストがありません(%s)。何もインストールされていません。' ;; 'YadoriLink %s is already installed and up to date. (--force reinstalls.)') printf '%s' 'YadoriLink %s は既にインストールされており、最新です。(--force で再インストールできます。)' ;; 'installed version %s is newer than %s on the %s channel; not downgrading. Nothing was changed.') printf '%s' 'インストール済みのバージョン %s は %s(%s チャンネル)より新しいため、ダウングレードしません。何も変更されていません。' ;; '%s/%s already exists and was not installed by this script. Remove it or choose another --prefix. Nothing was changed.') printf '%s' '%s/%s は既に存在し、このスクリプトでインストールされたものではありません。削除するか、別の --prefix を指定してください。何も変更されていません。' ;; 'Using %s (%s) from %s...') printf '%s' '%s(%s)を %s から使用しています...' ;; 'cannot copy %s/%s. Nothing was installed.') printf '%s' '%s/%s をコピーできません。何もインストールされていません。' ;; 'Downloading %s (%s)...') printf '%s' '%s(%s)をダウンロードしています...' ;; 'download failed or was interrupted (%s). Nothing was installed.') printf '%s' 'ダウンロードに失敗したか中断されました(%s)。何もインストールされていません。' ;; 'could not unpack the bundle') printf '%s' 'バンドルを展開できませんでした' ;; 'the new yadorilink does not run on this system (--version failed). Nothing was changed.') printf '%s' '新しい yadorilink はこのシステムでは動作しません(--version が失敗しました)。何も変更されていません。' ;; 'could not write the install receipt beside the binaries. Nothing was changed.') printf '%s' 'バイナリの隣にインストール記録を書き込めませんでした。何も変更されていません。' ;; 'could not switch %s/yadorilink-daemon. The previous install was kept.') printf '%s' '%s/yadorilink-daemon を切り替えられませんでした。以前のインストールはそのまま残しました。' ;; 'could not switch %s/yadorilink. The previous install was restored.') printf '%s' '%s/yadorilink を切り替えられませんでした。以前のインストールに戻しました。' ;; 'could not write the install receipt. The previous install was restored.') printf '%s' 'インストール記録を書き込めませんでした。以前のインストールに戻しました。' ;; 'Installed YadoriLink %s (%s, %s) in %s.') printf '%s' 'YadoriLink %s(%s、%s)を %s にインストールしました。' ;; 'Release notes: %s') printf '%s' 'リリースノート: %s' ;; 'Next: sign in with') printf '%s' '次の手順: 次のコマンドでサインインしてください' ;; 'Upgrade later by running the installer again. Remove with: install.sh --uninstall') printf '%s' 'アップグレードするには、後でもう一度インストーラーを実行してください。削除するには: install.sh --uninstall' ;; 'The daemon was not started (--no-start). Start it with: yadorilink daemon start') printf '%s' 'デーモンは起動していません(--no-start)。起動するには: yadorilink daemon start' ;; 'YadoriLink is running.') printf '%s' 'YadoriLink は実行中です。' ;; 'The old daemon was stopped; the new one is running.') printf '%s' '古いデーモンを停止し、新しいデーモンが実行中です。' ;; 'The YadoriLink daemon could not be started. Start it with: yadorilink daemon start') printf '%s' 'YadoriLink デーモンを起動できませんでした。起動するには: yadorilink daemon start' ;; 'Added %s to your PATH in %s. Open a new terminal, or run:') printf '%s' '%s を %s の PATH に追加しました。新しいターミナルを開くか、次を実行してください:' ;; '%s is not on your PATH. Add it, for example:') printf '%s' '%s は PATH に含まれていません。たとえば次のように追加してください:' ;; 'could not switch %s/yadorilink-daemon. Nothing was changed.') printf '%s' '%s/yadorilink-daemon を切り替えられませんでした。何も変更されていません。' ;; 'could not switch %s/yadorilink. The current version was restored.') printf '%s' '%s/yadorilink を切り替えられませんでした。現在のバージョンに戻しました。' ;; 'could not write the install receipt. The current version was restored.') printf '%s' 'インストール記録を書き込めませんでした。現在のバージョンに戻しました。' ;; 'Rolled back to YadoriLink %s (from %s).') printf '%s' 'YadoriLink %s にロールバックしました(%s から)。' ;; 'The running daemon was stopped. Start it again: yadorilink daemon start') printf '%s' '実行中のデーモンを停止しました。もう一度起動するには: yadorilink daemon start' ;; 'No daemon was running. Start it with: yadorilink daemon start') printf '%s' 'デーモンは実行されていませんでした。起動するには: yadorilink daemon start' ;; 'Upgrade YadoriLink %s -> %s under %s?') printf '%s' 'YadoriLink を %s から %s にアップグレードしますか?(%s)' ;; 'Remove the YadoriLink install under %s?') printf '%s' '%s の YadoriLink のインストールを削除しますか?' ;; 'Install YadoriLink %s under %s?') printf '%s' 'YadoriLink %s を %s にインストールしますか?' ;; *) return 1 ;; esac } usage() { # Print the header comment block (everything up to the first blank line # after "Options:"). awk 'NR==1{next} /^set -eu/{exit} /^#/{sub(/^# ?/,""); print}' "$0" 2>/dev/null || say "usage: install.sh [--prefix DIR] [--channel beta|nightly] [--version X.Y.Z] [--from FILE] [--no-modify-path] [--no-start] [--yes] [--force] [--uninstall]" } # parse_args and derive paths. All work happens in main, called on the last # line, so a truncated download runs nothing. setup() { while [ $# -gt 0 ]; do case $1 in --prefix) [ $# -ge 2 ] || refuse 2 "--prefix needs a directory"; PREFIX=$2; shift 2 ;; --prefix=*) PREFIX=${1#--prefix=}; shift ;; --channel) [ $# -ge 2 ] || refuse 2 "--channel needs a name"; CHANNEL=$2; shift 2 ;; --channel=*) CHANNEL=${1#--channel=}; shift ;; --version) [ $# -ge 2 ] || refuse 2 "--version needs a value"; WANT_VERSION=$2; shift 2 ;; --version=*) WANT_VERSION=${1#--version=}; shift ;; --from) [ $# -ge 2 ] || refuse 2 "--from needs a file"; FROM_FILE=$2; shift 2 ;; --from=*) FROM_FILE=${1#--from=}; shift ;; --no-modify-path) MODIFY_PATH=0; shift ;; --no-start) START_DAEMON=0; shift ;; --yes|-y) ASSUME_YES=1; shift ;; --force) FORCE=1; shift ;; --uninstall) ACTION=uninstall; shift ;; --rollback) ACTION=rollback; shift ;; --help|-h) usage; exit 0 ;; *) refuse 2 "unknown option: $1 (try --help)" ;; esac done case $CHANNEL in beta) IDENTITY_CHANNEL=beta ;; nightly) IDENTITY_CHANNEL=dev ;; *) refuse 2 "unknown channel '$CHANNEL' (published channels: beta, nightly)" ;; esac WANT_VERSION=${WANT_VERSION#v} [ -n "${HOME:-}" ] || tdie 'HOME is not set' [ -n "$PREFIX" ] || PREFIX=$HOME/.local case $PREFIX in /*) ;; *) refuse 2 "--prefix must be an absolute path" ;; esac BIN=$PREFIX/bin SHARE=$PREFIX/share/yadorilink RECEIPT=$SHARE/install-receipt PATH_BEGIN="# >>> yadorilink ($PREFIX) >>>" PATH_END="# <<< yadorilink ($PREFIX) <<<" } # ---- helpers ---------------------------------------------------------------- have() { command -v "$1" >/dev/null 2>&1; } sha256_of() { if have sha256sum; then sha256sum "$1" | awk '{print $1}' elif have shasum; then shasum -a 256 "$1" | awk '{print $1}' elif have openssl; then openssl dgst -sha256 "$1" | awk '{print $NF}' else tdie 'need sha256sum, shasum or openssl to check downloads' fi } file_size() { wc -c < "$1" | tr -d ' '; } # hex_to_file HEX OUT: write the bytes HEX spells (lowercase hex) to OUT. hex_to_file() { esc=$(printf '%s' "$1" | awk '{ n = length($0) for (i = 1; i < n; i += 2) { v = 0 for (j = 0; j < 2; j++) v = v * 16 + index("0123456789abcdef", substr($0, i + j, 1)) - 1 printf "\\%03o", v } }') # shellcheck disable=SC2059 # the format is octal escapes built above printf "$esc" > "$2" } b64_to_file() { # B64 OUT if printf '%s' "$1" | base64 -d > "$2" 2>/dev/null; then : elif printf '%s' "$1" | base64 -D > "$2" 2>/dev/null; then : elif have openssl && printf '%s' "$1" | openssl base64 -d -A > "$2" 2>/dev/null; then : else return 1 fi } confirm() { # PROMPT; true when ok to continue [ "$ASSUME_YES" = 1 ] && return 0 if ( : < /dev/tty ) 2>/dev/null; then printf '%s [y/N] ' "$1" > /dev/tty read -r answer < /dev/tty || answer=n case $answer in y|Y|yes|YES) return 0 ;; *) return 1 ;; esac fi # No terminal (for example `curl | sh` from a script): the caller already # asked for exactly this, so proceed. return 0 } # ---- Ed25519 verification --------------------------------------------------- # The signature is checked by a small static verifier program, released as a # signed asset of the release process. This script pins the verifier's SHA-256 # per platform (VERIFIER_SHA_* below) and runs it only after the downloaded # bytes match that pin. The verifier is proven by a known-answer test (RFC 8032 # test vector 2, plus a corrupted signature that must fail) before it is used. # # Trust, stated plainly: this trusts (a) this script, which you fetched over # HTTPS from yadori.link (its SHA-256 is published for the verify-first flow), # and through it (b) the pinned verifier hash and (c) the pinned release keys. # Nothing else needs to be trusted for the manifest signature check. WORK="" # The installer carried by the verified bundle, kept for `yadorilink self-update` (see write_receipt). KEPT_INSTALLER_SHA="" VERIFIER="" # fetch_verifier: get the pinned verifier for $TARGET into $WORK (downloaded, or for # --from taken from the directory next to the manifest file) and make it ready to # run, or refuse. It only runs after its bytes match the pin. fetch_verifier() { case $TARGET in macos-aarch64) pin=$VERIFIER_SHA_macos_aarch64 ;; linux-x86_64) pin=$VERIFIER_SHA_linux_x86_64 ;; *) pin="" ;; esac [ -z "$TEST_VERIFIER_SHA" ] || pin=$TEST_VERIFIER_SHA [ -n "$pin" ] || refuse 4 "this installer has no pinned Ed25519 verifier for $TARGET yet (the verifier release is not published). Nothing was installed." VERIFIER=$WORK/yadorilink-verify if [ -n "$FROM_FILE" ]; then [ -f "$FROM_DIR/yadorilink-verify-$TARGET" ] || refuse 2 "--from needs the verifier program yadorilink-verify-$TARGET next to $FROM_FILE (release asset). Nothing was installed." cp "$FROM_DIR/yadorilink-verify-$TARGET" "$VERIFIER" else vurl=$VERIFIER_BASE/yadorilink-verify-$TARGET code=$(curl -sSL --max-redirs 5 --proto "$CURL_PROTO" --proto-redir "$CURL_PROTO" --tlsv1.2 --connect-timeout 15 --max-time 120 \ --max-filesize 8388608 -o "$VERIFIER" -w '%{http_code}' "$vurl") || refuse 3 "could not download the signature verifier ($vurl). Nothing was installed." [ "$code" = 200 ] || refuse 3 "could not download the signature verifier (HTTP $code from $vurl). Nothing was installed." fi [ "$(sha256_of "$VERIFIER")" = "$pin" ] || refuse 4 "the downloaded signature verifier does not match the SHA-256 pinned in this script. Refusing. Nothing was installed." chmod 700 "$VERIFIER" # RFC 8032 test vector 2. kat_pub=3d4017c3e843895a92b70aa74d1b7ebc9c982ccf2ec4968cc0cd55f12af4660c kat_sig=92a009a9f0d4cab8720e820b5f642540a2b27b5416503f8fb3762223ebdb69da085ac1e43e15996e458f3613d0f11d8c387b2eaeb4302aeeb00d291612bb0c00 kat_bad=92a009a9f0d4cab8720e820b5f642540a2b27b5416503f8fb3762223ebdb69da085ac1e43e15996e458f3613d0f11d8c387b2eaeb4302aeeb00d291612bb0c01 hex_to_file 72 "$WORK/kat.msg" hex_to_file "$kat_sig" "$WORK/kat.sig" hex_to_file "$kat_bad" "$WORK/kat.bad" if ! "$VERIFIER" "$kat_pub" "$WORK/kat.msg" "$WORK/kat.sig" >/dev/null 2>&1 || "$VERIFIER" "$kat_pub" "$WORK/kat.msg" "$WORK/kat.bad" >/dev/null 2>&1; then refuse 4 "the signature verifier failed its self-test. Refusing. Nothing was installed." fi } # ed25519_check PUBHEX MSGFILE SIGFILE ed25519_check() { "$VERIFIER" "$1" "$2" "$3" >/dev/null 2>&1; } # ---- JSON extraction (awk; fails closed, the signature has already been # checked when the manifest body is parsed) ---------------------------------- # envelope_field FILE KEY: the unescaped string value of KEY, exactly once. envelope_field() { LC_ALL=C awk -v want="$2" ' BEGIN { RS = "\001" } { s = $0 } END { # The optional top-level trust_chain array (key-rotation records) carries its own # nested key_id fields. The installer pins the trust set itself and ignores the # chain, so cut the array out before looking for a top-level field. tc = index(s, "\"trust_chain\"") if (want != "trust_chain" && tc > 0) { m = length(s); k = tc + 13 while (k <= m && substr(s, k, 1) ~ /[ \t\r\n]/) k++ if (substr(s, k, 1) != ":") exit 5 k++ while (k <= m && substr(s, k, 1) ~ /[ \t\r\n]/) k++ if (substr(s, k, 1) != "[") exit 5 depth = 0; instr = 0; e2 = 0 for (; k <= m; k++) { ch = substr(s, k, 1) if (instr) { if (ch == "\\") k++; else if (ch == "\"") instr = 0; continue } if (ch == "\"") instr = 1 else if (ch == "[") depth++ else if (ch == "]") { depth--; if (depth == 0) { e2 = k; break } } } if (e2 == 0) exit 5 s = substr(s, 1, tc - 1) substr(s, e2 + 1) } pat = "\"" want "\"" first = index(s, pat) if (first == 0) exit 3 rest = substr(s, first + length(pat)) if (index(rest, pat) != 0) exit 4 n = length(rest); i = 1 while (i <= n && substr(rest, i, 1) ~ /[ \t\r\n]/) i++ if (substr(rest, i, 1) != ":") exit 5 i++ while (i <= n && substr(rest, i, 1) ~ /[ \t\r\n]/) i++ if (substr(rest, i, 1) != "\"") exit 5 i++ out = "" while (i <= n) { c = substr(rest, i, 1) if (c == "\"") { printf "%s", out; exit 0 } if (c == "\\") { i++; e = substr(rest, i, 1) if (e == "\"" || e == "\\" || e == "/") out = out e else if (e == "n") out = out "\n" else if (e == "t") out = out "\t" else if (e == "r") out = out "\r" else if (e == "u") { h = substr(rest, i + 1, 4) if (h !~ /^00[0-7][0-9a-fA-F]$/ || h == "0000") exit 6 v = 0 for (j = 3; j <= 4; j++) v = v * 16 + index("0123456789abcdef", tolower(substr(h, j, 1))) - 1 out = out sprintf("%c", v); i += 4 } else exit 6 } else out = out c i++ } exit 7 }' "$1" } # select_entry FILE CHANNEL PLATFORM ARCH [lenient]: print "OK" then key=value lines # of the one release entry whose channel, platform, arch and install_source # ("script": the engine bundle for script installs) match, plus the manifest's # generated_at, expires_at and withdrawn_versions; otherwise an "ERR " line. In # lenient mode a manifest without a matching entry still prints "OK" (with the # entry fields empty): the live manifest of a channel is consulted for its # withdrawals even when it has no entry for this platform. select_entry() { LC_ALL=C awk -v ch="$2" -v pl="$3" -v ar="$4" -v lenient="${5:-}" ' BEGIN { RS = "\001" } { body = $0 } function fail(m) { print "ERR " m; bad = 1; exit 0 } END { n = length(body); depth = 0; i = 1; key = ""; expectkey = 0; matches = 0; schema = ""; gen = ""; expv = ""; withdrawn = "" while (i <= n) { c = substr(body, i, 1) if (c ~ /[ \t\r\n,:]/) { i++; continue } if (c == "{") { depth++; expectkey = 0; if (depth == 3) { delete e } if (depth > 3) fail("unexpected nesting"); i++; continue } if (c == "[") { depth++; expectkey = 0; if (depth > 2) fail("unexpected nesting"); i++; continue } if (c == "}" || c == "]") { if (c == "}" && depth == 3) { if (e["channel"] == ch && e["platform"] == pl && e["arch"] == ar && e["install_source"] == "script") { matches++; for (k in e) sel[k] = e[k] } } depth--; expectkey = 0; i++; continue } if (c == "\"") { j = i + 1; tok = "" while (j <= n && substr(body, j, 1) != "\"") { if (substr(body, j, 1) == "\\") fail("escape in manifest field") tok = tok substr(body, j, 1); j++ } i = j + 1 } else { j = i; tok = "" while (j <= n && substr(body, j, 1) !~ /[ \t\r\n,}\]]/) { tok = tok substr(body, j, 1); j++ } i = j } if (depth == 2 && key == "withdrawn_versions") { withdrawn = withdrawn (withdrawn == "" ? "" : " ") tok; continue } if (depth == 1 || depth == 3) { if (!expectkey) { key = tok; expectkey = 1 } else { if (depth == 3) e[key] = tok else if (key == "schema_version") schema = tok else if (key == "generated_at") gen = tok else if (key == "expires_at") expv = tok expectkey = 0 } } } if (bad) exit 0 if (schema != "2") fail("unsupported manifest schema_version") if (matches == 0 && lenient == "") fail("no matching release") if (matches > 1) fail("ambiguous releases") print "OK" split("version minimum_supported_version kill_switch artifact_url artifact_sha256 artifact_size release_notes_url", names, " ") for (q = 1; q <= 7; q++) print names[q] "=" sel[names[q]] print "generated_at=" gen print "expires_at=" expv print "withdrawn_versions=" withdrawn }' "$1" } # semver_cmp A B: print -1, 0 or 1 (A relative to B). semver_cmp() { awk -v a="$1" -v b="$2" ' function core(v, out, t) { sub(/^v/, "", v); sub(/\+.*/, "", v); split(v, t, "-"); split(t[1], out, "."); return substr(v, length(t[1]) + 2) } BEGIN { pa = core(a, ca); pb = core(b, cb) for (i = 1; i <= 3; i++) { if (ca[i] + 0 < cb[i] + 0) { print -1; exit } if (ca[i] + 0 > cb[i] + 0) { print 1; exit } } if (pa == pb) { print 0; exit } if (pa == "") { print 1; exit } if (pb == "") { print -1; exit } na = split(pa, xa, "."); nb = split(pb, xb, ".") for (i = 1; i <= (na > nb ? na : nb); i++) { if (i > na) { print -1; exit } if (i > nb) { print 1; exit } numa = xa[i] ~ /^[0-9]+$/; numb = xb[i] ~ /^[0-9]+$/ if (numa && numb) { if (xa[i] + 0 != xb[i] + 0) { print (xa[i] + 0 < xb[i] + 0) ? -1 : 1; exit } } else if (numa) { print -1; exit } else if (numb) { print 1; exit } else if (xa[i] != xb[i]) { print (xa[i] < xb[i]) ? -1 : 1; exit } } print 0 }' } # id_field FILE KEY: the value of KEY in engine-identity.json (written with # one key per line); exactly one occurrence or failure. id_field() { vals=$(sed -n "s/^ *\"$2\": *\"\\([^\"]*\\)\" *,\\{0,1\\} *\$/\\1/p; s/^ *\"$2\": *\\([0-9][0-9]*\\) *,\\{0,1\\} *\$/\\1/p" "$1") [ -n "$vals" ] || return 1 [ "$(printf '%s\n' "$vals" | wc -l | tr -d ' ')" = 1 ] || return 1 printf '%s' "$vals" } # ---- ownership -------------------------------------------------------------- # state_dir: the data area the daemon uses (crates/yadorilink-fs-platform/src/state_dir.rs). state_dir() { if [ -n "${YADORILINK_CONFIG_DIR:-}" ]; then printf '%s' "$YADORILINK_CONFIG_DIR" elif [ "$PLATFORM" = macos ]; then printf '%s' "$HOME/Library/Application Support/yadorilink" else printf '%s' "${XDG_DATA_HOME:-$HOME/.local/share}/yadorilink"; fi } # check_not_managed_elsewhere: one data area has one owner, and only the owner's updater # changes its programs (see the desktop and CLI coexistence design). The ownership record # decides: no record means nothing is owned yet; a record naming the app, another installation # or an unreadable one stops the install before anything is fetched or written. check_not_managed_elsewhere() { rec=$(state_dir)/install-owner.json [ -e "$rec" ] || return 0 unreadable="the ownership record $rec cannot be read, so who manages YadoriLink here is unknown. Nothing was installed. Fix or remove that file only if you know no other YadoriLink install uses this data." rec_owner=$(id_field "$rec" owner) || refuse 19 "$unreadable" rec_source=$(id_field "$rec" install_source) || refuse 19 "$unreadable" switch="To use this installer instead: $(remove_how "$rec_source"), delete $rec, then run the installer again." [ "$rec_owner" != gui ] || refuse 19 "YadoriLink is managed by the app ($rec_source), which already provides the yadorilink command. Nothing was installed. $switch" [ "$rec_source" = script ] || refuse 19 "this machine's YadoriLink data belongs to another installation ($rec_source). Nothing was installed. $switch" } # remove_how SOURCE: how the installation named by an ownership record's install_source is # removed (mirrors ownership::uninstaller_elsewhere). remove_how() { case $1 in app_bundle) printf '%s' "quit YadoriLink and move YadoriLink.app to the Trash" ;; app_store) printf '%s' "quit YadoriLink and delete it from Applications" ;; microsoft_store) printf '%s' "uninstall YadoriLink in Settings > Apps > Installed apps" ;; homebrew) printf '%s' "run brew uninstall yadorilink" ;; apt) printf '%s' "run sudo apt remove yadorilink" ;; winget) printf '%s' "run winget uninstall yadorilink" ;; container) printf '%s' "stop and remove the container" ;; *) printf '%s' "run yadorilink daemon stop and delete the binaries you placed by hand (a standalone copy has no installer or updater)" ;; esac } # ---- platform --------------------------------------------------------------- detect_target() { os=${TEST_OS:-$(uname -s)} machine=${TEST_ARCH:-$(uname -m)} case $os in Darwin|macos) PLATFORM=macos ;; Linux|linux) PLATFORM=linux ;; MINGW*|MSYS*|CYGWIN*|windows) trefuse 5 'this is the macOS/Linux installer. On Windows, install YadoriLink from the Microsoft Store.' ;; *) refuse 5 "unsupported operating system '$os'. YadoriLink engine bundles exist for macOS (Apple silicon), Linux (x86_64) and Windows (x86_64)." ;; esac case $machine in arm64|aarch64) ARCH=aarch64 ;; x86_64|amd64) ARCH=x86_64 ;; *) refuse 5 "unsupported CPU '$machine'. YadoriLink engine bundles exist for macOS (Apple silicon), Linux (x86_64) and Windows (x86_64)." ;; esac # An x86_64 shell under Rosetta on Apple silicon still wants the native build. if [ -z "$TEST_OS" ] && [ "$PLATFORM" = macos ] && [ "$ARCH" = x86_64 ] && [ "$(sysctl -n hw.optional.arm64 2>/dev/null || true)" = 1 ]; then ARCH=aarch64 fi TARGET=$PLATFORM-$ARCH case $TARGET in macos-aarch64|linux-x86_64) ;; *) trefuse 5 'no YadoriLink engine bundle for %s yet (available: macos-aarch64, linux-x86_64). Nothing was installed.' "$TARGET" ;; esac # The Linux bundle is built for glibc. Without glibc's dynamic loader (Alpine and other musl # systems, NixOS) it cannot run, so refuse before downloading ~70 MB for nothing. if [ "$TARGET" = linux-x86_64 ] && { [ -z "$TEST_OS" ] || [ -n "$TEST_LOADER" ]; }; then [ -e "${TEST_LOADER:-/lib64/ld-linux-x86-64.so.2}" ] || refuse 5 "this Linux has no glibc dynamic loader (/lib64/ld-linux-x86-64.so.2), so the engine bundle would not run (Alpine and other musl-based systems are not supported by this installer). For a NAS or server, use the container image ghcr.io/yadorilink/yadorilink instead. Nothing was installed." fi if [ "$PLATFORM" = macos ]; then BIN_SUBDIR=bin/; else BIN_SUBDIR=""; fi } # ---- state ------------------------------------------------------------------ receipt_get() { [ -f "$RECEIPT" ] || return 0; sed -n "s/^$1=//p" "$RECEIPT" | head -n 1; } cleanup() { status=$? if [ -n "${STAGE:-}" ] && [ -d "$STAGE" ]; then rm -rf "$STAGE"; fi if [ -n "${LOCKDIR:-}" ] && [ -d "$LOCKDIR" ] && [ "$(cat "$LOCKDIR/pid" 2>/dev/null)" = "$$" ]; then rm -rf "$LOCKDIR"; fi if [ -n "$WORK" ] && [ -d "$WORK" ]; then rm -rf "$WORK"; fi if [ "$status" != 0 ] && [ "${STOPPED_DAEMON:-0}" = 1 ]; then terr 'note: the running YadoriLink daemon was stopped before this failure. Start it again with: yadorilink daemon start' fi exit "$status" } take_lock() { LOCKDIR=$SHARE/.lock if ! mkdir "$LOCKDIR" 2>/dev/null; then other=$(cat "$LOCKDIR/pid" 2>/dev/null || true) if [ -z "$other" ]; then sleep 1; other=$(cat "$LOCKDIR/pid" 2>/dev/null || true); fi if [ -z "$other" ] && [ -d "$LOCKDIR" ]; then LOCKDIR="" tdie 'another yadorilink install is starting, or left %s/.lock behind; remove that directory if no install is running' "$SHARE" fi if [ -n "$other" ] && kill -0 "$other" 2>/dev/null; then LOCKDIR="" tdie 'another yadorilink install is running (pid %s)' "$other" fi rm -rf "$LOCKDIR" mkdir "$LOCKDIR" 2>/dev/null || { LOCKDIR=""; tdie 'cannot take the install lock in %s' "$SHARE"; } fi printf '%s\n' "$$" > "$LOCKDIR/pid" } # set_link TARGET LINK: point LINK at TARGET atomically (temp link + rename). set_link() { if ln -s "$1" "$2.new.$$" && mv -f "$2.new.$$" "$2"; then return 0; fi rm -f "$2.new.$$" return 1 } # write_receipt: persist the install receipt for $VERSION in $VDIR (atomically). write_receipt() { [ "$TEST_FAIL_RECEIPT" != 1 ] || return 1 { printf 'version=%s\n' "$VERSION" printf 'dir=%s\n' "$SHARE/$VDIR" printf 'previous_dir=%s\n' "$old_dir" printf 'previous_version=%s\n' "$old_version" printf 'channel=%s\n' "$CHANNEL" printf 'target=%s\n' "$TARGET" printf 'engine_api_version=%s\n' "$id_api" printf 'install_source=script\n' printf 'path_profile=%s\n' "$(receipt_get path_profile || true)" # The installer a self-update runs: the copy that came in the verified bundle, proven by the # same SHA256SUMS chain as everything else, and the hash it was proven to have. A bundle # without one records nothing, and a self-update then only prints the command. if [ -n "$KEPT_INSTALLER_SHA" ]; then printf 'installer=%s\n' "$SHARE/installer/install.sh" printf 'installer_sha256=%s\n' "$KEPT_INSTALLER_SHA" fi } > "$SHARE/receipt.new.$$" || return 1 if [ -n "$KEPT_INSTALLER_SHA" ]; then if ! { mkdir -p "$SHARE/installer" && cp "$SHARE/$VDIR/install.sh" "$SHARE/installer/install.sh.new.$$" && chmod 755 "$SHARE/installer/install.sh.new.$$" && [ "$(sha256_of "$SHARE/installer/install.sh.new.$$")" = "$KEPT_INSTALLER_SHA" ] && mv -f "$SHARE/installer/install.sh.new.$$" "$SHARE/installer/install.sh" }; then rm -f "$SHARE/installer/install.sh.new.$$" return 1 fi else rm -f "$SHARE/installer/install.sh" fi mv -f "$SHARE/receipt.new.$$" "$RECEIPT" } # stop_old_daemon: a daemon that keeps running while its programs are replaced is left running the old # version, and the new command line cannot talk to it (it reports a protocol version mismatch and cannot # stop it). So before the programs are switched, the CURRENT install's own command line stops its daemon. # `yadorilink status` exits 0 when a daemon answers and 4 when none is running. Anything that cannot be # stopped, or cannot be told apart, refuses with the exact next step, before anything has been changed. stop_old_daemon() { old_cli=$BIN/yadorilink is_ours "$old_cli" && [ -x "$old_cli" ] || return 0 sd_rc=0 sd_out=$("$old_cli" status 2>&1 /dev/null 2>&1 &1 /dev/null || sleep 1 sd_n=$((sd_n + 1)) done [ "$sd_rc" = 4 ] || trefuse 1 'The YadoriLink daemon did not stop within 30 seconds. Run: yadorilink daemon stop (then run this installer again). Nothing was changed.' STOPPED_DAEMON=1 } # is_ours LINK: true when LINK is a symlink into $SHARE. is_ours() { [ -L "$1" ] || return 1 case $(readlink "$1") in "$SHARE"/*) return 0 ;; *) return 1 ;; esac } path_profile() { case ${SHELL:-} in */zsh) printf '%s\n' "${ZDOTDIR:-$HOME}/.zshrc" ;; */bash) # Bash reads the first of these that exists when it is a login shell; # creating a new .bash_profile would hide an existing .profile. if [ "$(uname -s)" != Darwin ] && [ -f "$HOME/.bashrc" ]; then printf '%s\n' "$HOME/.bashrc" elif [ -f "$HOME/.bash_profile" ]; then printf '%s\n' "$HOME/.bash_profile" elif [ -f "$HOME/.bash_login" ]; then printf '%s\n' "$HOME/.bash_login" else printf '%s\n' "$HOME/.profile"; fi ;; *) printf '%s\n' "$HOME/.profile" ;; esac } remove_path_block() { # FILE [ -f "$1" ] || return 0 grep -qF "$PATH_BEGIN" "$1" || return 0 tmp=$1.yadorilink.$$ PB=$PATH_BEGIN PE=$PATH_END awk '$0 == ENVIRON["PB"] { skip = 1; next } $0 == ENVIRON["PE"] { skip = 0; next } !skip' "$1" > "$tmp" && cat "$tmp" > "$1" rm -f "$tmp" } # ---- uninstall -------------------------------------------------------------- do_uninstall() { if [ ! -d "$SHARE" ]; then tsay 'Nothing to uninstall: no YadoriLink install found under %s.' "$PREFIX" return 0 fi confirm "$(tf 'Remove the YadoriLink install under %s?' "$PREFIX")" || tdie 'cancelled' take_lock # The start-at-login entry `daemon start` made; an older command line has no such flag and # made no entry, so a failure here is not one. if is_ours "$BIN/yadorilink" && [ -x "$BIN/yadorilink" ]; then "$BIN/yadorilink" daemon stop --disable >/dev/null 2>&1 /dev/null || true tsay 'Removed. Your sync data and sign-in are untouched (a running daemon was stopped);' tsay 'remove its data directory yourself if you want it gone.' } # ---- manifests ---------------------------------------------------------------- # str_lt A B: true when A sorts before B (fixed-width timestamps compare as text). str_lt() { [ "$1" != "$2" ] && [ "$(printf '%s\n%s\n' "$1" "$2" | LC_ALL=C sort | head -n 1)" = "$1" ]; } # list_has "a b c" X: true when X is one of the words. list_has() { for w in $1; do [ "$w" = "$2" ] && return 0; done; return 1; } # fetch_envelope URL OUT: download a signed envelope. Returns 0 (HTTP 200), 1 (404), # 2 (another HTTP status, in $HTTP_CODE) or 3 (the network failed). fetch_envelope() { HTTP_CODE=$(curl -sSL --max-redirs 5 --proto "$CURL_PROTO" --proto-redir "$CURL_PROTO" --tlsv1.2 --connect-timeout 15 --max-time 60 \ --max-filesize 1048576 -o "$2" -w '%{http_code}' "$1") || return 3 case $HTTP_CODE in 200) return 0 ;; 404) return 1 ;; *) return 2 ;; esac } # verify_envelope ENVFILE BODYFILE: check the key id and the Ed25519 signature against the # pinned trust set and write the signed body to BODYFILE, or refuse (4). A key that is not pinned # is status $UNPINNED_STATUS: 22 only while the LIVE channel manifest is being checked ("this # installer is too far behind the keys now in use"); any other manifest is a plain refusal (4). UNPINNED_STATUS=4 verify_envelope() { env_key=$(envelope_field "$1" key_id) || refuse 4 "update manifest is malformed (key_id)" KEY_HEX="" for pair in $TRUST_SET; do [ "${pair%%:*}" = "$env_key" ] && KEY_HEX=${pair#*:} done [ -n "$KEY_HEX" ] || refuse "$UNPINNED_STATUS" "update manifest is signed with key '$env_key', which is not one of the pinned release keys. Nothing was installed.$( [ "$UNPINNED_STATUS" != 22 ] || printf ' This installer is older than the keys now in use: run the one-line installer again (curl -fsSL https://yadori.link/install.sh | sh) to get one that pins them.')" envelope_field "$1" manifest_json > "$2" || refuse 4 "update manifest is malformed (manifest_json)" sig_b64=$(envelope_field "$1" signature_base64) || refuse 4 "update manifest is malformed (signature_base64)" b64_to_file "$sig_b64" "$WORK/manifest.sig" || refuse 4 "update manifest signature is not valid base64" [ "$(file_size "$WORK/manifest.sig")" = 64 ] || refuse 4 "update manifest signature has the wrong length" [ -n "$VERIFIER" ] || fetch_verifier if ! ed25519_check "$KEY_HEX" "$2" "$WORK/manifest.sig"; then refuse 4 "the update manifest signature does NOT verify against the pinned release key. Refusing. Nothing was installed." fi ENV_KEY=$env_key } # read_manifest BODYFILE [lenient]: parse a verified body into the M_* variables for this # channel, platform and architecture. read_manifest() { sel=$(select_entry "$1" "$CHANNEL" "$PLATFORM" "$ARCH" "${2:-}") || die "cannot read the update manifest" case $sel in OK*) ;; *) refuse 4 "update manifest: ${sel#ERR } for $CHANNEL/$TARGET. Nothing was installed." ;; esac M_VERSION=""; M_KILL=""; M_URL=""; M_SHA=""; M_SIZE=""; M_NOTES=""; M_MIN=""; M_GEN=""; M_EXPIRES=""; M_WITHDRAWN="" while IFS='=' read -r k v; do case $k in version) M_VERSION=$v ;; minimum_supported_version) M_MIN=$v ;; kill_switch) M_KILL=$v ;; artifact_url) M_URL=$v ;; artifact_sha256) M_SHA=$v ;; artifact_size) M_SIZE=$v ;; release_notes_url) M_NOTES=$v ;; generated_at) M_GEN=$v ;; expires_at) M_EXPIRES=$v ;; withdrawn_versions) M_WITHDRAWN=$v ;; esac done </dev/null || [ ! -w "$SHARE" ] || [ ! -w "$BIN" ]; then trefuse 7 'cannot write to %s (read-only or no permission). Use --prefix with a directory you own. Nothing was installed.' "$PREFIX" fi take_lock STAGE=$SHARE/.staging.$$ rm -rf "$STAGE" mkdir "$STAGE" || tdie 'cannot create %s' "$STAGE" chmod 700 "$STAGE" WORK=$STAGE/work mkdir "$WORK" # 1. Signed manifests. The live channel manifest is always consulted: it is the only # place a withdrawal, a kill switch or a raised minimum can appear, so even a pinned # version is checked against it. Only an offline --from install may go on without it. live_url=$MANIFEST_BASE/$CHANNEL/manifest.json LIVE_VERSION=""; LIVE_KILL=""; LIVE_MIN=""; LIVE_WITHDRAWN="" if [ -n "$FROM_FILE" ]; then [ -f "$FROM_FILE" ] || refuse 2 "--from: $FROM_FILE is not a file" FROM_DIR=$(cd "$(dirname "$FROM_FILE")" && pwd) cp "$FROM_FILE" "$WORK/from-envelope.json" verify_envelope "$WORK/from-envelope.json" "$WORK/from-manifest.json" say "Manifest signature verified (key $ENV_KEY), offline install from $FROM_FILE." read_manifest "$WORK/from-manifest.json" check_entry VERSION=$M_VERSION; URL=$M_URL; WANT_SHA=$M_SHA; WANT_SIZE=$M_SIZE; NOTES=$M_NOTES [ -z "$WANT_VERSION" ] || [ "$WANT_VERSION" = "$VERSION" ] || refuse 4 "$FROM_FILE is for version $VERSION, not $WANT_VERSION. Nothing was installed." if [ "$M_KILL" = true ]; then refuse 4 "version $VERSION in $FROM_FILE is marked withdrawn (kill switch). Nothing was installed." fi # The supplied manifest's own withdrawals and minimum apply even when the live one is unreachable. take_live check_live_allows "$VERSION" LIVE_VERSION=""; LIVE_KILL=""; LIVE_MIN=""; LIVE_WITHDRAWN="" # The live manifest is checked when it can be reached; otherwise say so loudly. fetch_envelope "$live_url" "$WORK/envelope.json" && live_rc=0 || live_rc=$? if [ "$live_rc" = 0 ]; then UNPINNED_STATUS=22 verify_envelope "$WORK/envelope.json" "$WORK/manifest.json" UNPINNED_STATUS=4 read_manifest "$WORK/manifest.json" lenient check_replay_floor check_not_expired take_live check_live_allows "$VERSION" tsay 'The live %s manifest does not withdraw %s.' "$CHANNEL" "$VERSION" else terr 'WARNING: %s could not be reached (offline?), so it was NOT checked whether version %s has been withdrawn. Withdrawal not checked.' "$live_url" "$VERSION" fi else tsay 'Checking %s channel (%s)...' "$CHANNEL" "$TARGET" fetch_envelope "$live_url" "$WORK/envelope.json" && rc=0 || rc=$? case $rc in 0) ;; 1) tsay 'No release is published yet on the %s channel (no update manifest at %s).' "$CHANNEL" "$live_url" tsay 'Nothing was installed. Check https://yadori.link/ for the first release.' exit 1 ;; 3) trefuse 3 'could not reach %s. Check your network connection. Nothing was installed.' "$live_url" ;; *) trefuse 3 'unexpected HTTP status %s from %s. Nothing was installed.' "$HTTP_CODE" "$live_url" ;; esac UNPINNED_STATUS=22 verify_envelope "$WORK/envelope.json" "$WORK/manifest.json" UNPINNED_STATUS=4 say "Manifest signature verified (key $ENV_KEY)." read_manifest "$WORK/manifest.json" lenient check_replay_floor check_not_expired take_live if [ -n "$WANT_VERSION" ] && [ "$WANT_VERSION" != "$LIVE_VERSION" ]; then # A pinned version: its signed per-version manifest proves integrity, the live # manifest decides whether it may still be installed. check_live_allows "$WANT_VERSION" snap_url=$MANIFEST_BASE/$WANT_VERSION/manifest.json fetch_envelope "$snap_url" "$WORK/snapshot-envelope.json" && rc=0 || rc=$? case $rc in 0) ;; 1) tdie 'version %s has no published manifest (%s). Nothing was installed.' "$WANT_VERSION" "$snap_url" ;; 3) trefuse 3 'could not reach %s. Check your network connection. Nothing was installed.' "$snap_url" ;; *) trefuse 3 'unexpected HTTP status %s from %s. Nothing was installed.' "$HTTP_CODE" "$snap_url" ;; esac verify_envelope "$WORK/snapshot-envelope.json" "$WORK/snapshot.json" read_manifest "$WORK/snapshot.json" [ "$M_VERSION" = "$WANT_VERSION" ] || refuse 4 "the manifest for $WANT_VERSION describes version $M_VERSION. Refusing. Nothing was installed." else read_manifest "$WORK/manifest.json" fi check_entry VERSION=$M_VERSION; URL=$M_URL; WANT_SHA=$M_SHA; WANT_SIZE=$M_SIZE; NOTES=$M_NOTES if [ "$M_KILL" = true ]; then refuse 4 "version $VERSION on the $CHANNEL channel has been withdrawn (kill switch). Nothing was installed." fi check_live_allows "$VERSION" fi installed=$(receipt_get version || true) if [ -n "$installed" ]; then rel=$(semver_cmp "$VERSION" "$installed") if [ "$rel" = 0 ] && [ "$FORCE" != 1 ] && [ "$(receipt_get channel)" = "$CHANNEL" ]; then tsay 'YadoriLink %s is already installed and up to date. (--force reinstalls.)' "$installed" setup_path return 0 fi if [ "$rel" = -1 ]; then trefuse 6 'installed version %s is newer than %s on the %s channel; not downgrading. Nothing was changed.' "$installed" "$VERSION" "$CHANNEL" fi fi for name in yadorilink yadorilink-daemon; do if [ -e "$BIN/$name" ] || [ -L "$BIN/$name" ]; then is_ours "$BIN/$name" || tdie '%s/%s already exists and was not installed by this script. Remove it or choose another --prefix. Nothing was changed.' "$BIN" "$name" fi done if [ -n "$installed" ]; then confirm "$(tf 'Upgrade YadoriLink %s -> %s under %s?' "$installed" "$VERSION" "$PREFIX")" || tdie 'cancelled' else confirm "$(tf 'Install YadoriLink %s under %s?' "$VERSION" "$PREFIX")" || tdie 'cancelled' fi # 2. Bundle download and hashes against the signed manifest. zip_name=${URL##*/} ZIP=$STAGE/$zip_name if [ -n "$FROM_FILE" ]; then [ -f "$FROM_DIR/$zip_name" ] || refuse 2 "--from needs the engine bundle $zip_name next to $FROM_FILE. Nothing was installed." tsay 'Using %s (%s) from %s...' "$zip_name" "$VERSION" "$FROM_DIR" cp "$FROM_DIR/$zip_name" "$ZIP" || tdie 'cannot copy %s/%s. Nothing was installed.' "$FROM_DIR" "$zip_name" else tsay 'Downloading %s (%s)...' "$zip_name" "$VERSION" curl -sSL -f --max-redirs 5 --proto "$CURL_PROTO" --proto-redir "$CURL_PROTO" --tlsv1.2 --connect-timeout 15 --max-time 900 \ --max-filesize "$WANT_SIZE" -o "$ZIP" "$URL" || trefuse 3 'download failed or was interrupted (%s). Nothing was installed.' "$URL" fi [ "$(file_size "$ZIP")" = "$WANT_SIZE" ] || refuse 4 "downloaded bundle has the wrong size (expected $WANT_SIZE bytes). Refusing. Nothing was installed." [ "$(sha256_of "$ZIP")" = "$WANT_SHA" ] || refuse 4 "downloaded bundle SHA-256 does not match the signed manifest. Refusing. Nothing was installed." # 3. Unpack privately and verify the contents. EXTRACT=$STAGE/x mkdir "$EXTRACT" if have unzip; then names=$(unzip -Z1 "$ZIP") || refuse 4 "bundle is not a valid zip" else names=$(bsdtar -tf "$ZIP") || refuse 4 "bundle is not a valid zip" fi printf '%s\n' "$names" | awk '/^\// || /\\/ || /(^|\/)\.\.(\/|$)/ { bad = 1 } END { exit bad }' || refuse 4 "bundle contains unsafe paths. Refusing. Nothing was installed." if have unzip; then unzip -q -o "$ZIP" -d "$EXTRACT" || tdie 'could not unpack the bundle' else bsdtar -xf "$ZIP" -C "$EXTRACT" || tdie 'could not unpack the bundle'; fi [ -z "$(find "$EXTRACT" -type l | head -n 1)" ] || refuse 4 "bundle contains symbolic links. Refusing. Nothing was installed." for f in SHA256SUMS engine-identity.json LICENSE SOURCE.txt THIRD-PARTY-LICENSES; do [ -f "$EXTRACT/$f" ] || refuse 4 "bundle is missing $f. Refusing. Nothing was installed." done # Every file except SHA256SUMS and engine-identity.json is listed with the # right hash, and nothing is unlisted. (cd "$EXTRACT" && find . -type f ! -name SHA256SUMS ! -name engine-identity.json | sed 's|^\./||' | LC_ALL=C sort) > "$WORK/actual.txt" sed -n 's/^[0-9a-f]\{64\} //p' "$EXTRACT/SHA256SUMS" | LC_ALL=C sort > "$WORK/listed.txt" cmp -s "$WORK/actual.txt" "$WORK/listed.txt" || refuse 4 "bundle contents differ from its SHA256SUMS. Refusing. Nothing was installed." [ "$(grep -c . "$EXTRACT/SHA256SUMS")" = "$(grep -c . "$WORK/listed.txt")" ] || refuse 4 "SHA256SUMS is malformed. Refusing. Nothing was installed." while IFS= read -r line; do h=${line%% *} p=${line#* } [ "$(sha256_of "$EXTRACT/$p")" = "$h" ] || refuse 4 "bundle file $p does not match SHA256SUMS. Refusing. Nothing was installed." done < "$EXTRACT/SHA256SUMS" ident=$EXTRACT/engine-identity.json id_bundle=$(id_field "$ident" bundle_sha256) || refuse 4 "engine-identity.json is malformed (bundle_sha256)" [ "$(sha256_of "$EXTRACT/SHA256SUMS")" = "$id_bundle" ] || refuse 4 "engine identity does not cover this bundle's SHA256SUMS. Refusing. Nothing was installed." id_chan=$(id_field "$ident" channel) || refuse 4 "engine-identity.json is malformed (channel)" [ "$id_chan" = "$IDENTITY_CHANNEL" ] || refuse 4 "bundle is for channel '$id_chan', expected '$IDENTITY_CHANNEL'. Refusing. Nothing was installed." id_api=$(id_field "$ident" engine_api_version) || refuse 4 "engine-identity.json is malformed (engine_api_version)" id_commit=$(id_field "$ident" public_commit_sha) || refuse 4 "engine-identity.json is malformed (public_commit_sha)" case $id_commit in *[!0-9a-f]*|"") refuse 4 "engine identity has an invalid commit" ;; esac [ ${#id_commit} = 40 ] || refuse 4 "engine identity has an invalid commit" grep -qF "$id_commit" "$EXTRACT/SOURCE.txt" || refuse 4 "SOURCE.txt does not name the engine identity's source commit. Refusing. Nothing was installed." id_asset=$(id_field "$ident" asset_name) || refuse 4 "engine-identity.json is malformed (source.asset_name)" [ "$id_asset" = "$zip_name" ] || refuse 4 "engine identity names asset '$id_asset' but '$zip_name' was downloaded. Refusing. Nothing was installed." [ "$zip_name" = "yadorilink-engine-$id_chan-$TARGET.zip" ] || refuse 4 "bundle '$zip_name' is not the $TARGET engine bundle for channel $id_chan. Refusing. Nothing was installed." id_daemon=$(id_field "$ident" artifact_sha256) || refuse 4 "engine-identity.json is malformed (artifact_sha256)" [ "$(sha256_of "$EXTRACT/${BIN_SUBDIR}yadorilink-daemon")" = "$id_daemon" ] || refuse 4 "daemon does not match the engine identity. Refusing. Nothing was installed." for name in yadorilink yadorilink-daemon; do [ -f "$EXTRACT/$BIN_SUBDIR$name" ] || refuse 4 "bundle lacks $BIN_SUBDIR$name. Refusing. Nothing was installed." chmod 755 "$EXTRACT/$BIN_SUBDIR$name" done "$EXTRACT/${BIN_SUBDIR}yadorilink" --version >/dev/null 2>&1 || tdie 'the new yadorilink does not run on this system (--version failed). Nothing was changed.' # The installer in the bundle was listed in SHA256SUMS and hashed above with everything else, so # this is the hash the signed chain vouches for. KEPT_INSTALLER_SHA="" if [ -f "$EXTRACT/install.sh" ]; then KEPT_INSTALLER_SHA=$(sha256_of "$EXTRACT/install.sh"); fi # The data already on this machine must be readable by what is about to be installed. Only the # bundle's own daemon knows its state versions, and the database is the only record of the # data's version, so it is asked to read it (read-only; nothing is created or changed). # Status 2 is a bundle from before the check existed: its daemon refuses newer data itself. compat_rc=0 compat_out=$("$EXTRACT/${BIN_SUBDIR}yadorilink-daemon" --check-data-compat 2>&1 >/dev/null) || compat_rc=$? case $compat_rc in 0) [ -z "$compat_out" ] || printf 'yadorilink-install: %s\n' "$compat_out" >&2 ;; 2) say "note: this release does not check the existing data before installing" ;; 20) refuse 20 "$compat_out Nothing was installed." ;; 21) refuse 21 "$compat_out Nothing was installed." ;; *) refuse 21 "the data check of the new daemon failed unexpectedly (status $compat_rc): $compat_out Nothing was installed." ;; esac # The daemon recognises a script install by this file beside its binaries # (crates/yadorilink-daemon/src/update/install_source.rs). It lives in the # version tree, so it follows the binaries and is removed with them. Written # after verification; failing to write it aborts before anything is switched. printf '{"source":"script","version":"%s","channel":"%s"}\n' "$VERSION" "$CHANNEL" > "$EXTRACT/${BIN_SUBDIR}install-receipt.json" || tdie 'could not write the install receipt beside the binaries. Nothing was changed.' # 4. Activate: stop the current install's daemon, move the verified tree into place, then switch the links. stop_old_daemon VDIR=versions/$VERSION.$(date +%s).$$ mkdir -p "$SHARE/versions" mv "$EXTRACT" "$SHARE/$VDIR" || die "cannot move the verified bundle into place. Nothing was changed." old_dir=$(receipt_get dir || true) old_version=$(receipt_get version || true) old_prev=$(receipt_get previous_dir || true) # Switch the daemon link, then the CLI link; if a later step fails, put # the earlier links back. daemon_old="" if [ -L "$BIN/yadorilink-daemon" ]; then daemon_old=$(readlink "$BIN/yadorilink-daemon"); fi cli_old="" if [ -L "$BIN/yadorilink" ]; then cli_old=$(readlink "$BIN/yadorilink"); fi restore_links() { if [ -n "$daemon_old" ]; then set_link "$daemon_old" "$BIN/yadorilink-daemon" || true else rm -f "$BIN/yadorilink-daemon"; fi if [ -n "$cli_old" ]; then set_link "$cli_old" "$BIN/yadorilink" || true else rm -f "$BIN/yadorilink"; fi } set_link "$SHARE/$VDIR/${BIN_SUBDIR}yadorilink-daemon" "$BIN/yadorilink-daemon" || { rm -rf "${SHARE:?}/$VDIR"; tdie 'could not switch %s/yadorilink-daemon. The previous install was kept.' "$BIN"; } if ! set_link "$SHARE/$VDIR/${BIN_SUBDIR}yadorilink" "$BIN/yadorilink"; then restore_links rm -rf "${SHARE:?}/$VDIR" tdie 'could not switch %s/yadorilink. The previous install was restored.' "$BIN" fi # The receipt is what uninstall and the next upgrade trust. If it cannot be # written, the links go back and the new tree is removed. if ! write_receipt; then rm -f "$SHARE/receipt.new.$$" restore_links rm -rf "${SHARE:?}/$VDIR" rmdir "$SHARE/versions" 2>/dev/null || true tdie 'could not write the install receipt. The previous install was restored.' fi # Drop only the version tree the previous receipt recorded as "previous" # (it is now two generations old). Nothing else under versions/ is ours to # delete. case $old_prev in "$SHARE"/versions/?*) if [ "$old_prev" != "$SHARE/$VDIR" ] && [ "$old_prev" != "$old_dir" ] && [ -d "$old_prev" ] && [ ! -L "$old_prev" ]; then rm -rf "$old_prev" fi ;; esac say "" tsay 'Installed YadoriLink %s (%s, %s) in %s.' "$VERSION" "$CHANNEL" "$TARGET" "$PREFIX" [ -z "$NOTES" ] || tsay 'Release notes: %s' "$NOTES" setup_path say "" start_daemon_or_say_how say "" tsay 'Next: sign in with' say " yadorilink login" tsay 'Upgrade later by running the installer again. Remove with: install.sh --uninstall' } # Starts the daemon of the install just made, so the only step left is signing in. A start that # does not take is not an install failure: the command that starts it is said instead. start_daemon_or_say_how() { [ "$START_DAEMON" = 1 ] || { tsay 'The daemon was not started (--no-start). Start it with: yadorilink daemon start'; return 0; } if "$BIN/yadorilink" daemon start >/dev/null 2>&1; then if [ "${STOPPED_DAEMON:-0}" = 0 ]; then tsay 'YadoriLink is running.' else tsay 'The old daemon was stopped; the new one is running.'; fi else tsay 'The YadoriLink daemon could not be started. Start it with: yadorilink daemon start' fi } setup_path() { case ":$PATH:" in *":$BIN:"*) return 0 ;; esac if [ "$MODIFY_PATH" = 1 ]; then profile=$(path_profile) if ! grep -qF "$PATH_BEGIN" "$profile" 2>/dev/null; then { printf '\n%s\n' "$PATH_BEGIN" # Single-quote the directory so $, backticks and quotes in it stay literal. quoted=$(printf '%s' "$BIN" | sed "s/'/'\\\\''/g") # shellcheck disable=SC2016 printf "export PATH='%s':\"\$PATH\"\n" "$quoted" printf '%s\n' "$PATH_END" } >> "$profile" fi PROFILE_VALUE=$profile awk '/^path_profile=/ { print "path_profile=" ENVIRON["PROFILE_VALUE"]; next } { print }' "$RECEIPT" > "$RECEIPT.new.$$" && mv -f "$RECEIPT.new.$$" "$RECEIPT" tsay 'Added %s to your PATH in %s. Open a new terminal, or run:' "$BIN" "$profile" else tsay '%s is not on your PATH. Add it, for example:' "$BIN" fi say " export PATH=\"$BIN:\$PATH\"" } # do_rollback: switch back to the previous version this script installed. No network and no # manifest: that tree was verified when it was installed, and here its files are checked against its # own SHA256SUMS again (a changed file refuses). Nothing is deleted; the receipt's current and # previous entries trade places, so a second rollback goes forward again. do_rollback() { [ -f "$RECEIPT" ] || refuse 1 "nothing to roll back: no install receipt under $SHARE (this is not a script install). Nothing was changed." detect_target check_not_managed_elsewhere # Everything below is read and checked under the lock, so an upgrade cannot replace or prune # the trees between the checks and the switch. take_lock cur_dir=$(receipt_get dir || true) prev_dir=$(receipt_get previous_dir || true) cur_version=$(receipt_get version || true) prev_version=$(receipt_get previous_version || true) if [ -z "$prev_dir" ] || [ -z "$prev_version" ]; then refuse 1 "there is no previous version to go back to (the receipt records none). Nothing was changed." fi # Exactly one name under versions/: no separator, no dot components, and neither versions/ nor the # tree is a link, so the tree cannot be elsewhere. case $prev_dir in "$SHARE"/versions/?*) ;; *) refuse 1 "the receipt's previous version is not under $SHARE/versions. Nothing was changed." ;; esac prev_name=${prev_dir#"$SHARE"/versions/} case $prev_name in */*|.|..) refuse 1 "the receipt's previous version is not a plain directory under $SHARE/versions. Nothing was changed." ;; esac { [ -d "$SHARE/versions" ] && [ ! -L "$SHARE/versions" ]; } || refuse 1 "$SHARE/versions is not a plain directory. Nothing was changed." { [ -d "$prev_dir" ] && [ ! -L "$prev_dir" ]; } || refuse 1 "the previous version's files ($prev_dir) are gone. Nothing was changed. Run the one-line installer to install a version." [ "$prev_dir" != "$cur_dir" ] || refuse 1 "the previous and the current version are the same tree. Nothing was changed." [ -f "$prev_dir/SHA256SUMS" ] || refuse 4 "the previous version has no SHA256SUMS to check it by. Refusing. Nothing was changed." # Every file the previous tree's SHA256SUMS lists must still match it, and the programs must be there. # The list must be whole: tied to the engine identity the install verified, naming both programs, # and every file it lists must still match. A truncated list cannot vouch for anything. [ -f "$prev_dir/engine-identity.json" ] || refuse 4 "the previous version has no engine identity to check its file list by. Refusing. Nothing was changed." id_sums=$(id_field "$prev_dir/engine-identity.json" bundle_sha256) || refuse 4 "the previous version's engine identity is malformed. Refusing. Nothing was changed." [ "$(sha256_of "$prev_dir/SHA256SUMS")" = "$id_sums" ] || refuse 4 "the previous version's SHA256SUMS no longer matches its engine identity. Refusing. Nothing was changed." bin_sub="" [ -f "$prev_dir/yadorilink" ] || { [ -f "$prev_dir/bin/yadorilink" ] && bin_sub=bin/; } for prog in yadorilink yadorilink-daemon; do grep -q "^[0-9a-f]\{64\} ${bin_sub}${prog}\$" "$prev_dir/SHA256SUMS" || refuse 4 "the previous version's file list does not name ${bin_sub}${prog}. Refusing. Nothing was changed." done checked=0 while IFS= read -r line; do h=${line%% *} p=${line#* } [ "$(sha256_of "$prev_dir/$p" 2>/dev/null)" = "$h" ] || refuse 4 "the previous version's file $p no longer matches its SHA256SUMS. Refusing. Nothing was changed." checked=$((checked + 1)) done < "$prev_dir/SHA256SUMS" [ "$checked" -gt 0 ] || refuse 4 "the previous version's SHA256SUMS lists nothing. Refusing. Nothing was changed." { [ -f "$prev_dir/${bin_sub}yadorilink" ] && [ -f "$prev_dir/${bin_sub}yadorilink-daemon" ]; } || refuse 4 "the previous version lacks its programs. Refusing. Nothing was changed." stop_old_daemon daemon_old="" if [ -L "$BIN/yadorilink-daemon" ]; then daemon_old=$(readlink "$BIN/yadorilink-daemon"); fi cli_old="" if [ -L "$BIN/yadorilink" ]; then cli_old=$(readlink "$BIN/yadorilink"); fi set_link "$prev_dir/${bin_sub}yadorilink-daemon" "$BIN/yadorilink-daemon" || tdie 'could not switch %s/yadorilink-daemon. Nothing was changed.' "$BIN" if ! set_link "$prev_dir/${bin_sub}yadorilink" "$BIN/yadorilink"; then if [ -n "$daemon_old" ]; then set_link "$daemon_old" "$BIN/yadorilink-daemon" || true; fi tdie 'could not switch %s/yadorilink. The current version was restored.' "$BIN" fi # The values go in through the environment so a path with awk-special characters stays literal. if ! { YL_V=$prev_version YL_D=$prev_dir YL_PD=$cur_dir YL_PV=$cur_version \ awk 'BEGIN { FS = OFS = "=" } $1 == "version" { print "version=" ENVIRON["YL_V"]; next } $1 == "dir" { print "dir=" ENVIRON["YL_D"]; next } $1 == "previous_dir" { print "previous_dir=" ENVIRON["YL_PD"]; next } $1 == "previous_version" { print "previous_version=" ENVIRON["YL_PV"]; next } { print }' "$RECEIPT" > "$SHARE/receipt.new.$$" && mv -f "$SHARE/receipt.new.$$" "$RECEIPT" }; then rm -f "$SHARE/receipt.new.$$" if [ -n "$daemon_old" ]; then set_link "$daemon_old" "$BIN/yadorilink-daemon" || true; fi if [ -n "$cli_old" ]; then set_link "$cli_old" "$BIN/yadorilink" || true; fi tdie 'could not write the install receipt. The current version was restored.' fi tsay 'Rolled back to YadoriLink %s (from %s).' "$prev_version" "$cur_version" if [ "${STOPPED_DAEMON:-0}" = 1 ]; then tsay 'The running daemon was stopped. Start it again: yadorilink daemon start' else tsay 'No daemon was running. Start it with: yadorilink daemon start'; fi } main() { setup "$@" trap cleanup EXIT trap 'exit 130' INT trap 'exit 143' TERM trap 'exit 129' HUP case $ACTION in install) do_install ;; uninstall) do_uninstall ;; rollback) do_rollback ;; esac } main "$@"