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.

145 lines
3.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
  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" ]; 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. }
  71. #################### Private functions below ##################################
  72. #_acme-challenge.www.domain.com
  73. #returns
  74. # _sub_domain=_acme-challenge.www
  75. # _domain=domain.com
  76. # _domain_id=sdjkglgdfewsdfg
  77. _get_root() {
  78. domain=$1
  79. i=2
  80. p=1
  81. if ! _LUA_rest GET "zones"; then
  82. return 1
  83. fi
  84. while true; do
  85. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  86. _debug h "$h"
  87. if [ -z "$h" ]; then
  88. #not valid
  89. return 1
  90. fi
  91. if _contains "$response" "\"name\":\"$h\""; then
  92. _domain_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":[^,]*,\"name\":\"$h\"" | cut -d : -f 2 | cut -d , -f 1)
  93. _debug _domain_id "$_domain_id"
  94. if [ "$_domain_id" ]; then
  95. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  96. _domain="$h"
  97. return 0
  98. fi
  99. return 1
  100. fi
  101. p=$i
  102. i=$(_math "$i" + 1)
  103. done
  104. return 1
  105. }
  106. _LUA_rest() {
  107. m=$1
  108. ep="$2"
  109. data="$3"
  110. _debug "$ep"
  111. export _H1="Accept: application/json"
  112. export _H2="Authorization: Basic $LUA_auth"
  113. if [ "$data" ]; then
  114. _debug data "$data"
  115. response="$(_post "$data" "$LUA_Api/$ep" "" "$m")"
  116. else
  117. response="$(_get "$LUA_Api/$ep")"
  118. fi
  119. if [ "$?" != "0" ]; then
  120. _err "error $ep"
  121. return 1
  122. fi
  123. _debug2 response "$response"
  124. return 0
  125. }