acme-cpanel.sh 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #!/usr/bin/env bash
  2. # This script uses acme.sh to issue and deploy SSL certificates from Let's Encrypt for a list of domains
  3. # using the webroot or dns methods
  4. #
  5. # See README.md for more details
  6. #
  7. # Copyright 2020 Bryan Roessler <bryanroessler@gmail.com>
  8. #
  9. # USAGE
  10. # ./acme-cpanel.sh [OPTIONS] [FILES...]
  11. #
  12. # EXAMPLES
  13. # TESTING: ./acme-cpanel-webroot.sh --debug -e me@gmail.com multisites/flatwhitedesign.pw multisites/greengingermultisite.website
  14. # PRODUCTION: ./acme-cpanel-webroot.sh --force -e me@gmail.com multisites/flatwhitedesign.pw multisites/greengingermultisite.website
  15. #
  16. # TESTING: ./acme-cpanel-webroot.sh --debug -s multisites
  17. # PRODUCTION: ./acme-cpanel-webroot.sh --force -s multisites
  18. #
  19. # FILES is a list of files containing first-level DOMAIN names (see domains.txt) on newlines
  20. # Certificates will automatically be issued and deployed for DOMAIN and www.DOMAIN using the webroot method
  21. #
  22. # NOTE: The webroot method does NOT support wildcard domains, Let's Encrypt requires wildcard domains to
  23. # use DNS challenges, which the CPANEL uapi does not support (use dns_cpaneldns plugin instead)
  24. unset SITES_DIR USEREMAIL DOMAIN_FILES DOMAIN_GROUPS DEPLOY_CMD_PREFIX ISSUE_CMD_PREFIX DEBUG GROUP
  25. DEBUG="true" # quote this line to stop DEBUG mode and issue certificates for real, or use --force in user options
  26. METHOD="dns" # set the default method
  27. parse_input() {
  28. local input
  29. declare -g USEREMAIL
  30. declare -ag DOMAIN_FILES
  31. if input=$(getopt -o +m:e:fgs:d -l method:,email:,force,group-by-file,sites-dir:,debug -- "$@"); then
  32. eval set -- "$input"
  33. while true; do
  34. case "$1" in
  35. --method|-m)
  36. shift
  37. METHOD="${1,,}"
  38. ;;
  39. --email|-e)
  40. shift
  41. USEREMAIL="$1"
  42. ;;
  43. --force|-f)
  44. unset DEBUG
  45. ;;
  46. --group-by-file|-g)
  47. GROUP="true"
  48. ;;
  49. --sites-dir|-s)
  50. shift
  51. SITES_DIR="$1"
  52. ;;
  53. --debug|-d)
  54. DEBUG="true"
  55. ;;
  56. --)
  57. shift
  58. break
  59. ;;
  60. esac
  61. shift
  62. done
  63. else
  64. echo "Incorrect options provided"
  65. exit 1
  66. fi
  67. # Load domain files from remaining arguments
  68. if [[ $# -lt 1 ]]; then
  69. [[ -v SITES_DIR && -d "$SITES_DIR" ]] && return 0
  70. if [[ -f "domains.txt" ]]; then
  71. echo "You have not supplied any domain files, using domains.txt by default"
  72. DOMAIN_FILES=("domains.txt")
  73. else
  74. echo "You must specify a domain list or use domains.txt by default"
  75. exit 1
  76. fi
  77. else
  78. DOMAIN_FILES=("$@")
  79. fi
  80. }
  81. get_acme() {
  82. curl https://get.acme.sh | sh
  83. source "$HOME/.bashrc"
  84. "$HOME/.acme.sh/acme.sh" --upgrade --auto-upgrade
  85. }
  86. update_email() { [[ -v USEREMAIL ]] && "$HOME/.acme.sh/acme.sh" --update-account --accountemail "${USEREMAIL}"; }
  87. command_prefixes() {
  88. declare -ag ISSUE_CMD_PREFIX DEPLOY_CMD_PREFIX
  89. ISSUE_CMD_PREFIX=("$HOME/.acme.sh/acme.sh" "--issue")
  90. [[ "$METHOD" == "dns" ]] && ISSUE_CMD_PREFIX=("${ISSUE_CMD_PREFIX[@]}" "--dns" "dns_cpaneldns")
  91. [[ -v DEBUG ]] && ISSUE_CMD_PREFIX=("${ISSUE_CMD_PREFIX[@]}" "--staging") || ISSUE_CMD_PREFIX=("${ISSUE_CMD_PREFIX[@]}" "--force")
  92. DEPLOY_CMD_PREFIX=("$HOME/.acme.sh/acme.sh" "--deploy" "--deploy-hook" "cpanel_uapi")
  93. }
  94. get_webroot() {
  95. local webroot
  96. if ! webroot=$(uapi DomainInfo single_domain_data domain="$1" | grep documentroot); then
  97. echo "UAPI call failed" >&2
  98. fi
  99. if [[ ! -v webroot || "$webroot" == "" ]]; then
  100. if [[ -v DEBUG ]]; then
  101. webroot="/tmp" # set missing webroot in DEBUG mode for testing
  102. else
  103. echo "Could not find $1's webroot" >&2
  104. exit 1
  105. fi
  106. fi
  107. echo "$webroot"
  108. }
  109. # Either create a single array of all domains (DOMAINS) to issue one-by-one or create an array of array names to issue for a single webroot
  110. load_domains() {
  111. local domain_file
  112. declare -ag DOMAIN_GROUPS=()
  113. if [[ -v SITES_DIR ]]; then
  114. for domain_file in "$SITES_DIR"/*; do
  115. DOMAIN_GROUPS+=("$(<"$domain_file")")
  116. done
  117. fi
  118. for domain_file in "${DOMAIN_FILES[@]}"; do
  119. # Load list of domains as space-delimited strings in elements of the DOMAINS array
  120. # We can keep these separate or combine them later
  121. DOMAIN_GROUPS+=("$(<"$domain_file")")
  122. done
  123. }
  124. issue_and_deploy_certs() {
  125. local domain_root domain domain_group
  126. local -a issue_cmd=()
  127. local -a deploy_cmd=()
  128. if [[ -v GROUP ]]; then
  129. for domain_group in "${DOMAIN_GROUPS[@]}"; do
  130. unset i
  131. for domain in $domain_group; do # we want to split on whitespace
  132. [[ "$domain" == "" ]] && continue
  133. # Get the webroot from the first domain
  134. if [[ ! -v i ]]; then
  135. local i="set"
  136. domain_root=$(get_webroot "$domain")
  137. issue_cmd=("${ISSUE_CMD_PREFIX[@]}" "-w" "$domain_root")
  138. fi
  139. issue_cmd+=("-d" "$domain" "-d" "www.$domain")
  140. done
  141. # Issue certificate for entire domain group
  142. echo "Running:" "${issue_cmd[@]}"
  143. if ! "${issue_cmd[@]}"; then
  144. echo "Failed to issue certificate"
  145. fi
  146. # Deploy certificates one by one
  147. for domain in $domain_group; do
  148. deploy_cmd=("${DEPLOY_CMD_PREFIX[@]}" "-w" "$domain_root" "-d" "$domain")
  149. echo "Running:" "${deploy_cmd[@]}"
  150. "${deploy_cmd[@]}"
  151. done
  152. done
  153. else
  154. for domain_group in "${DOMAIN_GROUPS[@]}"; do
  155. # Issue and deploy certificates one by one
  156. for domain in $domain_group; do # we want to split on whitespace
  157. issue_cmd=("${ISSUE_CMD_PREFIX[@]}" "-d" "$domain" "-d" "www.$domain")
  158. [[ "$METHOD" == "webroot" ]] && domain_root=$(get_webroot "$domain") && issue_cmd=("${issue_cmd[@]}" "-w" "$domain_root")
  159. deploy_cmd=("${DEPLOY_CMD_PREFIX[@]}" "-d" "$domain") # I think we only need to deploy to the domain, not subdomains
  160. [[ "$METHOD" == "webroot" ]] && deploy_cmd=("${deploy_cmd[@]}" "-w" "$domain_root")
  161. echo "Running:" "${issue_cmd[@]}"
  162. if ! "${issue_cmd[@]}"; then
  163. echo "Failed to issue certificate for $domain"
  164. err=1
  165. fi
  166. echo "Running:" "${deploy_cmd[@]}"
  167. if ! "${deploy_cmd[@]}"; then
  168. echo "Failed to deploy certificate for $domain"
  169. err=1
  170. fi
  171. done
  172. done
  173. fi
  174. }
  175. main() {
  176. parse_input "$@"
  177. get_acme
  178. update_email
  179. command_prefixes
  180. load_domains
  181. issue_and_deploy_certs
  182. }
  183. main "$@"
  184. exit "${err:-0}"