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.

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