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.

144 lines
3.4 KiB

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