user.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. // TODO(awly): we should use genet on more platforms, like FreeBSD.
  46. if runtime.GOOS != "linux" {
  47. u, err := std(usernameOrUID)
  48. return u, "", err
  49. }
  50. // No getent on Gokrazy. So hard-code the login shell.
  51. if distro.Get() == distro.Gokrazy {
  52. var shell string
  53. if wantShell {
  54. shell = "/tmp/serial-busybox/ash"
  55. }
  56. u, err := std(usernameOrUID)
  57. if err != nil {
  58. return &user.User{
  59. Uid: "0",
  60. Gid: "0",
  61. Username: "root",
  62. Name: "Gokrazy",
  63. HomeDir: "/",
  64. }, shell, nil
  65. }
  66. return u, shell, nil
  67. }
  68. // Start with getent if caller wants to get the user shell.
  69. if wantShell {
  70. return userLookupGetent(usernameOrUID, std)
  71. }
  72. // If shell is not required, try os/user.Lookup* first and only use getent
  73. // if that fails. This avoids spawning a child process when os/user lookup
  74. // succeeds.
  75. if u, err := std(usernameOrUID); err == nil {
  76. return u, "", nil
  77. }
  78. return userLookupGetent(usernameOrUID, std)
  79. }
  80. func checkGetentInput(usernameOrUID string) bool {
  81. maxUid := 32
  82. if runtime.GOOS == "linux" {
  83. maxUid = 256
  84. }
  85. if len(usernameOrUID) > maxUid || len(usernameOrUID) == 0 {
  86. return false
  87. }
  88. for _, r := range usernameOrUID {
  89. if r < ' ' || r == 0x7f || r == utf8.RuneError { // TODO(bradfitz): more?
  90. return false
  91. }
  92. }
  93. return true
  94. }
  95. // userLookupGetent uses "getent" to look up users so that even with static
  96. // tailscaled binaries without cgo (as we distribute), we can still look up
  97. // PAM/NSS users which the standard library's os/user without cgo won't get
  98. // (because of no libc hooks). If "getent" fails, userLookupGetent falls back
  99. // to the standard library.
  100. func userLookupGetent(usernameOrUID string, std lookupStd) (*user.User, string, error) {
  101. // Do some basic validation before passing this string to "getent", even though
  102. // getent should do its own validation.
  103. if !checkGetentInput(usernameOrUID) {
  104. return nil, "", errors.New("invalid username or UID")
  105. }
  106. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  107. defer cancel()
  108. out, err := exec.CommandContext(ctx, "getent", "passwd", usernameOrUID).Output()
  109. if err != nil {
  110. log.Printf("error calling getent for user %q: %v", usernameOrUID, err)
  111. u, err := std(usernameOrUID)
  112. return u, "", err
  113. }
  114. // output is "alice:x:1001:1001:Alice Smith,,,:/home/alice:/bin/bash"
  115. f := strings.SplitN(strings.TrimSpace(string(out)), ":", 10)
  116. for len(f) < 7 {
  117. f = append(f, "")
  118. }
  119. return &user.User{
  120. Username: f[0],
  121. Uid: f[2],
  122. Gid: f[3],
  123. Name: f[4],
  124. HomeDir: f[5],
  125. }, f[6], nil
  126. }