#!/bin/sh set -eu umask 077 PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin export PATH RUSTPROXY_URL="${RUSTPROXY_URL:-}" RUSTPROXY_KEY_ID="${RUSTPROXY_KEY_ID:-}" RUSTPROXY_TOKEN="${RUSTPROXY_TOKEN:-}" RUSTPROXY_CERTIFICATES="${RUSTPROXY_CERTIFICATES:-}" CERTIFICATE_ID="${CERTIFICATE_ID:-}" ACME_CERT_NAME="${ACME_CERT_NAME:-}" CERT_FILE="${CERT_FILE:-}" KEY_FILE="${KEY_FILE:-}" RUSTPROXY_TLS_VERIFY="${RUSTPROXY_TLS_VERIFY:-yes}" RUSTPROXY_CACERT="${RUSTPROXY_CACERT:-}" RUSTPROXY_PINNED_PUBKEY="${RUSTPROXY_PINNED_PUBKEY:-}" RUSTPROXY_RESOLVE="${RUSTPROXY_RESOLVE:-}" RUSTPROXY_ALLOW_HTTP="${RUSTPROXY_ALLOW_HTTP:-no}" ACME_CERTS_DIR="${ACME_CERTS_DIR:-/var/etc/acme-client/certs}" ACME_KEYS_DIR="${ACME_KEYS_DIR:-/var/etc/acme-client/keys}" CONFIG_FILE="${RUSTPROXY_ACME_CONFIG:-/usr/local/etc/rustproxy-acme.conf}" MODE=deploy while [ $# -gt 0 ]; do case "$1" in --check) MODE=check ;; --list) MODE=list ;; --config) shift; CONFIG_FILE="${1:?--config needs a file}" ;; -h|--help) printf 'Usage: %s [--check|--list] [--config FILE]\n' "$0"; exit 0 ;; *) printf 'Unknown option: %s\n' "$1" >&2; exit 2 ;; esac shift done log() { logger -t rustproxy-acme -- "$*" 2>/dev/null || true printf '%s\n' "$*" } fail() { log "ERROR: $*" exit 1 } case "$CONFIG_FILE" in */*) ;; *) CONFIG_FILE="./$CONFIG_FILE" ;; esac if [ -r "$CONFIG_FILE" ]; then . "$CONFIG_FILE" elif [ -e "$CONFIG_FILE" ]; then fail "Cannot read config: $CONFIG_FILE" fi OPENSSL="${OPENSSL:-$(command -v openssl || true)}" [ -n "$OPENSSL" ] || fail "openssl not found" cert_names() { "$OPENSSL" x509 -in "$1" -noout -text 2>/dev/null \ | tr ',' '\n' | sed -n 's/^[[:space:]]*DNS:\([^[:space:]]*\).*/\1/p' \ | tr 'A-Z' 'a-z' "$OPENSSL" x509 -in "$1" -noout -subject 2>/dev/null \ | sed -n 's/.*CN[[:space:]]*=[[:space:]]*\([^,/]*\).*/\1/p' | tr 'A-Z' 'a-z' } cert_end() { end=$("$OPENSSL" x509 -in "$1" -noout -enddate 2>/dev/null | sed 's/^notAfter=//') date -j -f '%b %e %T %Y %Z' "$end" +%s 2>/dev/null \ || date -d "$end" +%s 2>/dev/null \ || echo 0 } if [ "$MODE" = list ]; then found=no for cert in "$ACME_CERTS_DIR"/*/cert.pem; do [ -r "$cert" ] || continue found=yes id=$(basename "$(dirname "$cert")") names=$(cert_names "$cert" | sort -u | tr '\n' ' ') until=$("$OPENSSL" x509 -in "$cert" -noout -enddate 2>/dev/null | sed 's/^notAfter=//') printf '%s %s (valid until %s)\n' "$id" "$names" "$until" done [ "$found" = yes ] || printf 'No os-acme-client certificates in %s\n' "$ACME_CERTS_DIR" exit 0 fi [ -n "$RUSTPROXY_URL" ] || fail "Set RUSTPROXY_URL (in $CONFIG_FILE or at the top of this script)" [ -n "$RUSTPROXY_KEY_ID" ] || fail "Set RUSTPROXY_KEY_ID (Settings -> Certificate API key id)" [ -n "$RUSTPROXY_TOKEN" ] || fail "Set RUSTPROXY_TOKEN (Settings -> Certificate API token)" CURL="${CURL:-$(command -v curl || true)}" [ -n "$CURL" ] || fail "curl not found" case "$RUSTPROXY_KEY_ID" in *[!A-Za-z0-9_-]*) fail "RUSTPROXY_KEY_ID may contain only A-Z, a-z, 0-9, _ and -" ;; esac case "$RUSTPROXY_TOKEN" in *[!A-Za-z0-9._~-]*) fail "RUSTPROXY_TOKEN contains unexpected characters (copy it again from RustProxy, without quotes or spaces)" ;; esac [ "${#RUSTPROXY_TOKEN}" -ge 32 ] && [ "${#RUSTPROXY_TOKEN}" -le 128 ] \ || fail "RUSTPROXY_TOKEN must be 32-128 characters long" BASE_URL="${RUSTPROXY_URL%/}" case "$BASE_URL" in https://*) SCHEME=https ;; http://*) SCHEME=http ;; *) fail "RUSTPROXY_URL must start with https:// or http://" ;; esac case "$BASE_URL" in *://*/*) fail "RUSTPROXY_URL must be the panel origin only, e.g. https://rustproxy.lan:9090" ;; esac HOST_PORT="${BASE_URL#*://}" HOST_ONLY="${HOST_PORT%:*}" case "$HOST_ONLY" in \[*) HOST_ONLY="${HOST_PORT%%]*}]" ;; esac if [ "$SCHEME" = http ]; then case "$HOST_ONLY" in 127.*|localhost|\[::1\]) ;; *) [ "$RUSTPROXY_ALLOW_HTTP" = yes ] || fail "RUSTPROXY_URL uses http:// to a remote host. RustProxy accepts certificate API calls over plain HTTP only from loopback (HTTP 403 certificate_api.https_required) and the token would travel unencrypted. Use https:// (for a self-signed panel RUSTPROXY_TLS_VERIFY=no, ideally with RUSTPROXY_PINNED_PUBKEY), or set RUSTPROXY_ALLOW_HTTP=yes when a trusted reverse proxy in front of RustProxy terminates HTTPS." log "WARNING: sending the certificate API token over plain HTTP" ;; esac fi case "$RUSTPROXY_TLS_VERIFY" in yes|no) ;; *) fail "RUSTPROXY_TLS_VERIFY must be yes or no" ;; esac if [ -n "$RUSTPROXY_CACERT" ]; then [ -r "$RUSTPROXY_CACERT" ] || fail "CA file not readable: $RUSTPROXY_CACERT" fi if [ -n "$RUSTPROXY_PINNED_PUBKEY" ]; then case "$RUSTPROXY_PINNED_PUBKEY" in sha256//*) ;; *) fail "RUSTPROXY_PINNED_PUBKEY must look like sha256//BASE64 (see the README for the openssl command)" ;; esac fi if [ "$SCHEME" = https ] && [ "$RUSTPROXY_TLS_VERIFY" = no ] && [ -z "$RUSTPROXY_PINNED_PUBKEY" ]; then log "WARNING: TLS certificate of $BASE_URL is not verified; set RUSTPROXY_PINNED_PUBKEY to pin it" fi TMP_DIR=$(mktemp -d "${TMPDIR:-/tmp}/rustproxy-acme.XXXXXX") || fail "mktemp failed" trap 'rm -rf "$TMP_DIR"' EXIT HUP INT TERM find_acme_cert() { wanted=$(printf '%s' "$1" | tr 'A-Z' 'a-z') best="" best_end=0 for cert in "$ACME_CERTS_DIR"/*/cert.pem; do [ -r "$cert" ] || continue cert_names "$cert" | grep -Fqx -- "$wanted" || continue id=$(basename "$(dirname "$cert")") [ -r "$ACME_KEYS_DIR/$id/private.key" ] || continue end=$(cert_end "$cert") if [ -z "$best" ] || [ "$end" -gt "$best_end" ]; then best="$id" best_end="$end" fi done [ -n "$best" ] || return 1 printf '%s\n' "$best" } pem_json() { awk '{ gsub(/\\/, "\\\\"); gsub(/\"/, "\\\""); sub(/\r$/, ""); printf "%s\\n", $0; }' "$1" } hint() { case "$1" in certificate_api.deploy_forbidden) echo "The key may only reload certificates. In RustProxy Settings -> Certificate API edit key $RUSTPROXY_KEY_ID and enable 'Also allow deploy' (keys created before 0.28.2 were migrated as reload-only)." ;; certificate_api.certificate_forbidden) echo "Key $RUSTPROXY_KEY_ID is not allowed to use certificate id $2. Add the id to the key's allowed certificate IDs in Settings -> Certificate API (* is not accepted when saving a key)." ;; certificate_api.https_required) echo "RustProxy accepts certificate API calls over plain HTTP only from loopback. Use https:// in RUSTPROXY_URL." ;; certificate_api.invalid_token|api_token.invalid_token) echo "Key id or token is wrong, revoked or regenerated. Copy both again from Settings -> Certificate API." ;; certificate_api.invalid_route) echo "Wrong URL path: RUSTPROXY_URL must be the panel origin only." ;; auth.invalid_host) echo "Use a name from Settings -> Administration (public panel HTTPS origin or additional allowed DNS hostnames), with RUSTPROXY_RESOLVE=name:port:address to reach it by IP." ;; auth.invalid_origin) echo "The scheme or host does not match the public panel HTTPS origin or the trusted proxy settings. Use exactly that origin (https://name:port) from Settings -> Administration." ;; *) echo "See the RustProxy audit log and the response above." ;; esac } deploy() { certificate_id="$1" cert_file="$2" key_file="$3" case "$certificate_id" in ''|*[!A-Za-z0-9_-]*) log "ERROR: certificate id '$certificate_id' may contain only A-Z, a-z, 0-9, _ and -"; return 1 ;; esac [ "${#certificate_id}" -le 64 ] || { log "ERROR: certificate id $certificate_id is longer than 64 characters"; return 1; } [ -r "$cert_file" ] || { log "ERROR: certificate not readable: $cert_file"; return 1; } [ -r "$key_file" ] || { log "ERROR: private key not readable: $key_file"; return 1; } "$OPENSSL" x509 -in "$cert_file" -noout >/dev/null 2>&1 || { log "ERROR: invalid certificate PEM: $cert_file"; return 1; } "$OPENSSL" pkey -in "$key_file" -noout >/dev/null 2>&1 || { log "ERROR: invalid private key PEM: $key_file"; return 1; } cert_pub=$("$OPENSSL" x509 -in "$cert_file" -pubkey -noout 2>/dev/null | "$OPENSSL" pkey -pubin -outform DER 2>/dev/null | "$OPENSSL" dgst -sha256 2>/dev/null) key_pub=$("$OPENSSL" pkey -in "$key_file" -pubout -outform DER 2>/dev/null | "$OPENSSL" dgst -sha256 2>/dev/null) [ -n "$cert_pub" ] && [ "$cert_pub" = "$key_pub" ] || { log "ERROR: certificate and private key do not match: $cert_file"; return 1; } "$OPENSSL" x509 -in "$cert_file" -checkend 0 -noout >/dev/null 2>&1 || { log "ERROR: certificate is already expired: $cert_file"; return 1; } [ "$(wc -c < "$cert_file" | tr -d ' ')" -le 131072 ] && [ "$(wc -c < "$key_file" | tr -d ' ')" -le 131072 ] \ || { log "ERROR: certificate and key must each be at most 128 KiB"; return 1; } if [ "$MODE" = check ]; then log "Check OK: id=$certificate_id cert=$cert_file names=$(cert_names "$cert_file" | sort -u | tr '\n' ' ')" return 0 fi json="$TMP_DIR/body.json" curl_config="$TMP_DIR/curl.conf" response="$TMP_DIR/response" { printf '{"certificate_id":"%s","cert_pem":"' "$certificate_id" pem_json "$cert_file" printf '","key_pem":"' pem_json "$key_file" printf '"}\n' } > "$json" cat > "$curl_config" <> "$curl_config" [ -z "$RUSTPROXY_CACERT" ] || printf 'cacert = "%s"\n' "$RUSTPROXY_CACERT" >> "$curl_config" [ -z "$RUSTPROXY_PINNED_PUBKEY" ] || printf 'pinnedpubkey = "%s"\n' "$RUSTPROXY_PINNED_PUBKEY" >> "$curl_config" fi [ -z "$RUSTPROXY_RESOLVE" ] || printf 'resolve = "%s"\n' "$RUSTPROXY_RESOLVE" >> "$curl_config" curl_exit=0 http_code=$("$CURL" --config "$curl_config") || curl_exit=$? rm -f "$json" if [ "$curl_exit" -ne 0 ]; then case "$curl_exit" in 6) log "ERROR: cannot resolve the host in RUSTPROXY_URL (use RUSTPROXY_RESOLVE=host:port:address)" ;; 7|28) log "ERROR: cannot connect to $BASE_URL (curl exit $curl_exit): check address, port and firewall" ;; 35) log "ERROR: TLS handshake failed with $BASE_URL (curl exit 35): check that the panel listens with https://" ;; 60|77) log "ERROR: TLS certificate of $BASE_URL is not trusted (curl exit $curl_exit): set RUSTPROXY_CACERT, RUSTPROXY_PINNED_PUBKEY or RUSTPROXY_TLS_VERIFY=no" ;; 51) log "ERROR: TLS certificate of $BASE_URL does not match the host name (curl exit 51): use the name from the certificate with RUSTPROXY_RESOLVE, or RUSTPROXY_TLS_VERIFY=no with RUSTPROXY_PINNED_PUBKEY" ;; 90) log "ERROR: TLS public key of $BASE_URL does not match RUSTPROXY_PINNED_PUBKEY (curl exit 90)" ;; *) log "ERROR: request to $BASE_URL failed (curl exit $curl_exit)" ;; esac return 1 fi error_code=$(sed -n 's/.*"error"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' "$response" | head -n 1) case "$http_code" in 2??) log "RustProxy certificate reconcile succeeded (HTTP $http_code, id=$certificate_id)" cat "$response" printf '\n' ;; *) log "RustProxy certificate deploy failed (HTTP $http_code${error_code:+, $error_code}, id=$certificate_id)" cat "$response" >&2 printf '\n' >&2 log "Hint: $(hint "$error_code" "$certificate_id")" return 1 ;; esac } deploy_named() { name="$1" certificate_id="$2" if ! id=$(find_acme_cert "$name"); then log "ERROR: no os-acme-client certificate for $name in $ACME_CERTS_DIR (run with --list to see the available ones)" return 1 fi deploy "$certificate_id" "$ACME_CERTS_DIR/$id/fullchain.pem" "$ACME_KEYS_DIR/$id/private.key" } status=0 count=0 if [ -n "$RUSTPROXY_CERTIFICATES" ]; then set -f set -- $RUSTPROXY_CERTIFICATES set +f for entry in "$@"; do case "$entry" in *=*) ;; *) log "ERROR: RUSTPROXY_CERTIFICATES entry '$entry' must be acme-name=rustproxy-id"; status=1; continue ;; esac count=$((count + 1)) deploy_named "${entry%%=*}" "${entry#*=}" || status=1 done fi if [ -n "$CERTIFICATE_ID" ]; then count=$((count + 1)) if [ -n "$CERT_FILE" ] || [ -n "$KEY_FILE" ]; then [ -n "$CERT_FILE" ] && [ -n "$KEY_FILE" ] || fail "Set both CERT_FILE and KEY_FILE, or ACME_CERT_NAME instead" deploy "$CERTIFICATE_ID" "$CERT_FILE" "$KEY_FILE" || status=1 elif [ -n "$ACME_CERT_NAME" ]; then deploy_named "$ACME_CERT_NAME" "$CERTIFICATE_ID" || status=1 else fail "Set ACME_CERT_NAME (or CERT_FILE and KEY_FILE) for CERTIFICATE_ID=$CERTIFICATE_ID" fi fi [ "$count" -gt 0 ] || fail "Nothing to deploy: set RUSTPROXY_CERTIFICATES, or CERTIFICATE_ID with ACME_CERT_NAME" exit "$status"