]>
Commit | Line | Data |
---|---|---|
8ee5726e LS |
1 | #!/usr/bin/env sh |
2 | ||
3 | # World4You - www.world4you.com | |
4 | # Lorenz Stechauner, 2020 - https://www.github.com/NerLOR | |
5 | ||
6 | WORLD4YOU_API="https://my.world4you.com/en" | |
abe05456 | 7 | PAKETNR='' |
5cfe5e31 LS |
8 | TLD='' |
9 | RECORD='' | |
8ee5726e LS |
10 | |
11 | ################ Public functions ################ | |
12 | ||
13 | # Usage: dns_world4you_add <fqdn> <value> | |
14 | dns_world4you_add() { | |
3a29e034 | 15 | fqdn=$(echo "$1" | _lower_case) |
8ee5726e | 16 | value="$2" |
40631f46 | 17 | _info "Using world4you to add record" |
8ee5726e LS |
18 | _debug fulldomain "$fqdn" |
19 | _debug txtvalue "$value" | |
20 | ||
8ee5726e LS |
21 | _login |
22 | if [ "$?" != 0 ]; then | |
23 | return 1 | |
24 | fi | |
25 | ||
26 | export _H1="Cookie: W4YSESSID=$sessid" | |
190ec0c1 | 27 | form=$(_get "$WORLD4YOU_API/") |
5cfe5e31 | 28 | _get_paketnr "$fqdn" "$form" |
abe05456 | 29 | paketnr="$PAKETNR" |
8ee5726e LS |
30 | if [ -z "$paketnr" ]; then |
31 | _err "Unable to parse paketnr" | |
32 | return 3 | |
33 | fi | |
34 | _debug paketnr "$paketnr" | |
35 | ||
36 | export _H1="Cookie: W4YSESSID=$sessid" | |
37 | form=$(_get "$WORLD4YOU_API/$paketnr/dns") | |
38 | formiddp=$(echo "$form" | grep 'AddDnsRecordForm\[uniqueFormIdDP\]' | sed 's/^.*name="AddDnsRecordForm\[uniqueFormIdDP\]" value="\([^"]*\)".*$/\1/') | |
8ee5726e LS |
39 | form_token=$(echo "$form" | grep 'AddDnsRecordForm\[_token\]' | sed 's/^.*name="AddDnsRecordForm\[_token\]" value="\([^"]*\)".*$/\1/') |
40 | if [ -z "$formiddp" ]; then | |
41 | _err "Unable to parse form" | |
42 | return 3 | |
43 | fi | |
44 | ||
bfccf29c | 45 | _resethttp |
fbcbc101 | 46 | export ACME_HTTP_NO_REDIRECTS=1 |
aa7bf916 | 47 | body="AddDnsRecordForm[name]=$RECORD&AddDnsRecordForm[dnsType][type]=TXT&AddDnsRecordForm[value]=$value&AddDnsRecordForm[uniqueFormIdDP]=$formiddp&AddDnsRecordForm[_token]=$form_token" |
8ee5726e LS |
48 | _info "Adding record..." |
49 | ret=$(_post "$body" "$WORLD4YOU_API/$paketnr/dns" '' POST 'application/x-www-form-urlencoded') | |
bfccf29c | 50 | _resethttp |
8ee5726e | 51 | |
68c2478e | 52 | if _contains "$(_head_n 1 <"$HTTP_HEADER")" '302'; then |
be43cebf LS |
53 | res=$(_get "$WORLD4YOU_API/$paketnr/dns") |
54 | if _contains "$res" "successfully"; then | |
da839aae LS |
55 | return 0 |
56 | else | |
29f12dda | 57 | msg=$(echo "$res" | grep -A 15 'data-type="danger"' | grep "<h3[^>]*>[^<]" | sed 's/<[^>]*>//g' | sed 's/^\s*//g') |
db83643c LS |
58 | if [ "$msg" = '' ]; then |
59 | _err "Unable to add record: Unknown error" | |
cb90167c LS |
60 | echo "$ret" >'error-01.html' |
61 | echo "$res" >'error-02.html' | |
ac4ae85a | 62 | _err "View error-01.html and error-02.html for debugging" |
db83643c LS |
63 | else |
64 | _err "Unable to add record: my.world4you.com: $msg" | |
ac4ae85a | 65 | fi |
da839aae LS |
66 | return 1 |
67 | fi | |
8ee5726e | 68 | else |
a8f71f79 LS |
69 | msg=$(echo "$ret" | grep '"form-error-message"' | sed 's/^.*<div class="form-error-message">\([^<]*\)<\/div>.*$/\1/') |
70 | _err "Unable to add record: my.world4you.com: $msg" | |
f665c73b | 71 | return 1 |
8ee5726e LS |
72 | fi |
73 | } | |
74 | ||
75 | # Usage: dns_world4you_rm <fqdn> <value> | |
76 | dns_world4you_rm() { | |
3a29e034 | 77 | fqdn=$(echo "$1" | _lower_case) |
8ee5726e | 78 | value="$2" |
40631f46 | 79 | _info "Using world4you to remove record" |
8ee5726e LS |
80 | _debug fulldomain "$fqdn" |
81 | _debug txtvalue "$value" | |
82 | ||
8ee5726e LS |
83 | _login |
84 | if [ "$?" != 0 ]; then | |
85 | return 1 | |
86 | fi | |
87 | ||
88 | export _H1="Cookie: W4YSESSID=$sessid" | |
190ec0c1 | 89 | form=$(_get "$WORLD4YOU_API/") |
5cfe5e31 | 90 | _get_paketnr "$fqdn" "$form" |
abe05456 | 91 | paketnr="$PAKETNR" |
8ee5726e LS |
92 | if [ -z "$paketnr" ]; then |
93 | _err "Unable to parse paketnr" | |
94 | return 3 | |
95 | fi | |
96 | _debug paketnr "$paketnr" | |
97 | ||
98 | form=$(_get "$WORLD4YOU_API/$paketnr/dns") | |
99 | formiddp=$(echo "$form" | grep 'DeleteDnsRecordForm\[uniqueFormIdDP\]' | sed 's/^.*name="DeleteDnsRecordForm\[uniqueFormIdDP\]" value="\([^"]*\)".*$/\1/') | |
8ee5726e LS |
100 | form_token=$(echo "$form" | grep 'DeleteDnsRecordForm\[_token\]' | sed 's/^.*name="DeleteDnsRecordForm\[_token\]" value="\([^"]*\)".*$/\1/') |
101 | if [ -z "$formiddp" ]; then | |
102 | _err "Unable to parse form" | |
103 | return 3 | |
104 | fi | |
105 | ||
106 | recordid=$(printf "TXT:%s.:\"%s\"" "$fqdn" "$value" | _base64) | |
107 | _debug recordid "$recordid" | |
108 | ||
bfccf29c | 109 | _resethttp |
fbcbc101 | 110 | export ACME_HTTP_NO_REDIRECTS=1 |
aa7bf916 | 111 | body="DeleteDnsRecordForm[recordId]=$recordid&DeleteDnsRecordForm[uniqueFormIdDP]=$formiddp&DeleteDnsRecordForm[_token]=$form_token" |
8ee5726e | 112 | _info "Removing record..." |
aa7bf916 | 113 | ret=$(_post "$body" "$WORLD4YOU_API/$paketnr/dns/record/delete" '' POST 'application/x-www-form-urlencoded') |
bfccf29c | 114 | _resethttp |
8ee5726e | 115 | |
68c2478e | 116 | if _contains "$(_head_n 1 <"$HTTP_HEADER")" '302'; then |
be43cebf LS |
117 | res=$(_get "$WORLD4YOU_API/$paketnr/dns") |
118 | if _contains "$res" "successfully"; then | |
da839aae LS |
119 | return 0 |
120 | else | |
29f12dda | 121 | msg=$(echo "$res" | grep -A 15 'data-type="danger"' | grep "<h3[^>]*>[^<]" | sed 's/<[^>]*>//g' | sed 's/^\s*//g') |
db83643c LS |
122 | if [ "$msg" = '' ]; then |
123 | _err "Unable to remove record: Unknown error" | |
cb90167c LS |
124 | echo "$ret" >'error-01.html' |
125 | echo "$res" >'error-02.html' | |
ac4ae85a | 126 | _err "View error-01.html and error-02.html for debugging" |
db83643c LS |
127 | else |
128 | _err "Unable to remove record: my.world4you.com: $msg" | |
ac4ae85a | 129 | fi |
da839aae LS |
130 | return 1 |
131 | fi | |
8ee5726e | 132 | else |
a8f71f79 LS |
133 | msg=$(echo "$ret" | grep "form-error-message" | sed 's/^.*<div class="form-error-message">\([^<]*\)<\/div>.*$/\1/') |
134 | _err "Unable to remove record: my.world4you.com: $msg" | |
f665c73b | 135 | return 1 |
8ee5726e LS |
136 | fi |
137 | } | |
138 | ||
139 | ################ Private functions ################ | |
140 | ||
141 | # Usage: _login | |
142 | _login() { | |
143 | WORLD4YOU_USERNAME="${WORLD4YOU_USERNAME:-$(_readaccountconf_mutable WORLD4YOU_USERNAME)}" | |
144 | WORLD4YOU_PASSWORD="${WORLD4YOU_PASSWORD:-$(_readaccountconf_mutable WORLD4YOU_PASSWORD)}" | |
145 | ||
146 | if [ -z "$WORLD4YOU_USERNAME" ] || [ -z "$WORLD4YOU_PASSWORD" ]; then | |
147 | WORLD4YOU_USERNAME="" | |
148 | WORLD4YOU_PASSWORD="" | |
0dcf6771 | 149 | _err "You didn't specify world4you username and password yet." |
8ee5726e LS |
150 | _err "Usage: export WORLD4YOU_USERNAME=<name>" |
151 | _err "Usage: export WORLD4YOU_PASSWORD=<password>" | |
f665c73b | 152 | return 1 |
8ee5726e LS |
153 | fi |
154 | ||
155 | _saveaccountconf_mutable WORLD4YOU_USERNAME "$WORLD4YOU_USERNAME" | |
156 | _saveaccountconf_mutable WORLD4YOU_PASSWORD "$WORLD4YOU_PASSWORD" | |
157 | ||
68c2478e LS |
158 | _resethttp |
159 | export ACME_HTTP_NO_REDIRECTS=1 | |
160 | page=$(_get "$WORLD4YOU_API/login") | |
161 | _resethttp | |
162 | ||
163 | if _contains "$(_head_n 1 <"$HTTP_HEADER")" '302'; then | |
164 | _info "Already logged in" | |
165 | _parse_sessid | |
166 | return 0 | |
167 | fi | |
168 | ||
8ee5726e LS |
169 | _info "Logging in..." |
170 | ||
171 | username="$WORLD4YOU_USERNAME" | |
172 | password="$WORLD4YOU_PASSWORD" | |
68c2478e | 173 | csrf_token=$(echo "$page" | grep '_csrf_token' | sed 's/^.*<input[^>]*value=\"\([^"]*\)\".*$/\1/') |
4d8b661d | 174 | _parse_sessid |
8ee5726e LS |
175 | |
176 | export _H1="Cookie: W4YSESSID=$sessid" | |
177 | export _H2="X-Requested-With: XMLHttpRequest" | |
178 | body="_username=$username&_password=$password&_csrf_token=$csrf_token" | |
179 | ret=$(_post "$body" "$WORLD4YOU_API/login" '' POST 'application/x-www-form-urlencoded') | |
180 | unset _H2 | |
4d8b661d | 181 | |
8ee5726e LS |
182 | _debug ret "$ret" |
183 | if _contains "$ret" "\"success\":true"; then | |
184 | _info "Successfully logged in" | |
4d8b661d | 185 | _parse_sessid |
8ee5726e | 186 | else |
a8f71f79 LS |
187 | msg=$(echo "$ret" | sed 's/^.*"message":"\([^\"]*\)".*$/\1/') |
188 | _err "Unable to log in: my.world4you.com: $msg" | |
8ee5726e LS |
189 | return 1 |
190 | fi | |
191 | } | |
94495015 | 192 | |
4d8b661d | 193 | # Usage: _get_paketnr <fqdn> <form> |
abe05456 | 194 | _get_paketnr() { |
5cfe5e31 | 195 | fqdn="$1" |
abe05456 LS |
196 | form="$2" |
197 | ||
5d6d0c61 | 198 | domains=$(echo "$form" | grep '<ul class="nav header-paket-list">' | sed 's/<li/\n<li/g' | sed 's/<[^>]*>/ /g' | sed 's/^.*>\([^>]*\)$/\1/') |
ef914751 LS |
199 | domain='' |
200 | for domain in $domains; do | |
ac4ae85a | 201 | if _contains "$fqdn" "$domain\$"; then |
ef914751 | 202 | break |
abe05456 | 203 | fi |
5cfe5e31 | 204 | domain='' |
abe05456 | 205 | done |
ef914751 LS |
206 | if [ -z "$domain" ]; then |
207 | return 1 | |
208 | fi | |
209 | ||
5cfe5e31 | 210 | TLD="$domain" |
48b2a271 | 211 | _debug domain "$domain" |
5cfe5e31 | 212 | RECORD=$(echo "$fqdn" | cut -c"1-$((${#fqdn} - ${#TLD} - 1))") |
5d6d0c61 | 213 | PAKETNR=$(echo "$domains" | grep "$domain" | sed 's/^[^,]*, *\([0-9]*\).*$/\1/') |
ef914751 | 214 | return 0 |
abe05456 | 215 | } |
4d8b661d LS |
216 | |
217 | # Usage: _parse_sessid | |
218 | _parse_sessid() { | |
219 | sessid=$(grep 'W4YSESSID' <"$HTTP_HEADER" | _tail_n 1 | sed 's/^.*W4YSESSID=\([^;]*\);.*$/\1/') | |
220 | } |