Configuração do dhcp trabalhando com o Bind DLZ no Samba 4

Vamos instalar o dhcpd

aptitude install dhcp3-server -y

Vamos criar um usuário para o dhcp

samba-tool user create dhcp dhcp@134* --description="Unprivileged user for DNS updates via DHCP server"

Agora vamos inserir o dhcp no grupo do bind

samba-tool group addmembers DnsAdmins dhcp

Agora vamos tirar a opção da senha expirar

samba-tool user setexpiry dhcp --noexpiry

Agora vamos criar o script que vai passar os dados para ser criado o registro no dns

vim /etc/dhcp/update.sh
#!/bin/bash
# Variables
DOMAIN="douglasqsantos.com.br"
NAMESERVER="pdc.${DOMAIN}"
ZONE="${DOMAIN}"
USER="dhcp"
PASS="dhcp@134*"
REV="1.168.192.in-addr.arpa"
SAMBA_TOOL="/usr/local/samba/bin/samba-tool"
LOGGER="/usr/bin/logger"
ACTION=$1
IP=$2
HNAME=$3
IP_REV=$(echo ${IP} | cut -d '.' -f 4)


#Function for manipulate the hosts on dns server
add_host(){
${LOGGER} -s -p daemon.info -t dhcpd "Adding A record for host $HNAME with IP $IP to zone $ZONE on server $NAMESERVER"
${LOGGER} -s -p daemon.info -t dhcpd "Adding PTR record for ${IP_REV} with hostname $HNAME  to zone $REV on server $NAMESERVER"
${SAMBA_TOOL} dns add ${NAMESERVER} ${ZONE} ${HNAME} A ${IP} -U${USER} --password=${PASS}
${SAMBA_TOOL} dns add ${NAMESERVER} ${REV} ${IP_REV} PTR ${HNAME}.${DOMAIN} -U${USER} --password=${PASS}
}

del_host(){
${LOGGER} -s -p daemon.info -t dhcpd "Removing A record for host $HNAME with IP $IP to zone $ZONE on server $NAMESERVER"
${LOGGER} -s -p daemon.info -t dhcpd "Removing PTR record for ${IP_REV} with hostname $HNAME  to zone $REV on server $NAMESERVER"
${SAMBA_TOOL} dns delete ${NAMESERVER} ${ZONE} ${HNAME} A ${IP} -U${USER} --password=${PASS}
${SAMBA_TOOL} dns delete ${NAMESERVER} ${REV} ${IP_REV} PTR ${HNAME}.${DOMAIN} -U${USER} --password=${PASS}
}

update_host(){
del_host
add_host
}

case "$1" in
add)
add_host
;;
delete)
del_host
;;
*)
echo "opcao validas: (add|del)"
;;
esac

Agora vamos ajustar as permissões do script

chmod 755 /etc/dhcp/update.sh

Agora vamos criar o arquivo de controle do dhcpd

vim /etc/dhcp/dhcpd.conf
# Internal subnet
subnet 192.168.1.0 netmask 255.255.255.0 {
  range 192.168.1.100 192.168.1.199;
  option subnet-mask 255.255.255.0;
  option routers 192.168.1.40;
  option domain-name "douglasqsantos.com.br";
  option domain-name-servers 192.168.1.49;
  option broadcast-address 192.168.1.255;
  default-lease-time 28800;
  max-lease-time 43200;
  authoritative;

  on commit {
    set ClientIP = binary-to-ascii(10, 8, ".", leased-address);
    set ClientName = pick-first-value(option host-name, host-decl-name);
    execute("/etc/dhcp/update.sh", "add", ClientIP, ClientName);
  }

  on release {
    set ClientIP = binary-to-ascii(10, 8, ".", leased-address);
    set ClientName = pick-first-value(option host-name, host-decl-name);
    execute("/etc/dhcp/update.sh", "delete", ClientIP, ClientName);
  }

    on expiry {
    set ClientIP = binary-to-ascii(10, 8, ".", leased-address);
    set ClientName = pick-first-value(option host-name, host-decl-name);
    execute("/etc/dhcp/update.sh", "delete", ClientIP, ClientName);
  }
}

Agora vamos ajustar por qual interface o dhcp vai responder

vim /etc/default/isc-dhcp-server
[...]
INTERFACES="eth0"

Agora precisamos reiniciar o dhcpd

/etc/init.d/isc-dhcp-server restart