dnsmasq: backport --tftp-no-fail to ignore missing tftp root
[openwrt/svn-archive/archive.git] / package / network / services / dnsmasq / files / dnsmasq.init
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2007-2012 OpenWrt.org
3
4 START=60
5
6 USE_PROCD=1
7 PROG=/usr/sbin/dnsmasq
8
9 DNS_SERVERS=""
10 DOMAIN=""
11
12 ADD_LOCAL_DOMAIN=1
13 ADD_LOCAL_HOSTNAME=1
14
15 CONFIGFILE="/var/etc/dnsmasq.conf"
16 HOSTFILE="/tmp/hosts/dhcp"
17 TRUSTANCHORSFILE="/usr/share/dnsmasq/trust-anchors.conf"
18
19 xappend() {
20 local value="$1"
21
22 echo "${value#--}" >> $CONFIGFILE
23 }
24
25 dhcp_calc() {
26 local ip="$1"
27 local res=0
28
29 while [ -n "$ip" ]; do
30 part="${ip%%.*}"
31 res="$(($res * 256))"
32 res="$(($res + $part))"
33 [ "${ip%.*}" != "$ip" ] && ip="${ip#*.}" || ip=
34 done
35 echo "$res"
36 }
37
38 dhcp_check() {
39 local ifname="$1"
40 local stamp="/var/run/dnsmasq.$ifname.dhcp"
41 local rv=0
42
43 [ -s "$stamp" ] && return $(cat "$stamp")
44
45 udhcpc -n -q -s /bin/true -t 1 -i "$ifname" >&- && rv=1 || rv=0
46
47 [ $rv -eq 1 ] && \
48 logger -t dnsmasq \
49 "found already running DHCP-server on interface '$ifname'" \
50 "refusing to start, use 'option force 1' to override"
51
52 echo $rv > "$stamp"
53 return $rv
54 }
55
56 log_once() {
57 pidof dnsmasq >/dev/null || \
58 logger -t dnsmasq "$@"
59 }
60
61 append_bool() {
62 local section="$1"
63 local option="$2"
64 local value="$3"
65 local _loctmp
66 config_get_bool _loctmp "$section" "$option" 0
67 [ $_loctmp -gt 0 ] && xappend "$value"
68 }
69
70 append_parm() {
71 local section="$1"
72 local option="$2"
73 local switch="$3"
74 local _loctmp
75 config_get _loctmp "$section" "$option"
76 [ -z "$_loctmp" ] && return 0
77 xappend "$switch=$_loctmp"
78 }
79
80 append_server() {
81 xappend "--server=$1"
82 }
83
84 append_address() {
85 xappend "--address=$1"
86 }
87
88 append_ipset() {
89 xappend "--ipset=$1"
90 }
91
92 append_interface() {
93 local ifname=$(uci_get_state network "$1" ifname "$1")
94 xappend "--interface=$ifname"
95 }
96
97 append_notinterface() {
98 local ifname=$(uci_get_state network "$1" ifname "$1")
99 xappend "--except-interface=$ifname"
100 }
101
102 append_addnhosts() {
103 xappend "--addn-hosts=$1"
104 }
105
106 append_bogusnxdomain() {
107 xappend "--bogus-nxdomain=$1"
108 }
109
110 append_pxe_service() {
111 xappend "--pxe-service=$1"
112 }
113
114 dnsmasq() {
115 local cfg="$1"
116 append_bool "$cfg" authoritative "--dhcp-authoritative"
117 append_bool "$cfg" nodaemon "--no-daemon"
118 append_bool "$cfg" domainneeded "--domain-needed"
119 append_bool "$cfg" filterwin2k "--filterwin2k"
120 append_bool "$cfg" nohosts "--no-hosts"
121 append_bool "$cfg" nonegcache "--no-negcache"
122 append_bool "$cfg" strictorder "--strict-order"
123 append_bool "$cfg" logqueries "--log-queries"
124 append_bool "$cfg" noresolv "--no-resolv"
125 append_bool "$cfg" localise_queries "--localise-queries"
126 append_bool "$cfg" readethers "--read-ethers"
127 append_bool "$cfg" dbus "--enable-dbus"
128 append_bool "$cfg" boguspriv "--bogus-priv"
129 append_bool "$cfg" expandhosts "--expand-hosts"
130 append_bool "$cfg" enable_tftp "--enable-tftp"
131 append_bool "$cfg" tftp_no_fail "--tftp-no-fail"
132 append_bool "$cfg" nonwildcard "--bind-interfaces"
133 append_bool "$cfg" fqdn "--dhcp-fqdn"
134 append_bool "$cfg" proxydnssec "--proxy-dnssec"
135 append_bool "$cfg" localservice "--local-service"
136 append_bool "$cfg" quietdhcp "--quiet-dhcp"
137
138 append_parm "$cfg" dhcpscript "--dhcp-script"
139 append_parm "$cfg" cachesize "--cache-size"
140 append_parm "$cfg" dnsforwardmax "--dns-forward-max"
141 append_parm "$cfg" port "--port"
142 append_parm "$cfg" ednspacket_max "--edns-packet-max"
143 append_parm "$cfg" dhcpleasemax "--dhcp-lease-max"
144 append_parm "$cfg" "queryport" "--query-port"
145 append_parm "$cfg" "domain" "--domain"
146 append_parm "$cfg" "local" "--server"
147 config_list_foreach "$cfg" "server" append_server
148 config_list_foreach "$cfg" "address" append_address
149 config_list_foreach "$cfg" "ipset" append_ipset
150 config_list_foreach "$cfg" "interface" append_interface
151 config_list_foreach "$cfg" "notinterface" append_notinterface
152 config_list_foreach "$cfg" "addnhosts" append_addnhosts
153 config_list_foreach "$cfg" "bogusnxdomain" append_bogusnxdomain
154 append_parm "$cfg" "leasefile" "--dhcp-leasefile"
155 append_parm "$cfg" "resolvfile" "--resolv-file"
156 append_parm "$cfg" "tftp_root" "--tftp-root"
157 append_parm "$cfg" "dhcp_boot" "--dhcp-boot"
158 append_parm "$cfg" "local_ttl" "--local-ttl"
159 append_parm "$cfg" "pxe_prompt" "--pxe-prompt"
160 config_list_foreach "$cfg" "pxe_service" append_pxe_service
161 config_get DOMAIN "$cfg" domain
162
163 config_get_bool ADD_LOCAL_DOMAIN "$cfg" add_local_domain 1
164 config_get_bool ADD_LOCAL_HOSTNAME "$cfg" add_local_hostname 1
165
166 config_get_bool readethers "$cfg" readethers
167 [ "$readethers" = "1" -a \! -e "/etc/ethers" ] && touch /etc/ethers
168
169 config_get leasefile $cfg leasefile
170 [ -n "$leasefile" -a \! -e "$leasefile" ] && touch "$leasefile"
171 config_get_bool cachelocal "$cfg" cachelocal 1
172
173 config_get hostsfile "$cfg" dhcphostsfile
174 [ -e "$hostsfile" ] && xappend "--dhcp-hostsfile=$hostsfile"
175
176 mkdir -p /tmp/hosts /tmp/dnsmasq.d
177 xappend "--addn-hosts=/tmp/hosts"
178 xappend "--conf-dir=/tmp/dnsmasq.d"
179
180 local rebind
181 config_get_bool rebind "$cfg" rebind_protection 1
182 [ $rebind -gt 0 ] && {
183 log_once \
184 "DNS rebinding protection is active," \
185 "will discard upstream RFC1918 responses!"
186 xappend "--stop-dns-rebind"
187
188 local rebind_localhost
189 config_get_bool rebind_localhost "$cfg" rebind_localhost 0
190 [ $rebind_localhost -gt 0 ] && {
191 log_once "Allowing 127.0.0.0/8 responses"
192 xappend "--rebind-localhost-ok"
193 }
194
195 append_rebind_domain() {
196 log_once "Allowing RFC1918 responses for domain $1"
197 xappend "--rebind-domain-ok=$1"
198 }
199
200 config_list_foreach "$cfg" rebind_domain append_rebind_domain
201 }
202
203 config_get_bool dnssec "$cfg" dnssec 0
204 [ "$dnssec" -gt 0 ] && {
205 xappend "--conf-file=$TRUSTANCHORSFILE"
206 xappend "--dnssec"
207 append_bool "$cfg" dnsseccheckunsigned "--dnssec-check-unsigned"
208 }
209
210 dhcp_option_add "$cfg" "" 0
211
212 xappend "--dhcp-broadcast=tag:needs-broadcast"
213
214 echo >> $CONFIGFILE
215 }
216
217 dhcp_subscrid_add() {
218 local cfg="$1"
219
220 config_get networkid "$cfg" networkid
221 [ -n "$networkid" ] || return 0
222
223 config_get subscriberid "$cfg" subscriberid
224 [ -n "$subscriberid" ] || return 0
225
226 xappend "--dhcp-subscrid=$networkid,$subscriberid"
227
228 config_get_bool force "$cfg" force 0
229
230 dhcp_option_add "$cfg" "$networkid" "$force"
231 }
232
233 dhcp_remoteid_add() {
234 local cfg="$1"
235
236 config_get networkid "$cfg" networkid
237 [ -n "$networkid" ] || return 0
238
239 config_get remoteid "$cfg" remoteid
240 [ -n "$remoteid" ] || return 0
241
242 xappend "--dhcp-remoteid=$networkid,$remoteid"
243
244 config_get_bool force "$cfg" force 0
245
246 dhcp_option_add "$cfg" "$networkid" "$force"
247 }
248
249 dhcp_circuitid_add() {
250 local cfg="$1"
251
252 config_get networkid "$cfg" networkid
253 [ -n "$networkid" ] || return 0
254
255 config_get circuitid "$cfg" circuitid
256 [ -n "$circuitid" ] || return 0
257
258 xappend "--dhcp-circuitid=$networkid,$circuitid"
259
260 config_get_bool force "$cfg" force 0
261
262 dhcp_option_add "$cfg" "$networkid" "$force"
263 }
264
265 dhcp_userclass_add() {
266 local cfg="$1"
267
268 config_get networkid "$cfg" networkid
269 [ -n "$networkid" ] || return 0
270
271 config_get userclass "$cfg" userclass
272 [ -n "$userclass" ] || return 0
273
274 xappend "--dhcp-userclass=$networkid,$userclass"
275
276 config_get_bool force "$cfg" force 0
277
278 dhcp_option_add "$cfg" "$networkid" "$force"
279 }
280
281 dhcp_vendorclass_add() {
282 local cfg="$1"
283
284 config_get networkid "$cfg" networkid
285 [ -n "$networkid" ] || return 0
286
287 config_get vendorclass "$cfg" vendorclass
288 [ -n "$vendorclass" ] || return 0
289
290 xappend "--dhcp-vendorclass=$networkid,$vendorclass"
291
292 config_get_bool force "$cfg" force 0
293
294 dhcp_option_add "$cfg" "$networkid" "$force"
295 }
296
297 dhcp_host_add() {
298 local cfg="$1"
299
300 config_get_bool force "$cfg" force 0
301
302 config_get networkid "$cfg" networkid
303 [ -n "$networkid" ] && dhcp_option_add "$cfg" "$networkid" "$force"
304
305 config_get name "$cfg" name
306 config_get ip "$cfg" ip
307 [ -n "$ip" -o -n "$name" ] || return 0
308
309 config_get_bool dns "$cfg" dns 0
310 [ "$dns" = "1" -a -n "$ip" -a -n "$name" ] && {
311 echo "$ip $name${DOMAIN:+.$DOMAIN}" >> $HOSTFILE
312 }
313
314 config_get mac "$cfg" mac
315 if [ -n "$mac" ]; then
316 # --dhcp-host=00:20:e0:3b:13:af,192.168.0.199,lap
317 macs=""
318 for m in $mac; do append macs "$m" ","; done
319 else
320 # --dhcp-host=lap,192.168.0.199
321 [ -n "$name" ] || return 0
322 macs="$name"
323 name=""
324 fi
325
326 config_get tag "$cfg" tag
327
328 config_get_bool broadcast "$cfg" broadcast 0
329 [ "$broadcast" = "0" ] && broadcast=
330
331 xappend "--dhcp-host=$macs${networkid:+,net:$networkid}${broadcast:+,set:needs-broadcast}${tag:+,set:$tag}${ip:+,$ip}${name:+,$name}"
332 }
333
334 dhcp_tag_add() {
335 local cfg="$1"
336
337 tag="$cfg"
338
339 [ -n "$tag" ] || return 0
340
341 config_get_bool force "$cfg" force 0
342 [ "$force" = "0" ] && force=
343
344 config_get option "$cfg" dhcp_option
345 for o in $option; do
346 xappend "--dhcp-option${force:+-force}=tag:$tag,$o"
347 done
348 }
349
350 dhcp_mac_add() {
351 local cfg="$1"
352
353 config_get networkid "$cfg" networkid
354 [ -n "$networkid" ] || return 0
355
356 config_get mac "$cfg" mac
357 [ -n "$mac" ] || return 0
358
359 xappend "--dhcp-mac=$networkid,$mac"
360
361 dhcp_option_add "$cfg" "$networkid"
362 }
363
364 dhcp_boot_add() {
365 local cfg="$1"
366
367 config_get networkid "$cfg" networkid
368
369 config_get filename "$cfg" filename
370 [ -n "$filename" ] || return 0
371
372 config_get servername "$cfg" servername
373 config_get serveraddress "$cfg" serveraddress
374
375 [ -n "$serveraddress" -a ! -n "$servername" ] && return 0
376
377 xappend "--dhcp-boot=${networkid:+net:$networkid,}${filename}${servername:+,$servername}${serveraddress:+,$serveraddress}"
378
379 config_get_bool force "$cfg" force 0
380
381 dhcp_option_add "$cfg" "$networkid" "$force"
382 }
383
384
385 dhcp_add() {
386 local cfg="$1"
387 config_get net "$cfg" interface
388 [ -n "$net" ] || return 0
389
390 config_get dhcpv4 "$cfg" dhcpv4
391 [ "$dhcpv4" != "disabled" ] || return 0
392
393 config_get networkid "$cfg" networkid
394 [ -n "$networkid" ] || networkid="$net"
395
396 network_get_subnet subnet "$net" || return 0
397 network_get_device ifname "$net" || return 0
398 network_get_protocol proto "$net" || return 0
399
400 [ "$cachelocal" = "0" ] && network_get_dnsserver dnsserver "$net" && {
401 DNS_SERVERS="$DNS_SERVERS $dnsserver"
402 }
403
404 append_bool "$cfg" ignore "--no-dhcp-interface=$ifname" && return 0
405
406 # Do not support non-static interfaces for now
407 [ static = "$proto" ] || return 0
408
409 # Override interface netmask with dhcp config if applicable
410 config_get netmask "$cfg" netmask "${subnet##*/}"
411
412 #check for an already active dhcp server on the interface, unless 'force' is set
413 config_get_bool force "$cfg" force 0
414 [ $force -gt 0 ] || dhcp_check "$ifname" || return 0
415
416 config_get start "$cfg" start
417 config_get limit "$cfg" limit
418 config_get leasetime "$cfg" leasetime
419 config_get options "$cfg" options
420 config_get_bool dynamicdhcp "$cfg" dynamicdhcp 1
421
422 leasetime="${leasetime:-12h}"
423 start="$(dhcp_calc "${start:-100}")"
424 limit="${limit:-150}"
425 [ "$limit" -gt 0 ] && limit=$((limit-1))
426 eval "$(ipcalc.sh "${subnet%%/*}" $netmask $start $limit)"
427 if [ "$dynamicdhcp" = "0" ]; then END="static"; fi
428 xappend "--dhcp-range=$networkid,$START,$END,$NETMASK,$leasetime${options:+ $options}"
429
430 dhcp_option_add "$cfg" "$networkid"
431 }
432
433 dhcp_option_add() {
434 local cfg="$1"
435 local networkid="$2"
436 local force="$3"
437
438 [ "$force" = "0" ] && force=
439
440 config_get dhcp_option "$cfg" dhcp_option
441 for o in $dhcp_option; do
442 xappend "--dhcp-option${force:+-force}=${networkid:+$networkid,}$o"
443 done
444
445 }
446
447 dhcp_domain_add() {
448 local cfg="$1"
449 local ip name names record
450
451 config_get names "$cfg" name "$2"
452 [ -n "$names" ] || return 0
453
454 config_get ip "$cfg" ip "$3"
455 [ -n "$ip" ] || return 0
456
457 for name in $names; do
458 record="${record:+$record }$name"
459 done
460
461 echo "$ip $record" >> $HOSTFILE
462 }
463
464 dhcp_srv_add() {
465 local cfg="$1"
466
467 config_get srv "$cfg" srv
468 [ -n "$srv" ] || return 0
469
470 config_get target "$cfg" target
471 [ -n "$target" ] || return 0
472
473 config_get port "$cfg" port
474 [ -n "$port" ] || return 0
475
476 config_get class "$cfg" class
477 config_get weight "$cfg" weight
478
479 local service="$srv,$target,$port${class:+,$class${weight:+,$weight}}"
480
481 xappend "--srv-host=$service"
482 }
483
484 dhcp_mx_add() {
485 local cfg="$1"
486 local domain relay pref
487
488 config_get domain "$cfg" domain
489 [ -n "$domain" ] || return 0
490
491 config_get relay "$cfg" relay
492 [ -n "$relay" ] || return 0
493
494 config_get pref "$cfg" pref 0
495
496 local service="$domain,$relay,$pref"
497
498 xappend "--mx-host=$service"
499 }
500
501 dhcp_cname_add() {
502 local cfg="$1"
503 local cname target
504
505 config_get cname "$cfg" cname
506 [ -n "$cname" ] || return 0
507
508 config_get target "$cfg" target
509 [ -n "$target" ] || return 0
510
511 xappend "--cname=${cname},${target}"
512 }
513
514 dhcp_hostrecord_add() {
515 local cfg="$1"
516 local names addresses record val
517
518 config_get names "$cfg" name "$2"
519 if [ -z "$names" ]; then
520 return 0
521 fi
522
523 config_get addresses "$cfg" ip "$3"
524 if [ -z "$addresses" ]; then
525 return 0
526 fi
527
528 for val in $names $addresses; do
529 record="${record:+$record,}$val"
530 done
531
532 xappend "--host-record=$record"
533 }
534
535 service_triggers()
536 {
537 procd_add_reload_trigger "dhcp"
538 }
539
540 boot() {
541 # Will be launched through hotplug
542 return 0
543 }
544
545 start_service() {
546 include /lib/functions
547
548 config_load dhcp
549
550 procd_open_instance
551 procd_set_param command $PROG -C $CONFIGFILE -k -x /var/run/dnsmasq/dnsmasq.pid
552 procd_set_param file $CONFIGFILE
553 procd_set_param respawn
554
555 procd_add_jail dnsmasq ubus log
556 procd_add_jail_mount $CONFIGFILE $TRUSTANCHORSFILE $HOSTFILE /etc/passwd /dev/urandom /etc/dnsmasq.conf /tmp/dnsmasq.d /tmp/resolv.conf.auto /etc/hosts /etc/ethers
557 procd_add_jail_mount_rw /var/run/dnsmasq/ /tmp/dhcp.leases
558
559 procd_close_instance
560
561 # before we can call xappend
562 mkdir -p /var/run/dnsmasq/
563 mkdir -p $(dirname $CONFIGFILE)
564 mkdir -p /var/lib/misc
565 touch /tmp/dhcp.leases
566
567
568 echo "# auto-generated config file from /etc/config/dhcp" > $CONFIGFILE
569 echo "# auto-generated config file from /etc/config/dhcp" > $HOSTFILE
570
571 # if we did this last, we could override auto-generated config
572 [ -f /etc/dnsmasq.conf ] && {
573 xappend "--conf-file=/etc/dnsmasq.conf"
574 }
575
576 args=""
577 config_foreach dnsmasq dnsmasq
578 config_foreach dhcp_host_add host
579 echo >> $CONFIGFILE
580 config_foreach dhcp_boot_add boot
581 config_foreach dhcp_mac_add mac
582 config_foreach dhcp_tag_add tag
583 config_foreach dhcp_vendorclass_add vendorclass
584 config_foreach dhcp_userclass_add userclass
585 config_foreach dhcp_circuitid_add circuitid
586 config_foreach dhcp_remoteid_add remoteid
587 config_foreach dhcp_subscrid_add subscrid
588 config_foreach dhcp_domain_add domain
589 config_foreach dhcp_hostrecord_add hostrecord
590
591 # add own hostname
592 local lanaddr
593 [ $ADD_LOCAL_HOSTNAME -eq 1 ] && network_get_ipaddr lanaddr "lan" && {
594 local hostname="$(uci_get system @system[0] hostname OpenWrt)"
595 dhcp_domain_add "" "$hostname" "$lanaddr"
596 }
597
598 echo >> $CONFIGFILE
599 config_foreach dhcp_srv_add srvhost
600 config_foreach dhcp_mx_add mxhost
601 echo >> $CONFIGFILE
602
603 config_get odhcpd_is_active odhcpd maindhcp
604 if [ "$odhcpd_is_active" != "1" ]; then
605 config_foreach dhcp_add dhcp
606 fi
607
608 echo >> $CONFIGFILE
609 config_foreach dhcp_cname_add cname
610 echo >> $CONFIGFILE
611
612 rm -f /tmp/resolv.conf
613 [ $ADD_LOCAL_DOMAIN -eq 1 ] && [ -n "$DOMAIN" ] && {
614 echo "search $DOMAIN" >> /tmp/resolv.conf
615 }
616 DNS_SERVERS="$DNS_SERVERS 127.0.0.1"
617 for DNS_SERVER in $DNS_SERVERS ; do
618 echo "nameserver $DNS_SERVER" >> /tmp/resolv.conf
619 done
620 }
621
622 reload_service() {
623 rc_procd start_service "$@"
624 return 0
625 }
626
627 stop_service() {
628 [ -f /tmp/resolv.conf ] && {
629 rm -f /tmp/resolv.conf
630 ln -s /tmp/resolv.conf.auto /tmp/resolv.conf
631 }
632 rm -f /var/run/dnsmasq.*.dhcp
633 }