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.

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