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.

186 lines
4.5 KiB

7 years ago
7 years ago
  1. #!/usr/bin/env sh
  2. #
  3. #UNO_Key="sdfsdfsdfljlbjkljlkjsdfoiwje"
  4. #
  5. #UNO_User="UExxxxxx"
  6. Uno_Api="https://api.unoeuro.com/1"
  7. ######## Public functions #####################
  8. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  9. dns_unoeuro_add() {
  10. fulldomain=$1
  11. txtvalue=$2
  12. UNO_Key="${UNO_Key:-$(_readaccountconf_mutable UNO_Key)}"
  13. UNO_User="${UNO_User:-$(_readaccountconf_mutable UNO_User)}"
  14. if [ -z "$UNO_Key" ] || [ -z "$UNO_User" ]; then
  15. UNO_Key=""
  16. UNO_User=""
  17. _err "You haven't specified a UnoEuro api key and account yet."
  18. _err "Please create your key and try again."
  19. return 1
  20. fi
  21. if ! _contains "$UNO_User" "UE"; then
  22. _err "It seems that the UNO_User=$UNO_User is not a valid username."
  23. _err "Please check and retry."
  24. return 1
  25. fi
  26. #save the api key and email to the account conf file.
  27. _saveaccountconf_mutable UNO_Key "$UNO_Key"
  28. _saveaccountconf_mutable UNO_User "$UNO_User"
  29. _debug "First detect the root zone"
  30. if ! _get_root "$fulldomain"; then
  31. _err "invalid domain"
  32. return 1
  33. fi
  34. _debug _domain_id "$_domain_id"
  35. _debug _sub_domain "$_sub_domain"
  36. _debug _domain "$_domain"
  37. _debug "Getting txt records"
  38. _uno_rest GET "my/products/$h/dns/records"
  39. if ! _contains "$response" "\"status\": 200" >/dev/null; then
  40. _err "Error"
  41. return 1
  42. fi
  43. _info "Adding record"
  44. if _uno_rest POST "my/products/$h/dns/records" "{\"name\":\"$fulldomain\",\"type\":\"TXT\",\"data\":\"$txtvalue\",\"ttl\":120}"; then
  45. if _contains "$response" "\"status\": 200" >/dev/null; then
  46. _info "Added, OK"
  47. return 0
  48. else
  49. _err "Add txt record error."
  50. return 1
  51. fi
  52. fi
  53. _err "Add txt record error."
  54. }
  55. #fulldomain txtvalue
  56. dns_unoeuro_rm() {
  57. fulldomain=$1
  58. txtvalue=$2
  59. UNO_Key="${UNO_Key:-$(_readaccountconf_mutable UNO_Key)}"
  60. UNO_User="${UNO_User:-$(_readaccountconf_mutable UNO_User)}"
  61. if [ -z "$UNO_Key" ] || [ -z "$UNO_User" ]; then
  62. UNO_Key=""
  63. UNO_User=""
  64. _err "You haven't specified a UnoEuro api key and account yet."
  65. _err "Please create your key and try again."
  66. return 1
  67. fi
  68. if ! _contains "$UNO_User" "UE"; then
  69. _err "It seems that the UNO_User=$UNO_User is not a valid username."
  70. _err "Please check and retry."
  71. return 1
  72. fi
  73. _debug "First detect the root zone"
  74. if ! _get_root "$fulldomain"; then
  75. _err "invalid domain"
  76. return 1
  77. fi
  78. _debug _domain_id "$_domain_id"
  79. _debug _sub_domain "$_sub_domain"
  80. _debug _domain "$_domain"
  81. _debug "Getting txt records"
  82. _uno_rest GET "my/products/$h/dns/records"
  83. if ! _contains "$response" "\"status\": 200"; then
  84. _err "Error"
  85. return 1
  86. fi
  87. if ! _contains "$response" "$_sub_domain"; then
  88. _info "Don't need to remove."
  89. else
  90. for record_line_number in $(echo "$response" | grep -n "$_sub_domain" | cut -d : -f 1); do
  91. record_line_number=$(_math "$record_line_number" - 1)
  92. _debug "record_line_number" "$record_line_number"
  93. record_id=$(echo "$response" | _head_n "$record_line_number" | _tail_n 1 1 | _egrep_o "[0-9]{1,}")
  94. _debug "record_id" "$record_id"
  95. if [ -z "$record_id" ]; then
  96. _err "Can not get record id to remove."
  97. return 1
  98. fi
  99. if ! _uno_rest DELETE "my/products/$h/dns/records/$record_id"; then
  100. _err "Delete record error."
  101. return 1
  102. fi
  103. _contains "$response" "\"status\": 200"
  104. done
  105. fi
  106. }
  107. #################### Private functions below ##################################
  108. #_acme-challenge.www.domain.com
  109. #returns
  110. # _sub_domain=_acme-challenge.www
  111. # _domain=domain.com
  112. # _domain_id=sdjkglgdfewsdfg
  113. _get_root() {
  114. domain=$1
  115. i=2
  116. p=1
  117. while true; do
  118. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  119. _debug h "$h"
  120. if [ -z "$h" ]; then
  121. #not valid
  122. return 1
  123. fi
  124. if ! _uno_rest GET "my/products/$h/dns/records"; then
  125. return 1
  126. fi
  127. if _contains "$response" "\"status\": 200"; then
  128. _domain_id=$h
  129. if [ "$_domain_id" ]; then
  130. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  131. _domain=$h
  132. return 0
  133. fi
  134. return 1
  135. fi
  136. p=$i
  137. i=$(_math "$i" + 1)
  138. done
  139. return 1
  140. }
  141. _uno_rest() {
  142. m=$1
  143. ep="$2"
  144. data="$3"
  145. _debug "$ep"
  146. export _H1="Content-Type: application/json"
  147. if [ "$m" != "GET" ]; then
  148. _debug data "$data"
  149. response="$(_post "$data" "$Uno_Api/$UNO_User/$UNO_Key/$ep" "" "$m")"
  150. else
  151. response="$(_get "$Uno_Api/$UNO_User/$UNO_Key/$ep")"
  152. fi
  153. if [ "$?" != "0" ]; then
  154. _err "error $ep"
  155. return 1
  156. fi
  157. _debug2 response "$response"
  158. return 0
  159. }