package config import ( "fmt" "io/fs" "os" ) // CheckFileMode bricht ab, wenn die Datei mehr Rechte trägt als maxMode. func CheckFileMode(path string, maxMode fs.FileMode) error { info, err := os.Stat(path) if err != nil { return fmt.Errorf("Datei %s nicht lesbar: %w", path, err) } if info.IsDir() { return fmt.Errorf("%s ist ein Verzeichnis, erwartet wurde eine Datei", path) } return checkMode(path, info.Mode().Perm(), maxMode) } // CheckDirMode bricht ab, wenn das Verzeichnis mehr Rechte trägt als maxMode. func CheckDirMode(path string, maxMode fs.FileMode) error { info, err := os.Stat(path) if err != nil { return fmt.Errorf("Verzeichnis %s nicht lesbar: %w", path, err) } if !info.IsDir() { return fmt.Errorf("%s ist kein Verzeichnis", path) } return checkMode(path, info.Mode().Perm(), maxMode) } func checkMode(path string, actual, maxMode fs.FileMode) error { if extra := actual &^ maxMode; extra != 0 { return fmt.Errorf( "%s hat zu weite Dateirechte %#o (erlaubt höchstens %#o). Beheben mit: chmod %#o %s", path, actual, maxMode, maxMode, path) } return nil }