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.

279 lines
7.6 KiB

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