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