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