#!/usr/bin/env bash # ============================================================================== # NexusPBX - Script de Réparation Automatique des Permissions & Services # ============================================================================== set -e if [ "$(id -u)" -ne 0 ]; then echo "[ERREUR] Ce script doit être exécuté en root." exit 1 fi echo "-> Correction des répertoires Git et mise à jour..." git config --global --add safe.directory /var/www/nexus-pbx 2>/dev/null || true cd /var/www/nexus-pbx git fetch origin main git reset --hard origin/main echo "-> Installation des dépendances système indispensables (ACL, ffmpeg, rsync, sox, sshpass)..." which ffmpeg >/dev/null 2>&1 && which rsync >/dev/null 2>&1 && which setfacl >/dev/null 2>&1 && which sshpass >/dev/null 2>&1 || (apt-get update -y && apt-get install -y --no-install-recommends acl ffmpeg rsync sox sshpass) echo "-> Création des dossiers et des fichiers de journalisation..." mkdir -p /etc/fail2ban/filter.d /etc/asterisk /var/log/asterisk /var/log/nexus-pbx /var/www/nexus-pbx/storage/backups touch /var/log/asterisk/messages /var/log/asterisk/security.log /var/log/nexus_security.log chown asterisk:asterisk /var/log/asterisk/messages /var/log/asterisk/security.log 2>/dev/null || true chmod 0664 /var/log/asterisk/messages /var/log/asterisk/security.log 2>/dev/null || true chown www-data:asterisk /var/log/nexus_security.log 2>/dev/null || true chmod 0664 /var/log/nexus_security.log 2>/dev/null || true echo "-> Filtres Fail2ban Nexus-Web..." cat << 'EOF' > /etc/fail2ban/filter.d/nexus-web.conf [Definition] failregex = ^.*\[NEXUS-WEB-AUTH-FAIL\]:\s*.*$ ignoreregex = EOF chmod 0644 /etc/fail2ban/filter.d/nexus-web.conf echo "-> Génération et application de /etc/fail2ban/jail.local..." php -r ' require_once "/var/www/nexus-pbx/src/Database.php"; require_once "/var/www/nexus-pbx/src/FirewallManager.php"; $st = App\FirewallManager::getSettings(); App\FirewallManager::generateJailLocal( $st["fail2ban_whitelist_ips"], (int)$st["fail2ban_sip_maxretry"], (int)$st["fail2ban_sip_findtime"], (int)$st["fail2ban_sip_bantime"], (int)$st["fail2ban_web_maxretry"], (int)$st["fail2ban_web_bantime"], (int)$st["admin_https_port"] ); App\FirewallManager::applyNginxConfig((int)$st["admin_https_port"], (int)$st["certbot_strict_port80"]); ' 2>/dev/null || true echo "-> Droits applicatifs et ACL POSIX (/etc/asterisk et /var/www/nexus-pbx)..." usermod -aG asterisk www-data 2>/dev/null || true usermod -aG www-data asterisk 2>/dev/null || true mkdir -p /var/www/nexus-pbx/public/avatars /var/www/nexus-pbx/storage/sounds /var/www/nexus-pbx/storage/backups /var/www/nexus-pbx/storage/system_backups chown -R www-data:asterisk /var/www/nexus-pbx chmod 2775 /var/www/nexus-pbx/storage /var/www/nexus-pbx/storage/backups /var/www/nexus-pbx/storage/system_backups /var/www/nexus-pbx/storage/sounds /var/www/nexus-pbx/public/avatars 2>/dev/null || true touch /etc/asterisk/pjsip.conf \ /etc/asterisk/pjsip.custom.conf \ /etc/asterisk/extensions.conf \ /etc/asterisk/extensions.custom.conf \ /etc/asterisk/musiconhold.conf \ /etc/asterisk/musiconhold.custom.conf \ /etc/asterisk/voicemail.conf \ /etc/asterisk/voicemail.custom.conf \ /etc/asterisk/modules.conf \ /etc/asterisk/rtp.conf \ /etc/asterisk/features.conf 2>/dev/null || true # Désactivation des modules ODBC/IMAP incompatibles sur Debian (évite conflit bloquant app_voicemail) if ! grep -q 'noload => app_voicemail_odbc.so' /etc/asterisk/modules.conf 2>/dev/null; then if grep -q '\[modules\]' /etc/asterisk/modules.conf 2>/dev/null; then sed -i '/\[modules\]/a noload => app_voicemail_odbc.so\nnoload => app_voicemail_imap.so' /etc/asterisk/modules.conf 2>/dev/null || true else printf '[modules]\nautoload=yes\nnoload => app_voicemail_odbc.so\nnoload => app_voicemail_imap.so\n' >> /etc/asterisk/modules.conf fi fi # Inclusions Asterisk et configuration messagerie vocale if ! grep -q 'pjsip.custom.conf' /etc/asterisk/pjsip.conf 2>/dev/null; then printf '\n#tryinclude "pjsip.custom.conf"\n' >> /etc/asterisk/pjsip.conf fi if ! grep -q 'extensions.custom.conf' /etc/asterisk/extensions.conf 2>/dev/null; then printf '\n#tryinclude "extensions.custom.conf"\n' >> /etc/asterisk/extensions.conf fi if ! grep -q 'musiconhold.custom.conf' /etc/asterisk/musiconhold.conf 2>/dev/null; then sed -i 's/^\[default\]/;[default]/' /etc/asterisk/musiconhold.conf 2>/dev/null || true sed -i 's/^mode=files/;mode=files/' /etc/asterisk/musiconhold.conf 2>/dev/null || true sed -i 's/^directory=moh/;directory=moh/' /etc/asterisk/musiconhold.conf 2>/dev/null || true printf '\n#tryinclude "/etc/asterisk/musiconhold.custom.conf"\n' >> /etc/asterisk/musiconhold.conf fi if ! grep -q 'voicemail.custom.conf' /etc/asterisk/voicemail.conf 2>/dev/null; then sed -i 's/^1234 => 4242/;1234 => 4242/' /etc/asterisk/voicemail.conf 2>/dev/null || true sed -i 's/^;\s*passwordlocation\s*=\s*spooldir/passwordlocation=spooldir/' /etc/asterisk/voicemail.conf 2>/dev/null || true sed -i 's|^;\s*externpassnotify\s*=.*|externpassnotify=/usr/bin/php /var/www/nexus-pbx/scripts/voicemail_pass_sync.php|' /etc/asterisk/voicemail.conf 2>/dev/null || true printf '\n#tryinclude "voicemail.custom.conf"\n' >> /etc/asterisk/voicemail.conf fi mkdir -p /var/lib/asterisk/sounds/custom /var/lib/asterisk/moh /var/spool/asterisk/voicemail/default 2>/dev/null || true for lang in fr en es; do if [ -d "/usr/share/asterisk/sounds/$lang" ] && [ ! -e "/var/lib/asterisk/sounds/$lang" ]; then ln -sf "/usr/share/asterisk/sounds/$lang" "/var/lib/asterisk/sounds/$lang" 2>/dev/null || true fi if [ -d "/var/lib/asterisk/sounds/$lang" ] && [ ! -e "/var/lib/asterisk/sounds/$lang/custom" ]; then ln -sf /var/lib/asterisk/sounds/custom "/var/lib/asterisk/sounds/$lang/custom" 2>/dev/null || true fi done if [ ! -e "/usr/share/asterisk/sounds/custom" ]; then ln -sf /var/lib/asterisk/sounds/custom /usr/share/asterisk/sounds/custom 2>/dev/null || true fi chown -R asterisk:asterisk /etc/asterisk /var/lib/asterisk /var/spool/asterisk /var/log/asterisk 2>/dev/null || true chmod -R 775 /etc/asterisk /var/lib/asterisk/sounds /var/lib/asterisk/moh /var/spool/asterisk 2>/dev/null || true chmod 2775 /etc/asterisk /var/lib/asterisk/sounds /var/lib/asterisk/sounds/custom /var/lib/asterisk/moh /var/spool/asterisk /var/spool/asterisk/voicemail 2>/dev/null || true chmod 2750 /etc/asterisk/keys 2>/dev/null || true chmod 0664 /etc/asterisk/*.conf /etc/asterisk/*.custom.conf 2>/dev/null || true setfacl -R -m u:www-data:rwx,g:asterisk:rwx /etc/asterisk /var/lib/asterisk/sounds /var/lib/asterisk/moh /var/spool/asterisk 2>/dev/null || true setfacl -R -d -m u:www-data:rwx,g:asterisk:rwx /etc/asterisk /var/lib/asterisk/sounds /var/lib/asterisk/moh /var/spool/asterisk 2>/dev/null || true chown root:asterisk /etc/fail2ban/jail.local /etc/nginx/sites-available/nexus-pbx 2>/dev/null || true chmod 0664 /etc/fail2ban/jail.local /etc/nginx/sites-available/nexus-pbx 2>/dev/null || true setfacl -m u:www-data:rw /etc/fail2ban/jail.local /etc/nginx/sites-available/nexus-pbx 2>/dev/null || true cat << 'EOF' > /etc/sudoers.d/nexus-pbx www-data ALL=(ALL) NOPASSWD: /usr/sbin/asterisk, /usr/sbin/nginx, /usr/bin/systemctl, /usr/bin/fail2ban-client, /bin/fail2ban-client, /var/www/nexus-pbx/scripts/nexus-firewall, /usr/bin/certbot, /usr/sbin/nft, /usr/sbin/iptables, /var/www/nexus-pbx/scripts/update.sh, /usr/local/bin/nexus-update EOF chmod 0440 /etc/sudoers.d/nexus-pbx echo "-> Autorisation d'écriture systemd pour PHP-FPM (Debian 12/13 ProtectSystem)..." for fpm_svc in /lib/systemd/system/php*-fpm.service /usr/lib/systemd/system/php*-fpm.service; do if [ -f "$fpm_svc" ]; then svc_name=$(basename "$fpm_svc") mkdir -p "/etc/systemd/system/${svc_name}.d" cat << 'EOF_OVERRIDE' > "/etc/systemd/system/${svc_name}.d/override.conf" [Service] ProtectSystem=false ReadWritePaths=/etc/asterisk /etc/fail2ban /etc/nginx /var/log /var/lib/asterisk /var/spool/asterisk /var/www /usr/local/bin /etc/sudoers.d EOF_OVERRIDE fi done systemctl daemon-reload echo "-> Configuration des limites d'upload PHP (512 Mo)..." for php_dir in /etc/php/*; do if [ -d "$php_dir/fpm/conf.d" ]; then cat << 'EOF_PHP' > "$php_dir/fpm/conf.d/99-nexus.ini" upload_max_filesize = 512M post_max_size = 512M memory_limit = 512M max_execution_time = 300 max_input_time = 300 EOF_PHP fi if [ -d "$php_dir/cli/conf.d" ]; then cat << 'EOF_PHP' > "$php_dir/cli/conf.d/99-nexus.ini" upload_max_filesize = 512M post_max_size = 512M memory_limit = 512M max_execution_time = 300 max_input_time = 300 EOF_PHP fi done echo "-> Génération des configurations Asterisk..." php -r ' require_once "/var/www/nexus-pbx/src/Database.php"; require_once "/var/www/nexus-pbx/src/ConfigGenerator.php"; $db = App\Database::getConnection(); $gen = new App\ConfigGenerator($db); $gen->writeAllConfigs(); ' 2>/dev/null || true echo "-> Redémarrage des services..." systemctl daemon-reload systemctl restart php*-fpm 2>/dev/null || true PHP_SVC=$(systemctl list-unit-files --type=service 2>/dev/null | grep -E "php.*-fpm" | awk '{print $1}' | head -n 1 || echo "") if [ -n "$PHP_SVC" ]; then systemctl restart "$PHP_SVC" 2>/dev/null || true fi systemctl restart asterisk 2>/dev/null || true systemctl restart fail2ban 2>/dev/null || true systemctl restart nginx 2>/dev/null || true echo "" echo "==========================================================" echo " [OK] Réparation terminée ! Toutes les permissions sont actives." echo "==========================================================" /var/www/nexus-pbx/scripts/nexus-banner.sh --force || true