#!/bin/sh # renewal-watch-agent.sh: report TLS certificates the watch service cannot # reach itself (internal services, VPNs, appliances, mail ports, hosts on # the service's own network) by pushing them from inside your network. # # For each target it connects with openssl, keeps only the certificates the # server sends (public data, the same any client sees) and posts them with # curl to the service's push endpoint, under the target's name. Nothing else # leaves the machine: no private key, no configuration, no host details. # Run it with --dry-run to see exactly what would be sent. # # POSIX sh (busybox sh, dash, bash). Needs openssl and curl. MIT licence, # see LICENSE next to this file. # # Settings (environment): # RW_URL the service's address, https://... (required to send) # RW_TOKEN the agent token from the dashboard, or # RW_TOKEN_FILE a file holding it (default /etc/renewal-watch/token) # RW_TARGETS the targets file (default /etc/renewal-watch/targets) # RW_INTERVAL seconds between your runs, 3600 to 604800; the service # reports the agent silent after three intervals without # a push (it assumes 86400, once a day, when unset) # RW_TIMEOUT seconds per connection (default 15) # # Targets file, one target per line, "#" starts a comment: # # address:port [starttls] [name] # # intranet.example.lan:443 # mail.example.com:25 smtp # 10.0.0.5:8443 - nas.example.lan # # starttls: smtp, imap, pop3, ftp, xmpp, xmpp-server, irc, postgres, mysql, # lmtp, nntp, sieve or ldap, as openssl s_client -starttls takes them; # "-" (or nothing) for TLS from the first byte. # name: what the service calls the endpoint, host or host:port (default: # address:port). Its host part is also the name sent as SNI. # # Usage: # renewal-watch-agent.sh [--targets FILE] [--dry-run] # renewal-watch-agent.sh --file chain.pem --name host[:port] [--dry-run] # # --file reads certificates from disk instead (for example a fullchain.pem); # only the CERTIFICATE blocks are read, so a file that also holds a private # key never sends it. # # Exit codes: 0 everything pushed (or shown), 1 at least one target failed, # 2 usage or settings, 3 openssl or curl missing, 4 the token was refused. set -u umask 077 VERSION=1 STARTTLS=" smtp imap pop3 ftp xmpp xmpp-server irc postgres mysql lmtp nntp sieve ldap " say() { printf '%s\n' "$*"; } warn() { printf '%s\n' "$*" >&2; } usage() { say "Usage: renewal-watch-agent.sh [--targets FILE] [--dry-run]" say " renewal-watch-agent.sh --file chain.pem --name host[:port] [--dry-run]" say "Settings: RW_URL, RW_TOKEN or RW_TOKEN_FILE, RW_TARGETS, RW_INTERVAL, RW_TIMEOUT." say "The comment at the top of the script explains each." } fail_usage() { warn "$1" usage >&2 exit 2 } need_value() { [ "$2" -ge 2 ] || fail_usage "$1 needs a value." } targets=${RW_TARGETS:-/etc/renewal-watch/targets} file= name_opt= dry=0 while [ $# -gt 0 ]; do case $1 in -t | --targets) need_value "$1" $# targets=$2 shift 2 ;; -f | --file) need_value "$1" $# file=$2 shift 2 ;; -n | --name) need_value "$1" $# name_opt=$2 shift 2 ;; --dry-run) dry=1 shift ;; -h | --help) usage exit 0 ;; -V | --version) say "renewal-watch-agent $VERSION" exit 0 ;; *) fail_usage "Unknown option: $1" ;; esac done command -v openssl >/dev/null 2>&1 || { warn "openssl is missing." exit 3 } timeout_s=${RW_TIMEOUT:-15} case $timeout_s in '' | *[!0-9]*) fail_usage "RW_TIMEOUT must be a number of seconds." ;; esac interval=${RW_INTERVAL:-} case $interval in *[!0-9]*) fail_usage "RW_INTERVAL must be a number of seconds." ;; esac url=${RW_URL:-} url=${url%/} token= if [ "$dry" = 0 ]; then command -v curl >/dev/null 2>&1 || { warn "curl is missing." exit 3 } case $url in https://?*) ;; # Plain HTTP only to this machine, for trying the agent out. http://127.0.0.1 | http://127.0.0.1:* | http://localhost | http://localhost:*) ;; '') fail_usage "Set RW_URL to the service's address (https://...)." ;; *) fail_usage "RW_URL must start with https://." ;; esac token=${RW_TOKEN:-} if [ -z "$token" ]; then token_file=${RW_TOKEN_FILE:-/etc/renewal-watch/token} if [ -r "$token_file" ]; then token=$(head -n 1 "$token_file" | tr -d ' \t\r\n') fi fi case $token in '') fail_usage "Set RW_TOKEN, or put the token into RW_TOKEN_FILE." ;; *[!A-Za-z0-9_-]*) fail_usage "The token contains characters a token never has." ;; esac fi has_timeout= if command -v timeout >/dev/null 2>&1; then has_timeout=1 fi tmp=$(mktemp -d "${TMPDIR:-/tmp}/rw-agent.XXXXXX") || { warn "Cannot create a temporary directory." exit 2 } trap 'rm -rf "$tmp"' EXIT trap 'exit 130' INT TERM if [ -n "$token" ]; then # A header file keeps the token out of the process list. printf 'Authorization: Bearer %s\n' "$token" >"$tmp/auth" fi limited() { if [ -n "$has_timeout" ]; then timeout "$timeout_s" "$@" else "$@" fi } # Only the certificate blocks of a PEM stream: nothing else is ever sent. certs_only() { sed -n '/^-----BEGIN CERTIFICATE-----/,/^-----END CERTIFICATE-----/p' } # The host part of a name as SNI; nothing for IP addresses. sni_of() { case $1 in \[*) return 0 ;; esac sni_host=${1%%:*} case $sni_host in *[!0-9.]*) printf '%s' "$sni_host" ;; esac } # fetch ADDRESS:PORT STARTTLS SNI OUT: the served chain as PEM. fetch() { fetch_connect=$1 fetch_proto=$2 fetch_sni=$3 fetch_out=$4 set -- s_client -connect "$fetch_connect" -showcerts if [ -n "$fetch_sni" ]; then set -- "$@" -servername "$fetch_sni" fi if [ "$fetch_proto" != - ]; then set -- "$@" -starttls "$fetch_proto" fi limited openssl "$@" "$tmp/err" | certs_only >"$fetch_out" [ -s "$fetch_out" ] } urlencode_name() { printf '%s' "$1" | sed -e 's/:/%3A/g' -e 's/\[/%5B/g' -e 's/\]/%5D/g' } json_field() { sed -n "s/.*\"$1\": *\"\\([^\"]*\\)\".*/\\1/p" "$2" | head -n 1 } # send NAME PEMFILE: push one chain (or show it with --dry-run). send() { send_count=$(grep -c 'BEGIN CERTIFICATE' "$2") query="name=$(urlencode_name "$1")" if [ -n "$interval" ]; then query="$query&interval=$interval" fi if [ "$dry" = 1 ]; then send_leaf=$(openssl x509 -noout -subject -enddate -in "$2" 2>/dev/null | tr '\n' ' ') say "$1: $send_count certificate(s); leaf: $send_leaf" say " would send: POST ${url:-}/api/v1/push?$query with these certificates, nothing else" return 0 fi send_code=$(curl -sS -g --max-time 60 --proto '=https,http' \ -o "$tmp/response" -w '%{http_code}' \ -A "renewal-watch-agent/$VERSION" \ -H @"$tmp/auth" -H 'Content-Type: application/x-pem-file' -H 'Expect:' \ --data-binary @"$2" "$url/api/v1/push?$query" "$tmp/curl-err") || send_code=000 case $send_code in 200) say "$1: $(json_field state "$tmp/response")" return 0 ;; 401) warn "$1: the service refused the token ($(json_field error "$tmp/response"))." exit 4 ;; 000) warn "$1: could not reach the service: $(head -n 1 "$tmp/curl-err")" ;; *) send_error=$(json_field error "$tmp/response") warn "$1: refused with HTTP $send_code${send_error:+: $send_error}" ;; esac return 1 } # One line of the targets file, split into words. target_line() { t_target=$1 t_proto=${2:--} t_name=${3:-} if [ $# -gt 3 ]; then warn "$t_target: too many words on the line; expected address:port [starttls] [name]." return 1 fi case $t_target in \[*\]) t_addr=$t_target t_port=443 ;; \[*\]:*) t_addr=${t_target%:*} t_port=${t_target##*:} ;; *:*:*) warn "$t_target: write IPv6 addresses in brackets, [address]:port." return 1 ;; *:*) t_addr=${t_target%:*} t_port=${t_target##*:} ;; *) t_addr=$t_target t_port=443 ;; esac case $t_port in '' | *[!0-9]*) warn "$t_target: the port must be a number." return 1 ;; esac case $t_addr in '' | *[!]A-Za-z0-9.:_[-]*) warn "$t_target: not a hostname or address." return 1 ;; esac if [ "$t_proto" != - ]; then case $STARTTLS in *" $t_proto "*) ;; *) warn "$t_target: unknown STARTTLS protocol $t_proto (use - for none)." return 1 ;; esac fi t_name=${t_name:-$t_addr:$t_port} case $t_name in *[!]A-Za-z0-9.:_[-]*) warn "$t_name: a name is host or host:port." return 1 ;; esac if ! fetch "$t_addr:$t_port" "$t_proto" "$(sni_of "$t_name")" "$tmp/chain.pem"; then warn "$t_name: no certificates read from $t_addr:$t_port: $(tail -n 1 "$tmp/err" 2>/dev/null)" return 1 fi send "$t_name" "$tmp/chain.pem" } failed=0 if [ -n "$file" ]; then [ -n "$name_opt" ] || fail_usage "--file needs --name host[:port]." case $name_opt in *[!]A-Za-z0-9.:_[-]*) fail_usage "A name is host or host:port." ;; esac [ -r "$file" ] || fail_usage "Cannot read $file." certs_only <"$file" >"$tmp/chain.pem" if [ ! -s "$tmp/chain.pem" ]; then warn "$file: no PEM certificates in it." exit 1 fi send "$name_opt" "$tmp/chain.pem" || failed=1 else [ -r "$targets" ] || fail_usage "Cannot read the targets file $targets (set RW_TARGETS or --targets)." count=0 while IFS= read -r line || [ -n "$line" ]; do line=${line%%#*} set -f # Word splitting on purpose: address, protocol and name. # shellcheck disable=SC2086 set -- $line set +f [ $# -eq 0 ] && continue count=$((count + 1)) target_line "$@" || failed=1 done <"$targets" if [ "$count" -eq 0 ]; then warn "No targets in $targets." exit 2 fi fi exit "$failed"