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.

302 lines
7.0 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 _domain_id "$_domain_id"
  109. _debug _sub_domain "$_sub_domain"
  110. _debug _domain "$_domain"
  111. _debug "Getting txt records"
  112. _ovh_rest GET "domain/zone/$_domain/record?fieldType=TXT&subDomain=$_sub_domain"
  113. if _contains "$response" '\[\]' || _contains "$response" "This service does not exist" ; then
  114. _info "Adding record"
  115. if _ovh_rest POST "domain/zone/$_domain/record" "{\"fieldType\":\"TXT\",\"subDomain\":\"$_sub_domain\",\"target\":\"$txtvalue\",\"ttl\":60}"; then
  116. if _contains "$response" "$txtvalue" ; then
  117. _ovh_rest POST "domain/zone/$_domain/refresh"
  118. _debug "Refresh:$response"
  119. _info "Added, sleeping 10 seconds"
  120. sleep 10
  121. return 0
  122. fi
  123. fi
  124. _err "Add txt record error."
  125. else
  126. _info "Updating record"
  127. record_id=$(printf "%s" "$response" | tr -d "[]" | cut -d , -f 1)
  128. if [ -z "$record_id" ] ; then
  129. _err "Can not get record id."
  130. return 1
  131. fi
  132. _debug "record_id" $record_id
  133. if _ovh_rest PUT "domain/zone/$_domain/record/$record_id" "{\"target\":\"$txtvalue\",\"subDomain\":\"$_sub_domain\",\"ttl\":60}" ; then
  134. if _contains "$response" "null" ; then
  135. _ovh_rest POST "domain/zone/$_domain/refresh"
  136. _debug "Refresh:$response"
  137. _info "Updated, sleeping 10 seconds"
  138. sleep 10
  139. return 0;
  140. fi
  141. fi
  142. _err "Update error"
  143. return 1
  144. fi
  145. }
  146. #################### Private functions bellow ##################################
  147. _ovh_authentication() {
  148. _H1="X-Ovh-Application: $OVH_AK"
  149. _H2="Content-type: application/json"
  150. _H3=""
  151. _H4=""
  152. _ovhdata='{"accessRules": [{"method": "GET","path": "/*"},{"method": "POST","path": "/*"},{"method": "PUT","path": "/*"},{"method": "DELETE","path": "/*"}],"redirection":"'$ovh_success'"}'
  153. response="$(_post "$_ovhdata" "$OVH_API/auth/credential")"
  154. _debug3 response "$response"
  155. validationUrl="$(echo "$response" | _egrep_o "validationUrl\":\"[^\"]*\"" | _egrep_o "http.*\"" | tr -d '"')"
  156. if [ -z "$validationUrl" ] ; then
  157. _err "Unable to get validationUrl"
  158. return 1
  159. fi
  160. _debug validationUrl "$validationUrl"
  161. consumerKey="$(echo "$response" | _egrep_o "consumerKey\":\"[^\"]*\"" | cut -d : -f 2 | tr -d '"')"
  162. if [ -z "$consumerKey" ] ; then
  163. _err "Unable to get consumerKey"
  164. return 1
  165. fi
  166. _debug consumerKey "$consumerKey"
  167. OVH_CK="$consumerKey"
  168. _saveaccountconf OVH_CK "$OVH_CK"
  169. _info "Please open this link to do authentication: $(__green "$validationUrl" )"
  170. _info "Here is a guide for you: $(__green "$wiki" )"
  171. _info "Please retry after the authentication is done."
  172. }
  173. #_acme-challenge.www.domain.com
  174. #returns
  175. # _sub_domain=_acme-challenge.www
  176. # _domain=domain.com
  177. # _domain_id=sdjkglgdfewsdfg
  178. _get_root() {
  179. domain=$1
  180. i=2
  181. p=1
  182. while [ '1' ] ; do
  183. h=$(printf $domain | cut -d . -f $i-100)
  184. if [ -z "$h" ] ; then
  185. #not valid
  186. return 1;
  187. fi
  188. if ! _ovh_rest GET "domain/zone/$h" ; then
  189. return 1
  190. fi
  191. if ! _contains "$response" "This service does not exist" >/dev/null ; then
  192. _sub_domain=$(printf $domain | cut -d . -f 1-$p)
  193. _domain=$h
  194. return 0
  195. fi
  196. p=$i
  197. i=$(expr $i + 1)
  198. done
  199. return 1
  200. }
  201. _ovh_timestamp() {
  202. _H1=""
  203. _H2=""
  204. _H3=""
  205. _H4=""
  206. _H5=""
  207. _get "$OVH_API/auth/time" "" 30
  208. }
  209. _ovh_rest() {
  210. m=$1
  211. ep="$2"
  212. data="$3"
  213. _debug $ep
  214. _ovh_url="$OVH_API/$ep"
  215. _debug2 _ovh_url "$_ovh_url"
  216. _ovh_t="$(_ovh_timestamp)"
  217. _debug2 _ovh_t "$_ovh_t"
  218. _ovh_p="$OVH_AS+$OVH_CK+$m+$_ovh_url+$data+$_ovh_t"
  219. _debug _ovh_p "$_ovh_p"
  220. _ovh_hex="$(printf "%s" "$_ovh_p" | _digest sha1 hex)"
  221. _debug2 _ovh_hex "$_ovh_hex"
  222. _H1="X-Ovh-Application: $OVH_AK"
  223. _H2="X-Ovh-Signature: \$1\$$_ovh_hex"
  224. _debug2 _H2 "$_H2"
  225. _H3="X-Ovh-Timestamp: $_ovh_t"
  226. _H4="X-Ovh-Consumer: $OVH_CK"
  227. _H5="Content-Type: application/json;charset=utf-8"
  228. if [ "$data" ] || [ "$m" = "POST" ] || [ "$m" = "PUT" ] ; then
  229. _debug data "$data"
  230. response="$(_post "$data" "$_ovh_url" "" $m)"
  231. else
  232. response="$(_get "$_ovh_url")"
  233. fi
  234. if [ "$?" != "0" ] ; then
  235. _err "error $ep"
  236. return 1
  237. fi
  238. _debug2 response "$response"
  239. return 0
  240. }