1 #!/bin/sh /etc/rc.common
10 lease_file
=/tmp
/dhcpd.leases
11 config_file
=/tmp
/run
/dhcpd.conf
14 conf_local_file
=$dyndir/named.conf.
local
16 session_key_name
=local-ddns
17 session_key_file
=/var
/run
/named
/session.key
21 local multiplier number suffix
23 suffix
="${timestring//[0-9 ]}"
24 number
="${timestring%%$suffix}"
25 [ "$number$suffix" != "$timestring" ] && return 1
46 echo $
(( number
* multiplier
))
52 echo "$arg" |
sed -e 's/^ *//' -e 's/ *$//'
56 local octets
="$(echo "${1%%/*}" | cut -d. -f1)"
58 [ "$octets" = "10" ] && { echo "$octets"; return; }
60 octets
="$(echo "${1%%/*}" | cut -d. -f1-2)"
63 172.1[6789]|
172.2[0-9]|
172.3[01]|
192.168)
70 [ -n "$(named-checkconf -px \
71 | sed -r -ne '1N; N; /^\tlisten-on-v6 ?\{\n\t\t"none
";\n\t\};$/{ p; q; }; D')" ]
74 # duplicated from dnsmasq init script
77 local hex
="${2#0x}" # strip optional "0x" prefix
79 if [ -n "${hex//[0-9a-fA-F]/}" ]; then
80 # is invalid hex literal
84 # convert into host id
87 $(((0x$hex >> 16) % 65536)) \
96 /^\d+\.\d+\.\d+\.\d+$/ { print "ip\n"; next; }
97 /^(true|false)$/ { print "bool\n"; next; }
98 /^\d+$/ { print "integer\n"; next; }
99 /^"[^"]*"$/ { print "string\n"; next; }
100 /^[0-9a-fA-F]{2,2}(:[0-9a-fA-F]{2,2})*$/ { print "string\n"; next; }
101 { print "other\n"; next; }
106 local lhs
="$1" family
="$2" type="$3"
109 [ $dynamicdns -eq 1 ] && \
110 echo -e "$PREFIX" "$lhs $family $type $@\nsend" >> $dyn_file
116 echo "$arg" |
sed -e 's/\./, /g'
120 local str
="$1" delim
="$2"
121 local frag result
="" IFS
="$delim"
124 result
="$frag${result:+$delim}$result"
130 create_empty_zone
() {
133 if [ ! -f $dyndir/db.
"$zone" ]; then
134 cp -p /etc
/bind
/db.empty
$dyndir/db.
"$zone"
135 chmod g
+w
$dyndir/db.
"$zone"
136 chgrp
bind $dyndir/db.
"$zone"
141 local tuple tuples
="$1"
145 for tuple
in $tuples; do
146 local network prefix router save octets compacted
148 tuple
="$(trim "$tuple")"
151 router
="$(trim "${tuple#${save} }")"
153 network
="$(trim "${save%/[0-9]*}")"
155 prefix
="$(trim "${save##${network}/}")"
157 octets
=$
((($prefix + 7) / 8))
158 compacted
="$(echo "$network" | cut -d. -f1-$octets)"
160 string
="${string:+, }$(explode "$prefix.
$compacted.
$router")"
163 echo " option classless-ipv4-route $string;"
166 append_dhcp_options
() {
169 # strip redundant "option:" prefix
170 tuple
="${tuple#option:}"
172 local tag
="${tuple%%,*}"
173 local values
="${tuple#$tag,}"
175 local formatted value
177 for value
in $values; do
178 # detect type of $value and quote if necessary
179 case $
(typeof
"$value") in
180 ip|bool|integer|string
)
186 formatted
="$formatted${formatted:+, }$value"
188 echo " option $tag $formatted;"
195 config_get cname
"$cfg" "cname"
196 [ -n "$cname" ] ||
return 0
197 config_get target
"$cfg" "target"
198 [ -n "$target" ] ||
return 0
200 update
"$cname.$domain." IN CNAME
"$target.$domain."
204 config_foreach static_cname_add cname
"$@"
207 static_domain_add
() {
209 local name ip ips revip
211 config_get name
"$cfg" "name"
212 [ -n "$name" ] ||
return 0
213 config_get ip
"$cfg" "ip"
214 [ -n "$ip" ] ||
return 0
218 revip
="$(rev_str "$ip" ".
")"
220 update
"$name.$domain." IN A
"$ip"
221 [ -n "$(rfc1918_prefix "$ip")" ] && \
222 update
"$revip.in-addr.arpa." IN PTR
"$name.$domain."
227 config_foreach static_domain_add domain
"$@"
230 static_mxhost_add
() {
232 local domain2 relay pref
234 config_get domain2
"$cfg" "domain"
235 [ -n "$domain2" ] ||
return 0
236 config_get relay
"$cfg" "relay"
237 [ -n "$relay" ] ||
return 0
238 config_get pref
"$cfg" "pref"
239 [ -n "$pref" ] ||
return 0
241 if [ "$domain2" = "@" ]; then
242 update
"$domain." IN MX
"$pref" "$relay.$domain."
244 update
"$domain2.$domain." IN MX
"$pref" "$relay.$domain."
249 config_foreach static_mxhost_add mxhost
"$@"
252 static_srvhost_add
() {
254 local srv target port priority weight
256 config_get srv
"$cfg" "srv"
257 [ -n "$srv" ] ||
return 0
258 config_get target
"$cfg" "target"
259 [ -n "$target" ] ||
return 0
260 config_get port
"$cfg" "port"
261 [ -n "$port" ] ||
return 0
262 config_get priority
"$cfg" "priority"
263 [ -n "$priority" ] ||
return 0
264 config_get weight
"$cfg" "weight"
265 [ -n "$weight" ] ||
return 0
267 update
"$srv.$domain." IN SRV
"$priority" "$weight" "$port" "$target.$domain"
271 config_foreach static_srvhost_add srvhost
"$@"
276 local broadcast hostid macn macs mac name ip ips revip leasetime
278 config_get macs
"$cfg" "mac"
279 [ -n "$macs" ] ||
return 0
280 config_get name
"$cfg" "name"
281 [ -n "$name" ] ||
return 0
282 config_get ip
"$cfg" "ip"
283 [ -n "$ip" ] ||
return 0
285 config_get_bool broadcast
"$cfg" "broadcast" 0
286 config_get dns
"$cfg" "dns"
287 config_get gateway
"$cfg" "gateway"
288 config_get leasetime
"$cfg" "leasetime"
289 if [ -n "$leasetime" ] ; then
290 leasetime
="$(time2seconds "$leasetime")"
291 [ "$?" -ne 0 ] && return 1
294 config_get hostid
"$cfg" "hostid"
295 if [ -n "$hostid" ] ; then
296 hex_to_hostid hostid
"$hostid" ||
return 1
305 local secname
="$name"
306 if [ $macn -gt 1 ] ; then
307 secname
="${name}-${mac//:}"
309 echo "host $secname {"
310 echo " hardware ethernet $mac;"
311 echo " fixed-address $ip;"
312 echo " option host-name \"$name\";"
313 if [ "$broadcast" -eq 1 ] ; then
314 echo " always-broadcast true;"
316 if [ -n "$leasetime" ] ; then
317 echo " default-lease-time $leasetime;"
318 echo " max-lease-time $leasetime;"
320 if [ -n "$hostid" ] ; then
321 echo " option dhcp-client-identifier $hostid;"
323 if [ -n "$dns" ] ; then
324 echo " option domain-name-servers $dns;"
326 if [ -n "$gateway" ] ; then
327 echo " option routers $gateway;"
329 config_list_foreach
"$cfg" "routes" append_routes
330 config_list_foreach
"$cfg" "dhcp_option" append_dhcp_options
336 revip
="$(rev_str "$ip" ".
")"
338 update
"$name.$domain." IN A
"$ip"
339 update
"$revip.in-addr.arpa." IN PTR
"$name.$domain."
344 config_foreach static_host_add
host "$@"
350 echo "subnet $NETWORK netmask $NETMASK {"
351 echo " range $START $END;"
352 echo " option subnet-mask $netmask;"
353 if [ "$BROADCAST" != "0.0.0.0" ] ; then
354 echo " option broadcast-address $BROADCAST;"
356 if [ "$dynamicdhcp" -eq 0 ] ; then
357 if [ "$authoritative" -eq 1 ] ; then
358 echo " deny unknown-clients;"
360 echo " ignore unknown-clients;"
363 if [ -n "$leasetime" ] ; then
364 echo " default-lease-time $leasetime;"
365 echo " max-lease-time $leasetime;"
367 echo " option routers $gateway;"
368 echo " option domain-name-servers $DNS;"
369 config_list_foreach
"$cfg" "routes" append_routes
370 config_list_foreach
"$cfg" "dhcp_option" append_dhcp_options
375 local cfg
="$1" synthesize
="$2"
376 local dhcp6range
="::"
377 local dynamicdhcp end gateway ifname ignore leasetime limit net netmask
378 local proto networkid start subnet
379 local IP NETMASK BROADCAST NETWORK PREFIX DNS START END
381 config_get_bool ignore
"$cfg" "ignore" 0
382 [ "$ignore" = "0" ] ||
return 0
384 config_get net
"$cfg" "interface"
385 [ -n "$net" ] ||
return 0
387 config_get start
"$cfg" "start"
388 [ -n "$start" ] ||
return 0
390 config_get limit
"$cfg" "limit"
391 [ -n "$limit" ] ||
return 0
393 network_get_subnet subnet
"$net" ||
return 0
394 network_get_device ifname
"$net" ||
return 0
395 network_get_protocol proto
"$net" ||
return 0
397 [ static
= "$proto" ] ||
return 0
399 local octets
="$(rfc1918_prefix "$subnet")"
401 [ -n "$octets" ] && rfc1918_nets
="$rfc1918_nets${rfc1918_nets:+ }$octets"
403 [ $synthesize -eq 0 ] && return
405 config_get_bool dynamicdhcp
"$cfg" "dynamicdhcp" 1
407 dhcp_ifs
="$dhcp_ifs $ifname"
409 eval "$(ipcalc.sh $subnet $start $limit)"
411 config_get netmask
"$cfg" "netmask" "$NETMASK"
412 config_get leasetime
"$cfg" "leasetime"
413 if [ -n "$leasetime" ] ; then
414 leasetime
="$(time2seconds "$leasetime")"
415 [ "$?" -ne 0 ] && return 1
418 if network_get_dnsserver dnsserver
"$net" ; then
419 for dnsserv
in $dnsserver ; do
420 DNS
="$DNS${DNS:+, }$dnsserv"
426 if ! network_get_gateway gateway
"$net" ; then
430 gen_dhcp_subnet
"$cfg"
434 local always_broadcast boot_unknown_clients log_facility
435 local default_lease_time max_lease_time
437 config_get_bool always_broadcast
"isc_dhcpd" "always_broadcast" 0
438 config_get_bool authoritative
"isc_dhcpd" "authoritative" 1
439 config_get_bool boot_unknown_clients
"isc_dhcpd" "boot_unknown_clients" 1
440 config_get default_lease_time
"isc_dhcpd" "default_lease_time" 3600
441 config_get max_lease_time
"isc_dhcpd" "max_lease_time" 86400
442 config_get log_facility
"isc_dhcpd" "log_facility"
444 config_get domain
"isc_dhcpd" "domain"
445 config_get_bool dynamicdns
"isc_dhcpd" dynamicdns
0
447 [ $always_broadcast -eq 1 ] && echo "always-broadcast true;"
448 [ $authoritative -eq 1 ] && echo "authoritative;"
449 [ $boot_unknown_clients -eq 0 ] && echo "boot-unknown-clients false;"
451 default_lease_time
="$(time2seconds "$default_lease_time")"
452 [ "$?" -ne 0 ] && return 1
453 max_lease_time
="$(time2seconds "$max_lease_time")"
454 [ "$?" -ne 0 ] && return 1
456 if [ $dynamicdns -eq 1 ]; then
457 create_empty_zone
"$domain"
461 for mynet
in $rfc1918_nets; do
462 mynet
="$(rev_str "$mynet" ".
")"
463 create_empty_zone
"$mynet.in-addr.arpa"
466 cat <<EOF > $conf_local_file
469 file "$dyndir/db.$domain";
471 grant $session_key_name zonesub any;
477 for mynet
in $rfc1918_nets; do
478 mynet
="$(rev_str "$mynet" ".
")"
479 cat <<EOF >> $conf_local_file
480 zone "$mynet.in-addr.arpa" {
482 file "$dyndir/db.$mynet.in-addr.arpa";
484 grant $session_key_name zonesub any;
491 /etc
/init.d
/named reload
495 ddns-domainname "$domain.";
496 ddns-update-style standard;
498 ignore client-updates;
500 update-static-leases on;
501 use-host-decl-names on;
502 update-conflict-detection off;
503 update-optimization off;
505 include "$session_key_file";
509 key $session_key_name;
514 for mynet
in $rfc1918_nets; do
515 mynet
="$(rev_str "$mynet" ".
")"
517 zone $mynet.in-addr.arpa. {
519 key $session_key_name;
526 if [ -n "$log_facility" ] ; then
527 echo "log-facility $log_facility;"
529 echo "default-lease-time $default_lease_time;"
530 echo "max-lease-time $max_lease_time;"
532 [ -n "$domain" ] && echo "option domain-name \"$domain\";"
534 echo -e "\n# additional codes\noption classless-ipv4-route code 121 = array of { unsigned integer 8 };\n"
536 rm -f /tmp
/resolv.conf
537 echo "# This file is generated by the DHCPD service" > /tmp
/resolv.conf
538 [ -n "$domain" ] && echo "domain $domain" >> /tmp
/resolv.conf
539 echo "nameserver 127.0.0.1" >> /tmp
/resolv.conf
550 local domain dhcp_ifs authoritative dynamicdns
552 if [ -n "$DHCPD_BOOT" ] ; then
556 if [ ! -e $lease_file ] ; then
560 if [ -e "/etc/dhcpd.conf" ] ; then
561 config_file
="/etc/dhcpd.conf"
563 .
/lib
/functions
/network.sh
565 local dyn_file
=$
(mktemp
-u /tmp
/dhcpd.XXXXXX
)
569 local rfc1918_nets
=""
571 # alas we have to make 2 passes...
572 config_foreach dhcpd_add dhcp
0
574 rfc1918_nets
="$(echo "$rfc1918_nets" | tr ' ' $'\n' | sort | uniq | tr $'\n' ' ')"
576 general_config
> $config_file
578 if [ $dynamicdns -eq 1 ]; then
579 cat <<EOF > $dyn_file
580 ; Generated by /etc/init.d/dhcpd at $(date)
589 config_foreach dhcpd_add dhcp
1 >> $config_file
591 static_hosts
>> $config_file
593 static_cnames
>> $config_file
595 static_domains
>> $config_file
597 static_mxhosts
>> $config_file
599 static_srvhosts
>> $config_file
601 if [ $dynamicdns -eq 1 ]; then
606 nsupdate
-l -v $args $dyn_file
612 [ -z "$dhcp_ifs" ] && return 0
616 procd_set_param
command $PROG -q -f -cf $config_file -lf $lease_file $dhcp_ifs
621 rc_procd start_service
"$@"
622 procd_send_signal dhcpd
"$@"
625 add_interface_trigger
() {
629 config_get trigger
"$cfg" interface
630 config_get_bool ignore
"$cfg" ignore
0
632 if [ -n "$trigger" -a $ignore -eq 0 ] ; then
633 procd_add_reload_interface_trigger
"$trigger"
638 if [ -n "$DHCPD_BOOT" ] ; then
639 # Make the first start robust to slow interfaces; wait a while
640 procd_add_raw_trigger
"interface.*.up" 5000 /etc
/init.d
/dhcpd restart
643 # reload with normal parameters
644 procd_add_reload_trigger
"network" "dhcp"
646 config_foreach add_interface_trigger dhcp