dnsmasq: support and use local-service by default (#14951)
[15.05/openwrt.git] / package / network / services / dnsmasq / files / dnsmasq.init
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2007-2012 OpenWrt.org
3
4 START=60
5
6 USE_PROCD=1
7 PROG=/usr/sbin/dnsmasq
8
9 DNS_SERVERS=""
10 DOMAIN=""
11
12 ADD_LOCAL_DOMAIN=1
13 ADD_LOCAL_HOSTNAME=1
14
15 CONFIGFILE="/var/etc/dnsmasq.conf"
16 HOSTFILE="/tmp/hosts/dhcp"
17 TRUSTANCHORSFILE="/usr/share/dnsmasq/trust-anchors.conf"
18
19 xappend() {
20         local value="$1"
21
22         echo "${value#--}" >> $CONFIGFILE
23 }
24
25 dhcp_calc() {
26         local ip="$1"
27         local res=0
28
29         while [ -n "$ip" ]; do
30                 part="${ip%%.*}"
31                 res="$(($res * 256))"
32                 res="$(($res + $part))"
33                 [ "${ip%.*}" != "$ip" ] && ip="${ip#*.}" || ip=
34         done
35         echo "$res"
36 }
37
38 dhcp_check() {
39         local ifname="$1"
40         local stamp="/var/run/dnsmasq.$ifname.dhcp"
41         local rv=0
42
43         [ -s "$stamp" ] && return $(cat "$stamp")
44
45         udhcpc -n -q -s /bin/true -t 1 -i "$ifname" >&- && rv=1 || rv=0
46
47         [ $rv -eq 1 ] && \
48                 logger -t dnsmasq \
49                         "found already running DHCP-server on interface '$ifname'" \
50                         "refusing to start, use 'option force 1' to override"
51
52         echo $rv > "$stamp"
53         return $rv
54 }
55
56 log_once() {
57         pidof dnsmasq >/dev/null || \
58                 logger -t dnsmasq "$@"
59 }
60
61 append_bool() {
62         local section="$1"
63         local option="$2"
64         local value="$3"
65         local _loctmp
66         config_get_bool _loctmp "$section" "$option" 0
67         [ $_loctmp -gt 0 ] && xappend "$value"
68 }
69
70 append_parm() {
71         local section="$1"
72         local option="$2"
73         local switch="$3"
74         local _loctmp
75         config_get _loctmp "$section" "$option"
76         [ -z "$_loctmp" ] && return 0
77         xappend "$switch=$_loctmp"
78 }
79
80 append_server() {
81         xappend "--server=$1"
82 }
83
84 append_address() {
85         xappend "--address=$1"
86 }
87
88 append_ipset() {
89         xappend "--ipset=$1"
90 }
91
92 append_interface() {
93         local ifname=$(uci_get_state network "$1" ifname "$1")
94         xappend "--interface=$ifname"
95 }
96
97 append_notinterface() {
98         local ifname=$(uci_get_state network "$1" ifname "$1")
99         xappend "--except-interface=$ifname"
100 }
101
102 append_addnhosts() {
103         xappend "--addn-hosts=$1"
104 }
105
106 append_bogusnxdomain() {
107         xappend "--bogus-nxdomain=$1"
108 }
109
110 dnsmasq() {
111         local cfg="$1"
112         append_bool "$cfg" authoritative "--dhcp-authoritative"
113         append_bool "$cfg" nodaemon "--no-daemon"
114         append_bool "$cfg" domainneeded "--domain-needed"
115         append_bool "$cfg" filterwin2k "--filterwin2k"
116         append_bool "$cfg" nohosts "--no-hosts"
117         append_bool "$cfg" nonegcache "--no-negcache"
118         append_bool "$cfg" strictorder "--strict-order"
119         append_bool "$cfg" logqueries "--log-queries"
120         append_bool "$cfg" noresolv "--no-resolv"
121         append_bool "$cfg" localise_queries "--localise-queries"
122         append_bool "$cfg" readethers "--read-ethers"
123         append_bool "$cfg" dbus "--enable-dbus"
124         append_bool "$cfg" boguspriv "--bogus-priv"
125         append_bool "$cfg" expandhosts "--expand-hosts"
126         append_bool "$cfg" enable_tftp "--enable-tftp"
127         append_bool "$cfg" nonwildcard "--bind-interfaces"
128         append_bool "$cfg" fqdn "--dhcp-fqdn"
129         append_bool "$cfg" proxydnssec "--proxy-dnssec"
130         append_bool "$cfg" localservice "--local-service"
131
132         append_parm "$cfg" dhcpscript "--dhcp-script"
133         append_parm "$cfg" cachesize "--cache-size"
134         append_parm "$cfg" dnsforwardmax "--dns-forward-max"
135         append_parm "$cfg" port "--port"
136         append_parm "$cfg" ednspacket_max "--edns-packet-max"
137         append_parm "$cfg" dhcpleasemax "--dhcp-lease-max"
138         append_parm "$cfg" "queryport" "--query-port"
139         append_parm "$cfg" "domain" "--domain"
140         append_parm "$cfg" "local" "--server"
141         config_list_foreach "$cfg" "server" append_server
142         config_list_foreach "$cfg" "address" append_address
143         config_list_foreach "$cfg" "ipset" append_ipset
144         config_list_foreach "$cfg" "interface" append_interface
145         config_list_foreach "$cfg" "notinterface" append_notinterface
146         config_list_foreach "$cfg" "addnhosts" append_addnhosts
147         config_list_foreach "$cfg" "bogusnxdomain" append_bogusnxdomain
148         append_parm "$cfg" "leasefile" "--dhcp-leasefile"
149         append_parm "$cfg" "resolvfile" "--resolv-file"
150         append_parm "$cfg" "tftp_root" "--tftp-root"
151         append_parm "$cfg" "dhcp_boot" "--dhcp-boot"
152         append_parm "$cfg" "local_ttl" "--local-ttl"
153
154         config_get DOMAIN "$cfg" domain
155
156         config_get_bool ADD_LOCAL_DOMAIN "$cfg" add_local_domain 1
157         config_get_bool ADD_LOCAL_HOSTNAME "$cfg" add_local_hostname 1
158
159         config_get_bool readethers "$cfg" readethers
160         [ "$readethers" = "1" -a \! -e "/etc/ethers" ] && touch /etc/ethers
161
162         config_get leasefile $cfg leasefile
163         [ -n "$leasefile" -a \! -e "$leasefile" ] && touch "$leasefile"
164         config_get_bool cachelocal "$cfg" cachelocal 1
165
166         config_get hostsfile "$cfg" dhcphostsfile
167         [ -e "$hostsfile" ] && xappend "--dhcp-hostsfile=$hostsfile"
168
169         mkdir -p /tmp/hosts /tmp/dnsmasq.d
170         xappend "--addn-hosts=/tmp/hosts"
171         xappend "--conf-dir=/tmp/dnsmasq.d"
172
173         local rebind
174         config_get_bool rebind "$cfg" rebind_protection 1
175         [ $rebind -gt 0 ] && {
176                 log_once \
177                         "DNS rebinding protection is active," \
178                         "will discard upstream RFC1918 responses!"
179                 xappend "--stop-dns-rebind"
180
181                 local rebind_localhost
182                 config_get_bool rebind_localhost "$cfg" rebind_localhost 0
183                 [ $rebind_localhost -gt 0 ] && {
184                         log_once "Allowing 127.0.0.0/8 responses"
185                         xappend "--rebind-localhost-ok"
186                 }
187
188                 append_rebind_domain() {
189                         log_once "Allowing RFC1918 responses for domain $1"
190                         xappend "--rebind-domain-ok=$1"
191                 }
192
193                 config_list_foreach "$cfg" rebind_domain append_rebind_domain
194         }
195
196         config_get_bool dnssec "$cfg" dnssec 0
197         [ "$dnssec" -gt 0 ] && {
198                 xappend "--conf-file=$TRUSTANCHORSFILE"
199                 xappend "--dnssec"
200                 append_bool "$cfg" dnsseccheckunsigned "--dnssec-check-unsigned"
201         }
202
203         dhcp_option_add "$cfg" "" 0
204
205         xappend "--dhcp-broadcast=tag:needs-broadcast"
206
207         echo >> $CONFIGFILE
208 }
209
210 dhcp_subscrid_add() {
211         local cfg="$1"
212
213         config_get networkid "$cfg" networkid
214         [ -n "$networkid" ] || return 0
215
216         config_get subscriberid "$cfg" subscriberid
217         [ -n "$subscriberid" ] || return 0
218
219         xappend "--dhcp-subscrid=$networkid,$subscriberid"
220
221         config_get_bool force "$cfg" force 0
222
223         dhcp_option_add "$cfg" "$networkid" "$force"
224 }
225
226 dhcp_remoteid_add() {
227         local cfg="$1"
228
229         config_get networkid "$cfg" networkid
230         [ -n "$networkid" ] || return 0
231
232         config_get remoteid "$cfg" remoteid
233         [ -n "$remoteid" ] || return 0
234
235         xappend "--dhcp-remoteid=$networkid,$remoteid"
236
237         config_get_bool force "$cfg" force 0
238
239         dhcp_option_add "$cfg" "$networkid" "$force"
240 }
241
242 dhcp_circuitid_add() {
243         local cfg="$1"
244
245         config_get networkid "$cfg" networkid
246         [ -n "$networkid" ] || return 0
247
248         config_get circuitid "$cfg" circuitid
249         [ -n "$circuitid" ] || return 0
250
251         xappend "--dhcp-circuitid=$networkid,$circuitid"
252
253         config_get_bool force "$cfg" force 0
254
255         dhcp_option_add "$cfg" "$networkid" "$force"
256 }
257
258 dhcp_userclass_add() {
259         local cfg="$1"
260
261         config_get networkid "$cfg" networkid
262         [ -n "$networkid" ] || return 0
263
264         config_get userclass "$cfg" userclass
265         [ -n "$userclass" ] || return 0
266
267         xappend "--dhcp-userclass=$networkid,$userclass"
268
269         config_get_bool force "$cfg" force 0
270
271         dhcp_option_add "$cfg" "$networkid" "$force"
272 }
273
274 dhcp_vendorclass_add() {
275         local cfg="$1"
276
277         config_get networkid "$cfg" networkid
278         [ -n "$networkid" ] || return 0
279
280         config_get vendorclass "$cfg" vendorclass
281         [ -n "$vendorclass" ] || return 0
282
283         xappend "--dhcp-vendorclass=$networkid,$vendorclass"
284
285         config_get_bool force "$cfg" force 0
286
287         dhcp_option_add "$cfg" "$networkid" "$force"
288 }
289
290 dhcp_host_add() {
291         local cfg="$1"
292
293         config_get_bool force "$cfg" force 0
294
295         config_get networkid "$cfg" networkid
296         [ -n "$networkid" ] && dhcp_option_add "$cfg" "$networkid" "$force"
297
298         config_get name "$cfg" name
299         config_get ip "$cfg" ip
300         [ -n "$ip" -o -n "$name" ] || return 0
301
302         config_get_bool dns "$cfg" dns 0
303         [ "$dns" = "1" -a -n "$ip" -a -n "$name" ] && {
304                 echo "$ip $name${DOMAIN:+.$DOMAIN}" >> $HOSTFILE
305         }
306
307         config_get mac "$cfg" mac
308         if [ -n "$mac" ]; then
309                 # --dhcp-host=00:20:e0:3b:13:af,192.168.0.199,lap
310                 macs=""
311                 for m in $mac; do append macs "$m" ","; done
312         else
313                 # --dhcp-host=lap,192.168.0.199
314                 [ -n "$name" ] || return 0
315                 macs="$name"
316                 name=""
317         fi
318
319         config_get tag "$cfg" tag
320
321         config_get_bool broadcast "$cfg" broadcast 0
322         [ "$broadcast" = "0" ] && broadcast=
323
324         xappend "--dhcp-host=$macs${networkid:+,net:$networkid}${broadcast:+,set:needs-broadcast}${tag:+,set:$tag}${ip:+,$ip}${name:+,$name}"
325 }
326
327 dhcp_tag_add() {
328         local cfg="$1"
329
330         tag="$cfg"
331
332         [ -n "$tag" ] || return 0
333
334         config_get_bool force "$cfg" force 0
335         [ "$force" = "0" ] && force=
336
337         config_get option "$cfg" dhcp_option
338         for o in $option; do
339                 xappend "--dhcp-option${force:+-force}=tag:$tag,$o"
340         done
341 }
342
343 dhcp_mac_add() {
344         local cfg="$1"
345
346         config_get networkid "$cfg" networkid
347         [ -n "$networkid" ] || return 0
348
349         config_get mac "$cfg" mac
350         [ -n "$mac" ] || return 0
351
352         xappend "--dhcp-mac=$networkid,$mac"
353
354         dhcp_option_add "$cfg" "$networkid"
355 }
356
357 dhcp_boot_add() {
358         local cfg="$1"
359
360         config_get networkid "$cfg" networkid
361
362         config_get filename "$cfg" filename
363         [ -n "$filename" ] || return 0
364
365         config_get servername "$cfg" servername
366         [ -n "$servername" ] || return 0
367
368         config_get serveraddress "$cfg" serveraddress
369         [ -n "$serveraddress" ] || return 0
370
371         xappend "--dhcp-boot=${networkid:+net:$networkid,}$filename,$servername,$serveraddress"
372
373         config_get_bool force "$cfg" force 0
374
375         dhcp_option_add "$cfg" "$networkid" "$force"
376 }
377
378
379 dhcp_add() {
380         local cfg="$1"
381         config_get net "$cfg" interface
382         [ -n "$net" ] || return 0
383
384         config_get dhcpv4 "$cfg" dhcpv4
385         [ "$dhcpv4" != "disabled" ] || return 0
386
387         config_get networkid "$cfg" networkid
388         [ -n "$networkid" ] || networkid="$net"
389
390         network_get_subnet subnet "$net" || return 0
391         network_get_device ifname "$net" || return 0
392         network_get_protocol proto "$net" || return 0
393
394         [ "$cachelocal" = "0" ] && network_get_dnsserver dnsserver "$net" && {
395                 DNS_SERVERS="$DNS_SERVERS $dnsserver"
396         }
397
398         append_bool "$cfg" ignore "--no-dhcp-interface=$ifname" && return 0
399
400         # Do not support non-static interfaces for now
401         [ static = "$proto" ] || return 0
402
403         # Override interface netmask with dhcp config if applicable
404         config_get netmask "$cfg" netmask "${subnet##*/}"
405
406         #check for an already active dhcp server on the interface, unless 'force' is set
407         config_get_bool force "$cfg" force 0
408         [ $force -gt 0 ] || dhcp_check "$ifname" || return 0
409
410         config_get start "$cfg" start
411         config_get limit "$cfg" limit
412         config_get leasetime "$cfg" leasetime
413         config_get options "$cfg" options
414         config_get_bool dynamicdhcp "$cfg" dynamicdhcp 1
415
416         leasetime="${leasetime:-12h}"
417         start="$(dhcp_calc "${start:-100}")"
418         limit="${limit:-150}"
419         [ "$limit" -gt 0 ] && limit=$((limit-1))
420         eval "$(ipcalc.sh "${subnet%%/*}" $netmask $start $limit)"
421         if [ "$dynamicdhcp" = "0" ]; then END="static"; fi
422         xappend "--dhcp-range=$networkid,$START,$END,$NETMASK,$leasetime${options:+ $options}"
423
424         dhcp_option_add "$cfg" "$networkid"
425 }
426
427 dhcp_option_add() {
428         local cfg="$1"
429         local networkid="$2"
430         local force="$3"
431
432         [ "$force" = "0" ] && force=
433
434         config_get dhcp_option "$cfg" dhcp_option
435         for o in $dhcp_option; do
436                 xappend "--dhcp-option${force:+-force}=${networkid:+$networkid,}$o"
437         done
438
439 }
440
441 dhcp_domain_add() {
442         local cfg="$1"
443         local ip name names record
444
445         config_get names "$cfg" name "$2"
446         [ -n "$names" ] || return 0
447
448         config_get ip "$cfg" ip "$3"
449         [ -n "$ip" ] || return 0
450
451         for name in $names; do
452                 record="${record:+$record }$name"
453         done
454
455         echo "$ip $record" >> $HOSTFILE
456 }
457
458 dhcp_srv_add() {
459         local cfg="$1"
460
461         config_get srv "$cfg" srv
462         [ -n "$srv" ] || return 0
463
464         config_get target "$cfg" target
465         [ -n "$target" ] || return 0
466
467         config_get port "$cfg" port
468         [ -n "$port" ] || return 0
469
470         config_get class "$cfg" class
471         config_get weight "$cfg" weight
472
473         local service="$srv,$target,$port${class:+,$class${weight:+,$weight}}"
474
475         xappend "--srv-host=$service"
476 }
477
478 dhcp_mx_add() {
479         local cfg="$1"
480         local domain relay pref
481
482         config_get domain "$cfg" domain
483         [ -n "$domain" ] || return 0
484
485         config_get relay "$cfg" relay
486         [ -n "$relay" ] || return 0
487
488         config_get pref "$cfg" pref 0
489
490         local service="$domain,$relay,$pref"
491
492         xappend "--mx-host=$service"
493 }
494
495 dhcp_cname_add() {
496         local cfg="$1"
497         local cname target
498
499         config_get cname "$cfg" cname
500         [ -n "$cname" ] || return 0
501
502         config_get target "$cfg" target
503         [ -n "$target" ] || return 0
504
505         xappend "--cname=${cname},${target}"
506 }
507
508 dhcp_hostrecord_add() {
509         local cfg="$1"
510         local names addresses record val
511
512         config_get names "$cfg" name "$2"
513         if [ -z "$names" ]; then
514                 return 0
515         fi
516
517         config_get addresses "$cfg" ip "$3"
518         if [ -z "$addresses" ]; then
519                 return 0
520         fi
521
522         for val in $names $addresses; do
523                 record="${record:+$record,}$val"
524         done
525
526         xappend "--host-record=$record"
527 }
528
529 service_triggers()
530 {
531         procd_add_reload_trigger "dhcp"
532 }
533
534 boot() {
535         # Will be launched through hotplug
536         return 0
537 }
538
539 start_service() {
540         include /lib/functions
541
542         config_load dhcp
543
544         procd_open_instance
545         procd_set_param command $PROG -C $CONFIGFILE -k
546         procd_set_param file $CONFIGFILE
547         procd_set_param respawn
548         procd_close_instance
549
550         # before we can call xappend
551         mkdir -p $(dirname $CONFIGFILE)
552
553         echo "# auto-generated config file from /etc/config/dhcp" > $CONFIGFILE
554         echo "# auto-generated config file from /etc/config/dhcp" > $HOSTFILE
555
556         # if we did this last, we could override auto-generated config
557         [ -f /etc/dnsmasq.conf ] && {
558                 xappend "--conf-file=/etc/dnsmasq.conf"
559         }
560
561         args=""
562         config_foreach dnsmasq dnsmasq
563         config_foreach dhcp_host_add host
564         echo >> $CONFIGFILE
565         config_foreach dhcp_boot_add boot
566         config_foreach dhcp_mac_add mac
567         config_foreach dhcp_tag_add tag
568         config_foreach dhcp_vendorclass_add vendorclass
569         config_foreach dhcp_userclass_add userclass
570         config_foreach dhcp_circuitid_add circuitid
571         config_foreach dhcp_remoteid_add remoteid
572         config_foreach dhcp_subscrid_add subscrid
573         config_foreach dhcp_domain_add domain
574         config_foreach dhcp_hostrecord_add hostrecord
575
576         # add own hostname
577         local lanaddr
578         [ $ADD_LOCAL_HOSTNAME -eq 1 ] && network_get_ipaddr lanaddr "lan" && {
579                 local hostname="$(uci_get system @system[0] hostname OpenWrt)"
580                 dhcp_domain_add "" "$hostname" "$lanaddr"
581         }
582
583         echo >> $CONFIGFILE
584         config_foreach dhcp_srv_add srvhost
585         config_foreach dhcp_mx_add mxhost
586         echo >> $CONFIGFILE
587
588         config_get odhcpd_is_active odhcpd maindhcp
589         if [ "$odhcpd_is_active" != "1" ]; then
590                 config_foreach dhcp_add dhcp
591         fi
592
593         echo >> $CONFIGFILE
594         config_foreach dhcp_cname_add cname
595         echo >> $CONFIGFILE
596
597         rm -f /tmp/resolv.conf
598         [ $ADD_LOCAL_DOMAIN -eq 1 ] && [ -n "$DOMAIN" ] && {
599                 echo "search $DOMAIN" >> /tmp/resolv.conf
600         }
601         DNS_SERVERS="$DNS_SERVERS 127.0.0.1"
602         for DNS_SERVER in $DNS_SERVERS ; do
603                 echo "nameserver $DNS_SERVER" >> /tmp/resolv.conf
604         done
605 }
606
607 reload_service() {
608         rc_procd start_service "$@"
609         return 0
610 }
611
612 stop_service() {
613         [ -f /tmp/resolv.conf ] && {
614                 rm -f /tmp/resolv.conf
615                 ln -s /tmp/resolv.conf.auto /tmp/resolv.conf
616         }
617         rm -f /var/run/dnsmasq.*.dhcp
618 }