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.

132 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. set_record() {
  52. _info "Adding record"
  53. root=$1
  54. full=$2
  55. txtvalue=$3
  56. 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
  57. _err "Set txt record error."
  58. return 1
  59. fi
  60. if ! _pdns_rest "PUT" "/api/v1/servers/$PDNS_ServerId/zones/$root./notify" ; then
  61. _err "Notify servers error."
  62. return 1
  63. fi
  64. return 0
  65. }
  66. #################### Private functions bellow ##################################
  67. #_acme-challenge.www.domain.com
  68. #returns
  69. # _domain=domain.com
  70. _get_root() {
  71. domain=$1
  72. i=1
  73. p=1
  74. if _pdns_rest "GET" "/api/v1/servers/$PDNS_ServerId/zones" ; then
  75. _zones_response=$response
  76. fi
  77. while [ '1' ] ; do
  78. h=$(printf $domain | cut -d . -f $i-100)
  79. if [ -z "$h" ] ; then
  80. return 1
  81. fi
  82. if printf "$_zones_response" | grep "\"name\": \"$h.\"" >/dev/null ; then
  83. _domain=$h
  84. return 0
  85. fi
  86. p=$i
  87. i=$(expr $i + 1)
  88. done
  89. _debug "$domain not found"
  90. return 1
  91. }
  92. _pdns_rest() {
  93. method=$1
  94. ep=$2
  95. data=$3
  96. _H1="X-API-Key: $PDNS_Token"
  97. if [ ! "$method" = "GET" ] ; then
  98. _debug data "$data"
  99. response="$(_post "$data" "$PDNS_Url$ep" "" "$method")"
  100. else
  101. response="$(_get "$PDNS_Url$ep")"
  102. fi
  103. if [ "$?" != "0" ] ; then
  104. _err "error $ep"
  105. return 1
  106. fi
  107. _debug2 response "$response"
  108. return 0
  109. }