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