package user
import "github.com/tredoe/osutil/user"
Package user provides access to UNIX users database in local files.
You must have enough privileges to access to databases in shadowed files '/etc/shadow' and '/etc/gshadow'. This usually means have to be root. Note: those files are backed-up before of be modified.
In testing, to print the configuration read from the system, there is to use "-v" flag.
Index ¶
- Constants
- Variables
- func AddGroup(name string, members ...string) (gid int, err error)
- func AddSystemGroup(name string, members ...string) (gid int, err error)
- func AddSystemUser(name, homeDir string, gid int) (uid int, err error)
- func AddUser(name string, gid int) (uid int, err error)
- func AddUsersToGroup(name string, members ...string) error
- func ChGPasswd(group string, key []byte) error
- func ChPasswd(user string, key []byte) error
- func DelGroup(name string) (err error)
- func DelUser(name string) (err error)
- func DelUsersInGroup(name string, members ...string) error
- func GetUsername() string
- func GetUsernameFromEnv() string
- func Getgroups() []int
- func GetgroupsName() []string
- func IsExist(err error) bool
- func LockUser(name string) error
- func NextGID() (int, error)
- func NextSystemGID() (int, error)
- func NextSystemUID() (int, error)
- func NextUID() (int, error)
- func SetCrypter(c crypt.Crypt)
- func UnlockUser(name string) error
- type EmptyMemberError
- type GShadow
- func LookupGShadow(name string) (*GShadow, error)
- func LookupInGShadow(field gshadowField, value string, n int) ([]*GShadow, error)
- func NewGShadow(username string, members ...string) *GShadow
- func (gs *GShadow) Add(key []byte) (err error)
- func (gs *GShadow) Passwd(key []byte)
- func (gs *GShadow) String() string
- type Group
- func LookupGID(gid int) (*Group, error)
- func LookupGroup(name string) (*Group, error)
- func LookupInGroup(field groupField, value interface{}, n int) ([]*Group, error)
- func NewGroup(name string, members ...string) *Group
- func NewSystemGroup(name string, members ...string) *Group
- func (g *Group) Add() (gid int, err error)
- func (g *Group) IsOfSystem() bool
- func (g *Group) String() string
- type HomeError
- type IdRangeError
- type IdUsedError
- type NoFoundError
- type RequiredError
- type Shadow
- func LookupInShadow(field shadowField, value interface{}, n int) ([]*Shadow, error)
- func LookupShadow(name string) (*Shadow, error)
- func NewShadow(username string) *Shadow
- func (s *Shadow) Add(key []byte) (err error)
- func (s *Shadow) DisableAging()
- func (s *Shadow) EnableAging()
- func (s *Shadow) Passwd(key []byte)
- func (s *Shadow) SetChangePasswd()
- func (s *Shadow) SetExpire(t *time.Time)
- func (s *Shadow) String() string
- type User
- func LookupInUser(field userField, value interface{}, n int) ([]*User, error)
- func LookupUID(uid int) (*User, error)
- func LookupUser(name string) (*User, error)
- func NewSystemUser(name, homeDir string, gid int) *User
- func NewUser(name string, gid int) *User
- func (u *User) Add() (uid int, err error)
- func (u *User) IsOfSystem() bool
- func (u *User) String() string
Constants ¶
const ( G_NAME groupField = 1 << iota G_PASSWD G_GID G_MEMBER G_ALL )
Field names for group database.
const ( GS_NAME gshadowField = 1 << iota GS_PASSWD GS_ADMIN GS_MEMBER GS_ALL )
Field names for shadowed group database.
const ( S_NAME shadowField = 1 << iota S_PASSWD S_CHANGED S_MIN S_MAX S_WARN S_INACTIVE S_EXPIRE S_FLAG S_ALL )
Field names for shadowed password database.
const ( U_NAME userField = 1 << iota U_PASSWD U_UID U_GID U_GECOS U_DIR U_SHELL U_ALL // To get lines without searching into a field. )
Field names for user database.
Variables ¶
var ( ErrUserExist = errors.New("user already exists") ErrGroupExist = errors.New("group already exists") )
var DO_BACKUP = true
DO_BACKUP does a backup before of modify the original files.
Functions ¶
func AddGroup ¶
AddGroup adds a group.
func AddSystemGroup ¶
AddSystemGroup adds a system group.
func AddSystemUser ¶
AddSystemUser adds a system user to both user and shadow files.
func AddUser ¶
AddUser adds an user to both user and shadow files.
func AddUsersToGroup ¶
AddUsersToGroup adds the members to a group.
func ChGPasswd ¶
ChGPasswd updates group passwd. The passwd must be supplied in clear-text.
func ChPasswd ¶
ChPasswd updates passwd. The passwd must be supplied in clear-text.
func DelGroup ¶
DelGroup removes a group from the system.
func DelUser ¶
DelUser removes an user from the system.
func DelUsersInGroup ¶
DelUsersInGroup removes the specific members from a group.
func GetUsername ¶
func GetUsername() string
GetUsername returns the user name from the password database for the actual process. It panics whther there is an error at searching the UID.
func GetUsernameFromEnv ¶
func GetUsernameFromEnv() string
GetUsernameFromEnv returns the user name from the environment variable for the actual process.
func Getgroups ¶
func Getgroups() []int
Getgroups returns a list of the numeric ids of groups that the caller belongs to.
func GetgroupsName ¶
func GetgroupsName() []string
GetgroupsName returns a list of the groups that the caller belongs to.
func IsExist ¶
IsExist returns whether the error is known to report that an user or group already exists. It is satisfied by ErrUserExist and ErrGroupExist.
func LockUser ¶
LockUser locks the passwd of the given user.
func NextGID ¶
NextGID returns the next free group id to use.
func NextSystemGID ¶
NextSystemGID returns the next free system group id to use.
func NextSystemUID ¶
NextSystemUID returns the next free system user id to use.
func NextUID ¶
NextUID returns the next free user id to use.
func SetCrypter ¶
SetCrypter sets the crypt function to can hash the passwords. The type "crypt.Crypt" comes from package "github.com/tredoe/osutil/user/crypt".
func UnlockUser ¶
UnlockUser unlocks the passwd of the given user.
Types ¶
type EmptyMemberError ¶
type EmptyMemberError string
EmptyMemberError reports an empty member.
func (EmptyMemberError) Error ¶
func (e EmptyMemberError) Error() string
type GShadow ¶
type GShadow struct { // Group name. (Unique) // // It must be a valid group name, which exist on the system. Name string // Group administrator list // // It must be a comma-separated list of user names. // // Administrators can change the password or the members of the group. // Administrators also have the same permissions as the members (see below). AdminList []string // Group member list // // It must be a comma-separated list of user names. // // Members can access the group without being prompted for a password. // You should use the same list of users as in /etc/group. UserList []string // contains filtered or unexported fields }
A GShadow represents the format of the shadowed information for a group account.
func LookupGShadow ¶
LookupGShadow looks up a shadowed group by name.
func LookupInGShadow ¶
LookupInGShadow looks up a shadowed group by the given values.
The count determines the number of fields to return:
n > 0: at most n fields n == 0: the result is nil (zero fields) n < 0: all fields
func NewGShadow ¶
NewGShadow returns a new GShadow.
func (*GShadow) Add ¶
Add adds a new shadowed group. If the key is not nil, generates a hashed password.
It is created a backup before of modify the original file.
func (*GShadow) Passwd ¶
Passwd sets a hashed passwd for the actual group. The passwd must be supplied in clear-text.
func (*GShadow) String ¶
type Group ¶
type Group struct { // Group name. (Unique) Name string // The numeric group ID. (Unique) GID int // User list // // A list of the usernames that are members of this group, separated by commas. UserList []string // contains filtered or unexported fields }
A Group represents the format of a group on the system.
func LookupGID ¶
LookupGID looks up a group by group ID.
func LookupGroup ¶
LookupGroup looks up a group by name.
func LookupInGroup ¶
LookupInGroup looks up a group by the given values.
The count determines the number of fields to return:
n > 0: at most n fields n == 0: the result is nil (zero fields) n < 0: all fields
func NewGroup ¶
NewGroup returns a new Group.
func NewSystemGroup ¶
NewSystemGroup adds a system group.
func (*Group) Add ¶
Add adds a new group. Whether GID is < 0, it will choose the first id available in the range set in the system configuration.
func (*Group) IsOfSystem ¶
IsOfSystem indicates whether it is a system group.
func (*Group) String ¶
type HomeError ¶
type HomeError string
A HomeError reports an error at adding an account with invalid home directory.
func (HomeError) Error ¶
type IdRangeError ¶
IdRangeError records an error during the search for a free id to use.
func (*IdRangeError) Error ¶
func (e *IdRangeError) Error() string
type IdUsedError ¶
type IdUsedError int
IdUsedError reports the presence of an identifier already used.
func (IdUsedError) Error ¶
func (e IdUsedError) Error() string
type NoFoundError ¶
type NoFoundError struct {
// contains filtered or unexported fields
}
A NoFoundError reports the absence of a value.
func (NoFoundError) Error ¶
func (e NoFoundError) Error() string
type RequiredError ¶
type RequiredError string
A RequiredError reports the name of a required field.
func (RequiredError) Error ¶
func (e RequiredError) Error() string
type Shadow ¶
type Shadow struct { // Login name. (Unique) // // It must be a valid account name, which exist on the system. Name string // Minimum password age // // The minimum password age is the number of days the user will have to wait // before he will be allowed to change her password again. // // An empty field and value 0 mean that there are no minimum password age. Min int // Maximum password age // // The maximum password age is the number of days after which the user will // have to change her password. // // After this number of days is elapsed, the password may still be valid. // The user should be asked to change her password the next time he will // log in. // // An empty field means that there are no maximum password age, no password // warning period, and no password inactivity period (see below). // // If the maximum password age is lower than the minimum password age, the // user cannot change her password. Max int // Password warning period // // The number of days before a password is going to expire (see the maximum // password age above) during which the user should be warned. // // An empty field and value 0 mean that there are no password warning period. Warn int // Password inactivity period // // The number of days after a password has expired (see the maximum password // age above) during which the password should still be accepted (and the // user should update her password during the next login). // // After expiration of the password and this expiration period is elapsed, // no login is possible using the current user's password. // The user should contact her administrator. // // An empty field means that there are no enforcement of an inactivity period. Inactive int // contains filtered or unexported fields }
A Shadow represents the format of the information for a system's account and optional aging information.
The fields "changed" and "expire" deal with days from Jan 1, 1970; but since package "time" deals with seconds, there is to divide it between the seconds that a day has (24*60*60) which is done by functions "setChange" and "SetExpire".
To simulate an empty field in numeric fields, it is used a negative value.
func LookupInShadow ¶
LookupInShadow looks up a shadowed password by the given values.
The count determines the number of fields to return:
n > 0: at most n fields n == 0: the result is nil (zero fields) n < 0: all fields
func LookupShadow ¶
LookupShadow looks for the entry for the given user name.
func NewShadow ¶
NewShadow returns a structure Shadow with fields "Min", "Max" and "Warn" got from the system configuration, and enabling the features of password aging.
func (*Shadow) Add ¶
Add adds a new shadowed user. If the key is not nil, generates a hashed password.
It is created a backup before of modify the original file.
func (*Shadow) DisableAging ¶
func (s *Shadow) DisableAging()
DisableAging disables the features of password aging.
func (*Shadow) EnableAging ¶
func (s *Shadow) EnableAging()
EnableAging enables the features of password aging.
func (*Shadow) Passwd ¶
Passwd sets a hashed passwd for the actual user. The passwd must be supplied in clear-text.
func (*Shadow) SetChangePasswd ¶
func (s *Shadow) SetChangePasswd()
SetChangePasswd sets the account for that the user change her pasword the next time he will log in the system.
func (*Shadow) SetExpire ¶
SetExpire sets the date of expiration of the account.
func (*Shadow) String ¶
type User ¶
type User struct { // Login name. (Unique) Name string // Numerical user ID. (Unique) UID int // Numerical group ID GID int // User name or comment field // // The comment field is used by various system utilities, such as "finger(1)". Gecos string // User home directory // // The home directory field provides the name of the initial working // directory. The login program uses this information to set the value of // the $HOME environmental variable. Dir string // Optional user command interpreter // // The command interpreter field provides the name of the user's command // language interpreter, or the name of the initial program to execute. // The login program uses this information to set the value of the "$SHELL" // environmental variable. If this field is empty, it defaults to the value // "/bin/sh". Shell string // contains filtered or unexported fields }
An User represents an user account.
func LookupInUser ¶
LookupInUser looks up an user by the given values.
The count determines the number of fields to return:
n > 0: at most n fields n == 0: the result is nil (zero fields) n < 0: all fields
func LookupUID ¶
LookupUID looks up an user by user ID.
func LookupUser ¶
LookupUser looks up an user by name.
func NewSystemUser ¶
NewSystemUser returns a new system user.
func NewUser ¶
NewUser returns a new User with both fields "Dir" and "Shell" got from the system configuration.
func (*User) Add ¶
Add adds a new user. Whether UID is < 0, it will choose the first id available in the range set in the system configuration.
func (*User) IsOfSystem ¶
IsOfSystem indicates whether it is a system user.
func (*User) String ¶
Source Files ¶
config_linux.go crypt.go dbfile.go doc.go error.go file.go group.go gshadow.go id.go shadow.go user.go util.go
Directories ¶
Path | Synopsis |
---|---|
user/crypt | Package crypt provides interface for password crypt functions and collects common constants. |
user/crypt/apr1_crypt | Package apr1_crypt implements the standard Unix MD5-crypt algorithm created by Poul-Henning Kamp for FreeBSD, and modified by the Apache project. |
user/crypt/common | Package common contains routines used by multiple password hashing algorithms. |
user/crypt/md5_crypt | Package md5_crypt implements the standard Unix MD5-crypt algorithm created by Poul-Henning Kamp for FreeBSD. |
user/crypt/sha256_crypt | Package sha256_crypt implements Ulrich Drepper's SHA256-crypt password hashing algorithm. |
user/crypt/sha512_crypt | Package sha512_crypt implements Ulrich Drepper's SHA512-crypt password hashing algorithm. |
- Version
- v1.5.0 (latest)
- Published
- Jun 4, 2024
- Platform
- linux/amd64
- Imports
- 20 packages
- Last checked
- 1 day ago –
Tools for package owners.