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.

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