Reply To: allow DNS dynamic updates from DHCP server

Home Page Forums Network Management ZeroShell allow DNS dynamic updates from DHCP server Reply To: allow DNS dynamic updates from DHCP server

#46313

donzaucker
Member

I have write a porting in bash of the perl script:
No warranty!

#!/bin/bash
#############################################################################
#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#Write by Alessandro Cartatone
#Porting in bash of perl script dhcp2ldap by Travis Groth for use in Zeroshell distribution
#############################################################################

lease_file=”/Database/var/register/system/dhcp/dhcpd.leases”
log_file=”/Database/dhcp2ldap.log” # insert a path to write log file
domain=”yourdomain.org”
reverse=”0.168.192.in-addr.arpa” #your reverse domain
FORWARD_BASE=”,dc=yourdomain,dc=org,ou=DNS,dc=yourdomain,dc=org”
REVERSE_BASE=”,dc=0,dc=168,dc=192,dc=in-addr,dc=arpa,ou=DNS,dc=yourdomain,dc=org”
USER=”cn=Manager,dc=yourdomain,dc=org” #default user manager of zeroshell ldap – verify in /etc/openldap/slapd.conf
PASSWORD=”xxxxx” #admin password – verify in /etc/openldap/slapd.conf
UPDATE_TIME=30 # In Seconds
AUTO_VERIFY=0
check_count=0
result=0

usage()
{

echo “dhcp2ldap.sh : Dynamic DNS Updates for the Bind9 LDAP backend
Copyright 2012 Alessandro Cartatone under the GNU GPL based on dhcp2ldapd Copyright 2005 Travis Groth
To demonize: nohup /dhcp2ldap.sh 0/dev/null 2>&1 &
Please edit the config variables before running!”

}

parse()
{
lease_found=0
counter=0

while read line
do

if [ “$(echo “$line” | awk ‘/lease/ {print $1}’)” = “lease” ]
then
ip=”$(echo “$line” | awk ‘/lease/ {print $2}’)”
lease_found=1
fi

if [ $lease_found ]
then
if [ “$(echo “$line” | awk ‘/client-hostname/ {print $1}’)” = “client-hostname” ]
then
hostname=”$(echo “$line” | awk ‘/client-hostname/ {print $2}’)”
hostname=”$(echo “$hostname” | sed “s/”//g”)”
hostname=”$(echo “$hostname” | sed “s/;//g”)”
hostnamearray[counter]=$hostname
iparray[counter]=$ip
lease_found=0
counter=$counter+1
fi

if [ “$(echo “$line” | awk ‘/lease/ {print $1}’)” = “}” ]
then
lease_found=0
fi

fi

done < $lease_file

}

addldapforward()
{
#add forward zone
rm -f /tmp/addhost.ldif 2>&1 >/dev/null
echo “dn: relativeDomainName=”$2$FORWARD_BASE > /tmp/addhost.ldif
echo “objectClass: top” >> /tmp/addhost.ldif
echo “objectClass: dNSZone” >> /tmp/addhost.ldif
echo “relativeDomainName: “$2 >> /tmp/addhost.ldif
echo “dNSTTL: 7200” >> /tmp/addhost.ldif
echo “zoneName: “$domain >> /tmp/addhost.ldif
echo “aRecord: “$1 >> /tmp/addhost.ldif
ldapadd -x -D $USER -w $PASSWORD -f /tmp/addhost.ldif 2>&1 >/dev/null
}

addldapreverse()
{
#add reverse zone
rm -f /tmp/addhost.ldif 2>&1 >/dev/null
echo “dn: relativeDomainName=”$(echo “$1” | cut -d’.’ -f4)$REVERSE_BASE > /tmp/addhost.ldif
echo “objectClass: top” >> /tmp/addhost.ldif
echo “objectClass: dNSZone” >> /tmp/addhost.ldif
echo “relativeDomainName: “$(echo “$1” | cut -d’.’ -f4) >> /tmp/addhost.ldif
echo “dNSTTL: 7200” >> /tmp/addhost.ldif
echo “zoneName: “$reverse >> /tmp/addhost.ldif
echo “pTRRecord: “$2″.”$domain”.” >> /tmp/addhost.ldif
ldapadd -x -D $USER -w $PASSWORD -f /tmp/addhost.ldif 2>&1 >/dev/null
}

deleteldapforward()
{

ldapdelete -x -D $USER -w $PASSWORD “relativeDomainName=”$1$FORWARD_BASE 2>&1 >/dev/null

}

deleteldapreverse()
{

ldapdelete -x -D $USER -w $PASSWORD “relativeDomainName=”$(echo “$1” | cut -d’.’ -f4)$REVERSE_BASE 2>&1 >/dev/null

}

do_stuff()
{
count=0
readd=0
notfound=”found:”
for i in “${iparray[@]}”
do
lookup=”$(host “${hostnamearray[count]}””.””$domain”)”
lookup=”$(echo “$lookup” | awk ‘{print $4}’)”
if [ “$lookup” = “${iparray[count]}” ]
then
count=$count+1
continue
fi

#when it’s “found:” no record in ldap
if “$lookup” != “$notfound” && “$lookup” != “${iparray[count]}”
then
#delete record in ldap
echo “$(date ‘+%F %T’)” “delete record in ldap” $lookup “${iparray[count]}” ${hostnamearray[count]} >> “$log_file”
deleteldapforward ${hostnamearray[count]}
deleteldapreverse “${iparray[count]}”
readd=1
fi

if | $readd
then
echo “$(date ‘+%F %T’)” “add record in ldap” “${iparray[count]}” ${hostnamearray[count]} >> “$log_file”
addldapforward “${iparray[count]}” ${hostnamearray[count]}
addldapreverse “${iparray[count]}” ${hostnamearray[count]}
fi

count=$count+1
done

}

changed()
{
result=0
curstat=”$(stat -c %y “$lease_file”)”

if [ $AUTO_VERIFY ]
then
check_count=$check_count+1
fi

if [ “$oldstat” != “$curstat” ] || [ $check_count = $AUTO_VERIFY -a $AUTO_VERIFY ]
then
oldstat=”$curstat”
check_count=0
result=1
else
result=0
fi

}

if [ “$1” = “-h” ]
then
usage
exit
else

while :
do
changed
if [ $result -eq 1 ]
then
parse
do_stuff
fi
sleep $UPDATE_TIME
done

fi