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.

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