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.

231 lines
6.0 KiB

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