123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
- #!/usr/bin/env sh
- dns_freedns_add() {
- fulldomain="$1"
- txtvalue="$2"
- _info "Add TXT record using FreeDNS"
- _debug "fulldomain: $fulldomain"
- _debug "txtvalue: $txtvalue"
- if [ -z "$FREEDNS_User" ] || [ -z "$FREEDNS_Password" ]; then
- FREEDNS_User=""
- FREEDNS_Password=""
- if [ -z "$FREEDNS_COOKIE" ]; then
- _err "You did not specify the FreeDNS username and password yet."
- _err "Please export as FREEDNS_User / FREEDNS_Password and try again."
- return 1
- fi
- using_cached_cookies="true"
- else
- FREEDNS_COOKIE="$(_freedns_login "$FREEDNS_User" "$FREEDNS_Password")"
- if [ -z "$FREEDNS_COOKIE" ]; then
- return 1
- fi
- using_cached_cookies="false"
- fi
- _debug "FreeDNS login cookies: $FREEDNS_COOKIE (cached = $using_cached_cookies)"
- _saveaccountconf FREEDNS_COOKIE "$FREEDNS_COOKIE"
-
- i="$(echo "$fulldomain" | tr '.' ' ' | wc -w)"
- i="$(_math "$i" - 1)"
- top_domain="$(echo "$fulldomain" | cut -d. -f "$i"-100)"
- i="$(_math "$i" - 1)"
- sub_domain="$(echo "$fulldomain" | cut -d. -f -"$i")"
-
-
-
-
-
- attempts=2
- while [ "$attempts" -gt "0" ]; do
- attempts="$(_math "$attempts" - 1)"
- htmlpage="$(_freedns_retrieve_subdomain_page "$FREEDNS_COOKIE")"
- if [ "$?" != "0" ]; then
- if [ "$using_cached_cookies" = "true" ]; then
- _err "Has your FreeDNS username and password channged? If so..."
- _err "Please export as FREEDNS_User / FREEDNS_Password and try again."
- fi
- return 1
- fi
-
-
- subdomain_csv="$(echo "$htmlpage" \
- | grep -i -e '</\?TABLE\|</\?TD\|</\?TR\|</\?TH' \
- | sed 's/^[\ \t]*//g' \
- | tr -d '\n' \
- | sed 's/<\/TR[^>]*>/\n/Ig' \
- | sed 's/<\/\?\(TABLE\|TR\)[^>]*>//Ig' \
- | sed 's/^<T[DH][^>]*>\|<\/\?T[DH][^>]*>$//Ig' \
- | sed 's/<\/T[DH][^>]*><T[DH][^>]*>/,/Ig' \
- | grep 'edit.php?' \
- | grep "$top_domain")"
-
-
-
-
-
- lines="$(echo "$subdomain_csv" | wc -l)"
- nl='
- '
- i=0
- found=0
- while [ "$i" -lt "$lines" ]; do
- i="$(_math "$i" + 1)"
- line="$(echo "$subdomain_csv" | cut -d "$nl" -f "$i")"
- tmp="$(echo "$line" | cut -d ',' -f 1)"
- if [ $found = 0 ] && _startswith "$tmp" "<td>$top_domain"; then
-
- DNSdomainid="$(echo "$line" | cut -d ',' -f 2 | sed 's/^.*domain_id=//;s/>.*//')"
- found=1
- else
-
- DNSname="$(echo "$line" | cut -d ',' -f 2 | sed 's/^[^>]*>//;s/<\/a>.*//')"
- DNStype="$(echo "$line" | cut -d ',' -f 3)"
- if [ "$DNSname" = "$fulldomain" ] && [ "$DNStype" = "TXT" ]; then
- DNSdataid="$(echo "$line" | cut -d ',' -f 2 | sed 's/^.*data_id=//;s/>.*//')"
-
-
-
-
-
- DNSvalue="$(echo "$line" | cut -d ',' -f 4 | sed 's/^[^"]*"//;s/".*//;s/<\/td>.*//')"
- if [ $found != 0 ]; then
- break
-
-
-
-
-
- fi
- else
- DNSname=""
- DNStype=""
- fi
- fi
- done
- _debug "DNSname: $DNSname DNStype: $DNStype DNSdomainid: $DNSdomainid DNSdataid: $DNSdataid"
- _debug "DNSvalue: $DNSvalue"
- if [ -z "$DNSdomainid" ]; then
-
-
- if [ "$attempts" = "0" ]; then
-
- _debug "$htmlpage"
- _debug "$subdomain_csv"
- _err "Domain $top_domain not found at FreeDNS"
- return 1
- fi
- else
-
- break
- fi
- _info "Domain $top_domain not found at FreeDNS"
- _info "Retry loading subdomain page ($attempts attempts remaining)"
- done
- if [ -z "$DNSdataid" ]; then
-
-
-
- _freedns_add_txt_record "$FREEDNS_COOKIE" "$DNSdomainid" "$sub_domain" "$txtvalue"
- return $?
- else
- if [ "$txtvalue" = "$DNSvalue" ]; then
-
-
-
-
-
-
-
- _info "No update necessary for $fulldomain at FreeDNS"
- return 0
- else
-
- _freedns_delete_txt_record "$FREEDNS_COOKIE" "$DNSdataid"
- if [ "$?" = "0" ]; then
-
- _freedns_add_txt_record "$FREEDNS_COOKIE" "$DNSdomainid" "$sub_domain" "$txtvalue"
- fi
- return $?
- fi
- fi
- return 0
- }
- dns_freedns_rm() {
- fulldomain="$1"
- txtvalue="$2"
- _info "Delete TXT record using FreeDNS"
- _debug "fulldomain: $fulldomain"
- _debug "txtvalue: $txtvalue"
-
-
-
- FREEDNS_COOKIE="$(_read_conf "$ACCOUNT_CONF_PATH" "FREEDNS_COOKIE")"
- _debug "FreeDNS login cookies: $FREEDNS_COOKIE"
-
-
-
-
-
- attempts=2
- while [ "$attempts" -gt "0" ]; do
- attempts="$(_math "$attempts" - 1)"
- htmlpage="$(_freedns_retrieve_subdomain_page "$FREEDNS_COOKIE")"
- if [ "$?" != "0" ]; then
- return 1
- fi
-
-
- subdomain_csv="$(echo "$htmlpage" \
- | grep -i -e '</\?TABLE\|</\?TD\|</\?TR\|</\?TH' \
- | sed 's/^[\ \t]*//g' \
- | tr -d '\n' \
- | sed 's/<\/TR[^>]*>/\n/Ig' \
- | sed 's/<\/\?\(TABLE\|TR\)[^>]*>//Ig' \
- | sed 's/^<T[DH][^>]*>\|<\/\?T[DH][^>]*>$//Ig' \
- | sed 's/<\/T[DH][^>]*><T[DH][^>]*>/,/Ig' \
- | grep 'edit.php?' \
- | grep "$fulldomain")"
-
-
-
-
-
- lines="$(echo "$subdomain_csv" | wc -l)"
- nl='
- '
- i=0
- found=0
- while [ "$i" -lt "$lines" ]; do
- i="$(_math "$i" + 1)"
- line="$(echo "$subdomain_csv" | cut -d "$nl" -f "$i")"
- DNSname="$(echo "$line" | cut -d ',' -f 2 | sed 's/^[^>]*>//;s/<\/a>.*//')"
- DNStype="$(echo "$line" | cut -d ',' -f 3)"
- if [ "$DNSname" = "$fulldomain" ] && [ "$DNStype" = "TXT" ]; then
- DNSdataid="$(echo "$line" | cut -d ',' -f 2 | sed 's/^.*data_id=//;s/>.*//')"
- DNSvalue="$(echo "$line" | cut -d ',' -f 4 | sed 's/^[^"]*"//;s/".*//;s/<\/td>.*//')"
- _debug "DNSvalue: $DNSvalue"
-
-
-
-
-
- _freedns_delete_txt_record "$FREEDNS_COOKIE" "$DNSdataid"
- return $?
-
- fi
- done
- done
-
-
- _debug2 "$subdomain_csv"
- _info "Cannot delete TXT record for $fulldomain/$txtvalue. Does not exist at FreeDNS"
- return 0
- }
- _freedns_login() {
- export _H1="Accept-Language:en-US"
- username="$1"
- password="$2"
- url="https://freedns.afraid.org/zc.php?step=2"
- _debug "Login to FreeDNS as user $username"
- htmlpage="$(_post "username=$(printf '%s' "$username" | _url_encode)&password=$(printf '%s' "$password" | _url_encode)&submit=Login&action=auth" "$url")"
- if [ "$?" != "0" ]; then
- _err "FreeDNS login failed for user $username bad RC from _post"
- return 1
- fi
- cookies="$(grep -i '^Set-Cookie.*dns_cookie.*$' "$HTTP_HEADER" | _head_n 1 | tr -d "\r\n" | cut -d " " -f 2)"
- # if cookies is not empty then logon successful
- if [ -z "$cookies" ]; then
- _debug "$htmlpage"
- _err "FreeDNS login failed for user $username. Check $HTTP_HEADER file"
- return 1
- fi
- printf "%s" "$cookies"
- return 0
- }
- # usage _freedns_retrieve_subdomain_page login_cookies
- # echo page retrieved (html)
- # returns 0 success
- _freedns_retrieve_subdomain_page() {
- export _H1="Cookie:$1"
- export _H2="Accept-Language:en-US"
- url="https://freedns.afraid.org/subdomain/"
- _debug "Retrieve subdmoain page from FreeDNS"
- htmlpage="$(_get "$url")"
- if [ "$?" != "0" ]; then
- _err "FreeDNS retrieve subdomins failed bad RC from _get"
- return 1
- elif [ -z "$htmlpage" ]; then
- _err "FreeDNS returned empty subdomain page"
- return 1
- fi
- _debug2 "$htmlpage"
- printf "%s" "$htmlpage"
- return 0
- }
- # usage _freedns_add_txt_record login_cookies domain_id subdomain value
- # returns 0 success
- _freedns_add_txt_record() {
- export _H1="Cookie:$1"
- export _H2="Accept-Language:en-US"
- domain_id="$2"
- subdomain="$3"
- value="$(printf '%s' "$4" | _url_encode)"
- url="http://freedns.afraid.org/subdomain/save.php?step=2"
- htmlpage="$(_post "type=TXT&domain_id=$domain_id&subdomain=$subdomain&address=%22$value%22&send=Save%21" "$url")"
- if [ "$?" != "0" ]; then
- _err "FreeDNS failed to add TXT record for $subdomain bad RC from _post"
- return 1
- elif ! grep "200 OK" "$HTTP_HEADER" >/dev/null; then
- _debug "$htmlpage"
- _err "FreeDNS failed to add TXT record for $subdomain. Check $HTTP_HEADER file"
- return 1
- elif _contains "$htmlpage" "security code was incorrect"; then
- _debug "$htmlpage"
- _err "FreeDNS failed to add TXT record for $subdomain as FreeDNS requested seurity code"
- _err "Note that you cannot use automatic DNS validation for FreeDNS public domains"
- return 1
- fi
- _debug2 "$htmlpage"
- _info "Added acme challenge TXT record for $fulldomain at FreeDNS"
- return 0
- }
- # usage _freedns_delete_txt_record login_cookies data_id
- # returns 0 success
- _freedns_delete_txt_record() {
- export _H1="Cookie:$1"
- export _H2="Accept-Language:en-US"
- data_id="$2"
- url="https://freedns.afraid.org/subdomain/delete2.php"
- htmlheader="$(_get "$url?data_id%5B%5D=$data_id&submit=delete+selected" "onlyheader")"
- if [ "$?" != "0" ]; then
- _err "FreeDNS failed to delete TXT record for $data_id bad RC from _get"
- return 1
- elif ! _contains "$htmlheader" "200 OK"; then
- _debug "$htmlheader"
- _err "FreeDNS failed to delete TXT record $data_id"
- return 1
- fi
- _info "Deleted acme challenge TXT record for $fulldomain at FreeDNS"
- return 0
- }
|