Add support for SRV records to dnsmasq config parsing
[openwrt/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"
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 append args "-i $1"
46 }
47
48 append_notinterface() {
49 append args "-I $1"
50 }
51
52 append_addnhosts() {
53 append args "-H $1"
54 }
55
56 dnsmasq() {
57 local cfg="$1"
58 append_bool "$cfg" authoritative "-K"
59 append_bool "$cfg" nodaemon "-d"
60 append_bool "$cfg" domainneeded "-D"
61 append_bool "$cfg" filterwin2k "-f"
62 append_bool "$cfg" nohosts "-h"
63 append_bool "$cfg" nonegcache "-N"
64 append_bool "$cfg" strictorder "-o"
65 append_bool "$cfg" logqueries "-q"
66 append_bool "$cfg" noresolv "-R"
67 append_bool "$cfg" localise_queries "-y"
68 append_bool "$cfg" readethers "-Z"
69 append_bool "$cfg" dbus "-1"
70 append_bool "$cfg" boguspriv "-b"
71 append_bool "$cfg" expandhosts "-E"
72 append_bool "$cfg" enable_tftp "--enable-tftp"
73 append_bool "$cfg" nonwildcard "-z"
74
75 append_parm "$cfg" dnsforwardmax "-0"
76 append_parm "$cfg" port "-p"
77 append_parm "$cfg" ednspacket_max "-P"
78 append_parm "$cfg" dhcpleasemax "-X"
79 append_parm "$cfg" "queryport" "-Q"
80 append_parm "$cfg" "domain" "-s"
81 append_parm "$cfg" "local" "-S"
82 config_list_foreach "$cfg" "server" append_server
83 config_list_foreach "$cfg" "interface" append_interface
84 config_list_foreach "$cfg" "notinterface" append_notinterface
85 config_list_foreach "$cfg" "addnhosts" append_addnhosts
86 append_parm "$cfg" "leasefile" "-l"
87 append_parm "$cfg" "resolvfile" "-r"
88 append_parm "$cfg" "tftp_root" "--tftp-root"
89 append_parm "$cfg" "dhcp_boot" "--dhcp-boot"
90
91 config_get DOMAIN "$cfg" domain
92
93 config_get_bool readethers "$cfg" readethers
94 [ "$readethers" = "1" ] && [ -e "/etc/ethers" ] || touch /etc/ethers
95
96 config_get leasefile $cfg leasefile
97 [ -e "$leasefile" ] || touch "$leasefile"
98 config_get_bool cachelocal "$cfg" cachelocal 1
99
100 config_get hostsfile "$cfg" dhcphostsfile
101 [ -e "$hostsfile" ] && append args "--dhcp-hostsfile=$hostsfile"
102 }
103
104 dhcp_subscrid_add() {
105 local cfg="$1"
106
107 config_get networkid "$cfg" networkid
108 [ -n "$networkid" ] || return 0
109
110 config_get subscriberid "$cfg" subscriberid
111 [ -n "$subscriberid" ] || return 0
112
113 append args "--dhcp-subscrid=$networkid,$subscriberid"
114
115 dhcp_option_add "$cfg" "$networkid"
116 }
117
118 dhcp_remoteid_add() {
119 local cfg="$1"
120
121 config_get networkid "$cfg" networkid
122 [ -n "$networkid" ] || return 0
123
124 config_get remoteid "$cfg" remoteid
125 [ -n "$remoteid" ] || return 0
126
127 append args "--dhcp-remoteid=$networkid,$remoteid"
128
129 dhcp_option_add "$cfg" "$networkid"
130 }
131
132 dhcp_circuitid_add() {
133 local cfg="$1"
134
135 config_get networkid "$cfg" networkid
136 [ -n "$networkid" ] || return 0
137
138 config_get circuitid "$cfg" circuitid
139 [ -n "$circuitid" ] || return 0
140
141 append args "--dhcp-circuitid=$networkid,$circuitid"
142
143 dhcp_option_add "$cfg" "$networkid"
144 }
145
146 dhcp_userclass_add() {
147 local cfg="$1"
148
149 config_get networkid "$cfg" networkid
150 [ -n "$networkid" ] || return 0
151
152 config_get userclass "$cfg" userclass
153 [ -n "$userclass" ] || return 0
154
155 append args "--dhcp-userclass=$networkid,$userclass"
156
157 dhcp_option_add "$cfg" "$networkid"
158 }
159
160 dhcp_vendorclass_add() {
161 local cfg="$1"
162
163 config_get networkid "$cfg" networkid
164 [ -n "$networkid" ] || return 0
165
166 config_get vendorclass "$cfg" vendorclass
167 [ -n "$vendorclass" ] || return 0
168
169 append args "--dhcp-vendorclass=$networkid,$vendorclass"
170
171 dhcp_option_add "$cfg" "$networkid"
172 }
173
174 dhcp_host_add() {
175 local cfg="$1"
176
177 config_get name "$cfg" name
178
179 config_get networkid "$cfg" networkid
180 [ -n "$networkid" ] && dhcp_option_add "$cfg" "$networkid"
181
182 config_get ip "$cfg" ip
183 [ -n "$ip" ] || return 0
184
185 macs=""
186 config_get mac "$cfg" mac
187 for m in $mac; do append macs "$m" ","; done
188 [ -n "$macs" ] || return 0
189
190 append args "--dhcp-host=$macs,${networkid:+net:$networkid,}$ip${name:+,$name}"
191 }
192
193 dhcp_mac_add() {
194 local cfg="$1"
195
196 config_get networkid "$cfg" networkid
197 [ -n "$networkid" ] || return 0
198
199 config_get mac "$cfg" mac
200 [ -n "$mac" ] || return 0
201
202 append args "--dhcp-mac=$networkid,$mac"
203
204 dhcp_option_add "$cfg" "$networkid"
205 }
206
207 dhcp_boot_add() {
208 local cfg="$1"
209
210 config_get networkid "$cfg" networkid
211
212 config_get filename "$cfg" filename
213 [ -n "$filename" ] || return 0
214
215 config_get servername "$cfg" servername
216 [ -n "$servername" ] || return 0
217
218 config_get serveraddress "$cfg" serveraddress
219 [ -n "$serveraddress" ] || return 0
220
221 append args "--dhcp-boot=${networkid:+net:$networkid,}$filename,$servername,$serveraddress"
222
223 dhcp_option_add "$cfg" "$networkid"
224 }
225
226
227 dhcp_add() {
228 local cfg="$1"
229 config_get net "$cfg" interface
230 [ -n "$net" ] || return 0
231
232 config_get networkid "$cfg" networkid
233 [ -n "$networkid" ] || networkid="$net"
234
235 config_get ifname "$net" ifname
236 [ -n "$ifname" ] || return 0
237
238 config_get dnsserver "$net" dns
239 [ "$cachelocal" = "0" -a -n "$dnsserver" ] && {
240 DNS_SERVERS="$DNS_SERVERS $dnsserver"
241 }
242
243 append_bool "$cfg" ignore "-2 '$ifname'" && return 0
244
245 config_get proto "$net" proto
246 [ static = "$proto" ] || return 0
247
248 config_get ipaddr "$net" ipaddr
249 config_get netmask "$cfg" netmask
250 [ -n "$netmask" ] || config_get netmask "$net" netmask
251
252 #check for an already active dhcp server on the interface, unless 'force' is set
253 config_get_bool force "$cfg" force 0
254 [ "$force" -gt 0 ] || {
255 udhcpc -n -q -s /bin/true -t 1 -i $ifname >&- && return 0
256 }
257
258 config_get start "$cfg" start
259 config_get limit "$cfg" limit
260 config_get leasetime "$cfg" leasetime
261 config_get options "$cfg" options
262 config_get_bool dynamicdhcp "$cfg" dynamicdhcp 1
263
264 leasetime="${leasetime:-12h}"
265 start="$(dhcp_calc "${start:-100}")"
266 limit="${limit:-150}"
267 eval "$(ipcalc.sh $ipaddr $netmask $start $limit)"
268 if [ "$dynamicdhcp" = "0" ]; then END="static"; fi
269 append args "--dhcp-range=$networkid,$START,$END,$NETMASK,$leasetime${options:+ $options}"
270
271 dhcp_option_add "$cfg" "$networkid"
272 }
273
274 dhcp_option_add() {
275 local cfg="$1"
276 local networkid="$2"
277
278 config_get dhcp_option "$cfg" dhcp_option
279 for o in $dhcp_option; do
280 append args "-O $networkid","$o"
281 done
282
283 }
284
285 dhcp_domain_add() {
286 local cfg="$1"
287 local ip name names
288
289 config_get names "$cfg" name
290 [ -n "$names" ] || return 0
291
292 config_get ip "$cfg" ip
293 [ -n "$ip" ] || return 0
294
295 local oIFS="$IFS"; IFS="."; set -- $ip; IFS="$oIFS"
296 local raddr="${4:+$4.$3.$2.$1.in-addr.arpa}"
297
298 for name in $names; do
299 local fqdn="$name"
300
301 [ "${fqdn%.*}" == "$fqdn" ] && \
302 fqdn="$fqdn${DOMAIN:+.$DOMAIN}"
303
304 append args "-A /$fqdn/$ip"
305
306 [ -n "$raddr" ] && {
307 append args "--ptr-record=$raddr,$fqdn"
308 raddr=""
309 }
310 done
311 }
312
313 dhcp_srv_add() {
314 local cfg="$1"
315
316 config_get srv "$cfg" srv
317 [ -n "$srv" ] || return 0
318
319 config_get target "$cfg" target
320 [ -n "$target" ] || return 0
321
322 config_get port "$cfg" port
323
324 local service="$srv,$target"
325 [ -n "$port" ] && service="$service,$port"
326
327 append args "-W $service"
328 }
329
330 start() {
331 include /lib/network
332 scan_interfaces
333 config_load dhcp
334
335 args=""
336 config_foreach dnsmasq dnsmasq
337 config_foreach dhcp_host_add host
338 config_foreach dhcp_boot_add boot
339 config_foreach dhcp_mac_add mac
340 config_foreach dhcp_vendorclass_add vendorclass
341 config_foreach dhcp_userclass_add userclass
342 config_foreach dhcp_circuitid_add circuitid
343 config_foreach dhcp_remoteid_add remoteid
344 config_foreach dhcp_subscrid_add subscrid
345 config_foreach dhcp_domain_add domain
346 config_foreach dhcp_srv_add srvhost
347 config_foreach dhcp_add dhcp
348
349 /usr/sbin/dnsmasq $args && {
350 rm -f /tmp/resolv.conf
351 [ -n "$DOMAIN" ] && echo "search $DOMAIN" >> /tmp/resolv.conf
352 DNS_SERVERS="$DNS_SERVERS 127.0.0.1"
353 for DNS_SERVER in $DNS_SERVERS ; do
354 echo "nameserver $DNS_SERVER" >> /tmp/resolv.conf
355 done
356 }
357 }
358
359 stop() {
360 [ -f /tmp/resolv.conf ] && {
361 rm -f /tmp/resolv.conf
362 ln -s /tmp/resolv.conf.auto /tmp/resolv.conf
363 }
364 killall dnsmasq
365 return 0
366 }