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.

1575 lines
43 KiB

  1. #!/bin/sh
  2. ##############################################################################
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License version 2 as
  6. # published by the Free Software Foundation.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # Copyright (C) 2016 Eric Luehrsen
  14. #
  15. ##############################################################################
  16. #
  17. # Unbound is a full featured recursive server with many options. The UCI
  18. # provided tries to simplify and bundle options. This should make Unbound
  19. # easier to deploy. Even light duty routers may resolve recursively instead of
  20. # depending on a stub with the ISP. The UCI also attempts to replicate dnsmasq
  21. # features as used in base LEDE/OpenWrt. If there is a desire for more
  22. # detailed tuning, then manual conf file overrides are also made available.
  23. #
  24. ##############################################################################
  25. UB_B_AUTH_ROOT=0
  26. UB_B_DNS_ASSIST=0
  27. UB_B_DNSSEC=0
  28. UB_B_DNS64=0
  29. UB_B_EXT_STATS=0
  30. UB_B_GATE_NAME=0
  31. UB_B_HIDE_BIND=1
  32. UB_B_IF_AUTO=1
  33. UB_B_LOCL_BLCK=0
  34. UB_B_LOCL_SERV=1
  35. UB_B_MAN_CONF=0
  36. UB_B_NTP_BOOT=1
  37. UB_B_QUERY_MIN=0
  38. UB_B_QRY_MINST=0
  39. UB_B_SLAAC6_MAC=0
  40. UB_D_CONTROL=0
  41. UB_D_DOMAIN_TYPE=static
  42. UB_D_DHCP_LINK=none
  43. UB_D_EXTRA_DNS=0
  44. UB_D_LAN_FQDN=0
  45. UB_D_PRIV_BLCK=1
  46. UB_D_PROTOCOL=mixed
  47. UB_D_RESOURCE=small
  48. UB_D_RECURSION=passive
  49. UB_D_VERBOSE=1
  50. UB_D_WAN_FQDN=0
  51. UB_IP_DNS64="64:ff9b::/96"
  52. UB_N_EDNS_SIZE=1280
  53. UB_N_RX_PORT=53
  54. UB_N_ROOT_AGE=9
  55. UB_N_THREADS=1
  56. UB_N_RATE_LMT=0
  57. UB_TTL_MIN=120
  58. UB_TXT_DOMAIN=lan
  59. UB_TXT_HOSTNAME=thisrouter
  60. ##############################################################################
  61. # reset as a combo with UB_B_NTP_BOOT and some time stamp files
  62. UB_B_READY=1
  63. # keep track of assignments during inserted resource records
  64. UB_LIST_NETW_ALL=""
  65. UB_LIST_NETW_LAN=""
  66. UB_LIST_NETW_WAN=""
  67. UB_LIST_INSECURE=""
  68. UB_LIST_ZONE_SERVERS=""
  69. UB_LIST_ZONE_NAMES=""
  70. ##############################################################################
  71. . /lib/functions.sh
  72. . /lib/functions/network.sh
  73. . /usr/lib/unbound/defaults.sh
  74. . /usr/lib/unbound/dnsmasq.sh
  75. . /usr/lib/unbound/iptools.sh
  76. ##############################################################################
  77. bundle_all_networks() {
  78. local cfg="$1"
  79. local ifname ifdashname validip
  80. local subnet subnets subnets4 subnets6
  81. network_get_subnets subnets4 "$cfg"
  82. network_get_subnets6 subnets6 "$cfg"
  83. network_get_device ifname "$cfg"
  84. ifdashname="${ifname//./-}"
  85. subnets="$subnets4 $subnets6"
  86. if [ -n "$subnets" ] ; then
  87. for subnet in $subnets ; do
  88. validip=$( valid_subnet_any $subnet )
  89. if [ "$validip" = "ok" ] ; then
  90. UB_LIST_NETW_ALL="$UB_LIST_NETW_ALL $ifdashname@$subnet"
  91. fi
  92. done
  93. fi
  94. }
  95. ##############################################################################
  96. bundle_lan_networks() {
  97. local cfg="$1"
  98. local interface ifsubnet ifname ifdashname ignore
  99. config_get_bool ignore "$cfg" ignore 0
  100. config_get interface "$cfg" interface ""
  101. network_get_device ifname "$interface"
  102. ifdashname="${ifname//./-}"
  103. if [ $ignore -eq 0 ] && [ -n "$ifdashname" ] \
  104. && [ -n "$UB_LIST_NETW_ALL" ] ; then
  105. for ifsubnet in $UB_LIST_NETW_ALL ; do
  106. case $ifsubnet in
  107. "${ifdashname}"@*)
  108. # Special GLA protection for local block; ULA protected default
  109. UB_LIST_NETW_LAN="$UB_LIST_NETW_LAN $ifsubnet"
  110. ;;
  111. esac
  112. done
  113. fi
  114. }
  115. ##############################################################################
  116. bundle_wan_networks() {
  117. local ifsubnet
  118. if [ -n "$UB_LIST_NETW_ALL" ] ; then
  119. for ifsubnet in $UB_LIST_NETW_ALL ; do
  120. case $UB_LIST_NETW_LAN in
  121. *"${ifsubnet}"*)
  122. # If LAN, then not WAN ...
  123. ;;
  124. *)
  125. UB_LIST_NETW_WAN="$UB_LIST_NETW_WAN $ifsubnet"
  126. ;;
  127. esac
  128. done
  129. fi
  130. }
  131. ##############################################################################
  132. bundle_resolv_conf_servers() {
  133. local resolvers=$( awk '/nameserver/ { print $2 }' /tmp/resolv.conf.auto )
  134. UB_LIST_ZONE_SERVERS="$UB_LIST_ZONE_SERVERS $resolvers"
  135. }
  136. ##############################################################################
  137. bundle_zone_names() {
  138. UB_LIST_ZONE_NAMES="$UB_LIST_ZONE_NAMES $1"
  139. }
  140. ##############################################################################
  141. bundle_zone_servers() {
  142. UB_LIST_ZONE_SERVERS="$UB_LIST_ZONE_SERVERS $1"
  143. }
  144. ##############################################################################
  145. bundle_domain_insecure() {
  146. UB_LIST_INSECURE="$UB_LIST_INSECURE $1"
  147. }
  148. ##############################################################################
  149. unbound_mkdir() {
  150. local filestuff
  151. if [ "$UB_D_DHCP_LINK" = "odhcpd" ] ; then
  152. local dhcp_origin=$( uci_get dhcp.@odhcpd[0].leasefile )
  153. local dhcp_dir=$( dirname $dhcp_origin )
  154. if [ ! -d "$dhcp_dir" ] ; then
  155. # make sure odhcpd has a directory to write (not done itself, yet)
  156. mkdir -p "$dhcp_dir"
  157. fi
  158. fi
  159. if [ -f $UB_RKEY_FILE ] ; then
  160. filestuff=$( cat $UB_RKEY_FILE )
  161. case "$filestuff" in
  162. *"state=2 [ VALID ]"*)
  163. # Lets not lose RFC 5011 tracking if we don't have to
  164. cp -p $UB_RKEY_FILE $UB_RKEY_FILE.keep
  165. ;;
  166. esac
  167. fi
  168. # Blind copy /etc/unbound to /var/lib/unbound
  169. mkdir -p $UB_VARDIR
  170. rm -f $UB_VARDIR/dhcp_*
  171. touch $UB_TOTAL_CONF
  172. cp -p /etc/unbound/* $UB_VARDIR/
  173. if [ ! -f $UB_RHINT_FILE ] ; then
  174. if [ -f /usr/share/dns/root.hints ] ; then
  175. # Debian-like package dns-root-data
  176. cp -p /usr/share/dns/root.hints $UB_RHINT_FILE
  177. elif [ $UB_B_READY -eq 0 ] ; then
  178. logger -t unbound -s "default root hints (built in root-servers.net)"
  179. fi
  180. fi
  181. if [ ! -f $UB_RKEY_FILE ] ; then
  182. if [ -f /usr/share/dns/root.key ] ; then
  183. # Debian-like package dns-root-data
  184. cp -p /usr/share/dns/root.key $UB_RKEY_FILE
  185. elif [ -x $UB_ANCHOR ] ; then
  186. $UB_ANCHOR -a $UB_RKEY_FILE
  187. elif [ $UB_B_READY -eq 0 ] ; then
  188. logger -t unbound -s "default trust anchor (built in root DS record)"
  189. fi
  190. fi
  191. if [ -f $UB_RKEY_FILE.keep ] ; then
  192. # root.key.keep is reused if newest
  193. cp -u $UB_RKEY_FILE.keep $UB_RKEY_FILE
  194. rm -f $UB_RKEY_FILE.keep
  195. fi
  196. if [ -f $UB_TLS_ETC_FILE ] ; then
  197. # copy the cert bundle into jail
  198. cp -p $UB_TLS_ETC_FILE $UB_TLS_FWD_FILE
  199. fi
  200. # Ensure access and prepare to jail
  201. chown -R unbound:unbound $UB_VARDIR
  202. chmod 755 $UB_VARDIR
  203. chmod 644 $UB_VARDIR/*
  204. if [ -f $UB_CTLKEY_FILE ] || [ -f $UB_CTLPEM_FILE ] \
  205. || [ -f $UB_SRVKEY_FILE ] || [ -f $UB_SRVPEM_FILE ] ; then
  206. # Keys (some) exist already; do not create new ones
  207. chmod 640 $UB_CTLKEY_FILE $UB_CTLPEM_FILE \
  208. $UB_SRVKEY_FILE $UB_SRVPEM_FILE
  209. elif [ -x /usr/sbin/unbound-control-setup ] ; then
  210. case "$UB_D_CONTROL" in
  211. [2-3])
  212. # unbound-control-setup for encrypt opt. 2 and 3, but not 4 "static"
  213. /usr/sbin/unbound-control-setup -d $UB_VARDIR
  214. chown -R unbound:unbound $UB_CTLKEY_FILE $UB_CTLPEM_FILE \
  215. $UB_SRVKEY_FILE $UB_SRVPEM_FILE
  216. chmod 640 $UB_CTLKEY_FILE $UB_CTLPEM_FILE \
  217. $UB_SRVKEY_FILE $UB_SRVPEM_FILE
  218. cp -p $UB_CTLKEY_FILE /etc/unbound/unbound_control.key
  219. cp -p $UB_CTLPEM_FILE /etc/unbound/unbound_control.pem
  220. cp -p $UB_SRVKEY_FILE /etc/unbound/unbound_server.key
  221. cp -p $UB_SRVPEM_FILE /etc/unbound/unbound_server.pem
  222. ;;
  223. esac
  224. fi
  225. if [ -f "$UB_TIME_FILE" ] ; then
  226. # NTP is done so its like you actually had an RTC
  227. UB_B_READY=1
  228. UB_B_NTP_BOOT=0
  229. elif [ $UB_B_NTP_BOOT -eq 0 ] ; then
  230. # time is considered okay on this device (ignore /etc/hotplug/ntpd/unbound)
  231. date -Is > $UB_TIME_FILE
  232. UB_B_READY=0
  233. UB_B_NTP_BOOT=0
  234. else
  235. # DNSSEC-TIME will not reconcile
  236. UB_B_READY=0
  237. UB_B_NTP_BOOT=1
  238. fi
  239. }
  240. ##############################################################################
  241. unbound_control() {
  242. echo "# $UB_CTRL_CONF generated by UCI $( date -Is )" > $UB_CTRL_CONF
  243. if [ $UB_D_CONTROL -gt 1 ] ; then
  244. if [ ! -f $UB_CTLKEY_FILE ] || [ ! -f $UB_CTLPEM_FILE ] \
  245. || [ ! -f $UB_SRVKEY_FILE ] || [ ! -f $UB_SRVPEM_FILE ] ; then
  246. # Key files need to be present; if unbound-control-setup was found, then
  247. # they might have been made during unbound_makedir() above.
  248. UB_D_CONTROL=0
  249. fi
  250. fi
  251. case "$UB_D_CONTROL" in
  252. 1)
  253. {
  254. # Local Host Only Unencrypted Remote Control
  255. echo "remote-control:"
  256. echo " control-enable: yes"
  257. echo " control-use-cert: no"
  258. echo " control-interface: 127.0.0.1"
  259. echo " control-interface: ::1"
  260. echo
  261. } >> $UB_CTRL_CONF
  262. ;;
  263. 2)
  264. {
  265. # Local Host Only Encrypted Remote Control
  266. echo "remote-control:"
  267. echo " control-enable: yes"
  268. echo " control-use-cert: yes"
  269. echo " control-interface: 127.0.0.1"
  270. echo " control-interface: ::1"
  271. echo " server-key-file: $UB_SRVKEY_FILE"
  272. echo " server-cert-file: $UB_SRVPEM_FILE"
  273. echo " control-key-file: $UB_CTLKEY_FILE"
  274. echo " control-cert-file: $UB_CTLPEM_FILE"
  275. echo
  276. } >> $UB_CTRL_CONF
  277. ;;
  278. [3-4])
  279. {
  280. # Network Encrypted Remote Control
  281. # (3) may auto setup and (4) must have static key/pem files
  282. # TODO: add UCI list for interfaces to bind
  283. echo "remote-control:"
  284. echo " control-enable: yes"
  285. echo " control-use-cert: yes"
  286. echo " control-interface: 0.0.0.0"
  287. echo " control-interface: ::0"
  288. echo " server-key-file: $UB_SRVKEY_FILE"
  289. echo " server-cert-file: $UB_SRVPEM_FILE"
  290. echo " control-key-file: $UB_CTLKEY_FILE"
  291. echo " control-cert-file: $UB_CTLPEM_FILE"
  292. echo
  293. } >> $UB_CTRL_CONF
  294. ;;
  295. esac
  296. }
  297. ##############################################################################
  298. unbound_zone() {
  299. local cfg=$1
  300. local servers_ip=""
  301. local servers_host=""
  302. local zone_sym zone_name zone_type zone_enabled zone_file
  303. local tls_upstream fallback
  304. local server port tls_port tls_index tls_suffix url_dir dns_ast
  305. if [ ! -f "$UB_ZONE_CONF" ] ; then
  306. echo "# $UB_ZONE_CONF generated by UCI $( date -Is )" > $UB_ZONE_CONF
  307. fi
  308. config_get_bool zone_enabled "$cfg" enabled 0
  309. if [ $zone_enabled -eq 1 ] ; then
  310. # these lists are built for each zone; empty to start
  311. UB_LIST_ZONE_NAMES=""
  312. UB_LIST_ZONE_SERVERS=""
  313. config_get zone_type "$cfg" zone_type ""
  314. config_get port "$cfg" port ""
  315. config_get tls_index "$cfg" tls_index ""
  316. config_get tls_port "$cfg" tls_port 853
  317. config_get url_dir "$cfg" url_dir ""
  318. config_get dns_ast "$cfg" dns_assist none
  319. config_get_bool resolv_conf "$cfg" resolv_conf 0
  320. config_get_bool fallback "$cfg" fallback 1
  321. config_get_bool tls_upstream "$cfg" tls_upstream 0
  322. config_list_foreach "$cfg" zone_name bundle_zone_names
  323. config_list_foreach "$cfg" server bundle_zone_servers
  324. # string formating for Unbound syntax
  325. tls_suffix="${tls_port:+@${tls_port}${tls_index:+#${tls_index}}}"
  326. [ $fallback -eq 0 ] && fallback=no || fallback=yes
  327. [ $tls_upstream -eq 0 ] && tls_upstream=no || tls_upstream=yes
  328. if [ $resolv_conf -eq 1 ] ; then
  329. bundle_resolv_conf_servers
  330. fi
  331. else
  332. zone_type=skip
  333. fi
  334. case "$dns_ast" in
  335. bind)
  336. if [ -x /usr/sbin/bind ] && [ -x /etc/init.d/bind ] ; then
  337. if /etc/init.d/bind enabled ; then
  338. dns_ast=1
  339. else
  340. dns_ast=0
  341. fi
  342. else
  343. dns_ast=0
  344. fi
  345. ;;
  346. dnsmasq)
  347. if [ -x /usr/sbin/dnsmasq ] && [ -x /etc/init.d/dnsmasq ] ; then
  348. if /etc/init.d/dnsmasq enabled ; then
  349. dns_ast=1
  350. else
  351. dns_ast=0
  352. fi
  353. else
  354. dns_ast=0
  355. fi
  356. ;;
  357. ipset-dns)
  358. if [ -x /usr/sbin/ipset-dns ] && [ -x /etc/init.d/ipset-dns ] ; then
  359. if /etc/init.d/ipset-dns enabled ; then
  360. dns_ast=1
  361. else
  362. dns_ast=0
  363. fi
  364. else
  365. dns_ast=0
  366. fi
  367. ;;
  368. nsd)
  369. if [ -x /usr/sbin/nsd ] && [ -x /etc/init.d/nsd ] ; then
  370. if /etc/init.d/nsd enabled ; then
  371. dns_ast=1
  372. else
  373. dns_ast=0
  374. fi
  375. else
  376. dns_ast=0
  377. fi
  378. ;;
  379. *)
  380. # Prevent a soft-brick event through local forwarding loops. Declare your
  381. # assistant program and this will check to be sure it is there.
  382. dns_ast=0
  383. esac
  384. if [ $dns_ast -gt 0 ] ; then
  385. UB_B_DNS_ASSIST=1
  386. fi
  387. case $zone_type in
  388. auth_zone)
  389. if [ $UB_B_NTP_BOOT -eq 0 ] && [ -n "$UB_LIST_ZONE_NAMES" ] \
  390. && { [ -n "$url_dir" ] || [ -n "$UB_LIST_ZONE_SERVERS" ] ; } ; then
  391. # Note AXFR may have large downloads. If NTP restart is configured,
  392. # then this can cause procd to force a process kill.
  393. for zone_name in $UB_LIST_ZONE_NAMES ; do
  394. if [ "$zone_name" = "." ] ; then
  395. zone_sym=.
  396. zone_name=root
  397. zone_file=root.zone
  398. else
  399. zone_sym=$zone_name
  400. zone_file=$zone_name.zone
  401. zone_file=${zone_file//../.}
  402. fi
  403. {
  404. # generate an auth-zone: with switches for prefetch cache
  405. echo "auth-zone:"
  406. echo " name: $zone_sym"
  407. for server in $UB_LIST_ZONE_SERVERS ; do
  408. echo " master: $server${port:+@${port}}"
  409. done
  410. if [ -n "$url_dir" ] ; then
  411. echo " url: $url_dir$zone_file"
  412. fi
  413. echo " fallback-enabled: $fallback"
  414. echo " for-downstream: no"
  415. echo " for-upstream: yes"
  416. echo " zonefile: $zone_file"
  417. echo
  418. } >> $UB_ZONE_CONF
  419. done
  420. fi
  421. ;;
  422. forward_zone)
  423. if [ ! -f $UB_TLS_FWD_FILE ] && [ "$tls_upstream" = "yes" ] ; then
  424. logger -p 4 -t unbound -s \
  425. "Forward-zone TLS benefits from authentication in package 'ca-bundle'"
  426. fi
  427. if [ -n "$UB_LIST_ZONE_NAMES" ] && [ -n "$UB_LIST_ZONE_SERVERS" ] ; then
  428. for server in $UB_LIST_ZONE_SERVERS ; do
  429. if [ "$( valid_subnet_any $server )" = "ok" ] \
  430. || { [ "$( local_subnet $server )" = "ok" ] \
  431. && [ $dns_ast -gt 0 ] ; } ; then
  432. case $server in
  433. *@[0-9]*|*#[A-Za-z0-9]*)
  434. # unique Unbound option for server address
  435. servers_ip="$servers_ip $server"
  436. ;;
  437. *)
  438. if [ "$tls_upstream" = "yes" ] ; then
  439. servers_ip="$servers_ip $server$tls_suffix"
  440. else
  441. servers_ip="$servers_ip $server${port:+@${port}}"
  442. fi
  443. ;;
  444. esac
  445. else
  446. case $server in
  447. *@[0-9]*|*#[A-Za-z0-9]*)
  448. # unique Unbound option for server host name
  449. servers_host="$servers_host $server"
  450. ;;
  451. *)
  452. if [ "$tls_upstream" = "yes" ] ; then
  453. servers_host="$servers_host $server${tls_port:+@${tls_port}}"
  454. else
  455. servers_host="$servers_host $server${port:+@${port}}"
  456. fi
  457. ;;
  458. esac
  459. fi
  460. done
  461. for zonename in $UB_LIST_ZONE_NAMES ; do
  462. {
  463. # generate a forward-zone with or without tls
  464. echo "forward-zone:"
  465. echo " name: $zonename"
  466. for server in $servers_host ; do
  467. echo " forward-host: $server"
  468. done
  469. for server in $servers_ip ; do
  470. echo " forward-addr: $server"
  471. done
  472. echo " forward-first: $fallback"
  473. echo " forward-tls-upstream: $tls_upstream"
  474. echo
  475. } >> $UB_ZONE_CONF
  476. done
  477. fi
  478. ;;
  479. stub_zone)
  480. if [ -n "$UB_LIST_ZONE_NAMES" ] && [ -n "$UB_LIST_ZONE_SERVERS" ] ; then
  481. for zonename in $UB_LIST_ZONE_NAMES ; do
  482. {
  483. # generate a stub-zone: or ensure short cut to authority NS
  484. echo "stub-zone:"
  485. echo " name: $zonename"
  486. for server in $UB_LIST_ZONE_SERVERS ; do
  487. echo " stub-addr: $server${port:+@${port}}"
  488. done
  489. echo " stub-first: $fallback"
  490. echo
  491. } >> $UB_ZONE_CONF
  492. done
  493. fi
  494. ;;
  495. *)
  496. {
  497. echo " # Special zone $zonename was not enabled or had UCI conflicts."
  498. echo
  499. } >> $UB_ZONE_CONF
  500. ;;
  501. esac
  502. }
  503. ##############################################################################
  504. unbound_conf() {
  505. local rt_mem rt_conn rt_buff modulestring domain ifsubnet moduleopts
  506. {
  507. # server: for this whole function
  508. echo "# $UB_CORE_CONF generated by UCI $( date -Is )"
  509. echo "server:"
  510. echo " username: unbound"
  511. echo " chroot: $UB_VARDIR"
  512. echo " directory: $UB_VARDIR"
  513. echo " pidfile: $UB_PIDFILE"
  514. } > $UB_CORE_CONF
  515. if [ -f "$UB_TLS_FWD_FILE" ] ; then
  516. # TLS cert bundle for upstream forwarder and https zone files
  517. # This is loaded before drop to root, so pull from /etc/ssl
  518. echo " tls-cert-bundle: $UB_TLS_FWD_FILE" >> $UB_CORE_CONF
  519. fi
  520. if [ -f "$UB_RHINT_FILE" ] ; then
  521. # Optional hints if found
  522. echo " root-hints: $UB_RHINT_FILE" >> $UB_CORE_CONF
  523. fi
  524. if [ $UB_B_DNSSEC -gt 0 ] && [ -f "$UB_RKEY_FILE" ] ; then
  525. {
  526. echo " auto-trust-anchor-file: $UB_RKEY_FILE"
  527. echo
  528. } >> $UB_CORE_CONF
  529. else
  530. echo >> $UB_CORE_CONF
  531. fi
  532. if [ $UB_N_THREADS -gt 1 ] \
  533. && $PROG -V | grep -q "Linked libs:.*libevent" ; then
  534. # heavy variant using "threads" may need substantial resources
  535. echo " num-threads: 2" >> $UB_CORE_CONF
  536. else
  537. # light variant with one "process" is much more efficient with light traffic
  538. echo " num-threads: 1" >> $UB_CORE_CONF
  539. fi
  540. {
  541. # Limited threading (2) with one shared slab
  542. echo " msg-cache-slabs: 1"
  543. echo " rrset-cache-slabs: 1"
  544. echo " infra-cache-slabs: 1"
  545. echo " key-cache-slabs: 1"
  546. echo " ratelimit-slabs: 1"
  547. echo " ip-ratelimit-slabs: 1"
  548. echo
  549. # Logging
  550. echo " use-syslog: yes"
  551. echo " statistics-interval: 0"
  552. echo " statistics-cumulative: no"
  553. } >> $UB_CORE_CONF
  554. if [ $UB_D_VERBOSE -ge 0 ] && [ $UB_D_VERBOSE -le 5 ] ; then
  555. echo " verbosity: $UB_D_VERBOSE" >> $UB_CORE_CONF
  556. fi
  557. if [ $UB_B_EXT_STATS -gt 0 ] ; then
  558. {
  559. # Log More
  560. echo " extended-statistics: yes"
  561. echo
  562. } >> $UB_CORE_CONF
  563. else
  564. {
  565. # Log Less
  566. echo " extended-statistics: no"
  567. echo
  568. } >> $UB_CORE_CONF
  569. fi
  570. if [ $UB_B_IF_AUTO -gt 0 ] ; then
  571. echo " interface-automatic: yes" >> $UB_CORE_CONF
  572. fi
  573. if [ $UB_B_DNS_ASSIST -gt 0 ] ; then
  574. echo " do-not-query-localhost: no" >> $UB_CORE_CONF
  575. fi
  576. case "$UB_D_PROTOCOL" in
  577. ip4_only)
  578. {
  579. echo " edns-buffer-size: $UB_N_EDNS_SIZE"
  580. echo " port: $UB_N_RX_PORT"
  581. echo " outgoing-port-permit: 10240-65535"
  582. echo " interface: 0.0.0.0"
  583. echo " outgoing-interface: 0.0.0.0"
  584. echo " do-ip4: yes"
  585. echo " do-ip6: no"
  586. echo
  587. } >> $UB_CORE_CONF
  588. ;;
  589. ip6_only)
  590. {
  591. echo " edns-buffer-size: $UB_N_EDNS_SIZE"
  592. echo " port: $UB_N_RX_PORT"
  593. echo " outgoing-port-permit: 10240-65535"
  594. echo " interface: ::0"
  595. echo " outgoing-interface: ::0"
  596. echo " do-ip4: no"
  597. echo " do-ip6: yes"
  598. echo
  599. } >> $UB_CORE_CONF
  600. ;;
  601. ip6_local)
  602. {
  603. echo " edns-buffer-size: $UB_N_EDNS_SIZE"
  604. echo " port: $UB_N_RX_PORT"
  605. echo " outgoing-port-permit: 10240-65535"
  606. echo " interface: 0.0.0.0"
  607. echo " interface: ::0"
  608. echo " outgoing-interface: 0.0.0.0"
  609. echo " do-ip4: yes"
  610. echo " do-ip6: yes"
  611. echo
  612. } >> $UB_CORE_CONF
  613. ;;
  614. ip6_prefer)
  615. {
  616. echo " edns-buffer-size: $UB_N_EDNS_SIZE"
  617. echo " port: $UB_N_RX_PORT"
  618. echo " outgoing-port-permit: 10240-65535"
  619. echo " interface: 0.0.0.0"
  620. echo " interface: ::0"
  621. echo " outgoing-interface: 0.0.0.0"
  622. echo " outgoing-interface: ::0"
  623. echo " do-ip4: yes"
  624. echo " do-ip6: yes"
  625. echo " prefer-ip6: yes"
  626. echo
  627. } >> $UB_CORE_CONF
  628. ;;
  629. mixed)
  630. {
  631. # Interface Wildcard (access contol handled by "option local_service")
  632. echo " edns-buffer-size: $UB_N_EDNS_SIZE"
  633. echo " port: $UB_N_RX_PORT"
  634. echo " outgoing-port-permit: 10240-65535"
  635. echo " interface: 0.0.0.0"
  636. echo " interface: ::0"
  637. echo " outgoing-interface: 0.0.0.0"
  638. echo " outgoing-interface: ::0"
  639. echo " do-ip4: yes"
  640. echo " do-ip6: yes"
  641. echo
  642. } >> $UB_CORE_CONF
  643. ;;
  644. *)
  645. if [ $UB_B_READY -eq 0 ] ; then
  646. logger -t unbound -s "default protocol configuration"
  647. fi
  648. {
  649. # outgoing-interface has useful defaults; incoming is localhost though
  650. echo " edns-buffer-size: $UB_N_EDNS_SIZE"
  651. echo " port: $UB_N_RX_PORT"
  652. echo " outgoing-port-permit: 10240-65535"
  653. echo " interface: 0.0.0.0"
  654. echo " interface: ::0"
  655. echo
  656. } >> $UB_CORE_CONF
  657. ;;
  658. esac
  659. case "$UB_D_RESOURCE" in
  660. # Tiny - Unbound's recommended cheap hardware config
  661. tiny) rt_mem=1 ; rt_conn=5 ; rt_buff=1 ;;
  662. # Small - Half RRCACHE and open ports
  663. small) rt_mem=8 ; rt_conn=10 ; rt_buff=2 ;;
  664. # Medium - Nearly default but with some added balancintg
  665. medium) rt_mem=16 ; rt_conn=20 ; rt_buff=4 ;;
  666. # Large - Double medium
  667. large) rt_mem=32 ; rt_conn=50 ; rt_buff=4 ;;
  668. # Whatever unbound does
  669. *) rt_mem=0 ; rt_conn=0 ;;
  670. esac
  671. if [ $rt_mem -gt 0 ] ; then
  672. {
  673. # Other harding and options for an embedded router
  674. echo " harden-short-bufsize: yes"
  675. echo " harden-large-queries: yes"
  676. echo " harden-glue: yes"
  677. echo " use-caps-for-id: no"
  678. echo
  679. # Set memory sizing parameters
  680. echo " msg-buffer-size: $(($rt_buff*8192))"
  681. echo " outgoing-range: $(($rt_conn*32))"
  682. echo " num-queries-per-thread: $(($rt_conn*16))"
  683. echo " outgoing-num-tcp: $(($rt_conn))"
  684. echo " incoming-num-tcp: $(($rt_conn))"
  685. echo " rrset-cache-size: $(($rt_mem*256))k"
  686. echo " msg-cache-size: $(($rt_mem*128))k"
  687. echo " stream-wait-size: $(($rt_mem*128))k"
  688. echo " key-cache-size: $(($rt_mem*128))k"
  689. echo " neg-cache-size: $(($rt_mem*32))k"
  690. echo " ratelimit-size: $(($rt_mem*32))k"
  691. echo " ip-ratelimit-size: $(($rt_mem*32))k"
  692. echo " infra-cache-numhosts: $(($rt_mem*256))"
  693. echo
  694. } >> $UB_CORE_CONF
  695. elif [ $UB_B_READY -eq 0 ] ; then
  696. logger -t unbound -s "default memory configuration"
  697. fi
  698. # Assembly of module-config: options is tricky; order matters
  699. moduleopts="$( /usr/sbin/unbound -V )"
  700. modulestring="iterator"
  701. case $moduleopts in
  702. *with-python*)
  703. modulestring="python $modulestring"
  704. ;;
  705. esac
  706. if [ $UB_B_DNSSEC -gt 0 ] ; then
  707. if [ $UB_B_NTP_BOOT -gt 0 ] ; then
  708. # DNSSEC chicken and egg with getting NTP time
  709. echo " val-override-date: -1" >> $UB_CORE_CONF
  710. fi
  711. {
  712. echo " harden-dnssec-stripped: yes"
  713. echo " val-clean-additional: yes"
  714. echo " ignore-cd-flag: yes"
  715. } >> $UB_CORE_CONF
  716. modulestring="validator $modulestring"
  717. fi
  718. case $moduleopts in
  719. *enable-subnet*)
  720. modulestring="subnetcache $modulestring"
  721. ;;
  722. esac
  723. if [ $UB_B_DNS64 -gt 0 ] ; then
  724. echo " dns64-prefix: $UB_IP_DNS64" >> $UB_CORE_CONF
  725. modulestring="dns64 $modulestring"
  726. fi
  727. {
  728. # Print final module string
  729. echo " module-config: \"$modulestring\""
  730. echo
  731. } >> $UB_CORE_CONF
  732. case "$UB_D_RECURSION" in
  733. passive)
  734. {
  735. # Some query privacy but "strict" will break some servers
  736. if [ $UB_B_QRY_MINST -gt 0 ] && [ "$UB_B_QUERY_MIN" -gt 0 ] ; then
  737. echo " qname-minimisation: yes"
  738. echo " qname-minimisation-strict: yes"
  739. elif [ $UB_B_QUERY_MIN -gt 0 ] ; then
  740. echo " qname-minimisation: yes"
  741. else
  742. echo " qname-minimisation: no"
  743. fi
  744. # Use DNSSEC to quickly understand NXDOMAIN ranges
  745. if [ $UB_B_DNSSEC -gt 0 ] ; then
  746. echo " aggressive-nsec: yes"
  747. echo " prefetch-key: no"
  748. fi
  749. # On demand fetching
  750. echo " prefetch: no"
  751. echo " target-fetch-policy: \"0 0 0 0 0\""
  752. echo
  753. } >> $UB_CORE_CONF
  754. ;;
  755. aggressive)
  756. {
  757. # Some query privacy but "strict" will break some servers
  758. if [ $UB_B_QRY_MINST -gt 0 ] && [ $UB_B_QUERY_MIN -gt 0 ] ; then
  759. echo " qname-minimisation: yes"
  760. echo " qname-minimisation-strict: yes"
  761. elif [ $UB_B_QUERY_MIN -gt 0 ] ; then
  762. echo " qname-minimisation: yes"
  763. else
  764. echo " qname-minimisation: no"
  765. fi
  766. # Use DNSSEC to quickly understand NXDOMAIN ranges
  767. if [ $UB_B_DNSSEC -gt 0 ] ; then
  768. echo " aggressive-nsec: yes"
  769. echo " prefetch-key: yes"
  770. fi
  771. # Prefetch what can be
  772. echo " prefetch: yes"
  773. echo " target-fetch-policy: \"3 2 1 0 0\""
  774. echo
  775. } >> $UB_CORE_CONF
  776. ;;
  777. *)
  778. if [ $UB_B_READY -eq 0 ] ; then
  779. logger -t unbound -s "default recursion configuration"
  780. fi
  781. ;;
  782. esac
  783. if [ 10 -lt $UB_N_RATE_LMT ] && [ $UB_N_RATE_LMT -lt 100000 ] ; then
  784. {
  785. # Protect the server from query floods which is helpful on weaker CPU
  786. # Per client rate limit is half the maximum to leave head room open
  787. echo " ratelimit: $UB_N_RATE_LMT"
  788. echo " ip-ratelimit: $(($UB_N_RATE_LMT/2))"
  789. echo
  790. } >> $UB_CORE_CONF
  791. fi
  792. {
  793. # Reload records more than 20 hours old
  794. # DNSSEC 5 minute bogus cool down before retry
  795. # Adaptive infrastructure info kept for 15 minutes
  796. echo " cache-min-ttl: $UB_TTL_MIN"
  797. echo " cache-max-ttl: 72000"
  798. echo " val-bogus-ttl: 300"
  799. echo " infra-host-ttl: 900"
  800. echo
  801. } >> $UB_CORE_CONF
  802. if [ $UB_B_HIDE_BIND -gt 0 ] ; then
  803. {
  804. # Block server id and version DNS TXT records
  805. echo " hide-identity: yes"
  806. echo " hide-version: yes"
  807. echo
  808. } >> $UB_CORE_CONF
  809. fi
  810. if [ $UB_D_PRIV_BLCK -gt 0 ] ; then
  811. {
  812. # Remove _upstream_ or global reponses with private addresses.
  813. # Unbounds own "local zone" and "forward zone" may still use these.
  814. # RFC1918, RFC3927, RFC4291, RFC6598, RFC6890
  815. echo " private-address: 10.0.0.0/8"
  816. echo " private-address: 100.64.0.0/10"
  817. echo " private-address: 169.254.0.0/16"
  818. echo " private-address: 172.16.0.0/12"
  819. echo " private-address: 192.168.0.0/16"
  820. echo " private-address: fc00::/7"
  821. echo " private-address: fe80::/10"
  822. echo
  823. } >> $UB_CORE_CONF
  824. fi
  825. if [ -n "$UB_LIST_NETW_LAN" ] && [ $UB_D_PRIV_BLCK -gt 1 ] ; then
  826. {
  827. for ifsubnet in $UB_LIST_NETW_LAN ; do
  828. case $ifsubnet in
  829. *@[1-9][0-9a-f][0-9a-f][0-9a-f]:*:[0-9a-f]*)
  830. # Remove global DNS responses with your local network IP6 GLA
  831. echo " private-address: ${ifsubnet#*@}"
  832. ;;
  833. esac
  834. done
  835. echo
  836. } >> $UB_CORE_CONF
  837. fi
  838. if [ $UB_B_LOCL_BLCK -gt 0 ] ; then
  839. {
  840. # Remove DNS reponses from upstream with loopback IP
  841. # Black hole DNS method for ad blocking, so consider...
  842. echo " private-address: 127.0.0.0/8"
  843. echo " private-address: ::1/128"
  844. echo
  845. } >> $UB_CORE_CONF
  846. fi
  847. if [ -n "$UB_LIST_INSECURE" ] ; then
  848. {
  849. for domain in $UB_LIST_INSECURE ; do
  850. # Except and accept domains without (DNSSEC); work around broken domains
  851. echo " domain-insecure: $domain"
  852. done
  853. echo
  854. } >> $UB_CORE_CONF
  855. fi
  856. if [ $UB_B_LOCL_SERV -gt 0 ] && [ -n "$UB_LIST_NETW_ALL" ] ; then
  857. {
  858. for ifsubnet in $UB_LIST_NETW_ALL ; do
  859. # Only respond to queries from subnets which have an interface.
  860. # Prevent DNS amplification attacks by not responding to the universe.
  861. echo " access-control: ${ifsubnet#*@} allow"
  862. done
  863. echo " access-control: 127.0.0.0/8 allow"
  864. echo " access-control: ::1/128 allow"
  865. echo " access-control: fe80::/10 allow"
  866. echo
  867. } >> $UB_CORE_CONF
  868. else
  869. {
  870. echo " access-control: 0.0.0.0/0 allow"
  871. echo " access-control: ::0/0 allow"
  872. echo
  873. } >> $UB_CORE_CONF
  874. fi
  875. }
  876. ##############################################################################
  877. unbound_hostname() {
  878. local ifsubnet ifarpa ifaddr ifname iffqdn
  879. local ulaprefix hostfqdn name names namerec ptrrec
  880. local zonetype=0
  881. echo "# $UB_HOST_CONF generated by UCI $( date -Is )" > $UB_HOST_CONF
  882. if [ "$UB_D_DHCP_LINK" = "dnsmasq" ] ; then
  883. {
  884. echo "# Local zone is handled by dnsmasq"
  885. echo
  886. } >> $UB_HOST_CONF
  887. elif [ -n "$UB_TXT_DOMAIN" ] \
  888. && { [ $UB_D_WAN_FQDN -gt 0 ] || [ $UB_D_LAN_FQDN -gt 0 ] ; } ; then
  889. case "$UB_D_DOMAIN_TYPE" in
  890. deny|inform_deny|refuse|static)
  891. {
  892. # type static means only this router has your domain
  893. echo " domain-insecure: $UB_TXT_DOMAIN"
  894. echo " private-domain: $UB_TXT_DOMAIN"
  895. echo " local-zone: $UB_TXT_DOMAIN $UB_D_DOMAIN_TYPE"
  896. echo " local-data: \"$UB_TXT_DOMAIN. $UB_XSOA\""
  897. echo " local-data: \"$UB_TXT_DOMAIN. $UB_XNS\""
  898. echo " local-data: '$UB_TXT_DOMAIN. $UB_XTXT'"
  899. echo
  900. if [ "$UB_TXT_DOMAIN" != "local" ] ; then
  901. # avoid involvement in RFC6762, unless it is the local zone name
  902. echo " local-zone: local always_nxdomain"
  903. echo
  904. fi
  905. } >> $UB_HOST_CONF
  906. zonetype=2
  907. ;;
  908. inform|transparent|typetransparent)
  909. {
  910. # transparent will permit forward-zone: or stub-zone: clauses
  911. echo " private-domain: $UB_TXT_DOMAIN"
  912. echo " local-zone: $UB_TXT_DOMAIN $UB_D_DOMAIN_TYPE"
  913. echo
  914. } >> $UB_HOST_CONF
  915. zonetype=1
  916. ;;
  917. esac
  918. {
  919. # Hostname as TLD works, but not transparent through recursion (singular)
  920. echo " domain-insecure: $UB_TXT_HOSTNAME"
  921. echo " private-domain: $UB_TXT_HOSTNAME"
  922. echo " local-zone: $UB_TXT_HOSTNAME static"
  923. echo " local-data: \"$UB_TXT_HOSTNAME. $UB_XSOA\""
  924. echo " local-data: \"$UB_TXT_HOSTNAME. $UB_XNS\""
  925. echo " local-data: '$UB_TXT_HOSTNAME. $UB_XTXT'"
  926. echo
  927. } >> $UB_HOST_CONF
  928. if [ -n "$UB_LIST_NETW_WAN" ] ; then
  929. for ifsubnet in $UB_LIST_NETW_WAN ; do
  930. ifaddr=${ifsubnet#*@}
  931. ifaddr=${ifaddr%/*}
  932. ifarpa=$( host_ptr_any "$ifaddr" )
  933. if [ -n "$ifarpa" ] ; then
  934. if [ $UB_D_WAN_FQDN -gt 0 ] ; then
  935. {
  936. # Create a static zone for WAN host record only (singular)
  937. echo " domain-insecure: $ifarpa"
  938. echo " private-address: $ifaddr"
  939. echo " local-zone: $ifarpa static"
  940. echo " local-data: \"$ifarpa. $UB_XSOA\""
  941. echo " local-data: \"$ifarpa. $UB_XNS\""
  942. echo " local-data: '$ifarpa. $UB_MTXT'"
  943. echo
  944. } >> $UB_HOST_CONF
  945. elif [ $zonetype -gt 0 ] ; then
  946. {
  947. echo " local-zone: $ifarpa transparent"
  948. echo
  949. } >> $UB_HOST_CONF
  950. fi
  951. fi
  952. done
  953. fi
  954. if [ -n "$UB_LIST_NETW_LAN" ] ; then
  955. for ifsubnet in $UB_LIST_NETW_LAN ; do
  956. ifarpa=$( domain_ptr_any "${ifsubnet#*@}" )
  957. if [ -n "$ifarpa" ] ; then
  958. if [ $zonetype -eq 2 ] ; then
  959. {
  960. # Do NOT forward queries with your ip6.arpa or in-addr.arpa
  961. echo " domain-insecure: $ifarpa"
  962. echo " local-zone: $ifarpa static"
  963. echo " local-data: \"$ifarpa. $UB_XSOA\""
  964. echo " local-data: \"$ifarpa. $UB_XNS\""
  965. echo " local-data: '$ifarpa. $UB_XTXT'"
  966. echo
  967. } >> $UB_HOST_CONF
  968. elif [ $zonetype -eq 1 ] && [ $UB_D_PRIV_BLCK -eq 0 ] ; then
  969. {
  970. echo " local-zone: $ifarpa transparent"
  971. echo
  972. } >> $UB_HOST_CONF
  973. fi
  974. fi
  975. done
  976. fi
  977. ulaprefix=$( uci_get network.@globals[0].ula_prefix )
  978. ulaprefix=${ulaprefix%%:/*}
  979. hostfqdn="$UB_TXT_HOSTNAME.$UB_TXT_DOMAIN"
  980. if [ -z "$ulaprefix" ] ; then
  981. # Nonsense so this option isn't globbed below
  982. ulaprefix="fdno:such:addr::"
  983. fi
  984. if [ "$UB_LIST_NETW_LAN" ] && [ $UB_D_LAN_FQDN -gt 0 ] ; then
  985. for ifsubnet in $UB_LIST_NETW_LAN ; do
  986. ifaddr=${ifsubnet#*@}
  987. ifaddr=${ifaddr%/*}
  988. ifname=${ifsubnet%@*}
  989. iffqdn="$ifname.$hostfqdn"
  990. if [ $UB_D_LAN_FQDN -eq 4 ] ; then
  991. names="$iffqdn $hostfqdn $UB_TXT_HOSTNAME"
  992. ptrrec=" local-data-ptr: \"$ifaddr 300 $iffqdn\""
  993. echo "$ptrrec" >> $UB_HOST_CONF
  994. elif [ $UB_D_LAN_FQDN -eq 3 ] ; then
  995. names="$hostfqdn $UB_TXT_HOSTNAME"
  996. ptrrec=" local-data-ptr: \"$ifaddr 300 $hostfqdn\""
  997. echo "$ptrrec" >> $UB_HOST_CONF
  998. else
  999. names="$UB_TXT_HOSTNAME"
  1000. ptrrec=" local-data-ptr: \"$ifaddr 300 $UB_TXT_HOSTNAME\""
  1001. echo "$ptrrec" >> $UB_HOST_CONF
  1002. fi
  1003. for name in $names ; do
  1004. case $ifaddr in
  1005. "${ulaprefix}"*)
  1006. # IP6 ULA only is assigned for OPTION 1
  1007. namerec=" local-data: \"$name. 300 IN AAAA $ifaddr\""
  1008. echo "$namerec" >> $UB_HOST_CONF
  1009. ;;
  1010. [1-9]*.*[0-9])
  1011. namerec=" local-data: \"$name. 300 IN A $ifaddr\""
  1012. echo "$namerec" >> $UB_HOST_CONF
  1013. ;;
  1014. *)
  1015. if [ $UB_D_LAN_FQDN -gt 1 ] ; then
  1016. # IP6 GLA is assigned for higher options
  1017. namerec=" local-data: \"$name. 300 IN AAAA $ifaddr\""
  1018. echo "$namerec" >> $UB_HOST_CONF
  1019. fi
  1020. ;;
  1021. esac
  1022. done
  1023. echo >> $UB_HOST_CONF
  1024. done
  1025. fi
  1026. if [ -n "$UB_LIST_NETW_WAN" ] && [ $UB_D_WAN_FQDN -gt 0 ] ; then
  1027. for ifsubnet in $UB_LIST_NETW_WAN ; do
  1028. ifaddr=${ifsubnet#*@}
  1029. ifaddr=${ifaddr%/*}
  1030. ifname=${ifsubnet%@*}
  1031. iffqdn="$ifname.$hostfqdn"
  1032. if [ $UB_D_WAN_FQDN -eq 4 ] ; then
  1033. names="$iffqdn $hostfqdn $UB_TXT_HOSTNAME"
  1034. ptrrec=" local-data-ptr: \"$ifaddr 300 $iffqdn\""
  1035. echo "$ptrrec" >> $UB_HOST_CONF
  1036. elif [ $UB_D_WAN_FQDN -eq 3 ] ; then
  1037. names="$hostfqdn $UB_TXT_HOSTNAME"
  1038. ptrrec=" local-data-ptr: \"$ifaddr 300 $hostfqdn\""
  1039. echo "$ptrrec" >> $UB_HOST_CONF
  1040. else
  1041. names="$UB_TXT_HOSTNAME"
  1042. ptrrec=" local-data-ptr: \"$ifaddr 300 $UB_TXT_HOSTNAME\""
  1043. echo "$ptrrec" >> $UB_HOST_CONF
  1044. fi
  1045. for name in $names ; do
  1046. case $ifaddr in
  1047. "${ulaprefix}"*)
  1048. # IP6 ULA only is assigned for OPTION 1
  1049. namerec=" local-data: \"$name. 300 IN AAAA $ifaddr\""
  1050. echo "$namerec" >> $UB_HOST_CONF
  1051. ;;
  1052. [1-9]*.*[0-9])
  1053. namerec=" local-data: \"$name. 300 IN A $ifaddr\""
  1054. echo "$namerec" >> $UB_HOST_CONF
  1055. ;;
  1056. *)
  1057. if [ $UB_D_WAN_FQDN -gt 1 ] ; then
  1058. # IP6 GLA is assigned for higher options
  1059. namerec=" local-data: \"$name. 300 IN AAAA $ifaddr\""
  1060. echo "$namerec" >> $UB_HOST_CONF
  1061. fi
  1062. ;;
  1063. esac
  1064. done
  1065. echo >> $UB_HOST_CONF
  1066. done
  1067. fi
  1068. fi # end if uci valid
  1069. }
  1070. ##############################################################################
  1071. unbound_uci() {
  1072. local cfg="$1"
  1073. local hostnm
  1074. hostnm=$( uci_get system.@system[0].hostname | awk '{print tolower($0)}' )
  1075. UB_TXT_HOSTNAME=${hostnm:-thisrouter}
  1076. config_get_bool UB_B_SLAAC6_MAC "$cfg" dhcp4_slaac6 0
  1077. config_get_bool UB_B_DNS64 "$cfg" dns64 0
  1078. config_get_bool UB_B_EXT_STATS "$cfg" extended_stats 0
  1079. config_get_bool UB_B_HIDE_BIND "$cfg" hide_binddata 1
  1080. config_get_bool UB_B_LOCL_SERV "$cfg" localservice 1
  1081. config_get_bool UB_B_MAN_CONF "$cfg" manual_conf 0
  1082. config_get_bool UB_B_QUERY_MIN "$cfg" query_minimize 0
  1083. config_get_bool UB_B_QRY_MINST "$cfg" query_min_strict 0
  1084. config_get_bool UB_B_AUTH_ROOT "$cfg" prefetch_root 0
  1085. config_get_bool UB_B_LOCL_BLCK "$cfg" rebind_localhost 0
  1086. config_get_bool UB_B_DNSSEC "$cfg" validator 0
  1087. config_get_bool UB_B_NTP_BOOT "$cfg" validator_ntp 1
  1088. config_get_bool UB_B_IF_AUTO "$cfg" interface_auto 1
  1089. config_get UB_IP_DNS64 "$cfg" dns64_prefix "64:ff9b::/96"
  1090. config_get UB_N_EDNS_SIZE "$cfg" edns_size 1280
  1091. config_get UB_N_RX_PORT "$cfg" listen_port 53
  1092. config_get UB_N_ROOT_AGE "$cfg" root_age 9
  1093. config_get UB_N_THREADS "$cfg" num_threads 1
  1094. config_get UB_N_RATE_LMT "$cfg" rate_limit 0
  1095. config_get UB_D_CONTROL "$cfg" unbound_control 0
  1096. config_get UB_D_DOMAIN_TYPE "$cfg" domain_type static
  1097. config_get UB_D_DHCP_LINK "$cfg" dhcp_link none
  1098. config_get UB_D_EXTRA_DNS "$cfg" add_extra_dns 0
  1099. config_get UB_D_LAN_FQDN "$cfg" add_local_fqdn 0
  1100. config_get UB_D_PRIV_BLCK "$cfg" rebind_protection 1
  1101. config_get UB_D_PROTOCOL "$cfg" protocol mixed
  1102. config_get UB_D_RECURSION "$cfg" recursion passive
  1103. config_get UB_D_RESOURCE "$cfg" resource small
  1104. config_get UB_D_VERBOSE "$cfg" verbosity 1
  1105. config_get UB_D_WAN_FQDN "$cfg" add_wan_fqdn 0
  1106. config_get UB_TTL_MIN "$cfg" ttl_min 120
  1107. config_get UB_TXT_DOMAIN "$cfg" domain lan
  1108. config_list_foreach "$cfg" domain_insecure bundle_domain_insecure
  1109. if [ "$UB_D_DHCP_LINK" = "none" ] ; then
  1110. config_get_bool UB_B_DNSMASQ "$cfg" dnsmasq_link_dns 0
  1111. if [ $UB_B_DNSMASQ -gt 0 ] ; then
  1112. UB_D_DHCP_LINK=dnsmasq
  1113. if [ $UB_B_READY -eq 0 ] ; then
  1114. logger -t unbound -s "Please use 'dhcp_link' selector instead"
  1115. fi
  1116. fi
  1117. fi
  1118. if [ "$UB_D_DHCP_LINK" = "dnsmasq" ] ; then
  1119. if [ ! -x /usr/sbin/dnsmasq ] || [ ! -x /etc/init.d/dnsmasq ] ; then
  1120. UB_D_DHCP_LINK=none
  1121. else
  1122. /etc/init.d/dnsmasq enabled || UB_D_DHCP_LINK=none
  1123. fi
  1124. if [ $UB_B_READY -eq 0 ] && [ "$UB_D_DHCP_LINK" = "none" ] ; then
  1125. logger -t unbound -s "cannot forward to dnsmasq"
  1126. fi
  1127. fi
  1128. if [ "$UB_D_DHCP_LINK" = "odhcpd" ] ; then
  1129. if [ ! -x /usr/sbin/odhcpd ] || [ ! -x /etc/init.d/odhcpd ] ; then
  1130. UB_D_DHCP_LINK=none
  1131. else
  1132. /etc/init.d/odhcpd enabled || UB_D_DHCP_LINK=none
  1133. fi
  1134. if [ $UB_B_READY -eq 0 ] && [ "$UB_D_DHCP_LINK" = "none" ] ; then
  1135. logger -t unbound -s "cannot receive records from odhcpd"
  1136. fi
  1137. fi
  1138. if [ $UB_N_EDNS_SIZE -lt 512 ] || [ 4096 -lt $UB_N_EDNS_SIZE ] ; then
  1139. logger -t unbound -s "edns_size exceeds range, using default"
  1140. UB_N_EDNS_SIZE=1280
  1141. fi
  1142. if [ $UB_N_RX_PORT -ne 53 ] \
  1143. && { [ $UB_N_RX_PORT -lt 1024 ] || [ 10240 -lt $UB_N_RX_PORT ] ; } ; then
  1144. logger -t unbound -s "privileged port or in 5 digits, using default"
  1145. UB_N_RX_PORT=53
  1146. fi
  1147. if [ $UB_TTL_MIN -gt 1800 ] ; then
  1148. logger -t unbound -s "ttl_min could have had awful side effects, using 300"
  1149. UB_TTL_MIN=300
  1150. fi
  1151. }
  1152. ##############################################################################
  1153. unbound_include() {
  1154. local adb_enabled
  1155. local adb_files=$( ls $UB_VARDIR/adb_list.* 2>/dev/null )
  1156. echo "# $UB_TOTAL_CONF generated by UCI $( date -Is )" > $UB_TOTAL_CONF
  1157. if [ -f "$UB_CORE_CONF" ] ; then
  1158. # Yes this all looks busy, but it is in TMPFS. Working on separate files
  1159. # and piecing together is easier. UCI order is less constrained.
  1160. cat $UB_CORE_CONF >> $UB_TOTAL_CONF
  1161. rm $UB_CORE_CONF
  1162. fi
  1163. if [ -f "$UB_HOST_CONF" ] ; then
  1164. # UCI definitions of local host or local subnet
  1165. cat $UB_HOST_CONF >> $UB_TOTAL_CONF
  1166. rm $UB_HOST_CONF
  1167. fi
  1168. if [ -f $UB_SRVMASQ_CONF ] ; then
  1169. # UCI found link to dnsmasq
  1170. cat $UB_SRVMASQ_CONF >> $UB_TOTAL_CONF
  1171. rm $UB_SRVMASQ_CONF
  1172. fi
  1173. if [ -f "$UB_DHCP_CONF" ] ; then
  1174. {
  1175. # Seed DHCP records because dhcp scripts trigger externally
  1176. # Incremental Unbound restarts may drop unbound-control records
  1177. echo "include: $UB_DHCP_CONF"
  1178. echo
  1179. } >> $UB_TOTAL_CONF
  1180. fi
  1181. if [ -z "$adb_files" ] || [ ! -x /usr/bin/adblock.sh ] \
  1182. || [ ! -x /etc/init.d/adblock ] ; then
  1183. adb_enabled=0
  1184. elif /etc/init.d/adblock enabled ; then
  1185. adb_enabled=1
  1186. {
  1187. # Pull in your selected openwrt/pacakges/net/adblock generated lists
  1188. echo "include: $UB_VARDIR/adb_list.*"
  1189. echo
  1190. } >> $UB_TOTAL_CONF
  1191. else
  1192. adb_enabled=0
  1193. fi
  1194. if [ -f $UB_SRV_CONF ] ; then
  1195. {
  1196. # Pull your own "server:" options here
  1197. echo "include: $UB_SRV_CONF"
  1198. echo
  1199. } >> $UB_TOTAL_CONF
  1200. fi
  1201. if [ -f "$UB_ZONE_CONF" ] ; then
  1202. # UCI defined forward, stub, and auth zones
  1203. cat $UB_ZONE_CONF >> $UB_TOTAL_CONF
  1204. rm $UB_ZONE_CONF
  1205. fi
  1206. if [ -f "$UB_CTRL_CONF" ] ; then
  1207. # UCI defined control application connection
  1208. cat $UB_CTRL_CONF >> $UB_TOTAL_CONF
  1209. rm $UB_CTRL_CONF
  1210. fi
  1211. if [ -f "$UB_EXTMASQ_CONF" ] ; then
  1212. # UCI found link to dnsmasq
  1213. cat $UB_EXTMASQ_CONF >> $UB_TOTAL_CONF
  1214. rm $UB_EXTMASQ_CONF
  1215. fi
  1216. if [ -f "$UB_EXT_CONF" ] ; then
  1217. {
  1218. # Pull your own extend feature clauses here
  1219. echo "include: $UB_EXT_CONF"
  1220. echo
  1221. } >> $UB_TOTAL_CONF
  1222. fi
  1223. }
  1224. ##############################################################################
  1225. resolv_setup() {
  1226. if [ "$UB_N_RX_PORT" != "53" ] ; then
  1227. # unbound is not the default on target resolver
  1228. echo "do nothing" >/dev/null
  1229. elif [ -x /etc/init.d/dnsmasq ] \
  1230. && /etc/init.d/dnsmasq enabled \
  1231. && nslookup localhost 127.0.0.1#53 >/dev/null 2>&1 ; then
  1232. # unbound is configured for port 53, but dnsmasq is enabled, and a resolver
  1233. # is already listening on port 53. Let dnsmasq manage resolve.conf.
  1234. # This also works to prevent clobbering while changing UCI.
  1235. echo "do nothing" >/dev/null
  1236. else
  1237. # unbound listens on 127.0.0.1#53 so set resolver file to local.
  1238. rm -f /tmp/resolv.conf
  1239. {
  1240. echo "# /tmp/resolv.conf generated by Unbound UCI $( date -Is )"
  1241. echo "nameserver 127.0.0.1"
  1242. echo "nameserver ::1"
  1243. echo "search $UB_TXT_DOMAIN."
  1244. } > /tmp/resolv.conf
  1245. fi
  1246. }
  1247. ##############################################################################
  1248. unbound_start() {
  1249. config_load unbound
  1250. config_foreach unbound_uci unbound
  1251. unbound_mkdir
  1252. if [ $UB_B_MAN_CONF -eq 0 ] ; then
  1253. # iterate zones before we load other UCI
  1254. # forward-zone: auth-zone: and stub-zone:
  1255. config_foreach unbound_zone zone
  1256. # associate potential DNS RR with interfaces
  1257. config_load network
  1258. config_foreach bundle_all_networks interface
  1259. config_load dhcp
  1260. config_foreach bundle_lan_networks dhcp
  1261. bundle_wan_networks
  1262. # server:
  1263. unbound_conf
  1264. unbound_hostname
  1265. # control:
  1266. unbound_control
  1267. # dnsmasq
  1268. dnsmasq_link
  1269. # merge
  1270. unbound_include
  1271. fi
  1272. resolv_setup
  1273. }
  1274. ##############################################################################