b84191ad9888f69983d1b3c88e780d050d087b66
[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 20/3 * * certbot renew
18 #
19 # Note:         If you symlink /etc/letsencrypt/renewal-hooks/{pre|post|deploy}
20 #               to the proper scripts then all you need is certbox renew. Also
21 #               if certbot doesn't think it's time to renew certs you can force it
22 #               with --force-renewal
23 #
24 # Author:       Andrew@DeFaria.com
25 # Created:      Fri 04 Jun 2021 11:20:16 PDT
26 # Modified:     Mon Oct 24 11:53:38 AM PDT 2022
27 # Language:     Bash
28 #
29 # (c) Copyright 2021, ClearSCM, Inc., all rights reserved
30 #
31 ################################################################################
32 logfile="/tmp/$(basename $0).log"
33 rm -f $logfile
34
35 function log {
36     echo $1 >> $logfile
37 } # log
38
39 log "Starting $0"
40
41 # The following are environment variables that certbot passes to us
42 #
43 # CERTBOT_DOMAIN:     Domain being authenticated.
44 # CERTBOT_VALIDATION: Validation string for domain.
45 #
46 # Check that CERTBOT_DOMAIN and CERTBOT_VALIDATION have been passed in properly:
47 if [ -z "$CERTBOT_DOMAIN"]; then
48     log "CERTBOT_DOMAIN not passed in!"
49     exit 1
50 else
51     log "CERTBOT_DOMAIN = $CERTBOT_DOMAIN"
52 fi
53
54 if [ -z "$CERTBOT_VALIDATION"]; then
55     log "CERTBOT_VALIDATION not passed in!"
56     exit 1
57 else
58     log "CERTBOT_VALIDATION = $CERTBOT_VALIDATION"
59 fi
60
61 # My DNS registar is Dreamhost. These variables are specific to their DNS API.
62 # Yours will probably be different.
63 #
64 # Dreamhost key - generate at https://panel.dreamhost.com/?tree=home.api
65 key=KHY6UJQXD9MEJZHR
66
67 # URL where the REST endpoint is
68 url="https://api.dreamhost.com/?key=$key"
69
70 # Add a TXT record to domain
71 function addTXT {
72     log "Adding TXT record $CERTBOT_DOMAIN = $CERTBOT_VALIDATION"
73     cmd="$url&unique_id=$(uuidgen)&cmd=dns-add_record&record=_acme-challenge.$CERTBOT_DOMAIN&type=TXT&value=$CERTBOT_VALIDATION"
74
75     log "cmd = $cmd"
76
77     response=$(wget -O- -q "$cmd")
78
79     log "Response = $response"
80 } # addTXT
81
82 # Verifies that the TXT record has propogated.
83 function verifyPropagation {
84     log "Enter verifyPropagation"
85
86     # We will try 20 times waiting 1 minutes in between
87     max_attempts=20
88     time_between_attempts=60
89
90     # Obviously it's not propagated immediately so first wait
91     attempt=0
92     while [ $attempt -lt $max_attempts ]; do
93         log "Waiting $time_between_attempts seconds for TXT record $CERTBOT_DOMAIN to propagate..."
94         sleep $time_between_attempts
95
96         ((attempt++))
97         log "Attempt #$attempt: Validating of propagation of TXT record $CERTBOT_DOMAIN"
98         TXT=$(nslookup -type=TXT _acme-challenge.$CERTBOT_DOMAIN | grep -vi "can't find" | grep $CERTBOT_DOMAIN)
99
100         if [ -n "$TXT" ]; then
101             log "TXT record _acme-challenge.$CERTBOT_DOMAIN propagated"
102             return
103         else
104             log "TXT record _acme-challenge.$CERTBOT_DOMAIN not propagated yet"
105         fi
106     done
107
108     log "ERROR: Unable to validate propagation"
109     exit 1
110 } # verifyPropagation
111
112 addTXT
113 verifyPropagation
114
115 # If we get here then new certs are produced but need to be made available
116 # for importation to the Synology. $certdir is a directory that is on the
117 # Synology mounted via NFS.
118 certdir=/System/Data/Certificates
119
120 mkdir -p $certdir
121 cp /etc/letsencrypt/live/$CERTBOT_DOMAIN/privkey.pem     $certdir && chmod 444 $certdir/privkey.pem
122 cp /etc/letsencrypt/live/$CERTBOT_DOMAIN/cert.pem        $certdir && chmod 444 $certdir/cert.pem
123 cp /etc/letsencrypt/live/$CERTBOT_DOMAIN/chain.pem       $certdir && chmod 444 $certdir/chain.pem
124 cp /etc/letsencrypt/live/$CERTBOT_DOMAIN/fullchain.pem   $certdir && chmod 444 $certdir/fullchain.pem