#!/bin/bash # Installs Überbringer behind nginx with a Let's Encrypt certificate. # # Usage: sudo ./install.sh # # Installs the latest release. To install a given release instead, name its tag: # sudo UBERBRINGER_VERSION=v0.2.0 ./install.sh # Re-running the script upgrades the binary and the systemd unit, and keeps the # config, the certificate and any `systemctl edit uberbringer` drop-ins. set -euo pipefail DOMAIN="${1:-}" ACME_EMAIL="${2:-}" PRINTER_ADDRESS="${3:-}" LOG=/var/log/uberbringer-install.log CONFIG_DIR=/etc/uberbringer CONFIG="$CONFIG_DIR/config.toml" TOKEN_FILE="$CONFIG_DIR/initial-token" BIN=/usr/local/bin/uberbringer SERVICE_USER=uberbringer # The vendor unit path for locally installed software. Units in # /etc/systemd/system take precedence over it, so operator changes belong in # drop-ins there. UNIT_DIR=/usr/local/lib/systemd/system UNIT_FILE="$UNIT_DIR/uberbringer.service" ETC_UNIT_FILE=/etc/systemd/system/uberbringer.service ACME=/root/.acme.sh/acme.sh CERT_DIR="/etc/letsencrypt/live/$DOMAIN" # Gitea resolves the tag "latest" to the newest release that is neither a # draft nor a prerelease. RELEASE_URL="https://git.anomalous.dev/57_Wolve/uberbringer/releases/download/${UBERBRINGER_VERSION:-latest}" # The checkout this script runs from, or empty when it is piped into bash. SCRIPT_DIR="" if [[ -n "${BASH_SOURCE[0]:-}" && -f "${BASH_SOURCE[0]}" ]]; then SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" fi # Set when the binary, the unit or the config changed, so the service is # restarted only then. RESTART_NEEDED=0 CONFIG_CREATED=0 WORK_DIR="" UNIT_SRC="" if [[ "$EUID" -ne 0 ]]; then echo -e "\e[31m[FATAL]\e[39m This script must be run as root." >&2 exit 1 fi if [[ -z "$DOMAIN" || -z "$ACME_EMAIL" || -z "$PRINTER_ADDRESS" ]]; then echo "Usage: $0 " >&2 exit 1 fi # The domain goes unquoted into nginx config and file paths, and the printer # address into a TOML string, so both are limited to host name and IP address # characters. if [[ ! "$DOMAIN" =~ ^[A-Za-z0-9.-]+$ ]]; then echo "Invalid domain: $DOMAIN" >&2 exit 1 fi if [[ ! "$PRINTER_ADDRESS" =~ ^[A-Za-z0-9.:-]+$ ]]; then echo "Invalid printer address: $PRINTER_ADDRESS (expected an IP address or host name)" >&2 exit 1 fi echo -e "\n\nINSTALL LOG FOR Uberbringer: $(date --rfc-3339=seconds)\n" >> "$LOG" info() { echo -e "\e[34m[INFO]\e[39m $1" echo "[INFO] $1" >> "$LOG" } warn() { echo -e "\e[33m[WARN]\e[39m $1" echo "[WARN] $1" >> "$LOG" } debug() { if [[ -n "${DEBUG:-}" ]]; then echo -e "\e[96m[DEBUG]\e[39m $1" fi echo "[DEBUG] $1" >> "$LOG" } # fatal writes to stderr so its message is not swallowed when it runs inside a # command substitution. fatal() { echo -e "\e[31m[FATAL]\e[39m $1" >&2 echo "[FATAL] $1" >> "$LOG" exit 1 } require() { command -v "$1" > /dev/null 2>&1 || fatal "Couldn't find $1 - please install it and rerun the script." } cleanup() { if [[ -n "$WORK_DIR" ]]; then rm -rf "$WORK_DIR" fi } detect_arch() { case "$(uname -m)" in x86_64) echo "amd64" ;; aarch64 | arm64) echo "arm64" ;; armv7l) echo "armv7" ;; armv6l) echo "armv6" ;; *) fatal "Unsupported architecture: $(uname -m)" ;; esac } # install_if_changed installs $1 as $3 with mode $2 unless $3 already has the # same content, and records that the service needs a restart. install_if_changed() { if cmp -s "$1" "$3"; then debug "$3 is unchanged." return fi install -m "$2" "$1" "$3" RESTART_NEEDED=1 } install_packages() { info "Installing curl, socat and nginx..." export DEBIAN_FRONTEND=noninteractive # A broken third-party source makes the update fail even when the # packages below are installed or still installable, so only the install # is fatal. apt-get update >> "$LOG" 2>&1 || warn "apt-get update failed; trying the install anyway (see $LOG)." apt-get install -y nginx curl socat >> "$LOG" 2>&1 || fatal "apt-get install failed; see $LOG." require nginx require curl require socat require sha256sum require systemctl } # download_release fetches the release archive and checksums.txt into # WORK_DIR, verifies the archive and unpacks it there. download_release() { local arch archive arch="$(detect_arch)" archive="uberbringer_linux_${arch}.tar" info "Downloading ${archive} from ${RELEASE_URL}..." curl -fsSL -o "$WORK_DIR/checksums.txt" "$RELEASE_URL/checksums.txt" || fatal "Couldn't download checksums.txt from $RELEASE_URL." curl -fsSL -o "$WORK_DIR/$archive" "$RELEASE_URL/$archive" || fatal "Couldn't download $archive from $RELEASE_URL." info "Verifying ${archive}..." # A leading * on the file name marks a binary-mode checksum line. awk -v f="$archive" '$2 == f || $2 == "*" f' "$WORK_DIR/checksums.txt" > "$WORK_DIR/archive.sha256" [[ -s "$WORK_DIR/archive.sha256" ]] || fatal "checksums.txt has no entry for $archive." (cd "$WORK_DIR" && sha256sum --check --strict --quiet archive.sha256) >> "$LOG" 2>&1 || fatal "$archive does not match checksums.txt; nothing was installed." mkdir "$WORK_DIR/release" tar -xf "$WORK_DIR/$archive" -C "$WORK_DIR/release" [[ -f "$WORK_DIR/release/uberbringer" ]] || fatal "$archive does not contain the uberbringer binary." } install_binary() { install_if_changed "$WORK_DIR/release/uberbringer" 0755 "$BIN" # Run from its installed path: WORK_DIR may be on a noexec /tmp. info "Installed $("$BIN" version)" } create_service_user() { if ! id "$SERVICE_USER" > /dev/null 2>&1; then info "Creating system user $SERVICE_USER..." useradd --system --no-create-home --shell /usr/sbin/nologin "$SERVICE_USER" fi } write_config() { mkdir -p "$CONFIG_DIR" if [[ -f "$CONFIG" ]]; then info "Keeping existing $CONFIG; edit printer.address there to change the printer." return fi info "Creating Uberbringer config..." local secret secret="$("$BIN" secret)" # The config holds the API secret, so it is never readable by others, not # even between being written and being chmodded. ( umask 027 cat < "$CONFIG" [webserver] bind_ip = "127.0.0.1" port = 8080 [api] secret = "$secret" [printer] address = "$PRINTER_ADDRESS" EOT ) chown root:"$SERVICE_USER" "$CONFIG" chmod 0640 "$CONFIG" CONFIG_CREATED=1 RESTART_NEEDED=1 } # unit_source prints the path of the unit to install: the release archive's # copy, or for releases that do not ship one, the copy in this checkout. unit_source() { local candidate for candidate in "$WORK_DIR/release/packaging/uberbringer.service" \ ${SCRIPT_DIR:+"$SCRIPT_DIR/packaging/uberbringer.service"}; do if [[ -f "$candidate" ]]; then echo "$candidate" return fi done fatal "Neither the release archive nor this checkout has packaging/uberbringer.service." } install_service() { debug "Installing the systemd unit from $UNIT_SRC..." # A full unit in /etc shadows the packaged one. One with this Description # is an installer-generated copy rather than an operator's override, so it # is moved aside; any other file there is left alone with a warning. if [[ -f "$ETC_UNIT_FILE" ]] && grep -qx 'Description=Uberbringer Daemon' "$ETC_UNIT_FILE"; then info "Replacing the unit in $ETC_UNIT_FILE with $UNIT_FILE (old copy kept as $ETC_UNIT_FILE.bak)..." # Disabling first removes the wants symlink that points at the old path. systemctl disable uberbringer >> "$LOG" 2>&1 mv "$ETC_UNIT_FILE" "$ETC_UNIT_FILE.bak" RESTART_NEEDED=1 elif [[ -e "$ETC_UNIT_FILE" ]]; then warn "$ETC_UNIT_FILE overrides $UNIT_FILE; remove it to use the packaged unit." fi mkdir -p "$UNIT_DIR" install_if_changed "$UNIT_SRC" 0644 "$UNIT_FILE" systemctl daemon-reload systemctl enable uberbringer >> "$LOG" 2>&1 if [[ "$RESTART_NEEDED" -eq 1 ]]; then debug "Restarting Uberbringer Web Service..." systemctl restart uberbringer else debug "Binary, unit and config are unchanged; starting Uberbringer only if it is stopped..." systemctl start uberbringer fi } install_nginx() { info "Configuring nginx..." mkdir -p /etc/nginx/includes "/var/www/$DOMAIN" cat < /etc/nginx/includes/letsencrypt-webroot location / { alias /var/www/$DOMAIN/; } EOT rm -f /etc/nginx/sites-enabled/default /etc/nginx/sites-available/default cat < /etc/nginx/sites-available/default.conf server { listen 80; listen [::]:80; server_name $DOMAIN; include includes/letsencrypt-webroot; } EOT ln -sf /etc/nginx/sites-available/default.conf /etc/nginx/sites-enabled/default.conf systemctl enable nginx >> "$LOG" 2>&1 debug "Starting Nginx..." systemctl restart nginx } # issue_certificate gets a certificate only when none is installed yet; # renewals are left to the cron job acme.sh installs. issue_certificate() { if [[ -s "$CERT_DIR/fullchain.pem" && -s "$CERT_DIR/privkey.pem" ]]; then info "Keeping the existing certificate in $CERT_DIR; acme.sh's cron job renews it." return fi if [[ ! -x "$ACME" ]]; then info "Installing acme.sh..." curl -fsSL https://get.acme.sh | sh -s email="$ACME_EMAIL" info "Enabling acme.sh automatic upgrade..." "$ACME" --upgrade --auto-upgrade || true fi info "Issuing SSL certificate..." local rc=0 "$ACME" --issue --server letsencrypt -w "/var/www/$DOMAIN" -d "$DOMAIN" || rc=$? # 2 means acme.sh already holds a certificate for the domain that is not # due for renewal; --install-cert below copies it into place. if [[ "$rc" -ne 0 && "$rc" -ne 2 ]]; then fatal "acme.sh could not issue a certificate for $DOMAIN (exit code $rc)." fi mkdir -p "$CERT_DIR" # acme.sh records these paths and the reload command, and reuses them when # its cron job renews the certificate. "$ACME" --install-cert -d "$DOMAIN" \ --key-file "$CERT_DIR/privkey.pem" \ --fullchain-file "$CERT_DIR/fullchain.pem" \ --reloadcmd "systemctl reload nginx" } install_reverse_proxy() { info "Configuring the HTTPS reverse proxy..." cat < /etc/nginx/sites-available/reverse-proxy.conf server { listen 443 ssl; listen [::]:443 ssl; server_name $DOMAIN; ssl_certificate $CERT_DIR/fullchain.pem; ssl_certificate_key $CERT_DIR/privkey.pem; ssl_session_cache shared:SSL:10m; ssl_protocols TLSv1.2 TLSv1.3; ssl_prefer_server_ciphers off; error_log /var/log/nginx/uberbringer_error.log; access_log /var/log/nginx/uberbringer_access.log; client_max_body_size 4m; location / { proxy_redirect off; proxy_set_header Host \$http_host; # Uberbringer trusts X-Real-IP from 127.0.0.1 for per-client rate limits. proxy_set_header X-Real-IP \$remote_addr; proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto \$scheme; proxy_pass http://127.0.0.1:8080; proxy_read_timeout 90; add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always; add_header X-Frame-Options DENY always; add_header X-Content-Type-Options nosniff always; add_header Referrer-Policy "no-referrer" always; } } EOT ln -sf /etc/nginx/sites-available/reverse-proxy.conf /etc/nginx/sites-enabled/reverse-proxy.conf nginx -t debug "Reloading Nginx..." systemctl reload nginx } # write_initial_token saves the first bearer token to a root-only file instead # of printing it, so it does not end up in terminal scrollback or the log. It # runs right after the config is created, because a re-run after a later step # failed keeps that config and writes no token. write_initial_token() { if [[ "$CONFIG_CREATED" -ne 1 ]]; then return fi rm -f "$TOKEN_FILE" ( umask 077 "$BIN" token --config "$CONFIG" > "$TOKEN_FILE" ) debug "Wrote the first API token to $TOKEN_FILE." } token_notice() { if [[ "$CONFIG_CREATED" -eq 1 ]]; then info "The first API token is in $TOKEN_FILE (readable by root only). Copy it to your client, then delete the file." return fi if [[ -s "$TOKEN_FILE" ]]; then warn "$TOKEN_FILE still holds the first API token; delete it once your client has it." fi info "Issue API tokens with: $BIN token --config $CONFIG --subject " } main() { info "Script loaded, starting the install process..." install_packages WORK_DIR="$(mktemp -d)" trap cleanup EXIT download_release # Resolved first, so a missing unit stops the script before any of # Uberbringer's files are installed. UNIT_SRC="$(unit_source)" install_binary create_service_user write_config write_initial_token install_service install_nginx issue_certificate install_reverse_proxy info "Uberbringer service is now installed." info "Override unit settings with 'systemctl edit uberbringer'; drop-ins survive re-runs of this script." token_notice } main