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.

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