b5ca5ca1766a16ad514d75e3b8adc7f563941004
[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
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" ] || [ -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 xappend "--dhcp-host=$macs${networkid:+,net:$networkid}${ip:+,$ip}${name:+,$name}"
235 }
236
237 dhcp_mac_add() {
238 local cfg="$1"
239
240 config_get networkid "$cfg" networkid
241 [ -n "$networkid" ] || return 0
242
243 config_get mac "$cfg" mac
244 [ -n "$mac" ] || return 0
245
246 xappend "--dhcp-mac=$networkid,$mac"
247
248 dhcp_option_add "$cfg" "$networkid"
249 }
250
251 dhcp_boot_add() {
252 local cfg="$1"
253
254 config_get networkid "$cfg" networkid
255
256 config_get filename "$cfg" filename
257 [ -n "$filename" ] || return 0
258
259 config_get servername "$cfg" servername
260 [ -n "$servername" ] || return 0
261
262 config_get serveraddress "$cfg" serveraddress
263 [ -n "$serveraddress" ] || return 0
264
265 xappend "--dhcp-boot=${networkid:+net:$networkid,}$filename,$servername,$serveraddress"
266
267 dhcp_option_add "$cfg" "$networkid"
268 }
269
270
271 dhcp_add() {
272 local cfg="$1"
273 config_get net "$cfg" interface
274 [ -n "$net" ] || return 0
275
276 config_get networkid "$cfg" networkid
277 [ -n "$networkid" ] || networkid="$net"
278
279 config_get ifname "$net" ifname
280 [ -n "$ifname" ] || return 0
281
282 config_get dnsserver "$net" dns
283 [ "$cachelocal" = "0" -a -n "$dnsserver" ] && {
284 DNS_SERVERS="$DNS_SERVERS $dnsserver"
285 }
286
287 append_bool "$cfg" ignore "--no-dhcp-interface=$ifname" && return 0
288
289 config_get proto "$net" proto
290 [ static = "$proto" ] || return 0
291
292 config_get ipaddr "$net" ipaddr
293 config_get netmask "$cfg" netmask
294 [ -n "$netmask" ] || config_get netmask "$net" netmask
295
296 #check for an already active dhcp server on the interface, unless 'force' is set
297 config_get_bool force "$cfg" force 0
298 [ $force -gt 0 ] || {
299 udhcpc -n -q -s /bin/true -t 1 -i $ifname >&- && {
300 logger -t dnsmasq \
301 "found already running DHCP-server on interface '$ifname'" \
302 "refusing to start, use 'option force 1' to override"
303 return 0
304 }
305 }
306
307 config_get start "$cfg" start
308 config_get limit "$cfg" limit
309 config_get leasetime "$cfg" leasetime
310 config_get options "$cfg" options
311 config_get_bool dynamicdhcp "$cfg" dynamicdhcp 1
312
313 leasetime="${leasetime:-12h}"
314 start="$(dhcp_calc "${start:-100}")"
315 limit="${limit:-150}"
316 [ "$limit" -gt 0 ] && limit=$((limit-1))
317 eval "$(ipcalc.sh $ipaddr $netmask $start $limit)"
318 if [ "$dynamicdhcp" = "0" ]; then END="static"; fi
319 xappend "--dhcp-range=$networkid,$START,$END,$NETMASK,$leasetime${options:+ $options}"
320
321 dhcp_option_add "$cfg" "$networkid"
322 }
323
324 dhcp_option_add() {
325 local cfg="$1"
326 local networkid="$2"
327
328 config_get dhcp_option "$cfg" dhcp_option
329 for o in $dhcp_option; do
330 xappend "--dhcp-option=$networkid","$o"
331 done
332
333 }
334
335 dhcp_domain_add() {
336 local cfg="$1"
337 local ip name names
338
339 config_get names "$cfg" name "$2"
340 [ -n "$names" ] || return 0
341
342 config_get ip "$cfg" ip "$3"
343 [ -n "$ip" ] || return 0
344
345 local oIFS="$IFS"; IFS="."; set -- $ip; IFS="$oIFS"
346 local raddr="${4:+$4.$3.$2.$1.in-addr.arpa}"
347
348 for name in $names; do
349 local fqdn="$name"
350
351 [ "${fqdn%.*}" == "$fqdn" ] && \
352 fqdn="$fqdn${DOMAIN:+.$DOMAIN}"
353
354 xappend "--address=/$fqdn/$ip"
355
356 [ -n "$raddr" ] && {
357 xappend "--ptr-record=$raddr,$fqdn"
358 raddr=""
359 }
360 done
361 }
362
363 dhcp_srv_add() {
364 local cfg="$1"
365
366 config_get srv "$cfg" srv
367 [ -n "$srv" ] || return 0
368
369 config_get target "$cfg" target
370 [ -n "$target" ] || return 0
371
372 config_get port "$cfg" port
373 [ -n "$port" ] || return 0
374
375 config_get class "$cfg" class
376 config_get weight "$cfg" weight
377
378 local service="$srv,$target,$port${class:+,$class${weight:+,$weight}}"
379
380 xappend "--srv-host=$service"
381 }
382
383 dhcp_mx_add() {
384 local cfg="$1"
385 local domain relay pref
386
387 config_get domain "$cfg" domain
388 [ -n "$domain" ] || return 0
389
390 config_get relay "$cfg" relay
391 [ -n "$relay" ] || return 0
392
393 config_get pref "$cfg" pref 0
394
395 local service="$domain,$relay,$pref"
396
397 append args "--mx-host=$service"
398 }
399
400 dhcp_cname_add() {
401 local cfg="$1"
402 local cname target
403
404 config_get cname "$cfg" cname
405 [ -n "$cname" ] || return 0
406
407 config_get target "$cfg" target
408 [ -n "$target" ] || return 0
409
410 xappend "--cname=${cname},${target}"
411 }
412
413 start() {
414 include /lib/network
415 scan_interfaces
416
417 local lanaddr
418 config_get lanaddr "lan" ipaddr
419
420 config_load dhcp
421
422 # before we can call xappend
423 mkdir -p $(dirname $CONFIGFILE)
424
425 echo "# auto-generated config file from /etc/config/dhcp" > $CONFIGFILE
426
427 # if we did this last, we could override auto-generated config
428 [ -f /etc/dnsmasq.conf ] && {
429 xappend "--conf-file=/etc/dnsmasq.conf"
430 }
431
432 args=""
433 config_foreach dnsmasq dnsmasq
434 config_foreach dhcp_host_add host
435 echo >> $CONFIGFILE
436 config_foreach dhcp_boot_add boot
437 config_foreach dhcp_mac_add mac
438 config_foreach dhcp_vendorclass_add vendorclass
439 config_foreach dhcp_userclass_add userclass
440 config_foreach dhcp_circuitid_add circuitid
441 config_foreach dhcp_remoteid_add remoteid
442 config_foreach dhcp_subscrid_add subscrid
443 config_foreach dhcp_domain_add domain
444 echo >> $CONFIGFILE
445 config_foreach dhcp_srv_add srvhost
446 config_foreach dhcp_mx_add mxhost
447 echo >> $CONFIGFILE
448 config_foreach dhcp_add dhcp
449 echo >> $CONFIGFILE
450 config_foreach dhcp_cname_add cname
451 echo >> $CONFIGFILE
452
453 # add own hostname
454 [ -z "$lanaddr" ] || {
455 local hostname="$(uci_get system.@system[0].hostname)"
456 dhcp_domain_add "" "${hostname:-OpenWrt}" "$lanaddr"
457 }
458
459 service_start /usr/sbin/dnsmasq -C $CONFIGFILE && {
460 rm -f /tmp/resolv.conf
461 [ -n "$DOMAIN" ] && echo "search $DOMAIN" >> /tmp/resolv.conf
462 DNS_SERVERS="$DNS_SERVERS 127.0.0.1"
463 for DNS_SERVER in $DNS_SERVERS ; do
464 echo "nameserver $DNS_SERVER" >> /tmp/resolv.conf
465 done
466 }
467 }
468
469 stop() {
470 service_stop /usr/sbin/dnsmasq && {
471 [ -f /tmp/resolv.conf ] && {
472 rm -f /tmp/resolv.conf
473 ln -s /tmp/resolv.conf.auto /tmp/resolv.conf
474 }
475 }
476 }