user.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. // Package osuser implements OS user lookup. It's a wrapper around os/user that
  4. // works on non-cgo builds.
  5. package osuser
  6. import (
  7. "context"
  8. "errors"
  9. "log"
  10. "os/exec"
  11. "os/user"
  12. "runtime"
  13. "strings"
  14. "time"
  15. "unicode/utf8"
  16. "tailscale.com/version/distro"
  17. )
  18. // LookupByUIDWithShell is like os/user.LookupId but handles a few edge cases
  19. // like gokrazy and non-cgo lookups, and returns the user shell. The user shell
  20. // lookup is best-effort and may be empty.
  21. func LookupByUIDWithShell(uid string) (u *user.User, shell string, err error) {
  22. return lookup(uid, user.LookupId, true)
  23. }
  24. // LookupByUsernameWithShell is like os/user.Lookup but handles a few edge
  25. // cases like gokrazy and non-cgo lookups, and returns the user shell. The user
  26. // shell lookup is best-effort and may be empty.
  27. func LookupByUsernameWithShell(username string) (u *user.User, shell string, err error) {
  28. return lookup(username, user.Lookup, true)
  29. }
  30. // LookupByUID is like os/user.LookupId but handles a few edge cases like
  31. // gokrazy and non-cgo lookups.
  32. func LookupByUID(uid string) (*user.User, error) {
  33. u, _, err := lookup(uid, user.LookupId, false)
  34. return u, err
  35. }
  36. // LookupByUsername is like os/user.Lookup but handles a few edge cases like
  37. // gokrazy and non-cgo lookups.
  38. func LookupByUsername(username string) (*user.User, error) {
  39. u, _, err := lookup(username, user.Lookup, false)
  40. return u, err
  41. }
  42. // lookupStd is either user.Lookup or user.LookupId.
  43. type lookupStd func(string) (*user.User, error)
  44. func lookup(usernameOrUID string, std lookupStd, wantShell bool) (*user.User, string, error) {
  45. // Skip getent entirely on Non-Unix platforms that won't ever have it.
  46. // (Using HasPrefix for "wasip1", anticipating that WASI support will
  47. // move beyond "preview 1" some day.)
  48. if runtime.GOOS == "windows" || runtime.GOOS == "js" || runtime.GOARCH == "wasm" || runtime.GOOS == "plan9" {
  49. var shell string
  50. if wantShell && runtime.GOOS == "plan9" {
  51. shell = "/bin/rc"
  52. }
  53. if runtime.GOOS == "plan9" {
  54. if u, err := user.Current(); err == nil {
  55. return u, shell, nil
  56. }
  57. }
  58. u, err := std(usernameOrUID)
  59. return u, shell, err
  60. }
  61. // No getent on Gokrazy. So hard-code the login shell.
  62. if distro.Get() == distro.Gokrazy {
  63. var shell string
  64. if wantShell {
  65. shell = "/tmp/serial-busybox/ash"
  66. }
  67. u, err := std(usernameOrUID)
  68. if err != nil {
  69. return &user.User{
  70. Uid: "0",
  71. Gid: "0",
  72. Username: "root",
  73. Name: "Gokrazy",
  74. HomeDir: "/",
  75. }, shell, nil
  76. }
  77. return u, shell, nil
  78. }
  79. if runtime.GOOS == "plan9" {
  80. return &user.User{
  81. Uid: "0",
  82. Gid: "0",
  83. Username: "glenda",
  84. Name: "Glenda",
  85. HomeDir: "/",
  86. }, "/bin/rc", nil
  87. }
  88. // Start with getent if caller wants to get the user shell.
  89. if wantShell {
  90. return userLookupGetent(usernameOrUID, std)
  91. }
  92. // If shell is not required, try os/user.Lookup* first and only use getent
  93. // if that fails. This avoids spawning a child process when os/user lookup
  94. // succeeds.
  95. if u, err := std(usernameOrUID); err == nil {
  96. return u, "", nil
  97. }
  98. return userLookupGetent(usernameOrUID, std)
  99. }
  100. func checkGetentInput(usernameOrUID string) bool {
  101. maxUid := 32
  102. if runtime.GOOS == "linux" {
  103. maxUid = 256
  104. }
  105. if len(usernameOrUID) > maxUid || len(usernameOrUID) == 0 {
  106. return false
  107. }
  108. for _, r := range usernameOrUID {
  109. if r < ' ' || r == 0x7f || r == utf8.RuneError { // TODO(bradfitz): more?
  110. return false
  111. }
  112. }
  113. return true
  114. }
  115. // userLookupGetent uses "getent" to look up users so that even with static
  116. // tailscaled binaries without cgo (as we distribute), we can still look up
  117. // PAM/NSS users which the standard library's os/user without cgo won't get
  118. // (because of no libc hooks). If "getent" fails, userLookupGetent falls back
  119. // to the standard library.
  120. func userLookupGetent(usernameOrUID string, std lookupStd) (*user.User, string, error) {
  121. // Do some basic validation before passing this string to "getent", even though
  122. // getent should do its own validation.
  123. if !checkGetentInput(usernameOrUID) {
  124. return nil, "", errors.New("invalid username or UID")
  125. }
  126. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  127. defer cancel()
  128. out, err := exec.CommandContext(ctx, "getent", "passwd", usernameOrUID).Output()
  129. if err != nil {
  130. log.Printf("error calling getent for user %q: %v", usernameOrUID, err)
  131. u, err := std(usernameOrUID)
  132. return u, "", err
  133. }
  134. // output is "alice:x:1001:1001:Alice Smith,,,:/home/alice:/bin/bash"
  135. f := strings.SplitN(strings.TrimSpace(string(out)), ":", 10)
  136. for len(f) < 7 {
  137. f = append(f, "")
  138. }
  139. var mandatoryFields = []int{0, 2, 3, 5}
  140. for _, v := range mandatoryFields {
  141. if f[v] == "" {
  142. log.Printf("getent for user %q returned invalid output: %q", usernameOrUID, out)
  143. u, err := std(usernameOrUID)
  144. return u, "", err
  145. }
  146. }
  147. return &user.User{
  148. Username: f[0],
  149. Uid: f[2],
  150. Gid: f[3],
  151. Name: f[4],
  152. HomeDir: f[5],
  153. }, f[6], nil
  154. }