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.

190 lines
5.9 KiB

  1. #!/usr/bin/env sh
  2. # World4You - www.world4you.com
  3. # Lorenz Stechauner, 2020 - https://www.github.com/NerLOR
  4. WORLD4YOU_API="https://my.world4you.com/en"
  5. PAKETNR=''
  6. TLD=''
  7. RECORD=''
  8. ################ Public functions ################
  9. # Usage: dns_world4you_add <fqdn> <value>
  10. dns_world4you_add() {
  11. fqdn="$1"
  12. value="$2"
  13. _info "Using world4you"
  14. _debug fulldomain "$fqdn"
  15. _debug txtvalue "$value"
  16. _login
  17. if [ "$?" != 0 ]; then
  18. return 1
  19. fi
  20. export _H1="Cookie: W4YSESSID=$sessid"
  21. form=$(_get "$WORLD4YOU_API/dashboard/paketuebersicht")
  22. _get_paketnr "$fqdn" "$form"
  23. paketnr="$PAKETNR"
  24. tld="$TLD"
  25. record="$RECORD"
  26. if [ -z "$paketnr" ]; then
  27. _err "Unable to parse paketnr"
  28. return 3
  29. fi
  30. _debug paketnr "$paketnr"
  31. export _H1="Cookie: W4YSESSID=$sessid"
  32. form=$(_get "$WORLD4YOU_API/$paketnr/dns")
  33. formiddp=$(echo "$form" | grep 'AddDnsRecordForm\[uniqueFormIdDP\]' | sed 's/^.*name="AddDnsRecordForm\[uniqueFormIdDP\]" value="\([^"]*\)".*$/\1/')
  34. formidttl=$(echo "$form" | grep 'AddDnsRecordForm\[uniqueFormIdTTL\]' | sed 's/^.*name="AddDnsRecordForm\[uniqueFormIdTTL\]" value="\([^"]*\)".*$/\1/')
  35. form_token=$(echo "$form" | grep 'AddDnsRecordForm\[_token\]' | sed 's/^.*name="AddDnsRecordForm\[_token\]" value="\([^"]*\)".*$/\1/')
  36. if [ -z "$formiddp" ]; then
  37. _err "Unable to parse form"
  38. return 3
  39. fi
  40. ACME_HTTP_NO_REDIRECTS=1
  41. _resethttp
  42. body="AddDnsRecordForm[name]=$record&AddDnsRecordForm[dnsType][type]=TXT&\
  43. AddDnsRecordForm[value]=$value&AddDnsRecordForm[aktivPaket]=$paketnr&AddDnsRecordForm[uniqueFormIdDP]=$formiddp&\
  44. AddDnsRecordForm[uniqueFormIdTTL]=$formidttl&AddDnsRecordForm[_token]=$form_token"
  45. _info "Adding record..."
  46. ret=$(_post "$body" "$WORLD4YOU_API/$paketnr/dns" '' POST 'application/x-www-form-urlencoded')
  47. unset ACME_HTTP_NO_REDIRECTS
  48. _resethttp
  49. if grep '302' >/dev/null <"$HTTP_HEADER"; then
  50. return 0
  51. else
  52. _err "$(head_n 1 <"$HTTP_HEADER")"
  53. return 2
  54. fi
  55. }
  56. # Usage: dns_world4you_rm <fqdn> <value>
  57. dns_world4you_rm() {
  58. fqdn="$1"
  59. value="$2"
  60. _info "Using world4you"
  61. _debug fulldomain "$fqdn"
  62. _debug txtvalue "$value"
  63. tld=$(echo "$fqdn" | _egrep_o '[^.]*\.[^.]*$')
  64. record=$(echo "$fqdn" | cut -c"1-$((${#fqdn} - ${#tld} - 1))")
  65. _login
  66. if [ "$?" != 0 ]; then
  67. return 1
  68. fi
  69. export _H1="Cookie: W4YSESSID=$sessid"
  70. form=$(_get "$WORLD4YOU_API/dashboard/paketuebersicht")
  71. _get_paketnr "$fqdn" "$form"
  72. paketnr="$PAKETNR"
  73. tld="$TLD"
  74. record="$RECORD"
  75. if [ -z "$paketnr" ]; then
  76. _err "Unable to parse paketnr"
  77. return 3
  78. fi
  79. _debug paketnr "$paketnr"
  80. form=$(_get "$WORLD4YOU_API/$paketnr/dns")
  81. formiddp=$(echo "$form" | grep 'DeleteDnsRecordForm\[uniqueFormIdDP\]' | sed 's/^.*name="DeleteDnsRecordForm\[uniqueFormIdDP\]" value="\([^"]*\)".*$/\1/')
  82. formidttl=$(echo "$form" | grep 'DeleteDnsRecordForm\[uniqueFormIdTTL\]' | sed 's/^.*name="DeleteDnsRecordForm\[uniqueFormIdTTL\]" value="\([^"]*\)".*$/\1/')
  83. form_token=$(echo "$form" | grep 'DeleteDnsRecordForm\[_token\]' | sed 's/^.*name="DeleteDnsRecordForm\[_token\]" value="\([^"]*\)".*$/\1/')
  84. if [ -z "$formiddp" ]; then
  85. _err "Unable to parse form"
  86. return 3
  87. fi
  88. recordid=$(printf "TXT:%s.:\"%s\"" "$fqdn" "$value" | _base64)
  89. _debug recordid "$recordid"
  90. ACME_HTTP_NO_REDIRECTS=1
  91. _resethttp
  92. body="DeleteDnsRecordForm[recordId]=$recordid&DeleteDnsRecordForm[aktivPaket]=$paketnr&\
  93. DeleteDnsRecordForm[uniqueFormIdDP]=$formiddp&DeleteDnsRecordForm[uniqueFormIdTTL]=$formidttl&\
  94. DeleteDnsRecordForm[_token]=$form_token"
  95. _info "Removing record..."
  96. ret=$(_post "$body" "$WORLD4YOU_API/$paketnr/deleteRecord" '' POST 'application/x-www-form-urlencoded')
  97. unset ACME_HTTP_NO_REDIRECTS
  98. _resethttp
  99. if grep '302' >/dev/null <"$HTTP_HEADER"; then
  100. return 0
  101. else
  102. _err "$(head_n 1 <"$HTTP_HEADER")"
  103. return 2
  104. fi
  105. }
  106. ################ Private functions ################
  107. # Usage: _login
  108. _login() {
  109. WORLD4YOU_USERNAME="${WORLD4YOU_USERNAME:-$(_readaccountconf_mutable WORLD4YOU_USERNAME)}"
  110. WORLD4YOU_PASSWORD="${WORLD4YOU_PASSWORD:-$(_readaccountconf_mutable WORLD4YOU_PASSWORD)}"
  111. if [ -z "$WORLD4YOU_USERNAME" ] || [ -z "$WORLD4YOU_PASSWORD" ]; then
  112. WORLD4YOU_USERNAME=""
  113. WORLD4YOU_PASSWORD=""
  114. _err "You didn't specify world4you username and password yet."
  115. _err "Usage: export WORLD4YOU_USERNAME=<name>"
  116. _err "Usage: export WORLD4YOU_PASSWORD=<password>"
  117. return 2
  118. fi
  119. _saveaccountconf_mutable WORLD4YOU_USERNAME "$WORLD4YOU_USERNAME"
  120. _saveaccountconf_mutable WORLD4YOU_PASSWORD "$WORLD4YOU_PASSWORD"
  121. _info "Logging in..."
  122. username="$WORLD4YOU_USERNAME"
  123. password="$WORLD4YOU_PASSWORD"
  124. csrf_token=$(_get "$WORLD4YOU_API/login" | grep '_csrf_token' | sed 's/^.*<input[^>]*value=\"\([^"]*\)\".*$/\1/')
  125. sessid=$(grep 'W4YSESSID' <"$HTTP_HEADER" | sed 's/^.*W4YSESSID=\([^;]*\);.*$/\1/')
  126. export _H1="Cookie: W4YSESSID=$sessid"
  127. export _H2="X-Requested-With: XMLHttpRequest"
  128. body="_username=$username&_password=$password&_csrf_token=$csrf_token"
  129. ret=$(_post "$body" "$WORLD4YOU_API/login" '' POST 'application/x-www-form-urlencoded')
  130. unset _H2
  131. _debug ret "$ret"
  132. if _contains "$ret" "\"success\":true"; then
  133. _info "Successfully logged in"
  134. sessid=$(grep 'W4YSESSID' <"$HTTP_HEADER" | sed 's/^.*W4YSESSID=\([^;]*\);.*$/\1/')
  135. else
  136. _err "Unable to log in: $(echo "$ret" | sed 's/^.*"message":"\([^\"]*\)".*$/\1/')"
  137. return 1
  138. fi
  139. }
  140. # Usage _get_paketnr <fqdn> <form>
  141. _get_paketnr() {
  142. fqdn="$1"
  143. form="$2"
  144. domains=$(echo "$form" | _egrep_o '^\s*([A-Za-z0-9_-]+\.)+[A-Za-z0-9_-]*$' | sed 's/^\s*\(\S*\)$/\1/')
  145. domain=''
  146. for domain in $domains; do
  147. if echo "$fqdn" | grep "$domain\$" >/dev/null; then
  148. break
  149. fi
  150. domain=''
  151. done
  152. if [ -z "$domain" ]; then
  153. return 1
  154. fi
  155. TLD="$domain"
  156. RECORD=$(echo "$fqdn" | cut -c"1-$((${#fqdn} - ${#TLD} - 1))")
  157. PAKETNR=$(echo "$form" | _egrep_o "data-textfilter=\" $domain .* [0-9]*" | head_n 1 | _egrep_o "[0-9]*")
  158. return 0
  159. }