dns_cn.sh 3.7 KB

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