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.

184 lines
3.7 KiB

8 years ago
  1. #!/usr/bin/env sh
  2. #PowerDNS Emdedded 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. txtvalue=$3
  75. if ! _pdns_rest "PATCH" "/api/v1/servers/$PDNS_ServerId/zones/$root." "{\"rrsets\": [{\"changetype\": \"REPLACE\", \"name\": \"$full.\", \"type\": \"TXT\", \"ttl\": $PDNS_Ttl, \"records\": [{\"name\": \"$full.\", \"type\": \"TXT\", \"content\": \"\\\"$txtvalue\\\"\", \"disabled\": false, \"ttl\": $PDNS_Ttl}]}]}"; then
  76. _err "Set txt record error."
  77. return 1
  78. fi
  79. if ! notify_slaves "$root"; then
  80. return 1
  81. fi
  82. return 0
  83. }
  84. rm_record() {
  85. _info "Remove record"
  86. root=$1
  87. full=$2
  88. if ! _pdns_rest "PATCH" "/api/v1/servers/$PDNS_ServerId/zones/$root." "{\"rrsets\": [{\"changetype\": \"DELETE\", \"name\": \"$full.\", \"type\": \"TXT\"}]}"; then
  89. _err "Delete txt record error."
  90. return 1
  91. fi
  92. if ! notify_slaves "$root"; then
  93. return 1
  94. fi
  95. return 0
  96. }
  97. notify_slaves() {
  98. root=$1
  99. if ! _pdns_rest "PUT" "/api/v1/servers/$PDNS_ServerId/zones/$root./notify"; then
  100. _err "Notify slaves error."
  101. return 1
  102. fi
  103. return 0
  104. }
  105. #################### Private functions below ##################################
  106. #_acme-challenge.www.domain.com
  107. #returns
  108. # _domain=domain.com
  109. _get_root() {
  110. domain=$1
  111. i=1
  112. if _pdns_rest "GET" "/api/v1/servers/$PDNS_ServerId/zones"; then
  113. _zones_response="$response"
  114. fi
  115. while true; do
  116. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  117. if [ -z "$h" ]; then
  118. return 1
  119. fi
  120. if _contains "$_zones_response" "\"name\": \"$h.\""; then
  121. _domain="$h"
  122. return 0
  123. fi
  124. i=$(_math $i + 1)
  125. done
  126. _debug "$domain not found"
  127. return 1
  128. }
  129. _pdns_rest() {
  130. method=$1
  131. ep=$2
  132. data=$3
  133. _H1="X-API-Key: $PDNS_Token"
  134. if [ ! "$method" = "GET" ]; then
  135. _debug data "$data"
  136. response="$(_post "$data" "$PDNS_Url$ep" "" "$method")"
  137. else
  138. response="$(_get "$PDNS_Url$ep")"
  139. fi
  140. if [ "$?" != "0" ]; then
  141. _err "error $ep"
  142. return 1
  143. fi
  144. _debug2 response "$response"
  145. return 0
  146. }