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