]> git.somenet.org - root/pub/somesible.git/blob - roles/server/letsencrypt-bot/files/default/hook.sh
[roles/server/letsencrypt-bot] install dehydrated bot - TODO: move to nginx?
[root/pub/somesible.git] / roles / server / letsencrypt-bot / files / default / hook.sh
1 #!/usr/bin/env bash
2 ################################################
3 ### Managed by someone's ansible provisioner ###
4 ################################################
5 # Part of: https://git.somenet.org/root/pub/somesible.git
6 # 2017-2024 by someone <someone@somenet.org>
7 #
8
9 deploy_challenge() {
10     local DOMAIN="${1}" TOKEN_FILENAME="${2}" TOKEN_VALUE="${3}"
11
12     # This hook is called once for every domain that needs to be
13     # validated, including any alternative names you may have listed.
14     #
15     # Parameters:
16     # - DOMAIN
17     #   The domain name (CN or subject alternative name) being
18     #   validated.
19     # - TOKEN_FILENAME
20     #   The name of the file containing the token to be served for HTTP
21     #   validation. Should be served by your web server as
22     #   /.well-known/acme-challenge/${TOKEN_FILENAME}.
23     # - TOKEN_VALUE
24     #   The token value that needs to be served for validation. For DNS
25     #   validation, this is what you want to put in the _acme-challenge
26     #   TXT record. For HTTP validation it is the value that is expected
27     #   be found in the $TOKEN_FILENAME file.
28
29     # Simple example: Use nsupdate with local named
30     # printf 'server 127.0.0.1\nupdate add _acme-challenge.%s 300 IN TXT "%s"\nsend\n' "${DOMAIN}" "${TOKEN_VALUE}" | nsupdate -k /var/run/named/session.key
31 }
32
33 clean_challenge() {
34     local DOMAIN="${1}" TOKEN_FILENAME="${2}" TOKEN_VALUE="${3}"
35
36     # This hook is called after attempting to validate each domain,
37     # whether or not validation was successful. Here you can delete
38     # files or DNS records that are no longer needed.
39     #
40     # The parameters are the same as for deploy_challenge.
41
42     # Simple example: Use nsupdate with local named
43     # printf 'server 127.0.0.1\nupdate delete _acme-challenge.%s TXT "%s"\nsend\n' "${DOMAIN}" "${TOKEN_VALUE}" | nsupdate -k /var/run/named/session.key
44 }
45
46 deploy_cert() {
47     local DOMAIN="${1}" KEYFILE="${2}" CERTFILE="${3}" FULLCHAINFILE="${4}" CHAINFILE="${5}" TIMESTAMP="${6}"
48
49     # This hook is called once for each certificate that has been
50     # produced. Here you might, for instance, copy your new certificates
51     # to service-specific locations and reload the service.
52     #
53     # Parameters:
54     # - DOMAIN
55     #   The primary domain name, i.e. the certificate common
56     #   name (CN).
57     # - KEYFILE
58     #   The path of the file containing the private key.
59     # - CERTFILE
60     #   The path of the file containing the signed certificate.
61     # - FULLCHAINFILE
62     #   The path of the file containing the full certificate chain.
63     # - CHAINFILE
64     #   The path of the file containing the intermediate certificate(s).
65     # - TIMESTAMP
66     #   Timestamp when the specified certificate was created.
67
68     # Simple example: Copy file to nginx config
69     # cp "${KEYFILE}" "${FULLCHAINFILE}" /etc/nginx/ssl/; chown -R nginx: /etc/nginx/ssl
70     # systemctl reload nginx
71 }
72
73 deploy_ocsp() {
74     local DOMAIN="${1}" OCSPFILE="${2}" TIMESTAMP="${3}"
75
76     # This hook is called once for each updated ocsp stapling file that has
77     # been produced. Here you might, for instance, copy your new ocsp stapling
78     # files to service-specific locations and reload the service.
79     #
80     # Parameters:
81     # - DOMAIN
82     #   The primary domain name, i.e. the certificate common
83     #   name (CN).
84     # - OCSPFILE
85     #   The path of the ocsp stapling file
86     # - TIMESTAMP
87     #   Timestamp when the specified ocsp stapling file was created.
88
89     # Simple example: Copy file to nginx config
90     # cp "${OCSPFILE}" /etc/nginx/ssl/; chown -R nginx: /etc/nginx/ssl
91     # systemctl reload nginx
92 }
93
94
95 unchanged_cert() {
96     local DOMAIN="${1}" KEYFILE="${2}" CERTFILE="${3}" FULLCHAINFILE="${4}" CHAINFILE="${5}"
97
98     # This hook is called once for each certificate that is still
99     # valid and therefore wasn't reissued.
100     #
101     # Parameters:
102     # - DOMAIN
103     #   The primary domain name, i.e. the certificate common
104     #   name (CN).
105     # - KEYFILE
106     #   The path of the file containing the private key.
107     # - CERTFILE
108     #   The path of the file containing the signed certificate.
109     # - FULLCHAINFILE
110     #   The path of the file containing the full certificate chain.
111     # - CHAINFILE
112     #   The path of the file containing the intermediate certificate(s).
113 }
114
115 invalid_challenge() {
116     local DOMAIN="${1}" RESPONSE="${2}"
117
118     # This hook is called if the challenge response has failed, so domain
119     # owners can be aware and act accordingly.
120     #
121     # Parameters:
122     # - DOMAIN
123     #   The primary domain name, i.e. the certificate common
124     #   name (CN).
125     # - RESPONSE
126     #   The response that the verification server returned
127
128     # Simple example: Send mail to root
129     # printf "Subject: Validation of ${DOMAIN} failed!\n\nOh noez!" | sendmail root
130 }
131
132 request_failure() {
133     local STATUSCODE="${1}" REASON="${2}" REQTYPE="${3}" HEADERS="${4}"
134
135     # This hook is called when an HTTP request fails (e.g., when the ACME
136     # server is busy, returns an error, etc). It will be called upon any
137     # response code that does not start with '2'. Useful to alert admins
138     # about problems with requests.
139     #
140     # Parameters:
141     # - STATUSCODE
142     #   The HTML status code that originated the error.
143     # - REASON
144     #   The specified reason for the error.
145     # - REQTYPE
146     #   The kind of request that was made (GET, POST...)
147     # - HEADERS
148     #   HTTP headers returned by the CA
149
150     # Simple example: Send mail to root
151     # printf "Subject: HTTP request failed failed!\n\nA http request failed with status ${STATUSCODE}!" | sendmail root
152 }
153
154 generate_csr() {
155     local DOMAIN="${1}" CERTDIR="${2}" ALTNAMES="${3}"
156
157     # This hook is called before any certificate signing operation takes place.
158     # It can be used to generate or fetch a certificate signing request with external
159     # tools.
160     # The output should be just the cerificate signing request formatted as PEM.
161     #
162     # Parameters:
163     # - DOMAIN
164     #   The primary domain as specified in domains.txt. This does not need to
165     #   match with the domains in the CSR, it's basically just the directory name.
166     # - CERTDIR
167     #   Certificate output directory for this particular certificate. Can be used
168     #   for storing additional files.
169     # - ALTNAMES
170     #   All domain names for the current certificate as specified in domains.txt.
171     #   Again, this doesn't need to match with the CSR, it's just there for convenience.
172
173     # Simple example: Look for pre-generated CSRs
174     # if [ -e "${CERTDIR}/pre-generated.csr" ]; then
175     #   cat "${CERTDIR}/pre-generated.csr"
176     # fi
177 }
178
179 startup_hook() {
180   # This hook is called before the cron command to do some initial tasks
181   # (e.g. starting a webserver).
182
183   :
184 }
185
186 exit_hook() {
187   # This hook is called at the end of the cron command and can be used to
188   # do some final (cleanup or other) tasks.
189   chmod -R u=rwX,g=rX,o-rwx "$CERTDIR"
190   :
191 }
192
193 HANDLER="$1"; shift
194 if [[ "${HANDLER}" =~ ^(deploy_challenge|clean_challenge|deploy_cert|deploy_ocsp|unchanged_cert|invalid_challenge|request_failure|generate_csr|startup_hook|exit_hook)$ ]]; then
195   "$HANDLER" "$@"
196 fi