]>
Commit | Line | Data |
---|---|---|
582c7780 | 1 | #!/usr/bin/env sh |
2 | ||
3 | # | |
4 | #VARIOMEDIA_API_TOKEN=000011112222333344445555666677778888 | |
5 | ||
6 | VARIOMEDIA_API="https://api.variomedia.de" | |
7 | ||
dca6a4bb | 8 | ######## Public functions ##################### |
582c7780 | 9 | |
dca6a4bb | 10 | #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs" |
582c7780 | 11 | dns_variomedia_add() { |
12 | fulldomain=$1 | |
13 | txtvalue=$2 | |
14 | _debug fulldomain "$fulldomain" | |
15 | _debug txtvalue "$txtvalue" | |
16 | ||
17 | VARIOMEDIA_API_TOKEN="${VARIOMEDIA_API_TOKEN:-$(_readaccountconf_mutable VARIOMEDIA_API_TOKEN)}" | |
18 | if test -z "$VARIOMEDIA_API_TOKEN"; then | |
19 | VARIOMEDIA_API_TOKEN="" | |
20 | _err 'VARIOMEDIA_API_TOKEN was not exported' | |
21 | return 1 | |
22 | fi | |
23 | ||
24 | _saveaccountconf_mutable VARIOMEDIA_API_TOKEN "$VARIOMEDIA_API_TOKEN" | |
25 | ||
26 | _debug 'First detect the root zone' | |
27 | if ! _get_root "$fulldomain"; then | |
28 | return 1 | |
29 | fi | |
30 | _debug _sub_domain "$_sub_domain" | |
31 | _debug _domain "$_domain" | |
32 | ||
582c7780 | 33 | if ! _variomedia_rest POST "dns-records" "{\"data\": {\"type\": \"dns-record\", \"attributes\": {\"record_type\": \"TXT\", \"name\": \"$_sub_domain\", \"domain\": \"$_domain\", \"data\": \"$txtvalue\", \"ttl\":300}}}"; then |
34 | _err "$response" | |
35 | return 1 | |
36 | fi | |
37 | ||
38 | _debug2 _response "$response" | |
39 | return 0 | |
40 | } | |
41 | ||
42 | #fulldomain txtvalue | |
43 | dns_variomedia_rm() { | |
44 | fulldomain=$1 | |
45 | txtvalue=$2 | |
46 | _debug fulldomain "$fulldomain" | |
47 | _debug txtvalue "$txtvalue" | |
48 | ||
49 | VARIOMEDIA_API_TOKEN="${VARIOMEDIA_API_TOKEN:-$(_readaccountconf_mutable VARIOMEDIA_API_TOKEN)}" | |
50 | if test -z "$VARIOMEDIA_API_TOKEN"; then | |
51 | VARIOMEDIA_API_TOKEN="" | |
52 | _err 'VARIOMEDIA_API_TOKEN was not exported' | |
53 | return 1 | |
54 | fi | |
55 | ||
56 | _saveaccountconf_mutable VARIOMEDIA_API_TOKEN "$VARIOMEDIA_API_TOKEN" | |
57 | ||
58 | _debug 'First detect the root zone' | |
59 | if ! _get_root "$fulldomain"; then | |
60 | return 1 | |
61 | fi | |
62 | _debug _sub_domain "$_sub_domain" | |
63 | _debug _domain "$_domain" | |
64 | ||
65 | _debug 'Getting txt records' | |
66 | ||
67 | if ! _variomedia_rest GET "dns-records?filter[domain]=$_domain"; then | |
68 | _err 'Error' | |
69 | return 1 | |
70 | fi | |
71 | ||
ae4c186f | 72 | _record_id="$(echo "$response" | sed -E 's/,"tags":\[[^]]*\]//g' | cut -d '[' -f2 | cut -d']' -f1 | sed 's/},[ \t]*{/\},§\{/g' | tr § '\n' | grep "$_sub_domain" | grep -- "$txtvalue" | sed 's/^{//;s/}[,]?$//' | tr , '\n' | tr -d '\"' | grep ^id | cut -d : -f2 | tr -d ' ')" |
582c7780 | 73 | _debug _record_id "$_record_id" |
74 | if [ "$_record_id" ]; then | |
75 | _info "Successfully retrieved the record id for ACME challenge." | |
76 | else | |
77 | _info "Empty record id, it seems no such record." | |
78 | return 0 | |
79 | fi | |
80 | ||
81 | if ! _variomedia_rest DELETE "/dns-records/$_record_id"; then | |
82 | _err "$response" | |
83 | return 1 | |
84 | fi | |
85 | ||
86 | _debug2 _response "$response" | |
87 | return 0 | |
88 | } | |
89 | ||
dca6a4bb | 90 | #################### Private functions below ################################## |
582c7780 | 91 | #_acme-challenge.www.domain.com |
92 | #returns | |
93 | # _sub_domain=_acme-challenge.www | |
94 | # _domain=domain.com | |
95 | _get_root() { | |
ae4c186f TG |
96 | domain=$1 |
97 | i=2 | |
98 | p=1 | |
582c7780 | 99 | while true; do |
ae4c186f | 100 | h=$(printf "%s" "$domain" | cut -d . -f $i-100) |
582c7780 | 101 | if [ -z "$h" ]; then |
102 | return 1 | |
103 | fi | |
104 | ||
105 | if ! _variomedia_rest GET "domains/$h"; then | |
106 | return 1 | |
107 | fi | |
108 | ||
ae4c186f TG |
109 | if _contains "$response" "\"id\":\"$h\""; then |
110 | _sub_domain=$(printf "%s" "$domain" | cut -d '.' -f 1-$p) | |
111 | _domain="$h" | |
112 | return 0 | |
582c7780 | 113 | fi |
ae4c186f | 114 | p=$i |
582c7780 | 115 | i=$(_math "$i" + 1) |
116 | done | |
582c7780 | 117 | return 1 |
118 | } | |
119 | ||
120 | _variomedia_rest() { | |
121 | m=$1 | |
122 | ep="$2" | |
123 | data="$3" | |
124 | _debug "$ep" | |
125 | ||
582c7780 | 126 | export _H1="Authorization: token $VARIOMEDIA_API_TOKEN" |
127 | export _H2="Content-Type: application/vnd.api+json" | |
128 | export _H3="Accept: application/vnd.variomedia.v1+json" | |
129 | ||
130 | if [ "$m" != "GET" ]; then | |
131 | _debug data "$data" | |
132 | response="$(_post "$data" "$VARIOMEDIA_API/$ep" "" "$m")" | |
133 | else | |
134 | response="$(_get "$VARIOMEDIA_API/$ep")" | |
135 | fi | |
136 | ||
137 | if [ "$?" != "0" ]; then | |
138 | _err "Error $ep" | |
139 | return 1 | |
140 | fi | |
141 | ||
142 | _debug2 response "$response" | |
143 | return 0 | |
144 | } |