]>
Commit | Line | Data |
---|---|---|
0a7c9364 | 1 | #!/usr/bin/env sh |
bfdf1f48 | 2 | |
5ea6e9c9 | 3 | VER=2.5.3 |
a7b7355d | 4 | |
6cc11ffb | 5 | PROJECT_NAME="acme.sh" |
a7b7355d | 6 | |
6cc11ffb | 7 | PROJECT_ENTRY="acme.sh" |
8 | ||
9 | PROJECT="https://github.com/Neilpang/$PROJECT_NAME" | |
4c3b3608 | 10 | |
f3e4cea3 | 11 | DEFAULT_INSTALL_HOME="$HOME/.$PROJECT_NAME" |
12 | _SCRIPT_="$0" | |
13 | ||
14 | ||
4c3b3608 | 15 | DEFAULT_CA="https://acme-v01.api.letsencrypt.org" |
c93ec933 | 16 | DEFAULT_AGREEMENT="https://letsencrypt.org/documents/LE-SA-v1.1.1-August-1-2016.pdf" |
4c3b3608 | 17 | |
08928b48 | 18 | DEFAULT_USER_AGENT="$PROJECT_ENTRY client v$VER : $PROJECT" |
bbbdcb09 | 19 | |
4c3b3608 | 20 | STAGE_CA="https://acme-staging.api.letsencrypt.org" |
21 | ||
22 | VTYPE_HTTP="http-01" | |
23 | VTYPE_DNS="dns-01" | |
e22bcf7c | 24 | VTYPE_TLS="tls-sni-01" |
25 | VTYPE_TLS2="tls-sni-02" | |
26 | ||
523c7682 | 27 | MAX_RENEW=80 |
28 | ||
4a4dacb5 | 29 | DEFAULT_DNS_SLEEP=120 |
30 | ||
e22bcf7c | 31 | W_TLS="tls" |
4c3b3608 | 32 | |
ec603bee | 33 | STATE_VERIFIED="verified_ok" |
34 | ||
88fab7d6 | 35 | BEGIN_CSR="-----BEGIN CERTIFICATE REQUEST-----" |
36 | END_CSR="-----END CERTIFICATE REQUEST-----" | |
37 | ||
38 | BEGIN_CERT="-----BEGIN CERTIFICATE-----" | |
39 | END_CERT="-----END CERTIFICATE-----" | |
40 | ||
cc179731 | 41 | RENEW_SKIP=2 |
42 | ||
43822d37 | 43 | ECC_SEP="_" |
44 | ECC_SUFFIX="${ECC_SEP}ecc" | |
45 | ||
8663fb7e | 46 | if [ -z "$AGREEMENT" ] ; then |
4c3b3608 | 47 | AGREEMENT="$DEFAULT_AGREEMENT" |
48 | fi | |
49 | ||
08ee072f | 50 | __INTERACTIVE="" |
51 | if [ -t 1 ] ; then | |
52 | __INTERACTIVE="1" | |
53 | fi | |
00a50605 | 54 | |
43822d37 | 55 | __green() { |
08ee072f | 56 | if [ "$__INTERACTIVE" ] ; then |
2d12b689 | 57 | printf '\033[1;31;32m' |
58 | fi | |
43822d37 | 59 | printf -- "$1" |
08ee072f | 60 | if [ "$__INTERACTIVE" ] ; then |
2d12b689 | 61 | printf '\033[0m' |
62 | fi | |
43822d37 | 63 | } |
64 | ||
65 | __red() { | |
08ee072f | 66 | if [ "$__INTERACTIVE" ] ; then |
2d12b689 | 67 | printf '\033[1;31;40m' |
68 | fi | |
43822d37 | 69 | printf -- "$1" |
08ee072f | 70 | if [ "$__INTERACTIVE" ] ; then |
2d12b689 | 71 | printf '\033[0m' |
72 | fi | |
43822d37 | 73 | } |
00a50605 | 74 | |
5ea6e9c9 | 75 | __mytee() { |
76 | tee -a $1 | |
77 | } | |
78 | ||
4c3b3608 | 79 | _info() { |
8663fb7e | 80 | if [ -z "$2" ] ; then |
5ea6e9c9 | 81 | printf -- "[$(date)] $1" | __mytee $LOG_FILE |
43822d37 | 82 | else |
5ea6e9c9 | 83 | printf -- "[$(date)] $1='$2'" | __mytee $LOG_FILE |
43822d37 | 84 | fi |
5ea6e9c9 | 85 | printf "\n" | __mytee $LOG_FILE |
43822d37 | 86 | } |
87 | ||
88 | ||
43822d37 | 89 | _err_e() { |
90 | if [ -z "$2" ] ; then | |
5ea6e9c9 | 91 | __red "$1" | __mytee $LOG_FILE >&2 |
4c3b3608 | 92 | else |
5ea6e9c9 | 93 | __red "$1='$2'" | __mytee $LOG_FILE >&2 |
4c3b3608 | 94 | fi |
5ea6e9c9 | 95 | printf "\n" | __mytee $LOG_FILE >&2 |
4c3b3608 | 96 | } |
97 | ||
98 | _err() { | |
5ea6e9c9 | 99 | printf -- "[$(date)] " | __mytee $LOG_FILE >&2 |
fac1e367 | 100 | _err_e "$@" |
4c3b3608 | 101 | return 1 |
102 | } | |
103 | ||
43822d37 | 104 | _usage() { |
43822d37 | 105 | _err_e "$@" |
43822d37 | 106 | } |
107 | ||
c60883ef | 108 | _debug() { |
8663fb7e | 109 | if [ -z "$DEBUG" ] ; then |
c60883ef | 110 | return |
111 | fi | |
43822d37 | 112 | |
113 | if [ -z "$2" ] ; then | |
5ea6e9c9 | 114 | printf -- "[$(date)] $1" | __mytee $LOG_FILE >&2 |
43822d37 | 115 | else |
5ea6e9c9 | 116 | printf -- "[$(date)] $1='$2'" | __mytee $LOG_FILE >&2 |
43822d37 | 117 | fi |
118 | ||
5ea6e9c9 | 119 | printf "\n" | __mytee $LOG_FILE >&2 |
c60883ef | 120 | return 0 |
121 | } | |
122 | ||
a63b05a9 | 123 | _debug2() { |
8663fb7e | 124 | if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ] ; then |
a63b05a9 | 125 | _debug "$@" |
126 | fi | |
127 | return | |
128 | } | |
129 | ||
22ea4004 | 130 | _debug3() { |
131 | if [ "$DEBUG" ] && [ "$DEBUG" -ge "3" ] ; then | |
132 | _debug "$@" | |
133 | fi | |
134 | return | |
135 | } | |
136 | ||
dceb3aca | 137 | _startswith(){ |
138 | _str="$1" | |
139 | _sub="$2" | |
19539575 | 140 | echo "$_str" | grep "^$_sub" >/dev/null 2>&1 |
dceb3aca | 141 | } |
142 | ||
43822d37 | 143 | _endswith(){ |
144 | _str="$1" | |
145 | _sub="$2" | |
146 | echo "$_str" | grep -- "$_sub\$" >/dev/null 2>&1 | |
147 | } | |
148 | ||
dceb3aca | 149 | _contains(){ |
150 | _str="$1" | |
151 | _sub="$2" | |
43822d37 | 152 | echo "$_str" | grep -- "$_sub" >/dev/null 2>&1 |
dceb3aca | 153 | } |
154 | ||
c53da1ef | 155 | _hasfield() { |
156 | _str="$1" | |
157 | _field="$2" | |
158 | _sep="$3" | |
159 | if [ -z "$_field" ] ; then | |
43822d37 | 160 | _usage "Usage: str field [sep]" |
c53da1ef | 161 | return 1 |
162 | fi | |
163 | ||
164 | if [ -z "$_sep" ] ; then | |
165 | _sep="," | |
166 | fi | |
167 | ||
168 | for f in $(echo "$_str" | tr ',' ' ') ; do | |
169 | if [ "$f" = "$_field" ] ; then | |
0c9546cc | 170 | _debug2 "'$_str' contains '$_field'" |
c53da1ef | 171 | return 0 #contains ok |
172 | fi | |
173 | done | |
0c9546cc | 174 | _debug2 "'$_str' does not contain '$_field'" |
c53da1ef | 175 | return 1 #not contains |
176 | } | |
177 | ||
dceb3aca | 178 | _exists(){ |
c60883ef | 179 | cmd="$1" |
8663fb7e | 180 | if [ -z "$cmd" ] ; then |
43822d37 | 181 | _usage "Usage: _exists cmd" |
c60883ef | 182 | return 1 |
183 | fi | |
eac18b1c | 184 | if type command >/dev/null 2>&1 ; then |
19539575 | 185 | command -v "$cmd" >/dev/null 2>&1 |
eac18b1c | 186 | else |
19539575 | 187 | type "$cmd" >/dev/null 2>&1 |
eac18b1c | 188 | fi |
c60883ef | 189 | ret="$?" |
690a5e20 | 190 | _debug3 "$cmd exists=$ret" |
c60883ef | 191 | return $ret |
192 | } | |
193 | ||
00a50605 | 194 | #a + b |
195 | _math(){ | |
196 | expr "$@" | |
197 | } | |
198 | ||
199 | _h_char_2_dec() { | |
200 | _ch=$1 | |
201 | case "${_ch}" in | |
202 | a|A) | |
19539575 | 203 | printf "10" |
00a50605 | 204 | ;; |
205 | b|B) | |
19539575 | 206 | printf "11" |
00a50605 | 207 | ;; |
208 | c|C) | |
19539575 | 209 | printf "12" |
00a50605 | 210 | ;; |
211 | d|D) | |
19539575 | 212 | printf "13" |
00a50605 | 213 | ;; |
214 | e|E) | |
19539575 | 215 | printf "14" |
00a50605 | 216 | ;; |
217 | f|F) | |
19539575 | 218 | printf "15" |
00a50605 | 219 | ;; |
220 | *) | |
19539575 | 221 | printf "%s" "$_ch" |
00a50605 | 222 | ;; |
19539575 | 223 | esac |
00a50605 | 224 | |
225 | } | |
226 | ||
fac1e367 | 227 | |
228 | _URGLY_PRINTF="" | |
229 | if [ "$(printf '\x41')" != 'A' ] ; then | |
230 | _URGLY_PRINTF=1 | |
231 | fi | |
232 | ||
4c3b3608 | 233 | _h2b() { |
234 | hex=$(cat) | |
235 | i=1 | |
236 | j=2 | |
00a50605 | 237 | if _exists let ; then |
238 | uselet="1" | |
239 | fi | |
690a5e20 | 240 | _debug3 uselet "$uselet" |
241 | _debug3 _URGLY_PRINTF "$_URGLY_PRINTF" | |
19539575 | 242 | while true ; do |
00a50605 | 243 | if [ -z "$_URGLY_PRINTF" ] ; then |
19539575 | 244 | h="$(printf $hex | cut -c $i-$j)" |
00a50605 | 245 | if [ -z "$h" ] ; then |
246 | break; | |
247 | fi | |
248 | printf "\x$h" | |
249 | else | |
19539575 | 250 | ic="$(printf $hex | cut -c $i)" |
251 | jc="$(printf $hex | cut -c $j)" | |
00a50605 | 252 | if [ -z "$ic$jc" ] ; then |
253 | break; | |
254 | fi | |
19539575 | 255 | ic="$(_h_char_2_dec "$ic")" |
256 | jc="$(_h_char_2_dec "$jc")" | |
00a50605 | 257 | printf '\'"$(printf %o "$(_math $ic \* 16 + $jc)")" |
4c3b3608 | 258 | fi |
00a50605 | 259 | if [ "$uselet" ] ; then |
f4312b44 | 260 | let "i+=2" >/dev/null |
261 | let "j+=2" >/dev/null | |
00a50605 | 262 | else |
263 | i="$(_math $i + 2)" | |
264 | j="$(_math $j + 2)" | |
265 | fi | |
4c3b3608 | 266 | done |
267 | } | |
268 | ||
c60883ef | 269 | #options file |
270 | _sed_i() { | |
271 | options="$1" | |
272 | filename="$2" | |
8663fb7e | 273 | if [ -z "$filename" ] ; then |
43822d37 | 274 | _usage "Usage:_sed_i options filename" |
c60883ef | 275 | return 1 |
276 | fi | |
14f3dbb7 | 277 | _debug2 options "$options" |
278 | if sed -h 2>&1 | grep "\-i\[SUFFIX]" >/dev/null 2>&1; then | |
c60883ef | 279 | _debug "Using sed -i" |
14f3dbb7 | 280 | sed -i "$options" "$filename" |
c60883ef | 281 | else |
282 | _debug "No -i support in sed" | |
19539575 | 283 | text="$(cat "$filename")" |
c60883ef | 284 | echo "$text" | sed "$options" > "$filename" |
285 | fi | |
286 | } | |
287 | ||
22ea4004 | 288 | _egrep_o() { |
289 | if _contains "$(egrep -o 2>&1)" "egrep: illegal option -- o" ; then | |
290 | sed -n 's/.*\('"$1"'\).*/\1/p' | |
291 | else | |
292 | egrep -o "$1" | |
293 | fi | |
294 | } | |
295 | ||
88fab7d6 | 296 | #Usage: file startline endline |
297 | _getfile() { | |
298 | filename="$1" | |
299 | startline="$2" | |
300 | endline="$3" | |
8663fb7e | 301 | if [ -z "$endline" ] ; then |
43822d37 | 302 | _usage "Usage: file startline endline" |
88fab7d6 | 303 | return 1 |
304 | fi | |
305 | ||
19539575 | 306 | i="$(grep -n -- "$startline" "$filename" | cut -d : -f 1)" |
8663fb7e | 307 | if [ -z "$i" ] ; then |
88fab7d6 | 308 | _err "Can not find start line: $startline" |
309 | return 1 | |
310 | fi | |
19539575 | 311 | i="$(_math "$i" + 1)" |
312 | _debug i "$i" | |
88fab7d6 | 313 | |
19539575 | 314 | j="$(grep -n -- "$endline" "$filename" | cut -d : -f 1)" |
8663fb7e | 315 | if [ -z "$j" ] ; then |
88fab7d6 | 316 | _err "Can not find end line: $endline" |
317 | return 1 | |
318 | fi | |
19539575 | 319 | j="$(_math "$j" - 1)" |
320 | _debug j "$j" | |
88fab7d6 | 321 | |
19539575 | 322 | sed -n "$i,${j}p" "$filename" |
88fab7d6 | 323 | |
324 | } | |
325 | ||
326 | #Usage: multiline | |
4c3b3608 | 327 | _base64() { |
8663fb7e | 328 | if [ "$1" ] ; then |
88fab7d6 | 329 | openssl base64 -e |
330 | else | |
331 | openssl base64 -e | tr -d '\r\n' | |
332 | fi | |
333 | } | |
334 | ||
335 | #Usage: multiline | |
336 | _dbase64() { | |
8663fb7e | 337 | if [ "$1" ] ; then |
88fab7d6 | 338 | openssl base64 -d -A |
339 | else | |
340 | openssl base64 -d | |
341 | fi | |
342 | } | |
343 | ||
e22bcf7c | 344 | #Usage: hashalg [outputhex] |
88fab7d6 | 345 | #Output Base64-encoded digest |
346 | _digest() { | |
347 | alg="$1" | |
8663fb7e | 348 | if [ -z "$alg" ] ; then |
43822d37 | 349 | _usage "Usage: _digest hashalg" |
88fab7d6 | 350 | return 1 |
351 | fi | |
352 | ||
e22bcf7c | 353 | outputhex="$2" |
354 | ||
a6014bf0 | 355 | if [ "$alg" = "sha256" ] || [ "$alg" = "sha1" ]; then |
e22bcf7c | 356 | if [ "$outputhex" ] ; then |
690a5e20 | 357 | openssl dgst -$alg -hex | cut -d = -f 2 | tr -d ' ' |
e22bcf7c | 358 | else |
a6014bf0 | 359 | openssl dgst -$alg -binary | _base64 |
e22bcf7c | 360 | fi |
88fab7d6 | 361 | else |
362 | _err "$alg is not supported yet" | |
363 | return 1 | |
364 | fi | |
365 | ||
366 | } | |
367 | ||
368 | #Usage: keyfile hashalg | |
369 | #Output: Base64-encoded signature value | |
370 | _sign() { | |
371 | keyfile="$1" | |
372 | alg="$2" | |
8663fb7e | 373 | if [ -z "$alg" ] ; then |
43822d37 | 374 | _usage "Usage: _sign keyfile hashalg" |
88fab7d6 | 375 | return 1 |
376 | fi | |
377 | ||
8663fb7e | 378 | if [ "$alg" = "sha256" ] ; then |
88fab7d6 | 379 | openssl dgst -sha256 -sign "$keyfile" | _base64 |
380 | else | |
381 | _err "$alg is not supported yet" | |
382 | return 1 | |
fac1e367 | 383 | fi |
88fab7d6 | 384 | |
4c3b3608 | 385 | } |
386 | ||
43822d37 | 387 | #keylength |
388 | _isEccKey() { | |
389 | _length="$1" | |
390 | ||
391 | if [ -z "$_length" ] ;then | |
392 | return 1 | |
393 | fi | |
394 | ||
395 | [ "$_length" != "1024" ] \ | |
396 | && [ "$_length" != "2048" ] \ | |
5fbc47eb | 397 | && [ "$_length" != "3072" ] \ |
43822d37 | 398 | && [ "$_length" != "4096" ] \ |
399 | && [ "$_length" != "8192" ] | |
400 | } | |
401 | ||
e22bcf7c | 402 | # _createkey 2048|ec-256 file |
403 | _createkey() { | |
404 | length="$1" | |
405 | f="$2" | |
43822d37 | 406 | eccname="$length" |
e22bcf7c | 407 | if _startswith "$length" "ec-" ; then |
e22bcf7c | 408 | length=$(printf $length | cut -d '-' -f 2-100) |
e22bcf7c | 409 | |
e22bcf7c | 410 | if [ "$length" = "256" ] ; then |
411 | eccname="prime256v1" | |
412 | fi | |
413 | if [ "$length" = "384" ] ; then | |
414 | eccname="secp384r1" | |
415 | fi | |
416 | if [ "$length" = "521" ] ; then | |
417 | eccname="secp521r1" | |
418 | fi | |
43822d37 | 419 | |
e22bcf7c | 420 | fi |
421 | ||
43822d37 | 422 | if [ -z "$length" ] ; then |
423 | length=2048 | |
424 | fi | |
425 | ||
cbcd7e0f | 426 | _debug "Use length $length" |
43822d37 | 427 | |
428 | if _isEccKey "$length" ; then | |
cbcd7e0f | 429 | _debug "Using ec name: $eccname" |
e22bcf7c | 430 | openssl ecparam -name $eccname -genkey 2>/dev/null > "$f" |
431 | else | |
cbcd7e0f | 432 | _debug "Using RSA: $length" |
e22bcf7c | 433 | openssl genrsa $length 2>/dev/null > "$f" |
434 | fi | |
43822d37 | 435 | |
436 | if [ "$?" != "0" ] ; then | |
437 | _err "Create key error." | |
438 | return 1 | |
439 | fi | |
e22bcf7c | 440 | } |
441 | ||
442 | #_createcsr cn san_list keyfile csrfile conf | |
443 | _createcsr() { | |
444 | _debug _createcsr | |
445 | domain="$1" | |
446 | domainlist="$2" | |
0c9546cc | 447 | csrkey="$3" |
e22bcf7c | 448 | csr="$4" |
449 | csrconf="$5" | |
450 | _debug2 domain "$domain" | |
451 | _debug2 domainlist "$domainlist" | |
0c9546cc | 452 | _debug2 csrkey "$csrkey" |
453 | _debug2 csr "$csr" | |
454 | _debug2 csrconf "$csrconf" | |
455 | ||
456 | printf "[ req_distinguished_name ]\n[ req ]\ndistinguished_name = req_distinguished_name\nreq_extensions = v3_req\n[ v3_req ]\n\nkeyUsage = nonRepudiation, digitalSignature, keyEncipherment" > "$csrconf" | |
457 | ||
e22bcf7c | 458 | if [ -z "$domainlist" ] || [ "$domainlist" = "no" ]; then |
459 | #single domain | |
460 | _info "Single domain" "$domain" | |
e22bcf7c | 461 | else |
462 | if _contains "$domainlist" "," ; then | |
463 | alt="DNS:$(echo $domainlist | sed "s/,/,DNS:/g")" | |
464 | else | |
465 | alt="DNS:$domainlist" | |
466 | fi | |
467 | #multi | |
468 | _info "Multi domain" "$alt" | |
0c9546cc | 469 | printf -- "\nsubjectAltName=$alt" >> "$csrconf" |
470 | fi | |
471 | if [ "$Le_OCSP_Stable" ] ; then | |
472 | _savedomainconf Le_OCSP_Stable "$Le_OCSP_Stable" | |
473 | printf -- "\nbasicConstraints = CA:FALSE\n1.3.6.1.5.5.7.1.24=DER:30:03:02:01:05" >> "$csrconf" | |
e22bcf7c | 474 | fi |
0c9546cc | 475 | openssl req -new -sha256 -key "$csrkey" -subj "/CN=$domain" -config "$csrconf" -out "$csr" |
e22bcf7c | 476 | } |
477 | ||
478 | #_signcsr key csr conf cert | |
479 | _signcsr() { | |
480 | key="$1" | |
481 | csr="$2" | |
482 | conf="$3" | |
483 | cert="$4" | |
5aa146a5 | 484 | _debug "_signcsr" |
e22bcf7c | 485 | |
5aa146a5 | 486 | _msg="$(openssl x509 -req -days 365 -in "$csr" -signkey "$key" -extensions v3_req -extfile "$conf" -out "$cert" 2>&1)" |
487 | _ret="$?" | |
488 | _debug "$_msg" | |
489 | return $_ret | |
e22bcf7c | 490 | } |
491 | ||
10afcaca | 492 | #_csrfile |
493 | _readSubjectFromCSR() { | |
494 | _csrfile="$1" | |
495 | if [ -z "$_csrfile" ] ; then | |
496 | _usage "_readSubjectFromCSR mycsr.csr" | |
497 | return 1 | |
498 | fi | |
1643b476 | 499 | openssl req -noout -in "$_csrfile" -subject | _egrep_o "CN=.*" | cut -d = -f 2 | cut -d / -f 1 | tr -d '\n' |
10afcaca | 500 | } |
501 | ||
502 | #_csrfile | |
503 | #echo comma separated domain list | |
504 | _readSubjectAltNamesFromCSR() { | |
505 | _csrfile="$1" | |
506 | if [ -z "$_csrfile" ] ; then | |
507 | _usage "_readSubjectAltNamesFromCSR mycsr.csr" | |
508 | return 1 | |
509 | fi | |
510 | ||
511 | _csrsubj="$(_readSubjectFromCSR "$_csrfile")" | |
512 | _debug _csrsubj "$_csrsubj" | |
513 | ||
1643b476 | 514 | _dnsAltnames="$(openssl req -noout -text -in "$_csrfile" | grep "^ *DNS:.*" | tr -d ' \n')" |
10afcaca | 515 | _debug _dnsAltnames "$_dnsAltnames" |
516 | ||
517 | if _contains "$_dnsAltnames," "DNS:$_csrsubj," ; then | |
518 | _debug "AltNames contains subject" | |
1643b476 | 519 | _dnsAltnames="$(printf "%s" "$_dnsAltnames," | sed "s/DNS:$_csrsubj,//g")" |
10afcaca | 520 | else |
521 | _debug "AltNames doesn't contain subject" | |
522 | fi | |
523 | ||
1643b476 | 524 | printf "%s" "$_dnsAltnames" | sed "s/DNS://g" |
10afcaca | 525 | } |
526 | ||
527 | #_csrfile | |
528 | _readKeyLengthFromCSR() { | |
529 | _csrfile="$1" | |
530 | if [ -z "$_csrfile" ] ; then | |
1643b476 | 531 | _usage "_readKeyLengthFromCSR mycsr.csr" |
10afcaca | 532 | return 1 |
533 | fi | |
534 | ||
535 | _outcsr="$(openssl req -noout -text -in "$_csrfile")" | |
536 | if _contains "$_outcsr" "Public Key Algorithm: id-ecPublicKey" ; then | |
537 | _debug "ECC CSR" | |
538 | echo "$_outcsr" | _egrep_o "^ *ASN1 OID:.*" | cut -d ':' -f 2 | tr -d ' ' | |
539 | else | |
540 | _debug "RSA CSR" | |
541 | echo "$_outcsr" | _egrep_o "^ *Public-Key:.*" | cut -d '(' -f 2 | cut -d ' ' -f 1 | |
542 | fi | |
543 | } | |
544 | ||
545 | ||
34c27e09 | 546 | _ss() { |
547 | _port="$1" | |
edf08da6 | 548 | |
549 | if _exists "ss" ; then | |
550 | _debug "Using: ss" | |
19539575 | 551 | ss -ntpl | grep ":$_port " |
edf08da6 | 552 | return 0 |
553 | fi | |
554 | ||
555 | if _exists "netstat" ; then | |
251fc37c | 556 | _debug "Using: netstat" |
ccb96535 | 557 | if netstat -h 2>&1 | grep "\-p proto" >/dev/null ; then |
558 | #for windows version netstat tool | |
19539575 | 559 | netstat -anb -p tcp | grep "LISTENING" | grep ":$_port " |
ccb96535 | 560 | else |
14165e5a | 561 | if netstat -help 2>&1 | grep "\-p protocol" >/dev/null ; then |
19539575 | 562 | netstat -an -p tcp | grep LISTEN | grep ":$_port " |
22ea4004 | 563 | elif netstat -help 2>&1 | grep -- '-P protocol' >/dev/null ; then |
564 | #for solaris | |
e3c66532 | 565 | netstat -an -P tcp | grep "\.$_port " | grep "LISTEN" |
edf08da6 | 566 | else |
19539575 | 567 | netstat -ntpl | grep ":$_port " |
edf08da6 | 568 | fi |
ccb96535 | 569 | fi |
34c27e09 | 570 | return 0 |
571 | fi | |
edf08da6 | 572 | |
34c27e09 | 573 | return 1 |
574 | } | |
575 | ||
43822d37 | 576 | #domain [password] [isEcc] |
ac2d5123 | 577 | toPkcs() { |
578 | domain="$1" | |
579 | pfxPassword="$2" | |
8663fb7e | 580 | if [ -z "$domain" ] ; then |
43822d37 | 581 | _usage "Usage: $PROJECT_ENTRY --toPkcs -d domain [--password pfx-password]" |
ac2d5123 | 582 | return 1 |
583 | fi | |
584 | ||
43822d37 | 585 | _isEcc="$3" |
ac2d5123 | 586 | |
43822d37 | 587 | _initpath "$domain" "$_isEcc" |
588 | ||
8663fb7e | 589 | if [ "$pfxPassword" ] ; then |
ac2d5123 | 590 | openssl pkcs12 -export -out "$CERT_PFX_PATH" -inkey "$CERT_KEY_PATH" -in "$CERT_PATH" -certfile "$CA_CERT_PATH" -password "pass:$pfxPassword" |
591 | else | |
592 | openssl pkcs12 -export -out "$CERT_PFX_PATH" -inkey "$CERT_KEY_PATH" -in "$CERT_PATH" -certfile "$CA_CERT_PATH" | |
593 | fi | |
594 | ||
8663fb7e | 595 | if [ "$?" = "0" ] ; then |
ac2d5123 | 596 | _info "Success, Pfx is exported to: $CERT_PFX_PATH" |
597 | fi | |
598 | ||
599 | } | |
600 | ||
5fbc47eb | 601 | #[2048] |
4c3b3608 | 602 | createAccountKey() { |
603 | _info "Creating account key" | |
8663fb7e | 604 | if [ -z "$1" ] ; then |
5fbc47eb | 605 | _usage "Usage: $PROJECT_ENTRY --createAccountKey --accountkeylength 2048" |
4c3b3608 | 606 | return |
607 | fi | |
608 | ||
5fbc47eb | 609 | length=$1 |
1643b476 | 610 | if _isEccKey "$length" ; then |
4c3b3608 | 611 | length=2048 |
612 | fi | |
613 | ||
5fbc47eb | 614 | if [ -z "$length" ] || [ "$length" = "no" ] ; then |
cbcd7e0f | 615 | _debug "Use default length 2048" |
4c3b3608 | 616 | length=2048 |
617 | fi | |
5fbc47eb | 618 | _debug length "$length" |
4c3b3608 | 619 | _initpath |
5fbc47eb | 620 | |
8663fb7e | 621 | if [ -f "$ACCOUNT_KEY_PATH" ] ; then |
4c3b3608 | 622 | _info "Account key exists, skip" |
623 | return | |
624 | else | |
625 | #generate account key | |
31a5487c | 626 | _createkey "$length" "$ACCOUNT_KEY_PATH" |
4c3b3608 | 627 | fi |
628 | ||
629 | } | |
630 | ||
43822d37 | 631 | #domain [length] |
4c3b3608 | 632 | createDomainKey() { |
633 | _info "Creating domain key" | |
8663fb7e | 634 | if [ -z "$1" ] ; then |
43822d37 | 635 | _usage "Usage: $PROJECT_ENTRY --createDomainKey -d domain.com [ --keylength 2048 ]" |
4c3b3608 | 636 | return |
637 | fi | |
638 | ||
639 | domain=$1 | |
e22bcf7c | 640 | length=$2 |
43822d37 | 641 | |
642 | _initpath $domain "$length" | |
e22bcf7c | 643 | |
8663fb7e | 644 | if [ ! -f "$CERT_KEY_PATH" ] || ( [ "$FORCE" ] && ! [ "$IS_RENEW" ] ); then |
e22bcf7c | 645 | _createkey "$length" "$CERT_KEY_PATH" |
4c3b3608 | 646 | else |
8663fb7e | 647 | if [ "$IS_RENEW" ] ; then |
4c3b3608 | 648 | _info "Domain key exists, skip" |
649 | return 0 | |
650 | else | |
651 | _err "Domain key exists, do you want to overwrite the key?" | |
41e3eafa | 652 | _err "Add '--force', and try again." |
4c3b3608 | 653 | return 1 |
654 | fi | |
655 | fi | |
656 | ||
657 | } | |
658 | ||
43822d37 | 659 | # domain domainlist isEcc |
4c3b3608 | 660 | createCSR() { |
661 | _info "Creating csr" | |
8663fb7e | 662 | if [ -z "$1" ] ; then |
43822d37 | 663 | _usage "Usage: $PROJECT_ENTRY --createCSR -d domain1.com [-d domain2.com -d domain3.com ... ]" |
4c3b3608 | 664 | return |
665 | fi | |
4c3b3608 | 666 | |
43822d37 | 667 | domain="$1" |
668 | domainlist="$2" | |
669 | _isEcc="$3" | |
670 | ||
671 | _initpath "$domain" "$_isEcc" | |
4c3b3608 | 672 | |
8663fb7e | 673 | if [ -f "$CSR_PATH" ] && [ "$IS_RENEW" ] && [ -z "$FORCE" ]; then |
4c3b3608 | 674 | _info "CSR exists, skip" |
675 | return | |
676 | fi | |
677 | ||
43822d37 | 678 | if [ ! -f "$CERT_KEY_PATH" ] ; then |
679 | _err "The key file is not found: $CERT_KEY_PATH" | |
680 | _err "Please create the key file first." | |
681 | return 1 | |
682 | fi | |
e22bcf7c | 683 | _createcsr "$domain" "$domainlist" "$CERT_KEY_PATH" "$CSR_PATH" "$DOMAIN_SSL_CONF" |
684 | ||
4c3b3608 | 685 | } |
686 | ||
166096dc | 687 | _urlencode() { |
4c3b3608 | 688 | __n=$(cat) |
689 | echo $__n | tr '/+' '_-' | tr -d '= ' | |
690 | } | |
691 | ||
692 | _time2str() { | |
693 | #BSD | |
694 | if date -u -d@$1 2>/dev/null ; then | |
695 | return | |
696 | fi | |
697 | ||
698 | #Linux | |
699 | if date -u -r $1 2>/dev/null ; then | |
700 | return | |
701 | fi | |
702 | ||
22ea4004 | 703 | #Soaris |
704 | if _exists adb ; then | |
705 | echo $(echo "0t${1}=Y" | adb) | |
706 | fi | |
707 | ||
4c3b3608 | 708 | } |
709 | ||
eae29099 | 710 | _normalizeJson() { |
711 | sed "s/\" *: *\([\"{\[]\)/\":\1/g" | sed "s/^ *\([^ ]\)/\1/" | tr -d "\r\n" | |
712 | } | |
713 | ||
44df2967 | 714 | _stat() { |
715 | #Linux | |
716 | if stat -c '%U:%G' "$1" 2>/dev/null ; then | |
717 | return | |
718 | fi | |
719 | ||
720 | #BSD | |
721 | if stat -f '%Su:%Sg' "$1" 2>/dev/null ; then | |
722 | return | |
723 | fi | |
3ad08e95 TB |
724 | |
725 | return 1; #error, 'stat' not found | |
44df2967 | 726 | } |
727 | ||
166096dc | 728 | #keyfile |
729 | _calcjwk() { | |
730 | keyfile="$1" | |
8663fb7e | 731 | if [ -z "$keyfile" ] ; then |
43822d37 | 732 | _usage "Usage: _calcjwk keyfile" |
166096dc | 733 | return 1 |
734 | fi | |
735 | EC_SIGN="" | |
736 | if grep "BEGIN RSA PRIVATE KEY" "$keyfile" > /dev/null 2>&1 ; then | |
737 | _debug "RSA key" | |
738 | pub_exp=$(openssl rsa -in $keyfile -noout -text | grep "^publicExponent:"| cut -d '(' -f 2 | cut -d 'x' -f 2 | cut -d ')' -f 1) | |
8663fb7e | 739 | if [ "${#pub_exp}" = "5" ] ; then |
166096dc | 740 | pub_exp=0$pub_exp |
741 | fi | |
22ea4004 | 742 | _debug3 pub_exp "$pub_exp" |
166096dc | 743 | |
744 | e=$(echo $pub_exp | _h2b | _base64) | |
22ea4004 | 745 | _debug3 e "$e" |
166096dc | 746 | |
747 | modulus=$(openssl rsa -in $keyfile -modulus -noout | cut -d '=' -f 2 ) | |
22ea4004 | 748 | _debug3 modulus "$modulus" |
19539575 | 749 | n="$(printf "%s" "$modulus"| _h2b | _base64 | _urlencode )" |
166096dc | 750 | jwk='{"e": "'$e'", "kty": "RSA", "n": "'$n'"}' |
22ea4004 | 751 | _debug3 jwk "$jwk" |
166096dc | 752 | |
753 | HEADER='{"alg": "RS256", "jwk": '$jwk'}' | |
22ea4004 | 754 | HEADERPLACE_PART1='{"nonce": "' |
755 | HEADERPLACE_PART2='", "alg": "RS256", "jwk": '$jwk'}' | |
166096dc | 756 | elif grep "BEGIN EC PRIVATE KEY" "$keyfile" > /dev/null 2>&1 ; then |
757 | _debug "EC key" | |
758 | EC_SIGN="1" | |
759 | crv="$(openssl ec -in $keyfile -noout -text 2>/dev/null | grep "^NIST CURVE:" | cut -d ":" -f 2 | tr -d " \r\n")" | |
22ea4004 | 760 | _debug3 crv "$crv" |
166096dc | 761 | |
762 | pubi="$(openssl ec -in $keyfile -noout -text 2>/dev/null | grep -n pub: | cut -d : -f 1)" | |
00a50605 | 763 | pubi=$(_math $pubi + 1) |
22ea4004 | 764 | _debug3 pubi "$pubi" |
166096dc | 765 | |
766 | pubj="$(openssl ec -in $keyfile -noout -text 2>/dev/null | grep -n "ASN1 OID:" | cut -d : -f 1)" | |
00a50605 | 767 | pubj=$(_math $pubj + 1) |
22ea4004 | 768 | _debug3 pubj "$pubj" |
166096dc | 769 | |
770 | pubtext="$(openssl ec -in $keyfile -noout -text 2>/dev/null | sed -n "$pubi,${pubj}p" | tr -d " \n\r")" | |
22ea4004 | 771 | _debug3 pubtext "$pubtext" |
166096dc | 772 | |
773 | xlen="$(printf "$pubtext" | tr -d ':' | wc -c)" | |
00a50605 | 774 | xlen=$(_math $xlen / 4) |
22ea4004 | 775 | _debug3 xlen "$xlen" |
00a50605 | 776 | |
19539575 | 777 | xend=$(_math "$xend" + 1) |
166096dc | 778 | x="$(printf $pubtext | cut -d : -f 2-$xend)" |
22ea4004 | 779 | _debug3 x "$x" |
166096dc | 780 | |
781 | x64="$(printf $x | tr -d : | _h2b | _base64 | _urlencode)" | |
22ea4004 | 782 | _debug3 x64 "$x64" |
00a50605 | 783 | |
19539575 | 784 | xend=$(_math "$xend" + 1) |
166096dc | 785 | y="$(printf $pubtext | cut -d : -f $xend-10000)" |
22ea4004 | 786 | _debug3 y "$y" |
166096dc | 787 | |
788 | y64="$(printf $y | tr -d : | _h2b | _base64 | _urlencode)" | |
22ea4004 | 789 | _debug3 y64 "$y64" |
166096dc | 790 | |
791 | jwk='{"kty": "EC", "crv": "'$crv'", "x": "'$x64'", "y": "'$y64'"}' | |
22ea4004 | 792 | _debug3 jwk "$jwk" |
166096dc | 793 | |
794 | HEADER='{"alg": "ES256", "jwk": '$jwk'}' | |
22ea4004 | 795 | HEADERPLACE_PART1='{"nonce": "' |
796 | HEADERPLACE_PART2='", "alg": "ES256", "jwk": '$jwk'}' | |
166096dc | 797 | else |
798 | _err "Only RSA or EC key is supported." | |
799 | return 1 | |
800 | fi | |
801 | ||
22ea4004 | 802 | _debug3 HEADER "$HEADER" |
166096dc | 803 | } |
fac1e367 | 804 | |
3aae1ae3 | 805 | _time() { |
806 | date -u "+%s" | |
807 | } | |
fac1e367 | 808 | |
809 | _mktemp() { | |
810 | if _exists mktemp ; then | |
811 | mktemp | |
7b164426 | 812 | return |
fac1e367 | 813 | fi |
3aae1ae3 | 814 | if [ -d "/tmp" ] ; then |
815 | echo "/tmp/${PROJECT_NAME}wefADf24sf.$(_time).tmp" | |
816 | return 0 | |
817 | fi | |
818 | _err "Can not create temp file." | |
fac1e367 | 819 | } |
820 | ||
821 | _inithttp() { | |
822 | ||
dfdc402f | 823 | if [ -z "$HTTP_HEADER" ] || ! touch "$HTTP_HEADER" ; then |
fac1e367 | 824 | HTTP_HEADER="$(_mktemp)" |
825 | _debug2 HTTP_HEADER "$HTTP_HEADER" | |
826 | fi | |
827 | ||
828 | if [ -z "$CURL" ] ; then | |
829 | CURL="curl -L --silent --dump-header $HTTP_HEADER " | |
830 | if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ] ; then | |
831 | _CURL_DUMP="$(_mktemp)" | |
832 | CURL="$CURL --trace-ascii $_CURL_DUMP " | |
833 | fi | |
834 | ||
78009539 PS |
835 | if [ "$CA_BUNDLE" ] ; then |
836 | CURL="$CURL --cacert $CA_BUNDLE " | |
837 | fi | |
838 | ||
fac1e367 | 839 | if [ "$HTTPS_INSECURE" ] ; then |
840 | CURL="$CURL --insecure " | |
841 | fi | |
842 | fi | |
843 | ||
844 | if [ -z "$WGET" ] ; then | |
845 | WGET="wget -q" | |
846 | if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ] ; then | |
847 | WGET="$WGET -d " | |
848 | fi | |
78009539 PS |
849 | if [ "$CA_BUNDLE" ] ; then |
850 | WGET="$WGET --ca-certificate $CA_BUNDLE " | |
851 | fi | |
fac1e367 | 852 | if [ "$HTTPS_INSECURE" ] ; then |
853 | WGET="$WGET --no-check-certificate " | |
854 | fi | |
855 | fi | |
856 | ||
857 | } | |
858 | ||
859 | ||
c839b2b0 | 860 | # body url [needbase64] [POST|PUT] |
c60883ef | 861 | _post() { |
862 | body="$1" | |
863 | url="$2" | |
864 | needbase64="$3" | |
a4270efa | 865 | httpmethod="$4" |
c60883ef | 866 | |
a4270efa | 867 | if [ -z "$httpmethod" ] ; then |
868 | httpmethod="POST" | |
869 | fi | |
870 | _debug $httpmethod | |
484d9d2a | 871 | _debug "url" "$url" |
30de13b4 | 872 | _debug2 "body" "$body" |
fac1e367 | 873 | |
874 | _inithttp | |
875 | ||
c60883ef | 876 | if _exists "curl" ; then |
fac1e367 | 877 | _CURL="$CURL" |
ec9fc8cb | 878 | _debug "_CURL" "$_CURL" |
8663fb7e | 879 | if [ "$needbase64" ] ; then |
690a5e20 | 880 | response="$($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" --data "$body" "$url" | _base64)" |
c60883ef | 881 | else |
690a5e20 | 882 | response="$($_CURL --user-agent "$USER_AGENT" -X $httpmethod -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" --data "$body" "$url" )" |
c60883ef | 883 | fi |
16679b57 | 884 | _ret="$?" |
687cfcc2 | 885 | if [ "$_ret" != "0" ] ; then |
87ab2d90 | 886 | _err "Please refer to https://curl.haxx.se/libcurl/c/libcurl-errors.html for error code: $_ret" |
887 | if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ] ; then | |
888 | _err "Here is the curl dump log:" | |
889 | _err "$(cat "$_CURL_DUMP")" | |
890 | fi | |
687cfcc2 | 891 | fi |
d0b748a4 | 892 | elif _exists "wget" ; then |
ec9fc8cb | 893 | _debug "WGET" "$WGET" |
8663fb7e | 894 | if [ "$needbase64" ] ; then |
8fb9a709 | 895 | if [ "$httpmethod"="POST" ] ; then |
690a5e20 | 896 | response="$($WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --post-data="$body" "$url" 2>"$HTTP_HEADER" | _base64)" |
8fb9a709 | 897 | else |
690a5e20 | 898 | response="$($WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --method $httpmethod --body-data="$body" "$url" 2>"$HTTP_HEADER" | _base64)" |
8fb9a709 | 899 | fi |
c60883ef | 900 | else |
8fb9a709 | 901 | if [ "$httpmethod"="POST" ] ; then |
690a5e20 | 902 | response="$($WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --post-data="$body" "$url" 2>"$HTTP_HEADER")" |
8fb9a709 | 903 | else |
690a5e20 | 904 | response="$($WGET -S -O - --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --method $httpmethod --body-data="$body" "$url" 2>"$HTTP_HEADER")" |
8fb9a709 | 905 | fi |
c60883ef | 906 | fi |
16679b57 | 907 | _ret="$?" |
687cfcc2 | 908 | if [ "$_ret" != "0" ] ; then |
909 | _err "Please refer to https://www.gnu.org/software/wget/manual/html_node/Exit-Status.html for error code: $_ret" | |
910 | fi | |
c60883ef | 911 | _sed_i "s/^ *//g" "$HTTP_HEADER" |
d0b748a4 | 912 | else |
913 | _ret="$?" | |
914 | _err "Neither curl nor wget is found, can not do $httpmethod." | |
c60883ef | 915 | fi |
16679b57 | 916 | _debug "_ret" "$_ret" |
19539575 | 917 | printf "%s" "$response" |
16679b57 | 918 | return $_ret |
c60883ef | 919 | } |
920 | ||
d529eb6d | 921 | |
75da0713 | 922 | # url getheader timeout |
c60883ef | 923 | _get() { |
a4270efa | 924 | _debug GET |
c60883ef | 925 | url="$1" |
926 | onlyheader="$2" | |
75da0713 | 927 | t="$3" |
c60883ef | 928 | _debug url $url |
75da0713 | 929 | _debug "timeout" "$t" |
fac1e367 | 930 | |
931 | _inithttp | |
932 | ||
c60883ef | 933 | if _exists "curl" ; then |
75da0713 | 934 | _CURL="$CURL" |
935 | if [ "$t" ] ; then | |
936 | _CURL="$_CURL --connect-timeout $t" | |
937 | fi | |
938 | _debug "_CURL" "$_CURL" | |
8663fb7e | 939 | if [ "$onlyheader" ] ; then |
690a5e20 | 940 | $_CURL -I --user-agent "$USER_AGENT" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" $url |
c60883ef | 941 | else |
690a5e20 | 942 | $_CURL --user-agent "$USER_AGENT" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" -H "$_H5" $url |
c60883ef | 943 | fi |
9aaf36cd | 944 | ret=$? |
fac1e367 | 945 | if [ "$ret" != "0" ] ; then |
d529eb6d | 946 | _err "Please refer to https://curl.haxx.se/libcurl/c/libcurl-errors.html for error code: $ret" |
fac1e367 | 947 | if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ] ; then |
948 | _err "Here is the curl dump log:" | |
949 | _err "$(cat "$_CURL_DUMP")" | |
950 | fi | |
951 | fi | |
d0b748a4 | 952 | elif _exists "wget" ; then |
75da0713 | 953 | _WGET="$WGET" |
954 | if [ "$t" ] ; then | |
955 | _WGET="$_WGET --timeout=$t" | |
956 | fi | |
957 | _debug "_WGET" "$_WGET" | |
8663fb7e | 958 | if [ "$onlyheader" ] ; then |
690a5e20 | 959 | $_WGET --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" -S -O /dev/null $url 2>&1 | sed 's/^[ ]*//g' |
c60883ef | 960 | else |
690a5e20 | 961 | $_WGET --user-agent="$USER_AGENT" --header "$_H5" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" -O - $url |
c60883ef | 962 | fi |
9aaf36cd | 963 | ret=$? |
fac1e367 | 964 | if [ "$ret" != "0" ] ; then |
d529eb6d | 965 | _err "Please refer to https://www.gnu.org/software/wget/manual/html_node/Exit-Status.html for error code: $ret" |
fac1e367 | 966 | fi |
d0b748a4 | 967 | else |
968 | ret=$? | |
969 | _err "Neither curl nor wget is found, can not do GET." | |
9aaf36cd | 970 | fi |
ec9fc8cb | 971 | _debug "ret" "$ret" |
c60883ef | 972 | return $ret |
973 | } | |
166096dc | 974 | |
fac1e367 | 975 | |
166096dc | 976 | # url payload needbase64 keyfile |
4c3b3608 | 977 | _send_signed_request() { |
978 | url=$1 | |
979 | payload=$2 | |
980 | needbase64=$3 | |
166096dc | 981 | keyfile=$4 |
8663fb7e | 982 | if [ -z "$keyfile" ] ; then |
166096dc | 983 | keyfile="$ACCOUNT_KEY_PATH" |
984 | fi | |
4c3b3608 | 985 | _debug url $url |
986 | _debug payload "$payload" | |
987 | ||
166096dc | 988 | if ! _calcjwk "$keyfile" ; then |
989 | return 1 | |
990 | fi | |
c60883ef | 991 | |
22ea4004 | 992 | payload64=$(printf "%s" "$payload" | _base64 | _urlencode) |
993 | _debug3 payload64 $payload64 | |
4c3b3608 | 994 | |
995 | nonceurl="$API/directory" | |
a272ee4f | 996 | _headers="$(_get $nonceurl "onlyheader")" |
997 | ||
7012b91f | 998 | if [ "$?" != "0" ] ; then |
999 | _err "Can not connect to $nonceurl to get nonce." | |
1000 | return 1 | |
1001 | fi | |
a272ee4f | 1002 | |
22ea4004 | 1003 | _debug3 _headers "$_headers" |
a272ee4f | 1004 | |
1005 | nonce="$( echo "$_headers" | grep "Replay-Nonce:" | head -1 | tr -d "\r\n " | cut -d ':' -f 2)" | |
1006 | ||
22ea4004 | 1007 | _debug3 nonce "$nonce" |
4c3b3608 | 1008 | |
22ea4004 | 1009 | protected="$HEADERPLACE_PART1$nonce$HEADERPLACE_PART2" |
1010 | _debug3 protected "$protected" | |
4c3b3608 | 1011 | |
166096dc | 1012 | protected64="$(printf "$protected" | _base64 | _urlencode)" |
22ea4004 | 1013 | _debug3 protected64 "$protected64" |
166096dc | 1014 | |
22ea4004 | 1015 | sig=$(printf "%s" "$protected64.$payload64" | _sign "$keyfile" "sha256" | _urlencode) |
1016 | _debug3 sig "$sig" | |
4c3b3608 | 1017 | |
1018 | body="{\"header\": $HEADER, \"protected\": \"$protected64\", \"payload\": \"$payload64\", \"signature\": \"$sig\"}" | |
22ea4004 | 1019 | _debug3 body "$body" |
4c3b3608 | 1020 | |
bbbdcb09 | 1021 | |
eae29099 | 1022 | response="$(_post "$body" $url "$needbase64")" |
7012b91f | 1023 | if [ "$?" != "0" ] ; then |
1024 | _err "Can not post to $url." | |
1025 | return 1 | |
1026 | fi | |
eae29099 | 1027 | _debug2 original "$response" |
1028 | ||
1029 | response="$( echo "$response" | _normalizeJson )" | |
4c3b3608 | 1030 | |
c60883ef | 1031 | responseHeaders="$(cat $HTTP_HEADER)" |
4c3b3608 | 1032 | |
a63b05a9 | 1033 | _debug2 responseHeaders "$responseHeaders" |
1034 | _debug2 response "$response" | |
c60883ef | 1035 | code="$(grep "^HTTP" $HTTP_HEADER | tail -1 | cut -d " " -f 2 | tr -d "\r\n" )" |
4c3b3608 | 1036 | _debug code $code |
1037 | ||
1038 | } | |
1039 | ||
4c3b3608 | 1040 | |
1041 | #setopt "file" "opt" "=" "value" [";"] | |
1042 | _setopt() { | |
1043 | __conf="$1" | |
1044 | __opt="$2" | |
1045 | __sep="$3" | |
1046 | __val="$4" | |
1047 | __end="$5" | |
8663fb7e | 1048 | if [ -z "$__opt" ] ; then |
43822d37 | 1049 | _usage usage: _setopt '"file" "opt" "=" "value" [";"]' |
4c3b3608 | 1050 | return |
1051 | fi | |
8663fb7e | 1052 | if [ ! -f "$__conf" ] ; then |
4c3b3608 | 1053 | touch "$__conf" |
1054 | fi | |
1055 | ||
22ea4004 | 1056 | if grep -n "^$__opt$__sep" "$__conf" > /dev/null ; then |
1057 | _debug3 OK | |
dceb3aca | 1058 | if _contains "$__val" "&" ; then |
4c3b3608 | 1059 | __val="$(echo $__val | sed 's/&/\\&/g')" |
1060 | fi | |
1061 | text="$(cat $__conf)" | |
6dfaaa70 | 1062 | echo "$text" | sed "s|^$__opt$__sep.*$|$__opt$__sep$__val$__end|" > "$__conf" |
4c3b3608 | 1063 | |
22ea4004 | 1064 | elif grep -n "^#$__opt$__sep" "$__conf" > /dev/null ; then |
dceb3aca | 1065 | if _contains "$__val" "&" ; then |
4c3b3608 | 1066 | __val="$(echo $__val | sed 's/&/\\&/g')" |
1067 | fi | |
1068 | text="$(cat $__conf)" | |
6dfaaa70 | 1069 | echo "$text" | sed "s|^#$__opt$__sep.*$|$__opt$__sep$__val$__end|" > "$__conf" |
4c3b3608 | 1070 | |
1071 | else | |
22ea4004 | 1072 | _debug3 APP |
4c3b3608 | 1073 | echo "$__opt$__sep$__val$__end" >> "$__conf" |
1074 | fi | |
22ea4004 | 1075 | _debug2 "$(grep -n "^$__opt$__sep" $__conf)" |
4c3b3608 | 1076 | } |
1077 | ||
1078 | #_savedomainconf key value | |
1079 | #save to domain.conf | |
1080 | _savedomainconf() { | |
0c9546cc | 1081 | _sdkey="$1" |
1082 | _sdvalue="$2" | |
8663fb7e | 1083 | if [ "$DOMAIN_CONF" ] ; then |
0c9546cc | 1084 | _setopt "$DOMAIN_CONF" "$_sdkey" "=" "\"$_sdvalue\"" |
4d2f38b0 | 1085 | else |
0c9546cc | 1086 | _err "DOMAIN_CONF is empty, can not save $_sdkey=$_sdvalue" |
4d2f38b0 | 1087 | fi |
1088 | } | |
1089 | ||
1090 | #_cleardomainconf key | |
1091 | _cleardomainconf() { | |
0c9546cc | 1092 | _sdkey="$1" |
4d2f38b0 | 1093 | if [ "$DOMAIN_CONF" ] ; then |
0c9546cc | 1094 | _sed_i "s/^$_sdkey.*$//" "$DOMAIN_CONF" |
4c3b3608 | 1095 | else |
0c9546cc | 1096 | _err "DOMAIN_CONF is empty, can not save $_sdkey=$value" |
4c3b3608 | 1097 | fi |
1098 | } | |
1099 | ||
61623d22 | 1100 | #_readdomainconf key |
1101 | _readdomainconf() { | |
0c9546cc | 1102 | _sdkey="$1" |
61623d22 | 1103 | if [ "$DOMAIN_CONF" ] ; then |
1104 | ( | |
0c9546cc | 1105 | eval $(grep "^$_sdkey *=" "$DOMAIN_CONF") |
1106 | eval "printf \"%s\" \"\$$_sdkey\"" | |
61623d22 | 1107 | ) |
1108 | else | |
0c9546cc | 1109 | _err "DOMAIN_CONF is empty, can not read $_sdkey" |
61623d22 | 1110 | fi |
1111 | } | |
1112 | ||
4c3b3608 | 1113 | #_saveaccountconf key value |
1114 | _saveaccountconf() { | |
0c9546cc | 1115 | _sckey="$1" |
1116 | _scvalue="$2" | |
8663fb7e | 1117 | if [ "$ACCOUNT_CONF_PATH" ] ; then |
0c9546cc | 1118 | _setopt "$ACCOUNT_CONF_PATH" "$_sckey" "=" "\"$_scvalue\"" |
4c3b3608 | 1119 | else |
0c9546cc | 1120 | _err "ACCOUNT_CONF_PATH is empty, can not save $_sckey=$_scvalue" |
4c3b3608 | 1121 | fi |
1122 | } | |
1123 | ||
fac1e367 | 1124 | #_clearaccountconf key |
1125 | _clearaccountconf() { | |
0c9546cc | 1126 | _scvalue="$1" |
fac1e367 | 1127 | if [ "$ACCOUNT_CONF_PATH" ] ; then |
0c9546cc | 1128 | _sed_i "s/^$_scvalue.*$//" "$ACCOUNT_CONF_PATH" |
fac1e367 | 1129 | else |
0c9546cc | 1130 | _err "ACCOUNT_CONF_PATH is empty, can not clear $_scvalue" |
fac1e367 | 1131 | fi |
1132 | } | |
1133 | ||
4c3b3608 | 1134 | _startserver() { |
1135 | content="$1" | |
6fc1447f | 1136 | _debug "startserver: $$" |
1b2e940d | 1137 | nchelp="$(nc -h 2>&1)" |
850c1128 | 1138 | |
399306a1 | 1139 | if echo "$nchelp" | grep "\-q[ ,]" >/dev/null ; then |
850c1128 | 1140 | _NC="nc -q 1 -l" |
1141 | else | |
f76eb452 | 1142 | if echo "$nchelp" | grep "GNU netcat" >/dev/null && echo "$nchelp" | grep "\-c, \-\-close" >/dev/null ; then |
1143 | _NC="nc -c -l" | |
6d60f288 | 1144 | elif echo "$nchelp" | grep "\-N" |grep "Shutdown the network socket after EOF on stdin" >/dev/null ; then |
1145 | _NC="nc -N -l" | |
f76eb452 | 1146 | else |
1147 | _NC="nc -l" | |
1148 | fi | |
4c3b3608 | 1149 | fi |
1b2e940d | 1150 | |
f76eb452 | 1151 | _debug "_NC" "$_NC" |
39c8f79f | 1152 | _debug Le_HTTPPort "$Le_HTTPPort" |
4c3b3608 | 1153 | # while true ; do |
8663fb7e | 1154 | if [ "$DEBUG" ] ; then |
2c554a4b | 1155 | if ! printf "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC -p $Le_HTTPPort ; then |
1156 | printf "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC $Le_HTTPPort ; | |
3aff11f6 | 1157 | fi |
4c3b3608 | 1158 | else |
2c554a4b | 1159 | if ! printf "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC -p $Le_HTTPPort > /dev/null 2>&1; then |
1160 | printf "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC $Le_HTTPPort > /dev/null 2>&1 | |
3aff11f6 | 1161 | fi |
4c3b3608 | 1162 | fi |
8663fb7e | 1163 | if [ "$?" != "0" ] ; then |
051c706d | 1164 | _err "nc listen error." |
6fc1447f | 1165 | exit 1 |
051c706d | 1166 | fi |
4c3b3608 | 1167 | # done |
1168 | } | |
1169 | ||
6fc1447f | 1170 | _stopserver(){ |
4c3b3608 | 1171 | pid="$1" |
6fc1447f | 1172 | _debug "pid" "$pid" |
8663fb7e | 1173 | if [ -z "$pid" ] ; then |
6fc1447f | 1174 | return |
1175 | fi | |
e22bcf7c | 1176 | |
dcf9cb58 | 1177 | _debug2 "Le_HTTPPort" "$Le_HTTPPort" |
1178 | if [ "$Le_HTTPPort" ] ; then | |
811bff6d | 1179 | if [ "$DEBUG" ] && [ "$DEBUG" -ge "3" ] ; then |
22ea4004 | 1180 | _get "http://localhost:$Le_HTTPPort" "" 1 |
dcf9cb58 | 1181 | else |
22ea4004 | 1182 | _get "http://localhost:$Le_HTTPPort" "" 1 >/dev/null 2>&1 |
dcf9cb58 | 1183 | fi |
1184 | fi | |
1185 | ||
1186 | _debug2 "Le_TLSPort" "$Le_TLSPort" | |
1187 | if [ "$Le_TLSPort" ] ; then | |
3324c0ae | 1188 | if [ "$DEBUG" ] && [ "$DEBUG" -ge "3" ] ; then |
75da0713 | 1189 | _get "https://localhost:$Le_TLSPort" "" 1 |
1190 | _get "https://localhost:$Le_TLSPort" "" 1 | |
dcf9cb58 | 1191 | else |
75da0713 | 1192 | _get "https://localhost:$Le_TLSPort" "" 1 >/dev/null 2>&1 |
1193 | _get "https://localhost:$Le_TLSPort" "" 1 >/dev/null 2>&1 | |
dcf9cb58 | 1194 | fi |
1195 | fi | |
4c3b3608 | 1196 | } |
1197 | ||
e22bcf7c | 1198 | |
1199 | # _starttlsserver san_a san_b port content | |
1200 | _starttlsserver() { | |
1201 | _info "Starting tls server." | |
1202 | san_a="$1" | |
1203 | san_b="$2" | |
1204 | port="$3" | |
1205 | content="$4" | |
1206 | ||
1207 | _debug san_a "$san_a" | |
1208 | _debug san_b "$san_b" | |
1209 | _debug port "$port" | |
1210 | ||
1211 | #create key TLS_KEY | |
1212 | if ! _createkey "2048" "$TLS_KEY" ; then | |
1213 | _err "Create tls validation key error." | |
1214 | return 1 | |
1215 | fi | |
1216 | ||
1217 | #create csr | |
1218 | alt="$san_a" | |
1219 | if [ "$san_b" ] ; then | |
1220 | alt="$alt,$san_b" | |
1221 | fi | |
1222 | if ! _createcsr "tls.acme.sh" "$alt" "$TLS_KEY" "$TLS_CSR" "$TLS_CONF" ; then | |
1223 | _err "Create tls validation csr error." | |
1224 | return 1 | |
1225 | fi | |
1226 | ||
1227 | #self signed | |
1228 | if ! _signcsr "$TLS_KEY" "$TLS_CSR" "$TLS_CONF" "$TLS_CERT" ; then | |
1229 | _err "Create tls validation cert error." | |
1230 | return 1 | |
1231 | fi | |
1232 | ||
1233 | #start openssl | |
bdbf323f | 1234 | _debug "openssl s_server -cert \"$TLS_CERT\" -key \"$TLS_KEY\" -accept $port -naccept 1 -tlsextdebug" |
fbad6a39 | 1235 | if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ] ; then |
75da0713 | 1236 | (printf "HTTP/1.1 200 OK\r\n\r\n$content" | openssl s_server -cert "$TLS_CERT" -key "$TLS_KEY" -accept $port -tlsextdebug ) & |
331c4bb6 | 1237 | else |
75da0713 | 1238 | (printf "HTTP/1.1 200 OK\r\n\r\n$content" | openssl s_server -cert "$TLS_CERT" -key "$TLS_KEY" -accept $port >/dev/null 2>&1) & |
331c4bb6 | 1239 | fi |
1240 | ||
e22bcf7c | 1241 | serverproc="$!" |
1242 | sleep 2 | |
1243 | _debug serverproc $serverproc | |
1244 | } | |
1245 | ||
5ea6e9c9 | 1246 | __initHome() { |
f3e4cea3 | 1247 | if [ -z "$_SCRIPT_HOME" ] ; then |
1248 | if _exists readlink && _exists dirname ; then | |
1249 | _debug "Lets guess script dir." | |
1250 | _debug "_SCRIPT_" "$_SCRIPT_" | |
1251 | _script="$(readlink -f "$_SCRIPT_")" | |
1252 | _debug "_script" "$_script" | |
1253 | _script_home="$(dirname "$_script")" | |
1254 | _debug "_script_home" "$_script_home" | |
1255 | if [ -d "$_script_home" ] ; then | |
1256 | _SCRIPT_HOME="$_script_home" | |
1257 | else | |
1258 | _err "It seems the script home is not correct:$_script_home" | |
1259 | fi | |
1260 | fi | |
1261 | fi | |
1262 | ||
1263 | ||
8663fb7e | 1264 | if [ -z "$LE_WORKING_DIR" ] ; then |
f3e4cea3 | 1265 | if [ -f "$DEFAULT_INSTALL_HOME/account.conf" ] ; then |
1266 | _debug "It seems tha $PROJECT_NAME is already installed in $DEFAULT_INSTALL_HOME" | |
1267 | LE_WORKING_DIR="$DEFAULT_INSTALL_HOME" | |
1268 | else | |
1269 | LE_WORKING_DIR="$_SCRIPT_HOME" | |
1270 | fi | |
4c3b3608 | 1271 | fi |
1272 | ||
f3e4cea3 | 1273 | if [ -z "$LE_WORKING_DIR" ] ; then |
1274 | _debug "Using default home:$DEFAULT_INSTALL_HOME" | |
1275 | LE_WORKING_DIR="$DEFAULT_INSTALL_HOME" | |
1276 | fi | |
1277 | ||
d53289d7 | 1278 | _DEFAULT_ACCOUNT_CONF_PATH="$LE_WORKING_DIR/account.conf" |
1279 | ||
8663fb7e | 1280 | if [ -z "$ACCOUNT_CONF_PATH" ] ; then |
1281 | if [ -f "$_DEFAULT_ACCOUNT_CONF_PATH" ] ; then | |
1282 | . "$_DEFAULT_ACCOUNT_CONF_PATH" | |
635695ec | 1283 | fi |
d53289d7 | 1284 | fi |
1285 | ||
8663fb7e | 1286 | if [ -z "$ACCOUNT_CONF_PATH" ] ; then |
d53289d7 | 1287 | ACCOUNT_CONF_PATH="$_DEFAULT_ACCOUNT_CONF_PATH" |
4c3b3608 | 1288 | fi |
5ea6e9c9 | 1289 | } |
1290 | ||
1291 | #[domain] [keylength] | |
1292 | _initpath() { | |
1293 | ||
1294 | __initHome | |
1295 | ||
8663fb7e | 1296 | if [ -f "$ACCOUNT_CONF_PATH" ] ; then |
1297 | . "$ACCOUNT_CONF_PATH" | |
4c3b3608 | 1298 | fi |
1299 | ||
8663fb7e | 1300 | if [ "$IN_CRON" ] ; then |
1301 | if [ ! "$_USER_PATH_EXPORTED" ] ; then | |
281aa349 | 1302 | _USER_PATH_EXPORTED=1 |
1303 | export PATH="$USER_PATH:$PATH" | |
1304 | fi | |
1305 | fi | |
1306 | ||
8663fb7e | 1307 | if [ -z "$API" ] ; then |
1308 | if [ -z "$STAGE" ] ; then | |
4c3b3608 | 1309 | API="$DEFAULT_CA" |
1310 | else | |
1311 | API="$STAGE_CA" | |
1312 | _info "Using stage api:$API" | |
1313 | fi | |
1314 | fi | |
1315 | ||
8663fb7e | 1316 | if [ -z "$ACME_DIR" ] ; then |
4c3b3608 | 1317 | ACME_DIR="/home/.acme" |
1318 | fi | |
1319 | ||
8663fb7e | 1320 | if [ -z "$APACHE_CONF_BACKUP_DIR" ] ; then |
8a144f4d | 1321 | APACHE_CONF_BACKUP_DIR="$LE_WORKING_DIR" |
4c3b3608 | 1322 | fi |
1323 | ||
8663fb7e | 1324 | if [ -z "$USER_AGENT" ] ; then |
bbbdcb09 | 1325 | USER_AGENT="$DEFAULT_USER_AGENT" |
1326 | fi | |
1327 | ||
933c169d | 1328 | if [ -z "$HTTP_HEADER" ] ; then |
1329 | HTTP_HEADER="$LE_WORKING_DIR/http.header" | |
1330 | fi | |
b2817897 | 1331 | |
1332 | _DEFAULT_ACCOUNT_KEY_PATH="$LE_WORKING_DIR/account.key" | |
8663fb7e | 1333 | if [ -z "$ACCOUNT_KEY_PATH" ] ; then |
b2817897 | 1334 | ACCOUNT_KEY_PATH="$_DEFAULT_ACCOUNT_KEY_PATH" |
4c3b3608 | 1335 | fi |
b2817897 | 1336 | |
a79b26af RD |
1337 | _DEFAULT_CERT_HOME="$LE_WORKING_DIR" |
1338 | if [ -z "$CERT_HOME" ] ; then | |
1339 | CERT_HOME="$_DEFAULT_CERT_HOME" | |
1340 | fi | |
1341 | ||
5fbc47eb | 1342 | if [ -z "$1" ] ; then |
4c3b3608 | 1343 | return 0 |
1344 | fi | |
5fbc47eb | 1345 | domain="$1" |
1346 | _ilength="$2" | |
4c3b3608 | 1347 | |
8663fb7e | 1348 | if [ -z "$DOMAIN_PATH" ] ; then |
43822d37 | 1349 | domainhome="$CERT_HOME/$domain" |
1350 | domainhomeecc="$CERT_HOME/$domain$ECC_SUFFIX" | |
1351 | ||
4c3b3608 | 1352 | DOMAIN_PATH="$domainhome" |
43822d37 | 1353 | |
5fbc47eb | 1354 | if _isEccKey "$_ilength" ; then |
43822d37 | 1355 | DOMAIN_PATH="$domainhomeecc" |
1356 | else | |
1357 | if [ ! -d "$domainhome" ] && [ -d "$domainhomeecc" ] ; then | |
6d4e903b | 1358 | _info "The domain '$domain' seems to have a ECC cert already, please add '$(__red "--ecc")' parameter if you want to use that cert." |
43822d37 | 1359 | fi |
1360 | fi | |
1361 | _debug DOMAIN_PATH "$DOMAIN_PATH" | |
4c3b3608 | 1362 | fi |
43822d37 | 1363 | |
933c169d | 1364 | if [ ! -d "$DOMAIN_PATH" ] ; then |
1365 | if ! mkdir -p "$DOMAIN_PATH" ; then | |
1366 | _err "Can not create domain path: $DOMAIN_PATH" | |
1367 | return 1 | |
1368 | fi | |
1369 | fi | |
1370 | ||
8663fb7e | 1371 | if [ -z "$DOMAIN_CONF" ] ; then |
43822d37 | 1372 | DOMAIN_CONF="$DOMAIN_PATH/$domain.conf" |
4c3b3608 | 1373 | fi |
1374 | ||
8663fb7e | 1375 | if [ -z "$DOMAIN_SSL_CONF" ] ; then |
0c9546cc | 1376 | DOMAIN_SSL_CONF="$DOMAIN_PATH/$domain.csr.conf" |
4c3b3608 | 1377 | fi |
1378 | ||
8663fb7e | 1379 | if [ -z "$CSR_PATH" ] ; then |
43822d37 | 1380 | CSR_PATH="$DOMAIN_PATH/$domain.csr" |
4c3b3608 | 1381 | fi |
8663fb7e | 1382 | if [ -z "$CERT_KEY_PATH" ] ; then |
43822d37 | 1383 | CERT_KEY_PATH="$DOMAIN_PATH/$domain.key" |
4c3b3608 | 1384 | fi |
8663fb7e | 1385 | if [ -z "$CERT_PATH" ] ; then |
43822d37 | 1386 | CERT_PATH="$DOMAIN_PATH/$domain.cer" |
4c3b3608 | 1387 | fi |
8663fb7e | 1388 | if [ -z "$CA_CERT_PATH" ] ; then |
43822d37 | 1389 | CA_CERT_PATH="$DOMAIN_PATH/ca.cer" |
4c3b3608 | 1390 | fi |
8663fb7e | 1391 | if [ -z "$CERT_FULLCHAIN_PATH" ] ; then |
43822d37 | 1392 | CERT_FULLCHAIN_PATH="$DOMAIN_PATH/fullchain.cer" |
caf1fc10 | 1393 | fi |
8663fb7e | 1394 | if [ -z "$CERT_PFX_PATH" ] ; then |
43822d37 | 1395 | CERT_PFX_PATH="$DOMAIN_PATH/$domain.pfx" |
ac2d5123 | 1396 | fi |
e22bcf7c | 1397 | |
1398 | if [ -z "$TLS_CONF" ] ; then | |
43822d37 | 1399 | TLS_CONF="$DOMAIN_PATH/tls.valdation.conf" |
e22bcf7c | 1400 | fi |
1401 | if [ -z "$TLS_CERT" ] ; then | |
43822d37 | 1402 | TLS_CERT="$DOMAIN_PATH/tls.valdation.cert" |
e22bcf7c | 1403 | fi |
1404 | if [ -z "$TLS_KEY" ] ; then | |
43822d37 | 1405 | TLS_KEY="$DOMAIN_PATH/tls.valdation.key" |
e22bcf7c | 1406 | fi |
1407 | if [ -z "$TLS_CSR" ] ; then | |
43822d37 | 1408 | TLS_CSR="$DOMAIN_PATH/tls.valdation.csr" |
e22bcf7c | 1409 | fi |
1410 | ||
4c3b3608 | 1411 | } |
1412 | ||
1413 | ||
1414 | _apachePath() { | |
c3dd3ef0 | 1415 | _APACHECTL="apachectl" |
80a0a7b5 | 1416 | if ! _exists apachectl ; then |
e4a19585 | 1417 | if _exists apache2ctl ; then |
c3dd3ef0 | 1418 | _APACHECTL="apache2ctl" |
e4a19585 | 1419 | else |
bc96082f | 1420 | _err "'apachectl not found. It seems that apache is not installed, or you are not root user.'" |
e4a19585 | 1421 | _err "Please use webroot mode to try again." |
1422 | return 1 | |
1423 | fi | |
80a0a7b5 | 1424 | fi |
c3dd3ef0 | 1425 | httpdconfname="$($_APACHECTL -V | grep SERVER_CONFIG_FILE= | cut -d = -f 2 | tr -d '"' )" |
78768e98 | 1426 | _debug httpdconfname "$httpdconfname" |
dceb3aca | 1427 | if _startswith "$httpdconfname" '/' ; then |
d62ee940 | 1428 | httpdconf="$httpdconfname" |
c456d954 | 1429 | httpdconfname="$(basename $httpdconfname)" |
d62ee940 | 1430 | else |
c3dd3ef0 | 1431 | httpdroot="$($_APACHECTL -V | grep HTTPD_ROOT= | cut -d = -f 2 | tr -d '"' )" |
78768e98 | 1432 | _debug httpdroot "$httpdroot" |
d62ee940 | 1433 | httpdconf="$httpdroot/$httpdconfname" |
8f63baf7 | 1434 | httpdconfname="$(basename $httpdconfname)" |
d62ee940 | 1435 | fi |
78768e98 | 1436 | _debug httpdconf "$httpdconf" |
8f63baf7 | 1437 | _debug httpdconfname "$httpdconfname" |
78768e98 | 1438 | if [ ! -f "$httpdconf" ] ; then |
1439 | _err "Apache Config file not found" "$httpdconf" | |
4c3b3608 | 1440 | return 1 |
1441 | fi | |
1442 | return 0 | |
1443 | } | |
1444 | ||
1445 | _restoreApache() { | |
8663fb7e | 1446 | if [ -z "$usingApache" ] ; then |
4c3b3608 | 1447 | return 0 |
1448 | fi | |
1449 | _initpath | |
1450 | if ! _apachePath ; then | |
1451 | return 1 | |
1452 | fi | |
1453 | ||
8663fb7e | 1454 | if [ ! -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname" ] ; then |
4c3b3608 | 1455 | _debug "No config file to restore." |
1456 | return 0 | |
1457 | fi | |
1458 | ||
ff3bce32 | 1459 | cat "$APACHE_CONF_BACKUP_DIR/$httpdconfname" > "$httpdconf" |
5ef501c5 | 1460 | _debug "Restored: $httpdconf." |
c3dd3ef0 | 1461 | if ! $_APACHECTL -t >/dev/null 2>&1 ; then |
4c3b3608 | 1462 | _err "Sorry, restore apache config error, please contact me." |
1463 | return 1; | |
1464 | fi | |
5ef501c5 | 1465 | _debug "Restored successfully." |
4c3b3608 | 1466 | rm -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname" |
1467 | return 0 | |
1468 | } | |
1469 | ||
1470 | _setApache() { | |
1471 | _initpath | |
1472 | if ! _apachePath ; then | |
1473 | return 1 | |
1474 | fi | |
1475 | ||
5fc5016d | 1476 | #test the conf first |
869578ce | 1477 | _info "Checking if there is an error in the apache config file before starting." |
c3dd3ef0 | 1478 | _msg="$($_APACHECTL -t 2>&1 )" |
5fc5016d | 1479 | if [ "$?" != "0" ] ; then |
869578ce | 1480 | _err "Sorry, apache config file has error, please fix it first, then try again." |
1481 | _err "Don't worry, there is nothing changed to your system." | |
5fc5016d | 1482 | _err "$_msg" |
1483 | return 1; | |
1484 | else | |
1485 | _info "OK" | |
1486 | fi | |
1487 | ||
4c3b3608 | 1488 | #backup the conf |
5778811a | 1489 | _debug "Backup apache config file" "$httpdconf" |
8f63baf7 | 1490 | if ! cp "$httpdconf" "$APACHE_CONF_BACKUP_DIR/" ; then |
869578ce | 1491 | _err "Can not backup apache config file, so abort. Don't worry, the apache config is not changed." |
8f63baf7 | 1492 | _err "This might be a bug of $PROJECT_NAME , pleae report issue: $PROJECT" |
1493 | return 1 | |
1494 | fi | |
4c3b3608 | 1495 | _info "JFYI, Config file $httpdconf is backuped to $APACHE_CONF_BACKUP_DIR/$httpdconfname" |
1496 | _info "In case there is an error that can not be restored automatically, you may try restore it yourself." | |
1497 | _info "The backup file will be deleted on sucess, just forget it." | |
1498 | ||
1499 | #add alias | |
b09d597c | 1500 | |
c3dd3ef0 | 1501 | apacheVer="$($_APACHECTL -V | grep "Server version:" | cut -d : -f 2 | cut -d " " -f 2 | cut -d '/' -f 2 )" |
b09d597c | 1502 | _debug "apacheVer" "$apacheVer" |
1503 | apacheMajer="$(echo "$apacheVer" | cut -d . -f 1)" | |
1504 | apacheMinor="$(echo "$apacheVer" | cut -d . -f 2)" | |
1505 | ||
5778811a | 1506 | if [ "$apacheVer" ] && [ "$apacheMajer$apacheMinor" -ge "24" ] ; then |
b09d597c | 1507 | echo " |
4c3b3608 | 1508 | Alias /.well-known/acme-challenge $ACME_DIR |
1509 | ||
1510 | <Directory $ACME_DIR > | |
1511 | Require all granted | |
b09d597c | 1512 | </Directory> |
5778811a | 1513 | " >> "$httpdconf" |
b09d597c | 1514 | else |
1515 | echo " | |
1516 | Alias /.well-known/acme-challenge $ACME_DIR | |
1517 | ||
1518 | <Directory $ACME_DIR > | |
1519 | Order allow,deny | |
1520 | Allow from all | |
4c3b3608 | 1521 | </Directory> |
5778811a | 1522 | " >> "$httpdconf" |
b09d597c | 1523 | fi |
1524 | ||
c3dd3ef0 | 1525 | _msg="$($_APACHECTL -t 2>&1 )" |
5fc5016d | 1526 | if [ "$?" != "0" ] ; then |
1527 | _err "Sorry, apache config error" | |
1528 | if _restoreApache ; then | |
869578ce | 1529 | _err "The apache config file is restored." |
5fc5016d | 1530 | else |
869578ce | 1531 | _err "Sorry, The apache config file can not be restored, please report bug." |
5fc5016d | 1532 | fi |
4c3b3608 | 1533 | return 1; |
1534 | fi | |
1535 | ||
8663fb7e | 1536 | if [ ! -d "$ACME_DIR" ] ; then |
4c3b3608 | 1537 | mkdir -p "$ACME_DIR" |
1538 | chmod 755 "$ACME_DIR" | |
1539 | fi | |
1540 | ||
c3dd3ef0 | 1541 | if ! $_APACHECTL graceful ; then |
1542 | _err "Sorry, $_APACHECTL graceful error, please contact me." | |
4c3b3608 | 1543 | _restoreApache |
1544 | return 1; | |
1545 | fi | |
1546 | usingApache="1" | |
1547 | return 0 | |
1548 | } | |
1549 | ||
5ef501c5 | 1550 | _clearup() { |
4c3b3608 | 1551 | _stopserver $serverproc |
1552 | serverproc="" | |
1553 | _restoreApache | |
e22bcf7c | 1554 | if [ -z "$DEBUG" ] ; then |
1555 | rm -f "$TLS_CONF" | |
1556 | rm -f "$TLS_CERT" | |
1557 | rm -f "$TLS_KEY" | |
1558 | rm -f "$TLS_CSR" | |
1559 | fi | |
4c3b3608 | 1560 | } |
1561 | ||
1562 | # webroot removelevel tokenfile | |
1563 | _clearupwebbroot() { | |
1564 | __webroot="$1" | |
8663fb7e | 1565 | if [ -z "$__webroot" ] ; then |
4c3b3608 | 1566 | _debug "no webroot specified, skip" |
1567 | return 0 | |
1568 | fi | |
1569 | ||
dcf9cb58 | 1570 | _rmpath="" |
8663fb7e | 1571 | if [ "$2" = '1' ] ; then |
dcf9cb58 | 1572 | _rmpath="$__webroot/.well-known" |
8663fb7e | 1573 | elif [ "$2" = '2' ] ; then |
dcf9cb58 | 1574 | _rmpath="$__webroot/.well-known/acme-challenge" |
8663fb7e | 1575 | elif [ "$2" = '3' ] ; then |
dcf9cb58 | 1576 | _rmpath="$__webroot/.well-known/acme-challenge/$3" |
4c3b3608 | 1577 | else |
cc179731 | 1578 | _debug "Skip for removelevel:$2" |
4c3b3608 | 1579 | fi |
1580 | ||
dcf9cb58 | 1581 | if [ "$_rmpath" ] ; then |
1582 | if [ "$DEBUG" ] ; then | |
1583 | _debug "Debugging, skip removing: $_rmpath" | |
1584 | else | |
1585 | rm -rf "$_rmpath" | |
1586 | fi | |
1587 | fi | |
1588 | ||
4c3b3608 | 1589 | return 0 |
1590 | ||
1591 | } | |
1592 | ||
b0070f03 | 1593 | _on_before_issue() { |
1594 | #run pre hook | |
1595 | if [ "$Le_PreHook" ] ; then | |
1596 | _info "Run pre hook:'$Le_PreHook'" | |
1597 | if ! ( | |
1598 | cd "$DOMAIN_PATH" && eval "$Le_PreHook" | |
1599 | ) ; then | |
1600 | _err "Error when run pre hook." | |
1601 | return 1 | |
1602 | fi | |
1603 | fi | |
1604 | } | |
1605 | ||
1606 | _on_issue_err() { | |
1607 | #run the post hook | |
1608 | if [ "$Le_PostHook" ] ; then | |
1609 | _info "Run post hook:'$Le_PostHook'" | |
1610 | if ! ( | |
1611 | cd "$DOMAIN_PATH" && eval "$Le_PostHook" | |
1612 | ) ; then | |
1613 | _err "Error when run post hook." | |
1614 | return 1 | |
1615 | fi | |
1616 | fi | |
1617 | } | |
1618 | ||
1619 | _on_issue_success() { | |
1620 | #run the post hook | |
1621 | if [ "$Le_PostHook" ] ; then | |
1622 | _info "Run post hook:'$Le_PostHook'" | |
1623 | if ! ( | |
1624 | cd "$DOMAIN_PATH" && eval "$Le_PostHook" | |
1625 | ) ; then | |
1626 | _err "Error when run post hook." | |
1627 | return 1 | |
1628 | fi | |
1629 | fi | |
1630 | ||
1631 | #run renew hook | |
1632 | if [ "$IS_RENEW" ] && [ "$Le_RenewHook" ] ; then | |
1633 | _info "Run renew hook:'$Le_RenewHook'" | |
1634 | if ! ( | |
1635 | cd "$DOMAIN_PATH" && eval "$Le_RenewHook" | |
1636 | ) ; then | |
1637 | _err "Error when run renew hook." | |
1638 | return 1 | |
1639 | fi | |
1640 | fi | |
1641 | ||
1642 | } | |
1643 | ||
1644 | ||
10afcaca | 1645 | #webroot, domain domainlist keylength |
4c3b3608 | 1646 | issue() { |
8663fb7e | 1647 | if [ -z "$2" ] ; then |
43822d37 | 1648 | _usage "Usage: $PROJECT_ENTRY --issue -d a.com -w /path/to/webroot/a.com/ " |
4c3b3608 | 1649 | return 1 |
1650 | fi | |
1651 | Le_Webroot="$1" | |
1652 | Le_Domain="$2" | |
1653 | Le_Alt="$3" | |
1654 | Le_Keylength="$4" | |
1655 | Le_RealCertPath="$5" | |
1656 | Le_RealKeyPath="$6" | |
1657 | Le_RealCACertPath="$7" | |
1658 | Le_ReloadCmd="$8" | |
a63b05a9 | 1659 | Le_RealFullChainPath="$9" |
b0070f03 | 1660 | Le_PreHook="${10}" |
1661 | Le_PostHook="${11}" | |
1662 | Le_RenewHook="${12}" | |
4c3b3608 | 1663 | |
eccec5f6 | 1664 | #remove these later. |
8663fb7e | 1665 | if [ "$Le_Webroot" = "dns-cf" ] ; then |
eccec5f6 | 1666 | Le_Webroot="dns_cf" |
1667 | fi | |
8663fb7e | 1668 | if [ "$Le_Webroot" = "dns-dp" ] ; then |
eccec5f6 | 1669 | Le_Webroot="dns_dp" |
1670 | fi | |
8663fb7e | 1671 | if [ "$Le_Webroot" = "dns-cx" ] ; then |
eccec5f6 | 1672 | Le_Webroot="dns_cx" |
1673 | fi | |
4c3b3608 | 1674 | |
43822d37 | 1675 | if [ ! "$IS_RENEW" ] ; then |
1676 | _initpath $Le_Domain "$Le_Keylength" | |
1677 | mkdir -p "$DOMAIN_PATH" | |
1678 | fi | |
eccec5f6 | 1679 | |
8663fb7e | 1680 | if [ -f "$DOMAIN_CONF" ] ; then |
61623d22 | 1681 | Le_NextRenewTime=$(_readdomainconf Le_NextRenewTime) |
a4270efa | 1682 | _debug Le_NextRenewTime "$Le_NextRenewTime" |
3aae1ae3 | 1683 | if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ $(_time) -lt $Le_NextRenewTime ] ; then |
e2053b22 | 1684 | _info "Skip, Next renewal time is: $(__green "$(_readdomainconf Le_NextRenewTimeStr)")" |
1685 | _info "Add '$(__red '--force')' to force to renew." | |
cc179731 | 1686 | return $RENEW_SKIP |
4c3b3608 | 1687 | fi |
1688 | fi | |
96a46cfc | 1689 | |
4d2f38b0 | 1690 | _savedomainconf "Le_Domain" "$Le_Domain" |
1691 | _savedomainconf "Le_Alt" "$Le_Alt" | |
1692 | _savedomainconf "Le_Webroot" "$Le_Webroot" | |
4c3b3608 | 1693 | |
b0070f03 | 1694 | _savedomainconf "Le_PreHook" "$Le_PreHook" |
1695 | _savedomainconf "Le_PostHook" "$Le_PostHook" | |
1696 | _savedomainconf "Le_RenewHook" "$Le_RenewHook" | |
1697 | ||
1698 | if ! _on_before_issue ; then | |
1699 | _err "_on_before_issue." | |
1700 | return 1 | |
1701 | fi | |
61623d22 | 1702 | |
8663fb7e | 1703 | if [ "$Le_Alt" = "no" ] ; then |
4c3b3608 | 1704 | Le_Alt="" |
1705 | fi | |
61623d22 | 1706 | |
c53da1ef | 1707 | if _hasfield "$Le_Webroot" "no" ; then |
4c3b3608 | 1708 | _info "Standalone mode." |
5ef501c5 | 1709 | if ! _exists "nc" ; then |
4c3b3608 | 1710 | _err "Please install netcat(nc) tools first." |
b0070f03 | 1711 | _on_issue_err |
4c3b3608 | 1712 | return 1 |
1713 | fi | |
1714 | ||
8663fb7e | 1715 | if [ -z "$Le_HTTPPort" ] ; then |
4c3b3608 | 1716 | Le_HTTPPort=80 |
054cb72e | 1717 | else |
1718 | _savedomainconf "Le_HTTPPort" "$Le_HTTPPort" | |
1719 | fi | |
4c3b3608 | 1720 | |
251fc37c | 1721 | netprc="$(_ss "$Le_HTTPPort" | grep "$Le_HTTPPort")" |
8663fb7e | 1722 | if [ "$netprc" ] ; then |
4c3b3608 | 1723 | _err "$netprc" |
1724 | _err "tcp port $Le_HTTPPort is already used by $(echo "$netprc" | cut -d : -f 4)" | |
1725 | _err "Please stop it first" | |
b0070f03 | 1726 | _on_issue_err |
4c3b3608 | 1727 | return 1 |
1728 | fi | |
1729 | fi | |
1730 | ||
e22bcf7c | 1731 | if _hasfield "$Le_Webroot" "$W_TLS" ; then |
1732 | _info "Standalone tls mode." | |
1733 | ||
1734 | if [ -z "$Le_TLSPort" ] ; then | |
1735 | Le_TLSPort=443 | |
1736 | else | |
1737 | _savedomainconf "Le_TLSPort" "$Le_TLSPort" | |
1738 | fi | |
1739 | ||
1740 | netprc="$(_ss "$Le_TLSPort" | grep "$Le_TLSPort")" | |
1741 | if [ "$netprc" ] ; then | |
1742 | _err "$netprc" | |
1743 | _err "tcp port $Le_TLSPort is already used by $(echo "$netprc" | cut -d : -f 4)" | |
1744 | _err "Please stop it first" | |
b0070f03 | 1745 | _on_issue_err |
e22bcf7c | 1746 | return 1 |
1747 | fi | |
1748 | fi | |
1749 | ||
c53da1ef | 1750 | if _hasfield "$Le_Webroot" "apache" ; then |
4c3b3608 | 1751 | if ! _setApache ; then |
1752 | _err "set up apache error. Report error to me." | |
b0070f03 | 1753 | _on_issue_err |
4c3b3608 | 1754 | return 1 |
1755 | fi | |
4c3b3608 | 1756 | else |
1757 | usingApache="" | |
1758 | fi | |
1759 | ||
8663fb7e | 1760 | if [ ! -f "$ACCOUNT_KEY_PATH" ] ; then |
5fbc47eb | 1761 | _acck="no" |
1762 | if [ "$Le_Keylength" ] ; then | |
1763 | _acck="$Le_Keylength" | |
1764 | fi | |
1765 | if ! createAccountKey "$_acck" ; then | |
41e3eafa | 1766 | _err "Create account key error." |
8663fb7e | 1767 | if [ "$usingApache" ] ; then |
5ef501c5 | 1768 | _restoreApache |
1769 | fi | |
b0070f03 | 1770 | _on_issue_err |
41e3eafa | 1771 | return 1 |
1772 | fi | |
1773 | fi | |
1774 | ||
166096dc | 1775 | if ! _calcjwk "$ACCOUNT_KEY_PATH" ; then |
8663fb7e | 1776 | if [ "$usingApache" ] ; then |
5ef501c5 | 1777 | _restoreApache |
1778 | fi | |
b0070f03 | 1779 | _on_issue_err |
166096dc | 1780 | return 1 |
1781 | fi | |
1782 | ||
22ea4004 | 1783 | accountkey_json=$(printf "%s" "$jwk" | tr -d ' ' ) |
1784 | thumbprint=$(printf "%s" "$accountkey_json" | _digest "sha256" | _urlencode) | |
4c3b3608 | 1785 | |
f574e581 | 1786 | regjson='{"resource": "new-reg", "agreement": "'$AGREEMENT'"}' |
1787 | if [ "$ACCOUNT_EMAIL" ] ; then | |
1788 | regjson='{"resource": "new-reg", "contact": ["mailto: '$ACCOUNT_EMAIL'"], "agreement": "'$AGREEMENT'"}' | |
1789 | fi | |
1790 | ||
88fab7d6 | 1791 | accountkeyhash="$(cat "$ACCOUNT_KEY_PATH" | _digest "sha256" )" |
f574e581 | 1792 | accountkeyhash="$(echo $accountkeyhash$API$regjson | _digest "sha256" )" |
8663fb7e | 1793 | if [ "$accountkeyhash" != "$ACCOUNT_KEY_HASH" ] ; then |
f574e581 | 1794 | _info "Registering account" |
1795 | _send_signed_request "$API/acme/new-reg" "$regjson" | |
8663fb7e | 1796 | if [ "$code" = "" ] || [ "$code" = '201' ] ; then |
166096dc | 1797 | _info "Registered" |
f574e581 | 1798 | echo "$response" > $LE_WORKING_DIR/account.json |
8663fb7e | 1799 | elif [ "$code" = '409' ] ; then |
166096dc | 1800 | _info "Already registered" |
1801 | else | |
1802 | _err "Register account Error: $response" | |
1803 | _clearup | |
b0070f03 | 1804 | _on_issue_err |
166096dc | 1805 | return 1 |
1806 | fi | |
1807 | ACCOUNT_KEY_HASH="$accountkeyhash" | |
1808 | _saveaccountconf "ACCOUNT_KEY_HASH" "$ACCOUNT_KEY_HASH" | |
1809 | else | |
1810 | _info "Skip register account key" | |
1811 | fi | |
1812 | ||
58f41a19 | 1813 | if [ "$Le_Keylength" = "no" ] ; then |
1814 | Le_Keylength="" | |
1815 | fi | |
1816 | ||
10afcaca | 1817 | |
1818 | if [ -f "$CSR_PATH" ] && [ ! -f "$CERT_KEY_PATH" ] ; then | |
1819 | _info "Signing from existing CSR." | |
1820 | else | |
1821 | _key=$(_readdomainconf Le_Keylength) | |
1822 | _debug "Read key length:$_key" | |
1823 | if [ ! -f "$CERT_KEY_PATH" ] || [ "$Le_Keylength" != "$_key" ] ; then | |
1824 | if ! createDomainKey $Le_Domain $Le_Keylength ; then | |
1825 | _err "Create domain key error." | |
1826 | _clearup | |
b0070f03 | 1827 | _on_issue_err |
10afcaca | 1828 | return 1 |
1829 | fi | |
1830 | fi | |
1831 | ||
1832 | if ! _createcsr "$Le_Domain" "$Le_Alt" "$CERT_KEY_PATH" "$CSR_PATH" "$DOMAIN_SSL_CONF" ; then | |
1833 | _err "Create CSR error." | |
5ef501c5 | 1834 | _clearup |
b0070f03 | 1835 | _on_issue_err |
41e3eafa | 1836 | return 1 |
1837 | fi | |
4c3b3608 | 1838 | fi |
10afcaca | 1839 | |
61623d22 | 1840 | _savedomainconf "Le_Keylength" "$Le_Keylength" |
58f41a19 | 1841 | |
4c3b3608 | 1842 | vlist="$Le_Vlist" |
1843 | # verify each domain | |
1844 | _info "Verify each domain" | |
1845 | sep='#' | |
8663fb7e | 1846 | if [ -z "$vlist" ] ; then |
4c3b3608 | 1847 | alldomains=$(echo "$Le_Domain,$Le_Alt" | tr ',' ' ' ) |
a63b05a9 | 1848 | _index=1 |
1849 | _currentRoot="" | |
4c3b3608 | 1850 | for d in $alldomains |
a63b05a9 | 1851 | do |
1852 | _info "Getting webroot for domain" $d | |
1853 | _w="$(echo $Le_Webroot | cut -d , -f $_index)" | |
1854 | _debug _w "$_w" | |
8663fb7e | 1855 | if [ "$_w" ] ; then |
a63b05a9 | 1856 | _currentRoot="$_w" |
1857 | fi | |
1858 | _debug "_currentRoot" "$_currentRoot" | |
00a50605 | 1859 | _index=$(_math $_index + 1) |
a63b05a9 | 1860 | |
1861 | vtype="$VTYPE_HTTP" | |
dceb3aca | 1862 | if _startswith "$_currentRoot" "dns" ; then |
a63b05a9 | 1863 | vtype="$VTYPE_DNS" |
1864 | fi | |
e22bcf7c | 1865 | |
1866 | if [ "$_currentRoot" = "$W_TLS" ] ; then | |
1867 | vtype="$VTYPE_TLS" | |
1868 | fi | |
1869 | ||
4c3b3608 | 1870 | _info "Getting token for domain" $d |
c4d8fd83 | 1871 | |
1872 | if ! _send_signed_request "$API/acme/new-authz" "{\"resource\": \"new-authz\", \"identifier\": {\"type\": \"dns\", \"value\": \"$d\"}}" ; then | |
1873 | _err "Can not get domain token." | |
1874 | _clearup | |
b0070f03 | 1875 | _on_issue_err |
c4d8fd83 | 1876 | return 1 |
1877 | fi | |
1878 | ||
8663fb7e | 1879 | if [ ! -z "$code" ] && [ ! "$code" = '201' ] ; then |
4c3b3608 | 1880 | _err "new-authz error: $response" |
1881 | _clearup | |
b0070f03 | 1882 | _on_issue_err |
4c3b3608 | 1883 | return 1 |
1884 | fi | |
1885 | ||
22ea4004 | 1886 | entry="$(printf "%s\n" "$response" | _egrep_o '[^{]*"type":"'$vtype'"[^}]*')" |
4c3b3608 | 1887 | _debug entry "$entry" |
19539575 | 1888 | if [ -z "$entry" ] ; then |
1889 | _err "Error, can not get domain token $d" | |
1890 | _clearup | |
b0070f03 | 1891 | _on_issue_err |
19539575 | 1892 | return 1 |
1893 | fi | |
22ea4004 | 1894 | token="$(printf "%s\n" "$entry" | _egrep_o '"token":"[^"]*' | cut -d : -f 2 | tr -d '"')" |
4c3b3608 | 1895 | _debug token $token |
1896 | ||
22ea4004 | 1897 | uri="$(printf "%s\n" "$entry" | _egrep_o '"uri":"[^"]*'| cut -d : -f 2,3 | tr -d '"' )" |
4c3b3608 | 1898 | _debug uri $uri |
1899 | ||
1900 | keyauthorization="$token.$thumbprint" | |
1901 | _debug keyauthorization "$keyauthorization" | |
1902 | ||
d35bf517 | 1903 | |
1904 | if printf "$response" | grep '"status":"valid"' >/dev/null 2>&1 ; then | |
1905 | _info "$d is already verified, skip." | |
1906 | keyauthorization=$STATE_VERIFIED | |
1907 | _debug keyauthorization "$keyauthorization" | |
ec603bee | 1908 | fi |
1909 | ||
d35bf517 | 1910 | |
a63b05a9 | 1911 | dvlist="$d$sep$keyauthorization$sep$uri$sep$vtype$sep$_currentRoot" |
4c3b3608 | 1912 | _debug dvlist "$dvlist" |
1913 | ||
1914 | vlist="$vlist$dvlist," | |
1915 | ||
1916 | done | |
1917 | ||
1918 | #add entry | |
1919 | dnsadded="" | |
1920 | ventries=$(echo "$vlist" | tr ',' ' ' ) | |
1921 | for ventry in $ventries | |
1922 | do | |
1923 | d=$(echo $ventry | cut -d $sep -f 1) | |
1924 | keyauthorization=$(echo $ventry | cut -d $sep -f 2) | |
a63b05a9 | 1925 | vtype=$(echo $ventry | cut -d $sep -f 4) |
1926 | _currentRoot=$(echo $ventry | cut -d $sep -f 5) | |
ec603bee | 1927 | |
bd5e57d8 | 1928 | if [ "$keyauthorization" = "$STATE_VERIFIED" ] ; then |
ec603bee | 1929 | _info "$d is already verified, skip $vtype." |
1930 | continue | |
1931 | fi | |
1932 | ||
8663fb7e | 1933 | if [ "$vtype" = "$VTYPE_DNS" ] ; then |
4c3b3608 | 1934 | dnsadded='0' |
1935 | txtdomain="_acme-challenge.$d" | |
1936 | _debug txtdomain "$txtdomain" | |
22ea4004 | 1937 | txt="$(printf "%s" "$keyauthorization" | _digest "sha256" | _urlencode)" |
4c3b3608 | 1938 | _debug txt "$txt" |
1939 | #dns | |
1940 | #1. check use api | |
1941 | d_api="" | |
8663fb7e | 1942 | if [ -f "$LE_WORKING_DIR/$d/$_currentRoot" ] ; then |
a63b05a9 | 1943 | d_api="$LE_WORKING_DIR/$d/$_currentRoot" |
8663fb7e | 1944 | elif [ -f "$LE_WORKING_DIR/$d/$_currentRoot.sh" ] ; then |
a63b05a9 | 1945 | d_api="$LE_WORKING_DIR/$d/$_currentRoot.sh" |
8663fb7e | 1946 | elif [ -f "$LE_WORKING_DIR/$_currentRoot" ] ; then |
a63b05a9 | 1947 | d_api="$LE_WORKING_DIR/$_currentRoot" |
8663fb7e | 1948 | elif [ -f "$LE_WORKING_DIR/$_currentRoot.sh" ] ; then |
a63b05a9 | 1949 | d_api="$LE_WORKING_DIR/$_currentRoot.sh" |
8663fb7e | 1950 | elif [ -f "$LE_WORKING_DIR/dnsapi/$_currentRoot" ] ; then |
a63b05a9 | 1951 | d_api="$LE_WORKING_DIR/dnsapi/$_currentRoot" |
8663fb7e | 1952 | elif [ -f "$LE_WORKING_DIR/dnsapi/$_currentRoot.sh" ] ; then |
a63b05a9 | 1953 | d_api="$LE_WORKING_DIR/dnsapi/$_currentRoot.sh" |
4c3b3608 | 1954 | fi |
1955 | _debug d_api "$d_api" | |
1956 | ||
8663fb7e | 1957 | if [ "$d_api" ] ; then |
4c3b3608 | 1958 | _info "Found domain api file: $d_api" |
1959 | else | |
1960 | _err "Add the following TXT record:" | |
cbcd7e0f | 1961 | _err "Domain: '$(__green $txtdomain)'" |
1962 | _err "TXT value: '$(__green $txt)'" | |
4c3b3608 | 1963 | _err "Please be aware that you prepend _acme-challenge. before your domain" |
1964 | _err "so the resulting subdomain will be: $txtdomain" | |
1965 | continue | |
1966 | fi | |
4c3b3608 | 1967 | |
73b8b120 | 1968 | ( |
8663fb7e | 1969 | if ! . $d_api ; then |
73b8b120 | 1970 | _err "Load file $d_api error. Please check your api file and try again." |
b0070f03 | 1971 | _on_issue_err |
73b8b120 | 1972 | return 1 |
1973 | fi | |
1974 | ||
158f22f7 | 1975 | addcommand="${_currentRoot}_add" |
d53289d7 | 1976 | if ! _exists $addcommand ; then |
73b8b120 | 1977 | _err "It seems that your api file is not correct, it must have a function named: $addcommand" |
b0070f03 | 1978 | _on_issue_err |
73b8b120 | 1979 | return 1 |
1980 | fi | |
1981 | ||
1982 | if ! $addcommand $txtdomain $txt ; then | |
1983 | _err "Error add txt for domain:$txtdomain" | |
b0070f03 | 1984 | _on_issue_err |
73b8b120 | 1985 | return 1 |
1986 | fi | |
1987 | ) | |
4c3b3608 | 1988 | |
8663fb7e | 1989 | if [ "$?" != "0" ] ; then |
5ef501c5 | 1990 | _clearup |
b0070f03 | 1991 | _on_issue_err |
4c3b3608 | 1992 | return 1 |
1993 | fi | |
1994 | dnsadded='1' | |
1995 | fi | |
1996 | done | |
1997 | ||
8663fb7e | 1998 | if [ "$dnsadded" = '0' ] ; then |
4d2f38b0 | 1999 | _savedomainconf "Le_Vlist" "$vlist" |
4c3b3608 | 2000 | _debug "Dns record not added yet, so, save to $DOMAIN_CONF and exit." |
2001 | _err "Please add the TXT records to the domains, and retry again." | |
5ef501c5 | 2002 | _clearup |
b0070f03 | 2003 | _on_issue_err |
4c3b3608 | 2004 | return 1 |
2005 | fi | |
2006 | ||
2007 | fi | |
2008 | ||
8663fb7e | 2009 | if [ "$dnsadded" = '1' ] ; then |
0e38c60d | 2010 | if [ -z "$Le_DNSSleep" ] ; then |
4a4dacb5 | 2011 | Le_DNSSleep=$DEFAULT_DNS_SLEEP |
0e38c60d | 2012 | else |
2013 | _savedomainconf "Le_DNSSleep" "$Le_DNSSleep" | |
2014 | fi | |
2015 | ||
5fbc47eb | 2016 | _info "Sleep $(__green $Le_DNSSleep) seconds for the txt records to take effect" |
0e38c60d | 2017 | sleep $Le_DNSSleep |
4c3b3608 | 2018 | fi |
2019 | ||
2020 | _debug "ok, let's start to verify" | |
a63b05a9 | 2021 | |
4c3b3608 | 2022 | ventries=$(echo "$vlist" | tr ',' ' ' ) |
2023 | for ventry in $ventries | |
2024 | do | |
2025 | d=$(echo $ventry | cut -d $sep -f 1) | |
2026 | keyauthorization=$(echo $ventry | cut -d $sep -f 2) | |
2027 | uri=$(echo $ventry | cut -d $sep -f 3) | |
a63b05a9 | 2028 | vtype=$(echo $ventry | cut -d $sep -f 4) |
2029 | _currentRoot=$(echo $ventry | cut -d $sep -f 5) | |
ec603bee | 2030 | |
bd5e57d8 | 2031 | if [ "$keyauthorization" = "$STATE_VERIFIED" ] ; then |
ec603bee | 2032 | _info "$d is already verified, skip $vtype." |
2033 | continue | |
2034 | fi | |
2035 | ||
4c3b3608 | 2036 | _info "Verifying:$d" |
2037 | _debug "d" "$d" | |
2038 | _debug "keyauthorization" "$keyauthorization" | |
2039 | _debug "uri" "$uri" | |
2040 | removelevel="" | |
e22bcf7c | 2041 | token="$(printf "%s" "$keyauthorization" | cut -d '.' -f 1)" |
a63b05a9 | 2042 | |
2043 | _debug "_currentRoot" "$_currentRoot" | |
2044 | ||
2045 | ||
8663fb7e | 2046 | if [ "$vtype" = "$VTYPE_HTTP" ] ; then |
2047 | if [ "$_currentRoot" = "no" ] ; then | |
4c3b3608 | 2048 | _info "Standalone mode server" |
2049 | _startserver "$keyauthorization" & | |
8663fb7e | 2050 | if [ "$?" != "0" ] ; then |
5ef501c5 | 2051 | _clearup |
b0070f03 | 2052 | _on_issue_err |
6fc1447f | 2053 | return 1 |
2054 | fi | |
4c3b3608 | 2055 | serverproc="$!" |
2056 | sleep 2 | |
2057 | _debug serverproc $serverproc | |
6fc1447f | 2058 | |
4c3b3608 | 2059 | else |
8663fb7e | 2060 | if [ "$_currentRoot" = "apache" ] ; then |
6f930641 | 2061 | wellknown_path="$ACME_DIR" |
2062 | else | |
a63b05a9 | 2063 | wellknown_path="$_currentRoot/.well-known/acme-challenge" |
8663fb7e | 2064 | if [ ! -d "$_currentRoot/.well-known" ] ; then |
6f930641 | 2065 | removelevel='1' |
8663fb7e | 2066 | elif [ ! -d "$_currentRoot/.well-known/acme-challenge" ] ; then |
6f930641 | 2067 | removelevel='2' |
2068 | else | |
2069 | removelevel='3' | |
2070 | fi | |
4c3b3608 | 2071 | fi |
6f930641 | 2072 | |
4c3b3608 | 2073 | _debug wellknown_path "$wellknown_path" |
6f930641 | 2074 | |
4c3b3608 | 2075 | _debug "writing token:$token to $wellknown_path/$token" |
2076 | ||
2077 | mkdir -p "$wellknown_path" | |
7939b419 | 2078 | printf "%s" "$keyauthorization" > "$wellknown_path/$token" |
8663fb7e | 2079 | if [ ! "$usingApache" ] ; then |
32fdc196 TB |
2080 | if webroot_owner=$(_stat $_currentRoot) ; then |
2081 | _debug "Changing owner/group of .well-known to $webroot_owner" | |
2082 | chown -R $webroot_owner "$_currentRoot/.well-known" | |
2083 | else | |
2084 | _debug "not chaning owner/group of webroot"; | |
2085 | fi | |
df886ffa | 2086 | fi |
4c3b3608 | 2087 | |
2088 | fi | |
e22bcf7c | 2089 | |
2090 | elif [ "$vtype" = "$VTYPE_TLS" ] ; then | |
2091 | #create A | |
2092 | #_hash_A="$(printf "%s" $token | _digest "sha256" "hex" )" | |
2093 | #_debug2 _hash_A "$_hash_A" | |
2094 | #_x="$(echo $_hash_A | cut -c 1-32)" | |
2095 | #_debug2 _x "$_x" | |
2096 | #_y="$(echo $_hash_A | cut -c 33-64)" | |
2097 | #_debug2 _y "$_y" | |
2098 | #_SAN_A="$_x.$_y.token.acme.invalid" | |
2099 | #_debug2 _SAN_A "$_SAN_A" | |
2100 | ||
2101 | #create B | |
2102 | _hash_B="$(printf "%s" $keyauthorization | _digest "sha256" "hex" )" | |
2103 | _debug2 _hash_B "$_hash_B" | |
2104 | _x="$(echo $_hash_B | cut -c 1-32)" | |
2105 | _debug2 _x "$_x" | |
2106 | _y="$(echo $_hash_B | cut -c 33-64)" | |
2107 | _debug2 _y "$_y" | |
2108 | ||
2109 | #_SAN_B="$_x.$_y.ka.acme.invalid" | |
2110 | ||
2111 | _SAN_B="$_x.$_y.acme.invalid" | |
2112 | _debug2 _SAN_B "$_SAN_B" | |
2113 | ||
2114 | if ! _starttlsserver "$_SAN_B" "$_SAN_A" "$Le_TLSPort" "$keyauthorization" ; then | |
2115 | _err "Start tls server error." | |
2116 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" | |
2117 | _clearup | |
b0070f03 | 2118 | _on_issue_err |
e22bcf7c | 2119 | return 1 |
2120 | fi | |
4c3b3608 | 2121 | fi |
2122 | ||
c4d8fd83 | 2123 | if ! _send_signed_request $uri "{\"resource\": \"challenge\", \"keyAuthorization\": \"$keyauthorization\"}" ; then |
2124 | _err "$d:Can not get challenge: $response" | |
2125 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" | |
2126 | _clearup | |
b0070f03 | 2127 | _on_issue_err |
c4d8fd83 | 2128 | return 1 |
2129 | fi | |
4c3b3608 | 2130 | |
8663fb7e | 2131 | if [ ! -z "$code" ] && [ ! "$code" = '202' ] ; then |
c60883ef | 2132 | _err "$d:Challenge error: $response" |
a63b05a9 | 2133 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" |
4c3b3608 | 2134 | _clearup |
b0070f03 | 2135 | _on_issue_err |
4c3b3608 | 2136 | return 1 |
2137 | fi | |
2138 | ||
6fc1447f | 2139 | waittimes=0 |
8663fb7e | 2140 | if [ -z "$MAX_RETRY_TIMES" ] ; then |
6fc1447f | 2141 | MAX_RETRY_TIMES=30 |
2142 | fi | |
2143 | ||
2ee5d873 | 2144 | while true ; do |
00a50605 | 2145 | waittimes=$(_math $waittimes + 1) |
8663fb7e | 2146 | if [ "$waittimes" -ge "$MAX_RETRY_TIMES" ] ; then |
6fc1447f | 2147 | _err "$d:Timeout" |
2148 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" | |
2149 | _clearup | |
b0070f03 | 2150 | _on_issue_err |
6fc1447f | 2151 | return 1 |
2152 | fi | |
2153 | ||
4c3b3608 | 2154 | _debug "sleep 5 secs to verify" |
2155 | sleep 5 | |
2156 | _debug "checking" | |
9aaf36cd | 2157 | response="$(_get $uri)" |
8663fb7e | 2158 | if [ "$?" != "0" ] ; then |
c60883ef | 2159 | _err "$d:Verify error:$response" |
a63b05a9 | 2160 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" |
4c3b3608 | 2161 | _clearup |
b0070f03 | 2162 | _on_issue_err |
4c3b3608 | 2163 | return 1 |
2164 | fi | |
9aaf36cd | 2165 | _debug2 original "$response" |
2166 | ||
2167 | response="$(echo "$response" | _normalizeJson )" | |
7012b91f | 2168 | _debug2 response "$response" |
4c3b3608 | 2169 | |
22ea4004 | 2170 | status=$(echo "$response" | _egrep_o '"status":"[^"]*' | cut -d : -f 2 | tr -d '"') |
8663fb7e | 2171 | if [ "$status" = "valid" ] ; then |
4c3b3608 | 2172 | _info "Success" |
2173 | _stopserver $serverproc | |
2174 | serverproc="" | |
a63b05a9 | 2175 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" |
4c3b3608 | 2176 | break; |
2177 | fi | |
2178 | ||
8663fb7e | 2179 | if [ "$status" = "invalid" ] ; then |
22ea4004 | 2180 | error="$(echo "$response" | _egrep_o '"error":\{[^}]*}')" |
b7ec6789 | 2181 | _debug2 error "$error" |
22ea4004 | 2182 | errordetail="$(echo $error | _egrep_o '"detail": *"[^"]*"' | cut -d '"' -f 4)" |
b7ec6789 | 2183 | _debug2 errordetail "$errordetail" |
2184 | if [ "$errordetail" ] ; then | |
2185 | _err "$d:Verify error:$errordetail" | |
2186 | else | |
2187 | _err "$d:Verify error:$error" | |
2188 | fi | |
dcf9cb58 | 2189 | if [ "$DEBUG" ] ; then |
2190 | if [ "$vtype" = "$VTYPE_HTTP" ] ; then | |
2191 | _get "http://$d/.well-known/acme-challenge/$token" | |
2192 | fi | |
2193 | fi | |
a63b05a9 | 2194 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" |
4c3b3608 | 2195 | _clearup |
b0070f03 | 2196 | _on_issue_err |
4c3b3608 | 2197 | return 1; |
2198 | fi | |
2199 | ||
8663fb7e | 2200 | if [ "$status" = "pending" ] ; then |
4c3b3608 | 2201 | _info "Pending" |
2202 | else | |
2203 | _err "$d:Verify error:$response" | |
a63b05a9 | 2204 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" |
4c3b3608 | 2205 | _clearup |
b0070f03 | 2206 | _on_issue_err |
4c3b3608 | 2207 | return 1 |
2208 | fi | |
2209 | ||
2210 | done | |
2211 | ||
2212 | done | |
2213 | ||
2214 | _clearup | |
2215 | _info "Verify finished, start to sign." | |
fa8311dc | 2216 | der="$(_getfile "${CSR_PATH}" "${BEGIN_CSR}" "${END_CSR}" | tr -d "\r\n" | _urlencode)" |
c4d8fd83 | 2217 | |
2218 | if ! _send_signed_request "$API/acme/new-cert" "{\"resource\": \"new-cert\", \"csr\": \"$der\"}" "needbase64" ; then | |
2219 | _err "Sign failed." | |
b0070f03 | 2220 | _on_issue_err |
c4d8fd83 | 2221 | return 1 |
2222 | fi | |
4c3b3608 | 2223 | |
2224 | ||
22ea4004 | 2225 | Le_LinkCert="$(grep -i '^Location.*$' $HTTP_HEADER | head -1 | tr -d "\r\n" | cut -d " " -f 2)" |
4d2f38b0 | 2226 | _savedomainconf "Le_LinkCert" "$Le_LinkCert" |
4c3b3608 | 2227 | |
8663fb7e | 2228 | if [ "$Le_LinkCert" ] ; then |
88fab7d6 | 2229 | echo "$BEGIN_CERT" > "$CERT_PATH" |
c60883ef | 2230 | _get "$Le_LinkCert" | _base64 "multiline" >> "$CERT_PATH" |
88fab7d6 | 2231 | echo "$END_CERT" >> "$CERT_PATH" |
43822d37 | 2232 | _info "$(__green "Cert success.")" |
4c3b3608 | 2233 | cat "$CERT_PATH" |
2234 | ||
66f08eb2 | 2235 | _info "Your cert is in $( __green " $CERT_PATH ")" |
5980ebc7 | 2236 | |
2237 | if [ -f "$CERT_KEY_PATH" ] ; then | |
2238 | _info "Your cert key is in $( __green " $CERT_KEY_PATH ")" | |
2239 | fi | |
2240 | ||
caf1fc10 | 2241 | cp "$CERT_PATH" "$CERT_FULLCHAIN_PATH" |
281aa349 | 2242 | |
8663fb7e | 2243 | if [ ! "$USER_PATH" ] || [ ! "$IN_CRON" ] ; then |
281aa349 | 2244 | USER_PATH="$PATH" |
2245 | _saveaccountconf "USER_PATH" "$USER_PATH" | |
2246 | fi | |
4c3b3608 | 2247 | fi |
2248 | ||
2249 | ||
8663fb7e | 2250 | if [ -z "$Le_LinkCert" ] ; then |
eae29099 | 2251 | response="$(echo $response | _dbase64 "multiline" | _normalizeJson )" |
22ea4004 | 2252 | _err "Sign failed: $(echo "$response" | _egrep_o '"detail":"[^"]*"')" |
b0070f03 | 2253 | _on_issue_err |
4c3b3608 | 2254 | return 1 |
2255 | fi | |
2256 | ||
4d2f38b0 | 2257 | _cleardomainconf "Le_Vlist" |
4c3b3608 | 2258 | |
bbbdcb09 | 2259 | Le_LinkIssuer=$(grep -i '^Link' $HTTP_HEADER | head -1 | cut -d " " -f 2| cut -d ';' -f 1 | tr -d '<>' ) |
fac1e367 | 2260 | if ! _contains "$Le_LinkIssuer" ":" ; then |
2261 | Le_LinkIssuer="$API$Le_LinkIssuer" | |
2262 | fi | |
2263 | ||
4d2f38b0 | 2264 | _savedomainconf "Le_LinkIssuer" "$Le_LinkIssuer" |
4c3b3608 | 2265 | |
8663fb7e | 2266 | if [ "$Le_LinkIssuer" ] ; then |
88fab7d6 | 2267 | echo "$BEGIN_CERT" > "$CA_CERT_PATH" |
c60883ef | 2268 | _get "$Le_LinkIssuer" | _base64 "multiline" >> "$CA_CERT_PATH" |
88fab7d6 | 2269 | echo "$END_CERT" >> "$CA_CERT_PATH" |
66f08eb2 | 2270 | _info "The intermediate CA cert is in $( __green " $CA_CERT_PATH ")" |
caf1fc10 | 2271 | cat "$CA_CERT_PATH" >> "$CERT_FULLCHAIN_PATH" |
66f08eb2 | 2272 | _info "And the full chain certs is there: $( __green " $CERT_FULLCHAIN_PATH ")" |
4c3b3608 | 2273 | fi |
2274 | ||
3aae1ae3 | 2275 | Le_CertCreateTime=$(_time) |
4d2f38b0 | 2276 | _savedomainconf "Le_CertCreateTime" "$Le_CertCreateTime" |
4c3b3608 | 2277 | |
2278 | Le_CertCreateTimeStr=$(date -u ) | |
4d2f38b0 | 2279 | _savedomainconf "Le_CertCreateTimeStr" "$Le_CertCreateTimeStr" |
4c3b3608 | 2280 | |
523c7682 | 2281 | if [ -z "$Le_RenewalDays" ] || [ "$Le_RenewalDays" -lt "0" ] || [ "$Le_RenewalDays" -gt "$MAX_RENEW" ] ; then |
2282 | Le_RenewalDays=$MAX_RENEW | |
054cb72e | 2283 | else |
2284 | _savedomainconf "Le_RenewalDays" "$Le_RenewalDays" | |
13d7cae9 | 2285 | fi |
2286 | ||
78009539 PS |
2287 | if [ "$CA_BUNDLE" ] ; then |
2288 | _saveaccountconf CA_BUNDLE "$CA_BUNDLE" | |
2289 | else | |
2290 | _clearaccountconf "CA_BUNDLE" | |
2291 | fi | |
2292 | ||
fac1e367 | 2293 | if [ "$HTTPS_INSECURE" ] ; then |
2294 | _saveaccountconf HTTPS_INSECURE "$HTTPS_INSECURE" | |
2295 | else | |
2296 | _clearaccountconf "HTTPS_INSECURE" | |
13d7cae9 | 2297 | fi |
00a50605 | 2298 | |
2299 | Le_NextRenewTime=$(_math $Le_CertCreateTime + $Le_RenewalDays \* 24 \* 60 \* 60) | |
4d2f38b0 | 2300 | _savedomainconf "Le_NextRenewTime" "$Le_NextRenewTime" |
4c3b3608 | 2301 | |
2302 | Le_NextRenewTimeStr=$( _time2str $Le_NextRenewTime ) | |
4d2f38b0 | 2303 | _savedomainconf "Le_NextRenewTimeStr" "$Le_NextRenewTimeStr" |
4c3b3608 | 2304 | |
b0070f03 | 2305 | _on_issue_success |
4c3b3608 | 2306 | |
4c0d3f1b | 2307 | if [ "$Le_RealCertPath$Le_RealKeyPath$Le_RealCACertPath$Le_ReloadCmd$Le_RealFullChainPath" ] ; then |
43822d37 | 2308 | _installcert |
01f54558 | 2309 | fi |
4c0d3f1b | 2310 | |
4c3b3608 | 2311 | } |
2312 | ||
43822d37 | 2313 | #domain [isEcc] |
4c3b3608 | 2314 | renew() { |
2315 | Le_Domain="$1" | |
8663fb7e | 2316 | if [ -z "$Le_Domain" ] ; then |
43822d37 | 2317 | _usage "Usage: $PROJECT_ENTRY --renew -d domain.com [--ecc]" |
4c3b3608 | 2318 | return 1 |
2319 | fi | |
2320 | ||
43822d37 | 2321 | _isEcc="$2" |
2322 | ||
2323 | _initpath $Le_Domain "$_isEcc" | |
2324 | ||
e2053b22 | 2325 | _info "$(__green "Renew: '$Le_Domain'")" |
8663fb7e | 2326 | if [ ! -f "$DOMAIN_CONF" ] ; then |
43822d37 | 2327 | _info "'$Le_Domain' is not a issued domain, skip." |
4c3b3608 | 2328 | return 0; |
2329 | fi | |
2330 | ||
1e6b68f5 | 2331 | if [ "$Le_RenewalDays" ] ; then |
2332 | _savedomainconf Le_RenewalDays "$Le_RenewalDays" | |
2333 | fi | |
2334 | ||
8663fb7e | 2335 | . "$DOMAIN_CONF" |
3aae1ae3 | 2336 | if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(_time)" -lt "$Le_NextRenewTime" ] ; then |
e2053b22 | 2337 | _info "Skip, Next renewal time is: $(__green "$Le_NextRenewTimeStr")" |
2338 | _info "Add '$(__red '--force')' to force to renew." | |
cc179731 | 2339 | return $RENEW_SKIP |
4c3b3608 | 2340 | fi |
2341 | ||
2342 | IS_RENEW="1" | |
b0070f03 | 2343 | 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" |
22ea4004 | 2344 | res=$? |
4c3b3608 | 2345 | IS_RENEW="" |
2346 | ||
2347 | return $res | |
2348 | } | |
2349 | ||
cc179731 | 2350 | #renewAll [stopRenewOnError] |
4c3b3608 | 2351 | renewAll() { |
2352 | _initpath | |
cc179731 | 2353 | _stopRenewOnError="$1" |
2354 | _debug "_stopRenewOnError" "$_stopRenewOnError" | |
2355 | _ret="0" | |
43822d37 | 2356 | |
b2817897 | 2357 | for d in $(ls -F ${CERT_HOME}/ | grep [^.].*[.].*/$ ) ; do |
4c3b3608 | 2358 | d=$(echo $d | cut -d '/' -f 1) |
43822d37 | 2359 | ( |
2360 | if _endswith $d "$ECC_SUFFIX" ; then | |
2361 | _isEcc=$(echo $d | cut -d "$ECC_SEP" -f 2) | |
2362 | d=$(echo $d | cut -d "$ECC_SEP" -f 1) | |
2363 | fi | |
2364 | renew "$d" "$_isEcc" | |
4d2f38b0 | 2365 | ) |
cc179731 | 2366 | rc="$?" |
2367 | _debug "Return code: $rc" | |
2368 | if [ "$rc" != "0" ] ; then | |
2369 | if [ "$rc" = "$RENEW_SKIP" ] ; then | |
2370 | _info "Skipped $d" | |
2371 | elif [ "$_stopRenewOnError" ] ; then | |
2372 | _err "Error renew $d, stop now." | |
2373 | return $rc | |
2374 | else | |
2375 | _ret="$rc" | |
2376 | _err "Error renew $d, Go ahead to next one." | |
2377 | fi | |
2378 | fi | |
4c3b3608 | 2379 | done |
cc179731 | 2380 | return $_ret |
4c3b3608 | 2381 | } |
2382 | ||
dcf4f8f6 | 2383 | |
10afcaca | 2384 | #csr webroot |
2385 | signcsr(){ | |
2386 | _csrfile="$1" | |
2387 | _csrW="$2" | |
2388 | if [ -z "$_csrfile" ] || [ -z "$_csrW" ]; then | |
2389 | _usage "Usage: $PROJECT_ENTRY --signcsr --csr mycsr.csr -w /path/to/webroot/a.com/ " | |
2390 | return 1 | |
2391 | fi | |
2392 | ||
2393 | _initpath | |
2394 | ||
2395 | _csrsubj=$(_readSubjectFromCSR "$_csrfile") | |
2396 | if [ "$?" != "0" ] || [ -z "$_csrsubj" ] ; then | |
2397 | _err "Can not read subject from csr: $_csrfile" | |
2398 | return 1 | |
2399 | fi | |
2400 | ||
2401 | _csrdomainlist=$(_readSubjectAltNamesFromCSR "$_csrfile") | |
2402 | if [ "$?" != "0" ] ; then | |
2403 | _err "Can not read domain list from csr: $_csrfile" | |
2404 | return 1 | |
2405 | fi | |
2406 | _debug "_csrdomainlist" "$_csrdomainlist" | |
2407 | ||
2408 | _csrkeylength=$(_readKeyLengthFromCSR "$_csrfile") | |
2409 | if [ "$?" != "0" ] || [ -z "$_csrkeylength" ] ; then | |
2410 | _err "Can not read key length from csr: $_csrfile" | |
2411 | return 1 | |
2412 | fi | |
2413 | ||
2414 | _initpath "$_csrsubj" "$_csrkeylength" | |
2415 | mkdir -p "$DOMAIN_PATH" | |
2416 | ||
2417 | _info "Copy csr to: $CSR_PATH" | |
2418 | cp "$_csrfile" "$CSR_PATH" | |
2419 | ||
2420 | issue "$_csrW" "$_csrsubj" "$_csrdomainlist" "$_csrkeylength" | |
2421 | ||
2422 | } | |
2423 | ||
2424 | showcsr() { | |
2425 | _csrfile="$1" | |
2426 | _csrd="$2" | |
2427 | if [ -z "$_csrfile" ] && [ -z "$_csrd" ]; then | |
2428 | _usage "Usage: $PROJECT_ENTRY --showcsr --csr mycsr.csr" | |
2429 | return 1 | |
2430 | fi | |
2431 | ||
2432 | _initpath | |
2433 | ||
2434 | _csrsubj=$(_readSubjectFromCSR "$_csrfile") | |
2435 | if [ "$?" != "0" ] || [ -z "$_csrsubj" ] ; then | |
2436 | _err "Can not read subject from csr: $_csrfile" | |
2437 | return 1 | |
2438 | fi | |
2439 | ||
2440 | _info "Subject=$_csrsubj" | |
2441 | ||
2442 | _csrdomainlist=$(_readSubjectAltNamesFromCSR "$_csrfile") | |
2443 | if [ "$?" != "0" ] ; then | |
2444 | _err "Can not read domain list from csr: $_csrfile" | |
2445 | return 1 | |
2446 | fi | |
2447 | _debug "_csrdomainlist" "$_csrdomainlist" | |
2448 | ||
2449 | _info "SubjectAltNames=$_csrdomainlist" | |
2450 | ||
2451 | ||
2452 | _csrkeylength=$(_readKeyLengthFromCSR "$_csrfile") | |
2453 | if [ "$?" != "0" ] || [ -z "$_csrkeylength" ] ; then | |
2454 | _err "Can not read key length from csr: $_csrfile" | |
2455 | return 1 | |
2456 | fi | |
2457 | _info "KeyLength=$_csrkeylength" | |
2458 | } | |
2459 | ||
6d7eda3e | 2460 | list() { |
22ea4004 | 2461 | _raw="$1" |
6d7eda3e | 2462 | _initpath |
dcf4f8f6 | 2463 | |
2464 | _sep="|" | |
2465 | if [ "$_raw" ] ; then | |
43822d37 | 2466 | printf "Main_Domain${_sep}KeyLength${_sep}SAN_Domains${_sep}Created${_sep}Renew\n" |
dcf4f8f6 | 2467 | for d in $(ls -F ${CERT_HOME}/ | grep [^.].*[.].*/$ ) ; do |
2468 | d=$(echo $d | cut -d '/' -f 1) | |
2469 | ( | |
43822d37 | 2470 | if _endswith $d "$ECC_SUFFIX" ; then |
2471 | _isEcc=$(echo $d | cut -d "$ECC_SEP" -f 2) | |
2472 | d=$(echo $d | cut -d "$ECC_SEP" -f 1) | |
2473 | fi | |
2474 | _initpath $d "$_isEcc" | |
dcf4f8f6 | 2475 | if [ -f "$DOMAIN_CONF" ] ; then |
2476 | . "$DOMAIN_CONF" | |
43822d37 | 2477 | printf "$Le_Domain${_sep}\"$Le_Keylength\"${_sep}$Le_Alt${_sep}$Le_CertCreateTimeStr${_sep}$Le_NextRenewTimeStr\n" |
dcf4f8f6 | 2478 | fi |
2479 | ) | |
2480 | done | |
2481 | else | |
22ea4004 | 2482 | if _exists column ; then |
2483 | list "raw" | column -t -s "$_sep" | |
2484 | else | |
43822d37 | 2485 | list "raw" | tr "$_sep" '\t' |
22ea4004 | 2486 | fi |
dcf4f8f6 | 2487 | fi |
6d7eda3e | 2488 | |
2489 | ||
2490 | } | |
2491 | ||
4c3b3608 | 2492 | installcert() { |
2493 | Le_Domain="$1" | |
8663fb7e | 2494 | if [ -z "$Le_Domain" ] ; then |
43822d37 | 2495 | _usage "Usage: $PROJECT_ENTRY --installcert -d domain.com [--ecc] [--certpath cert-file-path] [--keypath key-file-path] [--capath ca-cert-file-path] [ --reloadCmd reloadCmd] [--fullchainpath fullchain-path]" |
4c3b3608 | 2496 | return 1 |
2497 | fi | |
2498 | ||
2499 | Le_RealCertPath="$2" | |
2500 | Le_RealKeyPath="$3" | |
2501 | Le_RealCACertPath="$4" | |
2502 | Le_ReloadCmd="$5" | |
a63b05a9 | 2503 | Le_RealFullChainPath="$6" |
43822d37 | 2504 | _isEcc="$7" |
2505 | ||
2506 | _initpath $Le_Domain "$_isEcc" | |
2507 | if [ ! -d "$DOMAIN_PATH" ] ; then | |
2508 | _err "Domain is not valid:'$Le_Domain'" | |
2509 | return 1 | |
2510 | fi | |
2511 | ||
2512 | _installcert | |
2513 | } | |
4c3b3608 | 2514 | |
43822d37 | 2515 | |
2516 | _installcert() { | |
4c3b3608 | 2517 | |
4d2f38b0 | 2518 | _savedomainconf "Le_RealCertPath" "$Le_RealCertPath" |
2519 | _savedomainconf "Le_RealCACertPath" "$Le_RealCACertPath" | |
2520 | _savedomainconf "Le_RealKeyPath" "$Le_RealKeyPath" | |
2521 | _savedomainconf "Le_ReloadCmd" "$Le_ReloadCmd" | |
2522 | _savedomainconf "Le_RealFullChainPath" "$Le_RealFullChainPath" | |
4c3b3608 | 2523 | |
4d2f38b0 | 2524 | if [ "$Le_RealCertPath" = "no" ] ; then |
2525 | Le_RealCertPath="" | |
2526 | fi | |
2527 | if [ "$Le_RealKeyPath" = "no" ] ; then | |
2528 | Le_RealKeyPath="" | |
2529 | fi | |
2530 | if [ "$Le_RealCACertPath" = "no" ] ; then | |
2531 | Le_RealCACertPath="" | |
2532 | fi | |
2533 | if [ "$Le_ReloadCmd" = "no" ] ; then | |
2534 | Le_ReloadCmd="" | |
2535 | fi | |
2536 | if [ "$Le_RealFullChainPath" = "no" ] ; then | |
2537 | Le_RealFullChainPath="" | |
2538 | fi | |
2539 | ||
2540 | _installed="0" | |
8663fb7e | 2541 | if [ "$Le_RealCertPath" ] ; then |
4d2f38b0 | 2542 | _installed=1 |
2543 | _info "Installing cert to:$Le_RealCertPath" | |
43822d37 | 2544 | if [ -f "$Le_RealCertPath" ] && [ ! "$IS_RENEW" ] ; then |
ff3bce32 | 2545 | cp "$Le_RealCertPath" "$Le_RealCertPath".bak |
4c3b3608 | 2546 | fi |
2547 | cat "$CERT_PATH" > "$Le_RealCertPath" | |
2548 | fi | |
2549 | ||
8663fb7e | 2550 | if [ "$Le_RealCACertPath" ] ; then |
4d2f38b0 | 2551 | _installed=1 |
2552 | _info "Installing CA to:$Le_RealCACertPath" | |
8663fb7e | 2553 | if [ "$Le_RealCACertPath" = "$Le_RealCertPath" ] ; then |
4c3b3608 | 2554 | echo "" >> "$Le_RealCACertPath" |
2555 | cat "$CA_CERT_PATH" >> "$Le_RealCACertPath" | |
2556 | else | |
43822d37 | 2557 | if [ -f "$Le_RealCACertPath" ] && [ ! "$IS_RENEW" ] ; then |
ff3bce32 | 2558 | cp "$Le_RealCACertPath" "$Le_RealCACertPath".bak |
78552b18 | 2559 | fi |
4c3b3608 | 2560 | cat "$CA_CERT_PATH" > "$Le_RealCACertPath" |
2561 | fi | |
2562 | fi | |
2563 | ||
2564 | ||
8663fb7e | 2565 | if [ "$Le_RealKeyPath" ] ; then |
4d2f38b0 | 2566 | _installed=1 |
2567 | _info "Installing key to:$Le_RealKeyPath" | |
43822d37 | 2568 | if [ -f "$Le_RealKeyPath" ] && [ ! "$IS_RENEW" ] ; then |
ff3bce32 | 2569 | cp "$Le_RealKeyPath" "$Le_RealKeyPath".bak |
4c3b3608 | 2570 | fi |
2571 | cat "$CERT_KEY_PATH" > "$Le_RealKeyPath" | |
2572 | fi | |
a63b05a9 | 2573 | |
8663fb7e | 2574 | if [ "$Le_RealFullChainPath" ] ; then |
4d2f38b0 | 2575 | _installed=1 |
2576 | _info "Installing full chain to:$Le_RealFullChainPath" | |
43822d37 | 2577 | if [ -f "$Le_RealFullChainPath" ] && [ ! "$IS_RENEW" ] ; then |
ff3bce32 | 2578 | cp "$Le_RealFullChainPath" "$Le_RealFullChainPath".bak |
a63b05a9 | 2579 | fi |
2580 | cat "$CERT_FULLCHAIN_PATH" > "$Le_RealFullChainPath" | |
2581 | fi | |
4c3b3608 | 2582 | |
8663fb7e | 2583 | if [ "$Le_ReloadCmd" ] ; then |
4d2f38b0 | 2584 | _installed=1 |
4c3b3608 | 2585 | _info "Run Le_ReloadCmd: $Le_ReloadCmd" |
4d2f38b0 | 2586 | if (cd "$DOMAIN_PATH" && eval "$Le_ReloadCmd") ; then |
43822d37 | 2587 | _info "$(__green "Reload success")" |
4d2f38b0 | 2588 | else |
2589 | _err "Reload error for :$Le_Domain" | |
2590 | fi | |
2591 | fi | |
2592 | ||
4c3b3608 | 2593 | |
2594 | } | |
2595 | ||
2596 | installcronjob() { | |
2597 | _initpath | |
77546ea5 | 2598 | if ! _exists "crontab" ; then |
2599 | _err "crontab doesn't exist, so, we can not install cron jobs." | |
2600 | _err "All your certs will not be renewed automatically." | |
a7b7355d | 2601 | _err "You must add your own cron job to call '$PROJECT_ENTRY --cron' everyday." |
77546ea5 | 2602 | return 1 |
2603 | fi | |
2604 | ||
4c3b3608 | 2605 | _info "Installing cron job" |
a7b7355d | 2606 | if ! crontab -l | grep "$PROJECT_ENTRY --cron" ; then |
8663fb7e | 2607 | if [ -f "$LE_WORKING_DIR/$PROJECT_ENTRY" ] ; then |
a7b7355d | 2608 | lesh="\"$LE_WORKING_DIR\"/$PROJECT_ENTRY" |
4c3b3608 | 2609 | else |
a7b7355d | 2610 | _err "Can not install cronjob, $PROJECT_ENTRY not found." |
4c3b3608 | 2611 | return 1 |
2612 | fi | |
22ea4004 | 2613 | if _exists uname && uname -a | grep solaris >/dev/null ; then |
2614 | crontab -l | { cat; echo "0 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" > /dev/null"; } | crontab -- | |
2615 | else | |
2616 | crontab -l | { cat; echo "0 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" > /dev/null"; } | crontab - | |
2617 | fi | |
4c3b3608 | 2618 | fi |
8663fb7e | 2619 | if [ "$?" != "0" ] ; then |
4c3b3608 | 2620 | _err "Install cron job failed. You need to manually renew your certs." |
2621 | _err "Or you can add cronjob by yourself:" | |
a7b7355d | 2622 | _err "$lesh --cron --home \"$LE_WORKING_DIR\" > /dev/null" |
4c3b3608 | 2623 | return 1 |
2624 | fi | |
2625 | } | |
2626 | ||
2627 | uninstallcronjob() { | |
37db5b81 | 2628 | if ! _exists "crontab" ; then |
2629 | return | |
2630 | fi | |
4c3b3608 | 2631 | _info "Removing cron job" |
a7b7355d | 2632 | cr="$(crontab -l | grep "$PROJECT_ENTRY --cron")" |
8663fb7e | 2633 | if [ "$cr" ] ; then |
22ea4004 | 2634 | if _exists uname && uname -a | grep solaris >/dev/null ; then |
2635 | crontab -l | sed "/$PROJECT_ENTRY --cron/d" | crontab -- | |
2636 | else | |
2637 | crontab -l | sed "/$PROJECT_ENTRY --cron/d" | crontab - | |
2638 | fi | |
a7b7355d | 2639 | LE_WORKING_DIR="$(echo "$cr" | cut -d ' ' -f 9 | tr -d '"')" |
4c3b3608 | 2640 | _info LE_WORKING_DIR "$LE_WORKING_DIR" |
2641 | fi | |
2642 | _initpath | |
a7b7355d | 2643 | |
4c3b3608 | 2644 | } |
2645 | ||
6cb415f5 | 2646 | revoke() { |
2647 | Le_Domain="$1" | |
8663fb7e | 2648 | if [ -z "$Le_Domain" ] ; then |
43822d37 | 2649 | _usage "Usage: $PROJECT_ENTRY --revoke -d domain.com" |
6cb415f5 | 2650 | return 1 |
2651 | fi | |
2652 | ||
43822d37 | 2653 | _isEcc="$2" |
2654 | ||
2655 | _initpath $Le_Domain "$_isEcc" | |
8663fb7e | 2656 | if [ ! -f "$DOMAIN_CONF" ] ; then |
6cb415f5 | 2657 | _err "$Le_Domain is not a issued domain, skip." |
2658 | return 1; | |
2659 | fi | |
2660 | ||
8663fb7e | 2661 | if [ ! -f "$CERT_PATH" ] ; then |
6cb415f5 | 2662 | _err "Cert for $Le_Domain $CERT_PATH is not found, skip." |
2663 | return 1 | |
2664 | fi | |
2665 | ||
2666 | cert="$(_getfile "${CERT_PATH}" "${BEGIN_CERT}" "${END_CERT}"| tr -d "\r\n" | _urlencode)" | |
2667 | ||
8663fb7e | 2668 | if [ -z "$cert" ] ; then |
6cb415f5 | 2669 | _err "Cert for $Le_Domain is empty found, skip." |
2670 | return 1 | |
2671 | fi | |
2672 | ||
2673 | data="{\"resource\": \"revoke-cert\", \"certificate\": \"$cert\"}" | |
2674 | uri="$API/acme/revoke-cert" | |
2675 | ||
2676 | _info "Try domain key first." | |
2677 | if _send_signed_request $uri "$data" "" "$CERT_KEY_PATH"; then | |
8663fb7e | 2678 | if [ -z "$response" ] ; then |
6cb415f5 | 2679 | _info "Revoke success." |
2680 | rm -f $CERT_PATH | |
2681 | return 0 | |
2682 | else | |
2683 | _err "Revoke error by domain key." | |
c9c31c04 | 2684 | _err "$response" |
6cb415f5 | 2685 | fi |
2686 | fi | |
2687 | ||
2688 | _info "Then try account key." | |
2689 | ||
2690 | if _send_signed_request $uri "$data" "" "$ACCOUNT_KEY_PATH" ; then | |
8663fb7e | 2691 | if [ -z "$response" ] ; then |
6cb415f5 | 2692 | _info "Revoke success." |
2693 | rm -f $CERT_PATH | |
2694 | return 0 | |
2695 | else | |
2696 | _err "Revoke error." | |
c9c31c04 | 2697 | _debug "$response" |
6cb415f5 | 2698 | fi |
2699 | fi | |
2700 | return 1 | |
2701 | } | |
4c3b3608 | 2702 | |
2703 | # Detect profile file if not specified as environment variable | |
2704 | _detect_profile() { | |
a63b05a9 | 2705 | if [ -n "$PROFILE" -a -f "$PROFILE" ] ; then |
4c3b3608 | 2706 | echo "$PROFILE" |
2707 | return | |
2708 | fi | |
2709 | ||
4c3b3608 | 2710 | DETECTED_PROFILE='' |
4c3b3608 | 2711 | SHELLTYPE="$(basename "/$SHELL")" |
2712 | ||
8663fb7e | 2713 | if [ "$SHELLTYPE" = "bash" ] ; then |
2714 | if [ -f "$HOME/.bashrc" ] ; then | |
4c3b3608 | 2715 | DETECTED_PROFILE="$HOME/.bashrc" |
8663fb7e | 2716 | elif [ -f "$HOME/.bash_profile" ] ; then |
4c3b3608 | 2717 | DETECTED_PROFILE="$HOME/.bash_profile" |
2718 | fi | |
8663fb7e | 2719 | elif [ "$SHELLTYPE" = "zsh" ] ; then |
4c3b3608 | 2720 | DETECTED_PROFILE="$HOME/.zshrc" |
2721 | fi | |
2722 | ||
8663fb7e | 2723 | if [ -z "$DETECTED_PROFILE" ] ; then |
2724 | if [ -f "$HOME/.profile" ] ; then | |
4c3b3608 | 2725 | DETECTED_PROFILE="$HOME/.profile" |
8663fb7e | 2726 | elif [ -f "$HOME/.bashrc" ] ; then |
4c3b3608 | 2727 | DETECTED_PROFILE="$HOME/.bashrc" |
8663fb7e | 2728 | elif [ -f "$HOME/.bash_profile" ] ; then |
4c3b3608 | 2729 | DETECTED_PROFILE="$HOME/.bash_profile" |
8663fb7e | 2730 | elif [ -f "$HOME/.zshrc" ] ; then |
4c3b3608 | 2731 | DETECTED_PROFILE="$HOME/.zshrc" |
2732 | fi | |
2733 | fi | |
2734 | ||
8663fb7e | 2735 | if [ ! -z "$DETECTED_PROFILE" ] ; then |
4c3b3608 | 2736 | echo "$DETECTED_PROFILE" |
2737 | fi | |
2738 | } | |
2739 | ||
2740 | _initconf() { | |
2741 | _initpath | |
8663fb7e | 2742 | if [ ! -f "$ACCOUNT_CONF_PATH" ] ; then |
d53289d7 | 2743 | echo "#ACCOUNT_CONF_PATH=xxxx |
2744 | ||
2745 | #Account configurations: | |
4c3b3608 | 2746 | #Here are the supported macros, uncomment them to make them take effect. |
d53289d7 | 2747 | |
4c3b3608 | 2748 | #ACCOUNT_EMAIL=aaa@aaa.com # the account email used to register account. |
5fd3f21b | 2749 | #ACCOUNT_KEY_PATH=\"/path/to/account.key\" |
b2817897 | 2750 | #CERT_HOME=\"/path/to/cert/home\" |
4c3b3608 | 2751 | |
5ea6e9c9 | 2752 | #LOG_FILE=\"/var/log/$PROJECT_NAME.log\" |
2753 | ||
4c3b3608 | 2754 | #STAGE=1 # Use the staging api |
2755 | #FORCE=1 # Force to issue cert | |
2756 | #DEBUG=1 # Debug mode | |
2757 | ||
166096dc | 2758 | #ACCOUNT_KEY_HASH=account key hash |
2759 | ||
8814a348 | 2760 | #USER_AGENT=\"$USER_AGENT\" |
281aa349 | 2761 | |
2762 | #USER_PATH="" | |
2763 | ||
4c3b3608 | 2764 | #dns api |
2765 | ####################### | |
2766 | #Cloudflare: | |
2767 | #api key | |
3d49985a | 2768 | #CF_Key=\"sdfsdfsdfljlbjkljlkjsdfoiwje\" |
4c3b3608 | 2769 | #account email |
3d49985a | 2770 | #CF_Email=\"xxxx@sss.com\" |
4c3b3608 | 2771 | |
2772 | ####################### | |
2773 | #Dnspod.cn: | |
2774 | #api key id | |
3d49985a | 2775 | #DP_Id=\"1234\" |
4c3b3608 | 2776 | #api key |
3d49985a | 2777 | #DP_Key=\"sADDsdasdgdsf\" |
4c3b3608 | 2778 | |
2779 | ####################### | |
2780 | #Cloudxns.com: | |
3d49985a | 2781 | #CX_Key=\"1234\" |
4c3b3608 | 2782 | # |
3d49985a | 2783 | #CX_Secret=\"sADDsdasdgdsf\" |
30de13b4 | 2784 | |
2785 | ####################### | |
2786 | #Godaddy.com: | |
2787 | #GD_Key=\"sdfdsgdgdfdasfds\" | |
2788 | # | |
2789 | #GD_Secret=\"sADDsdasdfsdfdssdgdsf\" | |
4c3b3608 | 2790 | |
2791 | " > $ACCOUNT_CONF_PATH | |
2792 | fi | |
2793 | } | |
2794 | ||
c8e9a31e | 2795 | # nocron |
c60883ef | 2796 | _precheck() { |
c8e9a31e | 2797 | _nocron="$1" |
2798 | ||
c60883ef | 2799 | if ! _exists "curl" && ! _exists "wget"; then |
2800 | _err "Please install curl or wget first, we need to access http resources." | |
4c3b3608 | 2801 | return 1 |
2802 | fi | |
2803 | ||
c8e9a31e | 2804 | if [ -z "$_nocron" ] ; then |
2805 | if ! _exists "crontab" ; then | |
2806 | _err "It is recommended to install crontab first. try to install 'cron, crontab, crontabs or vixie-cron'." | |
2807 | _err "We need to set cron job to renew the certs automatically." | |
2808 | _err "Otherwise, your certs will not be able to be renewed automatically." | |
2809 | if [ -z "$FORCE" ] ; then | |
2810 | _err "Please add '--force' and try install again to go without crontab." | |
2811 | _err "./$PROJECT_ENTRY --install --force" | |
2812 | return 1 | |
2813 | fi | |
77546ea5 | 2814 | fi |
4c3b3608 | 2815 | fi |
2816 | ||
c60883ef | 2817 | if ! _exists "openssl" ; then |
2818 | _err "Please install openssl first." | |
2819 | _err "We need openssl to generate keys." | |
4c3b3608 | 2820 | return 1 |
2821 | fi | |
2822 | ||
c60883ef | 2823 | if ! _exists "nc" ; then |
2824 | _err "It is recommended to install nc first, try to install 'nc' or 'netcat'." | |
2825 | _err "We use nc for standalone server if you use standalone mode." | |
2826 | _err "If you don't use standalone mode, just ignore this warning." | |
2827 | fi | |
2828 | ||
2829 | return 0 | |
2830 | } | |
2831 | ||
0a7c9364 | 2832 | _setShebang() { |
2833 | _file="$1" | |
2834 | _shebang="$2" | |
2835 | if [ -z "$_shebang" ] ; then | |
43822d37 | 2836 | _usage "Usage: file shebang" |
0a7c9364 | 2837 | return 1 |
2838 | fi | |
2839 | cp "$_file" "$_file.tmp" | |
2840 | echo "$_shebang" > "$_file" | |
2841 | sed -n 2,99999p "$_file.tmp" >> "$_file" | |
2842 | rm -f "$_file.tmp" | |
2843 | } | |
2844 | ||
94dc5f33 | 2845 | _installalias() { |
2846 | _initpath | |
2847 | ||
2848 | _envfile="$LE_WORKING_DIR/$PROJECT_ENTRY.env" | |
2849 | if [ "$_upgrading" ] && [ "$_upgrading" = "1" ] ; then | |
2850 | echo "$(cat $_envfile)" | sed "s|^LE_WORKING_DIR.*$||" > "$_envfile" | |
2851 | echo "$(cat $_envfile)" | sed "s|^alias le.*$||" > "$_envfile" | |
2852 | echo "$(cat $_envfile)" | sed "s|^alias le.sh.*$||" > "$_envfile" | |
2853 | fi | |
2854 | ||
1786a5e5 | 2855 | _setopt "$_envfile" "export LE_WORKING_DIR" "=" "\"$LE_WORKING_DIR\"" |
94dc5f33 | 2856 | _setopt "$_envfile" "alias $PROJECT_ENTRY" "=" "\"$LE_WORKING_DIR/$PROJECT_ENTRY\"" |
2857 | ||
2858 | _profile="$(_detect_profile)" | |
2859 | if [ "$_profile" ] ; then | |
2860 | _debug "Found profile: $_profile" | |
2861 | _setopt "$_profile" ". \"$_envfile\"" | |
2862 | _info "OK, Close and reopen your terminal to start using $PROJECT_NAME" | |
2863 | else | |
2864 | _info "No profile is found, you will need to go into $LE_WORKING_DIR to use $PROJECT_NAME" | |
2865 | fi | |
2866 | ||
2867 | ||
2868 | #for csh | |
2869 | _cshfile="$LE_WORKING_DIR/$PROJECT_ENTRY.csh" | |
94dc5f33 | 2870 | _csh_profile="$HOME/.cshrc" |
2871 | if [ -f "$_csh_profile" ] ; then | |
6626371d | 2872 | _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\"" |
2873 | _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY\"" | |
94dc5f33 | 2874 | _setopt "$_csh_profile" "source \"$_cshfile\"" |
2875 | fi | |
acafa585 | 2876 | |
2877 | #for tcsh | |
2878 | _tcsh_profile="$HOME/.tcshrc" | |
2879 | if [ -f "$_tcsh_profile" ] ; then | |
2880 | _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\"" | |
2881 | _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY\"" | |
2882 | _setopt "$_tcsh_profile" "source \"$_cshfile\"" | |
2883 | fi | |
94dc5f33 | 2884 | |
2885 | } | |
2886 | ||
c8e9a31e | 2887 | # nocron |
c60883ef | 2888 | install() { |
f3e4cea3 | 2889 | |
2890 | if [ -z "$LE_WORKING_DIR" ] ; then | |
2891 | LE_WORKING_DIR="$DEFAULT_INSTALL_HOME" | |
2892 | fi | |
2893 | ||
c8e9a31e | 2894 | _nocron="$1" |
c60883ef | 2895 | if ! _initpath ; then |
2896 | _err "Install failed." | |
4c3b3608 | 2897 | return 1 |
2898 | fi | |
52677b0a | 2899 | if [ "$_nocron" ] ; then |
2900 | _debug "Skip install cron job" | |
2901 | fi | |
2902 | ||
c8e9a31e | 2903 | if ! _precheck "$_nocron" ; then |
c60883ef | 2904 | _err "Pre-check failed, can not install." |
4c3b3608 | 2905 | return 1 |
2906 | fi | |
c60883ef | 2907 | |
6cc11ffb | 2908 | #convert from le |
8663fb7e | 2909 | if [ -d "$HOME/.le" ] ; then |
6cc11ffb | 2910 | for envfile in "le.env" "le.sh.env" |
2911 | do | |
8663fb7e | 2912 | if [ -f "$HOME/.le/$envfile" ] ; then |
6cc11ffb | 2913 | if grep "le.sh" "$HOME/.le/$envfile" >/dev/null ; then |
2914 | _upgrading="1" | |
2915 | _info "You are upgrading from le.sh" | |
2916 | _info "Renaming \"$HOME/.le\" to $LE_WORKING_DIR" | |
2917 | mv "$HOME/.le" "$LE_WORKING_DIR" | |
2918 | mv "$LE_WORKING_DIR/$envfile" "$LE_WORKING_DIR/$PROJECT_ENTRY.env" | |
2919 | break; | |
2920 | fi | |
2921 | fi | |
2922 | done | |
2923 | fi | |
2924 | ||
4c3b3608 | 2925 | _info "Installing to $LE_WORKING_DIR" |
635695ec | 2926 | |
4a0f23e2 | 2927 | if ! mkdir -p "$LE_WORKING_DIR" ; then |
90035252 | 2928 | _err "Can not create working dir: $LE_WORKING_DIR" |
4a0f23e2 | 2929 | return 1 |
2930 | fi | |
2931 | ||
762978f8 | 2932 | chmod 700 "$LE_WORKING_DIR" |
2933 | ||
a7b7355d | 2934 | cp $PROJECT_ENTRY "$LE_WORKING_DIR/" && chmod +x "$LE_WORKING_DIR/$PROJECT_ENTRY" |
4c3b3608 | 2935 | |
8663fb7e | 2936 | if [ "$?" != "0" ] ; then |
a7b7355d | 2937 | _err "Install failed, can not copy $PROJECT_ENTRY" |
4c3b3608 | 2938 | return 1 |
2939 | fi | |
2940 | ||
a7b7355d | 2941 | _info "Installed to $LE_WORKING_DIR/$PROJECT_ENTRY" |
4c3b3608 | 2942 | |
94dc5f33 | 2943 | _installalias |
4c3b3608 | 2944 | |
8663fb7e | 2945 | if [ -d "dnsapi" ] ; then |
6ed1c718 | 2946 | mkdir -p $LE_WORKING_DIR/dnsapi |
2947 | cp dnsapi/* $LE_WORKING_DIR/dnsapi/ | |
2948 | fi | |
d53289d7 | 2949 | |
8663fb7e | 2950 | if [ ! -f "$ACCOUNT_CONF_PATH" ] ; then |
4c3b3608 | 2951 | _initconf |
2952 | fi | |
6cc11ffb | 2953 | |
8663fb7e | 2954 | if [ "$_DEFAULT_ACCOUNT_CONF_PATH" != "$ACCOUNT_CONF_PATH" ] ; then |
635695ec | 2955 | _setopt "$_DEFAULT_ACCOUNT_CONF_PATH" "ACCOUNT_CONF_PATH" "=" "\"$ACCOUNT_CONF_PATH\"" |
6cc11ffb | 2956 | fi |
2957 | ||
8663fb7e | 2958 | if [ "$_DEFAULT_CERT_HOME" != "$CERT_HOME" ] ; then |
b2817897 | 2959 | _saveaccountconf "CERT_HOME" "$CERT_HOME" |
2960 | fi | |
2961 | ||
8663fb7e | 2962 | if [ "$_DEFAULT_ACCOUNT_KEY_PATH" != "$ACCOUNT_KEY_PATH" ] ; then |
b2817897 | 2963 | _saveaccountconf "ACCOUNT_KEY_PATH" "$ACCOUNT_KEY_PATH" |
2964 | fi | |
2965 | ||
c8e9a31e | 2966 | if [ -z "$_nocron" ] ; then |
2967 | installcronjob | |
2968 | fi | |
0a7c9364 | 2969 | |
641989fd | 2970 | if [ -z "$NO_DETECT_SH" ] ; then |
2971 | #Modify shebang | |
2972 | if _exists bash ; then | |
2973 | _info "Good, bash is installed, change the shebang to use bash as prefered." | |
2974 | _shebang='#!/usr/bin/env bash' | |
2975 | _setShebang "$LE_WORKING_DIR/$PROJECT_ENTRY" "$_shebang" | |
2976 | if [ -d "$LE_WORKING_DIR/dnsapi" ] ; then | |
2977 | for _apifile in $(ls "$LE_WORKING_DIR/dnsapi/"*.sh) ; do | |
2978 | _setShebang "$_apifile" "$_shebang" | |
2979 | done | |
2980 | fi | |
0a7c9364 | 2981 | fi |
2982 | fi | |
2983 | ||
4c3b3608 | 2984 | _info OK |
2985 | } | |
2986 | ||
52677b0a | 2987 | # nocron |
4c3b3608 | 2988 | uninstall() { |
52677b0a | 2989 | _nocron="$1" |
2990 | if [ -z "$_nocron" ] ; then | |
2991 | uninstallcronjob | |
2992 | fi | |
4c3b3608 | 2993 | _initpath |
2994 | ||
2995 | _profile="$(_detect_profile)" | |
8663fb7e | 2996 | if [ "$_profile" ] ; then |
7203a1c1 | 2997 | text="$(cat $_profile)" |
94dc5f33 | 2998 | echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.env\"$||" > "$_profile" |
4c3b3608 | 2999 | fi |
3000 | ||
94dc5f33 | 3001 | _csh_profile="$HOME/.cshrc" |
3002 | if [ -f "$_csh_profile" ] ; then | |
3003 | text="$(cat $_csh_profile)" | |
3004 | echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" > "$_csh_profile" | |
3005 | fi | |
3006 | ||
acafa585 | 3007 | _tcsh_profile="$HOME/.tcshrc" |
3008 | if [ -f "$_tcsh_profile" ] ; then | |
3009 | text="$(cat $_tcsh_profile)" | |
3010 | echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" > "$_tcsh_profile" | |
3011 | fi | |
3012 | ||
a7b7355d | 3013 | rm -f $LE_WORKING_DIR/$PROJECT_ENTRY |
4c3b3608 | 3014 | _info "The keys and certs are in $LE_WORKING_DIR, you can remove them by yourself." |
3015 | ||
3016 | } | |
3017 | ||
3018 | cron() { | |
281aa349 | 3019 | IN_CRON=1 |
4c3b3608 | 3020 | renewAll |
cc179731 | 3021 | _ret="$?" |
281aa349 | 3022 | IN_CRON="" |
cc179731 | 3023 | return $_ret |
4c3b3608 | 3024 | } |
3025 | ||
3026 | version() { | |
a63b05a9 | 3027 | echo "$PROJECT" |
3028 | echo "v$VER" | |
4c3b3608 | 3029 | } |
3030 | ||
3031 | showhelp() { | |
3032 | version | |
a7b7355d | 3033 | echo "Usage: $PROJECT_ENTRY command ...[parameters].... |
a63b05a9 | 3034 | Commands: |
3035 | --help, -h Show this help message. | |
3036 | --version, -v Show version info. | |
a7b7355d | 3037 | --install Install $PROJECT_NAME to your system. |
3038 | --uninstall Uninstall $PROJECT_NAME, and uninstall the cron job. | |
10afcaca | 3039 | --upgrade Upgrade $PROJECT_NAME to the latest code from $PROJECT . |
a63b05a9 | 3040 | --issue Issue a cert. |
10afcaca | 3041 | --signcsr Issue a cert from an existing csr. |
a63b05a9 | 3042 | --installcert Install the issued cert to apache/nginx or any other server. |
3043 | --renew, -r Renew a cert. | |
10afcaca | 3044 | --renewAll Renew all the certs. |
a63b05a9 | 3045 | --revoke Revoke a cert. |
10afcaca | 3046 | --list List all the certs. |
3047 | --showcsr Show the content of a csr. | |
a63b05a9 | 3048 | --installcronjob Install the cron job to renew certs, you don't need to call this. The 'install' command can automatically install the cron job. |
3049 | --uninstallcronjob Uninstall the cron job. The 'uninstall' command can do this automatically. | |
3050 | --cron Run cron job to renew all the certs. | |
3051 | --toPkcs Export the certificate and key to a pfx file. | |
3052 | --createAccountKey, -cak Create an account private key, professional use. | |
3053 | --createDomainKey, -cdk Create an domain private key, professional use. | |
3054 | --createCSR, -ccsr Create CSR , professional use. | |
3055 | ||
3056 | Parameters: | |
3057 | --domain, -d domain.tld Specifies a domain, used to issue, renew or revoke etc. | |
3058 | --force, -f Used to force to install or force to renew a cert immediately. | |
3059 | --staging, --test Use staging server, just for test. | |
3060 | --debug Output debug info. | |
3061 | ||
3062 | --webroot, -w /path/to/webroot Specifies the web root folder for web root mode. | |
3063 | --standalone Use standalone mode. | |
e22bcf7c | 3064 | --tls Use standalone tls mode. |
a63b05a9 | 3065 | --apache Use apache mode. |
eccec5f6 | 3066 | --dns [dns_cf|dns_dp|dns_cx|/path/to/api/file] Use dns mode or dns api. |
4a4dacb5 | 3067 | --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. |
a63b05a9 | 3068 | |
3069 | --keylength, -k [2048] Specifies the domain key length: 2048, 3072, 4096, 8192 or ec-256, ec-384. | |
3070 | --accountkeylength, -ak [2048] Specifies the account key length. | |
5ea6e9c9 | 3071 | --logfile /path/to/logfile Specifies the log file. |
a63b05a9 | 3072 | |
3073 | These parameters are to install the cert to nginx/apache or anyother server after issue/renew a cert: | |
3074 | ||
3075 | --certpath /path/to/real/cert/file After issue/renew, the cert will be copied to this path. | |
3076 | --keypath /path/to/real/key/file After issue/renew, the key will be copied to this path. | |
3077 | --capath /path/to/real/ca/file After issue/renew, the intermediate cert will be copied to this path. | |
3078 | --fullchainpath /path/to/fullchain/file After issue/renew, the fullchain cert will be copied to this path. | |
3079 | ||
3080 | --reloadcmd \"service nginx reload\" After issue/renew, it's used to reload the server. | |
3081 | ||
3082 | --accountconf Specifies a customized account config file. | |
635695ec | 3083 | --home Specifies the home dir for $PROJECT_NAME . |
39c8f79f | 3084 | --certhome Specifies the home dir to save all the certs, only valid for '--install' command. |
635695ec | 3085 | --useragent Specifies the user agent string. it will be saved for future use too. |
b5eb4b90 | 3086 | --accountemail Specifies the account email for registering, Only valid for the '--install' command. |
06625071 | 3087 | --accountkey Specifies the account key path, Only valid for the '--install' command. |
523c7682 | 3088 | --days Specifies the days to renew the cert when using '--issue' command. The max value is $MAX_RENEW days. |
39c8f79f | 3089 | --httpport Specifies the standalone listening port. Only valid if the server is behind a reverse proxy or load balancer. |
e22bcf7c | 3090 | --tlsport Specifies the standalone tls listening port. Only valid if the server is behind a reverse proxy or load balancer. |
dcf4f8f6 | 3091 | --listraw Only used for '--list' command, list the certs in raw format. |
c8e9a31e | 3092 | --stopRenewOnError, -se Only valid for '--renewall' command. Stop if one cert has error in renewal. |
13d7cae9 | 3093 | --insecure Do not check the server certificate, in some devices, the api server's certificate may not be trusted. |
78009539 | 3094 | --ca-bundle Specifices the path to the CA certificate bundle to verify api server's certificate. |
bc96082f | 3095 | --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. |
43822d37 | 3096 | --ecc Specifies to use the ECC cert. Valid for '--installcert', '--renew', '--revoke', '--toPkcs' and '--createCSR' |
10afcaca | 3097 | --csr Specifies the input csr. |
b0070f03 | 3098 | --pre-hook Command to be run before obtaining any certificates. |
3099 | --post-hook Command to be run after attempting to obtain/renew certificates. No matter the obain/renew is success or failed. | |
3100 | --renew-hook Command to be run once for each successfully renewed certificate. | |
0c9546cc | 3101 | --ocsp-must-staple, --ocsp Generate ocsp must Staple extension. |
4c3b3608 | 3102 | " |
3103 | } | |
3104 | ||
52677b0a | 3105 | # nocron |
4a0f23e2 | 3106 | _installOnline() { |
3107 | _info "Installing from online archive." | |
52677b0a | 3108 | _nocron="$1" |
8663fb7e | 3109 | if [ ! "$BRANCH" ] ; then |
4a0f23e2 | 3110 | BRANCH="master" |
3111 | fi | |
a8df88ab | 3112 | |
4a0f23e2 | 3113 | target="$PROJECT/archive/$BRANCH.tar.gz" |
3114 | _info "Downloading $target" | |
3115 | localname="$BRANCH.tar.gz" | |
3116 | if ! _get "$target" > $localname ; then | |
df9547ae | 3117 | _err "Download error." |
4a0f23e2 | 3118 | return 1 |
3119 | fi | |
0bbe6eef | 3120 | ( |
4a0f23e2 | 3121 | _info "Extracting $localname" |
3122 | tar xzf $localname | |
0bbe6eef | 3123 | |
6cc11ffb | 3124 | cd "$PROJECT_NAME-$BRANCH" |
a7b7355d | 3125 | chmod +x $PROJECT_ENTRY |
52677b0a | 3126 | if ./$PROJECT_ENTRY install "$_nocron" ; then |
4a0f23e2 | 3127 | _info "Install success!" |
3128 | fi | |
3129 | ||
3130 | cd .. | |
0bbe6eef | 3131 | |
6cc11ffb | 3132 | rm -rf "$PROJECT_NAME-$BRANCH" |
4a0f23e2 | 3133 | rm -f "$localname" |
0bbe6eef | 3134 | ) |
4a0f23e2 | 3135 | } |
3136 | ||
52677b0a | 3137 | upgrade() { |
3138 | if ( | |
267f283a | 3139 | _initpath |
3140 | export LE_WORKING_DIR | |
d0b748a4 | 3141 | cd "$LE_WORKING_DIR" |
52677b0a | 3142 | _installOnline "nocron" |
3143 | ) ; then | |
3144 | _info "Upgrade success!" | |
096d8992 | 3145 | exit 0 |
52677b0a | 3146 | else |
3147 | _err "Upgrade failed!" | |
096d8992 | 3148 | exit 1 |
52677b0a | 3149 | fi |
3150 | } | |
a63b05a9 | 3151 | |
5ea6e9c9 | 3152 | _processAccountConf() { |
3153 | if [ "$_useragent" ] ; then | |
3154 | _saveaccountconf "USER_AGENT" "$_useragent" | |
3155 | fi | |
3156 | ||
3157 | if [ "$_accountemail" ] ; then | |
3158 | _saveaccountconf "ACCOUNT_EMAIL" "$_accountemail" | |
3159 | fi | |
3160 | ||
3161 | } | |
3162 | ||
a63b05a9 | 3163 | _process() { |
3164 | _CMD="" | |
3165 | _domain="" | |
3166 | _altdomains="no" | |
3167 | _webroot="" | |
bdbf323f | 3168 | _keylength="" |
3169 | _accountkeylength="" | |
3170 | _certpath="" | |
3171 | _keypath="" | |
3172 | _capath="" | |
3173 | _fullchainpath="" | |
4d2f38b0 | 3174 | _reloadcmd="" |
a63b05a9 | 3175 | _password="" |
635695ec | 3176 | _accountconf="" |
3177 | _useragent="" | |
b5eb4b90 | 3178 | _accountemail="" |
3179 | _accountkey="" | |
b2817897 | 3180 | _certhome="" |
39c8f79f | 3181 | _httpport="" |
e22bcf7c | 3182 | _tlsport="" |
0e38c60d | 3183 | _dnssleep="" |
dcf4f8f6 | 3184 | _listraw="" |
cc179731 | 3185 | _stopRenewOnError="" |
13d7cae9 | 3186 | _insecure="" |
78009539 | 3187 | _ca_bundle="" |
c8e9a31e | 3188 | _nocron="" |
43822d37 | 3189 | _ecc="" |
10afcaca | 3190 | _csr="" |
b0070f03 | 3191 | _pre_hook="" |
3192 | _post_hook="" | |
3193 | _renew_hook="" | |
5ea6e9c9 | 3194 | _logfile="" |
8663fb7e | 3195 | while [ ${#} -gt 0 ] ; do |
a63b05a9 | 3196 | case "${1}" in |
3197 | ||
3198 | --help|-h) | |
3199 | showhelp | |
3200 | return | |
3201 | ;; | |
3202 | --version|-v) | |
3203 | version | |
3204 | return | |
3205 | ;; | |
3206 | --install) | |
3207 | _CMD="install" | |
3208 | ;; | |
3209 | --uninstall) | |
3210 | _CMD="uninstall" | |
3211 | ;; | |
52677b0a | 3212 | --upgrade) |
3213 | _CMD="upgrade" | |
3214 | ;; | |
a63b05a9 | 3215 | --issue) |
3216 | _CMD="issue" | |
3217 | ;; | |
10afcaca | 3218 | --signcsr) |
3219 | _CMD="signcsr" | |
3220 | ;; | |
3221 | --showcsr) | |
3222 | _CMD="showcsr" | |
3223 | ;; | |
a63b05a9 | 3224 | --installcert|-i) |
3225 | _CMD="installcert" | |
3226 | ;; | |
3227 | --renew|-r) | |
3228 | _CMD="renew" | |
3229 | ;; | |
4d2f38b0 | 3230 | --renewAll|--renewall) |
a63b05a9 | 3231 | _CMD="renewAll" |
3232 | ;; | |
3233 | --revoke) | |
3234 | _CMD="revoke" | |
3235 | ;; | |
6d7eda3e | 3236 | --list) |
3237 | _CMD="list" | |
3238 | ;; | |
a63b05a9 | 3239 | --installcronjob) |
3240 | _CMD="installcronjob" | |
3241 | ;; | |
3242 | --uninstallcronjob) | |
3243 | _CMD="uninstallcronjob" | |
3244 | ;; | |
3245 | --cron) | |
3246 | _CMD="cron" | |
3247 | ;; | |
3248 | --toPkcs) | |
3249 | _CMD="toPkcs" | |
3250 | ;; | |
3251 | --createAccountKey|--createaccountkey|-cak) | |
3252 | _CMD="createAccountKey" | |
3253 | ;; | |
3254 | --createDomainKey|--createdomainkey|-cdk) | |
3255 | _CMD="createDomainKey" | |
3256 | ;; | |
3257 | --createCSR|--createcsr|-ccr) | |
3258 | _CMD="createCSR" | |
3259 | ;; | |
3260 | ||
3261 | ||
3262 | --domain|-d) | |
3263 | _dvalue="$2" | |
3264 | ||
ee1737a5 | 3265 | if [ "$_dvalue" ] ; then |
3266 | if _startswith "$_dvalue" "-" ; then | |
3267 | _err "'$_dvalue' is not a valid domain for parameter '$1'" | |
3268 | return 1 | |
3269 | fi | |
3270 | ||
3271 | if [ -z "$_domain" ] ; then | |
3272 | _domain="$_dvalue" | |
a63b05a9 | 3273 | else |
ee1737a5 | 3274 | if [ "$_altdomains" = "no" ] ; then |
3275 | _altdomains="$_dvalue" | |
3276 | else | |
3277 | _altdomains="$_altdomains,$_dvalue" | |
3278 | fi | |
a63b05a9 | 3279 | fi |
3280 | fi | |
ee1737a5 | 3281 | |
a63b05a9 | 3282 | shift |
3283 | ;; | |
3284 | ||
3285 | --force|-f) | |
3286 | FORCE="1" | |
3287 | ;; | |
3288 | --staging|--test) | |
3289 | STAGE="1" | |
3290 | ;; | |
3291 | --debug) | |
8663fb7e | 3292 | if [ -z "$2" ] || _startswith "$2" "-" ; then |
a63b05a9 | 3293 | DEBUG="1" |
3294 | else | |
3295 | DEBUG="$2" | |
3296 | shift | |
6fc1447f | 3297 | fi |
a63b05a9 | 3298 | ;; |
a63b05a9 | 3299 | --webroot|-w) |
3300 | wvalue="$2" | |
8663fb7e | 3301 | if [ -z "$_webroot" ] ; then |
a63b05a9 | 3302 | _webroot="$wvalue" |
3303 | else | |
3304 | _webroot="$_webroot,$wvalue" | |
3305 | fi | |
3306 | shift | |
3307 | ;; | |
3308 | --standalone) | |
3309 | wvalue="no" | |
8663fb7e | 3310 | if [ -z "$_webroot" ] ; then |
a63b05a9 | 3311 | _webroot="$wvalue" |
3312 | else | |
3313 | _webroot="$_webroot,$wvalue" | |
3314 | fi | |
3315 | ;; | |
3316 | --apache) | |
3317 | wvalue="apache" | |
8663fb7e | 3318 | if [ -z "$_webroot" ] ; then |
a63b05a9 | 3319 | _webroot="$wvalue" |
3320 | else | |
3321 | _webroot="$_webroot,$wvalue" | |
3322 | fi | |
3323 | ;; | |
e22bcf7c | 3324 | --tls) |
3325 | wvalue="$W_TLS" | |
3326 | if [ -z "$_webroot" ] ; then | |
3327 | _webroot="$wvalue" | |
3328 | else | |
3329 | _webroot="$_webroot,$wvalue" | |
3330 | fi | |
3331 | ;; | |
a63b05a9 | 3332 | --dns) |
3333 | wvalue="dns" | |
dceb3aca | 3334 | if ! _startswith "$2" "-" ; then |
a63b05a9 | 3335 | wvalue="$2" |
3336 | shift | |
3337 | fi | |
8663fb7e | 3338 | if [ -z "$_webroot" ] ; then |
a63b05a9 | 3339 | _webroot="$wvalue" |
3340 | else | |
3341 | _webroot="$_webroot,$wvalue" | |
3342 | fi | |
3343 | ;; | |
0e38c60d | 3344 | --dnssleep) |
3345 | _dnssleep="$2" | |
3346 | Le_DNSSleep="$_dnssleep" | |
3347 | shift | |
3348 | ;; | |
3349 | ||
a63b05a9 | 3350 | --keylength|-k) |
3351 | _keylength="$2" | |
2ce87fe2 | 3352 | if [ "$_accountkeylength" = "no" ] ; then |
3353 | _accountkeylength="$2" | |
3354 | fi | |
a63b05a9 | 3355 | shift |
3356 | ;; | |
3357 | --accountkeylength|-ak) | |
2ce87fe2 | 3358 | _accountkeylength="$2" |
a63b05a9 | 3359 | shift |
3360 | ;; | |
3361 | ||
3362 | --certpath) | |
3363 | _certpath="$2" | |
3364 | shift | |
3365 | ;; | |
3366 | --keypath) | |
3367 | _keypath="$2" | |
3368 | shift | |
3369 | ;; | |
3370 | --capath) | |
3371 | _capath="$2" | |
3372 | shift | |
3373 | ;; | |
3374 | --fullchainpath) | |
3375 | _fullchainpath="$2" | |
3376 | shift | |
3377 | ;; | |
635695ec | 3378 | --reloadcmd|--reloadCmd) |
a63b05a9 | 3379 | _reloadcmd="$2" |
3380 | shift | |
3381 | ;; | |
3382 | --password) | |
3383 | _password="$2" | |
3384 | shift | |
3385 | ;; | |
3386 | --accountconf) | |
635695ec | 3387 | _accountconf="$2" |
3388 | ACCOUNT_CONF_PATH="$_accountconf" | |
a7b7355d | 3389 | shift |
a63b05a9 | 3390 | ;; |
a7b7355d | 3391 | --home) |
a63b05a9 | 3392 | LE_WORKING_DIR="$2" |
a7b7355d | 3393 | shift |
a63b05a9 | 3394 | ;; |
b2817897 | 3395 | --certhome) |
3396 | _certhome="$2" | |
3397 | CERT_HOME="$_certhome" | |
3398 | shift | |
3399 | ;; | |
635695ec | 3400 | --useragent) |
3401 | _useragent="$2" | |
3402 | USER_AGENT="$_useragent" | |
3403 | shift | |
3404 | ;; | |
b5eb4b90 | 3405 | --accountemail ) |
3406 | _accountemail="$2" | |
3407 | ACCOUNT_EMAIL="$_accountemail" | |
3408 | shift | |
3409 | ;; | |
3410 | --accountkey ) | |
3411 | _accountkey="$2" | |
3412 | ACCOUNT_KEY_PATH="$_accountkey" | |
3413 | shift | |
3414 | ;; | |
06625071 | 3415 | --days ) |
3416 | _days="$2" | |
3417 | Le_RenewalDays="$_days" | |
3418 | shift | |
3419 | ;; | |
39c8f79f | 3420 | --httpport ) |
3421 | _httpport="$2" | |
3422 | Le_HTTPPort="$_httpport" | |
3423 | shift | |
3424 | ;; | |
e22bcf7c | 3425 | --tlsport ) |
3426 | _tlsport="$2" | |
3427 | Le_TLSPort="$_tlsport" | |
3428 | shift | |
3429 | ;; | |
3430 | ||
dcf4f8f6 | 3431 | --listraw ) |
3432 | _listraw="raw" | |
3433 | ;; | |
cc179731 | 3434 | --stopRenewOnError|--stoprenewonerror|-se ) |
3435 | _stopRenewOnError="1" | |
3436 | ;; | |
13d7cae9 | 3437 | --insecure) |
3438 | _insecure="1" | |
fac1e367 | 3439 | HTTPS_INSECURE="1" |
13d7cae9 | 3440 | ;; |
78009539 | 3441 | --ca-bundle) |
775bd1ab | 3442 | _ca_bundle="$(readlink -f $2)" |
78009539 PS |
3443 | CA_BUNDLE="$_ca_bundle" |
3444 | shift | |
3445 | ;; | |
c8e9a31e | 3446 | --nocron) |
3447 | _nocron="1" | |
3448 | ;; | |
43822d37 | 3449 | --ecc) |
3450 | _ecc="isEcc" | |
3451 | ;; | |
10afcaca | 3452 | --csr) |
3453 | _csr="$2" | |
3454 | shift | |
3455 | ;; | |
b0070f03 | 3456 | --pre-hook) |
3457 | _pre_hook="$2" | |
3458 | shift | |
3459 | ;; | |
3460 | --post-hook) | |
3461 | _post_hook="$2" | |
3462 | shift | |
3463 | ;; | |
3464 | --renew-hook) | |
3465 | _renew_hook="$2" | |
3466 | shift | |
3467 | ;; | |
0c9546cc | 3468 | --ocsp-must-staple|--ocsp) |
3469 | Le_OCSP_Stable="1" | |
3470 | ;; | |
5ea6e9c9 | 3471 | --logfile) |
3472 | _logfile="$2" | |
3473 | LOG_FILE="$_logfile" | |
3474 | shift | |
3475 | ;; | |
3476 | ||
a63b05a9 | 3477 | *) |
3478 | _err "Unknown parameter : $1" | |
3479 | return 1 | |
3480 | ;; | |
3481 | esac | |
3482 | ||
3483 | shift 1 | |
3484 | done | |
3485 | ||
5ea6e9c9 | 3486 | if [ "${_CMD}" != "install" ] ; then |
3487 | __initHome | |
3488 | if [ "$_logfile" ] ; then | |
3489 | _saveaccountconf "LOG_FILE" "$_logfile" | |
3490 | fi | |
3491 | _processAccountConf | |
3492 | fi | |
3493 | ||
dcf9cb58 | 3494 | if [ "$DEBUG" ] ; then |
3495 | version | |
3496 | fi | |
a63b05a9 | 3497 | |
3498 | case "${_CMD}" in | |
c8e9a31e | 3499 | install) install "$_nocron" ;; |
bc96082f | 3500 | uninstall) uninstall "$_nocron" ;; |
52677b0a | 3501 | upgrade) upgrade ;; |
a63b05a9 | 3502 | issue) |
b0070f03 | 3503 | issue "$_webroot" "$_domain" "$_altdomains" "$_keylength" "$_certpath" "$_keypath" "$_capath" "$_reloadcmd" "$_fullchainpath" "$_pre_hook" "$_post_hook" "$_renew_hook" |
a63b05a9 | 3504 | ;; |
10afcaca | 3505 | signcsr) |
3506 | signcsr "$_csr" "$_webroot" | |
3507 | ;; | |
3508 | showcsr) | |
3509 | showcsr "$_csr" "$_domain" | |
3510 | ;; | |
a63b05a9 | 3511 | installcert) |
43822d37 | 3512 | installcert "$_domain" "$_certpath" "$_keypath" "$_capath" "$_reloadcmd" "$_fullchainpath" "$_ecc" |
a63b05a9 | 3513 | ;; |
3514 | renew) | |
43822d37 | 3515 | renew "$_domain" "$_ecc" |
a63b05a9 | 3516 | ;; |
3517 | renewAll) | |
cc179731 | 3518 | renewAll "$_stopRenewOnError" |
a63b05a9 | 3519 | ;; |
3520 | revoke) | |
43822d37 | 3521 | revoke "$_domain" "$_ecc" |
a63b05a9 | 3522 | ;; |
6d7eda3e | 3523 | list) |
dcf4f8f6 | 3524 | list "$_listraw" |
6d7eda3e | 3525 | ;; |
a63b05a9 | 3526 | installcronjob) installcronjob ;; |
3527 | uninstallcronjob) uninstallcronjob ;; | |
3528 | cron) cron ;; | |
3529 | toPkcs) | |
43822d37 | 3530 | toPkcs "$_domain" "$_password" "$_ecc" |
a63b05a9 | 3531 | ;; |
3532 | createAccountKey) | |
5fbc47eb | 3533 | createAccountKey "$_accountkeylength" |
a63b05a9 | 3534 | ;; |
3535 | createDomainKey) | |
3536 | createDomainKey "$_domain" "$_keylength" | |
3537 | ;; | |
3538 | createCSR) | |
43822d37 | 3539 | createCSR "$_domain" "$_altdomains" "$_ecc" |
a63b05a9 | 3540 | ;; |
3541 | ||
3542 | *) | |
3543 | _err "Invalid command: $_CMD" | |
3544 | showhelp; | |
3545 | return 1 | |
3546 | ;; | |
3547 | esac | |
d3595686 | 3548 | _ret="$?" |
3549 | if [ "$_ret" != "0" ] ; then | |
3550 | return $_ret | |
3551 | fi | |
a63b05a9 | 3552 | |
5ea6e9c9 | 3553 | if [ "${_CMD}" = "install" ] ; then |
3554 | if [ "$_logfile" ] ; then | |
3555 | _saveaccountconf "LOG_FILE" "$_logfile" | |
3556 | fi | |
3557 | _processAccountConf | |
b5eb4b90 | 3558 | fi |
635695ec | 3559 | |
a63b05a9 | 3560 | } |
3561 | ||
3562 | ||
8663fb7e | 3563 | if [ "$INSTALLONLINE" ] ; then |
d1f97fc8 | 3564 | INSTALLONLINE="" |
4a0f23e2 | 3565 | _installOnline $BRANCH |
3566 | exit | |
3567 | fi | |
4c3b3608 | 3568 | |
8663fb7e | 3569 | if [ -z "$1" ] ; then |
4c3b3608 | 3570 | showhelp |
3571 | else | |
036e9d10 | 3572 | if echo "$1" | grep "^-" >/dev/null 2>&1 ; then |
a63b05a9 | 3573 | _process "$@" |
3574 | else | |
3575 | "$@" | |
3576 | fi | |
4c3b3608 | 3577 | fi |
a63b05a9 | 3578 | |
3579 |