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.

324 lines
9.9 KiB

  1. #!/usr/bin/env sh
  2. #
  3. # Acme.sh DNS API plugin for Oracle Cloud Infrastructure
  4. # Copyright (c) 2021, Oracle and/or its affiliates
  5. #
  6. # The plugin will automatically use the default profile from an OCI SDK and CLI
  7. # configuration file, if it exists.
  8. #
  9. # Alternatively, set the following environment variables:
  10. # - OCI_CLI_TENANCY : OCID of tenancy that contains the target DNS zone
  11. # - OCI_CLI_USER : OCID of user with permission to add/remove records from zones
  12. # - OCI_CLI_REGION : Should point to the tenancy home region
  13. #
  14. # One of the following two variables is required:
  15. # - OCI_CLI_KEY_FILE: Path to private API signing key file in PEM format; or
  16. # - OCI_CLI_KEY : The private API signing key in PEM format
  17. #
  18. # NOTE: using an encrypted private key that needs a passphrase is not supported.
  19. #
  20. dns_oci_add() {
  21. _fqdn="$1"
  22. _rdata="$2"
  23. if _get_oci_zone; then
  24. _add_record_body="{\"items\":[{\"domain\":\"${_sub_domain}.${_domain}\",\"rdata\":\"$_rdata\",\"rtype\":\"TXT\",\"ttl\": 30,\"operation\":\"ADD\"}]}"
  25. response=$(_signed_request "PATCH" "/20180115/zones/${_domain}/records" "$_add_record_body")
  26. if [ "$response" ]; then
  27. _info "Success: added TXT record for ${_sub_domain}.${_domain}."
  28. else
  29. _err "Error: failed to add TXT record for ${_sub_domain}.${_domain}."
  30. _err "Check that the user has permission to add records to this zone."
  31. return 1
  32. fi
  33. else
  34. return 1
  35. fi
  36. }
  37. dns_oci_rm() {
  38. _fqdn="$1"
  39. _rdata="$2"
  40. if _get_oci_zone; then
  41. _remove_record_body="{\"items\":[{\"domain\":\"${_sub_domain}.${_domain}\",\"rdata\":\"$_rdata\",\"rtype\":\"TXT\",\"operation\":\"REMOVE\"}]}"
  42. response=$(_signed_request "PATCH" "/20180115/zones/${_domain}/records" "$_remove_record_body")
  43. if [ "$response" ]; then
  44. _info "Success: removed TXT record for ${_sub_domain}.${_domain}."
  45. else
  46. _err "Error: failed to remove TXT record for ${_sub_domain}.${_domain}."
  47. _err "Check that the user has permission to remove records from this zone."
  48. return 1
  49. fi
  50. else
  51. return 1
  52. fi
  53. }
  54. #################### Private functions below ##################################
  55. _get_oci_zone() {
  56. if ! _oci_config; then
  57. return 1
  58. fi
  59. if ! _get_zone "$_fqdn"; then
  60. _err "Error: DNS Zone not found for $_fqdn in $OCI_CLI_TENANCY"
  61. return 1
  62. fi
  63. return 0
  64. }
  65. _oci_config() {
  66. _DEFAULT_OCI_CLI_CONFIG_FILE="$HOME/.oci/config"
  67. OCI_CLI_CONFIG_FILE="${OCI_CLI_CONFIG_FILE:-$(_readaccountconf_mutable OCI_CLI_CONFIG_FILE)}"
  68. if [ -z "$OCI_CLI_CONFIG_FILE" ]; then
  69. OCI_CLI_CONFIG_FILE="$_DEFAULT_OCI_CLI_CONFIG_FILE"
  70. fi
  71. if [ "$_DEFAULT_OCI_CLI_CONFIG_FILE" != "$OCI_CLI_CONFIG_FILE" ]; then
  72. _saveaccountconf_mutable OCI_CLI_CONFIG_FILE "$OCI_CLI_CONFIG_FILE"
  73. else
  74. _clearaccountconf_mutable OCI_CLI_CONFIG_FILE
  75. fi
  76. _DEFAULT_OCI_CLI_PROFILE="DEFAULT"
  77. OCI_CLI_PROFILE="${OCI_CLI_PROFILE:-$(_readaccountconf_mutable OCI_CLI_PROFILE)}"
  78. if [ "$_DEFAULT_OCI_CLI_PROFILE" != "$OCI_CLI_PROFILE" ]; then
  79. _saveaccountconf_mutable OCI_CLI_PROFILE "$OCI_CLI_PROFILE"
  80. else
  81. OCI_CLI_PROFILE="$_DEFAULT_OCI_CLI_PROFILE"
  82. _clearaccountconf_mutable OCI_CLI_PROFILE
  83. fi
  84. OCI_CLI_TENANCY="${OCI_CLI_TENANCY:-$(_readaccountconf_mutable OCI_CLI_TENANCY)}"
  85. if [ "$OCI_CLI_TENANCY" ]; then
  86. _saveaccountconf_mutable OCI_CLI_TENANCY "$OCI_CLI_TENANCY"
  87. elif [ -f "$OCI_CLI_CONFIG_FILE" ]; then
  88. _debug "Reading OCI_CLI_TENANCY value from: $OCI_CLI_CONFIG_FILE"
  89. OCI_CLI_TENANCY="${OCI_CLI_TENANCY:-$(_readini "$OCI_CLI_CONFIG_FILE" tenancy "$OCI_CLI_PROFILE")}"
  90. fi
  91. if [ -z "$OCI_CLI_TENANCY" ]; then
  92. _err "Error: unable to read OCI_CLI_TENANCY from config file or environment variable."
  93. return 1
  94. fi
  95. OCI_CLI_USER="${OCI_CLI_USER:-$(_readaccountconf_mutable OCI_CLI_USER)}"
  96. if [ "$OCI_CLI_USER" ]; then
  97. _saveaccountconf_mutable OCI_CLI_USER "$OCI_CLI_USER"
  98. elif [ -f "$OCI_CLI_CONFIG_FILE" ]; then
  99. _debug "Reading OCI_CLI_USER value from: $OCI_CLI_CONFIG_FILE"
  100. OCI_CLI_USER="${OCI_CLI_USER:-$(_readini "$OCI_CLI_CONFIG_FILE" user "$OCI_CLI_PROFILE")}"
  101. fi
  102. if [ -z "$OCI_CLI_USER" ]; then
  103. _err "Error: unable to read OCI_CLI_USER from config file or environment variable."
  104. return 1
  105. fi
  106. OCI_CLI_REGION="${OCI_CLI_REGION:-$(_readaccountconf_mutable OCI_CLI_REGION)}"
  107. if [ "$OCI_CLI_REGION" ]; then
  108. _saveaccountconf_mutable OCI_CLI_REGION "$OCI_CLI_REGION"
  109. elif [ -f "$OCI_CLI_CONFIG_FILE" ]; then
  110. _debug "Reading OCI_CLI_REGION value from: $OCI_CLI_CONFIG_FILE"
  111. OCI_CLI_REGION="${OCI_CLI_REGION:-$(_readini "$OCI_CLI_CONFIG_FILE" region "$OCI_CLI_PROFILE")}"
  112. fi
  113. if [ -z "$OCI_CLI_REGION" ]; then
  114. _err "Error: unable to read OCI_CLI_REGION from config file or environment variable."
  115. return 1
  116. fi
  117. OCI_CLI_KEY="${OCI_CLI_KEY:-$(_readaccountconf_mutable OCI_CLI_KEY)}"
  118. if [ -z "$OCI_CLI_KEY" ]; then
  119. _clearaccountconf_mutable OCI_CLI_KEY
  120. OCI_CLI_KEY_FILE="${OCI_CLI_KEY_FILE:-$(_readini "$OCI_CLI_CONFIG_FILE" key_file "$OCI_CLI_PROFILE")}"
  121. if [ "$OCI_CLI_KEY_FILE" ] && [ -f "$OCI_CLI_KEY_FILE" ]; then
  122. _debug "Reading OCI_CLI_KEY value from: $OCI_CLI_KEY_FILE"
  123. OCI_CLI_KEY=$(_base64 <"$OCI_CLI_KEY_FILE")
  124. _saveaccountconf_mutable OCI_CLI_KEY "$OCI_CLI_KEY"
  125. fi
  126. else
  127. _saveaccountconf_mutable OCI_CLI_KEY "$OCI_CLI_KEY"
  128. fi
  129. if [ -z "$OCI_CLI_KEY_FILE" ] && [ -z "$OCI_CLI_KEY" ]; then
  130. _err "Error: unable to find key file path in OCI config file or OCI_CLI_KEY_FILE."
  131. _err "Error: unable to load private API signing key from OCI_CLI_KEY."
  132. return 1
  133. fi
  134. if [ "$(printf "%s\n" "$OCI_CLI_KEY" | wc -l)" -eq 1 ]; then
  135. OCI_CLI_KEY=$(printf "%s" "$OCI_CLI_KEY" | _dbase64)
  136. fi
  137. return 0
  138. }
  139. # _get_zone(): retrieves the Zone name and OCID
  140. #
  141. # _sub_domain=_acme-challenge.www
  142. # _domain=domain.com
  143. # _domain_ociid=ocid1.dns-zone.oc1..
  144. _get_zone() {
  145. domain=$1
  146. i=1
  147. p=1
  148. while true; do
  149. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  150. _debug h "$h"
  151. if [ -z "$h" ]; then
  152. # not valid
  153. return 1
  154. fi
  155. _domain_id=$(_signed_request "GET" "/20180115/zones/$h" "" "id")
  156. if [ "$_domain_id" ]; then
  157. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  158. _domain=$h
  159. _debug _domain_id "$_domain_id"
  160. _debug _sub_domain "$_sub_domain"
  161. _debug _domain "$_domain"
  162. return 0
  163. fi
  164. p=$i
  165. i=$(_math "$i" + 1)
  166. done
  167. return 1
  168. }
  169. #Usage: privatekey
  170. #Output MD5 fingerprint
  171. _fingerprint() {
  172. pkey="$1"
  173. if [ -z "$pkey" ]; then
  174. _usage "Usage: _fingerprint privkey"
  175. return 1
  176. fi
  177. printf "%s" "$pkey" | ${ACME_OPENSSL_BIN:-openssl} rsa -pubout -outform DER 2>/dev/null | ${ACME_OPENSSL_BIN:-openssl} md5 -c | cut -d = -f 2 | tr -d ' '
  178. }
  179. _signed_request() {
  180. _sig_method="$1"
  181. _sig_target="$2"
  182. _sig_body="$3"
  183. _return_field="$4"
  184. _key_fingerprint=$(_fingerprint "$OCI_CLI_KEY")
  185. _sig_host="dns.$OCI_CLI_REGION.oraclecloud.com"
  186. _sig_keyId="$OCI_CLI_TENANCY/$OCI_CLI_USER/$_key_fingerprint"
  187. _sig_alg="rsa-sha256"
  188. _sig_version="1"
  189. _sig_now="$(LC_ALL=C \date -u "+%a, %d %h %Y %H:%M:%S GMT")"
  190. _request_method=$(printf %s "$_sig_method" | _lower_case)
  191. _curl_method=$(printf %s "$_sig_method" | _upper_case)
  192. _request_target="(request-target): $_request_method $_sig_target"
  193. _date_header="date: $_sig_now"
  194. _host_header="host: $_sig_host"
  195. _string_to_sign="$_request_target\n$_date_header\n$_host_header"
  196. _sig_headers="(request-target) date host"
  197. if [ "$_sig_body" ]; then
  198. _secure_debug3 _sig_body "$_sig_body"
  199. _sig_body_sha256="x-content-sha256: $(printf %s "$_sig_body" | _digest sha256)"
  200. _sig_body_type="content-type: application/json"
  201. _sig_body_length="content-length: ${#_sig_body}"
  202. _string_to_sign="$_string_to_sign\n$_sig_body_sha256\n$_sig_body_type\n$_sig_body_length"
  203. _sig_headers="$_sig_headers x-content-sha256 content-type content-length"
  204. fi
  205. _tmp_file=$(_mktemp)
  206. if [ -f "$_tmp_file" ]; then
  207. printf '%s' "$OCI_CLI_KEY" >"$_tmp_file"
  208. _signature=$(printf '%b' "$_string_to_sign" | _sign "$_tmp_file" sha256 | tr -d '\r\n')
  209. rm -f "$_tmp_file"
  210. fi
  211. _signed_header="Authorization: Signature version=\"$_sig_version\",keyId=\"$_sig_keyId\",algorithm=\"$_sig_alg\",headers=\"$_sig_headers\",signature=\"$_signature\""
  212. _secure_debug3 _signed_header "$_signed_header"
  213. if [ "$_curl_method" = "GET" ]; then
  214. export _H1="$_date_header"
  215. export _H2="$_signed_header"
  216. _response="$(_get "https://${_sig_host}${_sig_target}")"
  217. elif [ "$_curl_method" = "PATCH" ]; then
  218. export _H1="$_date_header"
  219. export _H2="$_sig_body_sha256"
  220. export _H3="$_sig_body_type"
  221. export _H4="$_sig_body_length"
  222. export _H5="$_signed_header"
  223. _response="$(_post "$_sig_body" "https://${_sig_host}${_sig_target}" "" "PATCH")"
  224. else
  225. _err "Unable to process method: $_curl_method."
  226. fi
  227. _ret="$?"
  228. if [ "$_return_field" ]; then
  229. _response="$(echo "$_response" | sed 's/\\\"//g'))"
  230. _return=$(echo "${_response}" | _egrep_o "\"$_return_field\"\\s*:\\s*\"[^\"]*\"" | _head_n 1 | cut -d : -f 2 | tr -d "\"")
  231. else
  232. _return="$_response"
  233. fi
  234. printf "%s" "$_return"
  235. return $_ret
  236. }
  237. # file key [section]
  238. _readini() {
  239. _file="$1"
  240. _key="$2"
  241. _section="${3:-DEFAULT}"
  242. _start_n=$(grep -n '\['"$_section"']' "$_file" | cut -d : -f 1)
  243. _debug3 _start_n "$_start_n"
  244. if [ -z "$_start_n" ]; then
  245. _err "Can not find section: $_section"
  246. return 1
  247. fi
  248. _start_nn=$(_math "$_start_n" + 1)
  249. _debug3 "_start_nn" "$_start_nn"
  250. _left="$(sed -n "${_start_nn},99999p" "$_file")"
  251. _debug3 _left "$_left"
  252. _end="$(echo "$_left" | grep -n "^\[" | _head_n 1)"
  253. _debug3 "_end" "$_end"
  254. if [ "$_end" ]; then
  255. _end_n=$(echo "$_end" | cut -d : -f 1)
  256. _debug3 "_end_n" "$_end_n"
  257. _seg_n=$(echo "$_left" | sed -n "1,${_end_n}p")
  258. else
  259. _seg_n="$_left"
  260. fi
  261. _debug3 "_seg_n" "$_seg_n"
  262. _lineini="$(echo "$_seg_n" | grep "^ *$_key *= *")"
  263. _inivalue="$(printf "%b" "$(eval "echo $_lineini | sed \"s/^ *${_key} *= *//g\"")")"
  264. _debug2 _inivalue "$_inivalue"
  265. echo "$_inivalue"
  266. }