v-update-letsencrypt-ssl 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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=$($BIN/v-list-users | tail -n+3 | awk '{ print $1 }')
  20. # Checking users
  21. for user in $users; do
  22. USER_DATA=$VESTA/data/users/$user
  23. # Checking user certificates
  24. for domain in $(search_objects 'web' 'LETSENCRYPT' 'yes' 'DOMAIN'); do
  25. crt="$VESTA/data/users/$user/ssl/$domain.crt"
  26. crt_data=$(openssl x509 -text -in "$crt")
  27. expire=$(echo "$crt_data" |grep "Not After")
  28. expire=$(echo "$expire" |cut -f 2,3,4 -d :)
  29. expire=$(date -d "$expire" +%s)
  30. now=$(date +%s)
  31. expire=$((expire - now))
  32. expire=$((expire / 86400))
  33. domain=$(basename $crt |sed -e "s/.crt$//")
  34. if [[ "$expire" -lt 31 ]]; then
  35. aliases=$(echo "$crt_data" |grep DNS:)
  36. aliases=$(echo "$aliases" |sed -e "s/DNS://g" -e "s/,//")
  37. aliases=$(echo "$aliases" |tr ' ' '\n' |sed "/^$/d")
  38. aliases=$(echo "$aliases" |grep -v "^$domain$")
  39. if [ ! -z "$aliases" ]; then
  40. aliases=$(echo "$aliases" |sed -e ':a;N;$!ba;s/\n/,/g')
  41. msg=$($BIN/v-add-letsencrypt-domain $user $domain $aliases)
  42. if [ $? -ne 0 ]; then
  43. echo "$domain $msg"
  44. fi
  45. else
  46. msg==$($BIN/v-add-letsencrypt-domain $user $domain)
  47. if [ $? -ne 0 ]; then
  48. echo "$domain $msg"
  49. fi
  50. fi
  51. sleep 10
  52. fi
  53. done
  54. done
  55. #----------------------------------------------------------#
  56. # Vesta #
  57. #----------------------------------------------------------#
  58. # No Logging
  59. #log_event "$OK" "$EVENT"
  60. exit