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