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.

143 lines
3.5 KiB

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\"" | wc -l)
  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 printf -- "%s" "$response" | grep "$fulldomain" >/dev/null; 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\"" | 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. if [ -z "$h" ]; then
  87. #not valid
  88. return 1
  89. fi
  90. if _contains "$response" "\"name\":\"$h\""; then
  91. _domain_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":[^,]*,\"name\":\"$h\"" | cut -d : -f 2 | cut -d , -f 1)
  92. if [ "$_domain_id" ]; then
  93. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  94. _domain="$h"
  95. return 0
  96. fi
  97. return 1
  98. fi
  99. p=$i
  100. i=$(_math "$i" + 1)
  101. done
  102. return 1
  103. }
  104. _LUA_rest() {
  105. m=$1
  106. ep="$2"
  107. data="$3"
  108. _debug "$ep"
  109. export _H1="Accept: application/json"
  110. export _H2="Authorization: Basic $LUA_auth"
  111. if [ "$data" ]; then
  112. _debug data "$data"
  113. response="$(_post "$data" "$LUA_Api/$ep" "" "$m")"
  114. else
  115. response="$(_get "$LUA_Api/$ep")"
  116. fi
  117. if [ "$?" != "0" ]; then
  118. _err "error $ep"
  119. return 1
  120. fi
  121. _debug2 response "$response"
  122. return 0
  123. }