Fixed Speak
[clearscm.git] / bin / certbot_authentication.sh
1 #!/bin/bash
2 ################################################################################
3 #
4 # File:         certbot_authentication.sh
5 # Revision:     1.0
6 # Description:  Perform domain validation by creating a TXT record on the domain
7 #               from certbot. This script is designed to work with
8 #               Dreamhost.com's API and certbot running on Ubuntu 20.04. Note
9 #               that it has not been extended to handle multiple domains.
10 #
11 #               Domain validation is the process of validating you have control
12 #               over a domain. Services like Let's Encrypt can then issue you
13 #               domain validated TLS certificates for use to secure websites.
14 #
15 # See also:     https://help.dreamhost.com/hc/en-us/articles/217555707-DNS-API-commands
16 #
17 # Crontab:      0 0 1 * * certbot renew --manual-auth-hook /path/to/certbot_authentication.sh --manual-cleanup-hook /path/to/certbot_cleanup.sh
18 #
19 # Author:       Andrew@DeFaria.com
20 # Created:      Fri 04 Jun 2021 11:20:16 PDT
21 # Modified:
22 # Language:     Bash
23 #
24 # (c) Copyright 2021, ClearSCM, Inc., all rights reserved
25 #
26 ################################################################################
27 logfile="/tmp/$(basename $0).log"
28 rm -f $logfile
29
30 function log {
31     echo $1 >> $logfile
32 } # log
33
34 log "Starting $0"
35
36 # The following are environment variables that certbot passes to us
37 #
38 # CERTBOT_DOMAIN:     Domain being authenticated.
39 # CERTBOT_VALIDATION: Validation string for domain.
40 #
41 # Check that CERTBOT_DOMAIN and CERTBOT_VALIDATION have been passed in properly:
42 if [ -z "$CERTBOT_DOMAIN"]; then
43     log "CERTBOT_DOMAIN not passed in!"
44     exit 1
45 else
46     log "CERTBOT_DOMAIN = $CERTBOT_DOMAIN"
47 fi
48
49 if [ -z "$CERTBOT_VALIDATION"]; then
50     log "CERTBOT_VALIDATION not passed in!"
51     exit 1
52 else
53     log "CERTBOT_VALIDATION = $CERTBOT_VALIDATION"
54 fi
55
56 # My DNS registar is Dreamhost. These variables are specific to their DNS API.
57 # Yours will probably be different.
58 #
59 # Dreamhost key - generate at https://panel.dreamhost.com/?tree=home.api
60 key=KHY6UJQXD9MEJZHR
61
62 # URL where the REST endpoint is
63 url="https://api.dreamhost.com/?key=$key"
64
65 # Add a TXT record to domain
66 function addTXT {
67     log "Adding TXT record $CERTBOT_DOMAIN = $CERTBOT_VALIDATION"
68     cmd="$url&unique_id=$(uuidgen)&cmd=dns-add_record&record=_acme-challenge.$CERTBOT_DOMAIN&type=TXT&value=$CERTBOT_VALIDATION"
69     
70     log "cmd = $cmd"
71     
72     response=$(wget -O- -q "$cmd")
73     
74     log "Response = $response"
75 } # addTXT
76
77 # Verifies that the TXT record has propogated.
78 function verifyPropagation {
79     log "Enter verifyPropagation"
80     
81     # We will try 4 times waiting 5 minutes in between
82     max_attempts=4
83     time_between_attempts=300 # 5 minutes (we might be able to shorten this)
84     
85     # Obviously it's not propagated immediately so first wait
86     attempt=0
87     while [ $attempt -lt 4 ]; do
88         log "Waiting $time_between_attempts seconds for TXT record $CERTBOT_DOMAIN to propagate..."
89         sleep $time_between_attempts
90         
91         ((attempt++))
92         log "Attempt #$attempt: Validating of propagation of TXT record $CERTBOT_DOMAIN"
93         TXT=$(nslookup -type=TXT _acme-challenge.$CERTBOT_DOMAIN | grep -vi "can't find" | grep $CERTBOT_DOMAIN)
94         
95         if [ -n "$TXT" ]; then
96             log "TXT record _acme-challenge.$CERTBOT_DOMAIN propagated"
97             return
98         else
99             log "TXT record _acme-challenge.$CERTBOT_DOMAIN not propagated yet"
100         fi
101     done
102     
103     log "ERROR: Unable to validate propagation"
104     exit 1
105 } # verifyPropagation
106
107 addTXT
108 verifyPropagation
109
110 # If we get here then new certs are produced but need to be made available
111 # for importation to the Synology. /System/tmp is a directory that is
112 # on the Synology mounted via NFS.
113 cp /etc/letsencrypt/live/$CERTBOT_DOMAIN/privkey.pem /System/tmp && chmod 444 /System/tmp/privkey.pem
114 cp /etc/letsencrypt/live/$CERTBOT_DOMAIN/cert.pem    /System/tmp && chmod 444 /System/tmp/cert.pem
115 cp /etc/letsencrypt/live/$CERTBOT_DOMAIN/chain.pem   /System/tmp && chmod 444 /System/tmp/chain.pem
116
117 echo "Now go to DSM > Control Panel > Security > Certificate, select $CERTBOT_DOMAIN"
118 echo "then Add, Replace an existing certificate for *.$CERTBOT_DOMAIN, Import"
119 echo "Certificate and supply privkey.pem, cert.pem, and chain.pem for Private Key"
120 echo "Certificate, and Intermediate certificate."