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.

162 lines
4.7 KiB

4 years ago
  1. #!/usr/bin/env sh
  2. # WEDOS_Auth sha1($login.sha1($wpass).date('H', time()));
  3. # TZ="Europe/Prague"
  4. #
  5. # Provide either Pass or Hash, hash'll be stored for renewal
  6. # WEDOS_User = test@test
  7. # WEDOS_Pass = test123
  8. # WEDOS_Hash = sha1(WEDOS_Pass)
  9. WEDOS_Api="https://api.wedos.com/wapi/json"
  10. WEDOS_Hour=$(TZ="CET-1CEST,M3.5.0,M10.5.0/3" date +%H)
  11. ######## Public functions #####################
  12. # Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  13. # Used to add txt record
  14. dns_wedos_add() {
  15. fulldomain=$1
  16. txtvalue=$2
  17. _wedos_init
  18. _info "Adding txt record"
  19. response=
  20. if _wedos_rest '{ "request": {"user": "'"$WEDOS_User"'", "auth": "'"$WEDOS_Token"'", "command": "dns-row-add", "data": { "domain": "'"$_domain"'", "name": "'"$_sub_domain"'", "ttl": "600", "type": "TXT", "rdata": "'"$txtvalue"'" }}}'; then
  21. _info "Added, OK"
  22. if _wedos_rest '{ "request": {"user": "'"$WEDOS_User"'", "auth": "'"$WEDOS_Token"'", "command": "dns-domain-commit", "data": { "name": "'"$_domain"'" }}}'; then
  23. _info "Commit changes, OK"
  24. return 0
  25. else
  26. _err "Commit changes, Error"
  27. return 1
  28. fi
  29. return 0
  30. fi
  31. _err "Adding txt record, Error"
  32. return 1
  33. }
  34. # Usage: fulldomain txtvalue
  35. # Used to remove the txt record after validation
  36. dns_wedos_rm() {
  37. fulldomain=$1
  38. txtvalue=$2
  39. _wedos_init
  40. _debug "Getting txt records"
  41. response=
  42. if _wedos_rest '{ "request": {"user": "'"$WEDOS_User"'", "auth": "'"$WEDOS_Token"'", "command": "dns-rows-list", "data": { "domain": "'"$_domain"'" }}}'; then
  43. domain_info=$(printf "%s\n" "$response" | _egrep_o '\"ID\":\"[0-9]+\",\"name\":\"'"$_sub_domain"'\",\"ttl\":\"[0-9]+\",\"rdtype\":\"TXT\",\"rdata\":\"'"$txtvalue"'\"')
  44. domain_id=$(printf "%s\n" "$domain_info" | _egrep_o '\"ID\":\"[0-9]+\"' | _egrep_o '[0-9]+')
  45. _debug "Found txt record: $domain_info"
  46. _debug "ID: $domain_id"
  47. if _wedos_rest '{ "request": {"user": "'"$WEDOS_User"'", "auth": "'"$WEDOS_Token"'", "command": "dns-row-detail", "data": { "domain": "'"$_domain"'", "row_id": "'"$domain_id"'" }}}'; then
  48. checked_domain_info=$(printf "%s\n" "$response" | _egrep_o '\"ID\":\"[0-9]+\",\"name\":\"'"$_sub_domain"'\"')
  49. checked_domain_id=$(printf "%s\n" "$checked_domain_info" | _egrep_o '\"[0-9]+\"')
  50. if _wedos_rest '{ "request": {"user": "'"$WEDOS_User"'", "auth": "'"$WEDOS_Token"'", "command": "dns-row-delete", "data": { "domain": "'"$_domain"'", "row_id": '"$checked_domain_id"' }}}'; then
  51. _info "Txt record id: $checked_domain_id deleted."
  52. return 0
  53. fi
  54. fi
  55. fi
  56. _err "Deleting txt record, Error"
  57. return 1
  58. }
  59. #################### Private functions below ##################################
  60. _wedos_rest() {
  61. _data=$(printf "%s" "$1" | _url_encode)
  62. _body_url="request="$_data
  63. _debug "$_data"
  64. _debug "$_body_url"
  65. # body url [needbase64] [POST|PUT|DELETE] [ContentType]
  66. response=$(_post "$_body_url" $WEDOS_Api "" "POST" "application/x-www-form-urlencoded")
  67. _debug "$response"
  68. if _contains "$response" '"code":1000'; then
  69. return 0
  70. fi
  71. _err "Rest request, Error"
  72. return 1
  73. }
  74. #_acme-challenge.www.domain.com
  75. #returns
  76. # _sub_domain=_acme-challenge.www
  77. # _domain=domain.com
  78. # _domain_id=sdjkglgdfewsdfg
  79. _get_root() {
  80. domain=$1
  81. if ! _wedos_rest '{ "request": {"user": "'"$WEDOS_User"'", "auth": "'"$WEDOS_Token"'", "command": "dns-domains-list" }}'; then
  82. return 1
  83. fi
  84. i=2
  85. p=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. if _contains "$response" "\"$h\"" >/dev/null; then
  94. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  95. _domain=$h
  96. return 0
  97. fi
  98. p=$i
  99. i=$(_math "$i" + 1)
  100. done
  101. return 1
  102. }
  103. _wedos_init() {
  104. WEDOS_Hash="${WEDOS_Hash:-$(_readaccountconf_mutable WEDOS_Hash)}"
  105. WEDOS_User="${WEDOS_User:-$(_readaccountconf_mutable WEDOS_User)}"
  106. if [ -n "$WEDOS_Pass" ] && [ -z "$WEDOS_Hash" ]; then
  107. WEDOS_Hash=$(printf "%s" "$WEDOS_Pass" | _digest sha1 1)
  108. fi
  109. if [ -z "$WEDOS_Hash" ]; then
  110. _err "You didn't specify a wedos hash or password yet."
  111. _err "Please create hash or password and try again."
  112. return 1
  113. fi
  114. _saveaccountconf_mutable WEDOS_Hash "$WEDOS_Hash"
  115. if [ -z "$WEDOS_User" ]; then
  116. WEDOS_User=""
  117. _err "You didn't specify a wedos username yet."
  118. _err "Please create user and try again."
  119. fi
  120. _saveaccountconf_mutable WEDOS_User "$WEDOS_User"
  121. WEDOS_Token=$(printf "%s" "$WEDOS_User$WEDOS_Hash$WEDOS_Hour" | _digest sha1 1)
  122. _debug "First detect the root zone"
  123. if ! _get_root "$fulldomain"; then
  124. _err "invalid domain"
  125. return 1
  126. fi
  127. _debug _sub_domain "$_sub_domain"
  128. _debug _domain "$_domain"
  129. }