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.

150 lines
4.2 KiB

  1. #!/usr/bin/env sh
  2. #Script to use with curanet.dk, scannet.dk, wannafind.dk, dandomain.dk DNS management.
  3. #Author: Peter L. Hansen <peter@r12.dk>
  4. CURANET_REST_URL="https://api.curanet.dk/dns/v1/Domains"
  5. CURANET_AUTH_URL="https://apiauth.dk.team.blue/auth/realms/Curanet/protocol/openid-connect/token"
  6. CURANET_ACCESS_TOKEN=""
  7. ######## Public functions #####################
  8. #Usage: dns_curanet_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  9. dns_curanet_add() {
  10. fulldomain=$1
  11. txtvalue=$2
  12. _info "Using curanet"
  13. _debug fulldomain "$fulldomain"
  14. _debug txtvalue "$txtvalue"
  15. CURANET_AUTHCLIENTID="${CURANET_AUTHCLIENTID:-$(_readaccountconf_mutable CURANET_AUTHCLIENTID)}"
  16. CURANET_AUTHSECRET="${CURANET_AUTHSECRET:-$(_readaccountconf_mutable CURANET_AUTHSECRET)}"
  17. if [ -z "$CURANET_AUTHCLIENTID" ] || [ -z "$CURANET_AUTHSECRET" ]; then
  18. CURANET_AUTHCLIENTID=""
  19. CURANET_AUTHSECRET=""
  20. _err "You don't specify curanet api client and secret."
  21. _err "Please create your auth info and try again."
  22. return 1
  23. fi
  24. #save the credentials to the account conf file.
  25. _saveaccountconf_mutable CURANET_AUTHCLIENTID "$CURANET_AUTHCLIENTID"
  26. _saveaccountconf_mutable CURANET_AUTHSECRET "$CURANET_AUTHSECRET"
  27. gettoken
  28. _get_root "$fulldomain"
  29. export _H1="Content-Type: application/json-patch+json"
  30. export _H2="Accept: application/json"
  31. export _H3="Authorization: Bearer $CURANET_ACCESS_TOKEN"
  32. data="{\"name\": \"$fulldomain\",\"type\": \"TXT\",\"ttl\": 60,\"priority\": 0,\"data\": \"$txtvalue\"}"
  33. response="$(_post "$data" "$CURANET_REST_URL/${_domain}/Records" "" "")"
  34. if _contains "$response" "$txtvalue"; then
  35. _debug "TXT record added OK"
  36. else
  37. _err "Unable to add TXT record"
  38. return 1
  39. fi
  40. return 0
  41. }
  42. #Usage: fulldomain txtvalue
  43. #Remove the txt record after validation.
  44. dns_curanet_rm() {
  45. fulldomain=$1
  46. txtvalue=$2
  47. _info "Using curanet"
  48. _debug fulldomain "$fulldomain"
  49. _debug txtvalue "$txtvalue"
  50. CURANET_AUTHCLIENTID="${CURANET_AUTHCLIENTID:-$(_readaccountconf_mutable CURANET_AUTHCLIENTID)}"
  51. CURANET_AUTHSECRET="${CURANET_AUTHSECRET:-$(_readaccountconf_mutable CURANET_AUTHSECRET)}"
  52. gettoken
  53. _get_root "$fulldomain"
  54. _debug "Getting current record list to identify TXT to delete"
  55. export _H1="Content-Type: application/json"
  56. export _H2="Accept: application/json"
  57. export _H3="Authorization: Bearer $CURANET_ACCESS_TOKEN"
  58. response="$(_get "$CURANET_REST_URL/${_domain}/Records" "" "")"
  59. if ! _contains "$response" "$txtvalue"; then
  60. _err "Unable to delete record (does not contain $txtvalue )"
  61. return 1
  62. fi
  63. recordid=$(echo "$response" | _egrep_o "{\"id\":[0-9]+,\"name\":\"$fulldomain\"" | _egrep_o "id\":[0-9]+" | cut -c 5-)
  64. re='^[0-9]+$'
  65. if ! [[ $recordid =~ $re ]] ; then
  66. err "Unable to delete record (did not find recordID to delete)"
  67. return 1
  68. fi
  69. _debug "Deleting recordID $recordid"
  70. response="$(_post "" "$CURANET_REST_URL/${_domain}/Records/$recordid" "" "DELETE")"
  71. return 0;
  72. }
  73. #################### Private functions below ##################################
  74. gettoken() {
  75. response="$(_post "grant_type=client_credentials&client_id=$CURANET_AUTHCLIENTID&client_secret=$CURANET_AUTHSECRET&scope=dns" "$CURANET_AUTH_URL" "" "")"
  76. if ! _contains "$response" "access_token"; then
  77. _err "Unable get access token"
  78. return 1
  79. fi
  80. CURANET_ACCESS_TOKEN=$(echo "$response" | _egrep_o "\"access_token\":\"[^\"]+\"" | cut -c 17-)
  81. CURANET_ACCESS_TOKEN=${CURANET_ACCESS_TOKEN::-1}
  82. }
  83. #_acme-challenge.www.domain.com
  84. #returns
  85. # _sub_domain=_acme-challenge.www
  86. # _domain=domain.com
  87. # _domain_id=sdjkglgdfewsdfg
  88. _get_root() {
  89. domain=$1
  90. i=1
  91. p=1
  92. while true; do
  93. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  94. _debug h "$h"
  95. if [ -z "$h" ]; then
  96. #not valid
  97. return 1
  98. fi
  99. export _H1="Content-Type: application/json"
  100. export _H2="Accept: application/json"
  101. export _H3="Authorization: Bearer $CURANET_ACCESS_TOKEN"
  102. response="$(_get "$CURANET_REST_URL/$h/Records" "" "")"
  103. if [ ! "$(echo "$response" | _egrep_o "Entity not found")" ]; then
  104. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  105. _domain=$h
  106. return 0
  107. fi
  108. p=$i
  109. i=$(_math "$i" + 1)
  110. done
  111. return 1
  112. }