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.

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