package/dnsmasq: permit dhcp_option in global dnsmasq config section
[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 dhcp_option_add "$cfg" "" 0
147
148 echo >> $CONFIGFILE
149 }
150
151 dhcp_subscrid_add() {
152 local cfg="$1"
153
154 config_get networkid "$cfg" networkid
155 [ -n "$networkid" ] || return 0
156
157 config_get subscriberid "$cfg" subscriberid
158 [ -n "$subscriberid" ] || return 0
159
160 xappend "--dhcp-subscrid=$networkid,$subscriberid"
161
162 config_get_bool force "$cfg" force 0
163
164 dhcp_option_add "$cfg" "$networkid" "$force"
165 }
166
167 dhcp_remoteid_add() {
168 local cfg="$1"
169
170 config_get networkid "$cfg" networkid
171 [ -n "$networkid" ] || return 0
172
173 config_get remoteid "$cfg" remoteid
174 [ -n "$remoteid" ] || return 0
175
176 xappend "--dhcp-remoteid=$networkid,$remoteid"
177
178 config_get_bool force "$cfg" force 0
179
180 dhcp_option_add "$cfg" "$networkid" "$force"
181 }
182
183 dhcp_circuitid_add() {
184 local cfg="$1"
185
186 config_get networkid "$cfg" networkid
187 [ -n "$networkid" ] || return 0
188
189 config_get circuitid "$cfg" circuitid
190 [ -n "$circuitid" ] || return 0
191
192 xappend "--dhcp-circuitid=$networkid,$circuitid"
193
194 config_get_bool force "$cfg" force 0
195
196 dhcp_option_add "$cfg" "$networkid" "$force"
197 }
198
199 dhcp_userclass_add() {
200 local cfg="$1"
201
202 config_get networkid "$cfg" networkid
203 [ -n "$networkid" ] || return 0
204
205 config_get userclass "$cfg" userclass
206 [ -n "$userclass" ] || return 0
207
208 xappend "--dhcp-userclass=$networkid,$userclass"
209
210 config_get_bool force "$cfg" force 0
211
212 dhcp_option_add "$cfg" "$networkid" "$force"
213 }
214
215 dhcp_vendorclass_add() {
216 local cfg="$1"
217
218 config_get networkid "$cfg" networkid
219 [ -n "$networkid" ] || return 0
220
221 config_get vendorclass "$cfg" vendorclass
222 [ -n "$vendorclass" ] || return 0
223
224 xappend "--dhcp-vendorclass=$networkid,$vendorclass"
225
226 config_get_bool force "$cfg" force 0
227
228 dhcp_option_add "$cfg" "$networkid" "$force"
229 }
230
231 dhcp_host_add() {
232 local cfg="$1"
233
234 config_get_bool force "$cfg" force 0
235
236 config_get networkid "$cfg" networkid
237 [ -n "$networkid" ] && dhcp_option_add "$cfg" "$networkid" "$force"
238
239 config_get name "$cfg" name
240 config_get ip "$cfg" ip
241 [ -n "$ip" -o -n "$name" ] || return 0
242
243 macs=""
244 config_get mac "$cfg" mac
245 for m in $mac; do append macs "$m" ","; done
246 [ -n "$macs" ] || return 0
247
248 config_get tag "$cfg" tag
249
250 xappend "--dhcp-host=$macs${networkid:+,net:$networkid}${tag:+,set:$tag}${ip:+,$ip}${name:+,$name}"
251 }
252
253 dhcp_tag_add() {
254 local cfg="$1"
255
256 tag="$cfg"
257
258 [ -n "$tag" ] || return 0
259
260 config_get_bool force "$cfg" force 0
261 [ "$force" = "0" ] && force=
262
263 config_get option "$cfg" dhcp_option
264 for o in $option; do
265 xappend "--dhcp-option${force:+-force}=tag:$tag,$o"
266 done
267 }
268
269 dhcp_mac_add() {
270 local cfg="$1"
271
272 config_get networkid "$cfg" networkid
273 [ -n "$networkid" ] || return 0
274
275 config_get mac "$cfg" mac
276 [ -n "$mac" ] || return 0
277
278 xappend "--dhcp-mac=$networkid,$mac"
279
280 dhcp_option_add "$cfg" "$networkid"
281 }
282
283 dhcp_boot_add() {
284 local cfg="$1"
285
286 config_get networkid "$cfg" networkid
287
288 config_get filename "$cfg" filename
289 [ -n "$filename" ] || return 0
290
291 config_get servername "$cfg" servername
292 [ -n "$servername" ] || return 0
293
294 config_get serveraddress "$cfg" serveraddress
295 [ -n "$serveraddress" ] || return 0
296
297 xappend "--dhcp-boot=${networkid:+net:$networkid,}$filename,$servername,$serveraddress"
298
299 config_get_bool force "$cfg" force 0
300
301 dhcp_option_add "$cfg" "$networkid" "$force"
302 }
303
304
305 dhcp_add() {
306 local cfg="$1"
307 config_get net "$cfg" interface
308 [ -n "$net" ] || return 0
309
310 config_get networkid "$cfg" networkid
311 [ -n "$networkid" ] || networkid="$net"
312
313 config_get ifname "$net" ifname
314 [ -n "$ifname" ] || return 0
315
316 config_get dnsserver "$net" dns
317 [ "$cachelocal" = "0" -a -n "$dnsserver" ] && {
318 DNS_SERVERS="$DNS_SERVERS $dnsserver"
319 }
320
321 append_bool "$cfg" ignore "--no-dhcp-interface=$ifname" && return 0
322
323 config_get proto "$net" proto
324 [ static = "$proto" ] || return 0
325
326 config_get ipaddr "$net" ipaddr
327 config_get netmask "$cfg" netmask
328 [ -n "$netmask" ] || config_get netmask "$net" netmask
329
330 #check for an already active dhcp server on the interface, unless 'force' is set
331 config_get_bool force "$cfg" force 0
332 [ $force -gt 0 ] || {
333 udhcpc -n -q -s /bin/true -t 1 -i $ifname >&- && {
334 logger -t dnsmasq \
335 "found already running DHCP-server on interface '$ifname'" \
336 "refusing to start, use 'option force 1' to override"
337 return 0
338 }
339 }
340
341 config_get start "$cfg" start
342 config_get limit "$cfg" limit
343 config_get leasetime "$cfg" leasetime
344 config_get options "$cfg" options
345 config_get_bool dynamicdhcp "$cfg" dynamicdhcp 1
346
347 leasetime="${leasetime:-12h}"
348 start="$(dhcp_calc "${start:-100}")"
349 limit="${limit:-150}"
350 [ "$limit" -gt 0 ] && limit=$((limit-1))
351 eval "$(ipcalc.sh $ipaddr $netmask $start $limit)"
352 if [ "$dynamicdhcp" = "0" ]; then END="static"; fi
353 xappend "--dhcp-range=$networkid,$START,$END,$NETMASK,$leasetime${options:+ $options}"
354
355 dhcp_option_add "$cfg" "$networkid"
356 }
357
358 dhcp_option_add() {
359 local cfg="$1"
360 local networkid="$2"
361 local force="$3"
362
363 [ "$force" = "0" ] && force=
364
365 config_get dhcp_option "$cfg" dhcp_option
366 for o in $dhcp_option; do
367 xappend "--dhcp-option${force:+-force}=${networkid:+$networkid,}$o"
368 done
369
370 }
371
372 dhcp_domain_add() {
373 local cfg="$1"
374 local ip name names
375
376 config_get names "$cfg" name "$2"
377 [ -n "$names" ] || return 0
378
379 config_get ip "$cfg" ip "$3"
380 [ -n "$ip" ] || return 0
381
382 local oIFS="$IFS"; IFS="."; set -- $ip; IFS="$oIFS"
383 local raddr="${4:+$4.$3.$2.$1.in-addr.arpa}"
384
385 for name in $names; do
386 local fqdn="$name"
387
388 [ "${fqdn%.*}" == "$fqdn" ] && \
389 fqdn="$fqdn${DOMAIN:+.$DOMAIN}"
390
391 xappend "--address=/$fqdn/$ip"
392
393 [ -n "$raddr" ] && {
394 xappend "--ptr-record=$raddr,$fqdn"
395 raddr=""
396 }
397 done
398 }
399
400 dhcp_srv_add() {
401 local cfg="$1"
402
403 config_get srv "$cfg" srv
404 [ -n "$srv" ] || return 0
405
406 config_get target "$cfg" target
407 [ -n "$target" ] || return 0
408
409 config_get port "$cfg" port
410 [ -n "$port" ] || return 0
411
412 config_get class "$cfg" class
413 config_get weight "$cfg" weight
414
415 local service="$srv,$target,$port${class:+,$class${weight:+,$weight}}"
416
417 xappend "--srv-host=$service"
418 }
419
420 dhcp_mx_add() {
421 local cfg="$1"
422 local domain relay pref
423
424 config_get domain "$cfg" domain
425 [ -n "$domain" ] || return 0
426
427 config_get relay "$cfg" relay
428 [ -n "$relay" ] || return 0
429
430 config_get pref "$cfg" pref 0
431
432 local service="$domain,$relay,$pref"
433
434 xappend "--mx-host=$service"
435 }
436
437 dhcp_cname_add() {
438 local cfg="$1"
439 local cname target
440
441 config_get cname "$cfg" cname
442 [ -n "$cname" ] || return 0
443
444 config_get target "$cfg" target
445 [ -n "$target" ] || return 0
446
447 xappend "--cname=${cname},${target}"
448 }
449
450 start() {
451 include /lib/network
452 scan_interfaces
453
454 local lanaddr
455 config_get lanaddr "lan" ipaddr
456
457 config_load dhcp
458
459 # before we can call xappend
460 mkdir -p $(dirname $CONFIGFILE)
461
462 echo "# auto-generated config file from /etc/config/dhcp" > $CONFIGFILE
463
464 # if we did this last, we could override auto-generated config
465 [ -f /etc/dnsmasq.conf ] && {
466 xappend "--conf-file=/etc/dnsmasq.conf"
467 }
468
469 args=""
470 config_foreach dnsmasq dnsmasq
471 config_foreach dhcp_host_add host
472 echo >> $CONFIGFILE
473 config_foreach dhcp_boot_add boot
474 config_foreach dhcp_mac_add mac
475 config_foreach dhcp_tag_add tag
476 config_foreach dhcp_vendorclass_add vendorclass
477 config_foreach dhcp_userclass_add userclass
478 config_foreach dhcp_circuitid_add circuitid
479 config_foreach dhcp_remoteid_add remoteid
480 config_foreach dhcp_subscrid_add subscrid
481 config_foreach dhcp_domain_add domain
482 echo >> $CONFIGFILE
483 config_foreach dhcp_srv_add srvhost
484 config_foreach dhcp_mx_add mxhost
485 echo >> $CONFIGFILE
486 config_foreach dhcp_add dhcp
487 echo >> $CONFIGFILE
488 config_foreach dhcp_cname_add cname
489 echo >> $CONFIGFILE
490
491 # add own hostname
492 [ -z "$lanaddr" ] || {
493 local hostname="$(uci_get system.@system[0].hostname)"
494 dhcp_domain_add "" "${hostname:-OpenWrt}" "$lanaddr"
495 }
496
497 service_start /usr/sbin/dnsmasq -C $CONFIGFILE && {
498 rm -f /tmp/resolv.conf
499 [ -n "$DOMAIN" ] && echo "search $DOMAIN" >> /tmp/resolv.conf
500 DNS_SERVERS="$DNS_SERVERS 127.0.0.1"
501 for DNS_SERVER in $DNS_SERVERS ; do
502 echo "nameserver $DNS_SERVER" >> /tmp/resolv.conf
503 done
504 }
505 }
506
507 stop() {
508 service_stop /usr/sbin/dnsmasq && {
509 [ -f /tmp/resolv.conf ] && {
510 rm -f /tmp/resolv.conf
511 ln -s /tmp/resolv.conf.auto /tmp/resolv.conf
512 }
513 }
514 }