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.

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