c7d723a5d97f0da4ea3cb80cf1dbc2fcfa56cf4a
[clearscm.git] / bin / certbot_cleanup.sh
1 #!/bin/bash
2 ################################################################################
3 #
4 # File:         certbot_cleanup.sh
5 # Revision:     1.0
6 # Description:  Perform cleanup after domain validation by removing the TXT
7 #               record on the domain created by certbot_authentication.sh
8 #
9 #               Domain validation is the process of validating you have control
10 #               over a domain. Services like Let's Encrypt can then issue you
11 #               domain validated TLS certificates for use to secure websites.
12 #
13 # See also:     https://help.dreamhost.com/hc/en-us/articles/217555707-DNS-API-commands
14 #
15 # Crontab:      0 0 1 * * certbot renew --manual-auth-hook /path/to/certbot_authentication.sh --manual-cleanup-hook /path/to/certbot_cleanup.sh
16 #
17 # Author:       Andrew@DeFaria.com
18 # Created:      Fri 04 Jun 2021 11:20:16 PDT
19 # Modified:
20 # Language:     Bash
21 #
22 # (c) Copyright 2021, ClearSCM, Inc., all rights reserved
23 #
24 ################################################################################
25 logfile="/tmp/$(basename $0).log"
26 rm -f $logfile
27
28 function log {
29     echo $1 >> $logfile
30 } # log
31
32 log "Starting $0"
33
34 # The following are environment variables that certbot passes to us
35 #
36 # CERTBOT_DOMAIN:     Domain being authenticated.
37 # CERTBOT_VALIDATION: Validation string for domain
38 #
39 # Check that CERTBOT_DOMAIN and CERTBOT_VALIDATION have been passed in properly
40 if [ -z "$CERTBOT_DOMAIN"]; then
41     log "CERTBOT_DOMAIN not passed in!"
42     exit 1
43 else
44     log "CERTBOT_DOMAIN = $CERTBOT_DOMAIN"
45 fi
46
47 if [ -z "$CERTBOT_VALIDATION"]; then
48     log "CERTBOT_VALIDATION not passed in!"
49     exit 1
50 else
51     log "CERTBOT_VALIDATION = $CERTBOT_VALIDATION"
52 fi
53
54 # My DNS registar is Dreamhost. These variables are specific to their DNS API.
55 # Yours will probably be different.
56 #
57 # Dreamhost key - generate at https://panel.dreamhost.com/?tree=home.api
58 key=KHY6UJQXD9MEJZHR
59
60 # URL where the REST endpoint is
61 url="https://api.dreamhost.com/?key=$key"
62
63 # Remove a TXT record. Oddly you must also specify the value.
64 function removeTXT {
65     log "Removing TXT record $CERTBOT_DOMAIN = $CERTBOT_VALIDATION"
66     cmd="$url&unique_id=$(uuidgen)&cmd=dns-remove_record&record=_acme-challenge.$CERTBOT_DOMAIN&type=TXT&value=$CERTBOT_VALIDATION"
67     log "cmd: $cmd"
68     
69     response=$(wget -O- -q "$cmd")
70     
71     log "Response = $response"
72 } # removeTXT
73
74 removeTXT
75
76 # Removal is instanteous but propagation will take some time. No need to wait
77 # around though...