You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

262 lines
6.4 KiB

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