]>
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 " |
8663fb7e | 588 | if [ "$needbase64" ] ; then |
9aaf36cd | 589 | response="$($_CURL -A "User-Agent: $USER_AGENT" -X $httpmethod -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" --data "$body" "$url" 2>"$HTTP_ERROR_FILE" | _base64)" |
c60883ef | 590 | else |
9aaf36cd | 591 | response="$($_CURL -A "User-Agent: $USER_AGENT" -X $httpmethod -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" --data "$body" "$url" 2>"$HTTP_ERROR_FILE" )" |
c60883ef | 592 | fi |
16679b57 | 593 | _ret="$?" |
c60883ef | 594 | else |
8663fb7e | 595 | if [ "$needbase64" ] ; then |
8fb9a709 | 596 | if [ "$httpmethod"="POST" ] ; then |
597 | response="$($WGET -S -O - --user-agent="$USER_AGENT" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --post-data="$body" "$url" 2>"$HTTP_HEADER" | _base64)" | |
598 | else | |
599 | 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)" | |
600 | fi | |
c60883ef | 601 | else |
8fb9a709 | 602 | if [ "$httpmethod"="POST" ] ; then |
603 | response="$($WGET -S -O - --user-agent="$USER_AGENT" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" --post-data="$body" "$url" 2>"$HTTP_HEADER")" | |
604 | else | |
605 | 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")" | |
606 | fi | |
c60883ef | 607 | fi |
16679b57 | 608 | _ret="$?" |
c60883ef | 609 | _sed_i "s/^ *//g" "$HTTP_HEADER" |
610 | fi | |
16679b57 | 611 | _debug "_ret" "$_ret" |
9aaf36cd | 612 | if [ "$ret" != "0" ] ; then |
613 | _err "$(cat "$HTTP_ERROR_FILE")" | |
614 | fi | |
19539575 | 615 | printf "%s" "$response" |
16679b57 | 616 | return $_ret |
c60883ef | 617 | } |
618 | ||
619 | # url getheader | |
620 | _get() { | |
a4270efa | 621 | _debug GET |
c60883ef | 622 | url="$1" |
623 | onlyheader="$2" | |
624 | _debug url $url | |
625 | if _exists "curl" ; then | |
8663fb7e | 626 | if [ "$onlyheader" ] ; then |
9aaf36cd | 627 | $CURL -I -A "User-Agent: $USER_AGENT" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" $url 2>"$HTTP_ERROR_FILE" |
c60883ef | 628 | else |
9aaf36cd | 629 | $CURL -A "User-Agent: $USER_AGENT" -H "$_H1" -H "$_H2" -H "$_H3" -H "$_H4" $url 2>"$HTTP_ERROR_FILE" |
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 |
9aaf36cd | 637 | $WGET --user-agent="$USER_AGENT" --header "$_H4" --header "$_H3" --header "$_H2" --header "$_H1" -O - $url 2>"$HTTP_ERROR_FILE" |
c60883ef | 638 | fi |
9aaf36cd | 639 | ret=$? |
640 | fi | |
641 | ||
642 | if [ "$ret" != "0" ] ; then | |
643 | _err "$(cat "$HTTP_ERROR_FILE")" | |
c60883ef | 644 | fi |
c60883ef | 645 | return $ret |
646 | } | |
166096dc | 647 | |
648 | # url payload needbase64 keyfile | |
4c3b3608 | 649 | _send_signed_request() { |
650 | url=$1 | |
651 | payload=$2 | |
652 | needbase64=$3 | |
166096dc | 653 | keyfile=$4 |
8663fb7e | 654 | if [ -z "$keyfile" ] ; then |
166096dc | 655 | keyfile="$ACCOUNT_KEY_PATH" |
656 | fi | |
4c3b3608 | 657 | _debug url $url |
658 | _debug payload "$payload" | |
659 | ||
166096dc | 660 | if ! _calcjwk "$keyfile" ; then |
661 | return 1 | |
662 | fi | |
c60883ef | 663 | |
166096dc | 664 | payload64=$(echo -n $payload | _base64 | _urlencode) |
a63b05a9 | 665 | _debug2 payload64 $payload64 |
4c3b3608 | 666 | |
667 | nonceurl="$API/directory" | |
a272ee4f | 668 | _headers="$(_get $nonceurl "onlyheader")" |
669 | ||
7012b91f | 670 | if [ "$?" != "0" ] ; then |
671 | _err "Can not connect to $nonceurl to get nonce." | |
672 | return 1 | |
673 | fi | |
a272ee4f | 674 | |
675 | _debug2 _headers "$_headers" | |
676 | ||
677 | nonce="$( echo "$_headers" | grep "Replay-Nonce:" | head -1 | tr -d "\r\n " | cut -d ':' -f 2)" | |
678 | ||
4c3b3608 | 679 | _debug nonce "$nonce" |
680 | ||
681 | protected="$(printf "$HEADERPLACE" | sed "s/NONCE/$nonce/" )" | |
a63b05a9 | 682 | _debug2 protected "$protected" |
4c3b3608 | 683 | |
166096dc | 684 | protected64="$(printf "$protected" | _base64 | _urlencode)" |
a63b05a9 | 685 | _debug2 protected64 "$protected64" |
166096dc | 686 | |
88fab7d6 | 687 | sig=$(echo -n "$protected64.$payload64" | _sign "$keyfile" "sha256" | _urlencode) |
a63b05a9 | 688 | _debug2 sig "$sig" |
4c3b3608 | 689 | |
690 | body="{\"header\": $HEADER, \"protected\": \"$protected64\", \"payload\": \"$payload64\", \"signature\": \"$sig\"}" | |
a63b05a9 | 691 | _debug2 body "$body" |
4c3b3608 | 692 | |
bbbdcb09 | 693 | |
eae29099 | 694 | response="$(_post "$body" $url "$needbase64")" |
7012b91f | 695 | if [ "$?" != "0" ] ; then |
696 | _err "Can not post to $url." | |
697 | return 1 | |
698 | fi | |
eae29099 | 699 | _debug2 original "$response" |
700 | ||
701 | response="$( echo "$response" | _normalizeJson )" | |
4c3b3608 | 702 | |
c60883ef | 703 | responseHeaders="$(cat $HTTP_HEADER)" |
4c3b3608 | 704 | |
a63b05a9 | 705 | _debug2 responseHeaders "$responseHeaders" |
706 | _debug2 response "$response" | |
c60883ef | 707 | code="$(grep "^HTTP" $HTTP_HEADER | tail -1 | cut -d " " -f 2 | tr -d "\r\n" )" |
4c3b3608 | 708 | _debug code $code |
709 | ||
710 | } | |
711 | ||
4c3b3608 | 712 | |
713 | #setopt "file" "opt" "=" "value" [";"] | |
714 | _setopt() { | |
715 | __conf="$1" | |
716 | __opt="$2" | |
717 | __sep="$3" | |
718 | __val="$4" | |
719 | __end="$5" | |
8663fb7e | 720 | if [ -z "$__opt" ] ; then |
4c3b3608 | 721 | echo usage: _setopt '"file" "opt" "=" "value" [";"]' |
722 | return | |
723 | fi | |
8663fb7e | 724 | if [ ! -f "$__conf" ] ; then |
4c3b3608 | 725 | touch "$__conf" |
726 | fi | |
727 | ||
728 | if grep -H -n "^$__opt$__sep" "$__conf" > /dev/null ; then | |
a63b05a9 | 729 | _debug2 OK |
dceb3aca | 730 | if _contains "$__val" "&" ; then |
4c3b3608 | 731 | __val="$(echo $__val | sed 's/&/\\&/g')" |
732 | fi | |
733 | text="$(cat $__conf)" | |
6dfaaa70 | 734 | echo "$text" | sed "s|^$__opt$__sep.*$|$__opt$__sep$__val$__end|" > "$__conf" |
4c3b3608 | 735 | |
736 | elif grep -H -n "^#$__opt$__sep" "$__conf" > /dev/null ; then | |
dceb3aca | 737 | if _contains "$__val" "&" ; then |
4c3b3608 | 738 | __val="$(echo $__val | sed 's/&/\\&/g')" |
739 | fi | |
740 | text="$(cat $__conf)" | |
6dfaaa70 | 741 | echo "$text" | sed "s|^#$__opt$__sep.*$|$__opt$__sep$__val$__end|" > "$__conf" |
4c3b3608 | 742 | |
743 | else | |
a63b05a9 | 744 | _debug2 APP |
4c3b3608 | 745 | echo "$__opt$__sep$__val$__end" >> "$__conf" |
746 | fi | |
747 | _debug "$(grep -H -n "^$__opt$__sep" $__conf)" | |
748 | } | |
749 | ||
750 | #_savedomainconf key value | |
751 | #save to domain.conf | |
752 | _savedomainconf() { | |
753 | key="$1" | |
754 | value="$2" | |
8663fb7e | 755 | if [ "$DOMAIN_CONF" ] ; then |
4d2f38b0 | 756 | _setopt "$DOMAIN_CONF" "$key" "=" "\"$value\"" |
757 | else | |
758 | _err "DOMAIN_CONF is empty, can not save $key=$value" | |
759 | fi | |
760 | } | |
761 | ||
762 | #_cleardomainconf key | |
763 | _cleardomainconf() { | |
764 | key="$1" | |
765 | if [ "$DOMAIN_CONF" ] ; then | |
766 | _sed_i "s/^$key.*$//" "$DOMAIN_CONF" | |
4c3b3608 | 767 | else |
768 | _err "DOMAIN_CONF is empty, can not save $key=$value" | |
769 | fi | |
770 | } | |
771 | ||
772 | #_saveaccountconf key value | |
773 | _saveaccountconf() { | |
774 | key="$1" | |
775 | value="$2" | |
8663fb7e | 776 | if [ "$ACCOUNT_CONF_PATH" ] ; then |
4d2f38b0 | 777 | _setopt "$ACCOUNT_CONF_PATH" "$key" "=" "\"$value\"" |
4c3b3608 | 778 | else |
779 | _err "ACCOUNT_CONF_PATH is empty, can not save $key=$value" | |
780 | fi | |
781 | } | |
782 | ||
783 | _startserver() { | |
784 | content="$1" | |
6fc1447f | 785 | _debug "startserver: $$" |
1b2e940d | 786 | nchelp="$(nc -h 2>&1)" |
850c1128 | 787 | |
399306a1 | 788 | if echo "$nchelp" | grep "\-q[ ,]" >/dev/null ; then |
850c1128 | 789 | _NC="nc -q 1 -l" |
790 | else | |
f76eb452 | 791 | if echo "$nchelp" | grep "GNU netcat" >/dev/null && echo "$nchelp" | grep "\-c, \-\-close" >/dev/null ; then |
792 | _NC="nc -c -l" | |
6d60f288 | 793 | elif echo "$nchelp" | grep "\-N" |grep "Shutdown the network socket after EOF on stdin" >/dev/null ; then |
794 | _NC="nc -N -l" | |
f76eb452 | 795 | else |
796 | _NC="nc -l" | |
797 | fi | |
4c3b3608 | 798 | fi |
1b2e940d | 799 | |
f76eb452 | 800 | _debug "_NC" "$_NC" |
39c8f79f | 801 | _debug Le_HTTPPort "$Le_HTTPPort" |
4c3b3608 | 802 | # while true ; do |
8663fb7e | 803 | if [ "$DEBUG" ] ; then |
2c554a4b | 804 | if ! printf "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC -p $Le_HTTPPort ; then |
805 | printf "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC $Le_HTTPPort ; | |
3aff11f6 | 806 | fi |
4c3b3608 | 807 | else |
2c554a4b | 808 | if ! printf "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC -p $Le_HTTPPort > /dev/null 2>&1; then |
809 | printf "HTTP/1.1 200 OK\r\n\r\n$content" | $_NC $Le_HTTPPort > /dev/null 2>&1 | |
3aff11f6 | 810 | fi |
4c3b3608 | 811 | fi |
8663fb7e | 812 | if [ "$?" != "0" ] ; then |
051c706d | 813 | _err "nc listen error." |
6fc1447f | 814 | exit 1 |
051c706d | 815 | fi |
4c3b3608 | 816 | # done |
817 | } | |
818 | ||
6fc1447f | 819 | _stopserver(){ |
4c3b3608 | 820 | pid="$1" |
6fc1447f | 821 | _debug "pid" "$pid" |
8663fb7e | 822 | if [ -z "$pid" ] ; then |
6fc1447f | 823 | return |
824 | fi | |
825 | ||
3d434e43 | 826 | _get "http://localhost:$Le_HTTPPort" >/dev/null 2>&1 |
4c3b3608 | 827 | |
828 | } | |
829 | ||
830 | _initpath() { | |
831 | ||
8663fb7e | 832 | if [ -z "$LE_WORKING_DIR" ] ; then |
6cc11ffb | 833 | LE_WORKING_DIR=$HOME/.$PROJECT_NAME |
4c3b3608 | 834 | fi |
835 | ||
d53289d7 | 836 | _DEFAULT_ACCOUNT_CONF_PATH="$LE_WORKING_DIR/account.conf" |
837 | ||
8663fb7e | 838 | if [ -z "$ACCOUNT_CONF_PATH" ] ; then |
839 | if [ -f "$_DEFAULT_ACCOUNT_CONF_PATH" ] ; then | |
840 | . "$_DEFAULT_ACCOUNT_CONF_PATH" | |
635695ec | 841 | fi |
d53289d7 | 842 | fi |
843 | ||
8663fb7e | 844 | if [ -z "$ACCOUNT_CONF_PATH" ] ; then |
d53289d7 | 845 | ACCOUNT_CONF_PATH="$_DEFAULT_ACCOUNT_CONF_PATH" |
4c3b3608 | 846 | fi |
847 | ||
8663fb7e | 848 | if [ -f "$ACCOUNT_CONF_PATH" ] ; then |
849 | . "$ACCOUNT_CONF_PATH" | |
4c3b3608 | 850 | fi |
851 | ||
8663fb7e | 852 | if [ "$IN_CRON" ] ; then |
853 | if [ ! "$_USER_PATH_EXPORTED" ] ; then | |
281aa349 | 854 | _USER_PATH_EXPORTED=1 |
855 | export PATH="$USER_PATH:$PATH" | |
856 | fi | |
857 | fi | |
858 | ||
8663fb7e | 859 | if [ -z "$API" ] ; then |
860 | if [ -z "$STAGE" ] ; then | |
4c3b3608 | 861 | API="$DEFAULT_CA" |
862 | else | |
863 | API="$STAGE_CA" | |
864 | _info "Using stage api:$API" | |
865 | fi | |
866 | fi | |
867 | ||
8663fb7e | 868 | if [ -z "$ACME_DIR" ] ; then |
4c3b3608 | 869 | ACME_DIR="/home/.acme" |
870 | fi | |
871 | ||
8663fb7e | 872 | if [ -z "$APACHE_CONF_BACKUP_DIR" ] ; then |
8a144f4d | 873 | APACHE_CONF_BACKUP_DIR="$LE_WORKING_DIR" |
4c3b3608 | 874 | fi |
875 | ||
8663fb7e | 876 | if [ -z "$USER_AGENT" ] ; then |
bbbdcb09 | 877 | USER_AGENT="$DEFAULT_USER_AGENT" |
878 | fi | |
879 | ||
880 | HTTP_HEADER="$LE_WORKING_DIR/http.header" | |
9aaf36cd | 881 | HTTP_ERROR_FILE="$LE_WORKING_DIR/http.err" |
bbbdcb09 | 882 | |
883 | WGET="wget -q" | |
8663fb7e | 884 | if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ] ; then |
bbbdcb09 | 885 | WGET="$WGET -d " |
886 | fi | |
887 | ||
888 | dp="$LE_WORKING_DIR/curl.dump" | |
4a0f23e2 | 889 | CURL="curl -L --silent" |
8663fb7e | 890 | if [ "$DEBUG" ] && [ "$DEBUG" -ge "2" ] ; then |
8f48168c | 891 | CURL="$CURL --trace-ascii $dp " |
bbbdcb09 | 892 | fi |
b2817897 | 893 | |
894 | _DEFAULT_ACCOUNT_KEY_PATH="$LE_WORKING_DIR/account.key" | |
8663fb7e | 895 | if [ -z "$ACCOUNT_KEY_PATH" ] ; then |
b2817897 | 896 | ACCOUNT_KEY_PATH="$_DEFAULT_ACCOUNT_KEY_PATH" |
4c3b3608 | 897 | fi |
b2817897 | 898 | |
a79b26af RD |
899 | _DEFAULT_CERT_HOME="$LE_WORKING_DIR" |
900 | if [ -z "$CERT_HOME" ] ; then | |
901 | CERT_HOME="$_DEFAULT_CERT_HOME" | |
902 | fi | |
903 | ||
b2817897 | 904 | domain="$1" |
4c3b3608 | 905 | |
8663fb7e | 906 | if [ -z "$domain" ] ; then |
4c3b3608 | 907 | return 0 |
908 | fi | |
909 | ||
b2817897 | 910 | domainhome="$CERT_HOME/$domain" |
4c3b3608 | 911 | mkdir -p "$domainhome" |
912 | ||
8663fb7e | 913 | if [ -z "$DOMAIN_PATH" ] ; then |
4c3b3608 | 914 | DOMAIN_PATH="$domainhome" |
915 | fi | |
8663fb7e | 916 | if [ -z "$DOMAIN_CONF" ] ; then |
1ad65f7d | 917 | DOMAIN_CONF="$domainhome/$domain.conf" |
4c3b3608 | 918 | fi |
919 | ||
8663fb7e | 920 | if [ -z "$DOMAIN_SSL_CONF" ] ; then |
1ad65f7d | 921 | DOMAIN_SSL_CONF="$domainhome/$domain.ssl.conf" |
4c3b3608 | 922 | fi |
923 | ||
8663fb7e | 924 | if [ -z "$CSR_PATH" ] ; then |
4c3b3608 | 925 | CSR_PATH="$domainhome/$domain.csr" |
926 | fi | |
8663fb7e | 927 | if [ -z "$CERT_KEY_PATH" ] ; then |
4c3b3608 | 928 | CERT_KEY_PATH="$domainhome/$domain.key" |
929 | fi | |
8663fb7e | 930 | if [ -z "$CERT_PATH" ] ; then |
4c3b3608 | 931 | CERT_PATH="$domainhome/$domain.cer" |
932 | fi | |
8663fb7e | 933 | if [ -z "$CA_CERT_PATH" ] ; then |
4c3b3608 | 934 | CA_CERT_PATH="$domainhome/ca.cer" |
935 | fi | |
8663fb7e | 936 | if [ -z "$CERT_FULLCHAIN_PATH" ] ; then |
850db6d4 | 937 | CERT_FULLCHAIN_PATH="$domainhome/fullchain.cer" |
caf1fc10 | 938 | fi |
8663fb7e | 939 | if [ -z "$CERT_PFX_PATH" ] ; then |
ac2d5123 | 940 | CERT_PFX_PATH="$domainhome/$domain.pfx" |
941 | fi | |
4c3b3608 | 942 | } |
943 | ||
944 | ||
945 | _apachePath() { | |
80a0a7b5 | 946 | if ! _exists apachectl ; then |
947 | _err "'apachecrl not found. It seems that apache is not installed, or you are not root user.'" | |
948 | _err "Please use webroot mode to try again." | |
949 | return 1 | |
950 | fi | |
4c3b3608 | 951 | httpdconfname="$(apachectl -V | grep SERVER_CONFIG_FILE= | cut -d = -f 2 | tr -d '"' )" |
78768e98 | 952 | _debug httpdconfname "$httpdconfname" |
dceb3aca | 953 | if _startswith "$httpdconfname" '/' ; then |
d62ee940 | 954 | httpdconf="$httpdconfname" |
c456d954 | 955 | httpdconfname="$(basename $httpdconfname)" |
d62ee940 | 956 | else |
957 | httpdroot="$(apachectl -V | grep HTTPD_ROOT= | cut -d = -f 2 | tr -d '"' )" | |
78768e98 | 958 | _debug httpdroot "$httpdroot" |
d62ee940 | 959 | httpdconf="$httpdroot/$httpdconfname" |
8f63baf7 | 960 | httpdconfname="$(basename $httpdconfname)" |
d62ee940 | 961 | fi |
78768e98 | 962 | _debug httpdconf "$httpdconf" |
8f63baf7 | 963 | _debug httpdconfname "$httpdconfname" |
78768e98 | 964 | if [ ! -f "$httpdconf" ] ; then |
965 | _err "Apache Config file not found" "$httpdconf" | |
4c3b3608 | 966 | return 1 |
967 | fi | |
968 | return 0 | |
969 | } | |
970 | ||
971 | _restoreApache() { | |
8663fb7e | 972 | if [ -z "$usingApache" ] ; then |
4c3b3608 | 973 | return 0 |
974 | fi | |
975 | _initpath | |
976 | if ! _apachePath ; then | |
977 | return 1 | |
978 | fi | |
979 | ||
8663fb7e | 980 | if [ ! -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname" ] ; then |
4c3b3608 | 981 | _debug "No config file to restore." |
982 | return 0 | |
983 | fi | |
984 | ||
ff3bce32 | 985 | cat "$APACHE_CONF_BACKUP_DIR/$httpdconfname" > "$httpdconf" |
5ef501c5 | 986 | _debug "Restored: $httpdconf." |
4c3b3608 | 987 | if ! apachectl -t ; then |
988 | _err "Sorry, restore apache config error, please contact me." | |
989 | return 1; | |
990 | fi | |
5ef501c5 | 991 | _debug "Restored successfully." |
4c3b3608 | 992 | rm -f "$APACHE_CONF_BACKUP_DIR/$httpdconfname" |
993 | return 0 | |
994 | } | |
995 | ||
996 | _setApache() { | |
997 | _initpath | |
998 | if ! _apachePath ; then | |
999 | return 1 | |
1000 | fi | |
1001 | ||
1002 | #backup the conf | |
5778811a | 1003 | _debug "Backup apache config file" "$httpdconf" |
8f63baf7 | 1004 | if ! cp "$httpdconf" "$APACHE_CONF_BACKUP_DIR/" ; then |
1005 | _err "Can not backup apache config file, so abort. Don't worry, your apache config is not changed." | |
1006 | _err "This might be a bug of $PROJECT_NAME , pleae report issue: $PROJECT" | |
1007 | return 1 | |
1008 | fi | |
4c3b3608 | 1009 | _info "JFYI, Config file $httpdconf is backuped to $APACHE_CONF_BACKUP_DIR/$httpdconfname" |
1010 | _info "In case there is an error that can not be restored automatically, you may try restore it yourself." | |
1011 | _info "The backup file will be deleted on sucess, just forget it." | |
1012 | ||
1013 | #add alias | |
b09d597c | 1014 | |
1015 | apacheVer="$(apachectl -V | grep "Server version:" | cut -d : -f 2 | cut -d " " -f 2 | cut -d '/' -f 2 )" | |
1016 | _debug "apacheVer" "$apacheVer" | |
1017 | apacheMajer="$(echo "$apacheVer" | cut -d . -f 1)" | |
1018 | apacheMinor="$(echo "$apacheVer" | cut -d . -f 2)" | |
1019 | ||
5778811a | 1020 | if [ "$apacheVer" ] && [ "$apacheMajer$apacheMinor" -ge "24" ] ; then |
b09d597c | 1021 | echo " |
4c3b3608 | 1022 | Alias /.well-known/acme-challenge $ACME_DIR |
1023 | ||
1024 | <Directory $ACME_DIR > | |
1025 | Require all granted | |
b09d597c | 1026 | </Directory> |
5778811a | 1027 | " >> "$httpdconf" |
b09d597c | 1028 | else |
1029 | echo " | |
1030 | Alias /.well-known/acme-challenge $ACME_DIR | |
1031 | ||
1032 | <Directory $ACME_DIR > | |
1033 | Order allow,deny | |
1034 | Allow from all | |
4c3b3608 | 1035 | </Directory> |
5778811a | 1036 | " >> "$httpdconf" |
b09d597c | 1037 | fi |
1038 | ||
4c3b3608 | 1039 | |
1040 | if ! apachectl -t ; then | |
1041 | _err "Sorry, apache config error, please contact me." | |
1042 | _restoreApache | |
1043 | return 1; | |
1044 | fi | |
1045 | ||
8663fb7e | 1046 | if [ ! -d "$ACME_DIR" ] ; then |
4c3b3608 | 1047 | mkdir -p "$ACME_DIR" |
1048 | chmod 755 "$ACME_DIR" | |
1049 | fi | |
1050 | ||
1051 | if ! apachectl graceful ; then | |
1052 | _err "Sorry, apachectl graceful error, please contact me." | |
1053 | _restoreApache | |
1054 | return 1; | |
1055 | fi | |
1056 | usingApache="1" | |
1057 | return 0 | |
1058 | } | |
1059 | ||
5ef501c5 | 1060 | _clearup() { |
4c3b3608 | 1061 | _stopserver $serverproc |
1062 | serverproc="" | |
1063 | _restoreApache | |
1064 | } | |
1065 | ||
1066 | # webroot removelevel tokenfile | |
1067 | _clearupwebbroot() { | |
1068 | __webroot="$1" | |
8663fb7e | 1069 | if [ -z "$__webroot" ] ; then |
4c3b3608 | 1070 | _debug "no webroot specified, skip" |
1071 | return 0 | |
1072 | fi | |
1073 | ||
8663fb7e | 1074 | if [ "$2" = '1' ] ; then |
4c3b3608 | 1075 | _debug "remove $__webroot/.well-known" |
1076 | rm -rf "$__webroot/.well-known" | |
8663fb7e | 1077 | elif [ "$2" = '2' ] ; then |
4c3b3608 | 1078 | _debug "remove $__webroot/.well-known/acme-challenge" |
1079 | rm -rf "$__webroot/.well-known/acme-challenge" | |
8663fb7e | 1080 | elif [ "$2" = '3' ] ; then |
4c3b3608 | 1081 | _debug "remove $__webroot/.well-known/acme-challenge/$3" |
1082 | rm -rf "$__webroot/.well-known/acme-challenge/$3" | |
1083 | else | |
1084 | _info "Skip for removelevel:$2" | |
1085 | fi | |
1086 | ||
1087 | return 0 | |
1088 | ||
1089 | } | |
1090 | ||
1091 | issue() { | |
8663fb7e | 1092 | if [ -z "$2" ] ; then |
a7b7355d | 1093 | echo "Usage: $PROJECT_ENTRY --issue -d a.com -w /path/to/webroot/a.com/ " |
4c3b3608 | 1094 | return 1 |
1095 | fi | |
1096 | Le_Webroot="$1" | |
1097 | Le_Domain="$2" | |
1098 | Le_Alt="$3" | |
1099 | Le_Keylength="$4" | |
1100 | Le_RealCertPath="$5" | |
1101 | Le_RealKeyPath="$6" | |
1102 | Le_RealCACertPath="$7" | |
1103 | Le_ReloadCmd="$8" | |
a63b05a9 | 1104 | Le_RealFullChainPath="$9" |
4c3b3608 | 1105 | |
eccec5f6 | 1106 | #remove these later. |
8663fb7e | 1107 | if [ "$Le_Webroot" = "dns-cf" ] ; then |
eccec5f6 | 1108 | Le_Webroot="dns_cf" |
1109 | fi | |
8663fb7e | 1110 | if [ "$Le_Webroot" = "dns-dp" ] ; then |
eccec5f6 | 1111 | Le_Webroot="dns_dp" |
1112 | fi | |
8663fb7e | 1113 | if [ "$Le_Webroot" = "dns-cx" ] ; then |
eccec5f6 | 1114 | Le_Webroot="dns_cx" |
1115 | fi | |
4c3b3608 | 1116 | |
eccec5f6 | 1117 | _initpath $Le_Domain |
1118 | ||
8663fb7e | 1119 | if [ -f "$DOMAIN_CONF" ] ; then |
a4270efa | 1120 | Le_NextRenewTime=$(grep "^Le_NextRenewTime=" "$DOMAIN_CONF" | cut -d '=' -f 2 | tr -d "'\"") |
1121 | _debug Le_NextRenewTime "$Le_NextRenewTime" | |
1122 | if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ $(date -u "+%s" ) -lt $Le_NextRenewTime ] ; then | |
4c3b3608 | 1123 | _info "Skip, Next renewal time is: $(grep "^Le_NextRenewTimeStr" "$DOMAIN_CONF" | cut -d '=' -f 2)" |
1124 | return 2 | |
1125 | fi | |
1126 | fi | |
96a46cfc | 1127 | |
4d2f38b0 | 1128 | _savedomainconf "Le_Domain" "$Le_Domain" |
1129 | _savedomainconf "Le_Alt" "$Le_Alt" | |
1130 | _savedomainconf "Le_Webroot" "$Le_Webroot" | |
1131 | _savedomainconf "Le_Keylength" "$Le_Keylength" | |
4c3b3608 | 1132 | |
8663fb7e | 1133 | if [ "$Le_Alt" = "no" ] ; then |
4c3b3608 | 1134 | Le_Alt="" |
1135 | fi | |
8663fb7e | 1136 | if [ "$Le_Keylength" = "no" ] ; then |
4c3b3608 | 1137 | Le_Keylength="" |
1138 | fi | |
4c3b3608 | 1139 | |
c53da1ef | 1140 | if _hasfield "$Le_Webroot" "no" ; then |
4c3b3608 | 1141 | _info "Standalone mode." |
5ef501c5 | 1142 | if ! _exists "nc" ; then |
4c3b3608 | 1143 | _err "Please install netcat(nc) tools first." |
1144 | return 1 | |
1145 | fi | |
1146 | ||
8663fb7e | 1147 | if [ -z "$Le_HTTPPort" ] ; then |
4c3b3608 | 1148 | Le_HTTPPort=80 |
1149 | fi | |
4d2f38b0 | 1150 | _savedomainconf "Le_HTTPPort" "$Le_HTTPPort" |
4c3b3608 | 1151 | |
251fc37c | 1152 | netprc="$(_ss "$Le_HTTPPort" | grep "$Le_HTTPPort")" |
8663fb7e | 1153 | if [ "$netprc" ] ; then |
4c3b3608 | 1154 | _err "$netprc" |
1155 | _err "tcp port $Le_HTTPPort is already used by $(echo "$netprc" | cut -d : -f 4)" | |
1156 | _err "Please stop it first" | |
1157 | return 1 | |
1158 | fi | |
1159 | fi | |
1160 | ||
c53da1ef | 1161 | if _hasfield "$Le_Webroot" "apache" ; then |
4c3b3608 | 1162 | if ! _setApache ; then |
1163 | _err "set up apache error. Report error to me." | |
1164 | return 1 | |
1165 | fi | |
4c3b3608 | 1166 | else |
1167 | usingApache="" | |
1168 | fi | |
1169 | ||
8663fb7e | 1170 | if [ ! -f "$ACCOUNT_KEY_PATH" ] ; then |
41e3eafa | 1171 | if ! createAccountKey $Le_Domain $Le_Keylength ; then |
1172 | _err "Create account key error." | |
8663fb7e | 1173 | if [ "$usingApache" ] ; then |
5ef501c5 | 1174 | _restoreApache |
1175 | fi | |
41e3eafa | 1176 | return 1 |
1177 | fi | |
1178 | fi | |
1179 | ||
166096dc | 1180 | if ! _calcjwk "$ACCOUNT_KEY_PATH" ; then |
8663fb7e | 1181 | if [ "$usingApache" ] ; then |
5ef501c5 | 1182 | _restoreApache |
1183 | fi | |
166096dc | 1184 | return 1 |
1185 | fi | |
1186 | ||
1187 | accountkey_json=$(echo -n "$jwk" | tr -d ' ' ) | |
88fab7d6 | 1188 | thumbprint=$(echo -n "$accountkey_json" | _digest "sha256" | _urlencode) |
4c3b3608 | 1189 | |
88fab7d6 | 1190 | accountkeyhash="$(cat "$ACCOUNT_KEY_PATH" | _digest "sha256" )" |
a63b05a9 | 1191 | accountkeyhash="$(echo $accountkeyhash$API | _digest "sha256" )" |
8663fb7e | 1192 | if [ "$accountkeyhash" != "$ACCOUNT_KEY_HASH" ] ; then |
166096dc | 1193 | _info "Registering account" |
1194 | regjson='{"resource": "new-reg", "agreement": "'$AGREEMENT'"}' | |
8663fb7e | 1195 | if [ "$ACCOUNT_EMAIL" ] ; then |
166096dc | 1196 | regjson='{"resource": "new-reg", "contact": ["mailto: '$ACCOUNT_EMAIL'"], "agreement": "'$AGREEMENT'"}' |
1197 | fi | |
1198 | _send_signed_request "$API/acme/new-reg" "$regjson" | |
1199 | ||
8663fb7e | 1200 | if [ "$code" = "" ] || [ "$code" = '201' ] ; then |
166096dc | 1201 | _info "Registered" |
1202 | echo $response > $LE_WORKING_DIR/account.json | |
8663fb7e | 1203 | elif [ "$code" = '409' ] ; then |
166096dc | 1204 | _info "Already registered" |
1205 | else | |
1206 | _err "Register account Error: $response" | |
1207 | _clearup | |
1208 | return 1 | |
1209 | fi | |
1210 | ACCOUNT_KEY_HASH="$accountkeyhash" | |
1211 | _saveaccountconf "ACCOUNT_KEY_HASH" "$ACCOUNT_KEY_HASH" | |
1212 | else | |
1213 | _info "Skip register account key" | |
1214 | fi | |
1215 | ||
8663fb7e | 1216 | if [ ! -f "$CERT_KEY_PATH" ] ; then |
41e3eafa | 1217 | if ! createDomainKey $Le_Domain $Le_Keylength ; then |
1218 | _err "Create domain key error." | |
5ef501c5 | 1219 | _clearup |
41e3eafa | 1220 | return 1 |
1221 | fi | |
4c3b3608 | 1222 | fi |
1223 | ||
1224 | if ! createCSR $Le_Domain $Le_Alt ; then | |
1225 | _err "Create CSR error." | |
5ef501c5 | 1226 | _clearup |
4c3b3608 | 1227 | return 1 |
1228 | fi | |
a63b05a9 | 1229 | |
4c3b3608 | 1230 | vlist="$Le_Vlist" |
1231 | # verify each domain | |
1232 | _info "Verify each domain" | |
1233 | sep='#' | |
8663fb7e | 1234 | if [ -z "$vlist" ] ; then |
4c3b3608 | 1235 | alldomains=$(echo "$Le_Domain,$Le_Alt" | tr ',' ' ' ) |
a63b05a9 | 1236 | _index=1 |
1237 | _currentRoot="" | |
4c3b3608 | 1238 | for d in $alldomains |
a63b05a9 | 1239 | do |
1240 | _info "Getting webroot for domain" $d | |
1241 | _w="$(echo $Le_Webroot | cut -d , -f $_index)" | |
1242 | _debug _w "$_w" | |
8663fb7e | 1243 | if [ "$_w" ] ; then |
a63b05a9 | 1244 | _currentRoot="$_w" |
1245 | fi | |
1246 | _debug "_currentRoot" "$_currentRoot" | |
00a50605 | 1247 | _index=$(_math $_index + 1) |
a63b05a9 | 1248 | |
1249 | vtype="$VTYPE_HTTP" | |
dceb3aca | 1250 | if _startswith "$_currentRoot" "dns" ; then |
a63b05a9 | 1251 | vtype="$VTYPE_DNS" |
1252 | fi | |
4c3b3608 | 1253 | _info "Getting token for domain" $d |
c4d8fd83 | 1254 | |
1255 | if ! _send_signed_request "$API/acme/new-authz" "{\"resource\": \"new-authz\", \"identifier\": {\"type\": \"dns\", \"value\": \"$d\"}}" ; then | |
1256 | _err "Can not get domain token." | |
1257 | _clearup | |
1258 | return 1 | |
1259 | fi | |
1260 | ||
8663fb7e | 1261 | if [ ! -z "$code" ] && [ ! "$code" = '201' ] ; then |
4c3b3608 | 1262 | _err "new-authz error: $response" |
1263 | _clearup | |
1264 | return 1 | |
1265 | fi | |
1266 | ||
230234e7 | 1267 | entry="$(printf "$response" | egrep -o '\{[^{]*"type":"'$vtype'"[^}]*')" |
4c3b3608 | 1268 | _debug entry "$entry" |
19539575 | 1269 | if [ -z "$entry" ] ; then |
1270 | _err "Error, can not get domain token $d" | |
1271 | _clearup | |
1272 | return 1 | |
1273 | fi | |
4c3b3608 | 1274 | token="$(printf "$entry" | egrep -o '"token":"[^"]*' | cut -d : -f 2 | tr -d '"')" |
1275 | _debug token $token | |
1276 | ||
1277 | uri="$(printf "$entry" | egrep -o '"uri":"[^"]*'| cut -d : -f 2,3 | tr -d '"' )" | |
1278 | _debug uri $uri | |
1279 | ||
1280 | keyauthorization="$token.$thumbprint" | |
1281 | _debug keyauthorization "$keyauthorization" | |
1282 | ||
a63b05a9 | 1283 | dvlist="$d$sep$keyauthorization$sep$uri$sep$vtype$sep$_currentRoot" |
4c3b3608 | 1284 | _debug dvlist "$dvlist" |
1285 | ||
1286 | vlist="$vlist$dvlist," | |
1287 | ||
1288 | done | |
1289 | ||
1290 | #add entry | |
1291 | dnsadded="" | |
1292 | ventries=$(echo "$vlist" | tr ',' ' ' ) | |
1293 | for ventry in $ventries | |
1294 | do | |
1295 | d=$(echo $ventry | cut -d $sep -f 1) | |
1296 | keyauthorization=$(echo $ventry | cut -d $sep -f 2) | |
a63b05a9 | 1297 | vtype=$(echo $ventry | cut -d $sep -f 4) |
1298 | _currentRoot=$(echo $ventry | cut -d $sep -f 5) | |
8663fb7e | 1299 | if [ "$vtype" = "$VTYPE_DNS" ] ; then |
4c3b3608 | 1300 | dnsadded='0' |
1301 | txtdomain="_acme-challenge.$d" | |
1302 | _debug txtdomain "$txtdomain" | |
c1c7d87b | 1303 | txt="$(echo -n $keyauthorization | _digest "sha256" | _urlencode)" |
4c3b3608 | 1304 | _debug txt "$txt" |
1305 | #dns | |
1306 | #1. check use api | |
1307 | d_api="" | |
8663fb7e | 1308 | if [ -f "$LE_WORKING_DIR/$d/$_currentRoot" ] ; then |
a63b05a9 | 1309 | d_api="$LE_WORKING_DIR/$d/$_currentRoot" |
8663fb7e | 1310 | elif [ -f "$LE_WORKING_DIR/$d/$_currentRoot.sh" ] ; then |
a63b05a9 | 1311 | d_api="$LE_WORKING_DIR/$d/$_currentRoot.sh" |
8663fb7e | 1312 | elif [ -f "$LE_WORKING_DIR/$_currentRoot" ] ; then |
a63b05a9 | 1313 | d_api="$LE_WORKING_DIR/$_currentRoot" |
8663fb7e | 1314 | elif [ -f "$LE_WORKING_DIR/$_currentRoot.sh" ] ; then |
a63b05a9 | 1315 | d_api="$LE_WORKING_DIR/$_currentRoot.sh" |
8663fb7e | 1316 | elif [ -f "$LE_WORKING_DIR/dnsapi/$_currentRoot" ] ; then |
a63b05a9 | 1317 | d_api="$LE_WORKING_DIR/dnsapi/$_currentRoot" |
8663fb7e | 1318 | elif [ -f "$LE_WORKING_DIR/dnsapi/$_currentRoot.sh" ] ; then |
a63b05a9 | 1319 | d_api="$LE_WORKING_DIR/dnsapi/$_currentRoot.sh" |
4c3b3608 | 1320 | fi |
1321 | _debug d_api "$d_api" | |
1322 | ||
8663fb7e | 1323 | if [ "$d_api" ] ; then |
4c3b3608 | 1324 | _info "Found domain api file: $d_api" |
1325 | else | |
1326 | _err "Add the following TXT record:" | |
1327 | _err "Domain: $txtdomain" | |
1328 | _err "TXT value: $txt" | |
1329 | _err "Please be aware that you prepend _acme-challenge. before your domain" | |
1330 | _err "so the resulting subdomain will be: $txtdomain" | |
1331 | continue | |
1332 | fi | |
4c3b3608 | 1333 | |
73b8b120 | 1334 | ( |
8663fb7e | 1335 | if ! . $d_api ; then |
73b8b120 | 1336 | _err "Load file $d_api error. Please check your api file and try again." |
1337 | return 1 | |
1338 | fi | |
1339 | ||
158f22f7 | 1340 | addcommand="${_currentRoot}_add" |
d53289d7 | 1341 | if ! _exists $addcommand ; then |
73b8b120 | 1342 | _err "It seems that your api file is not correct, it must have a function named: $addcommand" |
1343 | return 1 | |
1344 | fi | |
1345 | ||
1346 | if ! $addcommand $txtdomain $txt ; then | |
1347 | _err "Error add txt for domain:$txtdomain" | |
1348 | return 1 | |
1349 | fi | |
1350 | ) | |
4c3b3608 | 1351 | |
8663fb7e | 1352 | if [ "$?" != "0" ] ; then |
5ef501c5 | 1353 | _clearup |
4c3b3608 | 1354 | return 1 |
1355 | fi | |
1356 | dnsadded='1' | |
1357 | fi | |
1358 | done | |
1359 | ||
8663fb7e | 1360 | if [ "$dnsadded" = '0' ] ; then |
4d2f38b0 | 1361 | _savedomainconf "Le_Vlist" "$vlist" |
4c3b3608 | 1362 | _debug "Dns record not added yet, so, save to $DOMAIN_CONF and exit." |
1363 | _err "Please add the TXT records to the domains, and retry again." | |
5ef501c5 | 1364 | _clearup |
4c3b3608 | 1365 | return 1 |
1366 | fi | |
1367 | ||
1368 | fi | |
1369 | ||
8663fb7e | 1370 | if [ "$dnsadded" = '1' ] ; then |
4c3b3608 | 1371 | _info "Sleep 60 seconds for the txt records to take effect" |
1372 | sleep 60 | |
1373 | fi | |
1374 | ||
1375 | _debug "ok, let's start to verify" | |
a63b05a9 | 1376 | |
4c3b3608 | 1377 | ventries=$(echo "$vlist" | tr ',' ' ' ) |
1378 | for ventry in $ventries | |
1379 | do | |
1380 | d=$(echo $ventry | cut -d $sep -f 1) | |
1381 | keyauthorization=$(echo $ventry | cut -d $sep -f 2) | |
1382 | uri=$(echo $ventry | cut -d $sep -f 3) | |
a63b05a9 | 1383 | vtype=$(echo $ventry | cut -d $sep -f 4) |
1384 | _currentRoot=$(echo $ventry | cut -d $sep -f 5) | |
4c3b3608 | 1385 | _info "Verifying:$d" |
1386 | _debug "d" "$d" | |
1387 | _debug "keyauthorization" "$keyauthorization" | |
1388 | _debug "uri" "$uri" | |
1389 | removelevel="" | |
1390 | token="" | |
a63b05a9 | 1391 | |
1392 | _debug "_currentRoot" "$_currentRoot" | |
1393 | ||
1394 | ||
8663fb7e | 1395 | if [ "$vtype" = "$VTYPE_HTTP" ] ; then |
1396 | if [ "$_currentRoot" = "no" ] ; then | |
4c3b3608 | 1397 | _info "Standalone mode server" |
1398 | _startserver "$keyauthorization" & | |
8663fb7e | 1399 | if [ "$?" != "0" ] ; then |
5ef501c5 | 1400 | _clearup |
6fc1447f | 1401 | return 1 |
1402 | fi | |
4c3b3608 | 1403 | serverproc="$!" |
1404 | sleep 2 | |
1405 | _debug serverproc $serverproc | |
6fc1447f | 1406 | |
4c3b3608 | 1407 | else |
8663fb7e | 1408 | if [ "$_currentRoot" = "apache" ] ; then |
6f930641 | 1409 | wellknown_path="$ACME_DIR" |
1410 | else | |
a63b05a9 | 1411 | wellknown_path="$_currentRoot/.well-known/acme-challenge" |
8663fb7e | 1412 | if [ ! -d "$_currentRoot/.well-known" ] ; then |
6f930641 | 1413 | removelevel='1' |
8663fb7e | 1414 | elif [ ! -d "$_currentRoot/.well-known/acme-challenge" ] ; then |
6f930641 | 1415 | removelevel='2' |
1416 | else | |
1417 | removelevel='3' | |
1418 | fi | |
4c3b3608 | 1419 | fi |
6f930641 | 1420 | |
4c3b3608 | 1421 | _debug wellknown_path "$wellknown_path" |
6f930641 | 1422 | |
7939b419 | 1423 | token="$(printf "%s" "$keyauthorization" | cut -d '.' -f 1)" |
4c3b3608 | 1424 | _debug "writing token:$token to $wellknown_path/$token" |
1425 | ||
1426 | mkdir -p "$wellknown_path" | |
7939b419 | 1427 | printf "%s" "$keyauthorization" > "$wellknown_path/$token" |
8663fb7e | 1428 | if [ ! "$usingApache" ] ; then |
a63b05a9 | 1429 | webroot_owner=$(_stat $_currentRoot) |
df886ffa | 1430 | _debug "Changing owner/group of .well-known to $webroot_owner" |
a63b05a9 | 1431 | chown -R $webroot_owner "$_currentRoot/.well-known" |
df886ffa | 1432 | fi |
4c3b3608 | 1433 | |
1434 | fi | |
1435 | fi | |
1436 | ||
c4d8fd83 | 1437 | if ! _send_signed_request $uri "{\"resource\": \"challenge\", \"keyAuthorization\": \"$keyauthorization\"}" ; then |
1438 | _err "$d:Can not get challenge: $response" | |
1439 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" | |
1440 | _clearup | |
1441 | return 1 | |
1442 | fi | |
4c3b3608 | 1443 | |
8663fb7e | 1444 | if [ ! -z "$code" ] && [ ! "$code" = '202' ] ; then |
c60883ef | 1445 | _err "$d:Challenge error: $response" |
a63b05a9 | 1446 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" |
4c3b3608 | 1447 | _clearup |
1448 | return 1 | |
1449 | fi | |
1450 | ||
6fc1447f | 1451 | waittimes=0 |
8663fb7e | 1452 | if [ -z "$MAX_RETRY_TIMES" ] ; then |
6fc1447f | 1453 | MAX_RETRY_TIMES=30 |
1454 | fi | |
1455 | ||
2ee5d873 | 1456 | while true ; do |
00a50605 | 1457 | waittimes=$(_math $waittimes + 1) |
8663fb7e | 1458 | if [ "$waittimes" -ge "$MAX_RETRY_TIMES" ] ; then |
6fc1447f | 1459 | _err "$d:Timeout" |
1460 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" | |
1461 | _clearup | |
1462 | return 1 | |
1463 | fi | |
1464 | ||
4c3b3608 | 1465 | _debug "sleep 5 secs to verify" |
1466 | sleep 5 | |
1467 | _debug "checking" | |
9aaf36cd | 1468 | response="$(_get $uri)" |
8663fb7e | 1469 | if [ "$?" != "0" ] ; then |
c60883ef | 1470 | _err "$d:Verify error:$response" |
a63b05a9 | 1471 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" |
4c3b3608 | 1472 | _clearup |
1473 | return 1 | |
1474 | fi | |
9aaf36cd | 1475 | _debug2 original "$response" |
1476 | ||
1477 | response="$(echo "$response" | _normalizeJson )" | |
7012b91f | 1478 | _debug2 response "$response" |
4c3b3608 | 1479 | |
7d91e35f | 1480 | status=$(echo $response | egrep -o '"status":"[^"]*' | cut -d : -f 2 | tr -d '"') |
8663fb7e | 1481 | if [ "$status" = "valid" ] ; then |
4c3b3608 | 1482 | _info "Success" |
1483 | _stopserver $serverproc | |
1484 | serverproc="" | |
a63b05a9 | 1485 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" |
4c3b3608 | 1486 | break; |
1487 | fi | |
1488 | ||
8663fb7e | 1489 | if [ "$status" = "invalid" ] ; then |
6e180343 | 1490 | error="$(echo $response | tr -d "\r\n" | egrep -o '"error":{[^}]*}')" |
b7ec6789 | 1491 | _debug2 error "$error" |
1492 | errordetail="$(echo $error | grep -o '"detail": *"[^"]*"' | cut -d '"' -f 4)" | |
1493 | _debug2 errordetail "$errordetail" | |
1494 | if [ "$errordetail" ] ; then | |
1495 | _err "$d:Verify error:$errordetail" | |
1496 | else | |
1497 | _err "$d:Verify error:$error" | |
1498 | fi | |
a63b05a9 | 1499 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" |
4c3b3608 | 1500 | _clearup |
1501 | return 1; | |
1502 | fi | |
1503 | ||
8663fb7e | 1504 | if [ "$status" = "pending" ] ; then |
4c3b3608 | 1505 | _info "Pending" |
1506 | else | |
1507 | _err "$d:Verify error:$response" | |
a63b05a9 | 1508 | _clearupwebbroot "$_currentRoot" "$removelevel" "$token" |
4c3b3608 | 1509 | _clearup |
1510 | return 1 | |
1511 | fi | |
1512 | ||
1513 | done | |
1514 | ||
1515 | done | |
1516 | ||
1517 | _clearup | |
1518 | _info "Verify finished, start to sign." | |
fa8311dc | 1519 | der="$(_getfile "${CSR_PATH}" "${BEGIN_CSR}" "${END_CSR}" | tr -d "\r\n" | _urlencode)" |
c4d8fd83 | 1520 | |
1521 | if ! _send_signed_request "$API/acme/new-cert" "{\"resource\": \"new-cert\", \"csr\": \"$der\"}" "needbase64" ; then | |
1522 | _err "Sign failed." | |
1523 | return 1 | |
1524 | fi | |
4c3b3608 | 1525 | |
1526 | ||
bbbdcb09 | 1527 | Le_LinkCert="$(grep -i -o '^Location.*$' $HTTP_HEADER | head -1 | tr -d "\r\n" | cut -d " " -f 2)" |
4d2f38b0 | 1528 | _savedomainconf "Le_LinkCert" "$Le_LinkCert" |
4c3b3608 | 1529 | |
8663fb7e | 1530 | if [ "$Le_LinkCert" ] ; then |
88fab7d6 | 1531 | echo "$BEGIN_CERT" > "$CERT_PATH" |
c60883ef | 1532 | _get "$Le_LinkCert" | _base64 "multiline" >> "$CERT_PATH" |
88fab7d6 | 1533 | echo "$END_CERT" >> "$CERT_PATH" |
4c3b3608 | 1534 | _info "Cert success." |
1535 | cat "$CERT_PATH" | |
1536 | ||
1537 | _info "Your cert is in $CERT_PATH" | |
caf1fc10 | 1538 | cp "$CERT_PATH" "$CERT_FULLCHAIN_PATH" |
281aa349 | 1539 | |
8663fb7e | 1540 | if [ ! "$USER_PATH" ] || [ ! "$IN_CRON" ] ; then |
281aa349 | 1541 | USER_PATH="$PATH" |
1542 | _saveaccountconf "USER_PATH" "$USER_PATH" | |
1543 | fi | |
4c3b3608 | 1544 | fi |
1545 | ||
1546 | ||
8663fb7e | 1547 | if [ -z "$Le_LinkCert" ] ; then |
eae29099 | 1548 | response="$(echo $response | _dbase64 "multiline" | _normalizeJson )" |
4c3b3608 | 1549 | _err "Sign failed: $(echo "$response" | grep -o '"detail":"[^"]*"')" |
1550 | return 1 | |
1551 | fi | |
1552 | ||
4d2f38b0 | 1553 | _cleardomainconf "Le_Vlist" |
4c3b3608 | 1554 | |
bbbdcb09 | 1555 | Le_LinkIssuer=$(grep -i '^Link' $HTTP_HEADER | head -1 | cut -d " " -f 2| cut -d ';' -f 1 | tr -d '<>' ) |
4d2f38b0 | 1556 | _savedomainconf "Le_LinkIssuer" "$Le_LinkIssuer" |
4c3b3608 | 1557 | |
8663fb7e | 1558 | if [ "$Le_LinkIssuer" ] ; then |
88fab7d6 | 1559 | echo "$BEGIN_CERT" > "$CA_CERT_PATH" |
c60883ef | 1560 | _get "$Le_LinkIssuer" | _base64 "multiline" >> "$CA_CERT_PATH" |
88fab7d6 | 1561 | echo "$END_CERT" >> "$CA_CERT_PATH" |
4c3b3608 | 1562 | _info "The intermediate CA cert is in $CA_CERT_PATH" |
caf1fc10 | 1563 | cat "$CA_CERT_PATH" >> "$CERT_FULLCHAIN_PATH" |
1564 | _info "And the full chain certs is there: $CERT_FULLCHAIN_PATH" | |
4c3b3608 | 1565 | fi |
1566 | ||
1567 | Le_CertCreateTime=$(date -u "+%s") | |
4d2f38b0 | 1568 | _savedomainconf "Le_CertCreateTime" "$Le_CertCreateTime" |
4c3b3608 | 1569 | |
1570 | Le_CertCreateTimeStr=$(date -u ) | |
4d2f38b0 | 1571 | _savedomainconf "Le_CertCreateTimeStr" "$Le_CertCreateTimeStr" |
4c3b3608 | 1572 | |
8663fb7e | 1573 | if [ -z "$Le_RenewalDays" ] || [ "$Le_RenewalDays" -lt "0" ] || [ "$Le_RenewalDays" -gt "80" ] ; then |
4c3b3608 | 1574 | Le_RenewalDays=80 |
1575 | fi | |
1576 | ||
4d2f38b0 | 1577 | _savedomainconf "Le_RenewalDays" "$Le_RenewalDays" |
00a50605 | 1578 | |
1579 | Le_NextRenewTime=$(_math $Le_CertCreateTime + $Le_RenewalDays \* 24 \* 60 \* 60) | |
4d2f38b0 | 1580 | _savedomainconf "Le_NextRenewTime" "$Le_NextRenewTime" |
4c3b3608 | 1581 | |
1582 | Le_NextRenewTimeStr=$( _time2str $Le_NextRenewTime ) | |
4d2f38b0 | 1583 | _savedomainconf "Le_NextRenewTimeStr" "$Le_NextRenewTimeStr" |
4c3b3608 | 1584 | |
1585 | ||
01f54558 | 1586 | _output="$(installcert $Le_Domain "$Le_RealCertPath" "$Le_RealKeyPath" "$Le_RealCACertPath" "$Le_ReloadCmd" "$Le_RealFullChainPath" 2>&1)" |
1587 | _ret="$?" | |
1588 | if [ "$_ret" = "9" ] ; then | |
ca2a96b3 | 1589 | #ignore the empty install error. |
1590 | return 0 | |
1591 | fi | |
01f54558 | 1592 | if [ "$_ret" != "0" ] ; then |
1593 | _err "$_output" | |
1594 | return 1 | |
1595 | fi | |
4c3b3608 | 1596 | } |
1597 | ||
1598 | renew() { | |
1599 | Le_Domain="$1" | |
8663fb7e | 1600 | if [ -z "$Le_Domain" ] ; then |
a7b7355d | 1601 | _err "Usage: $PROJECT_ENTRY --renew -d domain.com" |
4c3b3608 | 1602 | return 1 |
1603 | fi | |
1604 | ||
1605 | _initpath $Le_Domain | |
1606 | ||
8663fb7e | 1607 | if [ ! -f "$DOMAIN_CONF" ] ; then |
4c3b3608 | 1608 | _info "$Le_Domain is not a issued domain, skip." |
1609 | return 0; | |
1610 | fi | |
1611 | ||
8663fb7e | 1612 | . "$DOMAIN_CONF" |
1613 | if [ -z "$FORCE" ] && [ "$Le_NextRenewTime" ] && [ "$(date -u "+%s" )" -lt "$Le_NextRenewTime" ] ; then | |
4c3b3608 | 1614 | _info "Skip, Next renewal time is: $Le_NextRenewTimeStr" |
1615 | return 2 | |
1616 | fi | |
1617 | ||
1618 | IS_RENEW="1" | |
a63b05a9 | 1619 | issue "$Le_Webroot" "$Le_Domain" "$Le_Alt" "$Le_Keylength" "$Le_RealCertPath" "$Le_RealKeyPath" "$Le_RealCACertPath" "$Le_ReloadCmd" "$Le_RealFullChainPath" |
4c3b3608 | 1620 | local res=$? |
1621 | IS_RENEW="" | |
1622 | ||
1623 | return $res | |
1624 | } | |
1625 | ||
1626 | renewAll() { | |
1627 | _initpath | |
b2817897 | 1628 | for d in $(ls -F ${CERT_HOME}/ | grep [^.].*[.].*/$ ) ; do |
4c3b3608 | 1629 | d=$(echo $d | cut -d '/' -f 1) |
4d2f38b0 | 1630 | ( |
1631 | _info "Renew: $d" | |
1632 | renew "$d" | |
1633 | ) | |
4c3b3608 | 1634 | done |
1635 | ||
1636 | } | |
1637 | ||
1638 | installcert() { | |
1639 | Le_Domain="$1" | |
8663fb7e | 1640 | if [ -z "$Le_Domain" ] ; then |
a7b7355d | 1641 | 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 | 1642 | return 1 |
1643 | fi | |
1644 | ||
1645 | Le_RealCertPath="$2" | |
1646 | Le_RealKeyPath="$3" | |
1647 | Le_RealCACertPath="$4" | |
1648 | Le_ReloadCmd="$5" | |
a63b05a9 | 1649 | Le_RealFullChainPath="$6" |
4c3b3608 | 1650 | |
1651 | _initpath $Le_Domain | |
1652 | ||
4d2f38b0 | 1653 | _savedomainconf "Le_RealCertPath" "$Le_RealCertPath" |
1654 | _savedomainconf "Le_RealCACertPath" "$Le_RealCACertPath" | |
1655 | _savedomainconf "Le_RealKeyPath" "$Le_RealKeyPath" | |
1656 | _savedomainconf "Le_ReloadCmd" "$Le_ReloadCmd" | |
1657 | _savedomainconf "Le_RealFullChainPath" "$Le_RealFullChainPath" | |
4c3b3608 | 1658 | |
4d2f38b0 | 1659 | if [ "$Le_RealCertPath" = "no" ] ; then |
1660 | Le_RealCertPath="" | |
1661 | fi | |
1662 | if [ "$Le_RealKeyPath" = "no" ] ; then | |
1663 | Le_RealKeyPath="" | |
1664 | fi | |
1665 | if [ "$Le_RealCACertPath" = "no" ] ; then | |
1666 | Le_RealCACertPath="" | |
1667 | fi | |
1668 | if [ "$Le_ReloadCmd" = "no" ] ; then | |
1669 | Le_ReloadCmd="" | |
1670 | fi | |
1671 | if [ "$Le_RealFullChainPath" = "no" ] ; then | |
1672 | Le_RealFullChainPath="" | |
1673 | fi | |
1674 | ||
1675 | _installed="0" | |
8663fb7e | 1676 | if [ "$Le_RealCertPath" ] ; then |
4d2f38b0 | 1677 | _installed=1 |
1678 | _info "Installing cert to:$Le_RealCertPath" | |
8663fb7e | 1679 | if [ -f "$Le_RealCertPath" ] ; then |
ff3bce32 | 1680 | cp "$Le_RealCertPath" "$Le_RealCertPath".bak |
4c3b3608 | 1681 | fi |
1682 | cat "$CERT_PATH" > "$Le_RealCertPath" | |
1683 | fi | |
1684 | ||
8663fb7e | 1685 | if [ "$Le_RealCACertPath" ] ; then |
4d2f38b0 | 1686 | _installed=1 |
1687 | _info "Installing CA to:$Le_RealCACertPath" | |
8663fb7e | 1688 | if [ "$Le_RealCACertPath" = "$Le_RealCertPath" ] ; then |
4c3b3608 | 1689 | echo "" >> "$Le_RealCACertPath" |
1690 | cat "$CA_CERT_PATH" >> "$Le_RealCACertPath" | |
1691 | else | |
8663fb7e | 1692 | if [ -f "$Le_RealCACertPath" ] ; then |
ff3bce32 | 1693 | cp "$Le_RealCACertPath" "$Le_RealCACertPath".bak |
78552b18 | 1694 | fi |
4c3b3608 | 1695 | cat "$CA_CERT_PATH" > "$Le_RealCACertPath" |
1696 | fi | |
1697 | fi | |
1698 | ||
1699 | ||
8663fb7e | 1700 | if [ "$Le_RealKeyPath" ] ; then |
4d2f38b0 | 1701 | _installed=1 |
1702 | _info "Installing key to:$Le_RealKeyPath" | |
8663fb7e | 1703 | if [ -f "$Le_RealKeyPath" ] ; then |
ff3bce32 | 1704 | cp "$Le_RealKeyPath" "$Le_RealKeyPath".bak |
4c3b3608 | 1705 | fi |
1706 | cat "$CERT_KEY_PATH" > "$Le_RealKeyPath" | |
1707 | fi | |
a63b05a9 | 1708 | |
8663fb7e | 1709 | if [ "$Le_RealFullChainPath" ] ; then |
4d2f38b0 | 1710 | _installed=1 |
1711 | _info "Installing full chain to:$Le_RealFullChainPath" | |
8663fb7e | 1712 | if [ -f "$Le_RealFullChainPath" ] ; then |
ff3bce32 | 1713 | cp "$Le_RealFullChainPath" "$Le_RealFullChainPath".bak |
a63b05a9 | 1714 | fi |
1715 | cat "$CERT_FULLCHAIN_PATH" > "$Le_RealFullChainPath" | |
1716 | fi | |
4c3b3608 | 1717 | |
8663fb7e | 1718 | if [ "$Le_ReloadCmd" ] ; then |
4d2f38b0 | 1719 | _installed=1 |
4c3b3608 | 1720 | _info "Run Le_ReloadCmd: $Le_ReloadCmd" |
4d2f38b0 | 1721 | if (cd "$DOMAIN_PATH" && eval "$Le_ReloadCmd") ; then |
1722 | _info "Reload success." | |
1723 | else | |
1724 | _err "Reload error for :$Le_Domain" | |
1725 | fi | |
1726 | fi | |
1727 | ||
1728 | if [ "$_installed" = "0" ] ; then | |
1729 | _err "Nothing to install. You don't specify any parameter." | |
ca2a96b3 | 1730 | return 9 |
4c3b3608 | 1731 | fi |
1732 | ||
1733 | } | |
1734 | ||
1735 | installcronjob() { | |
1736 | _initpath | |
77546ea5 | 1737 | if ! _exists "crontab" ; then |
1738 | _err "crontab doesn't exist, so, we can not install cron jobs." | |
1739 | _err "All your certs will not be renewed automatically." | |
a7b7355d | 1740 | _err "You must add your own cron job to call '$PROJECT_ENTRY --cron' everyday." |
77546ea5 | 1741 | return 1 |
1742 | fi | |
1743 | ||
4c3b3608 | 1744 | _info "Installing cron job" |
a7b7355d | 1745 | if ! crontab -l | grep "$PROJECT_ENTRY --cron" ; then |
8663fb7e | 1746 | if [ -f "$LE_WORKING_DIR/$PROJECT_ENTRY" ] ; then |
a7b7355d | 1747 | lesh="\"$LE_WORKING_DIR\"/$PROJECT_ENTRY" |
4c3b3608 | 1748 | else |
a7b7355d | 1749 | _err "Can not install cronjob, $PROJECT_ENTRY not found." |
4c3b3608 | 1750 | return 1 |
1751 | fi | |
a7b7355d | 1752 | crontab -l | { cat; echo "0 0 * * * $lesh --cron --home \"$LE_WORKING_DIR\" > /dev/null"; } | crontab - |
4c3b3608 | 1753 | fi |
8663fb7e | 1754 | if [ "$?" != "0" ] ; then |
4c3b3608 | 1755 | _err "Install cron job failed. You need to manually renew your certs." |
1756 | _err "Or you can add cronjob by yourself:" | |
a7b7355d | 1757 | _err "$lesh --cron --home \"$LE_WORKING_DIR\" > /dev/null" |
4c3b3608 | 1758 | return 1 |
1759 | fi | |
1760 | } | |
1761 | ||
1762 | uninstallcronjob() { | |
37db5b81 | 1763 | if ! _exists "crontab" ; then |
1764 | return | |
1765 | fi | |
4c3b3608 | 1766 | _info "Removing cron job" |
a7b7355d | 1767 | cr="$(crontab -l | grep "$PROJECT_ENTRY --cron")" |
8663fb7e | 1768 | if [ "$cr" ] ; then |
a7b7355d | 1769 | crontab -l | sed "/$PROJECT_ENTRY --cron/d" | crontab - |
1770 | LE_WORKING_DIR="$(echo "$cr" | cut -d ' ' -f 9 | tr -d '"')" | |
4c3b3608 | 1771 | _info LE_WORKING_DIR "$LE_WORKING_DIR" |
1772 | fi | |
1773 | _initpath | |
a7b7355d | 1774 | |
4c3b3608 | 1775 | } |
1776 | ||
6cb415f5 | 1777 | revoke() { |
1778 | Le_Domain="$1" | |
8663fb7e | 1779 | if [ -z "$Le_Domain" ] ; then |
a7b7355d | 1780 | echo "Usage: $PROJECT_ENTRY --revoke -d domain.com" |
6cb415f5 | 1781 | return 1 |
1782 | fi | |
1783 | ||
1784 | _initpath $Le_Domain | |
8663fb7e | 1785 | if [ ! -f "$DOMAIN_CONF" ] ; then |
6cb415f5 | 1786 | _err "$Le_Domain is not a issued domain, skip." |
1787 | return 1; | |
1788 | fi | |
1789 | ||
8663fb7e | 1790 | if [ ! -f "$CERT_PATH" ] ; then |
6cb415f5 | 1791 | _err "Cert for $Le_Domain $CERT_PATH is not found, skip." |
1792 | return 1 | |
1793 | fi | |
1794 | ||
1795 | cert="$(_getfile "${CERT_PATH}" "${BEGIN_CERT}" "${END_CERT}"| tr -d "\r\n" | _urlencode)" | |
1796 | ||
8663fb7e | 1797 | if [ -z "$cert" ] ; then |
6cb415f5 | 1798 | _err "Cert for $Le_Domain is empty found, skip." |
1799 | return 1 | |
1800 | fi | |
1801 | ||
1802 | data="{\"resource\": \"revoke-cert\", \"certificate\": \"$cert\"}" | |
1803 | uri="$API/acme/revoke-cert" | |
1804 | ||
1805 | _info "Try domain key first." | |
1806 | if _send_signed_request $uri "$data" "" "$CERT_KEY_PATH"; then | |
8663fb7e | 1807 | if [ -z "$response" ] ; then |
6cb415f5 | 1808 | _info "Revoke success." |
1809 | rm -f $CERT_PATH | |
1810 | return 0 | |
1811 | else | |
1812 | _err "Revoke error by domain key." | |
1813 | _err "$resource" | |
1814 | fi | |
1815 | fi | |
1816 | ||
1817 | _info "Then try account key." | |
1818 | ||
1819 | if _send_signed_request $uri "$data" "" "$ACCOUNT_KEY_PATH" ; then | |
8663fb7e | 1820 | if [ -z "$response" ] ; then |
6cb415f5 | 1821 | _info "Revoke success." |
1822 | rm -f $CERT_PATH | |
1823 | return 0 | |
1824 | else | |
1825 | _err "Revoke error." | |
1826 | _debug "$resource" | |
1827 | fi | |
1828 | fi | |
1829 | return 1 | |
1830 | } | |
4c3b3608 | 1831 | |
1832 | # Detect profile file if not specified as environment variable | |
1833 | _detect_profile() { | |
a63b05a9 | 1834 | if [ -n "$PROFILE" -a -f "$PROFILE" ] ; then |
4c3b3608 | 1835 | echo "$PROFILE" |
1836 | return | |
1837 | fi | |
1838 | ||
1839 | local DETECTED_PROFILE | |
1840 | DETECTED_PROFILE='' | |
1841 | local SHELLTYPE | |
1842 | SHELLTYPE="$(basename "/$SHELL")" | |
1843 | ||
8663fb7e | 1844 | if [ "$SHELLTYPE" = "bash" ] ; then |
1845 | if [ -f "$HOME/.bashrc" ] ; then | |
4c3b3608 | 1846 | DETECTED_PROFILE="$HOME/.bashrc" |
8663fb7e | 1847 | elif [ -f "$HOME/.bash_profile" ] ; then |
4c3b3608 | 1848 | DETECTED_PROFILE="$HOME/.bash_profile" |
1849 | fi | |
8663fb7e | 1850 | elif [ "$SHELLTYPE" = "zsh" ] ; then |
4c3b3608 | 1851 | DETECTED_PROFILE="$HOME/.zshrc" |
1852 | fi | |
1853 | ||
8663fb7e | 1854 | if [ -z "$DETECTED_PROFILE" ] ; then |
1855 | if [ -f "$HOME/.profile" ] ; then | |
4c3b3608 | 1856 | DETECTED_PROFILE="$HOME/.profile" |
8663fb7e | 1857 | elif [ -f "$HOME/.bashrc" ] ; then |
4c3b3608 | 1858 | DETECTED_PROFILE="$HOME/.bashrc" |
8663fb7e | 1859 | elif [ -f "$HOME/.bash_profile" ] ; then |
4c3b3608 | 1860 | DETECTED_PROFILE="$HOME/.bash_profile" |
8663fb7e | 1861 | elif [ -f "$HOME/.zshrc" ] ; then |
4c3b3608 | 1862 | DETECTED_PROFILE="$HOME/.zshrc" |
1863 | fi | |
1864 | fi | |
1865 | ||
8663fb7e | 1866 | if [ ! -z "$DETECTED_PROFILE" ] ; then |
4c3b3608 | 1867 | echo "$DETECTED_PROFILE" |
1868 | fi | |
1869 | } | |
1870 | ||
1871 | _initconf() { | |
1872 | _initpath | |
8663fb7e | 1873 | if [ ! -f "$ACCOUNT_CONF_PATH" ] ; then |
d53289d7 | 1874 | echo "#ACCOUNT_CONF_PATH=xxxx |
1875 | ||
1876 | #Account configurations: | |
4c3b3608 | 1877 | #Here are the supported macros, uncomment them to make them take effect. |
d53289d7 | 1878 | |
4c3b3608 | 1879 | #ACCOUNT_EMAIL=aaa@aaa.com # the account email used to register account. |
5fd3f21b | 1880 | #ACCOUNT_KEY_PATH=\"/path/to/account.key\" |
b2817897 | 1881 | #CERT_HOME=\"/path/to/cert/home\" |
4c3b3608 | 1882 | |
1883 | #STAGE=1 # Use the staging api | |
1884 | #FORCE=1 # Force to issue cert | |
1885 | #DEBUG=1 # Debug mode | |
1886 | ||
166096dc | 1887 | #ACCOUNT_KEY_HASH=account key hash |
1888 | ||
635695ec | 1889 | USER_AGENT=\"$USER_AGENT\" |
281aa349 | 1890 | |
1891 | #USER_PATH="" | |
1892 | ||
4c3b3608 | 1893 | #dns api |
1894 | ####################### | |
1895 | #Cloudflare: | |
1896 | #api key | |
3d49985a | 1897 | #CF_Key=\"sdfsdfsdfljlbjkljlkjsdfoiwje\" |
4c3b3608 | 1898 | #account email |
3d49985a | 1899 | #CF_Email=\"xxxx@sss.com\" |
4c3b3608 | 1900 | |
1901 | ####################### | |
1902 | #Dnspod.cn: | |
1903 | #api key id | |
3d49985a | 1904 | #DP_Id=\"1234\" |
4c3b3608 | 1905 | #api key |
3d49985a | 1906 | #DP_Key=\"sADDsdasdgdsf\" |
4c3b3608 | 1907 | |
1908 | ####################### | |
1909 | #Cloudxns.com: | |
3d49985a | 1910 | #CX_Key=\"1234\" |
4c3b3608 | 1911 | # |
3d49985a | 1912 | #CX_Secret=\"sADDsdasdgdsf\" |
4c3b3608 | 1913 | |
1914 | " > $ACCOUNT_CONF_PATH | |
1915 | fi | |
1916 | } | |
1917 | ||
c60883ef | 1918 | _precheck() { |
1919 | if ! _exists "curl" && ! _exists "wget"; then | |
1920 | _err "Please install curl or wget first, we need to access http resources." | |
4c3b3608 | 1921 | return 1 |
1922 | fi | |
1923 | ||
c60883ef | 1924 | if ! _exists "crontab" ; then |
77546ea5 | 1925 | _err "It is recommended to install crontab first. try to install 'cron, crontab, crontabs or vixie-cron'." |
c60883ef | 1926 | _err "We need to set cron job to renew the certs automatically." |
77546ea5 | 1927 | _err "Otherwise, your certs will not be able to be renewed automatically." |
8663fb7e | 1928 | if [ -z "$FORCE" ] ; then |
a7b7355d | 1929 | _err "Please add '--force' and try install again to go without crontab." |
1930 | _err "./$PROJECT_ENTRY --install --force" | |
77546ea5 | 1931 | return 1 |
1932 | fi | |
4c3b3608 | 1933 | fi |
1934 | ||
c60883ef | 1935 | if ! _exists "openssl" ; then |
1936 | _err "Please install openssl first." | |
1937 | _err "We need openssl to generate keys." | |
4c3b3608 | 1938 | return 1 |
1939 | fi | |
1940 | ||
c60883ef | 1941 | if ! _exists "nc" ; then |
1942 | _err "It is recommended to install nc first, try to install 'nc' or 'netcat'." | |
1943 | _err "We use nc for standalone server if you use standalone mode." | |
1944 | _err "If you don't use standalone mode, just ignore this warning." | |
1945 | fi | |
1946 | ||
1947 | return 0 | |
1948 | } | |
1949 | ||
0a7c9364 | 1950 | _setShebang() { |
1951 | _file="$1" | |
1952 | _shebang="$2" | |
1953 | if [ -z "$_shebang" ] ; then | |
1954 | _err "Usage: file shebang" | |
1955 | return 1 | |
1956 | fi | |
1957 | cp "$_file" "$_file.tmp" | |
1958 | echo "$_shebang" > "$_file" | |
1959 | sed -n 2,99999p "$_file.tmp" >> "$_file" | |
1960 | rm -f "$_file.tmp" | |
1961 | } | |
1962 | ||
94dc5f33 | 1963 | _installalias() { |
1964 | _initpath | |
1965 | ||
1966 | _envfile="$LE_WORKING_DIR/$PROJECT_ENTRY.env" | |
1967 | if [ "$_upgrading" ] && [ "$_upgrading" = "1" ] ; then | |
1968 | echo "$(cat $_envfile)" | sed "s|^LE_WORKING_DIR.*$||" > "$_envfile" | |
1969 | echo "$(cat $_envfile)" | sed "s|^alias le.*$||" > "$_envfile" | |
1970 | echo "$(cat $_envfile)" | sed "s|^alias le.sh.*$||" > "$_envfile" | |
1971 | fi | |
1972 | ||
1786a5e5 | 1973 | _setopt "$_envfile" "export LE_WORKING_DIR" "=" "\"$LE_WORKING_DIR\"" |
94dc5f33 | 1974 | _setopt "$_envfile" "alias $PROJECT_ENTRY" "=" "\"$LE_WORKING_DIR/$PROJECT_ENTRY\"" |
1975 | ||
1976 | _profile="$(_detect_profile)" | |
1977 | if [ "$_profile" ] ; then | |
1978 | _debug "Found profile: $_profile" | |
1979 | _setopt "$_profile" ". \"$_envfile\"" | |
1980 | _info "OK, Close and reopen your terminal to start using $PROJECT_NAME" | |
1981 | else | |
1982 | _info "No profile is found, you will need to go into $LE_WORKING_DIR to use $PROJECT_NAME" | |
1983 | fi | |
1984 | ||
1985 | ||
1986 | #for csh | |
1987 | _cshfile="$LE_WORKING_DIR/$PROJECT_ENTRY.csh" | |
94dc5f33 | 1988 | _csh_profile="$HOME/.cshrc" |
1989 | if [ -f "$_csh_profile" ] ; then | |
6626371d | 1990 | _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\"" |
1991 | _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY\"" | |
94dc5f33 | 1992 | _setopt "$_csh_profile" "source \"$_cshfile\"" |
1993 | fi | |
acafa585 | 1994 | |
1995 | #for tcsh | |
1996 | _tcsh_profile="$HOME/.tcshrc" | |
1997 | if [ -f "$_tcsh_profile" ] ; then | |
1998 | _setopt "$_cshfile" "setenv LE_WORKING_DIR" " " "\"$LE_WORKING_DIR\"" | |
1999 | _setopt "$_cshfile" "alias $PROJECT_ENTRY" " " "\"$LE_WORKING_DIR/$PROJECT_ENTRY\"" | |
2000 | _setopt "$_tcsh_profile" "source \"$_cshfile\"" | |
2001 | fi | |
94dc5f33 | 2002 | |
2003 | } | |
2004 | ||
c60883ef | 2005 | install() { |
635695ec | 2006 | |
c60883ef | 2007 | if ! _initpath ; then |
2008 | _err "Install failed." | |
4c3b3608 | 2009 | return 1 |
2010 | fi | |
635695ec | 2011 | |
c60883ef | 2012 | if ! _precheck ; then |
2013 | _err "Pre-check failed, can not install." | |
4c3b3608 | 2014 | return 1 |
2015 | fi | |
c60883ef | 2016 | |
6cc11ffb | 2017 | #convert from le |
8663fb7e | 2018 | if [ -d "$HOME/.le" ] ; then |
6cc11ffb | 2019 | for envfile in "le.env" "le.sh.env" |
2020 | do | |
8663fb7e | 2021 | if [ -f "$HOME/.le/$envfile" ] ; then |
6cc11ffb | 2022 | if grep "le.sh" "$HOME/.le/$envfile" >/dev/null ; then |
2023 | _upgrading="1" | |
2024 | _info "You are upgrading from le.sh" | |
2025 | _info "Renaming \"$HOME/.le\" to $LE_WORKING_DIR" | |
2026 | mv "$HOME/.le" "$LE_WORKING_DIR" | |
2027 | mv "$LE_WORKING_DIR/$envfile" "$LE_WORKING_DIR/$PROJECT_ENTRY.env" | |
2028 | break; | |
2029 | fi | |
2030 | fi | |
2031 | done | |
2032 | fi | |
2033 | ||
4c3b3608 | 2034 | _info "Installing to $LE_WORKING_DIR" |
635695ec | 2035 | |
4a0f23e2 | 2036 | if ! mkdir -p "$LE_WORKING_DIR" ; then |
2037 | _err "Can not craete working dir: $LE_WORKING_DIR" | |
2038 | return 1 | |
2039 | fi | |
2040 | ||
762978f8 | 2041 | chmod 700 "$LE_WORKING_DIR" |
2042 | ||
a7b7355d | 2043 | cp $PROJECT_ENTRY "$LE_WORKING_DIR/" && chmod +x "$LE_WORKING_DIR/$PROJECT_ENTRY" |
4c3b3608 | 2044 | |
8663fb7e | 2045 | if [ "$?" != "0" ] ; then |
a7b7355d | 2046 | _err "Install failed, can not copy $PROJECT_ENTRY" |
4c3b3608 | 2047 | return 1 |
2048 | fi | |
2049 | ||
a7b7355d | 2050 | _info "Installed to $LE_WORKING_DIR/$PROJECT_ENTRY" |
4c3b3608 | 2051 | |
94dc5f33 | 2052 | _installalias |
4c3b3608 | 2053 | |
8663fb7e | 2054 | if [ -d "dnsapi" ] ; then |
6ed1c718 | 2055 | mkdir -p $LE_WORKING_DIR/dnsapi |
2056 | cp dnsapi/* $LE_WORKING_DIR/dnsapi/ | |
2057 | fi | |
d53289d7 | 2058 | |
8663fb7e | 2059 | if [ ! -f "$ACCOUNT_CONF_PATH" ] ; then |
4c3b3608 | 2060 | _initconf |
2061 | fi | |
6cc11ffb | 2062 | |
8663fb7e | 2063 | if [ "$_DEFAULT_ACCOUNT_CONF_PATH" != "$ACCOUNT_CONF_PATH" ] ; then |
635695ec | 2064 | _setopt "$_DEFAULT_ACCOUNT_CONF_PATH" "ACCOUNT_CONF_PATH" "=" "\"$ACCOUNT_CONF_PATH\"" |
6cc11ffb | 2065 | fi |
2066 | ||
8663fb7e | 2067 | if [ "$_DEFAULT_CERT_HOME" != "$CERT_HOME" ] ; then |
b2817897 | 2068 | _saveaccountconf "CERT_HOME" "$CERT_HOME" |
2069 | fi | |
2070 | ||
8663fb7e | 2071 | if [ "$_DEFAULT_ACCOUNT_KEY_PATH" != "$ACCOUNT_KEY_PATH" ] ; then |
b2817897 | 2072 | _saveaccountconf "ACCOUNT_KEY_PATH" "$ACCOUNT_KEY_PATH" |
2073 | fi | |
2074 | ||
d53289d7 | 2075 | installcronjob |
0a7c9364 | 2076 | |
641989fd | 2077 | if [ -z "$NO_DETECT_SH" ] ; then |
2078 | #Modify shebang | |
2079 | if _exists bash ; then | |
2080 | _info "Good, bash is installed, change the shebang to use bash as prefered." | |
2081 | _shebang='#!/usr/bin/env bash' | |
2082 | _setShebang "$LE_WORKING_DIR/$PROJECT_ENTRY" "$_shebang" | |
2083 | if [ -d "$LE_WORKING_DIR/dnsapi" ] ; then | |
2084 | for _apifile in $(ls "$LE_WORKING_DIR/dnsapi/"*.sh) ; do | |
2085 | _setShebang "$_apifile" "$_shebang" | |
2086 | done | |
2087 | fi | |
0a7c9364 | 2088 | fi |
2089 | fi | |
2090 | ||
4c3b3608 | 2091 | _info OK |
2092 | } | |
2093 | ||
2094 | uninstall() { | |
2095 | uninstallcronjob | |
2096 | _initpath | |
2097 | ||
2098 | _profile="$(_detect_profile)" | |
8663fb7e | 2099 | if [ "$_profile" ] ; then |
7203a1c1 | 2100 | text="$(cat $_profile)" |
94dc5f33 | 2101 | echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.env\"$||" > "$_profile" |
4c3b3608 | 2102 | fi |
2103 | ||
94dc5f33 | 2104 | _csh_profile="$HOME/.cshrc" |
2105 | if [ -f "$_csh_profile" ] ; then | |
2106 | text="$(cat $_csh_profile)" | |
2107 | echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" > "$_csh_profile" | |
2108 | fi | |
2109 | ||
acafa585 | 2110 | _tcsh_profile="$HOME/.tcshrc" |
2111 | if [ -f "$_tcsh_profile" ] ; then | |
2112 | text="$(cat $_tcsh_profile)" | |
2113 | echo "$text" | sed "s|^.*\"$LE_WORKING_DIR/$PROJECT_NAME.csh\"$||" > "$_tcsh_profile" | |
2114 | fi | |
2115 | ||
a7b7355d | 2116 | rm -f $LE_WORKING_DIR/$PROJECT_ENTRY |
4c3b3608 | 2117 | _info "The keys and certs are in $LE_WORKING_DIR, you can remove them by yourself." |
2118 | ||
2119 | } | |
2120 | ||
2121 | cron() { | |
281aa349 | 2122 | IN_CRON=1 |
4c3b3608 | 2123 | renewAll |
281aa349 | 2124 | IN_CRON="" |
4c3b3608 | 2125 | } |
2126 | ||
2127 | version() { | |
a63b05a9 | 2128 | echo "$PROJECT" |
2129 | echo "v$VER" | |
4c3b3608 | 2130 | } |
2131 | ||
2132 | showhelp() { | |
2133 | version | |
a7b7355d | 2134 | echo "Usage: $PROJECT_ENTRY command ...[parameters].... |
a63b05a9 | 2135 | Commands: |
2136 | --help, -h Show this help message. | |
2137 | --version, -v Show version info. | |
a7b7355d | 2138 | --install Install $PROJECT_NAME to your system. |
2139 | --uninstall Uninstall $PROJECT_NAME, and uninstall the cron job. | |
a63b05a9 | 2140 | --issue Issue a cert. |
2141 | --installcert Install the issued cert to apache/nginx or any other server. | |
2142 | --renew, -r Renew a cert. | |
2143 | --renewAll Renew all the certs | |
2144 | --revoke Revoke a cert. | |
2145 | --installcronjob Install the cron job to renew certs, you don't need to call this. The 'install' command can automatically install the cron job. | |
2146 | --uninstallcronjob Uninstall the cron job. The 'uninstall' command can do this automatically. | |
2147 | --cron Run cron job to renew all the certs. | |
2148 | --toPkcs Export the certificate and key to a pfx file. | |
2149 | --createAccountKey, -cak Create an account private key, professional use. | |
2150 | --createDomainKey, -cdk Create an domain private key, professional use. | |
2151 | --createCSR, -ccsr Create CSR , professional use. | |
2152 | ||
2153 | Parameters: | |
2154 | --domain, -d domain.tld Specifies a domain, used to issue, renew or revoke etc. | |
2155 | --force, -f Used to force to install or force to renew a cert immediately. | |
2156 | --staging, --test Use staging server, just for test. | |
2157 | --debug Output debug info. | |
2158 | ||
2159 | --webroot, -w /path/to/webroot Specifies the web root folder for web root mode. | |
2160 | --standalone Use standalone mode. | |
2161 | --apache Use apache mode. | |
eccec5f6 | 2162 | --dns [dns_cf|dns_dp|dns_cx|/path/to/api/file] Use dns mode or dns api. |
a63b05a9 | 2163 | |
2164 | --keylength, -k [2048] Specifies the domain key length: 2048, 3072, 4096, 8192 or ec-256, ec-384. | |
2165 | --accountkeylength, -ak [2048] Specifies the account key length. | |
2166 | ||
2167 | These parameters are to install the cert to nginx/apache or anyother server after issue/renew a cert: | |
2168 | ||
2169 | --certpath /path/to/real/cert/file After issue/renew, the cert will be copied to this path. | |
2170 | --keypath /path/to/real/key/file After issue/renew, the key will be copied to this path. | |
2171 | --capath /path/to/real/ca/file After issue/renew, the intermediate cert will be copied to this path. | |
2172 | --fullchainpath /path/to/fullchain/file After issue/renew, the fullchain cert will be copied to this path. | |
2173 | ||
2174 | --reloadcmd \"service nginx reload\" After issue/renew, it's used to reload the server. | |
2175 | ||
2176 | --accountconf Specifies a customized account config file. | |
635695ec | 2177 | --home Specifies the home dir for $PROJECT_NAME . |
39c8f79f | 2178 | --certhome Specifies the home dir to save all the certs, only valid for '--install' command. |
635695ec | 2179 | --useragent Specifies the user agent string. it will be saved for future use too. |
b5eb4b90 | 2180 | --accountemail Specifies the account email for registering, Only valid for the '--install' command. |
06625071 | 2181 | --accountkey Specifies the account key path, Only valid for the '--install' command. |
2182 | --days Specifies the days to renew the cert when using '--issue' command. The max value is 80 days. | |
39c8f79f | 2183 | --httpport Specifies the standalone listening port. Only valid if the server is behind a reverse proxy or load balancer. |
4c3b3608 | 2184 | " |
2185 | } | |
2186 | ||
4a0f23e2 | 2187 | _installOnline() { |
2188 | _info "Installing from online archive." | |
8663fb7e | 2189 | if [ ! "$BRANCH" ] ; then |
4a0f23e2 | 2190 | BRANCH="master" |
2191 | fi | |
2192 | _initpath | |
2193 | target="$PROJECT/archive/$BRANCH.tar.gz" | |
2194 | _info "Downloading $target" | |
2195 | localname="$BRANCH.tar.gz" | |
2196 | if ! _get "$target" > $localname ; then | |
2197 | _debug "Download error." | |
2198 | return 1 | |
2199 | fi | |
2200 | _info "Extracting $localname" | |
2201 | tar xzf $localname | |
6cc11ffb | 2202 | cd "$PROJECT_NAME-$BRANCH" |
a7b7355d | 2203 | chmod +x $PROJECT_ENTRY |
2204 | if ./$PROJECT_ENTRY install ; then | |
4a0f23e2 | 2205 | _info "Install success!" |
2206 | fi | |
2207 | ||
2208 | cd .. | |
6cc11ffb | 2209 | rm -rf "$PROJECT_NAME-$BRANCH" |
4a0f23e2 | 2210 | rm -f "$localname" |
2211 | } | |
2212 | ||
a63b05a9 | 2213 | |
2214 | _process() { | |
2215 | _CMD="" | |
2216 | _domain="" | |
2217 | _altdomains="no" | |
2218 | _webroot="" | |
2219 | _keylength="no" | |
2220 | _accountkeylength="no" | |
2221 | _certpath="no" | |
2222 | _keypath="no" | |
2223 | _capath="no" | |
2224 | _fullchainpath="no" | |
4d2f38b0 | 2225 | _reloadcmd="" |
a63b05a9 | 2226 | _password="" |
635695ec | 2227 | _accountconf="" |
2228 | _useragent="" | |
b5eb4b90 | 2229 | _accountemail="" |
2230 | _accountkey="" | |
b2817897 | 2231 | _certhome="" |
39c8f79f | 2232 | _httpport="" |
8663fb7e | 2233 | while [ ${#} -gt 0 ] ; do |
a63b05a9 | 2234 | case "${1}" in |
2235 | ||
2236 | --help|-h) | |
2237 | showhelp | |
2238 | return | |
2239 | ;; | |
2240 | --version|-v) | |
2241 | version | |
2242 | return | |
2243 | ;; | |
2244 | --install) | |
2245 | _CMD="install" | |
2246 | ;; | |
2247 | --uninstall) | |
2248 | _CMD="uninstall" | |
2249 | ;; | |
2250 | --issue) | |
2251 | _CMD="issue" | |
2252 | ;; | |
2253 | --installcert|-i) | |
2254 | _CMD="installcert" | |
2255 | ;; | |
2256 | --renew|-r) | |
2257 | _CMD="renew" | |
2258 | ;; | |
4d2f38b0 | 2259 | --renewAll|--renewall) |
a63b05a9 | 2260 | _CMD="renewAll" |
2261 | ;; | |
2262 | --revoke) | |
2263 | _CMD="revoke" | |
2264 | ;; | |
2265 | --installcronjob) | |
2266 | _CMD="installcronjob" | |
2267 | ;; | |
2268 | --uninstallcronjob) | |
2269 | _CMD="uninstallcronjob" | |
2270 | ;; | |
2271 | --cron) | |
2272 | _CMD="cron" | |
2273 | ;; | |
2274 | --toPkcs) | |
2275 | _CMD="toPkcs" | |
2276 | ;; | |
2277 | --createAccountKey|--createaccountkey|-cak) | |
2278 | _CMD="createAccountKey" | |
2279 | ;; | |
2280 | --createDomainKey|--createdomainkey|-cdk) | |
2281 | _CMD="createDomainKey" | |
2282 | ;; | |
2283 | --createCSR|--createcsr|-ccr) | |
2284 | _CMD="createCSR" | |
2285 | ;; | |
2286 | ||
2287 | ||
2288 | --domain|-d) | |
2289 | _dvalue="$2" | |
2290 | ||
8663fb7e | 2291 | if [ -z "$_dvalue" ] || _startswith "$_dvalue" "-" ; then |
a63b05a9 | 2292 | _err "'$_dvalue' is not a valid domain for parameter '$1'" |
2293 | return 1 | |
2294 | fi | |
2295 | ||
8663fb7e | 2296 | if [ -z "$_domain" ] ; then |
a63b05a9 | 2297 | _domain="$_dvalue" |
2298 | else | |
8663fb7e | 2299 | if [ "$_altdomains" = "no" ] ; then |
a63b05a9 | 2300 | _altdomains="$_dvalue" |
2301 | else | |
2302 | _altdomains="$_altdomains,$_dvalue" | |
2303 | fi | |
2304 | fi | |
2305 | shift | |
2306 | ;; | |
2307 | ||
2308 | --force|-f) | |
2309 | FORCE="1" | |
2310 | ;; | |
2311 | --staging|--test) | |
2312 | STAGE="1" | |
2313 | ;; | |
2314 | --debug) | |
8663fb7e | 2315 | if [ -z "$2" ] || _startswith "$2" "-" ; then |
a63b05a9 | 2316 | DEBUG="1" |
2317 | else | |
2318 | DEBUG="$2" | |
2319 | shift | |
6fc1447f | 2320 | fi |
a63b05a9 | 2321 | ;; |
a63b05a9 | 2322 | --webroot|-w) |
2323 | wvalue="$2" | |
8663fb7e | 2324 | if [ -z "$_webroot" ] ; then |
a63b05a9 | 2325 | _webroot="$wvalue" |
2326 | else | |
2327 | _webroot="$_webroot,$wvalue" | |
2328 | fi | |
2329 | shift | |
2330 | ;; | |
2331 | --standalone) | |
2332 | wvalue="no" | |
8663fb7e | 2333 | if [ -z "$_webroot" ] ; then |
a63b05a9 | 2334 | _webroot="$wvalue" |
2335 | else | |
2336 | _webroot="$_webroot,$wvalue" | |
2337 | fi | |
2338 | ;; | |
2339 | --apache) | |
2340 | wvalue="apache" | |
8663fb7e | 2341 | if [ -z "$_webroot" ] ; then |
a63b05a9 | 2342 | _webroot="$wvalue" |
2343 | else | |
2344 | _webroot="$_webroot,$wvalue" | |
2345 | fi | |
2346 | ;; | |
2347 | --dns) | |
2348 | wvalue="dns" | |
dceb3aca | 2349 | if ! _startswith "$2" "-" ; then |
a63b05a9 | 2350 | wvalue="$2" |
2351 | shift | |
2352 | fi | |
8663fb7e | 2353 | if [ -z "$_webroot" ] ; then |
a63b05a9 | 2354 | _webroot="$wvalue" |
2355 | else | |
2356 | _webroot="$_webroot,$wvalue" | |
2357 | fi | |
2358 | ;; | |
2359 | --keylength|-k) | |
2360 | _keylength="$2" | |
2361 | accountkeylength="$2" | |
2362 | shift | |
2363 | ;; | |
2364 | --accountkeylength|-ak) | |
2365 | accountkeylength="$2" | |
2366 | shift | |
2367 | ;; | |
2368 | ||
2369 | --certpath) | |
2370 | _certpath="$2" | |
2371 | shift | |
2372 | ;; | |
2373 | --keypath) | |
2374 | _keypath="$2" | |
2375 | shift | |
2376 | ;; | |
2377 | --capath) | |
2378 | _capath="$2" | |
2379 | shift | |
2380 | ;; | |
2381 | --fullchainpath) | |
2382 | _fullchainpath="$2" | |
2383 | shift | |
2384 | ;; | |
635695ec | 2385 | --reloadcmd|--reloadCmd) |
a63b05a9 | 2386 | _reloadcmd="$2" |
2387 | shift | |
2388 | ;; | |
2389 | --password) | |
2390 | _password="$2" | |
2391 | shift | |
2392 | ;; | |
2393 | --accountconf) | |
635695ec | 2394 | _accountconf="$2" |
2395 | ACCOUNT_CONF_PATH="$_accountconf" | |
a7b7355d | 2396 | shift |
a63b05a9 | 2397 | ;; |
a7b7355d | 2398 | --home) |
a63b05a9 | 2399 | LE_WORKING_DIR="$2" |
a7b7355d | 2400 | shift |
a63b05a9 | 2401 | ;; |
b2817897 | 2402 | --certhome) |
2403 | _certhome="$2" | |
2404 | CERT_HOME="$_certhome" | |
2405 | shift | |
2406 | ;; | |
635695ec | 2407 | --useragent) |
2408 | _useragent="$2" | |
2409 | USER_AGENT="$_useragent" | |
2410 | shift | |
2411 | ;; | |
b5eb4b90 | 2412 | --accountemail ) |
2413 | _accountemail="$2" | |
2414 | ACCOUNT_EMAIL="$_accountemail" | |
2415 | shift | |
2416 | ;; | |
2417 | --accountkey ) | |
2418 | _accountkey="$2" | |
2419 | ACCOUNT_KEY_PATH="$_accountkey" | |
2420 | shift | |
2421 | ;; | |
06625071 | 2422 | --days ) |
2423 | _days="$2" | |
2424 | Le_RenewalDays="$_days" | |
2425 | shift | |
2426 | ;; | |
39c8f79f | 2427 | --httpport ) |
2428 | _httpport="$2" | |
2429 | Le_HTTPPort="$_httpport" | |
2430 | shift | |
2431 | ;; | |
a63b05a9 | 2432 | *) |
2433 | _err "Unknown parameter : $1" | |
2434 | return 1 | |
2435 | ;; | |
2436 | esac | |
2437 | ||
2438 | shift 1 | |
2439 | done | |
2440 | ||
2441 | ||
2442 | case "${_CMD}" in | |
2443 | install) install ;; | |
2444 | uninstall) uninstall ;; | |
2445 | issue) | |
70a55875 | 2446 | issue "$_webroot" "$_domain" "$_altdomains" "$_keylength" "$_certpath" "$_keypath" "$_capath" "$_reloadcmd" "$_fullchainpath" |
a63b05a9 | 2447 | ;; |
2448 | installcert) | |
3ed4102a | 2449 | installcert "$_domain" "$_certpath" "$_keypath" "$_capath" "$_reloadcmd" "$_fullchainpath" |
a63b05a9 | 2450 | ;; |
2451 | renew) | |
2452 | renew "$_domain" | |
2453 | ;; | |
2454 | renewAll) | |
2455 | renewAll | |
2456 | ;; | |
2457 | revoke) | |
2458 | revoke "$_domain" | |
2459 | ;; | |
2460 | installcronjob) installcronjob ;; | |
2461 | uninstallcronjob) uninstallcronjob ;; | |
2462 | cron) cron ;; | |
2463 | toPkcs) | |
2464 | toPkcs "$_domain" "$_password" | |
2465 | ;; | |
2466 | createAccountKey) | |
2467 | createAccountKey "$_domain" "$_accountkeylength" | |
2468 | ;; | |
2469 | createDomainKey) | |
2470 | createDomainKey "$_domain" "$_keylength" | |
2471 | ;; | |
2472 | createCSR) | |
2473 | createCSR "$_domain" "$_altdomains" | |
2474 | ;; | |
2475 | ||
2476 | *) | |
2477 | _err "Invalid command: $_CMD" | |
2478 | showhelp; | |
2479 | return 1 | |
2480 | ;; | |
2481 | esac | |
d3595686 | 2482 | _ret="$?" |
2483 | if [ "$_ret" != "0" ] ; then | |
2484 | return $_ret | |
2485 | fi | |
a63b05a9 | 2486 | |
8663fb7e | 2487 | if [ "$_useragent" ] ; then |
635695ec | 2488 | _saveaccountconf "USER_AGENT" "$_useragent" |
2489 | fi | |
8663fb7e | 2490 | if [ "$_accountemail" ] ; then |
b5eb4b90 | 2491 | _saveaccountconf "ACCOUNT_EMAIL" "$_accountemail" |
2492 | fi | |
b2817897 | 2493 | |
635695ec | 2494 | |
a63b05a9 | 2495 | } |
2496 | ||
2497 | ||
8663fb7e | 2498 | if [ "$INSTALLONLINE" ] ; then |
d1f97fc8 | 2499 | INSTALLONLINE="" |
4a0f23e2 | 2500 | _installOnline $BRANCH |
2501 | exit | |
2502 | fi | |
4c3b3608 | 2503 | |
8663fb7e | 2504 | if [ -z "$1" ] ; then |
4c3b3608 | 2505 | showhelp |
2506 | else | |
036e9d10 | 2507 | if echo "$1" | grep "^-" >/dev/null 2>&1 ; then |
a63b05a9 | 2508 | _process "$@" |
2509 | else | |
2510 | "$@" | |
2511 | fi | |
4c3b3608 | 2512 | fi |
a63b05a9 | 2513 | |
2514 |