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.

245 lines
6.1 KiB

8 years ago
3 months ago
3 months ago
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_pdns_info='PowerDNS Server API
  4. Site: PowerDNS.com
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi2#dns_pdns
  6. Options:
  7. PDNS_Url API URL. E.g. "http://ns.example.com:8081"
  8. PDNS_ServerId Server ID. E.g. "localhost"
  9. PDNS_Token API Token
  10. PDNS_Ttl=60 Domain TTL. Default: "60".
  11. '
  12. DEFAULT_PDNS_TTL=60
  13. ######## Public functions #####################
  14. #Usage: add _acme-challenge.www.domain.com "123456789ABCDEF0000000000000000000000000000000000000"
  15. #fulldomain
  16. #txtvalue
  17. dns_pdns_add() {
  18. fulldomain=$1
  19. txtvalue=$2
  20. PDNS_Url="${PDNS_Url:-$(_readaccountconf_mutable PDNS_Url)}"
  21. PDNS_ServerId="${PDNS_ServerId:-$(_readaccountconf_mutable PDNS_ServerId)}"
  22. PDNS_Token="${PDNS_Token:-$(_readaccountconf_mutable PDNS_Token)}"
  23. PDNS_Ttl="${PDNS_Ttl:-$(_readaccountconf_mutable PDNS_Ttl)}"
  24. if [ -z "$PDNS_Url" ]; then
  25. PDNS_Url=""
  26. _err "You don't specify PowerDNS address."
  27. _err "Please set PDNS_Url and try again."
  28. return 1
  29. fi
  30. if [ -z "$PDNS_ServerId" ]; then
  31. PDNS_ServerId=""
  32. _err "You don't specify PowerDNS server id."
  33. _err "Please set you PDNS_ServerId and try again."
  34. return 1
  35. fi
  36. if [ -z "$PDNS_Token" ]; then
  37. PDNS_Token=""
  38. _err "You don't specify PowerDNS token."
  39. _err "Please create you PDNS_Token and try again."
  40. return 1
  41. fi
  42. if [ -z "$PDNS_Ttl" ]; then
  43. PDNS_Ttl="$DEFAULT_PDNS_TTL"
  44. fi
  45. #save the api addr and key to the account conf file.
  46. _saveaccountconf_mutable PDNS_Url "$PDNS_Url"
  47. _saveaccountconf_mutable PDNS_ServerId "$PDNS_ServerId"
  48. _saveaccountconf_mutable PDNS_Token "$PDNS_Token"
  49. if [ "$PDNS_Ttl" != "$DEFAULT_PDNS_TTL" ]; then
  50. _saveaccountconf_mutable PDNS_Ttl "$PDNS_Ttl"
  51. fi
  52. _debug "Detect root zone"
  53. if ! _get_root "$fulldomain"; then
  54. _err "invalid domain"
  55. return 1
  56. fi
  57. _debug _domain "$_domain"
  58. if ! set_record "$_domain" "$fulldomain" "$txtvalue"; then
  59. return 1
  60. fi
  61. return 0
  62. }
  63. #fulldomain
  64. dns_pdns_rm() {
  65. fulldomain=$1
  66. txtvalue=$2
  67. PDNS_Url="${PDNS_Url:-$(_readaccountconf_mutable PDNS_Url)}"
  68. PDNS_ServerId="${PDNS_ServerId:-$(_readaccountconf_mutable PDNS_ServerId)}"
  69. PDNS_Token="${PDNS_Token:-$(_readaccountconf_mutable PDNS_Token)}"
  70. PDNS_Ttl="${PDNS_Ttl:-$(_readaccountconf_mutable PDNS_Ttl)}"
  71. if [ -z "$PDNS_Ttl" ]; then
  72. PDNS_Ttl="$DEFAULT_PDNS_TTL"
  73. fi
  74. _debug "Detect root zone"
  75. if ! _get_root "$fulldomain"; then
  76. _err "invalid domain"
  77. return 1
  78. fi
  79. _debug _domain "$_domain"
  80. if ! rm_record "$_domain" "$fulldomain" "$txtvalue"; then
  81. return 1
  82. fi
  83. return 0
  84. }
  85. set_record() {
  86. _info "Adding record"
  87. root=$1
  88. full=$2
  89. new_challenge=$3
  90. _record_string=""
  91. _build_record_string "$new_challenge"
  92. _list_existingchallenges
  93. for oldchallenge in $_existing_challenges; do
  94. _build_record_string "$oldchallenge"
  95. done
  96. if ! _pdns_rest "PATCH" "/api/v1/servers/$PDNS_ServerId/zones/$root" "{\"rrsets\": [{\"changetype\": \"REPLACE\", \"name\": \"$full.\", \"type\": \"TXT\", \"ttl\": $PDNS_Ttl, \"records\": [$_record_string]}]}" "application/json"; then
  97. _err "Set txt record error."
  98. return 1
  99. fi
  100. if ! notify_slaves "$root"; then
  101. return 1
  102. fi
  103. return 0
  104. }
  105. rm_record() {
  106. _info "Remove record"
  107. root=$1
  108. full=$2
  109. txtvalue=$3
  110. #Enumerate existing acme challenges
  111. _list_existingchallenges
  112. if _contains "$_existing_challenges" "$txtvalue"; then
  113. #Delete all challenges (PowerDNS API does not allow to delete content)
  114. if ! _pdns_rest "PATCH" "/api/v1/servers/$PDNS_ServerId/zones/$root" "{\"rrsets\": [{\"changetype\": \"DELETE\", \"name\": \"$full.\", \"type\": \"TXT\"}]}" "application/json"; then
  115. _err "Delete txt record error."
  116. return 1
  117. fi
  118. _record_string=""
  119. #If the only existing challenge was the challenge to delete: nothing to do
  120. if ! [ "$_existing_challenges" = "$txtvalue" ]; then
  121. for oldchallenge in $_existing_challenges; do
  122. #Build up the challenges to re-add, ommitting the one what should be deleted
  123. if ! [ "$oldchallenge" = "$txtvalue" ]; then
  124. _build_record_string "$oldchallenge"
  125. fi
  126. done
  127. #Recreate the existing challenges
  128. if ! _pdns_rest "PATCH" "/api/v1/servers/$PDNS_ServerId/zones/$root" "{\"rrsets\": [{\"changetype\": \"REPLACE\", \"name\": \"$full.\", \"type\": \"TXT\", \"ttl\": $PDNS_Ttl, \"records\": [$_record_string]}]}" "application/json"; then
  129. _err "Set txt record error."
  130. return 1
  131. fi
  132. fi
  133. if ! notify_slaves "$root"; then
  134. return 1
  135. fi
  136. else
  137. _info "Record not found, nothing to remove"
  138. fi
  139. return 0
  140. }
  141. notify_slaves() {
  142. root=$1
  143. if ! _pdns_rest "PUT" "/api/v1/servers/$PDNS_ServerId/zones/$root/notify"; then
  144. _err "Notify slaves error."
  145. return 1
  146. fi
  147. return 0
  148. }
  149. #################### Private functions below ##################################
  150. #_acme-challenge.www.domain.com
  151. #returns
  152. # _domain=domain.com
  153. _get_root() {
  154. domain=$1
  155. i=1
  156. if _pdns_rest "GET" "/api/v1/servers/$PDNS_ServerId/zones"; then
  157. _zones_response=$(echo "$response" | _normalizeJson)
  158. fi
  159. while true; do
  160. h=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
  161. if _contains "$_zones_response" "\"name\":\"$h.\""; then
  162. _domain="$h."
  163. if [ -z "$h" ]; then
  164. _domain="=2E"
  165. fi
  166. return 0
  167. fi
  168. if [ -z "$h" ]; then
  169. return 1
  170. fi
  171. i=$(_math "$i" + 1)
  172. done
  173. _debug "$domain not found"
  174. return 1
  175. }
  176. _pdns_rest() {
  177. method=$1
  178. ep=$2
  179. data=$3
  180. ct=$4
  181. export _H1="X-API-Key: $PDNS_Token"
  182. if [ ! "$method" = "GET" ]; then
  183. _debug data "$data"
  184. response="$(_post "$data" "$PDNS_Url$ep" "" "$method" "$ct")"
  185. else
  186. response="$(_get "$PDNS_Url$ep")"
  187. fi
  188. if [ "$?" != "0" ]; then
  189. _err "error $ep"
  190. return 1
  191. fi
  192. _debug2 response "$response"
  193. return 0
  194. }
  195. _build_record_string() {
  196. _record_string="${_record_string:+${_record_string}, }{\"content\": \"\\\"${1}\\\"\", \"disabled\": false}"
  197. }
  198. _list_existingchallenges() {
  199. _pdns_rest "GET" "/api/v1/servers/$PDNS_ServerId/zones/$root"
  200. _existing_challenges=$(echo "$response" | _normalizeJson | _egrep_o "\"name\":\"${fulldomain}[^]]*}" | _egrep_o 'content\":\"\\"[^\\]*' | sed -n 's/^content":"\\"//p')
  201. }