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.

172 lines
3.7 KiB

  1. #!/usr/bin/env sh
  2. #PowerDNS Mysql backend
  3. #
  4. #
  5. PDNS_Host="example.com"
  6. PDNS_Port=3306
  7. PDNS_User="username"
  8. PDNS_Pass="password"
  9. PDNS_Database="powerdns"
  10. PDNS_Ttl=60
  11. DEFAULT_PDNS_TTL=60
  12. ######## Public functions #####################
  13. #Usage: add _acme-challenge.www.domain.com "123456789ABCDEF0000000000000000000000000000000000000"
  14. #fulldomain
  15. #txtvalue
  16. dns_pdnsMysql_add() {
  17. fulldomain=$1
  18. txtvalue=$2
  19. if [ -z "$PDNS_Host" ]; then
  20. PDNS_Url=""
  21. _err "You didn't specify PowerDNS Mysql address."
  22. _err "Please set PDNS_Host and try again."
  23. return 1
  24. fi
  25. if [ -z "$PDNS_Port" ]; then
  26. PDNS_Url=""
  27. _err "You didn't specify PowerDNS Mysql Port."
  28. _err "Please set PDNS_Port and try again."
  29. return 1
  30. fi
  31. if [ -z "$PDNS_User" ]; then
  32. PDNS_User=""
  33. _err "You didn't specify PowerDNS Mysql username."
  34. _err "Please set PDNS_User and try again."
  35. return 1
  36. fi
  37. if [ -z "$PDNS_Pass" ]; then
  38. PDNS_Pass=""
  39. _err "You didn't specify PowerDNS Mysql password."
  40. _err "Please set PDNS_Pass and try again."
  41. return 1
  42. fi
  43. if [ -z "$PDNS_Database" ]; then
  44. PDNS_Database=""
  45. _err "You didn't specify PowerDNS Mysql database."
  46. _err "Please set PDNS_Database and try again."
  47. return 1
  48. fi
  49. if [ -z "$PDNS_Ttl" ]; then
  50. PDNS_Ttl="$DEFAULT_PDNS_TTL"
  51. fi
  52. #save the api addr and key to the account conf file.
  53. _saveaccountconf PDNS_Host "$PDNS_Host"
  54. _saveaccountconf PDNS_Port "$PDNS_Port"
  55. _saveaccountconf PDNS_User "$PDNS_User"
  56. _saveaccountconf PDNS_Pass "$PDNS_Pass"
  57. _saveaccountconf PDNS_Database "$PDNS_Database"
  58. if [ "$PDNS_Ttl" != "$DEFAULT_PDNS_TTL" ]; then
  59. _saveaccountconf PDNS_Ttl "$PDNS_Ttl"
  60. fi
  61. _debug "Detect root zone"
  62. if ! _get_root "$fulldomain"; then
  63. _err "invalid domain"
  64. return 1
  65. fi
  66. _debug _domain "$_domain"
  67. if ! set_record "$_domain" "$fulldomain" "$txtvalue"; then
  68. return 1
  69. fi
  70. return 0
  71. }
  72. #fulldomain
  73. dns_pdnsMysql_rm() {
  74. fulldomain=$1
  75. _debug "Detect root zone"
  76. if ! _get_root "$fulldomain"; then
  77. _err "invalid domain"
  78. return 1
  79. fi
  80. _debug _domain "$_domain"
  81. if ! rm_record "$_domain" "$fulldomain"; then
  82. return 1
  83. fi
  84. return 0
  85. }
  86. set_record() {
  87. _info "Adding record"
  88. root=$1
  89. full=$2
  90. txtvalue=$3
  91. _domain_id=$(mysql -ss "-h${PDNS_Host}" "-P${PDNS_Port}" "-u${PDNS_User}" "-p${PDNS_Pass}" -e "SELECT id FROM ${PDNS_Database}.domains WHERE name='${root}'")
  92. # insert challenge.
  93. mysql -ss "-h${PDNS_Host}" "-P${PDNS_Port}" "-u${PDNS_User}" "-p${PDNS_Pass}" -e "INSERT INTO ${PDNS_Database}.records (domain_id,name, content, type,ttl,prio) VALUES \
  94. (${_domain_id},'${full}','${txtvalue}','TXT',60,NULL);"
  95. if ! notify_slaves "$root"; then
  96. return 1
  97. fi
  98. return 0
  99. }
  100. rm_record() {
  101. _info "Remove record"
  102. root=$1
  103. full=$2
  104. mysql -ss "-h${PDNS_Host}" "-P${PDNS_Port}" "-u${PDNS_User}" "-p${PDNS_Pass}" -e "DELETE FROM ${PDNS_Database}.records WHERE name='${full}' AND type='TXT';"
  105. if ! notify_slaves "$root"; then
  106. return 1
  107. fi
  108. return 0
  109. }
  110. notify_slaves() {
  111. root=$1
  112. ### Need finished ###
  113. return 0
  114. }
  115. #################### Private functions below ##################################
  116. #_acme-challenge.www.domain.com
  117. #returns
  118. # _domain=domain.com
  119. _get_root() {
  120. domain=$1
  121. i=1
  122. _pdns_domains=$(mysql -ss "-h${PDNS_Host}" "-P${PDNS_Port}" "-u${PDNS_User}" "-p${PDNS_Pass}" -e "SELECT name FROM ${PDNS_Database}.domains")
  123. if [ -z "$_pdns_domains" ]; then
  124. return 1
  125. fi
  126. while true; do
  127. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  128. if [ -z "$h" ]; then
  129. return 1
  130. fi
  131. if _contains "$_pdns_domains" "$h"; then
  132. _domain="$h"
  133. return 0
  134. fi
  135. i=$(_math $i + 1)
  136. done
  137. _debug "$domain not found"
  138. return 1
  139. }