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.

314 lines
7.4 KiB

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