|
| 1 | +#!/bin/bash |
| 2 | +function print_cert_info { |
| 3 | + local enddate |
| 4 | + local subject |
| 5 | + local san_str |
| 6 | + |
| 7 | + # Get the wanted informations with OpenSSL. |
| 8 | + issuer="$(openssl x509 -noout -issuer -in "$1" | sed -n 's/.*CN=\(.*\)/\1/p')" |
| 9 | + enddate="$(openssl x509 -noout -enddate -in "$1" | sed -n 's/notAfter=\(.*$\)/\1/p')" |
| 10 | + subject="$(openssl x509 -noout -subject -in "$1" | sed -n 's/.*CN=\([a-z0-9.-]*\)/- \1/p')" |
| 11 | + san_str="$(openssl x509 -text -in "$1" | grep 'DNS:')" |
| 12 | + |
| 13 | + echo "Certificate was issued by $issuer" |
| 14 | + echo "Certificate is valid until $enddate" |
| 15 | + echo "Subject Name:" |
| 16 | + echo "$subject" |
| 17 | + |
| 18 | + # Display the SAN info only if there is more than one SAN domain. |
| 19 | + while IFS=',' read -ra SAN; do |
| 20 | + if [[ ${#SAN[@]} -gt 1 ]]; then |
| 21 | + echo "Subject Alternative Name:" |
| 22 | + for domain in "${SAN[@]}"; do |
| 23 | + echo "$domain" | sed -n 's/.*DNS:\([a-z0-9.-]*\)/- \1/p' |
| 24 | + done |
| 25 | + fi |
| 26 | + done <<< "$san_str" |
| 27 | +} |
| 28 | + |
| 29 | +echo '##### Certificate status #####' |
| 30 | +for cert in /etc/nginx/certs/*/fullchain.pem; do |
| 31 | + [[ -e "$cert" ]] || continue |
| 32 | + # Verify the certificate with OpenSSL. |
| 33 | + openssl verify -CAfile "${cert%fullchain.pem}chain.pem" "$cert" |
| 34 | + |
| 35 | + # Print certificate info. |
| 36 | + print_cert_info "$cert" |
| 37 | + |
| 38 | + # Find the .crt files in /etc/nginx/certs which are |
| 39 | + # symlinks pointing to the current certificate. |
| 40 | + unset symlinked_domains |
| 41 | + for symlink in /etc/nginx/certs/*.crt; do |
| 42 | + [[ -e "$symlink" ]] || continue |
| 43 | + if [[ "$(readlink -f "$symlink")" == "$cert" ]]; then |
| 44 | + domain="$(echo "${symlink%.crt}" | sed 's#/etc/nginx/certs/##g')" |
| 45 | + symlinked_domains+=("$domain") |
| 46 | + fi |
| 47 | + done |
| 48 | + |
| 49 | + # Display symlinks pointing to the current cert if there is any. |
| 50 | + if [[ ${#symlinked_domains[@]} -gt 0 ]]; then |
| 51 | + echo "Certificate is used by the following domain(s):" |
| 52 | + for domain in "${symlinked_domains[@]}"; do |
| 53 | + echo "- $domain" |
| 54 | + done |
| 55 | + fi |
| 56 | + |
| 57 | + echo '##############################' |
| 58 | +done |
0 commit comments