2024-02-02 18:45:32 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
|
|
|
|
package ipnlocal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"tailscale.com/ipn"
|
|
|
|
"tailscale.com/tailcfg"
|
|
|
|
"tailscale.com/tailfs"
|
|
|
|
"tailscale.com/types/netmap"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2024-02-09 17:26:43 +00:00
|
|
|
// TailFSLocalPort is the port on which the TailFS listens for location
|
2024-02-02 18:45:32 +00:00
|
|
|
// connections on quad 100.
|
2024-02-09 17:26:43 +00:00
|
|
|
TailFSLocalPort = 8080
|
2024-02-02 18:45:32 +00:00
|
|
|
|
|
|
|
tailfsSharesStateKey = ipn.StateKey("_tailfs-shares")
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
shareNameRegex = regexp.MustCompile(`^[a-z0-9_\(\) ]+$`)
|
|
|
|
errInvalidShareName = errors.New("Share names may only contain the letters a-z, underscore _, parentheses (), or spaces")
|
|
|
|
)
|
|
|
|
|
2024-02-09 17:26:43 +00:00
|
|
|
// TailFSSharingEnabled reports whether sharing to remote nodes via tailfs is
|
2024-02-02 18:45:32 +00:00
|
|
|
// enabled. This is currently based on checking for the tailfs:share node
|
|
|
|
// attribute.
|
2024-02-09 17:26:43 +00:00
|
|
|
func (b *LocalBackend) TailFSSharingEnabled() bool {
|
2024-02-02 18:45:32 +00:00
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
2024-02-09 17:26:43 +00:00
|
|
|
return b.tailFSSharingEnabledLocked()
|
2024-02-02 18:45:32 +00:00
|
|
|
}
|
|
|
|
|
2024-02-09 17:26:43 +00:00
|
|
|
func (b *LocalBackend) tailFSSharingEnabledLocked() bool {
|
2024-02-09 22:23:42 +00:00
|
|
|
return b.netMap != nil && b.netMap.SelfNode.HasCap(tailcfg.NodeAttrsTailFSShare)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TailFSAccessEnabled reports whether accessing TailFS shares on remote nodes
|
|
|
|
// is enabled. This is currently based on checking for the tailfs:access node
|
|
|
|
// attribute.
|
|
|
|
func (b *LocalBackend) TailFSAccessEnabled() bool {
|
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
|
|
|
return b.tailFSAccessEnabledLocked()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *LocalBackend) tailFSAccessEnabledLocked() bool {
|
|
|
|
return b.netMap != nil && b.netMap.SelfNode.HasCap(tailcfg.NodeAttrsTailFSAccess)
|
2024-02-02 18:45:32 +00:00
|
|
|
}
|
|
|
|
|
2024-02-09 17:26:43 +00:00
|
|
|
// TailFSSetFileServerAddr tells tailfs to use the given address for connecting
|
2024-02-02 18:45:32 +00:00
|
|
|
// to the tailfs.FileServer that's exposing local files as an unprivileged
|
|
|
|
// user.
|
2024-02-09 17:26:43 +00:00
|
|
|
func (b *LocalBackend) TailFSSetFileServerAddr(addr string) error {
|
|
|
|
fs, ok := b.sys.TailFSForRemote.GetOK()
|
|
|
|
if !ok {
|
2024-02-02 18:45:32 +00:00
|
|
|
return errors.New("tailfs not enabled")
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.SetFileServerAddr(addr)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-09 17:26:43 +00:00
|
|
|
// TailFSAddShare adds the given share if no share with that name exists, or
|
2024-02-02 18:45:32 +00:00
|
|
|
// replaces the existing share if one with the same name already exists.
|
|
|
|
// To avoid potential incompatibilities across file systems, share names are
|
|
|
|
// limited to alphanumeric characters and the underscore _.
|
2024-02-09 17:26:43 +00:00
|
|
|
func (b *LocalBackend) TailFSAddShare(share *tailfs.Share) error {
|
2024-02-02 18:45:32 +00:00
|
|
|
var err error
|
|
|
|
share.Name, err = normalizeShareName(share.Name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
b.mu.Lock()
|
|
|
|
shares, err := b.tailfsAddShareLocked(share)
|
|
|
|
b.mu.Unlock()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
b.tailfsNotifyShares(shares)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// normalizeShareName normalizes the given share name and returns an error if
|
|
|
|
// it contains any disallowed characters.
|
|
|
|
func normalizeShareName(name string) (string, error) {
|
|
|
|
// Force all share names to lowercase to avoid potential incompatibilities
|
|
|
|
// with clients that don't support case-sensitive filenames.
|
|
|
|
name = strings.ToLower(name)
|
|
|
|
|
|
|
|
// Trim whitespace
|
|
|
|
name = strings.TrimSpace(name)
|
|
|
|
|
|
|
|
if !shareNameRegex.MatchString(name) {
|
|
|
|
return "", errInvalidShareName
|
|
|
|
}
|
|
|
|
|
|
|
|
return name, nil
|
|
|
|
}
|
|
|
|
|
2024-02-28 03:22:45 +00:00
|
|
|
func (b *LocalBackend) tailfsAddShareLocked(share *tailfs.Share) (map[string]*tailfs.Share, error) {
|
2024-02-09 17:26:43 +00:00
|
|
|
fs, ok := b.sys.TailFSForRemote.GetOK()
|
|
|
|
if !ok {
|
2024-02-02 18:45:32 +00:00
|
|
|
return nil, errors.New("tailfs not enabled")
|
|
|
|
}
|
|
|
|
|
2024-02-29 19:49:45 +00:00
|
|
|
shares, err := b.TailFSGetShares()
|
2024-02-02 18:45:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
shares[share.Name] = share
|
|
|
|
data, err := json.Marshal(shares)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("marshal: %w", err)
|
|
|
|
}
|
|
|
|
err = b.store.WriteState(tailfsSharesStateKey, data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("write state: %w", err)
|
|
|
|
}
|
2024-02-09 17:26:43 +00:00
|
|
|
fs.SetShares(shares)
|
2024-02-02 18:45:32 +00:00
|
|
|
|
2024-02-29 19:49:45 +00:00
|
|
|
return shares, nil
|
2024-02-02 18:45:32 +00:00
|
|
|
}
|
|
|
|
|
2024-02-09 17:26:43 +00:00
|
|
|
// TailFSRemoveShare removes the named share. Share names are forced to
|
2024-02-02 18:45:32 +00:00
|
|
|
// lowercase.
|
2024-02-09 17:26:43 +00:00
|
|
|
func (b *LocalBackend) TailFSRemoveShare(name string) error {
|
2024-02-02 18:45:32 +00:00
|
|
|
// Force all share names to lowercase to avoid potential incompatibilities
|
|
|
|
// with clients that don't support case-sensitive filenames.
|
2024-02-09 17:26:43 +00:00
|
|
|
var err error
|
|
|
|
name, err = normalizeShareName(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-02-02 18:45:32 +00:00
|
|
|
|
|
|
|
b.mu.Lock()
|
|
|
|
shares, err := b.tailfsRemoveShareLocked(name)
|
|
|
|
b.mu.Unlock()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
b.tailfsNotifyShares(shares)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-28 03:22:45 +00:00
|
|
|
func (b *LocalBackend) tailfsRemoveShareLocked(name string) (map[string]*tailfs.Share, error) {
|
2024-02-09 17:26:43 +00:00
|
|
|
fs, ok := b.sys.TailFSForRemote.GetOK()
|
|
|
|
if !ok {
|
2024-02-02 18:45:32 +00:00
|
|
|
return nil, errors.New("tailfs not enabled")
|
|
|
|
}
|
|
|
|
|
2024-02-29 19:49:45 +00:00
|
|
|
shares, err := b.TailFSGetShares()
|
2024-02-02 18:45:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
_, shareExists := shares[name]
|
|
|
|
if !shareExists {
|
|
|
|
return nil, os.ErrNotExist
|
|
|
|
}
|
|
|
|
delete(shares, name)
|
|
|
|
data, err := json.Marshal(shares)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("marshal: %w", err)
|
|
|
|
}
|
|
|
|
err = b.store.WriteState(tailfsSharesStateKey, data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("write state: %w", err)
|
|
|
|
}
|
2024-02-09 17:26:43 +00:00
|
|
|
fs.SetShares(shares)
|
2024-02-02 18:45:32 +00:00
|
|
|
|
2024-02-29 19:49:45 +00:00
|
|
|
return shares, nil
|
2024-02-02 18:45:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// tailfsNotifyShares notifies IPN bus listeners (e.g. Mac Application process)
|
|
|
|
// about the latest set of shares, supplied as a map of name -> directory.
|
2024-02-28 03:22:45 +00:00
|
|
|
func (b *LocalBackend) tailfsNotifyShares(shares map[string]*tailfs.Share) {
|
2024-02-09 17:26:43 +00:00
|
|
|
b.send(ipn.Notify{TailFSShares: shares})
|
2024-02-02 18:45:32 +00:00
|
|
|
}
|
|
|
|
|
2024-02-29 19:49:45 +00:00
|
|
|
// tailFSNotifyCurrentSharesOnce sends a one-time ipn.Notify with the current
|
|
|
|
// set of TailFS shares.
|
|
|
|
func (b *LocalBackend) tailFSNotifyCurrentSharesOnce() {
|
|
|
|
b.notifyTailFSSharesOnce.Do(func() {
|
|
|
|
shares, err := b.TailFSGetShares()
|
|
|
|
if err != nil {
|
|
|
|
b.logf("error notifying current tailfs shares: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Do the below on a goroutine to avoid deadlocking on b.mu in b.send().
|
|
|
|
go b.tailfsNotifyShares(shares)
|
|
|
|
})
|
2024-02-02 18:45:32 +00:00
|
|
|
}
|
|
|
|
|
2024-02-09 17:26:43 +00:00
|
|
|
// TailFSGetShares returns the current set of shares from the state store,
|
2024-02-29 19:49:45 +00:00
|
|
|
// stored under ipn.StateKey("_tailfs-shares"). The caller owns this map and
|
|
|
|
// is free to mutate it.
|
2024-02-09 17:26:43 +00:00
|
|
|
func (b *LocalBackend) TailFSGetShares() (map[string]*tailfs.Share, error) {
|
2024-02-02 18:45:32 +00:00
|
|
|
data, err := b.store.ReadState(tailfsSharesStateKey)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, ipn.ErrStateNotExist) {
|
|
|
|
return make(map[string]*tailfs.Share), nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("read state: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var shares map[string]*tailfs.Share
|
|
|
|
err = json.Unmarshal(data, &shares)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unmarshal: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return shares, nil
|
|
|
|
}
|
|
|
|
|
2024-02-09 17:26:43 +00:00
|
|
|
// updateTailFSPeersLocked sets all applicable peers from the netmap as tailfs
|
2024-02-02 18:45:32 +00:00
|
|
|
// remotes.
|
2024-02-09 17:26:43 +00:00
|
|
|
func (b *LocalBackend) updateTailFSPeersLocked(nm *netmap.NetworkMap) {
|
|
|
|
fs, ok := b.sys.TailFSForLocal.GetOK()
|
2024-02-02 18:45:32 +00:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tailfsRemotes := make([]*tailfs.Remote, 0, len(nm.Peers))
|
|
|
|
for _, p := range nm.Peers {
|
2024-03-06 17:46:11 +00:00
|
|
|
// Exclude mullvad exit nodes from list of TailFS peers
|
|
|
|
// TODO(oxtoacart) - once we have a better mechanism for finding only accessible sharers
|
|
|
|
// (see below) we can remove this logic.
|
|
|
|
if strings.HasSuffix(p.Name(), ".mullvad.ts.net.") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-02-02 18:45:32 +00:00
|
|
|
peerID := p.ID()
|
2024-02-09 17:26:43 +00:00
|
|
|
url := fmt.Sprintf("%s/%s", peerAPIBase(nm, p), tailFSPrefix[1:])
|
2024-02-02 18:45:32 +00:00
|
|
|
tailfsRemotes = append(tailfsRemotes, &tailfs.Remote{
|
|
|
|
Name: p.DisplayName(false),
|
|
|
|
URL: url,
|
|
|
|
Available: func() bool {
|
|
|
|
// TODO(oxtoacart): need to figure out a performant and reliable way to only
|
|
|
|
// show the peers that have shares to which we have access
|
|
|
|
// This will require work on the control server to transmit the inverse
|
|
|
|
// of the "tailscale.com/cap/tailfs" capability.
|
|
|
|
// For now, at least limit it only to nodes that are online.
|
|
|
|
// Note, we have to iterate the latest netmap because the peer we got from the first iteration may not be it
|
|
|
|
b.mu.Lock()
|
|
|
|
latestNetMap := b.netMap
|
|
|
|
b.mu.Unlock()
|
|
|
|
|
|
|
|
for _, candidate := range latestNetMap.Peers {
|
|
|
|
if candidate.ID() == peerID {
|
|
|
|
online := candidate.Online()
|
|
|
|
// TODO(oxtoacart): for some reason, this correctly
|
|
|
|
// catches when a node goes from offline to online,
|
|
|
|
// but not the other way around...
|
|
|
|
return online != nil && *online
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// peer not found, must not be available
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2024-02-09 17:26:43 +00:00
|
|
|
fs.SetRemotes(b.netMap.Domain, tailfsRemotes, &tailFSTransport{b: b})
|
2024-02-02 18:45:32 +00:00
|
|
|
}
|