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.

276 lines
7.5 KiB

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