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.

196 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. _record_string=""
  77. _build_record_string "$new_challenge"
  78. _existing_challenges=$(echo "$response" | _normalizeJson | grep -Po "\"name\":\"$fulldomain\\K.*?}]" | grep -Po 'content\":\"\\"\K[^\\]*')
  79. for oldchallenge in $_existing_challenges; do
  80. _record_string="${_record_string}, "
  81. _build_record_string "$oldchallenge"
  82. done
  83. 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
  84. _err "Set txt record error."
  85. return 1
  86. fi
  87. return 0
  88. }
  89. rm_record() {
  90. _info "Remove record"
  91. root=$1
  92. full=$2
  93. if ! _pdns_rest "PATCH" "/api/v1/servers/$PDNS_ServerId/zones/$root" "{\"rrsets\": [{\"changetype\": \"DELETE\", \"name\": \"$full.\", \"type\": \"TXT\"}]}"; then
  94. _err "Delete txt record error."
  95. return 1
  96. fi
  97. if ! notify_slaves "$root"; then
  98. return 1
  99. fi
  100. return 0
  101. }
  102. notify_slaves() {
  103. root=$1
  104. if ! _pdns_rest "PUT" "/api/v1/servers/$PDNS_ServerId/zones/$root/notify"; then
  105. _err "Notify slaves error."
  106. return 1
  107. fi
  108. return 0
  109. }
  110. #################### Private functions below ##################################
  111. #_acme-challenge.www.domain.com
  112. #returns
  113. # _domain=domain.com
  114. _get_root() {
  115. domain=$1
  116. i=1
  117. if _pdns_rest "GET" "/api/v1/servers/$PDNS_ServerId/zones"; then
  118. _zones_response="$response"
  119. fi
  120. while true; do
  121. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  122. if _contains "$_zones_response" "\"name\": \"$h.\""; then
  123. _domain="$h."
  124. if [ -z "$h" ]; then
  125. _domain="=2E"
  126. fi
  127. return 0
  128. fi
  129. if [ -z "$h" ]; then
  130. return 1
  131. fi
  132. i=$(_math $i + 1)
  133. done
  134. _debug "$domain not found"
  135. return 1
  136. }
  137. _pdns_rest() {
  138. method=$1
  139. ep=$2
  140. data=$3
  141. export _H1="X-API-Key: $PDNS_Token"
  142. if [ ! "$method" = "GET" ]; then
  143. _debug data "$data"
  144. response="$(_post "$data" "$PDNS_Url$ep" "" "$method")"
  145. else
  146. response="$(_get "$PDNS_Url$ep")"
  147. fi
  148. if [ "$?" != "0" ]; then
  149. _err "error $ep"
  150. return 1
  151. fi
  152. _debug2 response "$response"
  153. return 0
  154. }
  155. _build_record_string() {
  156. _record_string="${_record_string}{\"content\": \"\\\"$1\\\"\", \"disabled\": false}"
  157. }