Check jail.local state and warn user if it is not fail2ban-UI managed, disable automatic jail.local migration because it is only testing

This commit is contained in:
2026-02-09 19:56:43 +01:00
parent e8592d17e6
commit 90b287f409
18 changed files with 232 additions and 244 deletions

View File

@@ -876,10 +876,6 @@ func EnsureJailLocalStructure() error {
}
// ensureJailLocalStructure creates or updates jail.local with proper structure:
// 1. Banner at top warning users not to edit manually
// 2. [DEFAULT] section with current UI settings
// 3. action_mwlg configuration
// 4. action = %(action_mwlg)s at the end
func ensureJailLocalStructure() error {
DebugLog("Running ensureJailLocalStructure()") // entry point
@@ -893,16 +889,25 @@ func ensureJailLocalStructure() error {
// Read existing jail.local content if it exists
var existingContent string
fileExists := false
if content, err := os.ReadFile(jailFile); err == nil {
existingContent = string(content)
fileExists = len(strings.TrimSpace(existingContent)) > 0
}
// Check if file already has our full banner (indicating it's already properly structured)
// Check for the complete banner pattern with hash line separators
// If jail.local exists but is NOT managed by Fail2ban-UI,
// it belongs to the user; never overwrite it.
hasUIAction := strings.Contains(existingContent, "ui-custom-action")
if fileExists && !hasUIAction {
DebugLog("jail.local exists but is not managed by Fail2ban-UI - skipping overwrite")
return nil
}
// Check if file already has our full banner; indicating it's already properly structured
hasFullBanner := strings.Contains(existingContent, "################################################################################") &&
strings.Contains(existingContent, "Fail2Ban-UI Managed Configuration") &&
strings.Contains(existingContent, "DO NOT EDIT THIS FILE MANUALLY")
hasActionMwlg := strings.Contains(existingContent, "action_mwlg") && strings.Contains(existingContent, "ui-custom-action")
hasActionMwlg := strings.Contains(existingContent, "action_mwlg") && hasUIAction
hasActionOverride := strings.Contains(existingContent, "action = %(action_mwlg)s")
// If file is already properly structured, just ensure DEFAULT section is up to date
@@ -952,7 +957,7 @@ chain = %s
defaultSection += "\n"
// Build action_mwlg configuration
// Note: action_mwlg depends on action_ which depends on banaction (now defined above)
// Note: action_mwlg depends on action_ which depends on banaction
// The multi-line format uses indentation for continuation
// ui-custom-action only needs logpath and chain
actionMwlgConfig := `# Custom Fail2Ban action for UI callbacks