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. "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 u.loginShellCached != "" {
  42. // This field should be populated on Linux, at least, because
  43. // func userLookup on Linux uses "getent" to look up the user
  44. // and that populates it.
  45. return u.loginShellCached
  46. }
  47. switch runtime.GOOS {
  48. case "darwin":
  49. // Note: /Users/username is key, and not the same as u.HomeDir.
  50. out, _ := exec.Command("dscl", ".", "-read", filepath.Join("/Users", u.Username), "UserShell").Output()
  51. // out is "UserShell: /bin/bash"
  52. s, ok := strings.CutPrefix(string(out), "UserShell: ")
  53. if ok {
  54. return strings.TrimSpace(s)
  55. }
  56. }
  57. if e := os.Getenv("SHELL"); e != "" {
  58. return e
  59. }
  60. return "/bin/sh"
  61. }
  62. // defaultPathTmpl specifies the default PATH template to use for new sessions.
  63. //
  64. // If empty, a default value is used based on the OS & distro to match OpenSSH's
  65. // usually-hardcoded behavior. (see
  66. // https://github.com/tailscale/tailscale/issues/5285 for background).
  67. //
  68. // The template may contain @{HOME} or @{PAM_USER} which expand to the user's
  69. // home directory and username, respectively. (PAM is not used, despite the
  70. // name)
  71. var defaultPathTmpl = envknob.RegisterString("TAILSCALE_SSH_DEFAULT_PATH")
  72. func defaultPathForUser(u *user.User) string {
  73. if s := defaultPathTmpl(); s != "" {
  74. return expandDefaultPathTmpl(s, u)
  75. }
  76. isRoot := u.Uid == "0"
  77. switch distro.Get() {
  78. case distro.Debian:
  79. hi := hostinfo.New()
  80. if hi.Distro == "ubuntu" {
  81. // distro.Get's Debian includes Ubuntu. But see if it's actually Ubuntu.
  82. // Ubuntu doesn't empirically seem to distinguish between root and non-root for the default.
  83. // And it includes /snap/bin.
  84. return "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin"
  85. }
  86. if isRoot {
  87. return "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
  88. }
  89. return "/usr/local/bin:/usr/bin:/bin:/usr/bn/games"
  90. case distro.NixOS:
  91. return defaultPathForUserOnNixOS(u)
  92. }
  93. if isRoot {
  94. return "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
  95. }
  96. return "/usr/local/bin:/usr/bin:/bin"
  97. }
  98. func defaultPathForUserOnNixOS(u *user.User) string {
  99. for lr := range lineiter.File("/etc/pam/environment") {
  100. lineb, err := lr.Value()
  101. if err != nil {
  102. return ""
  103. }
  104. if v := pathFromPAMEnvLine(lineb, u); v != "" {
  105. return v
  106. }
  107. }
  108. return ""
  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. }