Help us shape the future of Go! Take the 2026 Go Developer Survey

sql

package
v1.99.0 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2026 License: GPL-3.0 Imports: 16 Imported by: 0

Documentation

Overview

Package sql implements SQL passdb/userdb for yarilo-auth. Supports SQLite, MySQL, PostgreSQL via a unified interface, with Dovecot-style customizable queries (password_query / user_query / iterate_query) and %u/%n/%d parameter substitution.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseSCRAMSha1Credentials added in v1.39.0

func ParseSCRAMSha1Credentials(stored string) (*sasl.ScramCredentials, bool)

ParseSCRAMSha1Credentials is the SHA-1 counterpart used by the passdb to satisfy LookupSCRAMSha1.

func ParseSCRAMSha256Credentials added in v1.39.0

func ParseSCRAMSha256Credentials(stored string) (*sasl.ScramCredentials, bool)

ParseSCRAMSha256Credentials extracts the SCRAM verifier from a stored password column carrying the `{SCRAM-SHA-256}` scheme. Returns (nil, false) when the value does not carry that scheme or the blob is malformed — callers use the falsy outcome to mean "this user does not have a SCRAM verifier".

Types

type Config added in v0.3.5

type Config struct {
	Driver            string // sqlite | mysql | postgres
	DSN               string
	PasswordQuery     string // optional; defaults to built-in yarilo_users schema
	UserQuery         string // optional; if set, userdb lookup uses this query
	IterateQuery      string // optional; for admin tooling (list users)
	DefaultPassScheme string // assumed scheme when stored password has no {SCHEME} prefix (default PLAIN)
	SkipSchema        bool   // do not auto-create yarilo_users
}

Config is the open-time configuration for a SQL passdb.

type Passdb

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

Passdb is an SQL-backed passdb (and optional userdb) entry.

func New

func New(c Config) (*Passdb, error)

New opens an SQL passdb.

func (*Passdb) Authenticate

func (p *Passdb) Authenticate(req *protocol.Request) (protocol.Result, error)

Authenticate verifies req.Username / req.Password against the SQL store and writes user fields directly into req.Fields when the lookup succeeds. Phase AUTH-2 PR 2 wire — drivers no longer allocate their own AuthResponse; the Chain owns the bag and the Result enum drives chain control flow.

Outcomes:

ResultNext       — row not found in this database; chain falls through
ResultTempFail   — backend / query error (the error return carries
                    the underlying cause for the server-side log)
ResultFail       — user found but disabled OR password mismatch
ResultOK         — verified; req.Fields populated with user / home / mail

Columns are matched by name, not position. Only "password" is required; "home", "mail", and "enabled" are optional. When "enabled" is absent the row is treated as active — callers may use a WHERE active=1 guard instead. The optional UserQuery runs after the password check to enrich home/mail.

func (*Passdb) Close

func (p *Passdb) Close() error

Close releases the database connection.

func (*Passdb) Iterate added in v0.3.5

func (p *Passdb) Iterate() ([]string, error)

Iterate runs the iterate_query and returns the list of usernames. Returns an error if iterate_query is not configured.

func (*Passdb) LookupSCRAMSha1 added in v1.39.0

func (p *Passdb) LookupSCRAMSha1(username string) (*sasl.ScramCredentials, error)

LookupSCRAMSha1 satisfies protocol.SCRAMSha1Lookup. SHA-1 counterpart of LookupSCRAMSha256 — uses the same password_query path; only the verifier-blob scheme prefix differs.

func (*Passdb) LookupSCRAMSha256 added in v1.39.0

func (p *Passdb) LookupSCRAMSha256(username string) (*sasl.ScramCredentials, error)

LookupSCRAMSha256 satisfies protocol.SCRAMSha256Lookup. The SQL passdb runs its configured password_query, recognises the `{SCRAM-SHA-256}` scheme prefix on the password column, and returns the parsed ScramCredentials so the SCRAM-SHA-256 SASL mechanism can drive challenge-response without ever seeing a plain password.

Returns (nil, nil) for any of: user unknown / user disabled / stored password not a SCRAM-SHA-256 verifier. The session-side SCRAM server treats nil as "fabricate a fake verifier" so the exchange completes with a uniform auth-failed outcome and an attacker cannot enumerate users.

func (*Passdb) LookupUser added in v0.3.5

func (p *Passdb) LookupUser(username string) (home, mailLoc string, err error)

LookupUser runs the optional user_query and returns the userdb fields. Returns sql.ErrNoRows when the user does not exist.

type Userdb added in v1.39.0

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

Userdb is the SQL-backed implementation of protocol.Userdb. It shares Config + DSN with the existing Passdb (operators typically have one DB serving both roles) but is exposed as a separate type so a deployment can chain heterogeneous backends — e.g. SQL passdb + LDAP userdb.

The default lookup query selects the columns shipped in the built-in yarilo_users schema (home, mail). Operators with richer schemas point Config.UserQuery at their own SELECT that returns the column names listed in [defaultUserdbColumns] — see the rowScanner below for the exact mapping from column name → UserInfo field. Any column the row carries that does not match a known typed field lands in UserInfo.Extra so forward-compatibility is preserved without requiring driver changes for every new field.

func NewUserdb added in v1.39.0

func NewUserdb(c Config) (*Userdb, error)

NewUserdb opens an SQL userdb. The Config is shared with Passdb's New so callers can construct both from one block; the userdb honours Config.UserQuery and Config.IterateQuery, defaulting to the same yarilo_users columns the Passdb assumes when the operator has not overridden them.

func (*Userdb) Close added in v1.39.0

func (u *Userdb) Close() error

Close releases the underlying database connection.

func (*Userdb) Iterate added in v1.39.0

func (u *Userdb) Iterate() ([]string, error)

Iterate implements protocol.UserdbIterator when an iterate_query is configured. Returns an error if no query was set so callers can distinguish "no enumeration support" from "empty result".

func (*Userdb) Lookup added in v1.39.0

func (u *Userdb) Lookup(username string) (*protocol.UserInfo, error)

Lookup implements protocol.Userdb. Returns (nil, nil) when the query returns no rows so a UserdbChain falls through to the next backend; returns (nil, err) on driver / scan / parse errors so the chain short-circuits with the operator-visible failure.

Jump to

Keyboard shortcuts

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