v-update-letsencrypt-ssl 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #!/bin/bash
  2. # info: update letsencrypt ssl certificates
  3. # options: NONE
  4. # labels: panel
  5. #
  6. # example: v-update-letsencrypt-ssl
  7. #
  8. # The function for renew letsencrypt expired ssl certificate for all users
  9. #----------------------------------------------------------#
  10. # Variable&Function #
  11. #----------------------------------------------------------#
  12. # Importing system enviroment as we run this script
  13. # mostly by cron wich not read it by itself
  14. source /etc/profile
  15. # Includes
  16. source $HESTIA/func/main.sh
  17. source $HESTIA/conf/hestia.conf
  18. #----------------------------------------------------------#
  19. # Action #
  20. #----------------------------------------------------------#
  21. # Set LE counter
  22. lecounter=0
  23. max_LE_failures=30
  24. # Checking user certificates
  25. for user in $($HESTIA/bin/v-list-sys-users plain); do
  26. USER_DATA=$HESTIA/data/users/$user
  27. for domain in $(search_objects 'web' 'LETSENCRYPT' 'yes' 'DOMAIN'); do
  28. domain_suspended="$(get_object_value 'web' 'DOMAIN' "$domain" '$SUSPENDED')"
  29. if [ "$domain_suspended" = "yes" ]; then
  30. continue
  31. fi
  32. fail_counter="$(get_object_value 'web' 'DOMAIN' "$domain" '$LETSENCRYPT_FAIL_COUNT')"
  33. if [[ "$fail_counter" -gt "$max_LE_failures" ]]; then
  34. continue
  35. fi
  36. crt_data=$(openssl x509 -text -in $USER_DATA/ssl/$domain.crt)
  37. not_after=$(echo "$crt_data" |grep "Not After" |cut -f 2,3,4 -d :)
  38. expiration=$(date -d "$not_after" +%s)
  39. now=$(date +%s)
  40. seconds_valid=$((expiration - now))
  41. days_valid=$((seconds_valid / 86400))
  42. if [[ "$days_valid" -lt 31 ]]; then
  43. if [ $lecounter -gt 0 ]; then
  44. sleep 10
  45. fi
  46. ((lecounter++))
  47. aliases=$(echo "$crt_data" |grep DNS:)
  48. aliases=$(echo "$aliases" |sed -e "s/DNS://g" -e "s/,//g")
  49. aliases=$(echo "$aliases" |tr ' ' '\n' |sed "/^$/d")
  50. aliases=$(echo "$aliases" |egrep -v "^$domain,?$")
  51. aliases=$(echo "$aliases" |sed -e ':a;N;$!ba;s/\n/,/g')
  52. # Source domain.conf
  53. source <(cat $HESTIA/data/users/$user/web.conf | grep "DOMAIN='$domain'")
  54. # Split aliases into array
  55. IFS=',' read -r -a ALIASES <<< "$ALIAS"
  56. # Unset f_aliases
  57. f_aliases=''
  58. # Loop through all crt aliases
  59. for alias in ${aliases//,/ } ; do
  60. # Validate if the alias still exists in web.conf
  61. if [[ " ${ALIASES[@]} " =~ " ${alias} " ]]; then
  62. f_aliases+="$alias,"
  63. fi
  64. done
  65. # Remove leading comma
  66. if [[ ${f_aliases: -1} = ',' ]] ; then f_aliases=${f_aliases::-1}; fi
  67. # Write the filtered alias list to the default var
  68. aliases=$f_aliases
  69. msg=$($BIN/v-add-letsencrypt-domain $user $domain $aliases)
  70. if [ $? -ne 0 ]; then
  71. echo $msg
  72. log_event $E_INVALID "$domain $msg"
  73. if [ -z "$fail_counter" ]; then
  74. add_object_key "web" 'DOMAIN' "$domain" 'LETSENCRYPT_FAIL_COUNT' 'LETSENCRYPT'
  75. fi
  76. ((fail_counter++))
  77. update_object_value 'web' 'DOMAIN' "$domain" '$LETSENCRYPT_FAIL_COUNT' "$fail_counter"
  78. fi
  79. fi
  80. done
  81. for domain in $(search_objects 'mail' 'LETSENCRYPT' 'yes' 'DOMAIN'); do
  82. domain_suspended="$(get_object_value 'mail' 'DOMAIN' "$domain" '$SUSPENDED')"
  83. if [ "$domain_suspended" = "yes" ]; then
  84. continue
  85. fi
  86. fail_counter="$(get_object_value 'mail' 'DOMAIN' "$domain" '$LETSENCRYPT_FAIL_COUNT')"
  87. if [[ "$fail_counter" -gt "$max_LE_failures" ]]; then
  88. continue
  89. fi
  90. crt_data=$(openssl x509 -text -in $USER_DATA/ssl/mail.$domain.crt)
  91. not_after=$(echo "$crt_data" |grep "Not After" |cut -f 2,3,4 -d :)
  92. expiration=$(date -d "$not_after" +%s)
  93. now=$(date +%s)
  94. seconds_valid=$((expiration - now))
  95. days_valid=$((seconds_valid / 86400))
  96. if [[ "$days_valid" -lt 31 ]]; then
  97. if [ $lecounter -gt 0 ]; then
  98. sleep 10
  99. fi
  100. ((lecounter++))
  101. msg=$($BIN/v-add-letsencrypt-domain $user $domain ' ' yes)
  102. if [ $? -ne 0 ]; then
  103. echo $msg
  104. log_event $E_INVALID "$domain $msg"
  105. if [ -z "$fail_counter" ]; then
  106. add_object_key "mail" 'DOMAIN' "$domain" 'LETSENCRYPT_FAIL_COUNT' 'LETSENCRYPT'
  107. fi
  108. ((fail_counter++))
  109. update_object_value 'mail' 'DOMAIN' "$domain" '$LETSENCRYPT_FAIL_COUNT' "$fail_counter"
  110. fi
  111. fi
  112. done
  113. done
  114. #----------------------------------------------------------#
  115. # Hestia #
  116. #----------------------------------------------------------#
  117. # No Logging
  118. #log_event "$OK" "$EVENT"
  119. exit