dns_ovh.sh 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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/Neilpang/acme.sh/wiki/How-to-use-OVH-domain-api"
  26. ovh_success="https://github.com/Neilpang/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=2
  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 && ! _contains "$response" "NOT_GRANTED_CALL" >/dev/null; then
  215. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  216. _domain="$h"
  217. return 0
  218. fi
  219. p=$i
  220. i=$(_math "$i" + 1)
  221. done
  222. return 1
  223. }
  224. _ovh_timestamp() {
  225. _H1=""
  226. _H2=""
  227. _H3=""
  228. _H4=""
  229. _H5=""
  230. _get "$OVH_API/auth/time" "" 30
  231. }
  232. _ovh_rest() {
  233. m=$1
  234. ep="$2"
  235. data="$3"
  236. _debug "$ep"
  237. _ovh_url="$OVH_API/$ep"
  238. _debug2 _ovh_url "$_ovh_url"
  239. _ovh_t="$(_ovh_timestamp)"
  240. _debug2 _ovh_t "$_ovh_t"
  241. _ovh_p="$OVH_AS+$OVH_CK+$m+$_ovh_url+$data+$_ovh_t"
  242. _secure_debug _ovh_p "$_ovh_p"
  243. _ovh_hex="$(printf "%s" "$_ovh_p" | _digest sha1 hex)"
  244. _debug2 _ovh_hex "$_ovh_hex"
  245. export _H1="X-Ovh-Application: $OVH_AK"
  246. export _H2="X-Ovh-Signature: \$1\$$_ovh_hex"
  247. _debug2 _H2 "$_H2"
  248. export _H3="X-Ovh-Timestamp: $_ovh_t"
  249. export _H4="X-Ovh-Consumer: $OVH_CK"
  250. export _H5="Content-Type: application/json;charset=utf-8"
  251. if [ "$data" ] || [ "$m" = "POST" ] || [ "$m" = "PUT" ] || [ "$m" = "DELETE" ]; then
  252. _debug data "$data"
  253. response="$(_post "$data" "$_ovh_url" "" "$m")"
  254. else
  255. response="$(_get "$_ovh_url")"
  256. fi
  257. if [ "$?" != "0" ] || _contains "$response" "INVALID_CREDENTIAL"; then
  258. _err "error $response"
  259. return 1
  260. fi
  261. _debug2 response "$response"
  262. return 0
  263. }