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.

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