#!/usr/bin/env bash # Authcha one-line installer — https://get.authcha.cloud # # curl -fsSL https://get.authcha.cloud | sudo bash -s -- --license AUTHCHA-… # curl -fsSL https://get.authcha.cloud | sudo bash -s -- --trial you@company.com # ... --trial you@company.com (no terminal: starts the trial, prints next step) # ... --trial you@company.com --trial-code 123456 (finishes it with the emailed code) # sudo bash install.sh --bundle authcha-X.Y.Z.tar.gz --license-file customer.json (air-gapped) # # Options: # --license KEY | --license-file FILE | --trial EMAIL (one is required on a first install; an upgrade reuses the installed licence) # --trial-code CODE the 6-digit code emailed for --trial (also read from the # AUTHCHA_TRIAL_CODE environment variable, or from stdin # when there is no terminal). Without a terminal and # without a code, --trial only starts the trial and exits 3; # run the same command again with the code to finish. # The trial key and the pending request are kept (mode 0600) # in /root/.authcha-trial-license and # /root/.authcha-trial-request, so a rerun resumes. # --bundle FILE install from a downloaded bundle (its .sig must sit next to it) # --version X.Y.Z a specific release (default: latest) # --hostname FQDN the console's DNS name (recommended) # --prefix DIR install directory (default /opt/authcha) # --tls-cert FILE --tls-key FILE your certificate for the console # --admin-password-file FILE initial password for 'admin' (default: generated) # --no-docker-install fail instead of installing Docker Engine (use it when Docker is installed # from your own mirror, or on an offline server) # --docker-mirror URL install Docker Engine from this mirror of https://download.docker.com/linux # (also AUTHCHA_DOCKER_MIRROR); Docker already installed is used as it is # --offline this server has no internet access: no call to any Authcha host (the clock is # checked against the local NTP state), and the console starts with the update # check off ("updates are manual"). Implied by --bundle on a first install. # --download-only DIR download and verify the release (+ its .sig) into DIR and stop — # for carrying it to an air-gapped server # --force continue on an unsupported OS # # Everything runs from main() at the end, so a partial download never runs. AUTHCHA_LICENSE_SERVER="${AUTHCHA_LICENSE_SERVER:-https://license.authcha.cloud}" # Ed25519 key that signs every Authcha release bundle. BUNDLE_PUBLIC_KEY='-----BEGIN PUBLIC KEY----- MCowBQYDK2VwAyEAZlAU1PV/rWqep9OuHMR+2RNmO2centUtw3Ab7b0SXkU= -----END PUBLIC KEY-----' set -Eeuo pipefail c_ok=$'\e[32m' c_warn=$'\e[33m' c_err=$'\e[31m' c_b=$'\e[1m' c_off=$'\e[0m' [[ -t 1 ]] || { c_ok=""; c_warn=""; c_err=""; c_b=""; c_off=""; } log() { printf '%s==> %s%s\n' "$c_b" "$*" "$c_off"; } ok() { printf '%s ✓ %s%s\n' "$c_ok" "$*" "$c_off"; } warn() { printf '%s ! %s%s\n' "$c_warn" "$*" "$c_off"; } die() { printf '%s ✗ %s%s\n' "$c_err" "$*" "$c_off" >&2; exit 1; } json_get() { sed -n 's/.*"'"$1"'"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' | head -1; } json_str() { local s=${1//\\/\\\\}; s=${s//\"/\\\"}; printf '"%s"' "$s"; } # The host fingerprint sent with a trial request: the activation fingerprint formula with only this machine's id # (no installation exists yet). Optional for the license server; empty when there is no machine id. host_fingerprint() { local f="${AUTHCHA_MACHINE_ID_FILE:-/etc/machine-id}" mid mid="$(tr -d '[:space:]' < "$f" 2>/dev/null)" || true [[ -n "$mid" ]] || return 0 printf 'authcha-fp-v1\0%s' "$mid" | sha256sum | cut -d' ' -f1 } fingerprint_json() { local fp; fp="$(host_fingerprint)"; [[ -z "$fp" ]] || printf ',"fingerprint":"%s"' "$fp"; } # Like post_json, but keeps the answer of a refusal: sets TRIAL_HTTP and TRIAL_BODY (returns 1 only when the server can't be reached). trial_post() { local out out="$(curl -sS --max-time 60 -X POST -H 'content-type: application/json' -d "$2" -w '\n%{http_code}' "$AUTHCHA_LICENSE_SERVER$1")" || return 1 TRIAL_HTTP="${out##*$'\n'}"; TRIAL_BODY="${out%$'\n'*}" } # A trial that needs a person at Authcha to look at it (HTTP 202 {"status":"pending_review","message":...}): say so and stop, successfully. trial_pending_exit() { local m; m="$(printf '%s' "$TRIAL_BODY" | json_get message)" echo " ${m:-Your trial request is being reviewed.}" echo " Nothing was installed. The key is e-mailed to $TRIAL_EMAIL once the request is approved; then install it with: --license AUTHCHA-... (or: authcha license install FILE)." exit 0 } trial_refused() { # a refusal's own words when it has them local m; m="$(printf '%s' "$TRIAL_BODY" | json_get message)" die "${m:-the trial could not be started (one trial per email address and company) — contact sales@authcha.cloud}" } post_json() { curl -fsS --max-time 60 -X POST -H 'content-type: application/json' -d "$2" "$AUTHCHA_LICENSE_SERVER$1"; } LICENSE="" LICENSE_FILE="" TRIAL_EMAIL="" TRIAL_CODE="${AUTHCHA_TRIAL_CODE:-}" BUNDLE="" AC_VERSION=latest HOSTNAME_FQDN="" PREFIX=/opt/authcha TLS_CERT="" TLS_KEY="" ADMIN_PW_FILE="" DOCKER_INSTALL=1 FORCE=0 DOWNLOAD_ONLY="" OFFLINE=0 DOCKER_MIRROR="${AUTHCHA_DOCKER_MIRROR:-https://download.docker.com/linux}" WORK="" cleanup() { [[ -n "$WORK" && -d "$WORK" ]] && rm -rf "$WORK"; } parse_args() { while [[ $# -gt 0 ]]; do case "$1" in --license) LICENSE=$2; shift 2 ;; --license-file) LICENSE_FILE=$2; shift 2 ;; --trial) TRIAL_EMAIL=$2; shift 2 ;; --trial-code) TRIAL_CODE=$2; shift 2 ;; --bundle) BUNDLE=$2; shift 2 ;; --version) AC_VERSION=$2; shift 2 ;; --hostname) HOSTNAME_FQDN=$2; shift 2 ;; --prefix) PREFIX=$2; shift 2 ;; --tls-cert) TLS_CERT=$2; shift 2 ;; --tls-key) TLS_KEY=$2; shift 2 ;; --admin-password-file) ADMIN_PW_FILE=$2; shift 2 ;; --no-docker-install) DOCKER_INSTALL=0; shift ;; --download-only) DOWNLOAD_ONLY=$2; shift 2 ;; --force) FORCE=1; shift ;; --offline) OFFLINE=1; shift ;; --docker-mirror) DOCKER_MIRROR=${2%/}; shift 2 ;; --yes|-y) shift ;; -h|--help) sed -n '2,42p' "${BASH_SOURCE[0]:-/dev/null}" 2>/dev/null | sed 's/^# \{0,1\}//' || true; exit 0 ;; *) die "unknown option: $1 (see --help)" ;; esac done local n=0 [[ -n "$LICENSE" ]] && n=$((n+1)); [[ -n "$LICENSE_FILE" ]] && n=$((n+1)); [[ -n "$TRIAL_EMAIL" ]] && n=$((n+1)) # An upgrade reuses the licence this host already has. if (( n == 0 )) && [[ -s "$PREFIX/run/license.json" ]]; then LICENSE_FILE="$PREFIX/run/license.json"; n=1 ok "using the licence installed in $PREFIX" fi (( n == 1 )) || die "give exactly one of --license KEY, --license-file FILE or --trial EMAIL (none is installed under $PREFIX)" (( ! OFFLINE )) || [[ -n "$BUNDLE" ]] || die "--offline needs --bundle FILE (the release file and its .sig, carried to this server)" (( ! OFFLINE )) || [[ -z "$TRIAL_EMAIL" ]] || die "--trial needs internet access; with --offline use --license-file" [[ -z "$TRIAL_CODE" || -n "$TRIAL_EMAIL" ]] || die "--trial-code only goes with --trial EMAIL" } # A wrong clock breaks TLS and licensing. Offline (--offline, --bundle): never calls an Authcha host; the local NTP state is # only a warning. AUTHCHA_TIME_CHECK_URL: compare with the Date header of any HTTPS server you trust (an internal one too). time_check() { local url="${AUTHCHA_TIME_CHECK_URL:-}" server_date skew if [[ -z "$url" ]]; then if (( OFFLINE )) || [[ -n "$BUNDLE" ]]; then if command -v timedatectl >/dev/null && [[ "$(timedatectl show -p NTPSynchronized --value 2>/dev/null)" == no ]]; then warn "the clock is not synchronised (NTP): check the date and time, certificates and licenses depend on it" fi return 0 fi url="$AUTHCHA_LICENSE_SERVER/healthz" fi server_date="$(curl -fsSI --max-time 15 "$url" 2>/dev/null | tr -d '\r' | sed -n 's/^[Dd]ate: //p')" || true if [[ -n "$server_date" ]]; then skew=$(( $(date -u +%s) - $(date -u -d "$server_date" +%s) )); skew=${skew#-} (( skew < 3600 )) || die "this server's clock is off by $((skew/60)) minutes — fix the time (NTP) first" elif [[ -z "$BUNDLE" ]] && (( ! OFFLINE )); then die "can't reach ${url%/healthz} — check outbound HTTPS (set https_proxy if you use a proxy), or use --bundle / --offline" fi } preflight() { log "Checking this server" [[ $EUID -eq 0 ]] || die "run as root (… | sudo bash -s -- …)" [[ "$(uname -m)" == x86_64 ]] || die "Authcha needs an x86_64 server (this one is $(uname -m)); there is no arm64 build. See docs/product/installation.md, Supported platforms" # shellcheck disable=SC1091 . /etc/os-release 2>/dev/null || true case "${ID:-}:${VERSION_ID:-}" in ubuntu:22.04|ubuntu:24.04|debian:12|debian:13|rhel:9*|rocky:9*|almalinux:9*) ok "${PRETTY_NAME:-$ID}" ;; *) (( FORCE )) || die "${PRETTY_NAME:-this OS} isn't a supported OS (Ubuntu 22.04/24.04, Debian 12/13, RHEL/Rocky/Alma 9). Use --force to try anyway." warn "${PRETTY_NAME:-unknown OS} is not supported — continuing because of --force" ;; esac for t in curl tar openssl sha256sum base64; do command -v "$t" >/dev/null || die "$t is required"; done openssl version | grep -qE '^OpenSSL 3' || die "OpenSSL 3 is required to verify the release signature" local mem_kb; mem_kb="$(awk '/MemTotal/ {print $2}' /proc/meminfo)" (( mem_kb >= 3500000 )) || warn "less than 4 GB of memory — fine for a lab, too small for production" install -d -m 0750 "$PREFIX" local free_kb; free_kb="$(df -Pk "$PREFIX" | awk 'NR==2 {print $4}')" (( free_kb >= 10000000 )) || die "at least 10 GB must be free under $PREFIX (have $((free_kb/1024/1024)) GB)" time_check ok "preflight passed" } ensure_docker() { if command -v docker >/dev/null && docker compose version >/dev/null 2>&1; then local v; v="$(docker version --format '{{.Server.Version}}' 2>/dev/null || echo 0)" [[ "${v%%.*}" -ge 24 ]] || die "Docker Engine $v is too old (24+ needed) — upgrade it first" ok "Docker $v" return fi (( DOCKER_INSTALL )) || die "Docker Engine 24+ with Compose v2 is required (--no-docker-install was given): install it from your own package mirror, then run this again" if { (( OFFLINE )) || [[ -n "$BUNDLE" ]]; } && [[ "$DOCKER_MIRROR" == https://download.docker.com/linux ]]; then die "Docker Engine 24+ with Compose v2 is not installed and this is an offline install. Install it from your OS or internal package mirror (or give --docker-mirror URL), then run this again" fi log "Installing Docker Engine from $DOCKER_MIRROR" # shellcheck disable=SC1091 . /etc/os-release case "$ID" in ubuntu|debian) apt-get update -qq && apt-get install -y -qq ca-certificates curl gnupg >/dev/null install -m 0755 -d /etc/apt/keyrings curl -fsSL "$DOCKER_MIRROR/$ID/gpg" | gpg --dearmor --yes -o /etc/apt/keyrings/docker.gpg chmod a+r /etc/apt/keyrings/docker.gpg echo "deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.gpg] $DOCKER_MIRROR/$ID $VERSION_CODENAME stable" \ > /etc/apt/sources.list.d/docker.list apt-get update -qq && apt-get install -y -qq docker-ce docker-ce-cli containerd.io docker-compose-plugin >/dev/null ;; rhel|rocky|almalinux) dnf -q -y install dnf-plugins-core >/dev/null dnf -q config-manager --add-repo $DOCKER_MIRROR/rhel/docker-ce.repo dnf -q -y install docker-ce docker-ce-cli containerd.io docker-compose-plugin >/dev/null ;; *) die "install Docker Engine 24+ with the Compose plugin yourself, then run this again" ;; esac systemctl enable --now docker >/dev/null ok "Docker $(docker version --format '{{.Server.Version}}') installed" } # A trial key is saved the moment it is issued: the license server allows one # trial per address, so a failed download must not cost the customer theirs. # The pending request (id + email) is kept too, so a second run can finish a # trial that was started without a terminal. AUTHCHA_STATE_DIR="${AUTHCHA_STATE_DIR:-/root}" TRIAL_KEY_FILE="$AUTHCHA_STATE_DIR/.authcha-trial-license" TRIAL_REQ_FILE="$AUTHCHA_STATE_DIR/.authcha-trial-request" trial_verify() { # rid code -> sets LICENSE and saves it local key trial_post /v1/trial/verify "{\"request_id\":$(json_str "$1"),\"code\":$(json_str "$2")$(fingerprint_json)}" || return 1 [[ "$TRIAL_HTTP" != 202 ]] || trial_pending_exit [[ "$TRIAL_HTTP" == 200 ]] || return 1 key="$(printf '%s' "$TRIAL_BODY" | json_get license)" [[ -n "$key" ]] || return 1 LICENSE="$key"; ( umask 077; printf '%s' "$key" > "$TRIAL_KEY_FILE" ); rm -f "$TRIAL_REQ_FILE" ok "trial license issued (saved to $TRIAL_KEY_FILE)" } get_trial() { if [[ -r "$TRIAL_KEY_FILE" ]] && grep -q '^AUTHCHA-' "$TRIAL_KEY_FILE"; then LICENSE="$(tr -d ' \n\r' < "$TRIAL_KEY_FILE")"; ok "reusing the trial license saved in $TRIAL_KEY_FILE"; return fi local rid="" resp key code="$TRIAL_CODE" tty=0 attempt [[ -r /dev/tty ]] && ( : /dev/null && tty=1 if [[ -r "$TRIAL_REQ_FILE" && "$(sed -n 2p "$TRIAL_REQ_FILE")" == "$TRIAL_EMAIL" ]]; then rid="$(sed -n 1p "$TRIAL_REQ_FILE")"; ok "resuming the trial request for $TRIAL_EMAIL (request $rid)" else log "Starting your 30-day trial" trial_post /v1/trial/start "{\"email\":$(json_str "$TRIAL_EMAIL")$(fingerprint_json)}" \ || die "can't reach the license server to start the trial — check outbound HTTPS (https_proxy), or write to sales@authcha.cloud" case "$TRIAL_HTTP" in 200|201) ;; # 202 with a request id is the normal "a code was e-mailed" answer; only an explicit pending_review stops here 202) [[ "$(printf '%s' "$TRIAL_BODY" | json_get status)" != pending_review ]] || trial_pending_exit ;; *) trial_refused ;; # 422: a disposable email address is not accepted; others: one trial per address and company, or the request was declined esac resp="$TRIAL_BODY" # Approved after a review: the same request answers with the key itself. key="$(printf '%s' "$resp" | json_get license)" if [[ "$key" == AUTHCHA-* ]]; then LICENSE="$key"; ( umask 077; printf '%s' "$key" > "$TRIAL_KEY_FILE" ); rm -f "$TRIAL_REQ_FILE" ok "trial license issued (saved to $TRIAL_KEY_FILE)"; return fi rid="$(printf '%s' "$resp" | json_get request_id)" [[ -n "$rid" ]] || die "unexpected answer from the license server" ( umask 077; printf '%s\n%s\n' "$rid" "$TRIAL_EMAIL" > "$TRIAL_REQ_FILE" ) ok "we emailed a 6-digit code to $TRIAL_EMAIL (request $rid)" fi if [[ -z "$code" ]] && (( ! tty )) && [[ ! -t 0 ]]; then read -r -t 2 code || true; fi # piped in: echo 123456 | … if [[ -n "$code" ]]; then trial_verify "$rid" "$code" && return (( tty )) || die "that code didn't work — check it, or run again without --trial-code for a new request (rm $TRIAL_REQ_FILE)" warn "that code didn't work" elif (( ! tty )); then echo " No terminal to type the code into. Finish with the code from the email:" echo " sudo bash install.sh --trial $TRIAL_EMAIL --trial-code 123456 [same options]" echo " (or set AUTHCHA_TRIAL_CODE, or pipe the code on stdin). Request: $rid, kept in $TRIAL_REQ_FILE" exit 3 fi for attempt in 1 2 3; do read -r -p " Code: " code "$WORK/license/license.json"; chmod 600 "$WORK/license/license.json" fi } verify_signature() { # file sig_b64 printf '%s\n' "$BUNDLE_PUBLIC_KEY" > "$WORK/bundle-signing.pem" printf '%s' "$2" | base64 -d > "$WORK/bundle.sig" 2>/dev/null || return 1 openssl pkeyutl -verify -pubin -inkey "$WORK/bundle-signing.pem" -rawin -in "$1" -sigfile "$WORK/bundle.sig" >/dev/null 2>&1 } fetch_bundle() { if [[ -n "$BUNDLE" ]]; then [[ -r "$BUNDLE" && -r "$BUNDLE.sig" ]] || die "--bundle needs the file and its .sig next to it" local sig_b64; sig_b64="$(base64 -w0 < "$BUNDLE.sig")" # The .sig may be raw (64 bytes) or already base64. [[ "$(stat -c %s "$BUNDLE.sig")" == 64 ]] || sig_b64="$(tr -d '\n' < "$BUNDLE.sig")" verify_signature "$BUNDLE" "$sig_b64" || die "signature check FAILED for $BUNDLE — don't install it" ok "bundle signature verified" BUNDLE_FILE="$BUNDLE" return fi log "Downloading Authcha ($AC_VERSION)" local key_json resp url sha sig ver # The server takes the license JSON object or the AUTHCHA- key (as a string). local content; content="$(tr -d '\n\r' < "$WORK/license/license.json")" if [[ "$content" == AUTHCHA-* ]]; then key_json="$(json_str "${content// /}")"; else key_json="$content"; fi resp="$(post_json /v1/download "{\"license\":$key_json,\"version\":$(json_str "$AC_VERSION")}")" \ || die "the license server refused the download — check the license (valid, not expired, not revoked)" ver="$(printf '%s' "$resp" | json_get version)"; url="$(printf '%s' "$resp" | json_get url)" sha="$(printf '%s' "$resp" | json_get sha256)"; sig="$(printf '%s' "$resp" | json_get signature)" [[ -n "$ver" && -n "$url" && -n "$sha" && -n "$sig" ]] || die "unexpected answer from the license server" install -d -m 0700 /var/cache/authcha BUNDLE_FILE="/var/cache/authcha/authcha-$ver.tar.gz" curl -fL --retry 3 -C - --max-time 1800 -o "$BUNDLE_FILE" "$url" || die "download failed" [[ "$(sha256sum "$BUNDLE_FILE" | cut -d' ' -f1)" == "$sha" ]] || { rm -f "$BUNDLE_FILE"; die "checksum mismatch — download discarded"; } verify_signature "$BUNDLE_FILE" "$sig" || { rm -f "$BUNDLE_FILE"; die "signature check FAILED — download discarded"; } ok "Authcha $ver downloaded and verified (SHA-256 + Ed25519 signature)" BUNDLE_SIG_B64="$sig" } generate_password() { local chars='ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz23456789' pw="" g i for g in 1 2 3 4; do (( g > 1 )) && pw+="-" for i in 1 2 3 4; do pw+="${chars:$(( $(od -An -N2 -tu2 /dev/urandom) % ${#chars} )):1}"; done done printf '%s' "$pw" } install_bundle() { log "Installing" tar -xzf "$BUNDLE_FILE" -C "$WORK" # No `find -printf`: BusyBox find lacks it. Strip the file name instead (an empty result stays empty). local kit; kit="$(find "$WORK" -mindepth 1 -maxdepth 2 -name install.sh | grep -v "^$WORK/license" | head -1)"; kit="${kit%/install.sh}" [[ -n "$kit" ]] || die "the bundle has no installer" local args=(--prefix "$PREFIX" --license-file "$WORK/license/license.json") [[ -n "$HOSTNAME_FQDN" ]] && args+=(--hostname "$HOSTNAME_FQDN") (( FORCE )) && args+=(--force) # A bundle install on a new server is an offline site; an upgrade by bundle keeps what the site had. if (( OFFLINE )) || { [[ -n "$BUNDLE" && ! -f "$PREFIX/.env" ]]; }; then args+=(--offline); fi [[ -n "$TLS_CERT" ]] && args+=(--tls-cert "$TLS_CERT" --tls-key "$TLS_KEY") if [[ ! -f "$PREFIX/.env" ]]; then if [[ -z "$ADMIN_PW_FILE" ]]; then ADMIN_PW_FILE="$WORK/initial-admin-password" ( umask 077; generate_password > "$ADMIN_PW_FILE" ) fi args+=(--admin-password-file "$ADMIN_PW_FILE") fi "$kit/install.sh" "${args[@]}" } download_only() { [[ -z "$BUNDLE" ]] || die "--download-only and --bundle don't go together" install -d -m 0755 "$DOWNLOAD_ONLY" fetch_bundle local name; name="$(basename "$BUNDLE_FILE")" cp "$BUNDLE_FILE" "$DOWNLOAD_ONLY/$name" printf '%s' "$BUNDLE_SIG_B64" | base64 -d > "$DOWNLOAD_ONLY/$name.sig" ok "saved $DOWNLOAD_ONLY/$name and $name.sig" echo " Copy them and this installer (https://get.authcha.cloud/install.sh) to the server, then run:" echo " sudo bash install.sh --bundle $name --license-file --hostname " } main() { parse_args "$@" WORK="$(mktemp -d)"; trap cleanup EXIT if [[ -n "$DOWNLOAD_ONLY" ]]; then for t in curl openssl sha256sum base64; do command -v "$t" >/dev/null || die "$t is required"; done [[ -n "$TRIAL_EMAIL" ]] && get_trial stage_license download_only return fi preflight ensure_docker [[ -n "$TRIAL_EMAIL" ]] && get_trial stage_license fetch_bundle install_bundle } # AUTHCHA_INSTALL_SOURCED lets the tests load the functions without running. [[ -n "${AUTHCHA_INSTALL_SOURCED:-}" ]] || main "$@"