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-08-30 13:23:38 +01:00
|
|
|
//go:build !windows && !wasm && !plan9 && !tamago
|
2020-03-03 17:33:09 +00:00
|
|
|
|
|
|
|
package paths
|
|
|
|
|
|
|
|
import (
|
2021-09-26 15:54:53 +01:00
|
|
|
"fmt"
|
2021-03-30 16:20:21 +01: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
|
2023-08-24 05:19:11 +01:00
|
|
|
ensureStateDirPerms = ensureStateDirPermsUnix
|
2020-03-03 17:33:09 +00:00
|
|
|
}
|
|
|
|
|
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"
|
2024-04-14 04:56:11 +01:00
|
|
|
case "aix":
|
|
|
|
return "/var/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
|
2024-04-16 21:15:13 +01:00
|
|
|
for range 3 { // check writability of the file, /var/lib/tailscale, and /var/lib
|
2020-03-03 17:33:09 +00:00
|
|
|
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 16:20:21 +01: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 22:00:30 +01:00
|
|
|
|
2023-08-24 05:19:11 +01:00
|
|
|
func ensureStateDirPermsUnix(dir string) error {
|
2021-09-26 15:54:53 +01:00
|
|
|
if filepath.Base(dir) != "tailscale" {
|
2021-09-24 22:24:16 +01:00
|
|
|
return nil
|
|
|
|
}
|
2021-09-26 15:54:53 +01: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 22:00:30 +01:00
|
|
|
}
|