etcpwdparse

package module
v0.0.0-...-f0e5f07 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2017 License: MIT Imports: 4 Imported by: 4

README

Golang library etcpwdparse

This library provides simple access to the entries in the /etc/passwd file.

It was made for programs that need to be able to pull home directories, user id's and the like but do not want the cgo-dependence of the core os/user package.

The only real caveat is that it doesn't pull user entries from other sources like PAM or LDAP since it only operates by reading the file on disk.

Usage:

go get github.com/AstromechZA/etcpwdparse
// load the passwd entries
// err will be non-nill if there was an IO or parsing error while loading the file
cache, err := NewLoadedEtcPasswdCache()
if err != nil {
    panic(err)
}

// pull the home directory
// there will be an err if the user could not be found
homedir, err := cache.HomeDirForUsername("bob")

See the documentation at godoc.org/github.com/AstromechZA/etcpwdparse for more information.

Documentation

Overview

Package etcpwdparse provides straightforward functionality for loading an /etc/passwd file and doing lookups on its content.

Remember this only looks at an /etc/passwd type file, so will work best on Linux operating systems and wont pick up users from LDAP and other sources.

Example
// load the cache from the /etc/passwd file
cache, err := NewLoadedEtcPasswdCache()
if err != nil {
	panic(err)
}

// look up the current user
entry, _ := cache.LookupUserByUid(os.Getuid())

// print some result
fmt.Printf("Your current user is %s and your homedir is %s\n", entry.Username(), entry.Homedir())
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EtcPasswdCache

type EtcPasswdCache struct {
	// contains filtered or unexported fields
}

EtcPasswdCache is an object that stores a set of entries from the passwd file and has quick lookup functions.

func NewEtcPasswdCache

func NewEtcPasswdCache(ignoreBadLines bool) *EtcPasswdCache

NewEtcPasswdCache returns an empty passwd cache.

func NewLoadedEtcPasswdCache

func NewLoadedEtcPasswdCache() (*EtcPasswdCache, error)

NewLoadedEtcPasswdCache returns a loaded passwd cache in a single call.

func (*EtcPasswdCache) AddEntry

func (e *EtcPasswdCache) AddEntry(entry EtcPasswdEntry)

AddEntry adds an entry object to the cache object and links it into the lookup maps. Overrides any existing item in the lookup maps.

func (*EtcPasswdCache) HomeDirForUsername

func (e *EtcPasswdCache) HomeDirForUsername(name string) (string, error)

HomeDirForUsername is a shortcut function to get the home directory for the given username. Useful when needing to store things in the home directory.

func (*EtcPasswdCache) ListEntries

func (e *EtcPasswdCache) ListEntries() []*EtcPasswdEntry

ListEntries returns a slice containing references to all the entry objects

func (*EtcPasswdCache) LoadDefault

func (e *EtcPasswdCache) LoadDefault() error

LoadDefault loads the struct from the /etc/passwd file

func (*EtcPasswdCache) LoadFromPath

func (e *EtcPasswdCache) LoadFromPath(path string) error

LoadFromPath loads the struct from a file on disk and replaces the cached content.

func (*EtcPasswdCache) LookupUserByName

func (e *EtcPasswdCache) LookupUserByName(name string) (*EtcPasswdEntry, bool)

LookupUserByName returns the entry for the given username

func (*EtcPasswdCache) LookupUserByUid

func (e *EtcPasswdCache) LookupUserByUid(id int) (*EtcPasswdEntry, bool)

LookupUserByUid returns the entry for the given userid

func (*EtcPasswdCache) UidForUsername

func (e *EtcPasswdCache) UidForUsername(name string) (int, error)

UidForUsername is a shortcut function to get the user id for the given username. Useful when needing to chown a file.

type EtcPasswdEntry

type EtcPasswdEntry struct {
	// contains filtered or unexported fields
}

EtcPasswdEntry is a parsed line from the etc passwd file. It contains all 7 parts of the structure. Remember that the password field is encrypted or refers to an item in an alternative authentication scheme.

func ParsePasswdLine

func ParsePasswdLine(line string) (EtcPasswdEntry, error)

ParsePasswdLine is a function used to parse a 7 entry /etc/passwd line formatted line into a EtcPasswdEntry object.

func (*EtcPasswdEntry) Gid

func (e *EtcPasswdEntry) Gid() int

Gid function returns the group id for the entry

func (*EtcPasswdEntry) Homedir

func (e *EtcPasswdEntry) Homedir() string

Homedir function returns the home directory for the entry

func (*EtcPasswdEntry) Info

func (e *EtcPasswdEntry) Info() string

Info function returns the info string for the entry

func (*EtcPasswdEntry) Password

func (e *EtcPasswdEntry) Password() string

Password function returns the encrypted password string for the entry

func (*EtcPasswdEntry) Shell

func (e *EtcPasswdEntry) Shell() string

Shell function returns the users shell

func (*EtcPasswdEntry) Uid

func (e *EtcPasswdEntry) Uid() int

Uid function returns the user id for the entry

func (*EtcPasswdEntry) Username

func (e *EtcPasswdEntry) Username() string

Username function returns the username string for the entry

Jump to

Keyboard shortcuts

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