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.

210 lines
6.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
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
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: 2019-05-20
  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. #rev command not found on OpenWrt
  18. #mysubdomain=$(printf -- "%s" "$1" | rev | cut -d"." -f3- | rev)
  19. #mydomain=$(printf -- "%s" "$1" | rev | cut -d"." -f1-2 | rev)
  20. fulldomain=$1
  21. txtvalue=$2
  22. _debug "First detect the root zone"
  23. if ! _get_root "$fulldomain"; then
  24. _err "invalid domain"
  25. return 1
  26. fi
  27. mysubdomain=$_sub_domain
  28. mydomain=$_domain
  29. _debug mysubdomain "$mysubdomain"
  30. _debug mydomain "$mydomain"
  31. # get credentials
  32. ONECOM_User="${ONECOM_User:-$(_readaccountconf_mutable ONECOM_User)}"
  33. ONECOM_Password="${ONECOM_Password:-$(_readaccountconf_mutable ONECOM_Password)}"
  34. if [ -z "$ONECOM_User" ] || [ -z "$ONECOM_Password" ]; then
  35. ONECOM_User=""
  36. ONECOM_Password=""
  37. _err "You didn't specify a one.com username and password yet."
  38. _err "Please create the key and try again."
  39. return 1
  40. fi
  41. #save the api key and email to the account conf file.
  42. _saveaccountconf_mutable ONECOM_User "$ONECOM_User"
  43. _saveaccountconf_mutable ONECOM_Password "$ONECOM_Password"
  44. # Login with user and password
  45. postdata="loginDomain=true"
  46. postdata="$postdata&displayUsername=$ONECOM_User"
  47. postdata="$postdata&username=$ONECOM_User"
  48. postdata="$postdata&targetDomain=$mydomain"
  49. postdata="$postdata&password1=$ONECOM_Password"
  50. postdata="$postdata&loginTarget="
  51. #_debug postdata "$postdata"
  52. #CURL does not work
  53. local tmp_USE_WGET=$ACME_USE_WGET
  54. ACME_USE_WGET=1
  55. response="$(_post "$postdata" "https://www.one.com/admin/login.do" "" "POST" "application/x-www-form-urlencoded")"
  56. #_debug response "$response"
  57. JSESSIONID="$(grep "OneSIDCrmAdmin" "$HTTP_HEADER" | grep "^[Ss]et-[Cc]ookie:" | _tail_n 1 | _egrep_o 'OneSIDCrmAdmin=[^;]*;' | tr -d ';')"
  58. _debug jsessionid "$JSESSIONID"
  59. export _H1="Cookie: ${JSESSIONID}"
  60. # get entries
  61. response="$(_get "https://www.one.com/admin/api/domains/$mydomain/dns/custom_records")"
  62. _debug response "$response"
  63. #CSRF_G_TOKEN="$(grep "CSRF_G_TOKEN=" "$HTTP_HEADER" | grep "^Set-Cookie:" | _tail_n 1 | _egrep_o 'CSRF_G_TOKEN=[^;]*;' | tr -d ';')"
  64. #export _H2="Cookie: ${CSRF_G_TOKEN}"
  65. # Update the IP address for domain entry
  66. postdata="{\"type\":\"dns_custom_records\",\"attributes\":{\"priority\":0,\"ttl\":600,\"type\":\"TXT\",\"prefix\":\"$mysubdomain\",\"content\":\"$txtvalue\"}}"
  67. _debug postdata "$postdata"
  68. response="$(_post "$postdata" "https://www.one.com/admin/api/domains/$mydomain/dns/custom_records" "" "POST" "application/json")"
  69. response="$(echo "$response" | _normalizeJson)"
  70. _debug response "$response"
  71. 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")
  72. ACME_USE_WGET=$tmp_USE_WGET
  73. if [ -z "$id" ]; then
  74. _err "Add txt record error."
  75. return 1
  76. else
  77. _info "Added, OK ($id)"
  78. return 0
  79. fi
  80. }
  81. dns_one_rm() {
  82. #rev command not found on OpenWrt
  83. #mysubdomain=$(printf -- "%s" "$1" | rev | cut -d"." -f3- | rev)
  84. #mydomain=$(printf -- "%s" "$1" | rev | cut -d"." -f1-2 | rev)
  85. fulldomain=$1
  86. txtvalue=$2
  87. _debug "First detect the root zone"
  88. if ! _get_root "$fulldomain"; then
  89. _err "invalid domain"
  90. return 1
  91. fi
  92. mysubdomain=$_sub_domain
  93. mydomain=$_domain
  94. _debug mysubdomain "$mysubdomain"
  95. _debug mydomain "$mydomain"
  96. # get credentials
  97. ONECOM_User="${ONECOM_User:-$(_readaccountconf_mutable ONECOM_User)}"
  98. ONECOM_Password="${ONECOM_Password:-$(_readaccountconf_mutable ONECOM_Password)}"
  99. if [ -z "$ONECOM_User" ] || [ -z "$ONECOM_Password" ]; then
  100. ONECOM_User=""
  101. ONECOM_Password=""
  102. _err "You didn't specify a one.com username and password yet."
  103. _err "Please create the key and try again."
  104. return 1
  105. fi
  106. # Login with user and password
  107. postdata="loginDomain=true"
  108. postdata="$postdata&displayUsername=$ONECOM_User"
  109. postdata="$postdata&username=$ONECOM_User"
  110. postdata="$postdata&targetDomain=$mydomain"
  111. postdata="$postdata&password1=$ONECOM_Password"
  112. postdata="$postdata&loginTarget="
  113. #CURL does not work
  114. local tmp_USE_WGET=$ACME_USE_WGET
  115. ACME_USE_WGET=1
  116. response="$(_post "$postdata" "https://www.one.com/admin/login.do" "" "POST" "application/x-www-form-urlencoded")"
  117. #_debug response "$response"
  118. JSESSIONID="$(grep "OneSIDCrmAdmin" "$HTTP_HEADER" | grep "^[Ss]et-[Cc]ookie:" | _tail_n 1 | _egrep_o 'OneSIDCrmAdmin=[^;]*;' | tr -d ';')"
  119. _debug jsessionid "$JSESSIONID"
  120. export _H1="Cookie: ${JSESSIONID}"
  121. # get entries
  122. response="$(_get "https://www.one.com/admin/api/domains/$mydomain/dns/custom_records")"
  123. response="$(echo "$response" | _normalizeJson)"
  124. _debug response "$response"
  125. #CSRF_G_TOKEN="$(grep "CSRF_G_TOKEN=" "$HTTP_HEADER" | grep "^Set-Cookie:" | _tail_n 1 | _egrep_o 'CSRF_G_TOKEN=[^;]*;' | tr -d ';')"
  126. #export _H2="Cookie: ${CSRF_G_TOKEN}"
  127. 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")
  128. if [ -z "$id" ]; then
  129. _err "Txt record not found."
  130. ACME_USE_WGET=$tmp_USE_WGET
  131. return 1
  132. fi
  133. # delete entry
  134. response="$(_post "$postdata" "https://www.one.com/admin/api/domains/$mydomain/dns/custom_records/$id" "" "DELETE" "application/json")"
  135. response="$(echo "$response" | _normalizeJson)"
  136. _debug response "$response"
  137. ACME_USE_WGET=$tmp_USE_WGET
  138. if [ "$response" = '{"result":null,"metadata":null}' ]; then
  139. _info "Removed, OK"
  140. return 0
  141. else
  142. _err "Removing txt record error."
  143. return 1
  144. fi
  145. }
  146. #_acme-challenge.www.domain.com
  147. #returns
  148. # _sub_domain=_acme-challenge.www
  149. # _domain=domain.com
  150. _get_root() {
  151. domain=$1
  152. i=2
  153. p=1
  154. while true; do
  155. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  156. if [ -z "$h" ]; then
  157. #not valid
  158. return 1
  159. fi
  160. if [ "$(printf "%s" "$h" | tr '.' ' ' | wc -w)" = "2" ]; then
  161. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  162. _domain="$h"
  163. return 0
  164. fi
  165. p=$i
  166. i=$(_math "$i" + 1)
  167. done
  168. _err "Unable to parse this domain"
  169. return 1
  170. }