1
0

unbound.sh 33 KB

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