]> git.proxmox.com Git - mirror_acme.sh.git/blame - dnsapi/dns_njalla.sh
Merge pull request #4531 from NCDGHA/bugfix/issue_4530_fix_http_status_503
[mirror_acme.sh.git] / dnsapi / dns_njalla.sh
CommitLineData
81036894
PB
1#!/usr/bin/env sh
2
3#
4#NJALLA_Token="sdfsdfsdfljlbjkljlkjsdfoiwje"
5
6NJALLA_Api="https://njal.la/api/1/"
7
8######## Public functions #####################
9
10#Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
11dns_njalla_add() {
12 fulldomain=$1
13 txtvalue=$2
14
15 NJALLA_Token="${NJALLA_Token:-$(_readaccountconf_mutable NJALLA_Token)}"
16
17 if [ "$NJALLA_Token" ]; then
18 _saveaccountconf_mutable NJALLA_Token "$NJALLA_Token"
19 else
20 NJALLA_Token=""
21 _err "You didn't specify a Njalla api token yet."
22 return 1
23 fi
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 # For wildcard cert, the main root domain and the wildcard domain have the same txt subdomain name, so
34 # we can not use updating anymore.
35 # count=$(printf "%s\n" "$response" | _egrep_o "\"count\":[^,]*" | cut -d : -f 2)
36 # _debug count "$count"
37 # if [ "$count" = "0" ]; then
38 _info "Adding record"
39 if _njalla_rest "{\"method\":\"add-record\",\"params\":{\"domain\":\"$_domain\",\"type\":\"TXT\",\"name\":\"$_sub_domain\",\"content\":\"$txtvalue\",\"ttl\":120}}"; then
40 if _contains "$response" "$txtvalue"; then
41 _info "Added, OK"
42 return 0
43 else
44 _err "Add txt record error."
45 return 1
46 fi
47 fi
48 _err "Add txt record error."
49 return 1
50
51}
52
53#fulldomain txtvalue
54dns_njalla_rm() {
55 fulldomain=$1
56 txtvalue=$2
57
58 NJALLA_Token="${NJALLA_Token:-$(_readaccountconf_mutable NJALLA_Token)}"
59
60 if [ "$NJALLA_Token" ]; then
61 _saveaccountconf_mutable NJALLA_Token "$NJALLA_Token"
62 else
63 NJALLA_Token=""
64 _err "You didn't specify a Njalla api token yet."
65 return 1
66 fi
67
68 _debug "First detect the root zone"
69 if ! _get_root "$fulldomain"; then
70 _err "invalid domain"
71 return 1
72 fi
73 _debug _sub_domain "$_sub_domain"
74 _debug _domain "$_domain"
75
76 _debug "Getting records for domain"
77 if ! _njalla_rest "{\"method\":\"list-records\",\"params\":{\"domain\":\"${_domain}\"}}"; then
78 return 1
79 fi
80
81 if ! echo "$response" | tr -d " " | grep "\"id\":" >/dev/null; then
82 _err "Error: $response"
83 return 1
84 fi
85
86 records=$(echo "$response" | _egrep_o "\"records\":\s?\[(.*)\]\}" | _egrep_o "\[.*\]" | _egrep_o "\{[^\{\}]*\"id\":[^\{\}]*\}")
87 count=$(echo "$records" | wc -l)
88 _debug count "$count"
89
90 if [ "$count" = "0" ]; then
91 _info "Don't need to remove."
92 else
9bbcfead 93 echo "$records" | while read -r record; do
81036894
PB
94 record_name=$(echo "$record" | _egrep_o "\"name\":\s?\"[^\"]*\"" | cut -d : -f 2 | tr -d " " | tr -d \")
95 record_content=$(echo "$record" | _egrep_o "\"content\":\s?\"[^\"]*\"" | cut -d : -f 2 | tr -d " " | tr -d \")
96 record_id=$(echo "$record" | _egrep_o "\"id\":\s?[0-9]+" | cut -d : -f 2 | tr -d " " | tr -d \")
97 if [ "$_sub_domain" = "$record_name" ]; then
98 if [ "$txtvalue" = "$record_content" ]; then
99 _debug "record_id" "$record_id"
100 if ! _njalla_rest "{\"method\":\"remove-record\",\"params\":{\"domain\":\"${_domain}\",\"id\":${record_id}}}"; then
101 _err "Delete record error."
102 return 1
103 fi
104 echo "$response" | tr -d " " | grep "\"result\"" >/dev/null
105 fi
106 fi
107 done
108 fi
109
110}
111
112#################### Private functions below ##################################
113#_acme-challenge.www.domain.com
114#returns
115# _sub_domain=_acme-challenge.www
116# _domain=domain.com
117# _domain_id=sdjkglgdfewsdfg
118_get_root() {
119 domain=$1
120 i=1
121 p=1
122
123 while true; do
124 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
125 _debug h "$h"
126 if [ -z "$h" ]; then
127 #not valid
128 return 1
129 fi
130
81036894
PB
131 if ! _njalla_rest "{\"method\":\"get-domain\",\"params\":{\"domain\":\"${h}\"}}"; then
132 return 1
133 fi
134
135 if _contains "$response" "\"$h\""; then
136 _domain_returned=$(echo "$response" | _egrep_o "\{\"name\": *\"[^\"]*\"" | _head_n 1 | cut -d : -f 2 | tr -d \" | tr -d " ")
137 if [ "$_domain_returned" ]; then
138 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
139 _domain=$h
140 return 0
141 fi
142 return 1
143 fi
144 p=$i
145 i=$(_math "$i" + 1)
146 done
147 return 1
148}
149
150_njalla_rest() {
151 data="$1"
152
153 token_trimmed=$(echo "$NJALLA_Token" | tr -d '"')
154
155 export _H1="Content-Type: application/json"
156 export _H2="Accept: application/json"
157 export _H3="Authorization: Njalla $token_trimmed"
158
159 _debug data "$data"
160 response="$(_post "$data" "$NJALLA_Api" "" "POST")"
161
162 if [ "$?" != "0" ]; then
d904df57 163 _err "error $data"
81036894
PB
164 return 1
165 fi
166 _debug2 response "$response"
167 return 0
168}