2014-08-29 16:43:40 +08:00
|
|
|
// +build darwin dragonfly freebsd linux netbsd openbsd solaris
|
|
|
|
|
|
|
|
package user
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
2017-08-04 02:28:56 +08:00
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
2014-08-29 16:43:40 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Unix-specific path to the passwd and group formatted files.
|
|
|
|
const (
|
2014-12-16 22:39:09 +08:00
|
|
|
unixPasswdPath = "/etc/passwd"
|
|
|
|
unixGroupPath = "/etc/group"
|
2014-08-29 16:43:40 +08:00
|
|
|
)
|
|
|
|
|
2014-12-16 22:39:09 +08:00
|
|
|
func GetPasswdPath() (string, error) {
|
|
|
|
return unixPasswdPath, nil
|
2014-08-29 16:43:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetPasswd() (io.ReadCloser, error) {
|
2014-12-16 22:39:09 +08:00
|
|
|
return os.Open(unixPasswdPath)
|
2014-08-29 16:43:40 +08:00
|
|
|
}
|
|
|
|
|
2014-12-16 22:39:09 +08:00
|
|
|
func GetGroupPath() (string, error) {
|
|
|
|
return unixGroupPath, nil
|
2014-08-29 16:43:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetGroup() (io.ReadCloser, error) {
|
2014-12-16 22:39:09 +08:00
|
|
|
return os.Open(unixGroupPath)
|
2014-08-29 16:43:40 +08:00
|
|
|
}
|
2017-08-04 02:28:56 +08:00
|
|
|
|
|
|
|
// CurrentUser looks up the current user by their user id in /etc/passwd. If the
|
|
|
|
// user cannot be found (or there is no /etc/passwd file on the filesystem),
|
|
|
|
// then CurrentUser returns an error.
|
|
|
|
func CurrentUser() (User, error) {
|
|
|
|
return LookupUid(unix.Getuid())
|
|
|
|
}
|
|
|
|
|
|
|
|
// CurrentGroup looks up the current user's group by their primary group id's
|
|
|
|
// entry in /etc/passwd. If the group cannot be found (or there is no
|
|
|
|
// /etc/group file on the filesystem), then CurrentGroup returns an error.
|
|
|
|
func CurrentGroup() (Group, error) {
|
|
|
|
return LookupGid(unix.Getgid())
|
|
|
|
}
|