2024-02-02 18:45:32 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
|
|
|
|
package ipnlocal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/netip"
|
|
|
|
"os"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"tailscale.com/ipn"
|
|
|
|
"tailscale.com/logtail/backoff"
|
|
|
|
"tailscale.com/tailcfg"
|
|
|
|
"tailscale.com/tailfs"
|
|
|
|
"tailscale.com/types/logger"
|
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *LocalBackend) tailfsAddShareLocked(share *tailfs.Share) (map[string]string, 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-09 17:26:43 +00:00
|
|
|
shares, err := b.tailFSGetSharesLocked()
|
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
|
|
|
|
|
|
|
return shareNameMap(shares), nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *LocalBackend) tailfsRemoveShareLocked(name string) (map[string]string, 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-09 17:26:43 +00:00
|
|
|
shares, err := b.tailFSGetSharesLocked()
|
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
|
|
|
|
|
|
|
return shareNameMap(shares), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func shareNameMap(sharesByName map[string]*tailfs.Share) map[string]string {
|
|
|
|
sharesMap := make(map[string]string, len(sharesByName))
|
|
|
|
for _, share := range sharesByName {
|
|
|
|
sharesMap[share.Name] = share.Path
|
|
|
|
}
|
|
|
|
return sharesMap
|
|
|
|
}
|
|
|
|
|
|
|
|
// tailfsNotifyShares notifies IPN bus listeners (e.g. Mac Application process)
|
|
|
|
// about the latest set of shares, supplied as a map of name -> directory.
|
|
|
|
func (b *LocalBackend) tailfsNotifyShares(shares map[string]string) {
|
2024-02-09 17:26:43 +00:00
|
|
|
b.send(ipn.Notify{TailFSShares: shares})
|
2024-02-02 18:45:32 +00:00
|
|
|
}
|
|
|
|
|
2024-02-09 17:26:43 +00:00
|
|
|
// tailFSNotifyCurrentSharesLocked sends an ipn.Notify with the current set of
|
|
|
|
// TailFS shares.
|
|
|
|
func (b *LocalBackend) tailFSNotifyCurrentSharesLocked() {
|
|
|
|
shares, err := b.tailFSGetSharesLocked()
|
2024-02-02 18:45:32 +00:00
|
|
|
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(shareNameMap(shares))
|
|
|
|
}
|
|
|
|
|
2024-02-09 17:26:43 +00:00
|
|
|
// TailFSGetShares returns the current set of shares from the state store,
|
|
|
|
// stored under ipn.StateKey("_tailfs-shares").
|
|
|
|
func (b *LocalBackend) TailFSGetShares() (map[string]*tailfs.Share, error) {
|
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.tailFSGetSharesLocked()
|
2024-02-02 18:45:32 +00:00
|
|
|
}
|
|
|
|
|
2024-02-09 17:26:43 +00:00
|
|
|
func (b *LocalBackend) tailFSGetSharesLocked() (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
|
|
|
// updateTailFSListenersLocked creates listeners on the local TailFS port.
|
2024-02-02 18:45:32 +00:00
|
|
|
// This is needed to properly route local traffic when using kernel networking
|
|
|
|
// mode.
|
2024-02-09 17:26:43 +00:00
|
|
|
func (b *LocalBackend) updateTailFSListenersLocked() {
|
2024-02-02 18:45:32 +00:00
|
|
|
if b.netMap == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
addrs := b.netMap.GetAddresses()
|
2024-02-09 17:26:43 +00:00
|
|
|
oldListeners := b.tailFSListeners
|
2024-02-02 18:45:32 +00:00
|
|
|
newListeners := make(map[netip.AddrPort]*localListener, addrs.Len())
|
|
|
|
for i := range addrs.LenIter() {
|
2024-02-09 17:26:43 +00:00
|
|
|
if fs, ok := b.sys.TailFSForLocal.GetOK(); ok {
|
|
|
|
addrPort := netip.AddrPortFrom(addrs.At(i).Addr(), TailFSLocalPort)
|
|
|
|
if sl, ok := b.tailFSListeners[addrPort]; ok {
|
2024-02-02 18:45:32 +00:00
|
|
|
newListeners[addrPort] = sl
|
|
|
|
delete(oldListeners, addrPort)
|
|
|
|
continue // already listening
|
|
|
|
}
|
|
|
|
|
2024-02-09 17:26:43 +00:00
|
|
|
sl := b.newTailFSListener(context.Background(), fs, addrPort, b.logf)
|
2024-02-02 18:45:32 +00:00
|
|
|
newListeners[addrPort] = sl
|
|
|
|
go sl.Run()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point, anything left in oldListeners can be stopped.
|
|
|
|
for _, sl := range oldListeners {
|
|
|
|
sl.cancel()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-09 17:26:43 +00:00
|
|
|
// newTailFSListener returns a listener for local connections to a tailfs
|
2024-02-02 18:45:32 +00:00
|
|
|
// WebDAV FileSystem.
|
2024-02-09 17:26:43 +00:00
|
|
|
func (b *LocalBackend) newTailFSListener(ctx context.Context, fs tailfs.FileSystemForLocal, ap netip.AddrPort, logf logger.Logf) *localListener {
|
2024-02-02 18:45:32 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
return &localListener{
|
|
|
|
b: b,
|
|
|
|
ap: ap,
|
|
|
|
ctx: ctx,
|
|
|
|
cancel: cancel,
|
|
|
|
logf: logf,
|
|
|
|
|
|
|
|
handler: func(conn net.Conn) error {
|
2024-02-09 22:23:42 +00:00
|
|
|
if !b.TailFSAccessEnabled() {
|
|
|
|
conn.Close()
|
|
|
|
return nil
|
|
|
|
}
|
2024-02-02 18:45:32 +00:00
|
|
|
return fs.HandleConn(conn, conn.RemoteAddr())
|
|
|
|
},
|
|
|
|
bo: backoff.NewBackoff(fmt.Sprintf("tailfs-listener-%d", ap.Port()), logf, 30*time.Second),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
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
|
|
|
}
|