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.

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