perm_unix.go 695 B

1234567891011121314151617181920212223242526272829
  1. //go:build !windows
  2. package config
  3. import (
  4. "fmt"
  5. "io"
  6. "os"
  7. )
  8. // WarnIfLooseMode prints a one-line warning to w if path has any of the
  9. // group- or world-readable bits set (0044). POSIX only; no-op on Windows.
  10. // A missing file or stat error is silently ignored — callers have already
  11. // succeeded or failed on the file itself by this point.
  12. func WarnIfLooseMode(path string, w io.Writer) {
  13. if w == nil || path == "" {
  14. return
  15. }
  16. info, err := os.Stat(path)
  17. if err != nil {
  18. return
  19. }
  20. mode := info.Mode().Perm()
  21. if mode&0o044 != 0 {
  22. fmt.Fprintf(w,
  23. "zenmux-usage: warning: config file %s has mode %04o; run `chmod 600 %s` to protect your API keys\n",
  24. path, mode, path)
  25. }
  26. }