2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2020-03-03 17:33:09 +00:00
|
|
|
|
2023-06-11 15:02:52 +00:00
|
|
|
//go:build !windows && !js && !wasip1
|
2020-03-03 17:33:09 +00:00
|
|
|
|
|
|
|
package paths
|
|
|
|
|
|
|
|
import (
|
2021-09-26 14:54:53 +00:00
|
|
|
"fmt"
|
2021-03-30 15:20:21 +00:00
|
|
|
"os"
|
2020-03-03 17:33:09 +00:00
|
|
|
"path/filepath"
|
2020-03-04 01:37:37 +00:00
|
|
|
"runtime"
|
2020-03-03 17:33:09 +00:00
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
2022-03-02 03:49:24 +00:00
|
|
|
"tailscale.com/version/distro"
|
2020-03-03 17:33:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
stateFileFunc = stateFileUnix
|
|
|
|
}
|
|
|
|
|
2020-03-04 01:37:37 +00:00
|
|
|
func statePath() string {
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "linux":
|
|
|
|
return "/var/lib/tailscale/tailscaled.state"
|
|
|
|
case "freebsd", "openbsd":
|
|
|
|
return "/var/db/tailscale/tailscaled.state"
|
2021-02-13 20:10:20 +00:00
|
|
|
case "darwin":
|
|
|
|
return "/Library/Tailscale/tailscaled.state"
|
2020-03-04 01:37:37 +00:00
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-03 17:33:09 +00:00
|
|
|
func stateFileUnix() string {
|
2022-03-02 03:49:24 +00:00
|
|
|
if distro.Get() == distro.Gokrazy {
|
|
|
|
return "/perm/tailscaled/tailscaled.state"
|
|
|
|
}
|
2020-03-04 01:37:37 +00:00
|
|
|
path := statePath()
|
|
|
|
if path == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
try := path
|
2020-03-03 17:33:09 +00:00
|
|
|
for i := 0; i < 3; i++ { // check writability of the file, /var/lib/tailscale, and /var/lib
|
|
|
|
err := unix.Access(try, unix.O_RDWR)
|
|
|
|
if err == nil {
|
2020-03-04 01:37:37 +00:00
|
|
|
return path
|
2020-03-03 17:33:09 +00:00
|
|
|
}
|
|
|
|
try = filepath.Dir(try)
|
|
|
|
}
|
|
|
|
|
2021-03-30 15:20:21 +00:00
|
|
|
if os.Getuid() == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// For non-root users, fall back to $XDG_DATA_HOME/tailscale/*.
|
|
|
|
return filepath.Join(xdgDataHome(), "tailscale", "tailscaled.state")
|
|
|
|
}
|
|
|
|
|
|
|
|
func xdgDataHome() string {
|
|
|
|
if e := os.Getenv("XDG_DATA_HOME"); e != "" {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
return filepath.Join(os.Getenv("HOME"), ".local/share")
|
2020-03-03 17:33:09 +00:00
|
|
|
}
|
2021-09-21 21:00:30 +00:00
|
|
|
|
2021-09-26 14:54:53 +00:00
|
|
|
func ensureStateDirPerms(dir string) error {
|
|
|
|
if filepath.Base(dir) != "tailscale" {
|
2021-09-24 21:24:16 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-09-26 14:54:53 +00:00
|
|
|
fi, err := os.Stat(dir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !fi.IsDir() {
|
|
|
|
return fmt.Errorf("expected %q to be a directory; is %v", dir, fi.Mode())
|
|
|
|
}
|
|
|
|
const perm = 0700
|
|
|
|
if fi.Mode().Perm() == perm {
|
|
|
|
// Already correct.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return os.Chmod(dir, perm)
|
2021-09-21 21:00:30 +00:00
|
|
|
}
|
2021-10-06 19:27:35 +00:00
|
|
|
|
|
|
|
// LegacyStateFilePath is not applicable to UNIX; it is just stubbed out.
|
|
|
|
func LegacyStateFilePath() string {
|
|
|
|
return ""
|
|
|
|
}
|