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.

310 lines
7.2 KiB

7 years ago
7 years ago
8 years ago
7 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. if ! _ovh_rest GET "domain" || _contains "$response" "INVALID_CREDENTIAL"; then
  94. _err "The consumer key is invalid: $OVH_CK"
  95. _err "Please retry to create a new one."
  96. _clearaccountconf OVH_CK
  97. return 1
  98. fi
  99. _info "Consumer key is ok."
  100. return 0
  101. }
  102. ######## Public functions #####################
  103. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  104. dns_ovh_add() {
  105. fulldomain=$1
  106. txtvalue=$2
  107. if ! _initAuth; then
  108. return 1
  109. fi
  110. _debug "First detect the root zone"
  111. if ! _get_root "$fulldomain"; then
  112. _err "invalid domain"
  113. return 1
  114. fi
  115. _debug _sub_domain "$_sub_domain"
  116. _debug _domain "$_domain"
  117. _info "Adding record"
  118. if _ovh_rest POST "domain/zone/$_domain/record" "{\"fieldType\":\"TXT\",\"subDomain\":\"$_sub_domain\",\"target\":\"$txtvalue\",\"ttl\":60}"; then
  119. if _contains "$response" "$txtvalue"; then
  120. _ovh_rest POST "domain/zone/$_domain/refresh"
  121. _debug "Refresh:$response"
  122. _info "Added, sleep 10 seconds."
  123. _sleep 10
  124. return 0
  125. fi
  126. fi
  127. _err "Add txt record error."
  128. return 1
  129. }
  130. #fulldomain
  131. dns_ovh_rm() {
  132. fulldomain=$1
  133. txtvalue=$2
  134. if ! _initAuth; then
  135. return 1
  136. fi
  137. _debug "First detect the root zone"
  138. if ! _get_root "$fulldomain"; then
  139. _err "invalid domain"
  140. return 1
  141. fi
  142. _debug _sub_domain "$_sub_domain"
  143. _debug _domain "$_domain"
  144. _debug "Getting txt records"
  145. if ! _ovh_rest GET "domain/zone/$_domain/record?fieldType=TXT&subDomain=$_sub_domain"; then
  146. return 1
  147. fi
  148. for rid in $(echo "$response" | tr '][,' ' '); do
  149. _debug rid "$rid"
  150. if ! _ovh_rest GET "domain/zone/$_domain/record/$rid"; then
  151. return 1
  152. fi
  153. if _contains "$response" "\"target\":\"$txtvalue\""; then
  154. _debug "Found txt id:$rid"
  155. if ! _ovh_rest DELETE "domain/zone/$_domain/record/$rid"; then
  156. return 1
  157. fi
  158. return 0
  159. fi
  160. done
  161. return 1
  162. }
  163. #################### Private functions below ##################################
  164. _ovh_authentication() {
  165. _H1="X-Ovh-Application: $OVH_AK"
  166. _H2="Content-type: application/json"
  167. _H3=""
  168. _H4=""
  169. _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'"}'
  170. response="$(_post "$_ovhdata" "$OVH_API/auth/credential")"
  171. _debug3 response "$response"
  172. validationUrl="$(echo "$response" | _egrep_o "validationUrl\":\"[^\"]*\"" | _egrep_o "http.*\"" | tr -d '"')"
  173. if [ -z "$validationUrl" ]; then
  174. _err "Unable to get validationUrl"
  175. return 1
  176. fi
  177. _debug validationUrl "$validationUrl"
  178. consumerKey="$(echo "$response" | _egrep_o "consumerKey\":\"[^\"]*\"" | cut -d : -f 2 | tr -d '"')"
  179. if [ -z "$consumerKey" ]; then
  180. _err "Unable to get consumerKey"
  181. return 1
  182. fi
  183. _secure_debug consumerKey "$consumerKey"
  184. OVH_CK="$consumerKey"
  185. _saveaccountconf OVH_CK "$OVH_CK"
  186. _info "Please open this link to do authentication: $(__green "$validationUrl")"
  187. _info "Here is a guide for you: $(__green "$wiki")"
  188. _info "Please retry after the authentication is done."
  189. }
  190. #_acme-challenge.www.domain.com
  191. #returns
  192. # _sub_domain=_acme-challenge.www
  193. # _domain=domain.com
  194. _get_root() {
  195. domain=$1
  196. i=2
  197. p=1
  198. while true; do
  199. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  200. if [ -z "$h" ]; then
  201. #not valid
  202. return 1
  203. fi
  204. if ! _ovh_rest GET "domain/zone/$h"; then
  205. return 1
  206. fi
  207. if ! _contains "$response" "This service does not exist" >/dev/null && ! _contains "$response" "NOT_GRANTED_CALL" >/dev/null; then
  208. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  209. _domain="$h"
  210. return 0
  211. fi
  212. p=$i
  213. i=$(_math "$i" + 1)
  214. done
  215. return 1
  216. }
  217. _ovh_timestamp() {
  218. _H1=""
  219. _H2=""
  220. _H3=""
  221. _H4=""
  222. _H5=""
  223. _get "$OVH_API/auth/time" "" 30
  224. }
  225. _ovh_rest() {
  226. m=$1
  227. ep="$2"
  228. data="$3"
  229. _debug "$ep"
  230. _ovh_url="$OVH_API/$ep"
  231. _debug2 _ovh_url "$_ovh_url"
  232. _ovh_t="$(_ovh_timestamp)"
  233. _debug2 _ovh_t "$_ovh_t"
  234. _ovh_p="$OVH_AS+$OVH_CK+$m+$_ovh_url+$data+$_ovh_t"
  235. _secure_debug _ovh_p "$_ovh_p"
  236. _ovh_hex="$(printf "%s" "$_ovh_p" | _digest sha1 hex)"
  237. _debug2 _ovh_hex "$_ovh_hex"
  238. export _H1="X-Ovh-Application: $OVH_AK"
  239. export _H2="X-Ovh-Signature: \$1\$$_ovh_hex"
  240. _debug2 _H2 "$_H2"
  241. export _H3="X-Ovh-Timestamp: $_ovh_t"
  242. export _H4="X-Ovh-Consumer: $OVH_CK"
  243. export _H5="Content-Type: application/json;charset=utf-8"
  244. if [ "$data" ] || [ "$m" = "POST" ] || [ "$m" = "PUT" ] || [ "$m" = "DELETE" ]; then
  245. _debug data "$data"
  246. response="$(_post "$data" "$_ovh_url" "" "$m")"
  247. else
  248. response="$(_get "$_ovh_url")"
  249. fi
  250. if [ "$?" != "0" ] || _contains "$response" "INVALID_CREDENTIAL"; then
  251. _err "error $response"
  252. return 1
  253. fi
  254. _debug2 response "$response"
  255. return 0
  256. }