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