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.

140 lines
3.0 KiB

  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. dns_pdns_add() {
  13. fulldomain=$1
  14. txtvalue=$2
  15. if [ -z "$PDNS_Url" ]; then
  16. PDNS_Url=""
  17. _err "You don't specify PowerDNS address."
  18. _err "Please set PDNS_Url and try again."
  19. return 1
  20. fi
  21. if [ -z "$PDNS_ServerId" ]; then
  22. PDNS_ServerId=""
  23. _err "You don't specify PowerDNS server id."
  24. _err "Please set you PDNS_ServerId and try again."
  25. return 1
  26. fi
  27. if [ -z "$PDNS_Token" ]; then
  28. PDNS_Token=""
  29. _err "You don't specify PowerDNS token."
  30. _err "Please create you PDNS_Token and try again."
  31. return 1
  32. fi
  33. if [ -z "$PDNS_Ttl" ]; then
  34. PDNS_Ttl="$DEFAULT_PDNS_TTL"
  35. fi
  36. #save the api addr and key to the account conf file.
  37. _saveaccountconf PDNS_Url "$PDNS_Url"
  38. _saveaccountconf PDNS_ServerId "$PDNS_ServerId"
  39. _saveaccountconf PDNS_Token "$PDNS_Token"
  40. if [ "$PDNS_Ttl" != "$DEFAULT_PDNS_TTL" ]; then
  41. _saveaccountconf PDNS_Ttl "$PDNS_Ttl"
  42. fi
  43. _debug "First detect the root zone"
  44. if ! _get_root "$fulldomain"; then
  45. _err "invalid domain"
  46. return 1
  47. fi
  48. _debug _domain "$_domain"
  49. if ! set_record "$_domain" "$fulldomain" "$txtvalue"; then
  50. return 1
  51. fi
  52. return 0
  53. }
  54. #fulldomain
  55. dns_pdns_rm() {
  56. fulldomain=$1
  57. }
  58. set_record() {
  59. _info "Adding record"
  60. root=$1
  61. full=$2
  62. txtvalue=$3
  63. if ! _pdns_rest "PATCH" "/api/v1/servers/$PDNS_ServerId/zones/$root." "{\"rrsets\": [{\"name\": \"$full.\", \"changetype\": \"REPLACE\", \"type\": \"TXT\", \"ttl\": $PDNS_Ttl, \"records\": [{\"name\": \"$full.\", \"type\": \"TXT\", \"content\": \"\\\"$txtvalue\\\"\", \"disabled\": false, \"ttl\": $PDNS_Ttl}]}]}"; then
  64. _err "Set txt record error."
  65. return 1
  66. fi
  67. if ! _pdns_rest "PUT" "/api/v1/servers/$PDNS_ServerId/zones/$root./notify"; then
  68. _err "Notify servers error."
  69. return 1
  70. fi
  71. return 0
  72. }
  73. #################### Private functions bellow ##################################
  74. #_acme-challenge.www.domain.com
  75. #returns
  76. # _domain=domain.com
  77. _get_root() {
  78. domain=$1
  79. i=1
  80. if _pdns_rest "GET" "/api/v1/servers/$PDNS_ServerId/zones"; then
  81. _zones_response="$response"
  82. fi
  83. while true; do
  84. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  85. if [ -z "$h" ]; then
  86. return 1
  87. fi
  88. if _contains "$_zones_response" "\"name\": \"$h.\""; then
  89. _domain="$h"
  90. return 0
  91. fi
  92. i=$(_math $i + 1)
  93. done
  94. _debug "$domain not found"
  95. return 1
  96. }
  97. _pdns_rest() {
  98. method=$1
  99. ep=$2
  100. data=$3
  101. _H1="X-API-Key: $PDNS_Token"
  102. if [ ! "$method" = "GET" ]; then
  103. _debug data "$data"
  104. response="$(_post "$data" "$PDNS_Url$ep" "" "$method")"
  105. else
  106. response="$(_get "$PDNS_Url$ep")"
  107. fi
  108. if [ "$?" != "0" ]; then
  109. _err "error $ep"
  110. return 1
  111. fi
  112. _debug2 response "$response"
  113. return 0
  114. }