v-check-letsencrypt-domain 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #!/bin/bash
  2. # info: check letsencrypt domain
  3. # options: USER DOMAIN
  4. #
  5. # The function check and validates domain with LetsEncript
  6. #----------------------------------------------------------#
  7. # Variable&Function #
  8. #----------------------------------------------------------#
  9. # Argument definition
  10. user=$1
  11. domain=$(idn -t --quiet -u "$2" )
  12. domain=$(echo $domain | tr '[:upper:]' '[:lower:]')
  13. # Includes
  14. source $VESTA/func/main.sh
  15. source $VESTA/conf/vesta.conf
  16. # encode base64
  17. encode_base64() {
  18. cat |base64 |tr '+/' '-_' |tr -d '\r\n='
  19. }
  20. #----------------------------------------------------------#
  21. # Verifications #
  22. #----------------------------------------------------------#
  23. check_args '2' "$#" 'USER DOMAIN'
  24. is_format_valid 'user' 'domain'
  25. is_system_enabled "$WEB_SYSTEM" 'WEB_SYSTEM'
  26. is_object_valid 'user' 'USER' "$user"
  27. is_object_unsuspended 'user' 'USER' "$user"
  28. if [ ! -e "$USER_DATA/ssl/le.conf" ]; then
  29. check_result $E_NOTEXIST "LetsEncrypt key doesn't exist"
  30. fi
  31. check_domain=$(grep -w "$domain'" $USER_DATA/web.conf)
  32. if [ -z "$check_domain" ]; then
  33. check_result $E_NOTEXIST "domain $domain doesn't exist"
  34. fi
  35. #----------------------------------------------------------#
  36. # Action #
  37. #----------------------------------------------------------#
  38. source $USER_DATA/ssl/le.conf
  39. api='https://acme-v01.api.letsencrypt.org'
  40. r_domain=$(echo "$check_domain" |cut -f 2 -d \')
  41. key="$USER_DATA/ssl/user.key"
  42. exponent="$EXPONENT"
  43. modulus="$MODULUS"
  44. thumb="$THUMB"
  45. # Defining JWK header
  46. header='{"e":"'$exponent'","kty":"RSA","n":"'"$modulus"'"}'
  47. header='{"alg":"RS256","jwk":'"$header"'}'
  48. # Requesting nonce
  49. nonce=$(curl -s -I "$api/directory" |grep Nonce |cut -f2 -d \ |tr -d '\r\n')
  50. protected=$(echo -n '{"nonce":"'"$nonce"'"}' |encode_base64)
  51. # Defining ACME query (request challenge)
  52. query='{"resource":"new-authz","identifier"'
  53. query=$query':{"type":"dns","value":"'"$domain"'"}}'
  54. payload=$(echo -n "$query" |encode_base64)
  55. signature=$(printf "%s" "$protected.$payload" |\
  56. openssl dgst -sha256 -binary -sign "$key" |encode_base64)
  57. data='{"header":'"$header"',"protected":"'"$protected"'",'
  58. data=$data'"payload":"'"$payload"'","signature":"'"$signature"'"}'
  59. # Sending request to LetsEncrypt API
  60. answer=$(curl -s -i -d "$data" "$api/acme/new-authz")
  61. # Checking http answer status
  62. status=$(echo "$answer" |grep HTTP/1.1 |tail -n1 |cut -f2 -d ' ')
  63. if [[ "$status" -ne "201" ]]; then
  64. check_result $E_CONNECT "LetsEncrypt challenge request $status"
  65. fi
  66. # Parsing domain nonce,token and uri
  67. nonce=$(echo "$answer" |grep Nonce |cut -f2 -d \ |tr -d '\r\n')
  68. protected=$(echo -n '{"nonce":"'"$nonce"'"}' |encode_base64)
  69. token=$(echo "$answer" |tr ',' '\n' |grep -A 3 http-01 |grep token)
  70. token=$(echo "$token" |cut -f 4 -d \")
  71. uri=$(echo "$answer" |tr ',' '\n' |grep -A 3 http-01 |grep uri)
  72. uri=$(echo "$uri" |cut -f 4 -d \")
  73. # Adding location wrapper for request challenge
  74. if [ "$WEB_SYSTEM" = 'nginx' ] || [ "$PROXY_SYSTEM" = 'nginx' ]; then
  75. conf="$HOMEDIR/$user/conf/web/nginx.$r_domain.conf_letsencrypt"
  76. if [ ! -e "$conf" ]; then
  77. echo 'location ~ "^/\.well-known/acme-challenge/(.*)$" {' > $conf
  78. echo ' default_type text/plain;' >> $conf
  79. echo ' return 200 "$1.'$thumb'";' >> $conf
  80. echo '}' >> $conf
  81. if [ ! -z "$PROXY_SYSTEM" ]; then
  82. $BIN/v-restart-proxy
  83. check_result $? "Proxy restart failed" >/dev/null
  84. else
  85. $BIN/v-restart-web
  86. check_result $? "Web restart failed" >/dev/null
  87. fi
  88. fi
  89. else
  90. acme="$HOMEDIR/$user/web/$r_domain/public_html/.well-known/acme-challenge"
  91. echo "$token" > $acme/$token.$thumb
  92. chown -R $user:$user $HOMEDIR/$user/web/$r_domain/public_html/.well-known
  93. fi
  94. # Defining ACME query (request validation)
  95. query='{"resource":"challenge","type":"http-01","keyAuthorization"'
  96. query=$query':"'$token.$thumb'","token":"'$token'"}'
  97. payload=$(echo -n "$query" |encode_base64)
  98. signature=$(printf "%s" "$protected.$payload" |\
  99. openssl dgst -sha256 -binary -sign "$key" |encode_base64)
  100. data='{"header":'"$header"',"protected":"'"$protected"'",'
  101. data=$data'"payload":"'"$payload"'","signature":"'"$signature"'"}'
  102. # Sending request to LetsEncrypt API
  103. answer=$(curl -s -i -d "$data" "$uri")
  104. # Checking domain validation status
  105. status=$(echo $answer |tr ',' '\n' |grep status |cut -f 4 -d \")
  106. location=$(echo "$answer" |grep Location: |awk '{print $2}' |tr -d '\r\n')
  107. while [ "$status" = 'pending' ] ; do
  108. answer=$(curl -s -i "$location")
  109. status=$(echo "$answer" |tr ',' '\n' |grep status |cut -f 4 -d \")
  110. done
  111. if [ "$status" = 'invalid' ]; then
  112. detail="$(echo $answer |tr ',' '\n' |grep detail |cut -f 4 -d \")"
  113. check_result $E_CONNECT "$detail"
  114. fi
  115. #----------------------------------------------------------#
  116. # Vesta #
  117. #----------------------------------------------------------#
  118. # Logging
  119. log_event "$OK" "$ARGUMENTS"
  120. exit