#!/bin/bash #set -e #set -x # not added (yet): # Skip any leases that also are a static host # need DOMAINNAME value eval $(/usr/local/bin/readhash /var/ipfire/main/settings) dhcpCREstatus=$1 clientIP=$2 clientName=$3 dhcpLeasesFile="/etc/unbound/dhcp-leases2.conf" touch "${dhcpLeasesFile}" # change to lower case (if needed) #clientName=$( echo "${clientName}" | tr '[:upper:]' '[:lower:]' ) # create reverse IP reverseIP=$( echo ${clientIP} | awk -F. '{print $4"."$3"." $2"."$1}' ) # create "A" record and "PTR" record for unbound aRecord="local-data: \"${clientName}.${DOMAINNAME} 60 IN A ${clientIP}"\" ptrRecord="local-data: \"${reverseIP}.in-addr.arpa 60 IN PTR ${clientName}.${DOMAINNAME}"\" aRecordCount=$( /bin/grep --fixed-strings --count "${aRecord}" "${dhcpLeasesFile}" ) ptrRecordCount=$( /bin/grep --fixed-strings --count "${ptrRecord}" "${dhcpLeasesFile}" ) clientIPcount=$( /bin/grep --count "\s${clientIP}\b" "${dhcpLeasesFile}" ) clientNameCount=$( /bin/grep --fixed-strings --count "${clientName}.${DOMAINNAME}" "${dhcpLeasesFile}" ) # clientNameCount can be deleted. currently used for debug /usr/bin/logger --tag dhcpS \ "aRecordCount=${aRecordCount}, ptrRecordCount=${ptrRecordCount}, clientIPcount=${clientIPcount}, clientNameCount=${clientNameCount}" case "${dhcpCREstatus}" in commit) # does A record and PTR record already exist? if (( $aRecordCount == 0 )) && (( $ptrRecordCount == 0 )) ; then echo "${aRecord}" >> "${dhcpLeasesFile}" echo "${ptrRecord}" >> "${dhcpLeasesFile}" /usr/bin/logger --tag dhcpC "Record A and PTR added to unbound" fi ;; release) /usr/bin/logger --tag dhcpR "case = release" # delete lines based on IP address (and not names) if (( $clientIPcount > 0 )) ; then /usr/bin/logger --tag dhcpE "IP address exists" /bin/sed --in-place -e "/\s${clientIP}\b/d" -e "/${reverseIP}/d" "${dhcpLeasesFile}" clientIPcount=$( /bin/grep --count "\s${clientIP}\b" "${dhcpLeasesFile}" ) /usr/bin/logger --tag dhcpE "IP & reverse IP address removed (${clientIPcount})" fi ;; expiry) /usr/bin/logger --tag dhcpE "case = expiry" # this section is the same as "release" since I don't understand the differences # delete lines based on IP address (and not names) if (( $clientIPcount > 0 )) ; then /usr/bin/logger --tag dhcpE "IP address exists" /bin/sed --in-place -e "/\s${clientIP}\b/d" -e "/${reverseIP}/d" "${dhcpLeasesFile}" clientIPcount=$( /bin/grep --count "\s${clientIP}\b" "${dhcpLeasesFile}" ) clientNameCount=$( /bin/grep --fixed-strings --count "${clientName}.${DOMAINNAME}" "${dhcpLeasesFile}" ) /usr/bin/logger --tag dhcpE "IP & reverse IP address removed (A=${clientIPcount} PTR=${clientNameCount})" fi ;; *) /usr/bin/logger --tag dhcp "dhcpEvent script: case = no status" exit ;; esac exit