v-update-letsencrypt-ssl 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #!/bin/bash
  2. # info: update letsencrypt ssl certificates
  3. # options: NONE
  4. #
  5. # example: v-update-letsencrypt-ssl
  6. #
  7. # This function for renew letsencrypt expired ssl certificate for all users
  8. #----------------------------------------------------------#
  9. # Variables & Functions #
  10. #----------------------------------------------------------#
  11. # Includes
  12. # shellcheck source=/etc/hestiacp/hestia.conf
  13. source /etc/hestiacp/hestia.conf
  14. # shellcheck source=/usr/local/hestia/func/main.sh
  15. source $HESTIA/func/main.sh
  16. # shellcheck source=/usr/local/hestia/func/syshealth.sh
  17. source $HESTIA/func/syshealth.sh
  18. # load config file
  19. source_conf "$HESTIA/conf/hestia.conf"
  20. # Perform verification if read-only mode is enabled
  21. check_hestia_demo_mode
  22. #----------------------------------------------------------#
  23. # Action #
  24. #----------------------------------------------------------#
  25. # Set LE counter
  26. lecounter=0
  27. max_LE_failures=30
  28. days_valid_setting=31
  29. if [ "$LE_STAGING" = "yes" ]; then
  30. # Overwrite setting to allow testing for renewal to be done easier
  31. days_valid_setting=181
  32. fi
  33. # Checking user certificates
  34. for user in $($HESTIA/bin/v-list-sys-users plain); do
  35. USER_DATA=$HESTIA/data/users/$user
  36. for domain in $(search_objects 'web' 'LETSENCRYPT' 'yes' 'DOMAIN'); do
  37. # Clear any keys related to web domains
  38. sanitize_config_file "web"
  39. domain_suspended="$(get_object_value 'web' 'DOMAIN' "$domain" '$SUSPENDED')"
  40. if [ "$domain_suspended" = "yes" ]; then
  41. continue
  42. fi
  43. fail_counter="$(get_object_value 'web' 'DOMAIN' "$domain" '$LETSENCRYPT_FAIL_COUNT')"
  44. if [[ "$fail_counter" -gt "$max_LE_failures" ]]; then
  45. continue
  46. fi
  47. crt_data=$(openssl x509 -text -in $USER_DATA/ssl/$domain.crt)
  48. not_after=$(echo "$crt_data" |grep "Not After" |cut -f 2,3,4 -d :)
  49. expiration=$(date -d "$not_after" +%s)
  50. now=$(date +%s)
  51. seconds_valid=$((expiration - now))
  52. days_valid=$((seconds_valid / 86400))
  53. if [[ "$days_valid" -lt "$days_valid_setting" ]]; then
  54. if [ $lecounter -gt 0 ]; then
  55. sleep 10
  56. fi
  57. ((lecounter++))
  58. aliases=$(echo "$crt_data" |grep DNS:)
  59. aliases=$(echo "$aliases" |sed -e "s/DNS://g" -e "s/,//g")
  60. aliases=$(echo "$aliases" |tr ' ' '\n' |sed "/^$/d")
  61. aliases=$(echo "$aliases" |egrep -v "^$domain,?$")
  62. aliases=$(echo "$aliases" |sed -e ':a;N;$!ba;s/\n/,/g')
  63. # Parsing domain
  64. parse_object_kv_list $(grep "DOMAIN='$domain'" $USER_DATA/web.conf)
  65. # Split aliases into array
  66. IFS=',' read -r -a ALIASES <<< "$ALIAS"
  67. # Unset f_aliases
  68. f_aliases=''
  69. # Loop through all crt aliases
  70. for alias in ${aliases//,/ } ; do
  71. # Validate if the alias still exists in web.conf
  72. if [[ "$ALIAS" =~ $alias ]]; then
  73. f_aliases+="$alias,"
  74. fi
  75. done
  76. # Remove leading comma
  77. if [[ ${f_aliases: -1} = ',' ]] ; then f_aliases=${f_aliases::-1}; fi
  78. # Write the filtered alias list to the default var
  79. aliases=$f_aliases
  80. domain_redirect="$REDIRECT"
  81. if [[ -n "$domain_redirect" ]] ; then
  82. domain_redirect_code="$REDIRECT_CODE"
  83. $BIN/v-delete-web-domain-redirect $user $domain
  84. fi
  85. domain_forcessl="$SSL_FORCE"
  86. if [[ "$domain_forcessl" == 'yes' ]] ; then
  87. $BIN/v-delete-web-domain-ssl-force $user $domain
  88. fi
  89. msg=$($BIN/v-add-letsencrypt-domain "$user" "$domain" "$aliases")
  90. if [ $? -ne 0 ]; then
  91. echo "$msg"
  92. log_event "$E_INVALID" "$domain $msg"
  93. $BIN/v-log-action "$user" "Error" "Web" "Let's Encrypt SSL certificate update failed (Domain: $domain)."
  94. if [ -z "$fail_counter" ]; then
  95. add_object_key "web" 'DOMAIN' "$domain" 'LETSENCRYPT_FAIL_COUNT' 'LETSENCRYPT'
  96. fi
  97. ((fail_counter++))
  98. update_object_value 'web' 'DOMAIN' "$domain" '$LETSENCRYPT_FAIL_COUNT' "$fail_counter"
  99. else
  100. $BIN/v-log-action "$user" "Info" "Web" "Let's Encrypt SSL certificate renewed (Domain: $domain)."
  101. fi
  102. if [[ "$domain_forcessl" == 'yes' ]] ; then
  103. $BIN/v-add-web-domain-ssl-force $user $domain
  104. fi
  105. if [[ -n "$domain_redirect" ]] ; then
  106. $BIN/v-add-web-domain-redirect $user $domain $domain_redirect $domain_redirect_code
  107. fi
  108. if [ -n "$UPDATE_HOSTNAME_SSL" ] && [ "$UPDATE_HOSTNAME_SSL" = "yes" ]; then
  109. hostname=$(hostname -f)
  110. if [ "$hostname" = "$domain" ]; then
  111. $BIN/v-update-host-certificate "$user" "$domain"
  112. fi
  113. fi
  114. fi
  115. done
  116. for domain in $(search_objects 'mail' 'LETSENCRYPT' 'yes' 'DOMAIN'); do
  117. domain_suspended="$(get_object_value 'mail' 'DOMAIN' "$domain" '$SUSPENDED')"
  118. if [ "$domain_suspended" = "yes" ]; then
  119. continue
  120. fi
  121. fail_counter="$(get_object_value 'mail' 'DOMAIN' "$domain" '$LETSENCRYPT_FAIL_COUNT')"
  122. if [[ "$fail_counter" -gt "$max_LE_failures" ]]; then
  123. continue
  124. fi
  125. crt_data=$(openssl x509 -text -in $USER_DATA/ssl/mail.$domain.crt)
  126. not_after=$(echo "$crt_data" |grep "Not After" |cut -f 2,3,4 -d :)
  127. expiration=$(date -d "$not_after" +%s)
  128. now=$(date +%s)
  129. seconds_valid=$((expiration - now))
  130. days_valid=$((seconds_valid / 86400))
  131. if [[ "$days_valid" -lt 31 ]]; then
  132. if [ $lecounter -gt 0 ]; then
  133. sleep 10
  134. fi
  135. ((lecounter++))
  136. msg=$($BIN/v-add-letsencrypt-domain "$user" "$domain" "" "yes")
  137. if [ $? -ne 0 ]; then
  138. echo "$msg"
  139. $BIN/v-log-action "$user" "Error" "Web" "Let's Encrypt SSL certificate update failed (Domain: $domain)."
  140. log_event "$E_INVALID" "$domain $msg"
  141. if [ -z "$fail_counter" ]; then
  142. add_object_key "mail" 'DOMAIN' "$domain" 'LETSENCRYPT_FAIL_COUNT' 'LETSENCRYPT'
  143. fi
  144. ((fail_counter++))
  145. update_object_value 'mail' 'DOMAIN' "$domain" '$LETSENCRYPT_FAIL_COUNT' "$fail_counter"
  146. else
  147. $BIN/v-log-action "$user" "Info" "Web" "Let's Encrypt SSL certificate renewed (Domain: $domain)."
  148. fi
  149. fi
  150. done
  151. done
  152. # Restart related services
  153. $HESTIA/bin/v-restart-web yes
  154. $HESTIA/bin/v-restart-mail yes
  155. if [ -n "$PROXY_SYSTEM" ]; then
  156. $HESTIA/bin/v-restart-proxy yes
  157. fi
  158. #----------------------------------------------------------#
  159. # Hestia #
  160. #----------------------------------------------------------#
  161. # No Logging
  162. #log_event "$OK" "$EVENT"
  163. exit