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.

174 lines
4.5 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  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. if [ -z "$LUA_Key" ] || [ -z "$LUA_Email" ]; then
  15. LUA_Key=""
  16. LUA_Email=""
  17. _err "You don't specify luadns api key and email yet."
  18. _err "Please create you key and try again."
  19. return 1
  20. fi
  21. #save the api key and email to the account conf file.
  22. _saveaccountconf LUA_Key "$LUA_Key"
  23. _saveaccountconf LUA_Email "$LUA_Email"
  24. _debug "First detect the root zone"
  25. if ! _get_root "$fulldomain"; then
  26. _err "invalid domain"
  27. return 1
  28. fi
  29. _debug _domain_id "$_domain_id"
  30. _debug _sub_domain "$_sub_domain"
  31. _debug _domain "$_domain"
  32. _debug "Getting txt records"
  33. _LUA_rest GET "zones/${_domain_id}/records"
  34. if ! _contains "$response" "\"id\":"; then
  35. _err "Error"
  36. return 1
  37. fi
  38. count=$(printf "%s\n" "$response" | _egrep_o "\"name\":\"$fulldomain.\",\"type\":\"TXT\"" | wc -l | tr -d " ")
  39. _debug count "$count"
  40. if [ "$count" = "0" ]; then
  41. _info "Adding record"
  42. if _LUA_rest POST "zones/$_domain_id/records" "{\"type\":\"TXT\",\"name\":\"$fulldomain.\",\"content\":\"$txtvalue\",\"ttl\":120}"; then
  43. if _contains "$response" "$fulldomain"; then
  44. _info "Added"
  45. #todo: check if the record takes effect
  46. return 0
  47. else
  48. _err "Add txt record error."
  49. return 1
  50. fi
  51. fi
  52. _err "Add txt record error."
  53. else
  54. _info "Updating record"
  55. record_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":[^,]*,\"name\":\"$fulldomain.\",\"type\":\"TXT\"" | _head_n 1 | cut -d: -f2 | cut -d, -f1)
  56. _debug "record_id" "$record_id"
  57. _LUA_rest PUT "zones/$_domain_id/records/$record_id" "{\"id\":$record_id,\"type\":\"TXT\",\"name\":\"$fulldomain.\",\"content\":\"$txtvalue\",\"zone_id\":$_domain_id,\"ttl\":120}"
  58. if [ "$?" = "0" ] && _contains "$response" "updated_at"; then
  59. _info "Updated!"
  60. #todo: check if the record takes effect
  61. return 0
  62. fi
  63. _err "Update error"
  64. return 1
  65. fi
  66. }
  67. #fulldomain
  68. dns_lua_rm() {
  69. fulldomain=$1
  70. txtvalue=$2
  71. _debug "First detect the root zone"
  72. if ! _get_root "$fulldomain"; then
  73. _err "invalid domain"
  74. return 1
  75. fi
  76. _debug _domain_id "$_domain_id"
  77. _debug _sub_domain "$_sub_domain"
  78. _debug _domain "$_domain"
  79. _debug "Getting txt records"
  80. _LUA_rest GET "zones/${_domain_id}/records"
  81. count=$(printf "%s\n" "$response" | _egrep_o "\"name\":\"$fulldomain.\",\"type\":\"TXT\"" | wc -l | tr -d " ")
  82. _debug count "$count"
  83. if [ "$count" = "0" ]; then
  84. _info "Don't need to remove."
  85. else
  86. record_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":[^,]*,\"name\":\"$fulldomain.\",\"type\":\"TXT\"" | _head_n 1 | cut -d: -f2 | cut -d, -f1)
  87. _debug "record_id" "$record_id"
  88. if [ -z "$record_id" ]; then
  89. _err "Can not get record id to remove."
  90. return 1
  91. fi
  92. if ! _LUA_rest DELETE "/zones/$_domain_id/records/$record_id"; then
  93. _err "Delete record error."
  94. return 1
  95. fi
  96. _contains "$response" "$record_id"
  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=2
  108. p=1
  109. if ! _LUA_rest GET "zones"; then
  110. return 1
  111. fi
  112. while true; do
  113. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  114. _debug h "$h"
  115. if [ -z "$h" ]; then
  116. #not valid
  117. return 1
  118. fi
  119. if _contains "$response" "\"name\":\"$h\""; then
  120. _domain_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":[^,]*,\"name\":\"$h\"" | cut -d : -f 2 | cut -d , -f 1)
  121. _debug _domain_id "$_domain_id"
  122. if [ "$_domain_id" ]; then
  123. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  124. _domain="$h"
  125. return 0
  126. fi
  127. return 1
  128. fi
  129. p=$i
  130. i=$(_math "$i" + 1)
  131. done
  132. return 1
  133. }
  134. _LUA_rest() {
  135. m=$1
  136. ep="$2"
  137. data="$3"
  138. _debug "$ep"
  139. export _H1="Accept: application/json"
  140. export _H2="Authorization: Basic $LUA_auth"
  141. if [ "$m" != "GET" ]; then
  142. _debug data "$data"
  143. response="$(_post "$data" "$LUA_Api/$ep" "" "$m")"
  144. else
  145. response="$(_get "$LUA_Api/$ep")"
  146. fi
  147. if [ "$?" != "0" ]; then
  148. _err "error $ep"
  149. return 1
  150. fi
  151. _debug2 response "$response"
  152. return 0
  153. }