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
3.8 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
1 month ago
6 years ago
1 month ago
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_cn_info='Core-Networks.de
  4. Site: beta.api.Core-Networks.de/doc/
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi#dns_cn
  6. Options:
  7. CN_User User
  8. CN_Password Password
  9. Issues: github.com/acmesh-official/acme.sh/issues/2142
  10. Author: 5ll, francis
  11. '
  12. CN_API="https://beta.api.core-networks.de"
  13. ######## Public functions #####################
  14. dns_cn_add() {
  15. fulldomain=$1
  16. txtvalue=$2
  17. if ! _cn_login; then
  18. _err "login failed"
  19. return 1
  20. fi
  21. _debug "First detect the root zone"
  22. if ! _cn_get_root "$fulldomain"; then
  23. _err "invalid domain"
  24. return 1
  25. fi
  26. _debug "_sub_domain $_sub_domain"
  27. _debug "_domain $_domain"
  28. _info "Adding record"
  29. curData="{\"name\":\"$_sub_domain\",\"ttl\":120,\"type\":\"TXT\",\"data\":\"$txtvalue\"}"
  30. curResult="$(_post "${curData}" "${CN_API}/dnszones/${_domain}/records/")"
  31. _debug "curData $curData"
  32. _debug "curResult $curResult"
  33. if _contains "$curResult" ""; then
  34. _info "Added, OK"
  35. if ! _cn_commit; then
  36. _err "commiting changes failed"
  37. return 1
  38. fi
  39. return 0
  40. else
  41. _err "Add txt record error."
  42. _debug "curData is $curData"
  43. _debug "curResult is $curResult"
  44. _err "error adding text record, response was $curResult"
  45. return 1
  46. fi
  47. }
  48. dns_cn_rm() {
  49. fulldomain=$1
  50. txtvalue=$2
  51. if ! _cn_login; then
  52. _err "login failed"
  53. return 1
  54. fi
  55. _debug "First detect the root zone"
  56. if ! _cn_get_root "$fulldomain"; then
  57. _err "invalid domain"
  58. return 1
  59. fi
  60. _info "Deleting record"
  61. curData="{\"name\":\"$_sub_domain\",\"data\":\"$txtvalue\"}"
  62. curResult="$(_post "${curData}" "${CN_API}/dnszones/${_domain}/records/delete")"
  63. _debug curData is "$curData"
  64. _info "commiting changes"
  65. if ! _cn_commit; then
  66. _err "commiting changes failed"
  67. return 1
  68. fi
  69. _info "Deletet txt record"
  70. return 0
  71. }
  72. ################### Private functions below ##################################
  73. _cn_login() {
  74. CN_User="${CN_User:-$(_readaccountconf_mutable CN_User)}"
  75. CN_Password="${CN_Password:-$(_readaccountconf_mutable CN_Password)}"
  76. if [ -z "$CN_User" ] || [ -z "$CN_Password" ]; then
  77. CN_User=""
  78. CN_Password=""
  79. _err "You must export variables: CN_User and CN_Password"
  80. return 1
  81. fi
  82. #save the config variables to the account conf file.
  83. _saveaccountconf_mutable CN_User "$CN_User"
  84. _saveaccountconf_mutable CN_Password "$CN_Password"
  85. _info "Getting an AUTH-Token"
  86. curData="{\"login\":\"${CN_User}\",\"password\":\"${CN_Password}\"}"
  87. curResult="$(_post "${curData}" "${CN_API}/auth/token")"
  88. _debug "Calling _CN_login: '${curData}' '${CN_API}/auth/token'"
  89. if _contains "${curResult}" '"token":"'; then
  90. authToken=$(echo "${curResult}" | cut -d ":" -f2 | cut -d "," -f1 | sed 's/^.\(.*\).$/\1/')
  91. export _H1="Authorization: Bearer $authToken"
  92. _info "Successfully acquired AUTH-Token"
  93. _debug "AUTH-Token: '${authToken}'"
  94. _debug "_H1 '${_H1}'"
  95. else
  96. _err "Couldn't acquire an AUTH-Token"
  97. return 1
  98. fi
  99. }
  100. # Commit changes
  101. _cn_commit() {
  102. _info "Commiting changes"
  103. _post "" "${CN_API}/dnszones/$h/records/commit"
  104. }
  105. _cn_get_root() {
  106. domain=$1
  107. i=2
  108. p=1
  109. while true; do
  110. h=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
  111. _debug h "$h"
  112. _debug _H1 "${_H1}"
  113. if [ -z "$h" ]; then
  114. #not valid
  115. return 1
  116. fi
  117. _cn_zonelist="$(_get ${CN_API}/dnszones/)"
  118. _debug _cn_zonelist "${_cn_zonelist}"
  119. if [ "$?" != "0" ]; then
  120. _err "something went wrong while getting the zone list"
  121. return 1
  122. fi
  123. if _contains "$_cn_zonelist" "\"name\":\"$h\"" >/dev/null; then
  124. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-"$p")
  125. _domain=$h
  126. return 0
  127. else
  128. _debug "Zonelist does not contain domain - iterating "
  129. fi
  130. p=$i
  131. i=$(_math "$i" + 1)
  132. done
  133. _err "Zonelist does not contain domain - exiting"
  134. return 1
  135. }