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.

255 lines
7.9 KiB

  1. #!/usr/bin/env sh
  2. CONOHA_DNS_EP_PREFIX_REGEXP="https://dns-service\."
  3. ######## Public functions #####################
  4. #Usage: dns_conoha_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  5. dns_conoha_add() {
  6. fulldomain=$1
  7. txtvalue=$2
  8. _info "Using conoha"
  9. _debug fulldomain "$fulldomain"
  10. _debug txtvalue "$txtvalue"
  11. _debug "Check uesrname and password"
  12. CONOHA_Username="${CONOHA_Username:-$(_readaccountconf_mutable CONOHA_Username)}"
  13. CONOHA_Password="${CONOHA_Password:-$(_readaccountconf_mutable CONOHA_Password)}"
  14. CONOHA_TenantId="${CONOHA_TenantId:-$(_readaccountconf_mutable CONOHA_TenantId)}"
  15. CONOHA_IdentityServiceApi="${CONOHA_IdentityServiceApi:-$(_readaccountconf_mutable CONOHA_IdentityServiceApi)}"
  16. if [ -z "$CONOHA_Username" ] || [ -z "$CONOHA_Password" ] || [ -z "$CONOHA_TenantId" ] || [ -z "$CONOHA_IdentityServiceApi" ]; then
  17. CONOHA_Username=""
  18. CONOHA_Password=""
  19. CONOHA_TenantId=""
  20. CONOHA_IdentityServiceApi=""
  21. _err "You didn't specify a conoha api username and password yet."
  22. _err "Please create the user and try again."
  23. return 1
  24. fi
  25. _saveaccountconf_mutable CONOHA_Username "$CONOHA_Username"
  26. _saveaccountconf_mutable CONOHA_Password "$CONOHA_Password"
  27. _saveaccountconf_mutable CONOHA_TenantId "$CONOHA_TenantId"
  28. _saveaccountconf_mutable CONOHA_IdentityServiceApi "$CONOHA_IdentityServiceApi"
  29. if set -- $(_conoha_get_accesstoken "$CONOHA_IdentityServiceApi/tokens" "$CONOHA_Username" "$CONOHA_Password" "$CONOHA_TenantId"); then
  30. accesstoken=$1
  31. CONOHA_Api=$2
  32. else
  33. return 1
  34. fi
  35. #return 1 #XXX
  36. _debug "First detect the root zone"
  37. if ! _get_root "$fulldomain" "$CONOHA_Api" "$accesstoken"; then
  38. _err "invalid domain"
  39. return 1
  40. fi
  41. _debug _domain_id "$_domain_id"
  42. _debug _sub_domain "$_sub_domain"
  43. _debug _domain "$_domain"
  44. #return 1 #XXX
  45. _info "Adding record"
  46. body="{\"type\":\"TXT\",\"name\":\"$fulldomain.\",\"data\":\"$txtvalue\",\"ttl\":60}"
  47. if _conoha_rest POST "$CONOHA_Api/v1/domains/$_domain_id/records" "$body" "$accesstoken"; then
  48. if _contains "$response" '"data":"'"$txtvalue"'"'; then
  49. _info "Added, OK"
  50. return 0
  51. else
  52. _err "Add txt record error."
  53. return 1
  54. fi
  55. fi
  56. _err "Add txt record error."
  57. return 1
  58. }
  59. #Usage: fulldomain txtvalue
  60. #Remove the txt record after validation.
  61. dns_conoha_rm() {
  62. fulldomain=$1
  63. txtvalue=$2
  64. _info "Using conoha"
  65. _debug fulldomain "$fulldomain"
  66. _debug txtvalue "$txtvalue"
  67. _debug "Check uesrname and password"
  68. CONOHA_Username="${CONOHA_Username:-$(_readaccountconf_mutable CONOHA_Username)}"
  69. CONOHA_Password="${CONOHA_Password:-$(_readaccountconf_mutable CONOHA_Password)}"
  70. CONOHA_TenantId="${CONOHA_TenantId:-$(_readaccountconf_mutable CONOHA_TenantId)}"
  71. CONOHA_IdentityServiceApi="${CONOHA_IdentityServiceApi:-$(_readaccountconf_mutable CONOHA_IdentityServiceApi)}"
  72. if [ -z "$CONOHA_Username" ] || [ -z "$CONOHA_Password" ] || [ -z "$CONOHA_TenantId" ] || [ -z "$CONOHA_IdentityServiceApi" ]; then
  73. CONOHA_Username=""
  74. CONOHA_Password=""
  75. CONOHA_TenantId=""
  76. CONOHA_IdentityServiceApi=""
  77. _err "You didn't specify a conoha api username and password yet."
  78. _err "Please create the user and try again."
  79. return 1
  80. fi
  81. _saveaccountconf_mutable CONOHA_Username "$CONOHA_Username"
  82. _saveaccountconf_mutable CONOHA_Password "$CONOHA_Password"
  83. _saveaccountconf_mutable CONOHA_TenantId "$CONOHA_TenantId"
  84. _saveaccountconf_mutable CONOHA_IdentityServiceApi "$CONOHA_IdentityServiceApi"
  85. if set -- $(_conoha_get_accesstoken "$CONOHA_IdentityServiceApi/tokens" "$CONOHA_Username" "$CONOHA_Password" "$CONOHA_TenantId"); then
  86. accesstoken=$1
  87. CONOHA_Api=$2
  88. else
  89. return 1
  90. fi
  91. _debug "First detect the root zone"
  92. if ! _get_root "$fulldomain" "$CONOHA_Api" "$accesstoken"; then
  93. _err "invalid domain"
  94. return 1
  95. fi
  96. _debug _domain_id "$_domain_id"
  97. _debug _sub_domain "$_sub_domain"
  98. _debug _domain "$_domain"
  99. _debug "Getting txt records"
  100. if ! _conoha_rest GET "$CONOHA_Api/v1/domains/$_domain_id/records" "" "$accesstoken"; then
  101. _err "Error"
  102. return 1
  103. fi
  104. record_id=$(printf "%s" "$response" | _egrep_o '{[^}]*}' |
  105. grep '"type":"TXT"' | grep "\"data\":\"$txtvalue\"" | _egrep_o "\"id\":\"[^\"]*\"" |
  106. _head_n 1 | cut -d : -f 2 | tr -d \")
  107. if [ -z "$record_id" ]; then
  108. _err "Can not get record id to remove."
  109. return 1
  110. fi
  111. _debug record_id "$record_id"
  112. _info "Removing the txt record"
  113. if ! _conoha_rest DELETE "$CONOHA_Api/v1/domains/$_domain_id/records/$record_id" "" "$accesstoken"; then
  114. _err "Delete record error."
  115. return 1
  116. fi
  117. return 0
  118. }
  119. #################### Private functions below ##################################
  120. _conoha_rest() {
  121. m="$1"
  122. ep="$2"
  123. data="$3"
  124. accesstoken="$4"
  125. export _H1="Accept: application/json"
  126. export _H2="Content-Type: application/json"
  127. if [ -n "$accesstoken" ]; then
  128. export _H3="X-Auth-Token: $accesstoken"
  129. fi
  130. _debug "$ep"
  131. if [ "$m" != "GET" ]; then
  132. _secure_debug2 data "$data"
  133. response="$(_post "$data" "$ep" "" "$m")"
  134. else
  135. response="$(_get "$ep")"
  136. fi
  137. _ret="$?"
  138. _secure_debug2 response "$response"
  139. if [ "$_ret" != "0" ]; then
  140. _err "error $ep"
  141. return 1
  142. fi
  143. response="$(printf "%s" "$response" | _normalizeJson)"
  144. return 0
  145. }
  146. _conoha_get_accesstoken() {
  147. ep="$1"
  148. username="$2"
  149. password="$3"
  150. tenantId="$4"
  151. accesstoken="$(_readaccountconf_mutable conoha_accesstoken)"
  152. expires="$(_readaccountconf_mutable conoha_tokenvalidto)"
  153. CONOHA_Api="$(_readaccountconf_mutable conoha_dns_ep)"
  154. # can we reuse the access token?
  155. if [ -n "$accesstoken" ] && [ -n "$expires" ] && [ -n "$CONOHA_Api" ]; then
  156. utc_date="$(_utc_date | sed "s/ /T/")"
  157. if expr "$utc_date" "<" "$expires" >/dev/null; then
  158. # access token is still valid - reuse it
  159. _debug "reusing access token"
  160. printf "%s\n%s" "$accesstoken" "$CONOHA_Api"
  161. return 0
  162. else
  163. _debug "access token expired"
  164. fi
  165. fi
  166. _debug "getting new access token"
  167. body="$(printf '{"auth":{"passwordCredentials":{"username":"%s","password":"%s"},"tenantId":"%s"}}' "$username" "$password" "$tenantId")"
  168. if ! _conoha_rest POST "$ep" "$body" ""; then
  169. _err error "$response"
  170. return 1
  171. fi
  172. accesstoken=$(printf "%s" "$response" | _egrep_o "\"id\":\"[^\"]*\"" | _head_n 1 | cut -d : -f 2 | tr -d \")
  173. expires=$(printf "%s" "$response" | _egrep_o "\"expires\":\"[^\"]*\"" | _head_n 1 | cut -d : -f 2-4 | tr -d \" | tr -d Z) #expect UTC
  174. if [ -z "$accesstoken" ] || [ -z "$expires" ]; then
  175. _err "no acccess token received. Check your Conoha settings see $WIKI"
  176. return 1
  177. fi
  178. _saveaccountconf_mutable conoha_accesstoken "$accesstoken"
  179. _saveaccountconf_mutable conoha_tokenvalidto "$expires"
  180. CONOHA_Api=$(printf "%s" "$response" | _egrep_o 'publicURL":"'"$CONOHA_DNS_EP_PREFIX_REGEXP"'[^"]*"' | _head_n 1 | cut -d : -f 2-3 | tr -d \")
  181. if [ -z "$CONOHA_Api" ]; then
  182. _err "failed to get conoha dns endpoint url"
  183. return 1
  184. fi
  185. _saveaccountconf_mutable conoha_dns_ep "$CONOHA_Api"
  186. printf "%s\n%s" "$accesstoken" "$CONOHA_Api"
  187. return 0
  188. }
  189. #_acme-challenge.www.domain.com
  190. #returns
  191. # _sub_domain=_acme-challenge.www
  192. # _domain=domain.com
  193. # _domain_id=sdjkglgdfewsdfg
  194. _get_root() {
  195. domain="$1"
  196. ep="$2"
  197. accesstoken="$3"
  198. i=2
  199. p=1
  200. while true; do
  201. h=$(printf "%s" "$domain" | cut -d . -f $i-100).
  202. _debug h "$h"
  203. if [ -z "$h" ]; then
  204. #not valid
  205. return 1
  206. fi
  207. if ! _conoha_rest GET "$ep/v1/domains?name=$h" "" "$accesstoken"; then
  208. return 1
  209. fi
  210. if _contains "$response" "\"name\":\"$h\"" >/dev/null; then
  211. _domain_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":\"[^\"]*\"" | head -n 1 | cut -d : -f 2 | tr -d \")
  212. if [ "$_domain_id" ]; then
  213. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  214. _domain=$h
  215. return 0
  216. fi
  217. return 1
  218. fi
  219. p=$i
  220. i=$(_math "$i" + 1)
  221. done
  222. return 1
  223. }