]>
Commit | Line | Data |
---|---|---|
27f30631 GL |
1 | #!/usr/bin/env sh |
2 | ||
3 | # | |
4 | #GCORE_Key='773$7b7adaf2a2b32bfb1b83787b4ff32a67eb178e3ada1af733e47b1411f2461f7f4fa7ed7138e2772a46124377bad7384b3bb8d87748f87b3f23db4b8bbe41b2bb' | |
5 | # | |
6 | ||
7 | GCORE_Api="https://api.gcorelabs.com/dns/v2" | |
8 | GCORE_Doc="https://apidocs.gcore.com/dns" | |
9 | ||
10 | ######## Public functions ##################### | |
11 | ||
12 | #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs" | |
13 | dns_gcore_add() { | |
14 | fulldomain=$1 | |
15 | txtvalue=$2 | |
16 | ||
17 | GCORE_Key="${GCORE_Key:-$(_readaccountconf_mutable GCORE_Key)}" | |
18 | ||
19 | if [ -z "$GCORE_Key" ]; then | |
20 | GCORE_Key="" | |
21 | _err "You didn't specify a Gcore api key yet." | |
22 | _err "You can get yours from here $GCORE_Doc" | |
23 | return 1 | |
24 | fi | |
25 | ||
26 | #save the api key to the account conf file. | |
27 | _saveaccountconf_mutable GCORE_Key "$GCORE_Key" | |
28 | ||
29 | _debug "First detect the zone name" | |
30 | if ! _get_root "$fulldomain"; then | |
31 | _err "invalid domain" | |
32 | return 1 | |
33 | fi | |
34 | _debug _zone_name "$_zone_name" | |
35 | _debug _sub_domain "$_sub_domain" | |
36 | _debug _domain "$_domain" | |
37 | ||
38 | _debug "Getting txt records" | |
39 | _gcore_rest GET "zones/$_zone_name/$fulldomain/TXT" | |
40 | payload="" | |
41 | ||
42 | if echo "$response" | grep "record is not found" >/dev/null; then | |
43 | _info "Record doesn't exists" | |
44 | payload="{\"resource_records\":[{\"content\":[\"$txtvalue\"],\"enabled\":true}],\"ttl\":120}" | |
45 | elif echo "$response" | grep "$txtvalue" >/dev/null; then | |
46 | _info "Already exists, OK" | |
47 | return 0 | |
48 | elif echo "$response" | tr -d " " | grep \"name\":\""$fulldomain"\",\"type\":\"TXT\" >/dev/null; then | |
49 | _info "Record with mismatch txtvalue, try update it" | |
50 | payload=$(echo "$response" | tr -d " " | sed 's/"updated_at":[0-9]\+,//g' | sed 's/"meta":{}}]}/"meta":{}},{"content":['\""$txtvalue"\"'],"enabled":true}]}/') | |
51 | fi | |
52 | ||
53 | # For wildcard cert, the main root domain and the wildcard domain have the same txt subdomain name, so | |
54 | # we can not use updating anymore. | |
55 | # count=$(printf "%s\n" "$response" | _egrep_o "\"count\":[^,]*" | cut -d : -f 2) | |
56 | # _debug count "$count" | |
57 | # if [ "$count" = "0" ]; then | |
58 | _info "Adding record" | |
59 | if _gcore_rest PUT "zones/$_zone_name/$fulldomain/TXT" "$payload"; then | |
60 | if _contains "$response" "$txtvalue"; then | |
61 | _info "Added, OK" | |
62 | return 0 | |
63 | elif _contains "$response" "rrset is already exists"; then | |
64 | _info "Already exists, OK" | |
65 | return 0 | |
66 | else | |
67 | _err "Add txt record error." | |
68 | return 1 | |
69 | fi | |
70 | fi | |
71 | _err "Add txt record error." | |
72 | return 1 | |
73 | } | |
74 | ||
75 | #fulldomain txtvalue | |
76 | dns_gcore_rm() { | |
77 | fulldomain=$1 | |
78 | txtvalue=$2 | |
79 | ||
80 | GCORE_Key="${GCORE_Key:-$(_readaccountconf_mutable GCORE_Key)}" | |
81 | ||
82 | _debug "First detect the root zone" | |
83 | if ! _get_root "$fulldomain"; then | |
84 | _err "invalid domain" | |
85 | return 1 | |
86 | fi | |
87 | _debug _zone_name "$_zone_name" | |
88 | _debug _sub_domain "$_sub_domain" | |
89 | _debug _domain "$_domain" | |
90 | ||
91 | _debug "Getting txt records" | |
92 | _gcore_rest GET "zones/$_zone_name/$fulldomain/TXT" | |
93 | ||
94 | if echo "$response" | grep "record is not found" >/dev/null; then | |
95 | _info "No such txt recrod" | |
96 | return 0 | |
97 | fi | |
98 | ||
99 | if ! echo "$response" | tr -d " " | grep \"name\":\""$fulldomain"\",\"type\":\"TXT\" >/dev/null; then | |
100 | _err "Error: $response" | |
101 | return 1 | |
102 | fi | |
103 | ||
104 | if ! echo "$response" | tr -d " " | grep \""$txtvalue"\" >/dev/null; then | |
105 | _info "No such txt recrod" | |
106 | return 0 | |
107 | fi | |
108 | ||
109 | count="$(echo "$response" | grep -o "content" | wc -l)" | |
110 | ||
111 | if [ "$count" = "1" ]; then | |
112 | if ! _gcore_rest DELETE "zones/$_zone_name/$fulldomain/TXT"; then | |
113 | _err "Delete record error. $response" | |
114 | return 1 | |
115 | fi | |
116 | return 0 | |
117 | fi | |
118 | ||
119 | payload="$(echo "$response" | tr -d " " | sed 's/"updated_at":[0-9]\+,//g' | sed 's/{"id":[0-9]\+,"content":\["'"$txtvalue"'"\],"enabled":true,"meta":{}}//' | sed 's/\[,/\[/' | sed 's/,,/,/' | sed 's/,\]/\]/')" | |
120 | if ! _gcore_rest PUT "zones/$_zone_name/$fulldomain/TXT" "$payload"; then | |
121 | _err "Delete record error. $response" | |
122 | fi | |
123 | } | |
124 | ||
125 | #################### Private functions below ################################## | |
126 | #_acme-challenge.sub.domain.com | |
127 | #returns | |
128 | # _sub_domain=_acme-challenge.sub or _acme-challenge | |
129 | # _domain=domain.com | |
130 | # _zone_name=domain.com or sub.domain.com | |
131 | _get_root() { | |
132 | domain=$1 | |
133 | i=1 | |
134 | p=1 | |
135 | ||
136 | while true; do | |
137 | h=$(printf "%s" "$domain" | cut -d . -f $i-100) | |
138 | _debug h "$h" | |
139 | if [ -z "$h" ]; then | |
140 | #not valid | |
141 | return 1 | |
142 | fi | |
143 | ||
144 | if ! _gcore_rest GET "zones/$h"; then | |
145 | return 1 | |
146 | fi | |
147 | ||
148 | if _contains "$response" "\"name\":\"$h\""; then | |
149 | _zone_name=$h | |
150 | if [ "$_zone_name" ]; then | |
151 | _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p) | |
152 | _domain=$h | |
153 | return 0 | |
154 | fi | |
155 | return 1 | |
156 | fi | |
157 | p=$i | |
158 | i=$(_math "$i" + 1) | |
159 | done | |
160 | return 1 | |
161 | } | |
162 | ||
163 | _gcore_rest() { | |
164 | m=$1 | |
165 | ep="$2" | |
166 | data="$3" | |
167 | _debug "$ep" | |
168 | ||
169 | key_trimmed=$(echo "$GCORE_Key" | tr -d '"') | |
170 | ||
171 | export _H1="Content-Type: application/json" | |
172 | export _H2="Authorization: APIKey $key_trimmed" | |
173 | ||
174 | if [ "$m" != "GET" ]; then | |
175 | _debug data "$data" | |
176 | response="$(_post "$data" "$GCORE_Api/$ep" "" "$m")" | |
177 | else | |
178 | response="$(_get "$GCORE_Api/$ep")" | |
179 | fi | |
180 | ||
181 | if [ "$?" != "0" ]; then | |
182 | _err "error $ep" | |
183 | return 1 | |
184 | fi | |
185 | _debug2 response "$response" | |
186 | return 0 | |
187 | } |