2 #Author: Herman Sletteng
3 #Report Bugs here: https://github.com/loial/acme.sh
6 # Note, gratisdns requires a login first, so the script needs to handle
7 # temporary cookies. Since acme.sh _get/_post currently don't directly support
8 # cookies, I've defined wrapper functions _myget/_mypost to set the headers
10 GDNSDK_API
="https://admin.gratisdns.com"
11 ######## Public functions #####################
12 #Usage: dns_gdnsdk_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
16 _info
"Using gratisdns.dk"
17 _debug fulldomain
"$fulldomain"
18 _debug txtvalue
"$txtvalue"
19 if ! _gratisdns_login
; then
24 if ! _get_domain
; then
25 _err
"No matching root domain for $fulldomain found"
29 _info
"Adding the entry"
30 _mypost
"action=dns_primary_record_added_txt&user_domain=$_domain&name=$fulldomain&txtdata=$txtvalue&ttl=1"
31 if _successful_update
; then return 0; fi
32 _err
"Couldn't create entry!"
36 #Usage: fulldomain txtvalue
37 #Remove the txt record after validation.
41 _info
"Using gratisdns.dk"
42 _debug fulldomain
"$fulldomain"
43 _debug txtvalue
"$txtvalue"
44 if ! _gratisdns_login
; then
48 if ! _get_domain
; then
49 _err
"No matching root domain for $fulldomain found"
52 _findentry
"$fulldomain" "$txtvalue"
53 if [ -z "$_id" ]; then
54 _info
"Entry doesn't exist, nothing to delete"
57 _debug
"Deleting record..."
58 _mypost
"action=dns_primary_delete_txt&user_domain=$_domain&id=$_id"
61 if _successful_update
; then return 0; fi
62 _err
"Couldn't delete entry!"
66 #################### Private functions below ##################################
69 GDNSDK_Username
="${GDNSDK_Username:-$(_readaccountconf_mutable GDNSDK_Username)}"
70 GDNSDK_Password
="${GDNSDK_Password:-$(_readaccountconf_mutable GDNSDK_Password)}"
72 if [ -z "$GDNSDK_Username" ] ||
[ -z "$GDNSDK_Password" ]; then
75 _err
"You haven't specified gratisdns.dk username and password yet."
76 _err
"Please add credentials and try again."
79 #save the credentials to the account conf file.
80 _saveaccountconf_mutable GDNSDK_Username
"$GDNSDK_Username"
81 _saveaccountconf_mutable GDNSDK_Password
"$GDNSDK_Password"
86 GDNSDK_Cookie
="${GDNSDK_Cookie:-$(_readaccountconf_mutable GDNSDK_Cookie)}"
87 if [ -z "$GDNSDK_Cookie" ]; then
88 _debug
"No cached cookie found"
92 if (echo "$_result" |
grep -q "logmeout"); then
93 _debug
"Cached cookie still valid"
96 _debug
"Cached cookie no longer valid"
98 _saveaccountconf_mutable GDNSDK_Cookie
"$GDNSDK_Cookie"
103 if ! _checkcredentials
; then return 1; fi
105 if _checkcookie
; then
106 _debug
"Already logged in"
109 _debug
"Logging into GratisDNS with user $GDNSDK_Username"
111 if ! _mypost
"login=$GDNSDK_Username&password=$GDNSDK_Password&action=logmein"; then
112 _err
"GratisDNS login failed for user $GDNSDK_Username bad RC from _post"
116 GDNSDK_Cookie
="$(grep -A 15 '302 Found' "$HTTP_HEADER" | _egrep_o 'Cookie: [^;]*' | _head_n 1 | cut -d ' ' -f2)"
118 if [ -z "$GDNSDK_Cookie" ]; then
119 _err
"GratisDNS login failed for user $GDNSDK_Username. Check $HTTP_HEADER file"
123 _saveaccountconf_mutable GDNSDK_Cookie
"$GDNSDK_Cookie"
128 #Adds cookie to request
129 export _H1
="Cookie: $GDNSDK_Cookie"
130 _result
=$
(_get
"$GDNSDK_API?$1")
133 #Adds cookie to request
134 export _H1
="Cookie: $GDNSDK_Cookie"
135 _result
=$
(_post
"$1" "$GDNSDK_API")
139 _myget
'action=dns_primarydns'
140 _domains
=$
(echo "$_result" | _egrep_o
' domain="[[:alnum:]._-]+' |
sed 's/^.*"//')
141 if [ -z "$_domains" ]; then
142 _err
"Primary domain list not found!"
145 for _domain
in $_domains; do
146 if (_endswith
"$fulldomain" "$_domain"); then
147 _debug
"Root domain: $_domain"
154 _successful_update
() {
155 if (echo "$_result" |
grep -q 'table-success'); then return 0; fi
160 #args $1: fulldomain, $2: txtvalue
161 #returns id of dns entry, if it exists
162 _myget
"action=dns_primary_changeDNSsetup&user_domain=$_domain"
163 _debug3
"_result: $_result"
165 _tmp_result
=$
(echo "$_result" |
tr -d '\n\r' | _egrep_o
"<td>$1</td>\s*<td>$2</td>[^?]*[^&]*&id=[^&]*")
166 _debug _tmp_result
"$_tmp_result"
167 if [ -z "${_tmp_result:-}" ]; then
168 _debug
"The variable is _tmp_result is not supposed to be empty, there may be something wrong with the script"
171 _id
=$
(echo "$_tmp_result" |
sed 's/^.*=//')
172 if [ -n "$_id" ]; then
173 _debug
"Entry found with _id=$_id"