[package] dnsmasq: prevent bad command line error when a dhcp pool has ignore set...
[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"
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 leasefile $cfg leasefile
94 [ -e "$leasefile" ] || touch "$leasefile"
95 config_get_bool cachelocal "$cfg" cachelocal 1
96
97 config_get hostsfile "$cfg" dhcphostsfile
98 [ -e "$hostsfile" ] && append args "--dhcp-hostsfile=$hostsfile"
99 }
100
101 dhcp_subscrid_add() {
102 local cfg="$1"
103
104 config_get networkid "$cfg" networkid
105 [ -n "$networkid" ] || return 0
106
107 config_get subscriberid "$cfg" subscriberid
108 [ -n "$subscriberid" ] || return 0
109
110 append args "--dhcp-subscrid=$networkid,$subscriberid"
111
112 dhcp_option_add "$cfg" "$networkid"
113 }
114
115 dhcp_remoteid_add() {
116 local cfg="$1"
117
118 config_get networkid "$cfg" networkid
119 [ -n "$networkid" ] || return 0
120
121 config_get remoteid "$cfg" remoteid
122 [ -n "$remoteid" ] || return 0
123
124 append args "--dhcp-remoteid=$networkid,$remoteid"
125
126 dhcp_option_add "$cfg" "$networkid"
127 }
128
129 dhcp_circuitid_add() {
130 local cfg="$1"
131
132 config_get networkid "$cfg" networkid
133 [ -n "$networkid" ] || return 0
134
135 config_get circuitid "$cfg" circuitid
136 [ -n "$circuitid" ] || return 0
137
138 append args "--dhcp-circuitid=$networkid,$circuitid"
139
140 dhcp_option_add "$cfg" "$networkid"
141 }
142
143 dhcp_userclass_add() {
144 local cfg="$1"
145
146 config_get networkid "$cfg" networkid
147 [ -n "$networkid" ] || return 0
148
149 config_get userclass "$cfg" userclass
150 [ -n "$userclass" ] || return 0
151
152 append args "--dhcp-userclass=$networkid,$userclass"
153
154 dhcp_option_add "$cfg" "$networkid"
155 }
156
157 dhcp_vendorclass_add() {
158 local cfg="$1"
159
160 config_get networkid "$cfg" networkid
161 [ -n "$networkid" ] || return 0
162
163 config_get vendorclass "$cfg" vendorclass
164 [ -n "$vendorclass" ] || return 0
165
166 append args "--dhcp-vendorclass=$networkid,$vendorclass"
167
168 dhcp_option_add "$cfg" "$networkid"
169 }
170
171 dhcp_host_add() {
172 local cfg="$1"
173
174 config_get name "$cfg" name
175
176 config_get networkid "$cfg" networkid
177 [ -n "$networkid" ] && dhcp_option_add "$cfg" "$networkid"
178
179 config_get mac "$cfg" mac
180 [ -n "$mac" ] || return 0
181
182 config_get ip "$cfg" ip
183 [ -n "$ip" ] || return 0
184
185 append args "--dhcp-host=$mac,${networkid:+net:$networkid,}$ip${name:+,$name}"
186 }
187
188 dhcp_mac_add() {
189 local cfg="$1"
190
191 config_get networkid "$cfg" networkid
192 [ -n "$networkid" ] || return 0
193
194 config_get mac "$cfg" mac
195 [ -n "$mac" ] || return 0
196
197 append args "--dhcp-mac=$networkid,$mac"
198
199 dhcp_option_add "$cfg" "$networkid"
200 }
201
202 dhcp_boot_add() {
203 local cfg="$1"
204
205 config_get networkid "$cfg" networkid
206
207 config_get filename "$cfg" filename
208 [ -n "$filename" ] || return 0
209
210 config_get servername "$cfg" servername
211 [ -n "$servername" ] || return 0
212
213 config_get serveraddress "$cfg" serveraddress
214 [ -n "$serveraddress" ] || return 0
215
216 append args "--dhcp-boot=${networkid:+net:$networkid,}$filename,$servername,$serveraddress"
217
218 dhcp_option_add "$cfg" "$networkid"
219 }
220
221
222 dhcp_add() {
223 local cfg="$1"
224 config_get net "$cfg" interface
225 [ -n "$net" ] || return 0
226
227 config_get networkid "$cfg" networkid
228 [ -n "$networkid" ] || networkid="$net"
229
230 config_get ifname "$net" ifname
231 [ -n "$ifname" ] || return 0
232
233 config_get dnsserver "$net" dns
234 [ "$cachelocal" = "0" -a -n "$dnsserver" ] && {
235 DNS_SERVERS="$DNS_SERVERS $dnsserver"
236 }
237
238 append_bool "$cfg" ignore "-2 '$ifname'" && return 0
239
240 config_get proto "$net" proto
241 [ static = "$proto" ] || return 0
242
243 config_get ipaddr "$net" ipaddr
244 config_get netmask "$cfg" netmask
245 [ -n "$netmask" ] || config_get netmask "$net" netmask
246
247 #check for an already active dhcp server on the interface, unless 'force' is set
248 config_get_bool force "$cfg" force 0
249 [ "$force" -gt 0 ] || {
250 udhcpc -n -q -s /bin/true -t 1 -i $ifname >&- && return 0
251 }
252
253 config_get start "$cfg" start
254 config_get limit "$cfg" limit
255 config_get leasetime "$cfg" leasetime
256 config_get options "$cfg" options
257 config_get_bool dynamicdhcp "$cfg" dynamicdhcp 1
258
259 leasetime="${leasetime:-12h}"
260 start="$(dhcp_calc "${start:-100}")"
261 limit="$((${limit:-150} + 1))"
262 eval "$(ipcalc.sh $ipaddr $netmask $start $limit)"
263 if [ "$dynamicdhcp" = "0" ]; then END="static"; fi
264 append args "--dhcp-range=$networkid,$START,$END,$NETMASK,$leasetime${options:+ $options}"
265
266 dhcp_option_add "$cfg" "$networkid"
267 }
268
269 dhcp_option_add() {
270 local cfg="$1"
271 local networkid="$2"
272
273 config_get dhcp_option "$cfg" dhcp_option
274 for o in $dhcp_option; do
275 append args "-O $networkid","$o"
276 done
277
278 }
279
280 dhcp_domain_add() {
281 local cfg="$1"
282
283 config_get name "$cfg" name
284 [ -n "$name" ] || return 0
285
286 config_get ip "$cfg" ip
287 [ -n "$ip" ] || return 0
288
289 local oIFS="$IFS"; IFS="."; set -- $ip; IFS="$oIFS"
290 append args "-A /$name${DOMAIN:+.$DOMAIN}/$ip"
291 append args "--ptr-record=$4.$3.$2.$1.in-addr.arpa,$name${DOMAIN:+.$DOMAIN}"
292 }
293
294 start() {
295 include /lib/network
296 scan_interfaces
297 config_load dhcp
298
299 args=""
300 config_foreach dnsmasq dnsmasq
301 config_foreach dhcp_host_add host
302 config_foreach dhcp_boot_add boot
303 config_foreach dhcp_mac_add mac
304 config_foreach dhcp_vendorclass_add vendorclass
305 config_foreach dhcp_userclass_add userclass
306 config_foreach dhcp_circuitid_add circuitid
307 config_foreach dhcp_remoteid_add remoteid
308 config_foreach dhcp_subscrid_add subscrid
309 config_foreach dhcp_domain_add domain
310 config_foreach dhcp_add dhcp
311
312 /usr/sbin/dnsmasq $args && {
313 rm -f /tmp/resolv.conf
314 [ -n "$DOMAIN" ] && echo "search $DOMAIN" >> /tmp/resolv.conf
315 DNS_SERVERS="$DNS_SERVERS 127.0.0.1"
316 for DNS_SERVER in $DNS_SERVERS ; do
317 echo "nameserver $DNS_SERVER" >> /tmp/resolv.conf
318 done
319 }
320 }
321
322 stop() {
323 [ -f /tmp/resolv.conf ] && {
324 rm -f /tmp/resolv.conf
325 ln -s /tmp/resolv.conf.auto /tmp/resolv.conf
326 }
327 killall dnsmasq
328 return 0
329 }