mirror of
https://github.com/tailscale/tailscale.git
synced 2026-11-20 00:31:17 -08:00
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
117 lines
3.6 KiB
Go
117 lines
3.6 KiB
Go
// Copyright (c) Tailscale Inc & contributors
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
package tailssh
|
|
|
|
import (
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
|
|
"golang.org/x/sys/windows"
|
|
"golang.org/x/sys/windows/registry"
|
|
)
|
|
|
|
// GroupIds returns nil: on Windows the user's group memberships travel with
|
|
// the access token that the session process is created with, so tailssh
|
|
// never needs to enumerate them itself.
|
|
func (u *userMeta) GroupIds() ([]string, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
// isRootUser reports whether u is the Windows analog of root for the
|
|
// purposes of the "=" (autogroup:nonroot) SSH user mapping: the LocalSystem
|
|
// account or a built-in Administrator account (RID 500). Other members of the
|
|
// Administrators group are not considered root, as they are ordinary user
|
|
// accounts that happen to hold admin rights.
|
|
func isRootUser(u *userMeta) bool {
|
|
sid, err := windows.StringToSid(u.Uid)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
if sid.IsWellKnown(windows.WinLocalSystemSid) {
|
|
return true
|
|
}
|
|
const domainAdminRID = 500
|
|
n := sid.SubAuthorityCount()
|
|
return n > 1 && sid.SubAuthority(uint32(n)-1) == domainAdminRID
|
|
}
|
|
|
|
// LoginShell returns the absolute path of the shell to run for SSH sessions.
|
|
// It is the same shell for every user: the OpenSSH for Windows DefaultShell
|
|
// registry value when set, else PowerShell 7 (pwsh.exe) if installed, else
|
|
// Windows PowerShell, else cmd.exe.
|
|
func (u *userMeta) LoginShell() string {
|
|
return windowsLoginShell()
|
|
}
|
|
|
|
// openSSHRegKey is the registry key under HKEY_LOCAL_MACHINE where OpenSSH
|
|
// for Windows keeps its DefaultShell and DefaultShellCommandOption values.
|
|
// Honoring them lets Tailscale SSH sessions behave like sshd sessions on
|
|
// machines where an admin configured a shell.
|
|
const openSSHRegKey = `SOFTWARE\OpenSSH`
|
|
|
|
func openSSHRegValue(name string) string {
|
|
key, err := registry.OpenKey(registry.LOCAL_MACHINE, openSSHRegKey, registry.QUERY_VALUE|registry.WOW64_64KEY)
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
defer key.Close()
|
|
v, _, err := key.GetStringValue(name)
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
return strings.TrimSpace(v)
|
|
}
|
|
|
|
var windowsLoginShell = sync.OnceValue(func() string {
|
|
if shell := openSSHRegValue("DefaultShell"); shell != "" && filepath.IsAbs(shell) {
|
|
if p, err := exec.LookPath(shell); err == nil {
|
|
return p
|
|
}
|
|
}
|
|
if p, err := exec.LookPath("pwsh.exe"); err == nil {
|
|
return p
|
|
}
|
|
if p, err := exec.LookPath(filepath.Join(os.Getenv("ProgramFiles"), "PowerShell", "7", "pwsh.exe")); err == nil {
|
|
return p
|
|
}
|
|
if p, err := exec.LookPath(filepath.Join(systemRoot(), "System32", "WindowsPowerShell", "v1.0", "powershell.exe")); err == nil {
|
|
return p
|
|
}
|
|
return filepath.Join(systemRoot(), "System32", "cmd.exe")
|
|
})
|
|
|
|
func systemRoot() string {
|
|
if v := os.Getenv("SystemRoot"); v != "" {
|
|
return v
|
|
}
|
|
return `C:\Windows`
|
|
}
|
|
|
|
// shellCommandOption returns the flag that makes shell run a single command
|
|
// string given as the next argument: the OpenSSH for Windows
|
|
// DefaultShellCommandOption registry value when set, "/c" for cmd.exe, and
|
|
// "-c" for everything else. PowerShell and the common Unix-derived shells
|
|
// (bash, sh, zsh, fish) all accept "-c".
|
|
func shellCommandOption(shell string) string {
|
|
if opt := openSSHRegValue("DefaultShellCommandOption"); opt != "" {
|
|
return opt
|
|
}
|
|
if strings.EqualFold(filepath.Base(shell), "cmd.exe") {
|
|
return "/c"
|
|
}
|
|
return "-c"
|
|
}
|
|
|
|
// windowsShellArgs returns the arguments to pass to shell to run rawCmd, or
|
|
// to start an interactive session if rawCmd is empty.
|
|
func windowsShellArgs(shell, rawCmd string) []string {
|
|
if rawCmd == "" {
|
|
return nil
|
|
}
|
|
return []string{shellCommandOption(shell), rawCmd}
|
|
}
|