paths.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. if runtime.GOOS == "plan9" {
  26. return "/srv/tailscaled.sock"
  27. }
  28. switch distro.Get() {
  29. case distro.Synology:
  30. if distro.DSMVersion() == 6 {
  31. return "/var/packages/Tailscale/etc/tailscaled.sock"
  32. }
  33. // DSM 7 (and higher? or failure to detect.)
  34. return "/var/packages/Tailscale/var/tailscaled.sock"
  35. case distro.Gokrazy:
  36. return "/perm/tailscaled/tailscaled.sock"
  37. case distro.QNAP:
  38. return "/tmp/tailscale/tailscaled.sock"
  39. }
  40. if fi, err := os.Stat("/var/run"); err == nil && fi.IsDir() {
  41. return "/var/run/tailscale/tailscaled.sock"
  42. }
  43. return "tailscaled.sock"
  44. }
  45. // Overridden in init by OS-specific files.
  46. var (
  47. stateFileFunc func() string
  48. // ensureStateDirPerms applies a restrictive ACL/chmod
  49. // to the provided directory.
  50. ensureStateDirPerms = func(string) error { return nil }
  51. )
  52. // DefaultTailscaledStateFile returns the default path to the
  53. // tailscaled state file, or the empty string if there's no reasonable
  54. // default value.
  55. func DefaultTailscaledStateFile() string {
  56. if f := stateFileFunc; f != nil {
  57. return f()
  58. }
  59. if runtime.GOOS == "windows" {
  60. return filepath.Join(os.Getenv("ProgramData"), "Tailscale", "server-state.conf")
  61. }
  62. return ""
  63. }
  64. // MkStateDir ensures that dirPath, the daemon's configuration directory
  65. // containing machine keys etc, both exists and has the correct permissions.
  66. // We want it to only be accessible to the user the daemon is running under.
  67. func MkStateDir(dirPath string) error {
  68. if err := os.MkdirAll(dirPath, 0700); err != nil {
  69. return err
  70. }
  71. return ensureStateDirPerms(dirPath)
  72. }
  73. // LegacyStateFilePath returns the legacy path to the state file when
  74. // it was stored under the current user's %LocalAppData%.
  75. //
  76. // It is only called on Windows.
  77. func LegacyStateFilePath() string {
  78. if runtime.GOOS == "windows" {
  79. return filepath.Join(os.Getenv("LocalAppData"), "Tailscale", "server-state.conf")
  80. }
  81. return ""
  82. }