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.

168 lines
4.6 KiB

  1. #!/usr/bin/env sh
  2. #Author: Herman Sletteng
  3. #Report Bugs here: https://github.com/loial/acme.sh
  4. #
  5. #
  6. # Note, gratisdns requires a login first, so the script needs to handle
  7. # temporary cookies. Since acme.sh _get/_post currently don't directly support
  8. # cookies, I've defined wrapper functions _myget/_mypost to set the headers
  9. GDNSDK_API="https://admin.gratisdns.com"
  10. ######## Public functions #####################
  11. #Usage: dns_gdnsdk_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  12. dns_gdnsdk_add() {
  13. fulldomain=$1
  14. txtvalue=$2
  15. _info "Using gratisdns.dk"
  16. _debug fulldomain "$fulldomain"
  17. _debug txtvalue "$txtvalue"
  18. if ! _gratisdns_login; then
  19. _err "Login failed!"
  20. return 1
  21. fi
  22. #finding domain zone
  23. if ! _get_domain; then
  24. _err "No matching root domain for $fulldomain found"
  25. return 1
  26. fi
  27. # adding entry
  28. _info "Adding the entry"
  29. _mypost "action=dns_primary_record_added_txt&user_domain=$_domain&name=$fulldomain&txtdata=$txtvalue&ttl=1"
  30. if _successful_update; then return 0; fi
  31. _err "Couldn't create entry!"
  32. return 1
  33. }
  34. #Usage: fulldomain txtvalue
  35. #Remove the txt record after validation.
  36. dns_gdnsdk_rm() {
  37. fulldomain=$1
  38. txtvalue=$2
  39. _info "Using gratisdns.dk"
  40. _debug fulldomain "$fulldomain"
  41. _debug txtvalue "$txtvalue"
  42. if ! _gratisdns_login; then
  43. _err "Login failed!"
  44. return 1
  45. fi
  46. if ! _get_domain; then
  47. _err "No matching root domain for $fulldomain found"
  48. return 1
  49. fi
  50. _findentry "$fulldomain" "$txtvalue"
  51. if [ -z "$_id" ]; then
  52. _info "Entry doesn't exist, nothing to delete"
  53. return 0
  54. fi
  55. _debug "Deleting record..."
  56. _mypost "action=dns_primary_delete_txt&user_domain=$_domain&id=$_id"
  57. # removing entry
  58. if _successful_update; then return 0; fi
  59. _err "Couldn't delete entry!"
  60. return 1
  61. }
  62. #################### Private functions below ##################################
  63. _checkcredentials() {
  64. GDNSDK_Username="${GDNSDK_Username:-$(_readaccountconf_mutable GDNSDK_Username)}"
  65. GDNSDK_Password="${GDNSDK_Password:-$(_readaccountconf_mutable GDNSDK_Password)}"
  66. if [ -z "$GDNSDK_Username" ] || [ -z "$GDNSDK_Password" ]; then
  67. GDNSDK_Username=""
  68. GDNSDK_Password=""
  69. _err "You haven't specified gratisdns.dk username and password yet."
  70. _err "Please add credentials and try again."
  71. return 1
  72. fi
  73. #save the credentials to the account conf file.
  74. _saveaccountconf_mutable GDNSDK_Username "$GDNSDK_Username"
  75. _saveaccountconf_mutable GDNSDK_Password "$GDNSDK_Password"
  76. return 0
  77. }
  78. _checkcookie() {
  79. GDNSDK_Cookie="${GDNSDK_Cookie:-$(_readaccountconf_mutable GDNSDK_Cookie)}"
  80. if [ -z "$GDNSDK_Cookie" ]; then
  81. _debug "No cached cookie found"
  82. return 1
  83. fi
  84. _myget "action="
  85. if (echo "$_result" | grep -q "logmeout"); then
  86. _debug "Cached cookie still valid"
  87. return 0
  88. fi
  89. _debug "Cached cookie no longer valid"
  90. GDNSDK_Cookie=""
  91. _saveaccountconf_mutable GDNSDK_Cookie "$GDNSDK_Cookie"
  92. return 1
  93. }
  94. _gratisdns_login() {
  95. if ! _checkcredentials; then return 1; fi
  96. if _checkcookie; then
  97. _debug "Already logged in"
  98. return 0
  99. fi
  100. _debug "Logging into GratisDNS with user $GDNSDK_Username"
  101. if ! _mypost "login=$GDNSDK_Username&password=$GDNSDK_Password&action=logmein"; then
  102. _err "GratisDNS login failed for user $GDNSDK_Username bad RC from _post"
  103. return 1
  104. fi
  105. GDNSDK_Cookie="$(grep -A 15 '302 Found' "$HTTP_HEADER" | _egrep_o 'Cookie: [^;]*' | _head_n 1 | cut -d ' ' -f2)"
  106. if [ -z "$GDNSDK_Cookie" ]; then
  107. _err "GratisDNS login failed for user $GDNSDK_Username. Check $HTTP_HEADER file"
  108. return 1
  109. fi
  110. export GDNSDK_Cookie
  111. _saveaccountconf_mutable GDNSDK_Cookie "$GDNSDK_Cookie"
  112. return 0
  113. }
  114. _myget() {
  115. #Adds cookie to request
  116. export _H1="Cookie: $GDNSDK_Cookie"
  117. _result=$(_get "$GDNSDK_API?$1")
  118. }
  119. _mypost() {
  120. #Adds cookie to request
  121. export _H1="Cookie: $GDNSDK_Cookie"
  122. _result=$(_post "$1" "$GDNSDK_API")
  123. }
  124. _get_domain() {
  125. _myget 'action=dns_primarydns'
  126. _domains=$(echo "$_result" | grep -o -P ' domain="\K([[:alnum:].-_]+)')
  127. if [ -z "$_domains" ]; then
  128. _err "Primary domain list not found!"
  129. return 1
  130. fi
  131. for _domain in $_domains; do
  132. if (_endswith "$fulldomain" "$_domain"); then
  133. _debug "Root domain: $_domain"
  134. return 0
  135. fi
  136. done
  137. return 1
  138. }
  139. _successful_update() {
  140. if (echo "$_result" | grep -q 'table-success'); then return 0; fi
  141. return 1
  142. }
  143. _findentry() {
  144. #returns id of dns entry, if it exists
  145. _myget "action=dns_primary_changeDNSsetup&user_domain=$_domain"
  146. _id=$(echo "$_result" | grep -o -P "$1</td>\s*<td>$2.*?id=\K(\d*)")
  147. if [ -n "$_id" ]; then
  148. _debug "Entry found with _id=$_id"
  149. return 0
  150. fi
  151. return 1
  152. }