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.

295 lines
7.1 KiB

8 years ago
  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. ######## 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. OVH_AK=""
  71. OVH_AS=""
  72. _err "You don't specify OVH application key and application secret yet."
  73. _err "Please create you key and try again."
  74. return 1
  75. fi
  76. #save the api key and email to the account conf file.
  77. _saveaccountconf OVH_AK "$OVH_AK"
  78. _saveaccountconf OVH_AS "$OVH_AS"
  79. if [ -z "$OVH_END_POINT" ]; then
  80. OVH_END_POINT="ovh-eu"
  81. fi
  82. _info "Using OVH endpoint: $OVH_END_POINT"
  83. if [ "$OVH_END_POINT" != "ovh-eu" ]; then
  84. _saveaccountconf OVH_END_POINT "$OVH_END_POINT"
  85. fi
  86. OVH_API="$(_ovh_get_api $OVH_END_POINT)"
  87. _debug OVH_API "$OVH_API"
  88. if [ -z "$OVH_CK" ]; then
  89. _info "OVH consumer key is empty, Let's get one:"
  90. if ! _ovh_authentication; then
  91. _err "Can not get consumer key."
  92. fi
  93. #return and wait for retry.
  94. return 1
  95. fi
  96. _info "Checking authentication"
  97. response="$(_ovh_rest GET "domain")"
  98. if _contains "$response" "INVALID_CREDENTIAL"; then
  99. _err "The consumer key is invalid: $OVH_CK"
  100. _err "Please retry to create a new one."
  101. _clearaccountconf OVH_CK
  102. return 1
  103. fi
  104. _info "Consumer key is ok."
  105. _debug "First detect the root zone"
  106. if ! _get_root "$fulldomain"; then
  107. _err "invalid domain"
  108. return 1
  109. fi
  110. _debug _sub_domain "$_sub_domain"
  111. _debug _domain "$_domain"
  112. _debug "Getting txt records"
  113. _ovh_rest GET "domain/zone/$_domain/record?fieldType=TXT&subDomain=$_sub_domain"
  114. if _contains "$response" '\[\]' || _contains "$response" "This service does not exist"; then
  115. _info "Adding record"
  116. if _ovh_rest POST "domain/zone/$_domain/record" "{\"fieldType\":\"TXT\",\"subDomain\":\"$_sub_domain\",\"target\":\"$txtvalue\",\"ttl\":60}"; then
  117. if _contains "$response" "$txtvalue"; then
  118. _ovh_rest POST "domain/zone/$_domain/refresh"
  119. _debug "Refresh:$response"
  120. _info "Added, sleeping 10 seconds"
  121. sleep 10
  122. return 0
  123. fi
  124. fi
  125. _err "Add txt record error."
  126. else
  127. _info "Updating record"
  128. record_id=$(printf "%s" "$response" | tr -d "[]" | cut -d , -f 1)
  129. if [ -z "$record_id" ]; then
  130. _err "Can not get record id."
  131. return 1
  132. fi
  133. _debug "record_id" "$record_id"
  134. if _ovh_rest PUT "domain/zone/$_domain/record/$record_id" "{\"target\":\"$txtvalue\",\"subDomain\":\"$_sub_domain\",\"ttl\":60}"; then
  135. if _contains "$response" "null"; then
  136. _ovh_rest POST "domain/zone/$_domain/refresh"
  137. _debug "Refresh:$response"
  138. _info "Updated, sleeping 10 seconds"
  139. sleep 10
  140. return 0
  141. fi
  142. fi
  143. _err "Update error"
  144. return 1
  145. fi
  146. }
  147. #fulldomain
  148. dns_ovh_rm() {
  149. fulldomain=$1
  150. }
  151. #################### Private functions below ##################################
  152. _ovh_authentication() {
  153. _H1="X-Ovh-Application: $OVH_AK"
  154. _H2="Content-type: application/json"
  155. _H3=""
  156. _H4=""
  157. _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/*"}],"redirection":"'$ovh_success'"}'
  158. response="$(_post "$_ovhdata" "$OVH_API/auth/credential")"
  159. _debug3 response "$response"
  160. validationUrl="$(echo "$response" | _egrep_o "validationUrl\":\"[^\"]*\"" | _egrep_o "http.*\"" | tr -d '"')"
  161. if [ -z "$validationUrl" ]; then
  162. _err "Unable to get validationUrl"
  163. return 1
  164. fi
  165. _debug validationUrl "$validationUrl"
  166. consumerKey="$(echo "$response" | _egrep_o "consumerKey\":\"[^\"]*\"" | cut -d : -f 2 | tr -d '"')"
  167. if [ -z "$consumerKey" ]; then
  168. _err "Unable to get consumerKey"
  169. return 1
  170. fi
  171. _secure_debug consumerKey "$consumerKey"
  172. OVH_CK="$consumerKey"
  173. _saveaccountconf OVH_CK "$OVH_CK"
  174. _info "Please open this link to do authentication: $(__green "$validationUrl")"
  175. _info "Here is a guide for you: $(__green "$wiki")"
  176. _info "Please retry after the authentication is done."
  177. }
  178. #_acme-challenge.www.domain.com
  179. #returns
  180. # _sub_domain=_acme-challenge.www
  181. # _domain=domain.com
  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. _secure_debug _ovh_p "$_ovh_p"
  224. _ovh_hex="$(printf "%s" "$_ovh_p" | _digest sha1 hex)"
  225. _debug2 _ovh_hex "$_ovh_hex"
  226. export _H1="X-Ovh-Application: $OVH_AK"
  227. export _H2="X-Ovh-Signature: \$1\$$_ovh_hex"
  228. _debug2 _H2 "$_H2"
  229. export _H3="X-Ovh-Timestamp: $_ovh_t"
  230. export _H4="X-Ovh-Consumer: $OVH_CK"
  231. export _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. }