]>
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 | ||
bec26ce7 | 72 | _record_id="$(echo "$response" | 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() { | |
96 | fulldomain=$1 | |
97 | i=1 | |
98 | while true; do | |
99 | h=$(printf "%s" "$fulldomain" | cut -d . -f $i-100) | |
100 | _debug h "$h" | |
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 | ||
109 | if _startswith "$response" "\{\"data\":"; then | |
f02af8d4 | 110 | if _contains "$response" "\"id\":\"$h\""; then |
582c7780 | 111 | _sub_domain="$(echo "$fulldomain" | sed "s/\\.$h\$//")" |
112 | _domain=$h | |
113 | return 0 | |
582c7780 | 114 | fi |
582c7780 | 115 | fi |
116 | i=$(_math "$i" + 1) | |
117 | done | |
118 | ||
119 | _debug "root domain not found" | |
582c7780 | 120 | return 1 |
121 | } | |
122 | ||
123 | _variomedia_rest() { | |
124 | m=$1 | |
125 | ep="$2" | |
126 | data="$3" | |
127 | _debug "$ep" | |
128 | ||
582c7780 | 129 | export _H1="Authorization: token $VARIOMEDIA_API_TOKEN" |
130 | export _H2="Content-Type: application/vnd.api+json" | |
131 | export _H3="Accept: application/vnd.variomedia.v1+json" | |
132 | ||
133 | if [ "$m" != "GET" ]; then | |
134 | _debug data "$data" | |
135 | response="$(_post "$data" "$VARIOMEDIA_API/$ep" "" "$m")" | |
136 | else | |
137 | response="$(_get "$VARIOMEDIA_API/$ep")" | |
138 | fi | |
139 | ||
140 | if [ "$?" != "0" ]; then | |
141 | _err "Error $ep" | |
142 | return 1 | |
143 | fi | |
144 | ||
145 | _debug2 response "$response" | |
146 | return 0 | |
147 | } |