user.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. //go:build linux || (darwin && !ios) || freebsd || openbsd
  4. package tailssh
  5. import (
  6. "io"
  7. "os"
  8. "os/exec"
  9. "os/user"
  10. "path/filepath"
  11. "runtime"
  12. "strconv"
  13. "strings"
  14. "go4.org/mem"
  15. "tailscale.com/envknob"
  16. "tailscale.com/hostinfo"
  17. "tailscale.com/util/lineread"
  18. "tailscale.com/util/osuser"
  19. "tailscale.com/version/distro"
  20. )
  21. // userMeta is a wrapper around *user.User with extra fields.
  22. type userMeta struct {
  23. user.User
  24. // loginShellCached is the user's login shell, if known
  25. // at the time of userLookup.
  26. loginShellCached string
  27. }
  28. // GroupIds returns the list of group IDs that the user is a member of.
  29. func (u *userMeta) GroupIds() ([]string, error) {
  30. if runtime.GOOS == "linux" && distro.Get() == distro.Gokrazy {
  31. // Gokrazy is a single-user appliance with ~no userspace.
  32. // There aren't users to look up (no /etc/passwd, etc)
  33. // so rather than fail below, just hardcode root.
  34. // TODO(bradfitz): fix os/user upstream instead?
  35. return []string{"0"}, nil
  36. }
  37. return u.User.GroupIds()
  38. }
  39. // userLookup is like os/user.Lookup but it returns a *userMeta wrapper
  40. // around a *user.User with extra fields.
  41. func userLookup(username string) (*userMeta, error) {
  42. u, s, err := osuser.LookupByUsernameWithShell(username)
  43. if err != nil {
  44. return nil, err
  45. }
  46. return &userMeta{User: *u, loginShellCached: s}, nil
  47. }
  48. func (u *userMeta) LoginShell() string {
  49. if u.loginShellCached != "" {
  50. // This field should be populated on Linux, at least, because
  51. // func userLookup on Linux uses "getent" to look up the user
  52. // and that populates it.
  53. return u.loginShellCached
  54. }
  55. switch runtime.GOOS {
  56. case "darwin":
  57. // Note: /Users/username is key, and not the same as u.HomeDir.
  58. out, _ := exec.Command("dscl", ".", "-read", filepath.Join("/Users", u.Username), "UserShell").Output()
  59. // out is "UserShell: /bin/bash"
  60. s, ok := strings.CutPrefix(string(out), "UserShell: ")
  61. if ok {
  62. return strings.TrimSpace(s)
  63. }
  64. }
  65. if e := os.Getenv("SHELL"); e != "" {
  66. return e
  67. }
  68. return "/bin/sh"
  69. }
  70. // defaultPathTmpl specifies the default PATH template to use for new sessions.
  71. //
  72. // If empty, a default value is used based on the OS & distro to match OpenSSH's
  73. // usually-hardcoded behavior. (see
  74. // https://github.com/tailscale/tailscale/issues/5285 for background).
  75. //
  76. // The template may contain @{HOME} or @{PAM_USER} which expand to the user's
  77. // home directory and username, respectively. (PAM is not used, despite the
  78. // name)
  79. var defaultPathTmpl = envknob.RegisterString("TAILSCALE_SSH_DEFAULT_PATH")
  80. func defaultPathForUser(u *user.User) string {
  81. if s := defaultPathTmpl(); s != "" {
  82. return expandDefaultPathTmpl(s, u)
  83. }
  84. isRoot := u.Uid == "0"
  85. switch distro.Get() {
  86. case distro.Debian:
  87. hi := hostinfo.New()
  88. if hi.Distro == "ubuntu" {
  89. // distro.Get's Debian includes Ubuntu. But see if it's actually Ubuntu.
  90. // Ubuntu doesn't empirically seem to distinguish between root and non-root for the default.
  91. // And it includes /snap/bin.
  92. return "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin"
  93. }
  94. if isRoot {
  95. return "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
  96. }
  97. return "/usr/local/bin:/usr/bin:/bin:/usr/bn/games"
  98. case distro.NixOS:
  99. return defaultPathForUserOnNixOS(u)
  100. }
  101. if isRoot {
  102. return "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
  103. }
  104. return "/usr/local/bin:/usr/bin:/bin"
  105. }
  106. func defaultPathForUserOnNixOS(u *user.User) string {
  107. var path string
  108. lineread.File("/etc/pam/environment", func(lineb []byte) error {
  109. if v := pathFromPAMEnvLine(lineb, u); v != "" {
  110. path = v
  111. return io.EOF // stop iteration
  112. }
  113. return nil
  114. })
  115. return path
  116. }
  117. func pathFromPAMEnvLine(line []byte, u *user.User) (path string) {
  118. if !mem.HasPrefix(mem.B(line), mem.S("PATH")) {
  119. return ""
  120. }
  121. rest := strings.TrimSpace(strings.TrimPrefix(string(line), "PATH"))
  122. if quoted, ok := strings.CutPrefix(rest, "DEFAULT="); ok {
  123. if path, err := strconv.Unquote(quoted); err == nil {
  124. return expandDefaultPathTmpl(path, u)
  125. }
  126. }
  127. return ""
  128. }
  129. func expandDefaultPathTmpl(t string, u *user.User) string {
  130. p := strings.NewReplacer(
  131. "@{HOME}", u.HomeDir,
  132. "@{PAM_USER}", u.Username,
  133. ).Replace(t)
  134. if strings.Contains(p, "@{") {
  135. // If there are unknown expansions, conservatively fail closed.
  136. return ""
  137. }
  138. return p
  139. }