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.

322 lines
7.8 KiB

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