2
0

user.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. return osuser.GetGroupIds(&u.User)
  31. }
  32. // userLookup is like os/user.Lookup but it returns a *userMeta wrapper
  33. // around a *user.User with extra fields.
  34. func userLookup(username string) (*userMeta, error) {
  35. u, s, err := osuser.LookupByUsernameWithShell(username)
  36. if err != nil {
  37. return nil, err
  38. }
  39. return &userMeta{User: *u, loginShellCached: s}, nil
  40. }
  41. func (u *userMeta) LoginShell() string {
  42. if u.loginShellCached != "" {
  43. // This field should be populated on Linux, at least, because
  44. // func userLookup on Linux uses "getent" to look up the user
  45. // and that populates it.
  46. return u.loginShellCached
  47. }
  48. switch runtime.GOOS {
  49. case "darwin":
  50. // Note: /Users/username is key, and not the same as u.HomeDir.
  51. out, _ := exec.Command("dscl", ".", "-read", filepath.Join("/Users", u.Username), "UserShell").Output()
  52. // out is "UserShell: /bin/bash"
  53. s, ok := strings.CutPrefix(string(out), "UserShell: ")
  54. if ok {
  55. return strings.TrimSpace(s)
  56. }
  57. }
  58. if e := os.Getenv("SHELL"); e != "" {
  59. return e
  60. }
  61. return "/bin/sh"
  62. }
  63. // defaultPathTmpl specifies the default PATH template to use for new sessions.
  64. //
  65. // If empty, a default value is used based on the OS & distro to match OpenSSH's
  66. // usually-hardcoded behavior. (see
  67. // https://github.com/tailscale/tailscale/issues/5285 for background).
  68. //
  69. // The template may contain @{HOME} or @{PAM_USER} which expand to the user's
  70. // home directory and username, respectively. (PAM is not used, despite the
  71. // name)
  72. var defaultPathTmpl = envknob.RegisterString("TAILSCALE_SSH_DEFAULT_PATH")
  73. func defaultPathForUser(u *user.User) string {
  74. if s := defaultPathTmpl(); s != "" {
  75. return expandDefaultPathTmpl(s, u)
  76. }
  77. isRoot := u.Uid == "0"
  78. switch distro.Get() {
  79. case distro.Debian:
  80. hi := hostinfo.New()
  81. if hi.Distro == "ubuntu" {
  82. // distro.Get's Debian includes Ubuntu. But see if it's actually Ubuntu.
  83. // Ubuntu doesn't empirically seem to distinguish between root and non-root for the default.
  84. // And it includes /snap/bin.
  85. return "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin"
  86. }
  87. if isRoot {
  88. return "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
  89. }
  90. return "/usr/local/bin:/usr/bin:/bin:/usr/bn/games"
  91. case distro.NixOS:
  92. return defaultPathForUserOnNixOS(u)
  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"
  98. }
  99. func defaultPathForUserOnNixOS(u *user.User) string {
  100. var path string
  101. lineread.File("/etc/pam/environment", func(lineb []byte) error {
  102. if v := pathFromPAMEnvLine(lineb, u); v != "" {
  103. path = v
  104. return io.EOF // stop iteration
  105. }
  106. return nil
  107. })
  108. return path
  109. }
  110. func pathFromPAMEnvLine(line []byte, u *user.User) (path string) {
  111. if !mem.HasPrefix(mem.B(line), mem.S("PATH")) {
  112. return ""
  113. }
  114. rest := strings.TrimSpace(strings.TrimPrefix(string(line), "PATH"))
  115. if quoted, ok := strings.CutPrefix(rest, "DEFAULT="); ok {
  116. if path, err := strconv.Unquote(quoted); err == nil {
  117. return expandDefaultPathTmpl(path, u)
  118. }
  119. }
  120. return ""
  121. }
  122. func expandDefaultPathTmpl(t string, u *user.User) string {
  123. p := strings.NewReplacer(
  124. "@{HOME}", u.HomeDir,
  125. "@{PAM_USER}", u.Username,
  126. ).Replace(t)
  127. if strings.Contains(p, "@{") {
  128. // If there are unknown expansions, conservatively fail closed.
  129. return ""
  130. }
  131. return p
  132. }