For hosts we cannot reach

Push agent

Some endpoints are not reachable from the internet: internal services, VPN-only hosts, appliances, mail ports behind a firewall. A small shell script on a machine that can reach them reads their certificates and sends them to your watch. They are then evaluated exactly like the endpoints we check ourselves.

What it sends

Only the certificates each server sends during the TLS handshake (public data, the same any client sees) and the name you give the endpoint. Never a private key, a password, a configuration file or anything about the machine it runs on. With --file it reads only the certificate blocks of a file, so a file that also holds a private key never sends it. Run it with --dry-run to see exactly what it would send.

It is about 350 lines of POSIX shell, a fifth of them comments, (busybox sh, dash, bash) and needs openssl and curl. MIT licence. The full script is at the end of this page; read it before you run it.

1. Get a token

Sign in, then create one on the dashboard. It is shown once and lets the agent report endpoints into your account, nothing else. Each endpoint the agent reports counts toward your plan's endpoints.

2. Install

As root, on a machine that can reach the endpoints:

curl -fsSo /usr/local/bin/renewal-watch-agent.sh https://ttlwatch.com/agent/renewal-watch-agent.sh
chmod 755 /usr/local/bin/renewal-watch-agent.sh
mkdir -p /etc/renewal-watch
printf '%s\n' 'YOUR-TOKEN' > /etc/renewal-watch/token
chmod 600 /etc/renewal-watch/token

3. List the endpoints

The agent reads them from a plain text file, /etc/renewal-watch/targets, which you create yourself. One endpoint per line: the address and port to connect to, optionally a STARTTLS protocol (smtp, imap, pop3, or - for none), optionally the name the endpoint gets in your watch (the name's host part is also sent as SNI). Lines starting with # are comments. Create it with your own endpoints in place of these examples:

cat > /etc/renewal-watch/targets <<'EOF'
# address:port  [starttls]  [name]
intranet.example.lan:443
mail.example.com:25 smtp
mail.example.com:993
10.0.0.5:8443 - nas.example.lan
EOF

Or write it with any editor. The agent reads it on every run, so adding an endpoint later is one more line. A file elsewhere works too: point RW_TARGETS or --targets at it.

Then try it. --dry-run connects and shows what it would send, and sends nothing:

RW_URL=https://ttlwatch.com renewal-watch-agent.sh --dry-run
RW_URL=https://ttlwatch.com renewal-watch-agent.sh

4. Run it every day

Once a day is enough for certificates of 10 days or more. If an endpoint is not pushed for three of the agent's intervals, its endpoint reports the agent as silent. If you run it more or less often, set RW_INTERVAL to the time between runs in seconds (3600 to 604800).

cron

# /etc/cron.d/renewal-watch-agent
17 6 * * * root RW_URL=https://ttlwatch.com /usr/local/bin/renewal-watch-agent.sh >/dev/null

systemd timer

# /etc/systemd/system/renewal-watch-agent.service
[Unit]
Description=Push TLS certificates to TTLwatch
Wants=network-online.target
After=network-online.target

[Service]
Type=oneshot
Environment=RW_URL=https://ttlwatch.com
ExecStart=/usr/local/bin/renewal-watch-agent.sh
DynamicUser=yes
LoadCredential=token:/etc/renewal-watch/token
Environment=RW_TOKEN_FILE=%d/token

# /etc/systemd/system/renewal-watch-agent.timer
[Unit]
Description=Push TLS certificates to TTLwatch daily

[Timer]
OnCalendar=daily
RandomizedDelaySec=1h
Persistent=true

[Install]
WantedBy=timers.target
systemctl daemon-reload
systemctl enable --now renewal-watch-agent.timer

Docker

Without installing anything on the host: Alpine with openssl and curl, the script and the two files mounted read-only. --network host lets it reach what the host reaches.

docker run --rm --network host \
  -e RW_URL=https://ttlwatch.com \
  -v /usr/local/bin/renewal-watch-agent.sh:/agent.sh:ro \
  -v /etc/renewal-watch:/etc/renewal-watch:ro \
  alpine:3.22 sh -c 'apk add --no-cache -q openssl curl && sh /agent.sh'

Settings and exit codes

Exit codes: 0 all pushed, 1 at least one endpoint failed, 2 a usage or settings problem, 3 openssl or curl missing, 4 the token was refused. Errors go to standard error, so cron mails you only when something failed.

One token can push up to 60 times an hour. A name that we already check from our side cannot be pushed as well; push it under another name or remove it first.

The script

Download renewal-watch-agent.sh (MIT licence).

#!/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 "$@" </dev/null 2>"$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:-<RW_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" </dev/null 2>"$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"