You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

252 lines
7.0 KiB

9 years ago
9 years ago
9 years ago
8 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
7 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
3 months ago
9 years ago
9 years ago
3 months ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_cf_info='CloudFlare
  4. Site: CloudFlare.com
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi#dns_cf
  6. Options:
  7. CF_Key API Key
  8. CF_Email Your account email
  9. OptionsAlt:
  10. CF_Token API Token
  11. CF_Account_ID Account ID
  12. CF_Zone_ID Zone ID. Optional.
  13. '
  14. CF_Api="https://api.cloudflare.com/client/v4"
  15. ######## Public functions #####################
  16. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  17. dns_cf_add() {
  18. fulldomain=$1
  19. txtvalue=$2
  20. CF_Token="${CF_Token:-$(_readaccountconf_mutable CF_Token)}"
  21. CF_Account_ID="${CF_Account_ID:-$(_readaccountconf_mutable CF_Account_ID)}"
  22. CF_Zone_ID="${CF_Zone_ID:-$(_readaccountconf_mutable CF_Zone_ID)}"
  23. CF_Key="${CF_Key:-$(_readaccountconf_mutable CF_Key)}"
  24. CF_Email="${CF_Email:-$(_readaccountconf_mutable CF_Email)}"
  25. if [ "$CF_Token" ]; then
  26. if [ "$CF_Zone_ID" ]; then
  27. _savedomainconf CF_Token "$CF_Token"
  28. _savedomainconf CF_Account_ID "$CF_Account_ID"
  29. _savedomainconf CF_Zone_ID "$CF_Zone_ID"
  30. else
  31. _saveaccountconf_mutable CF_Token "$CF_Token"
  32. _saveaccountconf_mutable CF_Account_ID "$CF_Account_ID"
  33. _clearaccountconf_mutable CF_Zone_ID
  34. _clearaccountconf CF_Zone_ID
  35. fi
  36. else
  37. if [ -z "$CF_Key" ] || [ -z "$CF_Email" ]; then
  38. CF_Key=""
  39. CF_Email=""
  40. _err "You didn't specify a Cloudflare api key and email yet."
  41. _err "You can get yours from here https://dash.cloudflare.com/profile."
  42. return 1
  43. fi
  44. if ! _contains "$CF_Email" "@"; then
  45. _err "It seems that the CF_Email=$CF_Email is not a valid email address."
  46. _err "Please check and retry."
  47. return 1
  48. fi
  49. #save the api key and email to the account conf file.
  50. _saveaccountconf_mutable CF_Key "$CF_Key"
  51. _saveaccountconf_mutable CF_Email "$CF_Email"
  52. _clearaccountconf_mutable CF_Token
  53. _clearaccountconf_mutable CF_Account_ID
  54. _clearaccountconf_mutable CF_Zone_ID
  55. _clearaccountconf CF_Token
  56. _clearaccountconf CF_Account_ID
  57. _clearaccountconf CF_Zone_ID
  58. fi
  59. _debug "First detect the root zone"
  60. if ! _get_root "$fulldomain"; then
  61. _err "invalid domain"
  62. return 1
  63. fi
  64. _debug _domain_id "$_domain_id"
  65. _debug _sub_domain "$_sub_domain"
  66. _debug _domain "$_domain"
  67. _debug "Getting txt records"
  68. _cf_rest GET "zones/${_domain_id}/dns_records?type=TXT&name=$fulldomain"
  69. if ! echo "$response" | tr -d " " | grep \"success\":true >/dev/null; then
  70. _err "Error"
  71. return 1
  72. fi
  73. # For wildcard cert, the main root domain and the wildcard domain have the same txt subdomain name, so
  74. # we can not use updating anymore.
  75. # count=$(printf "%s\n" "$response" | _egrep_o "\"count\":[^,]*" | cut -d : -f 2)
  76. # _debug count "$count"
  77. # if [ "$count" = "0" ]; then
  78. _info "Adding record"
  79. if _cf_rest POST "zones/$_domain_id/dns_records" "{\"type\":\"TXT\",\"name\":\"$fulldomain\",\"content\":\"$txtvalue\",\"ttl\":120}"; then
  80. if _contains "$response" "$txtvalue"; then
  81. _info "Added, OK"
  82. return 0
  83. elif _contains "$response" "The record already exists"; then
  84. _info "Already exists, OK"
  85. return 0
  86. else
  87. _err "Add txt record error."
  88. return 1
  89. fi
  90. fi
  91. _err "Add txt record error."
  92. return 1
  93. }
  94. #fulldomain txtvalue
  95. dns_cf_rm() {
  96. fulldomain=$1
  97. txtvalue=$2
  98. CF_Token="${CF_Token:-$(_readaccountconf_mutable CF_Token)}"
  99. CF_Account_ID="${CF_Account_ID:-$(_readaccountconf_mutable CF_Account_ID)}"
  100. CF_Zone_ID="${CF_Zone_ID:-$(_readaccountconf_mutable CF_Zone_ID)}"
  101. CF_Key="${CF_Key:-$(_readaccountconf_mutable CF_Key)}"
  102. CF_Email="${CF_Email:-$(_readaccountconf_mutable CF_Email)}"
  103. _debug "First detect the root zone"
  104. if ! _get_root "$fulldomain"; then
  105. _err "invalid domain"
  106. return 1
  107. fi
  108. _debug _domain_id "$_domain_id"
  109. _debug _sub_domain "$_sub_domain"
  110. _debug _domain "$_domain"
  111. _debug "Getting txt records"
  112. _cf_rest GET "zones/${_domain_id}/dns_records?type=TXT&name=$fulldomain&content=$txtvalue"
  113. if ! echo "$response" | tr -d " " | grep \"success\":true >/dev/null; then
  114. _err "Error: $response"
  115. return 1
  116. fi
  117. count=$(echo "$response" | _egrep_o "\"count\": *[^,]*" | cut -d : -f 2 | tr -d " ")
  118. _debug count "$count"
  119. if [ "$count" = "0" ]; then
  120. _info "Don't need to remove."
  121. else
  122. record_id=$(echo "$response" | _egrep_o "\"id\": *\"[^\"]*\"" | cut -d : -f 2 | tr -d \" | _head_n 1 | tr -d " ")
  123. _debug "record_id" "$record_id"
  124. if [ -z "$record_id" ]; then
  125. _err "Can not get record id to remove."
  126. return 1
  127. fi
  128. if ! _cf_rest DELETE "zones/$_domain_id/dns_records/$record_id"; then
  129. _err "Delete record error."
  130. return 1
  131. fi
  132. echo "$response" | tr -d " " | grep \"success\":true >/dev/null
  133. fi
  134. }
  135. #################### Private functions below ##################################
  136. #_acme-challenge.www.domain.com
  137. #returns
  138. # _sub_domain=_acme-challenge.www
  139. # _domain=domain.com
  140. # _domain_id=sdjkglgdfewsdfg
  141. _get_root() {
  142. domain=$1
  143. i=1
  144. p=1
  145. # Use Zone ID directly if provided
  146. if [ "$CF_Zone_ID" ]; then
  147. if ! _cf_rest GET "zones/$CF_Zone_ID"; then
  148. return 1
  149. else
  150. if echo "$response" | tr -d " " | grep \"success\":true >/dev/null; then
  151. _domain=$(echo "$response" | _egrep_o "\"name\": *\"[^\"]*\"" | cut -d : -f 2 | tr -d \" | _head_n 1 | tr -d " ")
  152. if [ "$_domain" ]; then
  153. _cutlength=$((${#domain} - ${#_domain} - 1))
  154. _sub_domain=$(printf "%s" "$domain" | cut -c "1-$_cutlength")
  155. _domain_id=$CF_Zone_ID
  156. return 0
  157. else
  158. return 1
  159. fi
  160. else
  161. return 1
  162. fi
  163. fi
  164. fi
  165. while true; do
  166. h=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
  167. _debug h "$h"
  168. if [ -z "$h" ]; then
  169. #not valid
  170. return 1
  171. fi
  172. if [ "$CF_Account_ID" ]; then
  173. if ! _cf_rest GET "zones?name=$h&account.id=$CF_Account_ID"; then
  174. return 1
  175. fi
  176. else
  177. if ! _cf_rest GET "zones?name=$h"; then
  178. return 1
  179. fi
  180. fi
  181. if _contains "$response" "\"name\":\"$h\"" || _contains "$response" '"total_count":1'; then
  182. _domain_id=$(echo "$response" | _egrep_o "\[.\"id\": *\"[^\"]*\"" | _head_n 1 | cut -d : -f 2 | tr -d \" | tr -d " ")
  183. if [ "$_domain_id" ]; then
  184. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-"$p")
  185. _domain=$h
  186. return 0
  187. fi
  188. return 1
  189. fi
  190. p=$i
  191. i=$(_math "$i" + 1)
  192. done
  193. return 1
  194. }
  195. _cf_rest() {
  196. m=$1
  197. ep="$2"
  198. data="$3"
  199. _debug "$ep"
  200. email_trimmed=$(echo "$CF_Email" | tr -d '"')
  201. key_trimmed=$(echo "$CF_Key" | tr -d '"')
  202. token_trimmed=$(echo "$CF_Token" | tr -d '"')
  203. export _H1="Content-Type: application/json"
  204. if [ "$token_trimmed" ]; then
  205. export _H2="Authorization: Bearer $token_trimmed"
  206. else
  207. export _H2="X-Auth-Email: $email_trimmed"
  208. export _H3="X-Auth-Key: $key_trimmed"
  209. fi
  210. if [ "$m" != "GET" ]; then
  211. _debug data "$data"
  212. response="$(_post "$data" "$CF_Api/$ep" "" "$m")"
  213. else
  214. response="$(_get "$CF_Api/$ep")"
  215. fi
  216. if [ "$?" != "0" ]; then
  217. _err "error $ep"
  218. return 1
  219. fi
  220. _debug2 response "$response"
  221. return 0
  222. }