Files
tailscale/ssh/tailssh/c2n.go
Brad Fitzpatrick f4bf658006 ssh/tailssh, feature/ssh, cmd/tailscaled: run the Tailscale SSH server on Windows
Add the Windows versions of the per-OS hooks and build the SSH server on
Windows. As LocalSystem (the service), tailscaled logs the target user on
with an S4U logon via util/winutil/s4u and runs the session process with
that user's token, with pipes or, for PTYs, the s4u pseudoconsole relay.
A non-LocalSystem tailscaled can only run sessions as itself. SFTP runs
"tailscaled be-child sftp" as the user.

There is no per-connection monitor process on Windows: the session runs
under the user's token directly, so connmonitor_windows.go and
fwd_windows.go stub out the monitor and forwarding seams. TCP port
forwarding works; Unix socket forwarding and agent forwarding are
refused.

The shell follows OpenSSH for Windows's DefaultShell registry value, else
pwsh, Windows PowerShell, or cmd.exe. OpenSSH's host keys are reused when
readable. Policy user names match Windows accounts case-insensitively;
"=" treats LocalSystem and the built-in Administrator as root.

TestTailscaleSSH in tstest/integration covers the same-user path on all
platforms and, with --run-windows-service-tests, the S4U path.

Updates #4697

Signed-off-by: Brad Fitzpatrick <[email protected]>
Change-Id: I9e3f1a7c5b2d4e6f8a0c2b4d6e8f1a3c5b7d9e2f
2026-11-13 02:56:53 +00:00

143 lines
3.5 KiB
Go

// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
//go:build (linux && !android) || (darwin && !ios) || freebsd || openbsd || plan9 || windows
package tailssh
import (
"bytes"
"encoding/json"
"net/http"
"os/exec"
"runtime"
"slices"
"go4.org/mem"
"tailscale.com/ipn/ipnlocal"
"tailscale.com/tailcfg"
"tailscale.com/util/lineiter"
"tailscale.com/util/set"
)
// hookLocalUsernames, if non-nil, lists the local user accounts on
// platforms without /etc/passwd. It is set by platform-specific code.
var hookLocalUsernames func() ([]string, error)
func handleC2NSSHUsernames(b *ipnlocal.LocalBackend, w http.ResponseWriter, r *http.Request) {
var req tailcfg.C2NSSHUsernamesRequest
if r.Method == "POST" {
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
}
res, err := getSSHUsernames(b, &req)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(res)
}
// maxSSHUsernameHints caps the number of usernames returned to a client;
// the list is only a login hint.
const maxSSHUsernameHints = 100
// appendSSHUsernameHint appends u to names unless it is excluded or a
// service account, is already present, or the list already has max entries
// (so the result never exceeds max entries).
func appendSSHUsernameHint(names []string, u string, exclude set.Set[string], max int) []string {
if exclude.Contains(u) {
return names
}
switch u {
case "nobody", "daemon", "sync":
return names
}
if slices.Contains(names, u) {
return names
}
if len(names) >= max {
// Enough for a hint.
return names
}
return append(names, u)
}
// getSSHUsernames discovers and returns the list of usernames that are
// potential Tailscale SSH user targets.
func getSSHUsernames(b *ipnlocal.LocalBackend, req *tailcfg.C2NSSHUsernamesRequest) (*tailcfg.C2NSSHUsernamesResponse, error) {
res := new(tailcfg.C2NSSHUsernamesResponse)
if b == nil || !b.ShouldRunSSH() {
return res, nil
}
max := 10
if req != nil && req.Max != 0 {
// The client-supplied cap is just a hint; clamp it to a sane ceiling.
max = min(req.Max, maxSSHUsernameHints)
}
exclude := make(set.Set[string])
for u, e := range req.Exclude {
if e {
exclude.Add(u)
}
}
add := func(u string) {
res.Usernames = appendSSHUsernameHint(res.Usernames, u, exclude, max)
}
if opUser := b.OperatorUserName(); opUser != "" {
add(opUser)
}
// Check popular usernames and see if they exist with a real shell.
switch runtime.GOOS {
case "darwin":
out, err := exec.Command("dscl", ".", "list", "/Users").Output()
if err != nil {
return nil, err
}
for line := range lineiter.Bytes(out) {
line = bytes.TrimSpace(line)
if len(line) == 0 || line[0] == '_' {
continue
}
add(string(line))
}
default:
if hookLocalUsernames != nil {
names, err := hookLocalUsernames()
if err != nil {
return nil, err
}
for _, name := range names {
add(name)
}
break
}
for lr := range lineiter.File("/etc/passwd") {
line, err := lr.Value()
if err != nil {
break
}
line = bytes.TrimSpace(line)
if len(line) == 0 || line[0] == '#' || line[0] == '_' {
continue
}
if mem.HasSuffix(mem.B(line), mem.S("/nologin")) ||
mem.HasSuffix(mem.B(line), mem.S("/false")) {
continue
}
before, _, ok := bytes.Cut(line, []byte{':'})
if ok {
add(string(before))
}
}
}
return res, nil
}