2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2020-02-03 18:35:52 +00:00
|
|
|
|
|
|
|
package ipn
|
|
|
|
|
|
|
|
import (
|
2023-08-04 15:55:59 +01:00
|
|
|
"bytes"
|
2023-03-29 21:51:53 +01:00
|
|
|
"context"
|
2020-02-03 18:35:52 +00:00
|
|
|
"errors"
|
2022-10-04 04:39:45 +01:00
|
|
|
"fmt"
|
2023-03-29 21:51:53 +01:00
|
|
|
"net"
|
2022-10-04 04:39:45 +01:00
|
|
|
"strconv"
|
2020-02-03 18:35:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ErrStateNotExist is returned by StateStore.ReadState when the
|
2020-02-11 05:46:45 +00:00
|
|
|
// requested state ID doesn't exist.
|
|
|
|
var ErrStateNotExist = errors.New("no state with given ID")
|
2020-02-03 18:35:52 +00:00
|
|
|
|
2020-09-30 04:51:25 +01:00
|
|
|
const (
|
2020-09-28 23:28:26 +01:00
|
|
|
// MachineKeyStateKey is the key under which we store the machine key,
|
2021-10-28 18:40:44 +01:00
|
|
|
// in its key.NodePrivate.MarshalText representation.
|
2020-09-28 23:28:26 +01:00
|
|
|
MachineKeyStateKey = StateKey("_machinekey")
|
|
|
|
|
2022-11-09 05:58:10 +00:00
|
|
|
// LegacyGlobalDaemonStateKey is the ipn.StateKey that tailscaled
|
2020-09-30 04:51:25 +01:00
|
|
|
// loads on startup.
|
|
|
|
//
|
|
|
|
// We have to support multiple state keys for other OSes (Windows in
|
|
|
|
// particular), but right now Unix daemons run with a single
|
|
|
|
// node-global state. To keep open the option of having per-user state
|
|
|
|
// later, the global state key doesn't look like a username.
|
2022-11-09 05:58:10 +00:00
|
|
|
//
|
|
|
|
// As of 2022-10-21, it has been superseded by profiles and is no longer
|
|
|
|
// written to disk. It is only read at startup when there are no profiles,
|
|
|
|
// to migrate the state to the "default" profile.
|
|
|
|
// The existing state is left on disk in case the user downgrades to an
|
|
|
|
// older version of Tailscale that doesn't support profiles. We can
|
|
|
|
// remove this in a future release.
|
|
|
|
LegacyGlobalDaemonStateKey = StateKey("_daemon")
|
ipn, ipnserver, cmd/tailscale: add "server mode" support on Windows
This partially (but not yet fully) migrates Windows to tailscaled's
StateStore storage system.
This adds a new bool Pref, ForceDaemon, defined as:
// ForceDaemon specifies whether a platform that normally
// operates in "client mode" (that is, requires an active user
// logged in with the GUI app running) should keep running after the
// GUI ends and/or the user logs out.
//
// The only current applicable platform is Windows. This
// forced Windows to go into "server mode" where Tailscale is
// running even with no users logged in. This might also be
// used for macOS in the future. This setting has no effect
// for Linux/etc, which always operate in daemon mode.
Then, when ForceDaemon becomes true, we now write use the StateStore
to track which user started it in server mode, and store their prefs
under that key.
The ipnserver validates the connections/identities and informs that
LocalBackend which userid is currently in charge.
The GUI can then enable/disable server mode at runtime, without using
the CLI.
But the "tailscale up" CLI was also fixed, so Windows users can use
authkeys or ACL tags, etc.
Updates #275
2020-10-12 22:28:21 +01:00
|
|
|
|
|
|
|
// ServerModeStartKey's value, if non-empty, is the value of a
|
|
|
|
// StateKey containing the prefs to start with which to start the
|
|
|
|
// server.
|
|
|
|
//
|
|
|
|
// For example, the value might be "user-1234", meaning the
|
|
|
|
// the server should start with the Prefs JSON loaded from
|
|
|
|
// StateKey "user-1234".
|
|
|
|
ServerModeStartKey = StateKey("server-mode-start-key")
|
2022-08-01 23:46:41 +01:00
|
|
|
|
2022-11-09 05:58:10 +00:00
|
|
|
// KnownProfilesStateKey is the key under which we store the list of
|
|
|
|
// known profiles. The value is a JSON-encoded []LoginProfile.
|
|
|
|
KnownProfilesStateKey = StateKey("_profiles")
|
|
|
|
|
|
|
|
// CurrentProfileStateKey is the key under which we store the current
|
|
|
|
// profile.
|
|
|
|
CurrentProfileStateKey = StateKey("_current-profile")
|
2023-11-13 18:20:28 +00:00
|
|
|
|
|
|
|
// TaildropReceivedKey is the key to indicate whether any taildrop file
|
|
|
|
// has ever been received (even if partially).
|
|
|
|
// Any non-empty value indicates that at least one file has been received.
|
|
|
|
TaildropReceivedKey = StateKey("_taildrop-received")
|
2020-09-30 04:51:25 +01:00
|
|
|
)
|
|
|
|
|
2022-11-09 05:58:10 +00:00
|
|
|
// CurrentProfileID returns the StateKey that stores the
|
|
|
|
// current profile ID. The value is a JSON-encoded LoginProfile.
|
|
|
|
// If the userID is empty, the key returned is CurrentProfileStateKey,
|
|
|
|
// otherwise it is "_current/"+userID.
|
|
|
|
func CurrentProfileKey(userID string) StateKey {
|
|
|
|
if userID == "" {
|
|
|
|
return CurrentProfileStateKey
|
|
|
|
}
|
|
|
|
return StateKey("_current/" + userID)
|
|
|
|
}
|
|
|
|
|
2020-02-03 18:35:52 +00:00
|
|
|
// StateStore persists state, and produces it back on request.
|
2024-03-06 18:29:20 +00:00
|
|
|
// Implementations of StateStore are expected to be safe for concurrent use.
|
2020-02-03 18:35:52 +00:00
|
|
|
type StateStore interface {
|
2020-02-11 05:46:45 +00:00
|
|
|
// ReadState returns the bytes associated with ID. Returns (nil,
|
|
|
|
// ErrStateNotExist) if the ID doesn't have associated state.
|
2020-02-03 18:35:52 +00:00
|
|
|
ReadState(id StateKey) ([]byte, error)
|
2020-02-11 05:46:45 +00:00
|
|
|
// WriteState saves bs as the state associated with ID.
|
2023-08-04 15:55:59 +01:00
|
|
|
//
|
|
|
|
// Callers should generally use the ipn.WriteState wrapper func
|
|
|
|
// instead, which only writes if the value is different from what's
|
|
|
|
// already in the store.
|
2020-02-03 18:35:52 +00:00
|
|
|
WriteState(id StateKey, bs []byte) error
|
|
|
|
}
|
2022-10-04 04:39:45 +01:00
|
|
|
|
2023-08-04 15:55:59 +01:00
|
|
|
// WriteState is a wrapper around store.WriteState that only writes if
|
|
|
|
// the value is different from what's already in the store.
|
|
|
|
func WriteState(store StateStore, id StateKey, v []byte) error {
|
|
|
|
if was, err := store.ReadState(id); err == nil && bytes.Equal(was, v) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return store.WriteState(id, v)
|
|
|
|
}
|
|
|
|
|
2023-03-29 21:51:53 +01:00
|
|
|
// StateStoreDialerSetter is an optional interface that StateStores
|
|
|
|
// can implement to allow the caller to set a custom dialer.
|
|
|
|
type StateStoreDialerSetter interface {
|
|
|
|
SetDialer(d func(ctx context.Context, network, address string) (net.Conn, error))
|
|
|
|
}
|
|
|
|
|
2022-10-04 04:39:45 +01:00
|
|
|
// ReadStoreInt reads an integer from a StateStore.
|
|
|
|
func ReadStoreInt(store StateStore, id StateKey) (int64, error) {
|
|
|
|
v, err := store.ReadState(id)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return strconv.ParseInt(string(v), 10, 64)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutStoreInt puts an integer into a StateStore.
|
|
|
|
func PutStoreInt(store StateStore, id StateKey, val int64) error {
|
2023-08-04 15:55:59 +01:00
|
|
|
return WriteState(store, id, fmt.Appendf(nil, "%d", val))
|
2022-10-04 04:39:45 +01:00
|
|
|
}
|