dns_nederhost.sh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #!/usr/bin/env sh
  2. #NederHost_Key="sdfgikogfdfghjklkjhgfcdcfghjk"
  3. NederHost_Api="https://api.nederhost.nl/dns/v1"
  4. ######## Public functions #####################
  5. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  6. dns_nederhost_add() {
  7. fulldomain=$1
  8. txtvalue=$2
  9. NederHost_Key="${NederHost_Key:-$(_readaccountconf_mutable NederHost_Key)}"
  10. if [ -z "$NederHost_Key" ]; then
  11. NederHost_Key=""
  12. _err "You didn't specify a NederHost api key."
  13. _err "You can get yours from https://www.nederhost.nl/mijn_nederhost"
  14. return 1
  15. fi
  16. #save the api key and email to the account conf file.
  17. _saveaccountconf_mutable NederHost_Key "$NederHost_Key"
  18. _debug "First detect the root zone"
  19. if ! _get_root "$fulldomain"; then
  20. _err "invalid domain"
  21. return 1
  22. fi
  23. _debug _sub_domain "$_sub_domain"
  24. _debug _domain "$_domain"
  25. _info "Adding record"
  26. if _nederhost_rest PATCH "zones/$_domain/records/$fulldomain/TXT" "[{\"content\":\"$txtvalue\",\"ttl\":60}]"; then
  27. if _contains "$response" "$fulldomain"; then
  28. _info "Added, OK"
  29. return 0
  30. else
  31. _err "Add txt record error."
  32. return 1
  33. fi
  34. fi
  35. _err "Add txt record error."
  36. return 1
  37. }
  38. #fulldomain txtvalue
  39. dns_nederhost_rm() {
  40. fulldomain=$1
  41. txtvalue=$2
  42. NederHost_Key="${NederHost_Key:-$(_readaccountconf_mutable NederHost_Key)}"
  43. if [ -z "$NederHost_Key" ]; then
  44. NederHost_Key=""
  45. _err "You didn't specify a NederHost api key."
  46. _err "You can get yours from https://www.nederhost.nl/mijn_nederhost"
  47. return 1
  48. fi
  49. _debug "First detect the root zone"
  50. if ! _get_root "$fulldomain"; then
  51. _err "invalid domain"
  52. return 1
  53. fi
  54. _debug _sub_domain "$_sub_domain"
  55. _debug _domain "$_domain"
  56. _debug "Removing txt record"
  57. _nederhost_rest DELETE "zones/${_domain}/records/$fulldomain/TXT?content=$txtvalue"
  58. }
  59. #################### Private functions below ##################################
  60. #_acme-challenge.www.domain.com
  61. #returns
  62. # _sub_domain=_acme-challenge.www
  63. # _domain=domain.com
  64. _get_root() {
  65. domain=$1
  66. i=2
  67. p=1
  68. while true; do
  69. _domain=$(printf "%s" "$domain" | cut -d . -f $i-100)
  70. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  71. _debug _domain "$_domain"
  72. if [ -z "$_domain" ]; then
  73. #not valid
  74. return 1
  75. fi
  76. if _nederhost_rest GET "zones/${_domain}"; then
  77. if [ "${_code}" = "204" ]; then
  78. return 0
  79. fi
  80. else
  81. return 1
  82. fi
  83. p=$i
  84. i=$(_math "$i" + 1)
  85. done
  86. return 1
  87. }
  88. _nederhost_rest() {
  89. m=$1
  90. ep="$2"
  91. data="$3"
  92. _debug "$ep"
  93. export _H1="Authorization: Bearer $NederHost_Key"
  94. export _H2="Content-Type: application/json"
  95. if [ "$m" != "GET" ]; then
  96. _debug data "$data"
  97. response="$(_post "$data" "$NederHost_Api/$ep" "" "$m")"
  98. else
  99. response="$(_get "$NederHost_Api/$ep")"
  100. fi
  101. _code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r\\n")"
  102. _debug "http response code $_code"
  103. if [ "$?" != "0" ]; then
  104. _err "error $ep"
  105. return 1
  106. fi
  107. _debug2 response "$response"
  108. return 0
  109. }