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