base-files: move firstboot to /sbin
[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
7 dhcp_calc() {
8 local ip="$1"
9 local res=0
10
11 while [ -n "$ip" ]; do
12 part="${ip%%.*}"
13 res="$(($res * 256))"
14 res="$(($res + $part))"
15 [ "${ip%.*}" != "$ip" ] && ip="${ip#*.}" || ip=
16 done
17 echo "$res"
18 }
19
20 append_bool() {
21 local section="$1"
22 local option="$2"
23 local value="$3"
24 local _loctmp
25 config_get_bool _loctmp "$section" "$option"
26 [ "$_loctmp" -gt 0 ] && append args "$value"
27 }
28
29 append_parm() {
30 local section="$1"
31 local option="$2"
32 local switch="$3"
33 local _loctmp
34 config_get _loctmp "$section" "$option"
35 [ -z "$_loctmp" ] && return 0
36 append args "$switch $_loctmp"
37 }
38
39 append_server() {
40 append args "-S $1"
41 }
42
43 append_interface() {
44 append args "-i $1"
45 }
46
47 append_notinterface() {
48 append args "-I $1"
49 }
50
51 dnsmasq() {
52 local cfg="$1"
53 append_bool "$cfg" authoritative "-K"
54 append_bool "$cfg" nodaemon "-d"
55 append_bool "$cfg" domainneeded "-D"
56 append_bool "$cfg" filterwin2k "-f"
57 append_bool "$cfg" nohosts "-h"
58 append_bool "$cfg" nonegcache "-N"
59 append_bool "$cfg" strictorder "-o"
60 append_bool "$cfg" logqueries "-q"
61 append_bool "$cfg" noresolv "-R"
62 append_bool "$cfg" localise_queries "-y"
63 append_bool "$cfg" readethers "-Z"
64 append_bool "$cfg" dbus "-1"
65 append_bool "$cfg" boguspriv "-b"
66 append_bool "$cfg" expandhosts "-E"
67 append_bool "$cfg" enable_tftp "--enable-tftp"
68 append_bool "$cfg" nonwildcard "-z"
69
70 append_parm "$cfg" dnsforwardmax "-0"
71 append_parm "$cfg" port "-p"
72 append_parm "$cfg" ednspacket_max "-P"
73 append_parm "$cfg" dhcpleasemax "-X"
74 append_parm "$cfg" "addnhosts" "-H"
75 append_parm "$cfg" "queryport" "-Q"
76 append_parm "$cfg" "domain" "-s"
77 append_parm "$cfg" "local" "-S"
78 config_list_foreach "$cfg" "server" append_server
79 config_list_foreach "$cfg" "interface" append_interface
80 config_list_foreach "$cfg" "notinterface" append_notinterface
81 append_parm "$cfg" "leasefile" "-l"
82 append_parm "$cfg" "resolvfile" "-r"
83 append_parm "$cfg" "tftp_root" "--tftp-root"
84 append_parm "$cfg" "dhcp_boot" "--dhcp-boot"
85
86 config_get leasefile $cfg leasefile
87 [ -e "$leasefile" ] || touch "$leasefile"
88 config_get_bool cachelocal "$cfg" cachelocal 1
89
90 config_get hostsfile "$cfg" dhcphostsfile
91 [ -e "$hostsfile" ] && append args "--dhcp-hostsfile=$hostsfile"
92 }
93
94 dhcp_subscrid_add() {
95 local cfg="$1"
96
97 config_get name "$cfg" name
98 [ -n "$name" ] || return 0
99
100 config_get subscriberid "$cfg" subscriberid
101 [ -n "$subscriberid" ] || return 0
102
103 append args "--dhcp-subscrid=$name,$subscriberid"
104
105 dhcp_option_add "$cfg" "$name"
106 }
107
108 dhcp_remoteid_add() {
109 local cfg="$1"
110
111 config_get name "$cfg" name
112 [ -n "$name" ] || return 0
113
114 config_get remoteid "$cfg" remoteid
115 [ -n "$remoteid" ] || return 0
116
117 append args "--dhcp-remoteid=$name,$remoteid"
118
119 dhcp_option_add "$cfg" "$name"
120 }
121
122 dhcp_circuitid_add() {
123 local cfg="$1"
124
125 config_get name "$cfg" name
126 [ -n "$name" ] || return 0
127
128 config_get circuitid "$cfg" circuitid
129 [ -n "$circuitid" ] || return 0
130
131 append args "--dhcp-circuitid=$name,$circuitid"
132
133 dhcp_option_add "$cfg" "$name"
134 }
135
136 dhcp_userclass_add() {
137 local cfg="$1"
138
139 config_get name "$cfg" name
140 [ -n "$name" ] || return 0
141
142 config_get userclass "$cfg" userclass
143 [ -n "$userclass" ] || return 0
144
145 append args "--dhcp-userclass=$name,$userclass"
146
147 dhcp_option_add "$cfg" "$name"
148 }
149
150 dhcp_vendorclass_add() {
151 local cfg="$1"
152
153 config_get name "$cfg" name
154 [ -n "$name" ] || return 0
155
156 config_get vendorclass "$cfg" vendorclass
157 [ -n "$vendorclass" ] || return 0
158
159 append args "--dhcp-vendorclass=$name,$vendorclass"
160
161 dhcp_option_add "$cfg" "$name"
162 }
163
164 dhcp_host_add() {
165 local cfg="$1"
166
167 config_get name "$cfg" name
168 [ -n "$name" ] || return 0
169
170 config_get mac "$cfg" mac
171 [ -n "$mac" ] || return 0
172
173 config_get ip "$cfg" ip
174 [ -n "$ip" ] || return 0
175
176 append args "--dhcp-host=$mac,$ip"
177
178 dhcp_option_add "$cfg" "$name"
179 }
180
181 dhcp_mac_add() {
182 local cfg="$1"
183
184 config_get name "$cfg" name
185 [ -n "$name" ] || return 0
186
187 config_get mac "$cfg" mac
188 [ -n "$mac" ] || return 0
189
190 append args "--dhcp-mac=$name,$mac"
191
192 dhcp_option_add "$cfg" "$name"
193 }
194
195 dhcp_boot_add() {
196 local cfg="$1"
197
198 config_get name "$cfg" name
199
200 config_get filename "$cfg" filename
201 [ -n "$filename" ] || return 0
202
203 config_get servername "$cfg" servername
204 [ -n "$servername" ] || return 0
205
206 config_get serveraddress "$cfg" serveraddress
207 [ -n "$serveraddress" ] || return 0
208
209 append args "--dhcp-boot=${name:+net:$name,}$filename,$servername,$serveraddress"
210
211 dhcp_option_add "$cfg" "$name"
212 }
213
214
215 dhcp_add() {
216 local cfg="$1"
217 config_get net "$cfg" interface
218 [ -n "$net" ] || return 0
219
220 config_get name "$cfg" name
221 [ -n "$name" ] || name="$net"
222
223 config_get ifname "$net" ifname
224 [ -n "$ifname" ] || return 0
225
226 config_get dnsserver "$net" dns
227 [ "$cachelocal" = "0" -a -n "$dnsserver" ] && {
228 DNS_SERVERS="$DNS_SERVERS $dnsserver"
229 }
230
231 append_bool "$cfg" ignore "-2 $ifname" && return 0
232
233 config_get proto "$net" proto
234 [ static = "$proto" ] || return 0
235
236 config_get ipaddr "$net" ipaddr
237 config_get netmask "$cfg" netmask
238 [ -n "$netmask" ] || config_get netmask "$net" netmask
239
240 #check for an already active dhcp server on the interface, unless 'force' is set
241 config_get_bool force "$cfg" force 0
242 [ "$force" -gt 0 ] || {
243 udhcpc -n -q -s /bin/true -t 1 -i $ifname >&- && return 0
244 }
245
246 config_get start "$cfg" start
247 config_get limit "$cfg" limit
248 config_get leasetime "$cfg" leasetime
249 config_get options "$cfg" options
250 config_get_bool dynamicdhcp "$cfg" dynamicdhcp 1
251
252 leasetime="${leasetime:-12h}"
253 start="$(dhcp_calc "${start:-100}")"
254 limit="$((${limit:-150} + 1))"
255 eval "$(ipcalc.sh $ipaddr $netmask $start $limit)"
256 if [ "$dynamicdhcp" = "0" ]; then END="static"; fi
257 append args "--dhcp-range=$name,$START,$END,$NETMASK,$leasetime${options:+ $options}"
258
259 dhcp_option_add "$cfg" "$name"
260 }
261
262 dhcp_option_add() {
263 local cfg="$1"
264 local name="$2"
265
266 config_get dhcp_option "$cfg" dhcp_option
267 for o in $dhcp_option; do
268 append args "-O $name","$o"
269 done
270
271 }
272
273 start() {
274 include /lib/network
275 scan_interfaces
276 config_load dhcp
277
278 args=""
279 config_foreach dnsmasq dnsmasq
280 config_foreach dhcp_host_add host
281 config_foreach dhcp_boot_add boot
282 config_foreach dhcp_mac_add mac
283 config_foreach dhcp_vendorclass_add vendorclass
284 config_foreach dhcp_userclass_add userclass
285 config_foreach dhcp_circuitid_add circuitid
286 config_foreach dhcp_remoteid_add remoteid
287 config_foreach dhcp_subscrid_add subscrid
288 config_foreach dhcp_add dhcp
289
290 /usr/sbin/dnsmasq $args && {
291 rm -f /tmp/resolv.conf
292 DNS_SERVERS="$DNS_SERVERS 127.0.0.1"
293 for DNS_SERVER in $DNS_SERVERS ; do
294 echo "nameserver $DNS_SERVER" >> /tmp/resolv.conf
295 done
296 }
297 }
298
299 stop() {
300 [ -f /tmp/resolv.conf ] && {
301 rm -f /tmp/resolv.conf
302 ln -s /tmp/resolv.conf.auto /tmp/resolv.conf
303 }
304 killall dnsmasq
305 return 0
306 }
This page took 0.065776 seconds and 5 git commands to generate.