user

package module
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 26, 2026 License: Apache-2.0 Imports: 12 Imported by: 577

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoPasswdEntries is returned if no matching entries were found in /etc/passwd.
	ErrNoPasswdEntries = errors.New("no matching entries in passwd file")
	// ErrNoGroupEntries is returned if no matching entries were found in /etc/group.
	ErrNoGroupEntries = errors.New("no matching entries in group file")
	// ErrRange is returned if a UID or GID is outside of the valid range.
	ErrRange = fmt.Errorf("uids and gids must be in range %d-%d", minID, maxID)
)

Functions

func GetAdditionalGroups

func GetAdditionalGroups(additionalGroups []string, group io.Reader) ([]int, error)

GetAdditionalGroups looks up a list of groups by name or group id against the given /etc/group formatted data. If a group name cannot be found, an error will be returned. If a group id cannot be found, or the given group data is nil, the id will be returned as-is provided it is in the legal range.

func GetAdditionalGroupsPath

func GetAdditionalGroupsPath(additionalGroups []string, groupPath string) ([]int, error)

GetAdditionalGroupsPath is a wrapper around GetAdditionalGroups that opens the groupPath given and gives it as an argument to GetAdditionalGroups.

func GetGroup

func GetGroup() (io.ReadCloser, error)

func GetGroupPath

func GetGroupPath() (string, error)

func GetPasswd

func GetPasswd() (io.ReadCloser, error)

func GetPasswdPath

func GetPasswdPath() (string, error)

func MkdirAllAndChown added in v0.4.0

func MkdirAllAndChown(path string, mode os.FileMode, uid, gid int, opts ...MkdirOpt) error

MkdirAllAndChown creates a directory named path, along with any necessary parents, and applies the requested ownership and permission bits to all directories it creates.

If path already exists as a directory, its ownership and permission bits are updated unless WithOnlyNew is specified. Existing parent directories are not modified.

MkdirAllAndChown is similar to os.MkdirAll, but also applies ownership and reapplies the requested permission bits after creation, so the resulting permissions are not affected by the process umask.

func MkdirAndChown added in v0.4.0

func MkdirAndChown(path string, mode os.FileMode, uid, gid int, opts ...MkdirOpt) error

MkdirAndChown creates a directory named path and applies the requested ownership and permission bits.

If path already exists as a directory, its ownership and permission bits are updated unless WithOnlyNew is specified.

MkdirAndChown is similar to os.Mkdir, but returns nil if path already exists as a directory. The resulting permission bits are not affected by the process umask.

func WithOnlyNew added in v0.4.0

func WithOnlyNew(o *mkdirOptions)

WithOnlyNew configures MkdirAllAndChown and MkdirAndChown to leave the requested directory unchanged if it already exists. Newly created directories are still assigned the requested ownership and permissions.

Types

type ExecUser

type ExecUser struct {
	Uid   int
	Gid   int
	Sgids []int
	Home  string
}

func GetExecUser

func GetExecUser(userSpec string, defaults *ExecUser, passwd, group io.Reader) (*ExecUser, error)

GetExecUser parses a user specification string (using the passwd and group readers as sources for /etc/passwd and /etc/group data, respectively). In the case of blank fields or missing data from the sources, the values in defaults is used.

GetExecUser will return an error if a user or group literal could not be found in any entry in passwd and group respectively.

Examples of valid user specifications are:

  • ""
  • "user"
  • "uid"
  • "user:group"
  • "uid:gid
  • "user:gid"
  • "uid:group"

It should be noted that if you specify a numeric user or group id, they will not be evaluated as usernames (only the metadata will be filled). So attempting to parse a user with user.Name = "1337" will produce the user with a UID of 1337.

func GetExecUserPath

func GetExecUserPath(userSpec string, defaults *ExecUser, passwdPath, groupPath string) (*ExecUser, error)

GetExecUserPath is a wrapper for GetExecUser. It reads data from each of the given file paths and uses that data as the arguments to GetExecUser. If the files cannot be opened for any reason, the error is ignored and a nil io.Reader is passed instead.

type Group

type Group struct {
	Name string
	Pass string
	Gid  int
	List []string
}

func CurrentGroup

func CurrentGroup() (Group, error)

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 LookupGid

func LookupGid(gid int) (Group, error)

LookupGid looks up a group by its group id in /etc/group. If the group cannot be found (or there is no /etc/group file on the filesystem), then LookupGid returns an error.

func LookupGroup

func LookupGroup(groupname string) (Group, error)

LookupGroup looks up a group by its name in /etc/group. If the group cannot be found (or there is no /etc/group file on the filesystem), then LookupGroup returns an error.

func ParseGroup

func ParseGroup(group io.Reader) ([]Group, error)

func ParseGroupFile

func ParseGroupFile(path string) ([]Group, error)

func ParseGroupFileFilter

func ParseGroupFileFilter(path string, filter func(Group) bool) ([]Group, error)

func ParseGroupFilter

func ParseGroupFilter(r io.Reader, filter func(Group) bool) ([]Group, error)

type IDMap

type IDMap struct {
	ID       int64
	ParentID int64
	Count    int64
}

IDMap represents an entry in /proc/PID/{u,g}id_map

func CurrentProcessGIDMap

func CurrentProcessGIDMap() ([]IDMap, error)

func CurrentProcessUIDMap

func CurrentProcessUIDMap() ([]IDMap, error)

func ParseIDMap

func ParseIDMap(r io.Reader) ([]IDMap, error)

func ParseIDMapFile

func ParseIDMapFile(path string) ([]IDMap, error)

func ParseIDMapFileFilter

func ParseIDMapFileFilter(path string, filter func(IDMap) bool) ([]IDMap, error)

func ParseIDMapFilter

func ParseIDMapFilter(r io.Reader, filter func(IDMap) bool) ([]IDMap, error)

type IdentityMapping added in v0.4.0

type IdentityMapping struct {
	UIDMaps []IDMap `json:"UIDMaps"`
	GIDMaps []IDMap `json:"GIDMaps"`
}

IdentityMapping contains a mappings of UIDs and GIDs. The zero value represents an empty mapping.

func LoadIdentityMapping added in v0.4.0

func LoadIdentityMapping(name string) (IdentityMapping, error)

LoadIdentityMapping takes a requested username and using the data from /etc/sub{uid,gid} ranges, creates the proper uid and gid remapping ranges for that user/group pair

func (IdentityMapping) Empty added in v0.4.0

func (i IdentityMapping) Empty() bool

Empty returns true if there are no id mappings

func (IdentityMapping) RootPair added in v0.4.0

func (i IdentityMapping) RootPair() (int, int)

RootPair returns a uid and gid pair for the root user. The error is ignored because a root user always exists, and the defaults are correct when the uid and gid maps are empty.

func (IdentityMapping) ToContainer added in v0.4.0

func (i IdentityMapping) ToContainer(uid, gid int) (int, int, error)

ToContainer returns the container UID and GID for the host uid and gid

func (IdentityMapping) ToHost added in v0.4.0

func (i IdentityMapping) ToHost(uid, gid int) (int, int, error)

ToHost returns the host UID and GID for the container uid and gid. An empty mapping is treated as identity.

type MkdirOpt added in v0.4.0

type MkdirOpt func(*mkdirOptions)

MkdirOpt is a type for options to pass to Mkdir calls

type SubID

type SubID struct {
	Name  string
	SubID int64
	Count int64
}

SubID represents an entry in /etc/sub{u,g}id

func CurrentUserSubGIDs

func CurrentUserSubGIDs() ([]SubID, error)

func CurrentUserSubUIDs

func CurrentUserSubUIDs() ([]SubID, error)

func ParseSubID

func ParseSubID(subid io.Reader) ([]SubID, error)

func ParseSubIDFile

func ParseSubIDFile(path string) ([]SubID, error)

func ParseSubIDFileFilter

func ParseSubIDFileFilter(path string, filter func(SubID) bool) ([]SubID, error)

func ParseSubIDFilter

func ParseSubIDFilter(r io.Reader, filter func(SubID) bool) ([]SubID, error)

type User

type User struct {
	Name  string
	Pass  string
	Uid   int
	Gid   int
	Gecos string
	Home  string
	Shell string
}

func CurrentUser

func CurrentUser() (User, error)

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 LookupUid

func LookupUid(uid int) (User, error)

LookupUid looks up a 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 LookupId returns an error.

func LookupUser

func LookupUser(username string) (User, error)

LookupUser looks up a user by their username in /etc/passwd. If the user cannot be found (or there is no /etc/passwd file on the filesystem), then LookupUser returns an error.

func ParsePasswd

func ParsePasswd(passwd io.Reader) ([]User, error)

func ParsePasswdFile

func ParsePasswdFile(path string) ([]User, error)

func ParsePasswdFileFilter

func ParsePasswdFileFilter(path string, filter func(User) bool) ([]User, error)

func ParsePasswdFilter

func ParsePasswdFilter(r io.Reader, filter func(User) bool) ([]User, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL