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.

202 lines
4.1 KiB

8 years ago
  1. #!/usr/bin/env sh
  2. #PowerDNS Embedded API
  3. #https://doc.powerdns.com/md/httpapi/api_spec/
  4. #
  5. #PDNS_Url="http://ns.example.com:8081"
  6. #PDNS_ServerId="localhost"
  7. #PDNS_Token="0123456789ABCDEF"
  8. #PDNS_Ttl=60
  9. DEFAULT_PDNS_TTL=60
  10. ######## Public functions #####################
  11. #Usage: add _acme-challenge.www.domain.com "123456789ABCDEF0000000000000000000000000000000000000"
  12. #fulldomain
  13. #txtvalue
  14. dns_pdns_add() {
  15. fulldomain=$1
  16. txtvalue=$2
  17. if [ -z "$PDNS_Url" ]; then
  18. PDNS_Url=""
  19. _err "You don't specify PowerDNS address."
  20. _err "Please set PDNS_Url and try again."
  21. return 1
  22. fi
  23. if [ -z "$PDNS_ServerId" ]; then
  24. PDNS_ServerId=""
  25. _err "You don't specify PowerDNS server id."
  26. _err "Please set you PDNS_ServerId and try again."
  27. return 1
  28. fi
  29. if [ -z "$PDNS_Token" ]; then
  30. PDNS_Token=""
  31. _err "You don't specify PowerDNS token."
  32. _err "Please create you PDNS_Token and try again."
  33. return 1
  34. fi
  35. if [ -z "$PDNS_Ttl" ]; then
  36. PDNS_Ttl="$DEFAULT_PDNS_TTL"
  37. fi
  38. #save the api addr and key to the account conf file.
  39. _saveaccountconf PDNS_Url "$PDNS_Url"
  40. _saveaccountconf PDNS_ServerId "$PDNS_ServerId"
  41. _saveaccountconf PDNS_Token "$PDNS_Token"
  42. if [ "$PDNS_Ttl" != "$DEFAULT_PDNS_TTL" ]; then
  43. _saveaccountconf PDNS_Ttl "$PDNS_Ttl"
  44. fi
  45. _debug "Detect root zone"
  46. if ! _get_root "$fulldomain"; then
  47. _err "invalid domain"
  48. return 1
  49. fi
  50. _debug _domain "$_domain"
  51. if ! set_record "$_domain" "$fulldomain" "$txtvalue"; then
  52. return 1
  53. fi
  54. return 0
  55. }
  56. #fulldomain
  57. dns_pdns_rm() {
  58. fulldomain=$1
  59. _debug "Detect root zone"
  60. if ! _get_root "$fulldomain"; then
  61. _err "invalid domain"
  62. return 1
  63. fi
  64. _debug _domain "$_domain"
  65. if ! rm_record "$_domain" "$fulldomain"; then
  66. return 1
  67. fi
  68. return 0
  69. }
  70. set_record() {
  71. _info "Adding record"
  72. root=$1
  73. full=$2
  74. new_challenge=$3
  75. _pdns_rest "GET" "/api/v1/servers/$PDNS_ServerId/zones/$root"
  76. _existing_challenges=($(echo "$response" | _normalizeJson | grep -Po "\"name\":\"$fulldomain\K.*?}]" | grep -Po 'content\":\"\\"\K[^\\]*'))
  77. _record_string=""
  78. _build_record_string $new_challenge
  79. for i in "${_existing_challenges[@]}"
  80. do
  81. _record_string+=", "
  82. _build_record_string $i
  83. done
  84. if ! _pdns_rest "PATCH" "/api/v1/servers/$PDNS_ServerId/zones/$root" "{\"rrsets\": [{\"changetype\": \"REPLACE\", \"name\": \"$full.\", \"type\": \"TXT\", \"ttl\": $PDNS_Ttl, \"records\": [$_record_string]}]}"; then
  85. _err "Set txt record error."
  86. return 1
  87. fi
  88. if ! notify_slaves "$root"; then
  89. return 1
  90. fi
  91. return 0
  92. }
  93. rm_record() {
  94. _info "Remove record"
  95. root=$1
  96. full=$2
  97. if ! _pdns_rest "PATCH" "/api/v1/servers/$PDNS_ServerId/zones/$root" "{\"rrsets\": [{\"changetype\": \"DELETE\", \"name\": \"$full.\", \"type\": \"TXT\"}]}"; then
  98. _err "Delete txt record error."
  99. return 1
  100. fi
  101. if ! notify_slaves "$root"; then
  102. return 1
  103. fi
  104. return 0
  105. }
  106. notify_slaves() {
  107. root=$1
  108. if ! _pdns_rest "PUT" "/api/v1/servers/$PDNS_ServerId/zones/$root/notify"; then
  109. _err "Notify slaves error."
  110. return 1
  111. fi
  112. return 0
  113. }
  114. #################### Private functions below ##################################
  115. #_acme-challenge.www.domain.com
  116. #returns
  117. # _domain=domain.com
  118. _get_root() {
  119. domain=$1
  120. i=1
  121. if _pdns_rest "GET" "/api/v1/servers/$PDNS_ServerId/zones"; then
  122. _zones_response="$response"
  123. fi
  124. while true; do
  125. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  126. if _contains "$_zones_response" "\"name\": \"$h.\""; then
  127. _domain="$h."
  128. if [ -z "$h" ]; then
  129. _domain="=2E"
  130. fi
  131. return 0
  132. fi
  133. if [ -z "$h" ]; then
  134. return 1
  135. fi
  136. i=$(_math $i + 1)
  137. done
  138. _debug "$domain not found"
  139. return 1
  140. }
  141. _pdns_rest() {
  142. method=$1
  143. ep=$2
  144. data=$3
  145. export _H1="X-API-Key: $PDNS_Token"
  146. if [ ! "$method" = "GET" ]; then
  147. _debug data "$data"
  148. response="$(_post "$data" "$PDNS_Url$ep" "" "$method")"
  149. else
  150. response="$(_get "$PDNS_Url$ep")"
  151. fi
  152. if [ "$?" != "0" ]; then
  153. _err "error $ep"
  154. return 1
  155. fi
  156. _debug2 response "$response"
  157. return 0
  158. }
  159. _build_record_string() {
  160. _record_string+="{\"content\": \"\\\"$1\\\"\", \"disabled\": false}"
  161. }