Mirror of acme.sh — A pure Unix shell script implementing ACME client protocol
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.

dns_domeneshop.sh 4.0KB

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