2024-02-02 18:45:32 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
|
|
|
|
package ipnlocal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2024-03-08 16:43:32 +00:00
|
|
|
"os"
|
2024-02-02 18:45:32 +00:00
|
|
|
"regexp"
|
2024-03-08 16:43:32 +00:00
|
|
|
"slices"
|
2024-02-02 18:45:32 +00:00
|
|
|
"strings"
|
|
|
|
|
2024-04-02 20:32:30 +00:00
|
|
|
"tailscale.com/drive"
|
2024-02-02 18:45:32 +00:00
|
|
|
"tailscale.com/ipn"
|
|
|
|
"tailscale.com/tailcfg"
|
|
|
|
"tailscale.com/types/netmap"
|
2024-03-07 16:56:11 +00:00
|
|
|
"tailscale.com/types/views"
|
2024-02-02 18:45:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
shareNameRegex = regexp.MustCompile(`^[a-z0-9_\(\) ]+$`)
|
2024-03-08 16:43:32 +00:00
|
|
|
ErrTailFSNotEnabled = errors.New("TailFS not enabled")
|
|
|
|
ErrInvalidShareName = errors.New("Share names may only contain the letters a-z, underscore _, parentheses (), or spaces")
|
2024-02-02 18:45:32 +00:00
|
|
|
)
|
|
|
|
|
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-03-08 16:43:32 +00:00
|
|
|
return ErrTailFSNotEnabled
|
2024-02-02 18:45:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fs.SetFileServerAddr(addr)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-08 16:43:32 +00:00
|
|
|
// TailFSSetShare adds the given share if no share with that name exists, or
|
|
|
|
// replaces the existing share if one with the same name already exists. To
|
|
|
|
// avoid potential incompatibilities across file systems, share names are
|
2024-02-02 18:45:32 +00:00
|
|
|
// limited to alphanumeric characters and the underscore _.
|
2024-04-02 20:32:30 +00:00
|
|
|
func (b *LocalBackend) TailFSSetShare(share *drive.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()
|
2024-03-08 16:43:32 +00:00
|
|
|
shares, err := b.tailFSSetShareLocked(share)
|
2024-02-02 18:45:32 +00:00
|
|
|
b.mu.Unlock()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-03-07 16:56:11 +00:00
|
|
|
b.tailFSNotifyShares(shares)
|
2024-02-02 18:45:32 +00:00
|
|
|
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) {
|
2024-03-08 16:43:32 +00:00
|
|
|
return "", ErrInvalidShareName
|
2024-02-02 18:45:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return name, nil
|
|
|
|
}
|
|
|
|
|
2024-04-02 20:32:30 +00:00
|
|
|
func (b *LocalBackend) tailFSSetShareLocked(share *drive.Share) (views.SliceView[*drive.Share, drive.ShareView], error) {
|
2024-03-07 16:56:11 +00:00
|
|
|
existingShares := b.pm.prefs.TailFSShares()
|
|
|
|
|
2024-02-09 17:26:43 +00:00
|
|
|
fs, ok := b.sys.TailFSForRemote.GetOK()
|
|
|
|
if !ok {
|
2024-03-08 16:43:32 +00:00
|
|
|
return existingShares, ErrTailFSNotEnabled
|
2024-02-02 18:45:32 +00:00
|
|
|
}
|
|
|
|
|
2024-03-07 16:56:11 +00:00
|
|
|
addedShare := false
|
2024-04-02 20:32:30 +00:00
|
|
|
var shares []*drive.Share
|
2024-03-07 16:56:11 +00:00
|
|
|
for i := 0; i < existingShares.Len(); i++ {
|
|
|
|
existing := existingShares.At(i)
|
|
|
|
if existing.Name() != share.Name {
|
|
|
|
if !addedShare && existing.Name() > share.Name {
|
|
|
|
// Add share in order
|
|
|
|
shares = append(shares, share)
|
|
|
|
addedShare = true
|
|
|
|
}
|
|
|
|
shares = append(shares, existing.AsStruct())
|
|
|
|
}
|
2024-02-02 18:45:32 +00:00
|
|
|
}
|
2024-03-07 16:56:11 +00:00
|
|
|
if !addedShare {
|
|
|
|
shares = append(shares, share)
|
2024-02-02 18:45:32 +00:00
|
|
|
}
|
2024-03-07 16:56:11 +00:00
|
|
|
|
|
|
|
err := b.tailFSSetSharesLocked(shares)
|
2024-02-02 18:45:32 +00:00
|
|
|
if err != nil {
|
2024-03-07 16:56:11 +00:00
|
|
|
return existingShares, err
|
2024-02-02 18:45:32 +00:00
|
|
|
}
|
2024-02-09 17:26:43 +00:00
|
|
|
fs.SetShares(shares)
|
2024-02-02 18:45:32 +00:00
|
|
|
|
2024-03-07 16:56:11 +00:00
|
|
|
return b.pm.prefs.TailFSShares(), nil
|
2024-02-02 18:45:32 +00:00
|
|
|
}
|
|
|
|
|
2024-03-08 16:43:32 +00:00
|
|
|
// TailFSRenameShare renames the share at old name to new name. To avoid
|
|
|
|
// potential incompatibilities across file systems, the new share name is
|
|
|
|
// limited to alphanumeric characters and the underscore _.
|
|
|
|
// Any of the following will result in an error.
|
|
|
|
// - no share found under old name
|
|
|
|
// - new share name contains disallowed characters
|
|
|
|
// - share already exists under new name
|
|
|
|
func (b *LocalBackend) TailFSRenameShare(oldName, newName string) error {
|
|
|
|
var err error
|
|
|
|
newName, err = normalizeShareName(newName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
b.mu.Lock()
|
|
|
|
shares, err := b.tailFSRenameShareLocked(oldName, newName)
|
|
|
|
b.mu.Unlock()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
b.tailFSNotifyShares(shares)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-04-02 20:32:30 +00:00
|
|
|
func (b *LocalBackend) tailFSRenameShareLocked(oldName, newName string) (views.SliceView[*drive.Share, drive.ShareView], error) {
|
2024-03-08 16:43:32 +00:00
|
|
|
existingShares := b.pm.prefs.TailFSShares()
|
|
|
|
|
|
|
|
fs, ok := b.sys.TailFSForRemote.GetOK()
|
|
|
|
if !ok {
|
|
|
|
return existingShares, ErrTailFSNotEnabled
|
|
|
|
}
|
|
|
|
|
|
|
|
found := false
|
2024-04-02 20:32:30 +00:00
|
|
|
var shares []*drive.Share
|
2024-03-08 16:43:32 +00:00
|
|
|
for i := 0; i < existingShares.Len(); i++ {
|
|
|
|
existing := existingShares.At(i)
|
|
|
|
if existing.Name() == newName {
|
|
|
|
return existingShares, os.ErrExist
|
|
|
|
}
|
|
|
|
if existing.Name() == oldName {
|
|
|
|
share := existing.AsStruct()
|
|
|
|
share.Name = newName
|
|
|
|
shares = append(shares, share)
|
|
|
|
found = true
|
|
|
|
} else {
|
|
|
|
shares = append(shares, existing.AsStruct())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
return existingShares, os.ErrNotExist
|
|
|
|
}
|
|
|
|
|
2024-04-02 20:32:30 +00:00
|
|
|
slices.SortFunc(shares, drive.CompareShares)
|
2024-03-08 16:43:32 +00:00
|
|
|
err := b.tailFSSetSharesLocked(shares)
|
|
|
|
if err != nil {
|
|
|
|
return existingShares, err
|
|
|
|
}
|
|
|
|
fs.SetShares(shares)
|
|
|
|
|
|
|
|
return b.pm.prefs.TailFSShares(), 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()
|
2024-03-07 16:56:11 +00:00
|
|
|
shares, err := b.tailFSRemoveShareLocked(name)
|
2024-02-02 18:45:32 +00:00
|
|
|
b.mu.Unlock()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-03-07 16:56:11 +00:00
|
|
|
b.tailFSNotifyShares(shares)
|
2024-02-02 18:45:32 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-04-02 20:32:30 +00:00
|
|
|
func (b *LocalBackend) tailFSRemoveShareLocked(name string) (views.SliceView[*drive.Share, drive.ShareView], error) {
|
2024-03-07 16:56:11 +00:00
|
|
|
existingShares := b.pm.prefs.TailFSShares()
|
|
|
|
|
2024-02-09 17:26:43 +00:00
|
|
|
fs, ok := b.sys.TailFSForRemote.GetOK()
|
|
|
|
if !ok {
|
2024-03-08 16:43:32 +00:00
|
|
|
return existingShares, ErrTailFSNotEnabled
|
2024-02-02 18:45:32 +00:00
|
|
|
}
|
|
|
|
|
2024-03-08 16:43:32 +00:00
|
|
|
found := false
|
2024-04-02 20:32:30 +00:00
|
|
|
var shares []*drive.Share
|
2024-03-07 16:56:11 +00:00
|
|
|
for i := 0; i < existingShares.Len(); i++ {
|
|
|
|
existing := existingShares.At(i)
|
|
|
|
if existing.Name() != name {
|
|
|
|
shares = append(shares, existing.AsStruct())
|
2024-03-08 16:43:32 +00:00
|
|
|
} else {
|
|
|
|
found = true
|
2024-03-07 16:56:11 +00:00
|
|
|
}
|
2024-02-02 18:45:32 +00:00
|
|
|
}
|
2024-03-07 16:56:11 +00:00
|
|
|
|
2024-03-08 16:43:32 +00:00
|
|
|
if !found {
|
|
|
|
return existingShares, os.ErrNotExist
|
|
|
|
}
|
|
|
|
|
2024-03-07 16:56:11 +00:00
|
|
|
err := b.tailFSSetSharesLocked(shares)
|
2024-02-02 18:45:32 +00:00
|
|
|
if err != nil {
|
2024-03-07 16:56:11 +00:00
|
|
|
return existingShares, err
|
2024-02-02 18:45:32 +00:00
|
|
|
}
|
2024-02-09 17:26:43 +00:00
|
|
|
fs.SetShares(shares)
|
2024-02-02 18:45:32 +00:00
|
|
|
|
2024-03-07 16:56:11 +00:00
|
|
|
return b.pm.prefs.TailFSShares(), nil
|
2024-02-02 18:45:32 +00:00
|
|
|
}
|
|
|
|
|
2024-04-02 20:32:30 +00:00
|
|
|
func (b *LocalBackend) tailFSSetSharesLocked(shares []*drive.Share) error {
|
2024-03-07 16:56:11 +00:00
|
|
|
prefs := b.pm.prefs.AsStruct()
|
|
|
|
prefs.ApplyEdits(&ipn.MaskedPrefs{
|
|
|
|
Prefs: ipn.Prefs{
|
|
|
|
TailFSShares: shares,
|
|
|
|
},
|
|
|
|
TailFSSharesSet: true,
|
|
|
|
})
|
|
|
|
return b.pm.setPrefsLocked(prefs.View())
|
|
|
|
}
|
|
|
|
|
|
|
|
// tailFSNotifyShares notifies IPN bus listeners (e.g. Mac Application process)
|
|
|
|
// about the latest list of shares.
|
2024-04-02 20:32:30 +00:00
|
|
|
func (b *LocalBackend) tailFSNotifyShares(shares views.SliceView[*drive.Share, drive.ShareView]) {
|
2024-03-20 15:15:05 +00:00
|
|
|
// Ensures shares is not nil to distinguish "no shares" from "not notifying shares"
|
|
|
|
if shares.IsNil() {
|
2024-04-02 20:32:30 +00:00
|
|
|
shares = views.SliceOfViews(make([]*drive.Share, 0))
|
2024-03-20 15:15:05 +00:00
|
|
|
}
|
2024-02-09 17:26:43 +00:00
|
|
|
b.send(ipn.Notify{TailFSShares: shares})
|
2024-02-02 18:45:32 +00:00
|
|
|
}
|
|
|
|
|
2024-03-07 16:56:11 +00:00
|
|
|
// tailFSNotifyCurrentSharesLocked sends an ipn.Notify if the current set of
|
|
|
|
// shares has changed since the last notification.
|
|
|
|
func (b *LocalBackend) tailFSNotifyCurrentSharesLocked() {
|
2024-04-02 20:32:30 +00:00
|
|
|
var shares views.SliceView[*drive.Share, drive.ShareView]
|
2024-03-07 16:56:11 +00:00
|
|
|
if b.tailFSSharingEnabledLocked() {
|
|
|
|
// Only populate shares if sharing is enabled.
|
|
|
|
shares = b.pm.prefs.TailFSShares()
|
|
|
|
}
|
|
|
|
|
|
|
|
lastNotified := b.lastNotifiedTailFSShares.Load()
|
|
|
|
if lastNotified == nil || !tailFSShareViewsEqual(lastNotified, shares) {
|
2024-02-29 19:49:45 +00:00
|
|
|
// Do the below on a goroutine to avoid deadlocking on b.mu in b.send().
|
2024-03-07 16:56:11 +00:00
|
|
|
go b.tailFSNotifyShares(shares)
|
|
|
|
}
|
2024-02-02 18:45:32 +00:00
|
|
|
}
|
|
|
|
|
2024-04-02 20:32:30 +00:00
|
|
|
func tailFSShareViewsEqual(a *views.SliceView[*drive.Share, drive.ShareView], b views.SliceView[*drive.Share, drive.ShareView]) bool {
|
2024-03-07 16:56:11 +00:00
|
|
|
if a == nil {
|
|
|
|
return false
|
2024-02-02 18:45:32 +00:00
|
|
|
}
|
|
|
|
|
2024-03-07 16:56:11 +00:00
|
|
|
if a.Len() != b.Len() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < a.Len(); i++ {
|
2024-04-02 20:32:30 +00:00
|
|
|
if !drive.ShareViewsEqual(a.At(i), b.At(i)) {
|
2024-03-07 16:56:11 +00:00
|
|
|
return false
|
|
|
|
}
|
2024-02-02 18:45:32 +00:00
|
|
|
}
|
|
|
|
|
2024-03-07 16:56:11 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// TailFSGetShares() gets the current list of TailFS shares, sorted by name.
|
2024-04-02 20:32:30 +00:00
|
|
|
func (b *LocalBackend) TailFSGetShares() views.SliceView[*drive.Share, drive.ShareView] {
|
2024-03-07 16:56:11 +00:00
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
|
|
|
|
|
|
|
return b.pm.prefs.TailFSShares()
|
2024-02-02 18:45:32 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-04-02 20:32:30 +00:00
|
|
|
var tailFSRemotes []*drive.Remote
|
2024-03-07 16:56:11 +00:00
|
|
|
if b.tailFSAccessEnabledLocked() {
|
|
|
|
// Only populate peers if access is enabled, otherwise leave blank.
|
|
|
|
tailFSRemotes = b.tailFSRemotesFromPeers(nm)
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.SetRemotes(b.netMap.Domain, tailFSRemotes, &tailFSTransport{b: b})
|
|
|
|
}
|
|
|
|
|
2024-04-02 20:32:30 +00:00
|
|
|
func (b *LocalBackend) tailFSRemotesFromPeers(nm *netmap.NetworkMap) []*drive.Remote {
|
|
|
|
tailFSRemotes := make([]*drive.Remote, 0, len(nm.Peers))
|
2024-02-02 18:45:32 +00:00
|
|
|
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-04-02 20:32:30 +00:00
|
|
|
tailFSRemotes = append(tailFSRemotes, &drive.Remote{
|
2024-02-02 18:45:32 +00:00
|
|
|
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-03-07 16:56:11 +00:00
|
|
|
return tailFSRemotes
|
2024-02-02 18:45:32 +00:00
|
|
|
}
|