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