dns_one.sh 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #!/usr/bin/env sh
  2. # -*- mode: sh; tab-width: 2; indent-tabs-mode: s; coding: utf-8 -*-
  3. # one.com ui wrapper for acme.sh
  4. # Author: github: @diseq
  5. # Created: 2019-02-17
  6. # Fixed by: @der-berni
  7. # Modified: 2019-05-31
  8. #
  9. # export ONECOM_User="username"
  10. # export ONECOM_Password="password"
  11. #
  12. # Usage:
  13. # acme.sh --issue --dns dns_one -d example.com
  14. #
  15. # only single domain supported atm
  16. dns_one_add() {
  17. fulldomain=$1
  18. txtvalue=$2
  19. if ! _dns_one_login; then
  20. _err "login failed"
  21. return 1
  22. fi
  23. _debug "detect the root domain"
  24. if ! _get_root "$fulldomain"; then
  25. _err "root domain not found"
  26. return 1
  27. fi
  28. mysubdomain=$_sub_domain
  29. mydomain=$_domain
  30. _debug mysubdomain "$mysubdomain"
  31. _debug mydomain "$mydomain"
  32. # get entries
  33. response="$(_get "https://www.one.com/admin/api/domains/$mydomain/dns/custom_records")"
  34. _debug response "$response"
  35. # Update the IP address for domain entry
  36. postdata="{\"type\":\"dns_custom_records\",\"attributes\":{\"priority\":0,\"ttl\":600,\"type\":\"TXT\",\"prefix\":\"$mysubdomain\",\"content\":\"$txtvalue\"}}"
  37. _debug postdata "$postdata"
  38. response="$(_post "$postdata" "https://www.one.com/admin/api/domains/$mydomain/dns/custom_records" "" "POST" "application/json")"
  39. response="$(echo "$response" | _normalizeJson)"
  40. _debug response "$response"
  41. id=$(echo "$response" | sed -n "s/{\"result\":{\"data\":{\"type\":\"dns_custom_records\",\"id\":\"\([^\"]*\)\",\"attributes\":{\"prefix\":\"$mysubdomain\",\"type\":\"TXT\",\"content\":\"$txtvalue\",\"priority\":0,\"ttl\":600}}},\"metadata\":null}/\1/p")
  42. if [ -z "$id" ]; then
  43. _err "Add txt record error."
  44. return 1
  45. else
  46. _info "Added, OK ($id)"
  47. return 0
  48. fi
  49. }
  50. dns_one_rm() {
  51. fulldomain=$1
  52. txtvalue=$2
  53. if ! _dns_one_login; then
  54. _err "login failed"
  55. return 1
  56. fi
  57. _debug "detect the root domain"
  58. if ! _get_root "$fulldomain"; then
  59. _err "root domain not found"
  60. return 1
  61. fi
  62. mysubdomain=$_sub_domain
  63. mydomain=$_domain
  64. _debug mysubdomain "$mysubdomain"
  65. _debug mydomain "$mydomain"
  66. # get entries
  67. response="$(_get "https://www.one.com/admin/api/domains/$mydomain/dns/custom_records")"
  68. response="$(echo "$response" | _normalizeJson)"
  69. _debug response "$response"
  70. id=$(printf -- "%s" "$response" | sed -n "s/.*{\"type\":\"dns_custom_records\",\"id\":\"\([^\"]*\)\",\"attributes\":{\"prefix\":\"$mysubdomain\",\"type\":\"TXT\",\"content\":\"$txtvalue\",\"priority\":0,\"ttl\":600}.*/\1/p")
  71. if [ -z "$id" ]; then
  72. _err "Txt record not found."
  73. return 1
  74. fi
  75. # delete entry
  76. response="$(_post "$postdata" "https://www.one.com/admin/api/domains/$mydomain/dns/custom_records/$id" "" "DELETE" "application/json")"
  77. response="$(echo "$response" | _normalizeJson)"
  78. _debug response "$response"
  79. if [ "$response" = '{"result":null,"metadata":null}' ]; then
  80. _info "Removed, OK"
  81. return 0
  82. else
  83. _err "Removing txt record error."
  84. return 1
  85. fi
  86. }
  87. #_acme-challenge.www.domain.com
  88. #returns
  89. # _sub_domain=_acme-challenge.www
  90. # _domain=domain.com
  91. _get_root() {
  92. domain="$1"
  93. i=2
  94. p=1
  95. while true; do
  96. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  97. if [ -z "$h" ]; then
  98. #not valid
  99. return 1
  100. fi
  101. response="$(_get "https://www.one.com/admin/api/domains/$h/dns/custom_records")"
  102. if ! _contains "$response" "CRMRST_000302"; then
  103. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  104. _domain="$h"
  105. return 0
  106. fi
  107. p=$i
  108. i=$(_math "$i" + 1)
  109. done
  110. _err "Unable to parse this domain"
  111. return 1
  112. }
  113. _dns_one_login() {
  114. # get credentials
  115. ONECOM_User="${ONECOM_User:-$(_readaccountconf_mutable ONECOM_User)}"
  116. ONECOM_Password="${ONECOM_Password:-$(_readaccountconf_mutable ONECOM_Password)}"
  117. if [ -z "$ONECOM_User" ] || [ -z "$ONECOM_Password" ]; then
  118. ONECOM_User=""
  119. ONECOM_Password=""
  120. _err "You didn't specify a one.com username and password yet."
  121. _err "Please create the key and try again."
  122. return 1
  123. fi
  124. #save the api key and email to the account conf file.
  125. _saveaccountconf_mutable ONECOM_User "$ONECOM_User"
  126. _saveaccountconf_mutable ONECOM_Password "$ONECOM_Password"
  127. # Login with user and password
  128. postdata="loginDomain=true"
  129. postdata="$postdata&displayUsername=$ONECOM_User"
  130. postdata="$postdata&username=$ONECOM_User"
  131. postdata="$postdata&targetDomain="
  132. postdata="$postdata&password1=$ONECOM_Password"
  133. postdata="$postdata&loginTarget="
  134. #_debug postdata "$postdata"
  135. response="$(_post "$postdata" "https://www.one.com/admin/login.do" "" "POST" "application/x-www-form-urlencoded")"
  136. #_debug response "$response"
  137. # Get SessionID
  138. JSESSIONID="$(grep "OneSIDCrmAdmin" "$HTTP_HEADER" | grep "^[Ss]et-[Cc]ookie:" | _head_n 1 | _egrep_o 'OneSIDCrmAdmin=[^;]*;' | tr -d ';')"
  139. _debug jsessionid "$JSESSIONID"
  140. if [ -z "$JSESSIONID" ]; then
  141. _err "error sessionid cookie not found"
  142. return 1
  143. fi
  144. export _H1="Cookie: ${JSESSIONID}"
  145. return 0
  146. }