lookup.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package user
  2. import (
  3. "errors"
  4. )
  5. var (
  6. // The current operating system does not provide the required data for user lookups.
  7. ErrUnsupported = errors.New("user lookup: operating system does not provide passwd-formatted data")
  8. // No matching entries found in file.
  9. ErrNoPasswdEntries = errors.New("no matching entries in passwd file")
  10. ErrNoGroupEntries = errors.New("no matching entries in group file")
  11. )
  12. func lookupUser(filter func(u User) bool) (User, error) {
  13. // Get operating system-specific passwd reader-closer.
  14. passwd, err := GetPasswd()
  15. if err != nil {
  16. return User{}, err
  17. }
  18. defer passwd.Close()
  19. // Get the users.
  20. users, err := ParsePasswdFilter(passwd, filter)
  21. if err != nil {
  22. return User{}, err
  23. }
  24. // No user entries found.
  25. if len(users) == 0 {
  26. return User{}, ErrNoPasswdEntries
  27. }
  28. // Assume the first entry is the "correct" one.
  29. return users[0], nil
  30. }
  31. // LookupUser looks up a user by their username in /etc/passwd. If the user
  32. // cannot be found (or there is no /etc/passwd file on the filesystem), then
  33. // LookupUser returns an error.
  34. func LookupUser(username string) (User, error) {
  35. return lookupUser(func(u User) bool {
  36. return u.Name == username
  37. })
  38. }
  39. // LookupUid looks up a user by their user id in /etc/passwd. If the user cannot
  40. // be found (or there is no /etc/passwd file on the filesystem), then LookupId
  41. // returns an error.
  42. func LookupUid(uid int) (User, error) {
  43. return lookupUser(func(u User) bool {
  44. return u.Uid == uid
  45. })
  46. }
  47. func lookupGroup(filter func(g Group) bool) (Group, error) {
  48. // Get operating system-specific group reader-closer.
  49. group, err := GetGroup()
  50. if err != nil {
  51. return Group{}, err
  52. }
  53. defer group.Close()
  54. // Get the users.
  55. groups, err := ParseGroupFilter(group, filter)
  56. if err != nil {
  57. return Group{}, err
  58. }
  59. // No user entries found.
  60. if len(groups) == 0 {
  61. return Group{}, ErrNoGroupEntries
  62. }
  63. // Assume the first entry is the "correct" one.
  64. return groups[0], nil
  65. }
  66. // LookupGroup looks up a group by its name in /etc/group. If the group cannot
  67. // be found (or there is no /etc/group file on the filesystem), then LookupGroup
  68. // returns an error.
  69. func LookupGroup(groupname string) (Group, error) {
  70. return lookupGroup(func(g Group) bool {
  71. return g.Name == groupname
  72. })
  73. }
  74. // LookupGid looks up a group by its group id in /etc/group. If the group cannot
  75. // be found (or there is no /etc/group file on the filesystem), then LookupGid
  76. // returns an error.
  77. func LookupGid(gid int) (Group, error) {
  78. return lookupGroup(func(g Group) bool {
  79. return g.Gid == gid
  80. })
  81. }