paths.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package paths returns platform and user-specific default paths to
  5. // Tailscale files and directories.
  6. package paths
  7. import (
  8. "os"
  9. "path/filepath"
  10. "runtime"
  11. "sync/atomic"
  12. "tailscale.com/version/distro"
  13. )
  14. // AppSharedDir is a string set by the iOS or Android app on start
  15. // containing a directory we can read/write in.
  16. var AppSharedDir atomic.Value
  17. // DefaultTailscaledSocket returns the path to the tailscaled Unix socket
  18. // or the empty string if there's no reasonable default.
  19. func DefaultTailscaledSocket() string {
  20. if runtime.GOOS == "windows" {
  21. return ""
  22. }
  23. if runtime.GOOS == "darwin" {
  24. return "/var/run/tailscaled.socket"
  25. }
  26. if distro.Get() == distro.Synology {
  27. // TODO(maisem): be smarter about this. We can parse /etc/VERSION.
  28. const dsm6Sock = "/var/packages/Tailscale/etc/tailscaled.sock"
  29. const dsm7Sock = "/var/packages/Tailscale/var/tailscaled.sock"
  30. if fi, err := os.Stat(dsm6Sock); err == nil && !fi.IsDir() {
  31. return dsm6Sock
  32. }
  33. if fi, err := os.Stat(dsm7Sock); err == nil && !fi.IsDir() {
  34. return dsm7Sock
  35. }
  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 configurtaion 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. }