v-update-letsencrypt-ssl 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/bin/bash
  2. # info: update letsencrypt ssl certificates
  3. # options: NONE
  4. #
  5. # The function for renew letsencrypt expired ssl certificate for all users
  6. #----------------------------------------------------------#
  7. # Variable&Function #
  8. #----------------------------------------------------------#
  9. # Importing system enviroment as we run this script
  10. # mostly by cron wich not read it by itself
  11. source /etc/profile
  12. # Includes
  13. source $HESTIA/func/main.sh
  14. source $HESTIA/conf/hestia.conf
  15. #----------------------------------------------------------#
  16. # Action #
  17. #----------------------------------------------------------#
  18. # Set LE counter
  19. lecounter=0
  20. # Checking user certificates
  21. for user in $($BIN/v-list-users plain |cut -f 1); do
  22. USER_DATA=$HESTIA/data/users/$user
  23. for domain in $(search_objects 'web' 'LETSENCRYPT' 'yes' 'DOMAIN'); do
  24. crt_data=$(openssl x509 -text -in $USER_DATA/ssl/$domain.crt)
  25. not_after=$(echo "$crt_data" |grep "Not After" |cut -f 2,3,4 -d :)
  26. expiration=$(date -d "$not_after" +%s)
  27. now=$(date +%s)
  28. seconds_valid=$((expiration - now))
  29. days_valid=$((seconds_valid / 86400))
  30. if [[ "$days_valid" -lt 31 ]]; then
  31. if [ $lecounter -gt 0 ]; then
  32. sleep 10
  33. fi
  34. ((lecounter++))
  35. aliases=$(echo "$crt_data" |grep DNS:)
  36. aliases=$(echo "$aliases" |sed -e "s/DNS://g" -e "s/,//g")
  37. aliases=$(echo "$aliases" |tr ' ' '\n' |sed "/^$/d")
  38. aliases=$(echo "$aliases" |grep -v "^$domain$")
  39. aliases=$(echo "$aliases" |sed -e ':a;N;$!ba;s/\n/,/g')
  40. msg=$($BIN/v-add-letsencrypt-domain $user $domain $aliases)
  41. if [ $? -ne 0 ]; then
  42. echo "$domain $msg"
  43. fi
  44. fi
  45. done
  46. for domain in $(search_objects 'mail' 'LETSENCRYPT' 'yes' 'DOMAIN'); do
  47. crt_data=$(openssl x509 -text -in $USER_DATA/ssl/mail.$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 31 ]]; then
  54. if [ $lecounter -gt 0 ]; then
  55. sleep 10
  56. fi
  57. ((lecounter++))
  58. msg=$($BIN/v-add-letsencrypt-domain $user $domain ' ' yes)
  59. if [ $? -ne 0 ]; then
  60. echo "$domain $msg"
  61. fi
  62. fi
  63. done
  64. done
  65. #----------------------------------------------------------#
  66. # Hestia #
  67. #----------------------------------------------------------#
  68. # No Logging
  69. #log_event "$OK" "$EVENT"
  70. exit