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.

875 lines
23 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. # This builds the basic UCI components currently supported for Unbound. It is
  18. # intentionally NOT comprehensive and bundles a lot of options. The UCI is to
  19. # be a simpler presentation of the total Unbound conf set.
  20. #
  21. ##############################################################################
  22. UNBOUND_B_CONTROL=0
  23. UNBOUND_B_SLAAC6_MAC=0
  24. UNBOUND_B_DNSSEC=0
  25. UNBOUND_B_DNS64=0
  26. UNBOUND_B_GATE_NAME=0
  27. UNBOUND_B_HIDE_BIND=1
  28. UNBOUND_B_LOCL_BLCK=0
  29. UNBOUND_B_LOCL_SERV=1
  30. UNBOUND_B_MAN_CONF=0
  31. UNBOUND_B_NTP_BOOT=1
  32. UNBOUND_B_PRIV_BLCK=1
  33. UNBOUND_B_QUERY_MIN=0
  34. UNBOUND_B_QRY_MINST=0
  35. UNBOUND_D_DOMAIN_TYPE=static
  36. UNBOUND_D_DHCP_LINK=none
  37. UNBOUND_D_LAN_FQDN=0
  38. UNBOUND_D_PROTOCOL=mixed
  39. UNBOUND_D_RESOURCE=small
  40. UNBOUND_D_RECURSION=passive
  41. UNBOUND_D_WAN_FQDN=0
  42. UNBOUND_IP_DNS64="64:ff9b::/96"
  43. UNBOUND_N_EDNS_SIZE=1280
  44. UNBOUND_N_FWD_PORTS=""
  45. UNBOUND_N_RX_PORT=53
  46. UNBOUND_N_ROOT_AGE=28
  47. UNBOUND_TTL_MIN=120
  48. UNBOUND_TXT_DOMAIN=lan
  49. UNBOUND_TXT_FWD_ZONE=""
  50. UNBOUND_TXT_HOSTNAME=thisrouter
  51. ##############################################################################
  52. UNBOUND_LIBDIR=/usr/lib/unbound
  53. UNBOUND_VARDIR=/var/lib/unbound
  54. UNBOUND_PIDFILE=/var/run/unbound.pid
  55. UNBOUND_SRV_CONF=$UNBOUND_VARDIR/unbound_srv.conf
  56. UNBOUND_EXT_CONF=$UNBOUND_VARDIR/unbound_ext.conf
  57. UNBOUND_DHCP_CONF=$UNBOUND_VARDIR/unbound_dhcp.conf
  58. UNBOUND_CONFFILE=$UNBOUND_VARDIR/unbound.conf
  59. UNBOUND_KEYFILE=$UNBOUND_VARDIR/root.key
  60. UNBOUND_HINTFILE=$UNBOUND_VARDIR/root.hints
  61. UNBOUND_TIMEFILE=$UNBOUND_VARDIR/unbound.time
  62. ##############################################################################
  63. UNBOUND_ANCHOR=/usr/sbin/unbound-anchor
  64. UNBOUND_CONTROL=/usr/sbin/unbound-control
  65. UNBOUND_CONTROL_CFG="$UNBOUND_CONTROL -c $UNBOUND_CONFFILE"
  66. ##############################################################################
  67. . /lib/functions.sh
  68. . /lib/functions/network.sh
  69. . $UNBOUND_LIBDIR/dnsmasq.sh
  70. . $UNBOUND_LIBDIR/iptools.sh
  71. . $UNBOUND_LIBDIR/rootzone.sh
  72. ##############################################################################
  73. create_interface_dns() {
  74. local cfg="$1"
  75. local ipcommand logint ignore ifname ifdashname
  76. local name names address addresses
  77. local ulaprefix if_fqdn host_fqdn mode mode_ptr
  78. # Create local-data: references for this hosts interfaces (router).
  79. config_get logint "$cfg" interface
  80. config_get_bool ignore "$cfg" ignore 0
  81. network_get_device ifname "$cfg"
  82. ifdashname="${ifname//./-}"
  83. ipcommand="ip -o address show $ifname"
  84. addresses="$($ipcommand | awk '/inet/{sub(/\/.*/,"",$4); print $4}')"
  85. ulaprefix="$(uci_get network @globals[0] ula_prefix)"
  86. host_fqdn="$UNBOUND_TXT_HOSTNAME.$UNBOUND_TXT_DOMAIN"
  87. if_fqdn="$ifdashname.$host_fqdn"
  88. if [ "$ignore" -gt 0 ] ; then
  89. mode="$UNBOUND_D_WAN_FQDN"
  90. else
  91. mode="$UNBOUND_D_LAN_FQDN"
  92. fi
  93. case "$mode" in
  94. 3)
  95. mode_ptr="$host_fqdn"
  96. names="$host_fqdn $UNBOUND_TXT_HOSTNAME"
  97. ;;
  98. 4)
  99. mode_ptr="$if_fqdn"
  100. names="$if_fqdn $host_fqdn $UNBOUND_TXT_HOSTNAME"
  101. ;;
  102. *)
  103. mode_ptr="$UNBOUND_TXT_HOSTNAME"
  104. names="$UNBOUND_TXT_HOSTNAME"
  105. ;;
  106. esac
  107. if [ "$mode" -gt 1 ] ; then
  108. {
  109. for address in $addresses ; do
  110. case $address in
  111. fe80:*|169.254.*)
  112. echo " # note link address $address"
  113. ;;
  114. [1-9a-f]*:*[0-9a-f])
  115. # GA and ULA IP6 for HOST IN AAA records (ip command is robust)
  116. for name in $names ; do
  117. echo " local-data: \"$name. 120 IN AAAA $address\""
  118. done
  119. echo " local-data-ptr: \"$address 120 $mode_ptr\""
  120. ;;
  121. [1-9]*.*[0-9])
  122. # Old fashioned HOST IN A records
  123. for name in $names ; do
  124. echo " local-data: \"$name. 120 IN A $address\""
  125. done
  126. echo " local-data-ptr: \"$address 120 $mode_ptr\""
  127. ;;
  128. esac
  129. done
  130. echo
  131. } >> $UNBOUND_CONFFILE
  132. elif [ "$mode" -gt 0 ] ; then
  133. {
  134. for address in $addresses ; do
  135. case $address in
  136. fe80:*|169.254.*)
  137. echo " # note link address $address"
  138. ;;
  139. "${ulaprefix%%:/*}"*)
  140. # Only this networks ULA and only hostname
  141. echo " local-data: \"$UNBOUND_TXT_HOSTNAME. 120 IN AAAA $address\""
  142. echo " local-data-ptr: \"$address 120 $UNBOUND_TXT_HOSTNAME\""
  143. ;;
  144. [1-9]*.*[0-9])
  145. echo " local-data: \"$UNBOUND_TXT_HOSTNAME. 120 IN A $address\""
  146. echo " local-data-ptr: \"$address 120 $UNBOUND_TXT_HOSTNAME\""
  147. ;;
  148. esac
  149. done
  150. echo
  151. } >> $UNBOUND_CONFFILE
  152. fi
  153. }
  154. ##############################################################################
  155. create_access_control() {
  156. local cfg="$1"
  157. local subnets subnets4 subnets6
  158. local validip4 validip6
  159. network_get_subnets subnets4 "$cfg"
  160. network_get_subnets6 subnets6 "$cfg"
  161. subnets="$subnets4 $subnets6"
  162. if [ -n "$subnets" ] ; then
  163. for subnet in $subnets ; do
  164. validip4=$( valid_subnet4 $subnet )
  165. validip6=$( valid_subnet6 $subnet )
  166. if [ "$validip4" = "ok" -o "$validip6" = "ok" ] ; then
  167. # For each "network" UCI add "access-control:" white list for queries
  168. echo " access-control: $subnet allow" >> $UNBOUND_CONFFILE
  169. fi
  170. done
  171. fi
  172. }
  173. ##############################################################################
  174. create_domain_insecure() {
  175. echo " domain-insecure: \"$1\"" >> $UNBOUND_CONFFILE
  176. }
  177. ##############################################################################
  178. unbound_mkdir() {
  179. local resolvsym=0
  180. local dhcp_origin=$( uci get dhcp.@odhcpd[0].leasefile )
  181. local dhcp_dir=$( dirname "$dhcp_origin" )
  182. if [ ! -x /usr/sbin/dnsmasq -o ! -x /etc/init.d/dnsmasq ] ; then
  183. resolvsym=1
  184. else
  185. /etc/init.d/dnsmasq enabled || resolvsym=1
  186. fi
  187. if [ "$resolvsym" -gt 0 ] ; then
  188. rm -f /tmp/resolv.conf
  189. {
  190. # Set resolver file to local but not if /etc/init.d/dnsmasq will do it.
  191. echo "nameserver 127.0.0.1"
  192. echo "nameserver ::1"
  193. echo "search $UNBOUND_TXT_DOMAIN"
  194. } > /tmp/resolv.conf
  195. fi
  196. if [ "$UNBOUND_D_DHCP_LINK" = "odhcpd" -a ! -d "$dhcp_dir" ] ; then
  197. # make sure odhcpd has a directory to write (not done itself, yet)
  198. mkdir -p "$dhcp_dir"
  199. fi
  200. mkdir -p $UNBOUND_VARDIR
  201. rm -f $UNBOUND_VARDIR/dhcp_*
  202. touch $UNBOUND_CONFFILE
  203. touch $UNBOUND_SRV_CONF
  204. touch $UNBOUND_EXT_CONF
  205. cp -p /etc/unbound/* $UNBOUND_VARDIR/
  206. if [ ! -f $UNBOUND_HINTFILE ] ; then
  207. if [ -f /usr/share/dns/root.hints ] ; then
  208. # Debian-like package dns-root-data
  209. cp -p /usr/share/dns/root.hints $UNBOUND_HINTFILE
  210. else
  211. logger -t unbound -s "iterator will use built-in root hints"
  212. fi
  213. fi
  214. if [ ! -f $UNBOUND_KEYFILE ] ; then
  215. if [ -f /usr/share/dns/root.key ] ; then
  216. # Debian-like package dns-root-data
  217. cp -p /usr/share/dns/root.key $UNBOUND_KEYFILE
  218. elif [ -x "$UNBOUND_ANCHOR" ] ; then
  219. $UNBOUND_ANCHOR -a $UNBOUND_KEYFILE
  220. else
  221. logger -t unbound -s "validator will use built-in trust anchor"
  222. fi
  223. fi
  224. # Ensure access and prepare to jail
  225. chown -R unbound:unbound $UNBOUND_VARDIR
  226. chmod 775 $UNBOUND_VARDIR
  227. chmod 664 $UNBOUND_VARDIR/*
  228. }
  229. ##############################################################################
  230. unbound_control() {
  231. if [ "$UNBOUND_B_CONTROL" -gt 0 ] ; then
  232. {
  233. # Enable remote control tool, but only at local host for security
  234. # You can hand write fancier encrypted access with /etc/..._ext.conf
  235. echo "remote-control:"
  236. echo " control-enable: yes"
  237. echo " control-use-cert: no"
  238. echo " control-interface: 127.0.0.1"
  239. echo " control-interface: ::1"
  240. echo
  241. } >> $UNBOUND_CONFFILE
  242. fi
  243. {
  244. # Amend your own extended clauses here like forward zones or disable
  245. # above (local, no encryption) and amend your own remote encrypted control
  246. echo
  247. echo "include: $UNBOUND_EXT_CONF" >> $UNBOUND_CONFFILE
  248. echo
  249. } >> $UNBOUND_CONFFILE
  250. }
  251. ##############################################################################
  252. unbound_conf() {
  253. local cfg="$1"
  254. local rt_mem rt_conn modulestring
  255. {
  256. # Make fresh conf file
  257. echo "# $UNBOUND_CONFFILE generated by UCI $( date )"
  258. echo
  259. } > $UNBOUND_CONFFILE
  260. {
  261. # No threading
  262. echo "server:"
  263. echo " username: unbound"
  264. echo " num-threads: 1"
  265. echo " msg-cache-slabs: 1"
  266. echo " rrset-cache-slabs: 1"
  267. echo " infra-cache-slabs: 1"
  268. echo " key-cache-slabs: 1"
  269. echo
  270. } >> $UNBOUND_CONFFILE
  271. {
  272. # Logging
  273. echo " verbosity: 1"
  274. echo " statistics-interval: 0"
  275. echo " statistics-cumulative: no"
  276. echo " extended-statistics: no"
  277. echo
  278. } >> $UNBOUND_CONFFILE
  279. {
  280. # Interfaces (access contol "option local_service")
  281. echo " interface: 0.0.0.0"
  282. echo " interface: ::0"
  283. echo " outgoing-interface: 0.0.0.0"
  284. echo " outgoing-interface: ::0"
  285. echo
  286. } >> $UNBOUND_CONFFILE
  287. case "$UNBOUND_D_PROTOCOL" in
  288. ip4_only)
  289. {
  290. echo " do-ip4: yes"
  291. echo " do-ip6: no"
  292. } >> $UNBOUND_CONFFILE
  293. ;;
  294. ip6_only)
  295. {
  296. echo " do-ip4: no"
  297. echo " do-ip6: yes"
  298. } >> $UNBOUND_CONFFILE
  299. ;;
  300. ip6_prefer)
  301. {
  302. echo " do-ip4: yes"
  303. echo " do-ip6: yes"
  304. echo " prefer-ip6: yes"
  305. } >> $UNBOUND_CONFFILE
  306. ;;
  307. *)
  308. {
  309. echo " do-ip4: yes"
  310. echo " do-ip6: yes"
  311. } >> $UNBOUND_CONFFILE
  312. ;;
  313. esac
  314. {
  315. # protocol level tuning
  316. echo " edns-buffer-size: $UNBOUND_N_EDNS_SIZE"
  317. echo " msg-buffer-size: 8192"
  318. echo " port: $UNBOUND_N_RX_PORT"
  319. echo " outgoing-port-permit: 10240-65535"
  320. echo
  321. } >> $UNBOUND_CONFFILE
  322. {
  323. # Other harding and options for an embedded router
  324. echo " harden-short-bufsize: yes"
  325. echo " harden-large-queries: yes"
  326. echo " harden-glue: yes"
  327. echo " harden-below-nxdomain: no"
  328. echo " harden-referral-path: no"
  329. echo " use-caps-for-id: no"
  330. echo
  331. } >> $UNBOUND_CONFFILE
  332. {
  333. # Default Files
  334. echo " use-syslog: yes"
  335. echo " chroot: \"$UNBOUND_VARDIR\""
  336. echo " directory: \"$UNBOUND_VARDIR\""
  337. echo " pidfile: \"$UNBOUND_PIDFILE\""
  338. } >> $UNBOUND_CONFFILE
  339. if [ -f "$UNBOUND_HINTFILE" ] ; then
  340. # Optional hints if found
  341. echo " root-hints: \"$UNBOUND_HINTFILE\"" >> $UNBOUND_CONFFILE
  342. fi
  343. if [ "$UNBOUND_B_DNSSEC" -gt 0 -a -f "$UNBOUND_KEYFILE" ] ; then
  344. {
  345. echo " auto-trust-anchor-file: \"$UNBOUND_KEYFILE\""
  346. echo
  347. } >> $UNBOUND_CONFFILE
  348. else
  349. echo >> $UNBOUND_CONFFILE
  350. fi
  351. case "$UNBOUND_D_RESOURCE" in
  352. # Tiny - Unbound's recommended cheap hardware config
  353. tiny) rt_mem=1 ; rt_conn=1 ;;
  354. # Small - Half RRCACHE and open ports
  355. small) rt_mem=8 ; rt_conn=5 ;;
  356. # Medium - Nearly default but with some added balancintg
  357. medium) rt_mem=16 ; rt_conn=10 ;;
  358. # Large - Double medium
  359. large) rt_mem=32 ; rt_conn=10 ;;
  360. # Whatever unbound does
  361. *) rt_mem=0 ; rt_conn=0 ;;
  362. esac
  363. if [ "$rt_mem" -gt 0 ] ; then
  364. {
  365. # Set memory sizing parameters
  366. echo " outgoing-range: $(($rt_conn*64))"
  367. echo " num-queries-per-thread: $(($rt_conn*32))"
  368. echo " outgoing-num-tcp: $(($rt_conn))"
  369. echo " incoming-num-tcp: $(($rt_conn))"
  370. echo " rrset-cache-size: $(($rt_mem*256))k"
  371. echo " msg-cache-size: $(($rt_mem*128))k"
  372. echo " key-cache-size: $(($rt_mem*128))k"
  373. echo " neg-cache-size: $(($rt_mem*64))k"
  374. echo " infra-cache-numhosts: $(($rt_mem*256))"
  375. echo
  376. } >> $UNBOUND_CONFFILE
  377. else
  378. logger -t unbound -s "default memory resource consumption"
  379. fi
  380. # Assembly of module-config: options is tricky; order matters
  381. modulestring="iterator"
  382. if [ "$UNBOUND_B_DNSSEC" -gt 0 ] ; then
  383. if [ ! -f "$UNBOUND_TIMEFILE" -a "$UNBOUND_B_NTP_BOOT" -gt 0 ] ; then
  384. # DNSSEC chicken and egg with getting NTP time
  385. echo " val-override-date: -1" >> $UNBOUND_CONFFILE
  386. fi
  387. {
  388. echo " harden-dnssec-stripped: yes"
  389. echo " val-clean-additional: yes"
  390. echo " ignore-cd-flag: yes"
  391. } >> $UNBOUND_CONFFILE
  392. modulestring="validator $modulestring"
  393. fi
  394. if [ "$UNBOUND_B_DNS64" -gt 0 ] ; then
  395. echo " dns64-prefix: $UNBOUND_IP_DNS64" >> $UNBOUND_CONFFILE
  396. modulestring="dns64 $modulestring"
  397. fi
  398. {
  399. # Print final module string
  400. echo " module-config: \"$modulestring\""
  401. echo
  402. } >> $UNBOUND_CONFFILE
  403. if [ "$UNBOUND_B_QRY_MINST" -gt 0 -a "$UNBOUND_B_QUERY_MIN" -gt 0 ] ; then
  404. {
  405. # Some query privacy but "strict" will break some name servers
  406. echo " qname-minimisation: yes"
  407. echo " qname-minimisation-strict: yes"
  408. } >> $UNBOUND_CONFFILE
  409. elif [ "$UNBOUND_B_QUERY_MIN" -gt 0 ] ; then
  410. # Minor improvement on query privacy
  411. echo " qname-minimisation: yes" >> $UNBOUND_CONFFILE
  412. else
  413. echo " qname-minimisation: no" >> $UNBOUND_CONFFILE
  414. fi
  415. case "$UNBOUND_D_RECURSION" in
  416. passive)
  417. {
  418. echo " prefetch: no"
  419. echo " prefetch-key: no"
  420. echo " target-fetch-policy: \"0 0 0 0 0\""
  421. echo
  422. } >> $UNBOUND_CONFFILE
  423. ;;
  424. aggressive)
  425. {
  426. echo " prefetch: yes"
  427. echo " prefetch-key: yes"
  428. echo " target-fetch-policy: \"3 2 1 0 0\""
  429. echo
  430. } >> $UNBOUND_CONFFILE
  431. ;;
  432. *)
  433. logger -t unbound -s "default recursion configuration"
  434. ;;
  435. esac
  436. {
  437. # Reload records more than 10 hours old
  438. # DNSSEC 5 minute bogus cool down before retry
  439. # Adaptive infrastructure info kept for 15 minutes
  440. echo " cache-min-ttl: $UNBOUND_TTL_MIN"
  441. echo " cache-max-ttl: 36000"
  442. echo " val-bogus-ttl: 300"
  443. echo " infra-host-ttl: 900"
  444. echo
  445. } >> $UNBOUND_CONFFILE
  446. if [ "$UNBOUND_B_HIDE_BIND" -gt 0 ] ; then
  447. {
  448. # Block server id and version DNS TXT records
  449. echo " hide-identity: yes"
  450. echo " hide-version: yes"
  451. echo
  452. } >> $UNBOUND_CONFFILE
  453. fi
  454. if [ "$UNBOUND_B_PRIV_BLCK" -gt 0 ] ; then
  455. {
  456. # Remove _upstream_ or global reponses with private addresses.
  457. # Unbounds own "local zone" and "forward zone" may still use these.
  458. # RFC1918, RFC3927, RFC4291, RFC6598, RFC6890
  459. echo " private-address: 10.0.0.0/8"
  460. echo " private-address: 100.64.0.0/10"
  461. echo " private-address: 169.254.0.0/16"
  462. echo " private-address: 172.16.0.0/12"
  463. echo " private-address: 192.168.0.0/16"
  464. echo " private-address: fc00::/8"
  465. echo " private-address: fd00::/8"
  466. echo " private-address: fe80::/10"
  467. } >> $UNBOUND_CONFFILE
  468. fi
  469. if [ "$UNBOUND_B_LOCL_BLCK" -gt 0 ] ; then
  470. {
  471. # Remove DNS reponses from upstream with loopback IP
  472. # Black hole DNS method for ad blocking, so consider...
  473. echo " private-address: 127.0.0.0/8"
  474. echo " private-address: ::1/128"
  475. echo
  476. } >> $UNBOUND_CONFFILE
  477. else
  478. echo >> $UNBOUND_CONFFILE
  479. fi
  480. # Except and accept domains as insecure (DNSSEC); work around broken domains
  481. config_list_foreach "$cfg" "domain_insecure" create_domain_insecure
  482. echo >> $UNBOUND_CONFFILE
  483. }
  484. ##############################################################################
  485. unbound_access() {
  486. # TODO: Unbound 1.6.0 added "tags" and "views", so we can add tags to
  487. # each access-control IP block, and then divert access.
  488. # -- "guest" WIFI will not be allowed to see local zone data
  489. # -- "child" LAN can black whole a list of domains to http~deadpixel
  490. if [ "$UNBOUND_B_LOCL_SERV" -gt 0 ] ; then
  491. # Only respond to queries from which this device has an interface.
  492. # Prevent DNS amplification attacks by not responding to the universe.
  493. config_load network
  494. config_foreach create_access_control interface
  495. {
  496. echo " access-control: 127.0.0.0/8 allow"
  497. echo " access-control: ::1/128 allow"
  498. echo " access-control: fe80::/10 allow"
  499. echo
  500. } >> $UNBOUND_CONFFILE
  501. else
  502. {
  503. echo " access-control: 0.0.0.0/0 allow"
  504. echo " access-control: ::0/0 allow"
  505. echo
  506. } >> $UNBOUND_CONFFILE
  507. fi
  508. {
  509. # Amend your own "server:" stuff here
  510. echo " include: $UNBOUND_SRV_CONF"
  511. echo
  512. } >> $UNBOUND_CONFFILE
  513. }
  514. ##############################################################################
  515. unbound_adblock() {
  516. # TODO: Unbound 1.6.0 added "tags" and "views"; lets work with adblock team
  517. local adb_enabled adb_file
  518. if [ ! -x /usr/bin/adblock.sh -o ! -x /etc/init.d/adblock ] ; then
  519. adb_enabled=0
  520. else
  521. /etc/init.d/adblock enabled && adb_enabled=1 || adb_enabled=0
  522. fi
  523. if [ "$adb_enabled" -gt 0 ] ; then
  524. {
  525. # Pull in your selected openwrt/pacakges/net/adblock generated lists
  526. for adb_file in $UNBOUND_VARDIR/adb_list.* ; do
  527. echo " include: $adb_file"
  528. done
  529. echo
  530. } >> $UNBOUND_CONFFILE
  531. fi
  532. }
  533. ##############################################################################
  534. unbound_hostname() {
  535. if [ -n "$UNBOUND_TXT_DOMAIN" ] ; then
  536. {
  537. # TODO: Unbound 1.6.0 added "tags" and "views" and we could make
  538. # domains by interface to prevent DNS from "guest" to "home"
  539. echo " local-zone: $UNBOUND_TXT_DOMAIN. $UNBOUND_D_DOMAIN_TYPE"
  540. echo " domain-insecure: $UNBOUND_TXT_DOMAIN"
  541. echo " private-domain: $UNBOUND_TXT_DOMAIN"
  542. echo
  543. echo " local-zone: $UNBOUND_TXT_HOSTNAME. $UNBOUND_D_DOMAIN_TYPE"
  544. echo " domain-insecure: $UNBOUND_TXT_HOSTNAME"
  545. echo " private-domain: $UNBOUND_TXT_HOSTNAME"
  546. echo
  547. } >> $UNBOUND_CONFFILE
  548. case "$UNBOUND_D_DOMAIN_TYPE" in
  549. deny|inform_deny|refuse|static)
  550. {
  551. # avoid upstream involvement in RFC6762 like responses (link only)
  552. echo " local-zone: local. $UNBOUND_D_DOMAIN_TYPE"
  553. echo " domain-insecure: local"
  554. echo " private-domain: local"
  555. echo
  556. } >> $UNBOUND_CONFFILE
  557. ;;
  558. esac
  559. if [ "$UNBOUND_D_LAN_FQDN" -gt 0 -o "$UNBOUND_D_WAN_FQDN" -gt 0 ] ; then
  560. config_load dhcp
  561. config_foreach create_interface_dns dhcp
  562. fi
  563. if [ -f "$UNBOUND_DHCP_CONF" ] ; then
  564. {
  565. # Seed DHCP records because dhcp scripts trigger externally
  566. # Incremental Unbound restarts may drop unbound-control add records
  567. echo " include: $UNBOUND_DHCP_CONF"
  568. echo
  569. } >> $UNBOUND_CONFFILE
  570. fi
  571. fi
  572. }
  573. ##############################################################################
  574. unbound_uci() {
  575. local cfg="$1"
  576. local dnsmasqpath hostnm
  577. hostnm="$(uci_get system.@system[0].hostname | awk '{print tolower($0)}')"
  578. UNBOUND_TXT_HOSTNAME=${hostnm:-thisrouter}
  579. config_get_bool UNBOUND_B_SLAAC6_MAC "$cfg" dhcp4_slaac6 0
  580. config_get_bool UNBOUND_B_DNS64 "$cfg" dns64 0
  581. config_get_bool UNBOUND_B_HIDE_BIND "$cfg" hide_binddata 1
  582. config_get_bool UNBOUND_B_LOCL_SERV "$cfg" localservice 1
  583. config_get_bool UNBOUND_B_MAN_CONF "$cfg" manual_conf 0
  584. config_get_bool UNBOUND_B_QUERY_MIN "$cfg" query_minimize 0
  585. config_get_bool UNBOUND_B_QRY_MINST "$cfg" query_min_strict 0
  586. config_get_bool UNBOUND_B_PRIV_BLCK "$cfg" rebind_protection 1
  587. config_get_bool UNBOUND_B_LOCL_BLCK "$cfg" rebind_localhost 0
  588. config_get_bool UNBOUND_B_CONTROL "$cfg" unbound_control 0
  589. config_get_bool UNBOUND_B_DNSSEC "$cfg" validator 0
  590. config_get_bool UNBOUND_B_NTP_BOOT "$cfg" validator_ntp 1
  591. config_get UNBOUND_IP_DNS64 "$cfg" dns64_prefix "64:ff9b::/96"
  592. config_get UNBOUND_N_EDNS_SIZE "$cfg" edns_size 1280
  593. config_get UNBOUND_N_RX_PORT "$cfg" listen_port 53
  594. config_get UNBOUND_N_ROOT_AGE "$cfg" root_age 7
  595. config_get UNBOUND_D_DOMAIN_TYPE "$cfg" domain_type static
  596. config_get UNBOUND_D_DHCP_LINK "$cfg" dhcp_link none
  597. config_get UNBOUND_D_LAN_FQDN "$cfg" add_local_fqdn 0
  598. config_get UNBOUND_D_PROTOCOL "$cfg" protocol mixed
  599. config_get UNBOUND_D_RECURSION "$cfg" recursion passive
  600. config_get UNBOUND_D_RESOURCE "$cfg" resource small
  601. config_get UNBOUND_D_WAN_FQDN "$cfg" add_wan_fqdn 0
  602. config_get UNBOUND_TTL_MIN "$cfg" ttl_min 120
  603. config_get UNBOUND_TXT_DOMAIN "$cfg" domain lan
  604. if [ "$UNBOUND_D_DHCP_LINK" = "none" ] ; then
  605. config_get_bool UNBOUND_B_DNSMASQ "$cfg" dnsmasq_link_dns 0
  606. if [ "$UNBOUND_B_DNSMASQ" -gt 0 ] ; then
  607. UNBOUND_D_DHCP_LINK=dnsmasq
  608. logger -t unbound -s "Please use 'dhcp_link' selector instead"
  609. fi
  610. fi
  611. if [ "$UNBOUND_D_DHCP_LINK" = "dnsmasq" ] ; then
  612. if [ ! -x /usr/sbin/dnsmasq -o ! -x /etc/init.d/dnsmasq ] ; then
  613. UNBOUND_D_DHCP_LINK=none
  614. else
  615. /etc/init.d/dnsmasq enabled || UNBOUND_D_DHCP_LINK=none
  616. fi
  617. if [ "$UNBOUND_D_DHCP_LINK" = "none" ] ; then
  618. logger -t unbound -s "cannot forward to dnsmasq"
  619. fi
  620. fi
  621. if [ "$UNBOUND_D_DHCP_LINK" = "odhcpd" ] ; then
  622. if [ ! -x /usr/sbin/odhcpd -o ! -x /etc/init.d/odhcpd ] ; then
  623. UNBOUND_D_DHCP_LINK=none
  624. else
  625. /etc/init.d/odhcpd enabled || UNBOUND_D_DHCP_LINK=none
  626. fi
  627. if [ "$UNBOUND_D_DHCP_LINK" = "none" ] ; then
  628. logger -t unbound -s "cannot receive records from odhcpd"
  629. fi
  630. fi
  631. if [ "$UNBOUND_N_EDNS_SIZE" -lt 512 \
  632. -o 4096 -lt "$UNBOUND_N_EDNS_SIZE" ] ; then
  633. # exceeds range, back to default
  634. UNBOUND_N_EDNS_SIZE=1280
  635. fi
  636. if [ "$UNBOUND_N_RX_PORT" -lt 1024 \
  637. -o 10240 -lt "$UNBOUND_N_RX_PORT" ] ; then
  638. # special port or in 5 digits, back to default
  639. UNBOUND_N_RX_PORT=53
  640. fi
  641. if [ "$UNBOUND_TTL_MIN" -gt 1800 ] ; then
  642. # that could have had awful side effects
  643. UNBOUND_TTL_MIN=300
  644. fi
  645. }
  646. ##############################################################################
  647. unbound_start() {
  648. config_load unbound
  649. config_foreach unbound_uci unbound
  650. unbound_mkdir
  651. if [ "$UNBOUND_B_MAN_CONF" -eq 0 ] ; then
  652. unbound_conf
  653. unbound_access
  654. unbound_adblock
  655. if [ "$UNBOUND_D_DHCP_LINK" = "dnsmasq" ] ; then
  656. dnsmasq_link
  657. else
  658. unbound_hostname
  659. fi
  660. unbound_control
  661. fi
  662. }
  663. ##############################################################################
  664. unbound_stop() {
  665. local resolvsym=0
  666. rootzone_update
  667. if [ ! -x /usr/sbin/dnsmasq -o ! -x /etc/init.d/dnsmasq ] ; then
  668. resolvsym=1
  669. else
  670. /etc/init.d/dnsmasq enabled || resolvsym=1
  671. fi
  672. if [ "$resolvsym" -gt 0 ] ; then
  673. # set resolver file to normal, but don't stomp on dnsmasq
  674. rm -f /tmp/resolv.conf
  675. ln -s /tmp/resolv.conf.auto /tmp/resolv.conf
  676. fi
  677. # Unbound has a log dump which takes time; don't overlap a "restart"
  678. sleep 1
  679. }
  680. ##############################################################################