acme-cpanel-dns.sh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/usr/bin/env bash
  2. # This program will install and configure acme, request SSL certificates from Let's Encrypt, and enable them using the cPanel API
  3. # Comment the following line to skip issuing a test certificate
  4. test="true"
  5. unset err
  6. get_acme() {
  7. curl https://get.acme.sh | sh
  8. curl -o "$HOME/.acme.sh/dnsapi/dns_cpaneldns.sh" https://raw.githubusercontent.com/cryobry/dns_cpaneldns/master/dns_cpaneldns.sh
  9. "$HOME/.acme.sh/acme.sh" --upgrade --auto-upgrade
  10. }
  11. run_config() {
  12. if [[ -f "$HOME/.acme.sh/account.conf" ]]; then
  13. if grep -q "CPANELDNS_AUTH_PASSWORD" "$HOME/.acme.sh/account.conf"; then
  14. echo "cPanel credentials already present, skipping configuration..."
  15. echo "To rerun the configuration, first run 'rm \$HOME/.acme.sh/account.conf'"
  16. return 0
  17. else
  18. # Set contact e-mail for ACME failure
  19. read -rp 'Enter the e-mail address to contact in case of acme failure: ' EMAIL
  20. echo
  21. "$HOME/.acme.sh/acme.sh" --update-account --accountemail "$EMAIL"
  22. # Read in Namecheap API variables from user for acme
  23. read -rp 'Enter your cPanel username: ' CPANELDNS_AUTH_ID
  24. echo
  25. export CPANELDNS_AUTH_ID
  26. read -rp 'Enter your cPanel password: ' CPANELDNS_AUTH_PASSWORD
  27. echo
  28. export CPANELDNS_AUTH_PASSWORD
  29. read -rp 'Enter your cPanel address and port number (example: "https://www.example.com:2083/"): ' CPANELDNS_API
  30. echo
  31. export CPANELDNS_API
  32. fi
  33. else
  34. touch "$HOME/.acme.sh/account.conf"
  35. run_config
  36. fi
  37. }
  38. # Issue certificates
  39. issue_cert() {
  40. local multisite_file
  41. for multisite_file in ./multisites/*; do
  42. echo "Attempting to issue certificates for ${multisite_file##*/} and its multisites..."
  43. unset sites issue_cmd deploy_cmd
  44. declare -al sites issue_cmd deploy_cmd
  45. readarray -t sites < "${multisite_file}"
  46. issue_cmd=("$HOME/.acme.sh/acme.sh" "--issue" "--dns" "dns_cpaneldns")
  47. deploy_cmd=("$HOME/.acme.sh/acme.sh" "--deploy" "--deploy-hook" "cpanel_uapi")
  48. for site in "${sites[@]}"; do
  49. [[ "$site" != "" ]] && issue_cmd+=("-d" "$site")
  50. done
  51. # if test enabled, issue test certificate first
  52. if [[ "${test:-x}" == "true" ]]; then
  53. "${issue_cmd[@]}" --staging
  54. read -rp -n 1 "Was the certificate correctly issued without errors? [y/N]: "
  55. echo
  56. [[ ! "$REPLY" =~ ^[Yy]$ ]] && err=1 && return 1
  57. fi
  58. echo "Running:" "${issue_cmd[@]}"
  59. if "${issue_cmd[@]}" --force; then
  60. echo "Running:" "${deploy_cmd[@]}"
  61. ! "${deploy_cmd[@]}" && \
  62. echo "Could not deploy" && \
  63. err=1
  64. else
  65. echo "Could not issue"
  66. err=1
  67. fi
  68. done
  69. }
  70. main() {
  71. get_acme
  72. run_config
  73. issue_cert
  74. }
  75. main "$@"
  76. exit "${err:-0}"