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.

191 lines
6.2 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 to add record"
  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. res=$(_get "$WORLD4YOU_API/$paketnr/dns")
  48. if _contains "$res" "successfully"; then
  49. return 0
  50. else
  51. msg=$(echo "$res" | tr '\n' '\t' | sed 's/.*<h3 class="mb-5">[^\t]*\t *\([^\t]*\)\t.*/\1/')
  52. _err "Unable to add record: $msg"
  53. return 1
  54. fi
  55. else
  56. _err "$(_head_n 1 <"$HTTP_HEADER")"
  57. return 1
  58. fi
  59. }
  60. # Usage: dns_world4you_rm <fqdn> <value>
  61. dns_world4you_rm() {
  62. fqdn="$1"
  63. value="$2"
  64. _info "Using world4you to remove record"
  65. _debug fulldomain "$fqdn"
  66. _debug txtvalue "$value"
  67. _login
  68. if [ "$?" != 0 ]; then
  69. return 1
  70. fi
  71. export _H1="Cookie: W4YSESSID=$sessid"
  72. form=$(_get "$WORLD4YOU_API/dashboard/paketuebersicht")
  73. _get_paketnr "$fqdn" "$form"
  74. paketnr="$PAKETNR"
  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. _resethttp
  91. export ACME_HTTP_NO_REDIRECTS=1
  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. _resethttp
  98. if grep '302' >/dev/null <"$HTTP_HEADER"; then
  99. res=$(_get "$WORLD4YOU_API/$paketnr/dns")
  100. if _contains "$res" "successfully"; then
  101. return 0
  102. else
  103. msg=$(echo "$res" | tr '\n' '\t' | sed 's/.*<h3 class="mb-5">[^\t]*\t *\([^\t]*\)\t.*/\1/')
  104. _err "Unable to remove record: $msg"
  105. return 1
  106. fi
  107. else
  108. _err "$(_head_n 1 <"$HTTP_HEADER")"
  109. return 1
  110. fi
  111. }
  112. ################ Private functions ################
  113. # Usage: _login
  114. _login() {
  115. WORLD4YOU_USERNAME="${WORLD4YOU_USERNAME:-$(_readaccountconf_mutable WORLD4YOU_USERNAME)}"
  116. WORLD4YOU_PASSWORD="${WORLD4YOU_PASSWORD:-$(_readaccountconf_mutable WORLD4YOU_PASSWORD)}"
  117. if [ -z "$WORLD4YOU_USERNAME" ] || [ -z "$WORLD4YOU_PASSWORD" ]; then
  118. WORLD4YOU_USERNAME=""
  119. WORLD4YOU_PASSWORD=""
  120. _err "You didn't specify world4you username and password yet."
  121. _err "Usage: export WORLD4YOU_USERNAME=<name>"
  122. _err "Usage: export WORLD4YOU_PASSWORD=<password>"
  123. return 1
  124. fi
  125. _saveaccountconf_mutable WORLD4YOU_USERNAME "$WORLD4YOU_USERNAME"
  126. _saveaccountconf_mutable WORLD4YOU_PASSWORD "$WORLD4YOU_PASSWORD"
  127. _info "Logging in..."
  128. username="$WORLD4YOU_USERNAME"
  129. password="$WORLD4YOU_PASSWORD"
  130. csrf_token=$(_get "$WORLD4YOU_API/login" | grep '_csrf_token' | sed 's/^.*<input[^>]*value=\"\([^"]*\)\".*$/\1/')
  131. sessid=$(grep 'W4YSESSID' <"$HTTP_HEADER" | sed 's/^.*W4YSESSID=\([^;]*\);.*$/\1/')
  132. export _H1="Cookie: W4YSESSID=$sessid"
  133. export _H2="X-Requested-With: XMLHttpRequest"
  134. body="_username=$username&_password=$password&_csrf_token=$csrf_token"
  135. ret=$(_post "$body" "$WORLD4YOU_API/login" '' POST 'application/x-www-form-urlencoded')
  136. unset _H2
  137. _debug ret "$ret"
  138. if _contains "$ret" "\"success\":true"; then
  139. _info "Successfully logged in"
  140. sessid=$(grep 'W4YSESSID' <"$HTTP_HEADER" | sed 's/^.*W4YSESSID=\([^;]*\);.*$/\1/')
  141. else
  142. _err "Unable to log in: $(echo "$ret" | sed 's/^.*"message":"\([^\"]*\)".*$/\1/')"
  143. return 1
  144. fi
  145. }
  146. # Usage _get_paketnr <fqdn> <form>
  147. _get_paketnr() {
  148. fqdn="$1"
  149. form="$2"
  150. domains=$(echo "$form" | grep '^ *[A-Za-z0-9_\.-]*\.[A-Za-z0-9_-]*$' | sed 's/^\s*\(\S*\)$/\1/')
  151. domain=''
  152. for domain in $domains; do
  153. if echo "$fqdn" | grep "$domain\$" >/dev/null; then
  154. break
  155. fi
  156. domain=''
  157. done
  158. if [ -z "$domain" ]; then
  159. return 1
  160. fi
  161. TLD="$domain"
  162. RECORD=$(echo "$fqdn" | cut -c"1-$((${#fqdn} - ${#TLD} - 1))")
  163. PAKETNR=$(echo "$form" | grep "data-textfilter=\" $domain " | _head_n 1 | sed 's/^.* \([0-9]*\) .*$/\1/')
  164. return 0
  165. }