]>
Commit | Line | Data |
---|---|---|
0a7c9364 | 1 | #!/usr/bin/env sh |
bfdf1f48 | 2 | |
9044adec | 3 | VER=2.8.7 |
a7b7355d | 4 | |
6cc11ffb | 5 | PROJECT_NAME="acme.sh" |
a7b7355d | 6 | |
6cc11ffb | 7 | PROJECT_ENTRY="acme.sh" |
8 | ||
09f74a9a | 9 | PROJECT="https://github.com/acmesh-official/$PROJECT_NAME" |
4c3b3608 | 10 | |
f3e4cea3 | 11 | DEFAULT_INSTALL_HOME="$HOME/.$PROJECT_NAME" |
77f96b38 | 12 | |
13 | _WINDOWS_SCHEDULER_NAME="$PROJECT_NAME.cron" | |
14 | ||
f3e4cea3 | 15 | _SCRIPT_="$0" |
16 | ||
b50e701c | 17 | _SUB_FOLDER_NOTIFY="notify" |
18 | _SUB_FOLDER_DNSAPI="dnsapi" | |
19 | _SUB_FOLDER_DEPLOY="deploy" | |
20 | ||
21 | _SUB_FOLDERS="$_SUB_FOLDER_DNSAPI $_SUB_FOLDER_DEPLOY $_SUB_FOLDER_NOTIFY" | |
f3e4cea3 | 22 | |
c1151b0d | 23 | LETSENCRYPT_CA_V1="https://acme-v01.api.letsencrypt.org/directory" |
24 | LETSENCRYPT_STAGING_CA_V1="https://acme-staging.api.letsencrypt.org/directory" | |
25 | ||
26 | LETSENCRYPT_CA_V2="https://acme-v02.api.letsencrypt.org/directory" | |
27 | LETSENCRYPT_STAGING_CA_V2="https://acme-staging-v02.api.letsencrypt.org/directory" | |
28 | ||
f2add8de | 29 | DEFAULT_CA=$LETSENCRYPT_CA_V2 |
30 | DEFAULT_STAGING_CA=$LETSENCRYPT_STAGING_CA_V2 | |
f87890cb | 31 | |
07af4247 | 32 | DEFAULT_USER_AGENT="$PROJECT_NAME/$VER ($PROJECT)" |
d0871bda | 33 | DEFAULT_ACCOUNT_EMAIL="" |
bbbdcb09 | 34 | |
57e58ce7 | 35 | DEFAULT_ACCOUNT_KEY_LENGTH=2048 |
36 | DEFAULT_DOMAIN_KEY_LENGTH=2048 | |
37 | ||
a746139c | 38 | DEFAULT_OPENSSL_BIN="openssl" |
39 | ||
c1151b0d | 40 | _OLD_CA_HOST="https://acme-v01.api.letsencrypt.org" |
4a2ac7bd | 41 | _OLD_STAGE_CA_HOST="https://acme-staging.api.letsencrypt.org" |
4c3b3608 | 42 | |
43 | VTYPE_HTTP="http-01" | |
44 | VTYPE_DNS="dns-01" | |
08681f4a | 45 | VTYPE_ALPN="tls-alpn-01" |
e22bcf7c | 46 | |
0463b5d6 | 47 | LOCAL_ANY_ADDRESS="0.0.0.0" |
48 | ||
ec67a1b2 | 49 | DEFAULT_RENEW=60 |
523c7682 | 50 | |
3f4513b3 | 51 | NO_VALUE="no" |
52 | ||
3881f221 | 53 | W_DNS="dns" |
08681f4a | 54 | W_ALPN="alpn" |
64821ad4 | 55 | DNS_ALIAS_PREFIX="=" |
4c3b3608 | 56 | |
0e44f587 | 57 | MODE_STATELESS="stateless" |
58 | ||
ec603bee | 59 | STATE_VERIFIED="verified_ok" |
60 | ||
9d725af6 | 61 | NGINX="nginx:" |
03f8d6e9 | 62 | NGINX_START="#ACME_NGINX_START" |
63 | NGINX_END="#ACME_NGINX_END" | |
9d725af6 | 64 | |
88fab7d6 | 65 | BEGIN_CSR="-----BEGIN CERTIFICATE REQUEST-----" |
66 | END_CSR="-----END CERTIFICATE REQUEST-----" | |
67 | ||
68 | BEGIN_CERT="-----BEGIN CERTIFICATE-----" | |
69 | END_CERT="-----END CERTIFICATE-----" | |
70 | ||
45e386b2 | 71 | CONTENT_TYPE_JSON="application/jose+json" |
cc179731 | 72 | RENEW_SKIP=2 |
73 | ||
7690f73e | 74 | B64CONF_START="__ACME_BASE64__START_" |
75 | B64CONF_END="__ACME_BASE64__END_" | |
76 | ||
43822d37 | 77 | ECC_SEP="_" |
78 | ECC_SUFFIX="${ECC_SEP}ecc" | |
79 | ||
a73c5b33 | 80 | LOG_LEVEL_1=1 |
81 | LOG_LEVEL_2=2 | |
82 | LOG_LEVEL_3=3 | |
83 | DEFAULT_LOG_LEVEL="$LOG_LEVEL_1" | |
84 | ||
fc6cf4d9 | 85 | DEBUG_LEVEL_1=1 |
86 | DEBUG_LEVEL_2=2 | |
87 | DEBUG_LEVEL_3=3 | |
88 | DEBUG_LEVEL_DEFAULT=$DEBUG_LEVEL_1 | |
89 | DEBUG_LEVEL_NONE=0 | |
90 | ||
10eec7d4 | 91 | DOH_CLOUDFLARE=1 |
92 | DOH_GOOGLE=2 | |
93 | ||
e6e85b0c | 94 | HIDDEN_VALUE="[hidden](please add '--output-insecure' to see this value)" |
95 | ||
e2edf208 | 96 | SYSLOG_ERROR="user.error" |
fc6cf4d9 | 97 | SYSLOG_INFO="user.info" |
e2edf208 | 98 | SYSLOG_DEBUG="user.debug" |
99 | ||
fc6cf4d9 | 100 | #error |
113089be | 101 | SYSLOG_LEVEL_ERROR=3 |
fc6cf4d9 | 102 | #info |
113089be | 103 | SYSLOG_LEVEL_INFO=6 |
fc6cf4d9 | 104 | #debug |
113089be | 105 | SYSLOG_LEVEL_DEBUG=7 |
fc6cf4d9 | 106 | #debug2 |
113089be | 107 | SYSLOG_LEVEL_DEBUG_2=8 |
fc6cf4d9 | 108 | #debug3 |
113089be | 109 | SYSLOG_LEVEL_DEBUG_3=9 |
fc6cf4d9 | 110 | |
113089be | 111 | SYSLOG_LEVEL_DEFAULT=$SYSLOG_LEVEL_ERROR |
fc6cf4d9 | 112 | #none |
113 | SYSLOG_LEVEL_NONE=0 | |
114 | ||
b50e701c | 115 | NOTIFY_LEVEL_DISABLE=0 |
116 | NOTIFY_LEVEL_ERROR=1 | |
117 | NOTIFY_LEVEL_RENEW=2 | |
118 | NOTIFY_LEVEL_SKIP=3 | |
119 | ||
120 | NOTIFY_LEVEL_DEFAULT=$NOTIFY_LEVEL_RENEW | |
121 | ||
122 | NOTIFY_MODE_BULK=0 | |
123 | NOTIFY_MODE_CERT=1 | |
124 | ||
125 | NOTIFY_MODE_DEFAULT=$NOTIFY_MODE_BULK | |
126 | ||
09f74a9a | 127 | _DEBUG_WIKI="https://github.com/acmesh-official/acme.sh/wiki/How-to-debug-acme.sh" |
4c3b3608 | 128 | |
09f74a9a | 129 | _PREPARE_LINK="https://github.com/acmesh-official/acme.sh/wiki/Install-preparations" |
562a4c05 | 130 | |
09f74a9a | 131 | _STATELESS_WIKI="https://github.com/acmesh-official/acme.sh/wiki/Stateless-Mode" |
0e44f587 | 132 | |
09f74a9a | 133 | _DNS_ALIAS_WIKI="https://github.com/acmesh-official/acme.sh/wiki/DNS-alias-mode" |
875625b1 | 134 | |
09f74a9a | 135 | _DNS_MANUAL_WIKI="https://github.com/acmesh-official/acme.sh/wiki/dns-manual-mode" |
a0923622 | 136 | |
09f74a9a | 137 | _NOTIFY_WIKI="https://github.com/acmesh-official/acme.sh/wiki/notify" |
b50e701c | 138 | |
09f74a9a | 139 | _SUDO_WIKI="https://github.com/acmesh-official/acme.sh/wiki/sudo" |
5bdfdfef | 140 | |
1041c9f9 | 141 | _REVOKE_WIKI="https://github.com/acmesh-official/acme.sh/wiki/revokecert" |
142 | ||
309bec47 | 143 | _DNS_MANUAL_ERR="The dns manual mode can not renew automatically, you must issue it again manually. You'd better use the other modes instead." |
144 | ||
145 | _DNS_MANUAL_WARN="It seems that you are using dns manual mode. please take care: $_DNS_MANUAL_ERR" | |
146 | ||
a0923622 | 147 | _DNS_MANUAL_ERROR="It seems that you are using dns manual mode. Read this link first: $_DNS_MANUAL_WIKI" |
148 | ||
08ee072f | 149 | __INTERACTIVE="" |
4c2a3841 | 150 | if [ -t 1 ]; then |
08ee072f | 151 | __INTERACTIVE="1" |
152 | fi | |
00a50605 | 153 | |
43822d37 | 154 | __green() { |
137dc1ea | 155 | if [ "${__INTERACTIVE}${ACME_NO_COLOR:-0}" = "10" -o "${ACME_FORCE_COLOR}" = "1" ]; then |
671edc33 | 156 | printf '\33[1;32m%b\33[0m' "$1" |
137dc1ea | 157 | return |
2d12b689 | 158 | fi |
3576754c | 159 | printf -- "%b" "$1" |
43822d37 | 160 | } |
161 | ||
162 | __red() { | |
137dc1ea | 163 | if [ "${__INTERACTIVE}${ACME_NO_COLOR:-0}" = "10" -o "${ACME_FORCE_COLOR}" = "1" ]; then |
671edc33 | 164 | printf '\33[1;31m%b\33[0m' "$1" |
137dc1ea | 165 | return |
2d12b689 | 166 | fi |
3576754c | 167 | printf -- "%b" "$1" |
43822d37 | 168 | } |
00a50605 | 169 | |
a73c5b33 | 170 | _printargs() { |
65a7d569 | 171 | _exitstatus="$?" |
569d6c55 | 172 | if [ -z "$NO_TIMESTAMP" ] || [ "$NO_TIMESTAMP" = "0" ]; then |
173 | printf -- "%s" "[$(date)] " | |
174 | fi | |
4c2a3841 | 175 | if [ -z "$2" ]; then |
569d6c55 | 176 | printf -- "%s" "$1" |
43822d37 | 177 | else |
569d6c55 | 178 | printf -- "%s" "$1='$2'" |
43822d37 | 179 | fi |
a73c5b33 | 180 | printf "\n" |
ac9f6e3a | 181 | # return the saved exit status |
52351d7d | 182 | return "$_exitstatus" |
43822d37 | 183 | } |
184 | ||
9d548d81 | 185 | _dlg_versions() { |
186 | echo "Diagnosis versions: " | |
851fedf7 | 187 | echo "openssl:$ACME_OPENSSL_BIN" |
d8ba26e6 | 188 | if _exists "${ACME_OPENSSL_BIN:-openssl}"; then |
189 | ${ACME_OPENSSL_BIN:-openssl} version 2>&1 | |
9d548d81 | 190 | else |
eca57bee | 191 | echo "$ACME_OPENSSL_BIN doesn't exist." |
9d548d81 | 192 | fi |
4c2a3841 | 193 | |
9d548d81 | 194 | echo "apache:" |
4c2a3841 | 195 | if [ "$_APACHECTL" ] && _exists "$_APACHECTL"; then |
e735d8d4 | 196 | $_APACHECTL -V 2>&1 |
9d548d81 | 197 | else |
eca57bee | 198 | echo "apache doesn't exist." |
9d548d81 | 199 | fi |
4c2a3841 | 200 | |
326c386b | 201 | echo "nginx:" |
202 | if _exists "nginx"; then | |
203 | nginx -V 2>&1 | |
204 | else | |
eca57bee | 205 | echo "nginx doesn't exist." |
326c386b | 206 | fi |
207 | ||
3794b5cb | 208 | echo "socat:" |
209 | if _exists "socat"; then | |
463df9e4 | 210 | socat -V 2>&1 |
9d548d81 | 211 | else |
eca57bee | 212 | _debug "socat doesn't exist." |
9d548d81 | 213 | fi |
214 | } | |
43822d37 | 215 | |
e2edf208 | 216 | #class |
217 | _syslog() { | |
65a7d569 | 218 | _exitstatus="$?" |
fc6cf4d9 | 219 | if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" = "$SYSLOG_LEVEL_NONE" ]; then |
e2edf208 | 220 | return |
221 | fi | |
222 | _logclass="$1" | |
223 | shift | |
5b3e3d9c | 224 | if [ -z "$__logger_i" ]; then |
225 | if _contains "$(logger --help 2>&1)" "-i"; then | |
226 | __logger_i="logger -i" | |
227 | else | |
228 | __logger_i="logger" | |
229 | fi | |
230 | fi | |
231 | $__logger_i -t "$PROJECT_NAME" -p "$_logclass" "$(_printargs "$@")" >/dev/null 2>&1 | |
65a7d569 | 232 | return "$_exitstatus" |
e2edf208 | 233 | } |
234 | ||
a73c5b33 | 235 | _log() { |
236 | [ -z "$LOG_FILE" ] && return | |
95e06de5 | 237 | _printargs "$@" >>"$LOG_FILE" |
a73c5b33 | 238 | } |
239 | ||
240 | _info() { | |
fc6cf4d9 | 241 | _log "$@" |
113089be | 242 | if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_INFO" ]; then |
fc6cf4d9 | 243 | _syslog "$SYSLOG_INFO" "$@" |
244 | fi | |
a73c5b33 | 245 | _printargs "$@" |
4c3b3608 | 246 | } |
247 | ||
248 | _err() { | |
fc6cf4d9 | 249 | _syslog "$SYSLOG_ERROR" "$@" |
250 | _log "$@" | |
569d6c55 | 251 | if [ -z "$NO_TIMESTAMP" ] || [ "$NO_TIMESTAMP" = "0" ]; then |
252 | printf -- "%s" "[$(date)] " >&2 | |
253 | fi | |
4c2a3841 | 254 | if [ -z "$2" ]; then |
65de3110 | 255 | __red "$1" >&2 |
256 | else | |
257 | __red "$1='$2'" >&2 | |
258 | fi | |
b19ba13a | 259 | printf "\n" >&2 |
4c3b3608 | 260 | return 1 |
261 | } | |
262 | ||
43822d37 | 263 | _usage() { |
4c2a3841 | 264 | __red "$@" >&2 |
65de3110 | 265 | printf "\n" >&2 |
43822d37 | 266 | } |
267 | ||
bba5376a JV |
268 | __debug_bash_helper() { |
269 | # At this point only do for --debug 3 | |
270 | if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -lt "$DEBUG_LEVEL_3" ]; then | |
bba5376a JV |
271 | return |
272 | fi | |
273 | # Return extra debug info when running with bash, otherwise return empty | |
274 | # string. | |
275 | if [ -z "${BASH_VERSION}" ]; then | |
bba5376a JV |
276 | return |
277 | fi | |
278 | # We are a bash shell at this point, return the filename, function name, and | |
279 | # line number as a string | |
280 | _dbh_saveIFS=$IFS | |
281 | IFS=" " | |
adce8f52 JV |
282 | # Must use eval or syntax error happens under dash. The eval should use |
283 | # single quotes as older versions of busybox had a bug with double quotes and | |
284 | # eval. | |
bba5376a JV |
285 | # Use 'caller 1' as we want one level up the stack as we should be called |
286 | # by one of the _debug* functions | |
adce8f52 | 287 | eval '_dbh_called=($(caller 1))' |
bba5376a | 288 | IFS=$_dbh_saveIFS |
adce8f52 | 289 | eval '_dbh_file=${_dbh_called[2]}' |
bba5376a JV |
290 | if [ -n "${_script_home}" ]; then |
291 | # Trim off the _script_home directory name | |
adce8f52 | 292 | eval '_dbh_file=${_dbh_file#$_script_home/}' |
bba5376a | 293 | fi |
adce8f52 JV |
294 | eval '_dbh_function=${_dbh_called[1]}' |
295 | eval '_dbh_lineno=${_dbh_called[0]}' | |
bba5376a JV |
296 | printf "%-40s " "$_dbh_file:${_dbh_function}:${_dbh_lineno}" |
297 | } | |
298 | ||
c60883ef | 299 | _debug() { |
fc6cf4d9 | 300 | if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_1" ]; then |
301 | _log "$@" | |
a73c5b33 | 302 | fi |
113089be | 303 | if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG" ]; then |
fc6cf4d9 | 304 | _syslog "$SYSLOG_DEBUG" "$@" |
305 | fi | |
306 | if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_1" ]; then | |
bba5376a JV |
307 | _bash_debug=$(__debug_bash_helper) |
308 | _printargs "${_bash_debug}$@" >&2 | |
c60883ef | 309 | fi |
c60883ef | 310 | } |
311 | ||
e6e85b0c | 312 | #output the sensitive messages |
313 | _secure_debug() { | |
314 | if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_1" ]; then | |
315 | if [ "$OUTPUT_INSECURE" = "1" ]; then | |
316 | _log "$@" | |
317 | else | |
318 | _log "$1" "$HIDDEN_VALUE" | |
319 | fi | |
320 | fi | |
321 | if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG" ]; then | |
322 | _syslog "$SYSLOG_DEBUG" "$1" "$HIDDEN_VALUE" | |
323 | fi | |
324 | if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_1" ]; then | |
325 | if [ "$OUTPUT_INSECURE" = "1" ]; then | |
326 | _printargs "$@" >&2 | |
327 | else | |
328 | _printargs "$1" "$HIDDEN_VALUE" >&2 | |
329 | fi | |
330 | fi | |
331 | } | |
332 | ||
a63b05a9 | 333 | _debug2() { |
fc6cf4d9 | 334 | if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_2" ]; then |
335 | _log "$@" | |
a73c5b33 | 336 | fi |
113089be | 337 | if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG_2" ]; then |
fc6cf4d9 | 338 | _syslog "$SYSLOG_DEBUG" "$@" |
339 | fi | |
340 | if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_2" ]; then | |
bba5376a JV |
341 | _bash_debug=$(__debug_bash_helper) |
342 | _printargs "${_bash_debug}$@" >&2 | |
a63b05a9 | 343 | fi |
a63b05a9 | 344 | } |
345 | ||
e6e85b0c | 346 | _secure_debug2() { |
347 | if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_2" ]; then | |
348 | if [ "$OUTPUT_INSECURE" = "1" ]; then | |
349 | _log "$@" | |
350 | else | |
351 | _log "$1" "$HIDDEN_VALUE" | |
352 | fi | |
353 | fi | |
354 | if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG_2" ]; then | |
355 | _syslog "$SYSLOG_DEBUG" "$1" "$HIDDEN_VALUE" | |
356 | fi | |
357 | if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_2" ]; then | |
358 | if [ "$OUTPUT_INSECURE" = "1" ]; then | |
359 | _printargs "$@" >&2 | |
360 | else | |
361 | _printargs "$1" "$HIDDEN_VALUE" >&2 | |
362 | fi | |
363 | fi | |
364 | } | |
365 | ||
22ea4004 | 366 | _debug3() { |
fc6cf4d9 | 367 | if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_3" ]; then |
368 | _log "$@" | |
369 | fi | |
113089be | 370 | if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG_3" ]; then |
fc6cf4d9 | 371 | _syslog "$SYSLOG_DEBUG" "$@" |
a73c5b33 | 372 | fi |
fc6cf4d9 | 373 | if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_3" ]; then |
bba5376a JV |
374 | _bash_debug=$(__debug_bash_helper) |
375 | _printargs "${_bash_debug}$@" >&2 | |
22ea4004 | 376 | fi |
22ea4004 | 377 | } |
378 | ||
e6e85b0c | 379 | _secure_debug3() { |
380 | if [ "${LOG_LEVEL:-$DEFAULT_LOG_LEVEL}" -ge "$LOG_LEVEL_3" ]; then | |
381 | if [ "$OUTPUT_INSECURE" = "1" ]; then | |
382 | _log "$@" | |
383 | else | |
384 | _log "$1" "$HIDDEN_VALUE" | |
385 | fi | |
386 | fi | |
387 | if [ "${SYS_LOG:-$SYSLOG_LEVEL_NONE}" -ge "$SYSLOG_LEVEL_DEBUG_3" ]; then | |
388 | _syslog "$SYSLOG_DEBUG" "$1" "$HIDDEN_VALUE" | |
389 | fi | |
390 | if [ "${DEBUG:-$DEBUG_LEVEL_NONE}" -ge "$DEBUG_LEVEL_3" ]; then | |
391 | if [ "$OUTPUT_INSECURE" = "1" ]; then | |
392 | _printargs "$@" >&2 | |
393 | else | |
394 | _printargs "$1" "$HIDDEN_VALUE" >&2 | |
395 | fi | |
396 | fi | |
397 | } | |
398 | ||
c4bf5eef | 399 | _upper_case() { |
400 | # shellcheck disable=SC2018,SC2019 | |
401 | tr 'a-z' 'A-Z' | |
402 | } | |
403 | ||
404 | _lower_case() { | |
405 | # shellcheck disable=SC2018,SC2019 | |
406 | tr 'A-Z' 'a-z' | |
407 | } | |
408 | ||
4c2a3841 | 409 | _startswith() { |
dceb3aca | 410 | _str="$1" |
411 | _sub="$2" | |
19539575 | 412 | echo "$_str" | grep "^$_sub" >/dev/null 2>&1 |
dceb3aca | 413 | } |
414 | ||
4c2a3841 | 415 | _endswith() { |
43822d37 | 416 | _str="$1" |
417 | _sub="$2" | |
418 | echo "$_str" | grep -- "$_sub\$" >/dev/null 2>&1 | |
419 | } | |
420 | ||
4c2a3841 | 421 | _contains() { |
dceb3aca | 422 | _str="$1" |
423 | _sub="$2" | |
43822d37 | 424 | echo "$_str" | grep -- "$_sub" >/dev/null 2>&1 |
dceb3aca | 425 | } |
426 | ||
c53da1ef | 427 | _hasfield() { |
428 | _str="$1" | |
429 | _field="$2" | |
430 | _sep="$3" | |
4c2a3841 | 431 | if [ -z "$_field" ]; then |
43822d37 | 432 | _usage "Usage: str field [sep]" |
c53da1ef | 433 | return 1 |
434 | fi | |
4c2a3841 | 435 | |
436 | if [ -z "$_sep" ]; then | |
c53da1ef | 437 | _sep="," |
438 | fi | |
4c2a3841 | 439 | |
6cf7be4b | 440 | for f in $(echo "$_str" | tr "$_sep" ' '); do |
4c2a3841 | 441 | if [ "$f" = "$_field" ]; then |
0c9546cc | 442 | _debug2 "'$_str' contains '$_field'" |
c53da1ef | 443 | return 0 #contains ok |
444 | fi | |
445 | done | |
0c9546cc | 446 | _debug2 "'$_str' does not contain '$_field'" |
3c07f57a | 447 | return 1 #not contains |
c53da1ef | 448 | } |
449 | ||
422dd1fa | 450 | # str index [sep] |
4c2a3841 | 451 | _getfield() { |
0463b5d6 | 452 | _str="$1" |
453 | _findex="$2" | |
454 | _sep="$3" | |
4c2a3841 | 455 | |
456 | if [ -z "$_findex" ]; then | |
0463b5d6 | 457 | _usage "Usage: str field [sep]" |
458 | return 1 | |
459 | fi | |
4c2a3841 | 460 | |
461 | if [ -z "$_sep" ]; then | |
0463b5d6 | 462 | _sep="," |
463 | fi | |
464 | ||
201aa244 | 465 | _ffi="$_findex" |
4c2a3841 | 466 | while [ "$_ffi" -gt "0" ]; do |
201aa244 | 467 | _fv="$(echo "$_str" | cut -d "$_sep" -f "$_ffi")" |
4c2a3841 | 468 | if [ "$_fv" ]; then |
0463b5d6 | 469 | printf -- "%s" "$_fv" |
470 | return 0 | |
471 | fi | |
95e06de5 | 472 | _ffi="$(_math "$_ffi" - 1)" |
0463b5d6 | 473 | done |
4c2a3841 | 474 | |
0463b5d6 | 475 | printf -- "%s" "$_str" |
476 | ||
477 | } | |
478 | ||
4c2a3841 | 479 | _exists() { |
c60883ef | 480 | cmd="$1" |
4c2a3841 | 481 | if [ -z "$cmd" ]; then |
43822d37 | 482 | _usage "Usage: _exists cmd" |
c60883ef | 483 | return 1 |
484 | fi | |
82dc2244 | 485 | |
486 | if eval type type >/dev/null 2>&1; then | |
487 | eval type "$cmd" >/dev/null 2>&1 | |
488 | elif command >/dev/null 2>&1; then | |
19539575 | 489 | command -v "$cmd" >/dev/null 2>&1 |
ce4be4e9 | 490 | else |
e591d5cf | 491 | which "$cmd" >/dev/null 2>&1 |
eac18b1c | 492 | fi |
c60883ef | 493 | ret="$?" |
690a5e20 | 494 | _debug3 "$cmd exists=$ret" |
c60883ef | 495 | return $ret |
496 | } | |
497 | ||
00a50605 | 498 | #a + b |
4c2a3841 | 499 | _math() { |
be68fbd4 | 500 | _m_opts="$@" |
501 | printf "%s" "$(($_m_opts))" | |
00a50605 | 502 | } |
503 | ||
504 | _h_char_2_dec() { | |
505 | _ch=$1 | |
506 | case "${_ch}" in | |
4c2a3841 | 507 | a | A) |
19539575 | 508 | printf "10" |
4c2a3841 | 509 | ;; |
510 | b | B) | |
19539575 | 511 | printf "11" |
4c2a3841 | 512 | ;; |
513 | c | C) | |
19539575 | 514 | printf "12" |
4c2a3841 | 515 | ;; |
516 | d | D) | |
19539575 | 517 | printf "13" |
4c2a3841 | 518 | ;; |
519 | e | E) | |
19539575 | 520 | printf "14" |
4c2a3841 | 521 | ;; |
522 | f | F) | |
19539575 | 523 | printf "15" |
4c2a3841 | 524 | ;; |
00a50605 | 525 | *) |
19539575 | 526 | printf "%s" "$_ch" |
4c2a3841 | 527 | ;; |
19539575 | 528 | esac |
00a50605 | 529 | |
530 | } | |
531 | ||
fac1e367 | 532 | _URGLY_PRINTF="" |
4c2a3841 | 533 | if [ "$(printf '\x41')" != 'A' ]; then |
fac1e367 | 534 | _URGLY_PRINTF=1 |
535 | fi | |
536 | ||
f8bcfeb2 | 537 | _ESCAPE_XARGS="" |
841b7627 | 538 | if _exists xargs && [ "$(printf %s '\\x41' | xargs printf)" = 'A' ]; then |
f8bcfeb2 | 539 | _ESCAPE_XARGS=1 |
540 | fi | |
541 | ||
4c3b3608 | 542 | _h2b() { |
b615cce9 | 543 | if _exists xxd && xxd -r -p 2>/dev/null; then |
b420ec6c | 544 | return |
545 | fi | |
546 | ||
4c3b3608 | 547 | hex=$(cat) |
fa93d68b | 548 | ic="" |
549 | jc="" | |
b420ec6c | 550 | _debug2 _URGLY_PRINTF "$_URGLY_PRINTF" |
551 | if [ -z "$_URGLY_PRINTF" ]; then | |
f8bcfeb2 | 552 | if [ "$_ESCAPE_XARGS" ] && _exists xargs; then |
fa93d68b | 553 | _debug2 "xargs" |
ded4469e | 554 | echo "$hex" | _upper_case | sed 's/\([0-9A-F]\{2\}\)/\\\\\\x\1/g' | xargs printf |
fa93d68b | 555 | else |
ded4469e | 556 | for h in $(echo "$hex" | _upper_case | sed 's/\([0-9A-F]\{2\}\)/ \1/g'); do |
fa93d68b | 557 | if [ -z "$h" ]; then |
558 | break | |
559 | fi | |
560 | printf "\x$h%s" | |
561 | done | |
562 | fi | |
b420ec6c | 563 | else |
ded4469e | 564 | for c in $(echo "$hex" | _upper_case | sed 's/\([0-9A-F]\)/ \1/g'); do |
fa93d68b | 565 | if [ -z "$ic" ]; then |
566 | ic=$c | |
567 | continue | |
00a50605 | 568 | fi |
fa93d68b | 569 | jc=$c |
19539575 | 570 | ic="$(_h_char_2_dec "$ic")" |
571 | jc="$(_h_char_2_dec "$jc")" | |
e51bef6d | 572 | printf '\'"$(printf "%o" "$(_math "$ic" \* 16 + $jc)")""%s" |
fa93d68b | 573 | ic="" |
574 | jc="" | |
b420ec6c | 575 | done |
576 | fi | |
e591d5cf | 577 | |
4c3b3608 | 578 | } |
579 | ||
542d7977 | 580 | _is_solaris() { |
581 | _contains "${__OS__:=$(uname -a)}" "solaris" || _contains "${__OS__:=$(uname -a)}" "SunOS" | |
582 | } | |
583 | ||
9bdb799b | 584 | #_ascii_hex str |
585 | #this can only process ascii chars, should only be used when od command is missing as a backup way. | |
586 | _ascii_hex() { | |
587 | _debug2 "Using _ascii_hex" | |
588 | _str="$1" | |
589 | _str_len=${#_str} | |
590 | _h_i=1 | |
591 | while [ "$_h_i" -le "$_str_len" ]; do | |
592 | _str_c="$(printf "%s" "$_str" | cut -c "$_h_i")" | |
593 | printf " %02x" "'$_str_c" | |
594 | _h_i="$(_math "$_h_i" + 1)" | |
595 | done | |
596 | } | |
597 | ||
542d7977 | 598 | #stdin output hexstr splited by one space |
599 | #input:"abc" | |
600 | #output: " 61 62 63" | |
601 | _hex_dump() { | |
4e4a6d83 | 602 | if _exists od; then |
603 | od -A n -v -t x1 | tr -s " " | sed 's/ $//' | tr -d "\r\t\n" | |
604 | elif _exists hexdump; then | |
605 | _debug3 "using hexdump" | |
606 | hexdump -v -e '/1 ""' -e '/1 " %02x" ""' | |
607 | elif _exists xxd; then | |
608 | _debug3 "using xxd" | |
609 | xxd -ps -c 20 -i | sed "s/ 0x/ /g" | tr -d ",\n" | tr -s " " | |
610 | else | |
611 | _debug3 "using _ascii_hex" | |
9bdb799b | 612 | str=$(cat) |
613 | _ascii_hex "$str" | |
614 | fi | |
542d7977 | 615 | } |
616 | ||
617 | #url encode, no-preserved chars | |
618 | #A B C D E F G H I J K L M N O P Q R S T U V W X Y Z | |
619 | #41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 57 58 59 5a | |
620 | ||
621 | #a b c d e f g h i j k l m n o p q r s t u v w x y z | |
622 | #61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77 78 79 7a | |
623 | ||
624 | #0 1 2 3 4 5 6 7 8 9 - _ . ~ | |
625 | #30 31 32 33 34 35 36 37 38 39 2d 5f 2e 7e | |
626 | ||
627 | #stdin stdout | |
628 | _url_encode() { | |
629 | _hex_str=$(_hex_dump) | |
630 | _debug3 "_url_encode" | |
631 | _debug3 "_hex_str" "$_hex_str" | |
632 | for _hex_code in $_hex_str; do | |
633 | #upper case | |
634 | case "${_hex_code}" in | |
c3b1eb08 | 635 | "41") |
636 | printf "%s" "A" | |
637 | ;; | |
638 | "42") | |
639 | printf "%s" "B" | |
640 | ;; | |
641 | "43") | |
642 | printf "%s" "C" | |
643 | ;; | |
644 | "44") | |
645 | printf "%s" "D" | |
646 | ;; | |
647 | "45") | |
648 | printf "%s" "E" | |
649 | ;; | |
650 | "46") | |
651 | printf "%s" "F" | |
652 | ;; | |
653 | "47") | |
654 | printf "%s" "G" | |
655 | ;; | |
656 | "48") | |
657 | printf "%s" "H" | |
658 | ;; | |
659 | "49") | |
660 | printf "%s" "I" | |
661 | ;; | |
662 | "4a") | |
663 | printf "%s" "J" | |
664 | ;; | |
665 | "4b") | |
666 | printf "%s" "K" | |
667 | ;; | |
668 | "4c") | |
669 | printf "%s" "L" | |
670 | ;; | |
671 | "4d") | |
672 | printf "%s" "M" | |
673 | ;; | |
674 | "4e") | |
675 | printf "%s" "N" | |
676 | ;; | |
677 | "4f") | |
678 | printf "%s" "O" | |
679 | ;; | |
680 | "50") | |
681 | printf "%s" "P" | |
682 | ;; | |
683 | "51") | |
684 | printf "%s" "Q" | |
685 | ;; | |
686 | "52") | |
687 | printf "%s" "R" | |
688 | ;; | |
689 | "53") | |
690 | printf "%s" "S" | |
691 | ;; | |
692 | "54") | |
693 | printf "%s" "T" | |
694 | ;; | |
695 | "55") | |
696 | printf "%s" "U" | |
697 | ;; | |
698 | "56") | |
699 | printf "%s" "V" | |
700 | ;; | |
701 | "57") | |
702 | printf "%s" "W" | |
703 | ;; | |
704 | "58") | |
705 | printf "%s" "X" | |
706 | ;; | |
707 | "59") | |
708 | printf "%s" "Y" | |
709 | ;; | |
710 | "5a") | |
711 | printf "%s" "Z" | |
712 | ;; | |
713 | ||
714 | #lower case | |
715 | "61") | |
716 | printf "%s" "a" | |
717 | ;; | |
718 | "62") | |
719 | printf "%s" "b" | |
720 | ;; | |
721 | "63") | |
722 | printf "%s" "c" | |
723 | ;; | |
724 | "64") | |
725 | printf "%s" "d" | |
726 | ;; | |
727 | "65") | |
728 | printf "%s" "e" | |
729 | ;; | |
730 | "66") | |
731 | printf "%s" "f" | |
732 | ;; | |
733 | "67") | |
734 | printf "%s" "g" | |
735 | ;; | |
736 | "68") | |
737 | printf "%s" "h" | |
738 | ;; | |
739 | "69") | |
740 | printf "%s" "i" | |
741 | ;; | |
742 | "6a") | |
743 | printf "%s" "j" | |
744 | ;; | |
745 | "6b") | |
746 | printf "%s" "k" | |
747 | ;; | |
748 | "6c") | |
749 | printf "%s" "l" | |
750 | ;; | |
751 | "6d") | |
752 | printf "%s" "m" | |
753 | ;; | |
754 | "6e") | |
755 | printf "%s" "n" | |
756 | ;; | |
757 | "6f") | |
758 | printf "%s" "o" | |
759 | ;; | |
760 | "70") | |
761 | printf "%s" "p" | |
762 | ;; | |
763 | "71") | |
764 | printf "%s" "q" | |
765 | ;; | |
766 | "72") | |
767 | printf "%s" "r" | |
768 | ;; | |
769 | "73") | |
770 | printf "%s" "s" | |
771 | ;; | |
772 | "74") | |
773 | printf "%s" "t" | |
774 | ;; | |
775 | "75") | |
776 | printf "%s" "u" | |
777 | ;; | |
778 | "76") | |
779 | printf "%s" "v" | |
780 | ;; | |
781 | "77") | |
782 | printf "%s" "w" | |
783 | ;; | |
784 | "78") | |
785 | printf "%s" "x" | |
786 | ;; | |
787 | "79") | |
788 | printf "%s" "y" | |
789 | ;; | |
790 | "7a") | |
791 | printf "%s" "z" | |
792 | ;; | |
793 | #numbers | |
794 | "30") | |
795 | printf "%s" "0" | |
796 | ;; | |
797 | "31") | |
798 | printf "%s" "1" | |
799 | ;; | |
800 | "32") | |
801 | printf "%s" "2" | |
802 | ;; | |
803 | "33") | |
804 | printf "%s" "3" | |
805 | ;; | |
806 | "34") | |
807 | printf "%s" "4" | |
808 | ;; | |
809 | "35") | |
810 | printf "%s" "5" | |
811 | ;; | |
812 | "36") | |
813 | printf "%s" "6" | |
814 | ;; | |
815 | "37") | |
816 | printf "%s" "7" | |
817 | ;; | |
818 | "38") | |
819 | printf "%s" "8" | |
820 | ;; | |
821 | "39") | |
822 | printf "%s" "9" | |
823 | ;; | |
824 | "2d") | |
825 | printf "%s" "-" | |
826 | ;; | |
827 | "5f") | |
828 | printf "%s" "_" | |
829 | ;; | |
830 | "2e") | |
831 | printf "%s" "." | |
832 | ;; | |
833 | "7e") | |
834 | printf "%s" "~" | |
835 | ;; | |
3c07f57a | 836 | #other hex |
542d7977 | 837 | *) |
c3b1eb08 | 838 | printf '%%%s' "$_hex_code" |
839 | ;; | |
542d7977 | 840 | esac |
e009ec8b | 841 | done |
842 | } | |
843 | ||
b50e701c | 844 | _json_encode() { |
845 | _j_str="$(sed 's/"/\\"/g' | sed "s/\r/\\r/g")" | |
846 | _debug3 "_json_encode" | |
847 | _debug3 "_j_str" "$_j_str" | |
848 | echo "$_j_str" | _hex_dump | _lower_case | sed 's/0a/5c 6e/g' | tr -d ' ' | _h2b | tr -d "\r\n" | |
849 | } | |
850 | ||
93de1e49 | 851 | #from: http:\/\/ to http:// |
852 | _json_decode() { | |
853 | _j_str="$(sed 's#\\/#/#g')" | |
854 | _debug3 "_json_decode" | |
855 | _debug3 "_j_str" "$_j_str" | |
856 | echo "$_j_str" | |
857 | } | |
858 | ||
c60883ef | 859 | #options file |
860 | _sed_i() { | |
861 | options="$1" | |
862 | filename="$2" | |
4c2a3841 | 863 | if [ -z "$filename" ]; then |
43822d37 | 864 | _usage "Usage:_sed_i options filename" |
c60883ef | 865 | return 1 |
866 | fi | |
14f3dbb7 | 867 | _debug2 options "$options" |
868 | if sed -h 2>&1 | grep "\-i\[SUFFIX]" >/dev/null 2>&1; then | |
c60883ef | 869 | _debug "Using sed -i" |
14f3dbb7 | 870 | sed -i "$options" "$filename" |
c60883ef | 871 | else |
872 | _debug "No -i support in sed" | |
19539575 | 873 | text="$(cat "$filename")" |
4c2a3841 | 874 | echo "$text" | sed "$options" >"$filename" |
c60883ef | 875 | fi |
876 | } | |
877 | ||
22ea4004 | 878 | _egrep_o() { |
a3c0c754 | 879 | if ! egrep -o "$1" 2>/dev/null; then |
22ea4004 | 880 | sed -n 's/.*\('"$1"'\).*/\1/p' |
22ea4004 | 881 | fi |
882 | } | |
883 | ||
88fab7d6 | 884 | #Usage: file startline endline |
885 | _getfile() { | |
886 | filename="$1" | |
887 | startline="$2" | |
888 | endline="$3" | |
4c2a3841 | 889 | if [ -z "$endline" ]; then |
43822d37 | 890 | _usage "Usage: file startline endline" |
88fab7d6 | 891 | return 1 |
892 | fi | |
4c2a3841 | 893 | |
894 | i="$(grep -n -- "$startline" "$filename" | cut -d : -f 1)" | |
895 | if [ -z "$i" ]; then | |
88fab7d6 | 896 | _err "Can not find start line: $startline" |
897 | return 1 | |
898 | fi | |
19539575 | 899 | i="$(_math "$i" + 1)" |
900 | _debug i "$i" | |
4c2a3841 | 901 | |
902 | j="$(grep -n -- "$endline" "$filename" | cut -d : -f 1)" | |
903 | if [ -z "$j" ]; then | |
88fab7d6 | 904 | _err "Can not find end line: $endline" |
905 | return 1 | |
906 | fi | |
19539575 | 907 | j="$(_math "$j" - 1)" |
908 | _debug j "$j" | |
4c2a3841 | 909 | |
910 | sed -n "$i,${j}p" "$filename" | |
88fab7d6 | 911 | |
912 | } | |
913 | ||
914 | #Usage: multiline | |
4c3b3608 | 915 | _base64() { |
ec9975c3 | 916 | [ "" ] #urgly |
4c2a3841 | 917 | if [ "$1" ]; then |
24d2a8b9 | 918 | _debug3 "base64 multiline:'$1'" |
d8ba26e6 | 919 | ${ACME_OPENSSL_BIN:-openssl} base64 -e |
88fab7d6 | 920 | else |
4d8b99a3 | 921 | _debug3 "base64 single line." |
d8ba26e6 | 922 | ${ACME_OPENSSL_BIN:-openssl} base64 -e | tr -d '\r\n' |
88fab7d6 | 923 | fi |
924 | } | |
925 | ||
926 | #Usage: multiline | |
927 | _dbase64() { | |
4c2a3841 | 928 | if [ "$1" ]; then |
d8ba26e6 | 929 | ${ACME_OPENSSL_BIN:-openssl} base64 -d -A |
88fab7d6 | 930 | else |
d8ba26e6 | 931 | ${ACME_OPENSSL_BIN:-openssl} base64 -d |
88fab7d6 | 932 | fi |
933 | } | |
934 | ||
183063a2 | 935 | #file |
936 | _checkcert() { | |
937 | _cf="$1" | |
938 | if [ "$DEBUG" ]; then | |
7e381f8e | 939 | openssl x509 -noout -text -in "$_cf" |
183063a2 | 940 | else |
7e381f8e | 941 | openssl x509 -noout -text -in "$_cf" >/dev/null 2>&1 |
183063a2 | 942 | fi |
943 | } | |
944 | ||
e22bcf7c | 945 | #Usage: hashalg [outputhex] |
88fab7d6 | 946 | #Output Base64-encoded digest |
947 | _digest() { | |
948 | alg="$1" | |
4c2a3841 | 949 | if [ -z "$alg" ]; then |
43822d37 | 950 | _usage "Usage: _digest hashalg" |
88fab7d6 | 951 | return 1 |
952 | fi | |
4c2a3841 | 953 | |
e22bcf7c | 954 | outputhex="$2" |
4c2a3841 | 955 | |
c7b16249 | 956 | if [ "$alg" = "sha256" ] || [ "$alg" = "sha1" ] || [ "$alg" = "md5" ]; then |
4c2a3841 | 957 | if [ "$outputhex" ]; then |
d8ba26e6 | 958 | ${ACME_OPENSSL_BIN:-openssl} dgst -"$alg" -hex | cut -d = -f 2 | tr -d ' ' |
e22bcf7c | 959 | else |
d8ba26e6 | 960 | ${ACME_OPENSSL_BIN:-openssl} dgst -"$alg" -binary | _base64 |
b001840d | 961 | fi |
962 | else | |
963 | _err "$alg is not supported yet" | |
964 | return 1 | |
965 | fi | |
966 | ||
967 | } | |
968 | ||
e009ec8b | 969 | #Usage: hashalg secret_hex [outputhex] |
970 | #Output binary hmac | |
b001840d | 971 | _hmac() { |
972 | alg="$1" | |
e009ec8b | 973 | secret_hex="$2" |
b001840d | 974 | outputhex="$3" |
4c2a3841 | 975 | |
e009ec8b | 976 | if [ -z "$secret_hex" ]; then |
4c2a3841 | 977 | _usage "Usage: _hmac hashalg secret [outputhex]" |
b001840d | 978 | return 1 |
979 | fi | |
980 | ||
a6014bf0 | 981 | if [ "$alg" = "sha256" ] || [ "$alg" = "sha1" ]; then |
4c2a3841 | 982 | if [ "$outputhex" ]; then |
d8ba26e6 | 983 | (${ACME_OPENSSL_BIN:-openssl} dgst -"$alg" -mac HMAC -macopt "hexkey:$secret_hex" 2>/dev/null || ${ACME_OPENSSL_BIN:-openssl} dgst -"$alg" -hmac "$(printf "%s" "$secret_hex" | _h2b)") | cut -d = -f 2 | tr -d ' ' |
e22bcf7c | 984 | else |
d8ba26e6 | 985 | ${ACME_OPENSSL_BIN:-openssl} dgst -"$alg" -mac HMAC -macopt "hexkey:$secret_hex" -binary 2>/dev/null || ${ACME_OPENSSL_BIN:-openssl} dgst -"$alg" -hmac "$(printf "%s" "$secret_hex" | _h2b)" -binary |
e22bcf7c | 986 | fi |
88fab7d6 | 987 | else |
988 | _err "$alg is not supported yet" | |
989 | return 1 | |
990 | fi | |
991 | ||
992 | } | |
993 | ||
994 | #Usage: keyfile hashalg | |
995 | #Output: Base64-encoded signature value | |
996 | _sign() { | |
997 | keyfile="$1" | |
998 | alg="$2" | |
4c2a3841 | 999 | if [ -z "$alg" ]; then |
43822d37 | 1000 | _usage "Usage: _sign keyfile hashalg" |
88fab7d6 | 1001 | return 1 |
1002 | fi | |
4c2a3841 | 1003 | |
d8ba26e6 | 1004 | _sign_openssl="${ACME_OPENSSL_BIN:-openssl} dgst -sign $keyfile " |
4c2a3841 | 1005 | |
63031fb2 | 1006 | if grep "BEGIN RSA PRIVATE KEY" "$keyfile" >/dev/null 2>&1 || grep "BEGIN PRIVATE KEY" "$keyfile" >/dev/null 2>&1; then |
f87890cb | 1007 | $_sign_openssl -$alg | _base64 |
4c2a3841 | 1008 | elif grep "BEGIN EC PRIVATE KEY" "$keyfile" >/dev/null 2>&1; then |
f87890cb | 1009 | if ! _signedECText="$($_sign_openssl -sha$__ECC_KEY_LEN | ${ACME_OPENSSL_BIN:-openssl} asn1parse -inform DER)"; then |
67184d7b | 1010 | _err "Sign failed: $_sign_openssl" |
1011 | _err "Key file: $keyfile" | |
357b514b | 1012 | _err "Key content:$(wc -l <"$keyfile") lines" |
67184d7b | 1013 | return 1 |
1014 | fi | |
998783eb | 1015 | _debug3 "_signedECText" "$_signedECText" |
1016 | _ec_r="$(echo "$_signedECText" | _head_n 2 | _tail_n 1 | cut -d : -f 4 | tr -d "\r\n")" | |
998783eb | 1017 | _ec_s="$(echo "$_signedECText" | _head_n 3 | _tail_n 1 | cut -d : -f 4 | tr -d "\r\n")" |
a3295476 | 1018 | if [ "$__ECC_KEY_LEN" -eq "256" ]; then |
1019 | while [ "${#_ec_r}" -lt "64" ]; do | |
1020 | _ec_r="0${_ec_r}" | |
1021 | done | |
1022 | while [ "${#_ec_s}" -lt "64" ]; do | |
1023 | _ec_s="0${_ec_s}" | |
1024 | done | |
1025 | fi | |
1026 | if [ "$__ECC_KEY_LEN" -eq "384" ]; then | |
1027 | while [ "${#_ec_r}" -lt "96" ]; do | |
1028 | _ec_r="0${_ec_r}" | |
1029 | done | |
1030 | while [ "${#_ec_s}" -lt "96" ]; do | |
1031 | _ec_s="0${_ec_s}" | |
1032 | done | |
1033 | fi | |
1034 | if [ "$__ECC_KEY_LEN" -eq "512" ]; then | |
1035 | while [ "${#_ec_r}" -lt "132" ]; do | |
1036 | _ec_r="0${_ec_r}" | |
1037 | done | |
1038 | while [ "${#_ec_s}" -lt "132" ]; do | |
1039 | _ec_s="0${_ec_s}" | |
1040 | done | |
1041 | fi | |
1042 | _debug3 "_ec_r" "$_ec_r" | |
998783eb | 1043 | _debug3 "_ec_s" "$_ec_s" |
1044 | printf "%s" "$_ec_r$_ec_s" | _h2b | _base64 | |
1045 | else | |
1046 | _err "Unknown key file format." | |
1047 | return 1 | |
1048 | fi | |
4c2a3841 | 1049 | |
4c3b3608 | 1050 | } |
1051 | ||
3281043e | 1052 | #keylength or isEcc flag (empty str => not ecc) |
43822d37 | 1053 | _isEccKey() { |
1054 | _length="$1" | |
1055 | ||
4c2a3841 | 1056 | if [ -z "$_length" ]; then |
43822d37 | 1057 | return 1 |
1058 | fi | |
1059 | ||
1060 | [ "$_length" != "1024" ] \ | |
4c2a3841 | 1061 | && [ "$_length" != "2048" ] \ |
1062 | && [ "$_length" != "3072" ] \ | |
1063 | && [ "$_length" != "4096" ] \ | |
1064 | && [ "$_length" != "8192" ] | |
43822d37 | 1065 | } |
1066 | ||
e22bcf7c | 1067 | # _createkey 2048|ec-256 file |
1068 | _createkey() { | |
1069 | length="$1" | |
1070 | f="$2" | |
c4236e58 | 1071 | _debug2 "_createkey for file:$f" |
43822d37 | 1072 | eccname="$length" |
4c2a3841 | 1073 | if _startswith "$length" "ec-"; then |
f9a6988e | 1074 | length=$(printf "%s" "$length" | cut -d '-' -f 2-100) |
e22bcf7c | 1075 | |
4c2a3841 | 1076 | if [ "$length" = "256" ]; then |
e22bcf7c | 1077 | eccname="prime256v1" |
1078 | fi | |
4c2a3841 | 1079 | if [ "$length" = "384" ]; then |
e22bcf7c | 1080 | eccname="secp384r1" |
1081 | fi | |
4c2a3841 | 1082 | if [ "$length" = "521" ]; then |
e22bcf7c | 1083 | eccname="secp521r1" |
1084 | fi | |
43822d37 | 1085 | |
e22bcf7c | 1086 | fi |
1087 | ||
4c2a3841 | 1088 | if [ -z "$length" ]; then |
1089 | length=2048 | |
43822d37 | 1090 | fi |
4c2a3841 | 1091 | |
cbcd7e0f | 1092 | _debug "Use length $length" |
43822d37 | 1093 | |
81532f37 | 1094 | if ! touch "$f" >/dev/null 2>&1; then |
1095 | _f_path="$(dirname "$f")" | |
1096 | _debug _f_path "$_f_path" | |
1097 | if ! mkdir -p "$_f_path"; then | |
1098 | _err "Can not create path: $_f_path" | |
1099 | return 1 | |
1100 | fi | |
1101 | fi | |
1102 | ||
4c2a3841 | 1103 | if _isEccKey "$length"; then |
cbcd7e0f | 1104 | _debug "Using ec name: $eccname" |
e6df1828 | 1105 | if _opkey="$(${ACME_OPENSSL_BIN:-openssl} ecparam -name "$eccname" -genkey 2>/dev/null)"; then |
1106 | echo "$_opkey" >"$f" | |
1107 | else | |
1108 | _err "error ecc key name: $eccname" | |
1109 | return 1 | |
1110 | fi | |
e22bcf7c | 1111 | else |
cbcd7e0f | 1112 | _debug "Using RSA: $length" |
e6df1828 | 1113 | if _opkey="$(${ACME_OPENSSL_BIN:-openssl} genrsa "$length" 2>/dev/null)"; then |
1114 | echo "$_opkey" >"$f" | |
1115 | else | |
1116 | _err "error rsa key: $length" | |
1117 | return 1 | |
1118 | fi | |
e22bcf7c | 1119 | fi |
43822d37 | 1120 | |
4c2a3841 | 1121 | if [ "$?" != "0" ]; then |
43822d37 | 1122 | _err "Create key error." |
1123 | return 1 | |
1124 | fi | |
e22bcf7c | 1125 | } |
1126 | ||
9774b01b | 1127 | #domain |
1128 | _is_idn() { | |
1129 | _is_idn_d="$1" | |
049be104 | 1130 | _debug2 _is_idn_d "$_is_idn_d" |
d10f40f1 | 1131 | _idn_temp=$(printf "%s" "$_is_idn_d" | tr -d '0-9' | tr -d 'a-z' | tr -d 'A-Z' | tr -d '*.,-_') |
049be104 | 1132 | _debug2 _idn_temp "$_idn_temp" |
1133 | [ "$_idn_temp" ] | |
9774b01b | 1134 | } |
1135 | ||
1136 | #aa.com | |
1137 | #aa.com,bb.com,cc.com | |
1138 | _idn() { | |
1139 | __idn_d="$1" | |
4c2a3841 | 1140 | if ! _is_idn "$__idn_d"; then |
9774b01b | 1141 | printf "%s" "$__idn_d" |
1142 | return 0 | |
1143 | fi | |
4c2a3841 | 1144 | |
1145 | if _exists idn; then | |
1146 | if _contains "$__idn_d" ','; then | |
9774b01b | 1147 | _i_first="1" |
4c2a3841 | 1148 | for f in $(echo "$__idn_d" | tr ',' ' '); do |
9774b01b | 1149 | [ -z "$f" ] && continue |
4c2a3841 | 1150 | if [ -z "$_i_first" ]; then |
9774b01b | 1151 | printf "%s" "," |
1152 | else | |
1153 | _i_first="" | |
1154 | fi | |
2a1e06f8 | 1155 | idn --quiet "$f" | tr -d "\r\n" |
9774b01b | 1156 | done |
1157 | else | |
1158 | idn "$__idn_d" | tr -d "\r\n" | |
1159 | fi | |
1160 | else | |
1161 | _err "Please install idn to process IDN names." | |
1162 | fi | |
1163 | } | |
1164 | ||
08681f4a | 1165 | #_createcsr cn san_list keyfile csrfile conf acmeValidationv1 |
e22bcf7c | 1166 | _createcsr() { |
1167 | _debug _createcsr | |
1168 | domain="$1" | |
1169 | domainlist="$2" | |
0c9546cc | 1170 | csrkey="$3" |
e22bcf7c | 1171 | csr="$4" |
1172 | csrconf="$5" | |
08681f4a | 1173 | acmeValidationv1="$6" |
e22bcf7c | 1174 | _debug2 domain "$domain" |
1175 | _debug2 domainlist "$domainlist" | |
0c9546cc | 1176 | _debug2 csrkey "$csrkey" |
1177 | _debug2 csr "$csr" | |
1178 | _debug2 csrconf "$csrconf" | |
4c2a3841 | 1179 | |
1180 | printf "[ req_distinguished_name ]\n[ req ]\ndistinguished_name = req_distinguished_name\nreq_extensions = v3_req\n[ v3_req ]\n\nkeyUsage = nonRepudiation, digitalSignature, keyEncipherment" >"$csrconf" | |
1181 | ||
08681f4a | 1182 | if [ "$acmeValidationv1" ]; then |
4962cc3d | 1183 | domainlist="$(_idn "$domainlist")" |
08681f4a | 1184 | printf -- "\nsubjectAltName=DNS:$domainlist" >>"$csrconf" |
1185 | elif [ -z "$domainlist" ] || [ "$domainlist" = "$NO_VALUE" ]; then | |
e22bcf7c | 1186 | #single domain |
1187 | _info "Single domain" "$domain" | |
0093dc3d | 1188 | printf -- "\nsubjectAltName=DNS:$(_idn "$domain")" >>"$csrconf" |
e22bcf7c | 1189 | else |
f9a6988e | 1190 | domainlist="$(_idn "$domainlist")" |
9774b01b | 1191 | _debug2 domainlist "$domainlist" |
4c2a3841 | 1192 | if _contains "$domainlist" ","; then |
0093dc3d | 1193 | alt="DNS:$(_idn "$domain"),DNS:$(echo "$domainlist" | sed "s/,,/,/g" | sed "s/,/,DNS:/g")" |
e22bcf7c | 1194 | else |
0093dc3d | 1195 | alt="DNS:$(_idn "$domain"),DNS:$domainlist" |
e22bcf7c | 1196 | fi |
3c07f57a | 1197 | #multi |
e22bcf7c | 1198 | _info "Multi domain" "$alt" |
4c2a3841 | 1199 | printf -- "\nsubjectAltName=$alt" >>"$csrconf" |
0c9546cc | 1200 | fi |
6ba1eda9 | 1201 | if [ "$Le_OCSP_Staple" = "1" ]; then |
96db9362 | 1202 | _savedomainconf Le_OCSP_Staple "$Le_OCSP_Staple" |
4c2a3841 | 1203 | printf -- "\nbasicConstraints = CA:FALSE\n1.3.6.1.5.5.7.1.24=DER:30:03:02:01:05" >>"$csrconf" |
e22bcf7c | 1204 | fi |
4c2a3841 | 1205 | |
08681f4a | 1206 | if [ "$acmeValidationv1" ]; then |
ad613e24 | 1207 | printf "\n1.3.6.1.5.5.7.1.31=critical,DER:04:20:${acmeValidationv1}" >>"${csrconf}" |
08681f4a | 1208 | fi |
1209 | ||
9774b01b | 1210 | _csr_cn="$(_idn "$domain")" |
1211 | _debug2 _csr_cn "$_csr_cn" | |
34f25fa5 | 1212 | if _contains "$(uname -a)" "MINGW"; then |
d8ba26e6 | 1213 | ${ACME_OPENSSL_BIN:-openssl} req -new -sha256 -key "$csrkey" -subj "//CN=$_csr_cn" -config "$csrconf" -out "$csr" |
34f25fa5 | 1214 | else |
d8ba26e6 | 1215 | ${ACME_OPENSSL_BIN:-openssl} req -new -sha256 -key "$csrkey" -subj "/CN=$_csr_cn" -config "$csrconf" -out "$csr" |
34f25fa5 | 1216 | fi |
e22bcf7c | 1217 | } |
1218 | ||
1219 | #_signcsr key csr conf cert | |
1220 | _signcsr() { | |
1221 | key="$1" | |
1222 | csr="$2" | |
1223 | conf="$3" | |
1224 | cert="$4" | |
5aa146a5 | 1225 | _debug "_signcsr" |
4c2a3841 | 1226 | |
d8ba26e6 | 1227 | _msg="$(${ACME_OPENSSL_BIN:-openssl} x509 -req -days 365 -in "$csr" -signkey "$key" -extensions v3_req -extfile "$conf" -out "$cert" 2>&1)" |
5aa146a5 | 1228 | _ret="$?" |
1229 | _debug "$_msg" | |
1230 | return $_ret | |
e22bcf7c | 1231 | } |
1232 | ||
10afcaca | 1233 | #_csrfile |
1234 | _readSubjectFromCSR() { | |
1235 | _csrfile="$1" | |
4c2a3841 | 1236 | if [ -z "$_csrfile" ]; then |
10afcaca | 1237 | _usage "_readSubjectFromCSR mycsr.csr" |
1238 | return 1 | |
1239 | fi | |
b963dadc | 1240 | ${ACME_OPENSSL_BIN:-openssl} req -noout -in "$_csrfile" -subject | tr ',' "\n" | _egrep_o "CN *=.*" | cut -d = -f 2 | cut -d / -f 1 | tr -d ' \n' |
10afcaca | 1241 | } |
1242 | ||
1243 | #_csrfile | |
1244 | #echo comma separated domain list | |
1245 | _readSubjectAltNamesFromCSR() { | |
1246 | _csrfile="$1" | |
4c2a3841 | 1247 | if [ -z "$_csrfile" ]; then |
10afcaca | 1248 | _usage "_readSubjectAltNamesFromCSR mycsr.csr" |
1249 | return 1 | |
1250 | fi | |
4c2a3841 | 1251 | |
10afcaca | 1252 | _csrsubj="$(_readSubjectFromCSR "$_csrfile")" |
1253 | _debug _csrsubj "$_csrsubj" | |
4c2a3841 | 1254 | |
d8ba26e6 | 1255 | _dnsAltnames="$(${ACME_OPENSSL_BIN:-openssl} req -noout -text -in "$_csrfile" | grep "^ *DNS:.*" | tr -d ' \n')" |
10afcaca | 1256 | _debug _dnsAltnames "$_dnsAltnames" |
4c2a3841 | 1257 | |
1258 | if _contains "$_dnsAltnames," "DNS:$_csrsubj,"; then | |
10afcaca | 1259 | _debug "AltNames contains subject" |
2b9ebd66 | 1260 | _excapedAlgnames="$(echo "$_dnsAltnames" | tr '*' '#')" |
1261 | _debug _excapedAlgnames "$_excapedAlgnames" | |
1262 | _escapedSubject="$(echo "$_csrsubj" | tr '*' '#')" | |
1263 | _debug _escapedSubject "$_escapedSubject" | |
1264 | _dnsAltnames="$(echo "$_excapedAlgnames," | sed "s/DNS:$_escapedSubject,//g" | tr '#' '*' | sed "s/,\$//g")" | |
1265 | _debug _dnsAltnames "$_dnsAltnames" | |
10afcaca | 1266 | else |
1267 | _debug "AltNames doesn't contain subject" | |
1268 | fi | |
4c2a3841 | 1269 | |
2b9ebd66 | 1270 | echo "$_dnsAltnames" | sed "s/DNS://g" |
10afcaca | 1271 | } |
1272 | ||
3c07f57a | 1273 | #_csrfile |
10afcaca | 1274 | _readKeyLengthFromCSR() { |
1275 | _csrfile="$1" | |
4c2a3841 | 1276 | if [ -z "$_csrfile" ]; then |
1643b476 | 1277 | _usage "_readKeyLengthFromCSR mycsr.csr" |
10afcaca | 1278 | return 1 |
1279 | fi | |
4c2a3841 | 1280 | |
d8ba26e6 | 1281 | _outcsr="$(${ACME_OPENSSL_BIN:-openssl} req -noout -text -in "$_csrfile")" |
7df062b7 | 1282 | _debug2 _outcsr "$_outcsr" |
4c2a3841 | 1283 | if _contains "$_outcsr" "Public Key Algorithm: id-ecPublicKey"; then |
10afcaca | 1284 | _debug "ECC CSR" |
482cb737 | 1285 | echo "$_outcsr" | tr "\t" " " | _egrep_o "^ *ASN1 OID:.*" | cut -d ':' -f 2 | tr -d ' ' |
10afcaca | 1286 | else |
1287 | _debug "RSA CSR" | |
eb0ef6bd | 1288 | _rkl="$(echo "$_outcsr" | tr "\t" " " | _egrep_o "^ *Public.Key:.*" | cut -d '(' -f 2 | cut -d ' ' -f 1)" |
1289 | if [ "$_rkl" ]; then | |
1290 | echo "$_rkl" | |
1291 | else | |
1292 | echo "$_outcsr" | tr "\t" " " | _egrep_o "RSA Public.Key:.*" | cut -d '(' -f 2 | cut -d ' ' -f 1 | |
1293 | fi | |
10afcaca | 1294 | fi |
1295 | } | |
1296 | ||
34c27e09 | 1297 | _ss() { |
1298 | _port="$1" | |
4c2a3841 | 1299 | |
1300 | if _exists "ss"; then | |
edf08da6 | 1301 | _debug "Using: ss" |
14d7bfda | 1302 | ss -ntpl 2>/dev/null | grep ":$_port " |
edf08da6 | 1303 | return 0 |
1304 | fi | |
1305 | ||
4c2a3841 | 1306 | if _exists "netstat"; then |
251fc37c | 1307 | _debug "Using: netstat" |
97147b59 | 1308 | if netstat -help 2>&1 | grep "\-p proto" >/dev/null; then |
ccb96535 | 1309 | #for windows version netstat tool |
0463b5d6 | 1310 | netstat -an -p tcp | grep "LISTENING" | grep ":$_port " |
ccb96535 | 1311 | else |
4c2a3841 | 1312 | if netstat -help 2>&1 | grep "\-p protocol" >/dev/null; then |
19539575 | 1313 | netstat -an -p tcp | grep LISTEN | grep ":$_port " |
4c2a3841 | 1314 | elif netstat -help 2>&1 | grep -- '-P protocol' >/dev/null; then |
22ea4004 | 1315 | #for solaris |
e3c66532 | 1316 | netstat -an -P tcp | grep "\.$_port " | grep "LISTEN" |
f19f2100 | 1317 | elif netstat -help 2>&1 | grep "\-p" >/dev/null; then |
f21dd911 | 1318 | #for full linux |
19539575 | 1319 | netstat -ntpl | grep ":$_port " |
f21dd911 | 1320 | else |
1321 | #for busybox (embedded linux; no pid support) | |
1322 | netstat -ntl 2>/dev/null | grep ":$_port " | |
edf08da6 | 1323 | fi |
ccb96535 | 1324 | fi |
34c27e09 | 1325 | return 0 |
1326 | fi | |
edf08da6 | 1327 | |
34c27e09 | 1328 | return 1 |
1329 | } | |
1330 | ||
8eab77f3 BC |
1331 | #outfile key cert cacert [password [name [caname]]] |
1332 | _toPkcs() { | |
1333 | _cpfx="$1" | |
1334 | _ckey="$2" | |
1335 | _ccert="$3" | |
1336 | _cca="$4" | |
1337 | pfxPassword="$5" | |
1338 | pfxName="$6" | |
1339 | pfxCaname="$7" | |
1340 | ||
1341 | if [ "$pfxCaname" ]; then | |
1342 | ${ACME_OPENSSL_BIN:-openssl} pkcs12 -export -out "$_cpfx" -inkey "$_ckey" -in "$_ccert" -certfile "$_cca" -password "pass:$pfxPassword" -name "$pfxName" -caname "$pfxCaname" | |
1343 | elif [ "$pfxName" ]; then | |
1344 | ${ACME_OPENSSL_BIN:-openssl} pkcs12 -export -out "$_cpfx" -inkey "$_ckey" -in "$_ccert" -certfile "$_cca" -password "pass:$pfxPassword" -name "$pfxName" | |
1345 | elif [ "$pfxPassword" ]; then | |
1346 | ${ACME_OPENSSL_BIN:-openssl} pkcs12 -export -out "$_cpfx" -inkey "$_ckey" -in "$_ccert" -certfile "$_cca" -password "pass:$pfxPassword" | |
1347 | else | |
1348 | ${ACME_OPENSSL_BIN:-openssl} pkcs12 -export -out "$_cpfx" -inkey "$_ckey" -in "$_ccert" -certfile "$_cca" | |
1349 | fi | |
1350 | ||
1351 | } | |
1352 | ||
43822d37 | 1353 | #domain [password] [isEcc] |
ac2d5123 | 1354 | toPkcs() { |
1355 | domain="$1" | |
1356 | pfxPassword="$2" | |
4c2a3841 | 1357 | if [ -z "$domain" ]; then |
43822d37 | 1358 | _usage "Usage: $PROJECT_ENTRY --toPkcs -d domain [--password pfx-password]" |
ac2d5123 | 1359 | return 1 |
1360 | fi | |
1361 | ||
43822d37 | 1362 | _isEcc="$3" |
4c2a3841 | 1363 | |
43822d37 | 1364 | _initpath "$domain" "$_isEcc" |
1365 | ||
8eab77f3 | 1366 | _toPkcs "$CERT_PFX_PATH" "$CERT_KEY_PATH" "$CERT_PATH" "$CA_CERT_PATH" "$pfxPassword" |
4c2a3841 | 1367 | |
1368 | if [ "$?" = "0" ]; then | |
ac2d5123 | 1369 | _info "Success, Pfx is exported to: $CERT_PFX_PATH" |
1370 | fi | |
1371 | ||
1372 | } | |
1373 | ||
4410226d | 1374 | #domain [isEcc] |
1375 | toPkcs8() { | |
1376 | domain="$1" | |
1377 | ||
1378 | if [ -z "$domain" ]; then | |
1379 | _usage "Usage: $PROJECT_ENTRY --toPkcs8 -d domain [--ecc]" | |
1380 | return 1 | |
1381 | fi | |
1382 | ||
1383 | _isEcc="$2" | |
1384 | ||
1385 | _initpath "$domain" "$_isEcc" | |
1386 | ||
d8ba26e6 | 1387 | ${ACME_OPENSSL_BIN:-openssl} pkcs8 -topk8 -inform PEM -outform PEM -nocrypt -in "$CERT_KEY_PATH" -out "$CERT_PKCS8_PATH" |
4410226d | 1388 | |
1389 | if [ "$?" = "0" ]; then | |
1390 | _info "Success, $CERT_PKCS8_PATH" | |
1391 | fi | |
1392 | ||
1393 | } | |
1394 | ||
3c07f57a | 1395 | #[2048] |
4c3b3608 | 1396 | createAccountKey() { |
1397 | _info "Creating account key" | |
4c2a3841 | 1398 | if [ -z "$1" ]; then |
5fbc47eb | 1399 | _usage "Usage: $PROJECT_ENTRY --createAccountKey --accountkeylength 2048" |
4c3b3608 | 1400 | return |
1401 | fi | |
4c2a3841 | 1402 | |
5fbc47eb | 1403 | length=$1 |
57e58ce7 | 1404 | _create_account_key "$length" |
1405 | ||
1406 | } | |
1407 | ||
1408 | _create_account_key() { | |
1409 | ||
5fbc47eb | 1410 | length=$1 |
4c2a3841 | 1411 | |
1412 | if [ -z "$length" ] || [ "$length" = "$NO_VALUE" ]; then | |
57e58ce7 | 1413 | _debug "Use default length $DEFAULT_ACCOUNT_KEY_LENGTH" |
1414 | length="$DEFAULT_ACCOUNT_KEY_LENGTH" | |
4c3b3608 | 1415 | fi |
4c2a3841 | 1416 | |
5fbc47eb | 1417 | _debug length "$length" |
4c3b3608 | 1418 | _initpath |
5fbc47eb | 1419 | |
57e58ce7 | 1420 | mkdir -p "$CA_DIR" |
4f1888d2 | 1421 | if [ -s "$ACCOUNT_KEY_PATH" ]; then |
4c3b3608 | 1422 | _info "Account key exists, skip" |
4f1888d2 | 1423 | return 0 |
4c3b3608 | 1424 | else |
1425 | #generate account key | |
4f1888d2 | 1426 | if _createkey "$length" "$ACCOUNT_KEY_PATH"; then |
1427 | chmod 600 "$ACCOUNT_KEY_PATH" | |
1428 | _info "Create account key ok." | |
1429 | return 0 | |
1430 | else | |
1431 | _err "Create account key error." | |
1432 | return 1 | |
1433 | fi | |
4c3b3608 | 1434 | fi |
1435 | ||
1436 | } | |
1437 | ||
43822d37 | 1438 | #domain [length] |
4c3b3608 | 1439 | createDomainKey() { |
1440 | _info "Creating domain key" | |
4c2a3841 | 1441 | if [ -z "$1" ]; then |
43822d37 | 1442 | _usage "Usage: $PROJECT_ENTRY --createDomainKey -d domain.com [ --keylength 2048 ]" |
4c3b3608 | 1443 | return |
1444 | fi | |
4c2a3841 | 1445 | |
4c3b3608 | 1446 | domain=$1 |
2844d73d | 1447 | _cdl=$2 |
e22bcf7c | 1448 | |
2844d73d | 1449 | if [ -z "$_cdl" ]; then |
57e58ce7 | 1450 | _debug "Use DEFAULT_DOMAIN_KEY_LENGTH=$DEFAULT_DOMAIN_KEY_LENGTH" |
2844d73d | 1451 | _cdl="$DEFAULT_DOMAIN_KEY_LENGTH" |
57e58ce7 | 1452 | fi |
e22bcf7c | 1453 | |
2844d73d | 1454 | _initpath "$domain" "$_cdl" |
4c2a3841 | 1455 | |
4f1888d2 | 1456 | if [ ! -f "$CERT_KEY_PATH" ] || [ ! -s "$CERT_KEY_PATH" ] || ([ "$FORCE" ] && ! [ "$IS_RENEW" ]) || [ "$Le_ForceNewDomainKey" = "1" ]; then |
2844d73d | 1457 | if _createkey "$_cdl" "$CERT_KEY_PATH"; then |
1458 | _savedomainconf Le_Keylength "$_cdl" | |
1459 | _info "The domain key is here: $(__green $CERT_KEY_PATH)" | |
7aeb113c | 1460 | return 0 |
4f1888d2 | 1461 | else |
a7420ca3 | 1462 | _err "Can not create domain key" |
4f1888d2 | 1463 | return 1 |
2844d73d | 1464 | fi |
4c3b3608 | 1465 | else |
4c2a3841 | 1466 | if [ "$IS_RENEW" ]; then |
4c3b3608 | 1467 | _info "Domain key exists, skip" |
1468 | return 0 | |
1469 | else | |
1470 | _err "Domain key exists, do you want to overwrite the key?" | |
41e3eafa | 1471 | _err "Add '--force', and try again." |
4c3b3608 | 1472 | return 1 |
1473 | fi | |
1474 | fi | |
1475 | ||
1476 | } | |
1477 | ||
43822d37 | 1478 | # domain domainlist isEcc |
4c3b3608 | 1479 | createCSR() { |
1480 | _info "Creating csr" | |
4c2a3841 | 1481 | if [ -z "$1" ]; then |
43822d37 | 1482 | _usage "Usage: $PROJECT_ENTRY --createCSR -d domain1.com [-d domain2.com -d domain3.com ... ]" |
4c3b3608 | 1483 | return |
1484 | fi | |
4c2a3841 | 1485 | |
43822d37 | 1486 | domain="$1" |
1487 | domainlist="$2" | |
1488 | _isEcc="$3" | |
4c2a3841 | 1489 | |
43822d37 | 1490 | _initpath "$domain" "$_isEcc" |
4c2a3841 | 1491 | |
1492 | if [ -f "$CSR_PATH" ] && [ "$IS_RENEW" ] && [ -z "$FORCE" ]; then | |
4c3b3608 | 1493 | _info "CSR exists, skip" |
1494 | return | |
1495 | fi | |
4c2a3841 | 1496 | |
1497 | if [ ! -f "$CERT_KEY_PATH" ]; then | |
43822d37 | 1498 | _err "The key file is not found: $CERT_KEY_PATH" |
1499 | _err "Please create the key file first." | |
1500 | return 1 | |
1501 | fi | |
e22bcf7c | 1502 | _createcsr "$domain" "$domainlist" "$CERT_KEY_PATH" "$CSR_PATH" "$DOMAIN_SSL_CONF" |
4c2a3841 | 1503 | |
4c3b3608 | 1504 | } |
1505 | ||
11927a76 | 1506 | _url_replace() { |
f9a6988e | 1507 | tr '/+' '_-' | tr -d '= ' |
4c3b3608 | 1508 | } |
1509 | ||
1510 | _time2str() { | |
cb115809 JM |
1511 | #BSD |
1512 | if date -u -r "$1" 2>/dev/null; then | |
4c3b3608 | 1513 | return |
1514 | fi | |
4c2a3841 | 1515 | |
cb115809 JM |
1516 | #Linux |
1517 | if date -u -d@"$1" 2>/dev/null; then | |
4c3b3608 | 1518 | return |
1519 | fi | |
4c2a3841 | 1520 | |
cb115809 | 1521 | #Solaris |
4c2a3841 | 1522 | if _exists adb; then |
031e885e | 1523 | _t_s_a=$(echo "0t${1}=Y" | adb) |
1524 | echo "$_t_s_a" | |
22ea4004 | 1525 | fi |
4c2a3841 | 1526 | |
a07395fb M |
1527 | #Busybox |
1528 | if echo "$1" | awk '{ print strftime("%c", $0); }' 2>/dev/null; then | |
1529 | return | |
1530 | fi | |
4c3b3608 | 1531 | } |
1532 | ||
eae29099 | 1533 | _normalizeJson() { |
1534 | sed "s/\" *: *\([\"{\[]\)/\":\1/g" | sed "s/^ *\([^ ]\)/\1/" | tr -d "\r\n" | |
1535 | } | |
1536 | ||
44df2967 | 1537 | _stat() { |
1538 | #Linux | |
4c2a3841 | 1539 | if stat -c '%U:%G' "$1" 2>/dev/null; then |
44df2967 | 1540 | return |
1541 | fi | |
4c2a3841 | 1542 | |
44df2967 | 1543 | #BSD |
4c2a3841 | 1544 | if stat -f '%Su:%Sg' "$1" 2>/dev/null; then |
44df2967 | 1545 | return |
1546 | fi | |
4c2a3841 | 1547 | |
1548 | return 1 #error, 'stat' not found | |
44df2967 | 1549 | } |
1550 | ||
166096dc | 1551 | #keyfile |
1552 | _calcjwk() { | |
1553 | keyfile="$1" | |
4c2a3841 | 1554 | if [ -z "$keyfile" ]; then |
43822d37 | 1555 | _usage "Usage: _calcjwk keyfile" |
166096dc | 1556 | return 1 |
1557 | fi | |
4c2a3841 | 1558 | |
1559 | if [ "$JWK_HEADER" ] && [ "$__CACHED_JWK_KEY_FILE" = "$keyfile" ]; then | |
ae2db62f | 1560 | _debug2 "Use cached jwk for file: $__CACHED_JWK_KEY_FILE" |
1561 | return 0 | |
1562 | fi | |
4c2a3841 | 1563 | |
4c2a3841 | 1564 | if grep "BEGIN RSA PRIVATE KEY" "$keyfile" >/dev/null 2>&1; then |
166096dc | 1565 | _debug "RSA key" |
d8ba26e6 | 1566 | pub_exp=$(${ACME_OPENSSL_BIN:-openssl} rsa -in "$keyfile" -noout -text | grep "^publicExponent:" | cut -d '(' -f 2 | cut -d 'x' -f 2 | cut -d ')' -f 1) |
4c2a3841 | 1567 | if [ "${#pub_exp}" = "5" ]; then |
166096dc | 1568 | pub_exp=0$pub_exp |
1569 | fi | |
22ea4004 | 1570 | _debug3 pub_exp "$pub_exp" |
4c2a3841 | 1571 | |
f9a6988e | 1572 | e=$(echo "$pub_exp" | _h2b | _base64) |
22ea4004 | 1573 | _debug3 e "$e" |
4c2a3841 | 1574 | |
d8ba26e6 | 1575 | modulus=$(${ACME_OPENSSL_BIN:-openssl} rsa -in "$keyfile" -modulus -noout | cut -d '=' -f 2) |
22ea4004 | 1576 | _debug3 modulus "$modulus" |
11927a76 | 1577 | n="$(printf "%s" "$modulus" | _h2b | _base64 | _url_replace)" |
4d8b99a3 | 1578 | _debug3 n "$n" |
1579 | ||
166096dc | 1580 | jwk='{"e": "'$e'", "kty": "RSA", "n": "'$n'"}' |
22ea4004 | 1581 | _debug3 jwk "$jwk" |
4c2a3841 | 1582 | |
5982f4bc | 1583 | JWK_HEADER='{"alg": "RS256", "jwk": '$jwk'}' |
1584 | JWK_HEADERPLACE_PART1='{"nonce": "' | |
c1151b0d | 1585 | JWK_HEADERPLACE_PART2='", "alg": "RS256"' |
4c2a3841 | 1586 | elif grep "BEGIN EC PRIVATE KEY" "$keyfile" >/dev/null 2>&1; then |
166096dc | 1587 | _debug "EC key" |
d8ba26e6 | 1588 | crv="$(${ACME_OPENSSL_BIN:-openssl} ec -in "$keyfile" -noout -text 2>/dev/null | grep "^NIST CURVE:" | cut -d ":" -f 2 | tr -d " \r\n")" |
22ea4004 | 1589 | _debug3 crv "$crv" |
f87890cb | 1590 | __ECC_KEY_LEN=$(echo "$crv" | cut -d "-" -f 2) |
1591 | if [ "$__ECC_KEY_LEN" = "521" ]; then | |
1592 | __ECC_KEY_LEN=512 | |
1593 | fi | |
1594 | _debug3 __ECC_KEY_LEN "$__ECC_KEY_LEN" | |
4c2a3841 | 1595 | if [ -z "$crv" ]; then |
d22b7938 | 1596 | _debug "Let's try ASN1 OID" |
d8ba26e6 | 1597 | crv_oid="$(${ACME_OPENSSL_BIN:-openssl} ec -in "$keyfile" -noout -text 2>/dev/null | grep "^ASN1 OID:" | cut -d ":" -f 2 | tr -d " \r\n")" |
cae9cee2 | 1598 | _debug3 crv_oid "$crv_oid" |
d22b7938 | 1599 | case "${crv_oid}" in |
1600 | "prime256v1") | |
4c2a3841 | 1601 | crv="P-256" |
f87890cb | 1602 | __ECC_KEY_LEN=256 |
4c2a3841 | 1603 | ;; |
d22b7938 | 1604 | "secp384r1") |
4c2a3841 | 1605 | crv="P-384" |
f87890cb | 1606 | __ECC_KEY_LEN=384 |
4c2a3841 | 1607 | ;; |
d22b7938 | 1608 | "secp521r1") |
4c2a3841 | 1609 | crv="P-521" |
f87890cb | 1610 | __ECC_KEY_LEN=512 |
4c2a3841 | 1611 | ;; |
d22b7938 | 1612 | *) |
4c2a3841 | 1613 | _err "ECC oid : $crv_oid" |
1614 | return 1 | |
1615 | ;; | |
067d586c | 1616 | esac |
d22b7938 | 1617 | _debug3 crv "$crv" |
1618 | fi | |
4c2a3841 | 1619 | |
d8ba26e6 | 1620 | pubi="$(${ACME_OPENSSL_BIN:-openssl} ec -in "$keyfile" -noout -text 2>/dev/null | grep -n pub: | cut -d : -f 1)" |
79a267ab | 1621 | pubi=$(_math "$pubi" + 1) |
22ea4004 | 1622 | _debug3 pubi "$pubi" |
4c2a3841 | 1623 | |
d8ba26e6 | 1624 | pubj="$(${ACME_OPENSSL_BIN:-openssl} ec -in "$keyfile" -noout -text 2>/dev/null | grep -n "ASN1 OID:" | cut -d : -f 1)" |
79a267ab | 1625 | pubj=$(_math "$pubj" - 1) |
22ea4004 | 1626 | _debug3 pubj "$pubj" |
4c2a3841 | 1627 | |
d8ba26e6 | 1628 | pubtext="$(${ACME_OPENSSL_BIN:-openssl} ec -in "$keyfile" -noout -text 2>/dev/null | sed -n "$pubi,${pubj}p" | tr -d " \n\r")" |
22ea4004 | 1629 | _debug3 pubtext "$pubtext" |
4c2a3841 | 1630 | |
95e06de5 | 1631 | xlen="$(printf "%s" "$pubtext" | tr -d ':' | wc -c)" |
79a267ab | 1632 | xlen=$(_math "$xlen" / 4) |
22ea4004 | 1633 | _debug3 xlen "$xlen" |
00a50605 | 1634 | |
998783eb | 1635 | xend=$(_math "$xlen" + 1) |
f9a6988e | 1636 | x="$(printf "%s" "$pubtext" | cut -d : -f 2-"$xend")" |
22ea4004 | 1637 | _debug3 x "$x" |
4c2a3841 | 1638 | |
11927a76 | 1639 | x64="$(printf "%s" "$x" | tr -d : | _h2b | _base64 | _url_replace)" |
22ea4004 | 1640 | _debug3 x64 "$x64" |
00a50605 | 1641 | |
19539575 | 1642 | xend=$(_math "$xend" + 1) |
f9a6988e | 1643 | y="$(printf "%s" "$pubtext" | cut -d : -f "$xend"-10000)" |
22ea4004 | 1644 | _debug3 y "$y" |
4c2a3841 | 1645 | |
11927a76 | 1646 | y64="$(printf "%s" "$y" | tr -d : | _h2b | _base64 | _url_replace)" |
22ea4004 | 1647 | _debug3 y64 "$y64" |
4c2a3841 | 1648 | |
ae2db62f | 1649 | jwk='{"crv": "'$crv'", "kty": "EC", "x": "'$x64'", "y": "'$y64'"}' |
22ea4004 | 1650 | _debug3 jwk "$jwk" |
4c2a3841 | 1651 | |
f87890cb | 1652 | JWK_HEADER='{"alg": "ES'$__ECC_KEY_LEN'", "jwk": '$jwk'}' |
5982f4bc | 1653 | JWK_HEADERPLACE_PART1='{"nonce": "' |
c1151b0d | 1654 | JWK_HEADERPLACE_PART2='", "alg": "ES'$__ECC_KEY_LEN'"' |
166096dc | 1655 | else |
238990a2 | 1656 | _err "Only RSA or EC key is supported. keyfile=$keyfile" |
1657 | _debug2 "$(cat "$keyfile")" | |
166096dc | 1658 | return 1 |
1659 | fi | |
1660 | ||
5982f4bc | 1661 | _debug3 JWK_HEADER "$JWK_HEADER" |
ae2db62f | 1662 | __CACHED_JWK_KEY_FILE="$keyfile" |
166096dc | 1663 | } |
fac1e367 | 1664 | |
3aae1ae3 | 1665 | _time() { |
1666 | date -u "+%s" | |
1667 | } | |
fac1e367 | 1668 | |
5d2c5b01 | 1669 | _utc_date() { |
1670 | date -u "+%Y-%m-%d %H:%M:%S" | |
1671 | } | |
1672 | ||
fac1e367 | 1673 | _mktemp() { |
4c2a3841 | 1674 | if _exists mktemp; then |
1675 | if mktemp 2>/dev/null; then | |
610e0f21 | 1676 | return 0 |
4c2a3841 | 1677 | elif _contains "$(mktemp 2>&1)" "-t prefix" && mktemp -t "$PROJECT_NAME" 2>/dev/null; then |
5c48e139 | 1678 | #for Mac osx |
610e0f21 | 1679 | return 0 |
b19ba13a | 1680 | fi |
fac1e367 | 1681 | fi |
4c2a3841 | 1682 | if [ -d "/tmp" ]; then |
3aae1ae3 | 1683 | echo "/tmp/${PROJECT_NAME}wefADf24sf.$(_time).tmp" |
1684 | return 0 | |
4c2a3841 | 1685 | elif [ "$LE_TEMP_DIR" ] && mkdir -p "$LE_TEMP_DIR"; then |
610e0f21 | 1686 | echo "/$LE_TEMP_DIR/wefADf24sf.$(_time).tmp" |
1687 | return 0 | |
3aae1ae3 | 1688 | fi |
1689 | _err "Can not create temp file." | |
fac1e367 | 1690 | } |
1691 | ||
1692 | _inithttp() { | |
1693 | ||
4c2a3841 | 1694 | if [ -z "$HTTP_HEADER" ] || ! touch "$HTTP_HEADER"; then |
fac1e367 | 1695 | HTTP_HEADER="$(_mktemp)" |
1696 | _debug2 HTTP_HEADER "$HTTP_HEADER" | |
1697 | fi | |
1698 | ||
4c2a3841 | 1699 | if [ "$__HTTP_INITIALIZED" ]; then |
1700 | if [ "$_ACME_CURL$_ACME_WGET" ]; then | |
1befee5a | 1701 | _debug2 "Http already initialized." |
1702 | return 0 | |
1703 | fi | |
1704 | fi | |
4c2a3841 | 1705 | |
1706 | if [ -z "$_ACME_CURL" ] && _exists "curl"; then | |
1befee5a | 1707 | _ACME_CURL="curl -L --silent --dump-header $HTTP_HEADER " |
4c2a3841 | 1708 | if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then |
fac1e367 | 1709 | _CURL_DUMP="$(_mktemp)" |
1befee5a | 1710 | _ACME_CURL="$_ACME_CURL --trace-ascii $_CURL_DUMP " |
fac1e367 | 1711 | fi |
1712 | ||
2aa75f03 | 1713 | if [ "$CA_PATH" ]; then |
1714 | _ACME_CURL="$_ACME_CURL --capath $CA_PATH " | |
1715 | elif [ "$CA_BUNDLE" ]; then | |
1befee5a | 1716 | _ACME_CURL="$_ACME_CURL --cacert $CA_BUNDLE " |
78009539 PS |
1717 | fi |
1718 | ||
6ca5f3d8 | 1719 | if _contains "$(curl --help 2>&1)" "--globoff"; then |
1720 | _ACME_CURL="$_ACME_CURL -g " | |
1721 | fi | |
fac1e367 | 1722 | fi |
4c2a3841 | 1723 | |
1befee5a | 1724 | if [ -z "$_ACME_WGET" ] && _exists "wget"; then |
1725 | _ACME_WGET="wget -q" | |
4c2a3841 | 1726 | if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then |
1befee5a | 1727 | _ACME_WGET="$_ACME_WGET -d " |
fac1e367 | 1728 | fi |
2aa75f03 | 1729 | if [ "$CA_PATH" ]; then |
1730 | _ACME_WGET="$_ACME_WGET --ca-directory=$CA_PATH " | |
1731 | elif [ "$CA_BUNDLE" ]; then | |
1732 | _ACME_WGET="$_ACME_WGET --ca-certificate=$CA_BUNDLE " | |
78009539 | 1733 | fi |
fac1e367 | 1734 | fi |
1735 | ||
177b57e1 | 1736 | #from wget 1.14: do not skip body on 404 error |
58ef6d83 | 1737 | if [ "$_ACME_WGET" ] && _contains "$($_ACME_WGET --help 2>&1)" "--content-on-error"; then |
177b57e1 | 1738 | _ACME_WGET="$_ACME_WGET --content-on-error " |
1739 | fi | |
1740 | ||
1befee5a | 1741 | __HTTP_INITIALIZED=1 |
fac1e367 | 1742 | |
fac1e367 | 1743 | } |
fac1e367 | 1744 | |
9cecd525 | 1745 | # body url [needbase64] [POST|PUT|DELETE] [ContentType] |
c60883ef | 1746 | _post() { |
1747 | body="$1" | |
c1151b0d | 1748 | _post_url="$2" |
c60883ef | 1749 | needbase64="$3" |
a4270efa | 1750 | httpmethod="$4" |
45e386b2 | 1751 | _postContentType="$5" |
c60883ef | 1752 | |
4c2a3841 | 1753 | if [ -z "$httpmethod" ]; then |
a4270efa | 1754 | httpmethod="POST" |
1755 | fi | |
1756 | _debug $httpmethod | |
c1151b0d | 1757 | _debug "_post_url" "$_post_url" |
30de13b4 | 1758 | _debug2 "body" "$body" |
6a66ba8a | 1759 | _debug2 "_postContentType" "$_postContentType" |
4c2a3841 | 1760 | |
fac1e367 | 1761 | _inithttp |
4c2a3841 | 1762 | |
9b124070 | 1763 | if [ "$_ACME_CURL" ] && [ "${ACME_USE_WGET:-0}" = "0" ]; then |
1befee5a | 1764 | _CURL="$_ACME_CURL" |
7834c252 | 1765 | if [ "$HTTPS_INSECURE" ]; then |
1766 | _CURL="$_CURL --insecure " | |
1767 | fi | |
5723fd11 | 1768 | if [ "$httpmethod" = "HEAD" ]; then |
1769 | _CURL="$_CURL -I " | |
1770 | fi | |
ec9fc8cb | 1771 | _debug "_CURL" "$_CURL" |
4c2a3841 | 1772 | if [ "$needbase64" ]; then |
1ba4ab2b | 1773 | if [ "$body" ]; then |
1774 | if [ "$_postContentType" ]; then | |
1775 | response="$($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "Content-Type: $_postContentType" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" --data "$body" "$_post_url" | _base64)" | |
1776 | else | |
1777 | response="$($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" --data "$body" "$_post_url" | _base64)" | |
1778 | fi | |
39852662 | 1779 | else |
1ba4ab2b | 1780 | if [ "$_postContentType" ]; then |
1781 | response="$($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "Content-Type: $_postContentType" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" "$_post_url" | _base64)" | |
1782 | else | |
1783 | response="$($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" "$_post_url" | _base64)" | |
1784 | fi | |
39852662 | 1785 | fi |
c60883ef | 1786 | else |
1ba4ab2b | 1787 | if [ "$body" ]; then |
1788 | if [ "$_postContentType" ]; then | |
1789 | response="$($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "Content-Type: $_postContentType" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" --data "$body" "$_post_url")" | |
1790 | else | |
1791 | response="$($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" --data "$body" "$_post_url")" | |
1792 | fi | |
39852662 | 1793 | else |
1ba4ab2b | 1794 | if [ "$_postContentType" ]; then |
1795 | response="$($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "Content-Type: $_postContentType" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" "$_post_url")" | |
1796 | else | |
1797 | response="$($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" "$_post_url")" | |
1798 | fi | |
39852662 | 1799 | fi |
c60883ef | 1800 | fi |
16679b57 | 1801 | _ret="$?" |
4c2a3841 | 1802 | if [ "$_ret" != "0" ]; then |
87ab2d90 | 1803 | _err "Please refer to https://curl.haxx.se/libcurl/c/libcurl-errors.html for error code: $_ret" |
4c2a3841 | 1804 | if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then |
87ab2d90 | 1805 | _err "Here is the curl dump log:" |
1806 | _err "$(cat "$_CURL_DUMP")" | |
1807 | fi | |
687cfcc2 | 1808 | fi |
4c2a3841 | 1809 | elif [ "$_ACME_WGET" ]; then |
7834c252 | 1810 | _WGET="$_ACME_WGET" |
1811 | if [ "$HTTPS_INSECURE" ]; then | |
1812 | _WGET="$_WGET --no-check-certificate " | |
1813 | fi | |
51b4a9e3 | 1814 | if [ "$httpmethod" = "HEAD" ]; then |
1815 | _WGET="$_WGET --read-timeout=3.0 --tries=2 " | |
1816 | fi | |
7834c252 | 1817 | _debug "_WGET" "$_WGET" |
4c2a3841 | 1818 | if [ "$needbase64" ]; then |
1819 | if [ "$httpmethod" = "POST" ]; then | |
ef871775 | 1820 | if [ "$_postContentType" ]; then |
1821 | response="$($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --header "Content-Type: $_postContentType" --post-data="$body" "$_post_url" 2>"$HTTP_HEADER" | _base64)" | |
1822 | else | |
1823 | response="$($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --post-data="$body" "$_post_url" 2>"$HTTP_HEADER" | _base64)" | |
1824 | fi | |
8fb9a709 | 1825 | else |
ef871775 | 1826 | if [ "$_postContentType" ]; then |
1827 | response="$($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --header "Content-Type: $_postContentType" --method $httpmethod --body-data="$body" "$_post_url" 2>"$HTTP_HEADER" | _base64)" | |
1828 | else | |
1829 | response="$($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --method $httpmethod --body-data="$body" "$_post_url" 2>"$HTTP_HEADER" | _base64)" | |
1830 | fi | |
8fb9a709 | 1831 | fi |
c60883ef | 1832 | else |
4c2a3841 | 1833 | if [ "$httpmethod" = "POST" ]; then |
ef871775 | 1834 | if [ "$_postContentType" ]; then |
1835 | response="$($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --header "Content-Type: $_postContentType" --post-data="$body" "$_post_url" 2>"$HTTP_HEADER")" | |
1836 | else | |
1837 | response="$($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --post-data="$body" "$_post_url" 2>"$HTTP_HEADER")" | |
1838 | fi | |
51b4a9e3 | 1839 | elif [ "$httpmethod" = "HEAD" ]; then |
1840 | if [ "$_postContentType" ]; then | |
1841 | response="$($_WGET --spider -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --header "Content-Type: $_postContentType" --post-data="$body" "$_post_url" 2>"$HTTP_HEADER")" | |
1842 | else | |
1843 | response="$($_WGET --spider -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --post-data="$body" "$_post_url" 2>"$HTTP_HEADER")" | |
1844 | fi | |
8fb9a709 | 1845 | else |
ef871775 | 1846 | if [ "$_postContentType" ]; then |
1847 | response="$($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --header "Content-Type: $_postContentType" --method $httpmethod --body-data="$body" "$_post_url" 2>"$HTTP_HEADER")" | |
1848 | else | |
1849 | response="$($_WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --method $httpmethod --body-data="$body" "$_post_url" 2>"$HTTP_HEADER")" | |
1850 | fi | |
8fb9a709 | 1851 | fi |
c60883ef | 1852 | fi |
16679b57 | 1853 | _ret="$?" |
4c2a3841 | 1854 | if [ "$_ret" = "8" ]; then |
9f43c270 | 1855 | _ret=0 |
810c129c | 1856 | _debug "wget returns 8, the server returns a 'Bad request' response, lets process the response later." |
9f43c270 | 1857 | fi |
4c2a3841 | 1858 | if [ "$_ret" != "0" ]; then |
1859 | _err "Please refer to https://www.gnu.org/software/wget/manual/html_node/Exit-Status.html for error code: $_ret" | |
687cfcc2 | 1860 | fi |
c60883ef | 1861 | _sed_i "s/^ *//g" "$HTTP_HEADER" |
d0b748a4 | 1862 | else |
1863 | _ret="$?" | |
1864 | _err "Neither curl nor wget is found, can not do $httpmethod." | |
c60883ef | 1865 | fi |
16679b57 | 1866 | _debug "_ret" "$_ret" |
19539575 | 1867 | printf "%s" "$response" |
16679b57 | 1868 | return $_ret |
c60883ef | 1869 | } |
1870 | ||
75da0713 | 1871 | # url getheader timeout |
c60883ef | 1872 | _get() { |
a4270efa | 1873 | _debug GET |
c60883ef | 1874 | url="$1" |
1875 | onlyheader="$2" | |
75da0713 | 1876 | t="$3" |
79a267ab | 1877 | _debug url "$url" |
72f54ca6 | 1878 | _debug "timeout=$t" |
fac1e367 | 1879 | |
1880 | _inithttp | |
1881 | ||
9b124070 | 1882 | if [ "$_ACME_CURL" ] && [ "${ACME_USE_WGET:-0}" = "0" ]; then |
1befee5a | 1883 | _CURL="$_ACME_CURL" |
7834c252 | 1884 | if [ "$HTTPS_INSECURE" ]; then |
1885 | _CURL="$_CURL --insecure " | |
1886 | fi | |
4c2a3841 | 1887 | if [ "$t" ]; then |
75da0713 | 1888 | _CURL="$_CURL --connect-timeout $t" |
1889 | fi | |
1890 | _debug "_CURL" "$_CURL" | |
4c2a3841 | 1891 | if [ "$onlyheader" ]; then |
f9a6988e | 1892 | $_CURL -I --user-agent "$USER_AGENT" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" "$url" |
c60883ef | 1893 | else |
f9a6988e | 1894 | $_CURL --user-agent "$USER_AGENT" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" "$url" |
c60883ef | 1895 | fi |
9aaf36cd | 1896 | ret=$? |
4c2a3841 | 1897 | if [ "$ret" != "0" ]; then |
d529eb6d | 1898 | _err "Please refer to https://curl.haxx.se/libcurl/c/libcurl-errors.html for error code: $ret" |
4c2a3841 | 1899 | if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then |
fac1e367 | 1900 | _err "Here is the curl dump log:" |
1901 | _err "$(cat "$_CURL_DUMP")" | |
1902 | fi | |
1903 | fi | |
4c2a3841 | 1904 | elif [ "$_ACME_WGET" ]; then |
1befee5a | 1905 | _WGET="$_ACME_WGET" |
7834c252 | 1906 | if [ "$HTTPS_INSECURE" ]; then |
1907 | _WGET="$_WGET --no-check-certificate " | |
1908 | fi | |
4c2a3841 | 1909 | if [ "$t" ]; then |
75da0713 | 1910 | _WGET="$_WGET --timeout=$t" |
1911 | fi | |
1912 | _debug "_WGET" "$_WGET" | |
4c2a3841 | 1913 | if [ "$onlyheader" ]; then |
f9a6988e | 1914 | $_WGET --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" -S -O /dev/null "$url" 2>&1 | sed 's/^[ ]*//g' |
c60883ef | 1915 | else |
f9a6988e | 1916 | $_WGET --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" -O - "$url" |
c60883ef | 1917 | fi |
9aaf36cd | 1918 | ret=$? |
f731a4c7 | 1919 | if [ "$ret" = "8" ]; then |
39a1f1ef | 1920 | ret=0 |
810c129c | 1921 | _debug "wget returns 8, the server returns a 'Bad request' response, lets process the response later." |
9f43c270 | 1922 | fi |
4c2a3841 | 1923 | if [ "$ret" != "0" ]; then |
1924 | _err "Please refer to https://www.gnu.org/software/wget/manual/html_node/Exit-Status.html for error code: $ret" | |
fac1e367 | 1925 | fi |
d0b748a4 | 1926 | else |
1927 | ret=$? | |
1928 | _err "Neither curl nor wget is found, can not do GET." | |
9aaf36cd | 1929 | fi |
ec9fc8cb | 1930 | _debug "ret" "$ret" |
c60883ef | 1931 | return $ret |
1932 | } | |
166096dc | 1933 | |
c2c8f320 | 1934 | _head_n() { |
79a267ab | 1935 | head -n "$1" |
c2c8f320 | 1936 | } |
1937 | ||
1938 | _tail_n() { | |
f9a6988e | 1939 | if ! tail -n "$1" 2>/dev/null; then |
19ab2a29 | 1940 | #fix for solaris |
f9a6988e | 1941 | tail -"$1" |
19ab2a29 | 1942 | fi |
c2c8f320 | 1943 | } |
fac1e367 | 1944 | |
166096dc | 1945 | # url payload needbase64 keyfile |
4c3b3608 | 1946 | _send_signed_request() { |
1947 | url=$1 | |
1948 | payload=$2 | |
1949 | needbase64=$3 | |
166096dc | 1950 | keyfile=$4 |
4c2a3841 | 1951 | if [ -z "$keyfile" ]; then |
166096dc | 1952 | keyfile="$ACCOUNT_KEY_PATH" |
1953 | fi | |
f9a6988e | 1954 | _debug url "$url" |
4c3b3608 | 1955 | _debug payload "$payload" |
4c2a3841 | 1956 | |
1957 | if ! _calcjwk "$keyfile"; then | |
166096dc | 1958 | return 1 |
1959 | fi | |
c60883ef | 1960 | |
cc2d5946 | 1961 | __request_conent_type="$CONTENT_TYPE_JSON" |
1962 | ||
11927a76 | 1963 | payload64=$(printf "%s" "$payload" | _base64 | _url_replace) |
f9a6988e | 1964 | _debug3 payload64 "$payload64" |
4c2a3841 | 1965 | |
709a3fb0 | 1966 | MAX_REQUEST_RETRY_TIMES=20 |
1967 | _sleep_retry_sec=1 | |
0bc745f6 | 1968 | _request_retry_times=0 |
1969 | while [ "${_request_retry_times}" -lt "$MAX_REQUEST_RETRY_TIMES" ]; do | |
aad309ee | 1970 | _request_retry_times=$(_math "$_request_retry_times" + 1) |
b7924ce5 | 1971 | _debug3 _request_retry_times "$_request_retry_times" |
0bc745f6 | 1972 | if [ -z "$_CACHED_NONCE" ]; then |
8f01919f | 1973 | _headers="" |
8bd12ed0 K |
1974 | if [ "$ACME_NEW_NONCE" ]; then |
1975 | _debug2 "Get nonce with HEAD. ACME_NEW_NONCE" "$ACME_NEW_NONCE" | |
cae50e16 | 1976 | nonceurl="$ACME_NEW_NONCE" |
1ba4ab2b | 1977 | if _post "" "$nonceurl" "" "HEAD" "$__request_conent_type" >/dev/null; then |
cae50e16 | 1978 | _headers="$(cat "$HTTP_HEADER")" |
e7f7e96d | 1979 | _debug2 _headers "$_headers" |
1980 | _CACHED_NONCE="$(echo "$_headers" | grep -i "Replay-Nonce:" | _head_n 1 | tr -d "\r\n " | cut -d ':' -f 2)" | |
cae50e16 | 1981 | fi |
1982 | fi | |
e7f7e96d | 1983 | if [ -z "$_CACHED_NONCE" ]; then |
33226307 | 1984 | _debug2 "Get nonce with GET. ACME_DIRECTORY" "$ACME_DIRECTORY" |
cae50e16 | 1985 | nonceurl="$ACME_DIRECTORY" |
1986 | _headers="$(_get "$nonceurl" "onlyheader")" | |
e7f7e96d | 1987 | _debug2 _headers "$_headers" |
1988 | _CACHED_NONCE="$(echo "$_headers" | grep -i "Replay-Nonce:" | _head_n 1 | tr -d "\r\n " | cut -d ':' -f 2)" | |
cae50e16 | 1989 | fi |
e7f7e96d | 1990 | if [ -z "$_CACHED_NONCE" ] && [ "$ACME_NEW_NONCE" ]; then |
1991 | _debug2 "Get nonce with GET. ACME_NEW_NONCE" "$ACME_NEW_NONCE" | |
1992 | nonceurl="$ACME_NEW_NONCE" | |
1993 | _headers="$(_get "$nonceurl" "onlyheader")" | |
1994 | _debug2 _headers "$_headers" | |
1995 | _CACHED_NONCE="$(echo "$_headers" | grep -i "Replay-Nonce:" | _head_n 1 | tr -d "\r\n " | cut -d ':' -f 2)" | |
1996 | fi | |
1997 | _debug2 _CACHED_NONCE "$_CACHED_NONCE" | |
0bc745f6 | 1998 | if [ "$?" != "0" ]; then |
1999 | _err "Can not connect to $nonceurl to get nonce." | |
2000 | return 1 | |
2001 | fi | |
0bc745f6 | 2002 | else |
2003 | _debug2 "Use _CACHED_NONCE" "$_CACHED_NONCE" | |
2004 | fi | |
2005 | nonce="$_CACHED_NONCE" | |
2006 | _debug2 nonce "$nonce" | |
aad309ee | 2007 | if [ -z "$nonce" ]; then |
2008 | _info "Could not get nonce, let's try again." | |
2009 | _sleep 2 | |
2010 | continue | |
2011 | fi | |
c1151b0d | 2012 | if [ "$ACME_VERSION" = "2" ]; then |
114f2a14 | 2013 | if [ "$url" = "$ACME_NEW_ACCOUNT" ]; then |
2014 | protected="$JWK_HEADERPLACE_PART1$nonce\", \"url\": \"${url}$JWK_HEADERPLACE_PART2, \"jwk\": $jwk"'}' | |
2015 | elif [ "$url" = "$ACME_REVOKE_CERT" ] && [ "$keyfile" != "$ACCOUNT_KEY_PATH" ]; then | |
c1151b0d | 2016 | protected="$JWK_HEADERPLACE_PART1$nonce\", \"url\": \"${url}$JWK_HEADERPLACE_PART2, \"jwk\": $jwk"'}' |
2017 | else | |
6b798b01 | 2018 | protected="$JWK_HEADERPLACE_PART1$nonce\", \"url\": \"${url}$JWK_HEADERPLACE_PART2, \"kid\": \"${ACCOUNT_URL}\""'}' |
c1151b0d | 2019 | fi |
2020 | else | |
2021 | protected="$JWK_HEADERPLACE_PART1$nonce\", \"url\": \"${url}$JWK_HEADERPLACE_PART2, \"jwk\": $jwk"'}' | |
2022 | fi | |
0bc745f6 | 2023 | _debug3 protected "$protected" |
a272ee4f | 2024 | |
0bc745f6 | 2025 | protected64="$(printf "%s" "$protected" | _base64 | _url_replace)" |
2026 | _debug3 protected64 "$protected64" | |
4c2a3841 | 2027 | |
0bc745f6 | 2028 | if ! _sig_t="$(printf "%s" "$protected64.$payload64" | _sign "$keyfile" "sha256")"; then |
2029 | _err "Sign request failed." | |
2030 | return 1 | |
2031 | fi | |
2032 | _debug3 _sig_t "$_sig_t" | |
166096dc | 2033 | |
0bc745f6 | 2034 | sig="$(printf "%s" "$_sig_t" | _url_replace)" |
2035 | _debug3 sig "$sig" | |
4c2a3841 | 2036 | |
8bd12ed0 | 2037 | body="{\"protected\": \"$protected64\", \"payload\": \"$payload64\", \"signature\": \"$sig\"}" |
0bc745f6 | 2038 | _debug3 body "$body" |
4c2a3841 | 2039 | |
6a66ba8a | 2040 | response="$(_post "$body" "$url" "$needbase64" "POST" "$__request_conent_type")" |
0bc745f6 | 2041 | _CACHED_NONCE="" |
bbbdcb09 | 2042 | |
0bc745f6 | 2043 | if [ "$?" != "0" ]; then |
2044 | _err "Can not post to $url" | |
2045 | return 1 | |
2046 | fi | |
4c3b3608 | 2047 | |
64802502 | 2048 | responseHeaders="$(cat "$HTTP_HEADER")" |
0bc745f6 | 2049 | _debug2 responseHeaders "$responseHeaders" |
f2acdd27 | 2050 | |
0bc745f6 | 2051 | code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\r\n")" |
2052 | _debug code "$code" | |
4c2a3841 | 2053 | |
f2acdd27 | 2054 | _debug2 original "$response" |
0712e989 | 2055 | if echo "$responseHeaders" | grep -i "Content-Type: *application/json" >/dev/null 2>&1; then |
f2acdd27 | 2056 | response="$(echo "$response" | _normalizeJson)" |
8148bfea | 2057 | fi |
f2acdd27 | 2058 | _debug2 response "$response" |
36309e6d | 2059 | |
f2acdd27 | 2060 | _CACHED_NONCE="$(echo "$responseHeaders" | grep -i "Replay-Nonce:" | _head_n 1 | tr -d "\r\n " | cut -d ':' -f 2)" |
2061 | ||
a0ec5b18 | 2062 | if ! _startswith "$code" "2"; then |
f2acdd27 | 2063 | _body="$response" |
2064 | if [ "$needbase64" ]; then | |
2065 | _body="$(echo "$_body" | _dbase64 multiline)" | |
2066 | _debug3 _body "$_body" | |
2067 | fi | |
2068 | ||
2069 | if _contains "$_body" "JWS has invalid anti-replay nonce" || _contains "$_body" "JWS has an invalid anti-replay nonce"; then | |
2070 | _info "It seems the CA server is busy now, let's wait and retry. Sleeping $_sleep_retry_sec seconds." | |
2071 | _CACHED_NONCE="" | |
2072 | _sleep $_sleep_retry_sec | |
2073 | continue | |
2074 | fi | |
0bc745f6 | 2075 | fi |
a44ea0dd | 2076 | return 0 |
0bc745f6 | 2077 | done |
a44ea0dd | 2078 | _info "Giving up sending to CA server after $MAX_REQUEST_RETRY_TIMES retries." |
2079 | return 1 | |
4c3b3608 | 2080 | |
4c3b3608 | 2081 | } |
4c3b3608 | 2082 | |
2083 | #setopt "file" "opt" "=" "value" [";"] | |
2084 | _setopt() { | |
2085 | __conf="$1" | |
2086 | __opt="$2" | |
2087 | __sep="$3" | |
2088 | __val="$4" | |
2089 | __end="$5" | |
4c2a3841 | 2090 | if [ -z "$__opt" ]; then |
2091 | _usage usage: _setopt '"file" "opt" "=" "value" [";"]' | |
4c3b3608 | 2092 | return |
2093 | fi | |
4c2a3841 | 2094 | if [ ! -f "$__conf" ]; then |
4c3b3608 | 2095 | touch "$__conf" |
2096 | fi | |
2097 | ||
4c2a3841 | 2098 | if grep -n "^$__opt$__sep" "$__conf" >/dev/null; then |
22ea4004 | 2099 | _debug3 OK |
4c2a3841 | 2100 | if _contains "$__val" "&"; then |
79a267ab | 2101 | __val="$(echo "$__val" | sed 's/&/\\&/g')" |
4c3b3608 | 2102 | fi |
79a267ab | 2103 | text="$(cat "$__conf")" |
52f8b787 | 2104 | printf -- "%s\n" "$text" | sed "s|^$__opt$__sep.*$|$__opt$__sep$__val$__end|" >"$__conf" |
4c3b3608 | 2105 | |
4c2a3841 | 2106 | elif grep -n "^#$__opt$__sep" "$__conf" >/dev/null; then |
2107 | if _contains "$__val" "&"; then | |
79a267ab | 2108 | __val="$(echo "$__val" | sed 's/&/\\&/g')" |
4c3b3608 | 2109 | fi |
79a267ab | 2110 | text="$(cat "$__conf")" |
52f8b787 | 2111 | printf -- "%s\n" "$text" | sed "s|^#$__opt$__sep.*$|$__opt$__sep$__val$__end|" >"$__conf" |
4c3b3608 | 2112 | |
2113 | else | |
22ea4004 | 2114 | _debug3 APP |
4c2a3841 | 2115 | echo "$__opt$__sep$__val$__end" >>"$__conf" |
4c3b3608 | 2116 | fi |
1efb2085 | 2117 | _debug3 "$(grep -n "^$__opt$__sep" "$__conf")" |
4c3b3608 | 2118 | } |
2119 | ||
7690f73e | 2120 | #_save_conf file key value base64encode |
8a29fbc8 | 2121 | #save to conf |
2122 | _save_conf() { | |
2123 | _s_c_f="$1" | |
2124 | _sdkey="$2" | |
2125 | _sdvalue="$3" | |
7690f73e | 2126 | _b64encode="$4" |
53c01882 | 2127 | if [ "$_sdvalue" ] && [ "$_b64encode" ]; then |
7690f73e | 2128 | _sdvalue="${B64CONF_START}$(printf "%s" "${_sdvalue}" | _base64)${B64CONF_END}" |
2129 | fi | |
4c2a3841 | 2130 | if [ "$_s_c_f" ]; then |
8a29fbc8 | 2131 | _setopt "$_s_c_f" "$_sdkey" "=" "'$_sdvalue'" |
4d2f38b0 | 2132 | else |
8a29fbc8 | 2133 | _err "config file is empty, can not save $_sdkey=$_sdvalue" |
4d2f38b0 | 2134 | fi |
2135 | } | |
2136 | ||
8a29fbc8 | 2137 | #_clear_conf file key |
2138 | _clear_conf() { | |
2139 | _c_c_f="$1" | |
2140 | _sdkey="$2" | |
4c2a3841 | 2141 | if [ "$_c_c_f" ]; then |
20ea8591 | 2142 | _conf_data="$(cat "$_c_c_f")" |
fa574fe8 | 2143 | echo "$_conf_data" | sed "s/^$_sdkey *=.*$//" >"$_c_c_f" |
4c3b3608 | 2144 | else |
8a29fbc8 | 2145 | _err "config file is empty, can not clear" |
4c3b3608 | 2146 | fi |
2147 | } | |
2148 | ||
8a29fbc8 | 2149 | #_read_conf file key |
2150 | _read_conf() { | |
2151 | _r_c_f="$1" | |
2152 | _sdkey="$2" | |
4c2a3841 | 2153 | if [ -f "$_r_c_f" ]; then |
0cfeee4d | 2154 | _sdv="$( |
2155 | eval "$(grep "^$_sdkey *=" "$_r_c_f")" | |
2156 | eval "printf \"%s\" \"\$$_sdkey\"" | |
c97e43dc | 2157 | )" |
7690f73e | 2158 | if _startswith "$_sdv" "${B64CONF_START}" && _endswith "$_sdv" "${B64CONF_END}"; then |
2159 | _sdv="$(echo "$_sdv" | sed "s/${B64CONF_START}//" | sed "s/${B64CONF_END}//" | _dbase64)" | |
2160 | fi | |
2161 | printf "%s" "$_sdv" | |
61623d22 | 2162 | else |
57e58ce7 | 2163 | _debug "config file is empty, can not read $_sdkey" |
4c3b3608 | 2164 | fi |
4c3b3608 | 2165 | } |
2166 | ||
7690f73e | 2167 | #_savedomainconf key value base64encode |
4c3b3608 | 2168 | #save to domain.conf |
2169 | _savedomainconf() { | |
7690f73e | 2170 | _save_conf "$DOMAIN_CONF" "$@" |
4d2f38b0 | 2171 | } |
2172 | ||
2173 | #_cleardomainconf key | |
2174 | _cleardomainconf() { | |
8a29fbc8 | 2175 | _clear_conf "$DOMAIN_CONF" "$1" |
4c3b3608 | 2176 | } |
2177 | ||
61623d22 | 2178 | #_readdomainconf key |
2179 | _readdomainconf() { | |
8a29fbc8 | 2180 | _read_conf "$DOMAIN_CONF" "$1" |
61623d22 | 2181 | } |
2182 | ||
aec66362 | 2183 | #key value base64encode |
2184 | _savedeployconf() { | |
2185 | _savedomainconf "SAVED_$1" "$2" "$3" | |
2186 | #remove later | |
dc5eda7e | 2187 | _cleardomainconf "$1" |
aec66362 | 2188 | } |
2189 | ||
2190 | #key | |
2191 | _getdeployconf() { | |
2192 | _rac_key="$1" | |
2e3ddd3a | 2193 | _rac_value="$(eval echo \$"$_rac_key")" |
2194 | if [ "$_rac_value" ]; then | |
2195 | if _startswith "$_rac_value" '"' && _endswith "$_rac_value" '"'; then | |
2196 | _debug2 "trim quotation marks" | |
465ece5d | 2197 | eval "export $_rac_key=$_rac_value" |
2e3ddd3a | 2198 | fi |
aec66362 | 2199 | return 0 # do nothing |
2200 | fi | |
2201 | _saved=$(_readdomainconf "SAVED_$_rac_key") | |
22f9a3b4 | 2202 | eval "export $_rac_key=\"$_saved\"" |
aec66362 | 2203 | } |
2204 | ||
7690f73e | 2205 | #_saveaccountconf key value base64encode |
4c3b3608 | 2206 | _saveaccountconf() { |
7690f73e | 2207 | _save_conf "$ACCOUNT_CONF_PATH" "$@" |
4c3b3608 | 2208 | } |
2209 | ||
7690f73e | 2210 | #key value base64encode |
fcdf41ba | 2211 | _saveaccountconf_mutable() { |
7690f73e | 2212 | _save_conf "$ACCOUNT_CONF_PATH" "SAVED_$1" "$2" "$3" |
fcdf41ba | 2213 | #remove later |
2214 | _clearaccountconf "$1" | |
2215 | } | |
2216 | ||
2217 | #key | |
2218 | _readaccountconf() { | |
2219 | _read_conf "$ACCOUNT_CONF_PATH" "$1" | |
2220 | } | |
2221 | ||
2222 | #key | |
2223 | _readaccountconf_mutable() { | |
2224 | _rac_key="$1" | |
2225 | _readaccountconf "SAVED_$_rac_key" | |
2226 | } | |
2227 | ||
fac1e367 | 2228 | #_clearaccountconf key |
2229 | _clearaccountconf() { | |
8a29fbc8 | 2230 | _clear_conf "$ACCOUNT_CONF_PATH" "$1" |
2231 | } | |
2232 | ||
2233 | #_savecaconf key value | |
2234 | _savecaconf() { | |
2235 | _save_conf "$CA_CONF" "$1" "$2" | |
2236 | } | |
2237 | ||
2238 | #_readcaconf key | |
2239 | _readcaconf() { | |
2240 | _read_conf "$CA_CONF" "$1" | |
2241 | } | |
2242 | ||
2243 | #_clearaccountconf key | |
2244 | _clearcaconf() { | |
2245 | _clear_conf "$CA_CONF" "$1" | |
fac1e367 | 2246 | } |
2247 | ||
0463b5d6 | 2248 | # content localaddress |
4c3b3608 | 2249 | _startserver() { |
2250 | content="$1" | |
0463b5d6 | 2251 | ncaddr="$2" |
e7f7e96d | 2252 | _debug "content" "$content" |
0463b5d6 | 2253 | _debug "ncaddr" "$ncaddr" |
2254 | ||
6fc1447f | 2255 | _debug "startserver: $$" |
4c2a3841 | 2256 | |
39c8f79f | 2257 | _debug Le_HTTPPort "$Le_HTTPPort" |
6ae0f7f5 | 2258 | _debug Le_Listen_V4 "$Le_Listen_V4" |
2259 | _debug Le_Listen_V6 "$Le_Listen_V6" | |
4c2a3841 | 2260 | |
3794b5cb | 2261 | _NC="socat" |
4c2a3841 | 2262 | if [ "$Le_Listen_V4" ]; then |
6ae0f7f5 | 2263 | _NC="$_NC -4" |
4c2a3841 | 2264 | elif [ "$Le_Listen_V6" ]; then |
6ae0f7f5 | 2265 | _NC="$_NC -6" |
2266 | fi | |
4c2a3841 | 2267 | |
9ad7ac63 | 2268 | if [ "$DEBUG" ] && [ "$DEBUG" -gt "1" ]; then |
5c568d69 | 2269 | _NC="$_NC -d -d -v" |
2270 | fi | |
2271 | ||
9134b6ea HC |
2272 | SOCAT_OPTIONS=TCP-LISTEN:$Le_HTTPPort,crlf,reuseaddr,fork |
2273 | ||
2274 | #Adding bind to local-address | |
d84665cb | 2275 | if [ "$ncaddr" ]; then |
9ad7ac63 | 2276 | SOCAT_OPTIONS="$SOCAT_OPTIONS,bind=${ncaddr}" |
9134b6ea HC |
2277 | fi |
2278 | ||
e7f7e96d | 2279 | _content_len="$(printf "%s" "$content" | wc -c)" |
2280 | _debug _content_len "$_content_len" | |
5c568d69 | 2281 | _debug "_NC" "$_NC $SOCAT_OPTIONS" |
e7f7e96d | 2282 | $_NC $SOCAT_OPTIONS SYSTEM:"sleep 1; \ |
2283 | echo 'HTTP/1.0 200 OK'; \ | |
2284 | echo 'Content-Length\: $_content_len'; \ | |
2285 | echo ''; \ | |
2ffd8637 | 2286 | printf -- '$content';" & |
3794b5cb | 2287 | serverproc="$!" |
4c3b3608 | 2288 | } |
2289 | ||
4c2a3841 | 2290 | _stopserver() { |
4c3b3608 | 2291 | pid="$1" |
6fc1447f | 2292 | _debug "pid" "$pid" |
4c2a3841 | 2293 | if [ -z "$pid" ]; then |
6fc1447f | 2294 | return |
2295 | fi | |
e22bcf7c | 2296 | |
3794b5cb | 2297 | kill $pid |
2298 | ||
4c3b3608 | 2299 | } |
2300 | ||
fdcb6b72 | 2301 | # sleep sec |
2302 | _sleep() { | |
2303 | _sleep_sec="$1" | |
4c2a3841 | 2304 | if [ "$__INTERACTIVE" ]; then |
fdcb6b72 | 2305 | _sleep_c="$_sleep_sec" |
4c2a3841 | 2306 | while [ "$_sleep_c" -ge "0" ]; do |
c583d6bb | 2307 | printf "\r \r" |
fdcb6b72 | 2308 | __green "$_sleep_c" |
79a267ab | 2309 | _sleep_c="$(_math "$_sleep_c" - 1)" |
fdcb6b72 | 2310 | sleep 1 |
2311 | done | |
c583d6bb | 2312 | printf "\r" |
fdcb6b72 | 2313 | else |
2314 | sleep "$_sleep_sec" | |
2315 | fi | |
2316 | } | |
e22bcf7c | 2317 | |
08681f4a | 2318 | # _starttlsserver san_a san_b port content _ncaddr acmeValidationv1 |
e22bcf7c | 2319 | _starttlsserver() { |
2320 | _info "Starting tls server." | |
2321 | san_a="$1" | |
2322 | san_b="$2" | |
2323 | port="$3" | |
2324 | content="$4" | |
6ae0f7f5 | 2325 | opaddr="$5" |
08681f4a | 2326 | acmeValidationv1="$6" |
4c2a3841 | 2327 | |
e22bcf7c | 2328 | _debug san_a "$san_a" |
2329 | _debug san_b "$san_b" | |
2330 | _debug port "$port" | |
08681f4a | 2331 | _debug acmeValidationv1 "$acmeValidationv1" |
4c2a3841 | 2332 | |
e22bcf7c | 2333 | #create key TLS_KEY |
4c2a3841 | 2334 | if ! _createkey "2048" "$TLS_KEY"; then |
e22bcf7c | 2335 | _err "Create tls validation key error." |
2336 | return 1 | |
2337 | fi | |
4c2a3841 | 2338 | |
e22bcf7c | 2339 | #create csr |
2340 | alt="$san_a" | |
4c2a3841 | 2341 | if [ "$san_b" ]; then |
e22bcf7c | 2342 | alt="$alt,$san_b" |
2343 | fi | |
08681f4a | 2344 | if ! _createcsr "tls.acme.sh" "$alt" "$TLS_KEY" "$TLS_CSR" "$TLS_CONF" "$acmeValidationv1"; then |
e22bcf7c | 2345 | _err "Create tls validation csr error." |
2346 | return 1 | |
2347 | fi | |
4c2a3841 | 2348 | |
e22bcf7c | 2349 | #self signed |
4c2a3841 | 2350 | if ! _signcsr "$TLS_KEY" "$TLS_CSR" "$TLS_CONF" "$TLS_CERT"; then |
e22bcf7c | 2351 | _err "Create tls validation cert error." |
2352 | return 1 | |
2353 | fi | |
4c2a3841 | 2354 | |
5f6e3da7 | 2355 | __S_OPENSSL="${ACME_OPENSSL_BIN:-openssl} s_server -www -cert $TLS_CERT -key $TLS_KEY " |
2356 | if [ "$opaddr" ]; then | |
2357 | __S_OPENSSL="$__S_OPENSSL -accept $opaddr:$port" | |
2358 | else | |
2359 | __S_OPENSSL="$__S_OPENSSL -accept $port" | |
2360 | fi | |
6ae0f7f5 | 2361 | |
2362 | _debug Le_Listen_V4 "$Le_Listen_V4" | |
2363 | _debug Le_Listen_V6 "$Le_Listen_V6" | |
4c2a3841 | 2364 | if [ "$Le_Listen_V4" ]; then |
6ae0f7f5 | 2365 | __S_OPENSSL="$__S_OPENSSL -4" |
4c2a3841 | 2366 | elif [ "$Le_Listen_V6" ]; then |
6ae0f7f5 | 2367 | __S_OPENSSL="$__S_OPENSSL -6" |
2368 | fi | |
4c2a3841 | 2369 | |
08681f4a | 2370 | if [ "$acmeValidationv1" ]; then |
2371 | __S_OPENSSL="$__S_OPENSSL -alpn acme-tls/1" | |
2372 | fi | |
2373 | ||
6ae0f7f5 | 2374 | _debug "$__S_OPENSSL" |
5f6e3da7 | 2375 | if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ]; then |
2376 | $__S_OPENSSL -tlsextdebug & | |
2377 | else | |
2378 | $__S_OPENSSL >/dev/null 2>&1 & | |
2379 | fi | |
331c4bb6 | 2380 | |
e22bcf7c | 2381 | serverproc="$!" |
5dbf664a | 2382 | sleep 1 |
d5ec5f80 | 2383 | _debug serverproc "$serverproc" |
e22bcf7c | 2384 | } |
2385 | ||
18e46962 | 2386 | #file |
2387 | _readlink() { | |
2388 | _rf="$1" | |
2389 | if ! readlink -f "$_rf" 2>/dev/null; then | |
6c4cc357 | 2390 | if _startswith "$_rf" "/"; then |
2391 | echo "$_rf" | |
7da50703 | 2392 | return 0 |
2393 | fi | |
6c4cc357 | 2394 | echo "$(pwd)/$_rf" | _conapath |
18e46962 | 2395 | fi |
2396 | } | |
2397 | ||
6c4cc357 | 2398 | _conapath() { |
2399 | sed "s#/\./#/#g" | |
2400 | } | |
2401 | ||
5ea6e9c9 | 2402 | __initHome() { |
4c2a3841 | 2403 | if [ -z "$_SCRIPT_HOME" ]; then |
2404 | if _exists readlink && _exists dirname; then | |
66990cf8 | 2405 | _debug "Lets find script dir." |
f3e4cea3 | 2406 | _debug "_SCRIPT_" "$_SCRIPT_" |
18e46962 | 2407 | _script="$(_readlink "$_SCRIPT_")" |
f3e4cea3 | 2408 | _debug "_script" "$_script" |
2409 | _script_home="$(dirname "$_script")" | |
2410 | _debug "_script_home" "$_script_home" | |
4c2a3841 | 2411 | if [ -d "$_script_home" ]; then |
f3e4cea3 | 2412 | _SCRIPT_HOME="$_script_home" |
2413 | else | |
2414 | _err "It seems the script home is not correct:$_script_home" | |
2415 | fi | |
2416 | fi | |
2417 | fi | |
2418 | ||
219e9115 | 2419 | # if [ -z "$LE_WORKING_DIR" ]; then |
2420 | # if [ -f "$DEFAULT_INSTALL_HOME/account.conf" ]; then | |
2421 | # _debug "It seems that $PROJECT_NAME is already installed in $DEFAULT_INSTALL_HOME" | |
2422 | # LE_WORKING_DIR="$DEFAULT_INSTALL_HOME" | |
2423 | # else | |
2424 | # LE_WORKING_DIR="$_SCRIPT_HOME" | |
2425 | # fi | |
2426 | # fi | |
4c2a3841 | 2427 | |
2428 | if [ -z "$LE_WORKING_DIR" ]; then | |
f3e4cea3 | 2429 | _debug "Using default home:$DEFAULT_INSTALL_HOME" |
2430 | LE_WORKING_DIR="$DEFAULT_INSTALL_HOME" | |
2431 | fi | |
7da50703 | 2432 | export LE_WORKING_DIR |
f3e4cea3 | 2433 | |
f5b546b3 | 2434 | if [ -z "$LE_CONFIG_HOME" ]; then |
2435 | LE_CONFIG_HOME="$LE_WORKING_DIR" | |
27dbe77f | 2436 | fi |
f5b546b3 | 2437 | _debug "Using config home:$LE_CONFIG_HOME" |
2438 | export LE_CONFIG_HOME | |
27dbe77f | 2439 | |
f5b546b3 | 2440 | _DEFAULT_ACCOUNT_CONF_PATH="$LE_CONFIG_HOME/account.conf" |
d53289d7 | 2441 | |
4c2a3841 | 2442 | if [ -z "$ACCOUNT_CONF_PATH" ]; then |
2443 | if [ -f "$_DEFAULT_ACCOUNT_CONF_PATH" ]; then | |
8663fb7e | 2444 | . "$_DEFAULT_ACCOUNT_CONF_PATH" |
635695ec | 2445 | fi |
d53289d7 | 2446 | fi |
4c2a3841 | 2447 | |
2448 | if [ -z "$ACCOUNT_CONF_PATH" ]; then | |
d53289d7 | 2449 | ACCOUNT_CONF_PATH="$_DEFAULT_ACCOUNT_CONF_PATH" |
4c3b3608 | 2450 | fi |
f8f53a6b | 2451 | _debug3 ACCOUNT_CONF_PATH "$ACCOUNT_CONF_PATH" |
f5b546b3 | 2452 | DEFAULT_LOG_FILE="$LE_CONFIG_HOME/$PROJECT_NAME.log" |
4c2a3841 | 2453 | |
f5b546b3 | 2454 | DEFAULT_CA_HOME="$LE_CONFIG_HOME/ca" |
4c2a3841 | 2455 | |
2456 | if [ -z "$LE_TEMP_DIR" ]; then | |
f5b546b3 | 2457 | LE_TEMP_DIR="$LE_CONFIG_HOME/tmp" |
610e0f21 | 2458 | fi |
5ea6e9c9 | 2459 | } |
2460 | ||
48d9a8c1 | 2461 | #server |
2462 | _initAPI() { | |
2463 | _api_server="${1:-$ACME_DIRECTORY}" | |
2464 | _debug "_init api for server: $_api_server" | |
4cee14f3 | 2465 | |
cae50e16 | 2466 | if [ -z "$ACME_NEW_ACCOUNT" ]; then |
48d9a8c1 | 2467 | response=$(_get "$_api_server") |
2468 | if [ "$?" != "0" ]; then | |
2469 | _debug2 "response" "$response" | |
2470 | _err "Can not init api." | |
2471 | return 1 | |
2472 | fi | |
2473 | _debug2 "response" "$response" | |
2474 | ||
2475 | ACME_KEY_CHANGE=$(echo "$response" | _egrep_o 'key-change" *: *"[^"]*"' | cut -d '"' -f 3) | |
c1151b0d | 2476 | if [ -z "$ACME_KEY_CHANGE" ]; then |
2477 | ACME_KEY_CHANGE=$(echo "$response" | _egrep_o 'keyChange" *: *"[^"]*"' | cut -d '"' -f 3) | |
2478 | fi | |
48d9a8c1 | 2479 | export ACME_KEY_CHANGE |
2480 | ||
2481 | ACME_NEW_AUTHZ=$(echo "$response" | _egrep_o 'new-authz" *: *"[^"]*"' | cut -d '"' -f 3) | |
c1151b0d | 2482 | if [ -z "$ACME_NEW_AUTHZ" ]; then |
2483 | ACME_NEW_AUTHZ=$(echo "$response" | _egrep_o 'newAuthz" *: *"[^"]*"' | cut -d '"' -f 3) | |
2484 | fi | |
48d9a8c1 | 2485 | export ACME_NEW_AUTHZ |
2486 | ||
cae50e16 | 2487 | ACME_NEW_ORDER=$(echo "$response" | _egrep_o 'new-cert" *: *"[^"]*"' | cut -d '"' -f 3) |
a71eba07 | 2488 | ACME_NEW_ORDER_RES="new-cert" |
cae50e16 | 2489 | if [ -z "$ACME_NEW_ORDER" ]; then |
2490 | ACME_NEW_ORDER=$(echo "$response" | _egrep_o 'new-order" *: *"[^"]*"' | cut -d '"' -f 3) | |
a71eba07 | 2491 | ACME_NEW_ORDER_RES="new-order" |
c1151b0d | 2492 | if [ -z "$ACME_NEW_ORDER" ]; then |
2493 | ACME_NEW_ORDER=$(echo "$response" | _egrep_o 'newOrder" *: *"[^"]*"' | cut -d '"' -f 3) | |
2494 | fi | |
cae50e16 | 2495 | fi |
2496 | export ACME_NEW_ORDER | |
a71eba07 | 2497 | export ACME_NEW_ORDER_RES |
48d9a8c1 | 2498 | |
cae50e16 | 2499 | ACME_NEW_ACCOUNT=$(echo "$response" | _egrep_o 'new-reg" *: *"[^"]*"' | cut -d '"' -f 3) |
a71eba07 | 2500 | ACME_NEW_ACCOUNT_RES="new-reg" |
cae50e16 | 2501 | if [ -z "$ACME_NEW_ACCOUNT" ]; then |
2502 | ACME_NEW_ACCOUNT=$(echo "$response" | _egrep_o 'new-account" *: *"[^"]*"' | cut -d '"' -f 3) | |
a71eba07 | 2503 | ACME_NEW_ACCOUNT_RES="new-account" |
c1151b0d | 2504 | if [ -z "$ACME_NEW_ACCOUNT" ]; then |
2505 | ACME_NEW_ACCOUNT=$(echo "$response" | _egrep_o 'newAccount" *: *"[^"]*"' | cut -d '"' -f 3) | |
2506 | if [ "$ACME_NEW_ACCOUNT" ]; then | |
2507 | export ACME_VERSION=2 | |
2508 | fi | |
2509 | fi | |
cae50e16 | 2510 | fi |
2511 | export ACME_NEW_ACCOUNT | |
a71eba07 | 2512 | export ACME_NEW_ACCOUNT_RES |
48d9a8c1 | 2513 | |
2514 | ACME_REVOKE_CERT=$(echo "$response" | _egrep_o 'revoke-cert" *: *"[^"]*"' | cut -d '"' -f 3) | |
c1151b0d | 2515 | if [ -z "$ACME_REVOKE_CERT" ]; then |
2516 | ACME_REVOKE_CERT=$(echo "$response" | _egrep_o 'revokeCert" *: *"[^"]*"' | cut -d '"' -f 3) | |
2517 | fi | |
48d9a8c1 | 2518 | export ACME_REVOKE_CERT |
2519 | ||
cae50e16 | 2520 | ACME_NEW_NONCE=$(echo "$response" | _egrep_o 'new-nonce" *: *"[^"]*"' | cut -d '"' -f 3) |
c1151b0d | 2521 | if [ -z "$ACME_NEW_NONCE" ]; then |
2522 | ACME_NEW_NONCE=$(echo "$response" | _egrep_o 'newNonce" *: *"[^"]*"' | cut -d '"' -f 3) | |
2523 | fi | |
cae50e16 | 2524 | export ACME_NEW_NONCE |
4249e13e | 2525 | |
f87890cb | 2526 | ACME_AGREEMENT=$(echo "$response" | _egrep_o 'terms-of-service" *: *"[^"]*"' | cut -d '"' -f 3) |
c1151b0d | 2527 | if [ -z "$ACME_AGREEMENT" ]; then |
2528 | ACME_AGREEMENT=$(echo "$response" | _egrep_o 'termsOfService" *: *"[^"]*"' | cut -d '"' -f 3) | |
2529 | fi | |
f87890cb | 2530 | export ACME_AGREEMENT |
cae50e16 | 2531 | |
f87890cb | 2532 | _debug "ACME_KEY_CHANGE" "$ACME_KEY_CHANGE" |
2533 | _debug "ACME_NEW_AUTHZ" "$ACME_NEW_AUTHZ" | |
2534 | _debug "ACME_NEW_ORDER" "$ACME_NEW_ORDER" | |
2535 | _debug "ACME_NEW_ACCOUNT" "$ACME_NEW_ACCOUNT" | |
2536 | _debug "ACME_REVOKE_CERT" "$ACME_REVOKE_CERT" | |
2537 | _debug "ACME_AGREEMENT" "$ACME_AGREEMENT" | |
c1151b0d | 2538 | _debug "ACME_NEW_NONCE" "$ACME_NEW_NONCE" |
2539 | _debug "ACME_VERSION" "$ACME_VERSION" | |
48d9a8c1 | 2540 | |
f87890cb | 2541 | fi |
48d9a8c1 | 2542 | } |
2543 | ||
3281043e | 2544 | #[domain] [keylength or isEcc flag] |
5ea6e9c9 | 2545 | _initpath() { |
cd9fb3b6 | 2546 | domain="$1" |
2547 | _ilength="$2" | |
5ea6e9c9 | 2548 | |
2549 | __initHome | |
2550 | ||
4c2a3841 | 2551 | if [ -f "$ACCOUNT_CONF_PATH" ]; then |
8663fb7e | 2552 | . "$ACCOUNT_CONF_PATH" |
4c3b3608 | 2553 | fi |
2554 | ||
c6b68551 | 2555 | if [ "$ACME_IN_CRON" ]; then |
4c2a3841 | 2556 | if [ ! "$_USER_PATH_EXPORTED" ]; then |
281aa349 | 2557 | _USER_PATH_EXPORTED=1 |
2558 | export PATH="$USER_PATH:$PATH" | |
2559 | fi | |
2560 | fi | |
4c2a3841 | 2561 | |
2562 | if [ -z "$CA_HOME" ]; then | |
5c48e139 | 2563 | CA_HOME="$DEFAULT_CA_HOME" |
2564 | fi | |
281aa349 | 2565 | |
72f54ca6 | 2566 | if [ "$ACME_VERSION" = "2" ]; then |
2567 | DEFAULT_CA="$LETSENCRYPT_CA_V2" | |
2568 | DEFAULT_STAGING_CA="$LETSENCRYPT_STAGING_CA_V2" | |
2569 | fi | |
2570 | ||
48d9a8c1 | 2571 | if [ -z "$ACME_DIRECTORY" ]; then |
4c2a3841 | 2572 | if [ -z "$STAGE" ]; then |
48d9a8c1 | 2573 | ACME_DIRECTORY="$DEFAULT_CA" |
4c3b3608 | 2574 | else |
c1151b0d | 2575 | ACME_DIRECTORY="$DEFAULT_STAGING_CA" |
48d9a8c1 | 2576 | _info "Using stage ACME_DIRECTORY: $ACME_DIRECTORY" |
4c2a3841 | 2577 | fi |
4c3b3608 | 2578 | fi |
4c2a3841 | 2579 | |
66444663 | 2580 | _debug ACME_DIRECTORY "$ACME_DIRECTORY" |
98394f99 | 2581 | _ACME_SERVER_HOST="$(echo "$ACME_DIRECTORY" | cut -d : -f 2 | tr -s / | cut -d / -f 2)" |
48d9a8c1 | 2582 | _debug2 "_ACME_SERVER_HOST" "$_ACME_SERVER_HOST" |
2583 | ||
2584 | CA_DIR="$CA_HOME/$_ACME_SERVER_HOST" | |
4c2a3841 | 2585 | |
5c48e139 | 2586 | _DEFAULT_CA_CONF="$CA_DIR/ca.conf" |
4c2a3841 | 2587 | |
2588 | if [ -z "$CA_CONF" ]; then | |
5c48e139 | 2589 | CA_CONF="$_DEFAULT_CA_CONF" |
2590 | fi | |
c4236e58 | 2591 | _debug3 CA_CONF "$CA_CONF" |
4c2a3841 | 2592 | |
2593 | if [ -f "$CA_CONF" ]; then | |
5c48e139 | 2594 | . "$CA_CONF" |
2595 | fi | |
2596 | ||
4c2a3841 | 2597 | if [ -z "$ACME_DIR" ]; then |
4c3b3608 | 2598 | ACME_DIR="/home/.acme" |
2599 | fi | |
4c2a3841 | 2600 | |
2601 | if [ -z "$APACHE_CONF_BACKUP_DIR" ]; then | |
f5b546b3 | 2602 | APACHE_CONF_BACKUP_DIR="$LE_CONFIG_HOME" |
4c3b3608 | 2603 | fi |
4c2a3841 | 2604 | |
2605 | if [ -z "$USER_AGENT" ]; then | |
bbbdcb09 | 2606 | USER_AGENT="$DEFAULT_USER_AGENT" |
2607 | fi | |
4c2a3841 | 2608 | |
2609 | if [ -z "$HTTP_HEADER" ]; then | |
f5b546b3 | 2610 | HTTP_HEADER="$LE_CONFIG_HOME/http.header" |
933c169d | 2611 | fi |
b2817897 | 2612 | |
5c48e139 | 2613 | _OLD_ACCOUNT_KEY="$LE_WORKING_DIR/account.key" |
2614 | _OLD_ACCOUNT_JSON="$LE_WORKING_DIR/account.json" | |
4c2a3841 | 2615 | |
5c48e139 | 2616 | _DEFAULT_ACCOUNT_KEY_PATH="$CA_DIR/account.key" |
2617 | _DEFAULT_ACCOUNT_JSON_PATH="$CA_DIR/account.json" | |
4c2a3841 | 2618 | if [ -z "$ACCOUNT_KEY_PATH" ]; then |
b2817897 | 2619 | ACCOUNT_KEY_PATH="$_DEFAULT_ACCOUNT_KEY_PATH" |
4c3b3608 | 2620 | fi |
4c2a3841 | 2621 | |
2622 | if [ -z "$ACCOUNT_JSON_PATH" ]; then | |
5c48e139 | 2623 | ACCOUNT_JSON_PATH="$_DEFAULT_ACCOUNT_JSON_PATH" |
2624 | fi | |
4c2a3841 | 2625 | |
f5b546b3 | 2626 | _DEFAULT_CERT_HOME="$LE_CONFIG_HOME" |
4c2a3841 | 2627 | if [ -z "$CERT_HOME" ]; then |
a79b26af RD |
2628 | CERT_HOME="$_DEFAULT_CERT_HOME" |
2629 | fi | |
2630 | ||
77f1ea40 | 2631 | if [ -z "$ACME_OPENSSL_BIN" ] || [ ! -f "$ACME_OPENSSL_BIN" ] || [ ! -x "$ACME_OPENSSL_BIN" ]; then |
851fedf7 | 2632 | ACME_OPENSSL_BIN="$DEFAULT_OPENSSL_BIN" |
a746139c | 2633 | fi |
2634 | ||
cd9fb3b6 | 2635 | if [ -z "$domain" ]; then |
4c3b3608 | 2636 | return 0 |
2637 | fi | |
4c2a3841 | 2638 | |
4c2a3841 | 2639 | if [ -z "$DOMAIN_PATH" ]; then |
43822d37 | 2640 | domainhome="$CERT_HOME/$domain" |
2641 | domainhomeecc="$CERT_HOME/$domain$ECC_SUFFIX" | |
4c2a3841 | 2642 | |
4c3b3608 | 2643 | DOMAIN_PATH="$domainhome" |
4c2a3841 | 2644 | |
2645 | if _isEccKey "$_ilength"; then | |
43822d37 | 2646 | DOMAIN_PATH="$domainhomeecc" |
2647 | else | |
4c2a3841 | 2648 | if [ ! -d "$domainhome" ] && [ -d "$domainhomeecc" ]; then |
6d4e903b | 2649 | _info "The domain '$domain' seems to have a ECC cert already, please add '$(__red "--ecc")' parameter if you want to use that cert." |
43822d37 | 2650 | fi |
2651 | fi | |
2652 | _debug DOMAIN_PATH "$DOMAIN_PATH" | |
4c3b3608 | 2653 | fi |
4c2a3841 | 2654 | |
fd72cced | 2655 | if [ -z "$DOMAIN_BACKUP_PATH" ]; then |
d88f8e86 | 2656 | DOMAIN_BACKUP_PATH="$DOMAIN_PATH/backup" |
fd72cced | 2657 | fi |
2658 | ||
4c2a3841 | 2659 | if [ -z "$DOMAIN_CONF" ]; then |
43822d37 | 2660 | DOMAIN_CONF="$DOMAIN_PATH/$domain.conf" |
4c3b3608 | 2661 | fi |
4c2a3841 | 2662 | |
2663 | if [ -z "$DOMAIN_SSL_CONF" ]; then | |
0c9546cc | 2664 | DOMAIN_SSL_CONF="$DOMAIN_PATH/$domain.csr.conf" |
4c3b3608 | 2665 | fi |
4c2a3841 | 2666 | |
2667 | if [ -z "$CSR_PATH" ]; then | |
43822d37 | 2668 | CSR_PATH="$DOMAIN_PATH/$domain.csr" |
4c3b3608 | 2669 | fi |
4c2a3841 | 2670 | if [ -z "$CERT_KEY_PATH" ]; then |
43822d37 | 2671 | CERT_KEY_PATH="$DOMAIN_PATH/$domain.key" |
4c3b3608 | 2672 | fi |
4c2a3841 | 2673 | if [ -z "$CERT_PATH" ]; then |
43822d37 | 2674 | CERT_PATH="$DOMAIN_PATH/$domain.cer" |
4c3b3608 | 2675 | fi |
4c2a3841 | 2676 | if [ -z "$CA_CERT_PATH" ]; then |
43822d37 | 2677 | CA_CERT_PATH="$DOMAIN_PATH/ca.cer" |
4c3b3608 | 2678 | fi |
4c2a3841 | 2679 | if [ -z "$CERT_FULLCHAIN_PATH" ]; then |
43822d37 | 2680 | CERT_FULLCHAIN_PATH="$DOMAIN_PATH/fullchain.cer" |
caf1fc10 | 2681 | fi |
4c2a3841 | 2682 | if [ -z "$CERT_PFX_PATH" ]; then |
43822d37 | 2683 | CERT_PFX_PATH="$DOMAIN_PATH/$domain.pfx" |
ac2d5123 | 2684 | fi |
4410226d | 2685 | if [ -z "$CERT_PKCS8_PATH" ]; then |
2686 | CERT_PKCS8_PATH="$DOMAIN_PATH/$domain.pkcs8" | |
2687 | fi | |
4c2a3841 | 2688 | |
2689 | if [ -z "$TLS_CONF" ]; then | |
f94433e5 | 2690 | TLS_CONF="$DOMAIN_PATH/tls.validation.conf" |
e22bcf7c | 2691 | fi |
4c2a3841 | 2692 | if [ -z "$TLS_CERT" ]; then |
f94433e5 | 2693 | TLS_CERT="$DOMAIN_PATH/tls.validation.cert" |
e22bcf7c | 2694 | fi |
4c2a3841 | 2695 | if [ -z "$TLS_KEY" ]; then |
f94433e5 | 2696 | TLS_KEY="$DOMAIN_PATH/tls.validation.key" |
e22bcf7c | 2697 | fi |
4c2a3841 | 2698 | if [ -z "$TLS_CSR" ]; then |
f94433e5 | 2699 | TLS_CSR="$DOMAIN_PATH/tls.validation.csr" |
e22bcf7c | 2700 | fi |
4c2a3841 | 2701 | |
4c3b3608 | 2702 | } |
2703 | ||
610e0f21 | 2704 | _exec() { |
4c2a3841 | 2705 | if [ -z "$_EXEC_TEMP_ERR" ]; then |
610e0f21 | 2706 | _EXEC_TEMP_ERR="$(_mktemp)" |
2707 | fi | |
2708 | ||
4c2a3841 | 2709 | if [ "$_EXEC_TEMP_ERR" ]; then |
3e5b1024 | 2710 | eval "$@ 2>>$_EXEC_TEMP_ERR" |
610e0f21 | 2711 | else |
3e5b1024 | 2712 | eval "$@" |
610e0f21 | 2713 | fi |
2714 | } | |
2715 | ||
2716 | _exec_err() { | |
3e5b1024 | 2717 | [ "$_EXEC_TEMP_ERR" ] && _err "$(cat "$_EXEC_TEMP_ERR")" && echo "" >"$_EXEC_TEMP_ERR" |
610e0f21 | 2718 | } |
4c3b3608 | 2719 | |
2720 | _apachePath() { | |
c3dd3ef0 | 2721 | _APACHECTL="apachectl" |
4c2a3841 | 2722 | if ! _exists apachectl; then |
2723 | if _exists apache2ctl; then | |
2724 | _APACHECTL="apache2ctl" | |
e4a19585 | 2725 | else |
bc96082f | 2726 | _err "'apachectl not found. It seems that apache is not installed, or you are not root user.'" |
e4a19585 | 2727 | _err "Please use webroot mode to try again." |
2728 | return 1 | |
2729 | fi | |
80a0a7b5 | 2730 | fi |
4c2a3841 | 2731 | |
2732 | if ! _exec $_APACHECTL -V >/dev/null; then | |
610e0f21 | 2733 | _exec_err |
2734 | return 1 | |
2735 | fi | |
4c2a3841 | 2736 | |
2737 | if [ "$APACHE_HTTPD_CONF" ]; then | |
5be1449d | 2738 | _saveaccountconf APACHE_HTTPD_CONF "$APACHE_HTTPD_CONF" |
2739 | httpdconf="$APACHE_HTTPD_CONF" | |
79a267ab | 2740 | httpdconfname="$(basename "$httpdconfname")" |
d62ee940 | 2741 | else |
4c2a3841 | 2742 | httpdconfname="$($_APACHECTL -V | grep SERVER_CONFIG_FILE= | cut -d = -f 2 | tr -d '"')" |
5be1449d | 2743 | _debug httpdconfname "$httpdconfname" |
4c2a3841 | 2744 | |
2745 | if [ -z "$httpdconfname" ]; then | |
5be1449d | 2746 | _err "Can not read apache config file." |
2747 | return 1 | |
2748 | fi | |
4c2a3841 | 2749 | |
2750 | if _startswith "$httpdconfname" '/'; then | |
5be1449d | 2751 | httpdconf="$httpdconfname" |
79a267ab | 2752 | httpdconfname="$(basename "$httpdconfname")" |
5be1449d | 2753 | else |
4c2a3841 | 2754 | httpdroot="$($_APACHECTL -V | grep HTTPD_ROOT= | cut -d = -f 2 | tr -d '"')" |
5be1449d | 2755 | _debug httpdroot "$httpdroot" |
2756 | httpdconf="$httpdroot/$httpdconfname" | |
79a267ab | 2757 | httpdconfname="$(basename "$httpdconfname")" |
5be1449d | 2758 | fi |
d62ee940 | 2759 | fi |
78768e98 | 2760 | _debug httpdconf "$httpdconf" |
8f63baf7 | 2761 | _debug httpdconfname "$httpdconfname" |
4c2a3841 | 2762 | if [ ! -f "$httpdconf" ]; then |
78768e98 | 2763 | _err "Apache Config file not found" "$httpdconf" |
4c3b3608 | 2764 | return 1 |
2765 | fi | |
2766 | return 0 | |
2767 | } | |
2768 | ||
2769 | _restoreApache() { | |
4c2a3841 | 2770 | if [ -z "$usingApache" ]; then |
4c3b3608 | 2771 | return 0 |
2772 | fi | |
2773 | _initpath | |
4c2a3841 | 2774 | if ! _apachePath; then |
4c3b3608 | 2775 | return 1 |
2776 | fi | |
4c2a3841 | 2777 | |
2778 | if [ ! -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname" ]; then | |
4c3b3608 | 2779 | _debug "No config file to restore." |
2780 | return 0 | |
2781 | fi | |
4c2a3841 | 2782 | |
2783 | cat "$APACHE_CONF_BACKUP_DIR/$httpdconfname" >"$httpdconf" | |
5ef501c5 | 2784 | _debug "Restored: $httpdconf." |
4c2a3841 | 2785 | if ! _exec $_APACHECTL -t; then |
610e0f21 | 2786 | _exec_err |
4c3b3608 | 2787 | _err "Sorry, restore apache config error, please contact me." |
4c2a3841 | 2788 | return 1 |
4c3b3608 | 2789 | fi |
5ef501c5 | 2790 | _debug "Restored successfully." |
4c3b3608 | 2791 | rm -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname" |
4c2a3841 | 2792 | return 0 |
4c3b3608 | 2793 | } |
2794 | ||
2795 | _setApache() { | |
2796 | _initpath | |
4c2a3841 | 2797 | if ! _apachePath; then |
4c3b3608 | 2798 | return 1 |
2799 | fi | |
2800 | ||
5fc5016d | 2801 | #test the conf first |
869578ce | 2802 | _info "Checking if there is an error in the apache config file before starting." |
4c2a3841 | 2803 | |
44edb2bd | 2804 | if ! _exec "$_APACHECTL" -t >/dev/null; then |
610e0f21 | 2805 | _exec_err |
2806 | _err "The apache config file has error, please fix it first, then try again." | |
869578ce | 2807 | _err "Don't worry, there is nothing changed to your system." |
4c2a3841 | 2808 | return 1 |
5fc5016d | 2809 | else |
2810 | _info "OK" | |
2811 | fi | |
4c2a3841 | 2812 | |
4c3b3608 | 2813 | #backup the conf |
5778811a | 2814 | _debug "Backup apache config file" "$httpdconf" |
4c2a3841 | 2815 | if ! cp "$httpdconf" "$APACHE_CONF_BACKUP_DIR/"; then |
869578ce | 2816 | _err "Can not backup apache config file, so abort. Don't worry, the apache config is not changed." |
61a48a5b | 2817 | _err "This might be a bug of $PROJECT_NAME , please report issue: $PROJECT" |
8f63baf7 | 2818 | return 1 |
2819 | fi | |
4c3b3608 | 2820 | _info "JFYI, Config file $httpdconf is backuped to $APACHE_CONF_BACKUP_DIR/$httpdconfname" |
2821 | _info "In case there is an error that can not be restored automatically, you may try restore it yourself." | |
329174b6 | 2822 | _info "The backup file will be deleted on success, just forget it." |
4c2a3841 | 2823 | |
4c3b3608 | 2824 | #add alias |
4c2a3841 | 2825 | |
2826 | apacheVer="$($_APACHECTL -V | grep "Server version:" | cut -d : -f 2 | cut -d " " -f 2 | cut -d '/' -f 2)" | |
b09d597c | 2827 | _debug "apacheVer" "$apacheVer" |
94787d53 | 2828 | apacheMajor="$(echo "$apacheVer" | cut -d . -f 1)" |
b09d597c | 2829 | apacheMinor="$(echo "$apacheVer" | cut -d . -f 2)" |
2830 | ||
94787d53 | 2831 | if [ "$apacheVer" ] && [ "$apacheMajor$apacheMinor" -ge "24" ]; then |
b09d597c | 2832 | echo " |
4c3b3608 | 2833 | Alias /.well-known/acme-challenge $ACME_DIR |
2834 | ||
2835 | <Directory $ACME_DIR > | |
2836 | Require all granted | |
b09d597c | 2837 | </Directory> |
4c2a3841 | 2838 | " >>"$httpdconf" |
b09d597c | 2839 | else |
2840 | echo " | |
2841 | Alias /.well-known/acme-challenge $ACME_DIR | |
2842 | ||
2843 | <Directory $ACME_DIR > | |
2844 | Order allow,deny | |
2845 | Allow from all | |
4c3b3608 | 2846 | </Directory> |
4c2a3841 | 2847 | " >>"$httpdconf" |
b09d597c | 2848 | fi |
2849 | ||
4c2a3841 | 2850 | _msg="$($_APACHECTL -t 2>&1)" |
2851 | if [ "$?" != "0" ]; then | |
5fc5016d | 2852 | _err "Sorry, apache config error" |
4c2a3841 | 2853 | if _restoreApache; then |
869578ce | 2854 | _err "The apache config file is restored." |
5fc5016d | 2855 | else |
869578ce | 2856 | _err "Sorry, The apache config file can not be restored, please report bug." |
5fc5016d | 2857 | fi |
4c2a3841 | 2858 | return 1 |
4c3b3608 | 2859 | fi |
4c2a3841 | 2860 | |
2861 | if [ ! -d "$ACME_DIR" ]; then | |
4c3b3608 | 2862 | mkdir -p "$ACME_DIR" |
2863 | chmod 755 "$ACME_DIR" | |
2864 | fi | |
4c2a3841 | 2865 | |
44edb2bd | 2866 | if ! _exec "$_APACHECTL" graceful; then |
4c2a3841 | 2867 | _exec_err |
610e0f21 | 2868 | _err "$_APACHECTL graceful error, please contact me." |
4c3b3608 | 2869 | _restoreApache |
4c2a3841 | 2870 | return 1 |
4c3b3608 | 2871 | fi |
2872 | usingApache="1" | |
2873 | return 0 | |
2874 | } | |
2875 | ||
9d725af6 | 2876 | #find the real nginx conf file |
2877 | #backup | |
2878 | #set the nginx conf | |
2879 | #returns the real nginx conf file | |
2880 | _setNginx() { | |
2881 | _d="$1" | |
2882 | _croot="$2" | |
2883 | _thumbpt="$3" | |
37f39c08 | 2884 | |
9d725af6 | 2885 | FOUND_REAL_NGINX_CONF="" |
9f90618a | 2886 | FOUND_REAL_NGINX_CONF_LN="" |
9d725af6 | 2887 | BACKUP_NGINX_CONF="" |
2888 | _debug _croot "$_croot" | |
2889 | _start_f="$(echo "$_croot" | cut -d : -f 2)" | |
2890 | _debug _start_f "$_start_f" | |
2891 | if [ -z "$_start_f" ]; then | |
2892 | _debug "find start conf from nginx command" | |
2893 | if [ -z "$NGINX_CONF" ]; then | |
37f39c08 | 2894 | if ! _exists "nginx"; then |
2895 | _err "nginx command is not found." | |
2896 | return 1 | |
2897 | fi | |
9d725af6 | 2898 | NGINX_CONF="$(nginx -V 2>&1 | _egrep_o "--conf-path=[^ ]* " | tr -d " ")" |
2899 | _debug NGINX_CONF "$NGINX_CONF" | |
2900 | NGINX_CONF="$(echo "$NGINX_CONF" | cut -d = -f 2)" | |
2901 | _debug NGINX_CONF "$NGINX_CONF" | |
1081d98b | 2902 | if [ -z "$NGINX_CONF" ]; then |
2903 | _err "Can not find nginx conf." | |
2904 | NGINX_CONF="" | |
2905 | return 1 | |
2906 | fi | |
9d725af6 | 2907 | if [ ! -f "$NGINX_CONF" ]; then |
2908 | _err "'$NGINX_CONF' doesn't exist." | |
2909 | NGINX_CONF="" | |
2910 | return 1 | |
2911 | fi | |
2912 | _debug "Found nginx conf file:$NGINX_CONF" | |
2913 | fi | |
2914 | _start_f="$NGINX_CONF" | |
2915 | fi | |
03f8d6e9 | 2916 | _debug "Start detect nginx conf for $_d from:$_start_f" |
9d725af6 | 2917 | if ! _checkConf "$_d" "$_start_f"; then |
5378d9ca | 2918 | _err "Can not find conf file for domain $d" |
9d725af6 | 2919 | return 1 |
2920 | fi | |
2921 | _info "Found conf file: $FOUND_REAL_NGINX_CONF" | |
2922 | ||
9f90618a | 2923 | _ln=$FOUND_REAL_NGINX_CONF_LN |
03f8d6e9 | 2924 | _debug "_ln" "$_ln" |
2925 | ||
2926 | _lnn=$(_math $_ln + 1) | |
2927 | _debug _lnn "$_lnn" | |
2928 | _start_tag="$(sed -n "$_lnn,${_lnn}p" "$FOUND_REAL_NGINX_CONF")" | |
2929 | _debug "_start_tag" "$_start_tag" | |
2930 | if [ "$_start_tag" = "$NGINX_START" ]; then | |
2931 | _info "The domain $_d is already configured, skip" | |
2932 | FOUND_REAL_NGINX_CONF="" | |
2933 | return 0 | |
2934 | fi | |
2935 | ||
9d725af6 | 2936 | mkdir -p "$DOMAIN_BACKUP_PATH" |
2937 | _backup_conf="$DOMAIN_BACKUP_PATH/$_d.nginx.conf" | |
2938 | _debug _backup_conf "$_backup_conf" | |
2939 | BACKUP_NGINX_CONF="$_backup_conf" | |
2940 | _info "Backup $FOUND_REAL_NGINX_CONF to $_backup_conf" | |
2941 | if ! cp "$FOUND_REAL_NGINX_CONF" "$_backup_conf"; then | |
2942 | _err "backup error." | |
2943 | FOUND_REAL_NGINX_CONF="" | |
2944 | return 1 | |
2945 | fi | |
2946 | ||
37f39c08 | 2947 | if ! _exists "nginx"; then |
2948 | _err "nginx command is not found." | |
2949 | return 1 | |
2950 | fi | |
9d725af6 | 2951 | _info "Check the nginx conf before setting up." |
2952 | if ! _exec "nginx -t" >/dev/null; then | |
2953 | _exec_err | |
2954 | return 1 | |
2955 | fi | |
2956 | ||
2957 | _info "OK, Set up nginx config file" | |
9d725af6 | 2958 | |
302c41ed | 2959 | if ! sed -n "1,${_ln}p" "$_backup_conf" >"$FOUND_REAL_NGINX_CONF"; then |
03f8d6e9 | 2960 | cat "$_backup_conf" >"$FOUND_REAL_NGINX_CONF" |
9d725af6 | 2961 | _err "write nginx conf error, but don't worry, the file is restored to the original version." |
2962 | return 1 | |
2963 | fi | |
2964 | ||
03f8d6e9 | 2965 | echo "$NGINX_START |
9d725af6 | 2966 | location ~ \"^/\.well-known/acme-challenge/([-_a-zA-Z0-9]+)\$\" { |
2967 | default_type text/plain; | |
2968 | return 200 \"\$1.$_thumbpt\"; | |
3c07f57a | 2969 | } |
03f8d6e9 | 2970 | #NGINX_START |
2971 | " >>"$FOUND_REAL_NGINX_CONF" | |
9d725af6 | 2972 | |
03f8d6e9 | 2973 | if ! sed -n "${_lnn},99999p" "$_backup_conf" >>"$FOUND_REAL_NGINX_CONF"; then |
2974 | cat "$_backup_conf" >"$FOUND_REAL_NGINX_CONF" | |
9d725af6 | 2975 | _err "write nginx conf error, but don't worry, the file is restored." |
2976 | return 1 | |
2977 | fi | |
df711b0e | 2978 | _debug3 "Modified config:$(cat $FOUND_REAL_NGINX_CONF)" |
9d725af6 | 2979 | _info "nginx conf is done, let's check it again." |
2980 | if ! _exec "nginx -t" >/dev/null; then | |
2981 | _exec_err | |
2982 | _err "It seems that nginx conf was broken, let's restore." | |
302c41ed | 2983 | cat "$_backup_conf" >"$FOUND_REAL_NGINX_CONF" |
9d725af6 | 2984 | return 1 |
2985 | fi | |
2986 | ||
2987 | _info "Reload nginx" | |
2988 | if ! _exec "nginx -s reload" >/dev/null; then | |
2989 | _exec_err | |
2990 | _err "It seems that nginx reload error, let's restore." | |
302c41ed | 2991 | cat "$_backup_conf" >"$FOUND_REAL_NGINX_CONF" |
9d725af6 | 2992 | return 1 |
2993 | fi | |
2994 | ||
2995 | return 0 | |
2996 | } | |
2997 | ||
2998 | #d , conf | |
2999 | _checkConf() { | |
3000 | _d="$1" | |
3001 | _c_file="$2" | |
3002 | _debug "Start _checkConf from:$_c_file" | |
3003 | if [ ! -f "$2" ] && ! echo "$2" | grep '*$' >/dev/null && echo "$2" | grep '*' >/dev/null; then | |
3004 | _debug "wildcard" | |
3005 | for _w_f in $2; do | |
7f618e7e | 3006 | if [ -f "$_w_f" ] && _checkConf "$1" "$_w_f"; then |
9d725af6 | 3007 | return 0 |
3008 | fi | |
3009 | done | |
3010 | #not found | |
3011 | return 1 | |
3012 | elif [ -f "$2" ]; then | |
3013 | _debug "single" | |
3014 | if _isRealNginxConf "$1" "$2"; then | |
3015 | _debug "$2 is found." | |
3016 | FOUND_REAL_NGINX_CONF="$2" | |
3017 | return 0 | |
3018 | fi | |
f08a79d3 | 3019 | if cat "$2" | tr "\t" " " | grep "^ *include *.*;" >/dev/null; then |
9d725af6 | 3020 | _debug "Try include files" |
f08a79d3 | 3021 | for included in $(cat "$2" | tr "\t" " " | grep "^ *include *.*;" | sed "s/include //" | tr -d " ;"); do |
9d725af6 | 3022 | _debug "check included $included" |
3023 | if _checkConf "$1" "$included"; then | |
3024 | return 0 | |
3025 | fi | |
3026 | done | |
3027 | fi | |
3028 | return 1 | |
3029 | else | |
3030 | _debug "$2 not found." | |
3031 | return 1 | |
3032 | fi | |
3033 | return 1 | |
3034 | } | |
3035 | ||
3036 | #d , conf | |
3037 | _isRealNginxConf() { | |
3038 | _debug "_isRealNginxConf $1 $2" | |
302c41ed | 3039 | if [ -f "$2" ]; then |
3f1a76d9 | 3040 | for _fln in $(tr "\t" ' ' <"$2" | grep -n "^ *server_name.* $1" | cut -d : -f 1); do |
302c41ed | 3041 | _debug _fln "$_fln" |
3042 | if [ "$_fln" ]; then | |
04a609b5 | 3043 | _start=$(tr "\t" ' ' <"$2" | _head_n "$_fln" | grep -n "^ *server *" | grep -v server_name | _tail_n 1) |
9f90618a | 3044 | _debug "_start" "$_start" |
3045 | _start_n=$(echo "$_start" | cut -d : -f 1) | |
3046 | _start_nn=$(_math $_start_n + 1) | |
3047 | _debug "_start_n" "$_start_n" | |
3048 | _debug "_start_nn" "$_start_nn" | |
3049 | ||
3050 | _left="$(sed -n "${_start_nn},99999p" "$2")" | |
3051 | _debug2 _left "$_left" | |
012dd698 | 3052 | _end="$(echo "$_left" | tr "\t" ' ' | grep -n "^ *server *" | grep -v server_name | _head_n 1)" |
3053 | _debug "_end" "$_end" | |
3054 | if [ "$_end" ]; then | |
9f90618a | 3055 | _end_n=$(echo "$_end" | cut -d : -f 1) |
3056 | _debug "_end_n" "$_end_n" | |
3057 | _seg_n=$(echo "$_left" | sed -n "1,${_end_n}p") | |
3058 | else | |
3059 | _seg_n="$_left" | |
3060 | fi | |
3061 | ||
3062 | _debug "_seg_n" "$_seg_n" | |
3063 | ||
04a609b5 | 3064 | _skip_ssl=1 |
d1067c60 | 3065 | for _listen_i in $(echo "$_seg_n" | tr "\t" ' ' | grep "^ *listen" | tr -d " "); do |
04a609b5 | 3066 | if [ "$_listen_i" ]; then |
9841063d | 3067 | if [ "$(echo "$_listen_i" | _egrep_o "listen.*ssl")" ]; then |
04a609b5 | 3068 | _debug2 "$_listen_i is ssl" |
3069 | else | |
3070 | _debug2 "$_listen_i is plain text" | |
3071 | _skip_ssl="" | |
c05eb0b1 | 3072 | break |
3073 | fi | |
04a609b5 | 3074 | fi |
3075 | done | |
3076 | ||
3077 | if [ "$_skip_ssl" = "1" ]; then | |
9f90618a | 3078 | _debug "ssl on, skip" |
241cfc43 | 3079 | else |
3080 | FOUND_REAL_NGINX_CONF_LN=$_fln | |
3081 | _debug3 "found FOUND_REAL_NGINX_CONF_LN" "$FOUND_REAL_NGINX_CONF_LN" | |
3082 | return 0 | |
450efea1 | 3083 | fi |
302c41ed | 3084 | fi |
3085 | done | |
9d725af6 | 3086 | fi |
302c41ed | 3087 | return 1 |
9d725af6 | 3088 | } |
3089 | ||
3090 | #restore all the nginx conf | |
3091 | _restoreNginx() { | |
5d943a35 | 3092 | if [ -z "$NGINX_RESTORE_VLIST" ]; then |
9d725af6 | 3093 | _debug "No need to restore nginx, skip." |
3094 | return | |
3095 | fi | |
3096 | _debug "_restoreNginx" | |
5d943a35 | 3097 | _debug "NGINX_RESTORE_VLIST" "$NGINX_RESTORE_VLIST" |
9d725af6 | 3098 | |
5d943a35 | 3099 | for ng_entry in $(echo "$NGINX_RESTORE_VLIST" | tr "$dvsep" ' '); do |
9d725af6 | 3100 | _debug "ng_entry" "$ng_entry" |
3101 | _nd=$(echo "$ng_entry" | cut -d "$sep" -f 1) | |
3102 | _ngconf=$(echo "$ng_entry" | cut -d "$sep" -f 2) | |
3103 | _ngbackupconf=$(echo "$ng_entry" | cut -d "$sep" -f 3) | |
3104 | _info "Restoring from $_ngbackupconf to $_ngconf" | |
302c41ed | 3105 | cat "$_ngbackupconf" >"$_ngconf" |
9d725af6 | 3106 | done |
3107 | ||
3108 | _info "Reload nginx" | |
3109 | if ! _exec "nginx -s reload" >/dev/null; then | |
3110 | _exec_err | |
3111 | _err "It seems that nginx reload error, please report bug." | |
3112 | return 1 | |
3113 | fi | |
3114 | return 0 | |
3115 | } | |
3116 | ||
5ef501c5 | 3117 | _clearup() { |
44edb2bd | 3118 | _stopserver "$serverproc" |
4c3b3608 | 3119 | serverproc="" |
3120 | _restoreApache | |
9d725af6 | 3121 | _restoreNginx |
800e3f45 | 3122 | _clearupdns |
4c2a3841 | 3123 | if [ -z "$DEBUG" ]; then |
e22bcf7c | 3124 | rm -f "$TLS_CONF" |
3125 | rm -f "$TLS_CERT" | |
3126 | rm -f "$TLS_KEY" | |
3127 | rm -f "$TLS_CSR" | |
3128 | fi | |
4c3b3608 | 3129 | } |
3130 | ||
800e3f45 | 3131 | _clearupdns() { |
3132 | _debug "_clearupdns" | |
b5ca9bba | 3133 | _debug "dns_entries" "$dns_entries" |
3134 | ||
3135 | if [ -z "$dns_entries" ]; then | |
65b22b49 | 3136 | _debug "skip dns." |
800e3f45 | 3137 | return |
3138 | fi | |
875625b1 | 3139 | _info "Removing DNS records." |
800e3f45 | 3140 | |
b5ca9bba | 3141 | for entry in $dns_entries; do |
3142 | d=$(_getfield "$entry" 1) | |
3143 | txtdomain=$(_getfield "$entry" 2) | |
3144 | aliasDomain=$(_getfield "$entry" 3) | |
9a733a57 | 3145 | _currentRoot=$(_getfield "$entry" 4) |
b5ca9bba | 3146 | txt=$(_getfield "$entry" 5) |
3147 | d_api=$(_getfield "$entry" 6) | |
3148 | _debug "d" "$d" | |
3149 | _debug "txtdomain" "$txtdomain" | |
3150 | _debug "aliasDomain" "$aliasDomain" | |
9a733a57 | 3151 | _debug "_currentRoot" "$_currentRoot" |
b5ca9bba | 3152 | _debug "txt" "$txt" |
3153 | _debug "d_api" "$d_api" | |
3154 | if [ "$d_api" = "$txt" ]; then | |
3155 | d_api="" | |
800e3f45 | 3156 | fi |
4c2a3841 | 3157 | |
4c2a3841 | 3158 | if [ -z "$d_api" ]; then |
800e3f45 | 3159 | _info "Not Found domain api file: $d_api" |
3160 | continue | |
3161 | fi | |
4c2a3841 | 3162 | |
b5ca9bba | 3163 | if [ "$aliasDomain" ]; then |
3164 | txtdomain="$aliasDomain" | |
3165 | fi | |
3166 | ||
800e3f45 | 3167 | ( |
d5ec5f80 | 3168 | if ! . "$d_api"; then |
800e3f45 | 3169 | _err "Load file $d_api error. Please check your api file and try again." |
3170 | return 1 | |
3171 | fi | |
4c2a3841 | 3172 | |
800e3f45 | 3173 | rmcommand="${_currentRoot}_rm" |
d5ec5f80 | 3174 | if ! _exists "$rmcommand"; then |
800e3f45 | 3175 | _err "It seems that your api file doesn't define $rmcommand" |
3176 | return 1 | |
3177 | fi | |
a180b95c | 3178 | _info "Removing txt: $txt for domain: $txtdomain" |
21f201e3 | 3179 | if ! $rmcommand "$txtdomain" "$txt"; then |
800e3f45 | 3180 | _err "Error removing txt for domain:$txtdomain" |
3181 | return 1 | |
3182 | fi | |
a180b95c | 3183 | _info "Removed: Success" |
800e3f45 | 3184 | ) |
4c2a3841 | 3185 | |
800e3f45 | 3186 | done |
3187 | } | |
3188 | ||
4c3b3608 | 3189 | # webroot removelevel tokenfile |
3190 | _clearupwebbroot() { | |
3191 | __webroot="$1" | |
4c2a3841 | 3192 | if [ -z "$__webroot" ]; then |
4c3b3608 | 3193 | _debug "no webroot specified, skip" |
3194 | return 0 | |
3195 | fi | |
4c2a3841 | 3196 | |
dcf9cb58 | 3197 | _rmpath="" |
4c2a3841 | 3198 | if [ "$2" = '1' ]; then |
dcf9cb58 | 3199 | _rmpath="$__webroot/.well-known" |
4c2a3841 | 3200 | elif [ "$2" = '2' ]; then |
dcf9cb58 | 3201 | _rmpath="$__webroot/.well-known/acme-challenge" |
4c2a3841 | 3202 | elif [ "$2" = '3' ]; then |
dcf9cb58 | 3203 | _rmpath="$__webroot/.well-known/acme-challenge/$3" |
4c3b3608 | 3204 | else |
cc179731 | 3205 | _debug "Skip for removelevel:$2" |
4c3b3608 | 3206 | fi |
4c2a3841 | 3207 | |
3208 | if [ "$_rmpath" ]; then | |
3209 | if [ "$DEBUG" ]; then | |
dcf9cb58 | 3210 | _debug "Debugging, skip removing: $_rmpath" |
3211 | else | |
3212 | rm -rf "$_rmpath" | |
3213 | fi | |
3214 | fi | |
4c2a3841 | 3215 | |
4c3b3608 | 3216 | return 0 |
3217 | ||
3218 | } | |
3219 | ||
b0070f03 | 3220 | _on_before_issue() { |
af1cc3b3 | 3221 | _chk_web_roots="$1" |
02140ce7 | 3222 | _chk_main_domain="$2" |
3223 | _chk_alt_domains="$3" | |
85e1f4ea | 3224 | _chk_pre_hook="$4" |
3225 | _chk_local_addr="$5" | |
30c2d84c | 3226 | _debug _on_before_issue |
38f1b4d2 | 3227 | _debug _chk_main_domain "$_chk_main_domain" |
3228 | _debug _chk_alt_domains "$_chk_alt_domains" | |
d0f7c309 | 3229 | #run pre hook |
85e1f4ea | 3230 | if [ "$_chk_pre_hook" ]; then |
3231 | _info "Run pre hook:'$_chk_pre_hook'" | |
d0f7c309 | 3232 | if ! ( |
85e1f4ea | 3233 | cd "$DOMAIN_PATH" && eval "$_chk_pre_hook" |
d0f7c309 | 3234 | ); then |
3235 | _err "Error when run pre hook." | |
3236 | return 1 | |
3237 | fi | |
3238 | fi | |
3239 | ||
af1cc3b3 | 3240 | if _hasfield "$_chk_web_roots" "$NO_VALUE"; then |
3794b5cb | 3241 | if ! _exists "socat"; then |
3242 | _err "Please install socat tools first." | |
0463b5d6 | 3243 | return 1 |
3244 | fi | |
0463b5d6 | 3245 | fi |
3246 | ||
85e1f4ea | 3247 | _debug Le_LocalAddress "$_chk_local_addr" |
4c2a3841 | 3248 | |
0463b5d6 | 3249 | _index=1 |
3250 | _currentRoot="" | |
3251 | _addrIndex=1 | |
38f1b4d2 | 3252 | _w_index=1 |
931d19ee | 3253 | while true; do |
931d19ee | 3254 | d="$(echo "$_chk_main_domain,$_chk_alt_domains," | cut -d , -f "$_w_index")" |
3255 | _w_index="$(_math "$_w_index" + 1)" | |
3256 | _debug d "$d" | |
3257 | if [ -z "$d" ]; then | |
3258 | break | |
3259 | fi | |
d5ec5f80 | 3260 | _debug "Check for domain" "$d" |
af1cc3b3 | 3261 | _currentRoot="$(_getfield "$_chk_web_roots" $_index)" |
0463b5d6 | 3262 | _debug "_currentRoot" "$_currentRoot" |
3263 | _index=$(_math $_index + 1) | |
3264 | _checkport="" | |
4c2a3841 | 3265 | if [ "$_currentRoot" = "$NO_VALUE" ]; then |
0463b5d6 | 3266 | _info "Standalone mode." |
4c2a3841 | 3267 | if [ -z "$Le_HTTPPort" ]; then |
0463b5d6 | 3268 | Le_HTTPPort=80 |
e7f7e96d | 3269 | _cleardomainconf "Le_HTTPPort" |
0463b5d6 | 3270 | else |
4c2a3841 | 3271 | _savedomainconf "Le_HTTPPort" "$Le_HTTPPort" |
0463b5d6 | 3272 | fi |
3273 | _checkport="$Le_HTTPPort" | |
43ff787b | 3274 | elif [ "$_currentRoot" = "$W_ALPN" ]; then |
3275 | _info "Standalone alpn mode." | |
4c2a3841 | 3276 | if [ -z "$Le_TLSPort" ]; then |
0463b5d6 | 3277 | Le_TLSPort=443 |
3278 | else | |
4c2a3841 | 3279 | _savedomainconf "Le_TLSPort" "$Le_TLSPort" |
0463b5d6 | 3280 | fi |
3281 | _checkport="$Le_TLSPort" | |
3282 | fi | |
4c2a3841 | 3283 | |
3284 | if [ "$_checkport" ]; then | |
0463b5d6 | 3285 | _debug _checkport "$_checkport" |
85e1f4ea | 3286 | _checkaddr="$(_getfield "$_chk_local_addr" $_addrIndex)" |
0463b5d6 | 3287 | _debug _checkaddr "$_checkaddr" |
4c2a3841 | 3288 | |
0463b5d6 | 3289 | _addrIndex="$(_math $_addrIndex + 1)" |
4c2a3841 | 3290 | |
0463b5d6 | 3291 | _netprc="$(_ss "$_checkport" | grep "$_checkport")" |
3292 | netprc="$(echo "$_netprc" | grep "$_checkaddr")" | |
4c2a3841 | 3293 | if [ -z "$netprc" ]; then |
0463b5d6 | 3294 | netprc="$(echo "$_netprc" | grep "$LOCAL_ANY_ADDRESS")" |
3295 | fi | |
4c2a3841 | 3296 | if [ "$netprc" ]; then |
0463b5d6 | 3297 | _err "$netprc" |
4c2a3841 | 3298 | _err "tcp port $_checkport is already used by $(echo "$netprc" | cut -d : -f 4)" |
0463b5d6 | 3299 | _err "Please stop it first" |
3300 | return 1 | |
3301 | fi | |
3302 | fi | |
3303 | done | |
3304 | ||
af1cc3b3 | 3305 | if _hasfield "$_chk_web_roots" "apache"; then |
4c2a3841 | 3306 | if ! _setApache; then |
0463b5d6 | 3307 | _err "set up apache error. Report error to me." |
3308 | return 1 | |
3309 | fi | |
3310 | else | |
3311 | usingApache="" | |
3312 | fi | |
3313 | ||
b0070f03 | 3314 | } |
3315 | ||
3316 | _on_issue_err() { | |
85e1f4ea | 3317 | _chk_post_hook="$1" |
58e4d337 | 3318 | _chk_vlist="$2" |
30c2d84c | 3319 | _debug _on_issue_err |
cd8fc359 | 3320 | |
4c2a3841 | 3321 | if [ "$LOG_FILE" ]; then |
a73c5b33 | 3322 | _err "Please check log file for more details: $LOG_FILE" |
3323 | else | |
54ae008d | 3324 | _err "Please add '--debug' or '--log' to check more details." |
a73c5b33 | 3325 | _err "See: $_DEBUG_WIKI" |
3326 | fi | |
4c2a3841 | 3327 | |
b0070f03 | 3328 | #run the post hook |
85e1f4ea | 3329 | if [ "$_chk_post_hook" ]; then |
3330 | _info "Run post hook:'$_chk_post_hook'" | |
b0070f03 | 3331 | if ! ( |
85e1f4ea | 3332 | cd "$DOMAIN_PATH" && eval "$_chk_post_hook" |
4c2a3841 | 3333 | ); then |
b0070f03 | 3334 | _err "Error when run post hook." |
3335 | return 1 | |
3336 | fi | |
3337 | fi | |
58e4d337 | 3338 | |
3339 | #trigger the validation to flush the pending authz | |
ea722da3 | 3340 | _debug2 "_chk_vlist" "$_chk_vlist" |
58e4d337 | 3341 | if [ "$_chk_vlist" ]; then |
3342 | ( | |
c719a61e | 3343 | _debug2 "start to deactivate authz" |
3344 | ventries=$(echo "$_chk_vlist" | tr "$dvsep" ' ') | |
3345 | for ventry in $ventries; do | |
3346 | d=$(echo "$ventry" | cut -d "$sep" -f 1) | |
3347 | keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2) | |
3348 | uri=$(echo "$ventry" | cut -d "$sep" -f 3) | |
3349 | vtype=$(echo "$ventry" | cut -d "$sep" -f 4) | |
3350 | _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5) | |
f94433e5 | 3351 | __trigger_validation "$uri" "$keyauthorization" |
c719a61e | 3352 | done |
58e4d337 | 3353 | ) |
3354 | fi | |
3355 | ||
3881f221 | 3356 | if [ "$IS_RENEW" = "1" ] && _hasfield "$Le_Webroot" "$W_DNS"; then |
309bec47 | 3357 | _err "$_DNS_MANUAL_ERR" |
3358 | fi | |
3359 | ||
58e4d337 | 3360 | if [ "$DEBUG" ] && [ "$DEBUG" -gt "0" ]; then |
3361 | _debug "$(_dlg_versions)" | |
3362 | fi | |
3363 | ||
b0070f03 | 3364 | } |
3365 | ||
3366 | _on_issue_success() { | |
85e1f4ea | 3367 | _chk_post_hook="$1" |
3368 | _chk_renew_hook="$2" | |
30c2d84c | 3369 | _debug _on_issue_success |
c6b68551 | 3370 | |
b0070f03 | 3371 | #run the post hook |
85e1f4ea | 3372 | if [ "$_chk_post_hook" ]; then |
3373 | _info "Run post hook:'$_chk_post_hook'" | |
b0070f03 | 3374 | if ! ( |
c7849a43 | 3375 | export CERT_PATH |
3376 | export CERT_KEY_PATH | |
3377 | export CA_CERT_PATH | |
3378 | export CERT_FULLCHAIN_PATH | |
3379 | export Le_Domain="$_main_domain" | |
85e1f4ea | 3380 | cd "$DOMAIN_PATH" && eval "$_chk_post_hook" |
4c2a3841 | 3381 | ); then |
b0070f03 | 3382 | _err "Error when run post hook." |
3383 | return 1 | |
3384 | fi | |
3385 | fi | |
4c2a3841 | 3386 | |
b0070f03 | 3387 | #run renew hook |
85e1f4ea | 3388 | if [ "$IS_RENEW" ] && [ "$_chk_renew_hook" ]; then |
3389 | _info "Run renew hook:'$_chk_renew_hook'" | |
b0070f03 | 3390 | if ! ( |
c7849a43 | 3391 | export CERT_PATH |
3392 | export CERT_KEY_PATH | |
3393 | export CA_CERT_PATH | |
3394 | export CERT_FULLCHAIN_PATH | |
3395 | export Le_Domain="$_main_domain" | |
85e1f4ea | 3396 | cd "$DOMAIN_PATH" && eval "$_chk_renew_hook" |
4c2a3841 | 3397 | ); then |
b0070f03 | 3398 | _err "Error when run renew hook." |
3399 | return 1 | |
3400 | fi | |
4c2a3841 | 3401 | fi |
3402 | ||
867ec010 | 3403 | if _hasfield "$Le_Webroot" "$W_DNS" && [ -z "$FORCE_DNS_MANUAL" ]; then |
309bec47 | 3404 | _err "$_DNS_MANUAL_WARN" |
3405 | fi | |
3406 | ||
b0070f03 | 3407 | } |
3408 | ||
eb59817e | 3409 | registeraccount() { |
57e58ce7 | 3410 | _reg_length="$1" |
eb59817e | 3411 | _initpath |
57e58ce7 | 3412 | _regAccount "$_reg_length" |
eb59817e | 3413 | } |
d404e92d | 3414 | |
8a29fbc8 | 3415 | __calcAccountKeyHash() { |
ca7202eb | 3416 | [ -f "$ACCOUNT_KEY_PATH" ] && _digest sha256 <"$ACCOUNT_KEY_PATH" |
8a29fbc8 | 3417 | } |
3418 | ||
339a8ad6 | 3419 | __calc_account_thumbprint() { |
3420 | printf "%s" "$jwk" | tr -d ' ' | _digest "sha256" | _url_replace | |
3421 | } | |
3422 | ||
57e58ce7 | 3423 | #keylength |
d404e92d | 3424 | _regAccount() { |
3425 | _initpath | |
57e58ce7 | 3426 | _reg_length="$1" |
f87890cb | 3427 | _debug3 _regAccount "$_regAccount" |
c1151b0d | 3428 | _initAPI |
3429 | ||
1bbc33a0 | 3430 | mkdir -p "$CA_DIR" |
5c48e139 | 3431 | if [ ! -f "$ACCOUNT_KEY_PATH" ] && [ -f "$_OLD_ACCOUNT_KEY" ]; then |
3432 | _info "mv $_OLD_ACCOUNT_KEY to $ACCOUNT_KEY_PATH" | |
3433 | mv "$_OLD_ACCOUNT_KEY" "$ACCOUNT_KEY_PATH" | |
3434 | fi | |
4c2a3841 | 3435 | |
5c48e139 | 3436 | if [ ! -f "$ACCOUNT_JSON_PATH" ] && [ -f "$_OLD_ACCOUNT_JSON" ]; then |
3437 | _info "mv $_OLD_ACCOUNT_JSON to $ACCOUNT_JSON_PATH" | |
3438 | mv "$_OLD_ACCOUNT_JSON" "$ACCOUNT_JSON_PATH" | |
3439 | fi | |
4c2a3841 | 3440 | |
3441 | if [ ! -f "$ACCOUNT_KEY_PATH" ]; then | |
3442 | if ! _create_account_key "$_reg_length"; then | |
d404e92d | 3443 | _err "Create account key error." |
3444 | return 1 | |
3445 | fi | |
3446 | fi | |
4c2a3841 | 3447 | |
3448 | if ! _calcjwk "$ACCOUNT_KEY_PATH"; then | |
d404e92d | 3449 | return 1 |
3450 | fi | |
f8d22c48 | 3451 | |
c1151b0d | 3452 | if [ "$ACME_VERSION" = "2" ]; then |
3453 | regjson='{"termsOfServiceAgreed": true}' | |
3454 | if [ "$ACCOUNT_EMAIL" ]; then | |
7595808d | 3455 | regjson='{"contact": ["mailto:'$ACCOUNT_EMAIL'"], "termsOfServiceAgreed": true}' |
c1151b0d | 3456 | fi |
3457 | else | |
3458 | _reg_res="$ACME_NEW_ACCOUNT_RES" | |
3459 | regjson='{"resource": "'$_reg_res'", "terms-of-service-agreed": true, "agreement": "'$ACME_AGREEMENT'"}' | |
3460 | if [ "$ACCOUNT_EMAIL" ]; then | |
7595808d | 3461 | regjson='{"resource": "'$_reg_res'", "contact": ["mailto:'$ACCOUNT_EMAIL'"], "terms-of-service-agreed": true, "agreement": "'$ACME_AGREEMENT'"}' |
c1151b0d | 3462 | fi |
f87890cb | 3463 | fi |
4c2a3841 | 3464 | |
ca7ebd93 | 3465 | _info "Registering account" |
d404e92d | 3466 | |
f87890cb | 3467 | if ! _send_signed_request "${ACME_NEW_ACCOUNT}" "$regjson"; then |
3468 | _err "Register account Error: $response" | |
3469 | return 1 | |
3470 | fi | |
d404e92d | 3471 | |
f87890cb | 3472 | if [ "$code" = "" ] || [ "$code" = '201' ]; then |
3473 | echo "$response" >"$ACCOUNT_JSON_PATH" | |
3474 | _info "Registered" | |
7df20e50 | 3475 | elif [ "$code" = '409' ] || [ "$code" = '200' ]; then |
f87890cb | 3476 | _info "Already registered" |
3477 | else | |
3478 | _err "Register account Error: $response" | |
3479 | return 1 | |
3480 | fi | |
d404e92d | 3481 | |
5d8d217a | 3482 | _debug2 responseHeaders "$responseHeaders" |
0712e989 | 3483 | _accUri="$(echo "$responseHeaders" | grep -i "^Location:" | _head_n 1 | cut -d ':' -f 2- | tr -d "\r\n ")" |
f87890cb | 3484 | _debug "_accUri" "$_accUri" |
7e0b334b | 3485 | if [ -z "$_accUri" ]; then |
3486 | _err "Can not find account id url." | |
3487 | _err "$responseHeaders" | |
3488 | return 1 | |
3489 | fi | |
f87890cb | 3490 | _savecaconf "ACCOUNT_URL" "$_accUri" |
7e0b334b | 3491 | export ACCOUNT_URL="$_accUri" |
d404e92d | 3492 | |
f87890cb | 3493 | CA_KEY_HASH="$(__calcAccountKeyHash)" |
3494 | _debug "Calc CA_KEY_HASH" "$CA_KEY_HASH" | |
3495 | _savecaconf CA_KEY_HASH "$CA_KEY_HASH" | |
d404e92d | 3496 | |
f87890cb | 3497 | if [ "$code" = '403' ]; then |
3498 | _err "It seems that the account key is already deactivated, please use a new account key." | |
3499 | return 1 | |
3500 | fi | |
4c2a3841 | 3501 | |
f87890cb | 3502 | ACCOUNT_THUMBPRINT="$(__calc_account_thumbprint)" |
3503 | _info "ACCOUNT_THUMBPRINT" "$ACCOUNT_THUMBPRINT" | |
d404e92d | 3504 | } |
3505 | ||
79e2f8a2 | 3506 | #implement updateaccount |
3507 | updateaccount() { | |
3508 | _initpath | |
3509 | ||
3510 | if [ ! -f "$ACCOUNT_KEY_PATH" ] && [ -f "$_OLD_ACCOUNT_KEY" ]; then | |
3511 | _info "mv $_OLD_ACCOUNT_KEY to $ACCOUNT_KEY_PATH" | |
3512 | mv "$_OLD_ACCOUNT_KEY" "$ACCOUNT_KEY_PATH" | |
3513 | fi | |
3514 | ||
3515 | if [ ! -f "$ACCOUNT_JSON_PATH" ] && [ -f "$_OLD_ACCOUNT_JSON" ]; then | |
3516 | _info "mv $_OLD_ACCOUNT_JSON to $ACCOUNT_JSON_PATH" | |
3517 | mv "$_OLD_ACCOUNT_JSON" "$ACCOUNT_JSON_PATH" | |
3518 | fi | |
3519 | ||
3520 | if [ ! -f "$ACCOUNT_KEY_PATH" ]; then | |
3521 | _err "Account key is not found at: $ACCOUNT_KEY_PATH" | |
3522 | return 1 | |
3523 | fi | |
3524 | ||
3525 | _accUri=$(_readcaconf "ACCOUNT_URL") | |
3526 | _debug _accUri "$_accUri" | |
3527 | ||
3528 | if [ -z "$_accUri" ]; then | |
3529 | _err "The account url is empty, please run '--update-account' first to update the account info first," | |
3530 | _err "Then try again." | |
3531 | return 1 | |
3532 | fi | |
3533 | ||
3534 | if ! _calcjwk "$ACCOUNT_KEY_PATH"; then | |
3535 | return 1 | |
3536 | fi | |
3537 | _initAPI | |
3538 | ||
3539 | if [ "$ACME_VERSION" = "2" ]; then | |
3540 | if [ "$ACCOUNT_EMAIL" ]; then | |
7595808d | 3541 | updjson='{"contact": ["mailto:'$ACCOUNT_EMAIL'"]}' |
2febdfc3 SW |
3542 | else |
3543 | updjson='{"contact": []}' | |
79e2f8a2 | 3544 | fi |
3545 | else | |
3546 | # ACMEv1: Updates happen the same way a registration is done. | |
3547 | # https://tools.ietf.org/html/draft-ietf-acme-acme-01#section-6.3 | |
3548 | _regAccount | |
3549 | return | |
3550 | fi | |
3551 | ||
3552 | # this part handles ACMEv2 account updates. | |
3553 | _send_signed_request "$_accUri" "$updjson" | |
3554 | ||
3555 | if [ "$code" = '200' ]; then | |
72e1a1b2 | 3556 | echo "$response" >"$ACCOUNT_JSON_PATH" |
79e2f8a2 | 3557 | _info "account update success for $_accUri." |
3558 | else | |
3559 | _info "Error. The account was not updated." | |
3560 | return 1 | |
3561 | fi | |
3562 | } | |
3563 | ||
422dd1fa | 3564 | #Implement deactivate account |
3565 | deactivateaccount() { | |
3566 | _initpath | |
3567 | ||
3568 | if [ ! -f "$ACCOUNT_KEY_PATH" ] && [ -f "$_OLD_ACCOUNT_KEY" ]; then | |
422dd1fa | 3569 | _info "mv $_OLD_ACCOUNT_KEY to $ACCOUNT_KEY_PATH" |
3570 | mv "$_OLD_ACCOUNT_KEY" "$ACCOUNT_KEY_PATH" | |
3571 | fi | |
3572 | ||
3573 | if [ ! -f "$ACCOUNT_JSON_PATH" ] && [ -f "$_OLD_ACCOUNT_JSON" ]; then | |
422dd1fa | 3574 | _info "mv $_OLD_ACCOUNT_JSON to $ACCOUNT_JSON_PATH" |
3575 | mv "$_OLD_ACCOUNT_JSON" "$ACCOUNT_JSON_PATH" | |
3576 | fi | |
3577 | ||
3578 | if [ ! -f "$ACCOUNT_KEY_PATH" ]; then | |
3579 | _err "Account key is not found at: $ACCOUNT_KEY_PATH" | |
3580 | return 1 | |
3581 | fi | |
3582 | ||
3583 | _accUri=$(_readcaconf "ACCOUNT_URL") | |
3584 | _debug _accUri "$_accUri" | |
3585 | ||
3586 | if [ -z "$_accUri" ]; then | |
3587 | _err "The account url is empty, please run '--update-account' first to update the account info first," | |
3588 | _err "Then try again." | |
3589 | return 1 | |
3590 | fi | |
3591 | ||
3592 | if ! _calcjwk "$ACCOUNT_KEY_PATH"; then | |
3593 | return 1 | |
3594 | fi | |
3595 | _initAPI | |
3596 | ||
d2cde379 | 3597 | if [ "$ACME_VERSION" = "2" ]; then |
3598 | _djson="{\"status\":\"deactivated\"}" | |
3599 | else | |
3600 | _djson="{\"resource\": \"reg\", \"status\":\"deactivated\"}" | |
3601 | fi | |
3602 | if _send_signed_request "$_accUri" "$_djson" && _contains "$response" '"deactivated"'; then | |
422dd1fa | 3603 | _info "Deactivate account success for $_accUri." |
3604 | _accid=$(echo "$response" | _egrep_o "\"id\" *: *[^,]*," | cut -d : -f 2 | tr -d ' ,') | |
3605 | elif [ "$code" = "403" ]; then | |
3606 | _info "The account is already deactivated." | |
3607 | _accid=$(_getfield "$_accUri" "999" "/") | |
3608 | else | |
3609 | _err "Deactivate: account failed for $_accUri." | |
3610 | return 1 | |
3611 | fi | |
3612 | ||
3613 | _debug "Account id: $_accid" | |
3614 | if [ "$_accid" ]; then | |
3615 | _deactivated_account_path="$CA_DIR/deactivated/$_accid" | |
3616 | _debug _deactivated_account_path "$_deactivated_account_path" | |
3617 | if mkdir -p "$_deactivated_account_path"; then | |
3618 | _info "Moving deactivated account info to $_deactivated_account_path/" | |
3619 | mv "$CA_CONF" "$_deactivated_account_path/" | |
3620 | mv "$ACCOUNT_JSON_PATH" "$_deactivated_account_path/" | |
3621 | mv "$ACCOUNT_KEY_PATH" "$_deactivated_account_path/" | |
3622 | else | |
3623 | _err "Can not create dir: $_deactivated_account_path, try to remove the deactivated account key." | |
3624 | rm -f "$CA_CONF" | |
3625 | rm -f "$ACCOUNT_JSON_PATH" | |
3626 | rm -f "$ACCOUNT_KEY_PATH" | |
3627 | fi | |
3628 | fi | |
3629 | } | |
3630 | ||
a61fe418 | 3631 | # domain folder file |
3632 | _findHook() { | |
3633 | _hookdomain="$1" | |
3634 | _hookcat="$2" | |
3635 | _hookname="$3" | |
3636 | ||
c7b16249 | 3637 | if [ -f "$_SCRIPT_HOME/$_hookcat/$_hookname" ]; then |
3638 | d_api="$_SCRIPT_HOME/$_hookcat/$_hookname" | |
3639 | elif [ -f "$_SCRIPT_HOME/$_hookcat/$_hookname.sh" ]; then | |
3640 | d_api="$_SCRIPT_HOME/$_hookcat/$_hookname.sh" | |
b50e701c | 3641 | elif [ "$_hookdomain" ] && [ -f "$LE_WORKING_DIR/$_hookdomain/$_hookname" ]; then |
a61fe418 | 3642 | d_api="$LE_WORKING_DIR/$_hookdomain/$_hookname" |
b50e701c | 3643 | elif [ "$_hookdomain" ] && [ -f "$LE_WORKING_DIR/$_hookdomain/$_hookname.sh" ]; then |
a61fe418 | 3644 | d_api="$LE_WORKING_DIR/$_hookdomain/$_hookname.sh" |
4c2a3841 | 3645 | elif [ -f "$LE_WORKING_DIR/$_hookname" ]; then |
a61fe418 | 3646 | d_api="$LE_WORKING_DIR/$_hookname" |
4c2a3841 | 3647 | elif [ -f "$LE_WORKING_DIR/$_hookname.sh" ]; then |
a61fe418 | 3648 | d_api="$LE_WORKING_DIR/$_hookname.sh" |
4c2a3841 | 3649 | elif [ -f "$LE_WORKING_DIR/$_hookcat/$_hookname" ]; then |
a61fe418 | 3650 | d_api="$LE_WORKING_DIR/$_hookcat/$_hookname" |
4c2a3841 | 3651 | elif [ -f "$LE_WORKING_DIR/$_hookcat/$_hookname.sh" ]; then |
a61fe418 | 3652 | d_api="$LE_WORKING_DIR/$_hookcat/$_hookname.sh" |
3653 | fi | |
3654 | ||
3655 | printf "%s" "$d_api" | |
3656 | } | |
3657 | ||
f940b2a5 | 3658 | #domain |
3659 | __get_domain_new_authz() { | |
3660 | _gdnd="$1" | |
3661 | _info "Getting new-authz for domain" "$_gdnd" | |
40ef86f4 | 3662 | _initAPI |
f940b2a5 | 3663 | _Max_new_authz_retry_times=5 |
3664 | _authz_i=0 | |
4c2a3841 | 3665 | while [ "$_authz_i" -lt "$_Max_new_authz_retry_times" ]; do |
efd96153 | 3666 | _debug "Try new-authz for the $_authz_i time." |
48d9a8c1 | 3667 | if ! _send_signed_request "${ACME_NEW_AUTHZ}" "{\"resource\": \"new-authz\", \"identifier\": {\"type\": \"dns\", \"value\": \"$(_idn "$_gdnd")\"}}"; then |
f940b2a5 | 3668 | _err "Can not get domain new authz." |
3669 | return 1 | |
3670 | fi | |
5413bf87 | 3671 | if _contains "$response" "No registration exists matching provided key"; then |
3672 | _err "It seems there is an error, but it's recovered now, please try again." | |
3673 | _err "If you see this message for a second time, please report bug: $(__green "$PROJECT")" | |
3674 | _clearcaconf "CA_KEY_HASH" | |
3675 | break | |
3676 | fi | |
4c2a3841 | 3677 | if ! _contains "$response" "An error occurred while processing your request"; then |
f940b2a5 | 3678 | _info "The new-authz request is ok." |
3679 | break | |
3680 | fi | |
3681 | _authz_i="$(_math "$_authz_i" + 1)" | |
9e45ac93 | 3682 | _info "The server is busy, Sleep $_authz_i to retry." |
f940b2a5 | 3683 | _sleep "$_authz_i" |
4c2a3841 | 3684 | done |
f940b2a5 | 3685 | |
4c2a3841 | 3686 | if [ "$_authz_i" = "$_Max_new_authz_retry_times" ]; then |
efd96153 | 3687 | _err "new-authz retry reach the max $_Max_new_authz_retry_times times." |
f940b2a5 | 3688 | fi |
4c2a3841 | 3689 | |
78915896 | 3690 | if [ "$code" ] && [ "$code" != '201' ]; then |
f940b2a5 | 3691 | _err "new-authz error: $response" |
3692 | return 1 | |
3693 | fi | |
3694 | ||
3695 | } | |
3696 | ||
58e4d337 | 3697 | #uri keyAuthorization |
f94433e5 | 3698 | __trigger_validation() { |
8bd12ed0 | 3699 | _debug2 "Trigger domain validation." |
58e4d337 | 3700 | _t_url="$1" |
3701 | _debug2 _t_url "$_t_url" | |
3702 | _t_key_authz="$2" | |
3703 | _debug2 _t_key_authz "$_t_key_authz" | |
920cab6f K |
3704 | _t_vtype="$3" |
3705 | _debug2 _t_vtype "$_t_vtype" | |
c1151b0d | 3706 | if [ "$ACME_VERSION" = "2" ]; then |
e7f7e96d | 3707 | _send_signed_request "$_t_url" "{}" |
c1151b0d | 3708 | else |
8bd12ed0 | 3709 | _send_signed_request "$_t_url" "{\"resource\": \"challenge\", \"type\": \"$_t_vtype\", \"keyAuthorization\": \"$_t_key_authz\"}" |
c1151b0d | 3710 | fi |
58e4d337 | 3711 | } |
3712 | ||
b5ca9bba | 3713 | #endpoint domain type |
10eec7d4 | 3714 | _ns_lookup_impl() { |
b5ca9bba | 3715 | _ns_ep="$1" |
3716 | _ns_domain="$2" | |
3717 | _ns_type="$3" | |
3718 | _debug2 "_ns_ep" "$_ns_ep" | |
3719 | _debug2 "_ns_domain" "$_ns_domain" | |
3720 | _debug2 "_ns_type" "$_ns_type" | |
3721 | ||
3722 | response="$(_H1="accept: application/dns-json" _get "$_ns_ep?name=$_ns_domain&type=$_ns_type")" | |
3723 | _ret=$? | |
3724 | _debug2 "response" "$response" | |
3725 | if [ "$_ret" != "0" ]; then | |
3726 | return $_ret | |
3727 | fi | |
3728 | _answers="$(echo "$response" | tr '{}' '<>' | _egrep_o '"Answer":\[[^]]*]' | tr '<>' '\n\n')" | |
3729 | _debug2 "_answers" "$_answers" | |
3730 | echo "$_answers" | |
3731 | } | |
3732 | ||
3733 | #domain, type | |
3734 | _ns_lookup_cf() { | |
3735 | _cf_ld="$1" | |
3736 | _cf_ld_type="$2" | |
3737 | _cf_ep="https://cloudflare-dns.com/dns-query" | |
10eec7d4 | 3738 | _ns_lookup_impl "$_cf_ep" "$_cf_ld" "$_cf_ld_type" |
b5ca9bba | 3739 | } |
3740 | ||
3741 | #domain, type | |
3742 | _ns_purge_cf() { | |
3743 | _cf_d="$1" | |
3744 | _cf_d_type="$2" | |
3745 | _debug "Cloudflare purge $_cf_d_type record for domain $_cf_d" | |
b9b2cd27 | 3746 | _cf_purl="https://cloudflare-dns.com/api/v1/purge?domain=$_cf_d&type=$_cf_d_type" |
b5ca9bba | 3747 | response="$(_post "" "$_cf_purl")" |
3748 | _debug2 response "$response" | |
3749 | } | |
3750 | ||
10eec7d4 | 3751 | #checks if cf server is available |
3752 | _ns_is_available_cf() { | |
1e7534b9 | 3753 | if _get "https://cloudflare-dns.com" >/dev/null 2>&1; then |
10eec7d4 | 3754 | return 0 |
3755 | else | |
3756 | return 1 | |
3757 | fi | |
3758 | } | |
3759 | ||
3760 | #domain, type | |
3761 | _ns_lookup_google() { | |
3762 | _cf_ld="$1" | |
3763 | _cf_ld_type="$2" | |
3764 | _cf_ep="https://dns.google/resolve" | |
3765 | _ns_lookup_impl "$_cf_ep" "$_cf_ld" "$_cf_ld_type" | |
3766 | } | |
3767 | ||
3768 | #domain, type | |
3769 | _ns_lookup() { | |
3770 | if [ -z "$DOH_USE" ]; then | |
3771 | _debug "Detect dns server first." | |
3772 | if _ns_is_available_cf; then | |
3773 | _debug "Use cloudflare doh server" | |
3774 | export DOH_USE=$DOH_CLOUDFLARE | |
3775 | else | |
3776 | _debug "Use google doh server" | |
3777 | export DOH_USE=$DOH_GOOGLE | |
3778 | fi | |
3779 | fi | |
3780 | ||
3781 | if [ "$DOH_USE" = "$DOH_CLOUDFLARE" ] || [ -z "$DOH_USE" ]; then | |
3782 | _ns_lookup_cf "$@" | |
3783 | else | |
3784 | _ns_lookup_google "$@" | |
3785 | fi | |
3786 | ||
3787 | } | |
3788 | ||
b5ca9bba | 3789 | #txtdomain, alias, txt |
3790 | __check_txt() { | |
3791 | _c_txtdomain="$1" | |
3792 | _c_aliasdomain="$2" | |
3793 | _c_txt="$3" | |
3794 | _debug "_c_txtdomain" "$_c_txtdomain" | |
3795 | _debug "_c_aliasdomain" "$_c_aliasdomain" | |
3796 | _debug "_c_txt" "$_c_txt" | |
10eec7d4 | 3797 | _answers="$(_ns_lookup "$_c_aliasdomain" TXT)" |
b5ca9bba | 3798 | _contains "$_answers" "$_c_txt" |
3799 | ||
3800 | } | |
3801 | ||
3802 | #txtdomain | |
3803 | __purge_txt() { | |
3804 | _p_txtdomain="$1" | |
3805 | _debug _p_txtdomain "$_p_txtdomain" | |
10eec7d4 | 3806 | if [ "$DOH_USE" = "$DOH_CLOUDFLARE" ] || [ -z "$DOH_USE" ]; then |
3807 | _ns_purge_cf "$_p_txtdomain" "TXT" | |
3808 | else | |
3809 | _debug "no purge api for google dns api, just sleep 5 secs" | |
3810 | _sleep 5 | |
3811 | fi | |
3812 | ||
b5ca9bba | 3813 | } |
3814 | ||
3815 | #wait and check each dns entries | |
3816 | _check_dns_entries() { | |
3817 | _success_txt="," | |
3818 | _end_time="$(_time)" | |
3819 | _end_time="$(_math "$_end_time" + 1200)" #let's check no more than 20 minutes. | |
3820 | ||
3821 | while [ "$(_time)" -le "$_end_time" ]; do | |
3822 | _left="" | |
3823 | for entry in $dns_entries; do | |
3824 | d=$(_getfield "$entry" 1) | |
3825 | txtdomain=$(_getfield "$entry" 2) | |
0093dc3d | 3826 | txtdomain=$(_idn "$txtdomain") |
b5ca9bba | 3827 | aliasDomain=$(_getfield "$entry" 3) |
0093dc3d | 3828 | aliasDomain=$(_idn "$aliasDomain") |
b5ca9bba | 3829 | txt=$(_getfield "$entry" 5) |
3830 | d_api=$(_getfield "$entry" 6) | |
3831 | _debug "d" "$d" | |
3832 | _debug "txtdomain" "$txtdomain" | |
3833 | _debug "aliasDomain" "$aliasDomain" | |
3834 | _debug "txt" "$txt" | |
3835 | _debug "d_api" "$d_api" | |
3836 | _info "Checking $d for $aliasDomain" | |
3837 | if _contains "$_success_txt" ",$txt,"; then | |
3838 | _info "Already success, continue next one." | |
3839 | continue | |
3840 | fi | |
3841 | ||
3842 | if __check_txt "$txtdomain" "$aliasDomain" "$txt"; then | |
3843 | _info "Domain $d '$aliasDomain' success." | |
3844 | _success_txt="$_success_txt,$txt," | |
3845 | continue | |
3846 | fi | |
3847 | _left=1 | |
3848 | _info "Not valid yet, let's wait 10 seconds and check next one." | |
b5ca9bba | 3849 | __purge_txt "$txtdomain" |
3850 | if [ "$txtdomain" != "$aliasDomain" ]; then | |
3851 | __purge_txt "$aliasDomain" | |
3852 | fi | |
b9b2cd27 | 3853 | _sleep 10 |
b5ca9bba | 3854 | done |
3855 | if [ "$_left" ]; then | |
3856 | _info "Let's wait 10 seconds and check again". | |
3857 | _sleep 10 | |
3858 | else | |
3859 | _info "All success, let's return" | |
a44ea0dd | 3860 | return 0 |
b5ca9bba | 3861 | fi |
3862 | done | |
a44ea0dd | 3863 | _info "Timed out waiting for DNS." |
3864 | return 1 | |
b5ca9bba | 3865 | |
3866 | } | |
3867 | ||
3c07f57a | 3868 | #webroot, domain domainlist keylength |
4c3b3608 | 3869 | issue() { |
4c2a3841 | 3870 | if [ -z "$2" ]; then |
43822d37 | 3871 | _usage "Usage: $PROJECT_ENTRY --issue -d a.com -w /path/to/webroot/a.com/ " |
4c3b3608 | 3872 | return 1 |
3873 | fi | |
49d75a0c | 3874 | if [ -z "$1" ]; then |
3875 | _usage "Please specify at least one validation method: '--webroot', '--standalone', '--apache', '--nginx' or '--dns' etc." | |
3876 | return 1 | |
3877 | fi | |
af1cc3b3 | 3878 | _web_roots="$1" |
3879 | _main_domain="$2" | |
02140ce7 | 3880 | _alt_domains="$3" |
d2cde379 | 3881 | |
af1cc3b3 | 3882 | if _contains "$_main_domain" ","; then |
3883 | _main_domain=$(echo "$2,$3" | cut -d , -f 1) | |
02140ce7 | 3884 | _alt_domains=$(echo "$2,$3" | cut -d , -f 2- | sed "s/,${NO_VALUE}$//") |
2aff36e7 | 3885 | fi |
674b5088 | 3886 | _debug _main_domain "$_main_domain" |
3887 | _debug _alt_domains "$_alt_domains" | |
3888 | ||
d9c9114b | 3889 | _key_length="$4" |
85e1f4ea | 3890 | _real_cert="$5" |
3891 | _real_key="$6" | |
3892 | _real_ca="$7" | |
3893 | _reload_cmd="$8" | |
3894 | _real_fullchain="$9" | |
3895 | _pre_hook="${10}" | |
3896 | _post_hook="${11}" | |
3897 | _renew_hook="${12}" | |
3898 | _local_addr="${13}" | |
875625b1 | 3899 | _challenge_alias="${14}" |
eccec5f6 | 3900 | #remove these later. |
af1cc3b3 | 3901 | if [ "$_web_roots" = "dns-cf" ]; then |
3902 | _web_roots="dns_cf" | |
eccec5f6 | 3903 | fi |
af1cc3b3 | 3904 | if [ "$_web_roots" = "dns-dp" ]; then |
3905 | _web_roots="dns_dp" | |
eccec5f6 | 3906 | fi |
af1cc3b3 | 3907 | if [ "$_web_roots" = "dns-cx" ]; then |
3908 | _web_roots="dns_cx" | |
eccec5f6 | 3909 | fi |
4c2a3841 | 3910 | |
3911 | if [ ! "$IS_RENEW" ]; then | |
d9c9114b | 3912 | _initpath "$_main_domain" "$_key_length" |
43822d37 | 3913 | mkdir -p "$DOMAIN_PATH" |
3914 | fi | |
eccec5f6 | 3915 | |
a0923622 | 3916 | if _hasfield "$_web_roots" "$W_DNS" && [ -z "$FORCE_DNS_MANUAL" ]; then |
3917 | _err "$_DNS_MANUAL_ERROR" | |
3918 | return 1 | |
3919 | fi | |
3920 | ||
48d9a8c1 | 3921 | _debug "Using ACME_DIRECTORY: $ACME_DIRECTORY" |
3922 | ||
3923 | _initAPI | |
3924 | ||
4c2a3841 | 3925 | if [ -f "$DOMAIN_CONF" ]; then |
61623d22 | 3926 | Le_NextRenewTime=$(_readdomainconf Le_NextRenewTime) |
a4270efa | 3927 | _debug Le_NextRenewTime "$Le_NextRenewTime" |
95e06de5 | 3928 | if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(_time)" -lt "$Le_NextRenewTime" ]; then |
bb25febd | 3929 | _saved_domain=$(_readdomainconf Le_Domain) |
3930 | _debug _saved_domain "$_saved_domain" | |
3931 | _saved_alt=$(_readdomainconf Le_Alt) | |
3932 | _debug _saved_alt "$_saved_alt" | |
02140ce7 | 3933 | if [ "$_saved_domain,$_saved_alt" = "$_main_domain,$_alt_domains" ]; then |
bb25febd | 3934 | _info "Domains not changed." |
3935 | _info "Skip, Next renewal time is: $(__green "$(_readdomainconf Le_NextRenewTimeStr)")" | |
4c2a3841 | 3936 | _info "Add '$(__red '--force')' to force to renew." |
bb25febd | 3937 | return $RENEW_SKIP |
3938 | else | |
3939 | _info "Domains have changed." | |
3940 | fi | |
4c3b3608 | 3941 | fi |
3942 | fi | |
96a46cfc | 3943 | |
af1cc3b3 | 3944 | _savedomainconf "Le_Domain" "$_main_domain" |
02140ce7 | 3945 | _savedomainconf "Le_Alt" "$_alt_domains" |
af1cc3b3 | 3946 | _savedomainconf "Le_Webroot" "$_web_roots" |
4c2a3841 | 3947 | |
c7257bcf | 3948 | _savedomainconf "Le_PreHook" "$_pre_hook" "base64" |
3949 | _savedomainconf "Le_PostHook" "$_post_hook" "base64" | |
3950 | _savedomainconf "Le_RenewHook" "$_renew_hook" "base64" | |
4c2a3841 | 3951 | |
85e1f4ea | 3952 | if [ "$_local_addr" ]; then |
3953 | _savedomainconf "Le_LocalAddress" "$_local_addr" | |
72518d48 | 3954 | else |
3955 | _cleardomainconf "Le_LocalAddress" | |
3956 | fi | |
875625b1 | 3957 | if [ "$_challenge_alias" ]; then |
3958 | _savedomainconf "Le_ChallengeAlias" "$_challenge_alias" | |
3959 | else | |
3960 | _cleardomainconf "Le_ChallengeAlias" | |
3961 | fi | |
6ae0f7f5 | 3962 | |
f2add8de | 3963 | if [ "$ACME_DIRECTORY" != "$DEFAULT_CA" ]; then |
3964 | Le_API="$ACME_DIRECTORY" | |
3965 | _savedomainconf "Le_API" "$Le_API" | |
3966 | else | |
3967 | _cleardomainconf Le_API | |
3968 | fi | |
4c2a3841 | 3969 | |
02140ce7 | 3970 | if [ "$_alt_domains" = "$NO_VALUE" ]; then |
3971 | _alt_domains="" | |
4c3b3608 | 3972 | fi |
4c2a3841 | 3973 | |
d9c9114b | 3974 | if [ "$_key_length" = "$NO_VALUE" ]; then |
3975 | _key_length="" | |
d404e92d | 3976 | fi |
4c2a3841 | 3977 | |
85e1f4ea | 3978 | if ! _on_before_issue "$_web_roots" "$_main_domain" "$_alt_domains" "$_pre_hook" "$_local_addr"; then |
0463b5d6 | 3979 | _err "_on_before_issue." |
3980 | return 1 | |
4c3b3608 | 3981 | fi |
0463b5d6 | 3982 | |
8a29fbc8 | 3983 | _saved_account_key_hash="$(_readcaconf "CA_KEY_HASH")" |
3984 | _debug2 _saved_account_key_hash "$_saved_account_key_hash" | |
4c2a3841 | 3985 | |
e8b54a50 | 3986 | if [ -z "$ACCOUNT_URL" ] || [ -z "$_saved_account_key_hash" ] || [ "$_saved_account_key_hash" != "$(__calcAccountKeyHash)" ]; then |
57e58ce7 | 3987 | if ! _regAccount "$_accountkeylength"; then |
85e1f4ea | 3988 | _on_issue_err "$_post_hook" |
8a29fbc8 | 3989 | return 1 |
3990 | fi | |
57e58ce7 | 3991 | else |
3992 | _debug "_saved_account_key_hash is not changed, skip register account." | |
166096dc | 3993 | fi |
166096dc | 3994 | |
4c2a3841 | 3995 | if [ -f "$CSR_PATH" ] && [ ! -f "$CERT_KEY_PATH" ]; then |
10afcaca | 3996 | _info "Signing from existing CSR." |
3997 | else | |
3998 | _key=$(_readdomainconf Le_Keylength) | |
3999 | _debug "Read key length:$_key" | |
c4b2e582 | 4000 | if [ ! -f "$CERT_KEY_PATH" ] || [ "$_key_length" != "$_key" ] || [ "$Le_ForceNewDomainKey" = "1" ]; then |
d9c9114b | 4001 | if ! createDomainKey "$_main_domain" "$_key_length"; then |
10afcaca | 4002 | _err "Create domain key error." |
4003 | _clearup | |
85e1f4ea | 4004 | _on_issue_err "$_post_hook" |
10afcaca | 4005 | return 1 |
4006 | fi | |
4007 | fi | |
4008 | ||
02140ce7 | 4009 | if ! _createcsr "$_main_domain" "$_alt_domains" "$CERT_KEY_PATH" "$CSR_PATH" "$DOMAIN_SSL_CONF"; then |
10afcaca | 4010 | _err "Create CSR error." |
5ef501c5 | 4011 | _clearup |
85e1f4ea | 4012 | _on_issue_err "$_post_hook" |
41e3eafa | 4013 | return 1 |
4014 | fi | |
4c3b3608 | 4015 | fi |
10afcaca | 4016 | |
d9c9114b | 4017 | _savedomainconf "Le_Keylength" "$_key_length" |
4c2a3841 | 4018 | |
4c3b3608 | 4019 | vlist="$Le_Vlist" |
882ac74a | 4020 | _cleardomainconf "Le_Vlist" |
cae203be | 4021 | _info "Getting domain auth token for each domain" |
4c3b3608 | 4022 | sep='#' |
9d725af6 | 4023 | dvsep=',' |
4c2a3841 | 4024 | if [ -z "$vlist" ]; then |
d2cde379 | 4025 | if [ "$ACME_VERSION" = "2" ]; then |
c1151b0d | 4026 | #make new order request |
0093dc3d | 4027 | _identifiers="{\"type\":\"dns\",\"value\":\"$(_idn "$_main_domain")\"}" |
38f1b4d2 | 4028 | _w_index=1 |
674b5088 | 4029 | while true; do |
dd17124e | 4030 | d="$(echo "$_alt_domains," | cut -d , -f "$_w_index")" |
674b5088 | 4031 | _w_index="$(_math "$_w_index" + 1)" |
4032 | _debug d "$d" | |
4033 | if [ -z "$d" ]; then | |
4034 | break | |
c1151b0d | 4035 | fi |
0093dc3d | 4036 | _identifiers="$_identifiers,{\"type\":\"dns\",\"value\":\"$(_idn "$d")\"}" |
c1151b0d | 4037 | done |
4038 | _debug2 _identifiers "$_identifiers" | |
4039 | if ! _send_signed_request "$ACME_NEW_ORDER" "{\"identifiers\": [$_identifiers]}"; then | |
4040 | _err "Create new order error." | |
4041 | _clearup | |
4042 | _on_issue_err "$_post_hook" | |
4043 | return 1 | |
4044 | fi | |
05aa26e6 | 4045 | Le_LinkOrder="$(echo "$responseHeaders" | grep -i '^Location.*$' | _tail_n 1 | tr -d "\r\n " | cut -d ":" -f 2-)" |
fbdc5a0e | 4046 | _debug Le_LinkOrder "$Le_LinkOrder" |
dbc44c08 | 4047 | Le_OrderFinalize="$(echo "$response" | _egrep_o '"finalize" *: *"[^"]*"' | cut -d '"' -f 4)" |
d2cde379 | 4048 | _debug Le_OrderFinalize "$Le_OrderFinalize" |
4049 | if [ -z "$Le_OrderFinalize" ]; then | |
78915896 | 4050 | _err "Create new order error. Le_OrderFinalize not found. $response" |
c1151b0d | 4051 | _clearup |
4052 | _on_issue_err "$_post_hook" | |
4053 | return 1 | |
4054 | fi | |
4055 | ||
4056 | #for dns manual mode | |
d2cde379 | 4057 | _savedomainconf "Le_OrderFinalize" "$Le_OrderFinalize" |
c1151b0d | 4058 | |
93de1e49 | 4059 | _authorizations_seg="$(echo "$response" | _json_decode | _egrep_o '"authorizations" *: *\[[^\[]*\]' | cut -d '[' -f 2 | tr -d ']' | tr -d '"')" |
c1151b0d | 4060 | _debug2 _authorizations_seg "$_authorizations_seg" |
4061 | if [ -z "$_authorizations_seg" ]; then | |
4062 | _err "_authorizations_seg not found." | |
4063 | _clearup | |
4064 | _on_issue_err "$_post_hook" | |
4065 | return 1 | |
4066 | fi | |
4067 | ||
4068 | #domain and authz map | |
4069 | _authorizations_map="" | |
f8d22c48 | 4070 | for _authz_url in $(echo "$_authorizations_seg" | tr ',' ' '); do |
c1151b0d | 4071 | _debug2 "_authz_url" "$_authz_url" |
0483d841 | 4072 | if ! _send_signed_request "$_authz_url"; then |
c1151b0d | 4073 | _err "get to authz error." |
263c38ca | 4074 | _err "_authorizations_seg" "$_authorizations_seg" |
4075 | _err "_authz_url" "$_authz_url" | |
c1151b0d | 4076 | _clearup |
4077 | _on_issue_err "$_post_hook" | |
4078 | return 1 | |
4079 | fi | |
4080 | ||
4081 | response="$(echo "$response" | _normalizeJson)" | |
4082 | _debug2 response "$response" | |
4083 | _d="$(echo "$response" | _egrep_o '"value" *: *"[^"]*"' | cut -d : -f 2 | tr -d ' "')" | |
72f54ca6 | 4084 | if _contains "$response" "\"wildcard\" *: *true"; then |
4085 | _d="*.$_d" | |
4086 | fi | |
c1151b0d | 4087 | _debug2 _d "$_d" |
4088 | _authorizations_map="$_d,$response | |
4089 | $_authorizations_map" | |
4090 | done | |
4091 | _debug2 _authorizations_map "$_authorizations_map" | |
4092 | fi | |
4093 | ||
c1151b0d | 4094 | _index=0 |
a63b05a9 | 4095 | _currentRoot="" |
38f1b4d2 | 4096 | _w_index=1 |
88bbe55b | 4097 | while true; do |
88bbe55b | 4098 | d="$(echo "$_main_domain,$_alt_domains," | cut -d , -f "$_w_index")" |
4099 | _w_index="$(_math "$_w_index" + 1)" | |
4100 | _debug d "$d" | |
4101 | if [ -z "$d" ]; then | |
4102 | break | |
4103 | fi | |
ca7202eb | 4104 | _info "Getting webroot for domain" "$d" |
c1151b0d | 4105 | _index=$(_math $_index + 1) |
af1cc3b3 | 4106 | _w="$(echo $_web_roots | cut -d , -f $_index)" |
9d725af6 | 4107 | _debug _w "$_w" |
4c2a3841 | 4108 | if [ "$_w" ]; then |
a63b05a9 | 4109 | _currentRoot="$_w" |
4110 | fi | |
4111 | _debug "_currentRoot" "$_currentRoot" | |
4c2a3841 | 4112 | |
a63b05a9 | 4113 | vtype="$VTYPE_HTTP" |
c1151b0d | 4114 | #todo, v2 wildcard force to use dns |
3881f221 | 4115 | if _startswith "$_currentRoot" "$W_DNS"; then |
a63b05a9 | 4116 | vtype="$VTYPE_DNS" |
4117 | fi | |
4c2a3841 | 4118 | |
08681f4a | 4119 | if [ "$_currentRoot" = "$W_ALPN" ]; then |
4120 | vtype="$VTYPE_ALPN" | |
4121 | fi | |
4122 | ||
c1151b0d | 4123 | if [ "$ACME_VERSION" = "2" ]; then |
d04c6dd3 | 4124 | _idn_d="$(_idn "$d")" |
dbc43550 L |
4125 | _candidates="$(echo "$_authorizations_map" | grep -i "^$_idn_d,")" |
4126 | _debug2 _candidates "$_candidates" | |
4127 | if [ "$(echo "$_candidates" | wc -l)" -gt 1 ]; then | |
4128 | for _can in $_candidates; do | |
d04c6dd3 | 4129 | if _startswith "$(echo "$_can" | tr '.' '|')" "$(echo "$_idn_d" | tr '.' '|'),"; then |
dbc43550 | 4130 | _candidates="$_can" |
d04c6dd3 | 4131 | break |
4132 | fi | |
4133 | done | |
4134 | fi | |
dbc43550 | 4135 | response="$(echo "$_candidates" | sed "s/$_idn_d,//")" |
c1151b0d | 4136 | _debug2 "response" "$response" |
4137 | if [ -z "$response" ]; then | |
4138 | _err "get to authz error." | |
263c38ca | 4139 | _err "_authorizations_map" "$_authorizations_map" |
c1151b0d | 4140 | _clearup |
4141 | _on_issue_err "$_post_hook" | |
4142 | return 1 | |
4143 | fi | |
4144 | else | |
4145 | if ! __get_domain_new_authz "$d"; then | |
4146 | _clearup | |
4147 | _on_issue_err "$_post_hook" | |
4148 | return 1 | |
4149 | fi | |
c4d8fd83 | 4150 | fi |
4151 | ||
4c2a3841 | 4152 | if [ -z "$thumbprint" ]; then |
339a8ad6 | 4153 | thumbprint="$(__calc_account_thumbprint)" |
4c3b3608 | 4154 | fi |
4155 | ||
dbc44c08 | 4156 | entry="$(echo "$response" | _egrep_o '[^\{]*"type":"'$vtype'"[^\}]*')" |
4c3b3608 | 4157 | _debug entry "$entry" |
9541ea6a | 4158 | keyauthorization="" |
4c2a3841 | 4159 | if [ -z "$entry" ]; then |
9541ea6a | 4160 | if ! _startswith "$d" '*.'; then |
4161 | _debug "Not a wildcard domain, lets check whether the validation is already valid." | |
4162 | if echo "$response" | grep '"status":"valid"' >/dev/null 2>&1; then | |
4163 | _debug "$d is already valid." | |
4164 | keyauthorization="$STATE_VERIFIED" | |
4165 | _debug keyauthorization "$keyauthorization" | |
4166 | fi | |
4167 | fi | |
4168 | if [ -z "$keyauthorization" ]; then | |
fc3a1817 | 4169 | _err "Error, can not get domain token entry $d for $vtype" |
9541ea6a | 4170 | _supported_vtypes="$(echo "$response" | _egrep_o "\"challenges\":\[[^]]*]" | tr '{' "\n" | grep type | cut -d '"' -f 4 | tr "\n" ' ')" |
4171 | if [ "$_supported_vtypes" ]; then | |
4172 | _err "The supported validation types are: $_supported_vtypes, but you specified: $vtype" | |
4173 | fi | |
4174 | _clearup | |
4175 | _on_issue_err "$_post_hook" | |
4176 | return 1 | |
b51ed9bb | 4177 | fi |
c1151b0d | 4178 | fi |
f8b225e7 | 4179 | |
9541ea6a | 4180 | if [ -z "$keyauthorization" ]; then |
4181 | token="$(echo "$entry" | _egrep_o '"token":"[^"]*' | cut -d : -f 2 | tr -d '"')" | |
4182 | _debug token "$token" | |
cae203be | 4183 | |
9541ea6a | 4184 | if [ -z "$token" ]; then |
4185 | _err "Error, can not get domain token $entry" | |
4186 | _clearup | |
4187 | _on_issue_err "$_post_hook" | |
4188 | return 1 | |
4189 | fi | |
4190 | if [ "$ACME_VERSION" = "2" ]; then | |
4191 | uri="$(echo "$entry" | _egrep_o '"url":"[^"]*' | cut -d '"' -f 4 | _head_n 1)" | |
4192 | else | |
4193 | uri="$(echo "$entry" | _egrep_o '"uri":"[^"]*' | cut -d '"' -f 4)" | |
4194 | fi | |
4195 | _debug uri "$uri" | |
4c3b3608 | 4196 | |
9541ea6a | 4197 | if [ -z "$uri" ]; then |
4198 | _err "Error, can not get domain uri. $entry" | |
4199 | _clearup | |
4200 | _on_issue_err "$_post_hook" | |
4201 | return 1 | |
4202 | fi | |
4203 | keyauthorization="$token.$thumbprint" | |
d35bf517 | 4204 | _debug keyauthorization "$keyauthorization" |
9541ea6a | 4205 | |
4206 | if printf "%s" "$response" | grep '"status":"valid"' >/dev/null 2>&1; then | |
4207 | _debug "$d is already verified." | |
4208 | keyauthorization="$STATE_VERIFIED" | |
4209 | _debug keyauthorization "$keyauthorization" | |
4210 | fi | |
ec603bee | 4211 | fi |
4212 | ||
a63b05a9 | 4213 | dvlist="$d$sep$keyauthorization$sep$uri$sep$vtype$sep$_currentRoot" |
4c3b3608 | 4214 | _debug dvlist "$dvlist" |
4c2a3841 | 4215 | |
9d725af6 | 4216 | vlist="$vlist$dvlist$dvsep" |
4c3b3608 | 4217 | |
4218 | done | |
9d725af6 | 4219 | _debug vlist "$vlist" |
4c3b3608 | 4220 | #add entry |
b5ca9bba | 4221 | dns_entries="" |
4c3b3608 | 4222 | dnsadded="" |
9d725af6 | 4223 | ventries=$(echo "$vlist" | tr "$dvsep" ' ') |
1f7df33e | 4224 | _alias_index=1 |
4c2a3841 | 4225 | for ventry in $ventries; do |
ca7202eb | 4226 | d=$(echo "$ventry" | cut -d "$sep" -f 1) |
4227 | keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2) | |
4228 | vtype=$(echo "$ventry" | cut -d "$sep" -f 4) | |
4229 | _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5) | |
72f54ca6 | 4230 | _debug d "$d" |
4c2a3841 | 4231 | if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then |
69212114 | 4232 | _debug "$d is already verified, skip $vtype." |
fd536d37 | 4233 | _alias_index="$(_math "$_alias_index" + 1)" |
ec603bee | 4234 | continue |
4235 | fi | |
4236 | ||
4c2a3841 | 4237 | if [ "$vtype" = "$VTYPE_DNS" ]; then |
4c3b3608 | 4238 | dnsadded='0' |
72f54ca6 | 4239 | _dns_root_d="$d" |
4240 | if _startswith "$_dns_root_d" "*."; then | |
4241 | _dns_root_d="$(echo "$_dns_root_d" | sed 's/*.//')" | |
4242 | fi | |
875625b1 | 4243 | _d_alias="$(_getfield "$_challenge_alias" "$_alias_index")" |
4244 | _alias_index="$(_math "$_alias_index" + 1)" | |
4245 | _debug "_d_alias" "$_d_alias" | |
4246 | if [ "$_d_alias" ]; then | |
64821ad4 | 4247 | if _startswith "$_d_alias" "$DNS_ALIAS_PREFIX"; then |
4248 | txtdomain="$(echo "$_d_alias" | sed "s/$DNS_ALIAS_PREFIX//")" | |
4249 | else | |
4250 | txtdomain="_acme-challenge.$_d_alias" | |
4251 | fi | |
82b0ebb7 | 4252 | dns_entry="${_dns_root_d}${dvsep}_acme-challenge.$_dns_root_d$dvsep$txtdomain$dvsep$_currentRoot" |
875625b1 | 4253 | else |
4254 | txtdomain="_acme-challenge.$_dns_root_d" | |
82b0ebb7 | 4255 | dns_entry="${_dns_root_d}${dvsep}_acme-challenge.$_dns_root_d$dvsep$dvsep$_currentRoot" |
875625b1 | 4256 | fi |
82b0ebb7 | 4257 | |
4c3b3608 | 4258 | _debug txtdomain "$txtdomain" |
11927a76 | 4259 | txt="$(printf "%s" "$keyauthorization" | _digest "sha256" | _url_replace)" |
4c3b3608 | 4260 | _debug txt "$txt" |
a61fe418 | 4261 | |
b50e701c | 4262 | d_api="$(_findHook "$_dns_root_d" $_SUB_FOLDER_DNSAPI "$_currentRoot")" |
4c3b3608 | 4263 | _debug d_api "$d_api" |
82b0ebb7 | 4264 | |
4265 | dns_entry="$dns_entry$dvsep$txt${dvsep}$d_api" | |
4266 | _debug2 dns_entry "$dns_entry" | |
4c2a3841 | 4267 | if [ "$d_api" ]; then |
a180b95c | 4268 | _debug "Found domain api file: $d_api" |
4c3b3608 | 4269 | else |
3881f221 | 4270 | if [ "$_currentRoot" != "$W_DNS" ]; then |
4271 | _err "Can not find dns api hook for: $_currentRoot" | |
4272 | _info "You need to add the txt record manually." | |
4273 | fi | |
5f8b60a0 | 4274 | _info "$(__red "Add the following TXT record:")" |
81772fb7 | 4275 | _info "$(__red "Domain: '$(__green "$txtdomain")'")" |
4276 | _info "$(__red "TXT value: '$(__green "$txt")'")" | |
4277 | _info "$(__red "Please be aware that you prepend _acme-challenge. before your domain")" | |
4278 | _info "$(__red "so the resulting subdomain will be: $txtdomain")" | |
4c3b3608 | 4279 | continue |
4280 | fi | |
4c2a3841 | 4281 | |
73b8b120 | 4282 | ( |
ca7202eb | 4283 | if ! . "$d_api"; then |
73b8b120 | 4284 | _err "Load file $d_api error. Please check your api file and try again." |
4285 | return 1 | |
4286 | fi | |
4c2a3841 | 4287 | |
158f22f7 | 4288 | addcommand="${_currentRoot}_add" |
ca7202eb | 4289 | if ! _exists "$addcommand"; then |
73b8b120 | 4290 | _err "It seems that your api file is not correct, it must have a function named: $addcommand" |
4291 | return 1 | |
4292 | fi | |
a180b95c | 4293 | _info "Adding txt value: $txt for domain: $txtdomain" |
ca7202eb | 4294 | if ! $addcommand "$txtdomain" "$txt"; then |
73b8b120 | 4295 | _err "Error add txt for domain:$txtdomain" |
4296 | return 1 | |
4297 | fi | |
a180b95c | 4298 | _info "The txt record is added: Success." |
73b8b120 | 4299 | ) |
4c2a3841 | 4300 | |
4301 | if [ "$?" != "0" ]; then | |
ea722da3 | 4302 | _on_issue_err "$_post_hook" "$vlist" |
545f2355 | 4303 | _clearup |
4c3b3608 | 4304 | return 1 |
4305 | fi | |
82b0ebb7 | 4306 | dns_entries="$dns_entries$dns_entry |
4307 | " | |
4308 | _debug2 "$dns_entries" | |
4c3b3608 | 4309 | dnsadded='1' |
4310 | fi | |
4311 | done | |
4312 | ||
4c2a3841 | 4313 | if [ "$dnsadded" = '0' ]; then |
4314 | _savedomainconf "Le_Vlist" "$vlist" | |
4c3b3608 | 4315 | _debug "Dns record not added yet, so, save to $DOMAIN_CONF and exit." |
a8b62261 | 4316 | _err "Please add the TXT records to the domains, and re-run with --renew." |
5f8b60a0 | 4317 | _on_issue_err "$_post_hook" |
545f2355 | 4318 | _clearup |
4c3b3608 | 4319 | return 1 |
4320 | fi | |
4c2a3841 | 4321 | |
4c3b3608 | 4322 | fi |
4c2a3841 | 4323 | |
b5ca9bba | 4324 | if [ "$dns_entries" ]; then |
4c2a3841 | 4325 | if [ -z "$Le_DNSSleep" ]; then |
427c2780 | 4326 | _info "Let's check each DNS record now. Sleep 20 seconds first." |
b5ca9bba | 4327 | _sleep 20 |
4328 | if ! _check_dns_entries; then | |
4329 | _err "check dns error." | |
4330 | _on_issue_err "$_post_hook" | |
4331 | _clearup | |
4332 | return 1 | |
4333 | fi | |
0e38c60d | 4334 | else |
4c2a3841 | 4335 | _savedomainconf "Le_DNSSleep" "$Le_DNSSleep" |
b5ca9bba | 4336 | _info "Sleep $(__green $Le_DNSSleep) seconds for the txt records to take effect" |
4337 | _sleep "$Le_DNSSleep" | |
0e38c60d | 4338 | fi |
4c3b3608 | 4339 | fi |
4c2a3841 | 4340 | |
5d943a35 | 4341 | NGINX_RESTORE_VLIST="" |
4c3b3608 | 4342 | _debug "ok, let's start to verify" |
a63b05a9 | 4343 | |
0463b5d6 | 4344 | _ncIndex=1 |
9d725af6 | 4345 | ventries=$(echo "$vlist" | tr "$dvsep" ' ') |
4c2a3841 | 4346 | for ventry in $ventries; do |
ca7202eb | 4347 | d=$(echo "$ventry" | cut -d "$sep" -f 1) |
4348 | keyauthorization=$(echo "$ventry" | cut -d "$sep" -f 2) | |
4349 | uri=$(echo "$ventry" | cut -d "$sep" -f 3) | |
4350 | vtype=$(echo "$ventry" | cut -d "$sep" -f 4) | |
4351 | _currentRoot=$(echo "$ventry" | cut -d "$sep" -f 5) | |
ec603bee | 4352 | |
4c2a3841 | 4353 | if [ "$keyauthorization" = "$STATE_VERIFIED" ]; then |
ec603bee | 4354 | _info "$d is already verified, skip $vtype." |
4355 | continue | |
4356 | fi | |
4357 | ||
dd068467 | 4358 | _info "Verifying: $d" |
4c3b3608 | 4359 | _debug "d" "$d" |
4360 | _debug "keyauthorization" "$keyauthorization" | |
4361 | _debug "uri" "$uri" | |
4362 | removelevel="" | |
e22bcf7c | 4363 | token="$(printf "%s" "$keyauthorization" | cut -d '.' -f 1)" |
a63b05a9 | 4364 | |
4365 | _debug "_currentRoot" "$_currentRoot" | |
4366 | ||
4c2a3841 | 4367 | if [ "$vtype" = "$VTYPE_HTTP" ]; then |
4368 | if [ "$_currentRoot" = "$NO_VALUE" ]; then | |
4c3b3608 | 4369 | _info "Standalone mode server" |
85e1f4ea | 4370 | _ncaddr="$(_getfield "$_local_addr" "$_ncIndex")" |
0463b5d6 | 4371 | _ncIndex="$(_math $_ncIndex + 1)" |
3794b5cb | 4372 | _startserver "$keyauthorization" "$_ncaddr" |
4c2a3841 | 4373 | if [ "$?" != "0" ]; then |
5ef501c5 | 4374 | _clearup |
58e4d337 | 4375 | _on_issue_err "$_post_hook" "$vlist" |
6fc1447f | 4376 | return 1 |
4377 | fi | |
5dbf664a | 4378 | sleep 1 |
ca7202eb | 4379 | _debug serverproc "$serverproc" |
0e44f587 | 4380 | elif [ "$_currentRoot" = "$MODE_STATELESS" ]; then |
4381 | _info "Stateless mode for domain:$d" | |
4382 | _sleep 1 | |
9d725af6 | 4383 | elif _startswith "$_currentRoot" "$NGINX"; then |
4384 | _info "Nginx mode for domain:$d" | |
4385 | #set up nginx server | |
4386 | FOUND_REAL_NGINX_CONF="" | |
4387 | BACKUP_NGINX_CONF="" | |
4388 | if ! _setNginx "$d" "$_currentRoot" "$thumbprint"; then | |
4389 | _clearup | |
58e4d337 | 4390 | _on_issue_err "$_post_hook" "$vlist" |
9d725af6 | 4391 | return 1 |
03f8d6e9 | 4392 | fi |
302c41ed | 4393 | |
03f8d6e9 | 4394 | if [ "$FOUND_REAL_NGINX_CONF" ]; then |
9d725af6 | 4395 | _realConf="$FOUND_REAL_NGINX_CONF" |
4396 | _backup="$BACKUP_NGINX_CONF" | |
4397 | _debug _realConf "$_realConf" | |
5d943a35 | 4398 | NGINX_RESTORE_VLIST="$d$sep$_realConf$sep$_backup$dvsep$NGINX_RESTORE_VLIST" |
9d725af6 | 4399 | fi |
4400 | _sleep 1 | |
4c3b3608 | 4401 | else |
4c2a3841 | 4402 | if [ "$_currentRoot" = "apache" ]; then |
6f930641 | 4403 | wellknown_path="$ACME_DIR" |
4404 | else | |
a63b05a9 | 4405 | wellknown_path="$_currentRoot/.well-known/acme-challenge" |
4c2a3841 | 4406 | if [ ! -d "$_currentRoot/.well-known" ]; then |
6f930641 | 4407 | removelevel='1' |
4c2a3841 | 4408 | elif [ ! -d "$_currentRoot/.well-known/acme-challenge" ]; then |
6f930641 | 4409 | removelevel='2' |
4410 | else | |
4411 | removelevel='3' | |
4412 | fi | |
4c3b3608 | 4413 | fi |
6f930641 | 4414 | |
4c3b3608 | 4415 | _debug wellknown_path "$wellknown_path" |
6f930641 | 4416 | |
4c3b3608 | 4417 | _debug "writing token:$token to $wellknown_path/$token" |
4418 | ||
4419 | mkdir -p "$wellknown_path" | |
93fc48a2 | 4420 | |
4c2a3841 | 4421 | if ! printf "%s" "$keyauthorization" >"$wellknown_path/$token"; then |
93fc48a2 | 4422 | _err "$d:Can not write token to file : $wellknown_path/$token" |
4423 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" | |
4424 | _clearup | |
58e4d337 | 4425 | _on_issue_err "$_post_hook" "$vlist" |
93fc48a2 | 4426 | return 1 |
4427 | fi | |
4428 | ||
4c2a3841 | 4429 | if [ ! "$usingApache" ]; then |
44edb2bd | 4430 | if webroot_owner=$(_stat "$_currentRoot"); then |
32fdc196 | 4431 | _debug "Changing owner/group of .well-known to $webroot_owner" |
c87cd0de | 4432 | if ! _exec "chown -R \"$webroot_owner\" \"$_currentRoot/.well-known\""; then |
4433 | _debug "$(cat "$_EXEC_TEMP_ERR")" | |
4434 | _exec_err >/dev/null 2>&1 | |
4435 | fi | |
32fdc196 | 4436 | else |
b54ce310 | 4437 | _debug "not changing owner/group of webroot" |
32fdc196 | 4438 | fi |
df886ffa | 4439 | fi |
4c2a3841 | 4440 | |
4c3b3608 | 4441 | fi |
08681f4a | 4442 | elif [ "$vtype" = "$VTYPE_ALPN" ]; then |
4443 | acmevalidationv1="$(printf "%s" "$keyauthorization" | _digest "sha256" "hex")" | |
4444 | _debug acmevalidationv1 "$acmevalidationv1" | |
4445 | if ! _starttlsserver "$d" "" "$Le_TLSPort" "$keyauthorization" "$_ncaddr" "$acmevalidationv1"; then | |
4446 | _err "Start tls server error." | |
4447 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" | |
4448 | _clearup | |
4449 | _on_issue_err "$_post_hook" "$vlist" | |
4450 | return 1 | |
4451 | fi | |
4c3b3608 | 4452 | fi |
4c2a3841 | 4453 | |
920cab6f | 4454 | if ! __trigger_validation "$uri" "$keyauthorization" "$vtype"; then |
c4d8fd83 | 4455 | _err "$d:Can not get challenge: $response" |
4456 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" | |
4457 | _clearup | |
58e4d337 | 4458 | _on_issue_err "$_post_hook" "$vlist" |
c4d8fd83 | 4459 | return 1 |
4460 | fi | |
4c2a3841 | 4461 | |
c1151b0d | 4462 | if [ "$code" ] && [ "$code" != '202' ]; then |
8bd12ed0 | 4463 | if [ "$code" = '200' ]; then |
c1151b0d | 4464 | _debug "trigger validation code: $code" |
4465 | else | |
8bd12ed0 K |
4466 | _err "$d:Challenge error: $response" |
4467 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" | |
4468 | _clearup | |
4469 | _on_issue_err "$_post_hook" "$vlist" | |
4470 | return 1 | |
c1151b0d | 4471 | fi |
4c3b3608 | 4472 | fi |
4c2a3841 | 4473 | |
6fc1447f | 4474 | waittimes=0 |
4c2a3841 | 4475 | if [ -z "$MAX_RETRY_TIMES" ]; then |
6fc1447f | 4476 | MAX_RETRY_TIMES=30 |
4477 | fi | |
4c2a3841 | 4478 | |
4479 | while true; do | |
0c538f75 | 4480 | waittimes=$(_math "$waittimes" + 1) |
4c2a3841 | 4481 | if [ "$waittimes" -ge "$MAX_RETRY_TIMES" ]; then |
6fc1447f | 4482 | _err "$d:Timeout" |
4483 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" | |
4484 | _clearup | |
58e4d337 | 4485 | _on_issue_err "$_post_hook" "$vlist" |
6fc1447f | 4486 | return 1 |
4487 | fi | |
4c2a3841 | 4488 | |
5dbf664a | 4489 | _debug "sleep 2 secs to verify" |
4490 | sleep 2 | |
4c3b3608 | 4491 | _debug "checking" |
0483d841 | 4492 | if [ "$ACME_VERSION" = "2" ]; then |
4493 | _send_signed_request "$uri" | |
4494 | else | |
4495 | response="$(_get "$uri")" | |
4496 | fi | |
4c2a3841 | 4497 | if [ "$?" != "0" ]; then |
c60883ef | 4498 | _err "$d:Verify error:$response" |
a63b05a9 | 4499 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" |
4c3b3608 | 4500 | _clearup |
58e4d337 | 4501 | _on_issue_err "$_post_hook" "$vlist" |
4c3b3608 | 4502 | return 1 |
4503 | fi | |
9aaf36cd | 4504 | _debug2 original "$response" |
4c2a3841 | 4505 | |
4506 | response="$(echo "$response" | _normalizeJson)" | |
7012b91f | 4507 | _debug2 response "$response" |
4c2a3841 | 4508 | |
4509 | status=$(echo "$response" | _egrep_o '"status":"[^"]*' | cut -d : -f 2 | tr -d '"') | |
4510 | if [ "$status" = "valid" ]; then | |
93f3098a | 4511 | _info "$(__green Success)" |
ca7202eb | 4512 | _stopserver "$serverproc" |
4c3b3608 | 4513 | serverproc="" |
a63b05a9 | 4514 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" |
4c2a3841 | 4515 | break |
4c3b3608 | 4516 | fi |
4c2a3841 | 4517 | |
4518 | if [ "$status" = "invalid" ]; then | |
d0d74907 | 4519 | error="$(echo "$response" | _egrep_o '"error":\{[^\}]*')" |
4c2a3841 | 4520 | _debug2 error "$error" |
4521 | errordetail="$(echo "$error" | _egrep_o '"detail": *"[^"]*' | cut -d '"' -f 4)" | |
4522 | _debug2 errordetail "$errordetail" | |
4523 | if [ "$errordetail" ]; then | |
4524 | _err "$d:Verify error:$errordetail" | |
4525 | else | |
4526 | _err "$d:Verify error:$error" | |
4527 | fi | |
4528 | if [ "$DEBUG" ]; then | |
4529 | if [ "$vtype" = "$VTYPE_HTTP" ]; then | |
4530 | _debug "Debug: get token url." | |
4531 | _get "http://$d/.well-known/acme-challenge/$token" "" 1 | |
4532 | fi | |
4533 | fi | |
a63b05a9 | 4534 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" |
4c3b3608 | 4535 | _clearup |
58e4d337 | 4536 | _on_issue_err "$_post_hook" "$vlist" |
4c2a3841 | 4537 | return 1 |
4c3b3608 | 4538 | fi |
4c2a3841 | 4539 | |
4540 | if [ "$status" = "pending" ]; then | |
4c3b3608 | 4541 | _info "Pending" |
93740c99 | 4542 | elif [ "$status" = "processing" ]; then |
4543 | _info "Processing" | |
4c3b3608 | 4544 | else |
4c2a3841 | 4545 | _err "$d:Verify error:$response" |
a63b05a9 | 4546 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" |
4c3b3608 | 4547 | _clearup |
58e4d337 | 4548 | _on_issue_err "$_post_hook" "$vlist" |
4c3b3608 | 4549 | return 1 |
4550 | fi | |
4c2a3841 | 4551 | |
4c3b3608 | 4552 | done |
4c2a3841 | 4553 | |
4c3b3608 | 4554 | done |
4555 | ||
4556 | _clearup | |
4557 | _info "Verify finished, start to sign." | |
11927a76 | 4558 | der="$(_getfile "${CSR_PATH}" "${BEGIN_CSR}" "${END_CSR}" | tr -d "\r\n" | _url_replace)" |
4c2a3841 | 4559 | |
c1151b0d | 4560 | if [ "$ACME_VERSION" = "2" ]; then |
e7f7e96d | 4561 | _info "Lets finalize the order, Le_OrderFinalize: $Le_OrderFinalize" |
d2cde379 | 4562 | if ! _send_signed_request "${Le_OrderFinalize}" "{\"csr\": \"$der\"}"; then |
c1151b0d | 4563 | _err "Sign failed." |
4564 | _on_issue_err "$_post_hook" | |
4565 | return 1 | |
4566 | fi | |
4567 | if [ "$code" != "200" ]; then | |
e7f7e96d | 4568 | _err "Sign failed, finalize code is not 200." |
668c43ab | 4569 | _err "$response" |
c1151b0d | 4570 | _on_issue_err "$_post_hook" |
4571 | return 1 | |
4572 | fi | |
e7f7e96d | 4573 | if [ -z "$Le_LinkOrder" ]; then |
0712e989 | 4574 | Le_LinkOrder="$(echo "$responseHeaders" | grep -i '^Location.*$' | _tail_n 1 | tr -d "\r\n" | cut -d ":" -f 2-)" |
e7f7e96d | 4575 | fi |
fbdc5a0e | 4576 | |
e7f7e96d | 4577 | _savedomainconf "Le_LinkOrder" "$Le_LinkOrder" |
4578 | ||
4579 | _link_cert_retry=0 | |
ff9be30f | 4580 | _MAX_CERT_RETRY=30 |
a3d8b993 | 4581 | while [ "$_link_cert_retry" -lt "$_MAX_CERT_RETRY" ]; do |
e7f7e96d | 4582 | if _contains "$response" "\"status\":\"valid\""; then |
4583 | _debug "Order status is valid." | |
d0d74907 | 4584 | Le_LinkCert="$(echo "$response" | _egrep_o '"certificate" *: *"[^"]*"' | cut -d '"' -f 4)" |
e7f7e96d | 4585 | _debug Le_LinkCert "$Le_LinkCert" |
4586 | if [ -z "$Le_LinkCert" ]; then | |
4587 | _err "Sign error, can not find Le_LinkCert" | |
4588 | _err "$response" | |
4589 | _on_issue_err "$_post_hook" | |
4590 | return 1 | |
4591 | fi | |
4592 | break | |
4593 | elif _contains "$response" "\"processing\""; then | |
4594 | _info "Order status is processing, lets sleep and retry." | |
1fe8235a | 4595 | _retryafter=$(echo "$responseHeaders" | grep -i "^Retry-After *:" | cut -d : -f 2 | tr -d ' ' | tr -d '\r') |
4596 | _debug "_retryafter" "$_retryafter" | |
4597 | if [ "$_retryafter" ]; then | |
4598 | _info "Retry after: $_retryafter" | |
4599 | _sleep $_retryafter | |
4600 | else | |
4601 | _sleep 2 | |
4602 | fi | |
e7f7e96d | 4603 | else |
4604 | _err "Sign error, wrong status" | |
4605 | _err "$response" | |
4606 | _on_issue_err "$_post_hook" | |
4607 | return 1 | |
4608 | fi | |
fbdc5a0e | 4609 | #the order is processing, so we are going to poll order status |
4610 | if [ -z "$Le_LinkOrder" ]; then | |
4611 | _err "Sign error, can not get order link location header" | |
4612 | _err "responseHeaders" "$responseHeaders" | |
4613 | _on_issue_err "$_post_hook" | |
4614 | return 1 | |
4615 | fi | |
4616 | _info "Polling order status: $Le_LinkOrder" | |
e7f7e96d | 4617 | if ! _send_signed_request "$Le_LinkOrder"; then |
4618 | _err "Sign failed, can not post to Le_LinkOrder cert:$Le_LinkOrder." | |
4619 | _err "$response" | |
4620 | _on_issue_err "$_post_hook" | |
4621 | return 1 | |
4622 | fi | |
4623 | _link_cert_retry="$(_math $_link_cert_retry + 1)" | |
4624 | done | |
4c2a3841 | 4625 | |
e7f7e96d | 4626 | if [ -z "$Le_LinkCert" ]; then |
4627 | _err "Sign failed, can not get Le_LinkCert, retry time limit." | |
4628 | _err "$response" | |
4629 | _on_issue_err "$_post_hook" | |
4630 | return 1 | |
4631 | fi | |
4632 | _info "Download cert, Le_LinkCert: $Le_LinkCert" | |
f2acdd27 | 4633 | if ! _send_signed_request "$Le_LinkCert"; then |
668c43ab | 4634 | _err "Sign failed, can not download cert:$Le_LinkCert." |
4635 | _err "$response" | |
c1151b0d | 4636 | _on_issue_err "$_post_hook" |
4637 | return 1 | |
4638 | fi | |
4c3b3608 | 4639 | |
f2acdd27 | 4640 | echo "$response" >"$CERT_PATH" |
0483d841 | 4641 | |
1c35f46b | 4642 | if [ "$(grep -- "$BEGIN_CERT" "$CERT_PATH" | wc -l)" -gt "1" ]; then |
4643 | _debug "Found cert chain" | |
120cde16 | 4644 | cat "$CERT_PATH" >"$CERT_FULLCHAIN_PATH" |
1c35f46b | 4645 | _end_n="$(grep -n -- "$END_CERT" "$CERT_FULLCHAIN_PATH" | _head_n 1 | cut -d : -f 1)" |
4646 | _debug _end_n "$_end_n" | |
120cde16 | 4647 | sed -n "1,${_end_n}p" "$CERT_FULLCHAIN_PATH" >"$CERT_PATH" |
1c35f46b | 4648 | _end_n="$(_math $_end_n + 1)" |
120cde16 | 4649 | sed -n "${_end_n},9999p" "$CERT_FULLCHAIN_PATH" >"$CA_CERT_PATH" |
1c35f46b | 4650 | fi |
e7f7e96d | 4651 | |
c1151b0d | 4652 | else |
4653 | if ! _send_signed_request "${ACME_NEW_ORDER}" "{\"resource\": \"$ACME_NEW_ORDER_RES\", \"csr\": \"$der\"}" "needbase64"; then | |
323febe8 | 4654 | _err "Sign failed. $response" |
c1151b0d | 4655 | _on_issue_err "$_post_hook" |
4656 | return 1 | |
4657 | fi | |
4658 | _rcert="$response" | |
36a7a840 | 4659 | Le_LinkCert="$(grep -i '^Location.*$' "$HTTP_HEADER" | _tail_n 1 | tr -d "\r\n" | cut -d " " -f 2)" |
4c2a3841 | 4660 | echo "$BEGIN_CERT" >"$CERT_PATH" |
4c3b3608 | 4661 | |
72518d48 | 4662 | #if ! _get "$Le_LinkCert" | _base64 "multiline" >> "$CERT_PATH" ; then |
4663 | # _debug "Get cert failed. Let's try last response." | |
3c07f57a | 4664 | # printf -- "%s" "$_rcert" | _dbase64 "multiline" | _base64 "multiline" >> "$CERT_PATH" |
72518d48 | 4665 | #fi |
4c2a3841 | 4666 | |
4667 | if ! printf -- "%s" "$_rcert" | _dbase64 "multiline" | _base64 "multiline" >>"$CERT_PATH"; then | |
72518d48 | 4668 | _debug "Try cert link." |
4c2a3841 | 4669 | _get "$Le_LinkCert" | _base64 "multiline" >>"$CERT_PATH" |
d404e92d | 4670 | fi |
4671 | ||
4c2a3841 | 4672 | echo "$END_CERT" >>"$CERT_PATH" |
c1151b0d | 4673 | fi |
4674 | ||
4675 | _debug "Le_LinkCert" "$Le_LinkCert" | |
4676 | _savedomainconf "Le_LinkCert" "$Le_LinkCert" | |
4677 | ||
183063a2 | 4678 | if [ -z "$Le_LinkCert" ] || ! _checkcert "$CERT_PATH"; then |
716f7277 | 4679 | response="$(echo "$response" | _dbase64 "multiline" | tr -d '\0' | _normalizeJson)" |
183063a2 | 4680 | _err "Sign failed: $(echo "$response" | _egrep_o '"detail":"[^"]*"')" |
4681 | _on_issue_err "$_post_hook" | |
4682 | return 1 | |
4683 | fi | |
4684 | ||
c1151b0d | 4685 | if [ "$Le_LinkCert" ]; then |
43822d37 | 4686 | _info "$(__green "Cert success.")" |
4c3b3608 | 4687 | cat "$CERT_PATH" |
5980ebc7 | 4688 | |
4c2a3841 | 4689 | _info "Your cert is in $(__green " $CERT_PATH ")" |
4690 | ||
4691 | if [ -f "$CERT_KEY_PATH" ]; then | |
4692 | _info "Your cert key is in $(__green " $CERT_KEY_PATH ")" | |
5980ebc7 | 4693 | fi |
4694 | ||
c6b68551 | 4695 | if [ ! "$USER_PATH" ] || [ ! "$ACME_IN_CRON" ]; then |
281aa349 | 4696 | USER_PATH="$PATH" |
4697 | _saveaccountconf "USER_PATH" "$USER_PATH" | |
4698 | fi | |
4c3b3608 | 4699 | fi |
4c3b3608 | 4700 | |
1c35f46b | 4701 | if [ "$ACME_VERSION" = "2" ]; then |
4702 | _debug "v2 chain." | |
4703 | else | |
183063a2 | 4704 | cp "$CERT_PATH" "$CERT_FULLCHAIN_PATH" |
1c35f46b | 4705 | Le_LinkIssuer=$(grep -i '^Link' "$HTTP_HEADER" | _head_n 1 | cut -d " " -f 2 | cut -d ';' -f 1 | tr -d '<>') |
d8ba26e6 | 4706 | |
1c35f46b | 4707 | if [ "$Le_LinkIssuer" ]; then |
4708 | if ! _contains "$Le_LinkIssuer" ":"; then | |
4709 | _info "$(__red "Relative issuer link found.")" | |
4710 | Le_LinkIssuer="$_ACME_SERVER_HOST$Le_LinkIssuer" | |
d8ba26e6 | 4711 | fi |
1c35f46b | 4712 | _debug Le_LinkIssuer "$Le_LinkIssuer" |
4713 | _savedomainconf "Le_LinkIssuer" "$Le_LinkIssuer" | |
d8ba26e6 | 4714 | |
1c35f46b | 4715 | _link_issuer_retry=0 |
4716 | _MAX_ISSUER_RETRY=5 | |
4717 | while [ "$_link_issuer_retry" -lt "$_MAX_ISSUER_RETRY" ]; do | |
4718 | _debug _link_issuer_retry "$_link_issuer_retry" | |
4719 | if [ "$ACME_VERSION" = "2" ]; then | |
0483d841 | 4720 | if _send_signed_request "$Le_LinkIssuer"; then |
4721 | echo "$response" >"$CA_CERT_PATH" | |
1c35f46b | 4722 | break |
4723 | fi | |
4724 | else | |
4725 | if _get "$Le_LinkIssuer" >"$CA_CERT_PATH.der"; then | |
4726 | echo "$BEGIN_CERT" >"$CA_CERT_PATH" | |
4727 | _base64 "multiline" <"$CA_CERT_PATH.der" >>"$CA_CERT_PATH" | |
4728 | echo "$END_CERT" >>"$CA_CERT_PATH" | |
0f120c41 | 4729 | if ! _checkcert "$CA_CERT_PATH"; then |
183063a2 | 4730 | _err "Can not get the ca cert." |
4731 | break | |
4732 | fi | |
1c35f46b | 4733 | cat "$CA_CERT_PATH" >>"$CERT_FULLCHAIN_PATH" |
4734 | rm -f "$CA_CERT_PATH.der" | |
4735 | break | |
4736 | fi | |
c1151b0d | 4737 | fi |
1c35f46b | 4738 | _link_issuer_retry=$(_math $_link_issuer_retry + 1) |
4739 | _sleep "$_link_issuer_retry" | |
4740 | done | |
4741 | if [ "$_link_issuer_retry" = "$_MAX_ISSUER_RETRY" ]; then | |
4742 | _err "Max retry for issuer ca cert is reached." | |
d8ba26e6 | 4743 | fi |
1c35f46b | 4744 | else |
4745 | _debug "No Le_LinkIssuer header found." | |
d8ba26e6 | 4746 | fi |
4c3b3608 | 4747 | fi |
1c35f46b | 4748 | [ -f "$CA_CERT_PATH" ] && _info "The intermediate CA cert is in $(__green " $CA_CERT_PATH ")" |
4749 | [ -f "$CERT_FULLCHAIN_PATH" ] && _info "And the full chain certs is there: $(__green " $CERT_FULLCHAIN_PATH ")" | |
4c2a3841 | 4750 | |
3aae1ae3 | 4751 | Le_CertCreateTime=$(_time) |
4c2a3841 | 4752 | _savedomainconf "Le_CertCreateTime" "$Le_CertCreateTime" |
4753 | ||
4754 | Le_CertCreateTimeStr=$(date -u) | |
4755 | _savedomainconf "Le_CertCreateTimeStr" "$Le_CertCreateTimeStr" | |
4756 | ||
ec67a1b2 | 4757 | if [ -z "$Le_RenewalDays" ] || [ "$Le_RenewalDays" -lt "0" ]; then |
4758 | Le_RenewalDays="$DEFAULT_RENEW" | |
054cb72e | 4759 | else |
4c2a3841 | 4760 | _savedomainconf "Le_RenewalDays" "$Le_RenewalDays" |
13d7cae9 | 4761 | fi |
4c2a3841 | 4762 | |
4763 | if [ "$CA_BUNDLE" ]; then | |
78009539 PS |
4764 | _saveaccountconf CA_BUNDLE "$CA_BUNDLE" |
4765 | else | |
4766 | _clearaccountconf "CA_BUNDLE" | |
4767 | fi | |
4768 | ||
2aa75f03 | 4769 | if [ "$CA_PATH" ]; then |
4770 | _saveaccountconf CA_PATH "$CA_PATH" | |
4771 | else | |
4772 | _clearaccountconf "CA_PATH" | |
4773 | fi | |
78009539 | 4774 | |
4c2a3841 | 4775 | if [ "$HTTPS_INSECURE" ]; then |
fac1e367 | 4776 | _saveaccountconf HTTPS_INSECURE "$HTTPS_INSECURE" |
4777 | else | |
4c2a3841 | 4778 | _clearaccountconf "HTTPS_INSECURE" |
13d7cae9 | 4779 | fi |
00a50605 | 4780 | |
4c2a3841 | 4781 | if [ "$Le_Listen_V4" ]; then |
4782 | _savedomainconf "Le_Listen_V4" "$Le_Listen_V4" | |
50827188 | 4783 | _cleardomainconf Le_Listen_V6 |
4c2a3841 | 4784 | elif [ "$Le_Listen_V6" ]; then |
4785 | _savedomainconf "Le_Listen_V6" "$Le_Listen_V6" | |
50827188 | 4786 | _cleardomainconf Le_Listen_V4 |
4787 | fi | |
f6dcd989 | 4788 | |
c4b2e582 | 4789 | if [ "$Le_ForceNewDomainKey" = "1" ]; then |
4790 | _savedomainconf "Le_ForceNewDomainKey" "$Le_ForceNewDomainKey" | |
4791 | else | |
4792 | _cleardomainconf Le_ForceNewDomainKey | |
4793 | fi | |
4794 | ||
ca7202eb | 4795 | Le_NextRenewTime=$(_math "$Le_CertCreateTime" + "$Le_RenewalDays" \* 24 \* 60 \* 60) |
4c2a3841 | 4796 | |
ca7202eb | 4797 | Le_NextRenewTimeStr=$(_time2str "$Le_NextRenewTime") |
4c2a3841 | 4798 | _savedomainconf "Le_NextRenewTimeStr" "$Le_NextRenewTimeStr" |
4799 | ||
ca7202eb | 4800 | Le_NextRenewTime=$(_math "$Le_NextRenewTime" - 86400) |
4c2a3841 | 4801 | _savedomainconf "Le_NextRenewTime" "$Le_NextRenewTime" |
f6dcd989 | 4802 | |
85e1f4ea | 4803 | if [ "$_real_cert$_real_key$_real_ca$_reload_cmd$_real_fullchain" ]; then |
4804 | _savedomainconf "Le_RealCertPath" "$_real_cert" | |
4805 | _savedomainconf "Le_RealCACertPath" "$_real_ca" | |
4806 | _savedomainconf "Le_RealKeyPath" "$_real_key" | |
7690f73e | 4807 | _savedomainconf "Le_ReloadCmd" "$_reload_cmd" "base64" |
85e1f4ea | 4808 | _savedomainconf "Le_RealFullChainPath" "$_real_fullchain" |
ce8dca7a | 4809 | if ! _installcert "$_main_domain" "$_real_cert" "$_real_key" "$_real_ca" "$_real_fullchain" "$_reload_cmd"; then |
4810 | return 1 | |
4811 | fi | |
01f54558 | 4812 | fi |
4c0d3f1b | 4813 | |
ce8dca7a | 4814 | if ! _on_issue_success "$_post_hook" "$_renew_hook"; then |
4815 | _err "Call hook error." | |
4816 | return 1 | |
4817 | fi | |
4c3b3608 | 4818 | } |
4819 | ||
43822d37 | 4820 | #domain [isEcc] |
4c3b3608 | 4821 | renew() { |
4822 | Le_Domain="$1" | |
4c2a3841 | 4823 | if [ -z "$Le_Domain" ]; then |
43822d37 | 4824 | _usage "Usage: $PROJECT_ENTRY --renew -d domain.com [--ecc]" |
4c3b3608 | 4825 | return 1 |
4826 | fi | |
4827 | ||
43822d37 | 4828 | _isEcc="$2" |
4829 | ||
e799ef29 | 4830 | _initpath "$Le_Domain" "$_isEcc" |
43822d37 | 4831 | |
e2053b22 | 4832 | _info "$(__green "Renew: '$Le_Domain'")" |
4c2a3841 | 4833 | if [ ! -f "$DOMAIN_CONF" ]; then |
43822d37 | 4834 | _info "'$Le_Domain' is not a issued domain, skip." |
acae0ac2 | 4835 | return $RENEW_SKIP |
4c3b3608 | 4836 | fi |
4c2a3841 | 4837 | |
4838 | if [ "$Le_RenewalDays" ]; then | |
1e6b68f5 | 4839 | _savedomainconf Le_RenewalDays "$Le_RenewalDays" |
4840 | fi | |
4841 | ||
8663fb7e | 4842 | . "$DOMAIN_CONF" |
c5f1cca3 | 4843 | _debug Le_API "$Le_API" |
f2add8de | 4844 | |
4845 | if [ "$Le_API" = "$LETSENCRYPT_CA_V1" ]; then | |
4846 | _cleardomainconf Le_API | |
4847 | Le_API="$DEFAULT_CA" | |
4848 | fi | |
4849 | if [ "$Le_API" = "$LETSENCRYPT_STAGING_CA_V1" ]; then | |
4850 | _cleardomainconf Le_API | |
4851 | Le_API="$DEFAULT_STAGING_CA" | |
4852 | fi | |
4853 | ||
4c2a3841 | 4854 | if [ "$Le_API" ]; then |
48d9a8c1 | 4855 | if [ "$_OLD_CA_HOST" = "$Le_API" ]; then |
4856 | export Le_API="$DEFAULT_CA" | |
4857 | _savedomainconf Le_API "$Le_API" | |
4858 | fi | |
4a2ac7bd | 4859 | if [ "$_OLD_STAGE_CA_HOST" = "$Le_API" ]; then |
c1151b0d | 4860 | export Le_API="$DEFAULT_STAGING_CA" |
4a2ac7bd | 4861 | _savedomainconf Le_API "$Le_API" |
4862 | fi | |
48d9a8c1 | 4863 | export ACME_DIRECTORY="$Le_API" |
c4236e58 | 4864 | #reload ca configs |
4865 | ACCOUNT_KEY_PATH="" | |
4866 | ACCOUNT_JSON_PATH="" | |
4867 | CA_CONF="" | |
4868 | _debug3 "initpath again." | |
4869 | _initpath "$Le_Domain" "$_isEcc" | |
5c48e139 | 4870 | fi |
4c2a3841 | 4871 | |
4872 | if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(_time)" -lt "$Le_NextRenewTime" ]; then | |
e2053b22 | 4873 | _info "Skip, Next renewal time is: $(__green "$Le_NextRenewTimeStr")" |
4874 | _info "Add '$(__red '--force')' to force to renew." | |
e799ef29 | 4875 | return "$RENEW_SKIP" |
4c3b3608 | 4876 | fi |
4c2a3841 | 4877 | |
c6b68551 | 4878 | if [ "$ACME_IN_CRON" = "1" ] && [ -z "$Le_CertCreateTime" ]; then |
c4d0aec5 | 4879 | _info "Skip invalid cert for: $Le_Domain" |
acae0ac2 | 4880 | return $RENEW_SKIP |
c4d0aec5 | 4881 | fi |
4882 | ||
4c3b3608 | 4883 | IS_RENEW="1" |
7690f73e | 4884 | Le_ReloadCmd="$(_readdomainconf Le_ReloadCmd)" |
c7257bcf | 4885 | Le_PreHook="$(_readdomainconf Le_PreHook)" |
4886 | Le_PostHook="$(_readdomainconf Le_PostHook)" | |
4887 | Le_RenewHook="$(_readdomainconf Le_RenewHook)" | |
875625b1 | 4888 | issue "$Le_Webroot" "$Le_Domain" "$Le_Alt" "$Le_Keylength" "$Le_RealCertPath" "$Le_RealKeyPath" "$Le_RealCACertPath" "$Le_ReloadCmd" "$Le_RealFullChainPath" "$Le_PreHook" "$Le_PostHook" "$Le_RenewHook" "$Le_LocalAddress" "$Le_ChallengeAlias" |
e799ef29 | 4889 | res="$?" |
4c2a3841 | 4890 | if [ "$res" != "0" ]; then |
e799ef29 | 4891 | return "$res" |
a61fe418 | 4892 | fi |
4c2a3841 | 4893 | |
4894 | if [ "$Le_DeployHook" ]; then | |
93bce1b2 | 4895 | _deploy "$Le_Domain" "$Le_DeployHook" |
e799ef29 | 4896 | res="$?" |
a61fe418 | 4897 | fi |
4c2a3841 | 4898 | |
4c3b3608 | 4899 | IS_RENEW="" |
4900 | ||
e799ef29 | 4901 | return "$res" |
4c3b3608 | 4902 | } |
4903 | ||
cc179731 | 4904 | #renewAll [stopRenewOnError] |
4c3b3608 | 4905 | renewAll() { |
4906 | _initpath | |
cc179731 | 4907 | _stopRenewOnError="$1" |
4908 | _debug "_stopRenewOnError" "$_stopRenewOnError" | |
4909 | _ret="0" | |
b50e701c | 4910 | _success_msg="" |
4911 | _error_msg="" | |
4912 | _skipped_msg="" | |
c6b68551 | 4913 | _error_level=$NOTIFY_LEVEL_SKIP |
4914 | _notify_code=$RENEW_SKIP | |
f803c6c0 | 4915 | _set_level=${NOTIFY_LEVEL:-$NOTIFY_LEVEL_DEFAULT} |
4916 | _debug "_set_level" "$_set_level" | |
e591d5cf | 4917 | for di in "${CERT_HOME}"/*.*/; do |
4918 | _debug di "$di" | |
44483dba | 4919 | if ! [ -d "$di" ]; then |
3498a585 | 4920 | _debug "Not directory, skip: $di" |
4921 | continue | |
4922 | fi | |
e591d5cf | 4923 | d=$(basename "$di") |
201aa244 | 4924 | _debug d "$d" |
43822d37 | 4925 | ( |
201aa244 | 4926 | if _endswith "$d" "$ECC_SUFFIX"; then |
4927 | _isEcc=$(echo "$d" | cut -d "$ECC_SEP" -f 2) | |
4928 | d=$(echo "$d" | cut -d "$ECC_SEP" -f 1) | |
43822d37 | 4929 | fi |
4930 | renew "$d" "$_isEcc" | |
4d2f38b0 | 4931 | ) |
cc179731 | 4932 | rc="$?" |
4933 | _debug "Return code: $rc" | |
c6b68551 | 4934 | if [ "$rc" = "0" ]; then |
4935 | if [ $_error_level -gt $NOTIFY_LEVEL_RENEW ]; then | |
4936 | _error_level="$NOTIFY_LEVEL_RENEW" | |
4937 | _notify_code=0 | |
4938 | fi | |
4939 | if [ "$ACME_IN_CRON" ]; then | |
f803c6c0 | 4940 | if [ $_set_level -ge $NOTIFY_LEVEL_RENEW ]; then |
c6b68551 | 4941 | if [ "$NOTIFY_MODE" = "$NOTIFY_MODE_CERT" ]; then |
4942 | _send_notify "Renew $d success" "Good, the cert is renewed." "$NOTIFY_HOOK" 0 | |
4943 | fi | |
4944 | fi | |
4945 | fi | |
4946 | _success_msg="${_success_msg} $d | |
b50e701c | 4947 | " |
c6b68551 | 4948 | elif [ "$rc" = "$RENEW_SKIP" ]; then |
4949 | if [ $_error_level -gt $NOTIFY_LEVEL_SKIP ]; then | |
4950 | _error_level="$NOTIFY_LEVEL_SKIP" | |
4951 | _notify_code=$RENEW_SKIP | |
4952 | fi | |
4953 | if [ "$ACME_IN_CRON" ]; then | |
f803c6c0 | 4954 | if [ $_set_level -ge $NOTIFY_LEVEL_SKIP ]; then |
c6b68551 | 4955 | if [ "$NOTIFY_MODE" = "$NOTIFY_MODE_CERT" ]; then |
4956 | _send_notify "Renew $d skipped" "Good, the cert is skipped." "$NOTIFY_HOOK" "$RENEW_SKIP" | |
4957 | fi | |
b50e701c | 4958 | fi |
cc179731 | 4959 | fi |
c6b68551 | 4960 | _info "Skipped $d" |
4961 | _skipped_msg="${_skipped_msg} $d | |
4962 | " | |
b50e701c | 4963 | else |
c6b68551 | 4964 | if [ $_error_level -gt $NOTIFY_LEVEL_ERROR ]; then |
4965 | _error_level="$NOTIFY_LEVEL_ERROR" | |
4966 | _notify_code=1 | |
4967 | fi | |
4968 | if [ "$ACME_IN_CRON" ]; then | |
f803c6c0 | 4969 | if [ $_set_level -ge $NOTIFY_LEVEL_ERROR ]; then |
c6b68551 | 4970 | if [ "$NOTIFY_MODE" = "$NOTIFY_MODE_CERT" ]; then |
4971 | _send_notify "Renew $d error" "There is an error." "$NOTIFY_HOOK" 1 | |
4972 | fi | |
4973 | fi | |
4974 | fi | |
4975 | _error_msg="${_error_msg} $d | |
b50e701c | 4976 | " |
c6b68551 | 4977 | if [ "$_stopRenewOnError" ]; then |
4978 | _err "Error renew $d, stop now." | |
4979 | _ret="$rc" | |
4980 | break | |
4981 | else | |
4982 | _ret="$rc" | |
4983 | _err "Error renew $d." | |
4984 | fi | |
cc179731 | 4985 | fi |
4c3b3608 | 4986 | done |
c6b68551 | 4987 | _debug _error_level "$_error_level" |
a2738e85 | 4988 | _debug _set_level "$_set_level" |
f803c6c0 | 4989 | if [ "$ACME_IN_CRON" ] && [ $_error_level -le $_set_level ]; then |
b50e701c | 4990 | if [ -z "$NOTIFY_MODE" ] || [ "$NOTIFY_MODE" = "$NOTIFY_MODE_BULK" ]; then |
4991 | _msg_subject="Renew" | |
4992 | if [ "$_error_msg" ]; then | |
4993 | _msg_subject="${_msg_subject} Error" | |
c6b68551 | 4994 | _msg_data="Error certs: |
4995 | ${_error_msg} | |
4996 | " | |
b50e701c | 4997 | fi |
4998 | if [ "$_success_msg" ]; then | |
4999 | _msg_subject="${_msg_subject} Success" | |
c6b68551 | 5000 | _msg_data="${_msg_data}Success certs: |
5001 | ${_success_msg} | |
5002 | " | |
b50e701c | 5003 | fi |
5004 | if [ "$_skipped_msg" ]; then | |
5005 | _msg_subject="${_msg_subject} Skipped" | |
c6b68551 | 5006 | _msg_data="${_msg_data}Skipped certs: |
5007 | ${_skipped_msg} | |
b50e701c | 5008 | " |
c6b68551 | 5009 | fi |
5010 | ||
5011 | _send_notify "$_msg_subject" "$_msg_data" "$NOTIFY_HOOK" "$_notify_code" | |
b50e701c | 5012 | fi |
5013 | fi | |
5014 | ||
201aa244 | 5015 | return "$_ret" |
4c3b3608 | 5016 | } |
5017 | ||
10afcaca | 5018 | #csr webroot |
4c2a3841 | 5019 | signcsr() { |
10afcaca | 5020 | _csrfile="$1" |
5021 | _csrW="$2" | |
5022 | if [ -z "$_csrfile" ] || [ -z "$_csrW" ]; then | |
5023 | _usage "Usage: $PROJECT_ENTRY --signcsr --csr mycsr.csr -w /path/to/webroot/a.com/ " | |
5024 | return 1 | |
5025 | fi | |
5026 | ||
875625b1 | 5027 | _real_cert="$3" |
5028 | _real_key="$4" | |
5029 | _real_ca="$5" | |
5030 | _reload_cmd="$6" | |
5031 | _real_fullchain="$7" | |
5032 | _pre_hook="${8}" | |
5033 | _post_hook="${9}" | |
5034 | _renew_hook="${10}" | |
5035 | _local_addr="${11}" | |
5036 | _challenge_alias="${12}" | |
5037 | ||
10afcaca | 5038 | _csrsubj=$(_readSubjectFromCSR "$_csrfile") |
4c2a3841 | 5039 | if [ "$?" != "0" ]; then |
10afcaca | 5040 | _err "Can not read subject from csr: $_csrfile" |
5041 | return 1 | |
5042 | fi | |
ad752b31 | 5043 | _debug _csrsubj "$_csrsubj" |
2c9ed4c5 | 5044 | if _contains "$_csrsubj" ' ' || ! _contains "$_csrsubj" '.'; then |
5045 | _info "It seems that the subject: $_csrsubj is not a valid domain name. Drop it." | |
5046 | _csrsubj="" | |
5047 | fi | |
10afcaca | 5048 | |
5049 | _csrdomainlist=$(_readSubjectAltNamesFromCSR "$_csrfile") | |
4c2a3841 | 5050 | if [ "$?" != "0" ]; then |
10afcaca | 5051 | _err "Can not read domain list from csr: $_csrfile" |
5052 | return 1 | |
5053 | fi | |
5054 | _debug "_csrdomainlist" "$_csrdomainlist" | |
4c2a3841 | 5055 | |
5056 | if [ -z "$_csrsubj" ]; then | |
ad752b31 | 5057 | _csrsubj="$(_getfield "$_csrdomainlist" 1)" |
5058 | _debug _csrsubj "$_csrsubj" | |
5059 | _csrdomainlist="$(echo "$_csrdomainlist" | cut -d , -f 2-)" | |
5060 | _debug "_csrdomainlist" "$_csrdomainlist" | |
5061 | fi | |
4c2a3841 | 5062 | |
5063 | if [ -z "$_csrsubj" ]; then | |
ad752b31 | 5064 | _err "Can not read subject from csr: $_csrfile" |
5065 | return 1 | |
5066 | fi | |
4c2a3841 | 5067 | |
10afcaca | 5068 | _csrkeylength=$(_readKeyLengthFromCSR "$_csrfile") |
4c2a3841 | 5069 | if [ "$?" != "0" ] || [ -z "$_csrkeylength" ]; then |
10afcaca | 5070 | _err "Can not read key length from csr: $_csrfile" |
5071 | return 1 | |
5072 | fi | |
4c2a3841 | 5073 | |
cd9fb3b6 | 5074 | if [ -z "$ACME_VERSION" ] && _contains "$_csrsubj,$_csrdomainlist" "*."; then |
5075 | export ACME_VERSION=2 | |
5076 | fi | |
10afcaca | 5077 | _initpath "$_csrsubj" "$_csrkeylength" |
5078 | mkdir -p "$DOMAIN_PATH" | |
4c2a3841 | 5079 | |
10afcaca | 5080 | _info "Copy csr to: $CSR_PATH" |
5081 | cp "$_csrfile" "$CSR_PATH" | |
4c2a3841 | 5082 | |
875625b1 | 5083 | issue "$_csrW" "$_csrsubj" "$_csrdomainlist" "$_csrkeylength" "$_real_cert" "$_real_key" "$_real_ca" "$_reload_cmd" "$_real_fullchain" "$_pre_hook" "$_post_hook" "$_renew_hook" "$_local_addr" "$_challenge_alias" |
4c2a3841 | 5084 | |
10afcaca | 5085 | } |
5086 | ||
5087 | showcsr() { | |
4c2a3841 | 5088 | _csrfile="$1" |
10afcaca | 5089 | _csrd="$2" |
5090 | if [ -z "$_csrfile" ] && [ -z "$_csrd" ]; then | |
5091 | _usage "Usage: $PROJECT_ENTRY --showcsr --csr mycsr.csr" | |
5092 | return 1 | |
5093 | fi | |
5094 | ||
5095 | _initpath | |
4c2a3841 | 5096 | |
10afcaca | 5097 | _csrsubj=$(_readSubjectFromCSR "$_csrfile") |
4c2a3841 | 5098 | if [ "$?" != "0" ] || [ -z "$_csrsubj" ]; then |
10afcaca | 5099 | _err "Can not read subject from csr: $_csrfile" |
5100 | return 1 | |
5101 | fi | |
4c2a3841 | 5102 | |
10afcaca | 5103 | _info "Subject=$_csrsubj" |
5104 | ||
5105 | _csrdomainlist=$(_readSubjectAltNamesFromCSR "$_csrfile") | |
4c2a3841 | 5106 | if [ "$?" != "0" ]; then |
10afcaca | 5107 | _err "Can not read domain list from csr: $_csrfile" |
5108 | return 1 | |
5109 | fi | |
5110 | _debug "_csrdomainlist" "$_csrdomainlist" | |
5111 | ||
5112 | _info "SubjectAltNames=$_csrdomainlist" | |
5113 | ||
10afcaca | 5114 | _csrkeylength=$(_readKeyLengthFromCSR "$_csrfile") |
4c2a3841 | 5115 | if [ "$?" != "0" ] || [ -z "$_csrkeylength" ]; then |
10afcaca | 5116 | _err "Can not read key length from csr: $_csrfile" |
5117 | return 1 | |
5118 | fi | |
5119 | _info "KeyLength=$_csrkeylength" | |
5120 | } | |
5121 | ||
6d7eda3e | 5122 | list() { |
22ea4004 | 5123 | _raw="$1" |
6d7eda3e | 5124 | _initpath |
4c2a3841 | 5125 | |
dcf4f8f6 | 5126 | _sep="|" |
4c2a3841 | 5127 | if [ "$_raw" ]; then |
d5ec5f80 | 5128 | printf "%s\n" "Main_Domain${_sep}KeyLength${_sep}SAN_Domains${_sep}Created${_sep}Renew" |
e591d5cf | 5129 | for di in "${CERT_HOME}"/*.*/; do |
5130 | d=$(basename "$di") | |
201aa244 | 5131 | _debug d "$d" |
dcf4f8f6 | 5132 | ( |
201aa244 | 5133 | if _endswith "$d" "$ECC_SUFFIX"; then |
be0df07d | 5134 | _isEcc="ecc" |
201aa244 | 5135 | d=$(echo "$d" | cut -d "$ECC_SEP" -f 1) |
43822d37 | 5136 | fi |
be0df07d | 5137 | DOMAIN_CONF="$di/$d.conf" |
4c2a3841 | 5138 | if [ -f "$DOMAIN_CONF" ]; then |
dcf4f8f6 | 5139 | . "$DOMAIN_CONF" |
d5ec5f80 | 5140 | printf "%s\n" "$Le_Domain${_sep}\"$Le_Keylength\"${_sep}$Le_Alt${_sep}$Le_CertCreateTimeStr${_sep}$Le_NextRenewTimeStr" |
dcf4f8f6 | 5141 | fi |
5142 | ) | |
5143 | done | |
5144 | else | |
4c2a3841 | 5145 | if _exists column; then |
22ea4004 | 5146 | list "raw" | column -t -s "$_sep" |
5147 | else | |
43822d37 | 5148 | list "raw" | tr "$_sep" '\t' |
22ea4004 | 5149 | fi |
dcf4f8f6 | 5150 | fi |
6d7eda3e | 5151 | |
6d7eda3e | 5152 | } |
5153 | ||
93bce1b2 | 5154 | _deploy() { |
5155 | _d="$1" | |
5156 | _hooks="$2" | |
5157 | ||
5158 | for _d_api in $(echo "$_hooks" | tr ',' " "); do | |
b50e701c | 5159 | _deployApi="$(_findHook "$_d" $_SUB_FOLDER_DEPLOY "$_d_api")" |
93bce1b2 | 5160 | if [ -z "$_deployApi" ]; then |
5161 | _err "The deploy hook $_d_api is not found." | |
5162 | return 1 | |
5163 | fi | |
5164 | _debug _deployApi "$_deployApi" | |
5165 | ||
5166 | if ! ( | |
5167 | if ! . "$_deployApi"; then | |
5168 | _err "Load file $_deployApi error. Please check your api file and try again." | |
5169 | return 1 | |
5170 | fi | |
5171 | ||
5172 | d_command="${_d_api}_deploy" | |
5173 | if ! _exists "$d_command"; then | |
5174 | _err "It seems that your api file is not correct, it must have a function named: $d_command" | |
5175 | return 1 | |
5176 | fi | |
5177 | ||
5178 | if ! $d_command "$_d" "$CERT_KEY_PATH" "$CERT_PATH" "$CA_CERT_PATH" "$CERT_FULLCHAIN_PATH"; then | |
5179 | _err "Error deploy for domain:$_d" | |
5180 | return 1 | |
5181 | fi | |
5182 | ); then | |
5183 | _err "Deploy error." | |
5184 | return 1 | |
5185 | else | |
5186 | _info "$(__green Success)" | |
5187 | fi | |
5188 | done | |
5189 | } | |
5190 | ||
5191 | #domain hooks | |
a61fe418 | 5192 | deploy() { |
93bce1b2 | 5193 | _d="$1" |
5194 | _hooks="$2" | |
a61fe418 | 5195 | _isEcc="$3" |
93bce1b2 | 5196 | if [ -z "$_hooks" ]; then |
a61fe418 | 5197 | _usage "Usage: $PROJECT_ENTRY --deploy -d domain.com --deploy-hook cpanel [--ecc] " |
5198 | return 1 | |
5199 | fi | |
5200 | ||
93bce1b2 | 5201 | _initpath "$_d" "$_isEcc" |
4c2a3841 | 5202 | if [ ! -d "$DOMAIN_PATH" ]; then |
9672c6b8 | 5203 | _err "The domain '$_d' is not a cert name. You must use the cert name to specify the cert to install." |
5204 | _err "Can not find path:'$DOMAIN_PATH'" | |
a61fe418 | 5205 | return 1 |
5206 | fi | |
4c2a3841 | 5207 | |
93bce1b2 | 5208 | . "$DOMAIN_CONF" |
4c2a3841 | 5209 | |
93bce1b2 | 5210 | _savedomainconf Le_DeployHook "$_hooks" |
4c2a3841 | 5211 | |
93bce1b2 | 5212 | _deploy "$_d" "$_hooks" |
a61fe418 | 5213 | } |
5214 | ||
4c3b3608 | 5215 | installcert() { |
85e1f4ea | 5216 | _main_domain="$1" |
5217 | if [ -z "$_main_domain" ]; then | |
5c539af7 | 5218 | _usage "Usage: $PROJECT_ENTRY --installcert -d domain.com [--ecc] [--cert-file cert-file-path] [--key-file key-file-path] [--ca-file ca-cert-file-path] [ --reloadCmd reloadCmd] [--fullchain-file fullchain-path]" |
4c3b3608 | 5219 | return 1 |
5220 | fi | |
5221 | ||
85e1f4ea | 5222 | _real_cert="$2" |
5223 | _real_key="$3" | |
5224 | _real_ca="$4" | |
5225 | _reload_cmd="$5" | |
5226 | _real_fullchain="$6" | |
43822d37 | 5227 | _isEcc="$7" |
5228 | ||
85e1f4ea | 5229 | _initpath "$_main_domain" "$_isEcc" |
4c2a3841 | 5230 | if [ ! -d "$DOMAIN_PATH" ]; then |
9672c6b8 | 5231 | _err "The domain '$_main_domain' is not a cert name. You must use the cert name to specify the cert to install." |
5232 | _err "Can not find path:'$DOMAIN_PATH'" | |
43822d37 | 5233 | return 1 |
5234 | fi | |
5235 | ||
85e1f4ea | 5236 | _savedomainconf "Le_RealCertPath" "$_real_cert" |
5237 | _savedomainconf "Le_RealCACertPath" "$_real_ca" | |
5238 | _savedomainconf "Le_RealKeyPath" "$_real_key" | |
7690f73e | 5239 | _savedomainconf "Le_ReloadCmd" "$_reload_cmd" "base64" |
85e1f4ea | 5240 | _savedomainconf "Le_RealFullChainPath" "$_real_fullchain" |
5241 | ||
044da37c | 5242 | _installcert "$_main_domain" "$_real_cert" "$_real_key" "$_real_ca" "$_real_fullchain" "$_reload_cmd" |
43822d37 | 5243 | } |
4c3b3608 | 5244 | |
044da37c | 5245 | #domain cert key ca fullchain reloadcmd backup-prefix |
43822d37 | 5246 | _installcert() { |
85e1f4ea | 5247 | _main_domain="$1" |
5248 | _real_cert="$2" | |
5249 | _real_key="$3" | |
5250 | _real_ca="$4" | |
044da37c | 5251 | _real_fullchain="$5" |
5252 | _reload_cmd="$6" | |
5253 | _backup_prefix="$7" | |
4c3b3608 | 5254 | |
85e1f4ea | 5255 | if [ "$_real_cert" = "$NO_VALUE" ]; then |
5256 | _real_cert="" | |
4d2f38b0 | 5257 | fi |
85e1f4ea | 5258 | if [ "$_real_key" = "$NO_VALUE" ]; then |
5259 | _real_key="" | |
4d2f38b0 | 5260 | fi |
85e1f4ea | 5261 | if [ "$_real_ca" = "$NO_VALUE" ]; then |
5262 | _real_ca="" | |
4d2f38b0 | 5263 | fi |
85e1f4ea | 5264 | if [ "$_reload_cmd" = "$NO_VALUE" ]; then |
5265 | _reload_cmd="" | |
4d2f38b0 | 5266 | fi |
85e1f4ea | 5267 | if [ "$_real_fullchain" = "$NO_VALUE" ]; then |
5268 | _real_fullchain="" | |
4d2f38b0 | 5269 | fi |
4c2a3841 | 5270 | |
044da37c | 5271 | _backup_path="$DOMAIN_BACKUP_PATH/$_backup_prefix" |
5272 | mkdir -p "$_backup_path" | |
5273 | ||
85e1f4ea | 5274 | if [ "$_real_cert" ]; then |
5275 | _info "Installing cert to:$_real_cert" | |
5276 | if [ -f "$_real_cert" ] && [ ! "$IS_RENEW" ]; then | |
044da37c | 5277 | cp "$_real_cert" "$_backup_path/cert.bak" |
4c3b3608 | 5278 | fi |
206be3c1 | 5279 | cat "$CERT_PATH" >"$_real_cert" || return 1 |
4c3b3608 | 5280 | fi |
4c2a3841 | 5281 | |
85e1f4ea | 5282 | if [ "$_real_ca" ]; then |
5283 | _info "Installing CA to:$_real_ca" | |
5284 | if [ "$_real_ca" = "$_real_cert" ]; then | |
5285 | echo "" >>"$_real_ca" | |
206be3c1 | 5286 | cat "$CA_CERT_PATH" >>"$_real_ca" || return 1 |
4c3b3608 | 5287 | else |
85e1f4ea | 5288 | if [ -f "$_real_ca" ] && [ ! "$IS_RENEW" ]; then |
044da37c | 5289 | cp "$_real_ca" "$_backup_path/ca.bak" |
78552b18 | 5290 | fi |
206be3c1 | 5291 | cat "$CA_CERT_PATH" >"$_real_ca" || return 1 |
4c3b3608 | 5292 | fi |
5293 | fi | |
5294 | ||
85e1f4ea | 5295 | if [ "$_real_key" ]; then |
5296 | _info "Installing key to:$_real_key" | |
5297 | if [ -f "$_real_key" ] && [ ! "$IS_RENEW" ]; then | |
044da37c | 5298 | cp "$_real_key" "$_backup_path/key.bak" |
4c3b3608 | 5299 | fi |
82014583 | 5300 | if [ -f "$_real_key" ]; then |
206be3c1 | 5301 | cat "$CERT_KEY_PATH" >"$_real_key" || return 1 |
82014583 | 5302 | else |
206be3c1 | 5303 | cat "$CERT_KEY_PATH" >"$_real_key" || return 1 |
7b92371a | 5304 | chmod 600 "$_real_key" |
82014583 | 5305 | fi |
4c3b3608 | 5306 | fi |
4c2a3841 | 5307 | |
85e1f4ea | 5308 | if [ "$_real_fullchain" ]; then |
5309 | _info "Installing full chain to:$_real_fullchain" | |
5310 | if [ -f "$_real_fullchain" ] && [ ! "$IS_RENEW" ]; then | |
044da37c | 5311 | cp "$_real_fullchain" "$_backup_path/fullchain.bak" |
a63b05a9 | 5312 | fi |
206be3c1 | 5313 | cat "$CERT_FULLCHAIN_PATH" >"$_real_fullchain" || return 1 |
4c2a3841 | 5314 | fi |
4c3b3608 | 5315 | |
85e1f4ea | 5316 | if [ "$_reload_cmd" ]; then |
5317 | _info "Run reload cmd: $_reload_cmd" | |
25555b8c | 5318 | if ( |
839bf0e2 | 5319 | export CERT_PATH |
5320 | export CERT_KEY_PATH | |
5321 | export CA_CERT_PATH | |
5322 | export CERT_FULLCHAIN_PATH | |
b3f61297 | 5323 | export Le_Domain="$_main_domain" |
85e1f4ea | 5324 | cd "$DOMAIN_PATH" && eval "$_reload_cmd" |
839bf0e2 | 5325 | ); then |
43822d37 | 5326 | _info "$(__green "Reload success")" |
4d2f38b0 | 5327 | else |
5328 | _err "Reload error for :$Le_Domain" | |
5329 | fi | |
5330 | fi | |
5331 | ||
4c3b3608 | 5332 | } |
5333 | ||
77f96b38 | 5334 | __read_password() { |
5335 | unset _pp | |
5336 | prompt="Enter Password:" | |
0b04a7f1 | 5337 | while IFS= read -p "$prompt" -r -s -n 1 char; do |
5338 | if [ "$char" = $'\0' ]; then | |
4ebad105 | 5339 | break |
0b04a7f1 | 5340 | fi |
5341 | prompt='*' | |
5342 | _pp="$_pp$char" | |
77f96b38 | 5343 | done |
5344 | echo "$_pp" | |
5345 | } | |
5346 | ||
5347 | _install_win_taskscheduler() { | |
5348 | _lesh="$1" | |
5349 | _centry="$2" | |
5350 | _randomminute="$3" | |
5351 | if ! _exists cygpath; then | |
5352 | _err "cygpath not found" | |
5353 | return 1 | |
5354 | fi | |
5355 | if ! _exists schtasks; then | |
5356 | _err "schtasks.exe is not found, are you on Windows?" | |
5357 | return 1 | |
5358 | fi | |
5359 | _winbash="$(cygpath -w $(which bash))" | |
5360 | _debug _winbash "$_winbash" | |
5361 | if [ -z "$_winbash" ]; then | |
5362 | _err "can not find bash path" | |
5363 | return 1 | |
5364 | fi | |
5365 | _myname="$(whoami)" | |
5366 | _debug "_myname" "$_myname" | |
5367 | if [ -z "$_myname" ]; then | |
5368 | _err "can not find my user name" | |
5369 | return 1 | |
5370 | fi | |
5371 | _debug "_lesh" "$_lesh" | |
5372 | ||
5373 | _info "To install scheduler task in your Windows account, you must input your windows password." | |
5374 | _info "$PROJECT_NAME doesn't save your password." | |
5375 | _info "Please input your Windows password for: $(__green "$_myname")" | |
5376 | _password="$(__read_password)" | |
5377 | #SCHTASKS.exe '/create' '/SC' 'DAILY' '/TN' "$_WINDOWS_SCHEDULER_NAME" '/F' '/ST' "00:$_randomminute" '/RU' "$_myname" '/RP' "$_password" '/TR' "$_winbash -l -c '$_lesh --cron --home \"$LE_WORKING_DIR\" $_centry'" >/dev/null | |
5378 | echo SCHTASKS.exe '/create' '/SC' 'DAILY' '/TN' "$_WINDOWS_SCHEDULER_NAME" '/F' '/ST' "00:$_randomminute" '/RU' "$_myname" '/RP' "$_password" '/TR' "\"$_winbash -l -c '$_lesh --cron --home \"$LE_WORKING_DIR\" $_centry'\"" | cmd.exe >/dev/null | |
5379 | echo | |
5380 | ||
5381 | } | |
5382 | ||
5383 | _uninstall_win_taskscheduler() { | |
5384 | if ! _exists schtasks; then | |
5385 | _err "schtasks.exe is not found, are you on Windows?" | |
5386 | return 1 | |
5387 | fi | |
5388 | if ! echo SCHTASKS /query /tn "$_WINDOWS_SCHEDULER_NAME" | cmd.exe >/dev/null; then | |
5389 | _debug "scheduler $_WINDOWS_SCHEDULER_NAME is not found." | |
5390 | else | |
5391 | _info "Removing $_WINDOWS_SCHEDULER_NAME" | |
0b04a7f1 | 5392 | echo SCHTASKS /delete /f /tn "$_WINDOWS_SCHEDULER_NAME" | cmd.exe >/dev/null |
77f96b38 | 5393 | fi |
5394 | } | |
5395 | ||
27dbe77f | 5396 | #confighome |
4c3b3608 | 5397 | installcronjob() { |
27dbe77f | 5398 | _c_home="$1" |
4c3b3608 | 5399 | _initpath |
415f375c | 5400 | _CRONTAB="crontab" |
77f96b38 | 5401 | if [ -f "$LE_WORKING_DIR/$PROJECT_ENTRY" ]; then |
5402 | lesh="\"$LE_WORKING_DIR\"/$PROJECT_ENTRY" | |
5403 | else | |
5404 | _err "Can not install cronjob, $PROJECT_ENTRY not found." | |
5405 | return 1 | |
5406 | fi | |
5407 | if [ "$_c_home" ]; then | |
5408 | _c_entry="--config-home \"$_c_home\" " | |
5409 | fi | |
5410 | _t=$(_time) | |
5411 | random_minute=$(_math $_t % 60) | |
5412 | ||
415f375c | 5413 | if ! _exists "$_CRONTAB" && _exists "fcrontab"; then |
5414 | _CRONTAB="fcrontab" | |
5415 | fi | |
77f96b38 | 5416 | |
415f375c | 5417 | if ! _exists "$_CRONTAB"; then |
77f96b38 | 5418 | if _exists cygpath && _exists schtasks.exe; then |
5419 | _info "It seems you are on Windows, let's install Windows scheduler task." | |
5420 | if _install_win_taskscheduler "$lesh" "$_c_entry" "$random_minute"; then | |
5421 | _info "Install Windows scheduler task success." | |
5422 | return 0 | |
5423 | else | |
5424 | _err "Install Windows scheduler task failed." | |
5425 | return 1 | |
5426 | fi | |
5427 | fi | |
415f375c | 5428 | _err "crontab/fcrontab doesn't exist, so, we can not install cron jobs." |
77546ea5 | 5429 | _err "All your certs will not be renewed automatically." |
a7b7355d | 5430 | _err "You must add your own cron job to call '$PROJECT_ENTRY --cron' everyday." |
77546ea5 | 5431 | return 1 |
5432 | fi | |
4c3b3608 | 5433 | _info "Installing cron job" |
415f375c | 5434 | if ! $_CRONTAB -l | grep "$PROJECT_ENTRY --cron"; then |
e2c939fb | 5435 | if _exists uname && uname -a | grep SunOS >/dev/null; then |
415f375c | 5436 | $_CRONTAB -l | { |
4c2a3841 | 5437 | cat |
0533bde9 | 5438 | echo "$random_minute 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" $_c_entry> /dev/null" |
415f375c | 5439 | } | $_CRONTAB -- |
22ea4004 | 5440 | else |
415f375c | 5441 | $_CRONTAB -l | { |
4c2a3841 | 5442 | cat |
0533bde9 | 5443 | echo "$random_minute 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" $_c_entry> /dev/null" |
415f375c | 5444 | } | $_CRONTAB - |
22ea4004 | 5445 | fi |
4c3b3608 | 5446 | fi |
4c2a3841 | 5447 | if [ "$?" != "0" ]; then |
4c3b3608 | 5448 | _err "Install cron job failed. You need to manually renew your certs." |
5449 | _err "Or you can add cronjob by yourself:" | |
a7b7355d | 5450 | _err "$lesh --cron --home \"$LE_WORKING_DIR\" > /dev/null" |
4c3b3608 | 5451 | return 1 |
5452 | fi | |
5453 | } | |
5454 | ||
5455 | uninstallcronjob() { | |
415f375c | 5456 | _CRONTAB="crontab" |
5457 | if ! _exists "$_CRONTAB" && _exists "fcrontab"; then | |
5458 | _CRONTAB="fcrontab" | |
5459 | fi | |
5460 | ||
5461 | if ! _exists "$_CRONTAB"; then | |
77f96b38 | 5462 | if _exists cygpath && _exists schtasks.exe; then |
5463 | _info "It seems you are on Windows, let's uninstall Windows scheduler task." | |
5464 | if _uninstall_win_taskscheduler; then | |
5465 | _info "Uninstall Windows scheduler task success." | |
5466 | return 0 | |
5467 | else | |
5468 | _err "Uninstall Windows scheduler task failed." | |
5469 | return 1 | |
5470 | fi | |
5471 | fi | |
37db5b81 | 5472 | return |
5473 | fi | |
4c3b3608 | 5474 | _info "Removing cron job" |
415f375c | 5475 | cr="$($_CRONTAB -l | grep "$PROJECT_ENTRY --cron")" |
4c2a3841 | 5476 | if [ "$cr" ]; then |
5477 | if _exists uname && uname -a | grep solaris >/dev/null; then | |
415f375c | 5478 | $_CRONTAB -l | sed "/$PROJECT_ENTRY --cron/d" | $_CRONTAB -- |
22ea4004 | 5479 | else |
415f375c | 5480 | $_CRONTAB -l | sed "/$PROJECT_ENTRY --cron/d" | $_CRONTAB - |
22ea4004 | 5481 | fi |
a7b7355d | 5482 | LE_WORKING_DIR="$(echo "$cr" | cut -d ' ' -f 9 | tr -d '"')" |
4c3b3608 | 5483 | _info LE_WORKING_DIR "$LE_WORKING_DIR" |
27dbe77f | 5484 | if _contains "$cr" "--config-home"; then |
f5b546b3 | 5485 | LE_CONFIG_HOME="$(echo "$cr" | cut -d ' ' -f 11 | tr -d '"')" |
5486 | _debug LE_CONFIG_HOME "$LE_CONFIG_HOME" | |
27dbe77f | 5487 | fi |
4c2a3841 | 5488 | fi |
4c3b3608 | 5489 | _initpath |
a7b7355d | 5490 | |
4c3b3608 | 5491 | } |
5492 | ||
1041c9f9 | 5493 | #domain isECC revokeReason |
6cb415f5 | 5494 | revoke() { |
5495 | Le_Domain="$1" | |
4c2a3841 | 5496 | if [ -z "$Le_Domain" ]; then |
78f0201d | 5497 | _usage "Usage: $PROJECT_ENTRY --revoke -d domain.com [--ecc]" |
6cb415f5 | 5498 | return 1 |
5499 | fi | |
4c2a3841 | 5500 | |
43822d37 | 5501 | _isEcc="$2" |
1041c9f9 | 5502 | _reason="$3" |
5503 | if [ -z "$_reason" ]; then | |
5504 | _reason="0" | |
5505 | fi | |
c4a375b3 | 5506 | _initpath "$Le_Domain" "$_isEcc" |
4c2a3841 | 5507 | if [ ! -f "$DOMAIN_CONF" ]; then |
6cb415f5 | 5508 | _err "$Le_Domain is not a issued domain, skip." |
4c2a3841 | 5509 | return 1 |
6cb415f5 | 5510 | fi |
4c2a3841 | 5511 | |
5512 | if [ ! -f "$CERT_PATH" ]; then | |
6cb415f5 | 5513 | _err "Cert for $Le_Domain $CERT_PATH is not found, skip." |
5514 | return 1 | |
5515 | fi | |
6cb415f5 | 5516 | |
11927a76 | 5517 | cert="$(_getfile "${CERT_PATH}" "${BEGIN_CERT}" "${END_CERT}" | tr -d "\r\n" | _url_replace)" |
4c2a3841 | 5518 | |
5519 | if [ -z "$cert" ]; then | |
6cb415f5 | 5520 | _err "Cert for $Le_Domain is empty found, skip." |
5521 | return 1 | |
5522 | fi | |
4c2a3841 | 5523 | |
48d9a8c1 | 5524 | _initAPI |
5525 | ||
d2cde379 | 5526 | if [ "$ACME_VERSION" = "2" ]; then |
1041c9f9 | 5527 | data="{\"certificate\": \"$cert\",\"reason\":$_reason}" |
d2cde379 | 5528 | else |
5529 | data="{\"resource\": \"revoke-cert\", \"certificate\": \"$cert\"}" | |
5530 | fi | |
48d9a8c1 | 5531 | uri="${ACME_REVOKE_CERT}" |
6cb415f5 | 5532 | |
4c2a3841 | 5533 | if [ -f "$CERT_KEY_PATH" ]; then |
1befee5a | 5534 | _info "Try domain key first." |
c4a375b3 | 5535 | if _send_signed_request "$uri" "$data" "" "$CERT_KEY_PATH"; then |
4c2a3841 | 5536 | if [ -z "$response" ]; then |
1befee5a | 5537 | _info "Revoke success." |
c4a375b3 | 5538 | rm -f "$CERT_PATH" |
1befee5a | 5539 | return 0 |
4c2a3841 | 5540 | else |
1befee5a | 5541 | _err "Revoke error by domain key." |
5542 | _err "$response" | |
5543 | fi | |
6cb415f5 | 5544 | fi |
4c2a3841 | 5545 | else |
eca57bee | 5546 | _info "Domain key file doesn't exist." |
6cb415f5 | 5547 | fi |
6cb415f5 | 5548 | |
1befee5a | 5549 | _info "Try account key." |
6cb415f5 | 5550 | |
c4a375b3 | 5551 | if _send_signed_request "$uri" "$data" "" "$ACCOUNT_KEY_PATH"; then |
4c2a3841 | 5552 | if [ -z "$response" ]; then |
6cb415f5 | 5553 | _info "Revoke success." |
c4a375b3 | 5554 | rm -f "$CERT_PATH" |
6cb415f5 | 5555 | return 0 |
4c2a3841 | 5556 | else |
6cb415f5 | 5557 | _err "Revoke error." |
c9c31c04 | 5558 | _debug "$response" |
6cb415f5 | 5559 | fi |
5560 | fi | |
5561 | return 1 | |
5562 | } | |
4c3b3608 | 5563 | |
78f0201d | 5564 | #domain ecc |
5565 | remove() { | |
5566 | Le_Domain="$1" | |
5567 | if [ -z "$Le_Domain" ]; then | |
5568 | _usage "Usage: $PROJECT_ENTRY --remove -d domain.com [--ecc]" | |
5569 | return 1 | |
5570 | fi | |
5571 | ||
5572 | _isEcc="$2" | |
5573 | ||
5574 | _initpath "$Le_Domain" "$_isEcc" | |
5575 | _removed_conf="$DOMAIN_CONF.removed" | |
5576 | if [ ! -f "$DOMAIN_CONF" ]; then | |
5577 | if [ -f "$_removed_conf" ]; then | |
5578 | _err "$Le_Domain is already removed, You can remove the folder by yourself: $DOMAIN_PATH" | |
5579 | else | |
5580 | _err "$Le_Domain is not a issued domain, skip." | |
5581 | fi | |
5582 | return 1 | |
5583 | fi | |
5584 | ||
5585 | if mv "$DOMAIN_CONF" "$_removed_conf"; then | |
68aea3af | 5586 | _info "$Le_Domain is removed, the key and cert files are in $(__green $DOMAIN_PATH)" |
78f0201d | 5587 | _info "You can remove them by yourself." |
5588 | return 0 | |
5589 | else | |
5590 | _err "Remove $Le_Domain failed." | |
5591 | return 1 | |
5592 | fi | |
5593 | } | |
5594 | ||
0c00e870 | 5595 | #domain vtype |
5596 | _deactivate() { | |
5597 | _d_domain="$1" | |
5598 | _d_type="$2" | |
5599 | _initpath | |
4c2a3841 | 5600 | |
d2cde379 | 5601 | if [ "$ACME_VERSION" = "2" ]; then |
5602 | _identifiers="{\"type\":\"dns\",\"value\":\"$_d_domain\"}" | |
5603 | if ! _send_signed_request "$ACME_NEW_ORDER" "{\"identifiers\": [$_identifiers]}"; then | |
5604 | _err "Can not get domain new order." | |
5605 | return 1 | |
5606 | fi | |
d0d74907 | 5607 | _authorizations_seg="$(echo "$response" | _egrep_o '"authorizations" *: *\[[^\]*\]' | cut -d '[' -f 2 | tr -d ']' | tr -d '"')" |
d2cde379 | 5608 | _debug2 _authorizations_seg "$_authorizations_seg" |
5609 | if [ -z "$_authorizations_seg" ]; then | |
5610 | _err "_authorizations_seg not found." | |
5611 | _clearup | |
5612 | _on_issue_err "$_post_hook" | |
5613 | return 1 | |
5614 | fi | |
4c2a3841 | 5615 | |
d2cde379 | 5616 | authzUri="$_authorizations_seg" |
5617 | _debug2 "authzUri" "$authzUri" | |
0483d841 | 5618 | if ! _send_signed_request "$authzUri"; then |
d2cde379 | 5619 | _err "get to authz error." |
263c38ca | 5620 | _err "_authorizations_seg" "$_authorizations_seg" |
5621 | _err "authzUri" "$authzUri" | |
d2cde379 | 5622 | _clearup |
5623 | _on_issue_err "$_post_hook" | |
5624 | return 1 | |
5625 | fi | |
4c2a3841 | 5626 | |
d2cde379 | 5627 | response="$(echo "$response" | _normalizeJson)" |
5628 | _debug2 response "$response" | |
5629 | _URL_NAME="url" | |
5630 | else | |
5631 | if ! __get_domain_new_authz "$_d_domain"; then | |
5632 | _err "Can not get domain new authz token." | |
5633 | return 1 | |
5634 | fi | |
5635 | ||
0712e989 | 5636 | authzUri="$(echo "$responseHeaders" | grep "^Location:" | _head_n 1 | cut -d ':' -f 2- | tr -d "\r\n")" |
d2cde379 | 5637 | _debug "authzUri" "$authzUri" |
5638 | if [ "$code" ] && [ ! "$code" = '201' ]; then | |
5639 | _err "new-authz error: $response" | |
5640 | return 1 | |
5641 | fi | |
5642 | _URL_NAME="uri" | |
14d7bfda | 5643 | fi |
0c00e870 | 5644 | |
d2cde379 | 5645 | entries="$(echo "$response" | _egrep_o "{ *\"type\":\"[^\"]*\", *\"status\": *\"valid\", *\"$_URL_NAME\"[^}]*")" |
14d7bfda | 5646 | if [ -z "$entries" ]; then |
5647 | _info "No valid entries found." | |
5648 | if [ -z "$thumbprint" ]; then | |
5649 | thumbprint="$(__calc_account_thumbprint)" | |
5650 | fi | |
5651 | _debug "Trigger validation." | |
d2cde379 | 5652 | vtype="$VTYPE_DNS" |
d0d74907 | 5653 | entry="$(echo "$response" | _egrep_o '[^\{]*"type":"'$vtype'"[^\}]*')" |
14d7bfda | 5654 | _debug entry "$entry" |
5655 | if [ -z "$entry" ]; then | |
5656 | _err "Error, can not get domain token $d" | |
0c00e870 | 5657 | return 1 |
5658 | fi | |
d0d74907 | 5659 | token="$(echo "$entry" | _egrep_o '"token":"[^"]*' | cut -d : -f 2 | tr -d '"')" |
14d7bfda | 5660 | _debug token "$token" |
4c2a3841 | 5661 | |
d0d74907 | 5662 | uri="$(echo "$entry" | _egrep_o "\"$_URL_NAME\":\"[^\"]*" | cut -d : -f 2,3 | tr -d '"')" |
14d7bfda | 5663 | _debug uri "$uri" |
5664 | ||
5665 | keyauthorization="$token.$thumbprint" | |
5666 | _debug keyauthorization "$keyauthorization" | |
5667 | __trigger_validation "$uri" "$keyauthorization" | |
5668 | ||
5669 | fi | |
5670 | ||
5671 | _d_i=0 | |
5672 | _d_max_retry=$(echo "$entries" | wc -l) | |
5673 | while [ "$_d_i" -lt "$_d_max_retry" ]; do | |
5674 | _info "Deactivate: $_d_domain" | |
5675 | _d_i="$(_math $_d_i + 1)" | |
5676 | entry="$(echo "$entries" | sed -n "${_d_i}p")" | |
0c00e870 | 5677 | _debug entry "$entry" |
4c2a3841 | 5678 | |
5679 | if [ -z "$entry" ]; then | |
fb2029e7 | 5680 | _info "No more valid entry found." |
0c00e870 | 5681 | break |
5682 | fi | |
4c2a3841 | 5683 | |
d0d74907 | 5684 | _vtype="$(echo "$entry" | _egrep_o '"type": *"[^"]*"' | cut -d : -f 2 | tr -d '"')" |
c4a375b3 | 5685 | _debug _vtype "$_vtype" |
0c00e870 | 5686 | _info "Found $_vtype" |
5687 | ||
d0d74907 | 5688 | uri="$(echo "$entry" | _egrep_o "\"$_URL_NAME\":\"[^\"]*" | cut -d : -f 2,3 | tr -d '"')" |
c4a375b3 | 5689 | _debug uri "$uri" |
4c2a3841 | 5690 | |
5691 | if [ "$_d_type" ] && [ "$_d_type" != "$_vtype" ]; then | |
0c00e870 | 5692 | _info "Skip $_vtype" |
5693 | continue | |
5694 | fi | |
4c2a3841 | 5695 | |
0c00e870 | 5696 | _info "Deactivate: $_vtype" |
4c2a3841 | 5697 | |
d2cde379 | 5698 | if [ "$ACME_VERSION" = "2" ]; then |
5699 | _djson="{\"status\":\"deactivated\"}" | |
5700 | else | |
5701 | _djson="{\"resource\": \"authz\", \"status\":\"deactivated\"}" | |
5702 | fi | |
5703 | ||
5704 | if _send_signed_request "$authzUri" "$_djson" && _contains "$response" '"deactivated"'; then | |
14d7bfda | 5705 | _info "Deactivate: $_vtype success." |
5706 | else | |
0c00e870 | 5707 | _err "Can not deactivate $_vtype." |
14d7bfda | 5708 | break |
0c00e870 | 5709 | fi |
4c2a3841 | 5710 | |
0c00e870 | 5711 | done |
5712 | _debug "$_d_i" | |
14d7bfda | 5713 | if [ "$_d_i" -eq "$_d_max_retry" ]; then |
0c00e870 | 5714 | _info "Deactivated success!" |
5715 | else | |
5716 | _err "Deactivate failed." | |
5717 | fi | |
5718 | ||
5719 | } | |
5720 | ||
5721 | deactivate() { | |
3f4513b3 | 5722 | _d_domain_list="$1" |
0c00e870 | 5723 | _d_type="$2" |
5724 | _initpath | |
a3bdaa85 | 5725 | _initAPI |
3f4513b3 | 5726 | _debug _d_domain_list "$_d_domain_list" |
4c2a3841 | 5727 | if [ -z "$(echo $_d_domain_list | cut -d , -f 1)" ]; then |
3f4513b3 | 5728 | _usage "Usage: $PROJECT_ENTRY --deactivate -d domain.com [-d domain.com]" |
0c00e870 | 5729 | return 1 |
5730 | fi | |
4c2a3841 | 5731 | for _d_dm in $(echo "$_d_domain_list" | tr ',' ' '); do |
5732 | if [ -z "$_d_dm" ] || [ "$_d_dm" = "$NO_VALUE" ]; then | |
3f4513b3 | 5733 | continue |
5734 | fi | |
c4a375b3 | 5735 | if ! _deactivate "$_d_dm" "$_d_type"; then |
86c017ec | 5736 | return 1 |
5737 | fi | |
3f4513b3 | 5738 | done |
0c00e870 | 5739 | } |
5740 | ||
4c3b3608 | 5741 | # Detect profile file if not specified as environment variable |
5742 | _detect_profile() { | |
4c2a3841 | 5743 | if [ -n "$PROFILE" -a -f "$PROFILE" ]; then |
4c3b3608 | 5744 | echo "$PROFILE" |
5745 | return | |
5746 | fi | |
5747 | ||
4c3b3608 | 5748 | DETECTED_PROFILE='' |
4c3b3608 | 5749 | SHELLTYPE="$(basename "/$SHELL")" |
5750 | ||
4c2a3841 | 5751 | if [ "$SHELLTYPE" = "bash" ]; then |
5752 | if [ -f "$HOME/.bashrc" ]; then | |
4c3b3608 | 5753 | DETECTED_PROFILE="$HOME/.bashrc" |
4c2a3841 | 5754 | elif [ -f "$HOME/.bash_profile" ]; then |
4c3b3608 | 5755 | DETECTED_PROFILE="$HOME/.bash_profile" |
5756 | fi | |
4c2a3841 | 5757 | elif [ "$SHELLTYPE" = "zsh" ]; then |
4c3b3608 | 5758 | DETECTED_PROFILE="$HOME/.zshrc" |
5759 | fi | |
5760 | ||
4c2a3841 | 5761 | if [ -z "$DETECTED_PROFILE" ]; then |
5762 | if [ -f "$HOME/.profile" ]; then | |
4c3b3608 | 5763 | DETECTED_PROFILE="$HOME/.profile" |
4c2a3841 | 5764 | elif [ -f "$HOME/.bashrc" ]; then |
4c3b3608 | 5765 | DETECTED_PROFILE="$HOME/.bashrc" |
4c2a3841 | 5766 | elif [ -f "$HOME/.bash_profile" ]; then |
4c3b3608 | 5767 | DETECTED_PROFILE="$HOME/.bash_profile" |
4c2a3841 | 5768 | elif [ -f "$HOME/.zshrc" ]; then |
4c3b3608 | 5769 | DETECTED_PROFILE="$HOME/.zshrc" |
5770 | fi | |
5771 | fi | |
5772 | ||
1be222f6 | 5773 | echo "$DETECTED_PROFILE" |
4c3b3608 | 5774 | } |
5775 | ||
5776 | _initconf() { | |
5777 | _initpath | |
4c2a3841 | 5778 | if [ ! -f "$ACCOUNT_CONF_PATH" ]; then |
0ca5b799 | 5779 | echo " |
d404e92d | 5780 | |
d0871bda | 5781 | #LOG_FILE=\"$DEFAULT_LOG_FILE\" |
6b500036 | 5782 | #LOG_LEVEL=1 |
5ea6e9c9 | 5783 | |
251d1c5c | 5784 | #AUTO_UPGRADE=\"1\" |
89002ed2 | 5785 | |
569d6c55 | 5786 | #NO_TIMESTAMP=1 |
5b771039 | 5787 | |
d5ec5f80 | 5788 | " >"$ACCOUNT_CONF_PATH" |
4c3b3608 | 5789 | fi |
5790 | } | |
5791 | ||
c8e9a31e | 5792 | # nocron |
c60883ef | 5793 | _precheck() { |
c8e9a31e | 5794 | _nocron="$1" |
4c2a3841 | 5795 | |
5796 | if ! _exists "curl" && ! _exists "wget"; then | |
c60883ef | 5797 | _err "Please install curl or wget first, we need to access http resources." |
4c3b3608 | 5798 | return 1 |
5799 | fi | |
4c2a3841 | 5800 | |
5801 | if [ -z "$_nocron" ]; then | |
415f375c | 5802 | if ! _exists "crontab" && ! _exists "fcrontab"; then |
77f96b38 | 5803 | if _exists cygpath && _exists schtasks.exe; then |
5804 | _info "It seems you are on Windows, we will install Windows scheduler task." | |
5805 | else | |
5806 | _err "It is recommended to install crontab first. try to install 'cron, crontab, crontabs or vixie-cron'." | |
5807 | _err "We need to set cron job to renew the certs automatically." | |
5808 | _err "Otherwise, your certs will not be able to be renewed automatically." | |
5809 | if [ -z "$FORCE" ]; then | |
5810 | _err "Please add '--force' and try install again to go without crontab." | |
5811 | _err "./$PROJECT_ENTRY --install --force" | |
5812 | return 1 | |
5813 | fi | |
c8e9a31e | 5814 | fi |
77546ea5 | 5815 | fi |
4c3b3608 | 5816 | fi |
4c2a3841 | 5817 | |
d8ba26e6 | 5818 | if ! _exists "${ACME_OPENSSL_BIN:-openssl}"; then |
851fedf7 | 5819 | _err "Please install openssl first. ACME_OPENSSL_BIN=$ACME_OPENSSL_BIN" |
c60883ef | 5820 | _err "We need openssl to generate keys." |
4c3b3608 | 5821 | return 1 |
5822 | fi | |
4c2a3841 | 5823 | |
3794b5cb | 5824 | if ! _exists "socat"; then |
5825 | _err "It is recommended to install socat first." | |
5826 | _err "We use socat for standalone server if you use standalone mode." | |
c60883ef | 5827 | _err "If you don't use standalone mode, just ignore this warning." |
5828 | fi | |
4c2a3841 | 5829 | |
c60883ef | 5830 | return 0 |
5831 | } | |
5832 | ||
0a7c9364 | 5833 | _setShebang() { |
5834 | _file="$1" | |
5835 | _shebang="$2" | |
4c2a3841 | 5836 | if [ -z "$_shebang" ]; then |
43822d37 | 5837 | _usage "Usage: file shebang" |
0a7c9364 | 5838 | return 1 |
5839 | fi | |
5840 | cp "$_file" "$_file.tmp" | |
4c2a3841 | 5841 | echo "$_shebang" >"$_file" |
5842 | sed -n 2,99999p "$_file.tmp" >>"$_file" | |
5843 | rm -f "$_file.tmp" | |
0a7c9364 | 5844 | } |
5845 | ||
27dbe77f | 5846 | #confighome |
94dc5f33 | 5847 | _installalias() { |
27dbe77f | 5848 | _c_home="$1" |
94dc5f33 | 5849 | _initpath |
5850 | ||
5851 | _envfile="$LE_WORKING_DIR/$PROJECT_ENTRY.env" | |
4c2a3841 | 5852 | if [ "$_upgrading" ] && [ "$_upgrading" = "1" ]; then |
44edb2bd | 5853 | echo "$(cat "$_envfile")" | sed "s|^LE_WORKING_DIR.*$||" >"$_envfile" |
5854 | echo "$(cat "$_envfile")" | sed "s|^alias le.*$||" >"$_envfile" | |
5855 | echo "$(cat "$_envfile")" | sed "s|^alias le.sh.*$||" >"$_envfile" | |
94dc5f33 | 5856 | fi |
5857 | ||
27dbe77f | 5858 | if [ "$_c_home" ]; then |
be83a6a3 | 5859 | _c_entry=" --config-home '$_c_home'" |
27dbe77f | 5860 | fi |
5861 | ||
1786a5e5 | 5862 | _setopt "$_envfile" "export LE_WORKING_DIR" "=" "\"$LE_WORKING_DIR\"" |
f5b546b3 | 5863 | if [ "$_c_home" ]; then |
5864 | _setopt "$_envfile" "export LE_CONFIG_HOME" "=" "\"$LE_CONFIG_HOME\"" | |
d04434e3 | 5865 | else |
5866 | _sed_i "/^export LE_CONFIG_HOME/d" "$_envfile" | |
f5b546b3 | 5867 | fi |
be83a6a3 | 5868 | _setopt "$_envfile" "alias $PROJECT_ENTRY" "=" "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\"" |
94dc5f33 | 5869 | |
5870 | _profile="$(_detect_profile)" | |
4c2a3841 | 5871 | if [ "$_profile" ]; then |
94dc5f33 | 5872 | _debug "Found profile: $_profile" |
aba5c634 | 5873 | _info "Installing alias to '$_profile'" |
94dc5f33 | 5874 | _setopt "$_profile" ". \"$_envfile\"" |
5875 | _info "OK, Close and reopen your terminal to start using $PROJECT_NAME" | |
5876 | else | |
5877 | _info "No profile is found, you will need to go into $LE_WORKING_DIR to use $PROJECT_NAME" | |
5878 | fi | |
94dc5f33 | 5879 | |
5880 | #for csh | |
5881 | _cshfile="$LE_WORKING_DIR/$PROJECT_ENTRY.csh" | |
94dc5f33 | 5882 | _csh_profile="$HOME/.cshrc" |
4c2a3841 | 5883 | if [ -f "$_csh_profile" ]; then |
aba5c634 | 5884 | _info "Installing alias to '$_csh_profile'" |
6626371d | 5885 | _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\"" |
f5b546b3 | 5886 | if [ "$_c_home" ]; then |
5887 | _setopt "$_cshfile" "setenv LE_CONFIG_HOME" " " "\"$LE_CONFIG_HOME\"" | |
d04434e3 | 5888 | else |
5889 | _sed_i "/^setenv LE_CONFIG_HOME/d" "$_cshfile" | |
f5b546b3 | 5890 | fi |
be83a6a3 | 5891 | _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\"" |
4c2a3841 | 5892 | _setopt "$_csh_profile" "source \"$_cshfile\"" |
94dc5f33 | 5893 | fi |
4c2a3841 | 5894 | |
acafa585 | 5895 | #for tcsh |
5896 | _tcsh_profile="$HOME/.tcshrc" | |
4c2a3841 | 5897 | if [ -f "$_tcsh_profile" ]; then |
aba5c634 | 5898 | _info "Installing alias to '$_tcsh_profile'" |
acafa585 | 5899 | _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\"" |
f5b546b3 | 5900 | if [ "$_c_home" ]; then |
5901 | _setopt "$_cshfile" "setenv LE_CONFIG_HOME" " " "\"$LE_CONFIG_HOME\"" | |
5902 | fi | |
be83a6a3 | 5903 | _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY$_c_entry\"" |
4c2a3841 | 5904 | _setopt "$_tcsh_profile" "source \"$_cshfile\"" |
acafa585 | 5905 | fi |
94dc5f33 | 5906 | |
5907 | } | |
5908 | ||
86ef0a26 | 5909 | # nocron confighome noprofile |
c60883ef | 5910 | install() { |
f3e4cea3 | 5911 | |
4c2a3841 | 5912 | if [ -z "$LE_WORKING_DIR" ]; then |
f3e4cea3 | 5913 | LE_WORKING_DIR="$DEFAULT_INSTALL_HOME" |
5914 | fi | |
4c2a3841 | 5915 | |
c8e9a31e | 5916 | _nocron="$1" |
27dbe77f | 5917 | _c_home="$2" |
86ef0a26 | 5918 | _noprofile="$3" |
4c2a3841 | 5919 | if ! _initpath; then |
c60883ef | 5920 | _err "Install failed." |
4c3b3608 | 5921 | return 1 |
5922 | fi | |
4c2a3841 | 5923 | if [ "$_nocron" ]; then |
52677b0a | 5924 | _debug "Skip install cron job" |
5925 | fi | |
4c2a3841 | 5926 | |
c6b68551 | 5927 | if [ "$ACME_IN_CRON" != "1" ]; then |
4356eefb | 5928 | if ! _precheck "$_nocron"; then |
5929 | _err "Pre-check failed, can not install." | |
5930 | return 1 | |
5931 | fi | |
4c3b3608 | 5932 | fi |
4c2a3841 | 5933 | |
8e845d9f | 5934 | if [ -z "$_c_home" ] && [ "$LE_CONFIG_HOME" != "$LE_WORKING_DIR" ]; then |
5935 | _info "Using config home: $LE_CONFIG_HOME" | |
5936 | _c_home="$LE_CONFIG_HOME" | |
5937 | fi | |
5938 | ||
6cc11ffb | 5939 | #convert from le |
4c2a3841 | 5940 | if [ -d "$HOME/.le" ]; then |
5941 | for envfile in "le.env" "le.sh.env"; do | |
5942 | if [ -f "$HOME/.le/$envfile" ]; then | |
5943 | if grep "le.sh" "$HOME/.le/$envfile" >/dev/null; then | |
5944 | _upgrading="1" | |
5945 | _info "You are upgrading from le.sh" | |
5946 | _info "Renaming \"$HOME/.le\" to $LE_WORKING_DIR" | |
5947 | mv "$HOME/.le" "$LE_WORKING_DIR" | |
5948 | mv "$LE_WORKING_DIR/$envfile" "$LE_WORKING_DIR/$PROJECT_ENTRY.env" | |
5949 | break | |
6cc11ffb | 5950 | fi |
5951 | fi | |
5952 | done | |
5953 | fi | |
5954 | ||
4c3b3608 | 5955 | _info "Installing to $LE_WORKING_DIR" |
635695ec | 5956 | |
d04434e3 | 5957 | if [ ! -d "$LE_WORKING_DIR" ]; then |
5958 | if ! mkdir -p "$LE_WORKING_DIR"; then | |
5959 | _err "Can not create working dir: $LE_WORKING_DIR" | |
5960 | return 1 | |
5961 | fi | |
5962 | ||
5963 | chmod 700 "$LE_WORKING_DIR" | |
4a0f23e2 | 5964 | fi |
4c2a3841 | 5965 | |
d04434e3 | 5966 | if [ ! -d "$LE_CONFIG_HOME" ]; then |
5967 | if ! mkdir -p "$LE_CONFIG_HOME"; then | |
5968 | _err "Can not create config dir: $LE_CONFIG_HOME" | |
5969 | return 1 | |
5970 | fi | |
762978f8 | 5971 | |
d04434e3 | 5972 | chmod 700 "$LE_CONFIG_HOME" |
27dbe77f | 5973 | fi |
5974 | ||
d5ec5f80 | 5975 | cp "$PROJECT_ENTRY" "$LE_WORKING_DIR/" && chmod +x "$LE_WORKING_DIR/$PROJECT_ENTRY" |
4c3b3608 | 5976 | |
4c2a3841 | 5977 | if [ "$?" != "0" ]; then |
a7b7355d | 5978 | _err "Install failed, can not copy $PROJECT_ENTRY" |
4c3b3608 | 5979 | return 1 |
5980 | fi | |
5981 | ||
a7b7355d | 5982 | _info "Installed to $LE_WORKING_DIR/$PROJECT_ENTRY" |
4c3b3608 | 5983 | |
c6b68551 | 5984 | if [ "$ACME_IN_CRON" != "1" ] && [ -z "$_noprofile" ]; then |
4356eefb | 5985 | _installalias "$_c_home" |
5986 | fi | |
4c3b3608 | 5987 | |
4c2a3841 | 5988 | for subf in $_SUB_FOLDERS; do |
5989 | if [ -d "$subf" ]; then | |
d5ec5f80 | 5990 | mkdir -p "$LE_WORKING_DIR/$subf" |
5991 | cp "$subf"/* "$LE_WORKING_DIR"/"$subf"/ | |
a61fe418 | 5992 | fi |
5993 | done | |
5994 | ||
4c2a3841 | 5995 | if [ ! -f "$ACCOUNT_CONF_PATH" ]; then |
4c3b3608 | 5996 | _initconf |
5997 | fi | |
6cc11ffb | 5998 | |
4c2a3841 | 5999 | if [ "$_DEFAULT_ACCOUNT_CONF_PATH" != "$ACCOUNT_CONF_PATH" ]; then |
635695ec | 6000 | _setopt "$_DEFAULT_ACCOUNT_CONF_PATH" "ACCOUNT_CONF_PATH" "=" "\"$ACCOUNT_CONF_PATH\"" |
6cc11ffb | 6001 | fi |
6002 | ||
4c2a3841 | 6003 | if [ "$_DEFAULT_CERT_HOME" != "$CERT_HOME" ]; then |
b2817897 | 6004 | _saveaccountconf "CERT_HOME" "$CERT_HOME" |
6005 | fi | |
6006 | ||
4c2a3841 | 6007 | if [ "$_DEFAULT_ACCOUNT_KEY_PATH" != "$ACCOUNT_KEY_PATH" ]; then |
b2817897 | 6008 | _saveaccountconf "ACCOUNT_KEY_PATH" "$ACCOUNT_KEY_PATH" |
6009 | fi | |
4c2a3841 | 6010 | |
6011 | if [ -z "$_nocron" ]; then | |
27dbe77f | 6012 | installcronjob "$_c_home" |
c8e9a31e | 6013 | fi |
0a7c9364 | 6014 | |
4c2a3841 | 6015 | if [ -z "$NO_DETECT_SH" ]; then |
641989fd | 6016 | #Modify shebang |
4c2a3841 | 6017 | if _exists bash; then |
694af4ae | 6018 | _bash_path="$(bash -c "command -v bash 2>/dev/null")" |
6019 | if [ -z "$_bash_path" ]; then | |
6020 | _bash_path="$(bash -c 'echo $SHELL')" | |
6021 | fi | |
6022 | fi | |
6023 | if [ "$_bash_path" ]; then | |
329174b6 | 6024 | _info "Good, bash is found, so change the shebang to use bash as preferred." |
694af4ae | 6025 | _shebang='#!'"$_bash_path" |
641989fd | 6026 | _setShebang "$LE_WORKING_DIR/$PROJECT_ENTRY" "$_shebang" |
4c2a3841 | 6027 | for subf in $_SUB_FOLDERS; do |
6028 | if [ -d "$LE_WORKING_DIR/$subf" ]; then | |
6029 | for _apifile in "$LE_WORKING_DIR/$subf/"*.sh; do | |
a61fe418 | 6030 | _setShebang "$_apifile" "$_shebang" |
6031 | done | |
6032 | fi | |
6033 | done | |
0a7c9364 | 6034 | fi |
6035 | fi | |
6036 | ||
4c3b3608 | 6037 | _info OK |
6038 | } | |
6039 | ||
52677b0a | 6040 | # nocron |
4c3b3608 | 6041 | uninstall() { |
52677b0a | 6042 | _nocron="$1" |
4c2a3841 | 6043 | if [ -z "$_nocron" ]; then |
52677b0a | 6044 | uninstallcronjob |
6045 | fi | |
4c3b3608 | 6046 | _initpath |
6047 | ||
9aa3be7f | 6048 | _uninstallalias |
4c2a3841 | 6049 | |
d5ec5f80 | 6050 | rm -f "$LE_WORKING_DIR/$PROJECT_ENTRY" |
f5b546b3 | 6051 | _info "The keys and certs are in \"$(__green "$LE_CONFIG_HOME")\", you can remove them by yourself." |
9aa3be7f | 6052 | |
6053 | } | |
6054 | ||
6055 | _uninstallalias() { | |
6056 | _initpath | |
6057 | ||
4c3b3608 | 6058 | _profile="$(_detect_profile)" |
4c2a3841 | 6059 | if [ "$_profile" ]; then |
9aa3be7f | 6060 | _info "Uninstalling alias from: '$_profile'" |
d5ec5f80 | 6061 | text="$(cat "$_profile")" |
4c2a3841 | 6062 | echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.env\"$||" >"$_profile" |
4c3b3608 | 6063 | fi |
6064 | ||
94dc5f33 | 6065 | _csh_profile="$HOME/.cshrc" |
4c2a3841 | 6066 | if [ -f "$_csh_profile" ]; then |
9aa3be7f | 6067 | _info "Uninstalling alias from: '$_csh_profile'" |
d5ec5f80 | 6068 | text="$(cat "$_csh_profile")" |
4c2a3841 | 6069 | echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" >"$_csh_profile" |
94dc5f33 | 6070 | fi |
4c2a3841 | 6071 | |
acafa585 | 6072 | _tcsh_profile="$HOME/.tcshrc" |
4c2a3841 | 6073 | if [ -f "$_tcsh_profile" ]; then |
9aa3be7f | 6074 | _info "Uninstalling alias from: '$_csh_profile'" |
d5ec5f80 | 6075 | text="$(cat "$_tcsh_profile")" |
4c2a3841 | 6076 | echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" >"$_tcsh_profile" |
acafa585 | 6077 | fi |
4c3b3608 | 6078 | |
6079 | } | |
6080 | ||
6081 | cron() { | |
c6b68551 | 6082 | export ACME_IN_CRON=1 |
89002ed2 | 6083 | _initpath |
d8ba26e6 | 6084 | _info "$(__green "===Starting cron===")" |
4c2a3841 | 6085 | if [ "$AUTO_UPGRADE" = "1" ]; then |
89002ed2 | 6086 | export LE_WORKING_DIR |
6087 | ( | |
4c2a3841 | 6088 | if ! upgrade; then |
6089 | _err "Cron:Upgrade failed!" | |
6090 | return 1 | |
6091 | fi | |
89002ed2 | 6092 | ) |
d5ec5f80 | 6093 | . "$LE_WORKING_DIR/$PROJECT_ENTRY" >/dev/null |
1ab63043 | 6094 | |
4c2a3841 | 6095 | if [ -t 1 ]; then |
1ab63043 | 6096 | __INTERACTIVE="1" |
6097 | fi | |
4c2a3841 | 6098 | |
89002ed2 | 6099 | _info "Auto upgraded to: $VER" |
6100 | fi | |
4c3b3608 | 6101 | renewAll |
cc179731 | 6102 | _ret="$?" |
c6b68551 | 6103 | ACME_IN_CRON="" |
d8ba26e6 | 6104 | _info "$(__green "===End cron===")" |
0ba95a3d | 6105 | exit $_ret |
4c3b3608 | 6106 | } |
6107 | ||
6108 | version() { | |
a63b05a9 | 6109 | echo "$PROJECT" |
6110 | echo "v$VER" | |
4c3b3608 | 6111 | } |
6112 | ||
b50e701c | 6113 | # subject content hooks code |
6114 | _send_notify() { | |
6115 | _nsubject="$1" | |
6116 | _ncontent="$2" | |
6117 | _nhooks="$3" | |
6118 | _nerror="$4" | |
6119 | ||
6120 | if [ "$NOTIFY_LEVEL" = "$NOTIFY_LEVEL_DISABLE" ]; then | |
6121 | _debug "The NOTIFY_LEVEL is $NOTIFY_LEVEL, disabled, just return." | |
6122 | return 0 | |
6123 | fi | |
6124 | ||
6125 | if [ -z "$_nhooks" ]; then | |
6126 | _debug "The NOTIFY_HOOK is empty, just return." | |
6127 | return 0 | |
6128 | fi | |
6129 | ||
6130 | _send_err=0 | |
6131 | for _n_hook in $(echo "$_nhooks" | tr ',' " "); do | |
6132 | _n_hook_file="$(_findHook "" $_SUB_FOLDER_NOTIFY "$_n_hook")" | |
143eac09 | 6133 | _info "Sending via: $_n_hook" |
6134 | _debug "Found $_n_hook_file for $_n_hook" | |
75191e71 | 6135 | if [ -z "$_n_hook_file" ]; then |
6136 | _err "Can not find the hook file for $_n_hook" | |
6137 | continue | |
6138 | fi | |
b50e701c | 6139 | if ! ( |
6140 | if ! . "$_n_hook_file"; then | |
6141 | _err "Load file $_n_hook_file error. Please check your api file and try again." | |
6142 | return 1 | |
6143 | fi | |
6144 | ||
6145 | d_command="${_n_hook}_send" | |
6146 | if ! _exists "$d_command"; then | |
6147 | _err "It seems that your api file is not correct, it must have a function named: $d_command" | |
6148 | return 1 | |
6149 | fi | |
6150 | ||
6151 | if ! $d_command "$_nsubject" "$_ncontent" "$_nerror"; then | |
6152 | _err "Error send message by $d_command" | |
6153 | return 1 | |
6154 | fi | |
6155 | ||
6156 | return 0 | |
6157 | ); then | |
6158 | _err "Set $_n_hook_file error." | |
6159 | _send_err=1 | |
6160 | else | |
6161 | _info "$_n_hook $(__green Success)" | |
6162 | fi | |
6163 | done | |
6164 | return $_send_err | |
6165 | ||
6166 | } | |
6167 | ||
6168 | # hook | |
6169 | _set_notify_hook() { | |
6170 | _nhooks="$1" | |
6171 | ||
5698bec6 | 6172 | _test_subject="Hello, this is a notification from $PROJECT_NAME" |
143eac09 | 6173 | _test_content="If you receive this message, your notification works." |
b50e701c | 6174 | |
6175 | _send_notify "$_test_subject" "$_test_content" "$_nhooks" 0 | |
6176 | ||
6177 | } | |
6178 | ||
6179 | #[hook] [level] [mode] | |
6180 | setnotify() { | |
6181 | _nhook="$1" | |
6182 | _nlevel="$2" | |
6183 | _nmode="$3" | |
6184 | ||
6185 | _initpath | |
6186 | ||
6187 | if [ -z "$_nhook$_nlevel$_nmode" ]; then | |
6188 | _usage "Usage: $PROJECT_ENTRY --set-notify [--notify-hook mailgun] [--notify-level $NOTIFY_LEVEL_DEFAULT] [--notify-mode $NOTIFY_MODE_DEFAULT]" | |
6189 | _usage "$_NOTIFY_WIKI" | |
6190 | return 1 | |
6191 | fi | |
6192 | ||
6193 | if [ "$_nlevel" ]; then | |
6194 | _info "Set notify level to: $_nlevel" | |
6195 | export "NOTIFY_LEVEL=$_nlevel" | |
6196 | _saveaccountconf "NOTIFY_LEVEL" "$NOTIFY_LEVEL" | |
6197 | fi | |
6198 | ||
6199 | if [ "$_nmode" ]; then | |
6200 | _info "Set notify mode to: $_nmode" | |
6201 | export "NOTIFY_MODE=$_nmode" | |
6202 | _saveaccountconf "NOTIFY_MODE" "$NOTIFY_MODE" | |
6203 | fi | |
6204 | ||
6205 | if [ "$_nhook" ]; then | |
6206 | _info "Set notify hook to: $_nhook" | |
6207 | if [ "$_nhook" = "$NO_VALUE" ]; then | |
6208 | _info "Clear notify hook" | |
6209 | _clearaccountconf "NOTIFY_HOOK" | |
6210 | else | |
6211 | if _set_notify_hook "$_nhook"; then | |
6212 | export NOTIFY_HOOK="$_nhook" | |
6213 | _saveaccountconf "NOTIFY_HOOK" "$NOTIFY_HOOK" | |
6214 | return 0 | |
6215 | else | |
6216 | _err "Can not set notify hook to: $_nhook" | |
6217 | return 1 | |
6218 | fi | |
6219 | fi | |
6220 | fi | |
6221 | ||
6222 | } | |
6223 | ||
4c3b3608 | 6224 | showhelp() { |
d0871bda | 6225 | _initpath |
4c3b3608 | 6226 | version |
a7b7355d | 6227 | echo "Usage: $PROJECT_ENTRY command ...[parameters].... |
a63b05a9 | 6228 | Commands: |
6229 | --help, -h Show this help message. | |
6230 | --version, -v Show version info. | |
a7b7355d | 6231 | --install Install $PROJECT_NAME to your system. |
6232 | --uninstall Uninstall $PROJECT_NAME, and uninstall the cron job. | |
d8beaf72 | 6233 | --upgrade Upgrade $PROJECT_NAME to the latest code from $PROJECT. |
a63b05a9 | 6234 | --issue Issue a cert. |
10afcaca | 6235 | --signcsr Issue a cert from an existing csr. |
a61fe418 | 6236 | --deploy Deploy the cert to your server. |
27dbe77f | 6237 | --install-cert Install the issued cert to apache/nginx or any other server. |
a63b05a9 | 6238 | --renew, -r Renew a cert. |
27dbe77f | 6239 | --renew-all Renew all the certs. |
a63b05a9 | 6240 | --revoke Revoke a cert. |
47b49f1b | 6241 | --remove Remove the cert from list of certs known to $PROJECT_NAME. |
10afcaca | 6242 | --list List all the certs. |
6243 | --showcsr Show the content of a csr. | |
27dbe77f | 6244 | --install-cronjob Install the cron job to renew certs, you don't need to call this. The 'install' command can automatically install the cron job. |
6245 | --uninstall-cronjob Uninstall the cron job. The 'uninstall' command can do this automatically. | |
a63b05a9 | 6246 | --cron Run cron job to renew all the certs. |
6247 | --toPkcs Export the certificate and key to a pfx file. | |
4410226d | 6248 | --toPkcs8 Convert to pkcs8 format. |
27dbe77f | 6249 | --update-account Update account info. |
6250 | --register-account Register account key. | |
422dd1fa | 6251 | --deactivate-account Deactivate the account. |
0984585d | 6252 | --create-account-key Create an account private key, professional use. |
6253 | --create-domain-key Create an domain private key, professional use. | |
a63b05a9 | 6254 | --createCSR, -ccsr Create CSR , professional use. |
0c00e870 | 6255 | --deactivate Deactivate the domain authz, professional use. |
b50e701c | 6256 | --set-notify Set the cron notification hook, level or mode. |
6257 | ||
3c07f57a | 6258 | |
a63b05a9 | 6259 | Parameters: |
6260 | --domain, -d domain.tld Specifies a domain, used to issue, renew or revoke etc. | |
64821ad4 | 6261 | --challenge-alias domain.tld The challenge domain alias for DNS alias mode: $_DNS_ALIAS_WIKI |
6262 | --domain-alias domain.tld The domain alias for DNS alias mode: $_DNS_ALIAS_WIKI | |
a63b05a9 | 6263 | --force, -f Used to force to install or force to renew a cert immediately. |
6264 | --staging, --test Use staging server, just for test. | |
6265 | --debug Output debug info. | |
3c98fae4 | 6266 | --output-insecure Output all the sensitive messages. By default all the credentials/sensitive messages are hidden from the output/debug/log for security. |
a63b05a9 | 6267 | --webroot, -w /path/to/webroot Specifies the web root folder for web root mode. |
6268 | --standalone Use standalone mode. | |
08681f4a | 6269 | --alpn Use standalone alpn mode. |
0e44f587 | 6270 | --stateless Use stateless mode, see: $_STATELESS_WIKI |
a63b05a9 | 6271 | --apache Use apache mode. |
eccec5f6 | 6272 | --dns [dns_cf|dns_dp|dns_cx|/path/to/api/file] Use dns mode or dns api. |
f84a87f2 | 6273 | --dnssleep 300 The time in seconds to wait for all the txt records to take effect in dns api mode. It's not necessary to use this by default, $PROJECT_NAME polls dns status automatically. |
3c07f57a | 6274 | |
3c98fae4 | 6275 | --keylength, -k [2048] Specifies the domain key length: 2048, 3072, 4096, 8192 or ec-256, ec-384, ec-521. |
b6552aff | 6276 | --accountkeylength, -ak [2048] Specifies the account key length: 2048, 3072, 4096 |
d0871bda | 6277 | --log [/path/to/logfile] Specifies the log file. The default is: \"$DEFAULT_LOG_FILE\" if you don't give a file path here. |
a73c5b33 | 6278 | --log-level 1|2 Specifies the log level, default is 1. |
52765466 | 6279 | --syslog [0|3|6|7] Syslog level, 0: disable syslog, 3: error, 6: info, 7: debug. |
3c07f57a | 6280 | |
7903fcb4 | 6281 | These parameters are to install the cert to nginx/apache or any other server after issue/renew a cert: |
3c07f57a | 6282 | |
13fe54c9 | 6283 | --cert-file After issue/renew, the cert will be copied to this path. |
6284 | --key-file After issue/renew, the key will be copied to this path. | |
6285 | --ca-file After issue/renew, the intermediate cert will be copied to this path. | |
6286 | --fullchain-file After issue/renew, the fullchain cert will be copied to this path. | |
3c07f57a | 6287 | |
a63b05a9 | 6288 | --reloadcmd \"service nginx reload\" After issue/renew, it's used to reload the server. |
6289 | ||
3c98fae4 | 6290 | --server SERVER ACME Directory Resource URI. (default: $DEFAULT_CA) |
a63b05a9 | 6291 | --accountconf Specifies a customized account config file. |
5fee82ce | 6292 | --home Specifies the home dir for $PROJECT_NAME. |
27dbe77f | 6293 | --cert-home Specifies the home dir to save all the certs, only valid for '--install' command. |
6294 | --config-home Specifies the home dir to save all the configurations. | |
635695ec | 6295 | --useragent Specifies the user agent string. it will be saved for future use too. |
9082862b C |
6296 | --accountemail Specifies the account email, only valid for the '--install' and '--update-account' command. |
6297 | --accountkey Specifies the account key path, only valid for the '--install' command. | |
ec67a1b2 | 6298 | --days Specifies the days to renew the cert when using '--issue' command. The default value is $DEFAULT_RENEW days. |
39c8f79f | 6299 | --httpport Specifies the standalone listening port. Only valid if the server is behind a reverse proxy or load balancer. |
79a0a66f | 6300 | --tlsport Specifies the standalone tls listening port. Only valid if the server is behind a reverse proxy or load balancer. |
6ae0f7f5 | 6301 | --local-address Specifies the standalone/tls server listening address, in case you have multiple ip addresses. |
dcf4f8f6 | 6302 | --listraw Only used for '--list' command, list the certs in raw format. |
27dbe77f | 6303 | --stopRenewOnError, -se Only valid for '--renew-all' command. Stop if one cert has error in renewal. |
13d7cae9 | 6304 | --insecure Do not check the server certificate, in some devices, the api server's certificate may not be trusted. |
8f73e241 | 6305 | --ca-bundle Specifies the path to the CA certificate bundle to verify api server's certificate. |
13fe54c9 | 6306 | --ca-path Specifies directory containing CA certificates in PEM format, used by wget or curl. |
bc96082f | 6307 | --nocron Only valid for '--install' command, which means: do not install the default cron job. In this case, the certs will not be renewed automatically. |
61556a54 | 6308 | --noprofile Only valid for '--install' command, which means: do not install aliases to user profile. |
08b4e1a7 | 6309 | --no-color Do not output color text. |
e32b3aac | 6310 | --force-color Force output of color text. Useful for non-interactive use with the aha tool for HTML E-Mails. |
27dbe77f | 6311 | --ecc Specifies to use the ECC cert. Valid for '--install-cert', '--renew', '--revoke', '--toPkcs' and '--createCSR' |
10afcaca | 6312 | --csr Specifies the input csr. |
b0070f03 | 6313 | --pre-hook Command to be run before obtaining any certificates. |
84a6730b | 6314 | --post-hook Command to be run after attempting to obtain/renew certificates. No matter the obtain/renew is success or failed. |
b0070f03 | 6315 | --renew-hook Command to be run once for each successfully renewed certificate. |
a61fe418 | 6316 | --deploy-hook The hook file to deploy cert |
0c9546cc | 6317 | --ocsp-must-staple, --ocsp Generate ocsp must Staple extension. |
c4b2e582 | 6318 | --always-force-new-domain-key Generate new domain key when renewal. Otherwise, the domain key is not changed by default. |
6bf281f9 | 6319 | --auto-upgrade [0|1] Valid for '--upgrade' command, indicating whether to upgrade automatically in future. |
6ae0f7f5 | 6320 | --listen-v4 Force standalone/tls server to listen at ipv4. |
6321 | --listen-v6 Force standalone/tls server to listen at ipv6. | |
a746139c | 6322 | --openssl-bin Specifies a custom openssl bin location. |
9b124070 | 6323 | --use-wget Force to use wget, if you have both curl and wget installed. |
a0923622 | 6324 | --yes-I-know-dns-manual-mode-enough-go-ahead-please Force to use dns manual mode: $_DNS_MANUAL_WIKI |
66686de4 | 6325 | --branch, -b Only valid for '--upgrade' command, specifies the branch name to upgrade to. |
b50e701c | 6326 | |
6327 | --notify-level 0|1|2|3 Set the notification level: Default value is $NOTIFY_LEVEL_DEFAULT. | |
ac9f6e3a | 6328 | 0: disabled, no notification will be sent. |
c6b68551 | 6329 | 1: send notifications only when there is an error. |
6330 | 2: send notifications when a cert is successfully renewed, or there is an error. | |
6331 | 3: send notifications when a cert is skipped, renewed, or error. | |
b50e701c | 6332 | --notify-mode 0|1 Set notification mode. Default value is $NOTIFY_MODE_DEFAULT. |
c6b68551 | 6333 | 0: Bulk mode. Send all the domain's notifications in one message(mail). |
b50e701c | 6334 | 1: Cert mode. Send a message for every single cert. |
6335 | --notify-hook [hookname] Set the notify hook | |
1041c9f9 | 6336 | --revoke-reason [0-10] The reason for '--revoke' command. See: $_REVOKE_WIKI |
b50e701c | 6337 | |
6338 | " | |
4c3b3608 | 6339 | } |
6340 | ||
86ef0a26 | 6341 | # nocron noprofile |
4a0f23e2 | 6342 | _installOnline() { |
6343 | _info "Installing from online archive." | |
52677b0a | 6344 | _nocron="$1" |
86ef0a26 | 6345 | _noprofile="$2" |
4c2a3841 | 6346 | if [ ! "$BRANCH" ]; then |
4a0f23e2 | 6347 | BRANCH="master" |
6348 | fi | |
a8df88ab | 6349 | |
4a0f23e2 | 6350 | target="$PROJECT/archive/$BRANCH.tar.gz" |
6351 | _info "Downloading $target" | |
6352 | localname="$BRANCH.tar.gz" | |
4c2a3841 | 6353 | if ! _get "$target" >$localname; then |
df9547ae | 6354 | _err "Download error." |
4a0f23e2 | 6355 | return 1 |
6356 | fi | |
0bbe6eef | 6357 | ( |
4c2a3841 | 6358 | _info "Extracting $localname" |
3a3b0dd5 | 6359 | if ! (tar xzf $localname || gtar xzf $localname); then |
6360 | _err "Extraction error." | |
6361 | exit 1 | |
6362 | fi | |
4c2a3841 | 6363 | |
6364 | cd "$PROJECT_NAME-$BRANCH" | |
6365 | chmod +x $PROJECT_ENTRY | |
86ef0a26 | 6366 | if ./$PROJECT_ENTRY install "$_nocron" "" "$_noprofile"; then |
4c2a3841 | 6367 | _info "Install success!" |
ac3667c7 | 6368 | _initpath |
cb7e3857 | 6369 | _saveaccountconf "UPGRADE_HASH" "$(_getUpgradeHash)" |
4c2a3841 | 6370 | fi |
6371 | ||
6372 | cd .. | |
6373 | ||
6374 | rm -rf "$PROJECT_NAME-$BRANCH" | |
6375 | rm -f "$localname" | |
0bbe6eef | 6376 | ) |
4a0f23e2 | 6377 | } |
6378 | ||
cb7e3857 OB |
6379 | _getRepoHash() { |
6380 | _hash_path=$1 | |
6381 | shift | |
6382 | _hash_url="https://api.github.com/repos/acmesh-official/$PROJECT_NAME/git/refs/$_hash_path" | |
6383 | _get $_hash_url | tr -d "\r\n" | tr '{},' '\n' | grep '"sha":' | cut -d '"' -f 4 | |
6384 | } | |
6385 | ||
6386 | _getUpgradeHash() { | |
f716f606 | 6387 | _b="$BRANCH" |
6388 | if [ -z "$_b" ]; then | |
6389 | _b="master" | |
6390 | fi | |
cb7e3857 OB |
6391 | _hash=$(_getRepoHash "heads/$_b") |
6392 | if [ -z "$_hash" ]; then _hash=$(_getRepoHash "tags/$_b"); fi | |
6393 | echo $_hash | |
7a3c61b7 | 6394 | } |
6395 | ||
52677b0a | 6396 | upgrade() { |
6397 | if ( | |
267f283a | 6398 | _initpath |
cb7e3857 | 6399 | [ -z "$FORCE" ] && [ "$(_getUpgradeHash)" = "$(_readaccountconf "UPGRADE_HASH")" ] && _info "Already uptodate!" && exit 0 |
267f283a | 6400 | export LE_WORKING_DIR |
d0b748a4 | 6401 | cd "$LE_WORKING_DIR" |
86ef0a26 | 6402 | _installOnline "nocron" "noprofile" |
4c2a3841 | 6403 | ); then |
52677b0a | 6404 | _info "Upgrade success!" |
096d8992 | 6405 | exit 0 |
52677b0a | 6406 | else |
6407 | _err "Upgrade failed!" | |
096d8992 | 6408 | exit 1 |
52677b0a | 6409 | fi |
6410 | } | |
a63b05a9 | 6411 | |
5ea6e9c9 | 6412 | _processAccountConf() { |
4c2a3841 | 6413 | if [ "$_useragent" ]; then |
5ea6e9c9 | 6414 | _saveaccountconf "USER_AGENT" "$_useragent" |
4c2a3841 | 6415 | elif [ "$USER_AGENT" ] && [ "$USER_AGENT" != "$DEFAULT_USER_AGENT" ]; then |
d0871bda | 6416 | _saveaccountconf "USER_AGENT" "$USER_AGENT" |
5ea6e9c9 | 6417 | fi |
4c2a3841 | 6418 | |
6419 | if [ "$_accountemail" ]; then | |
5ea6e9c9 | 6420 | _saveaccountconf "ACCOUNT_EMAIL" "$_accountemail" |
4c2a3841 | 6421 | elif [ "$ACCOUNT_EMAIL" ] && [ "$ACCOUNT_EMAIL" != "$DEFAULT_ACCOUNT_EMAIL" ]; then |
d0871bda | 6422 | _saveaccountconf "ACCOUNT_EMAIL" "$ACCOUNT_EMAIL" |
5ea6e9c9 | 6423 | fi |
4c2a3841 | 6424 | |
a746139c | 6425 | if [ "$_openssl_bin" ]; then |
851fedf7 | 6426 | _saveaccountconf "ACME_OPENSSL_BIN" "$_openssl_bin" |
6427 | elif [ "$ACME_OPENSSL_BIN" ] && [ "$ACME_OPENSSL_BIN" != "$DEFAULT_OPENSSL_BIN" ]; then | |
6428 | _saveaccountconf "ACME_OPENSSL_BIN" "$ACME_OPENSSL_BIN" | |
a746139c | 6429 | fi |
6430 | ||
4c2a3841 | 6431 | if [ "$_auto_upgrade" ]; then |
6bf281f9 | 6432 | _saveaccountconf "AUTO_UPGRADE" "$_auto_upgrade" |
4c2a3841 | 6433 | elif [ "$AUTO_UPGRADE" ]; then |
6bf281f9 | 6434 | _saveaccountconf "AUTO_UPGRADE" "$AUTO_UPGRADE" |
6435 | fi | |
4c2a3841 | 6436 | |
9b124070 | 6437 | if [ "$_use_wget" ]; then |
6438 | _saveaccountconf "ACME_USE_WGET" "$_use_wget" | |
6439 | elif [ "$ACME_USE_WGET" ]; then | |
6440 | _saveaccountconf "ACME_USE_WGET" "$ACME_USE_WGET" | |
6441 | fi | |
6442 | ||
5ea6e9c9 | 6443 | } |
6444 | ||
5bdfdfef | 6445 | _checkSudo() { |
6446 | if [ "$SUDO_GID" ] && [ "$SUDO_COMMAND" ] && [ "$SUDO_USER" ] && [ "$SUDO_UID" ]; then | |
6447 | if [ "$SUDO_USER" = "root" ] && [ "$SUDO_UID" = "0" ]; then | |
6448 | #it's root using sudo, no matter it's using sudo or not, just fine | |
6449 | return 0 | |
6450 | fi | |
815a3be4 | 6451 | if [ "$SUDO_COMMAND" = "/bin/su" ] || [ "$SUDO_COMMAND" = "/bin/bash" ]; then |
6452 | #it's a normal user doing "sudo su", or `sudo -i` or `sudo -s` | |
5bdfdfef | 6453 | #fine |
6454 | return 0 | |
6455 | fi | |
6456 | #otherwise | |
6457 | return 1 | |
6458 | fi | |
6459 | return 0 | |
6460 | } | |
6461 | ||
a63b05a9 | 6462 | _process() { |
6463 | _CMD="" | |
6464 | _domain="" | |
3f4513b3 | 6465 | _altdomains="$NO_VALUE" |
a63b05a9 | 6466 | _webroot="" |
875625b1 | 6467 | _challenge_alias="" |
bdbf323f | 6468 | _keylength="" |
6469 | _accountkeylength="" | |
5c539af7 | 6470 | _cert_file="" |
6471 | _key_file="" | |
6472 | _ca_file="" | |
6473 | _fullchain_file="" | |
4d2f38b0 | 6474 | _reloadcmd="" |
a63b05a9 | 6475 | _password="" |
635695ec | 6476 | _accountconf="" |
6477 | _useragent="" | |
b5eb4b90 | 6478 | _accountemail="" |
6479 | _accountkey="" | |
b2817897 | 6480 | _certhome="" |
27dbe77f | 6481 | _confighome="" |
39c8f79f | 6482 | _httpport="" |
e22bcf7c | 6483 | _tlsport="" |
0e38c60d | 6484 | _dnssleep="" |
dcf4f8f6 | 6485 | _listraw="" |
cc179731 | 6486 | _stopRenewOnError="" |
e3698edd | 6487 | #_insecure="" |
78009539 | 6488 | _ca_bundle="" |
2aa75f03 | 6489 | _ca_path="" |
c8e9a31e | 6490 | _nocron="" |
61556a54 | 6491 | _noprofile="" |
43822d37 | 6492 | _ecc="" |
10afcaca | 6493 | _csr="" |
b0070f03 | 6494 | _pre_hook="" |
6495 | _post_hook="" | |
6496 | _renew_hook="" | |
a61fe418 | 6497 | _deploy_hook="" |
5ea6e9c9 | 6498 | _logfile="" |
d0871bda | 6499 | _log="" |
0463b5d6 | 6500 | _local_address="" |
a73c5b33 | 6501 | _log_level="" |
6bf281f9 | 6502 | _auto_upgrade="" |
6ae0f7f5 | 6503 | _listen_v4="" |
6504 | _listen_v6="" | |
a746139c | 6505 | _openssl_bin="" |
e2edf208 | 6506 | _syslog="" |
9b124070 | 6507 | _use_wget="" |
98394f99 | 6508 | _server="" |
b50e701c | 6509 | _notify_hook="" |
6510 | _notify_level="" | |
6511 | _notify_mode="" | |
1041c9f9 | 6512 | _revoke_reason="" |
4c2a3841 | 6513 | while [ ${#} -gt 0 ]; do |
a63b05a9 | 6514 | case "${1}" in |
4c2a3841 | 6515 | |
6516 | --help | -h) | |
a63b05a9 | 6517 | showhelp |
6518 | return | |
6519 | ;; | |
4c2a3841 | 6520 | --version | -v) |
a63b05a9 | 6521 | version |
6522 | return | |
6523 | ;; | |
4c2a3841 | 6524 | --install) |
a63b05a9 | 6525 | _CMD="install" |
6526 | ;; | |
4c2a3841 | 6527 | --uninstall) |
a63b05a9 | 6528 | _CMD="uninstall" |
6529 | ;; | |
4c2a3841 | 6530 | --upgrade) |
52677b0a | 6531 | _CMD="upgrade" |
6532 | ;; | |
4c2a3841 | 6533 | --issue) |
a63b05a9 | 6534 | _CMD="issue" |
6535 | ;; | |
4c2a3841 | 6536 | --deploy) |
a61fe418 | 6537 | _CMD="deploy" |
6538 | ;; | |
4c2a3841 | 6539 | --signcsr) |
10afcaca | 6540 | _CMD="signcsr" |
6541 | ;; | |
4c2a3841 | 6542 | --showcsr) |
10afcaca | 6543 | _CMD="showcsr" |
6544 | ;; | |
db7e4bf9 | 6545 | --installcert | -i | --install-cert) |
a63b05a9 | 6546 | _CMD="installcert" |
6547 | ;; | |
4c2a3841 | 6548 | --renew | -r) |
a63b05a9 | 6549 | _CMD="renew" |
6550 | ;; | |
db7e4bf9 | 6551 | --renewAll | --renewall | --renew-all) |
a63b05a9 | 6552 | _CMD="renewAll" |
6553 | ;; | |
4c2a3841 | 6554 | --revoke) |
a63b05a9 | 6555 | _CMD="revoke" |
6556 | ;; | |
78f0201d | 6557 | --remove) |
6558 | _CMD="remove" | |
6559 | ;; | |
4c2a3841 | 6560 | --list) |
6d7eda3e | 6561 | _CMD="list" |
6562 | ;; | |
ee20015d | 6563 | --installcronjob | --install-cronjob) |
a63b05a9 | 6564 | _CMD="installcronjob" |
6565 | ;; | |
db7e4bf9 | 6566 | --uninstallcronjob | --uninstall-cronjob) |
a63b05a9 | 6567 | _CMD="uninstallcronjob" |
6568 | ;; | |
4c2a3841 | 6569 | --cron) |
a63b05a9 | 6570 | _CMD="cron" |
6571 | ;; | |
4c2a3841 | 6572 | --toPkcs) |
a63b05a9 | 6573 | _CMD="toPkcs" |
4c2a3841 | 6574 | ;; |
4410226d | 6575 | --toPkcs8) |
6576 | _CMD="toPkcs8" | |
342128a4 | 6577 | ;; |
0984585d | 6578 | --createAccountKey | --createaccountkey | -cak | --create-account-key) |
a63b05a9 | 6579 | _CMD="createAccountKey" |
6580 | ;; | |
0984585d | 6581 | --createDomainKey | --createdomainkey | -cdk | --create-domain-key) |
a63b05a9 | 6582 | _CMD="createDomainKey" |
6583 | ;; | |
4c2a3841 | 6584 | --createCSR | --createcsr | -ccr) |
a63b05a9 | 6585 | _CMD="createCSR" |
6586 | ;; | |
4c2a3841 | 6587 | --deactivate) |
0c00e870 | 6588 | _CMD="deactivate" |
6589 | ;; | |
ee20015d | 6590 | --updateaccount | --update-account) |
eb59817e | 6591 | _CMD="updateaccount" |
6592 | ;; | |
ee20015d | 6593 | --registeraccount | --register-account) |
eb59817e | 6594 | _CMD="registeraccount" |
6595 | ;; | |
422dd1fa | 6596 | --deactivate-account) |
6597 | _CMD="deactivateaccount" | |
6598 | ;; | |
b50e701c | 6599 | --set-notify) |
6600 | _CMD="setnotify" | |
6601 | ;; | |
4c2a3841 | 6602 | --domain | -d) |
a63b05a9 | 6603 | _dvalue="$2" |
4c2a3841 | 6604 | |
6605 | if [ "$_dvalue" ]; then | |
6606 | if _startswith "$_dvalue" "-"; then | |
ee1737a5 | 6607 | _err "'$_dvalue' is not a valid domain for parameter '$1'" |
6608 | return 1 | |
6609 | fi | |
4c2a3841 | 6610 | if _is_idn "$_dvalue" && ! _exists idn; then |
9774b01b | 6611 | _err "It seems that $_dvalue is an IDN( Internationalized Domain Names), please install 'idn' command first." |
6612 | return 1 | |
6613 | fi | |
4c2a3841 | 6614 | |
9e9f839d | 6615 | if _startswith "$_dvalue" "*."; then |
6616 | _debug "Wildcard domain" | |
6617 | export ACME_VERSION=2 | |
6618 | fi | |
4c2a3841 | 6619 | if [ -z "$_domain" ]; then |
ee1737a5 | 6620 | _domain="$_dvalue" |
a63b05a9 | 6621 | else |
4c2a3841 | 6622 | if [ "$_altdomains" = "$NO_VALUE" ]; then |
ee1737a5 | 6623 | _altdomains="$_dvalue" |
6624 | else | |
6625 | _altdomains="$_altdomains,$_dvalue" | |
6626 | fi | |
a63b05a9 | 6627 | fi |
6628 | fi | |
4c2a3841 | 6629 | |
a63b05a9 | 6630 | shift |
6631 | ;; | |
6632 | ||
4c2a3841 | 6633 | --force | -f) |
a63b05a9 | 6634 | FORCE="1" |
6635 | ;; | |
4c2a3841 | 6636 | --staging | --test) |
a63b05a9 | 6637 | STAGE="1" |
6638 | ;; | |
48d9a8c1 | 6639 | --server) |
6640 | ACME_DIRECTORY="$2" | |
98394f99 | 6641 | _server="$ACME_DIRECTORY" |
48d9a8c1 | 6642 | export ACME_DIRECTORY |
6643 | shift | |
6644 | ;; | |
4c2a3841 | 6645 | --debug) |
6646 | if [ -z "$2" ] || _startswith "$2" "-"; then | |
fc6cf4d9 | 6647 | DEBUG="$DEBUG_LEVEL_DEFAULT" |
a63b05a9 | 6648 | else |
6649 | DEBUG="$2" | |
6650 | shift | |
4c2a3841 | 6651 | fi |
a63b05a9 | 6652 | ;; |
e6e85b0c | 6653 | --output-insecure) |
6654 | export OUTPUT_INSECURE=1 | |
6655 | ;; | |
4c2a3841 | 6656 | --webroot | -w) |
a63b05a9 | 6657 | wvalue="$2" |
4c2a3841 | 6658 | if [ -z "$_webroot" ]; then |
a63b05a9 | 6659 | _webroot="$wvalue" |
6660 | else | |
6661 | _webroot="$_webroot,$wvalue" | |
6662 | fi | |
6663 | shift | |
4c2a3841 | 6664 | ;; |
875625b1 | 6665 | --challenge-alias) |
6666 | cvalue="$2" | |
6667 | _challenge_alias="$_challenge_alias$cvalue," | |
6668 | shift | |
6669 | ;; | |
64821ad4 | 6670 | --domain-alias) |
6671 | cvalue="$DNS_ALIAS_PREFIX$2" | |
6672 | _challenge_alias="$_challenge_alias$cvalue," | |
6673 | shift | |
1f7df33e | 6674 | ;; |
4c2a3841 | 6675 | --standalone) |
3f4513b3 | 6676 | wvalue="$NO_VALUE" |
4c2a3841 | 6677 | if [ -z "$_webroot" ]; then |
a63b05a9 | 6678 | _webroot="$wvalue" |
6679 | else | |
6680 | _webroot="$_webroot,$wvalue" | |
6681 | fi | |
6682 | ;; | |
08681f4a | 6683 | --alpn) |
6684 | wvalue="$W_ALPN" | |
6685 | if [ -z "$_webroot" ]; then | |
6686 | _webroot="$wvalue" | |
6687 | else | |
6688 | _webroot="$_webroot,$wvalue" | |
6689 | fi | |
6690 | ;; | |
0e44f587 | 6691 | --stateless) |
6692 | wvalue="$MODE_STATELESS" | |
6693 | if [ -z "$_webroot" ]; then | |
6694 | _webroot="$wvalue" | |
6695 | else | |
6696 | _webroot="$_webroot,$wvalue" | |
6697 | fi | |
6698 | ;; | |
4c2a3841 | 6699 | --local-address) |
0463b5d6 | 6700 | lvalue="$2" |
6701 | _local_address="$_local_address$lvalue," | |
6702 | shift | |
6703 | ;; | |
4c2a3841 | 6704 | --apache) |
a63b05a9 | 6705 | wvalue="apache" |
4c2a3841 | 6706 | if [ -z "$_webroot" ]; then |
a63b05a9 | 6707 | _webroot="$wvalue" |
6708 | else | |
6709 | _webroot="$_webroot,$wvalue" | |
6710 | fi | |
6711 | ;; | |
9d725af6 | 6712 | --nginx) |
6713 | wvalue="$NGINX" | |
1081d98b | 6714 | if [ "$2" ] && ! _startswith "$2" "-"; then |
6715 | wvalue="$NGINX$2" | |
6716 | shift | |
6717 | fi | |
9d725af6 | 6718 | if [ -z "$_webroot" ]; then |
6719 | _webroot="$wvalue" | |
6720 | else | |
6721 | _webroot="$_webroot,$wvalue" | |
6722 | fi | |
6723 | ;; | |
4c2a3841 | 6724 | --dns) |
3881f221 | 6725 | wvalue="$W_DNS" |
a5c56c54 | 6726 | if [ "$2" ] && ! _startswith "$2" "-"; then |
a63b05a9 | 6727 | wvalue="$2" |
6728 | shift | |
6729 | fi | |
4c2a3841 | 6730 | if [ -z "$_webroot" ]; then |
a63b05a9 | 6731 | _webroot="$wvalue" |
6732 | else | |
6733 | _webroot="$_webroot,$wvalue" | |
6734 | fi | |
6735 | ;; | |
4c2a3841 | 6736 | --dnssleep) |
0e38c60d | 6737 | _dnssleep="$2" |
6738 | Le_DNSSleep="$_dnssleep" | |
6739 | shift | |
6740 | ;; | |
4c2a3841 | 6741 | |
6742 | --keylength | -k) | |
a63b05a9 | 6743 | _keylength="$2" |
a63b05a9 | 6744 | shift |
6745 | ;; | |
4c2a3841 | 6746 | --accountkeylength | -ak) |
2ce87fe2 | 6747 | _accountkeylength="$2" |
a63b05a9 | 6748 | shift |
6749 | ;; | |
6750 | ||
5c539af7 | 6751 | --cert-file | --certpath) |
6752 | _cert_file="$2" | |
a63b05a9 | 6753 | shift |
6754 | ;; | |
5c539af7 | 6755 | --key-file | --keypath) |
6756 | _key_file="$2" | |
a63b05a9 | 6757 | shift |
6758 | ;; | |
5c539af7 | 6759 | --ca-file | --capath) |
6760 | _ca_file="$2" | |
a63b05a9 | 6761 | shift |
6762 | ;; | |
5c539af7 | 6763 | --fullchain-file | --fullchainpath) |
6764 | _fullchain_file="$2" | |
a63b05a9 | 6765 | shift |
6766 | ;; | |
4c2a3841 | 6767 | --reloadcmd | --reloadCmd) |
a63b05a9 | 6768 | _reloadcmd="$2" |
6769 | shift | |
6770 | ;; | |
4c2a3841 | 6771 | --password) |
a63b05a9 | 6772 | _password="$2" |
6773 | shift | |
6774 | ;; | |
4c2a3841 | 6775 | --accountconf) |
635695ec | 6776 | _accountconf="$2" |
6777 | ACCOUNT_CONF_PATH="$_accountconf" | |
a7b7355d | 6778 | shift |
a63b05a9 | 6779 | ;; |
4c2a3841 | 6780 | --home) |
a63b05a9 | 6781 | LE_WORKING_DIR="$2" |
a7b7355d | 6782 | shift |
a63b05a9 | 6783 | ;; |
ee20015d | 6784 | --certhome | --cert-home) |
b2817897 | 6785 | _certhome="$2" |
6786 | CERT_HOME="$_certhome" | |
6787 | shift | |
4c2a3841 | 6788 | ;; |
27dbe77f | 6789 | --config-home) |
6790 | _confighome="$2" | |
f5b546b3 | 6791 | LE_CONFIG_HOME="$_confighome" |
27dbe77f | 6792 | shift |
6793 | ;; | |
4c2a3841 | 6794 | --useragent) |
635695ec | 6795 | _useragent="$2" |
6796 | USER_AGENT="$_useragent" | |
6797 | shift | |
6798 | ;; | |
4c2a3841 | 6799 | --accountemail) |
b5eb4b90 | 6800 | _accountemail="$2" |
6801 | ACCOUNT_EMAIL="$_accountemail" | |
6802 | shift | |
6803 | ;; | |
4c2a3841 | 6804 | --accountkey) |
b5eb4b90 | 6805 | _accountkey="$2" |
6806 | ACCOUNT_KEY_PATH="$_accountkey" | |
6807 | shift | |
6808 | ;; | |
4c2a3841 | 6809 | --days) |
06625071 | 6810 | _days="$2" |
6811 | Le_RenewalDays="$_days" | |
6812 | shift | |
6813 | ;; | |
4c2a3841 | 6814 | --httpport) |
39c8f79f | 6815 | _httpport="$2" |
6816 | Le_HTTPPort="$_httpport" | |
6817 | shift | |
6818 | ;; | |
79a0a66f | 6819 | --tlsport) |
6820 | _tlsport="$2" | |
6821 | Le_TLSPort="$_tlsport" | |
6822 | shift | |
6823 | ;; | |
4c2a3841 | 6824 | --listraw) |
dcf4f8f6 | 6825 | _listraw="raw" |
4c2a3841 | 6826 | ;; |
6827 | --stopRenewOnError | --stoprenewonerror | -se) | |
cc179731 | 6828 | _stopRenewOnError="1" |
6829 | ;; | |
4c2a3841 | 6830 | --insecure) |
e3698edd | 6831 | #_insecure="1" |
fac1e367 | 6832 | HTTPS_INSECURE="1" |
13d7cae9 | 6833 | ;; |
4c2a3841 | 6834 | --ca-bundle) |
78d1cfb4 | 6835 | _ca_bundle="$(_readlink "$2")" |
78009539 PS |
6836 | CA_BUNDLE="$_ca_bundle" |
6837 | shift | |
6838 | ;; | |
2aa75f03 | 6839 | --ca-path) |
6840 | _ca_path="$2" | |
6841 | CA_PATH="$_ca_path" | |
6842 | shift | |
6843 | ;; | |
4c2a3841 | 6844 | --nocron) |
c8e9a31e | 6845 | _nocron="1" |
6846 | ;; | |
61556a54 HH |
6847 | --noprofile) |
6848 | _noprofile="1" | |
6849 | ;; | |
08b4e1a7 | 6850 | --no-color) |
6851 | export ACME_NO_COLOR=1 | |
6852 | ;; | |
e32b3aac SB |
6853 | --force-color) |
6854 | export ACME_FORCE_COLOR=1 | |
6855 | ;; | |
4c2a3841 | 6856 | --ecc) |
43822d37 | 6857 | _ecc="isEcc" |
6858 | ;; | |
4c2a3841 | 6859 | --csr) |
10afcaca | 6860 | _csr="$2" |
6861 | shift | |
6862 | ;; | |
4c2a3841 | 6863 | --pre-hook) |
b0070f03 | 6864 | _pre_hook="$2" |
6865 | shift | |
6866 | ;; | |
4c2a3841 | 6867 | --post-hook) |
b0070f03 | 6868 | _post_hook="$2" |
6869 | shift | |
6870 | ;; | |
4c2a3841 | 6871 | --renew-hook) |
b0070f03 | 6872 | _renew_hook="$2" |
6873 | shift | |
6874 | ;; | |
4c2a3841 | 6875 | --deploy-hook) |
93bce1b2 | 6876 | if [ -z "$2" ] || _startswith "$2" "-"; then |
6877 | _usage "Please specify a value for '--deploy-hook'" | |
6878 | return 1 | |
6879 | fi | |
6880 | _deploy_hook="$_deploy_hook$2," | |
a61fe418 | 6881 | shift |
6882 | ;; | |
4c2a3841 | 6883 | --ocsp-must-staple | --ocsp) |
96db9362 | 6884 | Le_OCSP_Staple="1" |
0c9546cc | 6885 | ;; |
c4b2e582 | 6886 | --always-force-new-domain-key) |
6887 | if [ -z "$2" ] || _startswith "$2" "-"; then | |
6888 | Le_ForceNewDomainKey=1 | |
6889 | else | |
6890 | Le_ForceNewDomainKey="$2" | |
6891 | shift | |
6892 | fi | |
6893 | ;; | |
a0923622 | 6894 | --yes-I-know-dns-manual-mode-enough-go-ahead-please) |
6895 | export FORCE_DNS_MANUAL=1 | |
6896 | ;; | |
4c2a3841 | 6897 | --log | --logfile) |
d0871bda | 6898 | _log="1" |
5ea6e9c9 | 6899 | _logfile="$2" |
4c2a3841 | 6900 | if _startswith "$_logfile" '-'; then |
d0871bda | 6901 | _logfile="" |
6902 | else | |
6903 | shift | |
6904 | fi | |
5ea6e9c9 | 6905 | LOG_FILE="$_logfile" |
4c2a3841 | 6906 | if [ -z "$LOG_LEVEL" ]; then |
a73c5b33 | 6907 | LOG_LEVEL="$DEFAULT_LOG_LEVEL" |
6908 | fi | |
6909 | ;; | |
4c2a3841 | 6910 | --log-level) |
30bfc2ce | 6911 | _log_level="$2" |
a73c5b33 | 6912 | LOG_LEVEL="$_log_level" |
6913 | shift | |
5ea6e9c9 | 6914 | ;; |
e2edf208 | 6915 | --syslog) |
6916 | if ! _startswith "$2" '-'; then | |
6917 | _syslog="$2" | |
6918 | shift | |
6919 | fi | |
6920 | if [ -z "$_syslog" ]; then | |
fc6cf4d9 | 6921 | _syslog="$SYSLOG_LEVEL_DEFAULT" |
e2edf208 | 6922 | fi |
6923 | ;; | |
4c2a3841 | 6924 | --auto-upgrade) |
6bf281f9 | 6925 | _auto_upgrade="$2" |
4c2a3841 | 6926 | if [ -z "$_auto_upgrade" ] || _startswith "$_auto_upgrade" '-'; then |
6bf281f9 | 6927 | _auto_upgrade="1" |
6928 | else | |
6929 | shift | |
6930 | fi | |
6931 | AUTO_UPGRADE="$_auto_upgrade" | |
6932 | ;; | |
4c2a3841 | 6933 | --listen-v4) |
6ae0f7f5 | 6934 | _listen_v4="1" |
6935 | Le_Listen_V4="$_listen_v4" | |
6936 | ;; | |
4c2a3841 | 6937 | --listen-v6) |
6ae0f7f5 | 6938 | _listen_v6="1" |
6939 | Le_Listen_V6="$_listen_v6" | |
6940 | ;; | |
a746139c | 6941 | --openssl-bin) |
6942 | _openssl_bin="$2" | |
851fedf7 | 6943 | ACME_OPENSSL_BIN="$_openssl_bin" |
7c2e8754 | 6944 | shift |
a746139c | 6945 | ;; |
9b124070 | 6946 | --use-wget) |
6947 | _use_wget="1" | |
6948 | ACME_USE_WGET="1" | |
6949 | ;; | |
66686de4 | 6950 | --branch | -b) |
6951 | export BRANCH="$2" | |
6952 | shift | |
6953 | ;; | |
b50e701c | 6954 | --notify-hook) |
6955 | _nhook="$2" | |
6956 | if _startswith "$_nhook" "-"; then | |
6957 | _err "'$_nhook' is not a hook name for '$1'" | |
6958 | return 1 | |
6959 | fi | |
6960 | if [ "$_notify_hook" ]; then | |
6961 | _notify_hook="$_notify_hook,$_nhook" | |
6962 | else | |
6963 | _notify_hook="$_nhook" | |
6964 | fi | |
6965 | shift | |
6966 | ;; | |
6967 | --notify-level) | |
6968 | _nlevel="$2" | |
6969 | if _startswith "$_nlevel" "-"; then | |
6970 | _err "'$_nlevel' is not a integer for '$1'" | |
6971 | return 1 | |
6972 | fi | |
6973 | _notify_level="$_nlevel" | |
6974 | shift | |
6975 | ;; | |
6976 | --notify-mode) | |
6977 | _nmode="$2" | |
6978 | if _startswith "$_nmode" "-"; then | |
6979 | _err "'$_nmode' is not a integer for '$1'" | |
6980 | return 1 | |
6981 | fi | |
6982 | _notify_mode="$_nmode" | |
6983 | shift | |
1041c9f9 | 6984 | ;; |
a9953330 | 6985 | --revoke-reason) |
1041c9f9 | 6986 | _revoke_reason="$2" |
6987 | if _startswith "$_revoke_reason" "-"; then | |
6988 | _err "'$_revoke_reason' is not a integer for '$1'" | |
6989 | return 1 | |
6990 | fi | |
6991 | shift | |
b50e701c | 6992 | ;; |
4c2a3841 | 6993 | *) |
a63b05a9 | 6994 | _err "Unknown parameter : $1" |
6995 | return 1 | |
6996 | ;; | |
6997 | esac | |
6998 | ||
6999 | shift 1 | |
7000 | done | |
7001 | ||
4c2a3841 | 7002 | if [ "${_CMD}" != "install" ]; then |
5bdfdfef | 7003 | if [ "$__INTERACTIVE" ] && ! _checkSudo; then |
7004 | if [ -z "$FORCE" ]; then | |
7005 | #Use "echo" here, instead of _info. it's too early | |
7006 | echo "It seems that you are using sudo, please read this link first:" | |
7007 | echo "$_SUDO_WIKI" | |
7008 | return 1 | |
7009 | fi | |
7010 | fi | |
5ea6e9c9 | 7011 | __initHome |
661f0583 | 7012 | if [ "$_log" ]; then |
4c2a3841 | 7013 | if [ -z "$_logfile" ]; then |
661f0583 | 7014 | _logfile="$DEFAULT_LOG_FILE" |
7015 | fi | |
d0871bda | 7016 | fi |
4c2a3841 | 7017 | if [ "$_logfile" ]; then |
5ea6e9c9 | 7018 | _saveaccountconf "LOG_FILE" "$_logfile" |
661f0583 | 7019 | LOG_FILE="$_logfile" |
5ea6e9c9 | 7020 | fi |
a73c5b33 | 7021 | |
4c2a3841 | 7022 | if [ "$_log_level" ]; then |
a73c5b33 | 7023 | _saveaccountconf "LOG_LEVEL" "$_log_level" |
7024 | LOG_LEVEL="$_log_level" | |
7025 | fi | |
4c2a3841 | 7026 | |
e2edf208 | 7027 | if [ "$_syslog" ]; then |
7028 | if _exists logger; then | |
7029 | if [ "$_syslog" = "0" ]; then | |
7030 | _clearaccountconf "SYS_LOG" | |
7031 | else | |
7032 | _saveaccountconf "SYS_LOG" "$_syslog" | |
7033 | fi | |
7034 | SYS_LOG="$_syslog" | |
7035 | else | |
7036 | _err "The 'logger' command is not found, can not enable syslog." | |
7037 | _clearaccountconf "SYS_LOG" | |
7038 | SYS_LOG="" | |
7039 | fi | |
7040 | fi | |
7041 | ||
5ea6e9c9 | 7042 | _processAccountConf |
7043 | fi | |
4c2a3841 | 7044 | |
9d548d81 | 7045 | _debug2 LE_WORKING_DIR "$LE_WORKING_DIR" |
4c2a3841 | 7046 | |
7047 | if [ "$DEBUG" ]; then | |
dcf9cb58 | 7048 | version |
98394f99 | 7049 | if [ "$_server" ]; then |
7050 | _debug "Using server: $_server" | |
7051 | fi | |
dcf9cb58 | 7052 | fi |
9a733a57 | 7053 | _debug "Running cmd: ${_CMD}" |
a63b05a9 | 7054 | case "${_CMD}" in |
61556a54 | 7055 | install) install "$_nocron" "$_confighome" "$_noprofile" ;; |
bc96082f | 7056 | uninstall) uninstall "$_nocron" ;; |
52677b0a | 7057 | upgrade) upgrade ;; |
a63b05a9 | 7058 | issue) |
875625b1 | 7059 | issue "$_webroot" "$_domain" "$_altdomains" "$_keylength" "$_cert_file" "$_key_file" "$_ca_file" "$_reloadcmd" "$_fullchain_file" "$_pre_hook" "$_post_hook" "$_renew_hook" "$_local_address" "$_challenge_alias" |
a63b05a9 | 7060 | ;; |
a61fe418 | 7061 | deploy) |
7062 | deploy "$_domain" "$_deploy_hook" "$_ecc" | |
7063 | ;; | |
10afcaca | 7064 | signcsr) |
875625b1 | 7065 | signcsr "$_csr" "$_webroot" "$_cert_file" "$_key_file" "$_ca_file" "$_reloadcmd" "$_fullchain_file" "$_pre_hook" "$_post_hook" "$_renew_hook" "$_local_address" "$_challenge_alias" |
10afcaca | 7066 | ;; |
7067 | showcsr) | |
7068 | showcsr "$_csr" "$_domain" | |
7069 | ;; | |
a63b05a9 | 7070 | installcert) |
5c539af7 | 7071 | installcert "$_domain" "$_cert_file" "$_key_file" "$_ca_file" "$_reloadcmd" "$_fullchain_file" "$_ecc" |
a63b05a9 | 7072 | ;; |
4c2a3841 | 7073 | renew) |
43822d37 | 7074 | renew "$_domain" "$_ecc" |
a63b05a9 | 7075 | ;; |
4c2a3841 | 7076 | renewAll) |
cc179731 | 7077 | renewAll "$_stopRenewOnError" |
a63b05a9 | 7078 | ;; |
4c2a3841 | 7079 | revoke) |
1041c9f9 | 7080 | revoke "$_domain" "$_ecc" "$_revoke_reason" |
a63b05a9 | 7081 | ;; |
78f0201d | 7082 | remove) |
7083 | remove "$_domain" "$_ecc" | |
7084 | ;; | |
4c2a3841 | 7085 | deactivate) |
3f4513b3 | 7086 | deactivate "$_domain,$_altdomains" |
eb59817e | 7087 | ;; |
4c2a3841 | 7088 | registeraccount) |
57e58ce7 | 7089 | registeraccount "$_accountkeylength" |
eb59817e | 7090 | ;; |
4c2a3841 | 7091 | updateaccount) |
eb59817e | 7092 | updateaccount |
7093 | ;; | |
422dd1fa | 7094 | deactivateaccount) |
7095 | deactivateaccount | |
7096 | ;; | |
4c2a3841 | 7097 | list) |
dcf4f8f6 | 7098 | list "$_listraw" |
6d7eda3e | 7099 | ;; |
27dbe77f | 7100 | installcronjob) installcronjob "$_confighome" ;; |
a63b05a9 | 7101 | uninstallcronjob) uninstallcronjob ;; |
7102 | cron) cron ;; | |
4c2a3841 | 7103 | toPkcs) |
43822d37 | 7104 | toPkcs "$_domain" "$_password" "$_ecc" |
a63b05a9 | 7105 | ;; |
4410226d | 7106 | toPkcs8) |
7107 | toPkcs8 "$_domain" "$_ecc" | |
7108 | ;; | |
4c2a3841 | 7109 | createAccountKey) |
5fbc47eb | 7110 | createAccountKey "$_accountkeylength" |
a63b05a9 | 7111 | ;; |
4c2a3841 | 7112 | createDomainKey) |
a63b05a9 | 7113 | createDomainKey "$_domain" "$_keylength" |
7114 | ;; | |
4c2a3841 | 7115 | createCSR) |
43822d37 | 7116 | createCSR "$_domain" "$_altdomains" "$_ecc" |
a63b05a9 | 7117 | ;; |
b50e701c | 7118 | setnotify) |
7119 | setnotify "$_notify_hook" "$_notify_level" "$_notify_mode" | |
7120 | ;; | |
a63b05a9 | 7121 | *) |
27dbe77f | 7122 | if [ "$_CMD" ]; then |
7123 | _err "Invalid command: $_CMD" | |
7124 | fi | |
4c2a3841 | 7125 | showhelp |
a63b05a9 | 7126 | return 1 |
4c2a3841 | 7127 | ;; |
a63b05a9 | 7128 | esac |
d3595686 | 7129 | _ret="$?" |
4c2a3841 | 7130 | if [ "$_ret" != "0" ]; then |
d3595686 | 7131 | return $_ret |
7132 | fi | |
4c2a3841 | 7133 | |
7134 | if [ "${_CMD}" = "install" ]; then | |
7135 | if [ "$_log" ]; then | |
7136 | if [ -z "$LOG_FILE" ]; then | |
d0871bda | 7137 | LOG_FILE="$DEFAULT_LOG_FILE" |
7138 | fi | |
7139 | _saveaccountconf "LOG_FILE" "$LOG_FILE" | |
5ea6e9c9 | 7140 | fi |
4c2a3841 | 7141 | |
7142 | if [ "$_log_level" ]; then | |
a73c5b33 | 7143 | _saveaccountconf "LOG_LEVEL" "$_log_level" |
7144 | fi | |
e2edf208 | 7145 | |
7146 | if [ "$_syslog" ]; then | |
7147 | if _exists logger; then | |
7148 | if [ "$_syslog" = "0" ]; then | |
7149 | _clearaccountconf "SYS_LOG" | |
7150 | else | |
7151 | _saveaccountconf "SYS_LOG" "$_syslog" | |
7152 | fi | |
7153 | else | |
7154 | _err "The 'logger' command is not found, can not enable syslog." | |
7155 | _clearaccountconf "SYS_LOG" | |
7156 | SYS_LOG="" | |
7157 | fi | |
7158 | fi | |
7159 | ||
5ea6e9c9 | 7160 | _processAccountConf |
b5eb4b90 | 7161 | fi |
635695ec | 7162 | |
a63b05a9 | 7163 | } |
7164 | ||
4c2a3841 | 7165 | if [ "$INSTALLONLINE" ]; then |
d1f97fc8 | 7166 | INSTALLONLINE="" |
2fbf3991 | 7167 | _installOnline |
4a0f23e2 | 7168 | exit |
7169 | fi | |
4c3b3608 | 7170 | |
319e0ae3 | 7171 | main() { |
7172 | [ -z "$1" ] && showhelp && return | |
4c2a3841 | 7173 | if _startswith "$1" '-'; then _process "$@"; else "$@"; fi |
319e0ae3 | 7174 | } |
e69a7c38 | 7175 | |
aa7b82de | 7176 | main "$@" |