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