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.

130 lines
3.9 KiB

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