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.

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