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.

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