v-add-letsencrypt-domain 10 KB

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