Linux_frogg-profile.d/func/deb_sys.sh

348 lines
11 KiB
Bash

check_deb_status() {
local type="$1" params="$2"
case "$type" in
"pkg") dpkg -s "$params" >/dev/null 2>&1 ;;
"sys")
func="check_deb_${type}_${params}"
if declare -F "$func" >/dev/null; then
"$func"
else
return 1
fi
#case "$params" in
# "apparmor")check_deb_sys_apparmor;;
# "ipv6") [[ $(sysctl -n net.ipv6.conf.all.disable_ipv6 2>/dev/null) -eq 1 ]] ;;
# "mail") grep -q "ADMIN_MAIL=" /etc/environment 2>/dev/null ;;
# "zabbix") dpkg -s zabbix-agent >/dev/null 2>&1 ;;
# "msmtp") [[ -f /etc/msmtprc ]] ;;
# *) return 1 ;;
#esac ;;
esac
}
do_deb_install_action() {
local type="$1" params="$2" func
case "$type" in
"pkg") $SUDO apt-get install -y "$params" ;;
"sys")
func="do_deb_${type}_${params}"
if declare -F "$func" >/dev/null; then
"$func"
else
msg_error "la fonction do_deb_${type}_${params} n'est pas définie...(skip)"
fi
esac
}
check_deb_sys_ipv6(){
if [[ $(sysctl -n net.ipv6.conf.all.disable_ipv6 2>/dev/null) -eq 1 ]]; then
return 0
else
return 1
fi
}
check_deb_sys_zabbix(){
if dpkg -s zabbix-agent >/dev/null 2>&1; then
return 0
else
return 1
fi
}
check_deb_sys_msmtp(){
if [[ -f /etc/msmtprc ]]; then
return 1
else
return 0
fi
}
check_deb_sys_mail(){
if grep -q "ADMIN_MAIL=" /etc/environment 2>/dev/null; then
return 0
else
return 1
fi
}
check_deb_sys_fail2ban() {
# Si le paquet n'est PAS installé, on ne propose pas la config
if ! dpkg -s fail2ban >/dev/null 2>&1; then
return 0 # Masquer (on attend l'étape 1)
fi
# Si le paquet est là, on affiche la config tant que jail.local n'existe pas
if [[ ! -f /etc/fail2ban/jail.local ]]; then
return 1 # AFFICHER
else
return 0 # MASQUER (Déjà configuré)
fi
}
check_deb_sys_apparmor(){
if is_sys_lxc; then
! systemctl is-enabled apparmor >/dev/null 2>&1 #Container
else
[[ $($SUDO cat /sys/module/apparmor/parameters/enabled 2>/dev/null) == "Y" ]] #VM
fi
}
check_deb_sys_ufw() {
# 1. Si le paquet n'est pas là, on ne propose rien
if ! dpkg -s ufw >/dev/null 2>&1; then
return 0
fi
# 2. On vérifie l'état réel du firewall
# Si 'Status: active' est trouvé, on cache (0), sinon on affiche (1)
if $SUDO ufw status | grep -q "Status: active"; then
return 0 # Masquer
else
return 1 # AFFICHER
fi
}
do_deb_sys_ufw() {
local ports_to_allow=() input_port="" current_ssh_port
msg_info "Configuration interactive du Firewall UFW"
# 1. On commence par les bases
$SUDO ufw default deny incoming
$SUDO ufw default allow outgoing
# 2. Boucle de saisie des ports
while true; do
read -p "Entrez un port à ouvrir (ou 'f' pour terminer) : " input_port
[[ "$input_port" == "f" ]] && break
# Validation : est-ce un nombre ?
if [[ ! "$input_port" =~ ^[0-9]+$ ]]; then
msg_error "Veuillez entrer un numéro de port valide (chiffres uniquement)."
continue
fi
# Gestion des DOUBLONS
if [[ " ${ports_to_allow[@]} " =~ " ${input_port} " ]]; then
msg_warning "Le port $input_port est déjà dans votre liste de saisie."
else
ports_to_allow+=("$input_port")
msg_success "Port $input_port ajouté à la file d'attente."
fi
done
# 3. Sécurité SSH (vérification si le port 22 ou le port courant est là)
# On récupère le port SSH actuel pour éviter de se lock
current_ssh_port=$(ss -tlnp | grep sshd | awk '{print $4}' | awk -F':' '{print $NF}' | head -n1)
current_ssh_port=${current_ssh_port:-22}
if [[ ! " ${ports_to_allow[@]} " =~ " ${current_ssh_port} " ]]; then
msg_warning "ATTENTION : Votre port SSH ($current_ssh_port) n'est pas dans la liste !"
read -p "Voulez-vous l'ajouter par sécurité ? (O/n) : " add_ssh
[[ "$add_ssh" != "n" ]] && ports_to_allow+=("$current_ssh_port")
fi
# 4. Application des règles sans doublons dans UFW
msg_info "Application des règles..."
for port in "${ports_to_allow[@]}"; do
# On utilise /tcp par défaut pour être strict comme on l'a dit
$SUDO ufw allow "$port"/tcp >/dev/null
done
# 5. Désactiver l'IPv6 dans UFW
msg_info "Desactivation de l IP v6..."
$SUDO sed -i 's/IPV6=yes/IPV6=no/' /etc/default/ufw
# 6. Activation finale
if $SUDO ufw --force enable; then
msg_success "Firewall activé avec les ports : ${ports_to_allow[*]}"
fi
}
do_deb_sys_fail2ban() {
msg_info "Configuration de Fail2Ban..."
# On crée la configuration personnalisée
$SUDO tee /etc/fail2ban/jail.local <<EOF
[sshd]
enabled = true
port = ssh
filter = sshd
backend = systemd
maxretry = 5
bantime = 1h
allowipv6 = auto
EOF
$SUDO systemctl restart fail2ban
msg_success "Fail2Ban configuré et redémarré."
}
do_deb_sys_apparmor(){
if is_sys_lxc; then
msg_info "Détection d'un conteneur LXC..."
#$SUDO systemctl enable --now apparmor
msg_success "AppArmor activé (Niveau conteneur)."
msg_info "Note: La sécurité réelle est gérée par l'hôte Proxmox."
else
msg_info "Configuration AppArmor (Machine Physique/VM)..."
$SUDO systemctl enable --now apparmor
if [[ $($SUDO cat /sys/module/apparmor/parameters/enabled 2>/dev/null) != "Y" ]]; then
if [ -f /etc/default/grub ]; then
$SUDO sed -i 's/GRUB_CMDLINE_LINUX_DEFAULT="/GRUB_CMDLINE_LINUX_DEFAULT="apparmor=1 security=apparmor /' /etc/default/grub
$SUDO update-grub
msg_warning "Activation GRUB faite. Reboot nécessaire."
fi
else
msg_success "AppArmor est déjà actif dans le Kernel."
fi
fi
}
do_deb_sys_zabbix(){
read -rp "IP Zabbix : " ip;
$SUDO apt-get install -y zabbix-agent;
$SUDO sed -i "s/^Server=127.0.0.1/Server=$ip/" /etc/zabbix/zabbix_agentd.conf;
$SUDO systemctl restart zabbix-agent
}
do_deb_sys_ipv6(){
echo "net.ipv6.conf.all.disable_ipv6 = 1" | $SUDO tee -a /etc/sysctl.conf > /dev/null && $SUDO sysctl -p
}
do_deb_sys_env_mail(){
read -rp "Mail admin : " m;
echo "ADMIN_MAIL=\"$m\"" | $SUDO tee -a /etc/environment > /dev/null
# shellcheck disable=SC2034
ADMIN_MAIL=$m
}
do_deb_sys_static_ip() {
local interface
interface=$(ip -o link show | awk -F': ' '{print $2}' | grep -v 'lo' | head -n1)
echo -e "${COLOR_WHITE}Configuration de l'interface : ${COLOR_CYAN}$interface${NONE}"
read -rp "Entrez l'IP (ex: 192.168.1.50) : " new_ip
read -rp "Entrez la Passerelle (Gateway) : " new_gw
read -rp "Entrez le DNS Primaire (ex: 1.1.1.1) : " dns1
read -rp "Entrez le DNS Secondaire (ex: 8.8.8.8) : " dns2
# Validation minimale
if [[ -z "$new_ip" || -z "$new_gw" || -z "$dns1" ]]; then
msg_error "IP, Gateway ou DNS Primaire manquant. Annulation."
return 1
fi
# Si le DNS2 est vide, on peut mettre une valeur par défaut ou laisser vide
[[ -z "$dns2" ]] && dns2="8.8.4.4"
# --- CAS 1 : NETPLAN (Ubuntu / Debian récentes) ---
if [ -d "/etc/netplan" ]; then
msg_info "Configuration via Netplan..."
$SUDO bash -c "cat <<EOF > /etc/netplan/99-frogg-static.yaml
network:
version: 2
renderer: networkd
ethernets:
$interface:
addresses: [$new_ip/24]
routes: [{to: default, via: $new_gw}]
nameservers:
addresses: [$dns1, $dns2]
EOF"
$SUDO netplan apply
# --- CAS 2 : INTERFACES (Debian classique) ---
elif [ -f "/etc/network/interfaces" ]; then
msg_info "Configuration via /etc/network/interfaces..."
$SUDO bash -c "cat <<EOF > /etc/network/interfaces.d/$interface
auto $interface
iface $interface inet static
address $new_ip
netmask 255.255.255.0
gateway $new_gw
dns-nameservers $dns1 $dns2
EOF"
$SUDO systemctl restart networking
else
msg_error "Aucun gestionnaire réseau trouvé."
return 1
fi
msg_success "$interface > IP [$new_ip] Gateway[$new_gw] DNS[$dns1, $dns2]"
}
do_deb_sys_hostname() {
local old_h current_ip
old_h=$(hostname)
echo -e "${COLOR_WHITE}Nom actuel : ${COLOR_CYAN}$old_h${NONE}"
read -rp "Nouveau Hostname (FQDN recommandé, ex: machine.domaine.com) : " new_h
if [[ -n "$new_h" ]]; then
# 1. On définit le hostname dans le système
$SUDO hostnamectl set-hostname "$new_h"
# 2. On extrait le nom court
local short_h="${new_h%%.*}"
# 3. Mise à jour de /etc/hosts
# STRATÉGIE : On identifie la ligne qui contient l'ancien hostname
# et on la remplace intégralement par "IP FQDN NOM_COURT"
# On récupère l'IP associée à l'ancien hostname dans /etc/hosts
current_ip=$(grep -w "$old_h" /etc/hosts | awk '{print $1}' | head -n1)
if [[ -n "$current_ip" ]]; then
msg_info "Mise à jour de la ligne pour l'IP $current_ip..."
# On remplace toute la ligne qui commence par cette IP
$SUDO sed -i "s/^$current_ip.*/$current_ip\t$new_h $short_h/" /etc/hosts
else
# Si on ne trouve pas l'IP, on ajoute une ligne propre sur la loopback locale
msg_warning "IP non trouvée dans /etc/hosts, ajout sur 127.0.1.1"
echo -e "127.0.1.1\t$new_h $short_h" | $SUDO tee -a /etc/hosts > /dev/null
fi
msg_success "Hostname configuré : $new_h ($short_h)"
fi
}
do_deb_sys_msmtp() {
msg_info "Installation et configuration de MSMTP..."
# Installation du paquet
$SUDO apt-get update -qq && $SUDO apt-get install -y msmtp msmtp-mta ca-certificates -qq
# Saisie des informations
read -rp "Serveur SMTP (host) : " m_host
read -rp "Port (ex: 587 ou 465) : " m_port
read -rp "Email expéditeur (from) : " m_from
read -rp "Utilisateur (user/login) : " m_user
read -s -rp "Mot de passe : " m_pass
echo "" # Pour revenir à la ligne après le mot de passe masqué
# Construction du fichier msmtprc
$SUDO bash -c "cat <<EOF > /etc/msmtprc
# Configuration générée par Frogg Deploy
defaults
auth on
tls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
syslog LOG_MAIL
#logfile /var/log/msmtp.log
account default
host $m_host
port $m_port
from $m_from
user $m_user
password $m_pass
EOF"
# Sécurisation du fichier (Indispensable car contient un mot de passe)
$SUDO chmod 600 /etc/msmtprc
$SUDO chown root:root /etc/msmtprc
msg_success "MSMTP configuré. Test d'envoi recommandé : echo 'Test' | mail -s 'Test sujet' ton@mail.com"
}