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.

181 lines
5.7 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. _resethttp
  41. export ACME_HTTP_NO_REDIRECTS=1
  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. _resethttp
  48. if grep '302' >/dev/null <"$HTTP_HEADER"; then
  49. return 0
  50. else
  51. _err "$(_head_n 1 <"$HTTP_HEADER")"
  52. return 2
  53. fi
  54. }
  55. # Usage: dns_world4you_rm <fqdn> <value>
  56. dns_world4you_rm() {
  57. fqdn="$1"
  58. value="$2"
  59. _info "Using world4you"
  60. _debug fulldomain "$fqdn"
  61. _debug txtvalue "$value"
  62. _login
  63. if [ "$?" != 0 ]; then
  64. return 1
  65. fi
  66. export _H1="Cookie: W4YSESSID=$sessid"
  67. form=$(_get "$WORLD4YOU_API/dashboard/paketuebersicht")
  68. _get_paketnr "$fqdn" "$form"
  69. paketnr="$PAKETNR"
  70. tld="$TLD"
  71. record="$RECORD"
  72. if [ -z "$paketnr" ]; then
  73. _err "Unable to parse paketnr"
  74. return 3
  75. fi
  76. _debug paketnr "$paketnr"
  77. form=$(_get "$WORLD4YOU_API/$paketnr/dns")
  78. formiddp=$(echo "$form" | grep 'DeleteDnsRecordForm\[uniqueFormIdDP\]' | sed 's/^.*name="DeleteDnsRecordForm\[uniqueFormIdDP\]" value="\([^"]*\)".*$/\1/')
  79. formidttl=$(echo "$form" | grep 'DeleteDnsRecordForm\[uniqueFormIdTTL\]' | sed 's/^.*name="DeleteDnsRecordForm\[uniqueFormIdTTL\]" value="\([^"]*\)".*$/\1/')
  80. form_token=$(echo "$form" | grep 'DeleteDnsRecordForm\[_token\]' | sed 's/^.*name="DeleteDnsRecordForm\[_token\]" value="\([^"]*\)".*$/\1/')
  81. if [ -z "$formiddp" ]; then
  82. _err "Unable to parse form"
  83. return 3
  84. fi
  85. recordid=$(printf "TXT:%s.:\"%s\"" "$fqdn" "$value" | _base64)
  86. _debug recordid "$recordid"
  87. _resethttp
  88. export ACME_HTTP_NO_REDIRECTS=1
  89. body="DeleteDnsRecordForm[recordId]=$recordid&DeleteDnsRecordForm[aktivPaket]=$paketnr&\
  90. DeleteDnsRecordForm[uniqueFormIdDP]=$formiddp&DeleteDnsRecordForm[uniqueFormIdTTL]=$formidttl&\
  91. DeleteDnsRecordForm[_token]=$form_token"
  92. _info "Removing record..."
  93. ret=$(_post "$body" "$WORLD4YOU_API/$paketnr/deleteRecord" '' POST 'application/x-www-form-urlencoded')
  94. _resethttp
  95. if grep '302' >/dev/null <"$HTTP_HEADER"; then
  96. return 0
  97. else
  98. _err "$(_head_n 1 <"$HTTP_HEADER")"
  99. return 2
  100. fi
  101. }
  102. ################ Private functions ################
  103. # Usage: _login
  104. _login() {
  105. WORLD4YOU_USERNAME="${WORLD4YOU_USERNAME:-$(_readaccountconf_mutable WORLD4YOU_USERNAME)}"
  106. WORLD4YOU_PASSWORD="${WORLD4YOU_PASSWORD:-$(_readaccountconf_mutable WORLD4YOU_PASSWORD)}"
  107. if [ -z "$WORLD4YOU_USERNAME" ] || [ -z "$WORLD4YOU_PASSWORD" ]; then
  108. WORLD4YOU_USERNAME=""
  109. WORLD4YOU_PASSWORD=""
  110. _err "You didn't specify world4you username and password yet."
  111. _err "Usage: export WORLD4YOU_USERNAME=<name>"
  112. _err "Usage: export WORLD4YOU_PASSWORD=<password>"
  113. return 2
  114. fi
  115. _saveaccountconf_mutable WORLD4YOU_USERNAME "$WORLD4YOU_USERNAME"
  116. _saveaccountconf_mutable WORLD4YOU_PASSWORD "$WORLD4YOU_PASSWORD"
  117. _info "Logging in..."
  118. username="$WORLD4YOU_USERNAME"
  119. password="$WORLD4YOU_PASSWORD"
  120. csrf_token=$(_get "$WORLD4YOU_API/login" | grep '_csrf_token' | sed 's/^.*<input[^>]*value=\"\([^"]*\)\".*$/\1/')
  121. sessid=$(grep 'W4YSESSID' <"$HTTP_HEADER" | sed 's/^.*W4YSESSID=\([^;]*\);.*$/\1/')
  122. export _H1="Cookie: W4YSESSID=$sessid"
  123. export _H2="X-Requested-With: XMLHttpRequest"
  124. body="_username=$username&_password=$password&_csrf_token=$csrf_token"
  125. ret=$(_post "$body" "$WORLD4YOU_API/login" '' POST 'application/x-www-form-urlencoded')
  126. unset _H2
  127. _debug ret "$ret"
  128. if _contains "$ret" "\"success\":true"; then
  129. _info "Successfully logged in"
  130. sessid=$(grep 'W4YSESSID' <"$HTTP_HEADER" | sed 's/^.*W4YSESSID=\([^;]*\);.*$/\1/')
  131. else
  132. _err "Unable to log in: $(echo "$ret" | sed 's/^.*"message":"\([^\"]*\)".*$/\1/')"
  133. return 1
  134. fi
  135. }
  136. # Usage _get_paketnr <fqdn> <form>
  137. _get_paketnr() {
  138. fqdn="$1"
  139. form="$2"
  140. domains=$(echo "$form" | _egrep_o '^\s*([A-Za-z0-9_-]+\.)+[A-Za-z0-9_-]*$' | sed 's/^\s*\(\S*\)$/\1/')
  141. domain=''
  142. for domain in $domains; do
  143. if echo "$fqdn" | grep "$domain\$" >/dev/null; then
  144. break
  145. fi
  146. domain=''
  147. done
  148. if [ -z "$domain" ]; then
  149. return 1
  150. fi
  151. TLD="$domain"
  152. RECORD=$(echo "$fqdn" | cut -c"1-$((${#fqdn} - ${#TLD} - 1))")
  153. PAKETNR=$(echo "$form" | _egrep_o "data-textfilter=\" $domain .* [0-9]*" | _head_n 1 | _egrep_o "[0-9]*")
  154. return 0
  155. }