paths.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. // Package paths returns platform and user-specific default paths to
  4. // Tailscale files and directories.
  5. package paths
  6. import (
  7. "os"
  8. "path/filepath"
  9. "runtime"
  10. "tailscale.com/syncs"
  11. "tailscale.com/version/distro"
  12. )
  13. // AppSharedDir is a string set by the iOS or Android app on start
  14. // containing a directory we can read/write in.
  15. var AppSharedDir syncs.AtomicValue[string]
  16. // DefaultTailscaledSocket returns the path to the tailscaled Unix socket
  17. // or the empty string if there's no reasonable default.
  18. func DefaultTailscaledSocket() string {
  19. if runtime.GOOS == "windows" {
  20. return `\\.\pipe\ProtectedPrefix\Administrators\Tailscale\tailscaled`
  21. }
  22. if runtime.GOOS == "darwin" {
  23. return "/var/run/tailscaled.socket"
  24. }
  25. switch distro.Get() {
  26. case distro.Synology:
  27. if distro.DSMVersion() == 6 {
  28. return "/var/packages/Tailscale/etc/tailscaled.sock"
  29. }
  30. // DSM 7 (and higher? or failure to detect.)
  31. return "/var/packages/Tailscale/var/tailscaled.sock"
  32. case distro.Gokrazy:
  33. return "/perm/tailscaled/tailscaled.sock"
  34. case distro.QNAP:
  35. return "/tmp/tailscale/tailscaled.sock"
  36. }
  37. if fi, err := os.Stat("/var/run"); err == nil && fi.IsDir() {
  38. return "/var/run/tailscale/tailscaled.sock"
  39. }
  40. return "tailscaled.sock"
  41. }
  42. var stateFileFunc func() string
  43. // DefaultTailscaledStateFile returns the default path to the
  44. // tailscaled state file, or the empty string if there's no reasonable
  45. // default value.
  46. func DefaultTailscaledStateFile() string {
  47. if f := stateFileFunc; f != nil {
  48. return f()
  49. }
  50. if runtime.GOOS == "windows" {
  51. return filepath.Join(os.Getenv("ProgramData"), "Tailscale", "server-state.conf")
  52. }
  53. return ""
  54. }
  55. // MkStateDir ensures that dirPath, the daemon's configuration directory
  56. // containing machine keys etc, both exists and has the correct permissions.
  57. // We want it to only be accessible to the user the daemon is running under.
  58. func MkStateDir(dirPath string) error {
  59. if err := os.MkdirAll(dirPath, 0700); err != nil {
  60. return err
  61. }
  62. return ensureStateDirPerms(dirPath)
  63. }