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.

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