user.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. "context"
  7. "errors"
  8. "io"
  9. "log"
  10. "os"
  11. "os/exec"
  12. "os/user"
  13. "path/filepath"
  14. "runtime"
  15. "strconv"
  16. "strings"
  17. "time"
  18. "unicode/utf8"
  19. "go4.org/mem"
  20. "tailscale.com/envknob"
  21. "tailscale.com/hostinfo"
  22. "tailscale.com/util/lineread"
  23. "tailscale.com/version/distro"
  24. )
  25. // userMeta is a wrapper around *user.User with extra fields.
  26. type userMeta struct {
  27. user.User
  28. // loginShellCached is the user's login shell, if known
  29. // at the time of userLookup.
  30. loginShellCached string
  31. }
  32. // GroupIds returns the list of group IDs that the user is a member of.
  33. func (u *userMeta) GroupIds() ([]string, error) {
  34. if runtime.GOOS == "linux" && distro.Get() == distro.Gokrazy {
  35. // Gokrazy is a single-user appliance with ~no userspace.
  36. // There aren't users to look up (no /etc/passwd, etc)
  37. // so rather than fail below, just hardcode root.
  38. // TODO(bradfitz): fix os/user upstream instead?
  39. return []string{"0"}, nil
  40. }
  41. return u.User.GroupIds()
  42. }
  43. // userLookup is like os/user.Lookup but it returns a *userMeta wrapper
  44. // around a *user.User with extra fields.
  45. func userLookup(username string) (*userMeta, error) {
  46. if runtime.GOOS != "linux" {
  47. return userLookupStd(username)
  48. }
  49. // No getent on Gokrazy. So hard-code the login shell.
  50. if distro.Get() == distro.Gokrazy {
  51. um, err := userLookupStd(username)
  52. if err != nil {
  53. um = &userMeta{
  54. User: user.User{
  55. Uid: "0",
  56. Gid: "0",
  57. Username: "root",
  58. Name: "Gokrazy",
  59. HomeDir: "/",
  60. },
  61. }
  62. }
  63. um.loginShellCached = "/tmp/serial-busybox/ash"
  64. return um, err
  65. }
  66. // On Linux, default to using "getent" to look up users so that
  67. // even with static tailscaled binaries without cgo (as we distribute),
  68. // we can still look up PAM/NSS users which the standard library's
  69. // os/user without cgo won't get (because of no libc hooks).
  70. // But if "getent" fails, userLookupGetent falls back to the standard
  71. // library anyway.
  72. return userLookupGetent(username)
  73. }
  74. func validUsername(uid string) bool {
  75. maxUid := 32
  76. if runtime.GOOS == "linux" {
  77. maxUid = 256
  78. }
  79. if len(uid) > maxUid || len(uid) == 0 {
  80. return false
  81. }
  82. for _, r := range uid {
  83. if r < ' ' || r == 0x7f || r == utf8.RuneError { // TODO(bradfitz): more?
  84. return false
  85. }
  86. }
  87. return true
  88. }
  89. func userLookupGetent(username string) (*userMeta, error) {
  90. // Do some basic validation before passing this string to "getent", even though
  91. // getent should do its own validation.
  92. if !validUsername(username) {
  93. return nil, errors.New("invalid username")
  94. }
  95. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  96. defer cancel()
  97. out, err := exec.CommandContext(ctx, "getent", "passwd", username).Output()
  98. if err != nil {
  99. log.Printf("error calling getent for user %q: %v", username, err)
  100. return userLookupStd(username)
  101. }
  102. // output is "alice:x:1001:1001:Alice Smith,,,:/home/alice:/bin/bash"
  103. f := strings.SplitN(strings.TrimSpace(string(out)), ":", 10)
  104. for len(f) < 7 {
  105. f = append(f, "")
  106. }
  107. um := &userMeta{
  108. User: user.User{
  109. Username: f[0],
  110. Uid: f[2],
  111. Gid: f[3],
  112. Name: f[4],
  113. HomeDir: f[5],
  114. },
  115. loginShellCached: f[6],
  116. }
  117. return um, nil
  118. }
  119. func userLookupStd(username string) (*userMeta, error) {
  120. u, err := user.Lookup(username)
  121. if err != nil {
  122. return nil, err
  123. }
  124. return &userMeta{User: *u}, nil
  125. }
  126. func (u *userMeta) LoginShell() string {
  127. if u.loginShellCached != "" {
  128. // This field should be populated on Linux, at least, because
  129. // func userLookup on Linux uses "getent" to look up the user
  130. // and that populates it.
  131. return u.loginShellCached
  132. }
  133. switch runtime.GOOS {
  134. case "darwin":
  135. // Note: /Users/username is key, and not the same as u.HomeDir.
  136. out, _ := exec.Command("dscl", ".", "-read", filepath.Join("/Users", u.Username), "UserShell").Output()
  137. // out is "UserShell: /bin/bash"
  138. s, ok := strings.CutPrefix(string(out), "UserShell: ")
  139. if ok {
  140. return strings.TrimSpace(s)
  141. }
  142. }
  143. if e := os.Getenv("SHELL"); e != "" {
  144. return e
  145. }
  146. return "/bin/sh"
  147. }
  148. // defaultPathTmpl specifies the default PATH template to use for new sessions.
  149. //
  150. // If empty, a default value is used based on the OS & distro to match OpenSSH's
  151. // usually-hardcoded behavior. (see
  152. // https://github.com/tailscale/tailscale/issues/5285 for background).
  153. //
  154. // The template may contain @{HOME} or @{PAM_USER} which expand to the user's
  155. // home directory and username, respectively. (PAM is not used, despite the
  156. // name)
  157. var defaultPathTmpl = envknob.RegisterString("TAILSCALE_SSH_DEFAULT_PATH")
  158. func defaultPathForUser(u *user.User) string {
  159. if s := defaultPathTmpl(); s != "" {
  160. return expandDefaultPathTmpl(s, u)
  161. }
  162. isRoot := u.Uid == "0"
  163. switch distro.Get() {
  164. case distro.Debian:
  165. hi := hostinfo.New()
  166. if hi.Distro == "ubuntu" {
  167. // distro.Get's Debian includes Ubuntu. But see if it's actually Ubuntu.
  168. // Ubuntu doesn't empirically seem to distinguish between root and non-root for the default.
  169. // And it includes /snap/bin.
  170. return "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin"
  171. }
  172. if isRoot {
  173. return "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
  174. }
  175. return "/usr/local/bin:/usr/bin:/bin:/usr/bn/games"
  176. case distro.NixOS:
  177. return defaultPathForUserOnNixOS(u)
  178. }
  179. if isRoot {
  180. return "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
  181. }
  182. return "/usr/local/bin:/usr/bin:/bin"
  183. }
  184. func defaultPathForUserOnNixOS(u *user.User) string {
  185. var path string
  186. lineread.File("/etc/pam/environment", func(lineb []byte) error {
  187. if v := pathFromPAMEnvLine(lineb, u); v != "" {
  188. path = v
  189. return io.EOF // stop iteration
  190. }
  191. return nil
  192. })
  193. return path
  194. }
  195. func pathFromPAMEnvLine(line []byte, u *user.User) (path string) {
  196. if !mem.HasPrefix(mem.B(line), mem.S("PATH")) {
  197. return ""
  198. }
  199. rest := strings.TrimSpace(strings.TrimPrefix(string(line), "PATH"))
  200. if quoted, ok := strings.CutPrefix(rest, "DEFAULT="); ok {
  201. if path, err := strconv.Unquote(quoted); err == nil {
  202. return expandDefaultPathTmpl(path, u)
  203. }
  204. }
  205. return ""
  206. }
  207. func expandDefaultPathTmpl(t string, u *user.User) string {
  208. p := strings.NewReplacer(
  209. "@{HOME}", u.HomeDir,
  210. "@{PAM_USER}", u.Username,
  211. ).Replace(t)
  212. if strings.Contains(p, "@{") {
  213. // If there are unknown expansions, conservatively fail closed.
  214. return ""
  215. }
  216. return p
  217. }