2023-05-08 17:42:31 +01:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
|
|
|
|
//go:build linux || (darwin && !ios) || freebsd || openbsd
|
|
|
|
|
|
|
|
package tailssh
|
|
|
|
|
|
|
|
import (
|
2023-05-08 20:21:37 +01:00
|
|
|
"io"
|
|
|
|
"os"
|
2023-05-08 17:42:31 +01:00
|
|
|
"os/exec"
|
|
|
|
"os/user"
|
2023-05-08 20:21:37 +01:00
|
|
|
"path/filepath"
|
2023-05-08 17:42:31 +01:00
|
|
|
"runtime"
|
2023-05-08 20:21:37 +01:00
|
|
|
"strconv"
|
2023-05-08 17:42:31 +01:00
|
|
|
"strings"
|
|
|
|
|
2023-05-08 20:21:37 +01:00
|
|
|
"go4.org/mem"
|
|
|
|
"tailscale.com/envknob"
|
|
|
|
"tailscale.com/hostinfo"
|
|
|
|
"tailscale.com/util/lineread"
|
2023-11-09 17:49:06 +00:00
|
|
|
"tailscale.com/util/osuser"
|
2023-05-08 17:42:31 +01:00
|
|
|
"tailscale.com/version/distro"
|
|
|
|
)
|
|
|
|
|
|
|
|
// userMeta is a wrapper around *user.User with extra fields.
|
|
|
|
type userMeta struct {
|
|
|
|
user.User
|
|
|
|
|
2023-05-08 20:21:37 +01:00
|
|
|
// loginShellCached is the user's login shell, if known
|
|
|
|
// at the time of userLookup.
|
|
|
|
loginShellCached string
|
2023-05-08 17:42:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GroupIds returns the list of group IDs that the user is a member of.
|
|
|
|
func (u *userMeta) GroupIds() ([]string, error) {
|
|
|
|
if runtime.GOOS == "linux" && distro.Get() == distro.Gokrazy {
|
|
|
|
// Gokrazy is a single-user appliance with ~no userspace.
|
|
|
|
// There aren't users to look up (no /etc/passwd, etc)
|
|
|
|
// so rather than fail below, just hardcode root.
|
|
|
|
// TODO(bradfitz): fix os/user upstream instead?
|
|
|
|
return []string{"0"}, nil
|
|
|
|
}
|
|
|
|
return u.User.GroupIds()
|
|
|
|
}
|
|
|
|
|
2023-05-21 15:35:33 +01:00
|
|
|
// userLookup is like os/user.Lookup but it returns a *userMeta wrapper
|
2023-05-08 17:42:31 +01:00
|
|
|
// around a *user.User with extra fields.
|
2023-05-21 15:35:33 +01:00
|
|
|
func userLookup(username string) (*userMeta, error) {
|
2023-11-09 17:49:06 +00:00
|
|
|
u, s, err := osuser.LookupByUsernameWithShell(username)
|
2023-05-08 17:42:31 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-11-09 17:49:06 +00:00
|
|
|
return &userMeta{User: *u, loginShellCached: s}, nil
|
2023-05-08 17:42:31 +01:00
|
|
|
}
|
2023-05-08 20:21:37 +01:00
|
|
|
|
|
|
|
func (u *userMeta) LoginShell() string {
|
|
|
|
if u.loginShellCached != "" {
|
|
|
|
// This field should be populated on Linux, at least, because
|
|
|
|
// func userLookup on Linux uses "getent" to look up the user
|
|
|
|
// and that populates it.
|
|
|
|
return u.loginShellCached
|
|
|
|
}
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "darwin":
|
|
|
|
// Note: /Users/username is key, and not the same as u.HomeDir.
|
|
|
|
out, _ := exec.Command("dscl", ".", "-read", filepath.Join("/Users", u.Username), "UserShell").Output()
|
|
|
|
// out is "UserShell: /bin/bash"
|
|
|
|
s, ok := strings.CutPrefix(string(out), "UserShell: ")
|
|
|
|
if ok {
|
|
|
|
return strings.TrimSpace(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if e := os.Getenv("SHELL"); e != "" {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
return "/bin/sh"
|
|
|
|
}
|
|
|
|
|
|
|
|
// defaultPathTmpl specifies the default PATH template to use for new sessions.
|
|
|
|
//
|
|
|
|
// If empty, a default value is used based on the OS & distro to match OpenSSH's
|
|
|
|
// usually-hardcoded behavior. (see
|
|
|
|
// https://github.com/tailscale/tailscale/issues/5285 for background).
|
|
|
|
//
|
|
|
|
// The template may contain @{HOME} or @{PAM_USER} which expand to the user's
|
|
|
|
// home directory and username, respectively. (PAM is not used, despite the
|
|
|
|
// name)
|
|
|
|
var defaultPathTmpl = envknob.RegisterString("TAILSCALE_SSH_DEFAULT_PATH")
|
|
|
|
|
|
|
|
func defaultPathForUser(u *user.User) string {
|
|
|
|
if s := defaultPathTmpl(); s != "" {
|
|
|
|
return expandDefaultPathTmpl(s, u)
|
|
|
|
}
|
|
|
|
isRoot := u.Uid == "0"
|
|
|
|
switch distro.Get() {
|
|
|
|
case distro.Debian:
|
|
|
|
hi := hostinfo.New()
|
|
|
|
if hi.Distro == "ubuntu" {
|
|
|
|
// distro.Get's Debian includes Ubuntu. But see if it's actually Ubuntu.
|
|
|
|
// Ubuntu doesn't empirically seem to distinguish between root and non-root for the default.
|
|
|
|
// And it includes /snap/bin.
|
|
|
|
return "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin"
|
|
|
|
}
|
|
|
|
if isRoot {
|
|
|
|
return "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
|
|
|
|
}
|
|
|
|
return "/usr/local/bin:/usr/bin:/bin:/usr/bn/games"
|
|
|
|
case distro.NixOS:
|
|
|
|
return defaultPathForUserOnNixOS(u)
|
|
|
|
}
|
|
|
|
if isRoot {
|
|
|
|
return "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
|
|
|
|
}
|
|
|
|
return "/usr/local/bin:/usr/bin:/bin"
|
|
|
|
}
|
|
|
|
|
|
|
|
func defaultPathForUserOnNixOS(u *user.User) string {
|
|
|
|
var path string
|
|
|
|
lineread.File("/etc/pam/environment", func(lineb []byte) error {
|
|
|
|
if v := pathFromPAMEnvLine(lineb, u); v != "" {
|
|
|
|
path = v
|
|
|
|
return io.EOF // stop iteration
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
|
|
|
func pathFromPAMEnvLine(line []byte, u *user.User) (path string) {
|
|
|
|
if !mem.HasPrefix(mem.B(line), mem.S("PATH")) {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
rest := strings.TrimSpace(strings.TrimPrefix(string(line), "PATH"))
|
|
|
|
if quoted, ok := strings.CutPrefix(rest, "DEFAULT="); ok {
|
|
|
|
if path, err := strconv.Unquote(quoted); err == nil {
|
|
|
|
return expandDefaultPathTmpl(path, u)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func expandDefaultPathTmpl(t string, u *user.User) string {
|
|
|
|
p := strings.NewReplacer(
|
|
|
|
"@{HOME}", u.HomeDir,
|
|
|
|
"@{PAM_USER}", u.Username,
|
|
|
|
).Replace(t)
|
|
|
|
if strings.Contains(p, "@{") {
|
|
|
|
// If there are unknown expansions, conservatively fail closed.
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|