From 88cccabe0835db54f60883526cf3f77d2eb895fe Mon Sep 17 00:00:00 2001 From: Dan <36902356+danc403@users.noreply.github.com> Date: Wed, 28 Feb 2018 17:04:43 -0600 Subject: [PATCH] Work in progress Use PDNS Mysql backend --- dnsapi/dns_pdns-mysql.sh | 95 +++++++++++++++++++--------------------- 1 file changed, 45 insertions(+), 50 deletions(-) diff --git a/dnsapi/dns_pdns-mysql.sh b/dnsapi/dns_pdns-mysql.sh index 1fa2b4b5..77869a14 100644 --- a/dnsapi/dns_pdns-mysql.sh +++ b/dnsapi/dns_pdns-mysql.sh @@ -1,11 +1,13 @@ #!/usr/bin/env sh -#PowerDNS Embedded API -#https://doc.powerdns.com/md/httpapi/api_spec/ +#PowerDNS Mysql backend # -PDNS_Url="http://idragonfly.net:3306" -PDNS_ServerId="admin" -PDNS_Token="dc32asd#" +# +PDNS_Host="example.com" +PDNS_Port=3306 +PDNS_User="username" +PDNS_Pass="password" +PDNS_Database="powerdns" PDNS_Ttl=60 DEFAULT_PDNS_TTL=60 @@ -14,28 +16,42 @@ DEFAULT_PDNS_TTL=60 #Usage: add _acme-challenge.www.domain.com "123456789ABCDEF0000000000000000000000000000000000000" #fulldomain #txtvalue -dns_pdns_add() { +dns_pdns-mysql_add() { fulldomain=$1 txtvalue=$2 - if [ -z "$PDNS_Url" ]; then + if [ -z "$PDNS_Host" ]; then + PDNS_Url="" + _err "You didn't specify PowerDNS Mysql address." + _err "Please set PDNS_Host and try again." + return 1 + fi + + if [ -z "$PDNS_Port" ]; then PDNS_Url="" - _err "You don't specify PowerDNS address." - _err "Please set PDNS_Url and try again." + _err "You didn't specify PowerDNS Mysql Port." + _err "Please set PDNS_Port and try again." return 1 fi - if [ -z "$PDNS_ServerId" ]; then - PDNS_ServerId="" - _err "You don't specify PowerDNS server id." - _err "Please set you PDNS_ServerId and try again." + if [ -z "$PDNS_User" ]; then + PDNS_User="" + _err "You didn't specify PowerDNS Mysql username." + _err "Please set PDNS_User and try again." return 1 fi - if [ -z "$PDNS_Token" ]; then - PDNS_Token="" - _err "You don't specify PowerDNS token." - _err "Please create you PDNS_Token and try again." + if [ -z "$PDNS_Pass" ]; then + PDNS_Pass="" + _err "You didn't specify PowerDNS Mysql password." + _err "Please set PDNS_Pass and try again." + return 1 + fi + + if [ -z "$PDNS_Database" ]; then + PDNS_Database="" + _err "You didn't specify PowerDNS Mysql database." + _err "Please set PDNS_Database and try again." return 1 fi @@ -44,10 +60,12 @@ dns_pdns_add() { fi #save the api addr and key to the account conf file. - _saveaccountconf PDNS_Url "$PDNS_Url" - _saveaccountconf PDNS_ServerId "$PDNS_ServerId" - _saveaccountconf PDNS_Token "$PDNS_Token" - + _saveaccountconf PDNS_Host "$PDNS_Host" + _saveaccountconf PDNS_Port "$PDNS_Port" + _saveaccountconf PDNS_User "$PDNS_User" + _saveaccountconf PDNS_Pass "$PDNS_Pass" + _saveaccountconf PDNS_Database "$PDNS_Database" + if [ "$PDNS_Ttl" != "$DEFAULT_PDNS_TTL" ]; then _saveaccountconf PDNS_Ttl "$PDNS_Ttl" fi @@ -67,7 +85,7 @@ dns_pdns_add() { } #fulldomain -dns_pdns_rm() { +dns_pdns-mysql_rm() { fulldomain=$1 _debug "Detect root zone" @@ -90,7 +108,7 @@ set_record() { full=$2 txtvalue=$3 - 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 + if ! _pdns_rest "PATCH" "/api/v1/servers/$PDNS_User/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 _err "Set txt record error." return 1 fi @@ -107,7 +125,7 @@ rm_record() { root=$1 full=$2 - if ! _pdns_rest "PATCH" "/api/v1/servers/$PDNS_ServerId/zones/$root." "{\"rrsets\": [{\"changetype\": \"DELETE\", \"name\": \"$full.\", \"type\": \"TXT\"}]}"; then + if ! _pdns_rest "PATCH" "/api/v1/servers/$PDNS_User/zones/$root." "{\"rrsets\": [{\"changetype\": \"DELETE\", \"name\": \"$full.\", \"type\": \"TXT\"}]}"; then _err "Delete txt record error." return 1 fi @@ -122,7 +140,7 @@ rm_record() { notify_slaves() { root=$1 - if ! _pdns_rest "PUT" "/api/v1/servers/$PDNS_ServerId/zones/$root./notify"; then + if ! _pdns_rest "PUT" "/api/v1/servers/$PDNS_User/zones/$root./notify"; then _err "Notify slaves error." return 1 fi @@ -137,8 +155,8 @@ notify_slaves() { _get_root() { domain=$1 i=1 - - if _pdns_rest "GET" "/api/v1/servers/$PDNS_ServerId/zones"; then + _pdns_domains=$(mysql -ss "-h${PDNS_Host}" "-P${PDNS_Port}" "-u${PDNS_User}" "-p${PDNS_Pass}" -e "SELECT name FROM ${PDNS_Database}.domains") + if _pdns_rest "GET" "/api/v1/servers/$PDNS_User/zones"; then _zones_response="$response" fi @@ -159,26 +177,3 @@ _get_root() { return 1 } - -_pdns_rest() { - method=$1 - ep=$2 - data=$3 - - export _H1="X-API-Key: $PDNS_Token" - - if [ ! "$method" = "GET" ]; then - _debug data "$data" - response="$(_post "$data" "$PDNS_Url$ep" "" "$method")" - else - response="$(_get "$PDNS_Url$ep")" - fi - - if [ "$?" != "0" ]; then - _err "error $ep" - return 1 - fi - _debug2 response "$response" - - return 0 -}