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.

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