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