#!/bin/sh # get.sh - install DIN with one line, and check what was downloaded first. # # curl -fsSL https://dinctrl.com/install | sh # curl -fsSL https://dinctrl.com/install | sh -s -- --agent cursor # curl -fsSL https://dinctrl.com/install | sh -s -- --path # # Every argument is handed to the release's own install.sh unchanged (see # `sh install.sh --help` in the archive: --prefix, --agent, --dry-run, # --no-plugin, --no-start, ...), except one that is this script's own: # # --path also add DIN's bin/ to PATH in your shell's profile file # (without it, the line to do that is printed and nothing is edited) # # WHAT IT DOES, in order, and it stops at the first thing that is not right: # # 1. Checks this is Linux x86-64 or a Mac (Apple silicon or Intel), the # builds published for this shell. A Mac installs the macOS archive when # the signed list carries one (DIN for macOS is signed with Apple's # Developer ID and notarised), and is told it is on request when not. # 2. Fetches SHA256SUMS from the download page's own directory # (https://dinctrl.com/download/), the same file the page links. The # archive's name, and so the version, is read out of that list: nothing # here guesses a version or a URL. # 3. Checks SHA256SUMS.minisig against the release key written into this # script: with minisign when it is installed, else with openssl (3.0 or # later, which most Linux boxes have). With neither, it stops and says # how to get one: the checksum alone comes from the same server as the # archive, so it proves nothing about who made the release. The signed # trusted comment must name the release, "DIN ", the # archive must be that version, and the version must be no older than # DIN_MIN_VERSION below: an older release, however well signed, is a # rollback and is refused. # 4. Downloads the archive and checks its sha256 against the list. # 5. Unpacks it and runs its install.sh. # # Any failed check: the temporary directory is deleted and nothing is installed. # No sudo, ever. DIN installs into your home directory (~/din by default). # # DIN_INSTALL_URL= points it at a different copy of the download page # (a mirror, or a test server). The signing key and the minimum version do # not change with it. # # The whole script is one function, called on the LAST line. `curl | sh` runs # what has arrived so far, so a connection that dies halfway must deliver # nothing runnable rather than half of an installer. # The minisign key every DIN release's SHA256SUMS is signed with. The same key # is in RELEASE-SIGNING.pub, README.md, QUICKSTART.md and on the download page. # Written in here, never fetched: a key that came from the server it vouches for # would vouch for whatever is on that server. DIN_RELEASE_PUBKEY='RWRYpMRMG6vJNhzPUkYbbwTX9ebqgd1rW50Rlkv+/BZ4uAPzfda0KTqK' # The oldest release this script installs: the release of the console that # serves it (a test holds it to VERSION). Every release's signature names its # version and date in the signed trusted comment ("DIN "); # a correctly signed but OLDER release, from a mirror or a stale origin, is # refused, so nobody can be rolled back to a build with a known bug. DIN_MIN_VERSION='1.0.42' din_get_main() { set -eu page="https://dinctrl.com/download" base="${DIN_INSTALL_URL:-https://dinctrl.com}" base="${base%/}" say() { printf '%s\n' "$*"; } step() { printf ' %s\n' "$*"; } die() { printf 'get.sh: %s\n' "$*" >&2; exit 1; } # ---- our one option, everything else passed through ------------------------ # # "$@" is rebuilt in place without --path, which is the only way to carry an # argument list with its quoting intact in POSIX sh (no arrays). The prefix # and --dry-run are only READ here, for the PATH line at the end. add_path=no dry=no prefix="${DIN_PREFIX:-$HOME/din}" want_prefix=no n=$# while [ "$n" -gt 0 ]; do a="$1"; shift; n=$((n - 1)) if [ "$want_prefix" = yes ]; then prefix="$a"; want_prefix=no; fi case "$a" in --path) add_path=yes; continue ;; --prefix) want_prefix=yes ;; --dry-run) dry=yes ;; esac set -- "$@" "$a" done # ---- 1. which build ---------------------------------------------------------- # The two refusals below are matched, line for line, by tools/windows-copy.txt: # edit them there too. os="$(uname -s 2>/dev/null || echo unknown)" arch="$(uname -m 2>/dev/null || echo unknown)" plat=linux case "$os" in Linux) ;; Darwin) plat=macos ;; MINGW*|MSYS*|CYGWIN*) die "This installer is for Linux and macOS." ;; *) die "there is no DIN build for $os. Published: Linux x86-64 and macOS (Apple silicon and Intel). Questions: support@dinctrl.com. See $page" ;; esac case "$arch" in x86_64|amd64) ;; arm64|aarch64) [ "$plat" = macos ] || die "there is no DIN build for Linux $arch yet, only Linux x86-64. See $page" ;; *) if [ "$plat" = macos ]; then die "there is no DIN build for a Mac with a $arch CPU, only Apple silicon and Intel. See $page"; fi die "there is no DIN build for Linux $arch yet, only Linux x86-64. See $page" ;; esac # ---- tools -------------------------------------------------------------------- if command -v curl >/dev/null 2>&1; then fetcher=curl elif command -v wget >/dev/null 2>&1; then fetcher=wget else die "neither curl nor wget is installed, so nothing can be downloaded" fi if command -v sha256sum >/dev/null 2>&1; then hasher="sha256sum" elif command -v shasum >/dev/null 2>&1; then hasher="shasum -a 256" else die "no sha256sum (or shasum) is installed, so the download could not be checked. Nothing was downloaded." fi command -v tar >/dev/null 2>&1 && command -v gzip >/dev/null 2>&1 \ || die "tar and gzip are needed to unpack the download" # An https origin is held to https for every hop, redirects included: a # redirect to plain http would make the checksum list forgeable in transit, # and the checksum is the one check that always runs. fetch() { case "$base" in https://*) if [ "$fetcher" = curl ]; then curl -fsSL --proto '=https' --proto-redir '=https' --tlsv1.2 -o "$2" "$1" else wget -q --https-only -O "$2" "$1" fi ;; *) if [ "$fetcher" = curl ]; then curl -fsSL -o "$2" "$1"; else wget -q -O "$2" "$1"; fi ;; esac } tmp="$(mktemp -d "${TMPDIR:-/tmp}/din-get.XXXXXX")" || die "cannot make a temporary directory" # Deleted on every way out: success, a failed check, or Ctrl-C. trap 'rm -rf "$tmp"' EXIT trap 'exit 130' INT TERM HUP say "DIN one-line install" step "from $base/download/" # ---- 2. the checksum list ---------------------------------------------------- fetch "$base/download/SHA256SUMS" "$tmp/SHA256SUMS" \ || die "could not download $base/download/SHA256SUMS. Nothing was installed." # A release with no macOS archive tells a Mac so now, before it is asked to # install a verifier for a download that is not there. Only a refusal is read # out of the list before its signature is checked; nothing is chosen from it. if [ "$plat" = macos ] && ! grep -qE '^[0-9a-f]{64} din-[0-9][0-9A-Za-z.+-]*-macos-universal\.tar\.gz$' "$tmp/SHA256SUMS"; then die "DIN for macOS is available on request: this release has no macOS download. Write to support@dinctrl.com for the macOS build. Nothing was installed. See $page for what is published." fi # ---- 3. the signature over it ------------------------------------------------- # # Checked BEFORE a single name is read out of the list, so a forged list never # gets to choose what is downloaded next. With a verifier on the box a missing # signature is a failure, not a skip: a release we sign that arrives unsigned # is exactly what a tampered mirror looks like. # # The verifier is minisign when it is installed, and otherwise openssl, which # most Linux boxes already have. openssl is used only after it has passed a # known-answer test (RFC 8032 and RFC 7693 vectors written in here): an openssl # too old to do Ed25519 must read as "cannot check", never as "bad signature", # and never as "good signature". "Cannot check" is a refusal too: a mirror # that can forge SHA256SUMS can forge the archive to match it, so without the # signature nothing here would tell a real release from a planted one. sigbad() { die "SIGNATURE MISMATCH: the signature did not verify; SHA256SUMS is not signed by the DIN release key $DIN_RELEASE_PUBKEY. Nothing was installed." } # ossl_ed25519 MSG SIG KEY.der: 0 only when openssl says this exact Ed25519 # signature is good. Both the exit code and the words are required, so an # openssl that exits 0 without verifying cannot pass for one that did. ossl_ed25519() { o="$(openssl pkeyutl -verify -pubin -keyform DER -inkey "$3" -rawin -in "$1" -sigfile "$2" 2>/dev/null)" || return 1 [ "$o" = "Signature Verified Successfully" ] } b64d() { openssl base64 -d -A 2>/dev/null; } nbytes() { wc -c < "$1" | tr -d ' '; } # bytes FILE SKIP COUNT: COUNT bytes of FILE from offset SKIP, to stdout. bytes() { dd if="$1" bs=1 skip="$2" count="$3" 2>/dev/null; } ossl_works() { v="$tmp/kat"; mkdir "$v" || return 1 # RFC 8032 section 7.1, TEST 2: the one-byte message "r". One base64 blob # of the key as DER (44 bytes) then the signature (64): 108 bytes, so no # padding. printf 'r' > "$v/m" printf '%s' 'MCowBQYDK2VwAyEAPUAXw+hDiVqStwqnTRt+vJyYLM8uxJaMwM1V8Sr0ZgySoAmp8NTKuHIOggtfZCVAorJ7VBZQP4+zdiIj69tp2ghaweQ+FZluRY82E9DxHYw4ey6utDAq7rANKRYSuwwA' | b64d > "$v/ks" [ "$(nbytes "$v/ks")" = 108 ] || return 1 bytes "$v/ks" 0 44 > "$v/k" bytes "$v/ks" 44 64 > "$v/s" ossl_ed25519 "$v/m" "$v/s" "$v/k" || return 1 # And it must say no to the same signature over a different message. printf 's' > "$v/m" ossl_ed25519 "$v/m" "$v/s" "$v/k" && return 1 # RFC 7693 appendix A: BLAKE2b-512("abc"). h="$(printf 'abc' | openssl dgst -blake2b512 2>/dev/null | sed 's/^.*= //')" [ "$h" = ba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d17d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923 ] } # The minisign format, checked in full by openssl: # line 1 untrusted comment: ... (not signed, not read) # line 2 base64 of alg(2) keyid(8) sig(64) alg "ED": sig is over BLAKE2b-512 # of the file (what minisign makes by default); "Ed": over the file # line 3 trusted comment: # line 4 base64 of the global sig(64), over sig(64) followed by # The key is base64 of "Ed" keyid(8) pubkey(32). Its 32 bytes are wrapped in # the fixed 12-byte DER header of an Ed25519 SubjectPublicKeyInfo for openssl. # A .minisig saved with CRLF line ends (an editor or a mirror on Windows) is # the same signature: minisign drops the CR, so every CR goes before a line is # read, the trusted comment's included, which is signed without one. ossl_minisign() { d="$tmp/sig"; mkdir "$d" || return 1 tr -d '\r' < "$2" > "$d/ms" || return 1 sed -n 1p "$d/ms" | grep -q '^untrusted comment: ' || return 1 sed -n 3p "$d/ms" | grep -q '^trusted comment: ' || return 1 printf '%s' "$DIN_RELEASE_PUBKEY" | b64d > "$d/pk" sed -n 2p "$d/ms" | tr -d '\n' | b64d > "$d/sig" sed -n 4p "$d/ms" | tr -d '\n' | b64d > "$d/gsig" [ "$(nbytes "$d/pk")" = 42 ] && [ "$(nbytes "$d/sig")" = 74 ] && [ "$(nbytes "$d/gsig")" = 64 ] || return 1 [ "$(bytes "$d/pk" 0 2)" = Ed ] || return 1 bytes "$d/pk" 2 8 > "$d/pkid" bytes "$d/sig" 2 8 > "$d/sigid" cmp -s "$d/pkid" "$d/sigid" || return 1 { printf '\060\052\060\005\006\003\053\145\160\003\041\000'; bytes "$d/pk" 10 32; } > "$d/key" [ "$(nbytes "$d/key")" = 44 ] || return 1 bytes "$d/sig" 10 64 > "$d/s" case "$(bytes "$d/sig" 0 2)" in ED) openssl dgst -blake2b512 -binary < "$1" > "$d/m" 2>/dev/null || return 1 [ "$(nbytes "$d/m")" = 64 ] || return 1 ;; Ed) cp "$1" "$d/m" ;; *) return 1 ;; esac ossl_ed25519 "$d/m" "$d/s" "$d/key" || return 1 { cat "$d/s"; sed -n 3p "$d/ms" | sed 's/^trusted comment: //' | tr -d '\n'; } > "$d/g" ossl_ed25519 "$d/g" "$d/gsig" "$d/key" } verifier=none if command -v minisign >/dev/null 2>&1; then verifier=minisign elif command -v openssl >/dev/null 2>&1 && ossl_works; then verifier=openssl fi if [ "$verifier" = none ]; then # Named, so the person sees WHY this openssl did not count (1.1.1 is still # the system one on Ubuntu 20.04, Debian 11, RHEL 8 and Amazon Linux 2). have="no openssl is installed" if command -v openssl >/dev/null 2>&1; then have="the openssl here ($(openssl version 2>/dev/null | head -n 1)) cannot check Ed25519 signatures" fi # A Mac's own openssl is LibreSSL, which cannot do Ed25519, so on a Mac the # way out is named: Homebrew's minisign. mac_hint=""; [ "$plat" = macos ] && mac_hint=" On a Mac: brew install minisign." die "SIGNATURE NOT CHECKED, so nothing was installed: minisign is not installed and $have. Either way out works: install minisign (https://jedisct1.github.io/minisign/, or your package manager's minisign package), or run this on a machine with OpenSSL 3 or later. Then run the same line again.$mac_hint" fi fetch "$base/download/SHA256SUMS.minisig" "$tmp/SHA256SUMS.minisig" \ || die "the release has no signature at $base/download/SHA256SUMS.minisig, and $verifier is here to check one. Nothing was installed." if [ "$verifier" = minisign ]; then minisign -Vq -m "$tmp/SHA256SUMS" -x "$tmp/SHA256SUMS.minisig" -P "$DIN_RELEASE_PUBKEY" >/dev/null 2>&1 || sigbad else ossl_minisign "$tmp/SHA256SUMS" "$tmp/SHA256SUMS.minisig" || sigbad fi step "ok signature: SHA256SUMS is signed by the DIN release key (checked with $verifier)" # ---- which release the signature names ---------------------------------------- # # The trusted comment is covered by the signature just checked (by minisign, # or by the second Ed25519 check in ossl_minisign), so it is read only now, # CR-stripped as both verifiers read it. It must be exactly # "DIN ", and must be at least DIN_MIN_VERSION: # an older SHA256SUMS signed by the same key is a rollback, not a release. tc="$(tr -d '\r' < "$tmp/SHA256SUMS.minisig" | sed -n 3p | sed 's/^trusted comment: //')" sv="$(printf '%s\n' "$tc" | sed -n 's/^DIN \([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\) \([0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}\)$/\1/p')" sd="$(printf '%s\n' "$tc" | sed -n 's/^DIN \([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\) \([0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}\)$/\2/p')" [ -n "$sv" ] && [ -n "$sd" ] \ || die "ROLLBACK REFUSED: the signature names '$tc', not a release ('DIN '), so it is older than DIN $DIN_MIN_VERSION or not a DIN release. Nothing was installed." # ver_ge A B: 0 when version A (x.y.z) is B or newer, compared as numbers. ver_ge() { a1="${1%%.*}"; ar="${1#*.}"; a2="${ar%%.*}"; a3="${ar#*.}" b1="${2%%.*}"; br="${2#*.}"; b2="${br%%.*}"; b3="${br#*.}" [ "$a1" -ne "$b1" ] && { [ "$a1" -gt "$b1" ]; return; } [ "$a2" -ne "$b2" ] && { [ "$a2" -gt "$b2" ]; return; } [ "$a3" -ge "$b3" ] } ver_ge "$sv" "$DIN_MIN_VERSION" \ || die "ROLLBACK REFUSED: the signed release is DIN $sv, older than DIN $DIN_MIN_VERSION, the oldest this script installs. Nothing was installed." step "ok release: DIN $sv, signed $sd" # ---- which archive ------------------------------------------------------------- # # Exactly one line for this platform (din--linux-x86_64.tar.gz, or on a # Mac din--macos-universal.tar.gz, which carries both Mac CPUs), in exactly # the shape package-release.sh writes. The # shape check is what keeps a line from the list from being a path: no slash, # no dot-dot, no space can pass it. if [ "$plat" = macos ]; then sfx=macos-universal; what="macOS" lines="$(grep -E '^[0-9a-f]{64} din-[0-9][0-9A-Za-z.+-]*-macos-universal\.tar\.gz$' "$tmp/SHA256SUMS" || true)" else sfx=linux-x86_64; what="Linux x86-64" lines="$(grep -E '^[0-9a-f]{64} din-[0-9][0-9A-Za-z.+-]*-linux-x86_64\.tar\.gz$' "$tmp/SHA256SUMS" || true)" fi [ -n "$lines" ] || die "SHA256SUMS lists no $what archive. Nothing was installed." [ "$(printf '%s\n' "$lines" | wc -l | tr -d ' ')" = 1 ] \ || die "SHA256SUMS lists more than one $what archive, so which to install is not clear. Nothing was installed." want="${lines%% *}" name="${lines#* }" stem="${name%-$sfx.tar.gz}" [ "$stem" = "din-$sv" ] \ || die "$name is not the release the signature names (DIN $sv). Nothing was installed." step "found $name" # ---- 4. the archive, and its checksum ------------------------------------------ fetch "$base/download/$name" "$tmp/$name" \ || die "could not download $base/download/$name. Nothing was installed." got="$($hasher "$tmp/$name" | cut -d' ' -f1)" [ "$got" = "$want" ] \ || die "CHECKSUM MISMATCH on $name: SHA256SUMS says $want, the download is $got. Nothing was installed." step "ok checksum: $name matches SHA256SUMS" # ---- 5. unpack and install -------------------------------------------------------- mkdir "$tmp/x" ( cd "$tmp/x" && gzip -dc "$tmp/$name" | tar xf - ) \ || die "$name did not unpack. Nothing was installed." [ -f "$tmp/x/$stem/install.sh" ] \ || die "$name has no $stem/install.sh. Nothing was installed." step "ok unpacked $stem/" say # The installer asks which agent to wire when a person is at a terminal, and # it reads the answer from stdin. Under `curl | sh` stdin is THIS SCRIPT, so # it is handed the terminal instead when there is one, and /dev/null when # there is not (a CI job gets the installer's non-interactive default). The # probe is a subshell because a failed redirection on a special builtin # (`:`, `exec`) ends a POSIX shell outright instead of returning non-zero. # DIN_GET_SH=1: this script prints the PATH line at the end, so install.sh # leaves its own out (CLI-9: it was said twice). rc=0 if (exec /dev/null; then DIN_GET_SH=1 sh "$tmp/x/$stem/install.sh" "$@" &2; exit "$rc"; } # ---- PATH --------------------------------------------------------------------------- bin="$prefix/bin" line="export PATH=\"$bin:\$PATH\"" say if [ "$add_path" = no ] || [ "$dry" = yes ]; then say "For this shell:" say " $line" if [ "$add_path" = yes ]; then say "(--path with --dry-run: no profile file was edited)" else say "To add it to your shell's profile for every new shell, run this again with --path." fi return 0 fi case "${SHELL:-}" in */zsh) profile="$HOME/.zshrc" ;; */bash) profile="$HOME/.bashrc" ;; *) profile="$HOME/.profile" ;; esac if [ -f "$profile" ] && grep -qF "$line" "$profile"; then say "PATH: $profile already puts $bin on PATH. Not edited." else printf '\n# added by the DIN installer (dinctrl.com/install --path)\n%s\n' "$line" >> "$profile" \ || die "could not write to $profile. DIN is installed; add this line yourself: $line" say "PATH: added $bin to $profile. New shells have it; for this one:" fi say " $line" } din_get_main "$@"