auth

package
v0.0.0-...-1c49bda Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: AGPL-3.0 Imports: 29 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package auth checks a user's password with dmodem-login's backends (a users file, LDAP, SQL, the system's accounts by PAM), in turn, and changes it in the backend that holds the account.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoUser      = errors.New("no such user")
	ErrBadPassword = errors.New("wrong password")
)

ErrNoUser: the backend does not know the user (the next one is asked); ErrBadPassword: it knows the user, the password is wrong (no other backend is asked)

View Source
var ErrCannotRemove = errors.New("cannot remove accounts")

ErrCannotRemove: the backend cannot take back an account it made (the undo of a signup that failed in a later backend)

View Source
var ErrRefused = errors.New("new password refused")

ErrRefused: the backend did not take the new password (its policy)

View Source
var ErrTaken = errors.New("user name taken")

ErrTaken: the backend has an account of that name already

Functions

func Authenticate

func Authenticate(ctx context.Context, backends []Authenticator, user, password string, logf func(string, ...any)) (string, error)

Authenticate with each backend in turn: the name of the one that accepted the user, or why none did

func Burn

func Burn(password string)

Burn: the time a password check takes, for an unknown user

func CheckPassword

func CheckPassword(stored, password, scheme string) (bool, error)

CheckPassword against a stored password, in the scheme its prefix or scheme (auto: by prefix) names:

$2a$, $2b$, $2y$ ...    bcrypt
{SHA256}hex, {SHA512}hex (or base64), {SSHA256}/{SSHA512}base64(hash+salt)
{PLAIN}text, or text with scheme = plain

func CreateAccounts

func CreateAccounts(ctx context.Context, creators []AccountCreator, acct NewAccount, logf func(string, ...any)) ([]string, error)

CreateAccounts: acct made in each of creators, in turn. When one fails, the accounts already made are taken back (in the reverse sequence) and the error says which backend failed. The result is the names of the backends that have the account.

func InGroupsNSS

func InGroupsNSS(acct *Account, groups []string) (bool, error)

InGroupsNSS: whether the account is in any of the groups, by the name service (/etc/group, LDAP via NSS...)

func UserTaken

func UserTaken(ctx context.Context, checkers []UserChecker, user string) (string, error)

UserTaken: the first backend of checkers that has user (in any case where it can tell), or ""; an error when a backend cannot be asked

Types

type Account

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

Account: a user of the name service

func LookupNSS

func LookupNSS(name string) (*Account, error)

LookupNSS: the account from the name service (getpwnam with cgo)

type AccountCreator

type AccountCreator interface {
	UserChecker
	// CanCreate: whether this backend is set up to make accounts (sql:
	// insert_query; ldap: add_dn; system: create_command)
	CanCreate() bool
	// CanLock: whether a pending account can be made locked here
	CanLock() bool
	// CreateUser: the account made; ErrTaken when the name is there
	CreateUser(ctx context.Context, acct NewAccount) error
	// RemoveUser: the account that CreateUser made taken back, or
	// ErrCannotRemove
	RemoveUser(ctx context.Context, user string) error
}

AccountCreator: a backend that can make a new account (a signup)

type Authenticator

type Authenticator interface {
	Name() string
	Authenticate(ctx context.Context, user, password string) error
}

func NewFile

func NewFile(s *config.Section) (Authenticator, error)

NewFile: the [file] backend, its users file checked to exist

func NewLDAP

func NewLDAP(s *config.Section) (Authenticator, error)

NewLDAP: the ldap backend from its section, checked

func NewSQL

func NewSQL(s *config.Section) (Authenticator, error)

NewSQL: the sql backend, its database opened (not yet connected); args names the query's parameters (user, password)

func NewSystem

func NewSystem(s *config.Section) (Authenticator, error)

NewSystem: the [system] backend (PAM and the name service); an error when this dmodem-login is built without PAM

type DirUser

type DirUser struct {
	Name     string
	FullName string
	Posix    bool // a system account: UID, GID, Home and Shell are set
	UID, GID int
	Home     string
	Shell    string
}

DirUser is what the LDAP provider ([ldapd]) shows of a user. It never has the password.

type File

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

File: a text file of user:password lines (the password hashed as CheckPassword reads it, ! before it for a locked account; # comments), read at each login so that edits take effect at once

func FileAt

func FileAt(path, scheme string) *File

FileAt: a users file backend for path (a signup's [signup] file, which can differ from the [file] path)

func (*File) Authenticate

func (a *File) Authenticate(_ context.Context, user, password string) error

Authenticate: the user's line in the users file and its hash checked; a locked entry (!) is refused

func (*File) CanChangePassword

func (a *File) CanChangePassword() bool

func (*File) CanCreate

func (a *File) CanCreate() bool

CanCreate: always (a line at the end of the users file)

func (*File) CanLock

func (a *File) CanLock() bool

CanLock: always (user:!hash)

func (*File) ChangePassword

func (a *File) ChangePassword(_ context.Context, _ Messenger, user, old, new string) error

ChangePassword in the users file: the user's line rewritten in place (other lines as they are) under the lock signups take, the password as bcrypt (as text with hash = plain), a note after it kept

func (*File) CreateUser

func (a *File) CreateUser(_ context.Context, acct NewAccount) error

CreateUser: user:bcrypt:note at the end of the users file (user:!bcrypt for a pending account), under the lock that password changes take. The name is looked at again under the lock: two signups at one time.

func (*File) HasUser

func (a *File) HasUser(_ context.Context, user string) (bool, bool, error)

HasUser: whether the users file has user, in any case

func (*File) ListUsers

func (a *File) ListUsers() ([]DirUser, error)

ListUsers: the users of the users file, not the locked ones

func (*File) LookupUser

func (a *File) LookupUser(name string) (DirUser, error)

LookupUser: the user's line in the users file; ErrNoUser when it is not there or locked

func (*File) Name

func (a *File) Name() string

Name: the backend's Name, for {auth} and the log

func (*File) RemoveUser

func (a *File) RemoveUser(_ context.Context, user string) error

RemoveUser: the line of user (this name exactly) taken out of the users file, under its lock; the other lines stay as they are

func (*File) Scheme

func (a *File) Scheme() string

Scheme: hash, the scheme of the stored passwords (auto: by prefix)

type LDAP

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

LDAP: a bind as the user, to bind_dn with {user} in it, or to the DN that search_filter finds under search_base (searched as search_bind_dn, anonymously if none)

func (*LDAP) Authenticate

func (a *LDAP) Authenticate(ctx context.Context, user, password string) error

Authenticate: a bind as the user (found by search, or by bind_dn)

func (*LDAP) CanChangePassword

func (a *LDAP) CanChangePassword() bool

CanChangePassword: always (exop or ad)

func (*LDAP) CanCreate

func (a *LDAP) CanCreate() bool

CanCreate: whether add_dn is set

func (*LDAP) CanLock

func (a *LDAP) CanLock() bool

CanLock: whether add_pending_attributes is set (the attributes that lock an entry differ for each server: pwdAccountLockedTime for OpenLDAP, nsAccountLock for 389 DS, userAccountControl for AD)

func (*LDAP) ChangePassword

func (a *LDAP) ChangePassword(_ context.Context, _ Messenger, user, old, new string) error

ChangePassword bound as the user, so that the server applies its own password policy: by the Password Modify extended operation (RFC 3062), or for Active Directory (password_change = ad, which does not have it) by replacing unicodePwd, the old value deleted and the new one added (which AD takes only over TLS: ldaps:// or starttls)

func (*LDAP) CreateUser

func (a *LDAP) CreateUser(_ context.Context, acct NewAccount) error

CreateUser: the entry added as add_bind_dn, then (not for AD, where the entry has unicodePwd) its password set by the Password Modify extended operation, so that the server makes the hash by its own policy. If the password cannot be set, the entry is deleted again.

func (*LDAP) HasGroups

func (a *LDAP) HasGroups() bool

HasGroups: whether groups are looked up here (group_base is set)

func (*LDAP) HasUser

func (a *LDAP) HasUser(_ context.Context, user string) (bool, bool, error)

HasUser: with add_dn, whether the entry at add_dn or an entry that search_filter finds is there, searched as add_bind_dn. Without add_dn, the search of a login (search_base) only; with bind_dn and no search, known is false.

func (*LDAP) MemberOf

func (a *LDAP) MemberOf(user, password, group string) (bool, error)

MemberOf: whether user is in group, an entry under group_base that group_filter ({group}, the user's {dn} and {user}) finds; searched as search_bind_dn, else as the user (with their password), else anonymously. ErrNoUser without group_base: LDAP has no groups here.

func (*LDAP) Name

func (a *LDAP) Name() string

Name: the backend's Name, for {auth} and the log

func (*LDAP) RemoveUser

func (a *LDAP) RemoveUser(_ context.Context, user string) error

RemoveUser: the entry at add_dn deleted, as add_bind_dn

type Lister

type Lister interface {
	ListUsers() ([]DirUser, error)
}

Lister is a backend that can list its users (the users file).

type Looker

type Looker interface {
	LookupUser(name string) (DirUser, error)
}

Looker is a backend that can find one user by name without the password (the users file, the system's accounts).

type Messenger

type Messenger interface {
	Write(s string) error
}

Messenger: the caller's line, for what a backend has to tell them

type NewAccount

type NewAccount struct {
	User     string
	Password string // as the caller typed it; each backend hashes it its own way
	Pending  bool   // locked until the sysop unlocks it
	Note     string // signup DATE CALLERID, where the backend keeps one
}

NewAccount: what a signup gives each backend that makes the account

type PasswordChanger

type PasswordChanger interface {
	ChangePassword(ctx context.Context, t Messenger, user, old, new string) error
	// CanChangePassword: whether this backend is set up to (sql: an
	// update_query)
	CanChangePassword() bool
}

PasswordChanger: a backend that can change a user's password; old has been checked already. t is the caller's line, for a backend (PAM) that has something to tell them.

type SQL

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

SQL: query, with the user (and the password, for args = user password) as its arguments, returns the stored password of the user (checked as hash says), or with hash = none any row for a valid login

func (*SQL) Authenticate

func (a *SQL) Authenticate(ctx context.Context, user, password string) error

Authenticate: the query's row for the user, its first column the stored hash checked against the password (hash = none: a row is enough)

func (*SQL) CanChangePassword

func (a *SQL) CanChangePassword() bool

func (*SQL) CanCreate

func (a *SQL) CanCreate() bool

CanCreate: whether insert_query is set

func (*SQL) CanLock

func (a *SQL) CanLock() bool

CanLock: whether insert_args gives the query pending (the query stores the lock, and query or the database refuses locked rows)

func (*SQL) ChangePassword

func (a *SQL) ChangePassword(ctx context.Context, _ Messenger, user, old, new string) error

ChangePassword in the database: update_query with update_args, the password as update_hash says

func (*SQL) CreateUser

func (a *SQL) CreateUser(ctx context.Context, acct NewAccount) error

CreateUser: insert_query with insert_args (user, password, pending as 1 or 0, note), the password as insert_hash says. An error of the database (for example, a unique key) is not ErrTaken: the database does not say why.

func (*SQL) HasUser

func (a *SQL) HasUser(ctx context.Context, user string) (bool, bool, error)

HasUser: exists_query with the user (a row: the name is taken); else query, when its only argument is the user and it returns the stored password. Without these, known is false: insert_query (a unique key) refuses a name that is there.

func (*SQL) Name

func (a *SQL) Name() string

Name: the backend's Name, for {auth} and the log

func (*SQL) RemoveUser

func (a *SQL) RemoveUser(ctx context.Context, user string) error

RemoveUser: delete_query with the user, or ErrCannotRemove without one

type System

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

System: the accounts of the system, by PAM: the auth and account stacks of pam_service, in a build with -tags pam. Only accounts with a uid of at least min_uid (not root, unless allow_root), in one of groups if it is set, may log in.

func (*System) Authenticate

func (a *System) Authenticate(_ context.Context, user, password string) error

Authenticate: the account looked up, min_uid, root and groups checked, then the password by PAM

func (*System) CanChangePassword

func (a *System) CanChangePassword() bool

func (*System) CanCreate

func (a *System) CanCreate() bool

CanCreate: whether create_command is set

func (*System) CanLock

func (a *System) CanLock() bool

CanLock: always (create_command gets {pending} and locks the account)

func (*System) ChangePassword

func (a *System) ChangePassword(_ context.Context, t Messenger, user, old, new string) error

ChangePassword by PAM: the service's password stack

func (*System) CreateUser

func (a *System) CreateUser(ctx context.Context, acct NewAccount) error

CreateUser: create_command by /bin/sh, {user} and {pending} (yes or no) in it, the password and a line end on its input

func (*System) HasUser

func (a *System) HasUser(_ context.Context, user string) (bool, bool, error)

HasUser: whether the name service has user (any account, also one below min_uid: a new user must not get the name of a system account)

func (*System) LookupUser

func (a *System) LookupUser(name string) (DirUser, error)

LookupUser: the system account, when it is one that may log in (min_uid, root, groups)

func (*System) Name

func (a *System) Name() string

Name: the backend's Name, for {auth} and the log

func (*System) RemoveUser

func (a *System) RemoveUser(ctx context.Context, user string) error

RemoveUser: remove_command with {user}, or ErrCannotRemove without one

type UserChecker

type UserChecker interface {
	Name() string
	// HasUser: whether the backend has user; known is false when this
	// backend, as configured, cannot tell
	HasUser(ctx context.Context, user string) (has, known bool, err error)
}

UserChecker: a backend that can tell whether it has an account of a name, without a password (a signup's check of the new name)

Jump to

Keyboard shortcuts

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