# -*- sh -*- # run as root!
# letsencrypt for Ubuntu 22.04 and 24.04
# based on https://certbot.eff.org/instructions?ws=other&os=devuanbeowulf

# https://letsencrypt.org/getting-started/
# https://certbot.eff.org/docs/install.html
# https://certbot.eff.org/instructions?ws=other&os=devuanother
# https://eff-certbot.readthedocs.io/en/stable/install.html
# https://community.letsencrypt.org/t/certbot-auto-no-longer-works-on-debian-based-systems/139702/7
# https://certbot.eff.org/docs/using.html?highlight=webroot%20path#webroot

# letsencrypt certbot needs http on port 80 ==>
# do NOT deactivate http!

## Needed for RHEL:
# yum install certbot openssl
# cat >/etc/cron.d/certbot <<EOD
# SHELL=/bin/sh
# PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# 
# 0 */12 * * * root test -x /usr/bin/certbot && perl -e 'sleep int(rand(43200))' && certbot -q renew
# EOD

set -e

apt install certbot openssl
PATH="/home/fex/bin:$PATH"
cd /home/fex
cp -av letsencrypt /etc/
DEPLOY=/etc/letsencrypt/renewal-hooks/deploy/fex
PRE=/etc/letsencrypt/renewal-hooks/pre/fex
ll $DEPLOY $PRE

ll /etc/cron.d/certbot /lib/systemd/system/certbot*
systemctl list-unit-files | grep certbot
systemctl cat certbot.service
systemctl cat certbot.timer
# systemctl show certbot.timer

$PRE

eval $(fac -V)
#servers=fex.rus.uni-stuttgart.de,fex4.rus.uni-stuttgart.de,fex6.rus.uni-stuttgart.de
servers=$FEX_SERVER
certbot certonly --webroot -w /home/fex/htdocs -d $servers -m $FEX_ADMIN --dry-run --test-cert
certbot certonly --webroot -w /home/fex/htdocs -d $servers -m $FEX_ADMIN
sleep 30
ll /etc/letsencrypt/renewal/$FEX_SERVER.conf && $DEPLOY

openssl s_client -verify 1 -connect $FEX_SERVER:443 </dev/null 2>/dev/null|openssl x509 -text | grep -E 'Issuer:|Before|After|DNS:'

certbot renew --dry-run

# root@fex:/etc/letsencrypt# certbot certonly --webroot -w /home/fex/htdocs -d $servers -m $FEX_ADMIN
# Saving debug log to /var/log/letsencrypt/letsencrypt.log
# 
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Please read the Terms of Service at
# https://letsencrypt.org/documents/LE-SA-v1.3-September-21-2022.pdf. You must
# agree in order to register with the ACME server. Do you agree?
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# (Y)es/(N)o: y
# 
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Would you be willing, once your first certificate is successfully issued, to
# share your email address with the Electronic Frontier Foundation, a founding
# partner of the Let's Encrypt project and the non-profit organization that
# develops Certbot? We'd like to send you email about our work encrypting the web,
# EFF news, campaigns, and ways to support digital freedom.
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# (Y)es/(N)o: n
# Account registered.
# Requesting a certificate for fex.rus.uni-stuttgart.de and 2 more domains
# 
# Successfully received certificate.
# Certificate is saved at: /etc/letsencrypt/live/fex.rus.uni-stuttgart.de/fullchain.pem
# Key is saved at:         /etc/letsencrypt/live/fex.rus.uni-stuttgart.de/privkey.pem
# This certificate expires on 2024-01-14.
# These files will be updated when the certificate renews.
# Certbot has set up a scheduled task to automatically renew this certificate in the background.
# 
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# If you like Certbot, please consider supporting our work by:
#  * Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
#  * Donating to EFF:                    https://eff.org/donate-le
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# CONNECT:80 2023-10-16 16:17:58 - 2600:1f16:13c:c401:8c90:d3cd:8092:2b0b [551312_0]
# GET /.well-known/acme-challenge/beleuTe45BJ9sGjYOxi8xddbRreq18QBe54xg5GJqmg HTTP/1.1
# Host: fex.rus.uni-stuttgart.de
# User-Agent: Mozilla/5.0 (compatible; Let's Encrypt validation server; +https://www.letsencrypt.org)
# Accept: */*
# Accept-Encoding: gzip
# Connection: close
# 
# CONNECT:80 2023-10-16 16:17:58 ec2-3-140-252-119.us-east-2.compute.amazonaws.com 3.140.252.119 [551314_0]
# GET /.well-known/acme-challenge/eeacmcjpy-KycBa69bYj077_fL3eXgG66UztuxIIdbw HTTP/1.1
# Host: fex4.rus.uni-stuttgart.de
# User-Agent: Mozilla/5.0 (compatible; Let's Encrypt validation server; +https://www.letsencrypt.org)
# Accept: */*
# Accept-Encoding: gzip
# Connection: close
# 
# CONNECT:80 2023-10-16 16:17:58 - 2600:1f16:13c:c401:8c90:d3cd:8092:2b0b [551313_0]
# GET /.well-known/acme-challenge/jDqm4_6JncaX28z9pS_NDxFZh1LYilG2d86GYbezww4 HTTP/1.1
# Host: fex6.rus.uni-stuttgart.de
# User-Agent: Mozilla/5.0 (compatible; Let's Encrypt validation server; +https://www.letsencrypt.org)
# Accept: */*
# Accept-Encoding: gzip
# Connection: close
# 
# CONNECT:80 2023-10-16 16:17:58 ec2-34-220-86-115.us-west-2.compute.amazonaws.com 34.220.86.115 [551318_0]
# GET /.well-known/acme-challenge/eeacmcjpy-KycBa69bYj077_fL3eXgG66UztuxIIdbw HTTP/1.1
# Host: fex4.rus.uni-stuttgart.de
# User-Agent: Mozilla/5.0 (compatible; Let's Encrypt validation server; +https://www.letsencrypt.org)
# Accept: */*
# Accept-Encoding: gzip
# Connection: close
# 
# CONNECT:80 2023-10-16 16:17:58 - 2600:1f14:a8b:500:fc0b:a938:59d3:9d6c [551319_0]
# GET /.well-known/acme-challenge/beleuTe45BJ9sGjYOxi8xddbRreq18QBe54xg5GJqmg HTTP/1.1
# Host: fex.rus.uni-stuttgart.de
# User-Agent: Mozilla/5.0 (compatible; Let's Encrypt validation server; +https://www.letsencrypt.org)
# Accept: */*
# Accept-Encoding: gzip
# Connection: close
# 
# CONNECT:80 2023-10-16 16:17:58 - 2600:1f14:a8b:500:fc0b:a938:59d3:9d6c [551320_0]
# GET /.well-known/acme-challenge/jDqm4_6JncaX28z9pS_NDxFZh1LYilG2d86GYbezww4 HTTP/1.1
# Host: fex6.rus.uni-stuttgart.de
# User-Agent: Mozilla/5.0 (compatible; Let's Encrypt validation server; +https://www.letsencrypt.org)
# Accept: */*
# Accept-Encoding: gzip
# Connection: close
# 
# CONNECT:80 2023-10-16 16:17:59 outbound1g.letsencrypt.org 23.178.112.106 [551325_0]
# GET /.well-known/acme-challenge/eeacmcjpy-KycBa69bYj077_fL3eXgG66UztuxIIdbw HTTP/1.1
# Host: fex4.rus.uni-stuttgart.de
# User-Agent: Mozilla/5.0 (compatible; Let's Encrypt validation server; +https://www.letsencrypt.org)
# Accept: */*
# Accept-Encoding: gzip
# Connection: close
# 
# CONNECT:80 2023-10-16 16:17:59 - 2600:3000:2710:300::21 [551324_0]
# GET /.well-known/acme-challenge/jDqm4_6JncaX28z9pS_NDxFZh1LYilG2d86GYbezww4 HTTP/1.1
# Host: fex6.rus.uni-stuttgart.de
# User-Agent: Mozilla/5.0 (compatible; Let's Encrypt validation server; +https://www.letsencrypt.org)
# Accept: */*
# Accept-Encoding: gzip
# Connection: close
# 
# CONNECT:80 2023-10-16 16:18:09 outbound1.letsencrypt.org 66.133.109.36 [551331_0]
# GET /.well-known/acme-challenge/beleuTe45BJ9sGjYOxi8xddbRreq18QBe54xg5GJqmg HTTP/1.1
# Host: fex.rus.uni-stuttgart.de
# User-Agent: Mozilla/5.0 (compatible; Let's Encrypt validation server; +https://www.letsencrypt.org)
# Accept: */*
# Accept-Encoding: gzip
# Connection: close
# 
# CONNECT:80 2023-10-16 16:30:59 - 2600:1f16:269:da01:9693:c7ee:698a:d862 [551982_0]
# GET /.well-known/acme-challenge/y05b15gbRelGOAf5wnWtJ-59UJ57D-d4LVv5Yt--WW0 HTTP/1.1
# Host: fex.rus.uni-stuttgart.de
# User-Agent: Mozilla/5.0 (compatible; Let's Encrypt validation server; +https://www.letsencrypt.org)
# Accept: */*
# Accept-Encoding: gzip
# Connection: close
# 
# CONNECT:80 2023-10-16 16:30:59 ec2-3-135-239-243.us-east-2.compute.amazonaws.com 3.135.239.243 [551984_0]
# GET /.well-known/acme-challenge/uzVG-o_e54lTZ5m2V5jxjBGNYniRsLDrWGyeJOc4CLw HTTP/1.1
# Host: fex4.rus.uni-stuttgart.de
# User-Agent: Mozilla/5.0 (compatible; Let's Encrypt validation server; +https://www.letsencrypt.org)
# Accept: */*
# Accept-Encoding: gzip
# Connection: close
# 
# CONNECT:80 2023-10-16 16:30:59 - 2600:1f16:269:da01:9693:c7ee:698a:d862 [551987_0]
# GET /.well-known/acme-challenge/ar8LpvmEiz4PjNR5JhFraxIKQZTeq4DxcRzHmL6tKmU HTTP/1.1
# Host: fex6.rus.uni-stuttgart.de
# User-Agent: Mozilla/5.0 (compatible; Let's Encrypt validation server; +https://www.letsencrypt.org)
# Accept: */*
# Accept-Encoding: gzip
# Connection: close
# 
# CONNECT:80 2023-10-16 16:31:00 - 2600:3000:2710:200::85 [551986_0]
# GET /.well-known/acme-challenge/y05b15gbRelGOAf5wnWtJ-59UJ57D-d4LVv5Yt--WW0 HTTP/1.1
# Host: fex.rus.uni-stuttgart.de
# User-Agent: Mozilla/5.0 (compatible; Let's Encrypt validation server; +https://www.letsencrypt.org)
# Accept: */*
# Accept-Encoding: gzip
# Connection: close
# 
# CONNECT:80 2023-10-16 16:31:00 outbound1c.letsencrypt.org 23.178.112.102 [551989_0]
# GET /.well-known/acme-challenge/uzVG-o_e54lTZ5m2V5jxjBGNYniRsLDrWGyeJOc4CLw HTTP/1.1
# Host: fex4.rus.uni-stuttgart.de
# User-Agent: Mozilla/5.0 (compatible; Let's Encrypt validation server; +https://www.letsencrypt.org)
# Accept: */*
# Accept-Encoding: gzip
# Connection: close
# 
# CONNECT:80 2023-10-16 16:31:00 - 2600:1f14:804:fd00:8b15:ac1:5d66:63a [551991_0]
# GET /.well-known/acme-challenge/ar8LpvmEiz4PjNR5JhFraxIKQZTeq4DxcRzHmL6tKmU HTTP/1.1
# Host: fex6.rus.uni-stuttgart.de
# User-Agent: Mozilla/5.0 (compatible; Let's Encrypt validation server; +https://www.letsencrypt.org)
# Accept: */*
# Accept-Encoding: gzip
# Connection: close
# 
# CONNECT:80 2023-10-16 16:31:00 ec2-35-88-177-87.us-west-2.compute.amazonaws.com 35.88.177.87 [551996_0]
# GET /.well-known/acme-challenge/uzVG-o_e54lTZ5m2V5jxjBGNYniRsLDrWGyeJOc4CLw HTTP/1.1
# Host: fex4.rus.uni-stuttgart.de
# User-Agent: Mozilla/5.0 (compatible; Let's Encrypt validation server; +https://www.letsencrypt.org)
# Accept: */*
# Accept-Encoding: gzip
# Connection: close
# 
# CONNECT:80 2023-10-16 16:31:00 - 2600:1f14:804:fd00:8b15:ac1:5d66:63a [551995_0]
# GET /.well-known/acme-challenge/y05b15gbRelGOAf5wnWtJ-59UJ57D-d4LVv5Yt--WW0 HTTP/1.1
# Host: fex.rus.uni-stuttgart.de
# User-Agent: Mozilla/5.0 (compatible; Let's Encrypt validation server; +https://www.letsencrypt.org)
# Accept: */*
# Accept-Encoding: gzip
# Connection: close
# 
# CONNECT:80 2023-10-16 16:31:00 - 2600:3000:2710:200::82 [551990_0]
# GET /.well-known/acme-challenge/ar8LpvmEiz4PjNR5JhFraxIKQZTeq4DxcRzHmL6tKmU HTTP/1.1
# Host: fex6.rus.uni-stuttgart.de
# User-Agent: Mozilla/5.0 (compatible; Let's Encrypt validation server; +https://www.letsencrypt.org)
# Accept: */*
# Accept-Encoding: gzip
# Connection: close
