user.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. /*
  5. Package user allows user account lookups by name or id.
  6. For most Unix systems, this package has two internal implementations of
  7. resolving user and group ids to names, and listing supplementary group IDs.
  8. One is written in pure Go and parses /etc/passwd and /etc/group. The other
  9. is cgo-based and relies on the standard C library (libc) routines such as
  10. getpwuid_r, getgrnam_r, and getgrouplist.
  11. When cgo is available, and the required routines are implemented in libc
  12. for a particular platform, cgo-based (libc-backed) code is used.
  13. This can be overridden by using osusergo build tag, which enforces
  14. the pure Go implementation.
  15. */
  16. package user
  17. import (
  18. "strconv"
  19. )
  20. // These may be set to false in init() for a particular platform and/or
  21. // build flags to let the tests know to skip tests of some features.
  22. var (
  23. userImplemented = true
  24. groupImplemented = true
  25. groupListImplemented = true
  26. )
  27. // User represents a user account.
  28. type User struct {
  29. // Uid is the user ID.
  30. // On POSIX systems, this is a decimal number representing the uid.
  31. // On Windows, this is a security identifier (SID) in a string format.
  32. // On Plan 9, this is the contents of /dev/user.
  33. Uid string
  34. // Gid is the primary group ID.
  35. // On POSIX systems, this is a decimal number representing the gid.
  36. // On Windows, this is a SID in a string format.
  37. // On Plan 9, this is the contents of /dev/user.
  38. Gid string
  39. // Username is the login name.
  40. Username string
  41. // Name is the user's real or display name.
  42. // It might be blank.
  43. // On POSIX systems, this is the first (or only) entry in the GECOS field
  44. // list.
  45. // On Windows, this is the user's display name.
  46. // On Plan 9, this is the contents of /dev/user.
  47. Name string
  48. // HomeDir is the path to the user's home directory (if they have one).
  49. HomeDir string
  50. }
  51. // Group represents a grouping of users.
  52. //
  53. // On POSIX systems Gid contains a decimal number representing the group ID.
  54. type Group struct {
  55. Gid string // group ID
  56. Name string // group name
  57. }
  58. // UnknownUserIdError is returned by LookupId when a user cannot be found.
  59. type UnknownUserIdError int
  60. func (e UnknownUserIdError) Error() string {
  61. return "user: unknown userid " + strconv.Itoa(int(e))
  62. }
  63. // UnknownUserError is returned by Lookup when
  64. // a user cannot be found.
  65. type UnknownUserError string
  66. func (e UnknownUserError) Error() string {
  67. return "user: unknown user " + string(e)
  68. }
  69. // UnknownGroupIdError is returned by LookupGroupId when
  70. // a group cannot be found.
  71. type UnknownGroupIdError string
  72. func (e UnknownGroupIdError) Error() string {
  73. return "group: unknown groupid " + string(e)
  74. }
  75. // UnknownGroupError is returned by LookupGroup when
  76. // a group cannot be found.
  77. type UnknownGroupError string
  78. func (e UnknownGroupError) Error() string {
  79. return "group: unknown group " + string(e)
  80. }