2024-02-09 17:26:43 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
|
2024-04-02 20:32:30 +00:00
|
|
|
package driveimpl
|
2024-02-09 17:26:43 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2024-03-01 03:24:42 +00:00
|
|
|
"context"
|
2024-02-09 17:26:43 +00:00
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"math"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/netip"
|
2024-02-21 12:40:12 +00:00
|
|
|
"net/url"
|
2024-02-09 17:26:43 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2024-03-01 03:24:42 +00:00
|
|
|
"os/user"
|
2024-03-07 16:56:11 +00:00
|
|
|
"slices"
|
2024-02-09 17:26:43 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/tailscale/xnet/webdav"
|
2024-04-02 20:32:30 +00:00
|
|
|
"tailscale.com/drive"
|
|
|
|
"tailscale.com/drive/driveimpl/compositedav"
|
|
|
|
"tailscale.com/drive/driveimpl/dirfs"
|
|
|
|
"tailscale.com/drive/driveimpl/shared"
|
2024-02-09 17:26:43 +00:00
|
|
|
"tailscale.com/safesocket"
|
|
|
|
"tailscale.com/types/logger"
|
|
|
|
)
|
|
|
|
|
|
|
|
func NewFileSystemForRemote(logf logger.Logf) *FileSystemForRemote {
|
|
|
|
if logf == nil {
|
|
|
|
logf = log.Printf
|
|
|
|
}
|
|
|
|
fs := &FileSystemForRemote{
|
|
|
|
logf: logf,
|
|
|
|
lockSystem: webdav.NewMemLS(),
|
2024-02-21 12:40:12 +00:00
|
|
|
children: make(map[string]*compositedav.Child),
|
2024-02-09 17:26:43 +00:00
|
|
|
userServers: make(map[string]*userServer),
|
|
|
|
}
|
|
|
|
return fs
|
|
|
|
}
|
|
|
|
|
2024-04-03 17:09:58 +00:00
|
|
|
// FileSystemForRemote implements drive.FileSystemForRemote.
|
2024-02-09 17:26:43 +00:00
|
|
|
type FileSystemForRemote struct {
|
|
|
|
logf logger.Logf
|
|
|
|
lockSystem webdav.LockSystem
|
|
|
|
|
|
|
|
// mu guards the below values. Acquire a write lock before updating any of
|
|
|
|
// them, acquire a read lock before reading any of them.
|
2024-05-01 15:45:57 +00:00
|
|
|
mu sync.RWMutex
|
|
|
|
// fileServerTokenAndAddr is the secretToken|fileserverAddress
|
|
|
|
fileServerTokenAndAddr string
|
|
|
|
shares []*drive.Share
|
|
|
|
children map[string]*compositedav.Child
|
|
|
|
userServers map[string]*userServer
|
2024-02-09 17:26:43 +00:00
|
|
|
}
|
|
|
|
|
2024-04-03 17:09:58 +00:00
|
|
|
// SetFileServerAddr implements drive.FileSystemForRemote.
|
2024-02-09 17:26:43 +00:00
|
|
|
func (s *FileSystemForRemote) SetFileServerAddr(addr string) {
|
|
|
|
s.mu.Lock()
|
2024-05-01 15:45:57 +00:00
|
|
|
s.fileServerTokenAndAddr = addr
|
2024-02-09 17:26:43 +00:00
|
|
|
s.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2024-04-03 17:09:58 +00:00
|
|
|
// SetShares implements drive.FileSystemForRemote. Shares must be sorted
|
|
|
|
// according to drive.CompareShares.
|
2024-04-02 20:32:30 +00:00
|
|
|
func (s *FileSystemForRemote) SetShares(shares []*drive.Share) {
|
2024-02-09 17:26:43 +00:00
|
|
|
userServers := make(map[string]*userServer)
|
2024-04-02 20:32:30 +00:00
|
|
|
if drive.AllowShareAs() {
|
2024-03-01 03:24:42 +00:00
|
|
|
// Set up per-user server by running the current executable as an
|
|
|
|
// unprivileged user in order to avoid privilege escalation.
|
|
|
|
executable, err := os.Executable()
|
|
|
|
if err != nil {
|
|
|
|
s.logf("can't find executable: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-09 17:26:43 +00:00
|
|
|
for _, share := range shares {
|
|
|
|
p, found := userServers[share.As]
|
|
|
|
if !found {
|
|
|
|
p = &userServer{
|
2024-03-01 03:24:42 +00:00
|
|
|
logf: s.logf,
|
|
|
|
username: share.As,
|
|
|
|
executable: executable,
|
2024-02-09 17:26:43 +00:00
|
|
|
}
|
|
|
|
userServers[share.As] = p
|
|
|
|
}
|
|
|
|
p.shares = append(p.shares, share)
|
|
|
|
}
|
|
|
|
for _, p := range userServers {
|
|
|
|
go p.runLoop()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-21 12:40:12 +00:00
|
|
|
children := make(map[string]*compositedav.Child, len(shares))
|
2024-02-09 17:26:43 +00:00
|
|
|
for _, share := range shares {
|
2024-02-21 12:40:12 +00:00
|
|
|
children[share.Name] = s.buildChild(share)
|
2024-02-09 17:26:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s.mu.Lock()
|
|
|
|
s.shares = shares
|
|
|
|
oldUserServers := s.userServers
|
2024-02-21 12:40:12 +00:00
|
|
|
oldChildren := s.children
|
|
|
|
s.children = children
|
2024-02-09 17:26:43 +00:00
|
|
|
s.userServers = userServers
|
|
|
|
s.mu.Unlock()
|
|
|
|
|
|
|
|
s.stopUserServers(oldUserServers)
|
2024-02-21 12:40:12 +00:00
|
|
|
s.closeChildren(oldChildren)
|
2024-02-09 17:26:43 +00:00
|
|
|
}
|
|
|
|
|
2024-04-02 20:32:30 +00:00
|
|
|
func (s *FileSystemForRemote) buildChild(share *drive.Share) *compositedav.Child {
|
2024-05-01 15:45:57 +00:00
|
|
|
getTokenAndAddr := func(shareName string) (string, string, error) {
|
|
|
|
s.mu.RLock()
|
|
|
|
var share *drive.Share
|
|
|
|
i, shareFound := slices.BinarySearchFunc(s.shares, shareName, func(s *drive.Share, name string) int {
|
|
|
|
return strings.Compare(s.Name, name)
|
|
|
|
})
|
|
|
|
if shareFound {
|
|
|
|
share = s.shares[i]
|
|
|
|
}
|
|
|
|
userServers := s.userServers
|
|
|
|
fileServerTokenAndAddr := s.fileServerTokenAndAddr
|
|
|
|
s.mu.RUnlock()
|
|
|
|
|
|
|
|
if !shareFound {
|
|
|
|
return "", "", fmt.Errorf("unknown share %v", shareName)
|
|
|
|
}
|
|
|
|
|
|
|
|
var tokenAndAddr string
|
|
|
|
if !drive.AllowShareAs() {
|
|
|
|
tokenAndAddr = fileServerTokenAndAddr
|
|
|
|
} else {
|
|
|
|
userServer, found := userServers[share.As]
|
|
|
|
if found {
|
|
|
|
userServer.mu.RLock()
|
|
|
|
tokenAndAddr = userServer.tokenAndAddr
|
|
|
|
userServer.mu.RUnlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if tokenAndAddr == "" {
|
|
|
|
return "", "", fmt.Errorf("unable to determine address for share %v", shareName)
|
|
|
|
}
|
|
|
|
|
|
|
|
parts := strings.Split(tokenAndAddr, "|")
|
|
|
|
if len(parts) != 2 {
|
|
|
|
return "", "", fmt.Errorf("invalid address for share %v", shareName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return parts[0], parts[1], nil
|
|
|
|
}
|
|
|
|
|
2024-02-21 12:40:12 +00:00
|
|
|
return &compositedav.Child{
|
|
|
|
Child: &dirfs.Child{
|
|
|
|
Name: share.Name,
|
|
|
|
},
|
2024-05-01 15:45:57 +00:00
|
|
|
BaseURL: func() (string, error) {
|
|
|
|
secretToken, _, err := getTokenAndAddr(share.Name)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("http://%s/%s/%s", hex.EncodeToString([]byte(share.Name)), secretToken, url.PathEscape(share.Name)), nil
|
|
|
|
},
|
2024-02-09 17:26:43 +00:00
|
|
|
Transport: &http.Transport{
|
2024-06-11 02:38:10 +00:00
|
|
|
DialContext: func(ctx context.Context, _, shareAddr string) (net.Conn, error) {
|
2024-02-09 17:26:43 +00:00
|
|
|
shareNameHex, _, err := net.SplitHostPort(shareAddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to parse share address %v: %w", shareAddr, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We had to encode the share name in hex to make sure it's a valid hostname
|
|
|
|
shareNameBytes, err := hex.DecodeString(shareNameHex)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to decode share name from host %v: %v", shareNameHex, err)
|
|
|
|
}
|
|
|
|
shareName := string(shareNameBytes)
|
|
|
|
|
2024-05-01 15:45:57 +00:00
|
|
|
_, addr, err := getTokenAndAddr(shareName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2024-02-09 17:26:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = netip.ParseAddrPort(addr)
|
|
|
|
if err == nil {
|
|
|
|
// this is a regular network address, dial normally
|
2024-06-11 02:38:10 +00:00
|
|
|
var std net.Dialer
|
|
|
|
return std.DialContext(ctx, "tcp", addr)
|
2024-02-09 17:26:43 +00:00
|
|
|
}
|
|
|
|
// assume this is a safesocket address
|
2024-06-11 02:38:10 +00:00
|
|
|
return safesocket.ConnectContext(ctx, addr)
|
2024-02-09 17:26:43 +00:00
|
|
|
},
|
|
|
|
},
|
2024-02-21 12:40:12 +00:00
|
|
|
}
|
2024-02-09 17:26:43 +00:00
|
|
|
}
|
|
|
|
|
2024-04-03 17:09:58 +00:00
|
|
|
// ServeHTTPWithPerms implements drive.FileSystemForRemote.
|
2024-04-02 20:32:30 +00:00
|
|
|
func (s *FileSystemForRemote) ServeHTTPWithPerms(permissions drive.Permissions, w http.ResponseWriter, r *http.Request) {
|
2024-02-09 17:26:43 +00:00
|
|
|
isWrite := writeMethods[r.Method]
|
|
|
|
if isWrite {
|
|
|
|
share := shared.CleanAndSplit(r.URL.Path)[0]
|
|
|
|
switch permissions.For(share) {
|
2024-04-02 20:32:30 +00:00
|
|
|
case drive.PermissionNone:
|
2024-02-09 17:26:43 +00:00
|
|
|
// If we have no permissions to this share, treat it as not found
|
|
|
|
// to avoid leaking any information about the share's existence.
|
|
|
|
http.Error(w, "not found", http.StatusNotFound)
|
|
|
|
return
|
2024-04-02 20:32:30 +00:00
|
|
|
case drive.PermissionReadOnly:
|
2024-02-09 17:26:43 +00:00
|
|
|
http.Error(w, "permission denied", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s.mu.RLock()
|
2024-02-21 12:40:12 +00:00
|
|
|
childrenMap := s.children
|
2024-02-09 17:26:43 +00:00
|
|
|
s.mu.RUnlock()
|
|
|
|
|
2024-02-21 12:40:12 +00:00
|
|
|
children := make([]*compositedav.Child, 0, len(childrenMap))
|
2024-02-09 17:26:43 +00:00
|
|
|
// filter out shares to which the connecting principal has no access
|
2024-02-21 12:40:12 +00:00
|
|
|
for name, child := range childrenMap {
|
2024-04-02 20:32:30 +00:00
|
|
|
if permissions.For(name) == drive.PermissionNone {
|
2024-02-09 17:26:43 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-02-21 12:40:12 +00:00
|
|
|
children = append(children, child)
|
2024-02-09 17:26:43 +00:00
|
|
|
}
|
|
|
|
|
2024-02-21 12:40:12 +00:00
|
|
|
h := compositedav.Handler{
|
|
|
|
Logf: s.logf,
|
2024-02-09 17:26:43 +00:00
|
|
|
}
|
2024-02-21 12:40:12 +00:00
|
|
|
h.SetChildren("", children...)
|
2024-02-09 17:26:43 +00:00
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FileSystemForRemote) stopUserServers(userServers map[string]*userServer) {
|
|
|
|
for _, server := range userServers {
|
|
|
|
if err := server.Close(); err != nil {
|
2024-04-04 20:07:58 +00:00
|
|
|
s.logf("error closing taildrive user server: %v", err)
|
2024-02-09 17:26:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-21 12:40:12 +00:00
|
|
|
func (s *FileSystemForRemote) closeChildren(children map[string]*compositedav.Child) {
|
|
|
|
for _, child := range children {
|
|
|
|
child.CloseIdleConnections()
|
2024-02-09 17:26:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-03 17:09:58 +00:00
|
|
|
// Close() implements drive.FileSystemForRemote.
|
2024-02-09 17:26:43 +00:00
|
|
|
func (s *FileSystemForRemote) Close() error {
|
|
|
|
s.mu.Lock()
|
|
|
|
userServers := s.userServers
|
2024-02-21 12:40:12 +00:00
|
|
|
children := s.children
|
|
|
|
s.userServers = make(map[string]*userServer)
|
|
|
|
s.children = make(map[string]*compositedav.Child)
|
2024-02-09 17:26:43 +00:00
|
|
|
s.mu.Unlock()
|
|
|
|
|
|
|
|
s.stopUserServers(userServers)
|
2024-02-21 12:40:12 +00:00
|
|
|
s.closeChildren(children)
|
2024-02-09 17:26:43 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-04-04 20:07:58 +00:00
|
|
|
// userServer runs tailscaled serve-taildrive to serve webdav content for the
|
2024-02-09 17:26:43 +00:00
|
|
|
// given Shares. All Shares are assumed to have the same Share.As, and the
|
|
|
|
// content is served as that Share.As user.
|
|
|
|
type userServer struct {
|
2024-03-01 03:24:42 +00:00
|
|
|
logf logger.Logf
|
2024-04-02 20:32:30 +00:00
|
|
|
shares []*drive.Share
|
2024-03-01 03:24:42 +00:00
|
|
|
username string
|
|
|
|
executable string
|
2024-02-09 17:26:43 +00:00
|
|
|
|
|
|
|
// mu guards the below values. Acquire a write lock before updating any of
|
|
|
|
// them, acquire a read lock before reading any of them.
|
2024-05-01 15:45:57 +00:00
|
|
|
mu sync.RWMutex
|
|
|
|
cmd *exec.Cmd
|
|
|
|
tokenAndAddr string
|
|
|
|
closed bool
|
2024-02-09 17:26:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *userServer) Close() error {
|
|
|
|
s.mu.Lock()
|
|
|
|
cmd := s.cmd
|
|
|
|
s.closed = true
|
|
|
|
s.mu.Unlock()
|
|
|
|
if cmd != nil && cmd.Process != nil {
|
|
|
|
return cmd.Process.Kill()
|
|
|
|
}
|
|
|
|
// not running, that's okay
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *userServer) runLoop() {
|
|
|
|
maxSleepTime := 30 * time.Second
|
|
|
|
consecutiveFailures := float64(0)
|
|
|
|
var timeOfLastFailure time.Time
|
|
|
|
for {
|
|
|
|
s.mu.RLock()
|
|
|
|
closed := s.closed
|
|
|
|
s.mu.RUnlock()
|
|
|
|
if closed {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-01 03:24:42 +00:00
|
|
|
err := s.run()
|
2024-02-09 17:26:43 +00:00
|
|
|
now := time.Now()
|
|
|
|
timeSinceLastFailure := now.Sub(timeOfLastFailure)
|
|
|
|
timeOfLastFailure = now
|
|
|
|
if timeSinceLastFailure < maxSleepTime {
|
|
|
|
consecutiveFailures++
|
|
|
|
} else {
|
|
|
|
consecutiveFailures = 1
|
|
|
|
}
|
|
|
|
sleepTime := time.Duration(math.Pow(2, consecutiveFailures)) * time.Millisecond
|
|
|
|
if sleepTime > maxSleepTime {
|
|
|
|
sleepTime = maxSleepTime
|
|
|
|
}
|
2024-03-01 03:24:42 +00:00
|
|
|
s.logf("user server % v stopped with error %v, will try again in %v", s.executable, err, sleepTime)
|
2024-02-09 17:26:43 +00:00
|
|
|
time.Sleep(sleepTime)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-01 03:24:42 +00:00
|
|
|
// Run runs the user server using the configured executable. This function only
|
|
|
|
// works on UNIX systems, but those are the only ones on which we use
|
|
|
|
// userServers anyway.
|
|
|
|
func (s *userServer) run() error {
|
2024-02-09 17:26:43 +00:00
|
|
|
// set up the command
|
2024-04-04 20:07:58 +00:00
|
|
|
args := []string{"serve-taildrive"}
|
2024-02-09 17:26:43 +00:00
|
|
|
for _, s := range s.shares {
|
|
|
|
args = append(args, s.Name, s.Path)
|
|
|
|
}
|
2024-03-01 03:24:42 +00:00
|
|
|
var cmd *exec.Cmd
|
2024-08-29 20:32:11 +00:00
|
|
|
if su := s.canSU(); su != "" {
|
2024-04-04 20:07:58 +00:00
|
|
|
s.logf("starting taildrive file server as user %q", s.username)
|
2024-08-29 20:32:11 +00:00
|
|
|
// Quote and escape arguments. Use single quotes to prevent shell substitutions.
|
|
|
|
for i, arg := range args {
|
|
|
|
args[i] = "'" + strings.ReplaceAll(arg, "'", "'\"'\"'") + "'"
|
|
|
|
}
|
|
|
|
cmdString := fmt.Sprintf("%s %s", s.executable, strings.Join(args, " "))
|
|
|
|
allArgs := []string{s.username, "-c", cmdString}
|
|
|
|
cmd = exec.Command(su, allArgs...)
|
2024-03-01 03:24:42 +00:00
|
|
|
} else {
|
|
|
|
// If we were root, we should have been able to sudo as a specific
|
|
|
|
// user, but let's check just to make sure, since we never want to
|
|
|
|
// access shared folders as root.
|
|
|
|
err := s.assertNotRoot()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-04-04 20:07:58 +00:00
|
|
|
s.logf("starting taildrive file server as ourselves")
|
2024-03-01 03:24:42 +00:00
|
|
|
cmd = exec.Command(s.executable, args...)
|
|
|
|
}
|
2024-02-09 17:26:43 +00:00
|
|
|
stdout, err := cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("stdout pipe: %w", err)
|
|
|
|
}
|
|
|
|
defer stdout.Close()
|
|
|
|
stderr, err := cmd.StderrPipe()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("stderr pipe: %w", err)
|
|
|
|
}
|
|
|
|
defer stderr.Close()
|
|
|
|
|
|
|
|
err = cmd.Start()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("start: %w", err)
|
|
|
|
}
|
|
|
|
s.mu.Lock()
|
|
|
|
s.cmd = cmd
|
|
|
|
s.mu.Unlock()
|
|
|
|
|
|
|
|
// read address
|
|
|
|
stdoutScanner := bufio.NewScanner(stdout)
|
|
|
|
stdoutScanner.Scan()
|
|
|
|
if stdoutScanner.Err() != nil {
|
|
|
|
return fmt.Errorf("read addr: %w", stdoutScanner.Err())
|
|
|
|
}
|
|
|
|
addr := stdoutScanner.Text()
|
|
|
|
// send the rest of stdout and stderr to logger to avoid blocking
|
|
|
|
go func() {
|
|
|
|
for stdoutScanner.Scan() {
|
2024-04-04 20:07:58 +00:00
|
|
|
s.logf("tailscaled serve-taildrive stdout: %v", stdoutScanner.Text())
|
2024-02-09 17:26:43 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
stderrScanner := bufio.NewScanner(stderr)
|
|
|
|
go func() {
|
|
|
|
for stderrScanner.Scan() {
|
2024-04-04 20:07:58 +00:00
|
|
|
s.logf("tailscaled serve-taildrive stderr: %v", stderrScanner.Text())
|
2024-02-09 17:26:43 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
s.mu.Lock()
|
2024-05-01 15:45:57 +00:00
|
|
|
s.tokenAndAddr = strings.TrimSpace(addr)
|
2024-02-09 17:26:43 +00:00
|
|
|
s.mu.Unlock()
|
|
|
|
return cmd.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
var writeMethods = map[string]bool{
|
|
|
|
"PUT": true,
|
|
|
|
"POST": true,
|
|
|
|
"COPY": true,
|
|
|
|
"LOCK": true,
|
|
|
|
"UNLOCK": true,
|
|
|
|
"MKCOL": true,
|
|
|
|
"MOVE": true,
|
|
|
|
"PROPPATCH": true,
|
2024-05-01 01:31:49 +00:00
|
|
|
"DELETE": true,
|
2024-02-09 17:26:43 +00:00
|
|
|
}
|
2024-03-01 03:24:42 +00:00
|
|
|
|
2024-08-29 20:32:11 +00:00
|
|
|
// canSU checks whether the current process can run su with the right username.
|
|
|
|
// If su can be run, this returns the path to the su command.
|
|
|
|
// If not, this returns the empty string "".
|
|
|
|
func (s *userServer) canSU() string {
|
|
|
|
su, err := exec.LookPath("su")
|
|
|
|
if err != nil {
|
|
|
|
s.logf("can't find su command: %v", err)
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// First try to execute su <user> -c true to make sure we can su.
|
|
|
|
err = exec.Command(
|
|
|
|
su,
|
|
|
|
s.username,
|
|
|
|
"-c", "true",
|
|
|
|
).Run()
|
|
|
|
if err != nil {
|
|
|
|
s.logf("su check failed: %s", err)
|
|
|
|
return ""
|
2024-03-01 03:24:42 +00:00
|
|
|
}
|
2024-08-29 20:32:11 +00:00
|
|
|
|
|
|
|
return su
|
2024-03-01 03:24:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// assertNotRoot returns an error if the current user has UID 0 or if we cannot
|
|
|
|
// determine the current user.
|
|
|
|
//
|
|
|
|
// On Linux, root users will always have UID 0.
|
|
|
|
//
|
|
|
|
// On BSD, root users should always have UID 0.
|
|
|
|
func (s *userServer) assertNotRoot() error {
|
|
|
|
u, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("assertNotRoot failed to find current user: %s", err)
|
|
|
|
}
|
|
|
|
if u.Uid == "0" {
|
|
|
|
return fmt.Errorf("%q is root", u.Name)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|