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.

141 lines
3.4 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 $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. _err "You don't specify luadns api key and email yet."
  16. _err "Please create you key and try again."
  17. return 1
  18. fi
  19. #save the api key and email to the account conf file.
  20. _saveaccountconf LUA_Key "$LUA_Key"
  21. _saveaccountconf LUA_Email "$LUA_Email"
  22. _debug "First detect the root zone"
  23. if ! _get_root $fulldomain; then
  24. _err "invalid domain"
  25. return 1
  26. fi
  27. _debug _domain_id "$_domain_id"
  28. _debug _sub_domain "$_sub_domain"
  29. _debug _domain "$_domain"
  30. _debug "Getting txt records"
  31. _LUA_rest GET "zones/${_domain_id}/records"
  32. if ! printf "$response" | grep \"id\": >/dev/null; then
  33. _err "Error"
  34. return 1
  35. fi
  36. count=$(printf "%s\n" "$response" | _egrep_o \"name\":\"$fulldomain\" | wc -l)
  37. _debug count "$count"
  38. if [ "$count" = "0" ]; then
  39. _info "Adding record"
  40. if _LUA_rest POST "zones/$_domain_id/records" "{\"type\":\"TXT\",\"name\":\"$fulldomain.\",\"content\":\"$txtvalue\",\"ttl\":120}"; then
  41. if printf -- "%s" "$response" | grep $fulldomain >/dev/null; then
  42. _info "Added"
  43. #todo: check if the record takes effect
  44. return 0
  45. else
  46. _err "Add txt record error."
  47. return 1
  48. fi
  49. fi
  50. _err "Add txt record error."
  51. else
  52. _info "Updating record"
  53. record_id=$(printf "%s\n" "$response" | _egrep_o \"id\":[^,]*,\"name\":\"$fulldomain.\",\"type\":\"TXT\" | cut -d: -f2 | cut -d, -f1)
  54. _debug "record_id" $record_id
  55. _LUA_rest PUT "zones/$_domain_id/records/$record_id" "{\"id\":\"$record_id\",\"type\":\"TXT\",\"name\":\"$fulldomain.\",\"content\":\"$txtvalue\",\"zone_id\":\"$_domain_id\",\"ttl\":120}"
  56. if [ "$?" = "0" ]; then
  57. _info "Updated!"
  58. #todo: check if the record takes effect
  59. return 0
  60. fi
  61. _err "Update error"
  62. return 1
  63. fi
  64. }
  65. #fulldomain
  66. dns_lua_rm() {
  67. fulldomain=$1
  68. }
  69. #################### Private functions bellow ##################################
  70. #_acme-challenge.www.domain.com
  71. #returns
  72. # _sub_domain=_acme-challenge.www
  73. # _domain=domain.com
  74. # _domain_id=sdjkglgdfewsdfg
  75. _get_root() {
  76. domain=$1
  77. i=2
  78. p=1
  79. if ! _LUA_rest GET "zones"; then
  80. return 1
  81. fi
  82. while [ '1' ]; do
  83. h=$(printf $domain | cut -d . -f $i-100)
  84. if [ -z "$h" ]; then
  85. #not valid
  86. return 1
  87. fi
  88. if printf $response | grep \"name\":\"$h\" >/dev/null; then
  89. _domain_id=$(printf "%s\n" "$response" | _egrep_o \"id\":[^,]*,\"name\":\"$h\" | cut -d : -f 2 | cut -d , -f 1)
  90. if [ "$_domain_id" ]; then
  91. _sub_domain=$(printf $domain | cut -d . -f 1-$p)
  92. _domain=$h
  93. return 0
  94. fi
  95. return 1
  96. fi
  97. p=$i
  98. i=$(expr $i + 1)
  99. done
  100. return 1
  101. }
  102. _LUA_rest() {
  103. m=$1
  104. ep="$2"
  105. data="$3"
  106. _debug $ep
  107. _H1="Accept: application/json"
  108. _H2="Authorization: Basic $LUA_auth"
  109. if [ "$data" ]; then
  110. _debug data "$data"
  111. response="$(_post "$data" "$LUA_Api/$ep" "" $m)"
  112. else
  113. response="$(_get "$LUA_Api/$ep")"
  114. fi
  115. if [ "$?" != "0" ]; then
  116. _err "error $ep"
  117. return 1
  118. fi
  119. _debug2 response "$response"
  120. return 0
  121. }