]> git.proxmox.com Git - mirror_acme.sh.git/blame - dnsapi/dns_opnsense.sh
another whitespace
[mirror_acme.sh.git] / dnsapi / dns_opnsense.sh
CommitLineData
4bf1f579
JL
1#!/usr/bin/env sh
2
3#OPNsense Bind API
4#https://docs.opnsense.org/development/api.html
5#
6#OPNs_Host="opnsense.example.com"
7#OPNs_Port="443"
8#OPNs_Key="qocfU9RSbt8vTIBcnW8bPqCrpfAHMDvj5OzadE7Str+rbjyCyk7u6yMrSCHtBXabgDDXx/dY0POUp7ZA"
9#OPNs_Token="pZEQ+3ce8dDlfBBdg3N8EpqpF5I1MhFqdxX06le6Gl8YzyQvYCfCzNaFX9O9+IOSyAs7X71fwdRiZ+Lv"
dfb4883c 10#OPNs_Api_Insecure=0 # Set 1 for insecure and 0 for secure -> difference is whether ssl cert is checked for validity (0) or whether it is just accepted (1)
4bf1f579
JL
11
12######## Public functions #####################
13#Usage: add _acme-challenge.www.domain.com "123456789ABCDEF0000000000000000000000000000000000000"
14#fulldomain
15#txtvalue
16dns_opnsense_add() {
17 fulldomain=$1
18 txtvalue=$2
19
20 _opns_check_auth || return 1
21
22 if ! set_record "$fulldomain" "$txtvalue"; then
23 return 1
24 fi
25
26 return 0
27}
28
29#fulldomain
30dns_opnsense_rm() {
31 fulldomain=$1
32 txtvalue=$2
33
34 _opns_check_auth || return 1
35
36 if ! rm_record "$fulldomain" "$txtvalue"; then
37 return 1
38 fi
39
40 return 0
41}
42
43set_record() {
44 _info "Adding record"
45 fulldomain=$1
46 new_challenge=$2
47
48 _debug "Detect root zone"
49 if ! _get_root "$fulldomain"; then
50 _err "invalid domain"
51 return 1
52 fi
53 _debug _domain "$_domain"
54 _debug _host "$_host"
55 _debug _domainid "$_domainid"
56 _return_str=""
57 _record_string=""
58 _build_record_string "$_domainid" "$_host" "$new_challenge"
59 _uuid=""
60 if _existingchallenge "$_domain" "$_host" "$new_challenge"; then
61 # Update
62 if _opns_rest "POST" "/record/setRecord/${_uuid}" "$_record_string"; then
63 _return_str="$response"
64 else
65 return 1
66 fi
67
68 else
69 #create
70 if _opns_rest "POST" "/record/addRecord" "$_record_string"; then
71 _return_str="$response"
72 else
73 return 1
74 fi
75 fi
76
ec654d23 77 if echo "$_return_str" | _egrep_o "\"result\":\"saved\"" >/dev/null; then
4bf1f579
JL
78 _opns_rest "POST" "/service/reconfigure" "{}"
79 _debug "Record created"
80 else
81 _err "Error createing record $_record_string"
82 return 1
83 fi
84
85 return 0
86}
87
88rm_record() {
89 _info "Remove record"
90 fulldomain=$1
91 new_challenge="$2"
92
93 _debug "Detect root zone"
94 if ! _get_root "$fulldomain"; then
95 _err "invalid domain"
96 return 1
97 fi
98
99 _debug _domain "$_domain"
100 _debug _host "$_host"
101 _debug _domainid "$_domainid"
102 _uuid=""
103 if _existingchallenge "$_domain" "$_host" "$new_challenge"; then
104 # Delete
dfb4883c
JL
105 if _opns_rest "POST" "/record/delRecord/${_uuid}" "\{\}"; then
106 if echo "$_return_str" | _egrep_o "\"result\":\"deleted\"" >/dev/null; then
4bf1f579
JL
107 _opns_rest "POST" "/service/reconfigure" "{}"
108 _debug "Record deleted"
109 else
110 _err "Error delteting record $fulldomain"
111 return 1
112 fi
113 else
dfb4883c
JL
114 _err "Error delteting record $fulldomain"
115 return 1
4bf1f579
JL
116 fi
117 else
118 _info "Record not found, nothing to remove"
119 fi
120
121 return 0
122}
123
124#################### Private functions below ##################################
125#_acme-challenge.www.domain.com
126#returns
127# _domainid=domid
dfb4883c 128#_domain=domain.com
4bf1f579
JL
129_get_root() {
130 domain=$1
131 i=2
132 p=1
133 if _opns_rest "GET" "/domain/get"; then
134 _domain_response="$response"
135 else
136 return 1
137 fi
138
139 while true; do
140 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
141 if [ -z "$h" ]; then
142 #not valid
143 return 1
144 fi
145 _debug h "$h"
ec654d23 146 id=$(echo "$_domain_response" | _egrep_o "\"[^\"]*\":{\"enabled\":\"1\",\"type\":{\"master\":{\"value\":\"master\",\"selected\":1},\"slave\":{\"value\":\"slave\",\"selected\":0}},\"masterip\":\"[^\"]*\",\"domainname\":\"${h}\"" | cut -d ':' -f 1 | cut -d '"' -f 2)
4bf1f579 147
dfb4883c 148 if [ -n "$id" ]; then
4bf1f579
JL
149 _debug id "$id"
150 _host=$(printf "%s" "$domain" | cut -d . -f 1-$p)
151 _domain="${h}"
152 _domainid="${id}"
153 return 0
154 fi
155 p=$i
156 i=$(_math $i + 1)
157 done
158 _debug "$domain not found"
159
160 return 1
161}
162
163_opns_rest() {
164 method=$1
165 ep=$2
166 data=$3
167 #Percent encode user and token
dfb4883c
JL
168 key=$(echo "$OPNs_Key" | tr -d "\n\r" | _url_encode)
169 token=$(echo "$OPNs_Token" | tr -d "\n\r" | _url_encode)
4bf1f579
JL
170
171 opnsense_url="https://${key}:${token}@${OPNs_Host}:${OPNs_Port}/api/bind${ep}"
172 export _H1="Content-Type: application/json"
173 if [ ! "$method" = "GET" ]; then
174 _debug data "$data"
175 export _H1="Content-Type: application/json"
176 response="$(_post "$data" "$opnsense_url" "" "$method")"
177 else
178 export _H1=""
179 response="$(_get "$opnsense_url")"
180 fi
181
182 if [ "$?" != "0" ]; then
183 _err "error $ep"
184 return 1
185 fi
186 _debug2 response "$response"
187
188 return 0
189}
190
191_build_record_string() {
192 _record_string="{\"record\":{\"enabled\":\"1\",\"domain\":\"$1\",\"name\":\"$2\",\"type\":\"TXT\",\"value\":\"$3\"}}"
193}
194
195_existingchallenge() {
196 if _opns_rest "GET" "/record/searchRecord"; then
197 _record_response="$response"
198 else
199 return 1
200 fi
201 _uuid=""
bfa6e524 202 _uuid=$(echo "$_record_response" | _egrep_o "\"uuid\":\"[^\"]*\",\"enabled\":\"[01]\",\"domain\":\"$1\",\"name\":\"$2\",\"type\":\"TXT\",\"value\":\"$3\"" | cut -d ':' -f 2 | cut -d '"' -f 2)
4bf1f579 203
dfb4883c 204 if [ -n "$_uuid" ]; then
4bf1f579
JL
205 _debug uuid "$_uuid"
206 return 0
207 fi
208 _debug "${2}.$1{1} record not found"
209
210 return 1
211}
212
213_opns_check_auth() {
214 OPNs_Host="${OPNs_Host:-$(_readaccountconf_mutable OPNs_Host)}"
215 OPNs_Port="${OPNs_Port:-$(_readaccountconf_mutable OPNs_Port)}"
216 OPNs_Key="${OPNs_Key:-$(_readaccountconf_mutable OPNs_Key)}"
217 OPNs_Token="${OPNs_Token:-$(_readaccountconf_mutable OPNs_Token)}"
218 OPNs_Api_Insecure="${OPNs_Api_Insecure:-$(_readaccountconf_mutable OPNs_Api_Insecure)}"
219
220 if [ -z "$OPNs_Host" ]; then
221 OPNs_Host="localhost"
222 _err "You don't specify OPNsense address."
223 fi
224
225 if [ -z "$OPNs_Port" ]; then
226 OPNs_Port="443"
227 _err "You don't specify OPNsense Port."
228 fi
229
230 if [ -z "$OPNs_Api_Insecure" ]; then
231 OPNs_Api_Insecure="0"
232 fi
233
234 if [ -z "$OPNs_Key" ]; then
235 OPNs_Key=""
236 _err "You don't specify OPNsense api key id."
237 _err "Please set you OPNs_Key and try again."
238 return 1
239 fi
240
241 if [ -z "$OPNs_Token" ]; then
242 OPNs_Token=""
243 _err "You don't specify OPNsense token."
244 _err "Please create you OPNs_Token and try again."
245 return 1
246 fi
247
248 #save the api addr and key to the account conf file.
249 _saveaccountconf_mutable OPNs_Host "$OPNs_Host"
250 _saveaccountconf_mutable OPNs_Port "$OPNs_Port"
251 _saveaccountconf_mutable OPNs_Key "$OPNs_Key"
252 _saveaccountconf_mutable OPNs_Token "$OPNs_Token"
253 _saveaccountconf_mutable OPNs_Api_Insecure "$OPNs_Api_Insecure"
254 export HTTPS_INSECURE="${OPNs_Api_Insecure}"
255
dfb4883c 256 if ! _opns_rest "GET" "/general/get"; then
4bf1f579
JL
257 _err "Can't Access OPNsense"
258 return 1
259 fi
260 return 0
261}