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.

179 lines
4.3 KiB

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