v-update-letsencrypt-ssl 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 $VESTA/func/main.sh
  14. source $VESTA/conf/vesta.conf
  15. #----------------------------------------------------------#
  16. # Action #
  17. #----------------------------------------------------------#
  18. # Defining user list
  19. users=$(ls $VESTA/data/users/*/ssl/le.conf |cut -f 7 -d /)
  20. # Checking users
  21. for user in $users; do
  22. # Checking user certificates
  23. for crt in $(ls $VESTA/data/users/$user/ssl/*.crt 2>/dev/null); do
  24. # Checking certificate issuer
  25. crt_data=$(openssl x509 -text -in $crt)
  26. issuer=$(echo "$crt_data" |grep Issuer: |grep Encrypt)
  27. if [ ! -z "$issuer" ]; then
  28. expire=$(echo "$crt_data" |grep "Not After")
  29. expire=$(echo "$expire" |cut -f 2,3,4 -d :)
  30. expire=$(date -d "$expire" +%s)
  31. now=$(date +%s)
  32. expire=$((expire - now))
  33. expire=$((expire / 86400))
  34. domain=$(basename $crt |sed -e "s/.crt$//")
  35. if [[ "$expire" -lt 31 ]]; then
  36. aliases=$(echo "$crt_data" |grep DNS:)
  37. aliases=$(echo "$aliases" |sed -e "s/DNS://g" -e "s/,//")
  38. aliases=$(echo "$aliases" |tr ' ' '\n' |sed "/^$/d")
  39. aliases=$(echo "$aliases" |grep -v "^$domain$")
  40. if [ ! -z "$aliases" ]; then
  41. aliases=$(echo "$aliases" |sed -e ':a;N;$!ba;s/\n/,/g')
  42. $BIN/v-add-letsencrypt-domain $user $domain $aliases
  43. else
  44. $BIN/v-add-letsencrypt-domain $user $domain
  45. fi
  46. fi
  47. fi
  48. done
  49. done
  50. #----------------------------------------------------------#
  51. # Vesta #
  52. #----------------------------------------------------------#
  53. # No Logging
  54. #log_event "$OK" "$EVENT"
  55. exit