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