base-files: remove a stale reference to /usr/lib/common.awk (thx, swalker)
[openwrt.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" dhcpscript "-6"
82 append_parm "$cfg" cachesize "-c"
83 append_parm "$cfg" dnsforwardmax "-0"
84 append_parm "$cfg" port "-p"
85 append_parm "$cfg" ednspacket_max "-P"
86 append_parm "$cfg" dhcpleasemax "-X"
87 append_parm "$cfg" "queryport" "-Q"
88 append_parm "$cfg" "domain" "-s"
89 append_parm "$cfg" "local" "-S"
90 config_list_foreach "$cfg" "server" append_server
91 config_list_foreach "$cfg" "interface" append_interface
92 config_list_foreach "$cfg" "notinterface" append_notinterface
93 config_list_foreach "$cfg" "addnhosts" append_addnhosts
94 config_list_foreach "$cfg" "bogusnxdomain" append_bogusnxdomain
95 append_parm "$cfg" "leasefile" "-l"
96 append_parm "$cfg" "resolvfile" "-r"
97 append_parm "$cfg" "tftp_root" "--tftp-root"
98 append_parm "$cfg" "dhcp_boot" "--dhcp-boot"
99
100 config_get DOMAIN "$cfg" domain
101
102 config_get_bool readethers "$cfg" readethers
103 [ "$readethers" = "1" ] && [ -e "/etc/ethers" ] || touch /etc/ethers
104
105 config_get leasefile $cfg leasefile
106 [ -n "$leasefile" ] && [ -e "$leasefile" ] || touch "$leasefile"
107 config_get_bool cachelocal "$cfg" cachelocal 1
108
109 config_get hostsfile "$cfg" dhcphostsfile
110 [ -e "$hostsfile" ] && append args "--dhcp-hostsfile=$hostsfile"
111
112 local rebind
113 config_get_bool rebind "$cfg" rebind_protection 1
114 [ $rebind -gt 0 ] && {
115 logger -t dnsmasq \
116 "DNS rebinding protection is active," \
117 "will discard upstream RFC1918 responses!"
118 append args "--stop-dns-rebind"
119
120 local rebind_localhost
121 config_get_bool rebind_localhost "$cfg" rebind_localhost 0
122 [ $rebind_localhost -gt 0 ] && {
123 logger -t dnsmasq "Allowing 127.0.0.0/8 responses"
124 append args "--rebind-localhost-ok"
125 }
126
127 append_rebind_domain() {
128 logger -t dnsmasq "Allowing RFC1918 responses for domain $1"
129 append args "--rebind-domain-ok=$1"
130 }
131
132 config_list_foreach "$cfg" rebind_domain append_rebind_domain
133 }
134 }
135
136 dhcp_subscrid_add() {
137 local cfg="$1"
138
139 config_get networkid "$cfg" networkid
140 [ -n "$networkid" ] || return 0
141
142 config_get subscriberid "$cfg" subscriberid
143 [ -n "$subscriberid" ] || return 0
144
145 append args "--dhcp-subscrid=$networkid,$subscriberid"
146
147 dhcp_option_add "$cfg" "$networkid"
148 }
149
150 dhcp_remoteid_add() {
151 local cfg="$1"
152
153 config_get networkid "$cfg" networkid
154 [ -n "$networkid" ] || return 0
155
156 config_get remoteid "$cfg" remoteid
157 [ -n "$remoteid" ] || return 0
158
159 append args "--dhcp-remoteid=$networkid,$remoteid"
160
161 dhcp_option_add "$cfg" "$networkid"
162 }
163
164 dhcp_circuitid_add() {
165 local cfg="$1"
166
167 config_get networkid "$cfg" networkid
168 [ -n "$networkid" ] || return 0
169
170 config_get circuitid "$cfg" circuitid
171 [ -n "$circuitid" ] || return 0
172
173 append args "--dhcp-circuitid=$networkid,$circuitid"
174
175 dhcp_option_add "$cfg" "$networkid"
176 }
177
178 dhcp_userclass_add() {
179 local cfg="$1"
180
181 config_get networkid "$cfg" networkid
182 [ -n "$networkid" ] || return 0
183
184 config_get userclass "$cfg" userclass
185 [ -n "$userclass" ] || return 0
186
187 append args "--dhcp-userclass=$networkid,$userclass"
188
189 dhcp_option_add "$cfg" "$networkid"
190 }
191
192 dhcp_vendorclass_add() {
193 local cfg="$1"
194
195 config_get networkid "$cfg" networkid
196 [ -n "$networkid" ] || return 0
197
198 config_get vendorclass "$cfg" vendorclass
199 [ -n "$vendorclass" ] || return 0
200
201 append args "--dhcp-vendorclass=$networkid,$vendorclass"
202
203 dhcp_option_add "$cfg" "$networkid"
204 }
205
206 dhcp_host_add() {
207 local cfg="$1"
208
209 config_get name "$cfg" name
210
211 config_get networkid "$cfg" networkid
212 [ -n "$networkid" ] && dhcp_option_add "$cfg" "$networkid"
213
214 config_get ip "$cfg" ip
215 [ -n "$ip" ] || return 0
216
217 macs=""
218 config_get mac "$cfg" mac
219 for m in $mac; do append macs "$m" ","; done
220 [ -n "$macs" ] || return 0
221
222 append args "--dhcp-host=$macs,${networkid:+net:$networkid,}$ip${name:+,$name}"
223 }
224
225 dhcp_mac_add() {
226 local cfg="$1"
227
228 config_get networkid "$cfg" networkid
229 [ -n "$networkid" ] || return 0
230
231 config_get mac "$cfg" mac
232 [ -n "$mac" ] || return 0
233
234 append args "--dhcp-mac=$networkid,$mac"
235
236 dhcp_option_add "$cfg" "$networkid"
237 }
238
239 dhcp_boot_add() {
240 local cfg="$1"
241
242 config_get networkid "$cfg" networkid
243
244 config_get filename "$cfg" filename
245 [ -n "$filename" ] || return 0
246
247 config_get servername "$cfg" servername
248 [ -n "$servername" ] || return 0
249
250 config_get serveraddress "$cfg" serveraddress
251 [ -n "$serveraddress" ] || return 0
252
253 append args "--dhcp-boot=${networkid:+net:$networkid,}$filename,$servername,$serveraddress"
254
255 dhcp_option_add "$cfg" "$networkid"
256 }
257
258
259 dhcp_add() {
260 local cfg="$1"
261 config_get net "$cfg" interface
262 [ -n "$net" ] || return 0
263
264 config_get networkid "$cfg" networkid
265 [ -n "$networkid" ] || networkid="$net"
266
267 config_get ifname "$net" ifname
268 [ -n "$ifname" ] || return 0
269
270 config_get dnsserver "$net" dns
271 [ "$cachelocal" = "0" -a -n "$dnsserver" ] && {
272 DNS_SERVERS="$DNS_SERVERS $dnsserver"
273 }
274
275 append_bool "$cfg" ignore "-2 $ifname" && return 0
276
277 config_get proto "$net" proto
278 [ static = "$proto" ] || return 0
279
280 config_get ipaddr "$net" ipaddr
281 config_get netmask "$cfg" netmask
282 [ -n "$netmask" ] || config_get netmask "$net" netmask
283
284 #check for an already active dhcp server on the interface, unless 'force' is set
285 config_get_bool force "$cfg" force 0
286 [ $force -gt 0 ] || {
287 udhcpc -n -q -s /bin/true -t 1 -i $ifname >&- && {
288 logger -t dnsmasq \
289 "found already running DHCP-server on interface '$ifname'" \
290 "refusing to start, use 'option force 1' to override"
291 return 0
292 }
293 }
294
295 config_get start "$cfg" start
296 config_get limit "$cfg" limit
297 config_get leasetime "$cfg" leasetime
298 config_get options "$cfg" options
299 config_get_bool dynamicdhcp "$cfg" dynamicdhcp 1
300
301 leasetime="${leasetime:-12h}"
302 start="$(dhcp_calc "${start:-100}")"
303 limit="${limit:-150}"
304 [ "$limit" -gt 0 ] && limit=$((limit-1))
305 eval "$(ipcalc.sh $ipaddr $netmask $start $limit)"
306 if [ "$dynamicdhcp" = "0" ]; then END="static"; fi
307 append args "--dhcp-range=$networkid,$START,$END,$NETMASK,$leasetime${options:+ $options}"
308
309 dhcp_option_add "$cfg" "$networkid"
310 }
311
312 dhcp_option_add() {
313 local cfg="$1"
314 local networkid="$2"
315
316 config_get dhcp_option "$cfg" dhcp_option
317 for o in $dhcp_option; do
318 append args "-O $networkid","$o"
319 done
320
321 }
322
323 dhcp_domain_add() {
324 local cfg="$1"
325 local ip name names
326
327 config_get names "$cfg" name
328 [ -n "$names" ] || return 0
329
330 config_get ip "$cfg" ip
331 [ -n "$ip" ] || return 0
332
333 local oIFS="$IFS"; IFS="."; set -- $ip; IFS="$oIFS"
334 local raddr="${4:+$4.$3.$2.$1.in-addr.arpa}"
335
336 for name in $names; do
337 local fqdn="$name"
338
339 [ "${fqdn%.*}" == "$fqdn" ] && \
340 fqdn="$fqdn${DOMAIN:+.$DOMAIN}"
341
342 append args "-A /$fqdn/$ip"
343
344 [ -n "$raddr" ] && {
345 append args "--ptr-record=$raddr,$fqdn"
346 raddr=""
347 }
348 done
349 }
350
351 dhcp_srv_add() {
352 local cfg="$1"
353
354 config_get srv "$cfg" srv
355 [ -n "$srv" ] || return 0
356
357 config_get target "$cfg" target
358 [ -n "$target" ] || return 0
359
360 config_get port "$cfg" port
361
362 local service="$srv,$target"
363 [ -n "$port" ] && service="$service,$port"
364
365 append args "-W $service"
366 }
367
368 start() {
369 include /lib/network
370 scan_interfaces
371 config_load dhcp
372
373 args=""
374 config_foreach dnsmasq dnsmasq
375 config_foreach dhcp_host_add host
376 config_foreach dhcp_boot_add boot
377 config_foreach dhcp_mac_add mac
378 config_foreach dhcp_vendorclass_add vendorclass
379 config_foreach dhcp_userclass_add userclass
380 config_foreach dhcp_circuitid_add circuitid
381 config_foreach dhcp_remoteid_add remoteid
382 config_foreach dhcp_subscrid_add subscrid
383 config_foreach dhcp_domain_add domain
384 config_foreach dhcp_srv_add srvhost
385 config_foreach dhcp_add dhcp
386
387 /usr/sbin/dnsmasq $args && {
388 rm -f /tmp/resolv.conf
389 [ -n "$DOMAIN" ] && echo "search $DOMAIN" >> /tmp/resolv.conf
390 DNS_SERVERS="$DNS_SERVERS 127.0.0.1"
391 for DNS_SERVER in $DNS_SERVERS ; do
392 echo "nameserver $DNS_SERVER" >> /tmp/resolv.conf
393 done
394 }
395 }
396
397 stop() {
398 [ -f /tmp/resolv.conf ] && {
399 rm -f /tmp/resolv.conf
400 ln -s /tmp/resolv.conf.auto /tmp/resolv.conf
401 }
402 killall dnsmasq
403 return 0
404 }
This page took 0.063358 seconds and 5 git commands to generate.