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.

294 lines
6.9 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. #fulldomain
  147. dns_ovh_rm() {
  148. fulldomain=$1
  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 true; do
  187. h=$(printf "%s" "$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 "%s" "$domain" | cut -d . -f 1-$p)
  197. _domain="$h"
  198. return 0
  199. fi
  200. p=$i
  201. i=$(_math "$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. }