user.go 4.1 KB

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