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.

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