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.

168 lines
4.5 KiB

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