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.

164 lines
4.3 KiB

1 month ago
1 month ago
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_domeneshop_info='DomeneShop.no
  4. Site: DomeneShop.no
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi2#dns_domeneshop
  6. Options:
  7. DOMENESHOP_Token Token
  8. DOMENESHOP_Secret Secret
  9. Issues: github.com/acmesh-official/acme.sh/issues/2457
  10. '
  11. DOMENESHOP_Api_Endpoint="https://api.domeneshop.no/v0"
  12. ##################### Public functions #####################
  13. # Usage: dns_domeneshop_add <full domain> <txt record>
  14. # Example: dns_domeneshop_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  15. dns_domeneshop_add() {
  16. fulldomain=$1
  17. txtvalue=$2
  18. # Get token and secret
  19. DOMENESHOP_Token="${DOMENESHOP_Token:-$(_readaccountconf_mutable DOMENESHOP_Token)}"
  20. DOMENESHOP_Secret="${DOMENESHOP_Secret:-$(_readaccountconf_mutable DOMENESHOP_Secret)}"
  21. if [ -z "$DOMENESHOP_Token" ] || [ -z "$DOMENESHOP_Secret" ]; then
  22. DOMENESHOP_Token=""
  23. DOMENESHOP_Secret=""
  24. _err "You need to spesify a Domeneshop/Domainnameshop API Token and Secret."
  25. return 1
  26. fi
  27. # Save the api token and secret.
  28. _saveaccountconf_mutable DOMENESHOP_Token "$DOMENESHOP_Token"
  29. _saveaccountconf_mutable DOMENESHOP_Secret "$DOMENESHOP_Secret"
  30. # Get the domain name id
  31. if ! _get_domainid "$fulldomain"; then
  32. _err "Did not find domainname"
  33. return 1
  34. fi
  35. # Create record
  36. _domeneshop_rest POST "domains/$_domainid/dns" "{\"type\":\"TXT\",\"host\":\"$_sub_domain\",\"data\":\"$txtvalue\",\"ttl\":120}"
  37. }
  38. # Usage: dns_domeneshop_rm <full domain> <txt record>
  39. # Example: dns_domeneshop_rm _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  40. dns_domeneshop_rm() {
  41. fulldomain=$1
  42. txtvalue=$2
  43. # Get token and secret
  44. DOMENESHOP_Token="${DOMENESHOP_Token:-$(_readaccountconf_mutable DOMENESHOP_Token)}"
  45. DOMENESHOP_Secret="${DOMENESHOP_Secret:-$(_readaccountconf_mutable DOMENESHOP_Secret)}"
  46. if [ -z "$DOMENESHOP_Token" ] || [ -z "$DOMENESHOP_Secret" ]; then
  47. DOMENESHOP_Token=""
  48. DOMENESHOP_Secret=""
  49. _err "You need to spesify a Domeneshop/Domainnameshop API Token and Secret."
  50. return 1
  51. fi
  52. # Get the domain name id
  53. if ! _get_domainid "$fulldomain"; then
  54. _err "Did not find domainname"
  55. return 1
  56. fi
  57. # Find record
  58. if ! _get_recordid "$_domainid" "$_sub_domain" "$txtvalue"; then
  59. _err "Did not find dns record"
  60. return 1
  61. fi
  62. # Remove record
  63. _domeneshop_rest DELETE "domains/$_domainid/dns/$_recordid"
  64. }
  65. ##################### Private functions #####################
  66. _get_domainid() {
  67. domain=$1
  68. # Get domains
  69. _domeneshop_rest GET "domains"
  70. if ! _contains "$response" "\"id\":"; then
  71. _err "failed to get domain names"
  72. return 1
  73. fi
  74. i=2
  75. p=1
  76. while true; do
  77. h=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
  78. _debug "h" "$h"
  79. if [ -z "$h" ]; then
  80. #not valid
  81. return 1
  82. fi
  83. if _contains "$response" "\"$h\"" >/dev/null; then
  84. # We have found the domain name.
  85. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-"$p")
  86. _domain=$h
  87. _domainid=$(printf "%s" "$response" | _egrep_o "[^{]*\"domain\":\"$_domain\"[^}]*" | _egrep_o "\"id\":[0-9]+" | cut -d : -f 2)
  88. return 0
  89. fi
  90. p=$i
  91. i=$(_math "$i" + 1)
  92. done
  93. return 1
  94. }
  95. _get_recordid() {
  96. domainid=$1
  97. subdomain=$2
  98. txtvalue=$3
  99. # Get all dns records for the domainname
  100. _domeneshop_rest GET "domains/$domainid/dns"
  101. if ! _contains "$response" "\"id\":"; then
  102. _debug "No records in dns"
  103. return 1
  104. fi
  105. if ! _contains "$response" "\"host\":\"$subdomain\""; then
  106. _debug "Record does not exist"
  107. return 1
  108. fi
  109. # Get the id of the record in question
  110. _recordid=$(printf "%s" "$response" | _egrep_o "[^{]*\"host\":\"$subdomain\"[^}]*" | _egrep_o "[^{]*\"data\":\"$txtvalue\"[^}]*" | _egrep_o "\"id\":[0-9]+" | cut -d : -f 2)
  111. if [ -z "$_recordid" ]; then
  112. return 1
  113. fi
  114. return 0
  115. }
  116. _domeneshop_rest() {
  117. method=$1
  118. endpoint=$2
  119. data=$3
  120. credentials=$(printf "%b" "$DOMENESHOP_Token:$DOMENESHOP_Secret" | _base64)
  121. export _H1="Authorization: Basic $credentials"
  122. export _H2="Content-Type: application/json"
  123. if [ "$method" != "GET" ]; then
  124. response="$(_post "$data" "$DOMENESHOP_Api_Endpoint/$endpoint" "" "$method")"
  125. else
  126. response="$(_get "$DOMENESHOP_Api_Endpoint/$endpoint")"
  127. fi
  128. if [ "$?" != "0" ]; then
  129. _err "error $endpoint"
  130. return 1
  131. fi
  132. return 0
  133. }