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.

135 lines
3.4 KiB

  1. #!/usr/bin/bash
  2. #Hosttech_Key="abcdefg"
  3. Hosttech_Api="https://api.ns1.hosttech.eu/api/user/v1"
  4. ######## Public functions #####################
  5. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  6. dns_hosttech_add() {
  7. fulldomain=$1
  8. txtvalue=$2
  9. Hosttech_Key="${Hosttech_Key:-$(_readaccountconf_mutable Hosttech_Key)}"
  10. if [ -z "$Hosttech_Key" ]; then
  11. Hosttech_Key=""
  12. _err "You didn't specify a Hosttech api key."
  13. _err "You can get yours from https://www.myhosttech.eu/user/dns/api"
  14. return 1
  15. fi
  16. #save the api key and email to the account conf file.
  17. _saveaccountconf_mutable Hosttech_Key "$Hosttech_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 _hosttech_rest POST "zones/$_domain/records" "{\"type\":\"TXT\",\"name\":\"$_sub_domain\",\"text\":\"$txtvalue\",\"ttl\":600}"; then
  27. if _contains "$_response" "$_sub_domain"; then
  28. _debug recordID "$(echo "$_response" | grep -o '"id":[^"]*' | grep -Po "\d+")"
  29. #save the created recordID to the account conf file, so we can read it back for deleting in dns_hosttech_rm.
  30. _saveaccountconf recordID "$(echo "$_response" | grep -o '"id":[^"]*' | grep -Po "\d+")"
  31. _info "Added, OK"
  32. return 0
  33. else
  34. _err "Add txt record error."
  35. return 1
  36. fi
  37. fi
  38. _err "Add txt record error."
  39. return 1
  40. }
  41. #fulldomain txtvalue
  42. dns_hosttech_rm() {
  43. fulldomain=$1
  44. txtvalue=$2
  45. Hosttech_Key="${Hosttech_Key:-$(_readaccountconf_mutable Hosttech_Key)}"
  46. if [ -z "$Hosttech_Key" ]; then
  47. Hosttech_Key=""
  48. _err "You didn't specify a Hosttech api key."
  49. _err "You can get yours from https://www.hosttech.nl/mijn_hosttech"
  50. return 1
  51. fi
  52. _debug "First detect the root zone"
  53. if ! _get_root "$fulldomain"; then
  54. _err "invalid domain"
  55. return 1
  56. fi
  57. _debug _sub_domain "$_sub_domain"
  58. _debug _domain "$_domain"
  59. _debug "Removing txt record"
  60. delRecordID="$(_readaccountconf "recordID")"
  61. _hosttech_rest DELETE "zones/$_domain/records/$delRecordID"
  62. _clearaccountconf recordID
  63. }
  64. #################### Private functions below ##################################
  65. #_acme-challenge.www.domain.com
  66. #returns
  67. # _sub_domain=_acme-challenge.www
  68. # _domain=domain.com
  69. _get_root() {
  70. domain=$1
  71. i=2
  72. p=1
  73. while true; do
  74. _domain=$(printf "%s" "$domain" | cut -d . -f $i-100)
  75. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  76. _debug _domain "$_domain"
  77. if [ -z "$_domain" ]; then
  78. #not valid
  79. return 1
  80. fi
  81. if _hosttech_rest GET "zones?query=${_domain}"; then
  82. if [ "$(echo "$_response" | grep -o '"name":"[^"]*' | cut -d'"' -f4)" = "${_domain}" ]; then
  83. return 0
  84. fi
  85. else
  86. return 1
  87. fi
  88. p=$i
  89. i=$(_math "$i" + 1)
  90. done
  91. return 1
  92. }
  93. _hosttech_rest() {
  94. m=$1
  95. ep="$2"
  96. data="$3"
  97. _debug "$ep"
  98. export _H1="Authorization: Bearer $Hosttech_Key"
  99. export _H2="accept: application/json"
  100. export _H3="Content-Type: application/json"
  101. _debug data "$data"
  102. _response="$(_post "$data" "$Hosttech_Api/$ep" "" "$m")"
  103. _code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r\\n")"
  104. _debug "http response code $_code"
  105. if [ "$?" != "0" ]; then
  106. _err "error $ep"
  107. return 1
  108. fi
  109. _debug2 response "$_response"
  110. return 0
  111. }