Remove remaining usage of curl and replace with acme.sh _get and _post

plus other misc cleanup
This commit is contained in:
David Kerr 2017-01-28 21:53:03 -05:00
parent 6897fa9880
commit 076f057448

View File

@ -19,8 +19,8 @@
#Usage: dns_freedns_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs" #Usage: dns_freedns_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
dns_freedns_add() { dns_freedns_add() {
fulldomain=$1 fulldomain="$1"
txtvalue=$2 txtvalue="$2"
_info "Add TXT record using FreeDNS" _info "Add TXT record using FreeDNS"
_debug "fulldomain: $fulldomain" _debug "fulldomain: $fulldomain"
@ -46,7 +46,7 @@ dns_freedns_add() {
_saveaccountconf FREEDNS_COOKIE "$FREEDNS_COOKIE" _saveaccountconf FREEDNS_COOKIE "$FREEDNS_COOKIE"
htmlpage="$(_freedns_retrieve_subdomain_page $FREEDNS_COOKIE)" htmlpage="$(_freedns_retrieve_subdomain_page $FREEDNS_COOKIE)"
if [ $? != 0 ]; then if [ "$?" != "0" ]; then
if [ "$using_cached_cookies" = "true" ]; then if [ "$using_cached_cookies" = "true" ]; then
_err "Has your FreeDNS username and password channged? If so..." _err "Has your FreeDNS username and password channged? If so..."
_err "Please export as FREEDNS_USER / FREEDNS_PASSWORD and try again." _err "Please export as FREEDNS_USER / FREEDNS_PASSWORD and try again."
@ -155,7 +155,7 @@ dns_freedns_add() {
else else
# Delete the old TXT record (with the wrong value) # Delete the old TXT record (with the wrong value)
_freedns_delete_txt_record $FREEDNS_COOKIE $DNSdataid _freedns_delete_txt_record $FREEDNS_COOKIE $DNSdataid
if [ $? = 0 ]; then if [ "$?" = "0" ]; then
# And add in new TXT record with the value provided # And add in new TXT record with the value provided
_freedns_add_txt_record $FREEDNS_COOKIE $DNSdomainid $sub_domain "$txtvalue" _freedns_add_txt_record $FREEDNS_COOKIE $DNSdomainid $sub_domain "$txtvalue"
fi fi
@ -168,8 +168,8 @@ dns_freedns_add() {
#Usage: fulldomain txtvalue #Usage: fulldomain txtvalue
#Remove the txt record after validation. #Remove the txt record after validation.
dns_freedns_rm() { dns_freedns_rm() {
fulldomain=$1 fulldomain="$1"
txtvalue=$2 txtvalue="$2"
_info "Delete TXT record using FreeDNS" _info "Delete TXT record using FreeDNS"
_debug "fulldomain: $fulldomain" _debug "fulldomain: $fulldomain"
@ -183,7 +183,7 @@ dns_freedns_rm() {
_debug "FreeDNS login cookies: $FREEDNS_COOKIE" _debug "FreeDNS login cookies: $FREEDNS_COOKIE"
htmlpage="$(_freedns_retrieve_subdomain_page $FREEDNS_COOKIE)" htmlpage="$(_freedns_retrieve_subdomain_page $FREEDNS_COOKIE)"
if [ $? != 0 ]; then if [ "$?" != "0" ]; then
return 1 return 1
fi fi
@ -226,8 +226,8 @@ dns_freedns_rm() {
# field. So for now we will assume that there is only one TXT # field. So for now we will assume that there is only one TXT
# field for the sub domain and just delete it. Currently this # field for the sub domain and just delete it. Currently this
# is a safe assumption. # is a safe assumption.
_freedns_delete_txt_record $FREEDNS_COOKIE $DNSdataid
unset IFS unset IFS
_freedns_delete_txt_record $FREEDNS_COOKIE $DNSdataid
return $? return $?
# fi # fi
fi fi
@ -244,11 +244,11 @@ dns_freedns_rm() {
#################### Private functions below ################################## #################### Private functions below ##################################
# usage: _freedns_login username password # usage: _freedns_login username password
# echos string "cookie:value;cookie:value" etc # print string "cookie=value" etc.
# returns 0 success # returns 0 success
_freedns_login() { _freedns_login() {
username=$1 username="$1"
password=$2 password="$2"
url="https://freedns.afraid.org/zc.php?step=2" url="https://freedns.afraid.org/zc.php?step=2"
_debug "Login to FreeDNS as user $username" _debug "Login to FreeDNS as user $username"
@ -256,7 +256,7 @@ _freedns_login() {
htmlpage="$(_post "username=$(_freedns_urlencode "$username")&password=$(_freedns_urlencode "$password")&submit=Login&action=auth" "$url")" htmlpage="$(_post "username=$(_freedns_urlencode "$username")&password=$(_freedns_urlencode "$password")&submit=Login&action=auth" "$url")"
if [ "$?" != "0" ]; then if [ "$?" != "0" ]; then
_err "FreeDNS login failed for user $username bad RC from _post: $?" _err "FreeDNS login failed for user $username bad RC from _post"
return 1 return 1
fi fi
@ -284,8 +284,8 @@ _freedns_retrieve_subdomain_page() {
htmlpage="$(_get "$url")" htmlpage="$(_get "$url")"
if [ $? != 0 ]; then if [ "$?" != "0" ]; then
_err "FreeDNS retrieve subdomins failed bad RC from cURL: $?" _err "FreeDNS retrieve subdomins failed bad RC from _get"
return 1 return 1
fi fi
@ -300,57 +300,51 @@ _freedns_retrieve_subdomain_page() {
return 0 return 0
} }
# usage _freedns_add_txt_record login_cookies domain_id subdomain value
# returns 0 success
_freedns_add_txt_record() { _freedns_add_txt_record() {
cookies=$1 _H1="Cookie:$1"
domain_id=$2 domain_id="$2"
subdomain=$3 subdomain="$3"
value="$(_freedns_urlencode "$4")" value="$(_freedns_urlencode "$4")"
url="http://freedns.afraid.org/subdomain/save.php?step=2" url="http://freedns.afraid.org/subdomain/save.php?step=2"
#TODO Not using acme.sh _get() function becuase I need to pass in the cookies. htmlpage="$(_post "type=TXT&domain_id=$domain_id&subdomain=$subdomain&address=%22$value%22&send=Save%21" "$url")"
htmlpage="$(curl --silent \
--user-agent "$USER_AGENT" \
--cookie "$cookies" \
--data "type=TXT&domain_id=$domain_id&subdomain=$subdomain&address=%22$value%22&send=Save%21" \
$url )"
if [ $? != 0 ]; then if [ "$?" != "0" ]; then
_err "FreeDNS failed to add TXT record for $subdomain bad RC from cURL: $?" _err "FreeDNS failed to add TXT record for $subdomain bad RC from _post"
return 1 return 1
fi fi
# returned page should be empty on success if ! grep "200 OK" "$HTTP_HEADER" >/dev/null; then
if [ -n "$htmlpage" ]; then
_debug "$htmlpage" _debug "$htmlpage"
_err "FreeDNS failed to add TXT record for $subdomain" _err "FreeDNS failed to add TXT record for $subdomain. Check $HTTP_HEADER file"
return 1 return 1
fi fi
_info "Added acme challenge TXT record for $fulldomain at FreeDNS" _info "Added acme challenge TXT record for $fulldomain at FreeDNS"
return 0 return 0
} }
# usage _freedns_delete_txt_record login_cookies data_id
# returns 0 success
_freedns_delete_txt_record() { _freedns_delete_txt_record() {
cookies=$1 _H1="Cookie:$1"
data_id=$2 data_id="$2"
url="https://freedns.afraid.org/subdomain/delete2.php" url="https://freedns.afraid.org/subdomain/delete2.php"
#TODO Not using acme.sh _get() function becuase I need to pass in the cookies. htmlheader="$(_get "$url?data_id%5B%5D=$data_id&submit=delete+selected" "onlyheader")"
htmlpage="$(curl --silent \
--user-agent "$USER_AGENT" \
--cookie "$cookies" \
"$url?data_id%5B%5D=$data_id&submit=delete+selected" )"
if [ $? != 0 ]; then if [ "$?" != "0" ]; then
_err "FreeDNS failed to delete TXT record for $subdomain bad RC from cURL: $?" _err "FreeDNS failed to delete TXT record for $data_id bad RC from _get"
return 1 return 1
fi fi
# returned page should be empty on success if ! _contains "$htmlheader" "200 OK"; then
if [ -n "$htmlpage" ]; then _debug "$htmlheader"
_debug "$htmlpage"
_err "FreeDNS failed to delete TXT record $data_id" _err "FreeDNS failed to delete TXT record $data_id"
return 1 return 1
fi fi
_info "Deleted acme challenge TXT record for $fulldomain at FreeDNS" _info "Deleted acme challenge TXT record for $fulldomain at FreeDNS"
return 0 return 0
} }