]>
Commit | Line | Data |
---|---|---|
1756bbff HS |
1 | #!/usr/bin/env sh |
2 | #Author: Herman Sletteng | |
3 | #Report Bugs here: https://github.com/loial/acme.sh | |
4 | # | |
5 | # | |
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 | |
9 | ||
10 | GDNSDK_API="https://admin.gratisdns.com" | |
11 | ######## Public functions ##################### | |
12 | #Usage: dns_gdnsdk_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs" | |
13 | dns_gdnsdk_add() { | |
14 | fulldomain=$1 | |
15 | txtvalue=$2 | |
16 | _info "Using gratisdns.dk" | |
17 | _debug fulldomain "$fulldomain" | |
18 | _debug txtvalue "$txtvalue" | |
19 | if ! _gratisdns_login; then | |
20 | _err "Login failed!" | |
21 | return 1 | |
22 | fi | |
23 | #finding domain zone | |
24 | if ! _get_domain; then | |
25 | _err "No matching root domain for $fulldomain found" | |
26 | return 1 | |
27 | fi | |
28 | # adding entry | |
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!" | |
33 | return 1 | |
34 | } | |
35 | ||
36 | #Usage: fulldomain txtvalue | |
37 | #Remove the txt record after validation. | |
38 | dns_gdnsdk_rm() { | |
39 | fulldomain=$1 | |
40 | txtvalue=$2 | |
41 | _info "Using gratisdns.dk" | |
42 | _debug fulldomain "$fulldomain" | |
43 | _debug txtvalue "$txtvalue" | |
44 | if ! _gratisdns_login; then | |
45 | _err "Login failed!" | |
46 | return 1 | |
47 | fi | |
48 | if ! _get_domain; then | |
49 | _err "No matching root domain for $fulldomain found" | |
50 | return 1 | |
51 | fi | |
52 | _findentry "$fulldomain" "$txtvalue" | |
53 | if [ -z "$_id" ]; then | |
54 | _info "Entry doesn't exist, nothing to delete" | |
55 | return 0 | |
56 | fi | |
57 | _debug "Deleting record..." | |
58 | _mypost "action=dns_primary_delete_txt&user_domain=$_domain&id=$_id" | |
59 | # removing entry | |
60 | ||
61 | if _successful_update; then return 0; fi | |
62 | _err "Couldn't delete entry!" | |
63 | return 1 | |
64 | } | |
65 | ||
66 | #################### Private functions below ################################## | |
67 | ||
68 | _checkcredentials() { | |
69 | GDNSDK_Username="${GDNSDK_Username:-$(_readaccountconf_mutable GDNSDK_Username)}" | |
70 | GDNSDK_Password="${GDNSDK_Password:-$(_readaccountconf_mutable GDNSDK_Password)}" | |
71 | ||
72 | if [ -z "$GDNSDK_Username" ] || [ -z "$GDNSDK_Password" ]; then | |
73 | GDNSDK_Username="" | |
74 | GDNSDK_Password="" | |
75 | _err "You haven't specified gratisdns.dk username and password yet." | |
76 | _err "Please add credentials and try again." | |
77 | return 1 | |
78 | fi | |
79 | #save the credentials to the account conf file. | |
80 | _saveaccountconf_mutable GDNSDK_Username "$GDNSDK_Username" | |
81 | _saveaccountconf_mutable GDNSDK_Password "$GDNSDK_Password" | |
82 | return 0 | |
83 | } | |
84 | ||
85 | _checkcookie() { | |
86 | GDNSDK_Cookie="${GDNSDK_Cookie:-$(_readaccountconf_mutable GDNSDK_Cookie)}" | |
87 | if [ -z "$GDNSDK_Cookie" ]; then | |
88 | _debug "No cached cookie found" | |
89 | return 1 | |
90 | fi | |
91 | _myget "action=" | |
92 | if (echo "$_result" | grep -q "logmeout"); then | |
93 | _debug "Cached cookie still valid" | |
94 | return 0 | |
95 | fi | |
96 | _debug "Cached cookie no longer valid" | |
97 | GDNSDK_Cookie="" | |
98 | _saveaccountconf_mutable GDNSDK_Cookie "$GDNSDK_Cookie" | |
99 | return 1 | |
100 | } | |
101 | ||
102 | _gratisdns_login() { | |
103 | if ! _checkcredentials; then return 1; fi | |
104 | ||
105 | if _checkcookie; then | |
106 | _debug "Already logged in" | |
107 | return 0 | |
108 | fi | |
109 | _debug "Logging into GratisDNS with user $GDNSDK_Username" | |
110 | ||
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" | |
113 | return 1 | |
114 | fi | |
115 | ||
116 | GDNSDK_Cookie="$(grep -A 15 '302 Found' "$HTTP_HEADER" | _egrep_o 'Cookie: [^;]*' | _head_n 1 | cut -d ' ' -f2)" | |
117 | ||
118 | if [ -z "$GDNSDK_Cookie" ]; then | |
119 | _err "GratisDNS login failed for user $GDNSDK_Username. Check $HTTP_HEADER file" | |
120 | return 1 | |
121 | fi | |
122 | export GDNSDK_Cookie | |
123 | _saveaccountconf_mutable GDNSDK_Cookie "$GDNSDK_Cookie" | |
124 | return 0 | |
125 | } | |
126 | ||
127 | _myget() { | |
128 | #Adds cookie to request | |
129 | export _H1="Cookie: $GDNSDK_Cookie" | |
130 | _result=$(_get "$GDNSDK_API?$1") | |
131 | } | |
132 | _mypost() { | |
133 | #Adds cookie to request | |
134 | export _H1="Cookie: $GDNSDK_Cookie" | |
135 | _result=$(_post "$1" "$GDNSDK_API") | |
136 | } | |
137 | ||
138 | _get_domain() { | |
139 | _myget 'action=dns_primarydns' | |
7679df06 | 140 | _domains=$(echo "$_result" | _egrep_o ' domain="[[:alnum:]._-]+' | sed 's/^.*"//') |
1756bbff HS |
141 | if [ -z "$_domains" ]; then |
142 | _err "Primary domain list not found!" | |
143 | return 1 | |
144 | fi | |
145 | for _domain in $_domains; do | |
146 | if (_endswith "$fulldomain" "$_domain"); then | |
147 | _debug "Root domain: $_domain" | |
148 | return 0 | |
149 | fi | |
150 | done | |
151 | return 1 | |
152 | } | |
153 | ||
154 | _successful_update() { | |
155 | if (echo "$_result" | grep -q 'table-success'); then return 0; fi | |
156 | return 1 | |
157 | } | |
158 | ||
159 | _findentry() { | |
160 | #returns id of dns entry, if it exists | |
161 | _myget "action=dns_primary_changeDNSsetup&user_domain=$_domain" | |
1f25b4a8 | 162 | _id=$(echo "$_result" | _egrep_o "<td>$1</td>\s*<td>$2</td>[^?]*[^&]*&id=[^&]*" | sed 's/^.*=//') |
1756bbff HS |
163 | if [ -n "$_id" ]; then |
164 | _debug "Entry found with _id=$_id" | |
165 | return 0 | |
166 | fi | |
167 | return 1 | |
168 | } |