4 #https://docs.opnsense.org/development/api.html
6 #OPNs_Host="opnsense.example.com"
8 # optional, defaults to 443 if unset
9 #OPNs_Key="qocfU9RSbt8vTIBcnW8bPqCrpfAHMDvj5OzadE7Str+rbjyCyk7u6yMrSCHtBXabgDDXx/dY0POUp7ZA"
10 #OPNs_Token="pZEQ+3ce8dDlfBBdg3N8EpqpF5I1MhFqdxX06le6Gl8YzyQvYCfCzNaFX9O9+IOSyAs7X71fwdRiZ+Lv"
12 # optional, defaults to 0 if unset
13 # 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)
15 ######## Public functions #####################
16 #Usage: add _acme-challenge.www.domain.com "123456789ABCDEF0000000000000000000000000000000000000"
20 OPNs_DefaultApi_Insecure
=0
26 _opns_check_auth ||
return 1
28 if ! set_record
"$fulldomain" "$txtvalue"; then
40 _opns_check_auth ||
return 1
42 if ! rm_record
"$fulldomain" "$txtvalue"; then
52 _info
"Adding record $fulldomain with challenge: $new_challenge"
54 _debug
"Detect root zone"
55 if ! _get_root
"$fulldomain"; then
60 _debug _domain
"$_domain"
62 _debug _domainid
"$_domainid"
65 _build_record_string
"$_domainid" "$_host" "$new_challenge"
67 if _existingchallenge
"$_domain" "$_host" "$new_challenge"; then
69 if _opns_rest
"POST" "/record/setRecord/${_uuid}" "$_record_string"; then
70 _return_str
="$response"
77 if _opns_rest
"POST" "/record/addRecord" "$_record_string"; then
78 _return_str
="$response"
84 if echo "$_return_str" | _egrep_o
"\"result\":\"saved\"" >/dev
/null
; then
85 _opns_rest
"POST" "/service/reconfigure" "{}"
86 _debug
"Record created"
88 _err
"Error creating record $_record_string"
98 _info
"Remove record $fulldomain with challenge: $new_challenge"
100 _debug
"Detect root zone"
101 if ! _get_root
"$fulldomain"; then
102 _err
"invalid domain"
106 _debug _domain
"$_domain"
107 _debug _host
"$_host"
108 _debug _domainid
"$_domainid"
110 if _existingchallenge
"$_domain" "$_host" "$new_challenge"; then
112 if _opns_rest
"POST" "/record/delRecord/${_uuid}" "\{\}"; then
113 if echo "$_return_str" | _egrep_o
"\"result\":\"deleted\"" >/dev
/null
; then
114 _opns_rest
"POST" "/service/reconfigure" "{}"
115 _debug
"Record deleted"
117 _err
"Error deleting record $_host from domain $fulldomain"
121 _err
"Error deleting record $_host from domain $fulldomain"
125 _info
"Record not found, nothing to remove"
131 #################### Private functions below ##################################
132 #_acme-challenge.www.domain.com
140 if _opns_rest
"GET" "/domain/get"; then
141 _domain_response
="$response"
147 h
=$
(printf "%s" "$domain" | cut
-d .
-f $i-100)
153 id
=$
(echo "$_domain_response" | _egrep_o
"\"[^\"]*\":{\"enabled\":\"1\",\"type\":{\"master\":{\"value\":\"master\",\"selected\":1},\"slave\":{\"value\":\"slave\",\"selected\":0}},\"masterip\":{\"\":{[^}]*}}(,\"allownotifyslave\":{\"\":{[^}]*}},|,)\"domainname\":\"${h}\"" | cut
-d ':' -f 1 | cut
-d '"' -f 2)
155 if [ -n "$id" ]; then
157 _host
=$
(printf "%s" "$domain" | cut
-d .
-f 1-$p)
165 _debug
"$domain not found"
174 #Percent encode user and token
175 key
=$
(echo "$OPNs_Key" |
tr -d "\n\r" | _url_encode
)
176 token
=$
(echo "$OPNs_Token" |
tr -d "\n\r" | _url_encode
)
178 opnsense_url
="https://${key}:${token}@${OPNs_Host}:${OPNs_Port:-$OPNs_DefaultPort}/api/bind${ep}"
179 export _H1
="Content-Type: application/json"
180 _debug2
"Try to call api: https://${OPNs_Host}:${OPNs_Port:-$OPNs_DefaultPort}/api/bind${ep}"
181 if [ ! "$method" = "GET" ]; then
183 export _H1
="Content-Type: application/json"
184 response
="$(_post "$data" "$opnsense_url" "" "$method")"
187 response
="$(_get "$opnsense_url")"
190 if [ "$?" != "0" ]; then
194 _debug2 response
"$response"
199 _build_record_string
() {
200 _record_string
="{\"record\":{\"enabled\":\"1\",\"domain\":\"$1\",\"name\":\"$2\",\"type\":\"TXT\",\"value\":\"$3\"}}"
203 _existingchallenge
() {
204 if _opns_rest
"GET" "/record/searchRecord"; then
205 _record_response
="$response"
210 _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)
212 if [ -n "$_uuid" ]; then
216 _debug
"${2}.$1{1} record not found"
222 OPNs_Host
="${OPNs_Host:-$(_readaccountconf_mutable OPNs_Host)}"
223 OPNs_Port
="${OPNs_Port:-$(_readaccountconf_mutable OPNs_Port)}"
224 OPNs_Key
="${OPNs_Key:-$(_readaccountconf_mutable OPNs_Key)}"
225 OPNs_Token
="${OPNs_Token:-$(_readaccountconf_mutable OPNs_Token)}"
226 OPNs_Api_Insecure
="${OPNs_Api_Insecure:-$(_readaccountconf_mutable OPNs_Api_Insecure)}"
228 if [ -z "$OPNs_Host" ]; then
229 _err
"You don't specify OPNsense address."
232 _saveaccountconf_mutable OPNs_Host
"$OPNs_Host"
235 if ! printf '%s' "$OPNs_Port" |
grep '^[0-9]*$' >/dev
/null
; then
236 _err
'OPNs_Port specified but not numeric value'
238 elif [ -z "$OPNs_Port" ]; then
239 _info
"OPNSense port not specified. Defaulting to using port $OPNs_DefaultPort"
241 _saveaccountconf_mutable OPNs_Port
"$OPNs_Port"
244 if ! printf '%s' "$OPNs_Api_Insecure" |
grep '^[01]$' >/dev
/null
; then
245 _err
'OPNs_Api_Insecure specified but not 0/1 value'
247 elif [ -n "$OPNs_Api_Insecure" ]; then
248 _saveaccountconf_mutable OPNs_Api_Insecure
"$OPNs_Api_Insecure"
250 export HTTPS_INSECURE
="${OPNs_Api_Insecure:-$OPNs_DefaultApi_Insecure}"
252 if [ -z "$OPNs_Key" ]; then
253 _err
"you have not specified your OPNsense api key id."
254 _err
"Please set OPNs_Key and try again."
257 _saveaccountconf_mutable OPNs_Key
"$OPNs_Key"
260 if [ -z "$OPNs_Token" ]; then
261 _err
"you have not specified your OPNsense token."
262 _err
"Please create OPNs_Token and try again."
265 _saveaccountconf_mutable OPNs_Token
"$OPNs_Token"
268 if ! _opns_rest
"GET" "/general/get"; then
269 _err
"Call to OPNsense API interface failed. Unable to access OPNsense API."