Files
Claude 9d5cdfa76e Signup: make the new account in each login backend
A signup made the account in the users file only. Now it makes the
account in each backend of [general] auth that is set up to make
accounts: the users file, SQL ([sql] insert_query), LDAP ([ldap] add_dn,
added as add_bind_dn, the password set by Password Modify or unicodePwd
for AD) and system accounts ([system] create_command, with a useradd
sample script). [signup] backends limits the list.

Before the account is made, each backend checks that the name is free.
If one backend fails, the accounts already made are taken back
(delete_query, entry delete, remove_command). With pending = yes, each
backend must lock the account, or dmodem-login does not start.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B82eRRfuh3h6uNuHtaugux
2026-11-24 16:56:35 +00:00

106 lines
2.9 KiB
Go

// Copyright (C) 2026 D-Modem Authors
// SPDX-License-Identifier: AGPL-3.0-only
package auth
import (
"context"
"fmt"
"strings"
"time"
"github.com/D-Modem/login/internal/config"
)
// 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.
type System struct {
service string
minUID int
allowRoot bool
groups []string
// the name service and PAM, replaced in tests
lookup func(string) (*Account, error)
inGroups func(*Account, []string) (bool, error)
pam func(service, user, password string) error
// new accounts (signups): create_command, the password on its input;
// remove_command takes one back
createCommand string
removeCommand string
commandTimeout time.Duration
}
// Account: a user of the name service
type Account struct {
name string
uid, gid int
}
// NewSystem: the [system] backend (PAM and the name service);
// an error when this dmodem-login is built without PAM
func NewSystem(s *config.Section) (Authenticator, error) {
var err error
a := &System{
service: s.Get("pam_service", "dmodem-login"),
groups: config.Fields(s.Get("groups", "")),
lookup: LookupNSS,
inGroups: InGroupsNSS,
pam: pamAuthenticate,
}
a.createCommand = s.Get("create_command", "")
a.removeCommand = s.Get("remove_command", "")
if a.commandTimeout, err = s.Duration("command_timeout", 30*time.Second); err != nil {
return nil, err
}
if a.minUID, err = s.Int("min_uid", 1000); err != nil {
return nil, err
}
if a.allowRoot, err = s.Bool("allow_root", false); err != nil {
return nil, err
}
if !havePAM {
return nil, fmt.Errorf("[general] auth = system: this dmodem-login is built without PAM (make with libpam's headers, or go build -tags pam)")
}
return a, nil
}
// Name: the backend's Name, for {auth} and the log
func (a *System) Name() string { return "system" }
// Authenticate: the account looked up, min_uid, root and groups
// checked, then the password by PAM
func (a *System) Authenticate(_ context.Context, user, password string) error {
acct, err := a.lookup(user)
if err != nil {
return err
}
if err := a.allowed(acct); err != nil {
return err
}
return a.pam(a.service, user, password)
}
// allowed: an account that may dial in. The others are not this
// backend's users: the next one is asked
func (a *System) allowed(acct *Account) error {
if acct.uid == 0 && !a.allowRoot {
return fmt.Errorf("%w: %s is root", ErrNoUser, acct.name)
}
if acct.uid != 0 && acct.uid < a.minUID {
return fmt.Errorf("%w: %s has uid %d < min_uid %d", ErrNoUser, acct.name, acct.uid, a.minUID)
}
if len(a.groups) == 0 {
return nil
}
in, err := a.inGroups(acct, a.groups)
if err != nil {
return err
}
if !in {
return fmt.Errorf("%w: %s is in none of %s", ErrNoUser, acct.name, strings.Join(a.groups, ", "))
}
return nil
}