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.

293 lines
6.8 KiB

  1. #!/usr/bin/env sh
  2. #Applcation 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. ######## Public functions #####################
  65. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  66. dns_ovh_add() {
  67. fulldomain=$1
  68. txtvalue=$2
  69. if [ -z "$OVH_AK" ] || [ -z "$OVH_AS" ]; then
  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. #save the api key and email to the account conf file.
  75. _saveaccountconf OVH_AK "$OVH_AK"
  76. _saveaccountconf OVH_AS "$OVH_AS"
  77. if [ -z "$OVH_END_POINT" ]; then
  78. OVH_END_POINT="ovh-eu"
  79. fi
  80. _info "Using OVH endpoint: $OVH_END_POINT"
  81. if [ "$OVH_END_POINT" != "ovh-eu" ]; then
  82. _saveaccountconf OVH_END_POINT "$OVH_END_POINT"
  83. fi
  84. OVH_API="$(_ovh_get_api $OVH_END_POINT)"
  85. _debug OVH_API "$OVH_API"
  86. if [ -z "$OVH_CK" ]; then
  87. _info "OVH consumer key is empty, Let's get one:"
  88. if ! _ovh_authentication; then
  89. _err "Can not get consumer key."
  90. fi
  91. #return and wait for retry.
  92. return 1
  93. fi
  94. _info "Checking authentication"
  95. response="$(_ovh_rest GET "domain/")"
  96. if _contains "$response" "INVALID_CREDENTIAL"; then
  97. _err "The consumer key is invalid: $OVH_CK"
  98. _err "Please retry to create a new one."
  99. _clearaccountconf OVH_CK
  100. return 1
  101. fi
  102. _info "Consumer key is ok."
  103. _debug "First detect the root zone"
  104. if ! _get_root "$fulldomain"; then
  105. _err "invalid domain"
  106. return 1
  107. fi
  108. _debug _sub_domain "$_sub_domain"
  109. _debug _domain "$_domain"
  110. _debug "Getting txt records"
  111. _ovh_rest GET "domain/zone/$_domain/record?fieldType=TXT&subDomain=$_sub_domain"
  112. if _contains "$response" '\[\]' || _contains "$response" "This service does not exist"; then
  113. _info "Adding record"
  114. if _ovh_rest POST "domain/zone/$_domain/record" "{\"fieldType\":\"TXT\",\"subDomain\":\"$_sub_domain\",\"target\":\"$txtvalue\",\"ttl\":60}"; then
  115. if _contains "$response" "$txtvalue"; then
  116. _ovh_rest POST "domain/zone/$_domain/refresh"
  117. _debug "Refresh:$response"
  118. _info "Added, sleeping 10 seconds"
  119. sleep 10
  120. return 0
  121. fi
  122. fi
  123. _err "Add txt record error."
  124. else
  125. _info "Updating record"
  126. record_id=$(printf "%s" "$response" | tr -d "[]" | cut -d , -f 1)
  127. if [ -z "$record_id" ]; then
  128. _err "Can not get record id."
  129. return 1
  130. fi
  131. _debug "record_id" "$record_id"
  132. if _ovh_rest PUT "domain/zone/$_domain/record/$record_id" "{\"target\":\"$txtvalue\",\"subDomain\":\"$_sub_domain\",\"ttl\":60}"; then
  133. if _contains "$response" "null"; then
  134. _ovh_rest POST "domain/zone/$_domain/refresh"
  135. _debug "Refresh:$response"
  136. _info "Updated, sleeping 10 seconds"
  137. sleep 10
  138. return 0
  139. fi
  140. fi
  141. _err "Update error"
  142. return 1
  143. fi
  144. }
  145. #fulldomain
  146. dns_ovh_rm() {
  147. fulldomain=$1
  148. }
  149. #################### Private functions bellow ##################################
  150. _ovh_authentication() {
  151. _H1="X-Ovh-Application: $OVH_AK"
  152. _H2="Content-type: application/json"
  153. _H3=""
  154. _H4=""
  155. _ovhdata='{"accessRules": [{"method": "GET","path": "/*"},{"method": "POST","path": "/*"},{"method": "PUT","path": "/*"},{"method": "DELETE","path": "/*"}],"redirection":"'$ovh_success'"}'
  156. response="$(_post "$_ovhdata" "$OVH_API/auth/credential")"
  157. _debug3 response "$response"
  158. validationUrl="$(echo "$response" | _egrep_o "validationUrl\":\"[^\"]*\"" | _egrep_o "http.*\"" | tr -d '"')"
  159. if [ -z "$validationUrl" ]; then
  160. _err "Unable to get validationUrl"
  161. return 1
  162. fi
  163. _debug validationUrl "$validationUrl"
  164. consumerKey="$(echo "$response" | _egrep_o "consumerKey\":\"[^\"]*\"" | cut -d : -f 2 | tr -d '"')"
  165. if [ -z "$consumerKey" ]; then
  166. _err "Unable to get consumerKey"
  167. return 1
  168. fi
  169. _debug consumerKey "$consumerKey"
  170. OVH_CK="$consumerKey"
  171. _saveaccountconf OVH_CK "$OVH_CK"
  172. _info "Please open this link to do authentication: $(__green "$validationUrl")"
  173. _info "Here is a guide for you: $(__green "$wiki")"
  174. _info "Please retry after the authentication is done."
  175. }
  176. #_acme-challenge.www.domain.com
  177. #returns
  178. # _sub_domain=_acme-challenge.www
  179. # _domain=domain.com
  180. _get_root() {
  181. domain=$1
  182. i=2
  183. p=1
  184. while true; do
  185. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  186. if [ -z "$h" ]; then
  187. #not valid
  188. return 1
  189. fi
  190. if ! _ovh_rest GET "domain/zone/$h"; then
  191. return 1
  192. fi
  193. if ! _contains "$response" "This service does not exist" >/dev/null; then
  194. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  195. _domain="$h"
  196. return 0
  197. fi
  198. p=$i
  199. i=$(_math "$i" + 1)
  200. done
  201. return 1
  202. }
  203. _ovh_timestamp() {
  204. _H1=""
  205. _H2=""
  206. _H3=""
  207. _H4=""
  208. _H5=""
  209. _get "$OVH_API/auth/time" "" 30
  210. }
  211. _ovh_rest() {
  212. m=$1
  213. ep="$2"
  214. data="$3"
  215. _debug "$ep"
  216. _ovh_url="$OVH_API/$ep"
  217. _debug2 _ovh_url "$_ovh_url"
  218. _ovh_t="$(_ovh_timestamp)"
  219. _debug2 _ovh_t "$_ovh_t"
  220. _ovh_p="$OVH_AS+$OVH_CK+$m+$_ovh_url+$data+$_ovh_t"
  221. _debug _ovh_p "$_ovh_p"
  222. _ovh_hex="$(printf "%s" "$_ovh_p" | _digest sha1 hex)"
  223. _debug2 _ovh_hex "$_ovh_hex"
  224. _H1="X-Ovh-Application: $OVH_AK"
  225. _H2="X-Ovh-Signature: \$1\$$_ovh_hex"
  226. _debug2 _H2 "$_H2"
  227. _H3="X-Ovh-Timestamp: $_ovh_t"
  228. _H4="X-Ovh-Consumer: $OVH_CK"
  229. _H5="Content-Type: application/json;charset=utf-8"
  230. if [ "$data" ] || [ "$m" = "POST" ] || [ "$m" = "PUT" ]; then
  231. _debug data "$data"
  232. response="$(_post "$data" "$_ovh_url" "" "$m")"
  233. else
  234. response="$(_get "$_ovh_url")"
  235. fi
  236. if [ "$?" != "0" ]; then
  237. _err "error $ep"
  238. return 1
  239. fi
  240. _debug2 response "$response"
  241. return 0
  242. }