|
#!/bin/bash
|
|
### BEGIN INIT INFO
|
|
# Provides: dhcp-server-denegar
|
|
# Required-Start: $remote_fs $all
|
|
# Required-Stop:
|
|
# Should-Start: $network
|
|
# Should-Stop:
|
|
# Default-Start: 2
|
|
# Default-Stop:
|
|
# Short-Description: Genera el fichero dhcpd.conf, denegando la entrada a los no permitidos.
|
|
# Description: Controla el acceso al sistema. Los equipos denegados no tomarán ip
|
|
### END INIT INFO
|
|
|
|
# ***********************************************************************************************************
|
|
# Antonio J. Abasolo Sierra (Dic-2012)
|
|
#
|
|
# Controla el acceso al sistema, filtrando los equipos a los que el servidor dhcp del centro ofrecerá ip,
|
|
# y por lo tanto podrán navegar.
|
|
# Se base en el sistema de lista blanca (mantenida por el usuario),
|
|
# y lista negra (obtenida por filtrado de entradas del fichero del servidor dhcp: dhcpd.leases)
|
|
#
|
|
# La Lista_Blanca de equipos que dejamos conectarse la mantenemos nosotros, y debe tener este formato:
|
|
# MAC,EQUIPO[,IP][,NOMBRE]
|
|
# EQUIPO --> nombre personal para conocer al equipo.
|
|
# [,IP] --> Se rellenará automáticamente por el script.
|
|
# [,NOMBRE] --> Se rellenará automáticamente por el script (si lo tiene).
|
|
#
|
|
# Para dejarlo operativo colocaremos un crontab que ejecute este script cada 5 min. (por ej.)
|
|
# ***********************************************************************************************************
|
|
|
|
FILE_CONF=/etc/dhcp/dhcpd.conf
|
|
FILE_LEASES=/var/lib/dhcp/dhcpd.leases
|
|
LISTA_NEGRA=/etc/dhcp/hosts.deny
|
|
LISTA_BLANCA=/etc/dhcp/hosts.accept
|
|
SERVICIO=isc-dhcp-server
|
|
|
|
Crear_BaseConf() {
|
|
if [ ! -f $FILE_CONF.base ]; then
|
|
cat $FILE_CONF > $FILE_CONF.base
|
|
fi
|
|
}
|
|
|
|
Configuracion_Base() {
|
|
Crear_BaseConf
|
|
if [ -f $FILE_CONF.base ]; then
|
|
cat $FILE_CONF.base > $FILE_CONF
|
|
fi
|
|
}
|
|
|
|
Configuracion_Base2() {
|
|
echo "
|
|
# ------------------------------------------------
|
|
ldap-server "ldap";
|
|
ldap-port 389;
|
|
ldap-base-dn "dc=instituto,dc=extremadura,dc=es";
|
|
ldap-dhcp-server-cn "ldap";
|
|
ldap-method dynamic;
|
|
#ldap-method static;
|
|
ddns-update-style none;
|
|
# ------------------------------------------------
|
|
" > $FILE_CONF
|
|
}
|
|
|
|
Obtener_Leases() {
|
|
# Creamos un fichero de leases con una entrada por línea: MAC,IP,NOMBRE
|
|
echo " - Obtenemos 'pcs.leases': equipos conectados vía dhcp-server ..."
|
|
cat $FILE_LEASES | grep -v '#' > pcs.leases
|
|
rm -f pcs 2>/dev/null ; touch pcs
|
|
cat pcs.leases | while read linea ; do
|
|
KEY=`echo $linea | awk '{print $1}'`
|
|
case "$KEY" in
|
|
lease) IpPC=`echo $linea | awk '{print $2}'`
|
|
;;
|
|
hardware) MacPC=`echo $linea | awk '{print $3}' | cut -f1 -d';' | tr A-Z a-z`
|
|
;;
|
|
client-hostname) NombrePC=`echo $linea | awk -F'"' '{print $2}'`
|
|
;;
|
|
'}')
|
|
if [ $MacPC ]; then
|
|
esta=`grep -i $MacPC pcs` 2>/dev/null
|
|
if [ ! $esta ]; then
|
|
echo "$MacPC,$IpPC,$NombrePC" >> pcs
|
|
fi
|
|
NombrePc=""
|
|
fi
|
|
;;
|
|
"")
|
|
;;
|
|
esac
|
|
done
|
|
cat pcs | uniq | sort -t, -u -k2 > pcs.leases
|
|
rm -f pcs
|
|
}
|
|
|
|
Obtener_ListaNegra() {
|
|
# Recorremos el fichero de leases obtenido, y lo filtramos con la lista-blanca
|
|
echo " - Creamos $LISTA_NEGRA: lista de equipos a denegar (lista negra)"
|
|
# rm -f $LISTA_NEGRA 2>/dev/null ; touch $LISTA_NEGRA
|
|
cat pcs.leases | while read linea; do
|
|
MAC=`echo $linea | cut -f1 -d,`
|
|
IP=`echo $linea | cut -f2 -d,`
|
|
PC=`echo $linea | cut -f3 -d,`
|
|
|
|
ESTA=`grep -i $MAC $LISTA_BLANCA` 2>/dev/null
|
|
if [ ! $ESTA ]; then
|
|
ESTA=`grep -i $MAC $LISTA_NEGRA` 2>/dev/null
|
|
if [ ! $ESTA ]; then
|
|
echo "$MAC,$PC,$IP" >> $LISTA_NEGRA
|
|
fi
|
|
else
|
|
EQUIPO=`grep ^$MAC $LISTA_BLANCA | cut -f1-2 -d,`
|
|
grep -v ^$MAC $LISTA_BLANCA > tmp
|
|
mv tmp $LISTA_BLANCA
|
|
echo "$EQUIPO,$IP,$PC" >> $LISTA_BLANCA
|
|
grep -v ^$MAC $LISTA_NEGRA > tmp
|
|
mv tmp $LISTA_NEGRA
|
|
fi
|
|
done
|
|
rm -f pcs.leases
|
|
}
|
|
|
|
Crear_Configuracion() {
|
|
echo " - Creamos $FILE_CONF: fichero de configuración de dhcp-server"
|
|
Configuracion_Base
|
|
if [ ! -e $LISTA_BLANCA ]; then touch $LISTA_BLANCA; fi
|
|
if [ ! -e $LISTA_NEGRA ]; then touch $LISTA_NEGRA; fi
|
|
NUMPC=0
|
|
echo "" >> $FILE_CONF
|
|
echo "# Equipos con entrada denegada:" >> $FILE_CONF
|
|
echo "# -----------------------------" >> $FILE_CONF
|
|
cat $LISTA_NEGRA | grep -v '#' | while read linea; do
|
|
NUMPC=`expr $NUMPC + 1`
|
|
MAC=`echo $linea | cut -f1 -d,`
|
|
PC=`echo $linea | cut -f2 -d, | tr '-' '_'`
|
|
EQUIPO="Equipo_"$NUMPC
|
|
if [ "$PC" ]; then
|
|
EQUIPO=$EQUIPO"_"$PC
|
|
fi
|
|
# ESTA=`grep $PC $FILE_CONF` 2>/dev/null
|
|
# #if [ "$ESTA" ]; then PC=Equipo_$NUMPC; fi
|
|
## if [ "$ESTA" ]; then
|
|
# PC=$PC"_"$NUMPC
|
|
# # fi
|
|
# else
|
|
# PC=Equipo_$NUMPC
|
|
# fi
|
|
if [ "$MAC" ]; then
|
|
echo "host $EQUIPO {
|
|
hardware ethernet $MAC;
|
|
deny booting;
|
|
}" >> $FILE_CONF
|
|
fi
|
|
done
|
|
}
|
|
|
|
Reiniciar_Servicio() {
|
|
echo " - Reiniciamos el servicio $SERVICIO:"
|
|
#/etc/init.d/$SERVICIO restart
|
|
/etc/init.d/$SERVICIO stop > /dev/null
|
|
/etc/init.d/$SERVICIO start > /dev/null
|
|
if [ $? -ne 0 ]; then
|
|
echo " * Error al iniciar el servicio."
|
|
echo " * Comprueba el fichero de configuración creado: $FILE_CONF"
|
|
else
|
|
echo " * Servicio reiniciado correctamente."
|
|
fi
|
|
echo ""
|
|
}
|
|
|
|
Vaciar_Leases() {
|
|
echo " - Vaciamos la lista de equipos de $FILE_LEASES:"
|
|
echo "# The format of this file is documented in the dhcpd.leases(5) manual page.
|
|
# This lease file was written by isc-dhcp-4.1.1-P1
|
|
" > $FILE_LEASES
|
|
}
|
|
|
|
do_start(){
|
|
echo ""
|
|
echo " Configurando dhcp.conf para denegar conexiones ... "
|
|
echo " ---------------------------------------------------"
|
|
Obtener_Leases
|
|
Obtener_ListaNegra
|
|
|
|
Crear_Configuracion
|
|
Vaciar_Leases
|
|
Reiniciar_Servicio
|
|
}
|
|
|
|
do_stop(){
|
|
echo ""
|
|
echo " Configurando dhcp.conf para aceptar todas las conexiones ... "
|
|
echo " -------------------------------------------------------------"
|
|
|
|
Configuracion_Base
|
|
Reiniciar_Servicio
|
|
}
|
|
|
|
# -----------------------------------------------------------------------------
|
|
case "$1" in
|
|
start)
|
|
do_start
|
|
;;
|
|
stop)
|
|
do_stop
|
|
;;
|
|
restart)
|
|
do_start
|
|
;;
|
|
status)
|
|
;;
|
|
*)
|
|
N=/etc/init.d/$NAME
|
|
echo ""
|
|
echo "Usage: $N {start|stop|status}" >&2
|
|
echo ""
|
|
exit 1
|
|
;;
|
|
esac
|
|
|
|
exit 0
|
|
|
|
|