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.

320 lines
7.7 KiB

4 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
8 years ago
7 years ago
  1. #!/usr/bin/env sh
  2. #Application Key
  3. #OVH_AK="sdfsdfsdfljlbjkljlkjsdfoiwje"
  4. #
  5. #Application Secret
  6. #OVH_AS="sdfsafsdfsdfdsfsdfsa"
  7. #
  8. #Consumer Key
  9. #OVH_CK="sdfsdfsdfsdfsdfdsf"
  10. #OVH_END_POINT=ovh-eu
  11. #'ovh-eu'
  12. OVH_EU='https://eu.api.ovh.com/1.0'
  13. #'ovh-ca':
  14. OVH_CA='https://ca.api.ovh.com/1.0'
  15. #'kimsufi-eu'
  16. KSF_EU='https://eu.api.kimsufi.com/1.0'
  17. #'kimsufi-ca'
  18. KSF_CA='https://ca.api.kimsufi.com/1.0'
  19. #'soyoustart-eu'
  20. SYS_EU='https://eu.api.soyoustart.com/1.0'
  21. #'soyoustart-ca'
  22. SYS_CA='https://ca.api.soyoustart.com/1.0'
  23. #'runabove-ca'
  24. RAV_CA='https://api.runabove.com/1.0'
  25. wiki="https://github.com/acmesh-official/acme.sh/wiki/How-to-use-OVH-domain-api"
  26. ovh_success="https://github.com/acmesh-official/acme.sh/wiki/OVH-Success"
  27. _ovh_get_api() {
  28. _ogaep="$1"
  29. case "${_ogaep}" in
  30. ovh-eu | ovheu)
  31. printf "%s" $OVH_EU
  32. return
  33. ;;
  34. ovh-ca | ovhca)
  35. printf "%s" $OVH_CA
  36. return
  37. ;;
  38. kimsufi-eu | kimsufieu)
  39. printf "%s" $KSF_EU
  40. return
  41. ;;
  42. kimsufi-ca | kimsufica)
  43. printf "%s" $KSF_CA
  44. return
  45. ;;
  46. soyoustart-eu | soyoustarteu)
  47. printf "%s" $SYS_EU
  48. return
  49. ;;
  50. soyoustart-ca | soyoustartca)
  51. printf "%s" $SYS_CA
  52. return
  53. ;;
  54. runabove-ca | runaboveca)
  55. printf "%s" $RAV_CA
  56. return
  57. ;;
  58. *)
  59. _err "Unknown parameter : $1"
  60. return 1
  61. ;;
  62. esac
  63. }
  64. _initAuth() {
  65. OVH_AK="${OVH_AK:-$(_readaccountconf_mutable OVH_AK)}"
  66. OVH_AS="${OVH_AS:-$(_readaccountconf_mutable OVH_AS)}"
  67. if [ -z "$OVH_AK" ] || [ -z "$OVH_AS" ]; then
  68. OVH_AK=""
  69. OVH_AS=""
  70. _err "You don't specify OVH application key and application secret yet."
  71. _err "Please create you key and try again."
  72. return 1
  73. fi
  74. if [ "$OVH_AK" != "$(_readaccountconf OVH_AK)" ]; then
  75. _info "It seems that your ovh key is changed, let's clear consumer key first."
  76. _clearaccountconf OVH_CK
  77. fi
  78. _saveaccountconf_mutable OVH_AK "$OVH_AK"
  79. _saveaccountconf_mutable OVH_AS "$OVH_AS"
  80. OVH_END_POINT="${OVH_END_POINT:-$(_readaccountconf_mutable OVH_END_POINT)}"
  81. if [ -z "$OVH_END_POINT" ]; then
  82. OVH_END_POINT="ovh-eu"
  83. fi
  84. _info "Using OVH endpoint: $OVH_END_POINT"
  85. if [ "$OVH_END_POINT" != "ovh-eu" ]; then
  86. _saveaccountconf_mutable OVH_END_POINT "$OVH_END_POINT"
  87. fi
  88. OVH_API="$(_ovh_get_api $OVH_END_POINT)"
  89. _debug OVH_API "$OVH_API"
  90. OVH_CK="${OVH_CK:-$(_readaccountconf_mutable OVH_CK)}"
  91. if [ -z "$OVH_CK" ]; then
  92. _info "OVH consumer key is empty, Let's get one:"
  93. if ! _ovh_authentication; then
  94. _err "Can not get consumer key."
  95. fi
  96. #return and wait for retry.
  97. return 1
  98. fi
  99. _info "Checking authentication"
  100. if ! _ovh_rest GET "domain" || _contains "$response" "INVALID_CREDENTIAL" || _contains "$response" "NOT_CREDENTIAL"; then
  101. _err "The consumer key is invalid: $OVH_CK"
  102. _err "Please retry to create a new one."
  103. _clearaccountconf OVH_CK
  104. return 1
  105. fi
  106. _info "Consumer key is ok."
  107. return 0
  108. }
  109. ######## Public functions #####################
  110. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  111. dns_ovh_add() {
  112. fulldomain=$1
  113. txtvalue=$2
  114. if ! _initAuth; then
  115. return 1
  116. fi
  117. _debug "First detect the root zone"
  118. if ! _get_root "$fulldomain"; then
  119. _err "invalid domain"
  120. return 1
  121. fi
  122. _debug _sub_domain "$_sub_domain"
  123. _debug _domain "$_domain"
  124. _info "Adding record"
  125. if _ovh_rest POST "domain/zone/$_domain/record" "{\"fieldType\":\"TXT\",\"subDomain\":\"$_sub_domain\",\"target\":\"$txtvalue\",\"ttl\":60}"; then
  126. if _contains "$response" "$txtvalue"; then
  127. _ovh_rest POST "domain/zone/$_domain/refresh"
  128. _debug "Refresh:$response"
  129. _info "Added, sleep 10 seconds."
  130. _sleep 10
  131. return 0
  132. fi
  133. fi
  134. _err "Add txt record error."
  135. return 1
  136. }
  137. #fulldomain
  138. dns_ovh_rm() {
  139. fulldomain=$1
  140. txtvalue=$2
  141. if ! _initAuth; then
  142. return 1
  143. fi
  144. _debug "First detect the root zone"
  145. if ! _get_root "$fulldomain"; then
  146. _err "invalid domain"
  147. return 1
  148. fi
  149. _debug _sub_domain "$_sub_domain"
  150. _debug _domain "$_domain"
  151. _debug "Getting txt records"
  152. if ! _ovh_rest GET "domain/zone/$_domain/record?fieldType=TXT&subDomain=$_sub_domain"; then
  153. return 1
  154. fi
  155. for rid in $(echo "$response" | tr '][,' ' '); do
  156. _debug rid "$rid"
  157. if ! _ovh_rest GET "domain/zone/$_domain/record/$rid"; then
  158. return 1
  159. fi
  160. if _contains "$response" "\"target\":\"$txtvalue\""; then
  161. _debug "Found txt id:$rid"
  162. if ! _ovh_rest DELETE "domain/zone/$_domain/record/$rid"; then
  163. return 1
  164. fi
  165. return 0
  166. fi
  167. done
  168. return 1
  169. }
  170. #################### Private functions below ##################################
  171. _ovh_authentication() {
  172. _H1="X-Ovh-Application: $OVH_AK"
  173. _H2="Content-type: application/json"
  174. _H3=""
  175. _H4=""
  176. _ovhdata='{"accessRules": [{"method": "GET","path": "/auth/time"},{"method": "GET","path": "/domain"},{"method": "GET","path": "/domain/zone/*"},{"method": "GET","path": "/domain/zone/*/record"},{"method": "POST","path": "/domain/zone/*/record"},{"method": "POST","path": "/domain/zone/*/refresh"},{"method": "PUT","path": "/domain/zone/*/record/*"},{"method": "DELETE","path": "/domain/zone/*/record/*"}],"redirection":"'$ovh_success'"}'
  177. response="$(_post "$_ovhdata" "$OVH_API/auth/credential")"
  178. _debug3 response "$response"
  179. validationUrl="$(echo "$response" | _egrep_o "validationUrl\":\"[^\"]*\"" | _egrep_o "http.*\"" | tr -d '"')"
  180. if [ -z "$validationUrl" ]; then
  181. _err "Unable to get validationUrl"
  182. return 1
  183. fi
  184. _debug validationUrl "$validationUrl"
  185. consumerKey="$(echo "$response" | _egrep_o "consumerKey\":\"[^\"]*\"" | cut -d : -f 2 | tr -d '"')"
  186. if [ -z "$consumerKey" ]; then
  187. _err "Unable to get consumerKey"
  188. return 1
  189. fi
  190. _secure_debug consumerKey "$consumerKey"
  191. OVH_CK="$consumerKey"
  192. _saveaccountconf OVH_CK "$OVH_CK"
  193. _info "Please open this link to do authentication: $(__green "$validationUrl")"
  194. _info "Here is a guide for you: $(__green "$wiki")"
  195. _info "Please retry after the authentication is done."
  196. }
  197. #_acme-challenge.www.domain.com
  198. #returns
  199. # _sub_domain=_acme-challenge.www
  200. # _domain=domain.com
  201. _get_root() {
  202. domain=$1
  203. i=1
  204. p=1
  205. while true; do
  206. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  207. if [ -z "$h" ]; then
  208. #not valid
  209. return 1
  210. fi
  211. if ! _ovh_rest GET "domain/zone/$h"; then
  212. return 1
  213. fi
  214. if ! _contains "$response" "This service does not exist" >/dev/null && \
  215. ! _contains "$response" "This call has not been granted" >/dev/null && \
  216. ! _contains "$response" "NOT_GRANTED_CALL" >/dev/null; then
  217. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  218. _domain="$h"
  219. return 0
  220. fi
  221. p=$i
  222. i=$(_math "$i" + 1)
  223. done
  224. return 1
  225. }
  226. _ovh_timestamp() {
  227. _H1=""
  228. _H2=""
  229. _H3=""
  230. _H4=""
  231. _H5=""
  232. _get "$OVH_API/auth/time" "" 30
  233. }
  234. _ovh_rest() {
  235. m=$1
  236. ep="$2"
  237. data="$3"
  238. _debug "$ep"
  239. _ovh_url="$OVH_API/$ep"
  240. _debug2 _ovh_url "$_ovh_url"
  241. _ovh_t="$(_ovh_timestamp)"
  242. _debug2 _ovh_t "$_ovh_t"
  243. _ovh_p="$OVH_AS+$OVH_CK+$m+$_ovh_url+$data+$_ovh_t"
  244. _secure_debug _ovh_p "$_ovh_p"
  245. _ovh_hex="$(printf "%s" "$_ovh_p" | _digest sha1 hex)"
  246. _debug2 _ovh_hex "$_ovh_hex"
  247. export _H1="X-Ovh-Application: $OVH_AK"
  248. export _H2="X-Ovh-Signature: \$1\$$_ovh_hex"
  249. _debug2 _H2 "$_H2"
  250. export _H3="X-Ovh-Timestamp: $_ovh_t"
  251. export _H4="X-Ovh-Consumer: $OVH_CK"
  252. export _H5="Content-Type: application/json;charset=utf-8"
  253. if [ "$data" ] || [ "$m" = "POST" ] || [ "$m" = "PUT" ] || [ "$m" = "DELETE" ]; then
  254. _debug data "$data"
  255. response="$(_post "$data" "$_ovh_url" "" "$m")"
  256. else
  257. response="$(_get "$_ovh_url")"
  258. fi
  259. if [ "$?" != "0" ] || _contains "$response" "INVALID_CREDENTIAL"; then
  260. _err "error $response"
  261. return 1
  262. fi
  263. _debug2 response "$response"
  264. return 0
  265. }