]>
Commit | Line | Data |
---|---|---|
0a7c9364 | 1 | #!/usr/bin/env sh |
bfdf1f48 | 2 | |
2ee5d873 | 3 | VER=2.2.5 |
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 | |
11 | DEFAULT_CA="https://acme-v01.api.letsencrypt.org" | |
12 | DEFAULT_AGREEMENT="https://letsencrypt.org/documents/LE-SA-v1.0.1-July-27-2015.pdf" | |
13 | ||
a7b7355d | 14 | DEFAULT_USER_AGENT="$PROJECT_ENTRY client: $PROJECT" |
bbbdcb09 | 15 | |
4c3b3608 | 16 | STAGE_CA="https://acme-staging.api.letsencrypt.org" |
17 | ||
18 | VTYPE_HTTP="http-01" | |
19 | VTYPE_DNS="dns-01" | |
20 | ||
88fab7d6 | 21 | BEGIN_CSR="-----BEGIN CERTIFICATE REQUEST-----" |
22 | END_CSR="-----END CERTIFICATE REQUEST-----" | |
23 | ||
24 | BEGIN_CERT="-----BEGIN CERTIFICATE-----" | |
25 | END_CERT="-----END CERTIFICATE-----" | |
26 | ||
8663fb7e | 27 | if [ -z "$AGREEMENT" ] ; then |
4c3b3608 | 28 | AGREEMENT="$DEFAULT_AGREEMENT" |
29 | fi | |
30 | ||
4c3b3608 | 31 | |
00a50605 | 32 | |
33 | _URGLY_PRINTF="" | |
f4312b44 | 34 | if [ "$(printf '\x41')" != 'A' ] ; then |
00a50605 | 35 | _URGLY_PRINTF=1 |
36 | fi | |
37 | ||
38 | ||
4c3b3608 | 39 | _info() { |
8663fb7e | 40 | if [ -z "$2" ] ; then |
a63b05a9 | 41 | echo "[$(date)] $1" |
4c3b3608 | 42 | else |
19539575 | 43 | echo "[$(date)] $1='$2'" |
4c3b3608 | 44 | fi |
45 | } | |
46 | ||
47 | _err() { | |
a63b05a9 | 48 | _info "$@" >&2 |
4c3b3608 | 49 | return 1 |
50 | } | |
51 | ||
c60883ef | 52 | _debug() { |
8663fb7e | 53 | if [ -z "$DEBUG" ] ; then |
c60883ef | 54 | return |
55 | fi | |
a63b05a9 | 56 | _err "$@" |
c60883ef | 57 | return 0 |
58 | } | |
59 | ||
a63b05a9 | 60 | _debug2() { |
8663fb7e | 61 | if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ] ; then |
a63b05a9 | 62 | _debug "$@" |
63 | fi | |
64 | return | |
65 | } | |
66 | ||
dceb3aca | 67 | _startswith(){ |
68 | _str="$1" | |
69 | _sub="$2" | |
19539575 | 70 | echo "$_str" | grep "^$_sub" >/dev/null 2>&1 |
dceb3aca | 71 | } |
72 | ||
73 | _contains(){ | |
74 | _str="$1" | |
75 | _sub="$2" | |
19539575 | 76 | echo "$_str" | grep "$_sub" >/dev/null 2>&1 |
dceb3aca | 77 | } |
78 | ||
c53da1ef | 79 | _hasfield() { |
80 | _str="$1" | |
81 | _field="$2" | |
82 | _sep="$3" | |
83 | if [ -z "$_field" ] ; then | |
84 | _err "Usage: str field [sep]" | |
85 | return 1 | |
86 | fi | |
87 | ||
88 | if [ -z "$_sep" ] ; then | |
89 | _sep="," | |
90 | fi | |
91 | ||
92 | for f in $(echo "$_str" | tr ',' ' ') ; do | |
93 | if [ "$f" = "$_field" ] ; then | |
94 | _debug "'$_str' contains '$_field'" | |
95 | return 0 #contains ok | |
96 | fi | |
97 | done | |
98 | _debug "'$_str' does not contain '$_field'" | |
99 | return 1 #not contains | |
100 | } | |
101 | ||
dceb3aca | 102 | _exists(){ |
c60883ef | 103 | cmd="$1" |
8663fb7e | 104 | if [ -z "$cmd" ] ; then |
c60883ef | 105 | _err "Usage: _exists cmd" |
106 | return 1 | |
107 | fi | |
eac18b1c | 108 | if type command >/dev/null 2>&1 ; then |
19539575 | 109 | command -v "$cmd" >/dev/null 2>&1 |
eac18b1c | 110 | else |
19539575 | 111 | type "$cmd" >/dev/null 2>&1 |
eac18b1c | 112 | fi |
c60883ef | 113 | ret="$?" |
a63b05a9 | 114 | _debug2 "$cmd exists=$ret" |
c60883ef | 115 | return $ret |
116 | } | |
117 | ||
00a50605 | 118 | #a + b |
119 | _math(){ | |
120 | expr "$@" | |
121 | } | |
122 | ||
123 | _h_char_2_dec() { | |
124 | _ch=$1 | |
125 | case "${_ch}" in | |
126 | a|A) | |
19539575 | 127 | printf "10" |
00a50605 | 128 | ;; |
129 | b|B) | |
19539575 | 130 | printf "11" |
00a50605 | 131 | ;; |
132 | c|C) | |
19539575 | 133 | printf "12" |
00a50605 | 134 | ;; |
135 | d|D) | |
19539575 | 136 | printf "13" |
00a50605 | 137 | ;; |
138 | e|E) | |
19539575 | 139 | printf "14" |
00a50605 | 140 | ;; |
141 | f|F) | |
19539575 | 142 | printf "15" |
00a50605 | 143 | ;; |
144 | *) | |
19539575 | 145 | printf "%s" "$_ch" |
00a50605 | 146 | ;; |
19539575 | 147 | esac |
00a50605 | 148 | |
149 | } | |
150 | ||
4c3b3608 | 151 | _h2b() { |
152 | hex=$(cat) | |
153 | i=1 | |
154 | j=2 | |
00a50605 | 155 | if _exists let ; then |
156 | uselet="1" | |
157 | fi | |
158 | _debug uselet "$uselet" | |
f4312b44 | 159 | _debug _URGLY_PRINTF "$_URGLY_PRINTF" |
19539575 | 160 | while true ; do |
00a50605 | 161 | if [ -z "$_URGLY_PRINTF" ] ; then |
19539575 | 162 | h="$(printf $hex | cut -c $i-$j)" |
00a50605 | 163 | if [ -z "$h" ] ; then |
164 | break; | |
165 | fi | |
166 | printf "\x$h" | |
167 | else | |
19539575 | 168 | ic="$(printf $hex | cut -c $i)" |
169 | jc="$(printf $hex | cut -c $j)" | |
00a50605 | 170 | if [ -z "$ic$jc" ] ; then |
171 | break; | |
172 | fi | |
19539575 | 173 | ic="$(_h_char_2_dec "$ic")" |
174 | jc="$(_h_char_2_dec "$jc")" | |
00a50605 | 175 | printf '\'"$(printf %o "$(_math $ic \* 16 + $jc)")" |
4c3b3608 | 176 | fi |
00a50605 | 177 | if [ "$uselet" ] ; then |
f4312b44 | 178 | let "i+=2" >/dev/null |
179 | let "j+=2" >/dev/null | |
00a50605 | 180 | else |
181 | i="$(_math $i + 2)" | |
182 | j="$(_math $j + 2)" | |
183 | fi | |
4c3b3608 | 184 | done |
185 | } | |
186 | ||
c60883ef | 187 | #options file |
188 | _sed_i() { | |
189 | options="$1" | |
190 | filename="$2" | |
8663fb7e | 191 | if [ -z "$filename" ] ; then |
c60883ef | 192 | _err "Usage:_sed_i options filename" |
193 | return 1 | |
194 | fi | |
14f3dbb7 | 195 | _debug2 options "$options" |
196 | if sed -h 2>&1 | grep "\-i\[SUFFIX]" >/dev/null 2>&1; then | |
c60883ef | 197 | _debug "Using sed -i" |
14f3dbb7 | 198 | sed -i "$options" "$filename" |
c60883ef | 199 | else |
200 | _debug "No -i support in sed" | |
19539575 | 201 | text="$(cat "$filename")" |
c60883ef | 202 | echo "$text" | sed "$options" > "$filename" |
203 | fi | |
204 | } | |
205 | ||
88fab7d6 | 206 | #Usage: file startline endline |
207 | _getfile() { | |
208 | filename="$1" | |
209 | startline="$2" | |
210 | endline="$3" | |
8663fb7e | 211 | if [ -z "$endline" ] ; then |
88fab7d6 | 212 | _err "Usage: file startline endline" |
213 | return 1 | |
214 | fi | |
215 | ||
19539575 | 216 | i="$(grep -n -- "$startline" "$filename" | cut -d : -f 1)" |
8663fb7e | 217 | if [ -z "$i" ] ; then |
88fab7d6 | 218 | _err "Can not find start line: $startline" |
219 | return 1 | |
220 | fi | |
19539575 | 221 | i="$(_math "$i" + 1)" |
222 | _debug i "$i" | |
88fab7d6 | 223 | |
19539575 | 224 | j="$(grep -n -- "$endline" "$filename" | cut -d : -f 1)" |
8663fb7e | 225 | if [ -z "$j" ] ; then |
88fab7d6 | 226 | _err "Can not find end line: $endline" |
227 | return 1 | |
228 | fi | |
19539575 | 229 | j="$(_math "$j" - 1)" |
230 | _debug j "$j" | |
88fab7d6 | 231 | |
19539575 | 232 | sed -n "$i,${j}p" "$filename" |
88fab7d6 | 233 | |
234 | } | |
235 | ||
236 | #Usage: multiline | |
4c3b3608 | 237 | _base64() { |
8663fb7e | 238 | if [ "$1" ] ; then |
88fab7d6 | 239 | openssl base64 -e |
240 | else | |
241 | openssl base64 -e | tr -d '\r\n' | |
242 | fi | |
243 | } | |
244 | ||
245 | #Usage: multiline | |
246 | _dbase64() { | |
8663fb7e | 247 | if [ "$1" ] ; then |
88fab7d6 | 248 | openssl base64 -d -A |
249 | else | |
250 | openssl base64 -d | |
251 | fi | |
252 | } | |
253 | ||
254 | #Usage: hashalg | |
255 | #Output Base64-encoded digest | |
256 | _digest() { | |
257 | alg="$1" | |
8663fb7e | 258 | if [ -z "$alg" ] ; then |
88fab7d6 | 259 | _err "Usage: _digest hashalg" |
260 | return 1 | |
261 | fi | |
262 | ||
8663fb7e | 263 | if [ "$alg" = "sha256" ] ; then |
88fab7d6 | 264 | openssl dgst -sha256 -binary | _base64 |
265 | else | |
266 | _err "$alg is not supported yet" | |
267 | return 1 | |
268 | fi | |
269 | ||
270 | } | |
271 | ||
272 | #Usage: keyfile hashalg | |
273 | #Output: Base64-encoded signature value | |
274 | _sign() { | |
275 | keyfile="$1" | |
276 | alg="$2" | |
8663fb7e | 277 | if [ -z "$alg" ] ; then |
88fab7d6 | 278 | _err "Usage: _sign keyfile hashalg" |
279 | return 1 | |
280 | fi | |
281 | ||
8663fb7e | 282 | if [ "$alg" = "sha256" ] ; then |
88fab7d6 | 283 | openssl dgst -sha256 -sign "$keyfile" | _base64 |
284 | else | |
285 | _err "$alg is not supported yet" | |
286 | return 1 | |
287 | fi | |
288 | ||
4c3b3608 | 289 | } |
290 | ||
34c27e09 | 291 | _ss() { |
292 | _port="$1" | |
edf08da6 | 293 | |
294 | if _exists "ss" ; then | |
295 | _debug "Using: ss" | |
19539575 | 296 | ss -ntpl | grep ":$_port " |
edf08da6 | 297 | return 0 |
298 | fi | |
299 | ||
300 | if _exists "netstat" ; then | |
251fc37c | 301 | _debug "Using: netstat" |
ccb96535 | 302 | if netstat -h 2>&1 | grep "\-p proto" >/dev/null ; then |
303 | #for windows version netstat tool | |
19539575 | 304 | netstat -anb -p tcp | grep "LISTENING" | grep ":$_port " |
ccb96535 | 305 | else |
14165e5a | 306 | if netstat -help 2>&1 | grep "\-p protocol" >/dev/null ; then |
19539575 | 307 | netstat -an -p tcp | grep LISTEN | grep ":$_port " |
edf08da6 | 308 | else |
19539575 | 309 | netstat -ntpl | grep ":$_port " |
edf08da6 | 310 | fi |
ccb96535 | 311 | fi |
34c27e09 | 312 | return 0 |
313 | fi | |
edf08da6 | 314 | |
34c27e09 | 315 | return 1 |
316 | } | |
317 | ||
ac2d5123 | 318 | toPkcs() { |
319 | domain="$1" | |
320 | pfxPassword="$2" | |
8663fb7e | 321 | if [ -z "$domain" ] ; then |
a7b7355d | 322 | echo "Usage: $PROJECT_ENTRY --toPkcs -d domain [--password pfx-password]" |
ac2d5123 | 323 | return 1 |
324 | fi | |
325 | ||
326 | _initpath "$domain" | |
327 | ||
8663fb7e | 328 | if [ "$pfxPassword" ] ; then |
ac2d5123 | 329 | openssl pkcs12 -export -out "$CERT_PFX_PATH" -inkey "$CERT_KEY_PATH" -in "$CERT_PATH" -certfile "$CA_CERT_PATH" -password "pass:$pfxPassword" |
330 | else | |
331 | openssl pkcs12 -export -out "$CERT_PFX_PATH" -inkey "$CERT_KEY_PATH" -in "$CERT_PATH" -certfile "$CA_CERT_PATH" | |
332 | fi | |
333 | ||
8663fb7e | 334 | if [ "$?" = "0" ] ; then |
ac2d5123 | 335 | _info "Success, Pfx is exported to: $CERT_PFX_PATH" |
336 | fi | |
337 | ||
338 | } | |
339 | ||
4c3b3608 | 340 | #domain [2048] |
341 | createAccountKey() { | |
342 | _info "Creating account key" | |
8663fb7e | 343 | if [ -z "$1" ] ; then |
a7b7355d | 344 | echo Usage: $PROJECT_ENTRY --createAccountKey -d domain.com [--accountkeylength 2048] |
4c3b3608 | 345 | return |
346 | fi | |
347 | ||
348 | account=$1 | |
349 | length=$2 | |
b5eb4b90 | 350 | _debug account "$account" |
351 | _debug length "$length" | |
dceb3aca | 352 | if _startswith "$length" "ec-" ; then |
4c3b3608 | 353 | length=2048 |
354 | fi | |
355 | ||
8663fb7e | 356 | if [ -z "$2" ] || [ "$2" = "no" ] ; then |
4c3b3608 | 357 | _info "Use default length 2048" |
358 | length=2048 | |
359 | fi | |
360 | _initpath | |
361 | ||
8663fb7e | 362 | if [ -f "$ACCOUNT_KEY_PATH" ] ; then |
4c3b3608 | 363 | _info "Account key exists, skip" |
364 | return | |
365 | else | |
366 | #generate account key | |
f89d991d | 367 | openssl genrsa $length 2>/dev/null > "$ACCOUNT_KEY_PATH" |
4c3b3608 | 368 | fi |
369 | ||
370 | } | |
371 | ||
372 | #domain length | |
373 | createDomainKey() { | |
374 | _info "Creating domain key" | |
8663fb7e | 375 | if [ -z "$1" ] ; then |
a7b7355d | 376 | echo Usage: $PROJECT_ENTRY --createDomainKey -d domain.com [ --keylength 2048 ] |
4c3b3608 | 377 | return |
378 | fi | |
379 | ||
380 | domain=$1 | |
381 | length=$2 | |
382 | isec="" | |
dceb3aca | 383 | if _startswith "$length" "ec-" ; then |
4c3b3608 | 384 | isec="1" |
385 | length=$(printf $length | cut -d '-' -f 2-100) | |
386 | eccname="$length" | |
387 | fi | |
388 | ||
8663fb7e | 389 | if [ -z "$length" ] ; then |
390 | if [ "$isec" ] ; then | |
4c3b3608 | 391 | length=256 |
392 | else | |
393 | length=2048 | |
394 | fi | |
395 | fi | |
396 | _info "Use length $length" | |
397 | ||
8663fb7e | 398 | if [ "$isec" ] ; then |
399 | if [ "$length" = "256" ] ; then | |
4c3b3608 | 400 | eccname="prime256v1" |
401 | fi | |
8663fb7e | 402 | if [ "$length" = "384" ] ; then |
4c3b3608 | 403 | eccname="secp384r1" |
404 | fi | |
8663fb7e | 405 | if [ "$length" = "521" ] ; then |
4c3b3608 | 406 | eccname="secp521r1" |
407 | fi | |
408 | _info "Using ec name: $eccname" | |
409 | fi | |
410 | ||
411 | _initpath $domain | |
412 | ||
8663fb7e | 413 | if [ ! -f "$CERT_KEY_PATH" ] || ( [ "$FORCE" ] && ! [ "$IS_RENEW" ] ); then |
4c3b3608 | 414 | #generate account key |
8663fb7e | 415 | if [ "$isec" ] ; then |
4c3b3608 | 416 | openssl ecparam -name $eccname -genkey 2>/dev/null > "$CERT_KEY_PATH" |
417 | else | |
418 | openssl genrsa $length 2>/dev/null > "$CERT_KEY_PATH" | |
419 | fi | |
420 | else | |
8663fb7e | 421 | if [ "$IS_RENEW" ] ; then |
4c3b3608 | 422 | _info "Domain key exists, skip" |
423 | return 0 | |
424 | else | |
425 | _err "Domain key exists, do you want to overwrite the key?" | |
41e3eafa | 426 | _err "Add '--force', and try again." |
4c3b3608 | 427 | return 1 |
428 | fi | |
429 | fi | |
430 | ||
431 | } | |
432 | ||
433 | # domain domainlist | |
434 | createCSR() { | |
435 | _info "Creating csr" | |
8663fb7e | 436 | if [ -z "$1" ] ; then |
19539575 | 437 | echo "Usage: $PROJECT_ENTRY --createCSR -d domain1.com [-d domain2.com -d domain3.com ... ]" |
4c3b3608 | 438 | return |
439 | fi | |
440 | domain=$1 | |
19539575 | 441 | _initpath "$domain" |
4c3b3608 | 442 | |
443 | domainlist=$2 | |
444 | ||
8663fb7e | 445 | if [ -f "$CSR_PATH" ] && [ "$IS_RENEW" ] && [ -z "$FORCE" ]; then |
4c3b3608 | 446 | _info "CSR exists, skip" |
447 | return | |
448 | fi | |
449 | ||
8663fb7e | 450 | if [ -z "$domainlist" ] || [ "$domainlist" = "no" ]; then |
4c3b3608 | 451 | #single domain |
19539575 | 452 | _info "Single domain" "$domain" |
1ad65f7d | 453 | printf "[ req_distinguished_name ]\n[ req ]\ndistinguished_name = req_distinguished_name\n" > "$DOMAIN_SSL_CONF" |
454 | openssl req -new -sha256 -key "$CERT_KEY_PATH" -subj "/CN=$domain" -config "$DOMAIN_SSL_CONF" -out "$CSR_PATH" | |
4c3b3608 | 455 | else |
456 | alt="DNS:$(echo $domainlist | sed "s/,/,DNS:/g")" | |
457 | #multi | |
458 | _info "Multi domain" "$alt" | |
459 | printf "[ req_distinguished_name ]\n[ req ]\ndistinguished_name = req_distinguished_name\n[SAN]\nsubjectAltName=$alt" > "$DOMAIN_SSL_CONF" | |
460 | openssl req -new -sha256 -key "$CERT_KEY_PATH" -subj "/CN=$domain" -reqexts SAN -config "$DOMAIN_SSL_CONF" -out "$CSR_PATH" | |
461 | fi | |
462 | ||
463 | } | |
464 | ||
166096dc | 465 | _urlencode() { |
4c3b3608 | 466 | __n=$(cat) |
467 | echo $__n | tr '/+' '_-' | tr -d '= ' | |
468 | } | |
469 | ||
470 | _time2str() { | |
471 | #BSD | |
472 | if date -u -d@$1 2>/dev/null ; then | |
473 | return | |
474 | fi | |
475 | ||
476 | #Linux | |
477 | if date -u -r $1 2>/dev/null ; then | |
478 | return | |
479 | fi | |
480 | ||
481 | } | |
482 | ||
eae29099 | 483 | _normalizeJson() { |
484 | sed "s/\" *: *\([\"{\[]\)/\":\1/g" | sed "s/^ *\([^ ]\)/\1/" | tr -d "\r\n" | |
485 | } | |
486 | ||
44df2967 | 487 | _stat() { |
488 | #Linux | |
489 | if stat -c '%U:%G' "$1" 2>/dev/null ; then | |
490 | return | |
491 | fi | |
492 | ||
493 | #BSD | |
494 | if stat -f '%Su:%Sg' "$1" 2>/dev/null ; then | |
495 | return | |
496 | fi | |
497 | } | |
498 | ||
166096dc | 499 | #keyfile |
500 | _calcjwk() { | |
501 | keyfile="$1" | |
8663fb7e | 502 | if [ -z "$keyfile" ] ; then |
166096dc | 503 | _err "Usage: _calcjwk keyfile" |
504 | return 1 | |
505 | fi | |
506 | EC_SIGN="" | |
507 | if grep "BEGIN RSA PRIVATE KEY" "$keyfile" > /dev/null 2>&1 ; then | |
508 | _debug "RSA key" | |
509 | pub_exp=$(openssl rsa -in $keyfile -noout -text | grep "^publicExponent:"| cut -d '(' -f 2 | cut -d 'x' -f 2 | cut -d ')' -f 1) | |
8663fb7e | 510 | if [ "${#pub_exp}" = "5" ] ; then |
166096dc | 511 | pub_exp=0$pub_exp |
512 | fi | |
a63b05a9 | 513 | _debug2 pub_exp "$pub_exp" |
166096dc | 514 | |
515 | e=$(echo $pub_exp | _h2b | _base64) | |
a63b05a9 | 516 | _debug2 e "$e" |
166096dc | 517 | |
518 | modulus=$(openssl rsa -in $keyfile -modulus -noout | cut -d '=' -f 2 ) | |
00a50605 | 519 | _debug2 modulus "$modulus" |
19539575 | 520 | n="$(printf "%s" "$modulus"| _h2b | _base64 | _urlencode )" |
166096dc | 521 | jwk='{"e": "'$e'", "kty": "RSA", "n": "'$n'"}' |
a63b05a9 | 522 | _debug2 jwk "$jwk" |
166096dc | 523 | |
524 | HEADER='{"alg": "RS256", "jwk": '$jwk'}' | |
525 | HEADERPLACE='{"nonce": "NONCE", "alg": "RS256", "jwk": '$jwk'}' | |
526 | elif grep "BEGIN EC PRIVATE KEY" "$keyfile" > /dev/null 2>&1 ; then | |
527 | _debug "EC key" | |
528 | EC_SIGN="1" | |
529 | crv="$(openssl ec -in $keyfile -noout -text 2>/dev/null | grep "^NIST CURVE:" | cut -d ":" -f 2 | tr -d " \r\n")" | |
19539575 | 530 | _debug2 crv "$crv" |
166096dc | 531 | |
532 | pubi="$(openssl ec -in $keyfile -noout -text 2>/dev/null | grep -n pub: | cut -d : -f 1)" | |
00a50605 | 533 | pubi=$(_math $pubi + 1) |
19539575 | 534 | _debug2 pubi "$pubi" |
166096dc | 535 | |
536 | pubj="$(openssl ec -in $keyfile -noout -text 2>/dev/null | grep -n "ASN1 OID:" | cut -d : -f 1)" | |
00a50605 | 537 | pubj=$(_math $pubj + 1) |
19539575 | 538 | _debug2 pubj "$pubj" |
166096dc | 539 | |
540 | pubtext="$(openssl ec -in $keyfile -noout -text 2>/dev/null | sed -n "$pubi,${pubj}p" | tr -d " \n\r")" | |
a63b05a9 | 541 | _debug2 pubtext "$pubtext" |
166096dc | 542 | |
543 | xlen="$(printf "$pubtext" | tr -d ':' | wc -c)" | |
00a50605 | 544 | xlen=$(_math $xlen / 4) |
19539575 | 545 | _debug2 xlen "$xlen" |
00a50605 | 546 | |
19539575 | 547 | xend=$(_math "$xend" + 1) |
166096dc | 548 | x="$(printf $pubtext | cut -d : -f 2-$xend)" |
19539575 | 549 | _debug2 x "$x" |
166096dc | 550 | |
551 | x64="$(printf $x | tr -d : | _h2b | _base64 | _urlencode)" | |
19539575 | 552 | _debug2 x64 "$x64" |
00a50605 | 553 | |
19539575 | 554 | xend=$(_math "$xend" + 1) |
166096dc | 555 | y="$(printf $pubtext | cut -d : -f $xend-10000)" |
19539575 | 556 | _debug2 y "$y" |
166096dc | 557 | |
558 | y64="$(printf $y | tr -d : | _h2b | _base64 | _urlencode)" | |
19539575 | 559 | _debug2 y64 "$y64" |
166096dc | 560 | |
561 | jwk='{"kty": "EC", "crv": "'$crv'", "x": "'$x64'", "y": "'$y64'"}' | |
a63b05a9 | 562 | _debug2 jwk "$jwk" |
166096dc | 563 | |
564 | HEADER='{"alg": "ES256", "jwk": '$jwk'}' | |
565 | HEADERPLACE='{"nonce": "NONCE", "alg": "ES256", "jwk": '$jwk'}' | |
566 | ||
567 | else | |
568 | _err "Only RSA or EC key is supported." | |
569 | return 1 | |
570 | fi | |
571 | ||
a63b05a9 | 572 | _debug2 HEADER "$HEADER" |
166096dc | 573 | } |
c839b2b0 | 574 | # body url [needbase64] [POST|PUT] |
c60883ef | 575 | _post() { |
576 | body="$1" | |
577 | url="$2" | |
578 | needbase64="$3" | |
a4270efa | 579 | httpmethod="$4" |
c60883ef | 580 | |
a4270efa | 581 | if [ -z "$httpmethod" ] ; then |
582 | httpmethod="POST" | |
583 | fi | |
584 | _debug $httpmethod | |
484d9d2a | 585 | _debug "url" "$url" |
c60883ef | 586 | if _exists "curl" ; then |
8f48168c | 587 | _CURL="$CURL --dump-header $HTTP_HEADER " |
ec9fc8cb | 588 | _debug "_CURL" "$_CURL" |
8663fb7e | 589 | if [ "$needbase64" ] ; then |
cc7fdbd6 | 590 | response="$($_CURL -A "User-Agent: $USER_AGENT" -X $httpmethod -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" --data "$body" "$url" | _base64)" |
c60883ef | 591 | else |
cc7fdbd6 | 592 | response="$($_CURL -A "User-Agent: $USER_AGENT" -X $httpmethod -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" --data "$body" "$url" )" |
c60883ef | 593 | fi |
16679b57 | 594 | _ret="$?" |
c60883ef | 595 | else |
ec9fc8cb | 596 | _debug "WGET" "$WGET" |
8663fb7e | 597 | if [ "$needbase64" ] ; then |
8fb9a709 | 598 | if [ "$httpmethod"="POST" ] ; then |
599 | response="$($WGET -S -O - --user-agent="$USER_AGENT" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --post-data="$body" "$url" 2>"$HTTP_HEADER" | _base64)" | |
600 | else | |
601 | response="$($WGET -S -O - --user-agent="$USER_AGENT" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --method $httpmethod --body-data="$body" "$url" 2>"$HTTP_HEADER" | _base64)" | |
602 | fi | |
c60883ef | 603 | else |
8fb9a709 | 604 | if [ "$httpmethod"="POST" ] ; then |
605 | response="$($WGET -S -O - --user-agent="$USER_AGENT" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --post-data="$body" "$url" 2>"$HTTP_HEADER")" | |
606 | else | |
607 | response="$($WGET -S -O - --user-agent="$USER_AGENT" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --method $httpmethod --body-data="$body" "$url" 2>"$HTTP_HEADER")" | |
608 | fi | |
c60883ef | 609 | fi |
16679b57 | 610 | _ret="$?" |
c60883ef | 611 | _sed_i "s/^ *//g" "$HTTP_HEADER" |
612 | fi | |
16679b57 | 613 | _debug "_ret" "$_ret" |
19539575 | 614 | printf "%s" "$response" |
16679b57 | 615 | return $_ret |
c60883ef | 616 | } |
617 | ||
618 | # url getheader | |
619 | _get() { | |
a4270efa | 620 | _debug GET |
c60883ef | 621 | url="$1" |
622 | onlyheader="$2" | |
623 | _debug url $url | |
624 | if _exists "curl" ; then | |
ec9fc8cb | 625 | _debug "CURL" "$CURL" |
8663fb7e | 626 | if [ "$onlyheader" ] ; then |
cc7fdbd6 | 627 | $CURL -I -A "User-Agent: $USER_AGENT" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" $url |
c60883ef | 628 | else |
cc7fdbd6 | 629 | $CURL -A "User-Agent: $USER_AGENT" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" $url |
c60883ef | 630 | fi |
9aaf36cd | 631 | ret=$? |
c60883ef | 632 | else |
bbbdcb09 | 633 | _debug "WGET" "$WGET" |
8663fb7e | 634 | if [ "$onlyheader" ] ; then |
a4270efa | 635 | $WGET --user-agent="$USER_AGENT" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" -S -O /dev/null $url 2>&1 | sed 's/^[ ]*//g' |
c60883ef | 636 | else |
cc7fdbd6 | 637 | $WGET --user-agent="$USER_AGENT" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" -O - $url |
c60883ef | 638 | fi |
9aaf36cd | 639 | ret=$? |
640 | fi | |
ec9fc8cb | 641 | _debug "ret" "$ret" |
c60883ef | 642 | return $ret |
643 | } | |
166096dc | 644 | |
645 | # url payload needbase64 keyfile | |
4c3b3608 | 646 | _send_signed_request() { |
647 | url=$1 | |
648 | payload=$2 | |
649 | needbase64=$3 | |
166096dc | 650 | keyfile=$4 |
8663fb7e | 651 | if [ -z "$keyfile" ] ; then |
166096dc | 652 | keyfile="$ACCOUNT_KEY_PATH" |
653 | fi | |
4c3b3608 | 654 | _debug url $url |
655 | _debug payload "$payload" | |
656 | ||
166096dc | 657 | if ! _calcjwk "$keyfile" ; then |
658 | return 1 | |
659 | fi | |
c60883ef | 660 | |
166096dc | 661 | payload64=$(echo -n $payload | _base64 | _urlencode) |
a63b05a9 | 662 | _debug2 payload64 $payload64 |
4c3b3608 | 663 | |
664 | nonceurl="$API/directory" | |
a272ee4f | 665 | _headers="$(_get $nonceurl "onlyheader")" |
666 | ||
7012b91f | 667 | if [ "$?" != "0" ] ; then |
668 | _err "Can not connect to $nonceurl to get nonce." | |
669 | return 1 | |
670 | fi | |
a272ee4f | 671 | |
672 | _debug2 _headers "$_headers" | |
673 | ||
674 | nonce="$( echo "$_headers" | grep "Replay-Nonce:" | head -1 | tr -d "\r\n " | cut -d ':' -f 2)" | |
675 | ||
4c3b3608 | 676 | _debug nonce "$nonce" |
677 | ||
678 | protected="$(printf "$HEADERPLACE" | sed "s/NONCE/$nonce/" )" | |
a63b05a9 | 679 | _debug2 protected "$protected" |
4c3b3608 | 680 | |
166096dc | 681 | protected64="$(printf "$protected" | _base64 | _urlencode)" |
a63b05a9 | 682 | _debug2 protected64 "$protected64" |
166096dc | 683 | |
88fab7d6 | 684 | sig=$(echo -n "$protected64.$payload64" | _sign "$keyfile" "sha256" | _urlencode) |
a63b05a9 | 685 | _debug2 sig "$sig" |
4c3b3608 | 686 | |
687 | body="{\"header\": $HEADER, \"protected\": \"$protected64\", \"payload\": \"$payload64\", \"signature\": \"$sig\"}" | |
a63b05a9 | 688 | _debug2 body "$body" |
4c3b3608 | 689 | |
bbbdcb09 | 690 | |
eae29099 | 691 | response="$(_post "$body" $url "$needbase64")" |
7012b91f | 692 | if [ "$?" != "0" ] ; then |
693 | _err "Can not post to $url." | |
694 | return 1 | |
695 | fi | |
eae29099 | 696 | _debug2 original "$response" |
697 | ||
698 | response="$( echo "$response" | _normalizeJson )" | |
4c3b3608 | 699 | |
c60883ef | 700 | responseHeaders="$(cat $HTTP_HEADER)" |
4c3b3608 | 701 | |
a63b05a9 | 702 | _debug2 responseHeaders "$responseHeaders" |
703 | _debug2 response "$response" | |
c60883ef | 704 | code="$(grep "^HTTP" $HTTP_HEADER | tail -1 | cut -d " " -f 2 | tr -d "\r\n" )" |
4c3b3608 | 705 | _debug code $code |
706 | ||
707 | } | |
708 | ||
4c3b3608 | 709 | |
710 | #setopt "file" "opt" "=" "value" [";"] | |
711 | _setopt() { | |
712 | __conf="$1" | |
713 | __opt="$2" | |
714 | __sep="$3" | |
715 | __val="$4" | |
716 | __end="$5" | |
8663fb7e | 717 | if [ -z "$__opt" ] ; then |
4c3b3608 | 718 | echo usage: _setopt '"file" "opt" "=" "value" [";"]' |
719 | return | |
720 | fi | |
8663fb7e | 721 | if [ ! -f "$__conf" ] ; then |
4c3b3608 | 722 | touch "$__conf" |
723 | fi | |
724 | ||
725 | if grep -H -n "^$__opt$__sep" "$__conf" > /dev/null ; then | |
a63b05a9 | 726 | _debug2 OK |
dceb3aca | 727 | if _contains "$__val" "&" ; then |
4c3b3608 | 728 | __val="$(echo $__val | sed 's/&/\\&/g')" |
729 | fi | |
730 | text="$(cat $__conf)" | |
6dfaaa70 | 731 | echo "$text" | sed "s|^$__opt$__sep.*$|$__opt$__sep$__val$__end|" > "$__conf" |
4c3b3608 | 732 | |
733 | elif grep -H -n "^#$__opt$__sep" "$__conf" > /dev/null ; then | |
dceb3aca | 734 | if _contains "$__val" "&" ; then |
4c3b3608 | 735 | __val="$(echo $__val | sed 's/&/\\&/g')" |
736 | fi | |
737 | text="$(cat $__conf)" | |
6dfaaa70 | 738 | echo "$text" | sed "s|^#$__opt$__sep.*$|$__opt$__sep$__val$__end|" > "$__conf" |
4c3b3608 | 739 | |
740 | else | |
a63b05a9 | 741 | _debug2 APP |
4c3b3608 | 742 | echo "$__opt$__sep$__val$__end" >> "$__conf" |
743 | fi | |
744 | _debug "$(grep -H -n "^$__opt$__sep" $__conf)" | |
745 | } | |
746 | ||
747 | #_savedomainconf key value | |
748 | #save to domain.conf | |
749 | _savedomainconf() { | |
750 | key="$1" | |
751 | value="$2" | |
8663fb7e | 752 | if [ "$DOMAIN_CONF" ] ; then |
4d2f38b0 | 753 | _setopt "$DOMAIN_CONF" "$key" "=" "\"$value\"" |
754 | else | |
755 | _err "DOMAIN_CONF is empty, can not save $key=$value" | |
756 | fi | |
757 | } | |
758 | ||
759 | #_cleardomainconf key | |
760 | _cleardomainconf() { | |
761 | key="$1" | |
762 | if [ "$DOMAIN_CONF" ] ; then | |
763 | _sed_i "s/^$key.*$//" "$DOMAIN_CONF" | |
4c3b3608 | 764 | else |
765 | _err "DOMAIN_CONF is empty, can not save $key=$value" | |
766 | fi | |
767 | } | |
768 | ||
769 | #_saveaccountconf key value | |
770 | _saveaccountconf() { | |
771 | key="$1" | |
772 | value="$2" | |
8663fb7e | 773 | if [ "$ACCOUNT_CONF_PATH" ] ; then |
4d2f38b0 | 774 | _setopt "$ACCOUNT_CONF_PATH" "$key" "=" "\"$value\"" |
4c3b3608 | 775 | else |
776 | _err "ACCOUNT_CONF_PATH is empty, can not save $key=$value" | |
777 | fi | |
778 | } | |
779 | ||
780 | _startserver() { | |
781 | content="$1" | |
6fc1447f | 782 | _debug "startserver: $$" |
1b2e940d | 783 | nchelp="$(nc -h 2>&1)" |
850c1128 | 784 | |
399306a1 | 785 | if echo "$nchelp" | grep "\-q[ ,]" >/dev/null ; then |
850c1128 | 786 | _NC="nc -q 1 -l" |
787 | else | |
f76eb452 | 788 | if echo "$nchelp" | grep "GNU netcat" >/dev/null && echo "$nchelp" | grep "\-c, \-\-close" >/dev/null ; then |
789 | _NC="nc -c -l" | |
6d60f288 | 790 | elif echo "$nchelp" | grep "\-N" |grep "Shutdown the network socket after EOF on stdin" >/dev/null ; then |
791 | _NC="nc -N -l" | |
f76eb452 | 792 | else |
793 | _NC="nc -l" | |
794 | fi | |
4c3b3608 | 795 | fi |
1b2e940d | 796 | |
f76eb452 | 797 | _debug "_NC" "$_NC" |
39c8f79f | 798 | _debug Le_HTTPPort "$Le_HTTPPort" |
4c3b3608 | 799 | # while true ; do |
8663fb7e | 800 | if [ "$DEBUG" ] ; then |
2c554a4b | 801 | if ! printf "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC -p $Le_HTTPPort ; then |
802 | printf "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC $Le_HTTPPort ; | |
3aff11f6 | 803 | fi |
4c3b3608 | 804 | else |
2c554a4b | 805 | if ! printf "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC -p $Le_HTTPPort > /dev/null 2>&1; then |
806 | printf "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC $Le_HTTPPort > /dev/null 2>&1 | |
3aff11f6 | 807 | fi |
4c3b3608 | 808 | fi |
8663fb7e | 809 | if [ "$?" != "0" ] ; then |
051c706d | 810 | _err "nc listen error." |
6fc1447f | 811 | exit 1 |
051c706d | 812 | fi |
4c3b3608 | 813 | # done |
814 | } | |
815 | ||
6fc1447f | 816 | _stopserver(){ |
4c3b3608 | 817 | pid="$1" |
6fc1447f | 818 | _debug "pid" "$pid" |
8663fb7e | 819 | if [ -z "$pid" ] ; then |
6fc1447f | 820 | return |
821 | fi | |
822 | ||
3d434e43 | 823 | _get "http://localhost:$Le_HTTPPort" >/dev/null 2>&1 |
4c3b3608 | 824 | |
825 | } | |
826 | ||
827 | _initpath() { | |
828 | ||
8663fb7e | 829 | if [ -z "$LE_WORKING_DIR" ] ; then |
6cc11ffb | 830 | LE_WORKING_DIR=$HOME/.$PROJECT_NAME |
4c3b3608 | 831 | fi |
832 | ||
d53289d7 | 833 | _DEFAULT_ACCOUNT_CONF_PATH="$LE_WORKING_DIR/account.conf" |
834 | ||
8663fb7e | 835 | if [ -z "$ACCOUNT_CONF_PATH" ] ; then |
836 | if [ -f "$_DEFAULT_ACCOUNT_CONF_PATH" ] ; then | |
837 | . "$_DEFAULT_ACCOUNT_CONF_PATH" | |
635695ec | 838 | fi |
d53289d7 | 839 | fi |
840 | ||
8663fb7e | 841 | if [ -z "$ACCOUNT_CONF_PATH" ] ; then |
d53289d7 | 842 | ACCOUNT_CONF_PATH="$_DEFAULT_ACCOUNT_CONF_PATH" |
4c3b3608 | 843 | fi |
844 | ||
8663fb7e | 845 | if [ -f "$ACCOUNT_CONF_PATH" ] ; then |
846 | . "$ACCOUNT_CONF_PATH" | |
4c3b3608 | 847 | fi |
848 | ||
8663fb7e | 849 | if [ "$IN_CRON" ] ; then |
850 | if [ ! "$_USER_PATH_EXPORTED" ] ; then | |
281aa349 | 851 | _USER_PATH_EXPORTED=1 |
852 | export PATH="$USER_PATH:$PATH" | |
853 | fi | |
854 | fi | |
855 | ||
8663fb7e | 856 | if [ -z "$API" ] ; then |
857 | if [ -z "$STAGE" ] ; then | |
4c3b3608 | 858 | API="$DEFAULT_CA" |
859 | else | |
860 | API="$STAGE_CA" | |
861 | _info "Using stage api:$API" | |
862 | fi | |
863 | fi | |
864 | ||
8663fb7e | 865 | if [ -z "$ACME_DIR" ] ; then |
4c3b3608 | 866 | ACME_DIR="/home/.acme" |
867 | fi | |
868 | ||
8663fb7e | 869 | if [ -z "$APACHE_CONF_BACKUP_DIR" ] ; then |
8a144f4d | 870 | APACHE_CONF_BACKUP_DIR="$LE_WORKING_DIR" |
4c3b3608 | 871 | fi |
872 | ||
8663fb7e | 873 | if [ -z "$USER_AGENT" ] ; then |
bbbdcb09 | 874 | USER_AGENT="$DEFAULT_USER_AGENT" |
875 | fi | |
876 | ||
877 | HTTP_HEADER="$LE_WORKING_DIR/http.header" | |
878 | ||
879 | WGET="wget -q" | |
8663fb7e | 880 | if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ] ; then |
bbbdcb09 | 881 | WGET="$WGET -d " |
882 | fi | |
883 | ||
884 | dp="$LE_WORKING_DIR/curl.dump" | |
4a0f23e2 | 885 | CURL="curl -L --silent" |
8663fb7e | 886 | if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ] ; then |
8f48168c | 887 | CURL="$CURL --trace-ascii $dp " |
bbbdcb09 | 888 | fi |
b2817897 | 889 | |
890 | _DEFAULT_ACCOUNT_KEY_PATH="$LE_WORKING_DIR/account.key" | |
8663fb7e | 891 | if [ -z "$ACCOUNT_KEY_PATH" ] ; then |
b2817897 | 892 | ACCOUNT_KEY_PATH="$_DEFAULT_ACCOUNT_KEY_PATH" |
4c3b3608 | 893 | fi |
b2817897 | 894 | |
a79b26af RD |
895 | _DEFAULT_CERT_HOME="$LE_WORKING_DIR" |
896 | if [ -z "$CERT_HOME" ] ; then | |
897 | CERT_HOME="$_DEFAULT_CERT_HOME" | |
898 | fi | |
899 | ||
b2817897 | 900 | domain="$1" |
4c3b3608 | 901 | |
8663fb7e | 902 | if [ -z "$domain" ] ; then |
4c3b3608 | 903 | return 0 |
904 | fi | |
905 | ||
b2817897 | 906 | domainhome="$CERT_HOME/$domain" |
4c3b3608 | 907 | mkdir -p "$domainhome" |
908 | ||
8663fb7e | 909 | if [ -z "$DOMAIN_PATH" ] ; then |
4c3b3608 | 910 | DOMAIN_PATH="$domainhome" |
911 | fi | |
8663fb7e | 912 | if [ -z "$DOMAIN_CONF" ] ; then |
1ad65f7d | 913 | DOMAIN_CONF="$domainhome/$domain.conf" |
4c3b3608 | 914 | fi |
915 | ||
8663fb7e | 916 | if [ -z "$DOMAIN_SSL_CONF" ] ; then |
1ad65f7d | 917 | DOMAIN_SSL_CONF="$domainhome/$domain.ssl.conf" |
4c3b3608 | 918 | fi |
919 | ||
8663fb7e | 920 | if [ -z "$CSR_PATH" ] ; then |
4c3b3608 | 921 | CSR_PATH="$domainhome/$domain.csr" |
922 | fi | |
8663fb7e | 923 | if [ -z "$CERT_KEY_PATH" ] ; then |
4c3b3608 | 924 | CERT_KEY_PATH="$domainhome/$domain.key" |
925 | fi | |
8663fb7e | 926 | if [ -z "$CERT_PATH" ] ; then |
4c3b3608 | 927 | CERT_PATH="$domainhome/$domain.cer" |
928 | fi | |
8663fb7e | 929 | if [ -z "$CA_CERT_PATH" ] ; then |
4c3b3608 | 930 | CA_CERT_PATH="$domainhome/ca.cer" |
931 | fi | |
8663fb7e | 932 | if [ -z "$CERT_FULLCHAIN_PATH" ] ; then |
850db6d4 | 933 | CERT_FULLCHAIN_PATH="$domainhome/fullchain.cer" |
caf1fc10 | 934 | fi |
8663fb7e | 935 | if [ -z "$CERT_PFX_PATH" ] ; then |
ac2d5123 | 936 | CERT_PFX_PATH="$domainhome/$domain.pfx" |
937 | fi | |
4c3b3608 | 938 | } |
939 | ||
940 | ||
941 | _apachePath() { | |
80a0a7b5 | 942 | if ! _exists apachectl ; then |
943 | _err "'apachecrl not found. It seems that apache is not installed, or you are not root user.'" | |
944 | _err "Please use webroot mode to try again." | |
945 | return 1 | |
946 | fi | |
4c3b3608 | 947 | httpdconfname="$(apachectl -V | grep SERVER_CONFIG_FILE= | cut -d = -f 2 | tr -d '"' )" |
78768e98 | 948 | _debug httpdconfname "$httpdconfname" |
dceb3aca | 949 | if _startswith "$httpdconfname" '/' ; then |
d62ee940 | 950 | httpdconf="$httpdconfname" |
c456d954 | 951 | httpdconfname="$(basename $httpdconfname)" |
d62ee940 | 952 | else |
953 | httpdroot="$(apachectl -V | grep HTTPD_ROOT= | cut -d = -f 2 | tr -d '"' )" | |
78768e98 | 954 | _debug httpdroot "$httpdroot" |
d62ee940 | 955 | httpdconf="$httpdroot/$httpdconfname" |
8f63baf7 | 956 | httpdconfname="$(basename $httpdconfname)" |
d62ee940 | 957 | fi |
78768e98 | 958 | _debug httpdconf "$httpdconf" |
8f63baf7 | 959 | _debug httpdconfname "$httpdconfname" |
78768e98 | 960 | if [ ! -f "$httpdconf" ] ; then |
961 | _err "Apache Config file not found" "$httpdconf" | |
4c3b3608 | 962 | return 1 |
963 | fi | |
964 | return 0 | |
965 | } | |
966 | ||
967 | _restoreApache() { | |
8663fb7e | 968 | if [ -z "$usingApache" ] ; then |
4c3b3608 | 969 | return 0 |
970 | fi | |
971 | _initpath | |
972 | if ! _apachePath ; then | |
973 | return 1 | |
974 | fi | |
975 | ||
8663fb7e | 976 | if [ ! -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname" ] ; then |
4c3b3608 | 977 | _debug "No config file to restore." |
978 | return 0 | |
979 | fi | |
980 | ||
ff3bce32 | 981 | cat "$APACHE_CONF_BACKUP_DIR/$httpdconfname" > "$httpdconf" |
5ef501c5 | 982 | _debug "Restored: $httpdconf." |
c083e078 | 983 | if ! apachectl -t >/dev/null 2>&1 ; then |
4c3b3608 | 984 | _err "Sorry, restore apache config error, please contact me." |
985 | return 1; | |
986 | fi | |
5ef501c5 | 987 | _debug "Restored successfully." |
4c3b3608 | 988 | rm -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname" |
989 | return 0 | |
990 | } | |
991 | ||
992 | _setApache() { | |
993 | _initpath | |
994 | if ! _apachePath ; then | |
995 | return 1 | |
996 | fi | |
997 | ||
998 | #backup the conf | |
5778811a | 999 | _debug "Backup apache config file" "$httpdconf" |
8f63baf7 | 1000 | if ! cp "$httpdconf" "$APACHE_CONF_BACKUP_DIR/" ; then |
1001 | _err "Can not backup apache config file, so abort. Don't worry, your apache config is not changed." | |
1002 | _err "This might be a bug of $PROJECT_NAME , pleae report issue: $PROJECT" | |
1003 | return 1 | |
1004 | fi | |
4c3b3608 | 1005 | _info "JFYI, Config file $httpdconf is backuped to $APACHE_CONF_BACKUP_DIR/$httpdconfname" |
1006 | _info "In case there is an error that can not be restored automatically, you may try restore it yourself." | |
1007 | _info "The backup file will be deleted on sucess, just forget it." | |
1008 | ||
1009 | #add alias | |
b09d597c | 1010 | |
1011 | apacheVer="$(apachectl -V | grep "Server version:" | cut -d : -f 2 | cut -d " " -f 2 | cut -d '/' -f 2 )" | |
1012 | _debug "apacheVer" "$apacheVer" | |
1013 | apacheMajer="$(echo "$apacheVer" | cut -d . -f 1)" | |
1014 | apacheMinor="$(echo "$apacheVer" | cut -d . -f 2)" | |
1015 | ||
5778811a | 1016 | if [ "$apacheVer" ] && [ "$apacheMajer$apacheMinor" -ge "24" ] ; then |
b09d597c | 1017 | echo " |
4c3b3608 | 1018 | Alias /.well-known/acme-challenge $ACME_DIR |
1019 | ||
1020 | <Directory $ACME_DIR > | |
1021 | Require all granted | |
b09d597c | 1022 | </Directory> |
5778811a | 1023 | " >> "$httpdconf" |
b09d597c | 1024 | else |
1025 | echo " | |
1026 | Alias /.well-known/acme-challenge $ACME_DIR | |
1027 | ||
1028 | <Directory $ACME_DIR > | |
1029 | Order allow,deny | |
1030 | Allow from all | |
4c3b3608 | 1031 | </Directory> |
5778811a | 1032 | " >> "$httpdconf" |
b09d597c | 1033 | fi |
1034 | ||
4c3b3608 | 1035 | |
c083e078 | 1036 | if ! apachectl -t >/dev/null 2>&1; then |
4c3b3608 | 1037 | _err "Sorry, apache config error, please contact me." |
1038 | _restoreApache | |
1039 | return 1; | |
1040 | fi | |
1041 | ||
8663fb7e | 1042 | if [ ! -d "$ACME_DIR" ] ; then |
4c3b3608 | 1043 | mkdir -p "$ACME_DIR" |
1044 | chmod 755 "$ACME_DIR" | |
1045 | fi | |
1046 | ||
1047 | if ! apachectl graceful ; then | |
1048 | _err "Sorry, apachectl graceful error, please contact me." | |
1049 | _restoreApache | |
1050 | return 1; | |
1051 | fi | |
1052 | usingApache="1" | |
1053 | return 0 | |
1054 | } | |
1055 | ||
5ef501c5 | 1056 | _clearup() { |
4c3b3608 | 1057 | _stopserver $serverproc |
1058 | serverproc="" | |
1059 | _restoreApache | |
1060 | } | |
1061 | ||
1062 | # webroot removelevel tokenfile | |
1063 | _clearupwebbroot() { | |
1064 | __webroot="$1" | |
8663fb7e | 1065 | if [ -z "$__webroot" ] ; then |
4c3b3608 | 1066 | _debug "no webroot specified, skip" |
1067 | return 0 | |
1068 | fi | |
1069 | ||
8663fb7e | 1070 | if [ "$2" = '1' ] ; then |
4c3b3608 | 1071 | _debug "remove $__webroot/.well-known" |
1072 | rm -rf "$__webroot/.well-known" | |
8663fb7e | 1073 | elif [ "$2" = '2' ] ; then |
4c3b3608 | 1074 | _debug "remove $__webroot/.well-known/acme-challenge" |
1075 | rm -rf "$__webroot/.well-known/acme-challenge" | |
8663fb7e | 1076 | elif [ "$2" = '3' ] ; then |
4c3b3608 | 1077 | _debug "remove $__webroot/.well-known/acme-challenge/$3" |
1078 | rm -rf "$__webroot/.well-known/acme-challenge/$3" | |
1079 | else | |
1080 | _info "Skip for removelevel:$2" | |
1081 | fi | |
1082 | ||
1083 | return 0 | |
1084 | ||
1085 | } | |
1086 | ||
1087 | issue() { | |
8663fb7e | 1088 | if [ -z "$2" ] ; then |
a7b7355d | 1089 | echo "Usage: $PROJECT_ENTRY --issue -d a.com -w /path/to/webroot/a.com/ " |
4c3b3608 | 1090 | return 1 |
1091 | fi | |
1092 | Le_Webroot="$1" | |
1093 | Le_Domain="$2" | |
1094 | Le_Alt="$3" | |
1095 | Le_Keylength="$4" | |
1096 | Le_RealCertPath="$5" | |
1097 | Le_RealKeyPath="$6" | |
1098 | Le_RealCACertPath="$7" | |
1099 | Le_ReloadCmd="$8" | |
a63b05a9 | 1100 | Le_RealFullChainPath="$9" |
4c3b3608 | 1101 | |
eccec5f6 | 1102 | #remove these later. |
8663fb7e | 1103 | if [ "$Le_Webroot" = "dns-cf" ] ; then |
eccec5f6 | 1104 | Le_Webroot="dns_cf" |
1105 | fi | |
8663fb7e | 1106 | if [ "$Le_Webroot" = "dns-dp" ] ; then |
eccec5f6 | 1107 | Le_Webroot="dns_dp" |
1108 | fi | |
8663fb7e | 1109 | if [ "$Le_Webroot" = "dns-cx" ] ; then |
eccec5f6 | 1110 | Le_Webroot="dns_cx" |
1111 | fi | |
4c3b3608 | 1112 | |
eccec5f6 | 1113 | _initpath $Le_Domain |
1114 | ||
8663fb7e | 1115 | if [ -f "$DOMAIN_CONF" ] ; then |
a4270efa | 1116 | Le_NextRenewTime=$(grep "^Le_NextRenewTime=" "$DOMAIN_CONF" | cut -d '=' -f 2 | tr -d "'\"") |
1117 | _debug Le_NextRenewTime "$Le_NextRenewTime" | |
1118 | if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ $(date -u "+%s" ) -lt $Le_NextRenewTime ] ; then | |
4c3b3608 | 1119 | _info "Skip, Next renewal time is: $(grep "^Le_NextRenewTimeStr" "$DOMAIN_CONF" | cut -d '=' -f 2)" |
1120 | return 2 | |
1121 | fi | |
1122 | fi | |
96a46cfc | 1123 | |
4d2f38b0 | 1124 | _savedomainconf "Le_Domain" "$Le_Domain" |
1125 | _savedomainconf "Le_Alt" "$Le_Alt" | |
1126 | _savedomainconf "Le_Webroot" "$Le_Webroot" | |
1127 | _savedomainconf "Le_Keylength" "$Le_Keylength" | |
4c3b3608 | 1128 | |
8663fb7e | 1129 | if [ "$Le_Alt" = "no" ] ; then |
4c3b3608 | 1130 | Le_Alt="" |
1131 | fi | |
8663fb7e | 1132 | if [ "$Le_Keylength" = "no" ] ; then |
4c3b3608 | 1133 | Le_Keylength="" |
1134 | fi | |
4c3b3608 | 1135 | |
c53da1ef | 1136 | if _hasfield "$Le_Webroot" "no" ; then |
4c3b3608 | 1137 | _info "Standalone mode." |
5ef501c5 | 1138 | if ! _exists "nc" ; then |
4c3b3608 | 1139 | _err "Please install netcat(nc) tools first." |
1140 | return 1 | |
1141 | fi | |
1142 | ||
8663fb7e | 1143 | if [ -z "$Le_HTTPPort" ] ; then |
4c3b3608 | 1144 | Le_HTTPPort=80 |
1145 | fi | |
4d2f38b0 | 1146 | _savedomainconf "Le_HTTPPort" "$Le_HTTPPort" |
4c3b3608 | 1147 | |
251fc37c | 1148 | netprc="$(_ss "$Le_HTTPPort" | grep "$Le_HTTPPort")" |
8663fb7e | 1149 | if [ "$netprc" ] ; then |
4c3b3608 | 1150 | _err "$netprc" |
1151 | _err "tcp port $Le_HTTPPort is already used by $(echo "$netprc" | cut -d : -f 4)" | |
1152 | _err "Please stop it first" | |
1153 | return 1 | |
1154 | fi | |
1155 | fi | |
1156 | ||
c53da1ef | 1157 | if _hasfield "$Le_Webroot" "apache" ; then |
4c3b3608 | 1158 | if ! _setApache ; then |
1159 | _err "set up apache error. Report error to me." | |
1160 | return 1 | |
1161 | fi | |
4c3b3608 | 1162 | else |
1163 | usingApache="" | |
1164 | fi | |
1165 | ||
8663fb7e | 1166 | if [ ! -f "$ACCOUNT_KEY_PATH" ] ; then |
41e3eafa | 1167 | if ! createAccountKey $Le_Domain $Le_Keylength ; then |
1168 | _err "Create account key error." | |
8663fb7e | 1169 | if [ "$usingApache" ] ; then |
5ef501c5 | 1170 | _restoreApache |
1171 | fi | |
41e3eafa | 1172 | return 1 |
1173 | fi | |
1174 | fi | |
1175 | ||
166096dc | 1176 | if ! _calcjwk "$ACCOUNT_KEY_PATH" ; then |
8663fb7e | 1177 | if [ "$usingApache" ] ; then |
5ef501c5 | 1178 | _restoreApache |
1179 | fi | |
166096dc | 1180 | return 1 |
1181 | fi | |
1182 | ||
1183 | accountkey_json=$(echo -n "$jwk" | tr -d ' ' ) | |
88fab7d6 | 1184 | thumbprint=$(echo -n "$accountkey_json" | _digest "sha256" | _urlencode) |
4c3b3608 | 1185 | |
88fab7d6 | 1186 | accountkeyhash="$(cat "$ACCOUNT_KEY_PATH" | _digest "sha256" )" |
a63b05a9 | 1187 | accountkeyhash="$(echo $accountkeyhash$API | _digest "sha256" )" |
8663fb7e | 1188 | if [ "$accountkeyhash" != "$ACCOUNT_KEY_HASH" ] ; then |
166096dc | 1189 | _info "Registering account" |
1190 | regjson='{"resource": "new-reg", "agreement": "'$AGREEMENT'"}' | |
8663fb7e | 1191 | if [ "$ACCOUNT_EMAIL" ] ; then |
166096dc | 1192 | regjson='{"resource": "new-reg", "contact": ["mailto: '$ACCOUNT_EMAIL'"], "agreement": "'$AGREEMENT'"}' |
1193 | fi | |
1194 | _send_signed_request "$API/acme/new-reg" "$regjson" | |
1195 | ||
8663fb7e | 1196 | if [ "$code" = "" ] || [ "$code" = '201' ] ; then |
166096dc | 1197 | _info "Registered" |
1198 | echo $response > $LE_WORKING_DIR/account.json | |
8663fb7e | 1199 | elif [ "$code" = '409' ] ; then |
166096dc | 1200 | _info "Already registered" |
1201 | else | |
1202 | _err "Register account Error: $response" | |
1203 | _clearup | |
1204 | return 1 | |
1205 | fi | |
1206 | ACCOUNT_KEY_HASH="$accountkeyhash" | |
1207 | _saveaccountconf "ACCOUNT_KEY_HASH" "$ACCOUNT_KEY_HASH" | |
1208 | else | |
1209 | _info "Skip register account key" | |
1210 | fi | |
1211 | ||
8663fb7e | 1212 | if [ ! -f "$CERT_KEY_PATH" ] ; then |
41e3eafa | 1213 | if ! createDomainKey $Le_Domain $Le_Keylength ; then |
1214 | _err "Create domain key error." | |
5ef501c5 | 1215 | _clearup |
41e3eafa | 1216 | return 1 |
1217 | fi | |
4c3b3608 | 1218 | fi |
1219 | ||
1220 | if ! createCSR $Le_Domain $Le_Alt ; then | |
1221 | _err "Create CSR error." | |
5ef501c5 | 1222 | _clearup |
4c3b3608 | 1223 | return 1 |
1224 | fi | |
a63b05a9 | 1225 | |
4c3b3608 | 1226 | vlist="$Le_Vlist" |
1227 | # verify each domain | |
1228 | _info "Verify each domain" | |
1229 | sep='#' | |
8663fb7e | 1230 | if [ -z "$vlist" ] ; then |
4c3b3608 | 1231 | alldomains=$(echo "$Le_Domain,$Le_Alt" | tr ',' ' ' ) |
a63b05a9 | 1232 | _index=1 |
1233 | _currentRoot="" | |
4c3b3608 | 1234 | for d in $alldomains |
a63b05a9 | 1235 | do |
1236 | _info "Getting webroot for domain" $d | |
1237 | _w="$(echo $Le_Webroot | cut -d , -f $_index)" | |
1238 | _debug _w "$_w" | |
8663fb7e | 1239 | if [ "$_w" ] ; then |
a63b05a9 | 1240 | _currentRoot="$_w" |
1241 | fi | |
1242 | _debug "_currentRoot" "$_currentRoot" | |
00a50605 | 1243 | _index=$(_math $_index + 1) |
a63b05a9 | 1244 | |
1245 | vtype="$VTYPE_HTTP" | |
dceb3aca | 1246 | if _startswith "$_currentRoot" "dns" ; then |
a63b05a9 | 1247 | vtype="$VTYPE_DNS" |
1248 | fi | |
4c3b3608 | 1249 | _info "Getting token for domain" $d |
c4d8fd83 | 1250 | |
1251 | if ! _send_signed_request "$API/acme/new-authz" "{\"resource\": \"new-authz\", \"identifier\": {\"type\": \"dns\", \"value\": \"$d\"}}" ; then | |
1252 | _err "Can not get domain token." | |
1253 | _clearup | |
1254 | return 1 | |
1255 | fi | |
1256 | ||
8663fb7e | 1257 | if [ ! -z "$code" ] && [ ! "$code" = '201' ] ; then |
4c3b3608 | 1258 | _err "new-authz error: $response" |
1259 | _clearup | |
1260 | return 1 | |
1261 | fi | |
1262 | ||
230234e7 | 1263 | entry="$(printf "$response" | egrep -o '\{[^{]*"type":"'$vtype'"[^}]*')" |
4c3b3608 | 1264 | _debug entry "$entry" |
19539575 | 1265 | if [ -z "$entry" ] ; then |
1266 | _err "Error, can not get domain token $d" | |
1267 | _clearup | |
1268 | return 1 | |
1269 | fi | |
4c3b3608 | 1270 | token="$(printf "$entry" | egrep -o '"token":"[^"]*' | cut -d : -f 2 | tr -d '"')" |
1271 | _debug token $token | |
1272 | ||
1273 | uri="$(printf "$entry" | egrep -o '"uri":"[^"]*'| cut -d : -f 2,3 | tr -d '"' )" | |
1274 | _debug uri $uri | |
1275 | ||
1276 | keyauthorization="$token.$thumbprint" | |
1277 | _debug keyauthorization "$keyauthorization" | |
1278 | ||
a63b05a9 | 1279 | dvlist="$d$sep$keyauthorization$sep$uri$sep$vtype$sep$_currentRoot" |
4c3b3608 | 1280 | _debug dvlist "$dvlist" |
1281 | ||
1282 | vlist="$vlist$dvlist," | |
1283 | ||
1284 | done | |
1285 | ||
1286 | #add entry | |
1287 | dnsadded="" | |
1288 | ventries=$(echo "$vlist" | tr ',' ' ' ) | |
1289 | for ventry in $ventries | |
1290 | do | |
1291 | d=$(echo $ventry | cut -d $sep -f 1) | |
1292 | keyauthorization=$(echo $ventry | cut -d $sep -f 2) | |
a63b05a9 | 1293 | vtype=$(echo $ventry | cut -d $sep -f 4) |
1294 | _currentRoot=$(echo $ventry | cut -d $sep -f 5) | |
8663fb7e | 1295 | if [ "$vtype" = "$VTYPE_DNS" ] ; then |
4c3b3608 | 1296 | dnsadded='0' |
1297 | txtdomain="_acme-challenge.$d" | |
1298 | _debug txtdomain "$txtdomain" | |
c1c7d87b | 1299 | txt="$(echo -n $keyauthorization | _digest "sha256" | _urlencode)" |
4c3b3608 | 1300 | _debug txt "$txt" |
1301 | #dns | |
1302 | #1. check use api | |
1303 | d_api="" | |
8663fb7e | 1304 | if [ -f "$LE_WORKING_DIR/$d/$_currentRoot" ] ; then |
a63b05a9 | 1305 | d_api="$LE_WORKING_DIR/$d/$_currentRoot" |
8663fb7e | 1306 | elif [ -f "$LE_WORKING_DIR/$d/$_currentRoot.sh" ] ; then |
a63b05a9 | 1307 | d_api="$LE_WORKING_DIR/$d/$_currentRoot.sh" |
8663fb7e | 1308 | elif [ -f "$LE_WORKING_DIR/$_currentRoot" ] ; then |
a63b05a9 | 1309 | d_api="$LE_WORKING_DIR/$_currentRoot" |
8663fb7e | 1310 | elif [ -f "$LE_WORKING_DIR/$_currentRoot.sh" ] ; then |
a63b05a9 | 1311 | d_api="$LE_WORKING_DIR/$_currentRoot.sh" |
8663fb7e | 1312 | elif [ -f "$LE_WORKING_DIR/dnsapi/$_currentRoot" ] ; then |
a63b05a9 | 1313 | d_api="$LE_WORKING_DIR/dnsapi/$_currentRoot" |
8663fb7e | 1314 | elif [ -f "$LE_WORKING_DIR/dnsapi/$_currentRoot.sh" ] ; then |
a63b05a9 | 1315 | d_api="$LE_WORKING_DIR/dnsapi/$_currentRoot.sh" |
4c3b3608 | 1316 | fi |
1317 | _debug d_api "$d_api" | |
1318 | ||
8663fb7e | 1319 | if [ "$d_api" ] ; then |
4c3b3608 | 1320 | _info "Found domain api file: $d_api" |
1321 | else | |
1322 | _err "Add the following TXT record:" | |
1323 | _err "Domain: $txtdomain" | |
1324 | _err "TXT value: $txt" | |
1325 | _err "Please be aware that you prepend _acme-challenge. before your domain" | |
1326 | _err "so the resulting subdomain will be: $txtdomain" | |
1327 | continue | |
1328 | fi | |
4c3b3608 | 1329 | |
73b8b120 | 1330 | ( |
8663fb7e | 1331 | if ! . $d_api ; then |
73b8b120 | 1332 | _err "Load file $d_api error. Please check your api file and try again." |
1333 | return 1 | |
1334 | fi | |
1335 | ||
158f22f7 | 1336 | addcommand="${_currentRoot}_add" |
d53289d7 | 1337 | if ! _exists $addcommand ; then |
73b8b120 | 1338 | _err "It seems that your api file is not correct, it must have a function named: $addcommand" |
1339 | return 1 | |
1340 | fi | |
1341 | ||
1342 | if ! $addcommand $txtdomain $txt ; then | |
1343 | _err "Error add txt for domain:$txtdomain" | |
1344 | return 1 | |
1345 | fi | |
1346 | ) | |
4c3b3608 | 1347 | |
8663fb7e | 1348 | if [ "$?" != "0" ] ; then |
5ef501c5 | 1349 | _clearup |
4c3b3608 | 1350 | return 1 |
1351 | fi | |
1352 | dnsadded='1' | |
1353 | fi | |
1354 | done | |
1355 | ||
8663fb7e | 1356 | if [ "$dnsadded" = '0' ] ; then |
4d2f38b0 | 1357 | _savedomainconf "Le_Vlist" "$vlist" |
4c3b3608 | 1358 | _debug "Dns record not added yet, so, save to $DOMAIN_CONF and exit." |
1359 | _err "Please add the TXT records to the domains, and retry again." | |
5ef501c5 | 1360 | _clearup |
4c3b3608 | 1361 | return 1 |
1362 | fi | |
1363 | ||
1364 | fi | |
1365 | ||
8663fb7e | 1366 | if [ "$dnsadded" = '1' ] ; then |
4c3b3608 | 1367 | _info "Sleep 60 seconds for the txt records to take effect" |
1368 | sleep 60 | |
1369 | fi | |
1370 | ||
1371 | _debug "ok, let's start to verify" | |
a63b05a9 | 1372 | |
4c3b3608 | 1373 | ventries=$(echo "$vlist" | tr ',' ' ' ) |
1374 | for ventry in $ventries | |
1375 | do | |
1376 | d=$(echo $ventry | cut -d $sep -f 1) | |
1377 | keyauthorization=$(echo $ventry | cut -d $sep -f 2) | |
1378 | uri=$(echo $ventry | cut -d $sep -f 3) | |
a63b05a9 | 1379 | vtype=$(echo $ventry | cut -d $sep -f 4) |
1380 | _currentRoot=$(echo $ventry | cut -d $sep -f 5) | |
4c3b3608 | 1381 | _info "Verifying:$d" |
1382 | _debug "d" "$d" | |
1383 | _debug "keyauthorization" "$keyauthorization" | |
1384 | _debug "uri" "$uri" | |
1385 | removelevel="" | |
1386 | token="" | |
a63b05a9 | 1387 | |
1388 | _debug "_currentRoot" "$_currentRoot" | |
1389 | ||
1390 | ||
8663fb7e | 1391 | if [ "$vtype" = "$VTYPE_HTTP" ] ; then |
1392 | if [ "$_currentRoot" = "no" ] ; then | |
4c3b3608 | 1393 | _info "Standalone mode server" |
1394 | _startserver "$keyauthorization" & | |
8663fb7e | 1395 | if [ "$?" != "0" ] ; then |
5ef501c5 | 1396 | _clearup |
6fc1447f | 1397 | return 1 |
1398 | fi | |
4c3b3608 | 1399 | serverproc="$!" |
1400 | sleep 2 | |
1401 | _debug serverproc $serverproc | |
6fc1447f | 1402 | |
4c3b3608 | 1403 | else |
8663fb7e | 1404 | if [ "$_currentRoot" = "apache" ] ; then |
6f930641 | 1405 | wellknown_path="$ACME_DIR" |
1406 | else | |
a63b05a9 | 1407 | wellknown_path="$_currentRoot/.well-known/acme-challenge" |
8663fb7e | 1408 | if [ ! -d "$_currentRoot/.well-known" ] ; then |
6f930641 | 1409 | removelevel='1' |
8663fb7e | 1410 | elif [ ! -d "$_currentRoot/.well-known/acme-challenge" ] ; then |
6f930641 | 1411 | removelevel='2' |
1412 | else | |
1413 | removelevel='3' | |
1414 | fi | |
4c3b3608 | 1415 | fi |
6f930641 | 1416 | |
4c3b3608 | 1417 | _debug wellknown_path "$wellknown_path" |
6f930641 | 1418 | |
7939b419 | 1419 | token="$(printf "%s" "$keyauthorization" | cut -d '.' -f 1)" |
4c3b3608 | 1420 | _debug "writing token:$token to $wellknown_path/$token" |
1421 | ||
1422 | mkdir -p "$wellknown_path" | |
7939b419 | 1423 | printf "%s" "$keyauthorization" > "$wellknown_path/$token" |
8663fb7e | 1424 | if [ ! "$usingApache" ] ; then |
a63b05a9 | 1425 | webroot_owner=$(_stat $_currentRoot) |
df886ffa | 1426 | _debug "Changing owner/group of .well-known to $webroot_owner" |
a63b05a9 | 1427 | chown -R $webroot_owner "$_currentRoot/.well-known" |
df886ffa | 1428 | fi |
4c3b3608 | 1429 | |
1430 | fi | |
1431 | fi | |
1432 | ||
c4d8fd83 | 1433 | if ! _send_signed_request $uri "{\"resource\": \"challenge\", \"keyAuthorization\": \"$keyauthorization\"}" ; then |
1434 | _err "$d:Can not get challenge: $response" | |
1435 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" | |
1436 | _clearup | |
1437 | return 1 | |
1438 | fi | |
4c3b3608 | 1439 | |
8663fb7e | 1440 | if [ ! -z "$code" ] && [ ! "$code" = '202' ] ; then |
c60883ef | 1441 | _err "$d:Challenge error: $response" |
a63b05a9 | 1442 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" |
4c3b3608 | 1443 | _clearup |
1444 | return 1 | |
1445 | fi | |
1446 | ||
6fc1447f | 1447 | waittimes=0 |
8663fb7e | 1448 | if [ -z "$MAX_RETRY_TIMES" ] ; then |
6fc1447f | 1449 | MAX_RETRY_TIMES=30 |
1450 | fi | |
1451 | ||
2ee5d873 | 1452 | while true ; do |
00a50605 | 1453 | waittimes=$(_math $waittimes + 1) |
8663fb7e | 1454 | if [ "$waittimes" -ge "$MAX_RETRY_TIMES" ] ; then |
6fc1447f | 1455 | _err "$d:Timeout" |
1456 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" | |
1457 | _clearup | |
1458 | return 1 | |
1459 | fi | |
1460 | ||
4c3b3608 | 1461 | _debug "sleep 5 secs to verify" |
1462 | sleep 5 | |
1463 | _debug "checking" | |
9aaf36cd | 1464 | response="$(_get $uri)" |
8663fb7e | 1465 | if [ "$?" != "0" ] ; then |
c60883ef | 1466 | _err "$d:Verify error:$response" |
a63b05a9 | 1467 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" |
4c3b3608 | 1468 | _clearup |
1469 | return 1 | |
1470 | fi | |
9aaf36cd | 1471 | _debug2 original "$response" |
1472 | ||
1473 | response="$(echo "$response" | _normalizeJson )" | |
7012b91f | 1474 | _debug2 response "$response" |
4c3b3608 | 1475 | |
7d91e35f | 1476 | status=$(echo $response | egrep -o '"status":"[^"]*' | cut -d : -f 2 | tr -d '"') |
8663fb7e | 1477 | if [ "$status" = "valid" ] ; then |
4c3b3608 | 1478 | _info "Success" |
1479 | _stopserver $serverproc | |
1480 | serverproc="" | |
a63b05a9 | 1481 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" |
4c3b3608 | 1482 | break; |
1483 | fi | |
1484 | ||
8663fb7e | 1485 | if [ "$status" = "invalid" ] ; then |
6e180343 | 1486 | error="$(echo $response | tr -d "\r\n" | egrep -o '"error":{[^}]*}')" |
b7ec6789 | 1487 | _debug2 error "$error" |
1488 | errordetail="$(echo $error | grep -o '"detail": *"[^"]*"' | cut -d '"' -f 4)" | |
1489 | _debug2 errordetail "$errordetail" | |
1490 | if [ "$errordetail" ] ; then | |
1491 | _err "$d:Verify error:$errordetail" | |
1492 | else | |
1493 | _err "$d:Verify error:$error" | |
1494 | fi | |
a63b05a9 | 1495 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" |
4c3b3608 | 1496 | _clearup |
1497 | return 1; | |
1498 | fi | |
1499 | ||
8663fb7e | 1500 | if [ "$status" = "pending" ] ; then |
4c3b3608 | 1501 | _info "Pending" |
1502 | else | |
1503 | _err "$d:Verify error:$response" | |
a63b05a9 | 1504 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" |
4c3b3608 | 1505 | _clearup |
1506 | return 1 | |
1507 | fi | |
1508 | ||
1509 | done | |
1510 | ||
1511 | done | |
1512 | ||
1513 | _clearup | |
1514 | _info "Verify finished, start to sign." | |
fa8311dc | 1515 | der="$(_getfile "${CSR_PATH}" "${BEGIN_CSR}" "${END_CSR}" | tr -d "\r\n" | _urlencode)" |
c4d8fd83 | 1516 | |
1517 | if ! _send_signed_request "$API/acme/new-cert" "{\"resource\": \"new-cert\", \"csr\": \"$der\"}" "needbase64" ; then | |
1518 | _err "Sign failed." | |
1519 | return 1 | |
1520 | fi | |
4c3b3608 | 1521 | |
1522 | ||
bbbdcb09 | 1523 | Le_LinkCert="$(grep -i -o '^Location.*$' $HTTP_HEADER | head -1 | tr -d "\r\n" | cut -d " " -f 2)" |
4d2f38b0 | 1524 | _savedomainconf "Le_LinkCert" "$Le_LinkCert" |
4c3b3608 | 1525 | |
8663fb7e | 1526 | if [ "$Le_LinkCert" ] ; then |
88fab7d6 | 1527 | echo "$BEGIN_CERT" > "$CERT_PATH" |
c60883ef | 1528 | _get "$Le_LinkCert" | _base64 "multiline" >> "$CERT_PATH" |
88fab7d6 | 1529 | echo "$END_CERT" >> "$CERT_PATH" |
4c3b3608 | 1530 | _info "Cert success." |
1531 | cat "$CERT_PATH" | |
1532 | ||
1533 | _info "Your cert is in $CERT_PATH" | |
caf1fc10 | 1534 | cp "$CERT_PATH" "$CERT_FULLCHAIN_PATH" |
281aa349 | 1535 | |
8663fb7e | 1536 | if [ ! "$USER_PATH" ] || [ ! "$IN_CRON" ] ; then |
281aa349 | 1537 | USER_PATH="$PATH" |
1538 | _saveaccountconf "USER_PATH" "$USER_PATH" | |
1539 | fi | |
4c3b3608 | 1540 | fi |
1541 | ||
1542 | ||
8663fb7e | 1543 | if [ -z "$Le_LinkCert" ] ; then |
eae29099 | 1544 | response="$(echo $response | _dbase64 "multiline" | _normalizeJson )" |
4c3b3608 | 1545 | _err "Sign failed: $(echo "$response" | grep -o '"detail":"[^"]*"')" |
1546 | return 1 | |
1547 | fi | |
1548 | ||
4d2f38b0 | 1549 | _cleardomainconf "Le_Vlist" |
4c3b3608 | 1550 | |
bbbdcb09 | 1551 | Le_LinkIssuer=$(grep -i '^Link' $HTTP_HEADER | head -1 | cut -d " " -f 2| cut -d ';' -f 1 | tr -d '<>' ) |
4d2f38b0 | 1552 | _savedomainconf "Le_LinkIssuer" "$Le_LinkIssuer" |
4c3b3608 | 1553 | |
8663fb7e | 1554 | if [ "$Le_LinkIssuer" ] ; then |
88fab7d6 | 1555 | echo "$BEGIN_CERT" > "$CA_CERT_PATH" |
c60883ef | 1556 | _get "$Le_LinkIssuer" | _base64 "multiline" >> "$CA_CERT_PATH" |
88fab7d6 | 1557 | echo "$END_CERT" >> "$CA_CERT_PATH" |
4c3b3608 | 1558 | _info "The intermediate CA cert is in $CA_CERT_PATH" |
caf1fc10 | 1559 | cat "$CA_CERT_PATH" >> "$CERT_FULLCHAIN_PATH" |
1560 | _info "And the full chain certs is there: $CERT_FULLCHAIN_PATH" | |
4c3b3608 | 1561 | fi |
1562 | ||
1563 | Le_CertCreateTime=$(date -u "+%s") | |
4d2f38b0 | 1564 | _savedomainconf "Le_CertCreateTime" "$Le_CertCreateTime" |
4c3b3608 | 1565 | |
1566 | Le_CertCreateTimeStr=$(date -u ) | |
4d2f38b0 | 1567 | _savedomainconf "Le_CertCreateTimeStr" "$Le_CertCreateTimeStr" |
4c3b3608 | 1568 | |
8663fb7e | 1569 | if [ -z "$Le_RenewalDays" ] || [ "$Le_RenewalDays" -lt "0" ] || [ "$Le_RenewalDays" -gt "80" ] ; then |
4c3b3608 | 1570 | Le_RenewalDays=80 |
1571 | fi | |
1572 | ||
4d2f38b0 | 1573 | _savedomainconf "Le_RenewalDays" "$Le_RenewalDays" |
00a50605 | 1574 | |
1575 | Le_NextRenewTime=$(_math $Le_CertCreateTime + $Le_RenewalDays \* 24 \* 60 \* 60) | |
4d2f38b0 | 1576 | _savedomainconf "Le_NextRenewTime" "$Le_NextRenewTime" |
4c3b3608 | 1577 | |
1578 | Le_NextRenewTimeStr=$( _time2str $Le_NextRenewTime ) | |
4d2f38b0 | 1579 | _savedomainconf "Le_NextRenewTimeStr" "$Le_NextRenewTimeStr" |
4c3b3608 | 1580 | |
1581 | ||
01f54558 | 1582 | _output="$(installcert $Le_Domain "$Le_RealCertPath" "$Le_RealKeyPath" "$Le_RealCACertPath" "$Le_ReloadCmd" "$Le_RealFullChainPath" 2>&1)" |
1583 | _ret="$?" | |
1584 | if [ "$_ret" = "9" ] ; then | |
ca2a96b3 | 1585 | #ignore the empty install error. |
1586 | return 0 | |
1587 | fi | |
01f54558 | 1588 | if [ "$_ret" != "0" ] ; then |
1589 | _err "$_output" | |
1590 | return 1 | |
1591 | fi | |
4c3b3608 | 1592 | } |
1593 | ||
1594 | renew() { | |
1595 | Le_Domain="$1" | |
8663fb7e | 1596 | if [ -z "$Le_Domain" ] ; then |
a7b7355d | 1597 | _err "Usage: $PROJECT_ENTRY --renew -d domain.com" |
4c3b3608 | 1598 | return 1 |
1599 | fi | |
1600 | ||
1601 | _initpath $Le_Domain | |
1602 | ||
8663fb7e | 1603 | if [ ! -f "$DOMAIN_CONF" ] ; then |
4c3b3608 | 1604 | _info "$Le_Domain is not a issued domain, skip." |
1605 | return 0; | |
1606 | fi | |
1607 | ||
8663fb7e | 1608 | . "$DOMAIN_CONF" |
1609 | if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(date -u "+%s" )" -lt "$Le_NextRenewTime" ] ; then | |
4c3b3608 | 1610 | _info "Skip, Next renewal time is: $Le_NextRenewTimeStr" |
1611 | return 2 | |
1612 | fi | |
1613 | ||
1614 | IS_RENEW="1" | |
a63b05a9 | 1615 | issue "$Le_Webroot" "$Le_Domain" "$Le_Alt" "$Le_Keylength" "$Le_RealCertPath" "$Le_RealKeyPath" "$Le_RealCACertPath" "$Le_ReloadCmd" "$Le_RealFullChainPath" |
4c3b3608 | 1616 | local res=$? |
1617 | IS_RENEW="" | |
1618 | ||
1619 | return $res | |
1620 | } | |
1621 | ||
1622 | renewAll() { | |
1623 | _initpath | |
b2817897 | 1624 | for d in $(ls -F ${CERT_HOME}/ | grep [^.].*[.].*/$ ) ; do |
4c3b3608 | 1625 | d=$(echo $d | cut -d '/' -f 1) |
4d2f38b0 | 1626 | ( |
1627 | _info "Renew: $d" | |
1628 | renew "$d" | |
1629 | ) | |
4c3b3608 | 1630 | done |
1631 | ||
1632 | } | |
1633 | ||
1634 | installcert() { | |
1635 | Le_Domain="$1" | |
8663fb7e | 1636 | if [ -z "$Le_Domain" ] ; then |
a7b7355d | 1637 | echo "Usage: $PROJECT_ENTRY --installcert -d domain.com [--certpath cert-file-path] [--keypath key-file-path] [--capath ca-cert-file-path] [ --reloadCmd reloadCmd] [--fullchainpath fullchain-path]" |
4c3b3608 | 1638 | return 1 |
1639 | fi | |
1640 | ||
1641 | Le_RealCertPath="$2" | |
1642 | Le_RealKeyPath="$3" | |
1643 | Le_RealCACertPath="$4" | |
1644 | Le_ReloadCmd="$5" | |
a63b05a9 | 1645 | Le_RealFullChainPath="$6" |
4c3b3608 | 1646 | |
1647 | _initpath $Le_Domain | |
1648 | ||
4d2f38b0 | 1649 | _savedomainconf "Le_RealCertPath" "$Le_RealCertPath" |
1650 | _savedomainconf "Le_RealCACertPath" "$Le_RealCACertPath" | |
1651 | _savedomainconf "Le_RealKeyPath" "$Le_RealKeyPath" | |
1652 | _savedomainconf "Le_ReloadCmd" "$Le_ReloadCmd" | |
1653 | _savedomainconf "Le_RealFullChainPath" "$Le_RealFullChainPath" | |
4c3b3608 | 1654 | |
4d2f38b0 | 1655 | if [ "$Le_RealCertPath" = "no" ] ; then |
1656 | Le_RealCertPath="" | |
1657 | fi | |
1658 | if [ "$Le_RealKeyPath" = "no" ] ; then | |
1659 | Le_RealKeyPath="" | |
1660 | fi | |
1661 | if [ "$Le_RealCACertPath" = "no" ] ; then | |
1662 | Le_RealCACertPath="" | |
1663 | fi | |
1664 | if [ "$Le_ReloadCmd" = "no" ] ; then | |
1665 | Le_ReloadCmd="" | |
1666 | fi | |
1667 | if [ "$Le_RealFullChainPath" = "no" ] ; then | |
1668 | Le_RealFullChainPath="" | |
1669 | fi | |
1670 | ||
1671 | _installed="0" | |
8663fb7e | 1672 | if [ "$Le_RealCertPath" ] ; then |
4d2f38b0 | 1673 | _installed=1 |
1674 | _info "Installing cert to:$Le_RealCertPath" | |
8663fb7e | 1675 | if [ -f "$Le_RealCertPath" ] ; then |
ff3bce32 | 1676 | cp "$Le_RealCertPath" "$Le_RealCertPath".bak |
4c3b3608 | 1677 | fi |
1678 | cat "$CERT_PATH" > "$Le_RealCertPath" | |
1679 | fi | |
1680 | ||
8663fb7e | 1681 | if [ "$Le_RealCACertPath" ] ; then |
4d2f38b0 | 1682 | _installed=1 |
1683 | _info "Installing CA to:$Le_RealCACertPath" | |
8663fb7e | 1684 | if [ "$Le_RealCACertPath" = "$Le_RealCertPath" ] ; then |
4c3b3608 | 1685 | echo "" >> "$Le_RealCACertPath" |
1686 | cat "$CA_CERT_PATH" >> "$Le_RealCACertPath" | |
1687 | else | |
8663fb7e | 1688 | if [ -f "$Le_RealCACertPath" ] ; then |
ff3bce32 | 1689 | cp "$Le_RealCACertPath" "$Le_RealCACertPath".bak |
78552b18 | 1690 | fi |
4c3b3608 | 1691 | cat "$CA_CERT_PATH" > "$Le_RealCACertPath" |
1692 | fi | |
1693 | fi | |
1694 | ||
1695 | ||
8663fb7e | 1696 | if [ "$Le_RealKeyPath" ] ; then |
4d2f38b0 | 1697 | _installed=1 |
1698 | _info "Installing key to:$Le_RealKeyPath" | |
8663fb7e | 1699 | if [ -f "$Le_RealKeyPath" ] ; then |
ff3bce32 | 1700 | cp "$Le_RealKeyPath" "$Le_RealKeyPath".bak |
4c3b3608 | 1701 | fi |
1702 | cat "$CERT_KEY_PATH" > "$Le_RealKeyPath" | |
1703 | fi | |
a63b05a9 | 1704 | |
8663fb7e | 1705 | if [ "$Le_RealFullChainPath" ] ; then |
4d2f38b0 | 1706 | _installed=1 |
1707 | _info "Installing full chain to:$Le_RealFullChainPath" | |
8663fb7e | 1708 | if [ -f "$Le_RealFullChainPath" ] ; then |
ff3bce32 | 1709 | cp "$Le_RealFullChainPath" "$Le_RealFullChainPath".bak |
a63b05a9 | 1710 | fi |
1711 | cat "$CERT_FULLCHAIN_PATH" > "$Le_RealFullChainPath" | |
1712 | fi | |
4c3b3608 | 1713 | |
8663fb7e | 1714 | if [ "$Le_ReloadCmd" ] ; then |
4d2f38b0 | 1715 | _installed=1 |
4c3b3608 | 1716 | _info "Run Le_ReloadCmd: $Le_ReloadCmd" |
4d2f38b0 | 1717 | if (cd "$DOMAIN_PATH" && eval "$Le_ReloadCmd") ; then |
1718 | _info "Reload success." | |
1719 | else | |
1720 | _err "Reload error for :$Le_Domain" | |
1721 | fi | |
1722 | fi | |
1723 | ||
1724 | if [ "$_installed" = "0" ] ; then | |
1725 | _err "Nothing to install. You don't specify any parameter." | |
ca2a96b3 | 1726 | return 9 |
4c3b3608 | 1727 | fi |
1728 | ||
1729 | } | |
1730 | ||
1731 | installcronjob() { | |
1732 | _initpath | |
77546ea5 | 1733 | if ! _exists "crontab" ; then |
1734 | _err "crontab doesn't exist, so, we can not install cron jobs." | |
1735 | _err "All your certs will not be renewed automatically." | |
a7b7355d | 1736 | _err "You must add your own cron job to call '$PROJECT_ENTRY --cron' everyday." |
77546ea5 | 1737 | return 1 |
1738 | fi | |
1739 | ||
4c3b3608 | 1740 | _info "Installing cron job" |
a7b7355d | 1741 | if ! crontab -l | grep "$PROJECT_ENTRY --cron" ; then |
8663fb7e | 1742 | if [ -f "$LE_WORKING_DIR/$PROJECT_ENTRY" ] ; then |
a7b7355d | 1743 | lesh="\"$LE_WORKING_DIR\"/$PROJECT_ENTRY" |
4c3b3608 | 1744 | else |
a7b7355d | 1745 | _err "Can not install cronjob, $PROJECT_ENTRY not found." |
4c3b3608 | 1746 | return 1 |
1747 | fi | |
a7b7355d | 1748 | crontab -l | { cat; echo "0 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" > /dev/null"; } | crontab - |
4c3b3608 | 1749 | fi |
8663fb7e | 1750 | if [ "$?" != "0" ] ; then |
4c3b3608 | 1751 | _err "Install cron job failed. You need to manually renew your certs." |
1752 | _err "Or you can add cronjob by yourself:" | |
a7b7355d | 1753 | _err "$lesh --cron --home \"$LE_WORKING_DIR\" > /dev/null" |
4c3b3608 | 1754 | return 1 |
1755 | fi | |
1756 | } | |
1757 | ||
1758 | uninstallcronjob() { | |
37db5b81 | 1759 | if ! _exists "crontab" ; then |
1760 | return | |
1761 | fi | |
4c3b3608 | 1762 | _info "Removing cron job" |
a7b7355d | 1763 | cr="$(crontab -l | grep "$PROJECT_ENTRY --cron")" |
8663fb7e | 1764 | if [ "$cr" ] ; then |
a7b7355d | 1765 | crontab -l | sed "/$PROJECT_ENTRY --cron/d" | crontab - |
1766 | LE_WORKING_DIR="$(echo "$cr" | cut -d ' ' -f 9 | tr -d '"')" | |
4c3b3608 | 1767 | _info LE_WORKING_DIR "$LE_WORKING_DIR" |
1768 | fi | |
1769 | _initpath | |
a7b7355d | 1770 | |
4c3b3608 | 1771 | } |
1772 | ||
6cb415f5 | 1773 | revoke() { |
1774 | Le_Domain="$1" | |
8663fb7e | 1775 | if [ -z "$Le_Domain" ] ; then |
a7b7355d | 1776 | echo "Usage: $PROJECT_ENTRY --revoke -d domain.com" |
6cb415f5 | 1777 | return 1 |
1778 | fi | |
1779 | ||
1780 | _initpath $Le_Domain | |
8663fb7e | 1781 | if [ ! -f "$DOMAIN_CONF" ] ; then |
6cb415f5 | 1782 | _err "$Le_Domain is not a issued domain, skip." |
1783 | return 1; | |
1784 | fi | |
1785 | ||
8663fb7e | 1786 | if [ ! -f "$CERT_PATH" ] ; then |
6cb415f5 | 1787 | _err "Cert for $Le_Domain $CERT_PATH is not found, skip." |
1788 | return 1 | |
1789 | fi | |
1790 | ||
1791 | cert="$(_getfile "${CERT_PATH}" "${BEGIN_CERT}" "${END_CERT}"| tr -d "\r\n" | _urlencode)" | |
1792 | ||
8663fb7e | 1793 | if [ -z "$cert" ] ; then |
6cb415f5 | 1794 | _err "Cert for $Le_Domain is empty found, skip." |
1795 | return 1 | |
1796 | fi | |
1797 | ||
1798 | data="{\"resource\": \"revoke-cert\", \"certificate\": \"$cert\"}" | |
1799 | uri="$API/acme/revoke-cert" | |
1800 | ||
1801 | _info "Try domain key first." | |
1802 | if _send_signed_request $uri "$data" "" "$CERT_KEY_PATH"; then | |
8663fb7e | 1803 | if [ -z "$response" ] ; then |
6cb415f5 | 1804 | _info "Revoke success." |
1805 | rm -f $CERT_PATH | |
1806 | return 0 | |
1807 | else | |
1808 | _err "Revoke error by domain key." | |
1809 | _err "$resource" | |
1810 | fi | |
1811 | fi | |
1812 | ||
1813 | _info "Then try account key." | |
1814 | ||
1815 | if _send_signed_request $uri "$data" "" "$ACCOUNT_KEY_PATH" ; then | |
8663fb7e | 1816 | if [ -z "$response" ] ; then |
6cb415f5 | 1817 | _info "Revoke success." |
1818 | rm -f $CERT_PATH | |
1819 | return 0 | |
1820 | else | |
1821 | _err "Revoke error." | |
1822 | _debug "$resource" | |
1823 | fi | |
1824 | fi | |
1825 | return 1 | |
1826 | } | |
4c3b3608 | 1827 | |
1828 | # Detect profile file if not specified as environment variable | |
1829 | _detect_profile() { | |
a63b05a9 | 1830 | if [ -n "$PROFILE" -a -f "$PROFILE" ] ; then |
4c3b3608 | 1831 | echo "$PROFILE" |
1832 | return | |
1833 | fi | |
1834 | ||
1835 | local DETECTED_PROFILE | |
1836 | DETECTED_PROFILE='' | |
1837 | local SHELLTYPE | |
1838 | SHELLTYPE="$(basename "/$SHELL")" | |
1839 | ||
8663fb7e | 1840 | if [ "$SHELLTYPE" = "bash" ] ; then |
1841 | if [ -f "$HOME/.bashrc" ] ; then | |
4c3b3608 | 1842 | DETECTED_PROFILE="$HOME/.bashrc" |
8663fb7e | 1843 | elif [ -f "$HOME/.bash_profile" ] ; then |
4c3b3608 | 1844 | DETECTED_PROFILE="$HOME/.bash_profile" |
1845 | fi | |
8663fb7e | 1846 | elif [ "$SHELLTYPE" = "zsh" ] ; then |
4c3b3608 | 1847 | DETECTED_PROFILE="$HOME/.zshrc" |
1848 | fi | |
1849 | ||
8663fb7e | 1850 | if [ -z "$DETECTED_PROFILE" ] ; then |
1851 | if [ -f "$HOME/.profile" ] ; then | |
4c3b3608 | 1852 | DETECTED_PROFILE="$HOME/.profile" |
8663fb7e | 1853 | elif [ -f "$HOME/.bashrc" ] ; then |
4c3b3608 | 1854 | DETECTED_PROFILE="$HOME/.bashrc" |
8663fb7e | 1855 | elif [ -f "$HOME/.bash_profile" ] ; then |
4c3b3608 | 1856 | DETECTED_PROFILE="$HOME/.bash_profile" |
8663fb7e | 1857 | elif [ -f "$HOME/.zshrc" ] ; then |
4c3b3608 | 1858 | DETECTED_PROFILE="$HOME/.zshrc" |
1859 | fi | |
1860 | fi | |
1861 | ||
8663fb7e | 1862 | if [ ! -z "$DETECTED_PROFILE" ] ; then |
4c3b3608 | 1863 | echo "$DETECTED_PROFILE" |
1864 | fi | |
1865 | } | |
1866 | ||
1867 | _initconf() { | |
1868 | _initpath | |
8663fb7e | 1869 | if [ ! -f "$ACCOUNT_CONF_PATH" ] ; then |
d53289d7 | 1870 | echo "#ACCOUNT_CONF_PATH=xxxx |
1871 | ||
1872 | #Account configurations: | |
4c3b3608 | 1873 | #Here are the supported macros, uncomment them to make them take effect. |
d53289d7 | 1874 | |
4c3b3608 | 1875 | #ACCOUNT_EMAIL=aaa@aaa.com # the account email used to register account. |
5fd3f21b | 1876 | #ACCOUNT_KEY_PATH=\"/path/to/account.key\" |
b2817897 | 1877 | #CERT_HOME=\"/path/to/cert/home\" |
4c3b3608 | 1878 | |
1879 | #STAGE=1 # Use the staging api | |
1880 | #FORCE=1 # Force to issue cert | |
1881 | #DEBUG=1 # Debug mode | |
1882 | ||
166096dc | 1883 | #ACCOUNT_KEY_HASH=account key hash |
1884 | ||
635695ec | 1885 | USER_AGENT=\"$USER_AGENT\" |
281aa349 | 1886 | |
1887 | #USER_PATH="" | |
1888 | ||
4c3b3608 | 1889 | #dns api |
1890 | ####################### | |
1891 | #Cloudflare: | |
1892 | #api key | |
3d49985a | 1893 | #CF_Key=\"sdfsdfsdfljlbjkljlkjsdfoiwje\" |
4c3b3608 | 1894 | #account email |
3d49985a | 1895 | #CF_Email=\"xxxx@sss.com\" |
4c3b3608 | 1896 | |
1897 | ####################### | |
1898 | #Dnspod.cn: | |
1899 | #api key id | |
3d49985a | 1900 | #DP_Id=\"1234\" |
4c3b3608 | 1901 | #api key |
3d49985a | 1902 | #DP_Key=\"sADDsdasdgdsf\" |
4c3b3608 | 1903 | |
1904 | ####################### | |
1905 | #Cloudxns.com: | |
3d49985a | 1906 | #CX_Key=\"1234\" |
4c3b3608 | 1907 | # |
3d49985a | 1908 | #CX_Secret=\"sADDsdasdgdsf\" |
4c3b3608 | 1909 | |
1910 | " > $ACCOUNT_CONF_PATH | |
1911 | fi | |
1912 | } | |
1913 | ||
c60883ef | 1914 | _precheck() { |
1915 | if ! _exists "curl" && ! _exists "wget"; then | |
1916 | _err "Please install curl or wget first, we need to access http resources." | |
4c3b3608 | 1917 | return 1 |
1918 | fi | |
1919 | ||
c60883ef | 1920 | if ! _exists "crontab" ; then |
77546ea5 | 1921 | _err "It is recommended to install crontab first. try to install 'cron, crontab, crontabs or vixie-cron'." |
c60883ef | 1922 | _err "We need to set cron job to renew the certs automatically." |
77546ea5 | 1923 | _err "Otherwise, your certs will not be able to be renewed automatically." |
8663fb7e | 1924 | if [ -z "$FORCE" ] ; then |
a7b7355d | 1925 | _err "Please add '--force' and try install again to go without crontab." |
1926 | _err "./$PROJECT_ENTRY --install --force" | |
77546ea5 | 1927 | return 1 |
1928 | fi | |
4c3b3608 | 1929 | fi |
1930 | ||
c60883ef | 1931 | if ! _exists "openssl" ; then |
1932 | _err "Please install openssl first." | |
1933 | _err "We need openssl to generate keys." | |
4c3b3608 | 1934 | return 1 |
1935 | fi | |
1936 | ||
c60883ef | 1937 | if ! _exists "nc" ; then |
1938 | _err "It is recommended to install nc first, try to install 'nc' or 'netcat'." | |
1939 | _err "We use nc for standalone server if you use standalone mode." | |
1940 | _err "If you don't use standalone mode, just ignore this warning." | |
1941 | fi | |
1942 | ||
1943 | return 0 | |
1944 | } | |
1945 | ||
0a7c9364 | 1946 | _setShebang() { |
1947 | _file="$1" | |
1948 | _shebang="$2" | |
1949 | if [ -z "$_shebang" ] ; then | |
1950 | _err "Usage: file shebang" | |
1951 | return 1 | |
1952 | fi | |
1953 | cp "$_file" "$_file.tmp" | |
1954 | echo "$_shebang" > "$_file" | |
1955 | sed -n 2,99999p "$_file.tmp" >> "$_file" | |
1956 | rm -f "$_file.tmp" | |
1957 | } | |
1958 | ||
94dc5f33 | 1959 | _installalias() { |
1960 | _initpath | |
1961 | ||
1962 | _envfile="$LE_WORKING_DIR/$PROJECT_ENTRY.env" | |
1963 | if [ "$_upgrading" ] && [ "$_upgrading" = "1" ] ; then | |
1964 | echo "$(cat $_envfile)" | sed "s|^LE_WORKING_DIR.*$||" > "$_envfile" | |
1965 | echo "$(cat $_envfile)" | sed "s|^alias le.*$||" > "$_envfile" | |
1966 | echo "$(cat $_envfile)" | sed "s|^alias le.sh.*$||" > "$_envfile" | |
1967 | fi | |
1968 | ||
1786a5e5 | 1969 | _setopt "$_envfile" "export LE_WORKING_DIR" "=" "\"$LE_WORKING_DIR\"" |
94dc5f33 | 1970 | _setopt "$_envfile" "alias $PROJECT_ENTRY" "=" "\"$LE_WORKING_DIR/$PROJECT_ENTRY\"" |
1971 | ||
1972 | _profile="$(_detect_profile)" | |
1973 | if [ "$_profile" ] ; then | |
1974 | _debug "Found profile: $_profile" | |
1975 | _setopt "$_profile" ". \"$_envfile\"" | |
1976 | _info "OK, Close and reopen your terminal to start using $PROJECT_NAME" | |
1977 | else | |
1978 | _info "No profile is found, you will need to go into $LE_WORKING_DIR to use $PROJECT_NAME" | |
1979 | fi | |
1980 | ||
1981 | ||
1982 | #for csh | |
1983 | _cshfile="$LE_WORKING_DIR/$PROJECT_ENTRY.csh" | |
94dc5f33 | 1984 | _csh_profile="$HOME/.cshrc" |
1985 | if [ -f "$_csh_profile" ] ; then | |
6626371d | 1986 | _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\"" |
1987 | _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY\"" | |
94dc5f33 | 1988 | _setopt "$_csh_profile" "source \"$_cshfile\"" |
1989 | fi | |
acafa585 | 1990 | |
1991 | #for tcsh | |
1992 | _tcsh_profile="$HOME/.tcshrc" | |
1993 | if [ -f "$_tcsh_profile" ] ; then | |
1994 | _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\"" | |
1995 | _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY\"" | |
1996 | _setopt "$_tcsh_profile" "source \"$_cshfile\"" | |
1997 | fi | |
94dc5f33 | 1998 | |
1999 | } | |
2000 | ||
c60883ef | 2001 | install() { |
635695ec | 2002 | |
c60883ef | 2003 | if ! _initpath ; then |
2004 | _err "Install failed." | |
4c3b3608 | 2005 | return 1 |
2006 | fi | |
635695ec | 2007 | |
c60883ef | 2008 | if ! _precheck ; then |
2009 | _err "Pre-check failed, can not install." | |
4c3b3608 | 2010 | return 1 |
2011 | fi | |
c60883ef | 2012 | |
6cc11ffb | 2013 | #convert from le |
8663fb7e | 2014 | if [ -d "$HOME/.le" ] ; then |
6cc11ffb | 2015 | for envfile in "le.env" "le.sh.env" |
2016 | do | |
8663fb7e | 2017 | if [ -f "$HOME/.le/$envfile" ] ; then |
6cc11ffb | 2018 | if grep "le.sh" "$HOME/.le/$envfile" >/dev/null ; then |
2019 | _upgrading="1" | |
2020 | _info "You are upgrading from le.sh" | |
2021 | _info "Renaming \"$HOME/.le\" to $LE_WORKING_DIR" | |
2022 | mv "$HOME/.le" "$LE_WORKING_DIR" | |
2023 | mv "$LE_WORKING_DIR/$envfile" "$LE_WORKING_DIR/$PROJECT_ENTRY.env" | |
2024 | break; | |
2025 | fi | |
2026 | fi | |
2027 | done | |
2028 | fi | |
2029 | ||
4c3b3608 | 2030 | _info "Installing to $LE_WORKING_DIR" |
635695ec | 2031 | |
4a0f23e2 | 2032 | if ! mkdir -p "$LE_WORKING_DIR" ; then |
90035252 | 2033 | _err "Can not create working dir: $LE_WORKING_DIR" |
4a0f23e2 | 2034 | return 1 |
2035 | fi | |
2036 | ||
762978f8 | 2037 | chmod 700 "$LE_WORKING_DIR" |
2038 | ||
a7b7355d | 2039 | cp $PROJECT_ENTRY "$LE_WORKING_DIR/" && chmod +x "$LE_WORKING_DIR/$PROJECT_ENTRY" |
4c3b3608 | 2040 | |
8663fb7e | 2041 | if [ "$?" != "0" ] ; then |
a7b7355d | 2042 | _err "Install failed, can not copy $PROJECT_ENTRY" |
4c3b3608 | 2043 | return 1 |
2044 | fi | |
2045 | ||
a7b7355d | 2046 | _info "Installed to $LE_WORKING_DIR/$PROJECT_ENTRY" |
4c3b3608 | 2047 | |
94dc5f33 | 2048 | _installalias |
4c3b3608 | 2049 | |
8663fb7e | 2050 | if [ -d "dnsapi" ] ; then |
6ed1c718 | 2051 | mkdir -p $LE_WORKING_DIR/dnsapi |
2052 | cp dnsapi/* $LE_WORKING_DIR/dnsapi/ | |
2053 | fi | |
d53289d7 | 2054 | |
8663fb7e | 2055 | if [ ! -f "$ACCOUNT_CONF_PATH" ] ; then |
4c3b3608 | 2056 | _initconf |
2057 | fi | |
6cc11ffb | 2058 | |
8663fb7e | 2059 | if [ "$_DEFAULT_ACCOUNT_CONF_PATH" != "$ACCOUNT_CONF_PATH" ] ; then |
635695ec | 2060 | _setopt "$_DEFAULT_ACCOUNT_CONF_PATH" "ACCOUNT_CONF_PATH" "=" "\"$ACCOUNT_CONF_PATH\"" |
6cc11ffb | 2061 | fi |
2062 | ||
8663fb7e | 2063 | if [ "$_DEFAULT_CERT_HOME" != "$CERT_HOME" ] ; then |
b2817897 | 2064 | _saveaccountconf "CERT_HOME" "$CERT_HOME" |
2065 | fi | |
2066 | ||
8663fb7e | 2067 | if [ "$_DEFAULT_ACCOUNT_KEY_PATH" != "$ACCOUNT_KEY_PATH" ] ; then |
b2817897 | 2068 | _saveaccountconf "ACCOUNT_KEY_PATH" "$ACCOUNT_KEY_PATH" |
2069 | fi | |
2070 | ||
d53289d7 | 2071 | installcronjob |
0a7c9364 | 2072 | |
641989fd | 2073 | if [ -z "$NO_DETECT_SH" ] ; then |
2074 | #Modify shebang | |
2075 | if _exists bash ; then | |
2076 | _info "Good, bash is installed, change the shebang to use bash as prefered." | |
2077 | _shebang='#!/usr/bin/env bash' | |
2078 | _setShebang "$LE_WORKING_DIR/$PROJECT_ENTRY" "$_shebang" | |
2079 | if [ -d "$LE_WORKING_DIR/dnsapi" ] ; then | |
2080 | for _apifile in $(ls "$LE_WORKING_DIR/dnsapi/"*.sh) ; do | |
2081 | _setShebang "$_apifile" "$_shebang" | |
2082 | done | |
2083 | fi | |
0a7c9364 | 2084 | fi |
2085 | fi | |
2086 | ||
4c3b3608 | 2087 | _info OK |
2088 | } | |
2089 | ||
2090 | uninstall() { | |
2091 | uninstallcronjob | |
2092 | _initpath | |
2093 | ||
2094 | _profile="$(_detect_profile)" | |
8663fb7e | 2095 | if [ "$_profile" ] ; then |
7203a1c1 | 2096 | text="$(cat $_profile)" |
94dc5f33 | 2097 | echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.env\"$||" > "$_profile" |
4c3b3608 | 2098 | fi |
2099 | ||
94dc5f33 | 2100 | _csh_profile="$HOME/.cshrc" |
2101 | if [ -f "$_csh_profile" ] ; then | |
2102 | text="$(cat $_csh_profile)" | |
2103 | echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" > "$_csh_profile" | |
2104 | fi | |
2105 | ||
acafa585 | 2106 | _tcsh_profile="$HOME/.tcshrc" |
2107 | if [ -f "$_tcsh_profile" ] ; then | |
2108 | text="$(cat $_tcsh_profile)" | |
2109 | echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" > "$_tcsh_profile" | |
2110 | fi | |
2111 | ||
a7b7355d | 2112 | rm -f $LE_WORKING_DIR/$PROJECT_ENTRY |
4c3b3608 | 2113 | _info "The keys and certs are in $LE_WORKING_DIR, you can remove them by yourself." |
2114 | ||
2115 | } | |
2116 | ||
2117 | cron() { | |
281aa349 | 2118 | IN_CRON=1 |
4c3b3608 | 2119 | renewAll |
281aa349 | 2120 | IN_CRON="" |
4c3b3608 | 2121 | } |
2122 | ||
2123 | version() { | |
a63b05a9 | 2124 | echo "$PROJECT" |
2125 | echo "v$VER" | |
4c3b3608 | 2126 | } |
2127 | ||
2128 | showhelp() { | |
2129 | version | |
a7b7355d | 2130 | echo "Usage: $PROJECT_ENTRY command ...[parameters].... |
a63b05a9 | 2131 | Commands: |
2132 | --help, -h Show this help message. | |
2133 | --version, -v Show version info. | |
a7b7355d | 2134 | --install Install $PROJECT_NAME to your system. |
2135 | --uninstall Uninstall $PROJECT_NAME, and uninstall the cron job. | |
a63b05a9 | 2136 | --issue Issue a cert. |
2137 | --installcert Install the issued cert to apache/nginx or any other server. | |
2138 | --renew, -r Renew a cert. | |
2139 | --renewAll Renew all the certs | |
2140 | --revoke Revoke a cert. | |
2141 | --installcronjob Install the cron job to renew certs, you don't need to call this. The 'install' command can automatically install the cron job. | |
2142 | --uninstallcronjob Uninstall the cron job. The 'uninstall' command can do this automatically. | |
2143 | --cron Run cron job to renew all the certs. | |
2144 | --toPkcs Export the certificate and key to a pfx file. | |
2145 | --createAccountKey, -cak Create an account private key, professional use. | |
2146 | --createDomainKey, -cdk Create an domain private key, professional use. | |
2147 | --createCSR, -ccsr Create CSR , professional use. | |
2148 | ||
2149 | Parameters: | |
2150 | --domain, -d domain.tld Specifies a domain, used to issue, renew or revoke etc. | |
2151 | --force, -f Used to force to install or force to renew a cert immediately. | |
2152 | --staging, --test Use staging server, just for test. | |
2153 | --debug Output debug info. | |
2154 | ||
2155 | --webroot, -w /path/to/webroot Specifies the web root folder for web root mode. | |
2156 | --standalone Use standalone mode. | |
2157 | --apache Use apache mode. | |
eccec5f6 | 2158 | --dns [dns_cf|dns_dp|dns_cx|/path/to/api/file] Use dns mode or dns api. |
a63b05a9 | 2159 | |
2160 | --keylength, -k [2048] Specifies the domain key length: 2048, 3072, 4096, 8192 or ec-256, ec-384. | |
2161 | --accountkeylength, -ak [2048] Specifies the account key length. | |
2162 | ||
2163 | These parameters are to install the cert to nginx/apache or anyother server after issue/renew a cert: | |
2164 | ||
2165 | --certpath /path/to/real/cert/file After issue/renew, the cert will be copied to this path. | |
2166 | --keypath /path/to/real/key/file After issue/renew, the key will be copied to this path. | |
2167 | --capath /path/to/real/ca/file After issue/renew, the intermediate cert will be copied to this path. | |
2168 | --fullchainpath /path/to/fullchain/file After issue/renew, the fullchain cert will be copied to this path. | |
2169 | ||
2170 | --reloadcmd \"service nginx reload\" After issue/renew, it's used to reload the server. | |
2171 | ||
2172 | --accountconf Specifies a customized account config file. | |
635695ec | 2173 | --home Specifies the home dir for $PROJECT_NAME . |
39c8f79f | 2174 | --certhome Specifies the home dir to save all the certs, only valid for '--install' command. |
635695ec | 2175 | --useragent Specifies the user agent string. it will be saved for future use too. |
b5eb4b90 | 2176 | --accountemail Specifies the account email for registering, Only valid for the '--install' command. |
06625071 | 2177 | --accountkey Specifies the account key path, Only valid for the '--install' command. |
2178 | --days Specifies the days to renew the cert when using '--issue' command. The max value is 80 days. | |
39c8f79f | 2179 | --httpport Specifies the standalone listening port. Only valid if the server is behind a reverse proxy or load balancer. |
4c3b3608 | 2180 | " |
2181 | } | |
2182 | ||
4a0f23e2 | 2183 | _installOnline() { |
2184 | _info "Installing from online archive." | |
8663fb7e | 2185 | if [ ! "$BRANCH" ] ; then |
4a0f23e2 | 2186 | BRANCH="master" |
2187 | fi | |
2188 | _initpath | |
2189 | target="$PROJECT/archive/$BRANCH.tar.gz" | |
2190 | _info "Downloading $target" | |
2191 | localname="$BRANCH.tar.gz" | |
2192 | if ! _get "$target" > $localname ; then | |
2193 | _debug "Download error." | |
2194 | return 1 | |
2195 | fi | |
2196 | _info "Extracting $localname" | |
2197 | tar xzf $localname | |
6cc11ffb | 2198 | cd "$PROJECT_NAME-$BRANCH" |
a7b7355d | 2199 | chmod +x $PROJECT_ENTRY |
2200 | if ./$PROJECT_ENTRY install ; then | |
4a0f23e2 | 2201 | _info "Install success!" |
2202 | fi | |
2203 | ||
2204 | cd .. | |
6cc11ffb | 2205 | rm -rf "$PROJECT_NAME-$BRANCH" |
4a0f23e2 | 2206 | rm -f "$localname" |
2207 | } | |
2208 | ||
a63b05a9 | 2209 | |
2210 | _process() { | |
2211 | _CMD="" | |
2212 | _domain="" | |
2213 | _altdomains="no" | |
2214 | _webroot="" | |
2215 | _keylength="no" | |
2216 | _accountkeylength="no" | |
2217 | _certpath="no" | |
2218 | _keypath="no" | |
2219 | _capath="no" | |
2220 | _fullchainpath="no" | |
4d2f38b0 | 2221 | _reloadcmd="" |
a63b05a9 | 2222 | _password="" |
635695ec | 2223 | _accountconf="" |
2224 | _useragent="" | |
b5eb4b90 | 2225 | _accountemail="" |
2226 | _accountkey="" | |
b2817897 | 2227 | _certhome="" |
39c8f79f | 2228 | _httpport="" |
8663fb7e | 2229 | while [ ${#} -gt 0 ] ; do |
a63b05a9 | 2230 | case "${1}" in |
2231 | ||
2232 | --help|-h) | |
2233 | showhelp | |
2234 | return | |
2235 | ;; | |
2236 | --version|-v) | |
2237 | version | |
2238 | return | |
2239 | ;; | |
2240 | --install) | |
2241 | _CMD="install" | |
2242 | ;; | |
2243 | --uninstall) | |
2244 | _CMD="uninstall" | |
2245 | ;; | |
2246 | --issue) | |
2247 | _CMD="issue" | |
2248 | ;; | |
2249 | --installcert|-i) | |
2250 | _CMD="installcert" | |
2251 | ;; | |
2252 | --renew|-r) | |
2253 | _CMD="renew" | |
2254 | ;; | |
4d2f38b0 | 2255 | --renewAll|--renewall) |
a63b05a9 | 2256 | _CMD="renewAll" |
2257 | ;; | |
2258 | --revoke) | |
2259 | _CMD="revoke" | |
2260 | ;; | |
2261 | --installcronjob) | |
2262 | _CMD="installcronjob" | |
2263 | ;; | |
2264 | --uninstallcronjob) | |
2265 | _CMD="uninstallcronjob" | |
2266 | ;; | |
2267 | --cron) | |
2268 | _CMD="cron" | |
2269 | ;; | |
2270 | --toPkcs) | |
2271 | _CMD="toPkcs" | |
2272 | ;; | |
2273 | --createAccountKey|--createaccountkey|-cak) | |
2274 | _CMD="createAccountKey" | |
2275 | ;; | |
2276 | --createDomainKey|--createdomainkey|-cdk) | |
2277 | _CMD="createDomainKey" | |
2278 | ;; | |
2279 | --createCSR|--createcsr|-ccr) | |
2280 | _CMD="createCSR" | |
2281 | ;; | |
2282 | ||
2283 | ||
2284 | --domain|-d) | |
2285 | _dvalue="$2" | |
2286 | ||
8663fb7e | 2287 | if [ -z "$_dvalue" ] || _startswith "$_dvalue" "-" ; then |
a63b05a9 | 2288 | _err "'$_dvalue' is not a valid domain for parameter '$1'" |
2289 | return 1 | |
2290 | fi | |
2291 | ||
8663fb7e | 2292 | if [ -z "$_domain" ] ; then |
a63b05a9 | 2293 | _domain="$_dvalue" |
2294 | else | |
8663fb7e | 2295 | if [ "$_altdomains" = "no" ] ; then |
a63b05a9 | 2296 | _altdomains="$_dvalue" |
2297 | else | |
2298 | _altdomains="$_altdomains,$_dvalue" | |
2299 | fi | |
2300 | fi | |
2301 | shift | |
2302 | ;; | |
2303 | ||
2304 | --force|-f) | |
2305 | FORCE="1" | |
2306 | ;; | |
2307 | --staging|--test) | |
2308 | STAGE="1" | |
2309 | ;; | |
2310 | --debug) | |
8663fb7e | 2311 | if [ -z "$2" ] || _startswith "$2" "-" ; then |
a63b05a9 | 2312 | DEBUG="1" |
2313 | else | |
2314 | DEBUG="$2" | |
2315 | shift | |
6fc1447f | 2316 | fi |
a63b05a9 | 2317 | ;; |
a63b05a9 | 2318 | --webroot|-w) |
2319 | wvalue="$2" | |
8663fb7e | 2320 | if [ -z "$_webroot" ] ; then |
a63b05a9 | 2321 | _webroot="$wvalue" |
2322 | else | |
2323 | _webroot="$_webroot,$wvalue" | |
2324 | fi | |
2325 | shift | |
2326 | ;; | |
2327 | --standalone) | |
2328 | wvalue="no" | |
8663fb7e | 2329 | if [ -z "$_webroot" ] ; then |
a63b05a9 | 2330 | _webroot="$wvalue" |
2331 | else | |
2332 | _webroot="$_webroot,$wvalue" | |
2333 | fi | |
2334 | ;; | |
2335 | --apache) | |
2336 | wvalue="apache" | |
8663fb7e | 2337 | if [ -z "$_webroot" ] ; then |
a63b05a9 | 2338 | _webroot="$wvalue" |
2339 | else | |
2340 | _webroot="$_webroot,$wvalue" | |
2341 | fi | |
2342 | ;; | |
2343 | --dns) | |
2344 | wvalue="dns" | |
dceb3aca | 2345 | if ! _startswith "$2" "-" ; then |
a63b05a9 | 2346 | wvalue="$2" |
2347 | shift | |
2348 | fi | |
8663fb7e | 2349 | if [ -z "$_webroot" ] ; then |
a63b05a9 | 2350 | _webroot="$wvalue" |
2351 | else | |
2352 | _webroot="$_webroot,$wvalue" | |
2353 | fi | |
2354 | ;; | |
2355 | --keylength|-k) | |
2356 | _keylength="$2" | |
2357 | accountkeylength="$2" | |
2358 | shift | |
2359 | ;; | |
2360 | --accountkeylength|-ak) | |
2361 | accountkeylength="$2" | |
2362 | shift | |
2363 | ;; | |
2364 | ||
2365 | --certpath) | |
2366 | _certpath="$2" | |
2367 | shift | |
2368 | ;; | |
2369 | --keypath) | |
2370 | _keypath="$2" | |
2371 | shift | |
2372 | ;; | |
2373 | --capath) | |
2374 | _capath="$2" | |
2375 | shift | |
2376 | ;; | |
2377 | --fullchainpath) | |
2378 | _fullchainpath="$2" | |
2379 | shift | |
2380 | ;; | |
635695ec | 2381 | --reloadcmd|--reloadCmd) |
a63b05a9 | 2382 | _reloadcmd="$2" |
2383 | shift | |
2384 | ;; | |
2385 | --password) | |
2386 | _password="$2" | |
2387 | shift | |
2388 | ;; | |
2389 | --accountconf) | |
635695ec | 2390 | _accountconf="$2" |
2391 | ACCOUNT_CONF_PATH="$_accountconf" | |
a7b7355d | 2392 | shift |
a63b05a9 | 2393 | ;; |
a7b7355d | 2394 | --home) |
a63b05a9 | 2395 | LE_WORKING_DIR="$2" |
a7b7355d | 2396 | shift |
a63b05a9 | 2397 | ;; |
b2817897 | 2398 | --certhome) |
2399 | _certhome="$2" | |
2400 | CERT_HOME="$_certhome" | |
2401 | shift | |
2402 | ;; | |
635695ec | 2403 | --useragent) |
2404 | _useragent="$2" | |
2405 | USER_AGENT="$_useragent" | |
2406 | shift | |
2407 | ;; | |
b5eb4b90 | 2408 | --accountemail ) |
2409 | _accountemail="$2" | |
2410 | ACCOUNT_EMAIL="$_accountemail" | |
2411 | shift | |
2412 | ;; | |
2413 | --accountkey ) | |
2414 | _accountkey="$2" | |
2415 | ACCOUNT_KEY_PATH="$_accountkey" | |
2416 | shift | |
2417 | ;; | |
06625071 | 2418 | --days ) |
2419 | _days="$2" | |
2420 | Le_RenewalDays="$_days" | |
2421 | shift | |
2422 | ;; | |
39c8f79f | 2423 | --httpport ) |
2424 | _httpport="$2" | |
2425 | Le_HTTPPort="$_httpport" | |
2426 | shift | |
2427 | ;; | |
a63b05a9 | 2428 | *) |
2429 | _err "Unknown parameter : $1" | |
2430 | return 1 | |
2431 | ;; | |
2432 | esac | |
2433 | ||
2434 | shift 1 | |
2435 | done | |
2436 | ||
2437 | ||
2438 | case "${_CMD}" in | |
2439 | install) install ;; | |
2440 | uninstall) uninstall ;; | |
2441 | issue) | |
70a55875 | 2442 | issue "$_webroot" "$_domain" "$_altdomains" "$_keylength" "$_certpath" "$_keypath" "$_capath" "$_reloadcmd" "$_fullchainpath" |
a63b05a9 | 2443 | ;; |
2444 | installcert) | |
3ed4102a | 2445 | installcert "$_domain" "$_certpath" "$_keypath" "$_capath" "$_reloadcmd" "$_fullchainpath" |
a63b05a9 | 2446 | ;; |
2447 | renew) | |
2448 | renew "$_domain" | |
2449 | ;; | |
2450 | renewAll) | |
2451 | renewAll | |
2452 | ;; | |
2453 | revoke) | |
2454 | revoke "$_domain" | |
2455 | ;; | |
2456 | installcronjob) installcronjob ;; | |
2457 | uninstallcronjob) uninstallcronjob ;; | |
2458 | cron) cron ;; | |
2459 | toPkcs) | |
2460 | toPkcs "$_domain" "$_password" | |
2461 | ;; | |
2462 | createAccountKey) | |
2463 | createAccountKey "$_domain" "$_accountkeylength" | |
2464 | ;; | |
2465 | createDomainKey) | |
2466 | createDomainKey "$_domain" "$_keylength" | |
2467 | ;; | |
2468 | createCSR) | |
2469 | createCSR "$_domain" "$_altdomains" | |
2470 | ;; | |
2471 | ||
2472 | *) | |
2473 | _err "Invalid command: $_CMD" | |
2474 | showhelp; | |
2475 | return 1 | |
2476 | ;; | |
2477 | esac | |
d3595686 | 2478 | _ret="$?" |
2479 | if [ "$_ret" != "0" ] ; then | |
2480 | return $_ret | |
2481 | fi | |
a63b05a9 | 2482 | |
8663fb7e | 2483 | if [ "$_useragent" ] ; then |
635695ec | 2484 | _saveaccountconf "USER_AGENT" "$_useragent" |
2485 | fi | |
8663fb7e | 2486 | if [ "$_accountemail" ] ; then |
b5eb4b90 | 2487 | _saveaccountconf "ACCOUNT_EMAIL" "$_accountemail" |
2488 | fi | |
b2817897 | 2489 | |
635695ec | 2490 | |
a63b05a9 | 2491 | } |
2492 | ||
2493 | ||
8663fb7e | 2494 | if [ "$INSTALLONLINE" ] ; then |
d1f97fc8 | 2495 | INSTALLONLINE="" |
4a0f23e2 | 2496 | _installOnline $BRANCH |
2497 | exit | |
2498 | fi | |
4c3b3608 | 2499 | |
8663fb7e | 2500 | if [ -z "$1" ] ; then |
4c3b3608 | 2501 | showhelp |
2502 | else | |
036e9d10 | 2503 | if echo "$1" | grep "^-" >/dev/null 2>&1 ; then |
a63b05a9 | 2504 | _process "$@" |
2505 | else | |
2506 | "$@" | |
2507 | fi | |
4c3b3608 | 2508 | fi |
a63b05a9 | 2509 | |
2510 |