dns_lua.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #!/usr/bin/env sh
  2. # bug reports to dev@1e.ca
  3. #
  4. #LUA_Key="sdfsdfsdfljlbjkljlkjsdfoiwje"
  5. #
  6. #LUA_Email="user@luadns.net"
  7. LUA_Api="https://api.luadns.com/v1"
  8. LUA_auth=$(printf "%s" "$LUA_Email:$LUA_Key" | _base64)
  9. ######## Public functions #####################
  10. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  11. dns_lua_add() {
  12. fulldomain=$1
  13. txtvalue=$2
  14. LUA_Key="${LUA_Key:-$(_readaccountconf_mutable LUA_Key)}"
  15. LUA_Email="${LUA_Email:-$(_readaccountconf_mutable LUA_Email)}"
  16. if [ -z "$LUA_Key" ] || [ -z "$LUA_Email" ]; then
  17. LUA_Key=""
  18. LUA_Email=""
  19. _err "You don't specify luadns api key and email yet."
  20. _err "Please create you key and try again."
  21. return 1
  22. fi
  23. #save the api key and email to the account conf file.
  24. _saveaccountconf_mutable LUA_Key "$LUA_Key"
  25. _saveaccountconf_mutable LUA_Email "$LUA_Email"
  26. _debug "First detect the root zone"
  27. if ! _get_root "$fulldomain"; then
  28. _err "invalid domain"
  29. return 1
  30. fi
  31. _debug _domain_id "$_domain_id"
  32. _debug _sub_domain "$_sub_domain"
  33. _debug _domain "$_domain"
  34. _info "Adding record"
  35. if _LUA_rest POST "zones/$_domain_id/records" "{\"type\":\"TXT\",\"name\":\"$fulldomain.\",\"content\":\"$txtvalue\",\"ttl\":120}"; then
  36. if _contains "$response" "$fulldomain"; then
  37. _info "Added"
  38. #todo: check if the record takes effect
  39. return 0
  40. else
  41. _err "Add txt record error."
  42. return 1
  43. fi
  44. fi
  45. }
  46. #fulldomain
  47. dns_lua_rm() {
  48. fulldomain=$1
  49. txtvalue=$2
  50. LUA_Key="${LUA_Key:-$(_readaccountconf_mutable LUA_Key)}"
  51. LUA_Email="${LUA_Email:-$(_readaccountconf_mutable LUA_Email)}"
  52. _debug "First detect the root zone"
  53. if ! _get_root "$fulldomain"; then
  54. _err "invalid domain"
  55. return 1
  56. fi
  57. _debug _domain_id "$_domain_id"
  58. _debug _sub_domain "$_sub_domain"
  59. _debug _domain "$_domain"
  60. _debug "Getting txt records"
  61. _LUA_rest GET "zones/${_domain_id}/records"
  62. count=$(printf "%s\n" "$response" | _egrep_o "\"name\":\"$fulldomain.\",\"type\":\"TXT\"" | wc -l | tr -d " ")
  63. _debug count "$count"
  64. if [ "$count" = "0" ]; then
  65. _info "Don't need to remove."
  66. else
  67. record_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":[^,]*,\"name\":\"$fulldomain.\",\"type\":\"TXT\"" | _head_n 1 | cut -d: -f2 | cut -d, -f1)
  68. _debug "record_id" "$record_id"
  69. if [ -z "$record_id" ]; then
  70. _err "Can not get record id to remove."
  71. return 1
  72. fi
  73. if ! _LUA_rest DELETE "/zones/$_domain_id/records/$record_id"; then
  74. _err "Delete record error."
  75. return 1
  76. fi
  77. _contains "$response" "$record_id"
  78. fi
  79. }
  80. #################### Private functions below ##################################
  81. #_acme-challenge.www.domain.com
  82. #returns
  83. # _sub_domain=_acme-challenge.www
  84. # _domain=domain.com
  85. # _domain_id=sdjkglgdfewsdfg
  86. _get_root() {
  87. domain=$1
  88. i=2
  89. p=1
  90. if ! _LUA_rest GET "zones"; then
  91. return 1
  92. fi
  93. while true; do
  94. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  95. _debug h "$h"
  96. if [ -z "$h" ]; then
  97. #not valid
  98. return 1
  99. fi
  100. if _contains "$response" "\"name\":\"$h\""; then
  101. _domain_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":[^,]*,\"name\":\"$h\"" | cut -d : -f 2 | cut -d , -f 1)
  102. _debug _domain_id "$_domain_id"
  103. if [ "$_domain_id" ]; then
  104. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  105. _domain="$h"
  106. return 0
  107. fi
  108. return 1
  109. fi
  110. p=$i
  111. i=$(_math "$i" + 1)
  112. done
  113. return 1
  114. }
  115. _LUA_rest() {
  116. m=$1
  117. ep="$2"
  118. data="$3"
  119. _debug "$ep"
  120. export _H1="Accept: application/json"
  121. export _H2="Authorization: Basic $LUA_auth"
  122. if [ "$m" != "GET" ]; then
  123. _debug data "$data"
  124. response="$(_post "$data" "$LUA_Api/$ep" "" "$m")"
  125. else
  126. response="$(_get "$LUA_Api/$ep")"
  127. fi
  128. if [ "$?" != "0" ]; then
  129. _err "error $ep"
  130. return 1
  131. fi
  132. _debug2 response "$response"
  133. return 0
  134. }