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.

227 lines
5.9 KiB

6 years ago
4 years ago
4 years ago
4 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
4 years ago
  1. #!/usr/bin/env sh
  2. # one.com ui wrapper for acme.sh
  3. #
  4. # export ONECOM_User="username"
  5. # export ONECOM_Password="password"
  6. dns_one_add() {
  7. fulldomain=$1
  8. txtvalue=$2
  9. if ! _dns_one_login; then
  10. _err "login failed"
  11. return 1
  12. fi
  13. _debug "detect the root domain"
  14. if ! _get_root "$fulldomain"; then
  15. _err "root domain not found"
  16. return 1
  17. fi
  18. subdomain="${_sub_domain}"
  19. maindomain=${_domain}
  20. _debug subdomain "$subdomain"
  21. _debug maindomain "$maindomain"
  22. #Check if the TXT exists
  23. _dns_one_getrecord "TXT" "$subdomain" "$txtvalue"
  24. if [ -n "$id" ]; then
  25. _info "$(__green "Txt record with the same value found. Skip adding.")"
  26. return 0
  27. fi
  28. _dns_one_addrecord "TXT" "$subdomain" "$txtvalue"
  29. if [ -z "$id" ]; then
  30. _err "Add TXT record error."
  31. return 1
  32. else
  33. _info "$(__green "Added, OK ($id)")"
  34. return 0
  35. fi
  36. }
  37. dns_one_rm() {
  38. fulldomain=$1
  39. txtvalue=$2
  40. if ! _dns_one_login; then
  41. _err "login failed"
  42. return 1
  43. fi
  44. _debug "detect the root domain"
  45. if ! _get_root "$fulldomain"; then
  46. _err "root domain not found"
  47. return 1
  48. fi
  49. subdomain="${_sub_domain}"
  50. maindomain=${_domain}
  51. _debug subdomain "$subdomain"
  52. _debug maindomain "$maindomain"
  53. #Check if the TXT exists
  54. _dns_one_getrecord "TXT" "$subdomain" "$txtvalue"
  55. if [ -z "$id" ]; then
  56. _err "Txt record not found."
  57. return 1
  58. fi
  59. # delete entry
  60. if _dns_one_delrecord "$id"; then
  61. _info "$(__green Removed, OK)"
  62. return 0
  63. else
  64. _err "Removing txt record error."
  65. return 1
  66. fi
  67. }
  68. #_acme-challenge.www.domain.com
  69. #returns
  70. # _sub_domain=_acme-challenge.www
  71. # _domain=domain.com
  72. _get_root() {
  73. domain="$1"
  74. i=1
  75. p=1
  76. while true; do
  77. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  78. if [ -z "$h" ]; then
  79. #not valid
  80. return 1
  81. fi
  82. response="$(_get "https://www.one.com/admin/api/domains/$h/dns/custom_records")"
  83. if ! _contains "$response" "CRMRST_000302"; then
  84. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  85. _domain="$h"
  86. return 0
  87. fi
  88. p=$i
  89. i=$(_math "$i" + 1)
  90. done
  91. _err "Unable to parse this domain"
  92. return 1
  93. }
  94. _dns_one_login() {
  95. # get credentials
  96. ONECOM_User="${ONECOM_User:-$(_readaccountconf_mutable ONECOM_User)}"
  97. ONECOM_Password="${ONECOM_Password:-$(_readaccountconf_mutable ONECOM_Password)}"
  98. if [ -z "$ONECOM_User" ] || [ -z "$ONECOM_Password" ]; then
  99. ONECOM_User=""
  100. ONECOM_Password=""
  101. _err "You didn't specify a one.com username and password yet."
  102. _err "Please create the key and try again."
  103. return 1
  104. fi
  105. #save the api key and email to the account conf file.
  106. _saveaccountconf_mutable ONECOM_User "$ONECOM_User"
  107. _saveaccountconf_mutable ONECOM_Password "$ONECOM_Password"
  108. # Login with user and password
  109. postdata="loginDomain=true"
  110. postdata="$postdata&displayUsername=$ONECOM_User"
  111. postdata="$postdata&username=$ONECOM_User"
  112. postdata="$postdata&targetDomain="
  113. postdata="$postdata&password1=$ONECOM_Password"
  114. postdata="$postdata&loginTarget="
  115. #_debug postdata "$postdata"
  116. response="$(_post "$postdata" "https://www.one.com/admin/login.do" "" "POST" "application/x-www-form-urlencoded")"
  117. #_debug response "$response"
  118. # Get SessionID
  119. JSESSIONID="$(grep "OneSIDCrmAdmin" "$HTTP_HEADER" | grep "^[Ss]et-[Cc]ookie:" | _head_n 1 | _egrep_o 'OneSIDCrmAdmin=[^;]*;' | tr -d ';')"
  120. _debug jsessionid "$JSESSIONID"
  121. if [ -z "$JSESSIONID" ]; then
  122. _err "error sessionid cookie not found"
  123. return 1
  124. fi
  125. export _H1="Cookie: ${JSESSIONID}"
  126. return 0
  127. }
  128. _dns_one_getrecord() {
  129. type="$1"
  130. name="$2"
  131. value="$3"
  132. if [ -z "$type" ]; then
  133. type="TXT"
  134. fi
  135. if [ -z "$name" ]; then
  136. _err "Record name is empty."
  137. return 1
  138. fi
  139. response="$(_get "https://www.one.com/admin/api/domains/$maindomain/dns/custom_records")"
  140. response="$(echo "$response" | _normalizeJson)"
  141. _debug response "$response"
  142. if [ -z "${value}" ]; then
  143. id=$(printf -- "%s" "$response" | sed -n "s/.*{\"type\":\"dns_custom_records\",\"id\":\"\([^\"]*\)\",\"attributes\":{\"prefix\":\"${name}\",\"type\":\"${type}\",\"content\":\"[^\"]*\",\"priority\":0,\"ttl\":600}.*/\1/p")
  144. response=$(printf -- "%s" "$response" | sed -n "s/.*{\"type\":\"dns_custom_records\",\"id\":\"[^\"]*\",\"attributes\":{\"prefix\":\"${name}\",\"type\":\"${type}\",\"content\":\"\([^\"]*\)\",\"priority\":0,\"ttl\":600}.*/\1/p")
  145. else
  146. id=$(printf -- "%s" "$response" | sed -n "s/.*{\"type\":\"dns_custom_records\",\"id\":\"\([^\"]*\)\",\"attributes\":{\"prefix\":\"${name}\",\"type\":\"${type}\",\"content\":\"${value}\",\"priority\":0,\"ttl\":600}.*/\1/p")
  147. fi
  148. if [ -z "$id" ]; then
  149. return 1
  150. fi
  151. return 0
  152. }
  153. _dns_one_addrecord() {
  154. type="$1"
  155. name="$2"
  156. value="$3"
  157. if [ -z "$type" ]; then
  158. type="TXT"
  159. fi
  160. if [ -z "$name" ]; then
  161. _err "Record name is empty."
  162. return 1
  163. fi
  164. postdata="{\"type\":\"dns_custom_records\",\"attributes\":{\"priority\":0,\"ttl\":600,\"type\":\"${type}\",\"prefix\":\"${name}\",\"content\":\"${value}\"}}"
  165. _debug postdata "$postdata"
  166. response="$(_post "$postdata" "https://www.one.com/admin/api/domains/$maindomain/dns/custom_records" "" "POST" "application/json")"
  167. response="$(echo "$response" | _normalizeJson)"
  168. _debug response "$response"
  169. id=$(echo "$response" | sed -n "s/{\"result\":{\"data\":{\"type\":\"dns_custom_records\",\"id\":\"\([^\"]*\)\",\"attributes\":{\"prefix\":\"$subdomain\",\"type\":\"TXT\",\"content\":\"$txtvalue\",\"priority\":0,\"ttl\":600}}},\"metadata\":null}/\1/p")
  170. if [ -z "$id" ]; then
  171. return 1
  172. else
  173. return 0
  174. fi
  175. }
  176. _dns_one_delrecord() {
  177. id="$1"
  178. if [ -z "$id" ]; then
  179. return 1
  180. fi
  181. response="$(_post "" "https://www.one.com/admin/api/domains/$maindomain/dns/custom_records/$id" "" "DELETE" "application/json")"
  182. response="$(echo "$response" | _normalizeJson)"
  183. _debug response "$response"
  184. if [ "$response" = '{"result":null,"metadata":null}' ]; then
  185. return 0
  186. else
  187. return 1
  188. fi
  189. }