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