Aquest script agrega els usuaris al LDAP amb la seva OU i li assignà automàticament una ID mediant un arxiu .csv(nom,cognom,ou) generant un arxiu temporaltemporal .ldif
#!/bin/bash
DOMINIO="dc=illa3,dc=es"
ADMIN="cn=admin,$DOMINIO"
CONTADOR=2000
if [ ! -f "usuarios.csv" ]; then
echo "Error: No es trova usuarios.csv"
exit 1
fi
while IFS=, read -r nombre apellido depto; do
echo "Procesant: $nombre $apellido ($depto)..."
# Generamos el LDIF temporal
cat <<EOF > /tmp/nuevo_usuario.ldif
dn: uid=$nombre,ou=$depto,$DOMINIO
objectClass: inetOrgPerson
objectClass: posixAccount
objectClass: shadowAccount
uid: $nombre
sn: $apellido
cn: $nombre $apellido
uidNumber: $CONTADOR
gidNumber: 1000
homeDirectory: /home/$nombre
loginShell: /bin/bash
userPassword: password123
EOF
ldapadd -x -D "$ADMIN" -w "P@ssw0rd" -f /tmp/nuevo_usuario.ldif
if [ $? -eq 0 ]; then
echo " Usuari $nombre creat amb éxit."
CONTADOR=$((CONTADOR + 1))
else
echo "Error al crear a $nombre. (¿Existeix la OU '$depto'?)"
fi
done < usuarios.csv Aquest script agrega les ou de la tercera columna del arxiu .csv(nom,cognom,ou) directament com una ou del servidor
#!/bin/bash
# Automatización: Creación de OUs para dc=illa3,dc=es
DOMINIO="dc=illa3,dc=es"
ADMIN="cn=admin,$DOMINIO"
DEPTOS=$(cut -d',' -f3 usuarios.csv | sort | uniq)
for ou in $DEPTOS; do
echo "Intentant crear OU: $ou..."
cat <<EOF | ldapadd -x -D "$ADMIN" -w "P@ssw0rd" 2>/dev/null
dn: ou=$ou,$DOMINIO
objectClass: organizationalUnit
ou: $ou
EOF
if [ $? -eq 0 ]; then
echo "OU '$ou' creada amb éxit."
elif [ $? -eq 68 ]; then
echo "La OU '$ou' ya existeix en el servidor."
else
echo "Error inesperat al crear '$ou'. Revisa les credencials."
fi
done Aquest script comprova els fitxers de log i mostra els últims intents fallits d’inici de sessió fallits
#!/bin/bash
# Automatització 5: Monitor de seguretat LDAP millorat
# Detecció autom�| tica del fitxer de log
if [ -f "/var/log/syslog" ]; then
LOG_FILE="/var/log/syslog"
elif [ -f "/var/log/auth.log" ]; then
LOG_FILE="/var/log/auth.log"
elif [ -f "/var/log/messages" ]; then
LOG_FILE="/var/log/messages"
else
echo "ERROR: No s'ha trobat cap fitxer de log"
exit 1
fi
# Patrons d'error LDAP comuns
PATRON1="slapd.*RESULT tag=97 err=49" # Contrasenya incorrecta
PATRON2="ldap.*invalid credentials"
PATRON3="slapd.*connection closed"
echo "====================================="
echo " ALERTA DE SEGURETAT LDAP"
echo "====================================="
echo " Analitzant: $LOG_FILE"
echo " Data: $(date)"
echo "-------------------------------------"
# Comprova si el fitxer existeix i es pot llegir
if [ ! -r "$LOG_FILE" ]; then
echo "ERROR: No es pot llegir $LOG_FILE"
echo " Executa amb: sudo $0"
exit 1
fi
# Comptador total d'intents fallits
INTENTS_TOTALS=$(grep -E "$PATRON1|$PATRON2" "$LOG_FILE" 2>/dev/null | wc -l)
INTENTS_RECENTS=$(grep -E "$PATRON1|$PATRON2" "$LOG_FILE" 2>/dev/null | tail -20)
if [ "$INTENTS_TOTALS" -gt 5 ]; then
echo " ATENCIO: S'han detectat $INTENTS_TOTALS intents fallits d'autenticació"
echo ""
echo "Ultims intents sospitosos:"
echo "-------------------------------------"
grep -E "$PATRON1|$PATRON2" "$LOG_FILE" 2>/dev/null | tail -5 | while read line; do
echo " �~@� $line"
done
# Alerta si hi ha molts intents recents
INTENTS_ULTIMA_HORA=$(grep -E "$PATRON1|$PATRON2" "$LOG_FILE" 2>/dev/null | grep "$(date +%b.%d)" | wc -l)
if [ "$INTENTS_ULTIMA_HORA" -gt 10 ]; then
echo ""
echo " ALERTA CRITICA: $INTENTS_ULTIMA_HORA intents en les últimes 24h"
fi
else
echo "No es detecten anomalies en l'accés LDAP"
echo " Total d'intents fallits històrics: $INTENTS_TOTALS"
fi
echo "====================================="