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.

1451 lines
40 KiB

  1. #!/bin/sh
  2. ##############################################################################
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License version 2 as
  6. # published by the Free Software Foundation.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # Copyright (C) 2016 Eric Luehrsen
  14. #
  15. ##############################################################################
  16. #
  17. # Unbound is a full featured recursive server with many options. The UCI
  18. # provided tries to simplify and bundle options. This should make Unbound
  19. # easier to deploy. Even light duty routers may resolve recursively instead of
  20. # depending on a stub with the ISP. The UCI also attempts to replicate dnsmasq
  21. # features as used in base LEDE/OpenWrt. If there is a desire for more
  22. # detailed tuning, then manual conf file overrides are also made available.
  23. #
  24. ##############################################################################
  25. UB_B_SLAAC6_MAC=0
  26. UB_B_DNSSEC=0
  27. UB_B_DNS64=0
  28. UB_B_EXT_STATS=0
  29. UB_B_GATE_NAME=0
  30. UB_B_HIDE_BIND=1
  31. UB_B_LOCL_BLCK=0
  32. UB_B_LOCL_SERV=1
  33. UB_B_MAN_CONF=0
  34. UB_B_NTP_BOOT=1
  35. UB_B_QUERY_MIN=0
  36. UB_B_QRY_MINST=0
  37. UB_B_AUTH_ROOT=0
  38. UB_D_CONTROL=0
  39. UB_D_DOMAIN_TYPE=static
  40. UB_D_DHCP_LINK=none
  41. UB_D_EXTRA_DNS=0
  42. UB_D_LAN_FQDN=0
  43. UB_D_PRIV_BLCK=1
  44. UB_D_PROTOCOL=mixed
  45. UB_D_RESOURCE=small
  46. UB_D_RECURSION=passive
  47. UB_D_VERBOSE=1
  48. UB_D_WAN_FQDN=0
  49. UB_IP_DNS64="64:ff9b::/96"
  50. UB_N_EDNS_SIZE=1280
  51. UB_N_RX_PORT=53
  52. UB_N_ROOT_AGE=9
  53. UB_TTL_MIN=120
  54. UB_TXT_DOMAIN=lan
  55. UB_TXT_HOSTNAME=thisrouter
  56. ##############################################################################
  57. # reset as a combo with UB_B_NTP_BOOT and some time stamp files
  58. UB_B_READY=1
  59. # keep track of assignments during inserted resource records
  60. UB_LIST_NETW_ALL=""
  61. UB_LIST_NETW_LAN=""
  62. UB_LIST_NETW_WAN=""
  63. UB_LIST_INSECURE=""
  64. UB_LIST_ZONE_SERVERS=""
  65. UB_LIST_ZONE_NAMES=""
  66. ##############################################################################
  67. . /lib/functions.sh
  68. . /lib/functions/network.sh
  69. . /usr/lib/unbound/defaults.sh
  70. . /usr/lib/unbound/dnsmasq.sh
  71. . /usr/lib/unbound/iptools.sh
  72. ##############################################################################
  73. bundle_all_networks() {
  74. local cfg="$1"
  75. local ifname ifdashname validip
  76. local subnet subnets subnets4 subnets6
  77. network_get_subnets subnets4 "$cfg"
  78. network_get_subnets6 subnets6 "$cfg"
  79. network_get_device ifname "$cfg"
  80. ifdashname="${ifname//./-}"
  81. subnets="$subnets4 $subnets6"
  82. if [ -n "$subnets" ] ; then
  83. for subnet in $subnets ; do
  84. validip=$( valid_subnet_any $subnet )
  85. if [ "$validip" = "ok" ] ; then
  86. UB_LIST_NETW_ALL="$UB_LIST_NETW_ALL $ifdashname@$subnet"
  87. fi
  88. done
  89. fi
  90. }
  91. ##############################################################################
  92. bundle_lan_networks() {
  93. local cfg="$1"
  94. local ifsubnet ifname ifdashname ignore
  95. config_get_bool ignore "$cfg" ignore 0
  96. network_get_device ifname "$cfg"
  97. ifdashname="${ifname//./-}"
  98. if [ "$ignore" -eq 0 -a -n "$ifdashname" -a -n "$UB_LIST_NETW_ALL" ] ; then
  99. for ifsubnet in $UB_LIST_NETW_ALL ; do
  100. case $ifsubnet in
  101. "${ifdashname}"@*)
  102. # Special GLA protection for local block; ULA protected as a catagory
  103. UB_LIST_NETW_LAN="$UB_LIST_NETW_LAN $ifsubnet"
  104. ;;
  105. esac
  106. done
  107. fi
  108. }
  109. ##############################################################################
  110. bundle_wan_networks() {
  111. local ifsubnet
  112. if [ -n "$UB_LIST_NETW_ALL" ] ; then
  113. for ifsubnet in $UB_LIST_NETW_ALL ; do
  114. case $UB_LIST_NETW_LAN in
  115. *"${ifsubnet}"*)
  116. # If LAN, then not WAN ...
  117. ;;
  118. *)
  119. UB_LIST_NETW_WAN="$UB_LIST_NETW_WAN $ifsubnet"
  120. ;;
  121. esac
  122. done
  123. fi
  124. }
  125. ##############################################################################
  126. bundle_resolv_conf_servers() {
  127. local resolvers=$( awk '/nameserver/ { print $2 }' /tmp/resolv.conf.auto )
  128. UB_LIST_ZONE_SERVERS="$UB_LIST_ZONE_SERVERS $resolvers"
  129. }
  130. ##############################################################################
  131. bundle_zone_names() {
  132. UB_LIST_ZONE_NAMES="$UB_LIST_ZONE_NAMES $1"
  133. }
  134. ##############################################################################
  135. bundle_zone_servers() {
  136. UB_LIST_ZONE_SERVERS="$UB_LIST_ZONE_SERVERS $1"
  137. }
  138. ##############################################################################
  139. bundle_domain_insecure() {
  140. UB_LIST_INSECURE="$UB_LIST_INSECURE $1"
  141. }
  142. ##############################################################################
  143. unbound_mkdir() {
  144. local filestuff
  145. if [ "$UB_D_DHCP_LINK" = "odhcpd" ] ; then
  146. local dhcp_origin=$( uci_get dhcp.@odhcpd[0].leasefile )
  147. local dhcp_dir=$( dirname $dhcp_origin )
  148. if [ ! -d "$dhcp_dir" ] ; then
  149. # make sure odhcpd has a directory to write (not done itself, yet)
  150. mkdir -p "$dhcp_dir"
  151. fi
  152. fi
  153. if [ -f $UB_RKEY_FILE ] ; then
  154. filestuff=$( cat $UB_RKEY_FILE )
  155. case "$filestuff" in
  156. *"state=2 [ VALID ]"*)
  157. # Lets not lose RFC 5011 tracking if we don't have to
  158. cp -p $UB_RKEY_FILE $UB_RKEY_FILE.keep
  159. ;;
  160. esac
  161. fi
  162. # Blind copy /etc/unbound to /var/lib/unbound
  163. mkdir -p $UB_VARDIR
  164. rm -f $UB_VARDIR/dhcp_*
  165. touch $UB_TOTAL_CONF
  166. cp -p /etc/unbound/* $UB_VARDIR/
  167. if [ ! -f $UB_RHINT_FILE ] ; then
  168. if [ -f /usr/share/dns/root.hints ] ; then
  169. # Debian-like package dns-root-data
  170. cp -p /usr/share/dns/root.hints $UB_RHINT_FILE
  171. elif [ "$UB_B_READY" -eq 0 ] ; then
  172. logger -t unbound -s "default root hints (built in root-servers.net)"
  173. fi
  174. fi
  175. if [ ! -f $UB_RKEY_FILE ] ; then
  176. if [ -f /usr/share/dns/root.key ] ; then
  177. # Debian-like package dns-root-data
  178. cp -p /usr/share/dns/root.key $UB_RKEY_FILE
  179. elif [ -x $UB_ANCHOR ] ; then
  180. $UB_ANCHOR -a $UB_RKEY_FILE
  181. elif [ "$UB_B_READY" -eq 0 ] ; then
  182. logger -t unbound -s "default trust anchor (built in root DS record)"
  183. fi
  184. fi
  185. if [ -f $UB_RKEY_FILE.keep ] ; then
  186. # root.key.keep is reused if newest
  187. cp -u $UB_RKEY_FILE.keep $UB_RKEY_FILE
  188. rm -f $UB_RKEY_FILE.keep
  189. fi
  190. if [ -f $UB_TLS_ETC_FILE ] ; then
  191. # copy the cert bundle into jail
  192. cp -p $UB_TLS_ETC_FILE $UB_TLS_FWD_FILE
  193. fi
  194. # Ensure access and prepare to jail
  195. chown -R unbound:unbound $UB_VARDIR
  196. chmod 755 $UB_VARDIR
  197. chmod 644 $UB_VARDIR/*
  198. if [ -f $UB_CTLKEY_FILE -o -f $UB_CTLPEM_FILE \
  199. -o -f $UB_SRVKEY_FILE -o -f $UB_SRVPEM_FILE ] ; then
  200. # Keys (some) exist already; do not create new ones
  201. chmod 640 $UB_CTLKEY_FILE $UB_CTLPEM_FILE \
  202. $UB_SRVKEY_FILE $UB_SRVPEM_FILE
  203. elif [ -x /usr/sbin/unbound-control-setup ] ; then
  204. case "$UB_D_CONTROL" in
  205. [2-3])
  206. # unbound-control-setup for encrypt opt. 2 and 3, but not 4 "static"
  207. /usr/sbin/unbound-control-setup -d $UB_VARDIR
  208. chown -R unbound:unbound $UB_CTLKEY_FILE $UB_CTLPEM_FILE \
  209. $UB_SRVKEY_FILE $UB_SRVPEM_FILE
  210. chmod 640 $UB_CTLKEY_FILE $UB_CTLPEM_FILE \
  211. $UB_SRVKEY_FILE $UB_SRVPEM_FILE
  212. cp -p $UB_CTLKEY_FILE /etc/unbound/unbound_control.key
  213. cp -p $UB_CTLPEM_FILE /etc/unbound/unbound_control.pem
  214. cp -p $UB_SRVKEY_FILE /etc/unbound/unbound_server.key
  215. cp -p $UB_SRVPEM_FILE /etc/unbound/unbound_server.pem
  216. ;;
  217. esac
  218. fi
  219. if [ -f "$UB_TIME_FILE" ] ; then
  220. # NTP is done so its like you actually had an RTC
  221. UB_B_READY=1
  222. UB_B_NTP_BOOT=0
  223. elif [ "$UB_B_NTP_BOOT" -eq 0 ] ; then
  224. # time is considered okay on this device (ignore /etc/hotplug/ntpd/unbound)
  225. date -Is > $UB_TIME_FILE
  226. UB_B_READY=0
  227. UB_B_NTP_BOOT=0
  228. else
  229. # DNSSEC-TIME will not reconcile
  230. UB_B_READY=0
  231. UB_B_NTP_BOOT=1
  232. fi
  233. }
  234. ##############################################################################
  235. unbound_control() {
  236. echo "# $UB_CTRL_CONF generated by UCI $( date -Is )" > $UB_CTRL_CONF
  237. if [ "$UB_D_CONTROL" -gt 1 ] ; then
  238. if [ ! -f $UB_CTLKEY_FILE -o ! -f $UB_CTLPEM_FILE \
  239. -o ! -f $UB_SRVKEY_FILE -o ! -f $UB_SRVPEM_FILE ] ; then
  240. # Key files need to be present; if unbound-control-setup was found, then
  241. # they might have been made during unbound_makedir() above.
  242. UB_D_CONTROL=0
  243. fi
  244. fi
  245. case "$UB_D_CONTROL" in
  246. 1)
  247. {
  248. # Local Host Only Unencrypted Remote Control
  249. echo "remote-control:"
  250. echo " control-enable: yes"
  251. echo " control-use-cert: no"
  252. echo " control-interface: 127.0.0.1"
  253. echo " control-interface: ::1"
  254. echo
  255. } >> $UB_CTRL_CONF
  256. ;;
  257. 2)
  258. {
  259. # Local Host Only Encrypted Remote Control
  260. echo "remote-control:"
  261. echo " control-enable: yes"
  262. echo " control-use-cert: yes"
  263. echo " control-interface: 127.0.0.1"
  264. echo " control-interface: ::1"
  265. echo " server-key-file: $UB_SRVKEY_FILE"
  266. echo " server-cert-file: $UB_SRVPEM_FILE"
  267. echo " control-key-file: $UB_CTLKEY_FILE"
  268. echo " control-cert-file: $UB_CTLPEM_FILE"
  269. echo
  270. } >> $UB_CTRL_CONF
  271. ;;
  272. [3-4])
  273. {
  274. # Network Encrypted Remote Control
  275. # (3) may auto setup and (4) must have static key/pem files
  276. # TODO: add UCI list for interfaces to bind
  277. echo "remote-control:"
  278. echo " control-enable: yes"
  279. echo " control-use-cert: yes"
  280. echo " control-interface: 0.0.0.0"
  281. echo " control-interface: ::0"
  282. echo " server-key-file: $UB_SRVKEY_FILE"
  283. echo " server-cert-file: $UB_SRVPEM_FILE"
  284. echo " control-key-file: $UB_CTLKEY_FILE"
  285. echo " control-cert-file: $UB_CTLPEM_FILE"
  286. echo
  287. } >> $UB_CTRL_CONF
  288. ;;
  289. esac
  290. }
  291. ##############################################################################
  292. unbound_zone() {
  293. local cfg=$1
  294. local servers_ip=""
  295. local servers_host=""
  296. local zone_sym zone_name zone_type zone_enabled zone_file
  297. local tls_upstream fallback
  298. local server port tls_port tls_index tls_suffix url_dir
  299. if [ ! -f "$UB_ZONE_CONF" ] ; then
  300. echo "# $UB_ZONE_CONF generated by UCI $( date -Is )" > $UB_ZONE_CONF
  301. fi
  302. config_get_bool zone_enabled "$cfg" enabled 0
  303. if [ "$zone_enabled" -eq 1 ] ; then
  304. # these lists are built for each zone; empty to start
  305. UB_LIST_ZONE_NAMES=""
  306. UB_LIST_ZONE_SERVERS=""
  307. config_get zone_type "$cfg" zone_type ""
  308. config_get port "$cfg" port ""
  309. config_get tls_index "$cfg" tls_index ""
  310. config_get tls_port "$cfg" tls_port 853
  311. config_get url_dir "$cfg" url_dir ""
  312. config_get_bool resolv_conf "$cfg" resolv_conf 0
  313. config_get_bool fallback "$cfg" fallback 1
  314. config_get_bool tls_upstream "$cfg" tls_upstream 0
  315. config_list_foreach "$cfg" zone_name bundle_zone_names
  316. config_list_foreach "$cfg" server bundle_zone_servers
  317. # string formating for Unbound syntax
  318. tls_suffix="${tls_port:+@${tls_port}${tls_index:+#${tls_index}}}"
  319. [ "$fallback" -eq 0 ] && fallback=no || fallback=yes
  320. [ "$tls_upstream" -eq 0 ] && tls_upstream=no || tls_upstream=yes
  321. if [ $resolv_conf -eq 1 ] ; then
  322. bundle_resolv_conf_servers
  323. fi
  324. else
  325. zone_type=skip
  326. fi
  327. case $zone_type in
  328. auth_zone)
  329. if [ "$UB_B_NTP_BOOT" -eq 0 -a -n "$UB_LIST_ZONE_NAMES" \
  330. -a \( -n "$url_dir" -o -n "$UB_LIST_ZONE_SERVERS" \) ] ; then
  331. # Note AXFR may have large downloads. If NTP restart is configured,
  332. # then this can cause procd to force a process kill.
  333. for zone_name in $UB_LIST_ZONE_NAMES ; do
  334. if [ "$zone_name" = "." ] ; then
  335. zone_sym=.
  336. zone_name=root
  337. zone_file=root.zone
  338. else
  339. zone_sym=$zone_name
  340. zone_file=$zone_name.zone
  341. zone_file=${zone_file//../.}
  342. fi
  343. {
  344. # generate an auth-zone: with switches for prefetch cache
  345. echo "auth-zone:"
  346. echo " name: $zone_sym"
  347. for server in $UB_LIST_ZONE_SERVERS ; do
  348. echo " master: $server${port:+@${port}}"
  349. done
  350. if [ -n "$url_dir" ] ; then
  351. echo " url: $url_dir$zone_file"
  352. fi
  353. echo " fallback-enabled: $fallback"
  354. echo " for-downstream: no"
  355. echo " for-upstream: yes"
  356. echo " zonefile: $zone_file"
  357. echo
  358. } >> $UB_ZONE_CONF
  359. done
  360. fi
  361. ;;
  362. forward_zone)
  363. if [ ! -f $UB_TLS_FWD_FILE -a "$tls_upstream" = "yes" ] ; then
  364. logger -p 4 -t unbound -s \
  365. "Forward-zone TLS benefits from authentication in package 'ca-bundle'"
  366. fi
  367. if [ -n "$UB_LIST_ZONE_NAMES" -a -n "$UB_LIST_ZONE_SERVERS" ] ; then
  368. for server in $UB_LIST_ZONE_SERVERS ; do
  369. if [ "$( valid_subnet_any $server )" = "not" ] ; then
  370. case $server in
  371. *@[0-9]*)
  372. # unique Unbound option for server host name
  373. servers_host="$servers_host $server"
  374. ;;
  375. *)
  376. if [ "$tls_upstream" = "yes" ] ; then
  377. servers_host="$servers_host $server${tls_port:+@${tls_port}}"
  378. else
  379. servers_host="$servers_host $server${port:+@${port}}"
  380. fi
  381. esac
  382. else
  383. case $server in
  384. *[0-9]@[0-9]*)
  385. # unique Unbound option for server address
  386. servers_ip="$servers_ip $server"
  387. ;;
  388. *)
  389. if [ "$tls_upstream" = "yes" ] ; then
  390. servers_ip="$servers_ip $server$tls_suffix"
  391. else
  392. servers_ip="$servers_ip $server${port:+@${port}}"
  393. fi
  394. esac
  395. fi
  396. done
  397. for zonename in $UB_LIST_ZONE_NAMES ; do
  398. {
  399. # generate a forward-zone with or without tls
  400. echo "forward-zone:"
  401. echo " name: $zonename"
  402. for server in $servers_host ; do
  403. echo " forward-host: $server"
  404. done
  405. for server in $servers_ip ; do
  406. echo " forward-addr: $server"
  407. done
  408. echo " forward-first: $fallback"
  409. echo " forward-tls-upstream: $tls_upstream"
  410. echo
  411. } >> $UB_ZONE_CONF
  412. done
  413. fi
  414. ;;
  415. stub_zone)
  416. if [ -n "$UB_LIST_ZONE_NAMES" -a -n "$UB_LIST_ZONE_SERVERS" ] ; then
  417. for zonename in $UB_LIST_ZONE_NAMES ; do
  418. {
  419. # generate a stub-zone: or ensure short cut to authority NS
  420. echo "stub-zone:"
  421. echo " name: $zonename"
  422. for server in $UB_LIST_ZONE_SERVERS ; do
  423. echo " stub-addr: $server${port:+@${port}}"
  424. done
  425. echo " stub-first: $fallback"
  426. echo
  427. } >> $UB_ZONE_CONF
  428. done
  429. fi
  430. ;;
  431. esac
  432. }
  433. ##############################################################################
  434. unbound_conf() {
  435. local rt_mem rt_conn rt_buff modulestring domain ifsubnet nsubnet
  436. {
  437. # server: for this whole function
  438. echo "# $UB_CORE_CONF generated by UCI $( date -Is )"
  439. echo "server:"
  440. echo " username: unbound"
  441. echo " chroot: $UB_VARDIR"
  442. echo " directory: $UB_VARDIR"
  443. echo " pidfile: $UB_PIDFILE"
  444. } > $UB_CORE_CONF
  445. if [ -f "$UB_TLS_FWD_FILE" ] ; then
  446. # TLS cert bundle for upstream forwarder and https zone files
  447. # This is loaded before drop to root, so pull from /etc/ssl
  448. echo " tls-cert-bundle: $UB_TLS_FWD_FILE" >> $UB_CORE_CONF
  449. fi
  450. if [ -f "$UB_RHINT_FILE" ] ; then
  451. # Optional hints if found
  452. echo " root-hints: $UB_RHINT_FILE" >> $UB_CORE_CONF
  453. fi
  454. if [ "$UB_B_DNSSEC" -gt 0 -a -f "$UB_RKEY_FILE" ] ; then
  455. {
  456. echo " auto-trust-anchor-file: $UB_RKEY_FILE"
  457. echo
  458. } >> $UB_CORE_CONF
  459. else
  460. echo >> $UB_CORE_CONF
  461. fi
  462. {
  463. # No threading
  464. echo " num-threads: 1"
  465. echo " msg-cache-slabs: 1"
  466. echo " rrset-cache-slabs: 1"
  467. echo " infra-cache-slabs: 1"
  468. echo " key-cache-slabs: 1"
  469. echo
  470. # Logging
  471. echo " use-syslog: yes"
  472. echo " statistics-interval: 0"
  473. echo " statistics-cumulative: no"
  474. } >> $UB_CORE_CONF
  475. if [ "$UB_D_VERBOSE" -ge 0 -a "$UB_D_VERBOSE" -le 5 ] ; then
  476. echo " verbosity: $UB_D_VERBOSE" >> $UB_CORE_CONF
  477. fi
  478. if [ "$UB_B_EXT_STATS" -gt 0 ] ; then
  479. {
  480. # Log More
  481. echo " extended-statistics: yes"
  482. echo
  483. } >> $UB_CORE_CONF
  484. else
  485. {
  486. # Log Less
  487. echo " extended-statistics: no"
  488. echo
  489. } >> $UB_CORE_CONF
  490. fi
  491. case "$UB_D_PROTOCOL" in
  492. ip4_only)
  493. {
  494. echo " edns-buffer-size: $UB_N_EDNS_SIZE"
  495. echo " port: $UB_N_RX_PORT"
  496. echo " outgoing-port-permit: 10240-65535"
  497. echo " interface: 0.0.0.0"
  498. echo " outgoing-interface: 0.0.0.0"
  499. echo " do-ip4: yes"
  500. echo " do-ip6: no"
  501. echo
  502. } >> $UB_CORE_CONF
  503. ;;
  504. ip6_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"
  510. echo " outgoing-interface: ::0"
  511. echo " do-ip4: no"
  512. echo " do-ip6: yes"
  513. echo
  514. } >> $UB_CORE_CONF
  515. ;;
  516. ip6_local)
  517. {
  518. echo " edns-buffer-size: $UB_N_EDNS_SIZE"
  519. echo " port: $UB_N_RX_PORT"
  520. echo " outgoing-port-permit: 10240-65535"
  521. echo " interface: 0.0.0.0"
  522. echo " interface: ::0"
  523. echo " outgoing-interface: 0.0.0.0"
  524. echo " do-ip4: yes"
  525. echo " do-ip6: yes"
  526. echo
  527. } >> $UB_CORE_CONF
  528. ;;
  529. ip6_prefer)
  530. {
  531. echo " edns-buffer-size: $UB_N_EDNS_SIZE"
  532. echo " port: $UB_N_RX_PORT"
  533. echo " outgoing-port-permit: 10240-65535"
  534. echo " interface: 0.0.0.0"
  535. echo " interface: ::0"
  536. echo " outgoing-interface: 0.0.0.0"
  537. echo " outgoing-interface: ::0"
  538. echo " do-ip4: yes"
  539. echo " do-ip6: yes"
  540. echo " prefer-ip6: yes"
  541. echo
  542. } >> $UB_CORE_CONF
  543. ;;
  544. mixed)
  545. {
  546. # Interface Wildcard (access contol handled by "option local_service")
  547. echo " edns-buffer-size: $UB_N_EDNS_SIZE"
  548. echo " port: $UB_N_RX_PORT"
  549. echo " outgoing-port-permit: 10240-65535"
  550. echo " interface: 0.0.0.0"
  551. echo " interface: ::0"
  552. echo " outgoing-interface: 0.0.0.0"
  553. echo " outgoing-interface: ::0"
  554. echo " do-ip4: yes"
  555. echo " do-ip6: yes"
  556. echo
  557. } >> $UB_CORE_CONF
  558. ;;
  559. *)
  560. if [ "$UB_B_READY" -eq 0 ] ; then
  561. logger -t unbound -s "default protocol configuration"
  562. fi
  563. {
  564. # outgoing-interface has useful defaults; incoming is localhost though
  565. echo " edns-buffer-size: $UB_N_EDNS_SIZE"
  566. echo " port: $UB_N_RX_PORT"
  567. echo " outgoing-port-permit: 10240-65535"
  568. echo " interface: 0.0.0.0"
  569. echo " interface: ::0"
  570. echo
  571. } >> $UB_CORE_CONF
  572. ;;
  573. esac
  574. case "$UB_D_RESOURCE" in
  575. # Tiny - Unbound's recommended cheap hardware config
  576. tiny) rt_mem=1 ; rt_conn=2 ; rt_buff=1 ;;
  577. # Small - Half RRCACHE and open ports
  578. small) rt_mem=8 ; rt_conn=10 ; rt_buff=2 ;;
  579. # Medium - Nearly default but with some added balancintg
  580. medium) rt_mem=16 ; rt_conn=15 ; rt_buff=4 ;;
  581. # Large - Double medium
  582. large) rt_mem=32 ; rt_conn=20 ; rt_buff=4 ;;
  583. # Whatever unbound does
  584. *) rt_mem=0 ; rt_conn=0 ;;
  585. esac
  586. if [ "$rt_mem" -gt 0 ] ; then
  587. {
  588. # Other harding and options for an embedded router
  589. echo " harden-short-bufsize: yes"
  590. echo " harden-large-queries: yes"
  591. echo " harden-glue: yes"
  592. echo " use-caps-for-id: no"
  593. echo
  594. # Set memory sizing parameters
  595. echo " msg-buffer-size: $(($rt_buff*8192))"
  596. echo " outgoing-range: $(($rt_conn*32))"
  597. echo " num-queries-per-thread: $(($rt_conn*16))"
  598. echo " outgoing-num-tcp: $(($rt_conn))"
  599. echo " incoming-num-tcp: $(($rt_conn))"
  600. echo " rrset-cache-size: $(($rt_mem*256))k"
  601. echo " msg-cache-size: $(($rt_mem*128))k"
  602. echo " key-cache-size: $(($rt_mem*128))k"
  603. echo " neg-cache-size: $(($rt_mem*64))k"
  604. echo " infra-cache-numhosts: $(($rt_mem*256))"
  605. echo
  606. } >> $UB_CORE_CONF
  607. elif [ "$UB_B_READY" -eq 0 ] ; then
  608. logger -t unbound -s "default memory configuration"
  609. fi
  610. # Assembly of module-config: options is tricky; order matters
  611. modulestring="iterator"
  612. if [ "$UB_B_DNSSEC" -gt 0 ] ; then
  613. if [ "$UB_B_NTP_BOOT" -gt 0 ] ; then
  614. # DNSSEC chicken and egg with getting NTP time
  615. echo " val-override-date: -1" >> $UB_CORE_CONF
  616. fi
  617. {
  618. echo " harden-dnssec-stripped: yes"
  619. echo " val-clean-additional: yes"
  620. echo " ignore-cd-flag: yes"
  621. } >> $UB_CORE_CONF
  622. modulestring="validator $modulestring"
  623. fi
  624. if [ "$UB_B_DNS64" -gt 0 ] ; then
  625. echo " dns64-prefix: $UB_IP_DNS64" >> $UB_CORE_CONF
  626. modulestring="dns64 $modulestring"
  627. fi
  628. {
  629. # Print final module string
  630. echo " module-config: \"$modulestring\""
  631. echo
  632. } >> $UB_CORE_CONF
  633. case "$UB_D_RECURSION" in
  634. passive)
  635. {
  636. # Some query privacy but "strict" will break some servers
  637. if [ "$UB_B_QRY_MINST" -gt 0 \
  638. -a "$UB_B_QUERY_MIN" -gt 0 ] ; then
  639. echo " qname-minimisation: yes"
  640. echo " qname-minimisation-strict: yes"
  641. elif [ "$UB_B_QUERY_MIN" -gt 0 ] ; then
  642. echo " qname-minimisation: yes"
  643. else
  644. echo " qname-minimisation: no"
  645. fi
  646. # Use DNSSEC to quickly understand NXDOMAIN ranges
  647. if [ "$UB_B_DNSSEC" -gt 0 ] ; then
  648. echo " aggressive-nsec: yes"
  649. echo " prefetch-key: no"
  650. fi
  651. # On demand fetching
  652. echo " prefetch: no"
  653. echo " target-fetch-policy: \"0 0 0 0 0\""
  654. echo
  655. } >> $UB_CORE_CONF
  656. ;;
  657. aggressive)
  658. {
  659. # Some query privacy but "strict" will break some servers
  660. if [ "$UB_B_QRY_MINST" -gt 0 \
  661. -a "$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" -a "$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 -a -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. -a \( "$UB_D_WAN_FQDN" -gt 0 -o "$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 -a "$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" -a "$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" -a "$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 dnsmasqpath 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 -o ! -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 -a "$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 -o ! -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 -a "$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 \
  1033. -o 4096 -lt "$UB_N_EDNS_SIZE" ] ; then
  1034. logger -t unbound -s "edns_size exceeds range, using default"
  1035. UB_N_EDNS_SIZE=1280
  1036. fi
  1037. if [ "$UB_N_RX_PORT" -ne 53 \
  1038. -a \( "$UB_N_RX_PORT" -lt 1024 -o 10240 -lt "$UB_N_RX_PORT" \) ] ; then
  1039. logger -t unbound -s "privileged port or in 5 digits, using default"
  1040. UB_N_RX_PORT=53
  1041. fi
  1042. if [ "$UB_TTL_MIN" -gt 1800 ] ; then
  1043. logger -t unbound -s "ttl_min could have had awful side effects, using 300"
  1044. UB_TTL_MIN=300
  1045. fi
  1046. }
  1047. ##############################################################################
  1048. unbound_include() {
  1049. local adb_enabled
  1050. local adb_files=$( ls $UB_VARDIR/adb_list.* 2>/dev/null )
  1051. echo "# $UB_TOTAL_CONF generated by UCI $( date -Is )" > $UB_TOTAL_CONF
  1052. if [ -f "$UB_CORE_CONF" ] ; then
  1053. # Yes this all looks busy, but it is in TMPFS. Working on separate files
  1054. # and piecing together is easier. UCI order is less constrained.
  1055. cat $UB_CORE_CONF >> $UB_TOTAL_CONF
  1056. rm $UB_CORE_CONF
  1057. fi
  1058. if [ -f "$UB_HOST_CONF" ] ; then
  1059. # UCI definitions of local host or local subnet
  1060. cat $UB_HOST_CONF >> $UB_TOTAL_CONF
  1061. rm $UB_HOST_CONF
  1062. fi
  1063. if [ -f $UB_SRVMASQ_CONF ] ; then
  1064. # UCI found link to dnsmasq
  1065. cat $UB_SRVMASQ_CONF >> $UB_TOTAL_CONF
  1066. rm $UB_SRVMASQ_CONF
  1067. fi
  1068. if [ -f "$UB_DHCP_CONF" ] ; then
  1069. {
  1070. # Seed DHCP records because dhcp scripts trigger externally
  1071. # Incremental Unbound restarts may drop unbound-control records
  1072. echo "include: $UB_DHCP_CONF"
  1073. echo
  1074. }>> $UB_TOTAL_CONF
  1075. fi
  1076. if [ -z "$adb_files" \
  1077. -o ! -x /usr/bin/adblock.sh -o ! -x /etc/init.d/adblock ] ; then
  1078. adb_enabled=0
  1079. elif /etc/init.d/adblock enabled ; then
  1080. adb_enabled=1
  1081. {
  1082. # Pull in your selected openwrt/pacakges/net/adblock generated lists
  1083. echo "include: $UB_VARDIR/adb_list.*"
  1084. echo
  1085. } >> $UB_TOTAL_CONF
  1086. else
  1087. adb_enabled=0
  1088. fi
  1089. if [ -f $UB_SRV_CONF ] ; then
  1090. {
  1091. # Pull your own "server:" options here
  1092. echo "include: $UB_SRV_CONF"
  1093. echo
  1094. }>> $UB_TOTAL_CONF
  1095. fi
  1096. if [ -f "$UB_ZONE_CONF" ] ; then
  1097. # UCI defined forward, stub, and auth zones
  1098. cat $UB_ZONE_CONF >> $UB_TOTAL_CONF
  1099. rm $UB_ZONE_CONF
  1100. fi
  1101. if [ -f "$UB_CTRL_CONF" ] ; then
  1102. # UCI defined control application connection
  1103. cat $UB_CTRL_CONF >> $UB_TOTAL_CONF
  1104. rm $UB_CTRL_CONF
  1105. fi
  1106. if [ -f "$UB_EXTMASQ_CONF" ] ; then
  1107. # UCI found link to dnsmasq
  1108. cat $UB_EXTMASQ_CONF >> $UB_TOTAL_CONF
  1109. rm $UB_EXTMASQ_CONF
  1110. fi
  1111. if [ -f "$UB_EXT_CONF" ] ; then
  1112. {
  1113. # Pull your own extend feature clauses here
  1114. echo "include: $UB_EXT_CONF"
  1115. echo
  1116. } >> $UB_TOTAL_CONF
  1117. fi
  1118. }
  1119. ##############################################################################
  1120. resolv_setup() {
  1121. if [ "$UB_N_RX_PORT" != "53" ] ; then
  1122. return
  1123. elif [ -x /etc/init.d/dnsmasq ] \
  1124. && /etc/init.d/dnsmasq enabled \
  1125. && nslookup localhost 127.0.0.1#53 >/dev/null 2>&1 ; then
  1126. # unbound is configured for port 53, but dnsmasq is enabled and a resolver
  1127. # listens on localhost:53, lets assume dnsmasq manages the resolver file.
  1128. # TODO:
  1129. # really check if dnsmasq runs a local (main) resolver in stead of using
  1130. # nslookup that times out when no resolver listens on localhost:53.
  1131. return
  1132. fi
  1133. # unbound is designated to listen on 127.0.0.1#53,
  1134. # set resolver file to local.
  1135. rm -f /tmp/resolv.conf
  1136. {
  1137. echo "# /tmp/resolv.conf generated by Unbound UCI $( date -Is )"
  1138. echo "nameserver 127.0.0.1"
  1139. echo "nameserver ::1"
  1140. echo "search $UB_TXT_DOMAIN."
  1141. } > /tmp/resolv.conf
  1142. }
  1143. ##############################################################################
  1144. unbound_start() {
  1145. config_load unbound
  1146. config_foreach unbound_uci unbound
  1147. unbound_mkdir
  1148. if [ "$UB_B_MAN_CONF" -eq 0 ] ; then
  1149. # iterate zones before we load other UCI
  1150. # forward-zone: auth-zone: and stub-zone:
  1151. config_foreach unbound_zone zone
  1152. # associate potential DNS RR with interfaces
  1153. config_load network
  1154. config_foreach bundle_all_networks interface
  1155. config_load dhcp
  1156. config_foreach bundle_lan_networks dhcp
  1157. bundle_wan_networks
  1158. # server:
  1159. unbound_conf
  1160. unbound_hostname
  1161. # control:
  1162. unbound_control
  1163. # dnsmasq
  1164. dnsmasq_link
  1165. # merge
  1166. unbound_include
  1167. fi
  1168. resolv_setup
  1169. }
  1170. ##############################################################################