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.

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