diff --git a/custom/scripts/validate_and_rebuild.py b/custom/scripts/validate_and_rebuild.py index 0ba27574..fe794630 100755 --- a/custom/scripts/validate_and_rebuild.py +++ b/custom/scripts/validate_and_rebuild.py @@ -383,42 +383,100 @@ class EntityValidator: return True def check_file_permissions(self) -> bool: - """Prüfe Dateirechte im custom-Verzeichnis.""" + """Prüfe Dateirechte im custom-Verzeichnis und kritischen System-Dateien.""" print_header("6. DATEIRECHTE-PRÜFUNG") + # Verzeichnisse/Dateien die geprüft werden sollen + paths_to_check = [ + self.custom_path, # custom/Espo/Custom/Resources + self.client_custom_path, # client/custom + self.base_path / "data", # Gesamtes data/ Verzeichnis + ] + + # Kritische einzelne Dateien die UNBEDINGT www-data gehören müssen + critical_files = [ + self.base_path / "data" / "config.php", + self.base_path / "data" / "config-internal.php", + ] + + all_wrong_files = [] + critical_wrong_files = [] + try: - # Prüfe ob Dateien von www-data gehören - result = subprocess.run( - ['find', str(self.custom_path), '!', '-user', 'www-data', '-o', '!', '-group', 'www-data'], - capture_output=True, - text=True - ) - - wrong_owner_files = [line for line in result.stdout.strip().split('\n') if line] - - if wrong_owner_files: - print_warning(f"{len(wrong_owner_files)} Dateien mit falschen Rechten gefunden") - print_info("Versuche automatische Korrektur...") + # Prüfe jedes Verzeichnis + for path in paths_to_check: + if not path.exists(): + continue + + result = subprocess.run( + ['find', str(path), '!', '-user', 'www-data', '-o', '!', '-group', 'www-data'], + capture_output=True, + text=True + ) + + wrong_files = [line for line in result.stdout.strip().split('\n') if line] + all_wrong_files.extend(wrong_files) + + # Prüfe kritische Dateien einzeln + for critical_file in critical_files: + if not critical_file.exists(): + continue + + stat_result = critical_file.stat() + import pwd + import grp - # Versuche Rechte zu korrigieren try: - subprocess.run( - ['sudo', 'chown', '-R', 'www-data:www-data', str(self.custom_path)], - check=True, - capture_output=True - ) - subprocess.run( - ['sudo', 'find', str(self.custom_path), '-type', 'f', '-exec', 'chmod', '664', '{}', ';'], - check=True, - capture_output=True - ) - subprocess.run( - ['sudo', 'find', str(self.custom_path), '-type', 'd', '-exec', 'chmod', '775', '{}', ';'], - check=True, - capture_output=True - ) - print_success("Dateirechte korrigiert") - except subprocess.CalledProcessError: + owner = pwd.getpwuid(stat_result.st_uid).pw_name + group = grp.getgrgid(stat_result.st_gid).gr_name + + if owner != 'www-data' or group != 'www-data': + critical_wrong_files.append(str(critical_file)) + all_wrong_files.append(str(critical_file)) + print_error(f"KRITISCH: {critical_file.relative_to(self.base_path)} gehört {owner}:{group} statt www-data:www-data") + except (KeyError, OSError): + pass + + if all_wrong_files: + print_warning(f"{len(all_wrong_files)} Dateien/Verzeichnisse mit falschen Rechten gefunden") + + if critical_wrong_files: + print_error(f"{len(critical_wrong_files)} davon sind KRITISCHE System-Dateien!") + + print_info("Versuche automatische Korrektur aller Verzeichnisse...") + + # Korrigiere alle Pfade + success_count = 0 + for path in paths_to_check: + if not path.exists(): + continue + + try: + # Setze Owner + subprocess.run( + ['sudo', 'chown', '-R', 'www-data:www-data', str(path)], + check=True, + capture_output=True + ) + # Setze Permissions für Dateien + subprocess.run( + ['sudo', 'find', str(path), '-type', 'f', '-exec', 'chmod', '664', '{}', ';'], + check=True, + capture_output=True + ) + # Setze Permissions für Verzeichnisse + subprocess.run( + ['sudo', 'find', str(path), '-type', 'd', '-exec', 'chmod', '775', '{}', ';'], + check=True, + capture_output=True + ) + success_count += 1 + except subprocess.CalledProcessError as e: + print_warning(f"Konnte {path.relative_to(self.base_path)} nicht korrigieren: {e}") + + if success_count > 0: + print_success(f"Dateirechte für {success_count} Verzeichnis(se) korrigiert") + else: print_warning("Konnte Dateirechte nicht automatisch korrigieren (sudo erforderlich)") else: print_success("Alle Dateirechte korrekt (www-data:www-data)")