v-add-letsencrypt-domain 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. #!/bin/bash
  2. # info: check letsencrypt domain
  3. # options: USER DOMAIN [ALIASES]
  4. #
  5. # The function check and validates domain with Let's Encript
  6. #----------------------------------------------------------#
  7. # Variable&Function #
  8. #----------------------------------------------------------#
  9. # Argument definition
  10. user=$1
  11. domain=$2
  12. aliases=$3
  13. # LE API
  14. API='https://acme-v02.api.letsencrypt.org'
  15. # Includes
  16. source $VESTA/func/main.sh
  17. source $VESTA/func/domain.sh
  18. source $VESTA/conf/vesta.conf
  19. # Additional argument formatting
  20. format_identifier_idn() {
  21. identifier_idn=$identifier
  22. if [[ "$identifier_idn" = *[![:ascii:]]* ]]; then
  23. identifier_idn=$(idn -t --quiet -a $identifier_idn)
  24. fi
  25. }
  26. # encode base64
  27. encode_base64() {
  28. cat |base64 |tr '+/' '-_' |tr -d '\r\n='
  29. }
  30. # Let's Encrypt v2 curl function
  31. query_le_v2() {
  32. protected='{"nonce": "'$3'",'
  33. protected=''$protected' "url": "'$1'",'
  34. protected=''$protected' "alg": "RS256", "kid": "'$KID'"}'
  35. content="Content-Type: application/jose+json"
  36. payload_=$(echo -n "$2" |encode_base64)
  37. protected_=$(echo -n "$protected" |encode_base64)
  38. signature_=$(printf "%s" "$protected_.$payload_" |\
  39. openssl dgst -sha256 -binary -sign $USER_DATA/ssl/user.key |\
  40. encode_base64)
  41. post_data='{"protected":"'"$protected_"'",'
  42. post_data=$post_data'"payload":"'"$payload_"'",'
  43. post_data=$post_data'"signature":"'"$signature_"'"}'
  44. curl -s -i -d "$post_data" "$1" -H "$content"
  45. }
  46. #----------------------------------------------------------#
  47. # Verifications #
  48. #----------------------------------------------------------#
  49. check_args '2' "$#" 'USER DOMAIN [ALIASES]'
  50. is_format_valid 'user' 'domain' 'aliases'
  51. is_system_enabled "$WEB_SYSTEM" 'WEB_SYSTEM'
  52. is_object_valid 'user' 'USER' "$user"
  53. is_object_unsuspended 'user' 'USER' "$user"
  54. is_object_valid 'web' 'DOMAIN' "$domain"
  55. is_object_unsuspended 'web' 'DOMAIN' "$domain"
  56. get_domain_values 'web'
  57. # check if alias is the letsencrypt wildcard domain, if not, make the normal checks
  58. if [[ "$aliases" != "*.$domain" ]]; then
  59. for alias in $(echo "$aliases" |tr ',' '\n' |sort -u); do
  60. check_alias="$(echo $ALIAS |tr ',' '\n' |grep ^$alias$)"
  61. if [ -z "$check_alias" ]; then
  62. check_result $E_NOTEXIST "domain alias $alias doesn't exist"
  63. fi
  64. done
  65. fi;
  66. #----------------------------------------------------------#
  67. # Action #
  68. #----------------------------------------------------------#
  69. # Registering LetsEncrypt user account
  70. $BIN/v-add-letsencrypt-user $user
  71. if [ "$?" -ne 0 ]; then
  72. touch $VESTA/data/queue/letsencrypt.pipe
  73. sed -i "/ $domain /d" $VESTA/data/queue/letsencrypt.pipe
  74. send_notice "LETSENCRYPT" "Account registration failed"
  75. check_result $E_CONNECT "LE account registration" >/dev/null
  76. fi
  77. # Parsing LetsEncrypt account data
  78. source $USER_DATA/ssl/le.conf
  79. # Checking wildcard alias
  80. if [ "$aliases" = "*.$domain" ]; then
  81. wildcard='yes'
  82. proto="dns-01"
  83. if [ ! -e "$VESTA/data/users/$user/dns/$domain.conf" ]; then
  84. check_result $E_NOTEXIST "DNS domain $domain doesn't exist"
  85. fi
  86. else
  87. proto="http-01"
  88. fi
  89. # Requesting nonce / STEP 1
  90. answer=$(curl -s -I "$API/directory")
  91. nonce=$(echo "$answer" |grep -i nonce |cut -f2 -d \ |tr -d '\r\n')
  92. status=$(echo "$answer"|grep HTTP/ |tail -n1 |cut -f 2 -d ' ')
  93. if [[ "$status" -ne 200 ]]; then
  94. check_result $E_CONNECT "Let's Encrypt nonce request status $status"
  95. fi
  96. # Placing new order / STEP 2
  97. url="$API/acme/new-order"
  98. payload='{"identifiers":['
  99. for identifier in $(echo $domain,$aliases |tr ',' '\n' |sort -u); do
  100. format_identifier_idn
  101. payload=$payload'{"type":"dns","value":"'$identifier_idn'"},'
  102. done
  103. payload=$(echo "$payload"|sed "s/,$//")
  104. payload=$payload']}'
  105. answer=$(query_le_v2 "$url" "$payload" "$nonce")
  106. nonce=$(echo "$answer" |grep -i nonce |cut -f2 -d \ |tr -d '\r\n')
  107. authz=$(echo "$answer" |grep "acme/authz" |cut -f2 -d '"')
  108. finalize=$(echo "$answer" |grep 'finalize":' |cut -f4 -d '"')
  109. status=$(echo "$answer" |grep HTTP/ |tail -n1 |cut -f2 -d ' ')
  110. if [[ "$status" -ne 201 ]]; then
  111. check_result $E_CONNECT "Let's Encrypt new auth status $status"
  112. fi
  113. # Requesting authorization token / STEP 3
  114. for auth in $authz; do
  115. payload=''
  116. answer=$(query_le_v2 "$auth" "$payload" "$nonce")
  117. url=$(echo "$answer" |grep -A3 $proto |grep url |cut -f 4 -d \")
  118. token=$(echo "$answer" |grep -A3 $proto |grep token |cut -f 4 -d \")
  119. nonce=$(echo "$answer" |grep -i nonce |cut -f2 -d \ |tr -d '\r\n')
  120. status=$(echo "$answer"|grep HTTP/ |tail -n1 |cut -f 2 -d ' ')
  121. if [[ "$status" -ne 200 ]]; then
  122. check_result $E_CONNECT "Let's Encrypt acme/authz bad status $status"
  123. fi
  124. # Accepting challenge / STEP 4
  125. if [ "$wildcard" = 'yes' ]; then
  126. record=$(printf "%s" "$token.$THUMB" |\
  127. openssl dgst -sha256 -binary |encode_base64)
  128. old_records=$($BIN/v-list-dns-records $user $domain plain|grep 'TXT')
  129. old_records=$(echo "$old_records" |grep _acme-challenge |cut -f 1)
  130. for old_record in $old_records; do
  131. $BIN/v-delete-dns-record $user $domain $old_record
  132. done
  133. $BIN/v-add-dns-record $user $domain "_acme-challenge" "TXT" $record
  134. check_result $? "DNS _acme-challenge record wasn't created"
  135. else
  136. if [ "$WEB_SYSTEM" = 'nginx' ] || [ ! -z "$PROXY_SYSTEM" ]; then
  137. conf="$HOMEDIR/$user/conf/web/nginx.$domain.conf_letsencrypt"
  138. sconf="$HOMEDIR/$user/conf/web/snginx.$domain.conf_letsencrypt"
  139. if [ ! -e "$conf" ]; then
  140. echo 'location ~ "^/\.well-known/acme-challenge/(.*)$" {' \
  141. > $conf
  142. echo ' default_type text/plain;' >> $conf
  143. echo ' return 200 "$1.'$THUMB'";' >> $conf
  144. echo '}' >> $conf
  145. fi
  146. if [ ! -e "$sconf" ]; then
  147. ln -s "$conf" "$sconf"
  148. fi
  149. $BIN/v-restart-proxy
  150. check_result $? "Proxy restart failed" >/dev/null
  151. else
  152. well_known="$HOMEDIR/$user/web/$domain/public_html/.well-known"
  153. acme_challenge="$well_known/acme-challenge"
  154. mkdir -p $acme_challenge
  155. echo "$token.$THUMB" > $acme_challenge/$token
  156. chown -R $user:$user $well_known
  157. fi
  158. $BIN/v-restart-web
  159. check_result $? "Web restart failed" >/dev/null
  160. fi
  161. # Requesting ACME validation / STEP 5
  162. validation_check=$(echo "$answer" |grep '"valid"')
  163. if [[ ! -z "$validation_check" ]]; then
  164. validation='valid'
  165. else
  166. validation='pending'
  167. fi
  168. # Doing pol check on status
  169. i=1
  170. while [ "$validation" = 'pending' ]; do
  171. payload='{}'
  172. answer=$(query_le_v2 "$url" "$payload" "$nonce")
  173. validation=$(echo "$answer"|grep -A1 $proto |tail -n1|cut -f4 -d \")
  174. nonce=$(echo "$answer" |grep -i nonce |cut -f2 -d \ |tr -d '\r\n')
  175. status=$(echo "$answer"|grep HTTP/ |tail -n1 |cut -f 2 -d ' ')
  176. if [[ "$status" -ne 200 ]]; then
  177. check_result $E_CONNECT "Let's Encrypt validation status $status"
  178. fi
  179. i=$((i + 1))
  180. if [ "$i" -gt 10 ]; then
  181. check_result $E_CONNECT "Let's Encrypt domain validation timeout"
  182. fi
  183. sleep 1
  184. done
  185. if [ "$validation" = 'invalid' ]; then
  186. check_result $E_CONNECT "Let's Encrypt domain verification failed"
  187. fi
  188. done
  189. # Generating new ssl certificate
  190. ssl_dir=$($BIN/v-generate-ssl-cert "$domain" "info@$domain" "US" "California"\
  191. "San Francisco" "Vesta" "IT" "$aliases" |tail -n1 |awk '{print $2}')
  192. # Sending CSR to finalize order / STEP 6
  193. csr=$(openssl req -in $ssl_dir/$domain.csr -outform DER |encode_base64)
  194. payload='{"csr":"'$csr'"}'
  195. answer=$(query_le_v2 "$finalize" "$payload" "$nonce")
  196. nonce=$(echo "$answer" |grep -i nonce |cut -f2 -d \ |tr -d '\r\n')
  197. status=$(echo "$answer"|grep HTTP/ |tail -n1 |cut -f 2 -d ' ')
  198. certificate=$(echo "$answer"|grep 'certificate":' |cut -f4 -d '"')
  199. if [[ "$status" -ne 200 ]]; then
  200. check_result $E_CONNECT "Let's Encrypt finalize bad status $status"
  201. fi
  202. # Downloading signed certificate / STEP 7
  203. curl -s "$certificate" -o $ssl_dir/$domain.pem
  204. # Splitting up downloaded pem
  205. crt_end=$(grep -n END $ssl_dir/$domain.pem |head -n1 |cut -f1 -d:)
  206. head -n $crt_end $ssl_dir/$domain.pem > $ssl_dir/$domain.crt
  207. pem_lines=$(wc -l $ssl_dir/$domain.pem |cut -f 1 -d ' ')
  208. ca_end=$(grep -n "BEGIN" $ssl_dir/$domain.pem |tail -n1 |cut -f 1 -d :)
  209. ca_end=$(( pem_lines - crt_end + 1 ))
  210. tail -n $ca_end $ssl_dir/$domain.pem > $ssl_dir/$domain.ca
  211. # Adding SSL
  212. ssl_home=$(search_objects 'web' 'LETSENCRYPT' 'yes' 'SSL_HOME')
  213. $BIN/v-delete-web-domain-ssl $user $domain >/dev/null 2>&1
  214. $BIN/v-add-web-domain-ssl $user $domain $ssl_dir $ssl_home
  215. if [ "$?" -ne '0' ]; then
  216. touch $VESTA/data/queue/letsencrypt.pipe
  217. sed -i "/ $domain /d" $VESTA/data/queue/letsencrypt.pipe
  218. send_notice 'LETSENCRYPT' "$domain certificate installation failed"
  219. check_result $? "SSL install" >/dev/null
  220. fi
  221. # Adding LE autorenew cronjob
  222. if [ -z "$(grep v-update-lets $VESTA/data/users/admin/cron.conf)" ]; then
  223. min=$(generate_password '012345' '2')
  224. hour=$(generate_password '1234567' '1')
  225. cmd="sudo $BIN/v-update-letsencrypt-ssl"
  226. $BIN/v-add-cron-job admin "$min" "$hour" '*' '*' '*' "$cmd" > /dev/null
  227. fi
  228. # Updating letsencrypt key
  229. if [ -z "$LETSENCRYPT" ]; then
  230. add_object_key "web" 'DOMAIN' "$domain" 'LETSENCRYPT' 'FTP_USER'
  231. fi
  232. update_object_value 'web' 'DOMAIN' "$domain" '$LETSENCRYPT' 'yes'
  233. #----------------------------------------------------------#
  234. # Vesta #
  235. #----------------------------------------------------------#
  236. # Deleteing task from queue
  237. touch $VESTA/data/queue/letsencrypt.pipe
  238. sed -i "/ $domain /d" $VESTA/data/queue/letsencrypt.pipe
  239. # Notifying user
  240. send_notice 'LETSENCRYPT' "$domain SSL has been installed successfully"
  241. # Logging
  242. log_event "$OK" "$ARGUMENTS"
  243. exit