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.

1106 lines
29 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 dhcp_origin=$( uci_get dhcp.@odhcpd[0].leasefile )
  311. local dhcp_dir=$( dirname $dhcp_origin )
  312. local filestuff
  313. if [ "$UNBOUND_D_DHCP_LINK" = "odhcpd" -a ! -d "$dhcp_dir" ] ; then
  314. # make sure odhcpd has a directory to write (not done itself, yet)
  315. mkdir -p "$dhcp_dir"
  316. fi
  317. if [ -f $UNBOUND_KEYFILE ] ; then
  318. filestuff=$( cat $UNBOUND_KEYFILE )
  319. case "$filestuff" in
  320. *"state=2 [ VALID ]"*)
  321. # Lets not lose RFC 5011 tracking if we don't have to
  322. cp -p $UNBOUND_KEYFILE $UNBOUND_KEYFILE.keep
  323. ;;
  324. esac
  325. fi
  326. # Blind copy /etc/ to /var/lib/
  327. mkdir -p $UNBOUND_VARDIR
  328. rm -f $UNBOUND_VARDIR/dhcp_*
  329. touch $UNBOUND_CONFFILE
  330. touch $UNBOUND_SRV_CONF
  331. touch $UNBOUND_EXT_CONF
  332. cp -p /etc/unbound/* $UNBOUND_VARDIR/
  333. if [ ! -f $UNBOUND_HINTFILE ] ; then
  334. if [ -f /usr/share/dns/root.hints ] ; then
  335. # Debian-like package dns-root-data
  336. cp -p /usr/share/dns/root.hints $UNBOUND_HINTFILE
  337. elif [ ! -f "$UNBOUND_TIMEFILE" ] ; then
  338. logger -t unbound -s "iterator will use built-in root hints"
  339. fi
  340. fi
  341. if [ ! -f $UNBOUND_KEYFILE ] ; then
  342. if [ -f /usr/share/dns/root.key ] ; then
  343. # Debian-like package dns-root-data
  344. cp -p /usr/share/dns/root.key $UNBOUND_KEYFILE
  345. elif [ -x $UNBOUND_ANCHOR ] ; then
  346. $UNBOUND_ANCHOR -a $UNBOUND_KEYFILE
  347. elif [ ! -f "$UNBOUND_TIMEFILE" ] ; then
  348. logger -t unbound -s "validator will use built-in trust anchor"
  349. fi
  350. fi
  351. copy_dash_update
  352. # Ensure access and prepare to jail
  353. chown -R unbound:unbound $UNBOUND_VARDIR
  354. chmod 775 $UNBOUND_VARDIR
  355. chmod 664 $UNBOUND_VARDIR/*
  356. }
  357. ##############################################################################
  358. unbound_control() {
  359. if [ "$UNBOUND_B_CONTROL" -gt 0 ] ; then
  360. {
  361. # Enable remote control tool, but only at local host for security
  362. # You can hand write fancier encrypted access with /etc/..._ext.conf
  363. echo "remote-control:"
  364. echo " control-enable: yes"
  365. echo " control-use-cert: no"
  366. echo " control-interface: 127.0.0.1"
  367. echo " control-interface: ::1"
  368. echo
  369. } >> $UNBOUND_CONFFILE
  370. fi
  371. {
  372. # Amend your own extended clauses here like forward zones or disable
  373. # above (local, no encryption) and amend your own remote encrypted control
  374. echo
  375. echo "include: $UNBOUND_EXT_CONF" >> $UNBOUND_CONFFILE
  376. echo
  377. } >> $UNBOUND_CONFFILE
  378. }
  379. ##############################################################################
  380. unbound_conf() {
  381. local cfg="$1"
  382. local rt_mem rt_conn modulestring
  383. {
  384. # Make fresh conf file
  385. echo "# $UNBOUND_CONFFILE generated by UCI $( date )"
  386. echo
  387. } > $UNBOUND_CONFFILE
  388. {
  389. # No threading
  390. echo "server:"
  391. echo " username: unbound"
  392. echo " num-threads: 1"
  393. echo " msg-cache-slabs: 1"
  394. echo " rrset-cache-slabs: 1"
  395. echo " infra-cache-slabs: 1"
  396. echo " key-cache-slabs: 1"
  397. echo
  398. } >> $UNBOUND_CONFFILE
  399. {
  400. # Logging
  401. echo " verbosity: 1"
  402. echo " statistics-interval: 0"
  403. echo " statistics-cumulative: no"
  404. echo " extended-statistics: no"
  405. echo
  406. } >> $UNBOUND_CONFFILE
  407. {
  408. # Interfaces (access contol "option local_service")
  409. echo " interface: 0.0.0.0"
  410. echo " interface: ::0"
  411. echo " outgoing-interface: 0.0.0.0"
  412. echo " outgoing-interface: ::0"
  413. echo
  414. } >> $UNBOUND_CONFFILE
  415. case "$UNBOUND_D_PROTOCOL" in
  416. ip4_only)
  417. {
  418. echo " do-ip4: yes"
  419. echo " do-ip6: no"
  420. } >> $UNBOUND_CONFFILE
  421. ;;
  422. ip6_only)
  423. {
  424. echo " do-ip4: no"
  425. echo " do-ip6: yes"
  426. } >> $UNBOUND_CONFFILE
  427. ;;
  428. ip6_prefer)
  429. {
  430. echo " do-ip4: yes"
  431. echo " do-ip6: yes"
  432. echo " prefer-ip6: yes"
  433. } >> $UNBOUND_CONFFILE
  434. ;;
  435. *)
  436. {
  437. echo " do-ip4: yes"
  438. echo " do-ip6: yes"
  439. } >> $UNBOUND_CONFFILE
  440. ;;
  441. esac
  442. {
  443. # protocol level tuning
  444. echo " edns-buffer-size: $UNBOUND_N_EDNS_SIZE"
  445. echo " msg-buffer-size: 8192"
  446. echo " port: $UNBOUND_N_RX_PORT"
  447. echo " outgoing-port-permit: 10240-65535"
  448. echo
  449. } >> $UNBOUND_CONFFILE
  450. {
  451. # Other harding and options for an embedded router
  452. echo " harden-short-bufsize: yes"
  453. echo " harden-large-queries: yes"
  454. echo " harden-glue: yes"
  455. echo " harden-below-nxdomain: no"
  456. echo " harden-referral-path: no"
  457. echo " use-caps-for-id: no"
  458. echo
  459. } >> $UNBOUND_CONFFILE
  460. {
  461. # Default Files
  462. echo " use-syslog: yes"
  463. echo " chroot: \"$UNBOUND_VARDIR\""
  464. echo " directory: \"$UNBOUND_VARDIR\""
  465. echo " pidfile: \"$UNBOUND_PIDFILE\""
  466. } >> $UNBOUND_CONFFILE
  467. if [ -f "$UNBOUND_HINTFILE" ] ; then
  468. # Optional hints if found
  469. echo " root-hints: \"$UNBOUND_HINTFILE\"" >> $UNBOUND_CONFFILE
  470. fi
  471. if [ "$UNBOUND_B_DNSSEC" -gt 0 -a -f "$UNBOUND_KEYFILE" ] ; then
  472. {
  473. echo " auto-trust-anchor-file: \"$UNBOUND_KEYFILE\""
  474. echo
  475. } >> $UNBOUND_CONFFILE
  476. else
  477. echo >> $UNBOUND_CONFFILE
  478. fi
  479. case "$UNBOUND_D_RESOURCE" in
  480. # Tiny - Unbound's recommended cheap hardware config
  481. tiny) rt_mem=1 ; rt_conn=1 ;;
  482. # Small - Half RRCACHE and open ports
  483. small) rt_mem=8 ; rt_conn=5 ;;
  484. # Medium - Nearly default but with some added balancintg
  485. medium) rt_mem=16 ; rt_conn=10 ;;
  486. # Large - Double medium
  487. large) rt_mem=32 ; rt_conn=10 ;;
  488. # Whatever unbound does
  489. *) rt_mem=0 ; rt_conn=0 ;;
  490. esac
  491. if [ "$rt_mem" -gt 0 ] ; then
  492. {
  493. # Set memory sizing parameters
  494. echo " outgoing-range: $(($rt_conn*64))"
  495. echo " num-queries-per-thread: $(($rt_conn*32))"
  496. echo " outgoing-num-tcp: $(($rt_conn))"
  497. echo " incoming-num-tcp: $(($rt_conn))"
  498. echo " rrset-cache-size: $(($rt_mem*256))k"
  499. echo " msg-cache-size: $(($rt_mem*128))k"
  500. echo " key-cache-size: $(($rt_mem*128))k"
  501. echo " neg-cache-size: $(($rt_mem*64))k"
  502. echo " infra-cache-numhosts: $(($rt_mem*256))"
  503. echo
  504. } >> $UNBOUND_CONFFILE
  505. elif [ ! -f "$UNBOUND_TIMEFILE" ] ; then
  506. logger -t unbound -s "default memory resource consumption"
  507. fi
  508. # Assembly of module-config: options is tricky; order matters
  509. modulestring="iterator"
  510. if [ "$UNBOUND_B_DNSSEC" -gt 0 ] ; then
  511. if [ ! -f "$UNBOUND_TIMEFILE" -a "$UNBOUND_B_NTP_BOOT" -gt 0 ] ; then
  512. # DNSSEC chicken and egg with getting NTP time
  513. echo " val-override-date: -1" >> $UNBOUND_CONFFILE
  514. fi
  515. {
  516. echo " harden-dnssec-stripped: yes"
  517. echo " val-clean-additional: yes"
  518. echo " ignore-cd-flag: yes"
  519. } >> $UNBOUND_CONFFILE
  520. modulestring="validator $modulestring"
  521. fi
  522. if [ "$UNBOUND_B_DNS64" -gt 0 ] ; then
  523. echo " dns64-prefix: $UNBOUND_IP_DNS64" >> $UNBOUND_CONFFILE
  524. modulestring="dns64 $modulestring"
  525. fi
  526. {
  527. # Print final module string
  528. echo " module-config: \"$modulestring\""
  529. echo
  530. } >> $UNBOUND_CONFFILE
  531. if [ "$UNBOUND_B_QRY_MINST" -gt 0 -a "$UNBOUND_B_QUERY_MIN" -gt 0 ] ; then
  532. {
  533. # Some query privacy but "strict" will break some name servers
  534. echo " qname-minimisation: yes"
  535. echo " qname-minimisation-strict: yes"
  536. } >> $UNBOUND_CONFFILE
  537. elif [ "$UNBOUND_B_QUERY_MIN" -gt 0 ] ; then
  538. # Minor improvement on query privacy
  539. echo " qname-minimisation: yes" >> $UNBOUND_CONFFILE
  540. else
  541. echo " qname-minimisation: no" >> $UNBOUND_CONFFILE
  542. fi
  543. case "$UNBOUND_D_RECURSION" in
  544. passive)
  545. {
  546. echo " prefetch: no"
  547. echo " prefetch-key: no"
  548. echo " target-fetch-policy: \"0 0 0 0 0\""
  549. echo
  550. } >> $UNBOUND_CONFFILE
  551. ;;
  552. aggressive)
  553. {
  554. echo " prefetch: yes"
  555. echo " prefetch-key: yes"
  556. echo " target-fetch-policy: \"3 2 1 0 0\""
  557. echo
  558. } >> $UNBOUND_CONFFILE
  559. ;;
  560. *)
  561. if [ ! -f "$UNBOUND_TIMEFILE" ] ; then
  562. logger -t unbound -s "default recursion configuration"
  563. fi
  564. ;;
  565. esac
  566. {
  567. # Reload records more than 10 hours old
  568. # DNSSEC 5 minute bogus cool down before retry
  569. # Adaptive infrastructure info kept for 15 minutes
  570. echo " cache-min-ttl: $UNBOUND_TTL_MIN"
  571. echo " cache-max-ttl: 36000"
  572. echo " val-bogus-ttl: 300"
  573. echo " infra-host-ttl: 900"
  574. echo
  575. } >> $UNBOUND_CONFFILE
  576. if [ "$UNBOUND_B_HIDE_BIND" -gt 0 ] ; then
  577. {
  578. # Block server id and version DNS TXT records
  579. echo " hide-identity: yes"
  580. echo " hide-version: yes"
  581. echo
  582. } >> $UNBOUND_CONFFILE
  583. fi
  584. if [ "$UNBOUND_B_PRIV_BLCK" -gt 0 ] ; then
  585. {
  586. # Remove _upstream_ or global reponses with private addresses.
  587. # Unbounds own "local zone" and "forward zone" may still use these.
  588. # RFC1918, RFC3927, RFC4291, RFC6598, RFC6890
  589. echo " private-address: 10.0.0.0/8"
  590. echo " private-address: 100.64.0.0/10"
  591. echo " private-address: 169.254.0.0/16"
  592. echo " private-address: 172.16.0.0/12"
  593. echo " private-address: 192.168.0.0/16"
  594. echo " private-address: fc00::/8"
  595. echo " private-address: fd00::/8"
  596. echo " private-address: fe80::/10"
  597. } >> $UNBOUND_CONFFILE
  598. fi
  599. if [ "$UNBOUND_B_LOCL_BLCK" -gt 0 ] ; then
  600. {
  601. # Remove DNS reponses from upstream with loopback IP
  602. # Black hole DNS method for ad blocking, so consider...
  603. echo " private-address: 127.0.0.0/8"
  604. echo " private-address: ::1/128"
  605. echo
  606. } >> $UNBOUND_CONFFILE
  607. else
  608. echo >> $UNBOUND_CONFFILE
  609. fi
  610. # Except and accept domains as insecure (DNSSEC); work around broken domains
  611. config_list_foreach "$cfg" "domain_insecure" create_domain_insecure
  612. echo >> $UNBOUND_CONFFILE
  613. }
  614. ##############################################################################
  615. unbound_access() {
  616. # TODO: Unbound 1.6.0 added "tags" and "views", so we can add tags to
  617. # each access-control IP block, and then divert access.
  618. # -- "guest" WIFI will not be allowed to see local zone data
  619. # -- "child" LAN can black whole a list of domains to http~deadpixel
  620. if [ "$UNBOUND_B_LOCL_SERV" -gt 0 ] ; then
  621. # Only respond to queries from which this device has an interface.
  622. # Prevent DNS amplification attacks by not responding to the universe.
  623. config_load network
  624. config_foreach create_access_control interface
  625. {
  626. echo " access-control: 127.0.0.0/8 allow"
  627. echo " access-control: ::1/128 allow"
  628. echo " access-control: fe80::/10 allow"
  629. echo
  630. } >> $UNBOUND_CONFFILE
  631. else
  632. {
  633. echo " access-control: 0.0.0.0/0 allow"
  634. echo " access-control: ::0/0 allow"
  635. echo
  636. } >> $UNBOUND_CONFFILE
  637. fi
  638. {
  639. # Amend your own "server:" stuff here
  640. echo " include: $UNBOUND_SRV_CONF"
  641. echo
  642. } >> $UNBOUND_CONFFILE
  643. }
  644. ##############################################################################
  645. unbound_adblock() {
  646. # TODO: Unbound 1.6.0 added "tags" and "views"; lets work with adblock team
  647. local adb_enabled adb_file
  648. if [ ! -x /usr/bin/adblock.sh -o ! -x /etc/init.d/adblock ] ; then
  649. adb_enabled=0
  650. else
  651. /etc/init.d/adblock enabled && adb_enabled=1 || adb_enabled=0
  652. fi
  653. if [ "$adb_enabled" -gt 0 ] ; then
  654. {
  655. # Pull in your selected openwrt/pacakges/net/adblock generated lists
  656. for adb_file in $UNBOUND_VARDIR/adb_list.* ; do
  657. echo " include: $adb_file"
  658. done
  659. echo
  660. } >> $UNBOUND_CONFFILE
  661. fi
  662. }
  663. ##############################################################################
  664. unbound_hostname() {
  665. if [ -n "$UNBOUND_TXT_DOMAIN" ] ; then
  666. {
  667. # TODO: Unbound 1.6.0 added "tags" and "views" and we could make
  668. # domains by interface to prevent DNS from "guest" to "home"
  669. echo " local-zone: $UNBOUND_TXT_DOMAIN. $UNBOUND_D_DOMAIN_TYPE"
  670. echo " domain-insecure: $UNBOUND_TXT_DOMAIN"
  671. echo " private-domain: $UNBOUND_TXT_DOMAIN"
  672. echo
  673. echo " local-zone: $UNBOUND_TXT_HOSTNAME. $UNBOUND_D_DOMAIN_TYPE"
  674. echo " domain-insecure: $UNBOUND_TXT_HOSTNAME"
  675. echo " private-domain: $UNBOUND_TXT_HOSTNAME"
  676. echo
  677. } >> $UNBOUND_CONFFILE
  678. case "$UNBOUND_D_DOMAIN_TYPE" in
  679. deny|inform_deny|refuse|static)
  680. {
  681. # avoid upstream involvement in RFC6762 like responses (link only)
  682. echo " local-zone: local. $UNBOUND_D_DOMAIN_TYPE"
  683. echo " domain-insecure: local"
  684. echo " private-domain: local"
  685. echo
  686. } >> $UNBOUND_CONFFILE
  687. ;;
  688. esac
  689. if [ "$UNBOUND_D_LAN_FQDN" -gt 0 -o "$UNBOUND_D_WAN_FQDN" -gt 0 ] ; then
  690. config_load dhcp
  691. config_foreach create_interface_dns dhcp
  692. fi
  693. if [ -f "$UNBOUND_DHCP_CONF" ] ; then
  694. {
  695. # Seed DHCP records because dhcp scripts trigger externally
  696. # Incremental Unbound restarts may drop unbound-control add records
  697. echo " include: $UNBOUND_DHCP_CONF"
  698. echo
  699. } >> $UNBOUND_CONFFILE
  700. fi
  701. fi
  702. }
  703. ##############################################################################
  704. unbound_records() {
  705. if [ "$UNBOUND_D_EXTRA_DNS" -gt 0 ] ; then
  706. # Parasite from the uci.dhcp.domain clauses
  707. config_load dhcp
  708. config_foreach create_host_record domain
  709. fi
  710. if [ "$UNBOUND_D_EXTRA_DNS" -gt 1 ] ; then
  711. config_foreach create_srv_record srvhost
  712. config_foreach create_mx_record mxhost
  713. fi
  714. if [ "$UNBOUND_D_EXTRA_DNS" -gt 2 ] ; then
  715. config_foreach create_cname_record cname
  716. fi
  717. echo >> $UNBOUND_CONFFILE
  718. }
  719. ##############################################################################
  720. unbound_uci() {
  721. local cfg="$1"
  722. local dnsmasqpath hostnm
  723. hostnm=$( uci_get system.@system[0].hostname | awk '{print tolower($0)}' )
  724. UNBOUND_TXT_HOSTNAME=${hostnm:-thisrouter}
  725. config_get_bool UNBOUND_B_SLAAC6_MAC "$cfg" dhcp4_slaac6 0
  726. config_get_bool UNBOUND_B_DNS64 "$cfg" dns64 0
  727. config_get_bool UNBOUND_B_HIDE_BIND "$cfg" hide_binddata 1
  728. config_get_bool UNBOUND_B_LOCL_SERV "$cfg" localservice 1
  729. config_get_bool UNBOUND_B_MAN_CONF "$cfg" manual_conf 0
  730. config_get_bool UNBOUND_B_QUERY_MIN "$cfg" query_minimize 0
  731. config_get_bool UNBOUND_B_QRY_MINST "$cfg" query_min_strict 0
  732. config_get_bool UNBOUND_B_PRIV_BLCK "$cfg" rebind_protection 1
  733. config_get_bool UNBOUND_B_LOCL_BLCK "$cfg" rebind_localhost 0
  734. config_get_bool UNBOUND_B_CONTROL "$cfg" unbound_control 0
  735. config_get_bool UNBOUND_B_DNSSEC "$cfg" validator 0
  736. config_get_bool UNBOUND_B_NTP_BOOT "$cfg" validator_ntp 1
  737. config_get UNBOUND_IP_DNS64 "$cfg" dns64_prefix "64:ff9b::/96"
  738. config_get UNBOUND_N_EDNS_SIZE "$cfg" edns_size 1280
  739. config_get UNBOUND_N_RX_PORT "$cfg" listen_port 53
  740. config_get UNBOUND_N_ROOT_AGE "$cfg" root_age 9
  741. config_get UNBOUND_D_DOMAIN_TYPE "$cfg" domain_type static
  742. config_get UNBOUND_D_DHCP_LINK "$cfg" dhcp_link none
  743. config_get UNBOUND_D_EXTRA_DNS "$cfg" add_extra_dns 0
  744. config_get UNBOUND_D_LAN_FQDN "$cfg" add_local_fqdn 0
  745. config_get UNBOUND_D_PROTOCOL "$cfg" protocol mixed
  746. config_get UNBOUND_D_RECURSION "$cfg" recursion passive
  747. config_get UNBOUND_D_RESOURCE "$cfg" resource small
  748. config_get UNBOUND_D_WAN_FQDN "$cfg" add_wan_fqdn 0
  749. config_get UNBOUND_TTL_MIN "$cfg" ttl_min 120
  750. config_get UNBOUND_TXT_DOMAIN "$cfg" domain lan
  751. UNBOUND_LIST_DOMAINS="nowhere $UNBOUND_TXT_DOMAIN"
  752. if [ "$UNBOUND_D_DHCP_LINK" = "none" ] ; then
  753. config_get_bool UNBOUND_B_DNSMASQ "$cfg" dnsmasq_link_dns 0
  754. if [ "$UNBOUND_B_DNSMASQ" -gt 0 ] ; then
  755. UNBOUND_D_DHCP_LINK=dnsmasq
  756. if [ ! -f "$UNBOUND_TIMEFILE" ] ; then
  757. logger -t unbound -s "Please use 'dhcp_link' selector instead"
  758. fi
  759. fi
  760. fi
  761. if [ "$UNBOUND_D_DHCP_LINK" = "dnsmasq" ] ; then
  762. if [ ! -x /usr/sbin/dnsmasq -o ! -x /etc/init.d/dnsmasq ] ; then
  763. UNBOUND_D_DHCP_LINK=none
  764. else
  765. /etc/init.d/dnsmasq enabled || UNBOUND_D_DHCP_LINK=none
  766. fi
  767. if [ "$UNBOUND_D_DHCP_LINK" = "none" -a ! -f "$UNBOUND_TIMEFILE" ] ; then
  768. logger -t unbound -s "cannot forward to dnsmasq"
  769. fi
  770. fi
  771. if [ "$UNBOUND_D_DHCP_LINK" = "odhcpd" ] ; then
  772. if [ ! -x /usr/sbin/odhcpd -o ! -x /etc/init.d/odhcpd ] ; then
  773. UNBOUND_D_DHCP_LINK=none
  774. else
  775. /etc/init.d/odhcpd enabled || UNBOUND_D_DHCP_LINK=none
  776. fi
  777. if [ "$UNBOUND_D_DHCP_LINK" = "none" -a ! -f "$UNBOUND_TIMEFILE" ] ; then
  778. logger -t unbound -s "cannot receive records from odhcpd"
  779. fi
  780. fi
  781. if [ "$UNBOUND_N_EDNS_SIZE" -lt 512 \
  782. -o 4096 -lt "$UNBOUND_N_EDNS_SIZE" ] ; then
  783. logger -t unbound -s "edns_size exceeds range, using default"
  784. UNBOUND_N_EDNS_SIZE=1280
  785. fi
  786. if [ "$UNBOUND_N_RX_PORT" -ne 53 ] \
  787. && [ "$UNBOUND_N_RX_PORT" -lt 1024 -o 10240 -lt "$UNBOUND_N_RX_PORT" ] ; then
  788. logger -t unbound -s "privileged port or in 5 digits, using default"
  789. UNBOUND_N_RX_PORT=53
  790. fi
  791. if [ "$UNBOUND_TTL_MIN" -gt 1800 ] ; then
  792. logger -t unbound -s "ttl_min could have had awful side effects, using 300"
  793. UNBOUND_TTL_MIN=300
  794. fi
  795. }
  796. ##############################################################################
  797. _resolv_setup() {
  798. if [ "$UNBOUND_N_RX_PORT" != "53" ] ; then
  799. return
  800. fi
  801. if [ -x /etc/init.d/dnsmasq ] && /etc/init.d/dnsmasq enabled \
  802. && nslookup localhost 127.0.0.1#53 >/dev/null 2>&1 ; then
  803. # unbound is configured for port 53, but dnsmasq is enabled and a resolver
  804. # listens on localhost:53, lets assume dnsmasq manages the resolver file.
  805. # TODO:
  806. # really check if dnsmasq runs a local (main) resolver in stead of using
  807. # nslookup that times out when no resolver listens on localhost:53.
  808. return
  809. fi
  810. # unbound is designated to listen on 127.0.0.1#53,
  811. # set resolver file to local.
  812. rm -f /tmp/resolv.conf
  813. {
  814. echo "# /tmp/resolv.conf generated by Unbound UCI $( date )"
  815. echo "nameserver 127.0.0.1"
  816. echo "nameserver ::1"
  817. echo "search $UNBOUND_TXT_DOMAIN"
  818. } > /tmp/resolv.conf
  819. }
  820. ##############################################################################
  821. _resolv_teardown() {
  822. case $( cat /tmp/resolv.conf ) in
  823. *"generated by Unbound UCI"*)
  824. # our resolver file, reset to auto resolver file.
  825. rm -f /tmp/resolv.conf
  826. ln -s /tmp/resolv.conf.auto /tmp/resolv.conf
  827. ;;
  828. esac
  829. }
  830. ##############################################################################
  831. unbound_start() {
  832. config_load unbound
  833. config_foreach unbound_uci unbound
  834. unbound_mkdir
  835. if [ "$UNBOUND_B_MAN_CONF" -eq 0 ] ; then
  836. unbound_conf
  837. unbound_access
  838. unbound_adblock
  839. if [ "$UNBOUND_D_DHCP_LINK" = "dnsmasq" ] ; then
  840. dnsmasq_link
  841. else
  842. unbound_hostname
  843. unbound_records
  844. fi
  845. unbound_control
  846. fi
  847. _resolv_setup
  848. }
  849. ##############################################################################
  850. unbound_stop() {
  851. _resolv_teardown
  852. rootzone_update
  853. }
  854. ##############################################################################