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.

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