v-update-letsencrypt-ssl 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. msg=$($BIN/v-add-letsencrypt-domain "$user" "$domain" "$aliases")
  81. if [ $? -ne 0 ]; then
  82. echo "$msg"
  83. log_event "$E_INVALID" "$domain $msg"
  84. $BIN/v-log-action "$user" "Error" "Web" "Let's Encrypt SSL certificate update failed (Domain: $domain)."
  85. if [ -z "$fail_counter" ]; then
  86. add_object_key "web" 'DOMAIN' "$domain" 'LETSENCRYPT_FAIL_COUNT' 'LETSENCRYPT'
  87. fi
  88. ((fail_counter++))
  89. update_object_value 'web' 'DOMAIN' "$domain" '$LETSENCRYPT_FAIL_COUNT' "$fail_counter"
  90. else
  91. $BIN/v-log-action "$user" "Info" "Web" "Let's Encrypt SSL certificate renewed (Domain: $domain)."
  92. fi
  93. if [ -n "$UPDATE_HOSTNAME_SSL" ] && [ "$UPDATE_HOSTNAME_SSL" = "yes" ]; then
  94. hostname=$(hostname -f)
  95. if [ "$hostname" = "$domain" ]; then
  96. $BIN/v-update-host-certificate "$user" "$domain"
  97. fi
  98. fi
  99. fi
  100. done
  101. for domain in $(search_objects 'mail' 'LETSENCRYPT' 'yes' 'DOMAIN'); do
  102. domain_suspended="$(get_object_value 'mail' 'DOMAIN' "$domain" '$SUSPENDED')"
  103. if [ "$domain_suspended" = "yes" ]; then
  104. continue
  105. fi
  106. fail_counter="$(get_object_value 'mail' 'DOMAIN' "$domain" '$LETSENCRYPT_FAIL_COUNT')"
  107. if [[ "$fail_counter" -gt "$max_LE_failures" ]]; then
  108. continue
  109. fi
  110. crt_data=$(openssl x509 -text -in $USER_DATA/ssl/mail.$domain.crt)
  111. not_after=$(echo "$crt_data" |grep "Not After" |cut -f 2,3,4 -d :)
  112. expiration=$(date -d "$not_after" +%s)
  113. now=$(date +%s)
  114. seconds_valid=$((expiration - now))
  115. days_valid=$((seconds_valid / 86400))
  116. if [[ "$days_valid" -lt 31 ]]; then
  117. if [ $lecounter -gt 0 ]; then
  118. sleep 10
  119. fi
  120. ((lecounter++))
  121. msg=$($BIN/v-add-letsencrypt-domain "$user" "$domain" "" "yes")
  122. if [ $? -ne 0 ]; then
  123. echo "$msg"
  124. $BIN/v-log-action "$user" "Error" "Web" "Let's Encrypt SSL certificate update failed (Domain: $domain)."
  125. log_event "$E_INVALID" "$domain $msg"
  126. if [ -z "$fail_counter" ]; then
  127. add_object_key "mail" 'DOMAIN' "$domain" 'LETSENCRYPT_FAIL_COUNT' 'LETSENCRYPT'
  128. fi
  129. ((fail_counter++))
  130. update_object_value 'mail' 'DOMAIN' "$domain" '$LETSENCRYPT_FAIL_COUNT' "$fail_counter"
  131. else
  132. $BIN/v-log-action "$user" "Info" "Web" "Let's Encrypt SSL certificate renewed (Domain: $domain)."
  133. fi
  134. fi
  135. done
  136. done
  137. # Restart related services
  138. $HESTIA/bin/v-restart-web yes
  139. $HESTIA/bin/v-restart-mail yes
  140. if [ -n "$PROXY_SYSTEM" ]; then
  141. $HESTIA/bin/v-restart-proxy yes
  142. fi
  143. #----------------------------------------------------------#
  144. # Hestia #
  145. #----------------------------------------------------------#
  146. # No Logging
  147. #log_event "$OK" "$EVENT"
  148. exit