lookup_unix.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // +build darwin dragonfly freebsd linux netbsd openbsd solaris
  2. package user
  3. import (
  4. "io"
  5. "os"
  6. "golang.org/x/sys/unix"
  7. )
  8. // Unix-specific path to the passwd and group formatted files.
  9. const (
  10. unixPasswdPath = "/etc/passwd"
  11. unixGroupPath = "/etc/group"
  12. )
  13. func GetPasswdPath() (string, error) {
  14. return unixPasswdPath, nil
  15. }
  16. func GetPasswd() (io.ReadCloser, error) {
  17. return os.Open(unixPasswdPath)
  18. }
  19. func GetGroupPath() (string, error) {
  20. return unixGroupPath, nil
  21. }
  22. func GetGroup() (io.ReadCloser, error) {
  23. return os.Open(unixGroupPath)
  24. }
  25. // CurrentUser looks up the current user by their user id in /etc/passwd. If the
  26. // user cannot be found (or there is no /etc/passwd file on the filesystem),
  27. // then CurrentUser returns an error.
  28. func CurrentUser() (User, error) {
  29. return LookupUid(unix.Getuid())
  30. }
  31. // CurrentGroup looks up the current user's group by their primary group id's
  32. // entry in /etc/passwd. If the group cannot be found (or there is no
  33. // /etc/group file on the filesystem), then CurrentGroup returns an error.
  34. func CurrentGroup() (Group, error) {
  35. return LookupGid(unix.Getgid())
  36. }