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.

127 lines
3.0 KiB

2 years ago
  1. #!/usr/bin/env sh
  2. #NederHost_Key="sdfgikogfdfghjklkjhgfcdcfghj"
  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. _debug data "$data"
  96. response="$(_post "$data" "$NederHost_Api/$ep" "" "$m")"
  97. _code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r\\n")"
  98. _debug "http response code $_code"
  99. if [ "$?" != "0" ]; then
  100. _err "error $ep"
  101. return 1
  102. fi
  103. _debug2 response "$response"
  104. return 0
  105. }