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