#!/usr/bin/env bash # Install mcode for this user. No sudo: everything goes under your home folder. # # curl -fsSL https://code.munir.sh | bash the desktop app # curl -fsSL https://code.munir.sh/server | bash mcode-server, for a machine that stays on # # Running it again installs the newest release over the one that is there. # # Options go after `bash -s --`, or in the environment: # # --server MCODE_SERVER=1 mcode-server instead of the desktop app # --version X MCODE_VERSION=X release X instead of the newest # --dry-run MCODE_DRY_RUN=1 say what would be done and do none of it # --uninstall MCODE_UNINSTALL=1 remove it; sessions and settings are left # --no-service MCODE_NO_SERVICE=1 server: put the program in place and stop there # --port N, --local-only, --data DIR server: handed on to `mcode-server install` # # curl -fsSL https://code.munir.sh | bash -s -- --dry-run # # What it trusts. A release is described by a small manifest, stable.json, # signed with the mcode release key (Ed25519). The public half of that key is # in this file. The manifest is checked against it when the openssl here can # do that (3.0 or newer), and the script says whether it was. The download is # then checked against the size and SHA-256 the manifest gives. Anything that # does not match is refused and nothing is installed. if [ -z "${BASH_VERSION:-}" ]; then echo "mcode install: this needs bash. Run it as: curl -fsSL https://code.munir.sh | bash" >&2 exit 1 fi set -euo pipefail # The public keys mcode releases are signed with (Ed25519, base64), one to a # line. These are the lines of release.pub in mcode's source. RELEASE_KEYS=" gO2R/ASqiWIkQQrgoyZKrHJ5FVKXslfpWr7gjMvTkZg= " # What is installed when nothing says otherwise. code.munir.sh/server sends # this same file with `server` on this line. DEFAULT_KIND=app BASE="${MCODE_BASE_URL:-https://code.munir.sh}" BASE="${BASE%/}" work="" staging="" say() { printf '%s\n' "$*"; } fail() { printf 'mcode install: %s\n' "$*" >&2 exit 1 } cleanup() { if [ -n "$work" ]; then rm -rf "$work"; fi if [ -n "$staging" ]; then rm -rf "$staging"; fi } usage() { cat <<'EOF' Install mcode for this user. Nothing needs sudo. curl -fsSL https://code.munir.sh | bash the desktop app curl -fsSL https://code.munir.sh/server | bash mcode-server curl -fsSL https://code.munir.sh | bash -s -- --dry-run --server mcode-server instead of the desktop app --version X release X instead of the newest --dry-run say what would be done and do none of it --uninstall remove it; sessions and settings are left --no-service server: put the program in place and stop there --port N, --local-only, --data DIR server: handed on to `mcode-server install` EOF } have() { command -v "$1" >/dev/null 2>&1; } # This machine, as a release names it: linux-x86_64, macos-aarch64, windows-x86_64. platform() { local os arch os="$(uname -s)" arch="$(uname -m)" case "$os" in Linux) os=linux ;; Darwin) os=macos # A shell running under Rosetta says x86_64 on an Apple silicon Mac. if [ "$arch" = x86_64 ] && [ "$(sysctl -n hw.optional.arm64 2>/dev/null || true)" = 1 ]; then arch=arm64; fi ;; MINGW* | MSYS* | CYGWIN*) os=windows ;; esac case "$arch" in amd64) arch=x86_64 ;; arm64) arch=aarch64 ;; esac printf '%s-%s\n' "$os" "$arch" | tr '[:upper:]' '[:lower:]' | tr -c 'a-z0-9_\n-' '_' } # fetch URL FILE [progress]. Returns 4 when the server answered that there is # no such thing, and 1 for everything else that went wrong. fetch() { local url="$1" out="$2" progress="${3:-}" status=0 if have curl; then set -- -fL --retry 2 -o "$out" # From an https address, never follow a redirect to anything less. case "$url" in https://*) set -- "$@" --proto '=https' --tlsv1.2 ;; esac if [ -n "$progress" ] && [ -t 2 ]; then curl "$@" --progress-bar "$url" || status=$? else # Its own words are kept back until it is known they are wanted: # "there is no such thing" is said better by whoever asked. curl "$@" -sS "$url" 2>"$work/fetch.log" || status=$? if [ "$status" != 0 ] && [ "$status" != 22 ]; then cat "$work/fetch.log" >&2; fi fi if [ "$status" = 22 ]; then return 4; fi elif have wget; then set -- -O "$out" if [ -z "$progress" ] || [ ! -t 2 ]; then set -- "$@" -q; fi wget "$@" "$url" || status=$? if [ "$status" = 8 ]; then return 4; fi else fail "neither curl nor wget is here to download with." fi if [ "$status" != 0 ]; then return 1; fi } sha256_of() { if have sha256sum; then sha256sum "$1" | awk '{ print $1 }' elif have shasum; then shasum -a 256 "$1" | awk '{ print $1 }' else openssl dgst -sha256 -r "$1" | awk '{ print $1 }' fi } megabytes() { awk -v bytes="$1" 'BEGIN { if (bytes < 1048576) printf "%.0f KB", bytes / 1024; else printf "%.1f MB", bytes / 1048576 }' } # Every value in a JSON file, one to a line, as "pathvalue": the version # as `version`, a build's address as `artifacts.app.linux-x86_64.url`. Strings # are left as they are written, escapes and all; what is read from here is # checked for shape before it is used. json_values() { awk ' function emit(value, path, k) { path = "" for (k = 1; k <= depth; k++) { if (key[k] !~ /^[A-Za-z0-9_-]+$/) return path = path (k > 1 ? "." : "") key[k] } gsub(/[\t\r\n]/, " ", value) print path "\t" value } { text = text $0 "\n" } END { n = length(text) i = 1 depth = 0 while (i <= n) { c = substr(text, i, 1) if (c == "{") { depth++; kind[depth] = "object"; key[depth] = ""; naming[depth] = 1; i++ } else if (c == "[") { depth++; kind[depth] = "list"; key[depth] = 0; i++ } else if (c == "}" || c == "]") { if (depth > 0) depth-- i++ } else if (c == ",") { if (depth > 0 && kind[depth] == "list") key[depth]++ else naming[depth] = 1 i++ } else if (c == "\"") { j = i + 1 while (j <= n) { d = substr(text, j, 1) if (d == "\\") j += 2 else if (d == "\"") break else j++ } word = substr(text, i + 1, j - i - 1) i = j + 1 if (depth > 0 && kind[depth] == "object" && naming[depth]) { key[depth] = word; naming[depth] = 0 } else emit(word) } else if (c ~ /[-0-9a-z]/) { j = i while (j <= n && substr(text, j, 1) ~ /[-+.0-9A-Za-z]/) j++ emit(substr(text, i, j - i)) i = j } else i++ } }' "$1" } # value PATH: one value out of the manifest read into $manifest_values. value() { printf '%s\n' "$manifest_values" | awk -F '\t' -v path="$1" '$1 == path { found = $2 } END { print found }' } # Whether SIGNATURE (base64) is KEY's (base64, Ed25519) signature over the # bytes of FILE. The key is 32 bytes; twelve fixed bytes in front of it make # it the form openssl reads, and twelve bytes are sixteen letters of base64. signed_by() { local key="$1" file="$2" signature="$3" printf '%s\n%s%s\n%s\n' '-----BEGIN PUBLIC KEY-----' 'MCowBQYDK2VwAyEA' "$key" '-----END PUBLIC KEY-----' >"$work/key.pem" printf '%s\n' "$signature" | openssl base64 -d -A >"$work/signature.bin" 2>/dev/null || return 1 openssl pkeyutl -verify -pubin -inkey "$work/key.pem" -rawin -in "$file" -sigfile "$work/signature.bin" >/dev/null 2>&1 } # Whether the openssl here checks Ed25519 signatures. Found out by asking it # about one that is known to be good (RFC 8032, test 2) and must pass, and the # same one over a different message, which must not. can_check_signatures() { local key='PUAXw+hDiVqStwqnTRt+vJyYLM8uxJaMwM1V8Sr0Zgw=' local signature='kqAJqfDUyrhyDoILX2QlQKKye1QWUD+Ps3YiI+vbadoIWsHkPhWZbkWPNhPQ8R2MOHsurrQwKu6wDSkWErsMAA==' have openssl || return 1 printf 'r' >"$work/known" printf 's' >"$work/other" signed_by "$key" "$work/known" "$signature" || return 1 if signed_by "$key" "$work/other" "$signature"; then return 1; fi } # Refuse a manifest the release key did not sign. When the signature cannot be # checked here, say so and go on: the download is still held to its hash. check_signature() { local file="$1" signature key keys="$RELEASE_KEYS" signature="$(tr -d ' \t\r\n' <"$2")" if ! can_check_signatures; then if have openssl; then say "Signature: not checked. The openssl here is too old for Ed25519 (3.0 or newer does it)." else say "Signature: not checked. There is no openssl here to check it with." fi return 0 fi for key in $keys; do if signed_by "$key" "$file" "$signature"; then say "Signature: good. The manifest is signed with the mcode release key." return 0 fi done # For rehearsing a release with a throwaway key. Said out loud, so that it # cannot pass for the real thing. if [ -n "${MCODE_INSTALL_TEST_KEY:-}" ] && signed_by "$MCODE_INSTALL_TEST_KEY" "$file" "$signature"; then say "Signature: good, but by the test key in MCODE_INSTALL_TEST_KEY and not the mcode release key. This is a rehearsal." return 0 fi fail "the release manifest is not signed with the mcode release key, so nothing was installed. If a release is being published this minute, try again shortly." } # The processes of this user that are running out of a folder, if any. running_from() { local folder="$1" exe target [ -d "$folder" ] || return 1 folder="$(cd "$folder" && pwd -P)" for exe in /proc/[0-9]*/exe; do target="$(readlink "$exe" 2>/dev/null)" || continue case "$target" in "$folder"/*) return 0 ;; esac done return 1 } # Replacing the files under a running Chromium crashes its next helper process. refuse_while_running() { if running_from "$lib"; then fail "mcode is running from $lib. Close it, then run this again." fi } path_note() { case ":${PATH:-}:" in *":$bin:"*) ;; *) say "$bin is not on your PATH. Add this line to ~/.profile: export PATH=\"\$HOME/.local/bin:\$PATH\"" ;; esac } # The launcher entry and its icon. write_launcher() { local run=mcode-tauri # The whole path, so the entry works when ~/.local/bin is not on the # desktop session's PATH; the bare name when the path would need quoting. case "$bin" in *[!A-Za-z0-9/._-]*) ;; *) run="$bin/mcode-tauri" ;; esac mkdir -p "$data/applications" "$data/icons/hicolor/scalable/apps" cat >"$data/applications/mcode.desktop" <"$data/icons/hicolor/scalable/apps/mcode-tauri.svg" <<'EOF' EOF if have update-desktop-database; then update-desktop-database -q "$data/applications" 2>/dev/null || true; fi } # The desktop app: its folder at ~/.local/lib/mcode, a link to the program in # ~/.local/bin, and a launcher entry. install_app() { local archive="$1" version="$2" was="" staging="$lib.new" rm -rf "$staging" mkdir -p "$staging" "$bin" tar -xzf "$archive" -C "$staging" || fail "the download could not be unpacked." [ -f "$staging/mcode-tauri" ] || fail "the download is not an mcode app: there is no mcode-tauri in it." chmod +x "$staging/mcode-tauri" # Asked again: the download took a while and mcode may have been opened. refuse_while_running rm -rf "$lib.old" if [ -d "$lib" ]; then was=1 mv "$lib" "$lib.old" fi mv "$staging" "$lib" staging="" rm -rf "$lib.old" # A program of this name that is a file of its own and not a link is the # earlier WebKit build. It is kept, once, beside the link. if [ -f "$bin/mcode-tauri" ] && [ ! -L "$bin/mcode-tauri" ] && [ ! -e "$bin/mcode-tauri-webkit" ]; then mv "$bin/mcode-tauri" "$bin/mcode-tauri-webkit" fi ln -sfn "$lib/mcode-tauri" "$bin/mcode-tauri" write_launcher if [ -n "$was" ]; then say "Replaced the mcode in $lib with $version." else say "Installed mcode $version to $lib." fi path_note case ":${PATH:-}:" in *":$bin:"*) say "Start it from your app launcher, or run: mcode-tauri" ;; *) say "Start it from your app launcher, or run: $bin/mcode-tauri" ;; esac } # The server: one program at ~/.local/bin/mcode-server, which then sets itself # up as a service and prints a pairing code. install_server() { local archive="$1" version="$2" program="$bin/mcode-server" unit mkdir -p "$work/server" "$bin" tar -xzf "$archive" -C "$work/server" || fail "the download could not be unpacked." [ -f "$work/server/mcode-server" ] || fail "the download is not an mcode server: there is no mcode-server in it." # Copied beside where it goes and renamed into place: a running program # cannot be written over, but its name can be given to a new file. The one # it replaces is kept, which is what `mcode-server rollback` puts back. cp "$work/server/mcode-server" "$program.new" chmod +x "$program.new" if [ -f "$program" ]; then cp -p "$program" "$program.previous"; fi mv -f "$program.new" "$program" say "Installed mcode-server $version to $program." path_note if [ -n "$no_service" ]; then say "Left there, as asked. Run it in a terminal with: $program" return 0 fi if ! have systemctl || ! systemctl --user show-environment >/dev/null 2>&1; then say "There is no systemd user session here, so it is not set up to start at boot." say "Run it in a terminal with: $program" return 0 fi # Already a service, and nothing new was asked of it: start the new # program and leave the port and folder it was set up with alone. unit="${XDG_CONFIG_HOME:-$HOME/.config}/systemd/user/mcode-server.service" if [ -f "$unit" ] && [ "${#server_options[@]}" = 0 ]; then systemctl --user restart mcode-server.service || fail "the service did not restart. See: journalctl --user -u mcode-server -n 50" say "Restarted the service. Paired devices stay paired; \`mcode-server pair\` adds another." return 0 fi # Its own words from here: where it is listening, and a code and QR to # pair the first device with. "$program" install ${server_options[@]+"${server_options[@]}"}