]>
Commit | Line | Data |
---|---|---|
1167cdca SH |
1 | #!/usr/bin/env sh |
2 | ||
3 | #NederHost_Key="sdfgikogfdfghjklkjhgfcdcfghjk" | |
4 | ||
5 | NederHost_Api="https://api.nederhost.nl/dns/v1" | |
6 | ||
7 | ######## Public functions ##################### | |
8 | ||
9 | #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs" | |
10 | dns_nederhost_add() { | |
11 | fulldomain=$1 | |
12 | txtvalue=$2 | |
13 | ||
14 | NederHost_Key="${NederHost_Key:-$(_readaccountconf_mutable NederHost_Key)}" | |
15 | if [ -z "$NederHost_Key" ]; then | |
16 | NederHost_Key="" | |
17 | _err "You didn't specify a NederHost api key." | |
18 | _err "You can get yours from https://www.nederhost.nl/mijn_nederhost" | |
19 | return 1 | |
20 | fi | |
21 | ||
22 | #save the api key and email to the account conf file. | |
23 | _saveaccountconf_mutable NederHost_Key "$NederHost_Key" | |
24 | ||
25 | _debug "First detect the root zone" | |
26 | if ! _get_root "$fulldomain"; then | |
27 | _err "invalid domain" | |
28 | return 1 | |
29 | fi | |
30 | _debug _sub_domain "$_sub_domain" | |
31 | _debug _domain "$_domain" | |
32 | ||
33 | _info "Adding record" | |
34 | if _nederhost_rest PATCH "zones/$_domain/records/$fulldomain/TXT" "[{\"content\":\"$txtvalue\",\"ttl\":60}]"; then | |
35 | if _contains "$response" "$fulldomain"; then | |
36 | _info "Added, OK" | |
37 | return 0 | |
38 | else | |
39 | _err "Add txt record error." | |
40 | return 1 | |
41 | fi | |
42 | fi | |
43 | _err "Add txt record error." | |
44 | return 1 | |
45 | ||
46 | } | |
47 | ||
48 | #fulldomain txtvalue | |
49 | dns_nederhost_rm() { | |
50 | fulldomain=$1 | |
51 | txtvalue=$2 | |
52 | ||
53 | NederHost_Key="${NederHost_Key:-$(_readaccountconf_mutable NederHost_Key)}" | |
54 | if [ -z "$NederHost_Key" ]; then | |
55 | NederHost_Key="" | |
56 | _err "You didn't specify a NederHost api key." | |
57 | _err "You can get yours from https://www.nederhost.nl/mijn_nederhost" | |
58 | return 1 | |
59 | fi | |
60 | ||
61 | _debug "First detect the root zone" | |
62 | if ! _get_root "$fulldomain"; then | |
63 | _err "invalid domain" | |
64 | return 1 | |
65 | fi | |
66 | ||
67 | _debug _sub_domain "$_sub_domain" | |
68 | _debug _domain "$_domain" | |
69 | ||
70 | _debug "Removing txt record" | |
71 | _nederhost_rest DELETE "zones/${_domain}/records/$fulldomain/TXT?content=$txtvalue" | |
72 | ||
73 | } | |
74 | ||
75 | #################### Private functions below ################################## | |
76 | #_acme-challenge.www.domain.com | |
77 | #returns | |
78 | # _sub_domain=_acme-challenge.www | |
79 | # _domain=domain.com | |
80 | _get_root() { | |
81 | domain=$1 | |
82 | i=2 | |
83 | p=1 | |
84 | while true; do | |
85 | _domain=$(printf "%s" "$domain" | cut -d . -f $i-100) | |
86 | _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p) | |
87 | _debug _domain "$_domain" | |
88 | if [ -z "$_domain" ]; then | |
89 | #not valid | |
90 | return 1 | |
91 | fi | |
92 | ||
93 | if _nederhost_rest GET "zones/${_domain}"; then | |
b3e3e080 | 94 | if [ "${_code}" = "204" ]; then |
44dcb0d0 | 95 | return 0 |
1167cdca SH |
96 | fi |
97 | else | |
44dcb0d0 | 98 | return 1 |
1167cdca SH |
99 | fi |
100 | p=$i | |
101 | i=$(_math "$i" + 1) | |
102 | done | |
103 | return 1 | |
104 | } | |
105 | ||
106 | _nederhost_rest() { | |
107 | m=$1 | |
108 | ep="$2" | |
109 | data="$3" | |
110 | _debug "$ep" | |
111 | ||
112 | export _H1="Authorization: Bearer $NederHost_Key" | |
113 | export _H2="Content-Type: application/json" | |
114 | ||
1167cdca SH |
115 | if [ "$m" != "GET" ]; then |
116 | _debug data "$data" | |
117 | response="$(_post "$data" "$NederHost_Api/$ep" "" "$m")" | |
118 | else | |
119 | response="$(_get "$NederHost_Api/$ep")" | |
120 | fi | |
121 | ||
122 | _code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r\\n")" | |
123 | _debug "http response code $_code" | |
124 | ||
125 | if [ "$?" != "0" ]; then | |
126 | _err "error $ep" | |
127 | return 1 | |
128 | fi | |
129 | _debug2 response "$response" | |
130 | return 0 | |
131 | } |