]>
Commit | Line | Data |
---|---|---|
1c58913e JZ |
1 | #!/usr/bin/env sh |
2 | ||
3 | # | |
4 | #AURORA_Key="sdfsdfsdfljlbjkljlkjsdfoiwje" | |
5 | # | |
6 | #AURORA_Secret="sdfsdfsdfljlbjkljlkjsdfoiwje" | |
7 | ||
8 | AURORA_Api="https://api.auroradns.eu" | |
9 | ||
10 | ######## Public functions ##################### | |
11 | ||
12 | #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs" | |
13 | dns_aurora_add() { | |
14 | fulldomain=$1 | |
15 | txtvalue=$2 | |
16 | ||
17 | AURORA_Key="${AURORA_Key:-$(_readaccountconf_mutable AURORA_Key)}" | |
18 | AURORA_Secret="${AURORA_Secret:-$(_readaccountconf_mutable AURORA_Secret)}" | |
19 | ||
20 | if [ -z "$AURORA_Key" ] || [ -z "$AURORA_Secret" ]; then | |
21 | AURORA_Key="" | |
22 | AURORA_Secret="" | |
23 | _err "You didn't specify an Aurora api key and secret yet." | |
24 | _err "You can get yours from here https://cp.pcextreme.nl/auroradns/users." | |
25 | return 1 | |
26 | fi | |
27 | ||
28 | #save the api key and secret to the account conf file. | |
29 | _saveaccountconf_mutable AURORA_Key "$AURORA_Key" | |
30 | _saveaccountconf_mutable AURORA_Secret "$AURORA_Secret" | |
31 | ||
32 | _debug "First detect the root zone" | |
33 | if ! _get_root "$fulldomain"; then | |
34 | _err "invalid domain" | |
35 | return 1 | |
36 | fi | |
37 | _debug _domain_id "$_domain_id" | |
38 | _debug _sub_domain "$_sub_domain" | |
39 | _debug _domain "$_domain" | |
40 | ||
41 | _info "Adding record" | |
42 | if _aurora_rest POST "zones/$_domain_id/records" "{\"type\":\"TXT\",\"name\":\"$_sub_domain\",\"content\":\"$txtvalue\",\"ttl\":300}"; then | |
43 | if _contains "$response" "$txtvalue"; then | |
44 | _info "Added, OK" | |
45 | return 0 | |
46 | elif _contains "$response" "RecordExistsError"; then | |
47 | _info "Already exists, OK" | |
48 | return 0 | |
49 | else | |
50 | _err "Add txt record error." | |
51 | return 1 | |
52 | fi | |
53 | fi | |
54 | _err "Add txt record error." | |
55 | return 1 | |
56 | ||
57 | } | |
58 | ||
59 | #fulldomain txtvalue | |
60 | dns_aurora_rm() { | |
61 | fulldomain=$1 | |
62 | txtvalue=$2 | |
63 | ||
64 | AURORA_Key="${AURORA_Key:-$(_readaccountconf_mutable AURORA_Key)}" | |
65 | AURORA_Secret="${AURORA_Secret:-$(_readaccountconf_mutable AURORA_Secret)}" | |
66 | ||
67 | _debug "First detect the root zone" | |
68 | if ! _get_root "$fulldomain"; then | |
69 | _err "invalid domain" | |
70 | return 1 | |
71 | fi | |
72 | _debug _domain_id "$_domain_id" | |
73 | _debug _sub_domain "$_sub_domain" | |
74 | _debug _domain "$_domain" | |
75 | ||
76 | _debug "Getting records" | |
77 | _aurora_rest GET "zones/${_domain_id}/records" | |
78 | ||
79 | if ! _contains "$response" "$txtvalue"; then | |
80 | _info "Don't need to remove." | |
81 | else | |
82 | records=$(echo "$response" | _normalizeJson | tr -d "[]" | sed "s/},{/}|{/g" | tr "|" "\n") | |
83 | if [ "$(echo "$records" | wc -l)" -le 2 ]; then | |
84 | _err "Can not parse records." | |
85 | return 1 | |
86 | fi | |
87 | record_id=$(echo "$records" | grep "\"type\": *\"TXT\"" | grep "\"name\": *\"$_sub_domain\"" | grep "\"content\": *\"$txtvalue\"" | _egrep_o "\"id\": *\"[^\"]*\"" | cut -d : -f 2 | tr -d \" | _head_n 1 | tr -d " ") | |
88 | _debug "record_id" "$record_id" | |
89 | if [ -z "$record_id" ]; then | |
90 | _err "Can not get record id to remove." | |
91 | return 1 | |
92 | fi | |
93 | if ! _aurora_rest DELETE "zones/$_domain_id/records/$record_id"; then | |
94 | _err "Delete record error." | |
95 | return 1 | |
96 | fi | |
97 | fi | |
98 | return 0 | |
99 | ||
100 | } | |
101 | ||
102 | #################### Private functions below ################################## | |
103 | #_acme-challenge.www.domain.com | |
104 | #returns | |
105 | # _sub_domain=_acme-challenge.www | |
106 | # _domain=domain.com | |
107 | # _domain_id=sdjkglgdfewsdfg | |
108 | _get_root() { | |
109 | domain=$1 | |
110 | i=1 | |
111 | p=1 | |
112 | ||
113 | while true; do | |
114 | h=$(printf "%s" "$domain" | cut -d . -f $i-100) | |
115 | _debug h "$h" | |
116 | if [ -z "$h" ]; then | |
117 | #not valid | |
118 | return 1 | |
119 | fi | |
120 | ||
121 | if ! _aurora_rest GET "zones/$h"; then | |
122 | return 1 | |
123 | fi | |
124 | ||
125 | if _contains "$response" "\"name\": \"$h\""; then | |
126 | _domain_id=$(echo "$response" | _normalizeJson | tr -d "{}" | tr "," "\n" | grep "\"id\": *\"" | cut -d : -f 2 | tr -d \" | _head_n 1 | tr -d " ") | |
127 | _debug _domain_id "$_domain_id" | |
128 | if [ "$_domain_id" ]; then | |
129 | _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p) | |
130 | _domain=$h | |
131 | return 0 | |
132 | fi | |
133 | return 1 | |
134 | fi | |
135 | p=$i | |
136 | i=$(_math "$i" + 1) | |
137 | done | |
138 | return 1 | |
139 | } | |
140 | ||
141 | _aurora_rest() { | |
142 | m=$1 | |
143 | ep="$2" | |
144 | data="$3" | |
145 | _debug "$ep" | |
146 | ||
147 | key_trimmed=$(echo "$AURORA_Key" | tr -d '"') | |
148 | secret_trimmed=$(echo "$AURORA_Secret" | tr -d '"') | |
149 | ||
150 | timestamp=$(date -u +"%Y%m%dT%H%M%SZ") | |
151 | signature=$(printf "%s/%s%s" "$m" "$ep" "$timestamp" | _hmac sha256 "$(printf "%s" "$secret_trimmed" | _hex_dump | tr -d " ")" | _base64) | |
152 | authorization=$(printf "AuroraDNSv1 %s" "$(printf "%s:%s" "$key_trimmed" "$signature" | _base64)") | |
153 | ||
154 | export _H1="Content-Type: application/json; charset=UTF-8" | |
155 | export _H2="X-AuroraDNS-Date: $timestamp" | |
156 | export _H3="Authorization: $authorization" | |
157 | ||
158 | if [ "$m" != "GET" ]; then | |
159 | _debug data "$data" | |
160 | response="$(_post "$data" "$AURORA_Api/$ep" "" "$m")" | |
161 | else | |
162 | response="$(_get "$AURORA_Api/$ep")" | |
163 | fi | |
164 | ||
165 | if [ "$?" != "0" ]; then | |
166 | _err "error $ep" | |
167 | return 1 | |
168 | fi | |
169 | _debug2 response "$response" | |
170 | return 0 | |
171 | } |