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.

173 lines
4.7 KiB

3 months ago
3 months ago
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_njalla_info='Njalla
  4. Site: Njal.la
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi#dns_njalla
  6. Options:
  7. NJALLA_Token API Token
  8. Issues: github.com/acmesh-official/acme.sh/issues/2913
  9. '
  10. NJALLA_Api="https://njal.la/api/1/"
  11. ######## Public functions #####################
  12. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  13. dns_njalla_add() {
  14. fulldomain=$1
  15. txtvalue=$2
  16. NJALLA_Token="${NJALLA_Token:-$(_readaccountconf_mutable NJALLA_Token)}"
  17. if [ "$NJALLA_Token" ]; then
  18. _saveaccountconf_mutable NJALLA_Token "$NJALLA_Token"
  19. else
  20. NJALLA_Token=""
  21. _err "You didn't specify a Njalla api token yet."
  22. return 1
  23. fi
  24. _debug "First detect the root zone"
  25. if ! _get_root "$fulldomain"; then
  26. _err "invalid domain"
  27. return 1
  28. fi
  29. _debug _sub_domain "$_sub_domain"
  30. _debug _domain "$_domain"
  31. # For wildcard cert, the main root domain and the wildcard domain have the same txt subdomain name, so
  32. # we can not use updating anymore.
  33. # count=$(printf "%s\n" "$response" | _egrep_o "\"count\":[^,]*" | cut -d : -f 2)
  34. # _debug count "$count"
  35. # if [ "$count" = "0" ]; then
  36. _info "Adding record"
  37. if _njalla_rest "{\"method\":\"add-record\",\"params\":{\"domain\":\"$_domain\",\"type\":\"TXT\",\"name\":\"$_sub_domain\",\"content\":\"$txtvalue\",\"ttl\":120}}"; then
  38. if _contains "$response" "$txtvalue"; then
  39. _info "Added, OK"
  40. return 0
  41. else
  42. _err "Add txt record error."
  43. return 1
  44. fi
  45. fi
  46. _err "Add txt record error."
  47. return 1
  48. }
  49. #fulldomain txtvalue
  50. dns_njalla_rm() {
  51. fulldomain=$1
  52. txtvalue=$2
  53. NJALLA_Token="${NJALLA_Token:-$(_readaccountconf_mutable NJALLA_Token)}"
  54. if [ "$NJALLA_Token" ]; then
  55. _saveaccountconf_mutable NJALLA_Token "$NJALLA_Token"
  56. else
  57. NJALLA_Token=""
  58. _err "You didn't specify a Njalla api token yet."
  59. return 1
  60. fi
  61. _debug "First detect the root zone"
  62. if ! _get_root "$fulldomain"; then
  63. _err "invalid domain"
  64. return 1
  65. fi
  66. _debug _sub_domain "$_sub_domain"
  67. _debug _domain "$_domain"
  68. _debug "Getting records for domain"
  69. if ! _njalla_rest "{\"method\":\"list-records\",\"params\":{\"domain\":\"${_domain}\"}}"; then
  70. return 1
  71. fi
  72. if ! echo "$response" | tr -d " " | grep "\"id\":" >/dev/null; then
  73. _err "Error: $response"
  74. return 1
  75. fi
  76. records=$(echo "$response" | _egrep_o "\"records\":\s?\[(.*)\]\}" | _egrep_o "\[.*\]" | _egrep_o "\{[^\{\}]*\"id\":[^\{\}]*\}")
  77. count=$(echo "$records" | wc -l)
  78. _debug count "$count"
  79. if [ "$count" = "0" ]; then
  80. _info "Don't need to remove."
  81. else
  82. echo "$records" | while read -r record; do
  83. record_name=$(echo "$record" | _egrep_o "\"name\":\s?\"[^\"]*\"" | cut -d : -f 2 | tr -d " " | tr -d \")
  84. record_content=$(echo "$record" | _egrep_o "\"content\":\s?\"[^\"]*\"" | cut -d : -f 2 | tr -d " " | tr -d \")
  85. record_id=$(echo "$record" | _egrep_o "\"id\":\s?[0-9]+" | cut -d : -f 2 | tr -d " " | tr -d \")
  86. if [ "$_sub_domain" = "$record_name" ]; then
  87. if [ "$txtvalue" = "$record_content" ]; then
  88. _debug "record_id" "$record_id"
  89. if ! _njalla_rest "{\"method\":\"remove-record\",\"params\":{\"domain\":\"${_domain}\",\"id\":${record_id}}}"; then
  90. _err "Delete record error."
  91. return 1
  92. fi
  93. echo "$response" | tr -d " " | grep "\"result\"" >/dev/null
  94. fi
  95. fi
  96. done
  97. fi
  98. }
  99. #################### Private functions below ##################################
  100. #_acme-challenge.www.domain.com
  101. #returns
  102. # _sub_domain=_acme-challenge.www
  103. # _domain=domain.com
  104. # _domain_id=sdjkglgdfewsdfg
  105. _get_root() {
  106. domain=$1
  107. i=1
  108. p=1
  109. while true; do
  110. h=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
  111. _debug h "$h"
  112. if [ -z "$h" ]; then
  113. #not valid
  114. return 1
  115. fi
  116. if ! _njalla_rest "{\"method\":\"get-domain\",\"params\":{\"domain\":\"${h}\"}}"; then
  117. return 1
  118. fi
  119. if _contains "$response" "\"$h\""; then
  120. _domain_returned=$(echo "$response" | _egrep_o "\{\"name\": *\"[^\"]*\"" | _head_n 1 | cut -d : -f 2 | tr -d \" | tr -d " ")
  121. if [ "$_domain_returned" ]; then
  122. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-"$p")
  123. _domain=$h
  124. return 0
  125. fi
  126. return 1
  127. fi
  128. p=$i
  129. i=$(_math "$i" + 1)
  130. done
  131. return 1
  132. }
  133. _njalla_rest() {
  134. data="$1"
  135. token_trimmed=$(echo "$NJALLA_Token" | tr -d '"')
  136. export _H1="Content-Type: application/json"
  137. export _H2="Accept: application/json"
  138. export _H3="Authorization: Njalla $token_trimmed"
  139. _debug data "$data"
  140. response="$(_post "$data" "$NJALLA_Api" "" "POST")"
  141. if [ "$?" != "0" ]; then
  142. _err "error $data"
  143. return 1
  144. fi
  145. _debug2 response "$response"
  146. return 0
  147. }