root/dhcp-server-denegar/trunk/dhcp-server-denegar @ 517
451 | antoniojas | #!/bin/bash
|
|
487 | antoniojas | ### BEGIN INIT INFO
|
|
# Provides: dhcp-server-denegar
|
|||
# Required-Start: $remote_fs $all
|
|||
# Required-Stop:
|
|||
# Should-Start: $network
|
|||
# Should-Stop:
|
|||
# Default-Start: 2
|
|||
# Default-Stop:
|
|||
507 | antoniojas | # Short-Description: Genera el fichero dhcpd.conf, denegando la entrada a los no permitidos.
|
|
487 | antoniojas | # Description: Controla el acceso al sistema. Los equipos denegados no tomarán ip
|
|
### END INIT INFO
|
|||
451 | antoniojas | # ***********************************************************************************************************
|
|
# 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).
|
|||
#
|
|||
507 | antoniojas | # Para dejarlo operativo colocaremos un crontab que ejecute este script cada 5 min. (por ej.)
|
|
451 | antoniojas | # ***********************************************************************************************************
|
|
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}'`
|
|||
;;
|
|||
490 | antoniojas | '}')
|
|
451 | antoniojas | if [ $MacPC ]; then
|
|
esta=`grep -i $MacPC pcs` 2>/dev/null
|
|||
if [ ! $esta ]; then
|
|||
echo "$MacPC,$IpPC,$NombrePC" >> pcs
|
|||
fi
|
|||
507 | antoniojas | NombrePc=""
|
|
451 | antoniojas | 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)"
|
|||
507 | antoniojas | # rm -f $LISTA_NEGRA 2>/dev/null ; touch $LISTA_NEGRA
|
|
451 | antoniojas | 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
|
|||
452 | antoniojas | ESTA=`grep -i $MAC $LISTA_NEGRA` 2>/dev/null
|
|
if [ ! $ESTA ]; then
|
|||
echo "$MAC,$PC,$IP" >> $LISTA_NEGRA
|
|||
fi
|
|||
451 | antoniojas | 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
|
|||
452 | antoniojas | grep -v ^$MAC $LISTA_NEGRA > tmp
|
|
mv tmp $LISTA_NEGRA
|
|||
451 | antoniojas | fi
|
|
done
|
|||
507 | antoniojas | rm -f pcs.leases
|
|
451 | antoniojas | }
|
|
Crear_Configuracion() {
|
|||
echo " - Creamos $FILE_CONF: fichero de configuración de dhcp-server"
|
|||
Configuracion_Base
|
|||
507 | antoniojas | if [ ! -e $LISTA_BLANCA ]; then touch $LISTA_BLANCA; fi
|
|
if [ ! -e $LISTA_NEGRA ]; then touch $LISTA_NEGRA; fi
|
|||
451 | antoniojas | NUMPC=0
|
|
echo "" >> $FILE_CONF
|
|||
echo "# Equipos con entrada denegada:" >> $FILE_CONF
|
|||
echo "# -----------------------------" >> $FILE_CONF
|
|||
487 | antoniojas | cat $LISTA_NEGRA | grep -v '#' | while read linea; do
|
|
451 | antoniojas | NUMPC=`expr $NUMPC + 1`
|
|
MAC=`echo $linea | cut -f1 -d,`
|
|||
PC=`echo $linea | cut -f2 -d, | tr '-' '_'`
|
|||
461 | antoniojas | EQUIPO="Equipo_"$NUMPC
|
|
if [ "$PC" ]; then
|
|||
EQUIPO=$EQUIPO"_"$PC
|
|||
451 | antoniojas | fi
|
|
507 | antoniojas | # 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
|
|||
487 | antoniojas | if [ "$MAC" ]; then
|
|
echo "host $EQUIPO {
|
|||
451 | antoniojas | hardware ethernet $MAC;
|
|
deny booting;
|
|||
487 | antoniojas | }" >> $FILE_CONF
|
|
fi
|
|||
451 | antoniojas | 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 ""
|
|||
}
|
|||
507 | antoniojas | 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
|
|||
}
|
|||
511 | antoniojas | 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
|
|||
}
|
|||
451 | antoniojas | # -----------------------------------------------------------------------------
|
|
case "$1" in
|
|||
start)
|
|||
511 | antoniojas | do_start
|
|
451 | antoniojas | ;;
|
|
stop)
|
|||
511 | antoniojas | do_stop
|
|
451 | antoniojas | ;;
|
|
511 | antoniojas | restart)
|
|
do_start
|
|||
;;
|
|||
451 | antoniojas | status)
|
|
;;
|
|||
*)
|
|||
N=/etc/init.d/$NAME
|
|||
echo ""
|
|||
echo "Usage: $N {start|stop|status}" >&2
|
|||
echo ""
|
|||
exit 1
|
|||
;;
|
|||
esac
|
|||
exit 0
|
|||