]>
Commit | Line | Data |
---|---|---|
572adbaa MP |
1 | #!/usr/bin/env sh |
2 | ||
3 | #ACTIVE24_Token="sdfsdfsdfljlbjkljlkjsdfoiwje" | |
4 | ||
5 | ACTIVE24_Api="https://api.active24.com" | |
6 | ||
7 | ######## Public functions ##################### | |
8 | ||
9 | # Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs" | |
10 | # Used to add txt record | |
11 | dns_active24_add() { | |
12 | fulldomain=$1 | |
13 | txtvalue=$2 | |
14 | ||
15 | _active24_init | |
16 | ||
17 | _info "Adding txt record" | |
18 | if _active24_rest POST "dns/$_domain/txt/v1" "{\"name\":\"$_sub_domain\",\"text\":\"$txtvalue\",\"ttl\":0}"; then | |
19 | if _contains "$response" "errors"; then | |
20 | _err "Add txt record error." | |
21 | return 1 | |
22 | else | |
23 | _info "Added, OK" | |
24 | return 0 | |
25 | fi | |
26 | fi | |
27 | _err "Add txt record error." | |
28 | return 1 | |
29 | } | |
30 | ||
31 | # Usage: fulldomain txtvalue | |
32 | # Used to remove the txt record after validation | |
33 | dns_active24_rm() { | |
34 | fulldomain=$1 | |
35 | txtvalue=$2 | |
36 | ||
37 | _active24_init | |
38 | ||
39 | _debug "Getting txt records" | |
40 | _active24_rest GET "dns/$_domain/records/v1" | |
41 | ||
42 | if _contains "$response" "errors"; then | |
43 | _err "Error" | |
44 | return 1 | |
45 | fi | |
46 | ||
47 | hash_ids=$(echo "$response" | _egrep_o "[^{]+${txtvalue}[^}]+" | _egrep_o "hashId\":\"[^\"]+" | cut -c10-) | |
48 | ||
49 | for hash_id in $hash_ids; do | |
50 | _debug "Removing hash_id" "$hash_id" | |
51 | if _active24_rest DELETE "dns/$_domain/$hash_id/v1" ""; then | |
52 | if _contains "$response" "errors"; then | |
53 | _err "Unable to remove txt record." | |
54 | return 1 | |
55 | else | |
56 | _info "Removed txt record." | |
57 | return 0 | |
58 | fi | |
59 | fi | |
60 | done | |
61 | ||
62 | _err "No txt records found." | |
63 | return 1 | |
64 | } | |
65 | ||
66 | #################### Private functions below ################################## | |
67 | #_acme-challenge.www.domain.com | |
68 | #returns | |
69 | # _sub_domain=_acme-challenge.www | |
70 | # _domain=domain.com | |
71 | # _domain_id=sdjkglgdfewsdfg | |
72 | _get_root() { | |
73 | domain=$1 | |
74 | ||
75 | if ! _active24_rest GET "dns/domains/v1"; then | |
76 | return 1 | |
77 | fi | |
78 | ||
79 | i=2 | |
80 | p=1 | |
81 | while true; do | |
82 | h=$(printf "%s" "$domain" | cut -d . -f $i-100) | |
83 | _debug "h" "$h" | |
84 | if [ -z "$h" ]; then | |
85 | #not valid | |
86 | return 1 | |
87 | fi | |
88 | ||
89 | if _contains "$response" "\"$h\"" >/dev/null; then | |
90 | _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p) | |
91 | _domain=$h | |
92 | return 0 | |
93 | fi | |
94 | p=$i | |
95 | i=$(_math "$i" + 1) | |
96 | done | |
97 | return 1 | |
98 | } | |
99 | ||
100 | _active24_rest() { | |
101 | m=$1 | |
102 | ep="$2" | |
103 | data="$3" | |
104 | _debug "$ep" | |
105 | ||
106 | export _H1="Authorization: Bearer $ACTIVE24_Token" | |
107 | ||
108 | if [ "$m" != "GET" ]; then | |
109 | _debug "data" "$data" | |
110 | response="$(_post "$data" "$ACTIVE24_Api/$ep" "" "$m" "application/json")" | |
111 | else | |
112 | response="$(_get "$ACTIVE24_Api/$ep")" | |
113 | fi | |
114 | ||
115 | if [ "$?" != "0" ]; then | |
116 | _err "error $ep" | |
117 | return 1 | |
118 | fi | |
119 | _debug2 response "$response" | |
120 | return 0 | |
121 | } | |
122 | ||
123 | _active24_init() { | |
124 | ACTIVE24_Token="${ACTIVE24_Token:-$(_readaccountconf_mutable ACTIVE24_Token)}" | |
125 | if [ -z "$ACTIVE24_Token" ]; then | |
126 | ACTIVE24_Token="" | |
127 | _err "You didn't specify a Active24 api token yet." | |
128 | _err "Please create the token and try again." | |
129 | return 1 | |
130 | fi | |
131 | ||
f6d6658d | 132 | _saveaccountconf_mutable ACTIVE24_Token "$ACTIVE24_Token" |
572adbaa MP |
133 | |
134 | _debug "First detect the root zone" | |
135 | if ! _get_root "$fulldomain"; then | |
136 | _err "invalid domain" | |
137 | return 1 | |
138 | fi | |
139 | _debug _sub_domain "$_sub_domain" | |
140 | _debug _domain "$_domain" | |
141 | } |